xs/blake2b.xs

filter: Statementstmt 100% 118 / 118 Branchbran 100% 66 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 184 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/aead.xs ↑ Summary
1 SV *
2 blake2b(data, ...)
3 SV *data
4 PREINIT:
5 943 STRLEN len, keylen = 0;
6 943 const uint8_t *in, *key = NULL;
7 943 uint32_t bytes = SYMPRIM_BLAKE2B_MAX;
8 943 SV *wanted = NULL, *keyed = NULL;
9 int code;
10 CODE:
11 943 ▶ if (items > 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) 1 942 943
12 1 croak("Usage: Crypt::Symmetric::Primitives::blake2b(data, bytes, key)");
13 942 data = bytes_of(aTHX_ data);
14 940 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 665 275 940
15 665 wanted = settled(aTHX_ ST(1));
16 939 ▶ if (items > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) 604 335 939
17 604 keyed = settled(aTHX_ ST(2));
18 938 ▶ if (wanted && SvOK(wanted))
Branch: 2 branches
BranchTrueFalseTotal
if (wanted && SvOK(wanted)) 663 275 938
if (wanted && SvOK(wanted)) 660 3 663
19 660 bytes = number_named(aTHX_ "blake2b", "the length", wanted);
20 913 ▶ if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX) 912 1 913
if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX) 3 909 912
21 4 croak("blake2b gives 1 to 64 bytes");
22 909 ▶ keyed = keyed && SvOK(keyed) ? bytes_of(aTHX_ keyed) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
keyed = keyed && SvOK(keyed) ? bytes_of(aTHX_ keyed) : NULL; 603 306 909
keyed = keyed && SvOK(keyed) ? bytes_of(aTHX_ keyed) : NULL; 333 270 603
23 908 ▶ if (keyed) {
Branch: 1 branch
BranchTrueFalseTotal
if (keyed) { 332 576 908
24 332 key = bytes_at(aTHX_ keyed, &keylen);
25 332 ▶ if (keylen > SYMPRIM_BLAKE2B_KEY_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (keylen > SYMPRIM_BLAKE2B_KEY_MAX) 2 330 332
26 2 croak("a blake2b key is at most 64 bytes");
27 }
28 906 in = bytes_at(aTHX_ data, &len);
29 906 RETVAL = newSV(bytes);
30 906 SvPOK_only(RETVAL);
31 906 code = symprim_blake2b(SvPVX(RETVAL), bytes, in, len, key, keylen);
32 906 engine_agreed(aTHX_ code, RETVAL, NULL, 0);
33 906 SvPVX(RETVAL)[bytes] = '\0';
34 906 SvCUR_set(RETVAL, bytes);
35 OUTPUT:
36 RETVAL
37  
38 SV *
39 _blake2b_long(data, bytes)
40 SV *data
41 SV *bytes
42 PREINIT:
43 STRLEN len;
44 const uint8_t *in;
45 uint32_t want;
46 int code;
47 CODE:
48 53 data = bytes_of(aTHX_ data);
49 52 want = number_named(aTHX_ "_blake2b_long", "the length", settled(aTHX_ bytes));
50 /* No bytes at all is the engine's to refuse: this door is how a test
51 * sees that it does. */
52 50 ▶ if (want > 1024 * 1024)
Branch: 1 branch
BranchTrueFalseTotal
if (want > 1024 * 1024) 1 49 50
53 1 croak("_blake2b_long gives at most 1 MiB");
54 49 in = bytes_at(aTHX_ data, &len);
55 49 ▶ RETVAL = newSV(want ? want : 1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSV(want ? want : 1); 48 1 49
56 49 SvPOK_only(RETVAL);
57 49 code = symprim_blake2b_long(SvPVX(RETVAL), want, in, len);
58 49 ▶ if (code != SYMPRIM_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (code != SYMPRIM_OK) { 1 48 49
59 1 SvREFCNT_dec(RETVAL);
60 1 croak("%s", symprim_error_message(code));
61 }
62 48 SvPVX(RETVAL)[want] = '\0';
63 48 SvCUR_set(RETVAL, want);
64 OUTPUT:
65 RETVAL
66  
67 SV *
68 _blake2b_from_counter(data, low, high)
69 SV *data
70 SV *low
71 SV *high
72 PREINIT:
73 STRLEN len, lowlen, highlen;
74 const uint8_t *in, *lo, *hi;
75 symprim_blake2b_state s;
76 int i;
77 CODE:
78 32 data = bytes_of(aTHX_ data);
79 31 low = bytes_of(aTHX_ low);
80 30 high = bytes_of(aTHX_ high);
81 29 in = bytes_at(aTHX_ data, &len);
82 29 lo = bytes_at(aTHX_ low, &lowlen);
83 29 hi = bytes_at(aTHX_ high, &highlen);
84 29 ▶ if (lowlen != 8 || highlen != 8)
Branch: 2 branches
BranchTrueFalseTotal
if (lowlen != 8 || highlen != 8) 28 1 29
if (lowlen != 8 || highlen != 8) 1 27 28
85 2 croak("_blake2b_from_counter takes two 8 byte halves");
86 27 symprim_blake2b_init(&s, SYMPRIM_BLAKE2B_MAX, NULL, 0, NULL, NULL);
87 243 ▶ for (i = 7; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 7; i >= 0; i--) { 216 27 243
88 216 s.t[0] = (s.t[0] << 8) | lo[i];
89 216 s.t[1] = (s.t[1] << 8) | hi[i];
90 }
91 27 symprim_blake2b_update(&s, in, len);
92 27 RETVAL = newSV(SYMPRIM_BLAKE2B_MAX);
93 27 SvPOK_only(RETVAL);
94 27 symprim_blake2b_final(&s, SvPVX(RETVAL));
95 27 SvPVX(RETVAL)[SYMPRIM_BLAKE2B_MAX] = '\0';
96 27 SvCUR_set(RETVAL, SYMPRIM_BLAKE2B_MAX);
97 OUTPUT:
98 RETVAL
99  
100 MODULE = Crypt::Symmetric::Primitives PACKAGE = Crypt::Symmetric::Primitives::BLAKE2b
101  
102 SV *
103 new(class, ...)
104 SV *class
105 PREINIT:
106 static const named known[] = {
107 { "bytes", 0 }, { "key", 1 }, { "salt", 1 }, { "personal", 1 }
108 };
109 SV *slot[4];
110 652 const char *door = "Crypt::Symmetric::Primitives::BLAKE2b->new";
111 652 uint32_t bytes = SYMPRIM_BLAKE2B_MAX;
112 652 STRLEN keylen = 0, len;
113 652 const uint8_t *key = NULL, *value;
114 uint8_t salt[SYMPRIM_BLAKE2B_SALT], personal[SYMPRIM_BLAKE2B_PERSONAL];
115 symprim_blake2b_state made, *state;
116 HV *stash;
117 SV *object;
118 int code;
119 CODE:
120 652 class = settled(aTHX_ class);
121 651 ▶ if (SvROK(class))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(class)) 1 650 651
122 1 croak("%s is called on the class", door);
123 650 class = bytes_of(aTHX_ class);
124 650 named_arguments(aTHX_ door, &ST(1), items - 1, known, 4, slot);
125 596 ▶ if (slot[0])
Branch: 1 branch
BranchTrueFalseTotal
if (slot[0]) 253 343 596
126 253 bytes = number_named(aTHX_ door, "bytes", slot[0]);
127 594 stash = gv_stashsv(class, 0);
128 594 ▶ if (!stash || !sv_derived_from(class, "Crypt::Symmetric::Primitives::BLAKE2b"))
Branch: 2 branches
BranchTrueFalseTotal
if (!stash || !sv_derived_from(class, "Crypt::Symmetric::Primitives::BLAKE2b")) 575 19 594
if (!stash || !sv_derived_from(class, "Crypt::Symmetric::Primitives::BLAKE2b")) 1 574 575
129 20 croak("%s is called on the class", door);
130 574 ▶ if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX) 573 1 574
if (bytes < 1 || bytes > SYMPRIM_BLAKE2B_MAX) 1 572 573
131 2 croak("blake2b gives 1 to 64 bytes");
132 572 memset(salt, 0, sizeof salt);
133 572 memset(personal, 0, sizeof personal);
134 572 ▶ if (slot[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot[1]) { 171 401 572
135 171 key = bytes_at(aTHX_ slot[1], &keylen);
136 171 ▶ if (keylen > SYMPRIM_BLAKE2B_KEY_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (keylen > SYMPRIM_BLAKE2B_KEY_MAX) 1 170 171
137 1 croak("a blake2b key is at most 64 bytes");
138 }
139 571 ▶ if (slot[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot[2]) { 183 388 571
140 183 value = bytes_at(aTHX_ slot[2], &len);
141 183 ▶ if (len > sizeof salt)
Branch: 1 branch
BranchTrueFalseTotal
if (len > sizeof salt) 1 182 183
142 1 croak("a blake2b salt is at most 16 bytes");
143 182 memcpy(salt, value, len);
144 }
145 570 ▶ if (slot[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot[3]) { 184 386 570
146 184 value = bytes_at(aTHX_ slot[3], &len);
147 184 ▶ if (len > sizeof personal)
Branch: 1 branch
BranchTrueFalseTotal
if (len > sizeof personal) 1 183 184
148 1 croak("a blake2b personalisation is at most 16 bytes");
149 183 memcpy(personal, value, len);
150 }
151 569 code = symprim_blake2b_init(&made, bytes, key, keylen, salt, personal);
152 569 engine_agreed(aTHX_ code, NULL, &made, sizeof made);
153 569 Newx(state, 1, symprim_blake2b_state);
154 569 Copy(&made, state, 1, symprim_blake2b_state);
155 569 symprim_wipe(&made, sizeof made);
156 569 object = newSV(0);
157 569 blake2b_attach(aTHX_ object, state);
158 569 RETVAL = sv_bless(newRV_noinc(object), stash);
159 OUTPUT:
160 RETVAL
161  
162 void
163 add(self, ...)
164 SV *self
165 PREINIT:
166 symprim_blake2b_state *s;
167 STRLEN len;
168 const uint8_t *in;
169 int i;
170 PPCODE:
171 /* Every piece is read before the first is hashed, so a piece that
172 * cannot be read leaves the object as it was. */
173 1164 self = settled(aTHX_ self);
174 3490 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 2331 1159 3490
175 2331 ST(i) = bytes_of(aTHX_ ST(i));
176 1159 s = blake2b_state(aTHX_ self);
177 3478 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 2322 1156 3478
178 2322 in = bytes_at(aTHX_ ST(i), &len);
179 2322 symprim_blake2b_update(s, in, len);
180 }
181 1156 XSRETURN(1);
182  
183 SV *
184 digest(self)
185 SV *self
186 ALIAS:
187 hexdigest = 1
188 PREINIT:
189 symprim_blake2b_state copy;
190 uint8_t out[SYMPRIM_BLAKE2B_MAX];
191 char hex[2 * SYMPRIM_BLAKE2B_MAX];
192 size_t bytes, i;
193 CODE:
194 577 self = settled(aTHX_ self);
195 575 copy = *blake2b_state(aTHX_ self);
196 570 bytes = copy.outlen;
197 570 symprim_blake2b_final(&copy, out);
198 570 ▶ if (ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix) { 201 369 570
199 7319 ▶ for (i = 0; i < bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bytes; i++) { 7118 201 7319
200 7118 hex[2 * i] = "0123456789abcdef"[out[i] >> 4];
201 7118 hex[2 * i + 1] = "0123456789abcdef"[out[i] & 15];
202 }
203 201 RETVAL = newSVpvn(hex, 2 * bytes);
204 } else {
205 369 RETVAL = newSVpvn((const char *)out, bytes);
206 }
207 570 symprim_wipe(out, sizeof out);
208 570 symprim_wipe(hex, sizeof hex);
209 OUTPUT:
210 RETVAL
211  
212 MODULE = Crypt::Symmetric::Primitives PACKAGE = Crypt::Symmetric::Primitives
« xs/aead.xs ↑ Summary