src/argon2id.c

filter: Statementstmt 100% 153 / 153 Branchbran 99.1% 111 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 99.6% 264 / 265 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/blake2b.c ↑ Summary blib/lib/Crypt/Symmetric/Primitives.pm »
1 #include "symprim/symprim.h"
2 #include "symprim/argon2id.h"
3 #include "symprim/blake2b.h"
4 #include "symprim/wipe.h"
5  
6 #include <string.h>
7  
8 #define WORDS 128
9 #define BLOCK 1024
10 #define SLICES 4
11 #define ARGON2_VERSION 0x13
12 #define TYPE_ID 2
13  
14 typedef struct { uint64_t v[WORDS]; } block;
15  
16 136192 static uint64_t load64(const uint8_t *p)
17 {
18 136192 return (uint64_t)p[0] | ((uint64_t)p[1] << 8)
19 136192 | ((uint64_t)p[2] << 16) | ((uint64_t)p[3] << 24)
20 136192 | ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40)
21 136192 | ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56);
22 }
23  
24 59904 static void store64(uint8_t *p, uint64_t v)
25 {
26 int i;
27 539136 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 479232 59904 539136
28 479232 p[i] = (uint8_t)(v >> (8 * i));
29 59904 }
30  
31 6808 static void store32(uint8_t *p, uint32_t v)
32 {
33 6808 p[0] = (uint8_t)v;
34 6808 p[1] = (uint8_t)(v >> 8);
35 6808 p[2] = (uint8_t)(v >> 16);
36 6808 p[3] = (uint8_t)(v >> 24);
37 6808 }
38  
39 4680 static void hash32(symprim_blake2b_state *s, uint32_t v)
40 {
41 uint8_t b[4];
42 4680 store32(b, v);
43 4680 symprim_blake2b_update(s, b, 4);
44 4680 }
45  
46 1872 static void hash_field(symprim_blake2b_state *s, const uint8_t *p, size_t len)
47 {
48 1872 hash32(s, (uint32_t)len);
49 1872 ▶ if (len)
Branch: 1 branch
BranchTrueFalseTotal
if (len) 1616 256 1872
50 1616 symprim_blake2b_update(s, p, len);
51 1872 }
52  
53 #define ROTR(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
54  
55 /* BLAKE2b's addition with a multiplication of the low halves mixed in. */
56 22857728 static uint64_t mul_add(uint64_t x, uint64_t y)
57 {
58 22857728 return x + y + 2 * (x & UINT64_C(0xFFFFFFFF)) * (y & UINT64_C(0xFFFFFFFF));
59 }
60  
61 #define GB(a, b, c, d) \
62 do { \
63 a = mul_add(a, b); \
64 d = ROTR(d ^ a, 32); \
65 c = mul_add(c, d); \
66 b = ROTR(b ^ c, 24); \
67 a = mul_add(a, b); \
68 d = ROTR(d ^ a, 16); \
69 c = mul_add(c, d); \
70 b = ROTR(b ^ c, 63); \
71 } while (0)
72  
73 #define ROUND(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) \
74 do { \
75 GB(v0, v4, v8, v12); \
76 GB(v1, v5, v9, v13); \
77 GB(v2, v6, v10, v14); \
78 GB(v3, v7, v11, v15); \
79 GB(v0, v5, v10, v15); \
80 GB(v1, v6, v11, v12); \
81 GB(v2, v7, v8, v13); \
82 GB(v3, v4, v9, v14); \
83 } while (0)
84  
85 /* The compression function G: out = P(x ^ y) ^ x ^ y, and from the second
86 * pass on that is XORed over what out held before. out may be x or y. */
87 44644 static void compress(const block *x, const block *y, block *out, int over)
88 {
89 block r, z;
90 int i;
91  
92 5759076 ▶ for (i = 0; i < WORDS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < WORDS; i++) 5714432 44644 5759076
93 5714432 r.v[i] = x->v[i] ^ y->v[i];
94 44644 z = r;
95 401796 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 357152 44644 401796
96 357152 uint64_t *p = z.v + 16 * i;
97 357152 ROUND(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
98 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
99 }
100 401796 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 357152 44644 401796
101 357152 uint64_t *p = z.v + 2 * i;
102 357152 ROUND(p[0], p[1], p[16], p[17], p[32], p[33], p[48], p[49],
103 p[64], p[65], p[80], p[81], p[96], p[97], p[112], p[113]);
104 }
105 44644 ▶ if (over) {
Branch: 1 branch
BranchTrueFalseTotal
if (over) { 24272 20372 44644
106 3131088 ▶ for (i = 0; i < WORDS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < WORDS; i++) 3106816 24272 3131088
107 3106816 out->v[i] ^= z.v[i] ^ r.v[i];
108 } else {
109 2627988 ▶ for (i = 0; i < WORDS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < WORDS; i++) 2607616 20372 2627988
110 2607616 out->v[i] = z.v[i] ^ r.v[i];
111 }
112 44644 symprim_wipe(&r, sizeof r);
113 44644 symprim_wipe(&z, sizeof z);
114 44644 }
115  
116 typedef struct {
117 block *memory;
118 uint32_t passes, lanes, segment, lane_length, blocks;
119 } work;
120  
121 /* The next 128 reference words for the data-independent part: the position
122 * and a counter, compressed twice against a block of zeros. */
123 1094 static void next_addresses(block *address, block *input, const block *zero)
124 {
125 1094 input->v[6]++;
126 1094 compress(zero, input, address, 0);
127 1094 compress(zero, address, address, 0);
128 1094 }
129  
130 2932 static void fill_segment(const work *w, uint32_t pass, uint32_t lane, uint32_t slice)
131 {
132 /* Argon2id reads its references from a counter, not from the data, for
133 * the first half of the first pass, and from the data after that. */
134 2932 ▶ int independent = pass == 0 && slice < SLICES / 2;
Branch: 2 branches
BranchTrueFalseTotal
int independent = pass == 0 && slice < SLICES / 2; 2128 804 2932
int independent = pass == 0 && slice < SLICES / 2; 1064 1064 2128
135 2932 ▶ uint32_t start = (pass == 0 && slice == 0) ? 2 : 0;
Branch: 2 branches
BranchTrueFalseTotal
uint32_t start = (pass == 0 && slice == 0) ? 2 : 0; 2128 804 2932
uint32_t start = (pass == 0 && slice == 0) ? 2 : 0; 532 1596 2128
136 block address, input, zero;
137 uint32_t i;
138  
139 2932 ▶ if (independent) {
Branch: 1 branch
BranchTrueFalseTotal
if (independent) { 1064 1868 2932
140 1064 memset(&zero, 0, sizeof zero);
141 1064 memset(&input, 0, sizeof input);
142 1064 input.v[0] = pass;
143 1064 input.v[1] = lane;
144 1064 input.v[2] = slice;
145 1064 input.v[3] = w->blocks;
146 1064 input.v[4] = w->passes;
147 1064 input.v[5] = TYPE_ID;
148 1064 ▶ if (start)
Branch: 1 branch
BranchTrueFalseTotal
if (start) 532 532 1064
149 532 next_addresses(&address, &input, &zero);
150 }
151  
152 45388 ▶ for (i = start; i < w->segment; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < w->segment; i++) { 42456 2932 45388
153 42456 uint32_t current = slice * w->segment + i;
154 42456 ▶ uint32_t previous = current ? current - 1 : w->lane_length - 1;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t previous = current ? current - 1 : w->lane_length - 1; 42255 201 42456
155 42456 block *row = w->memory + (size_t)lane * w->lane_length;
156 uint64_t random, j1, area, relative, base;
157 uint32_t ref_lane, ref_index;
158  
159 42456 ▶ if (independent) {
Branch: 1 branch
BranchTrueFalseTotal
if (independent) { 8560 33896 42456
160 8560 ▶ if (i % WORDS == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i % WORDS == 0) 562 7998 8560
161 562 next_addresses(&address, &input, &zero);
162 8560 random = address.v[i % WORDS];
163 } else {
164 33896 random = row[previous].v[0];
165 }
166  
167 42456 ref_lane = (uint32_t)((random >> 32) % w->lanes);
168 42456 ▶ if (pass == 0 && slice == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (pass == 0 && slice == 0) 18184 24272 42456
if (pass == 0 && slice == 0) 3748 14436 18184
169 3748 ref_lane = lane;
170  
171 /* How many blocks may be referred to: everything finished, less the
172 * block just written in this lane, less one more in another lane when
173 * this is the first block of a segment. */
174 42456 ▶ if (pass == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pass == 0) 18184 24272 42456
175 18184 area = (uint64_t)slice * w->segment;
176 else
177 24272 area = (uint64_t)w->lane_length - w->segment;
178 42456 ▶ if (ref_lane == lane)
Branch: 1 branch
BranchTrueFalseTotal
if (ref_lane == lane) 30566 11890 42456
179 30566 area = area + i - 1;
180 11890 ▶ else if (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (i == 0) 521 11369 11890
181 521 area -= 1;
182  
183 42456 j1 = random & UINT64_C(0xFFFFFFFF);
184 42456 j1 = (j1 * j1) >> 32;
185 42456 relative = area - 1 - ((area * j1) >> 32);
186  
187 42456 ▶ base = (pass == 0 || slice == SLICES - 1) ? 0 : (uint64_t)(slice + 1) * w->segment;
Branch: 2 branches
BranchTrueFalseTotal
base = (pass == 0 || slice == SLICES - 1) ? 0 : (uint64_t)(slice + 1) * w->segment; 24272 18184 42456
base = (pass == 0 || slice == SLICES - 1) ? 0 : (uint64_t)(slice + 1) * w->segment; 18204 6068 24272
188 42456 ref_index = (uint32_t)((base + relative) % w->lane_length);
189  
190 42456 compress(row + previous,
191 42456 w->memory + (size_t)ref_lane * w->lane_length + ref_index,
192 42456 row + current, pass != 0);
193 }
194  
195 2932 ▶ if (independent) {
Branch: 1 branch
BranchTrueFalseTotal
if (independent) { 1064 1868 2932
196 1064 symprim_wipe(&address, sizeof address);
197 1064 symprim_wipe(&input, sizeof input);
198 }
199 2932 }
200  
201 2401 static int too_long(size_t len)
202 {
203 2401 return (uint64_t)len > UINT64_C(0xFFFFFFFF);
204 }
205  
206 489 int symprim_argon2id(const symprim_argon2_in *in, uint8_t *tag, size_t tag_len)
207 {
208 symprim_blake2b_state s;
209 uint8_t seed[SYMPRIM_BLAKE2B_MAX + 8], bytes[BLOCK];
210 block last;
211 work w;
212 uint32_t pass, slice, lane, i;
213 int k;
214  
215 /* The table looks for nothing given before this is reached, and the door
216 * always gives something.
217 # uncoverable condition left count:1 */
218 489 ▶ if (!in || !tag)
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !tag) 489 0 489
if (!in || !tag) 1 488 489
219 1 return SYMPRIM_E_ARGUMENT;
220 488 ▶ if (in->lanes < 1 || in->lanes > SYMPRIM_ARGON2_LANES_MAX || in->passes < 1
Branch: 3 branches
BranchTrueFalseTotal
if (in->lanes < 1 || in->lanes > SYMPRIM_ARGON2_LANES_MAX || in->passes < 1 487 1 488
if (in->lanes < 1 || in->lanes > SYMPRIM_ARGON2_LANES_MAX || in->passes < 1 486 1 487
if (in->lanes < 1 || in->lanes > SYMPRIM_ARGON2_LANES_MAX || in->passes < 1 485 1 486
221 485 ▶ || in->memory_kib < 8 * in->lanes
Branch: 1 branch
BranchTrueFalseTotal
|| in->memory_kib < 8 * in->lanes 484 1 485
222 484 ▶ || tag_len < SYMPRIM_ARGON2_TAG_MIN || too_long(tag_len)
Branch: 2 branches
BranchTrueFalseTotal
|| tag_len < SYMPRIM_ARGON2_TAG_MIN || too_long(tag_len) 483 1 484
|| tag_len < SYMPRIM_ARGON2_TAG_MIN || too_long(tag_len) 482 1 483
223 482 ▶ || in->salt_len < SYMPRIM_ARGON2_SALT_MIN || too_long(in->salt_len)
Branch: 2 branches
BranchTrueFalseTotal
|| in->salt_len < SYMPRIM_ARGON2_SALT_MIN || too_long(in->salt_len) 481 1 482
|| in->salt_len < SYMPRIM_ARGON2_SALT_MIN || too_long(in->salt_len) 480 1 481
224 480 ▶ || too_long(in->password_len) || too_long(in->secret_len) || too_long(in->data_len)
Branch: 3 branches
BranchTrueFalseTotal
|| too_long(in->password_len) || too_long(in->secret_len) || too_long(in->data_len) 479 1 480
|| too_long(in->password_len) || too_long(in->secret_len) || too_long(in->data_len) 478 1 479
|| too_long(in->password_len) || too_long(in->secret_len) || too_long(in->data_len) 477 1 478
225 477 ▶ || (in->password_len && !in->password) || !in->salt
Branch: 3 branches
BranchTrueFalseTotal
|| (in->password_len && !in->password) || !in->salt 473 4 477
|| (in->password_len && !in->password) || !in->salt 472 1 473
|| (in->password_len && !in->password) || !in->salt 475 1 476
226 475 ▶ || (in->secret_len && !in->secret) || (in->data_len && !in->data))
Branch: 4 branches
BranchTrueFalseTotal
|| (in->secret_len && !in->secret) || (in->data_len && !in->data)) 345 130 475
|| (in->secret_len && !in->secret) || (in->data_len && !in->data)) 344 1 345
|| (in->secret_len && !in->secret) || (in->data_len && !in->data)) 341 133 474
|| (in->secret_len && !in->secret) || (in->data_len && !in->data)) 1 340 341
227 15 return SYMPRIM_E_ARGUMENT;
228  
229 /* The memory is rounded down to a whole number of segments. */
230 473 w.passes = in->passes;
231 473 w.lanes = in->lanes;
232 473 w.segment = in->memory_kib / (SLICES * in->lanes);
233 473 w.lane_length = w.segment * SLICES;
234 473 w.blocks = w.lane_length * in->lanes;
235  
236 /* Only where a size_t is 32 bits can a count of blocks be more than a
237 * size_t holds in bytes. Where it is 64 no memory cost reaches this.
238 # uncoverable branch true class:ignore_covered_err */
239 if ((uint64_t)w.blocks > (uint64_t)((size_t)-1) / sizeof(block))
240 return SYMPRIM_E_MEMORY; /* # uncoverable statement class:ignore_covered_err */
241 473 w.memory = (block *)symprim_malloc((size_t)w.blocks * sizeof(block));
242 473 ▶ if (!w.memory)
Branch: 1 branch
BranchTrueFalseTotal
if (!w.memory) 5 468 473
243 5 return SYMPRIM_E_MEMORY;
244  
245 /* H0 takes the memory as it was asked for, not as it was rounded. */
246 468 symprim_blake2b_init(&s, SYMPRIM_BLAKE2B_MAX, NULL, 0, NULL, NULL);
247 468 hash32(&s, in->lanes);
248 468 hash32(&s, (uint32_t)tag_len);
249 468 hash32(&s, in->memory_kib);
250 468 hash32(&s, in->passes);
251 468 hash32(&s, ARGON2_VERSION);
252 468 hash32(&s, TYPE_ID);
253 468 hash_field(&s, in->password, in->password_len);
254 468 hash_field(&s, in->salt, in->salt_len);
255 468 hash_field(&s, in->secret, in->secret_len);
256 468 hash_field(&s, in->data, in->data_len);
257 468 symprim_blake2b_final(&s, seed);
258  
259 /* The first two blocks of every lane come straight from H0. */
260 1000 ▶ for (lane = 0; lane < w.lanes; lane++) {
Branch: 1 branch
BranchTrueFalseTotal
for (lane = 0; lane < w.lanes; lane++) { 532 468 1000
261 1596 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 1064 532 1596
262 1064 block *b = w.memory + (size_t)lane * w.lane_length + i;
263 1064 store32(seed + SYMPRIM_BLAKE2B_MAX, i);
264 1064 store32(seed + SYMPRIM_BLAKE2B_MAX + 4, lane);
265 1064 symprim_blake2b_long(bytes, BLOCK, seed, sizeof seed);
266 137256 ▶ for (k = 0; k < WORDS; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < WORDS; k++) 136192 1064 137256
267 136192 b->v[k] = load64(bytes + 8 * k);
268 }
269 }
270  
271 1053 ▶ for (pass = 0; pass < w.passes; pass++)
Branch: 1 branch
BranchTrueFalseTotal
for (pass = 0; pass < w.passes; pass++) 585 468 1053
272 2925 ▶ for (slice = 0; slice < SLICES; slice++)
Branch: 1 branch
BranchTrueFalseTotal
for (slice = 0; slice < SLICES; slice++) 2340 585 2925
273 5272 ▶ for (lane = 0; lane < w.lanes; lane++)
Branch: 1 branch
BranchTrueFalseTotal
for (lane = 0; lane < w.lanes; lane++) 2932 2340 5272
274 2932 fill_segment(&w, pass, lane, slice);
275  
276 468 last = w.memory[w.lane_length - 1];
277 532 ▶ for (lane = 1; lane < w.lanes; lane++) {
Branch: 1 branch
BranchTrueFalseTotal
for (lane = 1; lane < w.lanes; lane++) { 64 468 532
278 64 const block *b = w.memory + (size_t)lane * w.lane_length + w.lane_length - 1;
279 8256 ▶ for (k = 0; k < WORDS; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < WORDS; k++) 8192 64 8256
280 8192 last.v[k] ^= b->v[k];
281 }
282 60372 ▶ for (k = 0; k < WORDS; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < WORDS; k++) 59904 468 60372
283 59904 store64(bytes + 8 * k, last.v[k]);
284 468 symprim_blake2b_long(tag, (uint32_t)tag_len, bytes, BLOCK);
285  
286 468 symprim_wipe(w.memory, (size_t)w.blocks * sizeof(block));
287 468 symprim_free(w.memory);
288 468 symprim_wipe(seed, sizeof seed);
289 468 symprim_wipe(bytes, sizeof bytes);
290 468 symprim_wipe(&last, sizeof last);
291 468 return SYMPRIM_OK;
292 }
« src/blake2b.c ↑ Summary blib/lib/Crypt/Symmetric/Primitives.pm »