xs/hash.xs

filter: Statementstmt 85.2% 185 / 217 Branchbran 44.4% 24 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.1% 209 / 271 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/Sodium/XS.pm ↑ Summary xs/shorthash.xs »
1 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::hash
2  
3 void _define_constants()
4 PREINIT:
5 1 HV *stash = gv_stashpv("Crypt::Sodium::XS::hash", 0);
6  
7 PPCODE:
8 1 newCONSTSUB(stash, "hash_BYTES", newSVuv(crypto_hash_BYTES));
9 1 newCONSTSUB(stash, "hash_sha256_BYTES", newSVuv(crypto_hash_sha256_BYTES));
10 1 newCONSTSUB(stash, "hash_sha512_BYTES", newSVuv(crypto_hash_sha512_BYTES));
11 1 newCONSTSUB(stash, "hash_PRIMITIVE", newSVpvs(crypto_hash_PRIMITIVE));
12 #ifdef SODIUM_HAS_SHA3
13 1 newCONSTSUB(stash, "hash_sha3256_BYTES", newSVuv(crypto_hash_sha3256_BYTES));
14 1 newCONSTSUB(stash, "hash_sha3512_BYTES", newSVuv(crypto_hash_sha3512_BYTES));
15 1 newCONSTSUB(stash, "hash_sha3_available", &PL_sv_yes);
16 #else
17 newCONSTSUB(stash, "hash_sha3_available", &PL_sv_no);
18 #endif
19  
20 SV * hash(SV * msg)
21  
22 ALIAS:
23 hash_sha256 = 1
24 hash_sha512 = 2
25 hash_sha3256 = 3
26 hash_sha3512 = 4
27  
28 PREINIT:
29 10 protmem *msg_pm = NULL;
30 unsigned char *msg_buf, *out_buf;
31 STRLEN msg_len, out_len;
32 int (*func)(unsigned char *, const unsigned char *, unsigned long long);
33  
34 CODE:
35 10 switch(ix) {
36 2 case 1:
37 2 out_len = crypto_hash_sha256_BYTES;
38 2 func = crypto_hash_sha256;
39 2 break;
40 2 case 2:
41 2 out_len = crypto_hash_sha512_BYTES;
42 2 func = crypto_hash_sha512;
43 2 break;
44 2 case 3:
45 #ifdef SODIUM_HAS_SHA3
46 2 out_len = crypto_hash_sha3256_BYTES;
47 2 func = crypto_hash_sha3256;
48 #else
49 croak("SHA3 not supported by this version of libsodium");
50 #endif
51 2 break;
52 2 case 4:
53 #ifdef SODIUM_HAS_SHA3
54 2 out_len = crypto_hash_sha3512_BYTES;
55 2 func = crypto_hash_sha3512;
56 #else
57 croak("SHA3 not supported by this version of libsodium");
58 #endif
59 2 break;
60 2 default:
61 2 out_len = crypto_hash_BYTES;
62 2 func = crypto_hash;
63 }
64  
65 10 ▶ if (sv_derived_from(msg, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(msg, MEMVAULT_CLASS)) { 0 10 10
66 0 msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS);
67 0 msg_buf = msg_pm->pm_ptr;
68 0 msg_len = msg_pm->size;
69 }
70 else
71 10 msg_buf = (unsigned char *)SvPVbyte(msg, msg_len);
72  
73 10 Newx(out_buf, out_len + 1, unsigned char);
74 10 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 10 10
75 0 croak("hash: Failed to allocate memory");
76 10 out_buf[out_len] = '\0';
77  
78 10 ▶ if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 10 10
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
79 0 Safefree(out_buf);
80 0 croak("hash: Failed to grant msg protmem RO");
81 }
82  
83 10 func(out_buf, msg_buf, msg_len);
84  
85 10 ▶ if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 10 10
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
86 0 Safefree(out_buf);
87 0 croak("hash: Failed to release msg protmem RO");
88 }
89  
90 10 RETVAL = newSV(0);
91 10 sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL);
92  
93 OUTPUT:
94 RETVAL
95  
96 =for documentation
97  
98 providing a generic hash_init even though upstream doesn't, using its default
99 algorithm.
100  
101 =cut
102  
103 SV * hash_init(SV * flags = &PL_sv_undef)
104  
105 ALIAS:
106 hash_sha256_init = 1
107 hash_sha512_init = 2
108 hash_sha3256_init = 3
109 hash_sha3512_init = 4
110  
111 PREINIT:
112 protmem *state_pm;
113 8 ▶ unsigned int state_flags = g_protmem_default_flags_state;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int state_flags = g_protmem_default_flags_state; 8 0 8
114  
115 CODE:
116 8 SvGETMAGIC(flags);
117 8 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 8 8
118 0 state_flags = SvUV_nomg(flags);
119  
120 8 switch(ix) {
121 2 case 1:
122 2 state_pm = protmem_init(aTHX_ sizeof(crypto_hash_sha256_state), state_flags);
123 2 ▶ if (state_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state_pm == NULL) 0 2 2
124 0 croak("hash_init: Failed to allocate protmem");
125 2 crypto_hash_sha256_init(state_pm->pm_ptr);
126 2 break;
127 2 case 3:
128 #ifdef SODIUM_HAS_SHA3
129 2 state_pm = protmem_init(aTHX_ sizeof(crypto_hash_sha3256_state), state_flags);
130 2 ▶ if (state_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state_pm == NULL) 0 2 2
131 0 croak("hash_init: Failed to allocate protmem");
132 2 crypto_hash_sha3256_init(state_pm->pm_ptr);
133 #else
134 croak("SHA3 not supported by this version of libsodium");
135 #endif
136 2 break;
137 2 case 4:
138 #ifdef SODIUM_HAS_SHA3
139 2 state_pm = protmem_init(aTHX_ sizeof(crypto_hash_sha3512_state), state_flags);
140 2 ▶ if (state_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state_pm == NULL) 0 2 2
141 0 croak("hash_init: Failed to allocate protmem");
142 2 crypto_hash_sha3512_init(state_pm->pm_ptr);
143 #else
144 croak("SHA3 not supported by this version of libsodium");
145 #endif
146 2 break;
147 2 default:
148 2 state_pm = protmem_init(aTHX_ sizeof(crypto_hash_sha512_state), state_flags);
149 2 ▶ if (state_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state_pm == NULL) 0 2 2
150 0 croak("hash_init: Failed to allocate protmem");
151 2 crypto_hash_sha512_init(state_pm->pm_ptr);
152 }
153  
154 8 ▶ if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
155 0 protmem_free(aTHX_ state_pm);
156 0 croak("hash_init: Failed to release protmem RW");
157 }
158  
159 8 switch(ix) {
160 2 case 1:
161 2 RETVAL = protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::hash::sha256_multi");
162 2 break;
163 2 case 3:
164 #ifdef SODIUM_HAS_SHA3
165 2 RETVAL = protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::hash::sha3256_multi");
166 #endif
167 2 break;
168 2 case 4:
169 #ifdef SODIUM_HAS_SHA3
170 2 RETVAL = protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::hash::sha3512_multi");
171 #endif
172 2 break;
173 2 default:
174 2 RETVAL = protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::hash::sha512_multi");
175 }
176  
177 OUTPUT:
178 RETVAL
179  
180 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::hash::sha256_multi
181  
182 void DESTROY(SV * self)
183  
184 ALIAS:
185 Crypt::Sodium::XS::hash::sha512_multi::DESTROY = 1
186 Crypt::Sodium::XS::hash::sha3256_multi::DESTROY = 2
187 Crypt::Sodium::XS::hash::sha3512_multi::DESTROY = 3
188  
189 PREINIT:
190 protmem *state_pm;
191  
192 CODE:
193 16 switch(ix) {
194 4 case 1:
195 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha512_multi");
196 4 break;
197 4 case 2:
198 #ifndef SODIUM_HAS_SHA3
199 croak("SHA3 not supported by this version of libsodium");
200 #endif
201 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3256_multi");
202 4 break;
203 4 case 3:
204 #ifndef SODIUM_HAS_SHA3
205 croak("SHA3 not supported by this version of libsodium");
206 #endif
207 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3512_multi");
208 4 break;
209 4 default:
210 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha256_multi");
211 }
212 16 protmem_free(aTHX_ state_pm);
213  
214 SV * clone(SV * self)
215  
216 ALIAS:
217 Crypt::Sodium::XS::hash::sha512_multi::clone = 1
218 Crypt::Sodium::XS::hash::sha3256_multi::clone = 2
219 Crypt::Sodium::XS::hash::sha3512_multi::clone = 3
220  
221 CODE:
222 8 switch(ix) {
223 2 case 1:
224 2 RETVAL = protmem_clone_sv(aTHX_ self, "Crypt::Sodium::XS::hash::sha512_multi");
225 2 break;
226 2 case 2:
227 #ifndef SODIUM_HAS_SHA3
228 croak("SHA3 not supported by this version of libsodium");
229 #endif
230 2 RETVAL = protmem_clone_sv(aTHX_ self, "Crypt::Sodium::XS::hash::sha3256_multi");
231 2 break;
232 2 case 3:
233 #ifndef SODIUM_HAS_SHA3
234 croak("SHA3 not supported by this version of libsodium");
235 #endif
236 2 RETVAL = protmem_clone_sv(aTHX_ self, "Crypt::Sodium::XS::hash::sha3512_multi");
237 2 break;
238 2 default:
239 2 RETVAL = protmem_clone_sv(aTHX_ self, "Crypt::Sodium::XS::hash::sha256_multi");
240 }
241  
242 OUTPUT:
243 RETVAL
244  
245 SV * final(SV * self)
246  
247 ALIAS:
248 Crypt::Sodium::XS::hash::sha512_multi::final = 1
249 Crypt::Sodium::XS::hash::sha3256_multi::final = 2
250 Crypt::Sodium::XS::hash::sha3512_multi::final = 3
251  
252 PREINIT:
253 protmem *state_pm;
254 unsigned char *out_buf;
255  
256 CODE:
257 16 switch(ix) {
258 4 case 1:
259 4 Newx(out_buf, crypto_hash_sha512_BYTES + 1, unsigned char);
260 4 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 4 4
261 0 croak("final: Failed to allocate memory");
262 4 out_buf[crypto_hash_sha512_BYTES] = '\0';
263 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha512_multi");
264 4 break;
265 4 case 2:
266 #ifdef SODIUM_HAS_SHA3
267 4 Newx(out_buf, crypto_hash_sha3256_BYTES + 1, unsigned char);
268 4 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 4 4
269 0 croak("final: Failed to allocate memory");
270 4 out_buf[crypto_hash_sha3256_BYTES] = '\0';
271 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3256_multi");
272 #else
273 croak("SHA3 not supported by this version of libsodium");
274 #endif
275 4 break;
276 4 case 3:
277 #ifdef SODIUM_HAS_SHA3
278 4 Newx(out_buf, crypto_hash_sha3512_BYTES + 1, unsigned char);
279 4 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 4 4
280 0 croak("final: Failed to allocate memory");
281 4 out_buf[crypto_hash_sha3512_BYTES] = '\0';
282 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3512_multi");
283 #else
284 croak("SHA3 not supported by this version of libsodium");
285 #endif
286 4 break;
287 4 default:
288 4 Newx(out_buf, crypto_hash_sha256_BYTES + 1, unsigned char);
289 4 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 4 4
290 0 croak("final: Failed to allocate memory");
291 4 out_buf[crypto_hash_sha256_BYTES] = '\0';
292 4 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha256_multi");
293 }
294 16 ▶ if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 16 16
295 0 Safefree(out_buf);
296 0 croak("final: Failed to grant protmem RW");
297 }
298  
299 16 switch(ix) {
300 4 case 1:
301 4 crypto_hash_sha512_final(state_pm->pm_ptr, out_buf);
302 4 break;
303 4 case 2:
304 #ifdef SODIUM_HAS_SHA3
305 4 crypto_hash_sha3256_final(state_pm->pm_ptr, out_buf);
306 #endif
307 4 break;
308 4 case 3:
309 #ifdef SODIUM_HAS_SHA3
310 4 crypto_hash_sha3512_final(state_pm->pm_ptr, out_buf);
311 #endif
312 4 break;
313 4 default:
314 4 crypto_hash_sha256_final(state_pm->pm_ptr, out_buf);
315 }
316  
317 16 ▶ if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 16 16
318 0 Safefree(out_buf);
319 0 croak("final: Failed to release protmem RW");
320 }
321  
322 16 RETVAL = newSV(0);
323 16 switch(ix) {
324 4 case 1:
325 4 sv_usepvn_flags(RETVAL, (char *)out_buf, crypto_hash_sha512_BYTES, SV_HAS_TRAILING_NUL);
326 4 break;
327 4 case 2:
328 #ifdef SODIUM_HAS_SHA3
329 4 sv_usepvn_flags(RETVAL, (char *)out_buf, crypto_hash_sha3256_BYTES, SV_HAS_TRAILING_NUL);
330 #endif
331 4 break;
332 4 case 3:
333 #ifdef SODIUM_HAS_SHA3
334 4 sv_usepvn_flags(RETVAL, (char *)out_buf, crypto_hash_sha3512_BYTES, SV_HAS_TRAILING_NUL);
335 #endif
336 4 break;
337 4 default:
338 4 sv_usepvn_flags(RETVAL, (char *)out_buf, crypto_hash_sha256_BYTES, SV_HAS_TRAILING_NUL);
339 }
340  
341 OUTPUT:
342 RETVAL
343  
344 void update(SV * self, ...)
345  
346 ALIAS:
347 Crypt::Sodium::XS::hash::sha512_multi::update = 1
348 Crypt::Sodium::XS::hash::sha3256_multi::update = 2
349 Crypt::Sodium::XS::hash::sha3512_multi::update = 3
350  
351 PREINIT:
352 264 protmem *state_pm, *msg_pm = NULL;
353 unsigned char *msg_buf;
354 STRLEN msg_len;
355 I32 i;
356  
357 PPCODE:
358 264 switch(ix) {
359 66 case 1:
360 66 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha512_multi");
361 66 break;
362 66 case 2:
363 #ifndef SODIUM_HAS_SHA3
364 croak("SHA3 not supported by this version of libsodium");
365 #endif
366 66 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3256_multi");
367 66 break;
368 66 case 3:
369 #ifndef SODIUM_HAS_SHA3
370 croak("SHA3 not supported by this version of libsodium");
371 #endif
372 66 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha3512_multi");
373 66 break;
374 66 default:
375 66 state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::hash::sha256_multi");
376 }
377 264 ▶ if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 264 264
378 0 croak("update: Failed to grant state protmem RW");
379  
380 528 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 264 264 528
381 264 ▶ if (sv_derived_from(ST(i), MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(ST(i), MEMVAULT_CLASS)) { 0 264 264
382 0 msg_pm = protmem_get(aTHX_ ST(i), MEMVAULT_CLASS);
383 0 msg_buf = msg_pm->pm_ptr;
384 0 msg_len = msg_pm->size;
385 }
386 else
387 528 msg_buf = (unsigned char *)SvPVbyte(ST(i), msg_len);
388  
389 264 ▶ if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 264 264
if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
390 0 protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW);
391 0 croak("update: Failed to grant msg protmem RO");
392 }
393  
394 264 switch(ix) {
395 66 case 1:
396 66 crypto_hash_sha512_update(state_pm->pm_ptr, msg_buf, msg_len);
397 66 break;
398 66 case 2:
399 #ifdef SODIUM_HAS_SHA3
400 66 crypto_hash_sha3256_update(state_pm->pm_ptr, msg_buf, msg_len);
401 #endif
402 66 break;
403 66 case 3:
404 #ifdef SODIUM_HAS_SHA3
405 66 crypto_hash_sha3512_update(state_pm->pm_ptr, msg_buf, msg_len);
406 #endif
407 66 break;
408 66 default:
409 66 crypto_hash_sha256_update(state_pm->pm_ptr, msg_buf, msg_len);
410 }
411  
412 264 ▶ if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 264 264
if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
413 0 protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW);
414 0 croak("update: Failed to release msg protmem RO");
415 }
416  
417 264 msg_pm = NULL;
418 }
419  
420 264 ▶ if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 264 264
421 0 croak("update: Failed to release state protmem RW");
422  
423 264 XSRETURN(1);
« blib/lib/Crypt/Sodium/XS.pm ↑ Summary xs/shorthash.xs »