line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
MODULE = CryptX PACKAGE = Crypt::AuthEnc::ChaCha20Poly1305 |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Crypt::AuthEnc::ChaCha20Poly1305 |
6
|
|
|
|
|
|
|
new(Class, SV * key, SV * nonce = NULL) |
7
|
|
|
|
|
|
|
CODE: |
8
|
|
|
|
|
|
|
{ |
9
|
|
|
|
|
|
|
int rv; |
10
|
3
|
|
|
|
|
|
STRLEN iv_len=0, k_len=0; |
11
|
3
|
|
|
|
|
|
unsigned char *iv=NULL, *k=NULL; |
12
|
|
|
|
|
|
|
|
13
|
3
|
50
|
|
|
|
|
if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); |
14
|
3
|
50
|
|
|
|
|
k = (unsigned char *) SvPVbyte(key, k_len); |
15
|
3
|
50
|
|
|
|
|
if (nonce) { |
16
|
3
|
50
|
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
17
|
3
|
50
|
|
|
|
|
iv = (unsigned char *) SvPVbyte(nonce, iv_len); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
3
|
|
|
|
|
|
Newz(0, RETVAL, 1, chacha20poly1305_state); |
21
|
3
|
50
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
22
|
|
|
|
|
|
|
|
23
|
3
|
|
|
|
|
|
rv = chacha20poly1305_init(RETVAL, k, (unsigned long)k_len); |
24
|
3
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
25
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
26
|
0
|
|
|
|
|
|
croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv)); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
3
|
50
|
|
|
|
|
if (iv && iv_len > 0) { |
|
|
50
|
|
|
|
|
|
30
|
3
|
|
|
|
|
|
rv = chacha20poly1305_setiv(RETVAL, iv, (unsigned long)iv_len); |
31
|
3
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
32
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
33
|
0
|
|
|
|
|
|
croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv)); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
OUTPUT: |
38
|
|
|
|
|
|
|
RETVAL |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
void |
41
|
|
|
|
|
|
|
DESTROY(Crypt::AuthEnc::ChaCha20Poly1305 self) |
42
|
|
|
|
|
|
|
CODE: |
43
|
3
|
|
|
|
|
|
Safefree(self); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Crypt::AuthEnc::ChaCha20Poly1305 |
46
|
|
|
|
|
|
|
clone(Crypt::AuthEnc::ChaCha20Poly1305 self) |
47
|
|
|
|
|
|
|
CODE: |
48
|
0
|
|
|
|
|
|
Newz(0, RETVAL, 1, chacha20poly1305_state); |
49
|
0
|
0
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
50
|
0
|
|
|
|
|
|
Copy(self, RETVAL, 1, chacha20poly1305_state); |
51
|
|
|
|
|
|
|
OUTPUT: |
52
|
|
|
|
|
|
|
RETVAL |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
void |
55
|
|
|
|
|
|
|
set_iv(Crypt::AuthEnc::ChaCha20Poly1305 self, SV * nonce) |
56
|
|
|
|
|
|
|
PPCODE: |
57
|
|
|
|
|
|
|
{ |
58
|
|
|
|
|
|
|
int rv; |
59
|
0
|
|
|
|
|
|
STRLEN iv_len=0; |
60
|
0
|
|
|
|
|
|
unsigned char *iv=NULL; |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
63
|
0
|
0
|
|
|
|
|
iv = (unsigned char *) SvPVbyte(nonce, iv_len); |
64
|
0
|
|
|
|
|
|
rv = chacha20poly1305_setiv(self, iv, (unsigned long)iv_len); |
65
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv)); |
66
|
0
|
0
|
|
|
|
|
XPUSHs(ST(0)); /* return self */; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
void |
70
|
|
|
|
|
|
|
set_iv_rfc7905(Crypt::AuthEnc::ChaCha20Poly1305 self, SV * nonce, UV seqnum) |
71
|
|
|
|
|
|
|
PPCODE: |
72
|
|
|
|
|
|
|
{ |
73
|
|
|
|
|
|
|
int rv; |
74
|
0
|
|
|
|
|
|
STRLEN iv_len=0; |
75
|
0
|
|
|
|
|
|
unsigned char *iv=NULL; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
78
|
0
|
0
|
|
|
|
|
iv = (unsigned char *) SvPVbyte(nonce, iv_len); |
79
|
0
|
|
|
|
|
|
rv = chacha20poly1305_setiv_rfc7905(self, iv, (unsigned long)iv_len, (ulong64)seqnum); |
80
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv_rfc7905 failed: %s", error_to_string(rv)); |
81
|
0
|
0
|
|
|
|
|
XPUSHs(ST(0)); /* return self */ |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
void |
85
|
|
|
|
|
|
|
adata_add(Crypt::AuthEnc::ChaCha20Poly1305 self, SV * data) |
86
|
|
|
|
|
|
|
PPCODE: |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
int rv; |
89
|
|
|
|
|
|
|
STRLEN in_data_len; |
90
|
|
|
|
|
|
|
unsigned char *in_data; |
91
|
|
|
|
|
|
|
|
92
|
3
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
93
|
3
|
|
|
|
|
|
rv = chacha20poly1305_add_aad(self, in_data, (unsigned long)in_data_len); |
94
|
3
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_add_aad failed: %s", error_to_string(rv)); |
95
|
3
|
50
|
|
|
|
|
XPUSHs(ST(0)); /* return self */ |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
SV * |
99
|
|
|
|
|
|
|
decrypt_add(Crypt::AuthEnc::ChaCha20Poly1305 self, SV * data) |
100
|
|
|
|
|
|
|
CODE: |
101
|
|
|
|
|
|
|
{ |
102
|
|
|
|
|
|
|
int rv; |
103
|
|
|
|
|
|
|
STRLEN in_data_len; |
104
|
|
|
|
|
|
|
unsigned char *in_data, *out_data; |
105
|
|
|
|
|
|
|
|
106
|
21
|
100
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
107
|
21
|
50
|
|
|
|
|
if (in_data_len == 0) { |
108
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
else { |
111
|
21
|
|
|
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
112
|
21
|
|
|
|
|
|
SvPOK_only(RETVAL); |
113
|
21
|
|
|
|
|
|
SvCUR_set(RETVAL, in_data_len); |
114
|
21
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
115
|
21
|
|
|
|
|
|
rv = chacha20poly1305_decrypt(self, in_data, (unsigned long)in_data_len, out_data); |
116
|
21
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
117
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
118
|
0
|
|
|
|
|
|
croak("FATAL: chacha20poly1305_decrypt failed: %s", error_to_string(rv)); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
OUTPUT: |
123
|
|
|
|
|
|
|
RETVAL |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
SV * |
126
|
|
|
|
|
|
|
encrypt_add(Crypt::AuthEnc::ChaCha20Poly1305 self, SV * data) |
127
|
|
|
|
|
|
|
CODE: |
128
|
|
|
|
|
|
|
{ |
129
|
|
|
|
|
|
|
int rv; |
130
|
|
|
|
|
|
|
STRLEN in_data_len; |
131
|
|
|
|
|
|
|
unsigned char *in_data, *out_data; |
132
|
|
|
|
|
|
|
|
133
|
2
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
134
|
2
|
50
|
|
|
|
|
if (in_data_len == 0) { |
135
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else { |
138
|
2
|
|
|
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
139
|
2
|
|
|
|
|
|
SvPOK_only(RETVAL); |
140
|
2
|
|
|
|
|
|
SvCUR_set(RETVAL, in_data_len); |
141
|
2
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
142
|
2
|
|
|
|
|
|
rv = chacha20poly1305_encrypt(self, in_data, (unsigned long)in_data_len, out_data); |
143
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
144
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
145
|
0
|
|
|
|
|
|
croak("FATAL: chacha20poly1305_encrypt failed: %s", error_to_string(rv)); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
OUTPUT: |
150
|
|
|
|
|
|
|
RETVAL |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
void |
153
|
|
|
|
|
|
|
encrypt_done(Crypt::AuthEnc::ChaCha20Poly1305 self) |
154
|
|
|
|
|
|
|
PPCODE: |
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
int rv; |
157
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
158
|
1
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
|
|
|
rv = chacha20poly1305_done(self, tag, &tag_len); |
161
|
1
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_done failed: %s", error_to_string(rv)); |
162
|
1
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
void |
166
|
|
|
|
|
|
|
decrypt_done(Crypt::AuthEnc::ChaCha20Poly1305 self, ...) |
167
|
|
|
|
|
|
|
PPCODE: |
168
|
|
|
|
|
|
|
{ |
169
|
|
|
|
|
|
|
int rv; |
170
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
171
|
2
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
172
|
|
|
|
|
|
|
STRLEN expected_tag_len; |
173
|
|
|
|
|
|
|
unsigned char *expected_tag; |
174
|
|
|
|
|
|
|
|
175
|
2
|
|
|
|
|
|
rv = chacha20poly1305_done(self, tag, &tag_len); |
176
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_done failed: %s", error_to_string(rv)); |
177
|
2
|
50
|
|
|
|
|
if (items == 1) { |
178
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
else { |
181
|
0
|
0
|
|
|
|
|
if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
182
|
0
|
0
|
|
|
|
|
expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); |
183
|
0
|
0
|
|
|
|
|
if (expected_tag_len!=tag_len) { |
184
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
185
|
|
|
|
|
|
|
} |
186
|
0
|
0
|
|
|
|
|
else if (memNE(expected_tag, tag, tag_len)) { |
187
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
else { |
190
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(1))); /* true */ |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
void |
196
|
|
|
|
|
|
|
chacha20poly1305_encrypt_authenticate(SV *key, SV *nonce, SV *header, SV *plaintext) |
197
|
|
|
|
|
|
|
PPCODE: |
198
|
|
|
|
|
|
|
{ |
199
|
2
|
|
|
|
|
|
STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0; |
200
|
2
|
|
|
|
|
|
unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL; |
201
|
|
|
|
|
|
|
int rv; |
202
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
203
|
2
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
204
|
|
|
|
|
|
|
SV *output; |
205
|
|
|
|
|
|
|
|
206
|
2
|
50
|
|
|
|
|
if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len); |
|
|
50
|
|
|
|
|
|
207
|
2
|
50
|
|
|
|
|
if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len); |
|
|
50
|
|
|
|
|
|
208
|
2
|
50
|
|
|
|
|
if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len); |
|
|
50
|
|
|
|
|
|
209
|
2
|
50
|
|
|
|
|
if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len); |
|
|
50
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
2
|
50
|
|
|
|
|
output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */ |
212
|
2
|
|
|
|
|
|
SvPOK_only(output); |
213
|
2
|
|
|
|
|
|
SvCUR_set(output, pt_len); |
214
|
|
|
|
|
|
|
|
215
|
2
|
|
|
|
|
|
rv = chacha20poly1305_memory(k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
216
|
2
|
|
|
|
|
|
pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len, |
217
|
|
|
|
|
|
|
CHACHA20POLY1305_ENCRYPT); |
218
|
|
|
|
|
|
|
|
219
|
2
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
220
|
0
|
|
|
|
|
|
SvREFCNT_dec(output); |
221
|
0
|
|
|
|
|
|
croak("FATAL: ccm_memory failed: %s", error_to_string(rv)); |
222
|
|
|
|
|
|
|
} |
223
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(output)); |
224
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
void |
228
|
|
|
|
|
|
|
chacha20poly1305_decrypt_verify(SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv) |
229
|
|
|
|
|
|
|
PPCODE: |
230
|
|
|
|
|
|
|
{ |
231
|
4
|
|
|
|
|
|
STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0; |
232
|
4
|
|
|
|
|
|
unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL; |
233
|
|
|
|
|
|
|
int rv; |
234
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
235
|
|
|
|
|
|
|
unsigned long tag_len; |
236
|
|
|
|
|
|
|
SV *output; |
237
|
|
|
|
|
|
|
|
238
|
4
|
50
|
|
|
|
|
if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len); |
|
|
50
|
|
|
|
|
|
239
|
4
|
50
|
|
|
|
|
if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len); |
|
|
50
|
|
|
|
|
|
240
|
4
|
50
|
|
|
|
|
if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len); |
|
|
50
|
|
|
|
|
|
241
|
4
|
50
|
|
|
|
|
if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len); |
|
|
50
|
|
|
|
|
|
242
|
4
|
50
|
|
|
|
|
if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len); |
|
|
50
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
244
|
4
|
50
|
|
|
|
|
output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */ |
245
|
4
|
|
|
|
|
|
SvPOK_only(output); |
246
|
4
|
|
|
|
|
|
SvCUR_set(output, ct_len); |
247
|
4
|
|
|
|
|
|
tag_len = (unsigned long)t_len; |
248
|
4
|
|
|
|
|
|
Copy(t, tag, t_len, unsigned char); |
249
|
|
|
|
|
|
|
|
250
|
4
|
|
|
|
|
|
rv = chacha20poly1305_memory(k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
251
|
4
|
|
|
|
|
|
ct, (unsigned long)ct_len, (unsigned char *)SvPVX(output), tag, &tag_len, |
252
|
|
|
|
|
|
|
CHACHA20POLY1305_DECRYPT); |
253
|
|
|
|
|
|
|
|
254
|
4
|
100
|
|
|
|
|
if (rv != CRYPT_OK) { |
255
|
2
|
|
|
|
|
|
SvREFCNT_dec(output); |
256
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
else { |
259
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(output)); |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
} |