Sodium.xs

filter: Statementstmt 78.6% 1401 / 1781 Branchbran 58.7% 610 / 1038 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.3% 2011 / 2819 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/NaCl/Sodium.pm ↑ Summary
1  
2 #define PERL_NO_GET_CONTEXT
3 #define NO_XSLOCKS
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8 #include "patchlevel.h"
9 #include "ppport.h"
10  
11 /* libsodium */
12 #include "sodium.h"
13  
14 #define DUMP(v) do_sv_dump(0, Perl_debug_log, v, 0, 4, 0, 0);
15  
16 typedef struct {
17 unsigned char * bytes;
18 STRLEN length;
19 int locked;
20 } DataBytesLocker;
21  
22 #if defined(AES256GCM_IS_AVAILABLE)
23 typedef struct {
24 int locked;
25 crypto_aead_aes256gcm_state * ctx;
26 } CryptNaClSodiumAeadAes256gcmState;
27 #endif
28  
29 typedef struct {
30 crypto_generichash_state * state;
31 size_t init_bytes;
32 } CryptNaClSodiumGenerichashStream;
33  
34 typedef struct {
35 crypto_hash_sha256_state * state;
36 } CryptNaClSodiumHashSha256Stream;
37  
38 typedef struct {
39 crypto_hash_sha512_state * state;
40 } CryptNaClSodiumHashSha512Stream;
41  
42 typedef struct {
43 crypto_auth_hmacsha256_state * state;
44 } CryptNaClSodiumAuthHmacsha256Stream;
45  
46 typedef struct {
47 crypto_auth_hmacsha512_state * state;
48 } CryptNaClSodiumAuthHmacsha512Stream;
49  
50 typedef struct {
51 crypto_auth_hmacsha512256_state * state;
52 } CryptNaClSodiumAuthHmacsha512256Stream;
53  
54 typedef struct {
55 crypto_onetimeauth_state * state;
56 } CryptNaClSodiumOnetimeauthStream;
57  
58 typedef DataBytesLocker * Data__BytesLocker;
59 #if defined(AES256GCM_IS_AVAILABLE)
60 typedef CryptNaClSodiumAeadAes256gcmState * Crypt__NaCl__Sodium__aead__aes256gcmstate;
61 #endif
62 typedef CryptNaClSodiumGenerichashStream * Crypt__NaCl__Sodium__generichash__stream;
63 typedef CryptNaClSodiumHashSha256Stream * Crypt__NaCl__Sodium__hash__sha256stream;
64 typedef CryptNaClSodiumHashSha512Stream * Crypt__NaCl__Sodium__hash__sha512stream;
65 typedef CryptNaClSodiumAuthHmacsha256Stream * Crypt__NaCl__Sodium__auth__hmacsha256stream;
66 typedef CryptNaClSodiumAuthHmacsha512Stream * Crypt__NaCl__Sodium__auth__hmacsha512stream;
67 typedef CryptNaClSodiumAuthHmacsha512256Stream * Crypt__NaCl__Sodium__auth__hmacsha512256stream;
68 typedef CryptNaClSodiumOnetimeauthStream * Crypt__NaCl__Sodium__onetimeauth__stream;
69  
70 #define CLONESTATE(streamtype, statetype, padding, extra) \
71 Newx(new_stream, 1, streamtype);\
72 if ( new_stream == NULL ) {\
73 croak("Could not allocate enough memory");\
74 }\
75 new_stream->state = sodium_malloc(sizeof(crypto_ ## statetype ## _state) + padding);\
76 if ( new_stream->state == NULL ) {\
77 croak("Could not allocate enough memory");\
78 }\
79 extra;\
80 memcpy(new_stream->state,cur_stream->state,sizeof(crypto_ ## statetype ## _state) + padding);
81  
82 #if defined(USE_ITHREADS) && defined(MGf_DUP)
83 STATIC int dup_byteslocker(pTHX_ MAGIC *mg, CLONE_PARAMS *params)
84 {
85 DataBytesLocker *new_bl;
86 DataBytesLocker *cur_bl;
87 PERL_UNUSED_VAR(params);
88 Newx(new_bl, 1, DataBytesLocker);
89 if ( new_bl == NULL ) {
90 croak("Could not allocate enough memory");
91 }
92 cur_bl = (DataBytesLocker *)mg->mg_ptr;
93 new_bl->length = cur_bl->length;
94 new_bl->locked = cur_bl->locked;
95 new_bl->bytes = sodium_malloc(cur_bl->length + 1 );
96 if ( new_bl->bytes == NULL ) {
97 croak("Could not allocate enough memory");
98 }
99 memcpy(new_bl->bytes,cur_bl->bytes,cur_bl->length);
100 mg->mg_ptr = (char *)new_bl;
101 return 0;
102 }
103  
104 #define DUPSTREAM(streamtype, statetype, padding, extra) \
105 STATIC int dup_ ## statetype ## _stream(pTHX_ MAGIC *mg, CLONE_PARAMS *params)\
106 {\
107 streamtype *new_stream;\
108 streamtype *cur_stream;\
109 PERL_UNUSED_VAR(params);\
110 cur_stream = (streamtype *)mg->mg_ptr;\
111 CLONESTATE(streamtype, statetype, padding, extra)\
112 mg->mg_ptr = (char *)new_stream;\
113 return 0;\
114 }
115  
116 #if defined(AES256GCM_IS_AVAILABLE)
117 STATIC int dup_aead_aes256gcmstate(pTHX_ MAGIC *mg, CLONE_PARAMS *params)
118 {
119 CryptNaClSodiumAeadAes256gcmState *new_state;
120 CryptNaClSodiumAeadAes256gcmState *cur_state;
121 size_t sizeof_state = crypto_aead_aes256gcm_statebytes();
122 PERL_UNUSED_VAR(params);
123 cur_state = (CryptNaClSodiumAeadAes256gcmState *)mg->mg_ptr;
124  
125 Newx(new_state, 1, CryptNaClSodiumAeadAes256gcmState);
126 if ( new_state == NULL ) {
127 croak("Could not allocate enough memory");
128 }
129 new_state->ctx = sodium_malloc(sizeof_state);
130 if ( new_state->ctx == NULL ) {
131 croak("Could not allocate enough memory");
132 }
133 memcpy(new_state->ctx,cur_state->ctx,sizeof_state);
134  
135 mg->mg_ptr = (char *)new_state;
136 return 0;
137 }
138 #endif
139  
140 DUPSTREAM(CryptNaClSodiumGenerichashStream, generichash, (size_t)63U & ~(size_t) 63U, new_stream->init_bytes=cur_stream->init_bytes)
141 DUPSTREAM(CryptNaClSodiumHashSha256Stream, hash_sha256, 0, ((void)0))
142 DUPSTREAM(CryptNaClSodiumHashSha512Stream, hash_sha512, 0, ((void)0))
143 DUPSTREAM(CryptNaClSodiumAuthHmacsha256Stream, auth_hmacsha256, 0, ((void)0))
144 DUPSTREAM(CryptNaClSodiumAuthHmacsha512Stream, auth_hmacsha512, 0, ((void)0))
145 DUPSTREAM(CryptNaClSodiumAuthHmacsha512256Stream, auth_hmacsha512256, 0, ((void)0))
146 DUPSTREAM(CryptNaClSodiumOnetimeauthStream, onetimeauth, 0, ((void)0))
147  
148 #endif
149  
150 STATIC MGVTBL vtbl_byteslocker = {
151 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
152 #ifdef MGf_COPY
153 NULL, /* copy */
154 #endif
155 #ifdef MGf_DUP
156 # ifdef USE_ITHREADS
157 dup_byteslocker,
158 # else
159 NULL, /* dup */
160 # endif
161 #endif
162 #ifdef MGf_LOCAL
163 NULL /* local */
164 #endif
165 };
166 #if defined(AES256GCM_IS_AVAILABLE)
167 STATIC MGVTBL vtbl_aead_aes256gcmstate = {
168 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
169 #ifdef MGf_COPY
170 NULL, /* copy */
171 #endif
172 #ifdef MGf_DUP
173 # ifdef USE_ITHREADS
174 dup_aead_aes256gcmstate,
175 # else
176 NULL, /* dup */
177 # endif
178 #endif
179 #ifdef MGf_LOCAL
180 NULL /* local */
181 #endif
182 };
183 #endif
184 STATIC MGVTBL vtbl_generichash = {
185 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
186 #ifdef MGf_COPY
187 NULL, /* copy */
188 #endif
189 #ifdef MGf_DUP
190 # ifdef USE_ITHREADS
191 dup_generichash_stream,
192 # else
193 NULL, /* dup */
194 # endif
195 #endif
196 #ifdef MGf_LOCAL
197 NULL /* local */
198 #endif
199 };
200 STATIC MGVTBL vtbl_hash_sha256 = {
201 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
202 #ifdef MGf_COPY
203 NULL, /* copy */
204 #endif
205 #ifdef MGf_DUP
206 # ifdef USE_ITHREADS
207 dup_hash_sha256_stream,
208 # else
209 NULL, /* dup */
210 # endif
211 #endif
212 #ifdef MGf_LOCAL
213 NULL /* local */
214 #endif
215 };
216 STATIC MGVTBL vtbl_hash_sha512 = {
217 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
218 #ifdef MGf_COPY
219 NULL, /* copy */
220 #endif
221 #ifdef MGf_DUP
222 # ifdef USE_ITHREADS
223 dup_hash_sha512_stream,
224 # else
225 NULL, /* dup */
226 # endif
227 #endif
228 #ifdef MGf_LOCAL
229 NULL /* local */
230 #endif
231 };
232 STATIC MGVTBL vtbl_auth_hmacsha256 = {
233 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
234 #ifdef MGf_COPY
235 NULL, /* copy */
236 #endif
237 #ifdef MGf_DUP
238 # ifdef USE_ITHREADS
239 dup_auth_hmacsha256_stream,
240 # else
241 NULL, /* dup */
242 # endif
243 #endif
244 #ifdef MGf_LOCAL
245 NULL /* local */
246 #endif
247 };
248 STATIC MGVTBL vtbl_auth_hmacsha512 = {
249 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
250 #ifdef MGf_COPY
251 NULL, /* copy */
252 #endif
253 #ifdef MGf_DUP
254 # ifdef USE_ITHREADS
255 dup_auth_hmacsha512_stream,
256 # else
257 NULL, /* dup */
258 # endif
259 #endif
260 #ifdef MGf_LOCAL
261 NULL /* local */
262 #endif
263 };
264 STATIC MGVTBL vtbl_auth_hmacsha512256 = {
265 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
266 #ifdef MGf_COPY
267 NULL, /* copy */
268 #endif
269 #ifdef MGf_DUP
270 # ifdef USE_ITHREADS
271 dup_auth_hmacsha512256_stream,
272 # else
273 NULL, /* dup */
274 # endif
275 #endif
276 #ifdef MGf_LOCAL
277 NULL /* local */
278 #endif
279 };
280 STATIC MGVTBL vtbl_onetimeauth = {
281 NULL, /* get */ NULL, /* set */ NULL, /* len */ NULL, /* clear */ NULL, /* free */
282 #ifdef MGf_COPY
283 NULL, /* copy */
284 #endif
285 #ifdef MGf_DUP
286 # ifdef USE_ITHREADS
287 dup_onetimeauth_stream,
288 # else
289 NULL, /* dup */
290 # endif
291 #endif
292 #ifdef MGf_LOCAL
293 NULL /* local */
294 #endif
295 };
296  
297 115615 static DataBytesLocker * InitDataBytesLocker(pTHX_ STRLEN size) {
298 DataBytesLocker *bl;
299  
300 115615 ▶ if (size == SIZE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == SIZE_MAX) { 0 115615 115615
301 0 croak("Size requested in InitDataBytesLocker is too large");
302 }
303  
304 115615 Newx(bl, 1, DataBytesLocker);
305  
306 115615 ▶ if ( bl == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bl == NULL ) { 0 115615 115615
307 0 croak("Could not allocate enough memory");
308 }
309  
310 115615 bl->bytes = sodium_malloc(size + 1 );
311  
312 115615 ▶ if ( bl->bytes == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bl->bytes == NULL ) { 0 115615 115615
313 0 croak("Could not allocate enough memory");
314 }
315  
316 115615 bl->length = size;
317 115615 bl->locked = 0;
318  
319 115615 return bl;
320 }
321  
322 114129 static SV * DataBytesLocker2SV(pTHX_ DataBytesLocker *bl) {
323 114129 SV *sv = newSV(0);
324 114129 SV *obj = newRV_noinc(sv);
325 SV *default_locked;
326 #ifdef USE_ITHREADS
327 MAGIC *mg;
328 #endif
329  
330 114129 sv_bless(obj, gv_stashpv("Data::BytesLocker", 0));
331  
332 114129 ▶ if ( (default_locked = get_sv("Data::BytesLocker::DEFAULT_LOCKED", 0)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (default_locked = get_sv("Data::BytesLocker::DEFAULT_LOCKED", 0)) ) { 114129 0 114129
333 114129 ▶ if ( SvTRUE(default_locked) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTRUE(default_locked) ) { 13 114116 114129
334 13 int rc = sodium_mprotect_noaccess((void *)bl->bytes);
335  
336 13 ▶ if ( rc != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rc != 0 ) { 0 13 13
337 0 croak("Unable to protect BytesLocker object");
338 }
339 13 bl->locked = 1;
340 }
341 } else {
342 0 int rc = sodium_mprotect_readonly((void *)bl->bytes);
343  
344 0 ▶ if ( rc != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rc != 0 ) { 0 0 0
345 0 croak("Unable to protect BytesLocker object");
346 }
347 }
348  
349 #ifdef USE_ITHREADS
350 mg =
351 #endif
352 114129 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_byteslocker, (const char *)bl, 0);
353  
354 #if defined(USE_ITHREADS) && defined(MGf_DUP)
355 mg->mg_flags |= MGf_DUP;
356 #endif
357  
358 114129 return obj;
359 }
360  
361 442727 static DataBytesLocker* GetBytesLocker(pTHX_ SV* sv)
362 {
363 MAGIC *mg;
364  
365 442727 ▶ if (!sv_derived_from(sv, "Data::BytesLocker"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Data::BytesLocker")) 0 442727 442727
366 0 croak("Not a reference to a Data::BytesLocker object");
367  
368 442727 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 442727 0 442727
369 442727 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_byteslocker) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_byteslocker) { 442727 0 442727
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_byteslocker) { 442727 0 442727
370 442727 return (DataBytesLocker *)mg->mg_ptr;
371 }
372 }
373  
374 0 croak("Failed to get Data::BytesLocker pointer");
375 return (DataBytesLocker*)0; /* some compilers insist on a return value */
376 }
377  
378 #if defined(AES256GCM_IS_AVAILABLE)
379 384 static CryptNaClSodiumAeadAes256gcmState * InitAeadAes256gcmState(pTHX_ unsigned char * key) {
380 CryptNaClSodiumAeadAes256gcmState *pk;
381 384 Newx(pk, 1, CryptNaClSodiumAeadAes256gcmState);
382  
383 384 ▶ if ( pk == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pk == NULL ) { 0 384 384
384 0 croak("Could not allocate enough memory");
385 }
386  
387 384 pk->ctx = sodium_malloc(crypto_aead_aes256gcm_statebytes());
388  
389 384 ▶ if ( pk->ctx == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pk->ctx == NULL ) { 0 384 384
390 0 croak("Could not allocate enough memory");
391 }
392  
393  
394 384 crypto_aead_aes256gcm_beforenm(pk->ctx, key);
395 384 pk->locked = 0;
396  
397 384 return pk;
398 }
399  
400 384 static SV * AeadAes256gcmState2SV(pTHX_ CryptNaClSodiumAeadAes256gcmState *state) {
401 384 SV *sv = newSV(0);
402 384 SV *obj = newRV_noinc(sv);
403 SV *default_locked;
404 #ifdef USE_ITHREADS
405 MAGIC *mg;
406 #endif
407  
408 384 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::aead::aes256gcmstate", 0));
409  
410 384 ▶ if ( (default_locked = get_sv("Data::BytesLocker::DEFAULT_LOCKED", 0)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (default_locked = get_sv("Data::BytesLocker::DEFAULT_LOCKED", 0)) ) { 384 0 384
411 384 ▶ if ( SvTRUE(default_locked) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTRUE(default_locked) ) { 0 384 384
412 0 int rc = sodium_mprotect_noaccess((void *)state->ctx);
413  
414 0 ▶ if ( rc != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rc != 0 ) { 0 0 0
415 0 croak("Unable to protect AES256GCM precalculated key object");
416 }
417 0 state->locked = 1;
418 }
419 }
420  
421 #ifdef USE_ITHREADS
422 mg =
423 #endif
424 384 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_aead_aes256gcmstate, (const char *)state, 0);
425  
426 #if defined(USE_ITHREADS) && defined(MGf_DUP)
427 mg->mg_flags |= MGf_DUP;
428 #endif
429  
430 384 return obj;
431 }
432  
433 1160 static CryptNaClSodiumAeadAes256gcmState* GetAeadAes256gcmState(pTHX_ SV* sv)
434 {
435 MAGIC *mg;
436  
437 1160 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::aead::aes256gcmstate"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::aead::aes256gcmstate")) 0 1160 1160
438 0 croak("Not a reference to a Crypt::NaCl::Sodium::aead::aes256gcmstate object");
439  
440 1160 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 1160 0 1160
441 1160 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_aead_aes256gcmstate) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_aead_aes256gcmstate) { 1160 0 1160
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_aead_aes256gcmstate) { 1160 0 1160
442 1160 return (CryptNaClSodiumAeadAes256gcmState *)mg->mg_ptr;
443 }
444 }
445  
446 0 croak("Failed to get Crypt::NaCl::Sodium::aead::aes256gcmstate pointer");
447 return (CryptNaClSodiumAeadAes256gcmState*)0; /* some compilers insist on a return value */
448 }
449 #endif
450  
451 4783 static SV * GenerichashStream2SV(pTHX_ CryptNaClSodiumGenerichashStream *stream) {
452 4783 SV *sv = newSV(0);
453 4783 SV *obj = newRV_noinc(sv);
454 #ifdef USE_ITHREADS
455 MAGIC *mg;
456 #endif
457  
458 4783 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::generichash::stream", 0));
459  
460 #ifdef USE_ITHREADS
461 mg =
462 #endif
463 4783 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_generichash, (const char *)stream, 0);
464  
465 #if defined(USE_ITHREADS) && defined(MGf_DUP)
466 mg->mg_flags |= MGf_DUP;
467 #endif
468  
469 4783 return obj;
470 }
471  
472 133067 static CryptNaClSodiumGenerichashStream* GetGenerichashStream(pTHX_ SV* sv)
473 {
474 MAGIC *mg;
475  
476 133067 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::generichash::stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::generichash::stream")) 0 133067 133067
477 0 croak("Not a reference to a Crypt::NaCl::Sodium::generichash::stream object");
478  
479 133067 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 133067 0 133067
480 133067 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_generichash) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_generichash) { 133067 0 133067
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_generichash) { 133067 0 133067
481 133067 return (CryptNaClSodiumGenerichashStream *)mg->mg_ptr;
482 }
483 }
484  
485 0 croak("Failed to get Crypt::NaCl::Sodium::generichash::stream pointer");
486 return (CryptNaClSodiumGenerichashStream*)0; /* some compilers insist on a return value */
487 }
488  
489  
490 4 static SV * HashSha256Stream2SV(pTHX_ CryptNaClSodiumHashSha256Stream *stream) {
491 4 SV *sv = newSV(0);
492 4 SV *obj = newRV_noinc(sv);
493 #ifdef USE_ITHREADS
494 MAGIC *mg;
495 #endif
496  
497 4 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::hash::sha256stream", 0));
498  
499 #ifdef USE_ITHREADS
500 mg =
501 #endif
502 4 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_hash_sha256, (const char *)stream, 0);
503  
504 #if defined(USE_ITHREADS) && defined(MGf_DUP)
505 mg->mg_flags |= MGf_DUP;
506 #endif
507  
508 4 return obj;
509 }
510  
511 365 static CryptNaClSodiumHashSha256Stream* GetHashSha256Stream(pTHX_ SV* sv)
512 {
513 MAGIC *mg;
514  
515 365 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::hash::sha256stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::hash::sha256stream")) 0 365 365
516 0 croak("Not a reference to a Crypt::NaCl::Sodium::hash::sha256stream object");
517  
518 365 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 365 0 365
519 365 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha256) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha256) { 365 0 365
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha256) { 365 0 365
520 365 return (CryptNaClSodiumHashSha256Stream *)mg->mg_ptr;
521 }
522 }
523  
524 0 croak("Failed to get Crypt::NaCl::Sodium::hash::sha256stream pointer");
525 return (CryptNaClSodiumHashSha256Stream*)0; /* some compilers insist on a return value */
526 }
527  
528  
529 4 static SV * HashSha512Stream2SV(pTHX_ CryptNaClSodiumHashSha512Stream *stream) {
530 4 SV *sv = newSV(0);
531 4 SV *obj = newRV_noinc(sv);
532 #ifdef USE_ITHREADS
533 MAGIC *mg;
534 #endif
535  
536 4 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::hash::sha512stream", 0));
537  
538 #ifdef USE_ITHREADS
539 mg =
540 #endif
541 4 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_hash_sha512, (const char *)stream, 0);
542  
543 #if defined(USE_ITHREADS) && defined(MGf_DUP)
544 mg->mg_flags |= MGf_DUP;
545 #endif
546  
547 4 return obj;
548 }
549  
550 365 static CryptNaClSodiumHashSha512Stream* GetHashSha512Stream(pTHX_ SV* sv)
551 {
552 MAGIC *mg;
553  
554 365 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::hash::sha512stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::hash::sha512stream")) 0 365 365
555 0 croak("Not a reference to a Crypt::NaCl::Sodium::hash::sha512stream object");
556  
557 365 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 365 0 365
558 365 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha512) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha512) { 365 0 365
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_hash_sha512) { 365 0 365
559 365 return (CryptNaClSodiumHashSha512Stream *)mg->mg_ptr;
560 }
561 }
562  
563 0 croak("Failed to get Crypt::NaCl::Sodium::hash::sha512stream pointer");
564 return (CryptNaClSodiumHashSha512Stream*)0; /* some compilers insist on a return value */
565 }
566  
567  
568 197 static SV * AuthHmacsha256Stream2SV(pTHX_ CryptNaClSodiumAuthHmacsha256Stream *stream) {
569 197 SV *sv = newSV(0);
570 197 SV *obj = newRV_noinc(sv);
571 #ifdef USE_ITHREADS
572 MAGIC *mg;
573 #endif
574  
575 197 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::auth::hmacsha256stream", 0));
576  
577 #ifdef USE_ITHREADS
578 mg =
579 #endif
580 197 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_auth_hmacsha256, (const char *)stream, 0);
581  
582 #if defined(USE_ITHREADS) && defined(MGf_DUP)
583 mg->mg_flags |= MGf_DUP;
584 #endif
585  
586 197 return obj;
587 }
588  
589 643 static CryptNaClSodiumAuthHmacsha256Stream* GetAuthHmacsha256Stream(pTHX_ SV* sv)
590 {
591 MAGIC *mg;
592  
593 643 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha256stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha256stream")) 0 643 643
594 0 croak("Not a reference to a Crypt::NaCl::Sodium::auth::hmacsha256stream object");
595  
596 643 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 643 0 643
597 643 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha256) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha256) { 643 0 643
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha256) { 643 0 643
598 643 return (CryptNaClSodiumAuthHmacsha256Stream *)mg->mg_ptr;
599 }
600 }
601  
602 0 croak("Failed to get Crypt::NaCl::Sodium::auth::hmacsha256stream pointer");
603 return (CryptNaClSodiumAuthHmacsha256Stream*)0; /* some compilers insist on a return value */
604 }
605  
606  
607 197 static SV * AuthHmacsha512Stream2SV(pTHX_ CryptNaClSodiumAuthHmacsha512Stream *stream) {
608 197 SV *sv = newSV(0);
609 197 SV *obj = newRV_noinc(sv);
610 #ifdef USE_ITHREADS
611 MAGIC *mg;
612 #endif
613  
614 197 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::auth::hmacsha512stream", 0));
615  
616 #ifdef USE_ITHREADS
617 mg =
618 #endif
619 197 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_auth_hmacsha512, (const char *)stream, 0);
620  
621 #if defined(USE_ITHREADS) && defined(MGf_DUP)
622 mg->mg_flags |= MGf_DUP;
623 #endif
624  
625 197 return obj;
626 }
627  
628 643 static CryptNaClSodiumAuthHmacsha512Stream* GetAuthHmacsha512Stream(pTHX_ SV* sv)
629 {
630 MAGIC *mg;
631  
632 643 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha512stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha512stream")) 0 643 643
633 0 croak("Not a reference to a Crypt::NaCl::Sodium::auth::hmacsha512stream object");
634  
635 643 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 643 0 643
636 643 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512) { 643 0 643
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512) { 643 0 643
637 643 return (CryptNaClSodiumAuthHmacsha512Stream *)mg->mg_ptr;
638 }
639 }
640  
641 0 croak("Failed to get Crypt::NaCl::Sodium::auth::hmacsha512stream pointer");
642 return (CryptNaClSodiumAuthHmacsha512Stream*)0; /* some compilers insist on a return value */
643 }
644  
645  
646 199 static SV * AuthHmacsha512256Stream2SV(pTHX_ CryptNaClSodiumAuthHmacsha512256Stream *stream) {
647 199 SV *sv = newSV(0);
648 199 SV *obj = newRV_noinc(sv);
649 #ifdef USE_ITHREADS
650 MAGIC *mg;
651 #endif
652  
653 199 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::auth::hmacsha512256stream", 0));
654  
655 #ifdef USE_ITHREADS
656 mg =
657 #endif
658 199 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_auth_hmacsha512256, (const char *)stream, 0);
659  
660 #if defined(USE_ITHREADS) && defined(MGf_DUP)
661 mg->mg_flags |= MGf_DUP;
662 #endif
663  
664 199 return obj;
665 }
666  
667 675 static CryptNaClSodiumAuthHmacsha512256Stream* GetAuthHmacsha512256Stream(pTHX_ SV* sv)
668 {
669 MAGIC *mg;
670  
671 675 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha512256stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::auth::hmacsha512256stream")) 0 675 675
672 0 croak("Not a reference to a Crypt::NaCl::Sodium::auth::hmacsha512256stream object");
673  
674 675 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 675 0 675
675 675 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512256) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512256) { 675 0 675
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_auth_hmacsha512256) { 675 0 675
676 675 return (CryptNaClSodiumAuthHmacsha512256Stream *)mg->mg_ptr;
677 }
678 }
679  
680 0 croak("Failed to get Crypt::NaCl::Sodium::auth::hmacsha512256stream pointer");
681 return (CryptNaClSodiumAuthHmacsha512256Stream*)0; /* some compilers insist on a return value */
682 }
683  
684  
685 3 static SV * OnetimeauthStream2SV(pTHX_ CryptNaClSodiumOnetimeauthStream *stream) {
686 3 SV *sv = newSV(0);
687 3 SV *obj = newRV_noinc(sv);
688 #ifdef USE_ITHREADS
689 MAGIC *mg;
690 #endif
691  
692 3 sv_bless(obj, gv_stashpv("Crypt::NaCl::Sodium::onetimeauth::stream", 0));
693  
694 #ifdef USE_ITHREADS
695 mg =
696 #endif
697 3 sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_onetimeauth, (const char *)stream, 0);
698  
699 #if defined(USE_ITHREADS) && defined(MGf_DUP)
700 mg->mg_flags |= MGf_DUP;
701 #endif
702  
703 3 return obj;
704 }
705  
706 32 static CryptNaClSodiumOnetimeauthStream* GetOnetimeauthStream(pTHX_ SV* sv)
707 {
708 MAGIC *mg;
709  
710 32 ▶ if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::onetimeauth::stream"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(sv, "Crypt::NaCl::Sodium::onetimeauth::stream")) 0 32 32
711 0 croak("Not a reference to a Crypt::NaCl::Sodium::onetimeauth::stream object");
712  
713 32 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 32 0 32
714 32 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_onetimeauth) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_onetimeauth) { 32 0 32
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_onetimeauth) { 32 0 32
715 32 return (CryptNaClSodiumOnetimeauthStream *)mg->mg_ptr;
716 }
717 }
718  
719 0 croak("Failed to get Crypt::NaCl::Sodium::onetimeauth::stream pointer");
720 return (CryptNaClSodiumOnetimeauthStream*)0; /* some compilers insist on a return value */
721 }
722  
723  
724 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium
725  
726 BOOT:
727 {
728 /* Initialise library */
729 54 ▶ if ( sodium_init() == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_init() == -1 ) 0 54 54
730 {
731 0 croak("Failed to initialze library");
732 }
733 }
734  
735 PROTOTYPES: ENABLE
736  
737 const char *
738 sodium_version_string()
739  
740 SV *
741 add(left, right)
742 SV * left
743 SV * right
744 PREINIT:
745 unsigned char * copy;
746 INIT:
747 unsigned char * left_buf;
748 unsigned char * right_buf;
749 STRLEN left_len;
750 STRLEN right_len;
751 CODE:
752 {
753 1030 ▶ left_buf = (unsigned char *)SvPV(left, left_len);
Branch: 1 branch
BranchTrueFalseTotal
left_buf = (unsigned char *)SvPV(left, left_len); 1030 0 1030
754 1030 right_buf = (unsigned char *)SvPV(right, right_len);
755 1030 ▶ if (right_len > left_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_len > left_len) { 1 1029 1030
756 1 croak("You must have a RHS less than or equal in length to the LHS");
757 }
758 1029 copy = sodium_malloc(left_len);
759 1029 ▶ if (copy == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (copy == NULL) { 0 1029 1029
760 0 croak("Could not allocate memory");
761 }
762  
763 1029 memcpy(copy, left_buf, left_len);
764  
765 1029 sodium_add(copy, right_buf, right_len);
766 1029 RETVAL = newSVpvn((const char * const)copy, left_len);
767 }
768 OUTPUT:
769 RETVAL
770 CLEANUP:
771 1029 sodium_free(copy);
772  
773 void
774 has_aes128ctr()
775 PPCODE:
776 {
777 #ifdef AES128CTR_IS_AVAILABLE
778 XSRETURN_YES;
779 #else
780 1 XSRETURN_NO;
781 #endif
782 }
783  
784 void
785 memcmp(left, right, length = 0)
786 SV * left
787 SV * right
788 unsigned long length
789 INIT:
790 unsigned char * left_buf;
791 unsigned char * right_buf;
792 STRLEN left_len;
793 STRLEN right_len;
794 PPCODE:
795 {
796 1005 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 1005 1005
797 0 XSRETURN_EMPTY;
798 }
799  
800 1005 ▶ left_buf = (unsigned char *)SvPV(left, left_len);
Branch: 1 branch
BranchTrueFalseTotal
left_buf = (unsigned char *)SvPV(left, left_len); 1005 0 1005
801 1005 right_buf = (unsigned char *)SvPV(right, right_len);
802 1005 ▶ if ( length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length == 0 ) { 2 1003 1005
803 2 ▶ if ( left_len != right_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( left_len != right_len ) { 1 1 2
804 1 croak("Variables of unequal length cannot be automatically compared. Please provide the length argument");
805 }
806 1 length = left_len;
807 } else {
808 1003 ▶ if ( length > left_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length > left_len ) { 1 1002 1003
809 1 croak("First argument is shorter then requested length");
810 }
811 1002 ▶ else if ( length > right_len ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( length > right_len ) { 1 1001 1002
812 1 croak("Second argument is shorter then requested length");
813 }
814 }
815  
816 1002 ▶ if ( sodium_memcmp(left_buf, right_buf, length) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_memcmp(left_buf, right_buf, length) == 0 ) { 1 1001 1002
817 1 XSRETURN_YES;
818 } else {
819 1001 XSRETURN_NO;
820 }
821 }
822  
823 void
824 compare(left, right, length = 0)
825 SV * left
826 SV * right
827 unsigned long length
828 INIT:
829 unsigned char * left_buf;
830 unsigned char * right_buf;
831 STRLEN left_len;
832 STRLEN right_len;
833 PPCODE:
834 {
835 2004 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 2004 2004
836 0 XSRETURN_EMPTY;
837 }
838  
839 2004 ▶ left_buf = (unsigned char *)SvPV(left, left_len);
Branch: 1 branch
BranchTrueFalseTotal
left_buf = (unsigned char *)SvPV(left, left_len); 4 2000 2004
840 2004 right_buf = (unsigned char *)SvPV(right, right_len);
841 2004 ▶ if ( length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length == 0 ) { 1 2003 2004
842 1 ▶ if ( left_len != right_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( left_len != right_len ) { 1 0 1
843 1 croak("Variables of unequal length cannot be automatically compared. Please provide the length argument");
844 }
845 0 length = left_len;
846 } else {
847 2003 ▶ if ( length > left_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length > left_len ) { 1 2002 2003
848 1 croak("First argument is shorter then requested length");
849 }
850 2002 ▶ else if ( length > right_len ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( length > right_len ) { 1 2001 2002
851 1 croak("Second argument is shorter then requested length");
852 }
853 }
854  
855 2001 XSRETURN_IV( sodium_compare(left_buf, right_buf, length) );
856 }
857  
858 void
859 increment(...)
860 INIT:
861 unsigned char * number_buf;
862 STRLEN len;
863 unsigned int i;
864 PPCODE:
865 {
866 10 ▶ for ( i = 0; i < items; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < items; i++ ) { 5 5 10
867 5 ▶ if (sv_derived_from(ST(i), "Data::BytesLocker")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(ST(i), "Data::BytesLocker")) { 0 5 5
868 0 croak("This function does not handle BytesLocker objects");
869 }
870 5 number_buf = (unsigned char *)SvPV(ST(i), len);
871  
872 5 sodium_increment(number_buf, len);
873 }
874 5 XSRETURN_EMPTY;
875 }
876  
877 void
878 memzero(...)
879 INIT:
880 unsigned char * buf;
881 STRLEN len;
882 unsigned int i;
883 PPCODE:
884 {
885 3 ▶ for ( i = 0; i < items; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < items; i++ ) { 2 1 3
886 2 buf = (unsigned char *)SvPV_force(ST(i), len);
887 2 sodium_memzero( buf, len);
888 }
889 1 XSRETURN_YES;
890 }
891  
892 SV *
893 random_number(...)
894 INIT:
895 unsigned int num;
896 CODE:
897 {
898 122791 ▶ if ( items == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items == 1 ) { 122711 80 122791
899 122711 unsigned int upper_bound = (unsigned int)SvUV(ST(0));
900  
901 122711 num = randombytes_uniform(upper_bound);
902 }
903 else {
904 80 num = randombytes_random();
905 }
906  
907 122791 RETVAL = newSVuv(num);
908 }
909 OUTPUT:
910 RETVAL
911  
912 SV *
913 random_bytes(length)
914 SV * length
915 INIT:
916 size_t len;
917 DataBytesLocker *bl;
918 CODE:
919 {
920 34195 len = (size_t)SvUV(length);
921 34195 ▶ if ( len < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( len < 1 ) { 1 34194 34195
922 1 croak("Invalid length");
923 };
924  
925 34194 bl = InitDataBytesLocker(aTHX_ len);
926  
927 34194 randombytes_buf( bl->bytes, len );
928  
929 34194 RETVAL = DataBytesLocker2SV(aTHX_ bl);
930 }
931 OUTPUT:
932 RETVAL
933  
934  
935 SV *
936 bin2hex(bin_sv)
937 SV * bin_sv
938 PREINIT:
939 char * hex;
940 unsigned char * bin;
941 size_t hex_len;
942 STRLEN bin_len;
943 CODE:
944 5545 bin = (unsigned char *)SvPV(bin_sv, bin_len);
945  
946 5545 ▶ if ( bin_len > SIZE_MAX / 2 ) { /* avoid overflow */
Branch: 1 branch
BranchTrueFalseTotal
if ( bin_len > SIZE_MAX / 2 ) { /* avoid overflow */ 0 5545 5545
947 0 croak("Binary data provided too large (will cause hex_len > SIZE_MAX)");
948 }
949 5545 hex_len = bin_len * 2;
950 5545 hex = sodium_malloc(hex_len + 1);
951 5545 ▶ if ( hex == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hex == NULL ) { 0 5545 5545
952 0 croak("Could not allocate memory");
953 }
954 5545 sodium_bin2hex(hex, hex_len + 1, bin, bin_len);
955  
956 5545 RETVAL = newSVpvn((const char * const)hex, hex_len);
957 OUTPUT:
958 RETVAL
959 CLEANUP:
960 5545 sodium_free(hex);
961  
962  
963 SV *
964 hex2bin(hex_sv, ...)
965 SV * hex_sv
966 PREINIT:
967 5682 size_t hex_len = 0;
968 5682 size_t bin_len = 0;
969 5682 size_t bin_max_len = 0;
970 5682 int i = 0;
971 5682 STRLEN keylen = 0;
972 5682 char * hex = NULL;
973 5682 char * ignore = NULL;
974 5682 char * key = NULL;
975 5682 const char *hex_end = NULL;
976 5682 unsigned char * bin = NULL;
977  
978 /* idiot check our hex string and turn it into a C string */
979 5682 ▶ if (SvOK(hex_sv) && SvPOK(hex_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(hex_sv) && SvPOK(hex_sv)) { 5682 0 5682
if (SvOK(hex_sv) && SvPOK(hex_sv)) { 5682 0 5682
980 5682 hex = SvPV(hex_sv, hex_len);
981 5682 ▶ if (hex_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex_len == 0) { 156 5526 5682
982 156 XSRETURN_PV("");
983 }
984 } else {
985 0 croak("A hex string must be provided");
986 }
987 CODE:
988 5526 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 7 5519 5526
989 7 ▶ if ((items + 1) % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((items + 1) % 2 != 0) { 0 7 7
990 0 croak("Invalid number of arguments");
991 }
992 15 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 8 7 15
993 8 ▶ if (!SvOK(ST(i)) || !SvPOK(ST(i)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(ST(i)) || !SvPOK(ST(i))) 8 0 8
if (!SvOK(ST(i)) || !SvPOK(ST(i))) 0 8 8
994 0 continue;
995 8 key = SvPV(ST(i), keylen);
996 8 ▶ if (keylen == 6 && strnEQ(key, "ignore", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (keylen == 6 && strnEQ(key, "ignore", 6)) { 4 4 8
if (keylen == 6 && strnEQ(key, "ignore", 6)) { 4 0 4
997 4 ▶ if (SvOK(ST(i + 1)) && SvPOK(ST(i + 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(ST(i + 1)) && SvPOK(ST(i + 1))) { 4 0 4
if (SvOK(ST(i + 1)) && SvPOK(ST(i + 1))) { 4 0 4
998 4 ignore = SvPV(ST(i + 1), keylen);
999 4 ▶ if (keylen < 1 && ignore != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (keylen < 1 && ignore != NULL) { 0 4 4
if (keylen < 1 && ignore != NULL) { 0 0 0
1000 0 ignore = NULL;
1001 }
1002 }
1003 4 ▶ } else if (keylen == 7 && strnEQ(key, "max_len", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (keylen == 7 && strnEQ(key, "max_len", 7)) { 4 0 4
} else if (keylen == 7 && strnEQ(key, "max_len", 7)) { 4 0 4
1004 4 ▶ if (SvOK(ST(i + 1)) && (SvIOK(ST(i + 1)) || SvUOK(ST(i + 1)))) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(ST(i + 1)) && (SvIOK(ST(i + 1)) || SvUOK(ST(i + 1)))) { 4 0 4
if (SvOK(ST(i + 1)) && (SvIOK(ST(i + 1)) || SvUOK(ST(i + 1)))) { 0 4 4
if (SvOK(ST(i + 1)) && (SvIOK(ST(i + 1)) || SvUOK(ST(i + 1)))) { 0 0 0
1005 4 bin_max_len = SvUV(ST(i + 1));
1006 }
1007 else {
1008 0 croak("Invalid max_len. Must be an integer > 0.");
1009 }
1010 }
1011 }
1012 }
1013  
1014 5526 ▶ size_t max_bin_size = (ignore == NULL) ? (hex_len / 2) : hex_len;
Branch: 1 branch
BranchTrueFalseTotal
size_t max_bin_size = (ignore == NULL) ? (hex_len / 2) : hex_len; 5522 4 5526
1015  
1016 /* ensure our binary string has enough space */
1017 5526 bin = (unsigned char *) sodium_malloc(max_bin_size + 1);
1018 5526 ▶ if (bin == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (bin == NULL) { 0 5526 5526
1019 0 croak("Could not allocate memory");
1020 }
1021  
1022 5526 memset(bin, '\0', max_bin_size + 1);
1023  
1024 5526 int status = sodium_hex2bin(bin, max_bin_size, hex, hex_len, ignore, &bin_len, &hex_end);
1025  
1026 /* If the call was successful, truncate to max_len if user requested it */
1027 5526 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 5526 0 5526
1028 5526 ▶ if (bin_max_len > 0 && bin_max_len < bin_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (bin_max_len > 0 && bin_max_len < bin_len) { 4 5522 5526
if (bin_max_len > 0 && bin_max_len < bin_len) { 2 2 4
1029 2 bin_len = bin_max_len;
1030 }
1031 5526 RETVAL = newSVpvn((const char *)bin, bin_len);
1032 } else {
1033 /* Handle actual errors (invalid hex, etc.) */
1034 0 RETVAL = &PL_sv_undef;
1035 }
1036  
1037 OUTPUT:
1038 RETVAL
1039 CLEANUP:
1040 5526 ▶ if (bin != NULL) sodium_free(bin);
Branch: 1 branch
BranchTrueFalseTotal
if (bin != NULL) sodium_free(bin); 5526 0 5526
1041  
1042 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::secretbox
1043  
1044 PROTOTYPES: DISABLE
1045  
1046 unsigned int
1047 NONCEBYTES(...)
1048 CODE:
1049 1 ▶ RETVAL = crypto_secretbox_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_secretbox_NONCEBYTES; 0 1 1
1050 OUTPUT:
1051 RETVAL
1052  
1053 unsigned int
1054 KEYBYTES(...)
1055 CODE:
1056 1 ▶ RETVAL = crypto_secretbox_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_secretbox_KEYBYTES; 1 0 1
1057 OUTPUT:
1058 RETVAL
1059  
1060 unsigned int
1061 MACBYTES(...)
1062 CODE:
1063 6 ▶ RETVAL = crypto_secretbox_MACBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_secretbox_MACBYTES; 1 5 6
1064 OUTPUT:
1065 RETVAL
1066  
1067 PROTOTYPES: ENABLE
1068  
1069 SV *
1070 keygen(self)
1071 SV * self
1072 INIT:
1073 DataBytesLocker *bl;
1074 CODE:
1075 {
1076 PERL_UNUSED_VAR(self);
1077  
1078 6 bl = InitDataBytesLocker(aTHX_ crypto_secretbox_KEYBYTES);
1079  
1080 6 randombytes_buf(bl->bytes, bl->length);
1081  
1082 6 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1083 }
1084 OUTPUT:
1085 RETVAL
1086  
1087 SV *
1088 nonce(self, ...)
1089 SV * self
1090 PROTOTYPE: $;$
1091 INIT:
1092 DataBytesLocker *bl;
1093 CODE:
1094 PERL_UNUSED_VAR(self);
1095  
1096 5 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 5 5
1097 0 croak("Invalid number of arguments");
1098 }
1099  
1100 5 ▶ if (items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2 ) { 0 5 5
1101 0 ▶ if ( SvOK(ST(1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(ST(1)) ) { 0 0 0
1102 STRLEN prev_nonce_len;
1103 0 unsigned char * prev_nonce = (unsigned char *)SvPV(ST(1), prev_nonce_len);
1104  
1105 0 ▶ if ( prev_nonce_len > crypto_secretbox_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev_nonce_len > crypto_secretbox_NONCEBYTES ) { 0 0 0
1106 0 croak("Base nonce too long");
1107 }
1108  
1109 0 bl = InitDataBytesLocker(aTHX_ crypto_secretbox_NONCEBYTES);
1110 0 memcpy(bl->bytes, prev_nonce, prev_nonce_len);
1111 0 sodium_memzero(bl->bytes + prev_nonce_len, bl->length - prev_nonce_len);
1112 }
1113 else {
1114 0 croak("Base nonce invalid");
1115 }
1116 }
1117 else {
1118 5 bl = InitDataBytesLocker(aTHX_ crypto_secretbox_NONCEBYTES);
1119 5 randombytes_buf(bl->bytes, bl->length);
1120 }
1121 5 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1122 OUTPUT:
1123 RETVAL
1124  
1125  
1126  
1127 void
1128 encrypt(self, msg, nonce, key)
1129 SV * self
1130 SV * msg
1131 SV * nonce
1132 SV * key
1133 PROTOTYPE: $$$$
1134 INIT:
1135 STRLEN msg_len;
1136 STRLEN nonce_len;
1137 STRLEN key_len;
1138 STRLEN enc_len;
1139 unsigned char * msg_buf;
1140 unsigned char * nonce_buf;
1141 unsigned char * key_buf;
1142 DataBytesLocker *bl;
1143 PPCODE:
1144 {
1145 PERL_UNUSED_VAR(self);
1146  
1147 10 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 10 10
1148 0 XSRETURN_EMPTY;
1149 }
1150  
1151 10 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
1152 10 ▶ if ( nonce_len != crypto_secretbox_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_secretbox_NONCEBYTES ) { 0 10 10
1153 0 croak("Invalid nonce");
1154 }
1155  
1156 10 key_buf = (unsigned char *)SvPV(key, key_len);
1157 10 ▶ if ( key_len != crypto_secretbox_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_secretbox_KEYBYTES ) { 0 10 10
1158 0 croak("Invalid key");
1159 }
1160  
1161 10 msg_buf = (unsigned char *)SvPV(msg, msg_len);
1162  
1163 /* detached mode */
1164 10 ▶ if ( GIMME_V == G_ARRAY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_ARRAY ) { 5 5 10
1165 DataBytesLocker *bl_mac;
1166  
1167 5 bl = InitDataBytesLocker(aTHX_ msg_len);
1168 5 bl_mac = InitDataBytesLocker(aTHX_ crypto_secretbox_MACBYTES);
1169  
1170 5 crypto_secretbox_detached( bl->bytes, bl_mac->bytes, (unsigned char *)msg_buf,
1171 (unsigned long long) msg_len, nonce_buf, key_buf);
1172 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl_mac) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl_mac) ); 0 5 5
1173 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
1174 5 XSRETURN(2);
1175 }
1176 /* combined mode */
1177 else {
1178 5 enc_len = crypto_secretbox_MACBYTES + msg_len;
1179 5 bl = InitDataBytesLocker(aTHX_ enc_len);
1180 5 crypto_secretbox_easy( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf);
1181  
1182 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
1183 5 XSRETURN(1);
1184 }
1185 }
1186  
1187 void
1188 decrypt(self, ciphertext, nonce, key)
1189 SV * self
1190 SV * ciphertext
1191 SV * nonce
1192 SV * key
1193 PROTOTYPE: $$$$
1194 INIT:
1195 STRLEN msg_len;
1196 STRLEN nonce_len;
1197 STRLEN key_len;
1198 STRLEN enc_len;
1199 unsigned char * msg_buf;
1200 unsigned char * nonce_buf;
1201 unsigned char * key_buf;
1202 DataBytesLocker *bl;
1203 PPCODE:
1204 {
1205 PERL_UNUSED_VAR(self);
1206  
1207 5 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 5 5
1208 0 XSRETURN_EMPTY;
1209 }
1210  
1211 5 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
1212 5 ▶ if ( nonce_len != crypto_secretbox_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_secretbox_NONCEBYTES ) { 0 5 5
1213 0 croak("Invalid nonce");
1214 }
1215  
1216 5 key_buf = (unsigned char *)SvPV(key, key_len);
1217 5 ▶ if ( key_len != crypto_secretbox_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_secretbox_KEYBYTES ) { 0 5 5
1218 0 croak("Invalid key");
1219 }
1220  
1221 5 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
1222 5 ▶ if ( msg_len < crypto_secretbox_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < crypto_secretbox_MACBYTES ) { 0 5 5
1223 0 croak("Invalid ciphertext");
1224 }
1225 5 enc_len = msg_len - crypto_secretbox_MACBYTES;
1226  
1227 5 bl = InitDataBytesLocker(aTHX_ enc_len);
1228 5 ▶ if ( crypto_secretbox_open_easy( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_secretbox_open_easy( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf) == 0 ) { 5 0 5
1229 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
1230 5 XSRETURN(1);
1231 }
1232 else {
1233 0 sodium_free(bl->bytes);
1234 0 Safefree(bl);
1235 0 croak("Message forged");
1236 }
1237 }
1238  
1239  
1240  
1241 void
1242 decrypt_detached(self, mac, ciphertext, nonce, key)
1243 SV * self
1244 SV * mac
1245 SV * ciphertext
1246 SV * nonce
1247 SV * key
1248 PROTOTYPE: $$$$
1249 INIT:
1250 STRLEN msg_len;
1251 STRLEN nonce_len;
1252 STRLEN key_len;
1253 STRLEN mac_len;
1254 unsigned char * msg_buf;
1255 unsigned char * nonce_buf;
1256 unsigned char * key_buf;
1257 unsigned char * mac_buf;
1258 DataBytesLocker *bl;
1259 PPCODE:
1260 {
1261 PERL_UNUSED_VAR(self);
1262  
1263 3 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 3 3
1264 0 XSRETURN_EMPTY;
1265 }
1266  
1267 3 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
1268 3 ▶ if ( nonce_len != crypto_secretbox_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_secretbox_NONCEBYTES ) { 0 3 3
1269 0 croak("Invalid nonce");
1270 }
1271  
1272 3 key_buf = (unsigned char *)SvPV(key, key_len);
1273 3 ▶ if ( key_len != crypto_secretbox_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_secretbox_KEYBYTES ) { 0 3 3
1274 0 croak("Invalid key");
1275 }
1276  
1277 3 mac_buf = (unsigned char *)SvPV(mac, mac_len);
1278 3 ▶ if ( mac_len != crypto_secretbox_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != crypto_secretbox_MACBYTES ) { 0 3 3
1279 0 croak("Invalid mac");
1280 }
1281  
1282 3 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
1283  
1284 3 bl = InitDataBytesLocker(aTHX_ msg_len);
1285 3 ▶ if ( crypto_secretbox_open_detached( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_secretbox_open_detached( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, key_buf) == 0 ) { 3 0 3
1286 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 3 3
1287 3 XSRETURN(1);
1288 }
1289 else {
1290 0 sodium_free(bl->bytes);
1291 0 Safefree(bl);
1292 0 croak("Message forged");
1293 }
1294 }
1295  
1296  
1297  
1298 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::auth
1299  
1300 PROTOTYPES: DISABLE
1301  
1302 unsigned int
1303 KEYBYTES(...)
1304 ALIAS:
1305 HMACSHA256_KEYBYTES = 1
1306 HMACSHA512_KEYBYTES = 2
1307 HMACSHA512256_KEYBYTES = 3
1308 CODE:
1309 5 switch(ix) {
1310 1 case 1:
1311 1 RETVAL = crypto_auth_hmacsha256_KEYBYTES;
1312 1 break;
1313 1 case 2:
1314 1 RETVAL = crypto_auth_hmacsha512_KEYBYTES;
1315 1 break;
1316 1 case 3:
1317 1 RETVAL = crypto_auth_hmacsha512256_KEYBYTES;
1318 1 break;
1319 2 default:
1320 2 RETVAL = crypto_auth_KEYBYTES;
1321 }
1322 OUTPUT:
1323 RETVAL
1324  
1325 unsigned int
1326 BYTES(...)
1327 ALIAS:
1328 HMACSHA256_BYTES = 1
1329 HMACSHA512_BYTES = 2
1330 HMACSHA512256_BYTES = 3
1331 CODE:
1332 20002 switch(ix) {
1333 1 case 1:
1334 1 RETVAL = crypto_auth_hmacsha256_BYTES;
1335 1 break;
1336 10000 case 2:
1337 10000 RETVAL = crypto_auth_hmacsha512_BYTES;
1338 10000 break;
1339 1 case 3:
1340 1 RETVAL = crypto_auth_hmacsha512256_BYTES;
1341 1 break;
1342 10000 default:
1343 10000 RETVAL = crypto_auth_BYTES;
1344 }
1345 OUTPUT:
1346 RETVAL
1347  
1348 PROTOTYPES: ENABLE
1349  
1350 SV *
1351 keygen(self)
1352 SV * self
1353 ALIAS:
1354 hmacsha256_keygen = 1
1355 hmacsha512_keygen = 2
1356 hmacsha512256_keygen = 3
1357 INIT:
1358 DataBytesLocker *bl;
1359 unsigned int key_size;
1360 CODE:
1361 {
1362 PERL_UNUSED_VAR(self);
1363  
1364 20009 switch(ix) {
1365 2 case 1:
1366 2 key_size = crypto_auth_hmacsha256_KEYBYTES;
1367 2 break;
1368 10002 case 2:
1369 10002 key_size = crypto_auth_hmacsha512_KEYBYTES;
1370 10002 break;
1371 2 case 3:
1372 2 key_size = crypto_auth_hmacsha512256_KEYBYTES;
1373 2 break;
1374 10003 default:
1375 10003 key_size = crypto_auth_KEYBYTES;
1376 }
1377 20009 bl = InitDataBytesLocker(aTHX_ key_size);
1378 20009 randombytes_buf(bl->bytes, key_size);
1379 20009 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1380 }
1381 OUTPUT:
1382 RETVAL
1383  
1384 SV *
1385 mac(self, msg, key)
1386 SV * self
1387 SV * msg
1388 SV * key
1389 PROTOTYPE: $$$
1390 ALIAS:
1391 hmacsha256 = 1
1392 hmacsha512 = 2
1393 hmacsha512256 = 3
1394 INIT:
1395 STRLEN msg_len;
1396 STRLEN key_len;
1397 unsigned char * msg_buf;
1398 unsigned char * key_buf;
1399 unsigned int mac_size;
1400 unsigned int key_size;
1401 int (*mac_function)(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *);
1402 DataBytesLocker *bl;
1403 CODE:
1404 {
1405 PERL_UNUSED_VAR(self);
1406  
1407 20013 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 20013 20013
1408 0 XSRETURN_EMPTY;
1409 }
1410  
1411 20013 switch(ix) {
1412 4 case 1:
1413 4 mac_size = crypto_auth_hmacsha256_BYTES;
1414 4 key_size = crypto_auth_hmacsha256_KEYBYTES;
1415 4 mac_function = &crypto_auth_hmacsha256;
1416 4 break;
1417 10003 case 2:
1418 10003 mac_size = crypto_auth_hmacsha512_BYTES;
1419 10003 key_size = crypto_auth_hmacsha512_KEYBYTES;
1420 10003 mac_function = &crypto_auth_hmacsha512;
1421 10003 break;
1422 2 case 3:
1423 2 mac_size = crypto_auth_hmacsha512256_BYTES;
1424 2 key_size = crypto_auth_hmacsha512256_KEYBYTES;
1425 2 mac_function = &crypto_auth_hmacsha512256;
1426 2 break;
1427 10004 default:
1428 10004 mac_size = crypto_auth_BYTES;
1429 10004 key_size = crypto_auth_KEYBYTES;
1430 10004 mac_function = &crypto_auth;
1431 }
1432  
1433 20013 key_buf = (unsigned char *)SvPV(key, key_len);
1434 20013 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 20013 20013
1435 0 croak("Invalid key");
1436 }
1437  
1438 20013 msg_buf = (unsigned char *)SvPV(msg, msg_len);
1439  
1440 20013 bl = InitDataBytesLocker(aTHX_ mac_size);
1441 20013 (*mac_function)( bl->bytes, msg_buf, msg_len, key_buf);
1442  
1443 20013 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1444 }
1445 OUTPUT:
1446 RETVAL
1447  
1448 void
1449 verify(self, mac, msg, key)
1450 SV * self
1451 SV * mac
1452 SV * msg
1453 SV * key
1454 PROTOTYPE: $$$$
1455 INIT:
1456 STRLEN msg_len;
1457 STRLEN key_len;
1458 STRLEN mac_len;
1459 unsigned char * msg_buf;
1460 unsigned char * mac_buf;
1461 unsigned char * key_buf;
1462 PPCODE:
1463 {
1464 PERL_UNUSED_VAR(self);
1465  
1466 30002 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 30002 30002
1467 0 XSRETURN_EMPTY;
1468 }
1469  
1470 30002 mac_buf = (unsigned char *)SvPV(mac, mac_len);
1471 30002 ▶ if ( mac_len != crypto_auth_BYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != crypto_auth_BYTES ) { 0 30002 30002
1472 0 croak("Invalid mac");
1473 }
1474  
1475 30002 key_buf = (unsigned char *)SvPV(key, key_len);
1476 30002 ▶ if ( key_len != crypto_auth_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_auth_KEYBYTES ) { 0 30002 30002
1477 0 croak("Invalid key");
1478 }
1479  
1480 30002 msg_buf = (unsigned char *)SvPV(msg, msg_len);
1481  
1482 30002 ▶ if ( crypto_auth_verify( mac_buf, msg_buf, msg_len, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_auth_verify( mac_buf, msg_buf, msg_len, key_buf) == 0 ) { 10004 19998 30002
1483 10004 XSRETURN_YES;
1484 }
1485 else {
1486 19998 XSRETURN_NO;
1487 }
1488 }
1489  
1490  
1491 SV *
1492 hmacsha256_verify(self, mac, msg, key)
1493 SV * self
1494 SV * mac
1495 SV * msg
1496 SV * key
1497 PROTOTYPE: $$$$
1498 ALIAS:
1499 hmacsha512_verify = 2
1500 hmacsha512256_verify = 3
1501 INIT:
1502 STRLEN msg_len;
1503 STRLEN key_len;
1504 STRLEN mac_len;
1505 unsigned char * msg_buf;
1506 unsigned char * mac_buf;
1507 unsigned char * key_buf;
1508 30593 unsigned char * expected = NULL;
1509 unsigned int mac_size;
1510 unsigned int key_size;
1511 30593 int free_expected = 0;
1512 int (*verify_function)(const unsigned char *, const unsigned char *, unsigned long long, const unsigned char *);
1513 CODE:
1514 {
1515 PERL_UNUSED_VAR(self);
1516  
1517 30593 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 30593 30593
1518 0 XSRETURN_EMPTY;
1519 }
1520  
1521 30593 switch(ix) {
1522 30195 case 2:
1523 30195 mac_size = crypto_auth_hmacsha512_BYTES;
1524 30195 key_size = crypto_auth_hmacsha512_KEYBYTES;
1525 30195 verify_function = &crypto_auth_hmacsha512_verify;
1526 30195 break;
1527 199 case 3:
1528 199 mac_size = crypto_auth_hmacsha512256_BYTES;
1529 199 key_size = crypto_auth_hmacsha512256_KEYBYTES;
1530 199 verify_function = &crypto_auth_hmacsha512256_verify;
1531 199 break;
1532 199 default:
1533 199 mac_size = crypto_auth_hmacsha256_BYTES;
1534 199 key_size = crypto_auth_hmacsha256_KEYBYTES;
1535 199 verify_function = &crypto_auth_hmacsha256_verify;
1536 }
1537  
1538 30593 mac_buf = (unsigned char *)SvPV(mac, mac_len);
1539 30593 ▶ if ( mac_len != mac_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != mac_size ) { 0 30593 30593
1540 0 croak("Invalid mac");
1541 }
1542  
1543 30593 ▶ msg_buf = (unsigned char *)SvPV(msg, msg_len);
Branch: 1 branch
BranchTrueFalseTotal
msg_buf = (unsigned char *)SvPV(msg, msg_len); 5 30588 30593
1544  
1545 30593 key_buf = (unsigned char *)SvPV(key, key_len);
1546  
1547 30593 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 582 30011 30593
1548 582 expected = sodium_malloc(mac_size+1);
1549 582 ▶ if ( expected == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( expected == NULL ) { 0 582 582
1550 0 croak("Could not allocate memory");
1551 }
1552 582 free_expected = 1;
1553  
1554 582 switch(ix) {
1555 194 case 2:
1556 {
1557 crypto_auth_hmacsha512_state state_exp;
1558 194 crypto_auth_hmacsha512_init(&state_exp, key_buf, key_len);
1559 194 crypto_auth_hmacsha512_update(&state_exp, msg_buf, msg_len);
1560 194 crypto_auth_hmacsha512_final(&state_exp, expected);
1561 194 break;
1562 }
1563 194 case 3:
1564 {
1565 crypto_auth_hmacsha512256_state state_exp;
1566 194 crypto_auth_hmacsha512256_init(&state_exp, key_buf, key_len);
1567 194 crypto_auth_hmacsha512256_update(&state_exp, msg_buf, msg_len);
1568 194 crypto_auth_hmacsha512256_final(&state_exp, expected);
1569 194 break;
1570 }
1571 194 default:
1572 {
1573 crypto_auth_hmacsha256_state state_exp;
1574 194 crypto_auth_hmacsha256_init(&state_exp, key_buf, key_len);
1575 194 crypto_auth_hmacsha256_update(&state_exp, msg_buf, msg_len);
1576 194 crypto_auth_hmacsha256_final(&state_exp, expected);
1577 }
1578 }
1579  
1580 1164 RETVAL = sodium_memcmp( mac_buf, expected, mac_size ) == 0
1581 582 ▶ ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
? &PL_sv_yes : &PL_sv_no; 582 0 582
1582 } else {
1583 60022 RETVAL = (*verify_function)( mac_buf, msg_buf, msg_len, key_buf ) == 0
1584 30011 ▶ ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
? &PL_sv_yes : &PL_sv_no; 10013 19998 30011
1585 }
1586 }
1587 OUTPUT:
1588 RETVAL
1589 CLEANUP:
1590 30593 ▶ if ( free_expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( free_expected ) { 582 30011 30593
1591 582 sodium_free(expected);
1592 }
1593  
1594 void
1595 hmacsha256_init(self, key)
1596 SV * self
1597 SV * key
1598 PROTOTYPE: $$
1599 INIT:
1600 STRLEN key_len;
1601 unsigned char * key_buf;
1602 CryptNaClSodiumAuthHmacsha256Stream *stream;
1603 PPCODE:
1604 {
1605 PERL_UNUSED_VAR(self);
1606  
1607 197 key_buf = (unsigned char *)SvPV(key, key_len);
1608  
1609 197 Newx(stream, 1, CryptNaClSodiumAuthHmacsha256Stream);
1610 197 stream->state = sodium_malloc(sizeof(crypto_auth_hmacsha256_state));
1611 197 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 197 197
1612 0 croak("Could not allocate memory");
1613 }
1614  
1615 197 crypto_auth_hmacsha256_init(stream->state, key_buf, key_len);
1616  
1617 197 ST(0) = sv_2mortal(AuthHmacsha256Stream2SV(aTHX_ stream));
1618  
1619 197 XSRETURN(1);
1620 }
1621  
1622 void
1623 hmacsha512_init(self, key)
1624 SV * self
1625 SV * key
1626 PROTOTYPE: $$
1627 INIT:
1628 STRLEN key_len;
1629 unsigned char * key_buf;
1630 CryptNaClSodiumAuthHmacsha512Stream *stream;
1631 PPCODE:
1632 {
1633 PERL_UNUSED_VAR(self);
1634  
1635 197 key_buf = (unsigned char *)SvPV(key, key_len);
1636  
1637 197 Newx(stream, 1, CryptNaClSodiumAuthHmacsha512Stream);
1638 197 stream->state = sodium_malloc(sizeof(crypto_auth_hmacsha512_state));
1639 197 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 197 197
1640 0 croak("Could not allocate memory");
1641 }
1642  
1643 197 crypto_auth_hmacsha512_init(stream->state, key_buf, key_len);
1644  
1645 197 ST(0) = sv_2mortal(AuthHmacsha512Stream2SV(aTHX_ stream));
1646  
1647 197 XSRETURN(1);
1648 }
1649  
1650  
1651 void
1652 hmacsha512256_init(self, key)
1653 SV * self
1654 SV * key
1655 PROTOTYPE: $$
1656 INIT:
1657 STRLEN key_len;
1658 unsigned char * key_buf;
1659 CryptNaClSodiumAuthHmacsha512256Stream *stream;
1660 PPCODE:
1661 {
1662 PERL_UNUSED_VAR(self);
1663  
1664 199 key_buf = (unsigned char *)SvPV(key, key_len);
1665  
1666 199 Newx(stream, 1, CryptNaClSodiumAuthHmacsha512256Stream);
1667 199 stream->state = sodium_malloc(sizeof(crypto_auth_hmacsha512256_state));
1668 199 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 199 199
1669 0 croak("Could not allocate memory");
1670 }
1671  
1672 199 crypto_auth_hmacsha512256_init(stream->state, key_buf, key_len);
1673  
1674 199 ST(0) = sv_2mortal(AuthHmacsha512256Stream2SV(aTHX_ stream));
1675  
1676 199 XSRETURN(1);
1677 }
1678  
1679 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::auth::hmacsha256stream
1680  
1681 void
1682 clone(self)
1683 SV * self
1684 PREINIT:
1685 0 CryptNaClSodiumAuthHmacsha256Stream* cur_stream = GetAuthHmacsha256Stream(aTHX_ self);
1686 INIT:
1687 CryptNaClSodiumAuthHmacsha256Stream* new_stream;
1688 PPCODE:
1689 {
1690 0 ▶ CLONESTATE(CryptNaClSodiumAuthHmacsha256Stream, auth_hmacsha256, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumAuthHmacsha256Stream, auth_hmacsha256, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumAuthHmacsha256Stream, auth_hmacsha256, 0, ((void)0)) 0 0 0
1691 0 ST(0) = sv_2mortal(AuthHmacsha256Stream2SV(aTHX_ new_stream));
1692 0 XSRETURN(1);
1693 }
1694  
1695  
1696 void
1697 update(self, ...)
1698 SV * self
1699 PREINIT:
1700 249 CryptNaClSodiumAuthHmacsha256Stream* stream = GetAuthHmacsha256Stream(aTHX_ self);
1701 INIT:
1702 STRLEN msg_len;
1703 unsigned char * msg_buf;
1704 int i;
1705 PPCODE:
1706 {
1707 498 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 249 249 498
1708 249 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
1709  
1710 249 crypto_auth_hmacsha256_update(stream->state, msg_buf, msg_len);
1711 }
1712  
1713 249 XSRETURN(1);
1714 }
1715  
1716  
1717 SV *
1718 final(self)
1719 SV * self
1720 PROTOTYPE: $
1721 PREINIT:
1722 197 CryptNaClSodiumAuthHmacsha256Stream* stream = GetAuthHmacsha256Stream(aTHX_ self);
1723 INIT:
1724 DataBytesLocker *bl;
1725 CODE:
1726 {
1727 197 bl = InitDataBytesLocker(aTHX_ crypto_auth_hmacsha256_BYTES);
1728  
1729 197 crypto_auth_hmacsha256_final(stream->state, bl->bytes);
1730  
1731 197 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1732 }
1733 OUTPUT:
1734 RETVAL
1735  
1736 void
1737 DESTROY(self)
1738 SV * self
1739 PREINIT:
1740 197 CryptNaClSodiumAuthHmacsha256Stream* stream = GetAuthHmacsha256Stream(aTHX_ self);
1741 PPCODE:
1742 {
1743 197 sodium_free( stream->state );
1744 197 Safefree(stream);
1745 }
1746  
1747 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::auth::hmacsha512stream
1748  
1749 void
1750 clone(self)
1751 SV * self
1752 PREINIT:
1753 0 CryptNaClSodiumAuthHmacsha512Stream* cur_stream = GetAuthHmacsha512Stream(aTHX_ self);
1754 INIT:
1755 CryptNaClSodiumAuthHmacsha512Stream* new_stream;
1756 PPCODE:
1757 {
1758 0 ▶ CLONESTATE(CryptNaClSodiumAuthHmacsha512Stream, auth_hmacsha512, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumAuthHmacsha512Stream, auth_hmacsha512, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumAuthHmacsha512Stream, auth_hmacsha512, 0, ((void)0)) 0 0 0
1759 0 ST(0) = sv_2mortal(AuthHmacsha512Stream2SV(aTHX_ new_stream));
1760 0 XSRETURN(1);
1761 }
1762  
1763  
1764 void
1765 update(self, ...)
1766 SV * self
1767 PREINIT:
1768 249 CryptNaClSodiumAuthHmacsha512Stream* stream = GetAuthHmacsha512Stream(aTHX_ self);
1769 INIT:
1770 STRLEN msg_len;
1771 unsigned char * msg_buf;
1772 int i;
1773 PPCODE:
1774 {
1775 498 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 249 249 498
1776 249 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
1777  
1778 249 crypto_auth_hmacsha512_update(stream->state, msg_buf, msg_len);
1779 }
1780  
1781 249 XSRETURN(1);
1782 }
1783  
1784 SV *
1785 final(self)
1786 SV * self
1787 PROTOTYPE: $
1788 PREINIT:
1789 197 CryptNaClSodiumAuthHmacsha512Stream* stream = GetAuthHmacsha512Stream(aTHX_ self);
1790 INIT:
1791 DataBytesLocker *bl;
1792 CODE:
1793 {
1794 197 bl = InitDataBytesLocker(aTHX_ crypto_auth_hmacsha512_BYTES);
1795  
1796 197 crypto_auth_hmacsha512_final(stream->state, bl->bytes);
1797  
1798 197 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1799 }
1800 OUTPUT:
1801 RETVAL
1802  
1803  
1804 void
1805 DESTROY(self)
1806 SV * self
1807 PREINIT:
1808 197 CryptNaClSodiumAuthHmacsha512Stream* stream = GetAuthHmacsha512Stream(aTHX_ self);
1809 PPCODE:
1810 {
1811 197 sodium_free( stream->state );
1812 197 Safefree(stream);
1813 }
1814  
1815  
1816 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::auth::hmacsha512256stream
1817  
1818 void
1819 clone(self)
1820 SV * self
1821 PREINIT:
1822 0 CryptNaClSodiumAuthHmacsha512256Stream* cur_stream = GetAuthHmacsha512256Stream(aTHX_ self);
1823 INIT:
1824 CryptNaClSodiumAuthHmacsha512256Stream* new_stream;
1825 PPCODE:
1826 {
1827 0 ▶ CLONESTATE(CryptNaClSodiumAuthHmacsha512256Stream, auth_hmacsha512256, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumAuthHmacsha512256Stream, auth_hmacsha512256, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumAuthHmacsha512256Stream, auth_hmacsha512256, 0, ((void)0)) 0 0 0
1828 0 ST(0) = sv_2mortal(AuthHmacsha512256Stream2SV(aTHX_ new_stream));
1829 0 XSRETURN(1);
1830 }
1831  
1832  
1833 void
1834 update(self, ...)
1835 SV * self
1836 PREINIT:
1837 277 CryptNaClSodiumAuthHmacsha512256Stream* stream = GetAuthHmacsha512256Stream(aTHX_ self);
1838 INIT:
1839 STRLEN msg_len;
1840 unsigned char * msg_buf;
1841 int i;
1842 PPCODE:
1843 {
1844 554 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 277 277 554
1845 277 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
1846  
1847 277 crypto_auth_hmacsha512256_update(stream->state, msg_buf, msg_len);
1848 }
1849  
1850 277 XSRETURN(1);
1851 }
1852  
1853 SV *
1854 final(self)
1855 SV * self
1856 PROTOTYPE: $
1857 PREINIT:
1858 199 CryptNaClSodiumAuthHmacsha512256Stream* stream = GetAuthHmacsha512256Stream(aTHX_ self);
1859 INIT:
1860 DataBytesLocker *bl;
1861 CODE:
1862 {
1863 199 bl = InitDataBytesLocker(aTHX_ crypto_auth_hmacsha512256_BYTES);
1864  
1865 199 crypto_auth_hmacsha512256_final(stream->state, bl->bytes);
1866  
1867 199 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1868 }
1869 OUTPUT:
1870 RETVAL
1871  
1872  
1873 void
1874 DESTROY(self)
1875 SV * self
1876 PREINIT:
1877 199 CryptNaClSodiumAuthHmacsha512256Stream* stream = GetAuthHmacsha512256Stream(aTHX_ self);
1878 PPCODE:
1879 {
1880 199 sodium_free( stream->state );
1881 199 Safefree(stream);
1882 }
1883  
1884  
1885 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::aead
1886  
1887 PROTOTYPES: DISABLE
1888  
1889 unsigned int
1890 KEYBYTES(...)
1891 CODE:
1892 3 ▶ RETVAL = crypto_aead_chacha20poly1305_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_chacha20poly1305_KEYBYTES; 0 3 3
1893 OUTPUT:
1894 RETVAL
1895  
1896 unsigned int
1897 AES256GCM_KEYBYTES(...)
1898 CODE:
1899 #if defined(AES256GCM_IS_AVAILABLE)
1900 384 ▶ RETVAL = crypto_aead_aes256gcm_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_aes256gcm_KEYBYTES; 382 2 384
1901 #else
1902 croak("AES256-GCM is not supported by this CPU");
1903 #endif
1904 OUTPUT:
1905 RETVAL
1906  
1907 unsigned int
1908 AES256GCM_NPUBBYTES(...)
1909 CODE:
1910 #if defined(AES256GCM_IS_AVAILABLE)
1911 384 ▶ RETVAL = crypto_aead_aes256gcm_NPUBBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_aes256gcm_NPUBBYTES; 382 2 384
1912 #else
1913 croak("AES256-GCM is not supported by this CPU");
1914 #endif
1915 OUTPUT:
1916 RETVAL
1917  
1918 unsigned int
1919 AES256GCM_ABYTES(...)
1920 CODE:
1921 #if defined(AES256GCM_IS_AVAILABLE)
1922 1761 ▶ RETVAL = crypto_aead_aes256gcm_ABYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_aes256gcm_ABYTES; 1756 5 1761
1923 #else
1924 croak("AES256-GCM is not supported by this CPU");
1925 #endif
1926 OUTPUT:
1927 RETVAL
1928  
1929 unsigned int
1930 NPUBBYTES(...)
1931 CODE:
1932 2 ▶ RETVAL = crypto_aead_chacha20poly1305_NPUBBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_chacha20poly1305_NPUBBYTES; 1 1 2
1933 OUTPUT:
1934 RETVAL
1935  
1936 unsigned int
1937 IETF_NPUBBYTES(...)
1938 CODE:
1939 1 ▶ RETVAL = crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_chacha20poly1305_IETF_NPUBBYTES; 0 1 1
1940 OUTPUT:
1941 RETVAL
1942  
1943 unsigned int
1944 ABYTES(...)
1945 CODE:
1946 7 ▶ RETVAL = crypto_aead_chacha20poly1305_ABYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_aead_chacha20poly1305_ABYTES; 1 6 7
1947 OUTPUT:
1948 RETVAL
1949  
1950 PROTOTYPES: ENABLE
1951  
1952 void
1953 aes256gcm_is_available(self)
1954 SV * self
1955 PPCODE:
1956 {
1957 1 ▶ if ( crypto_aead_aes256gcm_is_available() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_aead_aes256gcm_is_available() ) { 1 0 1
1958 1 XSRETURN_YES;
1959 }
1960 0 XSRETURN_NO;
1961 }
1962  
1963 SV *
1964 keygen(self)
1965 SV * self
1966 ALIAS:
1967 aes256gcm_keygen = 1
1968 INIT:
1969 unsigned int key_size;
1970 DataBytesLocker *bl;
1971 CODE:
1972 {
1973 PERL_UNUSED_VAR(self);
1974  
1975 3 ▶ switch(ix) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ix) { 0 3 3
1976 0 case 1:
1977 #if defined(AES256GCM_IS_AVAILABLE)
1978 0 key_size = crypto_aead_aes256gcm_KEYBYTES;
1979 #else
1980 croak("AES256-GCM is not supported by this CPU");
1981 #endif
1982 0 break;
1983 3 default:
1984 3 key_size = crypto_aead_chacha20poly1305_KEYBYTES;
1985 }
1986 3 bl = InitDataBytesLocker(aTHX_ key_size);
1987 3 randombytes_buf(bl->bytes, key_size);
1988 3 RETVAL = DataBytesLocker2SV(aTHX_ bl);
1989 }
1990 OUTPUT:
1991 RETVAL
1992  
1993 SV *
1994 nonce(self, ...)
1995 SV * self
1996 PROTOTYPE: $;$
1997 ALIAS:
1998 ietf_nonce = 1
1999 aes256gcm_nonce = 2
2000 INIT:
2001 unsigned int nonce_size;
2002 DataBytesLocker *bl;
2003 CODE:
2004 PERL_UNUSED_VAR(self);
2005  
2006 4 switch(ix) {
2007 0 case 1:
2008 0 nonce_size = crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
2009 0 break;
2010 0 case 2:
2011 #if defined(AES256GCM_IS_AVAILABLE)
2012 0 nonce_size = crypto_aead_aes256gcm_NPUBBYTES;
2013 #else
2014 croak("AES256-GCM is not supported by this CPU");
2015 #endif
2016 0 break;
2017 4 default:
2018 4 nonce_size = crypto_aead_chacha20poly1305_NPUBBYTES;
2019 }
2020  
2021 4 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 4 4
2022 0 croak("Invalid number of arguments");
2023 }
2024  
2025 4 ▶ if (items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2 ) { 0 4 4
2026 0 ▶ if ( SvOK(ST(1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(ST(1)) ) { 0 0 0
2027 STRLEN prev_nonce_len;
2028 0 unsigned char * prev_nonce = (unsigned char *)SvPV(ST(1), prev_nonce_len);
2029  
2030 0 ▶ if ( prev_nonce_len > nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev_nonce_len > nonce_size ) { 0 0 0
2031 0 croak("Base nonce too long");
2032 }
2033  
2034 0 bl = InitDataBytesLocker(aTHX_ nonce_size);
2035 0 memcpy(bl->bytes, prev_nonce, prev_nonce_len);
2036 0 sodium_memzero(bl->bytes + prev_nonce_len, bl->length - prev_nonce_len);
2037 }
2038 else {
2039 0 croak("Base nonce invalid");
2040 }
2041 }
2042 else {
2043 4 bl = InitDataBytesLocker(aTHX_ nonce_size);
2044 4 randombytes_buf(bl->bytes, bl->length);
2045 }
2046 4 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2047 OUTPUT:
2048 RETVAL
2049  
2050 void
2051 encrypt(self, msg, adata, nonce, key)
2052 SV * self
2053 SV * msg
2054 SV * adata
2055 SV * nonce
2056 SV * key
2057 PROTOTYPE: $$$$$
2058 ALIAS:
2059 ietf_encrypt = 1
2060 aes256gcm_encrypt = 2
2061 INIT:
2062 STRLEN msg_len;
2063 STRLEN adata_len;
2064 STRLEN nonce_len;
2065 STRLEN key_len;
2066 STRLEN enc_len;
2067 unsigned char * msg_buf;
2068 unsigned char * adata_buf;
2069 unsigned char * nonce_buf;
2070 unsigned char * key_buf;
2071 unsigned int nonce_size;
2072 unsigned int adlen_size;
2073 unsigned int key_size;
2074 int (*encrypt_function)(unsigned char *, unsigned long long *, const unsigned char *, unsigned long long,
2075 const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *, const unsigned char *);
2076 DataBytesLocker *bl;
2077 PPCODE:
2078 {
2079 PERL_UNUSED_VAR(self);
2080  
2081 391 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 391 391
2082 0 XSRETURN_EMPTY;
2083 }
2084  
2085 391 switch(ix) {
2086 2 case 1:
2087 2 nonce_size = crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
2088 2 key_size = crypto_aead_chacha20poly1305_KEYBYTES;
2089 2 adlen_size = crypto_aead_chacha20poly1305_ABYTES;
2090 2 encrypt_function = &crypto_aead_chacha20poly1305_ietf_encrypt;
2091 2 break;
2092 383 case 2:
2093 #if defined(AES256GCM_IS_AVAILABLE)
2094 383 nonce_size = crypto_aead_aes256gcm_NPUBBYTES;
2095 383 key_size = crypto_aead_aes256gcm_KEYBYTES;
2096 383 adlen_size = crypto_aead_aes256gcm_ABYTES;
2097 383 encrypt_function = &crypto_aead_aes256gcm_encrypt;
2098 #else
2099 croak("AES256-GCM is not supported by this CPU");
2100 #endif
2101 383 break;
2102 6 default:
2103 6 nonce_size = crypto_aead_chacha20poly1305_NPUBBYTES;
2104 6 key_size = crypto_aead_chacha20poly1305_KEYBYTES;
2105 6 adlen_size = crypto_aead_chacha20poly1305_ABYTES;
2106 6 encrypt_function = &crypto_aead_chacha20poly1305_encrypt;
2107 }
2108  
2109 391 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2110 391 ▶ if ( nonce_len != nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != nonce_size ) { 0 391 391
2111 0 croak("Invalid nonce");
2112 }
2113  
2114 391 key_buf = (unsigned char *)SvPV(key, key_len);
2115 391 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 391 391
2116 0 croak("Invalid key");
2117 }
2118  
2119 391 ▶ msg_buf = (unsigned char *)SvPV(msg, msg_len);
Branch: 1 branch
BranchTrueFalseTotal
msg_buf = (unsigned char *)SvPV(msg, msg_len); 391 0 391
2120  
2121 391 adata_buf = (unsigned char *)SvPV(adata, adata_len);
2122  
2123 391 ▶ if (msg_len > SIZE_MAX - adlen_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg_len > SIZE_MAX - adlen_size) { 0 391 391
2124 0 croak("Encrypted length exceeds system memory limit (size_t overflow)");
2125 }
2126 391 enc_len = msg_len + adlen_size;
2127 391 bl = InitDataBytesLocker(aTHX_ enc_len);
2128  
2129 391 (*encrypt_function)( bl->bytes, NULL, msg_buf, msg_len,
2130 adata_buf, adata_len, NULL, nonce_buf, key_buf);
2131  
2132 391 bl->bytes[enc_len] = '\0';
2133  
2134 391 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 391 391
2135 391 XSRETURN(1);
2136 }
2137  
2138 void
2139 decrypt(self, msg, adata, nonce, key)
2140 SV * self
2141 SV * msg
2142 SV * adata
2143 SV * nonce
2144 SV * key
2145 PROTOTYPE: $$$$
2146 ALIAS:
2147 ietf_decrypt = 1
2148 aes256gcm_decrypt = 2
2149 INIT:
2150 STRLEN msg_len;
2151 STRLEN adata_len;
2152 STRLEN nonce_len;
2153 STRLEN key_len;
2154 STRLEN enc_len;
2155 unsigned char * msg_buf;
2156 unsigned char * adata_buf;
2157 unsigned char * nonce_buf;
2158 unsigned char * key_buf;
2159 unsigned int nonce_size;
2160 unsigned int adlen_size;
2161 unsigned int key_size;
2162 int (*decrypt_function)(unsigned char *, unsigned long long *, unsigned char *, const unsigned char *, unsigned long long,
2163 const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *);
2164 DataBytesLocker *bl;
2165 PPCODE:
2166 {
2167 PERL_UNUSED_VAR(self);
2168  
2169 1236 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 1236 1236
2170 0 XSRETURN_EMPTY;
2171 }
2172  
2173 1236 switch(ix) {
2174 132 case 1:
2175 132 nonce_size = crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
2176 132 key_size = crypto_aead_chacha20poly1305_KEYBYTES;
2177 132 adlen_size = crypto_aead_chacha20poly1305_ABYTES;
2178 132 decrypt_function = &crypto_aead_chacha20poly1305_ietf_decrypt;
2179 132 break;
2180 1072 case 2:
2181 #if defined(AES256GCM_IS_AVAILABLE)
2182 1072 nonce_size = crypto_aead_aes256gcm_NPUBBYTES;
2183 1072 key_size = crypto_aead_aes256gcm_KEYBYTES;
2184 1072 adlen_size = crypto_aead_aes256gcm_ABYTES;
2185 1072 decrypt_function = &crypto_aead_aes256gcm_decrypt;
2186 #else
2187 croak("AES256-GCM is not supported by this CPU");
2188 #endif
2189 1072 break;
2190 32 default:
2191 32 nonce_size = crypto_aead_chacha20poly1305_NPUBBYTES;
2192 32 key_size = crypto_aead_chacha20poly1305_KEYBYTES;
2193 32 adlen_size = crypto_aead_chacha20poly1305_ABYTES;
2194 32 decrypt_function = &crypto_aead_chacha20poly1305_decrypt;
2195 }
2196  
2197 1236 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2198 1236 ▶ if ( nonce_len != nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != nonce_size ) { 0 1236 1236
2199 0 croak("Invalid nonce");
2200 }
2201  
2202 1236 key_buf = (unsigned char *)SvPV(key, key_len);
2203 1236 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 1236 1236
2204 0 croak("Invalid key");
2205 }
2206  
2207 1236 msg_buf = (unsigned char *)SvPV(msg, msg_len);
2208  
2209 1236 ▶ if ( msg_len < adlen_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < adlen_size ) { 383 853 1236
2210 383 croak("Invalid ciphertext");
2211 }
2212  
2213 853 adata_buf = (unsigned char *)SvPV(adata, adata_len);
2214  
2215 853 enc_len = msg_len - adlen_size;
2216 853 bl = InitDataBytesLocker(aTHX_ enc_len);
2217  
2218 853 ▶ if ( (*decrypt_function)( bl->bytes, NULL, NULL, msg_buf, msg_len, adata_buf, adata_len, nonce_buf, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (*decrypt_function)( bl->bytes, NULL, NULL, msg_buf, msg_len, adata_buf, adata_len, nonce_buf, key_buf) == 0 ) { 391 462 853
2219  
2220 391 bl->bytes[enc_len] = '\0';
2221 391 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 391 391
2222 391 XSRETURN(1);
2223 }
2224 else {
2225 462 sodium_free(bl->bytes);
2226 462 Safefree(bl);
2227 462 croak("Message forged");
2228 }
2229 }
2230  
2231 void
2232 aes256gcm_beforenm(self, key)
2233 SV * self
2234 SV * key
2235 PROTOTYPE: $;%
2236 INIT:
2237 384 STRLEN key_len = 0;
2238 384 unsigned char * key_buf = NULL;
2239 #if defined(AES256GCM_IS_AVAILABLE)
2240 CryptNaClSodiumAeadAes256gcmState *state;
2241 #endif
2242 PPCODE:
2243 {
2244 PERL_UNUSED_VAR(self);
2245 #if defined(AES256GCM_IS_AVAILABLE)
2246 384 key_buf = (unsigned char *)SvPV(key, key_len);
2247 384 ▶ if ( key_len != crypto_aead_aes256gcm_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_aead_aes256gcm_KEYBYTES ) { 0 384 384
2248 0 croak("Invalid key");
2249 }
2250  
2251 384 state = InitAeadAes256gcmState(aTHX_ key_buf);
2252  
2253 384 ST(0) = sv_2mortal(AeadAes256gcmState2SV(aTHX_ state));
2254  
2255 384 XSRETURN(1);
2256 #else
2257 croak("AES256-GCM is not supported by this CPU");
2258 #endif
2259 }
2260  
2261 void
2262 aes256gcm_encrypt_afternm(self, msg, adata, nonce, precalculated_key)
2263 SV * self
2264 SV * msg
2265 SV * adata
2266 SV * nonce
2267 SV * precalculated_key
2268 PROTOTYPE: $$$$$
2269 INIT:
2270 STRLEN msg_len;
2271 STRLEN adata_len;
2272 STRLEN nonce_len;
2273 STRLEN enc_len;
2274 unsigned char * msg_buf;
2275 unsigned char * adata_buf;
2276 unsigned char * nonce_buf;
2277 #if defined(AES256GCM_IS_AVAILABLE)
2278 CryptNaClSodiumAeadAes256gcmState * precal_key;
2279 #endif
2280 DataBytesLocker *bl;
2281 PPCODE:
2282 {
2283 PERL_UNUSED_VAR(self);
2284 #if defined(AES256GCM_IS_AVAILABLE)
2285  
2286 385 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 385 385
2287 0 XSRETURN_EMPTY;
2288 }
2289  
2290 385 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2291 385 ▶ if ( nonce_len != crypto_aead_aes256gcm_NPUBBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_aead_aes256gcm_NPUBBYTES ) { 0 385 385
2292 0 croak("Invalid nonce");
2293 }
2294  
2295 385 precal_key = GetAeadAes256gcmState(aTHX_ precalculated_key);
2296  
2297 385 ▶ if ( precal_key->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( precal_key->locked ) { 1 384 385
2298 1 croak("Unlock AES256GCM precalculated key object before accessing the state");
2299 }
2300  
2301 384 ▶ msg_buf = (unsigned char *)SvPV(msg, msg_len);
Branch: 1 branch
BranchTrueFalseTotal
msg_buf = (unsigned char *)SvPV(msg, msg_len); 384 0 384
2302  
2303 384 adata_buf = (unsigned char *)SvPV(adata, adata_len);
2304  
2305 384 ▶ if (msg_len > SIZE_MAX - crypto_aead_aes256gcm_ABYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg_len > SIZE_MAX - crypto_aead_aes256gcm_ABYTES) { 0 384 384
2306 0 croak("Encrypted length exceeds system memory limit (size_t overflow)");
2307 }
2308 384 enc_len = msg_len + crypto_aead_aes256gcm_ABYTES;
2309 384 bl = InitDataBytesLocker(aTHX_ enc_len);
2310  
2311 384 crypto_aead_aes256gcm_encrypt_afternm( bl->bytes, NULL, msg_buf, msg_len,
2312 384 adata_buf, adata_len, NULL, nonce_buf, (const crypto_aead_aes256gcm_state *)precal_key->ctx);
2313  
2314 384 bl->bytes[enc_len] = '\0';
2315  
2316 384 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 384 384
2317  
2318 384 XSRETURN(1);
2319 #else
2320 croak("AES256-GCM is not supported by this CPU");
2321 #endif
2322 }
2323  
2324 void
2325 aes256gcm_decrypt_afternm(self, msg, adata, nonce, precalculated_key)
2326 SV * self
2327 SV * msg
2328 SV * adata
2329 SV * nonce
2330 SV * precalculated_key
2331 PROTOTYPE: $$$$
2332 INIT:
2333 STRLEN msg_len;
2334 STRLEN adata_len;
2335 STRLEN nonce_len;
2336 STRLEN enc_len;
2337 unsigned char * msg_buf;
2338 unsigned char * adata_buf;
2339 unsigned char * nonce_buf;
2340 #if defined(AES256GCM_IS_AVAILABLE)
2341 CryptNaClSodiumAeadAes256gcmState * precal_key;
2342 #endif
2343 DataBytesLocker *bl;
2344 PPCODE:
2345 {
2346 PERL_UNUSED_VAR(self);
2347 #if defined(AES256GCM_IS_AVAILABLE)
2348  
2349 386 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 386 386
2350 0 XSRETURN_EMPTY;
2351 }
2352  
2353 386 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2354 386 ▶ if ( nonce_len != crypto_aead_aes256gcm_NPUBBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_aead_aes256gcm_NPUBBYTES ) { 0 386 386
2355 0 croak("Invalid nonce");
2356 }
2357  
2358 386 msg_buf = (unsigned char *)SvPV(msg, msg_len);
2359  
2360 386 ▶ if ( msg_len < crypto_aead_aes256gcm_ABYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < crypto_aead_aes256gcm_ABYTES ) { 0 386 386
2361 0 croak("Invalid ciphertext");
2362 }
2363  
2364 386 precal_key = GetAeadAes256gcmState(aTHX_ precalculated_key);
2365  
2366 386 ▶ if ( precal_key->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( precal_key->locked ) { 1 385 386
2367 1 croak("Unlock AES256GCM precalculated key object before accessing the state");
2368 }
2369  
2370 385 adata_buf = (unsigned char *)SvPV(adata, adata_len);
2371  
2372 385 enc_len = msg_len - crypto_aead_aes256gcm_ABYTES;
2373 385 bl = InitDataBytesLocker(aTHX_ enc_len);
2374  
2375 385 ▶ if ( crypto_aead_aes256gcm_decrypt_afternm( bl->bytes, NULL, NULL, msg_buf, msg_len, adata_buf, adata_len, nonce_buf, (const crypto_aead_aes256gcm_state *) precal_key->ctx) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_aead_aes256gcm_decrypt_afternm( bl->bytes, NULL, NULL, msg_buf, msg_len, adata_buf, adata_len, nonce_buf, (const crypto_aead_aes256gcm_state *) precal_key->ctx) == 0 ) { 385 0 385
2376  
2377 385 bl->bytes[enc_len] = '\0';
2378 385 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 385 385
2379 385 XSRETURN(1);
2380 }
2381 else {
2382 0 sodium_free(bl->bytes);
2383 0 Safefree(bl);
2384 0 croak("Message forged");
2385 }
2386 #else
2387 croak("AES256-GCM is not supported by this CPU");
2388 #endif
2389 }
2390  
2391 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::aead::aes256gcmstate
2392  
2393 void
2394 lock(self)
2395 SV * self
2396 PPCODE:
2397 {
2398 int rc;
2399 #if defined(AES256GCM_IS_AVAILABLE)
2400 CryptNaClSodiumAeadAes256gcmState* state;
2401  
2402 1 state = GetAeadAes256gcmState(aTHX_ self);
2403  
2404 1 rc = sodium_mprotect_noaccess((void *)state->ctx);
2405  
2406 1 ▶ if (rc == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0 ) { 1 0 1
2407 1 state->locked = 1;
2408 1 XSRETURN_YES;
2409 }
2410  
2411 0 croak("Unable to lock memory: %s", Strerror(errno));
2412 #else
2413 croak("AES256-GCM is not supported by this CPU");
2414 #endif
2415 }
2416  
2417 void
2418 unlock(self)
2419 SV * self
2420 PPCODE:
2421 {
2422 int rc;
2423 #if defined(AES256GCM_IS_AVAILABLE)
2424 CryptNaClSodiumAeadAes256gcmState* state;
2425  
2426 1 state = GetAeadAes256gcmState(aTHX_ self);
2427  
2428 1 rc = sodium_mprotect_readonly((void *)state->ctx);
2429  
2430 1 ▶ if (rc == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0 ) { 1 0 1
2431 1 state->locked = 0;
2432 1 XSRETURN_YES;
2433 }
2434 0 croak("Unable to unlock memory: %s", Strerror(errno));
2435 #else
2436 croak("AES256-GCM is not supported by this CPU");
2437 #endif
2438 }
2439  
2440 void
2441 is_locked(self, ...)
2442 SV * self
2443 PPCODE:
2444 {
2445 #if defined(AES256GCM_IS_AVAILABLE)
2446 CryptNaClSodiumAeadAes256gcmState* state;
2447  
2448 3 state = GetAeadAes256gcmState(aTHX_ self);
2449 3 ▶ if ( state->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( state->locked ) { 1 2 3
2450 1 XSRETURN_YES;
2451 } else {
2452 2 XSRETURN_NO;
2453 }
2454 #else
2455 croak("AES256-GCM is not supported by this CPU");
2456 #endif
2457 }
2458  
2459 void
2460 DESTROY(self)
2461 SV * self
2462 PPCODE:
2463 {
2464 #if defined(AES256GCM_IS_AVAILABLE)
2465 CryptNaClSodiumAeadAes256gcmState* state;
2466 384 state = GetAeadAes256gcmState(aTHX_ self);
2467 384 sodium_free( state->ctx );
2468 384 Safefree(state);
2469 #else
2470 croak("AES256-GCM is not supported by this CPU");
2471 #endif
2472 }
2473  
2474  
2475 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::box
2476  
2477 PROTOTYPES: DISABLE
2478  
2479 unsigned int
2480 PUBLICKEYBYTES(...)
2481 CODE:
2482 1 ▶ RETVAL = crypto_box_PUBLICKEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_PUBLICKEYBYTES; 0 1 1
2483 OUTPUT:
2484 RETVAL
2485  
2486 unsigned int
2487 SECRETKEYBYTES(...)
2488 CODE:
2489 1 ▶ RETVAL = crypto_box_SECRETKEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_SECRETKEYBYTES; 1 0 1
2490 OUTPUT:
2491 RETVAL
2492  
2493 unsigned int
2494 NONCEBYTES(...)
2495 CODE:
2496 1 ▶ RETVAL = crypto_box_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_NONCEBYTES; 1 0 1
2497 OUTPUT:
2498 RETVAL
2499  
2500 unsigned int
2501 MACBYTES(...)
2502 CODE:
2503 6 ▶ RETVAL = crypto_box_MACBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_MACBYTES; 1 5 6
2504 OUTPUT:
2505 RETVAL
2506  
2507 unsigned int
2508 SEEDBYTES(...)
2509 CODE:
2510 1 ▶ RETVAL = crypto_box_SEEDBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_SEEDBYTES; 1 0 1
2511 OUTPUT:
2512 RETVAL
2513  
2514 unsigned int
2515 BEFORENMBYTES(...)
2516 CODE:
2517 1 ▶ RETVAL = crypto_box_BEFORENMBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_box_BEFORENMBYTES; 1 0 1
2518 OUTPUT:
2519 RETVAL
2520  
2521 PROTOTYPES: ENABLE
2522  
2523 void
2524 keypair(self, ...)
2525 SV * self
2526 PROTOTYPE: $;$
2527 INIT:
2528 DataBytesLocker *blp;
2529 DataBytesLocker *bls;
2530 PPCODE:
2531 {
2532 PERL_UNUSED_VAR(self);
2533  
2534 12 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 12 12
2535 0 croak("Invalid number of arguments");
2536 }
2537 /* from seed */
2538 12 ▶ else if ( items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( items == 2 ) { 4 8 12
2539 8 ▶ if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) {
Branch: 3 branches
BranchTrueFalseTotal
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 0 4
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 0 4
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 0 4
2540 STRLEN seed_len;
2541 4 unsigned char * seed_buf = (unsigned char *)SvPV(ST(1), seed_len);
2542  
2543 4 ▶ if ( seed_len != crypto_box_SEEDBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( seed_len != crypto_box_SEEDBYTES ) { 0 4 4
2544 0 croak("Invalid seed length: %" UVuf, (UV) seed_len);
2545 }
2546  
2547 4 blp = InitDataBytesLocker(aTHX_ crypto_box_PUBLICKEYBYTES);
2548 4 bls = InitDataBytesLocker(aTHX_ crypto_box_SECRETKEYBYTES);
2549  
2550 4 crypto_box_seed_keypair(blp->bytes, bls->bytes, seed_buf);
2551 } else {
2552 0 croak("Invalid seed");
2553 }
2554 }
2555 /* regular */
2556 else {
2557 8 blp = InitDataBytesLocker(aTHX_ crypto_box_PUBLICKEYBYTES);
2558 8 bls = InitDataBytesLocker(aTHX_ crypto_box_SECRETKEYBYTES);
2559 8 crypto_box_keypair(blp->bytes, bls->bytes);
2560 }
2561 12 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ blp) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ blp) ); 0 12 12
2562 12 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bls) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bls) ); 0 12 12
2563 12 XSRETURN(2);
2564 }
2565  
2566 SV *
2567 public_key(self, seckey)
2568 SV * self
2569 SV * seckey
2570 INIT:
2571 DataBytesLocker *bl;
2572 STRLEN skey_len;
2573 unsigned char * skey_buf;
2574 CODE:
2575 {
2576 PERL_UNUSED_VAR(self);
2577  
2578 4 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
2579 4 ▶ if ( skey_len != crypto_box_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_box_SECRETKEYBYTES ) { 0 4 4
2580 0 croak("Invalid secret key");
2581 }
2582 4 bl = InitDataBytesLocker(aTHX_ crypto_box_PUBLICKEYBYTES);
2583  
2584 4 crypto_scalarmult_base(bl->bytes, skey_buf);
2585  
2586 4 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2587 }
2588 OUTPUT:
2589 RETVAL
2590  
2591  
2592  
2593 SV *
2594 seed(self)
2595 SV * self
2596 INIT:
2597 DataBytesLocker *bl;
2598 CODE:
2599 {
2600 PERL_UNUSED_VAR(self);
2601  
2602 2 bl = InitDataBytesLocker(aTHX_ crypto_box_SEEDBYTES);
2603 2 randombytes_buf(bl->bytes, bl->length);
2604  
2605 2 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2606 }
2607 OUTPUT:
2608 RETVAL
2609  
2610  
2611 SV *
2612 beforenm(self, pubkey, seckey)
2613 SV * self
2614 SV * pubkey
2615 SV * seckey
2616 INIT:
2617 STRLEN pkey_len;
2618 STRLEN skey_len;
2619 unsigned char * pkey_buf;
2620 unsigned char * skey_buf;
2621 DataBytesLocker *bl;
2622 CODE:
2623 {
2624 PERL_UNUSED_VAR(self);
2625  
2626 8 pkey_buf = (unsigned char *)SvPV(pubkey, pkey_len);
2627 8 ▶ if ( pkey_len != crypto_box_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_box_PUBLICKEYBYTES ) { 0 8 8
2628 0 croak("Invalid public key");
2629 }
2630  
2631 8 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
2632 8 ▶ if ( skey_len != crypto_box_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_box_SECRETKEYBYTES ) { 0 8 8
2633 0 croak("Invalid secret key");
2634 }
2635  
2636 8 bl = InitDataBytesLocker(aTHX_ crypto_box_BEFORENMBYTES);
2637  
2638 8 ▶ if ( crypto_box_beforenm(bl->bytes, pkey_buf, skey_buf) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_beforenm(bl->bytes, pkey_buf, skey_buf) != 0 ) { 0 8 8
2639 0 sodium_free(bl->bytes);
2640 0 Safefree(bl);
2641 0 croak("Failed to pre-calculate key");
2642 }
2643  
2644 8 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2645 }
2646 OUTPUT:
2647 RETVAL
2648  
2649 SV *
2650 nonce(self, ...)
2651 SV * self
2652 PROTOTYPE: $;$
2653 INIT:
2654 DataBytesLocker *bl;
2655 CODE:
2656 PERL_UNUSED_VAR(self);
2657  
2658 5 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 5 5
2659 0 croak("Invalid number of arguments");
2660 }
2661  
2662 5 ▶ if (items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2 ) { 0 5 5
2663 0 ▶ if ( SvOK(ST(1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(ST(1)) ) { 0 0 0
2664 STRLEN prev_nonce_len;
2665 0 unsigned char * prev_nonce = (unsigned char *)SvPV(ST(1), prev_nonce_len);
2666  
2667 0 ▶ if ( prev_nonce_len > crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev_nonce_len > crypto_box_NONCEBYTES ) { 0 0 0
2668 0 croak("Base nonce too long");
2669 }
2670  
2671 0 bl = InitDataBytesLocker(aTHX_ crypto_box_NONCEBYTES);
2672 0 memcpy(bl->bytes, prev_nonce, prev_nonce_len);
2673 0 sodium_memzero(bl->bytes + prev_nonce_len, bl->length - prev_nonce_len);
2674 }
2675 else {
2676 0 croak("Base nonce invalid");
2677 }
2678 }
2679 else {
2680 5 bl = InitDataBytesLocker(aTHX_ crypto_box_NONCEBYTES);
2681 5 randombytes_buf(bl->bytes, bl->length);
2682 }
2683 5 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2684 OUTPUT:
2685 RETVAL
2686  
2687  
2688 void
2689 encrypt(self, msg, nonce, recipient_pubkey, sender_seckey)
2690 SV * self
2691 SV * msg
2692 SV * nonce
2693 SV * recipient_pubkey
2694 SV * sender_seckey
2695 PROTOTYPE: $$$$$
2696 INIT:
2697 STRLEN msg_len;
2698 STRLEN nonce_len;
2699 STRLEN pkey_len;
2700 STRLEN skey_len;
2701 STRLEN enc_len;
2702 unsigned char * msg_buf;
2703 unsigned char * nonce_buf;
2704 unsigned char * pkey_buf;
2705 unsigned char * skey_buf;
2706 DataBytesLocker *bl;
2707 DataBytesLocker *blm;
2708 PPCODE:
2709 {
2710 PERL_UNUSED_VAR(self);
2711  
2712 8 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 8 8
2713 0 XSRETURN_EMPTY;
2714 }
2715  
2716 8 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2717 8 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 8 8
2718 0 croak("Invalid nonce");
2719 }
2720  
2721 8 pkey_buf = (unsigned char *)SvPV(recipient_pubkey, pkey_len);
2722 8 ▶ if ( pkey_len != crypto_box_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_box_PUBLICKEYBYTES ) { 0 8 8
2723 0 croak("Invalid public key");
2724 }
2725  
2726 8 skey_buf = (unsigned char *)SvPV(sender_seckey, skey_len);
2727 8 ▶ if ( skey_len != crypto_box_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_box_SECRETKEYBYTES ) { 0 8 8
2728 0 croak("Invalid secret key");
2729 }
2730  
2731 8 msg_buf = (unsigned char *)SvPV(msg, msg_len);
2732  
2733 /* detached mode */
2734 8 ▶ if ( GIMME_V == G_ARRAY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_ARRAY ) { 3 5 8
2735 unsigned char *mac;
2736  
2737 3 bl = InitDataBytesLocker(aTHX_ msg_len);
2738 3 blm = InitDataBytesLocker(aTHX_ crypto_box_MACBYTES);
2739  
2740 3 ▶ if ( crypto_box_detached( bl->bytes, blm->bytes, (unsigned char *)msg_buf,
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_detached( bl->bytes, blm->bytes, (unsigned char *)msg_buf, 0 3 3
2741 (unsigned long long) msg_len, nonce_buf, pkey_buf, skey_buf) != 0 ) {
2742  
2743 0 sodium_free(bl->bytes);
2744 0 Safefree(bl);
2745 0 sodium_free(blm->bytes);
2746 0 Safefree(blm);
2747  
2748 0 croak("Failed to encrypt data");
2749 }
2750 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ blm) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ blm) ); 0 3 3
2751 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 3 3
2752 3 XSRETURN(2);
2753 }
2754 /* combined mode */
2755 else {
2756 5 enc_len = crypto_box_MACBYTES + msg_len;
2757 5 bl = InitDataBytesLocker(aTHX_ enc_len);
2758  
2759 5 ▶ if ( crypto_box_easy( bl->bytes, msg_buf, msg_len, nonce_buf, pkey_buf, skey_buf) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_easy( bl->bytes, msg_buf, msg_len, nonce_buf, pkey_buf, skey_buf) != 0 ) { 0 5 5
2760 0 sodium_free(bl->bytes);
2761 0 Safefree(bl);
2762 0 croak("Failed to encrypt data");
2763 }
2764  
2765 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
2766 5 XSRETURN(1);
2767 }
2768 }
2769  
2770 void
2771 encrypt_afternm(self, msg, nonce, precalculated_key)
2772 SV * self
2773 SV * msg
2774 SV * nonce
2775 SV * precalculated_key
2776 PROTOTYPE: $$$$
2777 INIT:
2778 STRLEN msg_len;
2779 STRLEN nonce_len;
2780 STRLEN key_len;
2781 STRLEN enc_len;
2782 unsigned char * msg_buf;
2783 unsigned char * nonce_buf;
2784 unsigned char * key_buf;
2785 DataBytesLocker *bl;
2786 DataBytesLocker *blm;
2787 PPCODE:
2788 {
2789 PERL_UNUSED_VAR(self);
2790  
2791 7 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 7 7
2792 0 XSRETURN_EMPTY;
2793 }
2794  
2795 7 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2796 7 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 7 7
2797 0 croak("Invalid nonce");
2798 }
2799  
2800 7 key_buf = (unsigned char *)SvPV(precalculated_key, key_len);
2801 7 ▶ if ( key_len != crypto_box_BEFORENMBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_box_BEFORENMBYTES ) { 0 7 7
2802 0 croak("Invalid precalculated key");
2803 }
2804  
2805 7 msg_buf = (unsigned char *)SvPV(msg, msg_len);
2806  
2807 /* detached mode */
2808 7 ▶ if ( GIMME_V == G_ARRAY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_ARRAY ) { 4 3 7
2809  
2810 4 bl = InitDataBytesLocker(aTHX_ msg_len);
2811 4 blm = InitDataBytesLocker(aTHX_ crypto_box_MACBYTES);
2812  
2813 4 crypto_box_detached_afternm( bl->bytes, blm->bytes, (unsigned char *)msg_buf,
2814 (unsigned long long) msg_len, nonce_buf, key_buf);
2815  
2816 4 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ blm) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ blm) ); 0 4 4
2817 4 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 4 4
2818 4 XSRETURN(2);
2819 }
2820 /* combined mode */
2821 else {
2822 3 enc_len = crypto_box_MACBYTES + msg_len;
2823 3 bl = InitDataBytesLocker(aTHX_ enc_len);
2824  
2825 3 crypto_box_easy_afternm( bl->bytes, msg_buf, msg_len,
2826 nonce_buf, key_buf);
2827  
2828 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 3 3
2829 3 XSRETURN(1);
2830 }
2831 }
2832  
2833  
2834 SV *
2835 decrypt(self, ciphertext, nonce, sender_pubkey, recipient_seckey)
2836 SV * self
2837 SV * ciphertext
2838 SV * nonce
2839 SV * sender_pubkey
2840 SV * recipient_seckey
2841 PROTOTYPE: $$$$$
2842 INIT:
2843 STRLEN msg_len;
2844 STRLEN nonce_len;
2845 STRLEN pkey_len;
2846 STRLEN skey_len;
2847 STRLEN enc_len;
2848 unsigned char * msg_buf;
2849 unsigned char * nonce_buf;
2850 unsigned char * pkey_buf;
2851 unsigned char * skey_buf;
2852 DataBytesLocker *bl;
2853 CODE:
2854 {
2855 PERL_UNUSED_VAR(self);
2856  
2857 5 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 5 5
2858 0 XSRETURN_EMPTY;
2859 }
2860  
2861 5 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2862 5 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 5 5
2863 0 croak("Invalid nonce");
2864 }
2865  
2866 5 pkey_buf = (unsigned char *)SvPV(sender_pubkey, pkey_len);
2867 5 ▶ if ( pkey_len != crypto_box_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_box_PUBLICKEYBYTES ) { 0 5 5
2868 0 croak("Invalid public key");
2869 }
2870  
2871 5 skey_buf = (unsigned char *)SvPV(recipient_seckey, skey_len);
2872 5 ▶ if ( skey_len != crypto_box_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_box_SECRETKEYBYTES ) { 0 5 5
2873 0 croak("Invalid secret key");
2874 }
2875  
2876 5 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
2877  
2878 5 ▶ if ( msg_len < crypto_box_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < crypto_box_MACBYTES ) { 0 5 5
2879 0 croak("Invalid ciphertext");
2880 }
2881  
2882 5 enc_len = msg_len - crypto_box_MACBYTES;
2883  
2884 5 bl = InitDataBytesLocker(aTHX_ enc_len);
2885 5 ▶ if ( crypto_box_open_easy( bl->bytes, msg_buf, msg_len, nonce_buf, pkey_buf, skey_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_open_easy( bl->bytes, msg_buf, msg_len, nonce_buf, pkey_buf, skey_buf) == 0 ) { 5 0 5
2886 5 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2887 }
2888 else {
2889 0 sodium_free(bl->bytes);
2890 0 Safefree(bl);
2891 0 croak("Message forged");
2892 }
2893 }
2894 OUTPUT:
2895 RETVAL
2896  
2897  
2898 SV *
2899 decrypt_detached(self, mac, ciphertext, nonce, sender_pubkey, recipient_seckey)
2900 SV * self
2901 SV * mac
2902 SV * ciphertext
2903 SV * nonce
2904 SV * sender_pubkey
2905 SV * recipient_seckey
2906 PROTOTYPE: $$$$$$
2907 INIT:
2908 STRLEN msg_len;
2909 STRLEN nonce_len;
2910 STRLEN pkey_len;
2911 STRLEN skey_len;
2912 STRLEN mac_len;
2913 unsigned char * msg_buf;
2914 unsigned char * nonce_buf;
2915 unsigned char * pkey_buf;
2916 unsigned char * skey_buf;
2917 unsigned char * mac_buf;
2918 DataBytesLocker *bl;
2919 CODE:
2920 {
2921 PERL_UNUSED_VAR(self);
2922  
2923 2 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 2 2
2924 0 XSRETURN_EMPTY;
2925 }
2926  
2927 2 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2928 2 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 2 2
2929 0 croak("Invalid nonce");
2930 }
2931  
2932 2 pkey_buf = (unsigned char *)SvPV(sender_pubkey, pkey_len);
2933 2 ▶ if ( pkey_len != crypto_box_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_box_PUBLICKEYBYTES ) { 0 2 2
2934 0 croak("Invalid public key");
2935 }
2936  
2937 2 skey_buf = (unsigned char *)SvPV(recipient_seckey, skey_len);
2938 2 ▶ if ( skey_len != crypto_box_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_box_SECRETKEYBYTES ) { 0 2 2
2939 0 croak("Invalid secret key");
2940 }
2941  
2942 2 mac_buf = (unsigned char *)SvPV(mac, mac_len);
2943 2 ▶ if ( mac_len != crypto_box_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != crypto_box_MACBYTES ) { 0 2 2
2944 0 croak("Invalid mac");
2945 }
2946  
2947 2 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
2948  
2949 2 bl = InitDataBytesLocker(aTHX_ msg_len);
2950 2 ▶ if ( crypto_box_open_detached( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, pkey_buf, skey_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_open_detached( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, pkey_buf, skey_buf) == 0 ) { 2 0 2
2951 2 RETVAL = DataBytesLocker2SV(aTHX_ bl);
2952 }
2953 else {
2954 0 sodium_free(bl->bytes);
2955 0 Safefree(bl);
2956 0 croak("Message forged");
2957 }
2958 }
2959 OUTPUT:
2960 RETVAL
2961  
2962  
2963 SV *
2964 decrypt_afternm(self, ciphertext, nonce, precalculated_key)
2965 SV * self
2966 SV * ciphertext
2967 SV * nonce
2968 SV * precalculated_key
2969 PROTOTYPE: $$$$
2970 INIT:
2971 STRLEN msg_len;
2972 STRLEN nonce_len;
2973 STRLEN key_len;
2974 STRLEN enc_len;
2975 unsigned char * msg_buf;
2976 unsigned char * nonce_buf;
2977 unsigned char * key_buf;
2978 DataBytesLocker *bl;
2979 CODE:
2980 {
2981 PERL_UNUSED_VAR(self);
2982  
2983 4 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 4 4
2984 0 XSRETURN_EMPTY;
2985 }
2986  
2987 4 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
2988 4 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 4 4
2989 0 croak("Invalid nonce");
2990 }
2991  
2992 4 key_buf = (unsigned char *)SvPV(precalculated_key, key_len);
2993 4 ▶ if ( key_len != crypto_box_BEFORENMBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_box_BEFORENMBYTES ) { 0 4 4
2994 0 croak("Invalid precalculated key");
2995 }
2996  
2997 4 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
2998  
2999 4 ▶ if ( msg_len < crypto_box_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < crypto_box_MACBYTES ) { 0 4 4
3000 0 croak("Invalid ciphertext");
3001 }
3002  
3003 4 enc_len = msg_len - crypto_box_MACBYTES;
3004  
3005 4 bl = InitDataBytesLocker(aTHX_ enc_len);
3006 4 ▶ if ( crypto_box_open_easy_afternm( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_open_easy_afternm( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf) == 0 ) { 4 0 4
3007 4 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3008 }
3009 else {
3010 0 sodium_free(bl->bytes);
3011 0 Safefree(bl);
3012 0 croak("Message forged");
3013 }
3014 }
3015 OUTPUT:
3016 RETVAL
3017  
3018  
3019 SV *
3020 decrypt_detached_afternm(self, mac, ciphertext, nonce, precalculated_key)
3021 SV * self
3022 SV * mac
3023 SV * ciphertext
3024 SV * nonce
3025 SV * precalculated_key
3026 PROTOTYPE: $$$$$
3027 INIT:
3028 STRLEN msg_len;
3029 STRLEN nonce_len;
3030 STRLEN key_len;
3031 STRLEN mac_len;
3032 unsigned char * msg_buf;
3033 unsigned char * nonce_buf;
3034 unsigned char * key_buf;
3035 unsigned char * mac_buf;
3036 DataBytesLocker *bl;
3037 CODE:
3038 {
3039 PERL_UNUSED_VAR(self);
3040  
3041 3 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 3 3
3042 0 XSRETURN_EMPTY;
3043 }
3044  
3045 3 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
3046 3 ▶ if ( nonce_len != crypto_box_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_box_NONCEBYTES ) { 0 3 3
3047 0 croak("Invalid nonce");
3048 }
3049  
3050 3 key_buf = (unsigned char *)SvPV(precalculated_key, key_len);
3051 3 ▶ if ( key_len != crypto_box_BEFORENMBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_box_BEFORENMBYTES ) { 0 3 3
3052 0 croak("Invalid precalculated key");
3053 }
3054  
3055 3 mac_buf = (unsigned char *)SvPV(mac, mac_len);
3056 3 ▶ if ( mac_len != crypto_box_MACBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != crypto_box_MACBYTES ) { 0 3 3
3057 0 croak("Invalid mac");
3058 }
3059  
3060 3 msg_buf = (unsigned char *)SvPV(ciphertext, msg_len);
3061  
3062 3 bl = InitDataBytesLocker(aTHX_ msg_len);
3063 3 ▶ if ( crypto_box_open_detached_afternm( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_box_open_detached_afternm( bl->bytes, msg_buf, mac_buf, msg_len, nonce_buf, key_buf) == 0 ) { 3 0 3
3064 3 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3065 }
3066 else {
3067 0 sodium_free(bl->bytes);
3068 0 Safefree(bl);
3069 0 croak("Message forged");
3070 }
3071 }
3072 OUTPUT:
3073 RETVAL
3074  
3075  
3076 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::sign
3077  
3078 PROTOTYPES: DISABLE
3079  
3080 unsigned int
3081 PUBLICKEYBYTES(...)
3082 CODE:
3083 1025 ▶ RETVAL = crypto_sign_PUBLICKEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_sign_PUBLICKEYBYTES; 1024 1 1025
3084 OUTPUT:
3085 RETVAL
3086  
3087 unsigned int
3088 SECRETKEYBYTES(...)
3089 CODE:
3090 1 ▶ RETVAL = crypto_sign_SECRETKEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_sign_SECRETKEYBYTES; 1 0 1
3091 OUTPUT:
3092 RETVAL
3093  
3094 unsigned int
3095 BYTES(...)
3096 CODE:
3097 1025 ▶ RETVAL = crypto_sign_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_sign_BYTES; 1023 2 1025
3098 OUTPUT:
3099 RETVAL
3100  
3101 unsigned int
3102 SEEDBYTES(...)
3103 CODE:
3104 1025 ▶ RETVAL = crypto_sign_SEEDBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_sign_SEEDBYTES; 1024 1 1025
3105 OUTPUT:
3106 RETVAL
3107  
3108 PROTOTYPES: ENABLE
3109  
3110 void
3111 keypair(self, ...)
3112 SV * self
3113 PROTOTYPE: $;$
3114 INIT:
3115 DataBytesLocker *blp;
3116 DataBytesLocker *bls;
3117 PPCODE:
3118 {
3119 PERL_UNUSED_VAR(self);
3120  
3121 511 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 511 511
3122 0 croak("Invalid number of arguments");
3123 }
3124 /* from seed */
3125 511 ▶ else if ( items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( items == 2 ) { 6 505 511
3126 12 ▶ if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) {
Branch: 3 branches
BranchTrueFalseTotal
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 2 6
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 0 4
if ( SvPOK(ST(1)) || (SvROK(ST(1)) && sv_derived_from(ST(1), "Data::BytesLocker"))) { 4 0 4
3127 STRLEN seed_len;
3128 6 unsigned char * seed_buf = (unsigned char *)SvPV(ST(1), seed_len);
3129  
3130 6 ▶ if ( seed_len != crypto_sign_SEEDBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( seed_len != crypto_sign_SEEDBYTES ) { 0 6 6
3131 0 croak("Invalid seed length: %" UVuf, (UV) seed_len);
3132 }
3133  
3134 6 blp = InitDataBytesLocker(aTHX_ crypto_sign_PUBLICKEYBYTES);
3135 6 bls = InitDataBytesLocker(aTHX_ crypto_sign_SECRETKEYBYTES);
3136  
3137 6 crypto_sign_seed_keypair(blp->bytes, bls->bytes, seed_buf);
3138 } else {
3139 0 croak("Invalid seed");
3140 }
3141 }
3142 /* regular */
3143 else {
3144 505 blp = InitDataBytesLocker(aTHX_ crypto_sign_PUBLICKEYBYTES);
3145 505 bls = InitDataBytesLocker(aTHX_ crypto_sign_SECRETKEYBYTES);
3146 505 crypto_sign_keypair(blp->bytes, bls->bytes);
3147 }
3148 511 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ blp) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ blp) ); 0 511 511
3149 511 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bls) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bls) ); 0 511 511
3150 511 XSRETURN(2);
3151 }
3152  
3153  
3154 void
3155 to_curve25519_keypair(self, pubkey, seckey)
3156 SV * self
3157 SV * pubkey
3158 SV * seckey
3159 PROTOTYPE: $$$
3160 INIT:
3161 STRLEN pkey_len;
3162 STRLEN skey_len;
3163 unsigned char * pkey_buf;
3164 unsigned char * skey_buf;
3165 DataBytesLocker *blp;
3166 DataBytesLocker *bls;
3167 PPCODE:
3168 {
3169 PERL_UNUSED_VAR(self);
3170  
3171 501 pkey_buf = (unsigned char *)SvPV(pubkey, pkey_len);
3172 501 ▶ if ( pkey_len != crypto_sign_ed25519_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_sign_ed25519_PUBLICKEYBYTES ) { 0 501 501
3173 0 croak("Invalid public key");
3174 }
3175  
3176 501 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
3177 501 ▶ if ( skey_len != crypto_sign_ed25519_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_sign_ed25519_SECRETKEYBYTES ) { 0 501 501
3178 0 croak("Invalid secret key");
3179 }
3180  
3181 501 blp = InitDataBytesLocker(aTHX_ crypto_scalarmult_curve25519_BYTES);
3182  
3183 501 ▶ if ( crypto_sign_ed25519_pk_to_curve25519( blp->bytes, pkey_buf) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_sign_ed25519_pk_to_curve25519( blp->bytes, pkey_buf) != 0 ) { 0 501 501
3184 0 sodium_free(blp->bytes);
3185 0 Safefree(blp);
3186 0 croak("Conversion of public key failed");
3187 }
3188  
3189 501 bls = InitDataBytesLocker(aTHX_ crypto_scalarmult_curve25519_BYTES);
3190 501 ▶ if ( crypto_sign_ed25519_sk_to_curve25519( bls->bytes, skey_buf) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_sign_ed25519_sk_to_curve25519( bls->bytes, skey_buf) != 0 ) { 0 501 501
3191 0 sodium_free(bls->bytes);
3192 0 Safefree(bls);
3193 0 croak("Conversion of secret key failed");
3194 }
3195  
3196 501 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ blp) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ blp) ); 0 501 501
3197 501 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bls) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bls) ); 0 501 501
3198 501 XSRETURN(2);
3199 }
3200  
3201  
3202 void
3203 public_key(self, seckey)
3204 SV * self
3205 SV * seckey
3206 INIT:
3207 STRLEN skey_len;
3208 unsigned char * skey_buf;
3209 DataBytesLocker *bl;
3210 PPCODE:
3211 {
3212 PERL_UNUSED_VAR(self);
3213  
3214 5 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
3215 5 ▶ if ( skey_len != crypto_sign_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_sign_SECRETKEYBYTES ) { 0 5 5
3216 0 croak("Invalid secret key");
3217 }
3218 5 bl = InitDataBytesLocker(aTHX_ crypto_sign_PUBLICKEYBYTES);
3219 5 crypto_sign_ed25519_sk_to_pk(bl->bytes, skey_buf);
3220 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
3221 5 XSRETURN(1);
3222 }
3223  
3224 void
3225 extract_seed(self, seckey)
3226 SV * self
3227 SV * seckey
3228 INIT:
3229 STRLEN skey_len;
3230 unsigned char * skey_buf;
3231 DataBytesLocker *bl;
3232 PPCODE:
3233 {
3234 PERL_UNUSED_VAR(self);
3235  
3236 5 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
3237 5 ▶ if ( skey_len != crypto_sign_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_sign_SECRETKEYBYTES ) { 0 5 5
3238 0 croak("Invalid secret key");
3239 }
3240 5 bl = InitDataBytesLocker(aTHX_ crypto_sign_SEEDBYTES);
3241 5 crypto_sign_ed25519_sk_to_seed(bl->bytes, skey_buf);
3242 5 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 5 5
3243 5 XSRETURN(1);
3244 }
3245  
3246 void
3247 seed(self)
3248 SV * self
3249 INIT:
3250 DataBytesLocker *bl;
3251 PPCODE:
3252 {
3253 PERL_UNUSED_VAR(self);
3254  
3255 2 bl = InitDataBytesLocker(aTHX_ crypto_sign_SEEDBYTES);
3256 2 randombytes_buf(bl->bytes, bl->length);
3257 2 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 2 2
3258 2 XSRETURN(1);
3259 }
3260  
3261 void
3262 seal(self, msg, seckey)
3263 SV * self
3264 SV * msg
3265 SV * seckey
3266 PROTOTYPE: $$$
3267 INIT:
3268 STRLEN msg_len;
3269 STRLEN skey_len;
3270 STRLEN enc_len;
3271 unsigned char * msg_buf;
3272 unsigned char * skey_buf;
3273 DataBytesLocker *bl;
3274 PPCODE:
3275 {
3276 PERL_UNUSED_VAR(self);
3277  
3278 1027 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 1027 1027
3279 0 XSRETURN_EMPTY;
3280 }
3281  
3282 1027 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
3283 1027 ▶ if ( skey_len != crypto_sign_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_sign_SECRETKEYBYTES ) { 0 1027 1027
3284 0 croak("Invalid secret key");
3285 }
3286  
3287 1027 msg_buf = (unsigned char *)SvPV(msg, msg_len);
3288  
3289 1027 ▶ if (msg_len > SIZE_MAX - crypto_sign_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg_len > SIZE_MAX - crypto_sign_BYTES) { 0 1027 1027
3290 0 croak("Encrypted length exceeds system memory limit (size_t overflow)");
3291 }
3292 1027 enc_len = crypto_sign_BYTES + msg_len;
3293 1027 bl = InitDataBytesLocker(aTHX_ enc_len);
3294 1027 crypto_sign( bl->bytes, NULL, msg_buf, msg_len, skey_buf);
3295  
3296 1027 bl->bytes[enc_len] = '\0';
3297  
3298 1027 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 1027 1027
3299 1027 XSRETURN(1);
3300 }
3301  
3302  
3303 void
3304 mac(self, msg, seckey)
3305 SV * self
3306 SV * msg
3307 SV * seckey
3308 PROTOTYPE: $$$
3309 INIT:
3310 STRLEN msg_len;
3311 STRLEN skey_len;
3312 unsigned char * msg_buf;
3313 unsigned char * skey_buf;
3314 DataBytesLocker *bl;
3315 PPCODE:
3316 {
3317 PERL_UNUSED_VAR(self);
3318  
3319 3 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 3 3
3320 0 XSRETURN_EMPTY;
3321 }
3322  
3323 3 skey_buf = (unsigned char *)SvPV(seckey, skey_len);
3324 3 ▶ if ( skey_len != crypto_sign_SECRETKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_sign_SECRETKEYBYTES ) { 0 3 3
3325 0 croak("Invalid secret key");
3326 }
3327  
3328 3 msg_buf = (unsigned char *)SvPV(msg, msg_len);
3329  
3330 3 bl = InitDataBytesLocker(aTHX_ crypto_sign_BYTES);
3331  
3332 3 crypto_sign_detached( bl->bytes, NULL, (unsigned char *)msg_buf,
3333 (unsigned long long) msg_len, skey_buf);
3334 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 3 3
3335 3 XSRETURN(1);
3336 }
3337  
3338 void
3339 verify(self, sig, msg, pubkey)
3340 SV * self
3341 SV * sig
3342 SV * msg
3343 SV * pubkey
3344 PROTOTYPE: $$$$
3345 INIT:
3346 STRLEN msg_len;
3347 STRLEN sig_len;
3348 STRLEN pkey_len;
3349 unsigned char * msg_buf;
3350 unsigned char * sig_buf;
3351 unsigned char * pkey_buf;
3352 PPCODE:
3353 {
3354 PERL_UNUSED_VAR(self);
3355  
3356 3 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 3 3
3357 0 XSRETURN_EMPTY;
3358 }
3359  
3360 3 sig_buf = (unsigned char *)SvPV(sig, sig_len);
3361 3 ▶ if ( sig_len != crypto_sign_BYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sig_len != crypto_sign_BYTES ) { 0 3 3
3362 0 croak("Invalid signature");
3363 }
3364  
3365 3 pkey_buf = (unsigned char *)SvPV(pubkey, pkey_len);
3366 3 ▶ if ( pkey_len != crypto_sign_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_sign_PUBLICKEYBYTES ) { 0 3 3
3367 0 croak("Invalid public key");
3368 }
3369  
3370 3 msg_buf = (unsigned char *)SvPV(msg, msg_len);
3371  
3372 3 ▶ if ( crypto_sign_verify_detached( sig_buf, msg_buf, msg_len, pkey_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_sign_verify_detached( sig_buf, msg_buf, msg_len, pkey_buf) == 0 ) { 3 0 3
3373 3 XSRETURN_YES;
3374 }
3375 else {
3376 0 XSRETURN_NO;
3377 }
3378 }
3379  
3380  
3381 void
3382 open(self, smsg, pubkey)
3383 SV * self
3384 SV * smsg
3385 SV * pubkey
3386 PROTOTYPE: $$$
3387 INIT:
3388 STRLEN msg_len;
3389 STRLEN pkey_len;
3390 STRLEN enc_len;
3391 unsigned char * msg_buf;
3392 unsigned char * pkey_buf;
3393 DataBytesLocker *bl;
3394 PPCODE:
3395 {
3396 PERL_UNUSED_VAR(self);
3397  
3398 2051 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 2051 2051
3399 0 XSRETURN_EMPTY;
3400 }
3401  
3402 2051 pkey_buf = (unsigned char *)SvPV(pubkey, pkey_len);
3403 2051 ▶ if ( pkey_len != crypto_sign_PUBLICKEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_sign_PUBLICKEYBYTES ) { 0 2051 2051
3404 0 croak("Invalid public key");
3405 }
3406  
3407 2051 msg_buf = (unsigned char *)SvPV(smsg, msg_len);
3408  
3409 2051 ▶ if ( msg_len < crypto_sign_BYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( msg_len < crypto_sign_BYTES ) { 0 2051 2051
3410 0 croak("Invalid input data");
3411 }
3412  
3413 2051 enc_len = msg_len - crypto_sign_BYTES;
3414  
3415 2051 bl = InitDataBytesLocker(aTHX_ enc_len);
3416 2051 ▶ if ( crypto_sign_open( bl->bytes, NULL, msg_buf, msg_len, pkey_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_sign_open( bl->bytes, NULL, msg_buf, msg_len, pkey_buf) == 0 ) { 1027 1024 2051
3417  
3418 1027 bl->bytes[enc_len] = '\0';
3419  
3420 1027 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 1027 1027
3421  
3422 1027 XSRETURN(1);
3423 }
3424 else {
3425 1024 sodium_free(bl->bytes);
3426 1024 Safefree(bl);
3427 1024 croak("Message forged");
3428 }
3429 }
3430  
3431 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::generichash
3432  
3433 PROTOTYPES: DISABLE
3434  
3435 unsigned int
3436 BYTES(...)
3437 CODE:
3438 2 ▶ RETVAL = crypto_generichash_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_BYTES; 0 2 2
3439 OUTPUT:
3440 RETVAL
3441  
3442 unsigned int
3443 BYTES_MIN(...)
3444 CODE:
3445 187 ▶ RETVAL = crypto_generichash_BYTES_MIN;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_BYTES_MIN; 182 5 187
3446 OUTPUT:
3447 RETVAL
3448  
3449 unsigned int
3450 BYTES_MAX(...)
3451 CODE:
3452 189 ▶ RETVAL = crypto_generichash_BYTES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_BYTES_MAX; 182 7 189
3453 OUTPUT:
3454 RETVAL
3455  
3456 unsigned int
3457 KEYBYTES(...)
3458 CODE:
3459 3 ▶ RETVAL = crypto_generichash_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_KEYBYTES; 2 1 3
3460 OUTPUT:
3461 RETVAL
3462  
3463 unsigned int
3464 KEYBYTES_MIN(...)
3465 CODE:
3466 15 ▶ RETVAL = crypto_generichash_KEYBYTES_MIN;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_KEYBYTES_MIN; 14 1 15
3467 OUTPUT:
3468 RETVAL
3469  
3470 unsigned int
3471 KEYBYTES_MAX(...)
3472 CODE:
3473 18 ▶ RETVAL = crypto_generichash_KEYBYTES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_generichash_KEYBYTES_MAX; 14 4 18
3474 OUTPUT:
3475 RETVAL
3476  
3477 PROTOTYPES: ENABLE
3478  
3479 SV *
3480 keygen(self, keybytes = crypto_generichash_KEYBYTES)
3481 SV * self
3482 size_t keybytes
3483 INIT:
3484 DataBytesLocker *bl;
3485 CODE:
3486 {
3487 PERL_UNUSED_VAR(self);
3488  
3489 169 ▶ if ( keybytes < crypto_generichash_KEYBYTES_MIN || keybytes > crypto_generichash_KEYBYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keybytes < crypto_generichash_KEYBYTES_MIN || keybytes > crypto_generichash_KEYBYTES_MAX ) { 169 0 169
if ( keybytes < crypto_generichash_KEYBYTES_MIN || keybytes > crypto_generichash_KEYBYTES_MAX ) { 0 169 169
3490 0 croak("Invalid keybytes value: %" UVuf, (UV) keybytes);
3491 }
3492  
3493 169 bl = InitDataBytesLocker(aTHX_ keybytes);
3494  
3495 169 randombytes_buf(bl->bytes, bl->length);
3496  
3497 169 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3498 }
3499 OUTPUT:
3500 RETVAL
3501  
3502 void
3503 mac(self, msg, ...)
3504 SV * self
3505 SV * msg
3506 PROTOTYPE: $$;%
3507 INIT:
3508 unsigned char * msg_buf;
3509 DataBytesLocker *bl;
3510 2419 unsigned char * key_buf = NULL;
3511 2419 STRLEN msg_len = 0;
3512 2419 STRLEN key_len = 0;
3513 2419 size_t bytes = crypto_generichash_BYTES;
3514 PPCODE:
3515 {
3516 PERL_UNUSED_VAR(self);
3517  
3518 2419 ▶ if ( items > 2 && ( items > 6 || items % 2 != 0 ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( items > 2 && ( items > 6 || items % 2 != 0 ) ) { 2419 0 2419
if ( items > 2 && ( items > 6 || items % 2 != 0 ) ) { 2419 0 2419
if ( items > 2 && ( items > 6 || items % 2 != 0 ) ) { 0 2419 2419
3519 0 croak("Invalid number of arguments");
3520 2419 ▶ } else if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 2 ) { 2419 0 2419
3521 2419 int i = 0;
3522 2419 STRLEN keylen = 0;
3523 char * key;
3524  
3525 7085 ▶ for ( i = 2; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 2; i < items; i += 2 ) { 4668 2417 7085
3526 4668 key = SvPV(ST(i), keylen);
3527 4668 ▶ if ( keylen == 3 && strnEQ(key, "key", 3) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 3 && strnEQ(key, "key", 3) ) { 2249 2419 4668
if ( keylen == 3 && strnEQ(key, "key", 3) ) { 2249 0 2249
3528 2249 key_buf = (unsigned char *)SvPV(ST(i+1), key_len);
3529 2249 ▶ if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) { 2249 0 2249
if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) { 0 2249 2249
3530 0 croak("Invalid key length: %" UVuf, (UV) key_len);
3531 }
3532 }
3533 2419 ▶ else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) {
Branch: 2 branches
BranchTrueFalseTotal
else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 2419 0 2419
else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 2419 0 2419
3534 2419 bytes = (size_t)SvUV(ST(i+1));
3535 2419 ▶ if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 2418 1 2419
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 1 2417 2418
3536 2 croak("Invalid bytes value: %" UVuf, (UV) bytes);
3537 }
3538 } else {
3539 0 croak("Invalid argument: %s", key);
3540 }
3541 }
3542 }
3543  
3544 2417 msg_buf = (unsigned char *)SvPV(msg, msg_len);
3545  
3546 2417 bl = InitDataBytesLocker(aTHX_ bytes);
3547 2417 crypto_generichash(bl->bytes, bytes, msg_buf, msg_len, key_buf, key_len);
3548  
3549 2417 ST(0) = sv_2mortal(DataBytesLocker2SV(aTHX_ bl));
3550 2417 XSRETURN(1);
3551 }
3552  
3553  
3554 void
3555 init(self, ...)
3556 SV * self
3557 PROTOTYPE: $;%
3558 INIT:
3559 4786 STRLEN key_len = 0;
3560 4786 unsigned char * key_buf = NULL;
3561 CryptNaClSodiumGenerichashStream *stream;
3562  
3563 4786 size_t bytes = crypto_generichash_BYTES;
3564 PPCODE:
3565 {
3566 PERL_UNUSED_VAR(self);
3567  
3568 4786 ▶ if ( items > 1 && ( items > 5 || (items + 1) % 2 != 0 ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( items > 1 && ( items > 5 || (items + 1) % 2 != 0 ) ) { 4786 0 4786
if ( items > 1 && ( items > 5 || (items + 1) % 2 != 0 ) ) { 4786 0 4786
if ( items > 1 && ( items > 5 || (items + 1) % 2 != 0 ) ) { 0 4786 4786
3569 0 croak("Invalid number of arguments");
3570 4786 ▶ } else if ( items > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 1 ) { 4786 0 4786
3571 4786 int i = 0;
3572 4786 STRLEN keylen = 0;
3573 char * key;
3574  
3575 14013 ▶ for ( i = 1; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items; i += 2 ) { 9230 4783 14013
3576 9230 key = (char *)SvPV(ST(i), keylen);
3577 9230 ▶ if ( keylen == 3 && strnEQ(key, "key", 3) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 3 && strnEQ(key, "key", 3) ) { 4445 4785 9230
if ( keylen == 3 && strnEQ(key, "key", 3) ) { 4445 0 4445
3578 4445 key_buf = (unsigned char *)SvPV(ST(i+1), key_len);
3579 4445 ▶ if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) { 4445 0 4445
if ( key_len < crypto_generichash_KEYBYTES_MIN || key_len > crypto_generichash_KEYBYTES_MAX ) { 1 4444 4445
3580 1 croak("Invalid key length: %" UVuf, (UV) key_len);
3581 }
3582 }
3583 4785 ▶ else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) {
Branch: 2 branches
BranchTrueFalseTotal
else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 4785 0 4785
else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 4785 0 4785
3584 4785 bytes = SvUV(ST(i+1));
3585 4785 ▶ if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 4784 1 4785
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 1 4783 4784
3586 2 croak("Invalid bytes value: %" UVuf, (UV) bytes);
3587 }
3588 } else {
3589 0 croak("Invalid argument: %s", key);
3590 }
3591 }
3592 }
3593  
3594 4783 Newx(stream, 1, CryptNaClSodiumGenerichashStream);
3595 4783 stream->state = sodium_malloc(sizeof(crypto_generichash_state) + (size_t)63U & ~(size_t) 63U);
3596 4783 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 4783 4783
3597 0 croak("Could not allocate memory");
3598 }
3599 4783 stream->init_bytes = bytes;
3600  
3601 4783 crypto_generichash_init(stream->state, key_buf, key_len, bytes);
3602  
3603 4783 ST(0) = sv_2mortal(GenerichashStream2SV(aTHX_ stream));
3604  
3605 4783 XSRETURN(1);
3606 }
3607  
3608 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::generichash::stream
3609  
3610 void
3611 clone(self)
3612 SV * self
3613 PREINIT:
3614 0 CryptNaClSodiumGenerichashStream* cur_stream = GetGenerichashStream(aTHX_ self);
3615 INIT:
3616 CryptNaClSodiumGenerichashStream* new_stream;
3617 PPCODE:
3618 {
3619 0 ▶ CLONESTATE(CryptNaClSodiumGenerichashStream, generichash, (size_t)63U & ~(size_t) 63U, new_stream->init_bytes=cur_stream->init_bytes)
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumGenerichashStream, generichash, (size_t)63U & ~(size_t) 63U, new_stream->init_bytes=cur_stream->init_bytes) 0 0 0
CLONESTATE(CryptNaClSodiumGenerichashStream, generichash, (size_t)63U & ~(size_t) 63U, new_stream->init_bytes=cur_stream->init_bytes) 0 0 0
3620 0 ST(0) = sv_2mortal(GenerichashStream2SV(aTHX_ new_stream));
3621 0 XSRETURN(1);
3622 }
3623  
3624  
3625 void
3626 update(self, ...)
3627 SV * self
3628 PREINIT:
3629 123502 CryptNaClSodiumGenerichashStream* stream = GetGenerichashStream(aTHX_ self);
3630 INIT:
3631 STRLEN msg_len;
3632 unsigned char * msg_buf;
3633 int i;
3634 PPCODE:
3635 {
3636 247004 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 123502 123502 247004
3637 123502 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
3638  
3639 123502 crypto_generichash_update(stream->state, msg_buf, msg_len);
3640 }
3641  
3642 123502 XSRETURN(1);
3643 }
3644  
3645 void
3646 final(self, ...)
3647 SV * self
3648 PROTOTYPE: $%
3649 PREINIT:
3650 4782 CryptNaClSodiumGenerichashStream* stream = GetGenerichashStream(aTHX_ self);
3651 INIT:
3652 DataBytesLocker *bl;
3653 size_t bytes;
3654 PPCODE:
3655 {
3656 4782 bytes = stream->init_bytes;
3657  
3658 4782 ▶ if ( items > 1 && items != 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( items > 1 && items != 3 ) { 4732 50 4782
if ( items > 1 && items != 3 ) { 0 4732 4732
3659 0 croak("Invalid number of arguments");
3660 4782 ▶ } else if ( items > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 1 ) { 4732 50 4782
3661 4732 int i = 0;
3662 4732 STRLEN keylen = 0;
3663 char * key;
3664  
3665 9464 ▶ for ( i = 1; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items; i += 2 ) { 4732 4732 9464
3666 4732 key = (char *)SvPV(ST(i), keylen);
3667 4732 ▶ if ( keylen == 5 && strnEQ(key, "bytes", 5) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 4732 0 4732
if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 4732 0 4732
3668 4732 bytes = SvUV(ST(i+1));
3669 4732 ▶ if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 4732 0 4732
if ( bytes < crypto_generichash_BYTES_MIN || bytes > crypto_generichash_BYTES_MAX ) { 0 4732 4732
3670 0 croak("Invalid bytes value: %" UVuf, (UV) bytes);
3671 }
3672 } else {
3673 0 croak("Invalid argument: %s", key);
3674 }
3675 }
3676 }
3677 4782 bl = InitDataBytesLocker(aTHX_ bytes);
3678  
3679 4782 crypto_generichash_final(stream->state, bl->bytes, bytes);
3680  
3681 4782 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 4782 4782
3682  
3683 4782 XSRETURN(1);
3684 }
3685  
3686 void
3687 DESTROY(self)
3688 SV * self
3689 PREINIT:
3690 4783 CryptNaClSodiumGenerichashStream* stream = GetGenerichashStream(aTHX_ self);
3691 PPCODE:
3692 {
3693 4783 sodium_free( stream->state );
3694 4783 Safefree(stream);
3695 }
3696  
3697 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::shorthash
3698  
3699 PROTOTYPES: DISABLE
3700  
3701 unsigned int
3702 BYTES(...)
3703 CODE:
3704 2 ▶ RETVAL = crypto_shorthash_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_shorthash_BYTES; 0 2 2
3705 OUTPUT:
3706 RETVAL
3707  
3708 unsigned int
3709 KEYBYTES(...)
3710 CODE:
3711 4 ▶ RETVAL = crypto_shorthash_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_shorthash_KEYBYTES; 2 2 4
3712 OUTPUT:
3713 RETVAL
3714  
3715 PROTOTYPES: ENABLE
3716  
3717 SV *
3718 keygen(self)
3719 SV * self
3720 INIT:
3721 DataBytesLocker *bl;
3722 CODE:
3723 {
3724 PERL_UNUSED_VAR(self);
3725  
3726 2 bl = InitDataBytesLocker(aTHX_ crypto_shorthash_KEYBYTES);
3727  
3728 2 randombytes_buf(bl->bytes, bl->length);
3729  
3730 2 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3731 }
3732 OUTPUT:
3733 RETVAL
3734  
3735  
3736 SV *
3737 mac(self, msg, key)
3738 SV * self
3739 SV * msg
3740 SV * key
3741 PROTOTYPE: $$$
3742 INIT:
3743 67 STRLEN msg_len = 0;
3744 67 STRLEN key_len = 0;
3745 unsigned char * msg_buf;
3746 unsigned char * key_buf;
3747 DataBytesLocker *bl;
3748 CODE:
3749 {
3750 PERL_UNUSED_VAR(self);
3751  
3752  
3753 67 key_buf = (unsigned char *)SvPV(key, key_len);
3754 67 ▶ if ( key_len != crypto_shorthash_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_shorthash_KEYBYTES ) { 0 67 67
3755 0 croak("Invalid key");
3756 }
3757  
3758 67 msg_buf = (unsigned char *)SvPV(msg, msg_len);
3759  
3760 67 bl = InitDataBytesLocker(aTHX_ crypto_shorthash_BYTES);
3761 67 crypto_shorthash(bl->bytes, msg_buf, msg_len, key_buf);
3762  
3763 67 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3764 }
3765 OUTPUT:
3766 RETVAL
3767  
3768  
3769 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::pwhash
3770  
3771 PROTOTYPES: DISABLE
3772  
3773 unsigned int
3774 SALTBYTES(...)
3775 CODE:
3776 2 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_SALTBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_SALTBYTES; 0 2 2
3777 OUTPUT:
3778 RETVAL
3779  
3780 unsigned int
3781 STRBYTES(...)
3782 CODE:
3783 20 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_STRBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_STRBYTES; 18 2 20
3784 OUTPUT:
3785 RETVAL
3786  
3787 unsigned long
3788 OPSLIMIT_INTERACTIVE(...)
3789 CODE:
3790 5 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE; 3 2 5
3791 OUTPUT:
3792 RETVAL
3793  
3794 unsigned long
3795 MEMLIMIT_INTERACTIVE(...)
3796 CODE:
3797 13 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE; 11 2 13
3798 OUTPUT:
3799 RETVAL
3800  
3801 unsigned long
3802 OPSLIMIT_SENSITIVE(...)
3803 CODE:
3804 1 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE; 1 0 1
3805 OUTPUT:
3806 RETVAL
3807  
3808 unsigned long
3809 MEMLIMIT_SENSITIVE(...)
3810 CODE:
3811 1 ▶ RETVAL = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE; 1 0 1
3812 OUTPUT:
3813 RETVAL
3814  
3815 PROTOTYPES: ENABLE
3816  
3817 SV *
3818 salt(self)
3819 SV * self
3820 INIT:
3821 DataBytesLocker *bl;
3822 CODE:
3823 {
3824 PERL_UNUSED_VAR(self);
3825  
3826 2 bl = InitDataBytesLocker(aTHX_ crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
3827  
3828 2 randombytes_buf(bl->bytes, bl->length);
3829  
3830 2 RETVAL = DataBytesLocker2SV(aTHX_ bl);
3831 }
3832 OUTPUT:
3833 RETVAL
3834  
3835  
3836 void
3837 key(self, passphrase, salt, ... )
3838 SV * self
3839 SV * salt
3840 SV * passphrase
3841 PROTOTYPE: $$$$;%
3842 INIT:
3843 DataBytesLocker *bl;
3844 19 STRLEN pwd_len = 0;
3845 19 STRLEN salt_len = 0;
3846 char * pwd_buf;
3847 unsigned char * salt_buf;
3848 19 unsigned long long outlen = crypto_pwhash_scryptsalsa208sha256_STRBYTES;
3849 19 unsigned long long opslimit = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
3850 19 size_t memlimit = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
3851 PPCODE:
3852 {
3853 PERL_UNUSED_VAR(self);
3854  
3855 19 ▶ if ( items > 3 && ( ( items + 1 ) % 2 != 0 || items > 9 ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( items > 3 && ( ( items + 1 ) % 2 != 0 || items > 9 ) ) { 19 0 19
if ( items > 3 && ( ( items + 1 ) % 2 != 0 || items > 9 ) ) { 19 0 19
if ( items > 3 && ( ( items + 1 ) % 2 != 0 || items > 9 ) ) { 0 19 19
3856 0 croak("Invalid number of arguments");
3857 19 ▶ } else if ( items > 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 4 ) { 19 0 19
3858 19 int i = 0;
3859 19 STRLEN keylen = 0;
3860 char * key;
3861  
3862 62 ▶ for ( i = 3; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 3; i < items; i += 2 ) { 43 19 62
3863 43 key = (char *)SvPV(ST(i), keylen);
3864 43 ▶ if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) { 24 19 43
if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) { 12 12 24
3865 12 opslimit = (unsigned long long)SvUV(ST(i+1));
3866 12 ▶ if ( opslimit < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( opslimit < 1 ) { 0 12 12
3867 0 croak("Invalid opslimit: %" UVuf, (UV) opslimit);
3868 }
3869 31 ▶ } else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) { 12 19 31
} else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) { 12 0 12
3870 12 memlimit = (unsigned long long)SvUV(ST(i+1));
3871 12 ▶ if ( memlimit < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( memlimit < 1 ) { 0 12 12
3872 0 croak("Invalid memlimit: %" UVuf, (UV) memlimit);
3873 }
3874 19 ▶ } else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 19 0 19
} else if ( keylen == 5 && strnEQ(key, "bytes", 5) ) { 19 0 19
3875 19 outlen = (unsigned long long)SvUV(ST(i+1));
3876 19 ▶ if ( outlen < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( outlen < 1 ) { 0 19 19
3877 0 croak("Invalid bytes: %" UVuf, (UV) outlen);
3878 }
3879 } else {
3880 0 croak("Invalid argument: %s", key);
3881 }
3882 }
3883 }
3884  
3885 19 salt_buf = (unsigned char *)SvPV(salt, salt_len);
3886 19 ▶ if ( salt_len != crypto_pwhash_scryptsalsa208sha256_SALTBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( salt_len != crypto_pwhash_scryptsalsa208sha256_SALTBYTES ) { 0 19 19
3887 0 croak("Invalid salt");
3888 }
3889  
3890 19 pwd_buf = (char *)SvPV(passphrase, pwd_len);
3891  
3892 19 bl = InitDataBytesLocker(aTHX_ outlen);
3893 19 ▶ if ( crypto_pwhash_scryptsalsa208sha256(bl->bytes, outlen, pwd_buf, pwd_len, salt_buf, opslimit, memlimit) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_pwhash_scryptsalsa208sha256(bl->bytes, outlen, pwd_buf, pwd_len, salt_buf, opslimit, memlimit) != 0 ) { 0 19 19
3894 0 sodium_free( bl->bytes );
3895 0 Safefree(bl);
3896 0 croak("Out of memory");
3897 }
3898  
3899 19 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 19 19
3900  
3901 19 XSRETURN(1);
3902 }
3903  
3904  
3905 void
3906 str(self, passphrase, ... )
3907 SV * self
3908 SV * passphrase
3909 PROTOTYPE: $$;%
3910 INIT:
3911 DataBytesLocker *bl;
3912 21 STRLEN pwd_len = 0;
3913 char * pwd_buf;
3914 21 unsigned long long opslimit = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
3915 21 size_t memlimit = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
3916 PPCODE:
3917 {
3918 PERL_UNUSED_VAR(self);
3919  
3920 21 ▶ if ( items > 2 && ( items % 2 != 0 || items > 6 ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( items > 2 && ( items % 2 != 0 || items > 6 ) ) { 20 1 21
if ( items > 2 && ( items % 2 != 0 || items > 6 ) ) { 20 0 20
if ( items > 2 && ( items % 2 != 0 || items > 6 ) ) { 0 20 20
3921 0 croak("Invalid number of arguments");
3922 21 ▶ } else if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 2 ) { 20 1 21
3923 20 int i = 0;
3924 20 STRLEN keylen = 0;
3925 char * key;
3926  
3927 60 ▶ for ( i = 2; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 2; i < items; i += 2 ) { 40 20 60
3928 40 key = (char *)SvPV(ST(i), keylen);
3929 40 ▶ if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) { 40 0 40
if ( keylen == 8 && strnEQ(key, "opslimit", 8) ) { 20 20 40
3930 20 opslimit = SvUV(ST(i+1));
3931 20 ▶ if ( opslimit < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( opslimit < 1 ) { 0 20 20
3932 0 croak("Invalid opslimit: %" UVuf, (UV) opslimit);
3933 }
3934 20 ▶ } else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) { 20 0 20
} else if ( keylen == 8 && strnEQ(key, "memlimit", 8) ) { 20 0 20
3935 20 memlimit = SvUV(ST(i+1));
3936 20 ▶ if ( memlimit < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( memlimit < 1 ) { 0 20 20
3937 0 croak("Invalid memlimit: %" UVuf, (UV) memlimit);
3938 }
3939 } else {
3940 0 croak("Invalid argument: %s", key);
3941 }
3942 }
3943 }
3944  
3945 21 pwd_buf = (char *)SvPV(passphrase, pwd_len);
3946  
3947 21 bl = InitDataBytesLocker(aTHX_ crypto_pwhash_scryptsalsa208sha256_STRBYTES);
3948 21 ▶ if ( crypto_pwhash_scryptsalsa208sha256_str((char *)bl->bytes, pwd_buf, pwd_len, opslimit, memlimit) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_pwhash_scryptsalsa208sha256_str((char *)bl->bytes, pwd_buf, pwd_len, opslimit, memlimit) != 0 ) { 0 21 21
3949 0 sodium_free( bl->bytes );
3950 0 Safefree(bl);
3951 0 croak("Out of memory");
3952 }
3953 21 bl->bytes[crypto_pwhash_scryptsalsa208sha256_STRBYTES] = 0;
3954  
3955 21 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 21 21
3956 21 XSRETURN(1);
3957 }
3958  
3959  
3960 SV *
3961 verify(self, str, passphrase )
3962 SV * self
3963 SV * str
3964 SV * passphrase
3965 PROTOTYPE: $$$
3966 INIT:
3967 STRLEN str_len;
3968 STRLEN pwd_len;
3969 char * str_buf;
3970 char * pwd_buf;
3971 49 int res = 0;
3972 CODE:
3973 {
3974 PERL_UNUSED_VAR(self);
3975  
3976 49 str_buf = (char *)SvPV(str, str_len);
3977  
3978 /* -1 - need to expand by single null char */
3979 49 ▶ if ( str_len == crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( str_len == crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1 ) { 23 26 49
3980 23 char * str102 = NULL; /* stores the terminating null byte */
3981  
3982 23 pwd_buf = (char *)SvPV(passphrase, pwd_len);
3983  
3984 23 str102 = (char *) sodium_malloc(crypto_pwhash_scryptsalsa208sha256_STRBYTES);
3985 23 ▶ if ( str102 == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( str102 == NULL ) { 0 23 23
3986 0 croak("Could not allocate memory");
3987 }
3988  
3989 23 memcpy(str102, str_buf, str_len);
3990 23 str102[crypto_pwhash_scryptsalsa208sha256_STRBYTES-1] = 0;
3991  
3992 23 res = crypto_pwhash_scryptsalsa208sha256_str_verify( str102, pwd_buf, pwd_len) == 0;
3993  
3994 23 sodium_free(str102);
3995 }
3996 /* already includes null byte */
3997 26 ▶ else if ( str_len == crypto_pwhash_scryptsalsa208sha256_STRBYTES
Branch: 1 branch
BranchTrueFalseTotal
else if ( str_len == crypto_pwhash_scryptsalsa208sha256_STRBYTES 21 5 26
3998 21 &&
3999 21 ▶ str_buf[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1] == 0
Branch: 1 branch
BranchTrueFalseTotal
str_buf[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1] == 0 21 0 21
4000 ) {
4001 21 pwd_buf = (char *)SvPV(passphrase, pwd_len);
4002  
4003 21 res = crypto_pwhash_scryptsalsa208sha256_str_verify( str_buf, pwd_buf, pwd_len) == 0;
4004 }
4005 else {
4006 5 croak("Invalid string");
4007 }
4008  
4009 44 ▶ RETVAL = res ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = res ? &PL_sv_yes : &PL_sv_no; 30 14 44
4010 }
4011 OUTPUT:
4012 RETVAL
4013  
4014  
4015 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::hash
4016  
4017 PROTOTYPES: DISABLE
4018  
4019 unsigned int
4020 SHA256_BYTES(...)
4021 CODE:
4022 2 ▶ RETVAL = crypto_hash_sha256_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_hash_sha256_BYTES; 0 2 2
4023 OUTPUT:
4024 RETVAL
4025  
4026 unsigned int
4027 SHA512_BYTES(...)
4028 CODE:
4029 2 ▶ RETVAL = crypto_hash_sha512_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_hash_sha512_BYTES; 2 0 2
4030 OUTPUT:
4031 RETVAL
4032  
4033 PROTOTYPES: ENABLE
4034  
4035 void
4036 sha256(self, msg)
4037 SV * self
4038 SV * msg
4039 PROTOTYPE: $$
4040 ALIAS:
4041 sha512 = 1
4042 INIT:
4043 16 STRLEN msg_len = 0;
4044 unsigned char * msg_buf;
4045 DataBytesLocker *bl;
4046 PPCODE:
4047 {
4048 PERL_UNUSED_VAR(self);
4049  
4050 16 msg_buf = (unsigned char *)SvPV(msg, msg_len);
4051  
4052 16 ▶ switch(ix) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ix) { 6 10 16
4053 6 case 1:
4054 6 bl = InitDataBytesLocker(aTHX_ crypto_hash_sha512_BYTES);
4055 6 crypto_hash_sha512(bl->bytes, msg_buf, msg_len);
4056 6 break;
4057 10 default:
4058 10 bl = InitDataBytesLocker(aTHX_ crypto_hash_sha256_BYTES);
4059 10 crypto_hash_sha256(bl->bytes, msg_buf, msg_len);
4060 }
4061  
4062 16 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 16 16
4063 16 XSRETURN(1);
4064 }
4065  
4066 void
4067 sha256_init(self)
4068 SV * self
4069 PROTOTYPE: $
4070 INIT:
4071 CryptNaClSodiumHashSha256Stream *stream;
4072 PPCODE:
4073 {
4074 PERL_UNUSED_VAR(self);
4075  
4076 4 Newx(stream, 1, CryptNaClSodiumHashSha256Stream);
4077 4 stream->state = sodium_malloc(sizeof(crypto_hash_sha256_state));
4078 4 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 4 4
4079 0 croak("Could not allocate memory");
4080 }
4081  
4082 4 crypto_hash_sha256_init(stream->state);
4083  
4084 4 ST(0) = sv_2mortal(HashSha256Stream2SV(aTHX_ stream));
4085  
4086 4 XSRETURN(1);
4087 }
4088  
4089 void
4090 sha512_init(self)
4091 SV * self
4092 PROTOTYPE: $
4093 INIT:
4094 CryptNaClSodiumHashSha512Stream *stream;
4095 PPCODE:
4096 {
4097 PERL_UNUSED_VAR(self);
4098  
4099 4 Newx(stream, 1, CryptNaClSodiumHashSha512Stream);
4100 4 stream->state = sodium_malloc(sizeof(crypto_hash_sha512_state));
4101 4 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 4 4
4102 0 croak("Could not allocate memory");
4103 }
4104  
4105 4 crypto_hash_sha512_init(stream->state);
4106  
4107 4 ST(0) = sv_2mortal(HashSha512Stream2SV(aTHX_ stream));
4108  
4109 4 XSRETURN(1);
4110 }
4111  
4112  
4113 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::hash::sha256stream
4114  
4115 void
4116 clone(self)
4117 SV * self
4118 PREINIT:
4119 0 CryptNaClSodiumHashSha256Stream* cur_stream = GetHashSha256Stream(aTHX_ self);
4120 INIT:
4121 CryptNaClSodiumHashSha256Stream* new_stream;
4122 PPCODE:
4123 {
4124 0 ▶ CLONESTATE(CryptNaClSodiumHashSha256Stream, hash_sha256, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumHashSha256Stream, hash_sha256, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumHashSha256Stream, hash_sha256, 0, ((void)0)) 0 0 0
4125 0 ST(0) = sv_2mortal(HashSha256Stream2SV(aTHX_ new_stream));
4126 0 XSRETURN(1);
4127 }
4128  
4129 void
4130 update(self, ...)
4131 SV * self
4132 PREINIT:
4133 357 CryptNaClSodiumHashSha256Stream* stream = GetHashSha256Stream(aTHX_ self);
4134 INIT:
4135 STRLEN msg_len;
4136 unsigned char * msg_buf;
4137 int i;
4138 PPCODE:
4139 {
4140 714 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 357 357 714
4141 357 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
4142  
4143 357 crypto_hash_sha256_update(stream->state, msg_buf, msg_len);
4144 }
4145  
4146 357 XSRETURN(1);
4147 }
4148  
4149 void
4150 final(self)
4151 SV * self
4152 PROTOTYPE: $
4153 PREINIT:
4154 4 CryptNaClSodiumHashSha256Stream* stream = GetHashSha256Stream(aTHX_ self);
4155 INIT:
4156 DataBytesLocker *bl;
4157 PPCODE:
4158 {
4159 4 bl = InitDataBytesLocker(aTHX_ crypto_hash_sha256_BYTES);
4160  
4161 4 crypto_hash_sha256_final(stream->state, bl->bytes);
4162  
4163 4 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 4 4
4164 4 XSRETURN(1);
4165 }
4166  
4167 void
4168 DESTROY(self)
4169 SV * self
4170 PREINIT:
4171 4 CryptNaClSodiumHashSha256Stream* stream = GetHashSha256Stream(aTHX_ self);
4172 PPCODE:
4173 {
4174 4 sodium_free( stream->state );
4175 4 Safefree(stream);
4176 }
4177  
4178  
4179 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::hash::sha512stream
4180  
4181 void
4182 clone(self)
4183 SV * self
4184 PREINIT:
4185 0 CryptNaClSodiumHashSha512Stream* cur_stream = GetHashSha512Stream(aTHX_ self);
4186 INIT:
4187 CryptNaClSodiumHashSha512Stream* new_stream;
4188 PPCODE:
4189 {
4190 0 ▶ CLONESTATE(CryptNaClSodiumHashSha512Stream, hash_sha512, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumHashSha512Stream, hash_sha512, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumHashSha512Stream, hash_sha512, 0, ((void)0)) 0 0 0
4191 0 ST(0) = sv_2mortal(HashSha512Stream2SV(aTHX_ new_stream));
4192 0 XSRETURN(1);
4193 }
4194  
4195 void
4196 update(self, ...)
4197 SV * self
4198 PREINIT:
4199 357 CryptNaClSodiumHashSha512Stream* stream = GetHashSha512Stream(aTHX_ self);
4200 INIT:
4201 STRLEN msg_len;
4202 unsigned char * msg_buf;
4203 int i;
4204 PPCODE:
4205 {
4206 714 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 357 357 714
4207 357 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
4208  
4209 357 crypto_hash_sha512_update(stream->state, msg_buf, msg_len);
4210 }
4211  
4212 357 XSRETURN(1);
4213 }
4214  
4215 void
4216 final(self)
4217 SV * self
4218 PROTOTYPE: $
4219 PREINIT:
4220 4 CryptNaClSodiumHashSha512Stream* stream = GetHashSha512Stream(aTHX_ self);
4221 INIT:
4222 DataBytesLocker *bl;
4223 PPCODE:
4224 {
4225 4 bl = InitDataBytesLocker(aTHX_ crypto_hash_sha512_BYTES);
4226  
4227 4 crypto_hash_sha512_final(stream->state, bl->bytes);
4228  
4229 4 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 4 4
4230 4 XSRETURN(1);
4231 }
4232  
4233  
4234 void
4235 DESTROY(self)
4236 SV * self
4237 PREINIT:
4238 4 CryptNaClSodiumHashSha512Stream* stream = GetHashSha512Stream(aTHX_ self);
4239 PPCODE:
4240 {
4241 4 sodium_free( stream->state );
4242 4 Safefree(stream);
4243 }
4244  
4245  
4246 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::onetimeauth
4247  
4248 PROTOTYPES: DISABLE
4249  
4250 unsigned int
4251 BYTES(...)
4252 CODE:
4253 9999 ▶ RETVAL = crypto_onetimeauth_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_onetimeauth_BYTES; 9998 1 9999
4254 OUTPUT:
4255 RETVAL
4256  
4257 unsigned int
4258 KEYBYTES(...)
4259 CODE:
4260 1 ▶ RETVAL = crypto_onetimeauth_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_onetimeauth_KEYBYTES; 0 1 1
4261 OUTPUT:
4262 RETVAL
4263  
4264 PROTOTYPES: ENABLE
4265  
4266 SV *
4267 keygen(self)
4268 SV * self
4269 INIT:
4270 DataBytesLocker *bl;
4271 CODE:
4272 {
4273 PERL_UNUSED_VAR(self);
4274  
4275 10001 bl = InitDataBytesLocker(aTHX_ crypto_onetimeauth_KEYBYTES);
4276  
4277 10001 randombytes_buf(bl->bytes, bl->length);
4278  
4279 10001 RETVAL = DataBytesLocker2SV(aTHX_ bl);
4280 }
4281 OUTPUT:
4282 RETVAL
4283  
4284  
4285 void
4286 mac(self, msg, key)
4287 SV * self
4288 SV * msg
4289 SV * key
4290 PROTOTYPE: $$$
4291 INIT:
4292 STRLEN msg_len;
4293 STRLEN key_len;
4294 unsigned char * msg_buf;
4295 unsigned char * key_buf;
4296 DataBytesLocker *bl;
4297 PPCODE:
4298 {
4299 PERL_UNUSED_VAR(self);
4300  
4301 10003 key_buf = (unsigned char *)SvPV(key, key_len);
4302 10003 ▶ if ( key_len != crypto_onetimeauth_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_onetimeauth_KEYBYTES ) { 0 10003 10003
4303 0 croak("Invalid key");
4304 }
4305  
4306 10003 msg_buf = (unsigned char *)SvPV(msg, msg_len);
4307  
4308 10003 bl = InitDataBytesLocker(aTHX_ crypto_onetimeauth_BYTES);
4309 10003 crypto_onetimeauth(bl->bytes, msg_buf, msg_len, key_buf);
4310  
4311 10003 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 10003 10003
4312 10003 XSRETURN(1);
4313 }
4314  
4315 void
4316 verify(self, mac, msg, key)
4317 SV * self
4318 SV * mac
4319 SV * msg
4320 SV * key
4321 PROTOTYPE: $$$$
4322 INIT:
4323 STRLEN msg_len;
4324 STRLEN key_len;
4325 STRLEN mac_len;
4326 unsigned char * msg_buf;
4327 unsigned char * mac_buf;
4328 unsigned char * key_buf;
4329 PPCODE:
4330 {
4331 PERL_UNUSED_VAR(self);
4332  
4333 30004 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 30004 30004
4334 0 XSRETURN_EMPTY;
4335 }
4336  
4337 30004 mac_buf = (unsigned char *)SvPV(mac, mac_len);
4338 30004 ▶ if ( mac_len != crypto_onetimeauth_BYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mac_len != crypto_onetimeauth_BYTES ) { 0 30004 30004
4339 0 croak("Invalid mac");
4340 }
4341  
4342 30004 key_buf = (unsigned char *)SvPV(key, key_len);
4343 30004 ▶ if ( key_len != crypto_onetimeauth_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_onetimeauth_KEYBYTES ) { 0 30004 30004
4344 0 croak("Invalid key");
4345 }
4346  
4347 30004 msg_buf = (unsigned char *)SvPV(msg, msg_len);
4348  
4349 30004 ▶ if ( crypto_onetimeauth_verify( mac_buf, msg_buf, msg_len, key_buf) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_onetimeauth_verify( mac_buf, msg_buf, msg_len, key_buf) == 0 ) { 10006 19998 30004
4350 10006 XSRETURN_YES;
4351 }
4352 else {
4353 19998 XSRETURN_NO;
4354 }
4355 }
4356  
4357  
4358 void
4359 init(self, key)
4360 SV * self
4361 SV * key
4362 PROTOTYPE: $;%
4363 INIT:
4364 STRLEN key_len;
4365 unsigned char * key_buf;
4366 CryptNaClSodiumOnetimeauthStream *stream;
4367 PPCODE:
4368 {
4369 PERL_UNUSED_VAR(self);
4370  
4371 3 key_buf = (unsigned char *)SvPV(key, key_len);
4372 3 ▶ if ( key_len != crypto_onetimeauth_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_onetimeauth_KEYBYTES ) { 0 3 3
4373 0 croak("Invalid key");
4374 }
4375  
4376 3 Newx(stream, 1, CryptNaClSodiumOnetimeauthStream);
4377 3 stream->state = sodium_malloc(sizeof(crypto_onetimeauth_state));
4378 3 ▶ if ( stream->state == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( stream->state == NULL ) { 0 3 3
4379 0 croak("Could not allocate memory");
4380 }
4381  
4382 3 crypto_onetimeauth_init(stream->state, key_buf);
4383  
4384 3 ST(0) = sv_2mortal(OnetimeauthStream2SV(aTHX_ stream));
4385  
4386 3 XSRETURN(1);
4387 }
4388  
4389  
4390 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::onetimeauth::stream
4391  
4392 void
4393 clone(self)
4394 SV * self
4395 PREINIT:
4396 0 CryptNaClSodiumOnetimeauthStream* cur_stream = GetOnetimeauthStream(aTHX_ self);
4397 INIT:
4398 CryptNaClSodiumOnetimeauthStream* new_stream;
4399 PPCODE:
4400 {
4401 0 ▶ CLONESTATE(CryptNaClSodiumOnetimeauthStream, onetimeauth, 0, ((void)0))
Branch: 2 branches
BranchTrueFalseTotal
CLONESTATE(CryptNaClSodiumOnetimeauthStream, onetimeauth, 0, ((void)0)) 0 0 0
CLONESTATE(CryptNaClSodiumOnetimeauthStream, onetimeauth, 0, ((void)0)) 0 0 0
4402 0 ST(0) = sv_2mortal(OnetimeauthStream2SV(aTHX_ new_stream));
4403 0 XSRETURN(1);
4404 }
4405  
4406  
4407 void
4408 update(self, ...)
4409 SV * self
4410 PREINIT:
4411 26 CryptNaClSodiumOnetimeauthStream* stream = GetOnetimeauthStream(aTHX_ self);
4412 INIT:
4413 STRLEN msg_len;
4414 unsigned char * msg_buf;
4415 int i;
4416 PPCODE:
4417 {
4418 52 ▶ for ( i = 1; i < items ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items ; i++ ) { 26 26 52
4419 26 msg_buf = (unsigned char *)SvPV(ST(i), msg_len);
4420  
4421 26 crypto_onetimeauth_update(stream->state, msg_buf, msg_len);
4422 }
4423  
4424 26 XSRETURN(1);
4425 }
4426  
4427 void
4428 final(self)
4429 SV * self
4430 PROTOTYPE: $
4431 PREINIT:
4432 3 CryptNaClSodiumOnetimeauthStream* stream = GetOnetimeauthStream(aTHX_ self);
4433 INIT:
4434 DataBytesLocker *bl;
4435 PPCODE:
4436 {
4437 3 bl = InitDataBytesLocker(aTHX_ crypto_onetimeauth_BYTES);
4438  
4439 3 crypto_onetimeauth_final(stream->state, bl->bytes);
4440  
4441 3 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 3 3
4442 3 XSRETURN(1);
4443 }
4444  
4445 void
4446 DESTROY(self)
4447 SV * self
4448 PREINIT:
4449 3 CryptNaClSodiumOnetimeauthStream* stream = GetOnetimeauthStream(aTHX_ self);
4450 PPCODE:
4451 {
4452 3 sodium_free( stream->state );
4453 3 Safefree(stream);
4454 }
4455  
4456  
4457 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::scalarmult
4458  
4459 PROTOTYPES: DISABLE
4460  
4461 unsigned int
4462 BYTES(...)
4463 CODE:
4464 1 ▶ RETVAL = crypto_scalarmult_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_scalarmult_BYTES; 0 1 1
4465 OUTPUT:
4466 RETVAL
4467  
4468  
4469 unsigned int
4470 SCALARBYTES(...)
4471 CODE:
4472 1 ▶ RETVAL = crypto_scalarmult_SCALARBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_scalarmult_SCALARBYTES; 1 0 1
4473 OUTPUT:
4474 RETVAL
4475  
4476 PROTOTYPES: ENABLE
4477  
4478 SV *
4479 keygen(self)
4480 SV * self
4481 INIT:
4482 DataBytesLocker *bl;
4483 CODE:
4484 {
4485 PERL_UNUSED_VAR(self);
4486  
4487 6 bl = InitDataBytesLocker(aTHX_ crypto_scalarmult_SCALARBYTES);
4488  
4489 6 randombytes_buf(bl->bytes, bl->length);
4490  
4491 6 RETVAL = DataBytesLocker2SV(aTHX_ bl);
4492 }
4493 OUTPUT:
4494 RETVAL
4495  
4496 void
4497 base(self, secret_key)
4498 SV * self
4499 SV * secret_key
4500 PROTOTYPE: $$
4501 INIT:
4502 STRLEN key_len;
4503 unsigned char * key_buf;
4504 DataBytesLocker *bl;
4505 PPCODE:
4506 {
4507 PERL_UNUSED_VAR(self);
4508  
4509 508 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 508 508
4510 0 XSRETURN_EMPTY;
4511 }
4512  
4513 508 key_buf = (unsigned char *)SvPV(secret_key, key_len);
4514 508 ▶ if ( key_len != crypto_scalarmult_SCALARBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_scalarmult_SCALARBYTES ) { 0 508 508
4515 0 croak("Invalid key");
4516 }
4517  
4518 508 bl = InitDataBytesLocker(aTHX_ crypto_scalarmult_BYTES);
4519 508 crypto_scalarmult_base( bl->bytes, key_buf);
4520  
4521 508 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 508 508
4522 508 XSRETURN(1);
4523 }
4524  
4525  
4526 void
4527 shared_secret(self, secret_key, public_key)
4528 SV * self
4529 SV * secret_key
4530 SV * public_key
4531 PROTOTYPE: $$$$
4532 INIT:
4533 STRLEN skey_len;
4534 STRLEN pkey_len;
4535 unsigned char * skey_buf;
4536 unsigned char * pkey_buf;
4537  
4538 DataBytesLocker *bl;
4539 PPCODE:
4540 {
4541 PERL_UNUSED_VAR(self);
4542  
4543 7 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 7 7
4544 0 XSRETURN_EMPTY;
4545 }
4546  
4547 7 skey_buf = (unsigned char *)SvPV(secret_key, skey_len);
4548 7 ▶ if ( skey_len != crypto_scalarmult_SCALARBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( skey_len != crypto_scalarmult_SCALARBYTES ) { 0 7 7
4549 0 croak("Invalid secret key");
4550 }
4551  
4552 7 pkey_buf = (unsigned char *)SvPV(public_key, pkey_len);
4553 7 ▶ if ( pkey_len != crypto_scalarmult_SCALARBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pkey_len != crypto_scalarmult_SCALARBYTES ) { 0 7 7
4554 0 croak("Invalid public key");
4555 }
4556  
4557 7 bl = InitDataBytesLocker(aTHX_ crypto_scalarmult_BYTES);
4558  
4559 7 ▶ if ( crypto_scalarmult( bl->bytes, skey_buf, pkey_buf) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( crypto_scalarmult( bl->bytes, skey_buf, pkey_buf) != 0 ) { 0 7 7
4560 0 sodium_free(bl->bytes);
4561 0 Safefree(bl);
4562 0 croak("Failed to calculate shared secret");
4563 }
4564  
4565 7 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 7 7
4566 7 XSRETURN(1);
4567 }
4568  
4569  
4570 MODULE = Crypt::NaCl::Sodium PACKAGE = Crypt::NaCl::Sodium::stream
4571  
4572 PROTOTYPES: DISABLE
4573  
4574 unsigned int
4575 NONCEBYTES(...)
4576 CODE:
4577 3 ▶ RETVAL = crypto_stream_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_NONCEBYTES; 1 2 3
4578 OUTPUT:
4579 RETVAL
4580  
4581 unsigned int
4582 KEYBYTES(...)
4583 CODE:
4584 3 ▶ RETVAL = crypto_stream_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_KEYBYTES; 2 1 3
4585 OUTPUT:
4586 RETVAL
4587  
4588 unsigned int
4589 CHACHA20_NONCEBYTES(...)
4590 CODE:
4591 3 ▶ RETVAL = crypto_stream_chacha20_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_chacha20_NONCEBYTES; 1 2 3
4592 OUTPUT:
4593 RETVAL
4594  
4595 unsigned int
4596 CHACHA20_IETF_NONCEBYTES(...)
4597 CODE:
4598 0 ▶ RETVAL = crypto_stream_chacha20_IETF_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_chacha20_IETF_NONCEBYTES; 0 0 0
4599 OUTPUT:
4600 RETVAL
4601  
4602 unsigned int
4603 CHACHA20_KEYBYTES(...)
4604 CODE:
4605 3 ▶ RETVAL = crypto_stream_chacha20_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_chacha20_KEYBYTES; 1 2 3
4606 OUTPUT:
4607 RETVAL
4608  
4609 unsigned int
4610 SALSA20_NONCEBYTES(...)
4611 CODE:
4612 3 ▶ RETVAL = crypto_stream_salsa20_NONCEBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_salsa20_NONCEBYTES; 2 1 3
4613 OUTPUT:
4614 RETVAL
4615  
4616 unsigned int
4617 SALSA20_KEYBYTES(...)
4618 CODE:
4619 3 ▶ RETVAL = crypto_stream_salsa20_KEYBYTES;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = crypto_stream_salsa20_KEYBYTES; 2 1 3
4620 OUTPUT:
4621 RETVAL
4622  
4623 unsigned int
4624 AES128CTR_NONCEBYTES(...)
4625 CODE:
4626 #ifdef AES128CTR_IS_AVAILABLE
4627 RETVAL = crypto_stream_aes128ctr_NONCEBYTES;
4628 #else
4629 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4630 #endif
4631 OUTPUT:
4632 RETVAL
4633  
4634 unsigned int
4635 AES128CTR_KEYBYTES(...)
4636 CODE:
4637 #ifdef AES128CTR_IS_AVAILABLE
4638 RETVAL = crypto_stream_aes128ctr_KEYBYTES;
4639 #else
4640 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4641 #endif
4642 OUTPUT:
4643 RETVAL
4644  
4645 PROTOTYPES: ENABLE
4646  
4647 SV *
4648 keygen(self)
4649 SV * self
4650 ALIAS:
4651 chacha20_keygen = 1
4652 salsa20_keygen = 2
4653 aes128ctr_keygen = 3
4654 INIT:
4655 unsigned int key_size;
4656 DataBytesLocker *bl;
4657 CODE:
4658 {
4659 PERL_UNUSED_VAR(self);
4660  
4661 3 switch(ix) {
4662 1 case 1:
4663 1 key_size = crypto_stream_chacha20_KEYBYTES;
4664 1 break;
4665 1 case 2:
4666 1 key_size = crypto_stream_salsa20_KEYBYTES;
4667 1 break;
4668 0 case 3:
4669 #ifdef AES128CTR_IS_AVAILABLE
4670 key_size = crypto_stream_aes128ctr_KEYBYTES;
4671 #else
4672 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4673 #endif
4674 break;
4675 1 default:
4676 1 key_size = crypto_stream_KEYBYTES;
4677 }
4678 3 bl = InitDataBytesLocker(aTHX_ key_size);
4679 3 randombytes_buf(bl->bytes, key_size);
4680 3 RETVAL = DataBytesLocker2SV(aTHX_ bl);
4681 }
4682 OUTPUT:
4683 RETVAL
4684  
4685 SV *
4686 nonce(self, ...)
4687 SV * self
4688 PROTOTYPE: $;$
4689 ALIAS:
4690 chacha20_nonce = 1
4691 salsa20_nonce = 2
4692 aes128ctr_nonce = 3
4693 chacha20_ietf_nonce = 4
4694 INIT:
4695 unsigned int nonce_size;
4696 DataBytesLocker *bl;
4697 CODE:
4698 PERL_UNUSED_VAR(self);
4699  
4700 3 switch(ix) {
4701 1 case 1:
4702 1 nonce_size = crypto_stream_chacha20_NONCEBYTES;
4703 1 break;
4704 1 case 2:
4705 1 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4706 1 break;
4707 0 case 3:
4708 #ifdef AES128CTR_IS_AVAILABLE
4709 nonce_size = crypto_stream_aes128ctr_NONCEBYTES;
4710 #else
4711 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4712 #endif
4713 break;
4714 0 case 4:
4715 0 nonce_size = crypto_stream_chacha20_IETF_NONCEBYTES;
4716 0 break;
4717 1 default:
4718 1 nonce_size = crypto_stream_NONCEBYTES;
4719 }
4720  
4721 3 ▶ if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items > 2 ) { 0 3 3
4722 0 croak("Invalid number of arguments");
4723 }
4724  
4725 3 ▶ if (items == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2 ) { 0 3 3
4726 0 ▶ if ( SvOK(ST(1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(ST(1)) ) { 0 0 0
4727 STRLEN prev_nonce_len;
4728 0 unsigned char * prev_nonce = (unsigned char *)SvPV(ST(1), prev_nonce_len);
4729  
4730 0 ▶ if ( prev_nonce_len > nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev_nonce_len > nonce_size ) { 0 0 0
4731 0 croak("Base nonce too long");
4732 }
4733  
4734 0 bl = InitDataBytesLocker(aTHX_ nonce_size);
4735 0 memcpy(bl->bytes, prev_nonce, prev_nonce_len);
4736 0 sodium_memzero(bl->bytes + prev_nonce_len, bl->length - prev_nonce_len);
4737 }
4738 else {
4739 0 croak("Base nonce invalid");
4740 }
4741 }
4742 else {
4743 3 bl = InitDataBytesLocker(aTHX_ nonce_size);
4744 3 randombytes_buf(bl->bytes, bl->length);
4745 }
4746 3 RETVAL = DataBytesLocker2SV(aTHX_ bl);
4747 OUTPUT:
4748 RETVAL
4749  
4750  
4751 void
4752 bytes(self, length, nonce, key)
4753 SV * self
4754 SV * length
4755 SV * nonce
4756 SV * key
4757 ALIAS:
4758 chacha20_bytes = 1
4759 salsa20_bytes = 2
4760 aes128ctr_bytes = 3
4761 salsa2012_bytes = 4
4762 salsa208_bytes = 5
4763 chacha20_ietf_bytes = 6
4764 INIT:
4765 STRLEN nonce_len;
4766 STRLEN key_len;
4767 unsigned char * nonce_buf;
4768 unsigned char * key_buf;
4769 unsigned int nonce_size;
4770 unsigned int key_size;
4771 unsigned int bytes_len;
4772 int (*bytes_function)(unsigned char *, unsigned long long, const unsigned char *, const unsigned char *);
4773 DataBytesLocker *bl;
4774 PPCODE:
4775 {
4776 PERL_UNUSED_VAR(self);
4777  
4778 16 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 16 16
4779 0 XSRETURN_EMPTY;
4780 }
4781  
4782 16 switch(ix) {
4783 8 case 1:
4784 8 nonce_size = crypto_stream_chacha20_NONCEBYTES;
4785 8 key_size = crypto_stream_chacha20_KEYBYTES;
4786 8 bytes_function = &crypto_stream_chacha20;
4787 8 break;
4788 3 case 2:
4789 3 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4790 3 key_size = crypto_stream_salsa20_KEYBYTES;
4791 3 bytes_function = &crypto_stream_salsa20;
4792 3 break;
4793 0 case 3:
4794 #ifdef AES128CTR_IS_AVAILABLE
4795 nonce_size = crypto_stream_aes128ctr_NONCEBYTES;
4796 key_size = crypto_stream_aes128ctr_KEYBYTES;
4797 bytes_function = &crypto_stream_aes128ctr;
4798 #else
4799 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4800 #endif
4801 break;
4802 0 case 4:
4803 0 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4804 0 key_size = crypto_stream_salsa20_KEYBYTES;
4805 0 bytes_function = &crypto_stream_salsa2012;
4806 0 break;
4807 0 case 5:
4808 #ifdef INCLUDE_DEPRECATED
4809 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4810 key_size = crypto_stream_salsa20_KEYBYTES;
4811 bytes_function = &crypto_stream_salsa208;
4812 #else
4813 0 croak("crypto_stream_salsa208 was deprecated in v1.0.18 define INCLUDE_DEPRECATED to use");
4814 #endif
4815 break;
4816 1 case 6:
4817 1 nonce_size = crypto_stream_chacha20_IETF_NONCEBYTES;
4818 1 key_size = crypto_stream_chacha20_KEYBYTES;
4819 1 bytes_function = &crypto_stream_chacha20_ietf;
4820 1 break;
4821 4 default:
4822 4 nonce_size = crypto_stream_NONCEBYTES;
4823 4 key_size = crypto_stream_KEYBYTES;
4824 4 bytes_function = &crypto_stream;
4825 }
4826  
4827 16 bytes_len = SvUV(length);
4828  
4829 16 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
4830 16 ▶ if ( nonce_len != nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != nonce_size ) { 0 16 16
4831 0 croak("Invalid nonce");
4832 }
4833  
4834 16 key_buf = (unsigned char *)SvPV(key, key_len);
4835 16 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 16 16
4836 0 croak("Invalid key");
4837 }
4838  
4839 16 bl = InitDataBytesLocker(aTHX_ bytes_len);
4840  
4841 16 (*bytes_function)( bl->bytes, bytes_len, nonce_buf, key_buf);
4842  
4843 16 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 16 16
4844 16 XSRETURN(1);
4845 }
4846  
4847  
4848 void
4849 xor(self, msg, nonce, key)
4850 SV * self
4851 SV * msg
4852 SV * nonce
4853 SV * key
4854 ALIAS:
4855 chacha20_xor = 1
4856 salsa20_xor = 2
4857 aes128ctr_xor = 3
4858 salsa2012_xor = 4
4859 salsa208_xor = 5
4860 chacha20_ietf_xor = 6
4861 INIT:
4862 STRLEN msg_len;
4863 STRLEN nonce_len;
4864 STRLEN key_len;
4865 unsigned char * msg_buf;
4866 unsigned char * nonce_buf;
4867 unsigned char * key_buf;
4868 unsigned int nonce_size;
4869 unsigned int key_size;
4870 int (*xor_function)(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *);
4871 DataBytesLocker *bl;
4872 PPCODE:
4873 {
4874 PERL_UNUSED_VAR(self);
4875  
4876 19 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 19 19
4877 0 XSRETURN_EMPTY;
4878 }
4879  
4880 19 switch(ix) {
4881 7 case 1:
4882 7 nonce_size = crypto_stream_chacha20_NONCEBYTES;
4883 7 key_size = crypto_stream_chacha20_KEYBYTES;
4884 7 xor_function = &crypto_stream_chacha20_xor;
4885 7 break;
4886 4 case 2:
4887 4 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4888 4 key_size = crypto_stream_salsa20_KEYBYTES;
4889 4 xor_function = &crypto_stream_salsa20_xor;
4890 4 break;
4891 0 case 3:
4892 #ifdef AES128CTR_IS_AVAILABLE
4893 nonce_size = crypto_stream_aes128ctr_NONCEBYTES;
4894 key_size = crypto_stream_aes128ctr_KEYBYTES;
4895 xor_function = &crypto_stream_aes128ctr_xor;
4896 #else
4897 0 croak("AES128CTR is only available in libsodium v1.0.14 and below");
4898 #endif
4899 break;
4900 0 case 4:
4901 0 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4902 0 key_size = crypto_stream_salsa20_KEYBYTES;
4903 0 xor_function = &crypto_stream_salsa2012_xor;
4904 0 break;
4905 0 case 5:
4906 #ifdef INCLUDE_DEPRECATED
4907 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4908 key_size = crypto_stream_salsa20_KEYBYTES;
4909 xor_function = &crypto_stream_salsa208_xor;
4910 #else
4911 0 croak("crypto_stream_salsa208_xor was deprecated in v1.0.18 define INCLUDE_DEPRECATED to use");
4912 #endif
4913 break;
4914 3 case 6:
4915 3 nonce_size = crypto_stream_chacha20_IETF_NONCEBYTES;
4916 3 key_size = crypto_stream_chacha20_KEYBYTES;
4917 3 xor_function = &crypto_stream_chacha20_ietf_xor;
4918 3 break;
4919 5 default:
4920 5 nonce_size = crypto_stream_NONCEBYTES;
4921 5 key_size = crypto_stream_KEYBYTES;
4922 5 xor_function = &crypto_stream_xor;
4923 }
4924  
4925 19 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
4926 19 ▶ if ( nonce_len != nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != nonce_size ) { 0 19 19
4927 0 croak("Invalid nonce");
4928 }
4929  
4930 19 key_buf = (unsigned char *)SvPV(key, key_len);
4931 19 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 19 19
4932 0 croak("Invalid key");
4933 }
4934  
4935 19 msg_buf = (unsigned char *)SvPV(msg, msg_len);
4936  
4937 19 bl = InitDataBytesLocker(aTHX_ msg_len);
4938  
4939 19 (*xor_function)( bl->bytes, msg_buf, msg_len, nonce_buf, key_buf);
4940  
4941 19 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 19 19
4942 19 XSRETURN(1);
4943 }
4944  
4945  
4946 void
4947 xor_ic(self, msg, nonce, ic, key)
4948 SV * self
4949 SV * msg
4950 SV * nonce
4951 SV * ic
4952 SV * key
4953 ALIAS:
4954 chacha20_xor_ic = 1
4955 salsa20_xor_ic = 2
4956 INIT:
4957 STRLEN msg_len;
4958 STRLEN nonce_len;
4959 unsigned int bc;
4960 STRLEN key_len;
4961 unsigned char * msg_buf;
4962 unsigned char * nonce_buf;
4963 unsigned char * key_buf;
4964 unsigned int nonce_size;
4965 unsigned int key_size;
4966 int (*xor_ic_function)(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, uint64_t, const unsigned char *);
4967 DataBytesLocker *bl;
4968 PPCODE:
4969 {
4970 PERL_UNUSED_VAR(self);
4971  
4972 13 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 13 13
4973 0 XSRETURN_EMPTY;
4974 }
4975  
4976 13 switch(ix) {
4977 5 case 1:
4978 5 nonce_size = crypto_stream_chacha20_NONCEBYTES;
4979 5 key_size = crypto_stream_chacha20_KEYBYTES;
4980 5 xor_ic_function = &crypto_stream_chacha20_xor_ic;
4981 5 break;
4982 4 case 2:
4983 4 nonce_size = crypto_stream_salsa20_NONCEBYTES;
4984 4 key_size = crypto_stream_salsa20_KEYBYTES;
4985 4 xor_ic_function = &crypto_stream_salsa20_xor_ic;
4986 4 break;
4987 4 default:
4988 4 nonce_size = crypto_stream_NONCEBYTES;
4989 4 key_size = crypto_stream_KEYBYTES;
4990 4 xor_ic_function = &crypto_stream_xsalsa20_xor_ic;
4991 }
4992  
4993 13 bc = SvUV(ic);
4994  
4995 13 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
4996 13 ▶ if ( nonce_len != nonce_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != nonce_size ) { 0 13 13
4997 0 croak("Invalid nonce");
4998 }
4999  
5000 13 key_buf = (unsigned char *)SvPV(key, key_len);
5001 13 ▶ if ( key_len != key_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != key_size ) { 0 13 13
5002 0 croak("Invalid key");
5003 }
5004  
5005 13 msg_buf = (unsigned char *)SvPV(msg, msg_len);
5006  
5007 13 bl = InitDataBytesLocker(aTHX_ msg_len);
5008  
5009 13 (*xor_ic_function)( bl->bytes, msg_buf, msg_len, nonce_buf, bc, key_buf);
5010  
5011 13 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 13 13
5012 13 XSRETURN(1);
5013 }
5014  
5015 void
5016 chacha20_ietf_xor_ic(self, msg, nonce, ic, key)
5017 SV * self
5018 SV * msg
5019 SV * nonce
5020 SV * ic
5021 SV * key
5022 INIT:
5023 STRLEN msg_len;
5024 STRLEN nonce_len;
5025 unsigned int bc;
5026 STRLEN key_len;
5027 unsigned char * msg_buf;
5028 unsigned char * nonce_buf;
5029 unsigned char * key_buf;
5030 DataBytesLocker *bl;
5031 PPCODE:
5032 {
5033 PERL_UNUSED_VAR(self);
5034  
5035 9 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 9 9
5036 0 XSRETURN_EMPTY;
5037 }
5038  
5039 9 bc = SvUV(ic);
5040  
5041 9 nonce_buf = (unsigned char *)SvPV(nonce, nonce_len);
5042 9 ▶ if ( nonce_len != crypto_stream_chacha20_IETF_NONCEBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nonce_len != crypto_stream_chacha20_IETF_NONCEBYTES ) { 0 9 9
5043 0 croak("Invalid nonce");
5044 }
5045  
5046 9 key_buf = (unsigned char *)SvPV(key, key_len);
5047 9 ▶ if ( key_len != crypto_stream_chacha20_KEYBYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( key_len != crypto_stream_chacha20_KEYBYTES ) { 0 9 9
5048 0 croak("Invalid key");
5049 }
5050  
5051 9 msg_buf = (unsigned char *)SvPV(msg, msg_len);
5052  
5053 9 bl = InitDataBytesLocker(aTHX_ msg_len);
5054  
5055 9 crypto_stream_chacha20_ietf_xor_ic( bl->bytes, msg_buf, msg_len, nonce_buf, bc, key_buf);
5056  
5057 9 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 9 9
5058 9 XSRETURN(1);
5059 }
5060  
5061 MODULE = Crypt::NaCl::Sodium PACKAGE = Data::BytesLocker
5062  
5063 FALLBACK: FALSE
5064  
5065 SV *
5066 new(class, bytes, ...)
5067 SV * class
5068 SV * bytes
5069 PROTOTYPE: $$;%
5070 INIT:
5071 DataBytesLocker *bl;
5072 unsigned char *buf;
5073 STRLEN buf_len;
5074 1015 int wipe = 0;
5075 1015 int readonly = 0;
5076 CODE:
5077 {
5078 1015 ▶ if ( SvREADONLY(bytes) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvREADONLY(bytes) ) { 6 1009 1015
5079 6 buf = (unsigned char *)SvPV(bytes, buf_len);
5080 6 readonly = 1;
5081 } else {
5082 1009 buf = (unsigned char *)SvPV_force(bytes, buf_len);
5083 }
5084  
5085 1015 ▶ if ( items > 2 && items != 4 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( items > 2 && items != 4 ) { 3 1012 1015
if ( items > 2 && items != 4 ) { 0 3 3
5086 0 croak("Invalid number of arguments");
5087 1015 ▶ } else if ( items > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( items > 2 ) { 3 1012 1015
5088 3 int i = 0;
5089 3 STRLEN keylen = 0;
5090 char * key;
5091  
5092 5 ▶ for ( i = 2; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 2; i < items; i += 2 ) { 3 2 5
5093 3 key = SvPV(ST(i), keylen);
5094 3 ▶ if ( keylen == 4 && strnEQ(key, "wipe", 4) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( keylen == 4 && strnEQ(key, "wipe", 4) ) { 3 0 3
if ( keylen == 4 && strnEQ(key, "wipe", 4) ) { 3 0 3
5095 3 wipe = SvTRUE(ST(i+1));
5096 3 ▶ if ( wipe && readonly ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( wipe && readonly ) { 3 0 3
if ( wipe && readonly ) { 1 2 3
5097 1 croak("Modification of a read-only value attempted");
5098 }
5099 } else {
5100 0 croak("Invalid argument: %s", key);
5101 }
5102 }
5103 }
5104  
5105 1014 bl = InitDataBytesLocker(aTHX_ buf_len);
5106 1014 memcpy(bl->bytes, buf, buf_len);
5107  
5108 1014 ▶ if ( wipe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( wipe ) { 2 1012 1014
5109 2 sodium_memzero( buf, buf_len);
5110 }
5111  
5112 1014 RETVAL = DataBytesLocker2SV(aTHX_ bl);
5113 }
5114 OUTPUT:
5115 RETVAL
5116  
5117 SV *
5118 _overload_mult(self, other, swapped)
5119 SV * self
5120 SV * other
5121 SV * swapped
5122 PREINIT:
5123 8 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5124 INIT:
5125 DataBytesLocker *bl;
5126 8 UV count = 0;
5127 8 UV cur = 0;
5128 OVERLOAD: x
5129 CODE:
5130 {
5131 8 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 8 8
5132 0 croak("Unlock BytesLocker object before accessing the data");
5133 }
5134  
5135 8 count = SvUV(other);
5136  
5137 8 ▶ if (sbl->length && count > SIZE_MAX / sbl->length) {
Branch: 2 branches
BranchTrueFalseTotal
if (sbl->length && count > SIZE_MAX / sbl->length) { 8 0 8
if (sbl->length && count > SIZE_MAX / sbl->length) { 0 8 8
5138 0 croak("Repetition length exceeds system memory limit (size_t overflow)");
5139 }
5140  
5141 8 bl = InitDataBytesLocker(aTHX_ sbl->length * count);
5142  
5143 48 ▶ while(count--) {
Branch: 1 branch
BranchTrueFalseTotal
while(count--) { 40 8 48
5144 40 memcpy(bl->bytes + sbl->length * cur++, sbl->bytes, sbl->length);
5145 }
5146  
5147 8 RETVAL = DataBytesLocker2SV(aTHX_ bl);
5148 }
5149 OUTPUT:
5150 RETVAL
5151  
5152  
5153 SV *
5154 _overload_concat(self, other, swapped)
5155 SV * self
5156 SV * other
5157 SV * swapped
5158 PREINIT:
5159 7 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5160 INIT:
5161 unsigned char *buf;
5162 STRLEN buf_len;
5163 DataBytesLocker *bl;
5164 OVERLOAD: .
5165 CODE:
5166 {
5167 7 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 7 7
5168 0 croak("Unlock BytesLocker object before accessing the data");
5169 }
5170  
5171 7 buf = (unsigned char *)SvPV(other, buf_len);
5172  
5173 7 ▶ if (buf_len > SIZE_MAX - sbl->length) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len > SIZE_MAX - sbl->length) { 0 7 7
5174 0 croak("Concatenated length exceeds system memory limit (size_t overflow)");
5175 }
5176  
5177 7 bl = InitDataBytesLocker(aTHX_ sbl->length + buf_len);
5178  
5179 7 ▶ if ( SvTRUE(swapped) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTRUE(swapped) ) { 2 5 7
5180 2 memcpy(memcpy(bl->bytes, buf, buf_len) + buf_len, sbl->bytes, sbl->length);
5181 }
5182 else {
5183 5 memcpy(memcpy(bl->bytes, sbl->bytes, sbl->length) + sbl->length, buf, buf_len);
5184 }
5185  
5186 7 RETVAL = DataBytesLocker2SV(aTHX_ bl);
5187 }
5188 OUTPUT:
5189 RETVAL
5190  
5191 void
5192 _overload_bool(self, ...)
5193 SV * self
5194 PREINIT:
5195 6766 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5196 INIT:
5197 int res;
5198 OVERLOAD: bool
5199 PPCODE:
5200 {
5201  
5202 6766 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 6766 6766
5203 0 croak("Unlock BytesLocker object before accessing the data");
5204 }
5205  
5206 6766 ▶ if ( sbl->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length ) { 6766 0 6766
5207 6766 res = 1;
5208 } else {
5209 0 res = 0;
5210 }
5211  
5212 6766 ▶ if ( res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( res ) { 6766 0 6766
5213 6766 XSRETURN_YES;
5214 } else {
5215 0 XSRETURN_NO;
5216 }
5217 }
5218  
5219 void
5220 _overload_not(self, ...)
5221 SV * self
5222 PREINIT:
5223 0 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5224 INIT:
5225 int res;
5226 OVERLOAD: !
5227 PPCODE:
5228 {
5229  
5230 0 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 0 0
5231 0 croak("Unlock BytesLocker object before accessing the data");
5232 }
5233  
5234 0 ▶ if ( sbl->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length ) { 0 0 0
5235 0 res = 0;
5236 } else {
5237 0 res = 1;
5238 }
5239  
5240 0 ▶ if ( res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( res ) { 0 0 0
5241 0 XSRETURN_YES;
5242 } else {
5243 0 XSRETURN_NO;
5244 }
5245 }
5246  
5247 void
5248 _overload_eq(self, other, swapped)
5249 SV * self
5250 SV * other
5251 SV * swapped
5252 PREINIT:
5253 1589 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5254 INIT:
5255 unsigned char *buf;
5256 STRLEN buf_len;
5257 int res;
5258 OVERLOAD: eq
5259 PPCODE:
5260 {
5261 1589 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 1589 1589
5262 0 croak("Unlock BytesLocker object before accessing the data");
5263 }
5264  
5265 1589 buf = (unsigned char *)SvPV(other, buf_len);
5266  
5267 1589 ▶ if ( sbl->length != buf_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length != buf_len ) { 0 1589 1589
5268 0 croak("Variables of unequal length cannot be automatically compared. Please use memcmp() with the length argument provided");
5269 }
5270  
5271 1589 ▶ if ( sodium_memcmp(sbl->bytes, buf, sbl->length) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_memcmp(sbl->bytes, buf, sbl->length) == 0 ) { 1589 0 1589
5272 1589 res = 1;
5273 } else {
5274 0 res = 0;
5275 }
5276  
5277 1589 ▶ if ( res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( res ) { 1589 0 1589
5278 1589 XSRETURN_YES;
5279 } else {
5280 0 XSRETURN_NO;
5281 }
5282 }
5283  
5284  
5285 void
5286 _overload_ne(self, other, swapped)
5287 SV * self
5288 SV * other
5289 SV * swapped
5290 PREINIT:
5291 1026 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5292 INIT:
5293 unsigned char *buf;
5294 STRLEN buf_len;
5295 int res;
5296 OVERLOAD: ne
5297 PPCODE:
5298 {
5299 1026 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 1026 1026
5300 0 croak("Unlock BytesLocker object before accessing the data");
5301 }
5302  
5303 1026 buf = (unsigned char *)SvPV(other, buf_len);
5304  
5305 1026 ▶ if ( sbl->length != buf_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length != buf_len ) { 0 1026 1026
5306 0 croak("Variables of unequal length cannot be automatically compared. Please use memcmp() with the length argument provided");
5307 }
5308  
5309 1026 ▶ if ( sodium_memcmp(sbl->bytes, buf, sbl->length) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_memcmp(sbl->bytes, buf, sbl->length) == 0 ) { 2 1024 1026
5310 2 res = 0;
5311 } else {
5312 1024 res = 1;
5313 }
5314  
5315 1026 ▶ if ( res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( res ) { 1024 2 1026
5316 1024 XSRETURN_YES;
5317 } else {
5318 2 XSRETURN_NO;
5319 }
5320 }
5321  
5322 void
5323 _overload_str(self, ...)
5324 SV * self
5325 PREINIT:
5326 300267 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5327 INIT:
5328 SV * pv;
5329 OVERLOAD: \"\"
5330 PPCODE:
5331 {
5332 300267 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 13 300254 300267
5333 13 croak("Unlock BytesLocker object before accessing the data");
5334 }
5335  
5336 300254 pv = newSVpvn((unsigned char *)sbl->bytes, sbl->length);
5337 300254 SvREADONLY_on(pv);
5338  
5339 300254 ▶ mXPUSHs(pv);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(pv); 0 300254 300254
5340 }
5341  
5342 void
5343 _overload_nomethod(self, ...)
5344 SV * self
5345 OVERLOAD: nomethod
5346 INIT:
5347 char * operator;
5348 PPCODE:
5349 {
5350 10 operator = SvPV_nolen(ST(3));
5351 10 croak("Operation \"%s\" is not supported", operator);
5352 }
5353  
5354 SV *
5355 clone(self)
5356 SV * self
5357 PREINIT:
5358 1 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5359 INIT:
5360 DataBytesLocker *bl;
5361 CODE:
5362 {
5363 1 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 1 1
5364 0 croak("Unlock BytesLocker object before accessing the data");
5365 }
5366  
5367 1 bl = InitDataBytesLocker(aTHX_ sbl->length);
5368  
5369 1 memcpy(bl->bytes, sbl->bytes, sbl->length);
5370  
5371 1 RETVAL = DataBytesLocker2SV(aTHX_ bl);
5372 }
5373 OUTPUT:
5374 RETVAL
5375  
5376 void
5377 lock(self)
5378 SV * self
5379 PREINIT:
5380 4 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5381 INIT:
5382 int rc;
5383 PPCODE:
5384 {
5385 4 rc = sodium_mprotect_noaccess((void *)sbl->bytes);
5386  
5387 4 ▶ if (rc == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0 ) { 4 0 4
5388 4 sbl->locked = 1;
5389 4 XSRETURN_YES;
5390 }
5391  
5392 0 croak("Unable to lock memory: %s", Strerror(errno));
5393 }
5394  
5395 void
5396 unlock(self)
5397 SV * self
5398 PREINIT:
5399 13 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5400 INIT:
5401 int rc;
5402 PPCODE:
5403 {
5404 13 rc = sodium_mprotect_readonly((void *)sbl->bytes);
5405  
5406 13 ▶ if (rc == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0 ) { 13 0 13
5407 13 sbl->locked = 0;
5408 13 XSRETURN_YES;
5409 }
5410  
5411 0 croak("Unable to unlock memory: %s", Strerror(errno));
5412 }
5413  
5414 SV *
5415 length(self)
5416 SV * self
5417 PREINIT:
5418 690 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5419 CODE:
5420 {
5421 690 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 690 690
5422 0 croak("Unlock BytesLocker object before accessing the data");
5423 }
5424  
5425 690 RETVAL = newSVuv((UV)sbl->length);
5426 }
5427 OUTPUT:
5428 RETVAL
5429  
5430 void
5431 is_locked(self, ...)
5432 SV * self
5433 PREINIT:
5434 7 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5435 PPCODE:
5436 {
5437 7 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 4 3 7
5438 4 XSRETURN_YES;
5439 } else {
5440 3 XSRETURN_NO;
5441 }
5442 }
5443  
5444 SV *
5445 to_hex(self)
5446 SV * self
5447 PREINIT:
5448 6304 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5449 INIT:
5450 char * hex;
5451 size_t hex_len;
5452 CODE:
5453 {
5454 6304 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 6304 6304
5455 0 croak("Unlock BytesLocker object before accessing the data");
5456 }
5457  
5458 6304 hex_len = sbl->length * 2;
5459 6304 hex = sodium_malloc(hex_len + 1);
5460 6304 ▶ if ( hex == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hex == NULL ) { 0 6304 6304
5461 0 croak("Could not allocate memory");
5462 }
5463 6304 sodium_bin2hex(hex, hex_len + 1, sbl->bytes, sbl->length);
5464  
5465 6304 RETVAL = newSVpvn((const char * const)hex, hex_len);
5466 }
5467 OUTPUT:
5468 RETVAL
5469 CLEANUP:
5470 6304 sodium_free(hex);
5471  
5472 void
5473 bytes(self)
5474 SV * self
5475 PREINIT:
5476 3464 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5477 INIT:
5478 SV * pv;
5479 PPCODE:
5480 {
5481 3464 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 1 3463 3464
5482 1 croak("Unlock BytesLocker object before accessing the data");
5483 }
5484  
5485 3463 pv = newSVpvn((unsigned char *)sbl->bytes, sbl->length);
5486  
5487 3463 ▶ mXPUSHs(pv);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(pv); 0 3463 3463
5488 }
5489  
5490 void
5491 memcmp(self, bytes, length = 0)
5492 SV * self
5493 SV * bytes
5494 unsigned long length
5495 PREINIT:
5496 1005 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5497 INIT:
5498 unsigned char * bytes_buf;
5499 STRLEN bytes_len;
5500 PPCODE:
5501 {
5502 1005 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 1005 1005
5503 0 XSRETURN_EMPTY;
5504 }
5505  
5506 1005 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 1005 1005
5507 0 croak("Unlock BytesLocker object before accessing the data");
5508 }
5509  
5510 1005 ▶ if (sv_derived_from(bytes, "Data::BytesLocker")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(bytes, "Data::BytesLocker")) { 0 1005 1005
5511 0 DataBytesLocker* rbl = GetBytesLocker(aTHX_ bytes);
5512 0 ▶ if ( rbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rbl->locked ) { 0 0 0
5513 0 croak("Unlock BytesLocker object before accessing the data");
5514 }
5515 0 bytes_buf = rbl->bytes;
5516 0 bytes_len = rbl->length;
5517 }
5518 else {
5519 1005 bytes_buf = (unsigned char *)SvPV(bytes, bytes_len);
5520 }
5521  
5522 1005 ▶ if ( length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length == 0 ) { 2 1003 1005
5523 2 ▶ if ( sbl->length != bytes_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length != bytes_len ) { 1 1 2
5524 1 croak("Variables of unequal length cannot be automatically compared. Please provide the length argument");
5525 }
5526 1 length = bytes_len;
5527 } else {
5528 1003 ▶ if ( length > sbl->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length > sbl->length ) { 1 1002 1003
5529 1 croak("The data is shorter then requested length");
5530 }
5531 1002 ▶ else if ( length > bytes_len ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( length > bytes_len ) { 1 1001 1002
5532 1 croak("The argument is shorter then requested length");
5533 }
5534 }
5535  
5536 1002 ▶ if ( sodium_memcmp(sbl->bytes, bytes_buf, length) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_memcmp(sbl->bytes, bytes_buf, length) == 0 ) { 1 1001 1002
5537 1 XSRETURN_YES;
5538 } else {
5539 1001 XSRETURN_NO;
5540 }
5541 }
5542  
5543  
5544 void
5545 compare(self, num, length = 0)
5546 SV * self
5547 SV * num
5548 unsigned long length
5549 PREINIT:
5550 2005 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5551 INIT:
5552 unsigned char * num_buf;
5553 STRLEN num_len;
5554 PPCODE:
5555 {
5556 2005 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 2005 2005
5557 0 XSRETURN_EMPTY;
5558 }
5559  
5560 2005 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 2005 2005
5561 0 croak("Unlock BytesLocker object before accessing the data");
5562 }
5563  
5564 2005 ▶ if (sv_derived_from(num, "Data::BytesLocker")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(num, "Data::BytesLocker")) { 1001 1004 2005
5565 1001 DataBytesLocker* rbl = GetBytesLocker(aTHX_ num);
5566 1001 ▶ if ( rbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rbl->locked ) { 0 1001 1001
5567 0 croak("Unlock BytesLocker object before accessing the data");
5568 }
5569 1001 num_buf = rbl->bytes;
5570 1001 num_len = rbl->length;
5571 }
5572 else {
5573 1004 num_buf = (unsigned char *)SvPV(num, num_len);
5574 }
5575  
5576 2005 ▶ if ( length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length == 0 ) { 2 2003 2005
5577 2 ▶ if ( sbl->length != num_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length != num_len ) { 1 1 2
5578 1 croak("Variables of unequal length cannot be automatically compared. Please provide the length argument");
5579 }
5580 1 length = num_len;
5581 } else {
5582 2003 ▶ if ( length > sbl->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length > sbl->length ) { 1 2002 2003
5583 1 croak("The data is shorter then requested length");
5584 }
5585 2002 ▶ else if ( length > num_len ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( length > num_len ) { 1 2001 2002
5586 1 croak("The argument is shorter then requested length");
5587 }
5588 }
5589  
5590 2002 XSRETURN_IV( sodium_compare(sbl->bytes, num_buf, length) );
5591 }
5592  
5593 void
5594 increment(self)
5595 SV * self
5596 PREINIT:
5597 4425 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5598 INIT:
5599 SV * pv;
5600 DataBytesLocker *bl;
5601 PPCODE:
5602 {
5603 4425 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 4425 4425
5604 0 XSRETURN_EMPTY;
5605 }
5606  
5607 4425 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 4425 4425
5608 0 croak("Unlock BytesLocker object before accessing the data");
5609 }
5610  
5611 4425 bl = InitDataBytesLocker(aTHX_ sbl->length);
5612  
5613 4425 memcpy(bl->bytes, sbl->bytes, sbl->length);
5614  
5615 4425 sodium_increment(bl->bytes, sbl->length);
5616  
5617 4425 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 4425 4425
5618  
5619 4425 XSRETURN(1);
5620 }
5621  
5622 void
5623 add(self, num, ...)
5624 SV * self
5625 SV * num
5626 PREINIT:
5627 7 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5628 INIT:
5629 unsigned char * num_buf;
5630 STRLEN num_len;
5631 STRLEN inc_len;
5632 DataBytesLocker *bl;
5633 PPCODE:
5634 {
5635 7 ▶ if ( GIMME_V == G_VOID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GIMME_V == G_VOID ) { 0 7 7
5636 0 XSRETURN_EMPTY;
5637 }
5638  
5639 7 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 7 7
5640 0 croak("Unlock BytesLocker object before accessing the data");
5641 }
5642  
5643 7 ▶ if (sv_derived_from(num, "Data::BytesLocker")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(num, "Data::BytesLocker")) { 7 0 7
5644 7 DataBytesLocker* rbl = GetBytesLocker(aTHX_ num);
5645 7 ▶ if ( rbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rbl->locked ) { 0 7 7
5646 0 croak("Unlock BytesLocker object before accessing the data");
5647 }
5648 7 num_buf = rbl->bytes;
5649 7 num_len = rbl->length;
5650 }
5651 else {
5652 0 num_buf = (unsigned char *)SvPV(num, num_len);
5653 }
5654  
5655 7 ▶ if ( items == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items == 3 ) { 6 1 7
5656 6 inc_len = (STRLEN)SvUV(ST(2));
5657 6 ▶ if ( inc_len > sbl->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( inc_len > sbl->length ) { 0 6 6
5658 0 croak("The data is shorter then requested length");
5659 }
5660 6 ▶ else if ( inc_len > num_len ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( inc_len > num_len ) { 0 6 6
5661 0 croak("The argument is shorter then requested length");
5662 }
5663 } else {
5664 1 ▶ if ( sbl->length != num_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->length != num_len ) { 0 1 1
5665 0 croak("Length of argument has to be equal to the length of data. Please provide the length argument");
5666 }
5667 1 inc_len = num_len;
5668 }
5669  
5670 7 bl = InitDataBytesLocker(aTHX_ sbl->length);
5671  
5672 7 memcpy(bl->bytes, sbl->bytes, sbl->length);
5673  
5674 7 sodium_add( bl->bytes, num_buf, inc_len );
5675  
5676 7 ▶ mXPUSHs( DataBytesLocker2SV(aTHX_ bl) );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs( DataBytesLocker2SV(aTHX_ bl) ); 0 7 7
5677  
5678 7 XSRETURN(1);
5679 }
5680  
5681 void
5682 is_zero(self, ...)
5683 SV * self
5684 PREINIT:
5685 2 DataBytesLocker* sbl = GetBytesLocker(aTHX_ self);
5686 INIT:
5687 SV * pv;
5688 PPCODE:
5689 {
5690 2 ▶ if ( sbl->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sbl->locked ) { 0 2 2
5691 0 croak("Unlock BytesLocker object before accessing the data");
5692 }
5693  
5694 2 ▶ if ( sodium_is_zero(sbl->bytes, sbl->length) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( sodium_is_zero(sbl->bytes, sbl->length) == 1 ) { 1 1 2
5695 1 XSRETURN_YES;
5696 }
5697 1 XSRETURN_NO;
5698 }
5699  
5700  
5701 void
5702 DESTROY(self)
5703 SV * self
5704 PREINIT:
5705 114129 DataBytesLocker* bl = GetBytesLocker(aTHX_ self);
5706 PPCODE:
5707 {
5708 114129 sodium_free( bl->bytes );
5709 114129 Safefree(bl);
5710 }
« blib/lib/Crypt/NaCl/Sodium.pm ↑ Summary