blake3.c

filter: Statementstmt 100% 177 / 177 Branchbran 100% 64 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 241 / 241 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sha2.c ↑ Summary blib/lib/File/Raw/Hash.pm »
1 /*
2 * blake3.c - BLAKE3 reference implementation, sequential, no SIMD.
3 *
4 * Implements the BLAKE3 hash function in hash mode with the default
5 * 32-byte (256-bit) output. Streaming via the spec's tree-stack
6 * algorithm: at any moment we hold one in-progress chunk plus up to
7 * BLAKE3_MAX_DEPTH chaining values waiting to be combined with their
8 * right siblings. Per-call memory is O(log N) over the input length.
9 */
10  
11 #include "blake3.h"
12 #include <string.h>
13  
14 /* ---------------- constants ---------------- */
15  
16 static const uint32_t IV[8] = {
17 0x6A09E667u, 0xBB67AE85u, 0x3C6EF372u, 0xA54FF53Au,
18 0x510E527Fu, 0x9B05688Cu, 0x1F83D9ABu, 0x5BE0CD19u
19 };
20  
21 /* Domain-separation flags. */
22 #define CHUNK_START 0x01u
23 #define CHUNK_END 0x02u
24 #define PARENT 0x04u
25 #define ROOT 0x08u
26 /* KEYED_HASH / DERIVE_KEY_* unused in v0.01 (hash mode only). */
27  
28 /* Message-word permutation applied between rounds (rounds 1..6). */
29 static const uint8_t MSG_PERM[16] = {
30 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8
31 };
32  
33 /* ---------------- bit/byte helpers ---------------- */
34  
35 #define ROR32(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
36  
37 static uint32_t
38 8288 load32_le(const unsigned char *p)
39 {
40 8288 return (uint32_t)p[0]
41 8288 | ((uint32_t)p[1] << 8)
42 8288 | ((uint32_t)p[2] << 16)
43 8288 | ((uint32_t)p[3] << 24);
44 }
45  
46 static void
47 152 store32_le(unsigned char *p, uint32_t v)
48 {
49 152 p[0] = (unsigned char)( v & 0xFFu);
50 152 p[1] = (unsigned char)((v >> 8) & 0xFFu);
51 152 p[2] = (unsigned char)((v >> 16) & 0xFFu);
52 152 p[3] = (unsigned char)((v >> 24) & 0xFFu);
53 152 }
54  
55 /* ---------------- compression ---------------- */
56  
57 static void
58 30520 g_mix(uint32_t state[16], int a, int b, int c, int d, uint32_t mx, uint32_t my)
59 {
60 30520 state[a] = state[a] + state[b] + mx;
61 30520 state[d] = ROR32(state[d] ^ state[a], 16);
62 30520 state[c] = state[c] + state[d];
63 30520 state[b] = ROR32(state[b] ^ state[c], 12);
64 30520 state[a] = state[a] + state[b] + my;
65 30520 state[d] = ROR32(state[d] ^ state[a], 8);
66 30520 state[c] = state[c] + state[d];
67 30520 state[b] = ROR32(state[b] ^ state[c], 7);
68 30520 }
69  
70 static void
71 3815 round_fn(uint32_t state[16], const uint32_t m[16])
72 {
73 /* Columns. */
74 3815 g_mix(state, 0, 4, 8, 12, m[0], m[1]);
75 3815 g_mix(state, 1, 5, 9, 13, m[2], m[3]);
76 3815 g_mix(state, 2, 6, 10, 14, m[4], m[5]);
77 3815 g_mix(state, 3, 7, 11, 15, m[6], m[7]);
78 /* Diagonals. */
79 3815 g_mix(state, 0, 5, 10, 15, m[ 8], m[ 9]);
80 3815 g_mix(state, 1, 6, 11, 12, m[10], m[11]);
81 3815 g_mix(state, 2, 7, 8, 13, m[12], m[13]);
82 3815 g_mix(state, 3, 4, 9, 14, m[14], m[15]);
83 3815 }
84  
85 static void
86 3270 permute(uint32_t m[16])
87 {
88 uint32_t tmp[16];
89 int i;
90 55590 ▶ for (i = 0; i < 16; i++) tmp[i] = m[MSG_PERM[i]];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) tmp[i] = m[MSG_PERM[i]]; 52320 3270 55590
91 55590 ▶ for (i = 0; i < 16; i++) m[i] = tmp[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) m[i] = tmp[i]; 52320 3270 55590
92 3270 }
93  
94 /* compress: produces full 16-word output. Caller takes the first 8
95 * words for a chaining value, or all 16 for a root-output block. */
96 static void
97 545 compress(const uint32_t chaining_value[8],
98 const uint32_t block_words_in[16],
99 uint64_t counter,
100 uint32_t block_len,
101 uint32_t flags,
102 uint32_t out[16])
103 {
104 uint32_t state[16];
105 uint32_t block_words[16];
106 int i;
107  
108 545 state[ 0] = chaining_value[0];
109 545 state[ 1] = chaining_value[1];
110 545 state[ 2] = chaining_value[2];
111 545 state[ 3] = chaining_value[3];
112 545 state[ 4] = chaining_value[4];
113 545 state[ 5] = chaining_value[5];
114 545 state[ 6] = chaining_value[6];
115 545 state[ 7] = chaining_value[7];
116 545 state[ 8] = IV[0];
117 545 state[ 9] = IV[1];
118 545 state[10] = IV[2];
119 545 state[11] = IV[3];
120 545 state[12] = (uint32_t)(counter & 0xFFFFFFFFull);
121 545 state[13] = (uint32_t)((counter >> 32) & 0xFFFFFFFFull);
122 545 state[14] = block_len;
123 545 state[15] = flags;
124  
125 9265 ▶ for (i = 0; i < 16; i++) block_words[i] = block_words_in[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) block_words[i] = block_words_in[i]; 8720 545 9265
126  
127 545 round_fn(state, block_words); /* round 1 */
128 545 permute(block_words);
129 545 round_fn(state, block_words); /* round 2 */
130 545 permute(block_words);
131 545 round_fn(state, block_words); /* round 3 */
132 545 permute(block_words);
133 545 round_fn(state, block_words); /* round 4 */
134 545 permute(block_words);
135 545 round_fn(state, block_words); /* round 5 */
136 545 permute(block_words);
137 545 round_fn(state, block_words); /* round 6 */
138 545 permute(block_words);
139 545 round_fn(state, block_words); /* round 7 */
140  
141 /* XOR upper half into lower half, then upper into chaining value
142 * - this is the "feedforward" the spec describes. The full 16-word
143 * output is what the caller can consume. */
144 4905 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 4360 545 4905
145 4360 state[i] ^= state[i + 8];
146 4360 state[i + 8] ^= chaining_value[i];
147 }
148  
149 9265 ▶ for (i = 0; i < 16; i++) out[i] = state[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) out[i] = state[i]; 8720 545 9265
150 545 }
151  
152 /* Convert a 64-byte block to 16 little-endian words. */
153 static void
154 518 block_to_words(const unsigned char *block, uint32_t out[16])
155 {
156 int i;
157 8806 ▶ for (i = 0; i < 16; i++) out[i] = load32_le(block + i * 4);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) out[i] = load32_le(block + i * 4); 8288 518 8806
158 518 }
159  
160 /* ---------------- chunk machinery ---------------- */
161  
162 static uint32_t
163 518 chunk_start_flag(const blake3_chunk_state_t *cs)
164 {
165 518 ▶ return cs->blocks_compressed == 0 ? CHUNK_START : 0;
Branch: 1 branch
BranchTrueFalseTotal
return cs->blocks_compressed == 0 ? CHUNK_START : 0; 46 472 518
166 }
167  
168 static void
169 46 chunk_init(blake3_chunk_state_t *cs, const uint32_t key[8], uint64_t counter)
170 {
171 int i;
172 414 ▶ for (i = 0; i < 8; i++) cs->chaining_value[i] = key[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) cs->chaining_value[i] = key[i]; 368 46 414
173 46 cs->chunk_counter = counter;
174 46 cs->buffer_len = 0;
175 46 cs->blocks_compressed = 0;
176 /* Zero the buffer so that a partial-or-empty last block is read as
177 * zero-padded message words. Without this an empty input would
178 * read stack garbage from the buffer in chunk_output_root. */
179 46 memset(cs->buffer, 0, BLAKE3_BLOCK_SIZE);
180 46 }
181  
182 static size_t
183 42 chunk_len(const blake3_chunk_state_t *cs)
184 {
185 42 return (size_t)cs->blocks_compressed * BLAKE3_BLOCK_SIZE + cs->buffer_len;
186 }
187  
188 static void
189 472 chunk_compress_buffer(blake3_chunk_state_t *cs, uint32_t flags)
190 {
191 uint32_t block_words[16];
192 uint32_t out[16];
193 int i;
194  
195 472 block_to_words(cs->buffer, block_words);
196 472 compress(cs->chaining_value, block_words, cs->chunk_counter,
197 472 (uint32_t)cs->buffer_len,
198 472 flags | chunk_start_flag(cs),
199 out);
200 4248 ▶ for (i = 0; i < 8; i++) cs->chaining_value[i] = out[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) cs->chaining_value[i] = out[i]; 3776 472 4248
201 472 cs->blocks_compressed++;
202 472 cs->buffer_len = 0;
203 472 memset(cs->buffer, 0, BLAKE3_BLOCK_SIZE);
204 472 }
205  
206 /* Append `len` bytes (len > 0). Caller must ensure chunk_len + len
207 * <= BLAKE3_CHUNK_SIZE. */
208 static void
209 42 chunk_update(blake3_chunk_state_t *cs, const unsigned char *p, size_t len,
210 uint32_t flags)
211 {
212 /* Fill any partial block, compressing only when we know more bytes
213 * (or a chunk boundary) will follow. We never compress the LAST
214 * block of a chunk here - the caller does that with CHUNK_END. */
215 556 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 514 42 556
216 size_t take;
217 514 ▶ if (cs->buffer_len == BLAKE3_BLOCK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (cs->buffer_len == BLAKE3_BLOCK_SIZE) { 472 42 514
218 472 chunk_compress_buffer(cs, flags);
219 }
220 514 take = BLAKE3_BLOCK_SIZE - cs->buffer_len;
221 514 ▶ if (take > len) take = len;
Branch: 1 branch
BranchTrueFalseTotal
if (take > len) take = len; 14 500 514
222 514 memcpy(cs->buffer + cs->buffer_len, p, take);
223 514 cs->buffer_len += (uint8_t)take;
224 514 p += take;
225 514 len -= take;
226 }
227 42 }
228  
229 /* Finalise a chunk into an 8-word chaining value (used as a leaf in
230 * the tree). Caller passes `is_root` to add the ROOT flag, in which
231 * case `out16` (16 words) gets the full output for byte extraction. */
232 static void
233 32 chunk_output_cv(blake3_chunk_state_t *cs, uint32_t flags, uint32_t out_cv[8])
234 {
235 uint32_t block_words[16];
236 uint32_t out[16];
237 int i;
238  
239 32 block_to_words(cs->buffer, block_words);
240 32 compress(cs->chaining_value, block_words, cs->chunk_counter,
241 32 (uint32_t)cs->buffer_len,
242 32 flags | chunk_start_flag(cs) | CHUNK_END,
243 out);
244 288 ▶ for (i = 0; i < 8; i++) out_cv[i] = out[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) out_cv[i] = out[i]; 256 32 288
245 32 }
246  
247 /* As above but produces the full 16-word root output. */
248 static void
249 14 chunk_output_root(blake3_chunk_state_t *cs, uint32_t flags,
250 unsigned char out_bytes[BLAKE3_DIGEST_SIZE])
251 {
252 uint32_t block_words[16];
253 uint32_t out[16];
254 int i;
255  
256 14 block_to_words(cs->buffer, block_words);
257 14 compress(cs->chaining_value, block_words, cs->chunk_counter,
258 14 (uint32_t)cs->buffer_len,
259 14 flags | chunk_start_flag(cs) | CHUNK_END | ROOT,
260 out);
261 126 ▶ for (i = 0; i < 8; i++) store32_le(out_bytes + i * 4, out[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) store32_le(out_bytes + i * 4, out[i]); 112 14 126
262 14 }
263  
264 /* ---------------- parent machinery ---------------- */
265  
266 /* Combine left+right CVs into a single parent CV. */
267 static void
268 22 parent_cv(const uint32_t left[8], const uint32_t right[8],
269 const uint32_t key[8], uint32_t flags, uint32_t out_cv[8])
270 {
271 uint32_t block_words[16];
272 uint32_t out[16];
273 int i;
274 198 ▶ for (i = 0; i < 8; i++) block_words[i] = left[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) block_words[i] = left[i]; 176 22 198
275 198 ▶ for (i = 0; i < 8; i++) block_words[i + 8] = right[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) block_words[i + 8] = right[i]; 176 22 198
276  
277 22 compress(key, block_words, 0, BLAKE3_BLOCK_SIZE,
278 flags | PARENT, out);
279 198 ▶ for (i = 0; i < 8; i++) out_cv[i] = out[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) out_cv[i] = out[i]; 176 22 198
280 22 }
281  
282 /* As above, root version producing the 32-byte output. */
283 static void
284 5 parent_output_root(const uint32_t left[8], const uint32_t right[8],
285 const uint32_t key[8], uint32_t flags,
286 unsigned char out_bytes[BLAKE3_DIGEST_SIZE])
287 {
288 uint32_t block_words[16];
289 uint32_t out[16];
290 int i;
291 45 ▶ for (i = 0; i < 8; i++) block_words[i] = left[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) block_words[i] = left[i]; 40 5 45
292 45 ▶ for (i = 0; i < 8; i++) block_words[i + 8] = right[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) block_words[i + 8] = right[i]; 40 5 45
293  
294 5 compress(key, block_words, 0, BLAKE3_BLOCK_SIZE,
295 flags | PARENT | ROOT, out);
296 45 ▶ for (i = 0; i < 8; i++) store32_le(out_bytes + i * 4, out[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) store32_le(out_bytes + i * 4, out[i]); 40 5 45
297 5 }
298  
299 /* ---------------- public API ---------------- */
300  
301 void
302 19 blake3_init(blake3_ctx_t *ctx)
303 {
304 int i;
305 171 ▶ for (i = 0; i < 8; i++) ctx->key_words[i] = IV[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) ctx->key_words[i] = IV[i]; 152 19 171
306 19 ctx->flags = 0; /* hash mode */
307 19 ctx->cv_stack_len = 0;
308 19 chunk_init(&ctx->chunk, ctx->key_words, 0);
309 19 }
310  
311 /* Push a CV onto the stack and collapse pairs of equal-level CVs into
312 * parents. The trick: every time we add chunk N+1 to the tree, we
313 * collapse pairs starting at the level matching the count of trailing
314 * zeros in N+1. We track the chunk index implicitly via the stack
315 * length; the "merge if total chunks is even at this level" check is
316 * encoded by counting trailing zeros in the post-add chunk count. */
317 static void
318 27 push_chunk_cv(blake3_ctx_t *ctx, uint32_t new_cv[8], uint64_t total_chunks)
319 {
320 /* `total_chunks` is the number of chunks ALREADY added (so the new
321 * one being pushed is chunk index `total_chunks`, and after push
322 * we'll have total_chunks+1 chunks total). The standard collapses
323 * stack pairs while the count of completed chunks is even at the
324 * relevant level - equivalently while the LSB-going-up bits of
325 * total_chunks (post-increment) match. */
326 27 uint64_t post = total_chunks; /* chunks completed so far before push */
327 uint32_t cv[8];
328 int i;
329 243 ▶ for (i = 0; i < 8; i++) cv[i] = new_cv[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) cv[i] = new_cv[i]; 216 27 243
330  
331 /* Merge while the bit at the current level of post is set; that
332 * indicates an unpaired CV at that level. */
333 47 ▶ while (post & 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (post & 1) { 20 27 47
334 uint32_t left[8];
335 20 ctx->cv_stack_len--;
336 180 ▶ for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i]; 160 20 180
337 20 parent_cv(left, cv, ctx->key_words, ctx->flags, cv);
338 20 post >>= 1;
339 }
340 243 ▶ for (i = 0; i < 8; i++) ctx->cv_stack[ctx->cv_stack_len][i] = cv[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) ctx->cv_stack[ctx->cv_stack_len][i] = cv[i]; 216 27 243
341 27 ctx->cv_stack_len++;
342 27 }
343  
344 void
345 15 blake3_update(blake3_ctx_t *ctx, const void *data, size_t len)
346 {
347 15 const unsigned char *p = (const unsigned char *)data;
348  
349 57 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 42 15 57
350 size_t want;
351 42 size_t cur = chunk_len(&ctx->chunk);
352  
353 /* If the current chunk is full, finalise it as a leaf and
354 * rotate to a fresh chunk. */
355 42 ▶ if (cur == BLAKE3_CHUNK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur == BLAKE3_CHUNK_SIZE) { 27 15 42
356 uint32_t leaf_cv[8];
357 27 uint64_t this_idx = ctx->chunk.chunk_counter;
358 27 chunk_output_cv(&ctx->chunk, ctx->flags, leaf_cv);
359 27 push_chunk_cv(ctx, leaf_cv, this_idx);
360 27 chunk_init(&ctx->chunk, ctx->key_words, this_idx + 1);
361 27 cur = 0;
362 }
363  
364 42 want = BLAKE3_CHUNK_SIZE - cur;
365 42 ▶ if (want > len) want = len;
Branch: 1 branch
BranchTrueFalseTotal
if (want > len) want = len; 14 28 42
366 42 chunk_update(&ctx->chunk, p, want, ctx->flags);
367 42 p += want;
368 42 len -= want;
369 }
370 15 }
371  
372 void
373 19 blake3_final(blake3_ctx_t *ctx, unsigned char out[BLAKE3_DIGEST_SIZE])
374 {
375 int i;
376  
377 /* If we never compressed any leaf into the stack, this is a
378 * single-chunk hash: the chunk itself is the root. */
379 19 ▶ if (ctx->cv_stack_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cv_stack_len == 0) { 14 5 19
380 14 chunk_output_root(&ctx->chunk, ctx->flags, out);
381 14 return;
382 }
383  
384 /* Otherwise we collapse the stack from the bottom up. The chunk
385 * we hold becomes the rightmost leaf at the bottom level; combine
386 * it with whatever sits at the top of the stack until we reach the
387 * single root parent that needs ROOT-flag output. */
388 {
389 uint32_t cv[8];
390 5 chunk_output_cv(&ctx->chunk, ctx->flags, cv);
391  
392 /* Walk the stack: every entry must be combined as the LEFT
393 * sibling of `cv`. When only one stack entry remains, the
394 * combination is the ROOT and produces output bytes. */
395 7 ▶ while (ctx->cv_stack_len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx->cv_stack_len > 1) { 2 5 7
396 uint32_t left[8];
397 2 ctx->cv_stack_len--;
398 18 ▶ for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i]; 16 2 18
399 2 parent_cv(left, cv, ctx->key_words, ctx->flags, cv);
400 }
401 /* Final root parent. */
402 {
403 uint32_t left[8];
404 5 ctx->cv_stack_len--;
405 45 ▶ for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) left[i] = ctx->cv_stack[ctx->cv_stack_len][i]; 40 5 45
406 5 parent_output_root(left, cv, ctx->key_words, ctx->flags, out);
407 }
408 }
409 }
« sha2.c ↑ Summary blib/lib/File/Raw/Hash.pm »