xs/util.xs

filter: Statementstmt 77.2% 139 / 180 Branchbran 57.0% 57 / 100 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.0% 196 / 280 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/generichash.xs ↑ Summary xs/scalarmult.xs »
1 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::Util
2  
3 SV * sodium_has_ip_codecs()
4  
5 CODE:
6 #ifdef SODIUM_HAS_IP_CODECS
7 RETVAL = &PL_sv_yes;
8 #else
9 1 RETVAL = &PL_sv_no;
10 #endif
11  
12 OUTPUT:
13 RETVAL
14  
15 SV * sodium_add(SV * x, SV * y)
16  
17 PREINIT:
18 unsigned char *x_buf, *y_buf, *longer_buf, *shorter_buf, *out_buf;
19 STRLEN x_len, y_len, longer_len, shorter_len;
20  
21 CODE:
22 6 ▶ x_buf = (unsigned char *)SvPVbyte(x, x_len);
Branch: 1 branch
BranchTrueFalseTotal
x_buf = (unsigned char *)SvPVbyte(x, x_len); 6 0 6
23 6 y_buf = (unsigned char *)SvPVbyte(y, y_len);
24  
25 6 ▶ if (x_len < y_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (x_len < y_len) { 2 4 6
26 2 longer_buf = y_buf;
27 2 longer_len = y_len;
28 2 shorter_buf = x_buf;
29 2 shorter_len = x_len;
30 }
31 else {
32 /* these are fine for the case of equal lengths. */
33 4 longer_buf = x_buf;
34 4 longer_len = x_len;
35 4 shorter_buf = y_buf;
36 4 shorter_len = y_len;
37 }
38  
39 6 RETVAL = newSV(0);
40 6 Newxz(out_buf, longer_len + 1, unsigned char);
41 6 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 6 6
42 0 croak("Could not allocate memory");
43 6 memcpy(out_buf, shorter_buf, shorter_len);
44  
45 6 sodium_add(out_buf, longer_buf, longer_len);
46  
47 6 sv_usepvn_flags(RETVAL, (char *)out_buf, longer_len, SV_HAS_TRAILING_NUL);
48  
49 OUTPUT:
50 RETVAL
51  
52 SV * sodium_bin2hex(SV * bytes)
53  
54 PREINIT:
55 char *bytes_buf, *out_buf;
56 STRLEN bytes_len, out_len;
57  
58 CODE:
59 1 bytes_buf = SvPVbyte(bytes, bytes_len);
60 1 ▶ if (bytes_len > SIZE_MAX / 2)
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_len > SIZE_MAX / 2) 0 1 1
61 0 croak("Message too large.");
62 1 out_len = bytes_len * 2;
63 1 Newx(out_buf, out_len + 1, char);
64 1 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 1 1
65 0 croak("Failed to allocate memory");
66 1 out_buf[out_len] = '\0';
67 1 sodium_bin2hex(out_buf, out_len + 1, (unsigned char *)bytes_buf, bytes_len);
68 1 RETVAL = newSV(0);
69 1 sv_usepvn_flags(RETVAL, out_buf, out_len, SV_HAS_TRAILING_NUL);
70  
71 OUTPUT:
72 RETVAL
73  
74 SV * sodium_bin2ip(SV * bytes)
75  
76 PREINIT:
77 char *bytes_buf, ipstr_buf[46];
78 STRLEN bytes_len;
79  
80 CODE:
81 #ifdef SODIUM_HAS_IPCODECS
82 8 bytes_buf = SvPVbyte(bytes, bytes_len);
83 8 ▶ if (bytes_len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_len != 16) 0 8 8
84 0 croak("Invalid binary length (must be 16 bytes)");
85 8 ▶ if (sodium_bin2ip(ipstr_buf, sizeof ipstr_buf, (unsigned char *)bytes_buf) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_bin2ip(ipstr_buf, sizeof ipstr_buf, (unsigned char *)bytes_buf) == NULL) 0 8 8
86 0 croak("sodium_bin2ip failed");
87 8 RETVAL = newSVpvn(ipstr_buf, strlen(ipstr_buf));
88 #else
89 croak("sodium_bin2ip not supported by this version of libsodium");
90 #endif
91  
92 OUTPUT:
93 RETVAL
94  
95 SV * sodium_compare(SV * x, SV * y, STRLEN len = 0)
96  
97 PREINIT:
98 unsigned char *x_buf, *y_buf;
99 STRLEN x_len, y_len;
100  
101 CODE:
102 5 ▶ x_buf = (unsigned char *)SvPVbyte(x, x_len);
Branch: 1 branch
BranchTrueFalseTotal
x_buf = (unsigned char *)SvPVbyte(x, x_len); 5 0 5
103 5 y_buf = (unsigned char *)SvPVbyte(y, y_len);
104  
105 5 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 4 1 5
106 4 ▶ if (x_len != y_len)
Branch: 1 branch
BranchTrueFalseTotal
if (x_len != y_len) 1 3 4
107 1 croak("Length of operands must be equal without a length argument");
108 3 len = x_len;
109 }
110 else {
111 1 ▶ if (len > x_len)
Branch: 1 branch
BranchTrueFalseTotal
if (len > x_len) 0 1 1
112 0 croak("The first argument is shorter then requested length");
113 1 ▶ else if (len > y_len)
Branch: 1 branch
BranchTrueFalseTotal
else if (len > y_len) 0 1 1
114 0 croak("The second argument is shorter then requested length");
115 }
116  
117 4 RETVAL = newSViv(sodium_compare(x_buf, y_buf, len));
118  
119 OUTPUT:
120 RETVAL
121  
122 SV * sodium_hex2bin(SV * bytes)
123  
124 PREINIT:
125 char *bytes_buf, *out_buf;
126 STRLEN bytes_len, out_len;
127  
128 CODE:
129 3 bytes_buf = SvPVbyte(bytes, bytes_len);
130 /* should check for overflow... */
131 3 out_len = ((bytes_len + 1) & ~1) / 2;
132 3 Newx(out_buf, out_len + 1, char);
133 3 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 3 3
134 0 croak("Failed to allocate memory");
135 3 sodium_hex2bin((unsigned char *)out_buf, out_len, bytes_buf, bytes_len,
136 NULL, &out_len, NULL);
137 3 out_buf[out_len] = '\0';
138 3 RETVAL = newSV(0);
139 3 sv_usepvn_flags(RETVAL, out_buf, out_len, SV_HAS_TRAILING_NUL);
140  
141 OUTPUT:
142 RETVAL
143  
144 SV * sodium_increment(SV * val)
145  
146 PREINIT:
147 unsigned char *buf, *out_buf;
148 STRLEN buf_len;
149  
150 CODE:
151 45 ▶ if (SvREADONLY(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(val)) 0 45 45
152 0 croak("Cannot increment a read-only value");
153 45 buf = (unsigned char *)SvPVbyte(val, buf_len);
154  
155 45 ▶ if (buf_len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len < 1) 0 45 45
156 0 croak("Cannot increment a zero-length value");
157  
158 45 Newx(out_buf, buf_len + 1, unsigned char);
159 45 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 45 45
160 0 croak("Failed to allocate memeory");
161 45 out_buf[buf_len] = '\0';
162  
163 45 memcpy(out_buf, buf, buf_len);
164  
165 45 sodium_increment(out_buf, buf_len);
166  
167 45 RETVAL=newSV(0);
168 45 sv_usepvn_flags(RETVAL, (char *)out_buf, buf_len, SV_HAS_TRAILING_NUL);
169  
170 OUTPUT:
171 RETVAL
172  
173 SV * sodium_ip2bin(SV * ip)
174  
175 PREINIT:
176 char *ip_buf, out_buf[16];
177 STRLEN ip_len;
178  
179 CODE:
180 #ifdef SODIUM_HAS_IPCODECS
181 12 ip_buf = SvPVbyte(ip, ip_len);
182 12 ▶ if (sodium_ip2bin((unsigned char *)out_buf, ip_buf, ip_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_ip2bin((unsigned char *)out_buf, ip_buf, ip_len) != 0) 0 12 12
183 0 croak("sodium_ip2bin failed (invalid IP address?)");
184 12 RETVAL = newSVpvn(out_buf, sizeof out_buf);
185 #else
186 croak("sodium_ip2bin not supported by this version of libsodium");
187 #endif
188  
189 OUTPUT:
190 RETVAL
191  
192 SV * sodium_is_zero(SV * bytes)
193  
194 PREINIT:
195 unsigned char *buf;
196 STRLEN buf_len;
197  
198 CODE:
199 2 buf = (unsigned char *)SvPVbyte(bytes, buf_len);
200 2 ▶ if(sodium_is_zero(buf, buf_len))
Branch: 1 branch
BranchTrueFalseTotal
if(sodium_is_zero(buf, buf_len)) 1 1 2
201 1 RETVAL = &PL_sv_yes;
202 else
203 1 RETVAL = &PL_sv_no;
204  
205 OUTPUT:
206 RETVAL
207  
208 SV * sodium_memcmp(SV * x, SV * y, STRLEN len = 0)
209  
210 PREINIT:
211 unsigned char *x_buf, *y_buf;
212 STRLEN x_len, y_len;
213  
214 CODE:
215 4 ▶ x_buf = (unsigned char *)SvPVbyte(x, x_len);
Branch: 1 branch
BranchTrueFalseTotal
x_buf = (unsigned char *)SvPVbyte(x, x_len); 4 0 4
216 4 y_buf = (unsigned char *)SvPVbyte(y, y_len);
217  
218 4 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 3 1 4
219 3 ▶ if (x_len != y_len)
Branch: 1 branch
BranchTrueFalseTotal
if (x_len != y_len) 1 2 3
220 1 croak("Length of operands must be equal without a length argument");
221 2 len = x_len;
222 }
223 else {
224 1 ▶ if (len > x_len)
Branch: 1 branch
BranchTrueFalseTotal
if (len > x_len) 0 1 1
225 0 croak("The first argument is shorter then requested length");
226 1 ▶ else if (len > y_len)
Branch: 1 branch
BranchTrueFalseTotal
else if (len > y_len) 0 1 1
227 0 croak("The second argument is shorter then requested length");
228 }
229  
230 3 ▶ if (sodium_memcmp(x_buf, y_buf, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_memcmp(x_buf, y_buf, len) == 0) 2 1 3
231 2 RETVAL = &PL_sv_yes;
232 else
233 1 RETVAL = &PL_sv_no;
234  
235 OUTPUT:
236 RETVAL
237  
238 SV * sodium_pad(SV * buf, STRLEN blocksize)
239  
240 PREINIT:
241 unsigned char *buf_buf, *padded_buf;
242 STRLEN buf_len, pad_len, padded_len;
243  
244 CODE:
245 5 ▶ if (blocksize <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (blocksize <= 0) 0 5 5
246 0 croak("sodium_pad: Invalid blocksize <= 0");
247  
248 5 buf_buf = (unsigned char *)SvPVbyte(buf, buf_len);
249  
250 5 pad_len = blocksize - 1;
251 5 ▶ if ((blocksize & (blocksize - 1)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((blocksize & (blocksize - 1)) == 0) 1 4 5
252 1 pad_len -= buf_len & (blocksize - 1);
253 else
254 4 pad_len -= buf_len % blocksize;
255 5 pad_len += 1; /* for 0x80 */
256  
257 5 ▶ if ((STRLEN)SIZE_MAX - buf_len - 1 <= pad_len)
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)SIZE_MAX - buf_len - 1 <= pad_len) 0 5 5
258 0 croak("sodium_pad: Pad exceeds SIZE_MAX");
259 5 padded_len = buf_len + pad_len;
260  
261 5 Newx(padded_buf, padded_len + 1, unsigned char);
262 5 ▶ if (padded_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (padded_buf == NULL) 0 5 5
263 0 croak("sodium_pad: Failed to allocate memory");
264 5 padded_buf[padded_len] = '\0';
265 5 memcpy(padded_buf, buf_buf, buf_len);
266 5 ▶ if (sodium_pad(&padded_len, padded_buf, buf_len, blocksize, padded_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_pad(&padded_len, padded_buf, buf_len, blocksize, padded_len) != 0) 0 5 5
267 /* should be impossible */
268 0 croak("BUG: sodium_pad: sodium_pad returned error");
269  
270 5 RETVAL=newSV(0);
271 5 sv_usepvn_flags(RETVAL, (char *)padded_buf, padded_len, SV_HAS_TRAILING_NUL);
272  
273 OUTPUT:
274 RETVAL
275  
276 SV * sodium_random_bytes( \
277 STRLEN out_len, \
278 SV * use_memvault = &PL_sv_undef, \
279 SV * flags = &PL_sv_undef \
280 )
281  
282 PREINIT:
283 2151 int mv_flags = g_protmem_default_flags_memvault;
284  
285 CODE:
286 2151 ▶ if (SvTRUE(use_memvault)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(use_memvault)) { 0 2151 2151
287 0 SvGETMAGIC(flags);
288 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
289 0 mv_flags = SvUV_nomg(flags);
290 protmem *new_pm;
291 0 new_pm = protmem_init(aTHX_ out_len, mv_flags);
292 0 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 0 0
293 0 croak("Failed to allocate protmem");
294 0 ▶ if (out_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (out_len > 0) 0 0 0
295 0 randombytes_buf(new_pm->pm_ptr, out_len);
296 0 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 0 0
297 0 protmem_free(aTHX_ new_pm);
298 0 croak("Failed to release protmem RW");
299 }
300 0 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
301 }
302 else {
303 unsigned char *out_buf;
304  
305 2151 Newx(out_buf, out_len + 1, unsigned char);
306 2151 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 2151 2151
307 0 croak("Failed to allocate memory");
308 2151 out_buf[out_len] = '\0';
309  
310 2151 ▶ if (out_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (out_len > 0) 2151 0 2151
311 2151 randombytes_buf(out_buf, out_len);
312  
313 2151 RETVAL = newSV(0);
314 2151 sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL);
315 }
316  
317 OUTPUT:
318 RETVAL
319  
320 SV * sodium_sub(SV * x, SV * y)
321  
322 PREINIT:
323 5 unsigned char *x_buf, *y_buf, *out_buf, *realloc_buf = NULL;
324 STRLEN x_len, y_len;
325  
326 CODE:
327 5 ▶ x_buf = (unsigned char *)SvPVbyte(x, x_len);
Branch: 1 branch
BranchTrueFalseTotal
x_buf = (unsigned char *)SvPVbyte(x, x_len); 5 0 5
328 5 y_buf = (unsigned char *)SvPVbyte(y, y_len);
329  
330 5 ▶ if (x_len < y_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (x_len < y_len) { 1 4 5
331 1 Newxz(realloc_buf, y_len + 1, unsigned char);
332 1 ▶ if (realloc_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_buf == NULL) 0 1 1
333 0 croak("Failed to allocate memory");
334 1 memcpy(realloc_buf, x_buf, x_len);
335 1 x_buf = realloc_buf;
336 1 x_len = y_len;
337 }
338 4 ▶ else if (x_len > y_len) {
Branch: 1 branch
BranchTrueFalseTotal
else if (x_len > y_len) { 2 2 4
339 2 Newxz(realloc_buf, x_len + 1, unsigned char);
340 2 ▶ if (realloc_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_buf == NULL) 0 2 2
341 0 croak("Failed to allocate memory");
342 2 memcpy(realloc_buf, y_buf, y_len);
343 2 y_buf = realloc_buf;
344 2 y_len = x_len;
345 }
346  
347 5 RETVAL = newSV(0);
348 5 Newxz(out_buf, x_len + 1, unsigned char);
349 5 ▶ if (out_buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) { 0 5 5
350 0 ▶ if (realloc_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_buf) 0 0 0
351 0 Safefree(realloc_buf);
352 0 croak("Could not allocate memory");
353 }
354 5 memcpy(out_buf, x_buf, x_len);
355  
356 5 sodium_sub(out_buf, y_buf, x_len);
357 5 ▶ if (realloc_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_buf) 3 2 5
358 3 Safefree(realloc_buf);
359  
360 5 sv_usepvn_flags(RETVAL, (char *)out_buf, x_len, SV_HAS_TRAILING_NUL);
361  
362 OUTPUT:
363 RETVAL
364  
365 SV * sodium_unpad(SV * buf, STRLEN blocksize);
366  
367 PREINIT:
368 unsigned char *buf_buf, *unpadded_buf;
369 STRLEN buf_len, unpadded_len;
370  
371 CODE:
372 3 ▶ if (blocksize <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (blocksize <= 0) 0 3 3
373 0 croak("sodium_unpad: Invalid blocksize <= 0");
374  
375 3 buf_buf = (unsigned char *)SvPVbyte(buf, buf_len);
376 3 ▶ if (buf_len < blocksize)
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len < blocksize) 0 3 3
377 0 croak("sodium_unpad: Buffer is shorter than blocksize");
378  
379 3 ▶ if (sodium_unpad(&unpadded_len, buf_buf, buf_len, blocksize) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_unpad(&unpadded_len, buf_buf, buf_len, blocksize) != 0) 0 3 3
380 0 croak("sodium_unpad: Invalid padded buffer");
381  
382 3 Newx(unpadded_buf, unpadded_len + 1, unsigned char);
383 3 ▶ if (unpadded_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (unpadded_buf == NULL) 0 3 3
384 0 croak("sodium_pad: Failed to allocate memory");
385 3 unpadded_buf[unpadded_len] = '\0';
386 3 memcpy(unpadded_buf, buf_buf, unpadded_len);
387  
388 3 RETVAL=newSV(0);
389 3 sv_usepvn_flags(RETVAL, (char *)unpadded_buf, unpadded_len, SV_HAS_TRAILING_NUL);
390  
391 OUTPUT:
392 RETVAL
393  
394 void sodium_memzero(...)
395  
396 PREINIT:
397 unsigned char *arg_buf;
398 STRLEN arg_len;
399 int i;
400  
401 PPCODE:
402 2 ▶ if (!items)
Branch: 1 branch
BranchTrueFalseTotal
if (!items) 0 2 2
403 0 croak("Missing arguments");
404  
405 5 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 3 2 5
406 3 arg_buf = (unsigned char *)SvPVbyte_force(ST(i), arg_len);
407 3 sodium_memzero(arg_buf, arg_len);
408 }
409  
410 2 XSRETURN_EMPTY;
« xs/generichash.xs ↑ Summary xs/scalarmult.xs »