xs/stream.xs

filter: Statementstmt 70.3% 258 / 367 Branchbran 34.7% 66 / 190 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 58.1% 324 / 557 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/secretstream.xs ↑ Summary xs/kx.xs »
1 =for documentation
2  
3 stream is provided for completeness, but should not be used without very good
4 understanding. it is not authenticated. better options are secretbox or box.
5  
6 salsa208 not implemented (deprecated in libsodium)
7  
8 libsodium does not provide interface to set internal counter when using stream
9 function (only for stream_*xor_ic). always starts at 0 and is incremented as
10 needed internally for output size.
11  
12 =cut
13  
14 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::stream
15  
16 void _define_constants()
17 PREINIT:
18 1 HV *stash = gv_stashpv("Crypt::Sodium::XS::stream", 0);
19  
20 PPCODE:
21 1 newCONSTSUB(stash, "stream_KEYBYTES", newSVuv(crypto_stream_KEYBYTES));
22 1 newCONSTSUB(stash, "stream_chacha20_KEYBYTES",
23 newSVuv(crypto_stream_chacha20_KEYBYTES));
24 1 newCONSTSUB(stash, "stream_chacha20_ietf_KEYBYTES",
25 newSVuv(crypto_stream_chacha20_ietf_KEYBYTES));
26 1 newCONSTSUB(stash, "stream_salsa20_KEYBYTES",
27 newSVuv(crypto_stream_salsa20_KEYBYTES));
28 1 newCONSTSUB(stash, "stream_salsa2012_KEYBYTES",
29 newSVuv(crypto_stream_salsa2012_KEYBYTES));
30 1 newCONSTSUB(stash, "stream_xchacha20_KEYBYTES",
31 newSVuv(crypto_stream_xchacha20_KEYBYTES));
32 1 newCONSTSUB(stash, "stream_xsalsa20_KEYBYTES",
33 newSVuv(crypto_stream_xsalsa20_KEYBYTES));
34 1 newCONSTSUB(stash, "stream_NONCEBYTES", newSVuv(crypto_stream_NONCEBYTES));
35 1 newCONSTSUB(stash, "stream_chacha20_NONCEBYTES",
36 newSVuv(crypto_stream_chacha20_NONCEBYTES));
37 1 newCONSTSUB(stash, "stream_chacha20_ietf_NONCEBYTES",
38 newSVuv(crypto_stream_chacha20_ietf_NONCEBYTES));
39 1 newCONSTSUB(stash, "stream_salsa20_NONCEBYTES",
40 newSVuv(crypto_stream_salsa20_NONCEBYTES));
41 1 newCONSTSUB(stash, "stream_salsa2012_NONCEBYTES",
42 newSVuv(crypto_stream_salsa2012_NONCEBYTES));
43 1 newCONSTSUB(stash, "stream_xchacha20_NONCEBYTES",
44 newSVuv(crypto_stream_xchacha20_NONCEBYTES));
45 1 newCONSTSUB(stash, "stream_xsalsa20_NONCEBYTES",
46 newSVuv(crypto_stream_xsalsa20_NONCEBYTES));
47 1 newCONSTSUB(stash, "stream_MESSAGEBYTES_MAX",
48 newSVuv(crypto_stream_MESSAGEBYTES_MAX));
49 1 newCONSTSUB(stash, "stream_chacha20_MESSAGEBYTES_MAX",
50 newSVuv(crypto_stream_chacha20_MESSAGEBYTES_MAX));
51 1 newCONSTSUB(stash, "stream_chacha20_ietf_MESSAGEBYTES_MAX",
52 newSVuv(crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX));
53 1 newCONSTSUB(stash, "stream_salsa20_MESSAGEBYTES_MAX",
54 newSVuv(crypto_stream_salsa20_MESSAGEBYTES_MAX));
55 1 newCONSTSUB(stash, "stream_salsa2012_MESSAGEBYTES_MAX",
56 newSVuv(crypto_stream_salsa2012_MESSAGEBYTES_MAX));
57 1 newCONSTSUB(stash, "stream_xchacha20_MESSAGEBYTES_MAX",
58 newSVuv(crypto_stream_xchacha20_MESSAGEBYTES_MAX));
59 1 newCONSTSUB(stash, "stream_xsalsa20_MESSAGEBYTES_MAX",
60 newSVuv(crypto_stream_xsalsa20_MESSAGEBYTES_MAX));
61 1 newCONSTSUB(stash, "stream_PRIMITIVE", newSVpvs(crypto_stream_PRIMITIVE));
62  
63 SV * stream(STRLEN out_len, SV * nonce, SV * key)
64  
65 ALIAS:
66 stream_chacha20 = 1
67 stream_chacha20_ietf = 2
68 stream_salsa20 = 3
69 stream_salsa2012 = 4
70 stream_xchacha20 = 5
71 stream_xsalsa20 = 6
72  
73 PREINIT:
74 7 protmem *key_pm = NULL;
75 unsigned char *nonce_buf, *key_buf, *out_buf;
76 STRLEN nonce_len, key_len, nonce_req_len, key_req_len;
77 int (*func)(unsigned char *, unsigned long long,
78 const unsigned char *, const unsigned char *);
79  
80 CODE:
81 7 switch(ix) {
82 1 case 1:
83 1 nonce_req_len = crypto_stream_chacha20_NONCEBYTES;
84 1 key_req_len = crypto_stream_chacha20_KEYBYTES;
85 1 func = crypto_stream_chacha20;
86 1 break;
87 1 case 2:
88 1 nonce_req_len = crypto_stream_chacha20_ietf_NONCEBYTES;
89 1 key_req_len = crypto_stream_chacha20_ietf_KEYBYTES;
90 1 func = crypto_stream_chacha20_ietf;
91 1 break;
92 1 case 3:
93 1 nonce_req_len = crypto_stream_salsa20_NONCEBYTES;
94 1 key_req_len = crypto_stream_salsa20_KEYBYTES;
95 1 func = crypto_stream_salsa20;
96 1 break;
97 1 case 4:
98 1 nonce_req_len = crypto_stream_salsa2012_NONCEBYTES;
99 1 key_req_len = crypto_stream_salsa2012_KEYBYTES;
100 1 func = crypto_stream_salsa2012;
101 1 break;
102 1 case 5:
103 1 nonce_req_len = crypto_stream_xchacha20_NONCEBYTES;
104 1 key_req_len = crypto_stream_xchacha20_KEYBYTES;
105 1 func = crypto_stream_xchacha20;
106 1 break;
107 1 case 6:
108 1 nonce_req_len = crypto_stream_xsalsa20_NONCEBYTES;
109 1 key_req_len = crypto_stream_xsalsa20_KEYBYTES;
110 1 func = crypto_stream_xsalsa20;
111 1 break;
112 1 default:
113 1 nonce_req_len = crypto_stream_NONCEBYTES;
114 1 key_req_len = crypto_stream_KEYBYTES;
115 1 func = crypto_stream;
116 }
117  
118 7 nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len);
119 7 ▶ if (nonce_len != nonce_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (nonce_len != nonce_req_len) 0 7 7
120 0 croak("stream: Invalid nonce length %lu", nonce_len);
121  
122 7 ▶ if (sv_derived_from(key, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(key, MEMVAULT_CLASS)) { 7 0 7
123 7 key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS);
124 7 key_buf = key_pm->pm_ptr;
125 7 key_len = key_pm->size;
126 }
127 else
128 0 key_buf = (unsigned char *)SvPVbyte(key, key_len);
129 7 ▶ if (key_len != key_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != key_req_len) 0 7 7
130 0 croak("stream: Invalid key length %lu", key_len);
131  
132 7 ▶ if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 7 0 7
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 7 7
133 0 croak("stream: Failed to grant key protmem RO");
134  
135 7 Newx(out_buf, out_len + 1, unsigned char);
136 7 ▶ if (out_buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) { 0 7 7
137 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
138 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
139 0 croak("stream: Failed to allocate memory");
140 }
141 7 out_buf[out_len] = '\0';
142  
143 7 func(out_buf, out_len, nonce_buf, key_buf);
144  
145 7 ▶ if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 7 0 7
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 7 7
146 0 croak("stream: Failed to release key protmem RO");
147  
148 7 RETVAL = newSV(0);
149 7 sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL);
150  
151 OUTPUT:
152 RETVAL
153  
154 SV * stream_keygen(SV * flags = &PL_sv_undef)
155  
156 ALIAS:
157 stream_chacha20_keygen = 1
158 stream_chacha20_ietf_keygen = 2
159 stream_salsa20_keygen = 3
160 stream_salsa2012_keygen = 4
161 stream_xchacha20_keygen = 5
162 stream_xsalsa20_keygen = 6
163  
164 CODE:
165 7 switch(ix) {
166 1 case 1:
167 1 RETVAL = sv_keygen(aTHX_ crypto_stream_chacha20_KEYBYTES, flags);
168 1 break;
169 1 case 2:
170 1 RETVAL = sv_keygen(aTHX_ crypto_stream_chacha20_ietf_KEYBYTES, flags);
171 1 break;
172 1 case 3:
173 1 RETVAL = sv_keygen(aTHX_ crypto_stream_salsa20_KEYBYTES, flags);
174 1 break;
175 1 case 4:
176 1 RETVAL = sv_keygen(aTHX_ crypto_stream_salsa2012_KEYBYTES, flags);
177 1 break;
178 1 case 5:
179 1 RETVAL = sv_keygen(aTHX_ crypto_stream_xchacha20_KEYBYTES, flags);
180 1 break;
181 1 case 6:
182 1 RETVAL = sv_keygen(aTHX_ crypto_stream_xsalsa20_KEYBYTES, flags);
183 1 break;
184 1 default:
185 1 RETVAL = sv_keygen(aTHX_ crypto_stream_KEYBYTES, flags);
186 }
187  
188 OUTPUT:
189 RETVAL
190  
191 SV * stream_nonce(SV * base = &PL_sv_undef)
192  
193 ALIAS:
194 stream_chacha20_nonce = 1
195 stream_chacha20_ietf_nonce = 2
196 stream_salsa20_nonce = 3
197 stream_salsa2012_nonce = 4
198 stream_xchacha20_nonce = 5
199 stream_xsalsa20_nonce = 6
200  
201 CODE:
202 /* NB: not getting magic for base as nonce_generate does it. may make these
203 * alg-specific checks fail if a base is magic */
204 11 switch(ix) {
205 2 case 1:
206 2 RETVAL = nonce_generate(aTHX_ crypto_stream_chacha20_NONCEBYTES, base);
207 2 ▶ if (!SvOK(base))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(base)) 1 1 2
208 1 croak("Random nonces are unsafe with this primitive");
209 1 break;
210 2 case 2:
211 2 ▶ if (!SvOK(base))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(base)) 1 1 2
212 1 croak("Random nonces are unsafe with this primitive");
213 1 RETVAL = nonce_generate(aTHX_ crypto_stream_chacha20_ietf_NONCEBYTES, base);
214 1 break;
215 2 case 3:
216 2 ▶ if (!SvOK(base))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(base)) 1 1 2
217 1 croak("Random nonces are unsafe with this primitive");
218 1 RETVAL = nonce_generate(aTHX_ crypto_stream_salsa20_NONCEBYTES, base);
219 1 break;
220 2 case 4:
221 2 ▶ if (!SvOK(base))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(base)) 1 1 2
222 1 croak("Random nonces are unsafe with this primitive");
223 1 RETVAL = nonce_generate(aTHX_ crypto_stream_salsa2012_NONCEBYTES, base);
224 1 break;
225 1 case 5:
226 1 RETVAL = nonce_generate(aTHX_ crypto_stream_xchacha20_NONCEBYTES, base);
227 1 break;
228 1 case 6:
229 1 RETVAL = nonce_generate(aTHX_ crypto_stream_xsalsa20_NONCEBYTES, base);
230 1 break;
231 1 default:
232 1 RETVAL = nonce_generate(aTHX_ crypto_stream_NONCEBYTES, base);
233 }
234 OUTPUT:
235 RETVAL
236  
237 SV * stream_xor(SV * msg, SV * nonce, SV * key, SV * flags = &PL_sv_undef)
238  
239 ALIAS:
240 stream_chacha20_xor = 1
241 stream_chacha20_ietf_xor = 2
242 stream_salsa20_xor = 3
243 stream_salsa2012_xor = 4
244 stream_xchacha20_xor = 5
245 stream_xsalsa20_xor = 6
246  
247 PREINIT:
248 27 protmem *msg_pm = NULL, *key_pm = NULL, *ct_pm = NULL;
249 unsigned char *msg_buf, *nonce_buf, *key_buf, *ct_buf;
250 27 ▶ unsigned int new_flags = g_protmem_default_flags_decrypt;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int new_flags = g_protmem_default_flags_decrypt; 14 13 27
251 STRLEN msg_len, nonce_len, key_len, nonce_req_len, key_req_len;
252 int (*func)(unsigned char *, const unsigned char *, unsigned long long,
253 const unsigned char *, const unsigned char *);
254  
255 CODE:
256 27 switch(ix) {
257 4 case 1:
258 4 nonce_req_len = crypto_stream_chacha20_NONCEBYTES;
259 4 key_req_len = crypto_stream_chacha20_KEYBYTES;
260 4 func = crypto_stream_chacha20_xor;
261 4 break;
262 4 case 2:
263 4 nonce_req_len = crypto_stream_chacha20_ietf_NONCEBYTES;
264 4 key_req_len = crypto_stream_chacha20_ietf_KEYBYTES;
265 4 func = crypto_stream_chacha20_ietf_xor;
266 4 break;
267 4 case 3:
268 4 nonce_req_len = crypto_stream_salsa20_NONCEBYTES;
269 4 key_req_len = crypto_stream_salsa20_KEYBYTES;
270 4 func = crypto_stream_salsa20_xor;
271 4 break;
272 3 case 4:
273 3 nonce_req_len = crypto_stream_salsa2012_NONCEBYTES;
274 3 key_req_len = crypto_stream_salsa2012_KEYBYTES;
275 3 func = crypto_stream_salsa2012_xor;
276 3 break;
277 4 case 5:
278 4 nonce_req_len = crypto_stream_xchacha20_NONCEBYTES;
279 4 key_req_len = crypto_stream_xchacha20_KEYBYTES;
280 4 func = crypto_stream_xchacha20_xor;
281 4 break;
282 4 case 6:
283 4 nonce_req_len = crypto_stream_xsalsa20_NONCEBYTES;
284 4 key_req_len = crypto_stream_xsalsa20_KEYBYTES;
285 4 func = crypto_stream_xsalsa20_xor;
286 4 break;
287 4 default:
288 4 nonce_req_len = crypto_stream_NONCEBYTES;
289 4 key_req_len = crypto_stream_KEYBYTES;
290 4 func = crypto_stream_xor;
291 }
292  
293 27 nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len);
294 27 ▶ if (nonce_len != nonce_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (nonce_len != nonce_req_len) 0 27 27
295 0 croak("stream_xor: Invalid nonce length %lu", nonce_len);
296  
297 27 ▶ if (sv_derived_from(msg, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(msg, MEMVAULT_CLASS)) { 0 27 27
298 0 msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS);
299 0 msg_buf = msg_pm->pm_ptr;
300 0 msg_len = msg_pm->size;
301 }
302 else
303 27 msg_buf = (unsigned char *)SvPVbyte(msg, msg_len);
304  
305 27 ▶ if (sv_derived_from(key, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(key, MEMVAULT_CLASS)) { 27 0 27
306 27 key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS);
307 27 key_buf = key_pm->pm_ptr;
308 27 key_len = key_pm->size;
309 }
310 else
311 0 key_buf = (unsigned char *)SvPVbyte(key, key_len);
312 27 ▶ if (key_len != key_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != key_req_len) 0 27 27
313 0 croak("stream_xor: Invalid key length %lu", key_len);
314  
315 27 ▶ if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 27 27
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
316 0 croak("stream_xor: Failed to grant msg protmem RO");
317  
318 27 ▶ if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 27 0 27
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 27 27
319 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
320 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
321 0 croak("stream_xor: Failed to grant key protmem RO");
322 }
323  
324 27 ▶ if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) { 14 13 27
325 14 Newx(ct_buf, msg_len + 1, unsigned char);
326 14 ▶ if (ct_buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_buf == NULL) { 0 14 14
327 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
328 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
329 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
330 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
331 0 croak("stream_xor: Failed to allocate memory");
332 }
333 14 ct_buf[msg_len] = '\0';
334 }
335 else {
336 13 SvGETMAGIC(flags);
337 13 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 13 0 13
338 13 new_flags = SvUV_nomg(flags);
339 13 ct_pm = protmem_init(aTHX_ msg_len, new_flags);
340 13 ▶ if (ct_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm == NULL) { 0 13 13
341 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
342 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
343 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
344 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
345 0 croak("stream_xor: Failed to allocate protmem");
346 }
347 13 ct_buf = ct_pm->pm_ptr;
348 }
349  
350 27 func(ct_buf, msg_buf, msg_len, nonce_buf, key_buf);
351  
352 27 ▶ if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 27 0 27
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 27 27
353 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
354 0 protmem_free(aTHX_ ct_pm);
355 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
356 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
357 0 croak("stream_xor: Failed to release key protmem RO");
358 }
359  
360 27 ▶ if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 27 27
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
361 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
362 0 protmem_free(aTHX_ ct_pm);
363 0 croak("stream_xor: Failed to release msg protmem RO");
364 }
365  
366 27 ▶ if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 13 14 27
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 13 13
367 0 croak("stream_xor: Failed to release protmem RW");
368  
369 27 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 13 14 27
370 13 RETVAL = protmem_to_sv(aTHX_ ct_pm, MEMVAULT_CLASS);
371 else {
372 14 RETVAL = newSV(0);
373 14 sv_usepvn_flags(RETVAL, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL);
374 }
375  
376 OUTPUT:
377 RETVAL
378  
379 =for documentation
380  
381 chacha20_ietf xor_ic uses 32-bit counter
382  
383 there's no xor_ic for salsa2012
384  
385 providing generic stream_xor_ic though libsodium doesn't
386  
387 =cut
388  
389 SV * stream_xor_ic(SV * msg, SV * nonce, UV ic, SV * key, SV * flags = &PL_sv_undef)
390  
391 ALIAS:
392 stream_chacha20_xor_ic = 1
393 stream_salsa20_xor_ic = 2
394 stream_xchacha20_xor_ic = 3
395 stream_xsalsa20_xor_ic = 4
396  
397 PREINIT:
398 10 protmem *msg_pm = NULL, *key_pm = NULL, *ct_pm = NULL;
399 unsigned char *msg_buf, *nonce_buf, *key_buf, *ct_buf;
400 10 unsigned int new_flags = g_protmem_default_flags_decrypt;
401 STRLEN msg_len, nonce_len, key_len, nonce_req_len, key_req_len;
402 int (*func)(unsigned char *, const unsigned char *, unsigned long long,
403 const unsigned char *, uint64_t, const unsigned char *);
404  
405 CODE:
406 10 switch(ix) {
407 2 case 1:
408 2 nonce_req_len = crypto_stream_chacha20_NONCEBYTES;
409 2 key_req_len = crypto_stream_chacha20_KEYBYTES;
410 2 func = crypto_stream_chacha20_xor_ic;
411 2 break;
412 2 case 2:
413 2 nonce_req_len = crypto_stream_salsa20_NONCEBYTES;
414 2 key_req_len = crypto_stream_salsa20_KEYBYTES;
415 2 func = crypto_stream_salsa20_xor_ic;
416 2 break;
417 2 case 3:
418 2 nonce_req_len = crypto_stream_xchacha20_NONCEBYTES;
419 2 key_req_len = crypto_stream_xchacha20_KEYBYTES;
420 2 func = crypto_stream_xchacha20_xor_ic;
421 2 break;
422 4 default:
423 4 nonce_req_len = crypto_stream_xsalsa20_NONCEBYTES;
424 4 key_req_len = crypto_stream_xsalsa20_KEYBYTES;
425 4 func = crypto_stream_xsalsa20_xor_ic;
426 }
427  
428 10 nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len);
429 10 ▶ if (nonce_len != nonce_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (nonce_len != nonce_req_len) 0 10 10
430 0 croak("stream_xor_ic: Invalid nonce length %lu", nonce_len);
431  
432 10 ▶ if (sv_derived_from(msg, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(msg, MEMVAULT_CLASS)) { 0 10 10
433 0 msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS);
434 0 msg_buf = msg_pm->pm_ptr;
435 0 msg_len = msg_pm->size;
436 }
437 else
438 10 msg_buf = (unsigned char *)SvPVbyte(msg, msg_len);
439  
440 10 ▶ if (sv_derived_from(key, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(key, MEMVAULT_CLASS)) { 10 0 10
441 10 key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS);
442 10 key_buf = key_pm->pm_ptr;
443 10 key_len = key_pm->size;
444 }
445 else
446 0 key_buf = (unsigned char *)SvPVbyte(key, key_len);
447 10 ▶ if (key_len != key_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != key_req_len) 0 10 10
448 0 croak("stream_xor_ic: Invalid key length %lu", key_len);
449  
450 10 ▶ if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 10 10
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
451 0 croak("stream_xor_ic: Failed to grant msg protmem RO");
452  
453 10 ▶ if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 10 0 10
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 10 10
454 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
455 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
456 0 croak("stream_xor_ic: Failed to grant key protmem RO");
457 }
458  
459 10 ▶ if (items == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) { 10 0 10
460 10 Newx(ct_buf, msg_len + 1, unsigned char);
461 10 ▶ if (ct_buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_buf == NULL) { 0 10 10
462 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
463 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
464 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
465 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
466 0 croak("stream_xor_ic: Failed to allocate memory");
467 }
468 10 ct_buf[msg_len] = '\0';
469 }
470 else {
471 0 SvGETMAGIC(flags);
472 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
473 0 new_flags = SvUV_nomg(flags);
474 0 ct_pm = protmem_init(aTHX_ msg_len, new_flags);
475 0 ▶ if (ct_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm == NULL) { 0 0 0
476 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
477 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
478 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
479 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
480 0 croak("stream_xor: Failed to allocate protmem");
481 }
482 0 ct_buf = ct_pm->pm_ptr;
483 }
484  
485 10 func(ct_buf, msg_buf, msg_len, nonce_buf, ic, key_buf);
486  
487 10 ▶ if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 10 0 10
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 10 10
488 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
489 0 protmem_free(aTHX_ ct_pm);
490 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
491 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
492 0 croak("stream_xor_ic: Failed to release key protmem RO");
493 }
494  
495 10 ▶ if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 10 10
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
496 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
497 0 protmem_free(aTHX_ ct_pm);
498 0 croak("stream_xor_ic: Failed to release msg protmem RO");
499 }
500  
501 10 ▶ if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 10 10
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 0 0
502 0 croak("stream_xor: Failed to release protmem RW");
503  
504 10 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 10 10
505 0 RETVAL = protmem_to_sv(aTHX_ ct_pm, MEMVAULT_CLASS);
506 else {
507 10 RETVAL = newSV(0);
508 10 sv_usepvn_flags(RETVAL, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL);
509 }
510  
511 OUTPUT:
512 RETVAL
513  
514 =for documentation
515  
516 chacha20_ietf is special snowflake, with 32-bit counter
517  
518 do not use with a counter that could exceed 2 ** 32 -1.
519  
520 =cut
521  
522 SV * stream_chacha20_ietf_xor_ic(SV * msg, SV * nonce, U32 ic, SV * key, SV * flags = &PL_sv_undef)
523  
524 PREINIT:
525 2 protmem *msg_pm = NULL, *key_pm = NULL, *ct_pm = NULL;
526 unsigned char *msg_buf, *nonce_buf, *key_buf, *ct_buf;
527 2 unsigned int new_flags = g_protmem_default_flags_decrypt;
528 STRLEN msg_len, nonce_len, key_len;
529  
530 CODE:
531 2 nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len);
532 2 ▶ if (nonce_len != crypto_stream_chacha20_ietf_NONCEBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (nonce_len != crypto_stream_chacha20_ietf_NONCEBYTES) 0 2 2
533 0 croak("stream_chacha20_ietf_xor_ic: Invalid nonce length %lu", nonce_len);
534  
535 2 ▶ if (sv_derived_from(msg, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(msg, MEMVAULT_CLASS)) { 0 2 2
536 0 msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS);
537 0 msg_buf = msg_pm->pm_ptr;
538 0 msg_len = msg_pm->size;
539 }
540 else
541 2 msg_buf = (unsigned char *)SvPVbyte(msg, msg_len);
542  
543 2 ▶ if (sv_derived_from(key, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(key, MEMVAULT_CLASS)) { 2 0 2
544 2 key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS);
545 2 key_buf = key_pm->pm_ptr;
546 2 key_len = key_pm->size;
547 }
548 else
549 0 key_buf = (unsigned char *)SvPVbyte(key, key_len);
550 2 ▶ if (key_len != crypto_stream_chacha20_ietf_KEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != crypto_stream_chacha20_ietf_KEYBYTES) 0 2 2
551 0 croak("stream_chacha20_ietf_xor_ic: Invalid key length %lu", key_len);
552  
553 2 ▶ if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 2 2
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
554 0 croak("stream_chacha20_ietf_xor_ic: Failed to grant msg protmem RO");
555  
556 2 ▶ if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 2 0 2
if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
557 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
558 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
559 0 croak("stream_chacha20_ietf_xor_ic: Failed to grant key protmem RO");
560 }
561  
562 2 ▶ if (items == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) { 2 0 2
563 2 Newx(ct_buf, msg_len + 1, unsigned char);
564 2 ▶ if (ct_buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_buf == NULL) { 0 2 2
565 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
566 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
567 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
568 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
569 0 croak("stream_chacha20_ietf_xor_ic: Failed to allocate memory");
570 }
571 2 ct_buf[msg_len] = '\0';
572 }
573 else {
574 0 SvGETMAGIC(flags);
575 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
576 0 new_flags = SvUV_nomg(flags);
577 0 ct_pm = protmem_init(aTHX_ msg_len, new_flags);
578 0 ▶ if (ct_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm == NULL) { 0 0 0
579 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
580 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
581 0 ▶ if (key_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (key_pm) 0 0 0
582 0 protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO);
583 0 croak("stream_xor: Failed to allocate protmem");
584 }
585 0 ct_buf = ct_pm->pm_ptr;
586 }
587  
588 2 crypto_stream_chacha20_ietf_xor_ic(ct_buf, msg_buf, msg_len,
589 nonce_buf, ic, key_buf);
590  
591 2 ▶ if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 2 0 2
if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
592 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
593 0 protmem_free(aTHX_ ct_pm);
594 0 ▶ if (msg_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (msg_pm) 0 0 0
595 0 protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO);
596 0 croak("stream_chacha20_ietf_xor_ic: Failed to release key protmem RO");
597 }
598  
599 2 ▶ if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
600 0 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 0 0
601 0 protmem_free(aTHX_ ct_pm);
602 0 croak("stream_chacha20_ietf_xor_ic: Failed to release msg protmem RO");
603 }
604  
605 2 ▶ if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 2 2
if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 0 0
606 0 croak("stream_xor: Failed to release protmem RW");
607  
608 2 ▶ if (ct_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ct_pm) 0 2 2
609 0 RETVAL = protmem_to_sv(aTHX_ ct_pm, MEMVAULT_CLASS);
610 else {
611 2 RETVAL = newSV(0);
612 2 sv_usepvn_flags(RETVAL, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL);
613 }
614  
615 OUTPUT:
616 RETVAL
« xs/secretstream.xs ↑ Summary xs/kx.xs »