line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
MODULE = CryptX PACKAGE = Crypt::Stream::ChaCha |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Crypt::Stream::ChaCha |
6
|
|
|
|
|
|
|
new(Class, SV * key, SV * nonce, UV counter = 0, int rounds = 20) |
7
|
|
|
|
|
|
|
CODE: |
8
|
|
|
|
|
|
|
{ |
9
|
|
|
|
|
|
|
int rv; |
10
|
2
|
|
|
|
|
|
STRLEN iv_len=0, k_len=0; |
11
|
2
|
|
|
|
|
|
unsigned char *iv=NULL, *k=NULL; |
12
|
|
|
|
|
|
|
|
13
|
2
|
50
|
|
|
|
|
if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); |
14
|
2
|
50
|
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
15
|
2
|
50
|
|
|
|
|
k = (unsigned char *) SvPVbyte(key, k_len); |
16
|
2
|
50
|
|
|
|
|
iv = (unsigned char *) SvPVbyte(nonce, iv_len); |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
|
Newz(0, RETVAL, 1, chacha_state); |
19
|
2
|
50
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
20
|
|
|
|
|
|
|
|
21
|
2
|
|
|
|
|
|
rv = chacha_setup(RETVAL, k, (unsigned long)k_len, rounds); |
22
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
23
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
24
|
0
|
|
|
|
|
|
croak("FATAL: chacha_setup failed: %s", error_to_string(rv)); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
2
|
50
|
|
|
|
|
if (iv_len == 12) { |
28
|
2
|
|
|
|
|
|
rv = chacha_ivctr32(RETVAL, iv, (unsigned long)iv_len, (ulong32)counter); |
29
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
30
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
31
|
0
|
|
|
|
|
|
croak("FATAL: chacha_ivctr32 failed: %s", error_to_string(rv)); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
0
|
0
|
|
|
|
|
else if (iv_len == 8) { |
35
|
0
|
|
|
|
|
|
rv = chacha_ivctr64(RETVAL, iv, (unsigned long)iv_len, (ulong64)counter); |
36
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) { |
37
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
38
|
0
|
|
|
|
|
|
croak("FATAL: chacha_ivctr64 failed: %s", error_to_string(rv)); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
43
|
0
|
|
|
|
|
|
croak("FATAL: chacha IV length must be 8 or 12 bytes"); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
OUTPUT: |
47
|
|
|
|
|
|
|
RETVAL |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
void |
50
|
|
|
|
|
|
|
DESTROY(Crypt::Stream::ChaCha self) |
51
|
|
|
|
|
|
|
CODE: |
52
|
2
|
|
|
|
|
|
chacha_done(self); |
53
|
2
|
|
|
|
|
|
Safefree(self); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Crypt::Stream::ChaCha |
56
|
|
|
|
|
|
|
clone(Crypt::Stream::ChaCha self) |
57
|
|
|
|
|
|
|
CODE: |
58
|
0
|
|
|
|
|
|
Newz(0, RETVAL, 1, chacha_state); |
59
|
0
|
0
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
60
|
0
|
|
|
|
|
|
Copy(self, RETVAL, 1, chacha_state); |
61
|
|
|
|
|
|
|
OUTPUT: |
62
|
|
|
|
|
|
|
RETVAL |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
SV * |
65
|
|
|
|
|
|
|
keystream(Crypt::Stream::ChaCha self, STRLEN out_len) |
66
|
|
|
|
|
|
|
CODE: |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
int rv; |
69
|
|
|
|
|
|
|
unsigned char *out_data; |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
if (out_len == 0) { |
72
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
0
|
|
|
|
|
|
RETVAL = NEWSV(0, out_len); /* avoid zero! */ |
76
|
0
|
|
|
|
|
|
SvPOK_only(RETVAL); |
77
|
0
|
|
|
|
|
|
SvCUR_set(RETVAL, out_len); |
78
|
0
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
79
|
0
|
|
|
|
|
|
rv = chacha_keystream(self, out_data, (unsigned long)out_len); |
80
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) { |
81
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
82
|
0
|
|
|
|
|
|
croak("FATAL: chacha_keystream failed: %s", error_to_string(rv)); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
OUTPUT: |
87
|
|
|
|
|
|
|
RETVAL |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
SV * |
90
|
|
|
|
|
|
|
crypt(Crypt::Stream::ChaCha self, SV * data) |
91
|
|
|
|
|
|
|
CODE: |
92
|
|
|
|
|
|
|
{ |
93
|
|
|
|
|
|
|
int rv; |
94
|
|
|
|
|
|
|
STRLEN in_data_len; |
95
|
|
|
|
|
|
|
unsigned char *in_data, *out_data; |
96
|
|
|
|
|
|
|
|
97
|
2
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
98
|
2
|
50
|
|
|
|
|
if (in_data_len == 0) { |
99
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
2
|
|
|
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
103
|
2
|
|
|
|
|
|
SvPOK_only(RETVAL); |
104
|
2
|
|
|
|
|
|
SvCUR_set(RETVAL, in_data_len); |
105
|
2
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
106
|
2
|
|
|
|
|
|
rv = chacha_crypt(self, in_data, (unsigned long)in_data_len, out_data); |
107
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
108
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
109
|
0
|
|
|
|
|
|
croak("FATAL: chacha_crypt failed: %s", error_to_string(rv)); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
OUTPUT: |
114
|
|
|
|
|
|
|
RETVAL |