Debian Codegraph @

Modules/_hacl/Hacl_Hash_Blake2b.c

raw
1 /* MIT License
2 *
3 * Copyright (c) 2016-2022 INRIA, CMU and Microsoft Corporation
4 * Copyright (c) 2022-2023 HACL* Contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25
26 #include "internal/Hacl_Hash_Blake2b.h"
27
28 #include "Hacl_Streaming_Types.h"
29
30 #include "internal/Hacl_Streaming_Types.h"
31 #include "internal/Hacl_Impl_Blake2_Constants.h"
32 #include "lib_memzero0.h"
33
34 static void
35 update_block(
36 uint64_t *wv,
37 uint64_t *hash,
38 bool flag,
39 bool last_node,
40 FStar_UInt128_uint128 totlen,
41 uint8_t *d
42 )
43 {
44 uint64_t m_w[16U] = { 0U };
45 KRML_MAYBE_FOR16(i,
46 0U,
47 16U,
48 1U,
49 uint64_t *os = m_w;
50 uint8_t *bj = d + i * 8U;
51 uint64_t u = load64_le(bj);
52 uint64_t r = u;
53 uint64_t x = r;
54 os[i] = x;);
55 uint64_t mask[4U] = { 0U };
56 uint64_t wv_14;
57 if (flag)
58 {
59 wv_14 = 0xFFFFFFFFFFFFFFFFULL;
60 }
61 else
62 {
63 wv_14 = 0ULL;
64 }
65 uint64_t wv_15;
66 if (last_node)
67 {
68 wv_15 = 0xFFFFFFFFFFFFFFFFULL;
69 }
70 else
71 {
72 wv_15 = 0ULL;
73 }
74 mask[0U] = FStar_UInt128_uint128_to_uint64(totlen);
75 mask[1U] = FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(totlen, 64U));
76 mask[2U] = wv_14;
77 mask[3U] = wv_15;
78 memcpy(wv, hash, 16U * sizeof (uint64_t));
79 uint64_t *wv3 = wv + 12U;
80 KRML_MAYBE_FOR4(i,
81 0U,
82 4U,
83 1U,
84 uint64_t *os = wv3;
85 uint64_t x = wv3[i] ^ mask[i];
86 os[i] = x;);
87 KRML_MAYBE_FOR12(i0,
88 0U,
89 12U,
90 1U,
91 uint32_t start_idx = i0 % 10U * 16U;
92 uint64_t m_st[16U] = { 0U };
93 uint64_t *r0 = m_st;
94 uint64_t *r1 = m_st + 4U;
95 uint64_t *r20 = m_st + 8U;
96 uint64_t *r30 = m_st + 12U;
97 uint32_t s0 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 0U];
98 uint32_t s1 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 1U];
99 uint32_t s2 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 2U];
100 uint32_t s3 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 3U];
101 uint32_t s4 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 4U];
102 uint32_t s5 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 5U];
103 uint32_t s6 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 6U];
104 uint32_t s7 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 7U];
105 uint32_t s8 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 8U];
106 uint32_t s9 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 9U];
107 uint32_t s10 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 10U];
108 uint32_t s11 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 11U];
109 uint32_t s12 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 12U];
110 uint32_t s13 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 13U];
111 uint32_t s14 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 14U];
112 uint32_t s15 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 15U];
113 uint64_t uu____0 = m_w[s2];
114 uint64_t uu____1 = m_w[s4];
115 uint64_t uu____2 = m_w[s6];
116 r0[0U] = m_w[s0];
117 r0[1U] = uu____0;
118 r0[2U] = uu____1;
119 r0[3U] = uu____2;
120 uint64_t uu____3 = m_w[s3];
121 uint64_t uu____4 = m_w[s5];
122 uint64_t uu____5 = m_w[s7];
123 r1[0U] = m_w[s1];
124 r1[1U] = uu____3;
125 r1[2U] = uu____4;
126 r1[3U] = uu____5;
127 uint64_t uu____6 = m_w[s10];
128 uint64_t uu____7 = m_w[s12];
129 uint64_t uu____8 = m_w[s14];
130 r20[0U] = m_w[s8];
131 r20[1U] = uu____6;
132 r20[2U] = uu____7;
133 r20[3U] = uu____8;
134 uint64_t uu____9 = m_w[s11];
135 uint64_t uu____10 = m_w[s13];
136 uint64_t uu____11 = m_w[s15];
137 r30[0U] = m_w[s9];
138 r30[1U] = uu____9;
139 r30[2U] = uu____10;
140 r30[3U] = uu____11;
141 uint64_t *x = m_st;
142 uint64_t *y = m_st + 4U;
143 uint64_t *z = m_st + 8U;
144 uint64_t *w = m_st + 12U;
145 uint32_t a = 0U;
146 uint32_t b0 = 1U;
147 uint32_t c0 = 2U;
148 uint32_t d10 = 3U;
149 uint64_t *wv_a0 = wv + a * 4U;
150 uint64_t *wv_b0 = wv + b0 * 4U;
151 KRML_MAYBE_FOR4(i,
152 0U,
153 4U,
154 1U,
155 uint64_t *os = wv_a0;
156 uint64_t x1 = wv_a0[i] + wv_b0[i];
157 os[i] = x1;);
158 KRML_MAYBE_FOR4(i,
159 0U,
160 4U,
161 1U,
162 uint64_t *os = wv_a0;
163 uint64_t x1 = wv_a0[i] + x[i];
164 os[i] = x1;);
165 uint64_t *wv_a1 = wv + d10 * 4U;
166 uint64_t *wv_b1 = wv + a * 4U;
167 KRML_MAYBE_FOR4(i,
168 0U,
169 4U,
170 1U,
171 uint64_t *os = wv_a1;
172 uint64_t x1 = wv_a1[i] ^ wv_b1[i];
173 os[i] = x1;);
174 uint64_t *r10 = wv_a1;
175 KRML_MAYBE_FOR4(i,
176 0U,
177 4U,
178 1U,
179 uint64_t *os = r10;
180 uint64_t x1 = r10[i];
181 uint64_t x10 = x1 >> 32U | x1 << 32U;
182 os[i] = x10;);
183 uint64_t *wv_a2 = wv + c0 * 4U;
184 uint64_t *wv_b2 = wv + d10 * 4U;
185 KRML_MAYBE_FOR4(i,
186 0U,
187 4U,
188 1U,
189 uint64_t *os = wv_a2;
190 uint64_t x1 = wv_a2[i] + wv_b2[i];
191 os[i] = x1;);
192 uint64_t *wv_a3 = wv + b0 * 4U;
193 uint64_t *wv_b3 = wv + c0 * 4U;
194 KRML_MAYBE_FOR4(i,
195 0U,
196 4U,
197 1U,
198 uint64_t *os = wv_a3;
199 uint64_t x1 = wv_a3[i] ^ wv_b3[i];
200 os[i] = x1;);
201 uint64_t *r12 = wv_a3;
202 KRML_MAYBE_FOR4(i,
203 0U,
204 4U,
205 1U,
206 uint64_t *os = r12;
207 uint64_t x1 = r12[i];
208 uint64_t x10 = x1 >> 24U | x1 << 40U;
209 os[i] = x10;);
210 uint64_t *wv_a4 = wv + a * 4U;
211 uint64_t *wv_b4 = wv + b0 * 4U;
212 KRML_MAYBE_FOR4(i,
213 0U,
214 4U,
215 1U,
216 uint64_t *os = wv_a4;
217 uint64_t x1 = wv_a4[i] + wv_b4[i];
218 os[i] = x1;);
219 KRML_MAYBE_FOR4(i,
220 0U,
221 4U,
222 1U,
223 uint64_t *os = wv_a4;
224 uint64_t x1 = wv_a4[i] + y[i];
225 os[i] = x1;);
226 uint64_t *wv_a5 = wv + d10 * 4U;
227 uint64_t *wv_b5 = wv + a * 4U;
228 KRML_MAYBE_FOR4(i,
229 0U,
230 4U,
231 1U,
232 uint64_t *os = wv_a5;
233 uint64_t x1 = wv_a5[i] ^ wv_b5[i];
234 os[i] = x1;);
235 uint64_t *r13 = wv_a5;
236 KRML_MAYBE_FOR4(i,
237 0U,
238 4U,
239 1U,
240 uint64_t *os = r13;
241 uint64_t x1 = r13[i];
242 uint64_t x10 = x1 >> 16U | x1 << 48U;
243 os[i] = x10;);
244 uint64_t *wv_a6 = wv + c0 * 4U;
245 uint64_t *wv_b6 = wv + d10 * 4U;
246 KRML_MAYBE_FOR4(i,
247 0U,
248 4U,
249 1U,
250 uint64_t *os = wv_a6;
251 uint64_t x1 = wv_a6[i] + wv_b6[i];
252 os[i] = x1;);
253 uint64_t *wv_a7 = wv + b0 * 4U;
254 uint64_t *wv_b7 = wv + c0 * 4U;
255 KRML_MAYBE_FOR4(i,
256 0U,
257 4U,
258 1U,
259 uint64_t *os = wv_a7;
260 uint64_t x1 = wv_a7[i] ^ wv_b7[i];
261 os[i] = x1;);
262 uint64_t *r14 = wv_a7;
263 KRML_MAYBE_FOR4(i,
264 0U,
265 4U,
266 1U,
267 uint64_t *os = r14;
268 uint64_t x1 = r14[i];
269 uint64_t x10 = x1 >> 63U | x1 << 1U;
270 os[i] = x10;);
271 uint64_t *r15 = wv + 4U;
272 uint64_t *r21 = wv + 8U;
273 uint64_t *r31 = wv + 12U;
274 uint64_t *r110 = r15;
275 uint64_t x00 = r110[1U];
276 uint64_t x10 = r110[2U];
277 uint64_t x20 = r110[3U];
278 uint64_t x30 = r110[0U];
279 r110[0U] = x00;
280 r110[1U] = x10;
281 r110[2U] = x20;
282 r110[3U] = x30;
283 uint64_t *r111 = r21;
284 uint64_t x01 = r111[2U];
285 uint64_t x11 = r111[3U];
286 uint64_t x21 = r111[0U];
287 uint64_t x31 = r111[1U];
288 r111[0U] = x01;
289 r111[1U] = x11;
290 r111[2U] = x21;
291 r111[3U] = x31;
292 uint64_t *r112 = r31;
293 uint64_t x02 = r112[3U];
294 uint64_t x12 = r112[0U];
295 uint64_t x22 = r112[1U];
296 uint64_t x32 = r112[2U];
297 r112[0U] = x02;
298 r112[1U] = x12;
299 r112[2U] = x22;
300 r112[3U] = x32;
301 uint32_t a0 = 0U;
302 uint32_t b = 1U;
303 uint32_t c = 2U;
304 uint32_t d1 = 3U;
305 uint64_t *wv_a = wv + a0 * 4U;
306 uint64_t *wv_b8 = wv + b * 4U;
307 KRML_MAYBE_FOR4(i,
308 0U,
309 4U,
310 1U,
311 uint64_t *os = wv_a;
312 uint64_t x1 = wv_a[i] + wv_b8[i];
313 os[i] = x1;);
314 KRML_MAYBE_FOR4(i,
315 0U,
316 4U,
317 1U,
318 uint64_t *os = wv_a;
319 uint64_t x1 = wv_a[i] + z[i];
320 os[i] = x1;);
321 uint64_t *wv_a8 = wv + d1 * 4U;
322 uint64_t *wv_b9 = wv + a0 * 4U;
323 KRML_MAYBE_FOR4(i,
324 0U,
325 4U,
326 1U,
327 uint64_t *os = wv_a8;
328 uint64_t x1 = wv_a8[i] ^ wv_b9[i];
329 os[i] = x1;);
330 uint64_t *r16 = wv_a8;
331 KRML_MAYBE_FOR4(i,
332 0U,
333 4U,
334 1U,
335 uint64_t *os = r16;
336 uint64_t x1 = r16[i];
337 uint64_t x13 = x1 >> 32U | x1 << 32U;
338 os[i] = x13;);
339 uint64_t *wv_a9 = wv + c * 4U;
340 uint64_t *wv_b10 = wv + d1 * 4U;
341 KRML_MAYBE_FOR4(i,
342 0U,
343 4U,
344 1U,
345 uint64_t *os = wv_a9;
346 uint64_t x1 = wv_a9[i] + wv_b10[i];
347 os[i] = x1;);
348 uint64_t *wv_a10 = wv + b * 4U;
349 uint64_t *wv_b11 = wv + c * 4U;
350 KRML_MAYBE_FOR4(i,
351 0U,
352 4U,
353 1U,
354 uint64_t *os = wv_a10;
355 uint64_t x1 = wv_a10[i] ^ wv_b11[i];
356 os[i] = x1;);
357 uint64_t *r17 = wv_a10;
358 KRML_MAYBE_FOR4(i,
359 0U,
360 4U,
361 1U,
362 uint64_t *os = r17;
363 uint64_t x1 = r17[i];
364 uint64_t x13 = x1 >> 24U | x1 << 40U;
365 os[i] = x13;);
366 uint64_t *wv_a11 = wv + a0 * 4U;
367 uint64_t *wv_b12 = wv + b * 4U;
368 KRML_MAYBE_FOR4(i,
369 0U,
370 4U,
371 1U,
372 uint64_t *os = wv_a11;
373 uint64_t x1 = wv_a11[i] + wv_b12[i];
374 os[i] = x1;);
375 KRML_MAYBE_FOR4(i,
376 0U,
377 4U,
378 1U,
379 uint64_t *os = wv_a11;
380 uint64_t x1 = wv_a11[i] + w[i];
381 os[i] = x1;);
382 uint64_t *wv_a12 = wv + d1 * 4U;
383 uint64_t *wv_b13 = wv + a0 * 4U;
384 KRML_MAYBE_FOR4(i,
385 0U,
386 4U,
387 1U,
388 uint64_t *os = wv_a12;
389 uint64_t x1 = wv_a12[i] ^ wv_b13[i];
390 os[i] = x1;);
391 uint64_t *r18 = wv_a12;
392 KRML_MAYBE_FOR4(i,
393 0U,
394 4U,
395 1U,
396 uint64_t *os = r18;
397 uint64_t x1 = r18[i];
398 uint64_t x13 = x1 >> 16U | x1 << 48U;
399 os[i] = x13;);
400 uint64_t *wv_a13 = wv + c * 4U;
401 uint64_t *wv_b14 = wv + d1 * 4U;
402 KRML_MAYBE_FOR4(i,
403 0U,
404 4U,
405 1U,
406 uint64_t *os = wv_a13;
407 uint64_t x1 = wv_a13[i] + wv_b14[i];
408 os[i] = x1;);
409 uint64_t *wv_a14 = wv + b * 4U;
410 uint64_t *wv_b = wv + c * 4U;
411 KRML_MAYBE_FOR4(i,
412 0U,
413 4U,
414 1U,
415 uint64_t *os = wv_a14;
416 uint64_t x1 = wv_a14[i] ^ wv_b[i];
417 os[i] = x1;);
418 uint64_t *r19 = wv_a14;
419 KRML_MAYBE_FOR4(i,
420 0U,
421 4U,
422 1U,
423 uint64_t *os = r19;
424 uint64_t x1 = r19[i];
425 uint64_t x13 = x1 >> 63U | x1 << 1U;
426 os[i] = x13;);
427 uint64_t *r113 = wv + 4U;
428 uint64_t *r2 = wv + 8U;
429 uint64_t *r3 = wv + 12U;
430 uint64_t *r11 = r113;
431 uint64_t x03 = r11[3U];
432 uint64_t x13 = r11[0U];
433 uint64_t x23 = r11[1U];
434 uint64_t x33 = r11[2U];
435 r11[0U] = x03;
436 r11[1U] = x13;
437 r11[2U] = x23;
438 r11[3U] = x33;
439 uint64_t *r114 = r2;
440 uint64_t x04 = r114[2U];
441 uint64_t x14 = r114[3U];
442 uint64_t x24 = r114[0U];
443 uint64_t x34 = r114[1U];
444 r114[0U] = x04;
445 r114[1U] = x14;
446 r114[2U] = x24;
447 r114[3U] = x34;
448 uint64_t *r115 = r3;
449 uint64_t x0 = r115[1U];
450 uint64_t x1 = r115[2U];
451 uint64_t x2 = r115[3U];
452 uint64_t x3 = r115[0U];
453 r115[0U] = x0;
454 r115[1U] = x1;
455 r115[2U] = x2;
456 r115[3U] = x3;);
457 uint64_t *s0 = hash;
458 uint64_t *s1 = hash + 4U;
459 uint64_t *r0 = wv;
460 uint64_t *r1 = wv + 4U;
461 uint64_t *r2 = wv + 8U;
462 uint64_t *r3 = wv + 12U;
463 KRML_MAYBE_FOR4(i,
464 0U,
465 4U,
466 1U,
467 uint64_t *os = s0;
468 uint64_t x = s0[i] ^ r0[i];
469 os[i] = x;);
470 KRML_MAYBE_FOR4(i,
471 0U,
472 4U,
473 1U,
474 uint64_t *os = s0;
475 uint64_t x = s0[i] ^ r2[i];
476 os[i] = x;);
477 KRML_MAYBE_FOR4(i,
478 0U,
479 4U,
480 1U,
481 uint64_t *os = s1;
482 uint64_t x = s1[i] ^ r1[i];
483 os[i] = x;);
484 KRML_MAYBE_FOR4(i,
485 0U,
486 4U,
487 1U,
488 uint64_t *os = s1;
489 uint64_t x = s1[i] ^ r3[i];
490 os[i] = x;);
491 }
492
493 void Hacl_Hash_Blake2b_init(uint64_t *hash, uint32_t kk, uint32_t nn)
494 {
495 uint8_t salt[16U] = { 0U };
496 uint8_t personal[16U] = { 0U };
497 Hacl_Hash_Blake2b_blake2_params
498 p =
499 {
500 .digest_length = 64U, .key_length = 0U, .fanout = 1U, .depth = 1U, .leaf_length = 0U,
501 .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt, .personal = personal
502 };
503 uint64_t tmp[8U] = { 0U };
504 uint64_t *r0 = hash;
505 uint64_t *r1 = hash + 4U;
506 uint64_t *r2 = hash + 8U;
507 uint64_t *r3 = hash + 12U;
508 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
509 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
510 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
511 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
512 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
513 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
514 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
515 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
516 r2[0U] = iv0;
517 r2[1U] = iv1;
518 r2[2U] = iv2;
519 r2[3U] = iv3;
520 r3[0U] = iv4;
521 r3[1U] = iv5;
522 r3[2U] = iv6;
523 r3[3U] = iv7;
524 uint8_t kk1 = (uint8_t)kk;
525 uint8_t nn1 = (uint8_t)nn;
526 KRML_MAYBE_FOR2(i,
527 0U,
528 2U,
529 1U,
530 uint64_t *os = tmp + 4U;
531 uint8_t *bj = p.salt + i * 8U;
532 uint64_t u = load64_le(bj);
533 uint64_t r = u;
534 uint64_t x = r;
535 os[i] = x;);
536 KRML_MAYBE_FOR2(i,
537 0U,
538 2U,
539 1U,
540 uint64_t *os = tmp + 6U;
541 uint8_t *bj = p.personal + i * 8U;
542 uint64_t u = load64_le(bj);
543 uint64_t r = u;
544 uint64_t x = r;
545 os[i] = x;);
546 tmp[0U] =
547 (uint64_t)nn1 ^
548 ((uint64_t)kk1 << 8U ^
549 ((uint64_t)p.fanout << 16U ^ ((uint64_t)p.depth << 24U ^ (uint64_t)p.leaf_length << 32U)));
550 tmp[1U] = p.node_offset;
551 tmp[2U] = (uint64_t)p.node_depth ^ (uint64_t)p.inner_length << 8U;
552 tmp[3U] = 0ULL;
553 uint64_t tmp0 = tmp[0U];
554 uint64_t tmp1 = tmp[1U];
555 uint64_t tmp2 = tmp[2U];
556 uint64_t tmp3 = tmp[3U];
557 uint64_t tmp4 = tmp[4U];
558 uint64_t tmp5 = tmp[5U];
559 uint64_t tmp6 = tmp[6U];
560 uint64_t tmp7 = tmp[7U];
561 uint64_t iv0_ = iv0 ^ tmp0;
562 uint64_t iv1_ = iv1 ^ tmp1;
563 uint64_t iv2_ = iv2 ^ tmp2;
564 uint64_t iv3_ = iv3 ^ tmp3;
565 uint64_t iv4_ = iv4 ^ tmp4;
566 uint64_t iv5_ = iv5 ^ tmp5;
567 uint64_t iv6_ = iv6 ^ tmp6;
568 uint64_t iv7_ = iv7 ^ tmp7;
569 r0[0U] = iv0_;
570 r0[1U] = iv1_;
571 r0[2U] = iv2_;
572 r0[3U] = iv3_;
573 r1[0U] = iv4_;
574 r1[1U] = iv5_;
575 r1[2U] = iv6_;
576 r1[3U] = iv7_;
577 }
578
579 static void update_key(uint64_t *wv, uint64_t *hash, uint32_t kk, uint8_t *k, uint32_t ll)
580 {
581 FStar_UInt128_uint128 lb = FStar_UInt128_uint64_to_uint128((uint64_t)128U);
582 uint8_t b[128U] = { 0U };
583 memcpy(b, k, kk * sizeof (uint8_t));
584 if (ll == 0U)
585 {
586 update_block(wv, hash, true, false, lb, b);
587 }
588 else
589 {
590 update_block(wv, hash, false, false, lb, b);
591 }
592 Lib_Memzero0_memzero(b, 128U, uint8_t, void *);
593 }
594
595 void
596 Hacl_Hash_Blake2b_update_multi(
597 uint32_t len,
598 uint64_t *wv,
599 uint64_t *hash,
600 FStar_UInt128_uint128 prev,
601 uint8_t *blocks,
602 uint32_t nb
603 )
604 {
605 KRML_MAYBE_UNUSED_VAR(len);
606 for (uint32_t i = 0U; i < nb; i++)
607 {
608 FStar_UInt128_uint128
609 totlen =
610 FStar_UInt128_add_mod(prev,
611 FStar_UInt128_uint64_to_uint128((uint64_t)((i + 1U) * 128U)));
612 uint8_t *b = blocks + i * 128U;
613 update_block(wv, hash, false, false, totlen, b);
614 }
615 }
616
617 void
618 Hacl_Hash_Blake2b_update_last(
619 uint32_t len,
620 uint64_t *wv,
621 uint64_t *hash,
622 bool last_node,
623 FStar_UInt128_uint128 prev,
624 uint32_t rem,
625 uint8_t *d
626 )
627 {
628 uint8_t b[128U] = { 0U };
629 uint8_t *last = d + len - rem;
630 memcpy(b, last, rem * sizeof (uint8_t));
631 FStar_UInt128_uint128
632 totlen = FStar_UInt128_add_mod(prev, FStar_UInt128_uint64_to_uint128((uint64_t)len));
633 update_block(wv, hash, true, last_node, totlen, b);
634 Lib_Memzero0_memzero(b, 128U, uint8_t, void *);
635 }
636
637 static void
638 update_blocks(
639 uint32_t len,
640 uint64_t *wv,
641 uint64_t *hash,
642 FStar_UInt128_uint128 prev,
643 uint8_t *blocks
644 )
645 {
646 uint32_t nb0 = len / 128U;
647 uint32_t rem0 = len % 128U;
648 uint32_t nb;
649 if (rem0 == 0U && nb0 > 0U)
650 {
651 nb = nb0 - 1U;
652 }
653 else
654 {
655 nb = nb0;
656 }
657 uint32_t rem;
658 if (rem0 == 0U && nb0 > 0U)
659 {
660 rem = 128U;
661 }
662 else
663 {
664 rem = rem0;
665 }
666 Hacl_Hash_Blake2b_update_multi(len, wv, hash, prev, blocks, nb);
667 Hacl_Hash_Blake2b_update_last(len, wv, hash, false, prev, rem, blocks);
668 }
669
670 static inline void
671 update(uint64_t *wv, uint64_t *hash, uint32_t kk, uint8_t *k, uint32_t ll, uint8_t *d)
672 {
673 FStar_UInt128_uint128 lb = FStar_UInt128_uint64_to_uint128((uint64_t)128U);
674 if (kk > 0U)
675 {
676 update_key(wv, hash, kk, k, ll);
677 if (!(ll == 0U))
678 {
679 update_blocks(ll, wv, hash, lb, d);
680 return;
681 }
682 return;
683 }
684 update_blocks(ll, wv, hash, FStar_UInt128_uint64_to_uint128((uint64_t)0U), d);
685 }
686
687 void Hacl_Hash_Blake2b_finish(uint32_t nn, uint8_t *output, uint64_t *hash)
688 {
689 uint8_t b[64U] = { 0U };
690 uint8_t *first = b;
691 uint8_t *second = b + 32U;
692 uint64_t *row0 = hash;
693 uint64_t *row1 = hash + 4U;
694 KRML_MAYBE_FOR4(i, 0U, 4U, 1U, store64_le(first + i * 8U, row0[i]););
695 KRML_MAYBE_FOR4(i, 0U, 4U, 1U, store64_le(second + i * 8U, row1[i]););
696 uint8_t *final = b;
697 memcpy(output, final, nn * sizeof (uint8_t));
698 Lib_Memzero0_memzero(b, 64U, uint8_t, void *);
699 }
700
701 typedef struct option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t____s
702 {
703 Hacl_Streaming_Types_optional tag;
704 Hacl_Hash_Blake2b_block_state_t v;
705 }
706 option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___;
707
708 static Hacl_Hash_Blake2b_state_t
709 *malloc_raw(Hacl_Hash_Blake2b_index kk, Hacl_Hash_Blake2b_params_and_key key)
710 {
711 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
712 if (buf == NULL)
713 {
714 return NULL;
715 }
716 uint8_t *buf1 = buf;
717 uint64_t *wv0 = (uint64_t *)KRML_HOST_CALLOC(16U, sizeof (uint64_t));
718 option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___ block_state;
719 if (wv0 == NULL)
720 {
721 block_state =
722 (
723 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
724 .tag = Hacl_Streaming_Types_None
725 }
726 );
727 }
728 else
729 {
730 uint64_t *b = (uint64_t *)KRML_HOST_CALLOC(16U, sizeof (uint64_t));
731 if (b == NULL)
732 {
733 KRML_HOST_FREE(wv0);
734 block_state =
735 (
736 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
737 .tag = Hacl_Streaming_Types_None
738 }
739 );
740 }
741 else
742 {
743 block_state =
744 (
745 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
746 .tag = Hacl_Streaming_Types_Some,
747 .v = {
748 .fst = kk.key_length,
749 .snd = kk.digest_length,
750 .thd = kk.last_node,
751 .f3 = { .fst = wv0, .snd = b }
752 }
753 }
754 );
755 }
756 }
757 if (block_state.tag == Hacl_Streaming_Types_None)
758 {
759 KRML_HOST_FREE(buf1);
760 return NULL;
761 }
762 if (block_state.tag == Hacl_Streaming_Types_Some)
763 {
764 Hacl_Hash_Blake2b_block_state_t block_state1 = block_state.v;
765 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
766 switch (k_)
767 {
768 case Hacl_Streaming_Types_None:
769 {
770 return NULL;
771 }
772 case Hacl_Streaming_Types_Some:
773 {
774 uint8_t kk10 = kk.key_length;
775 uint32_t ite;
776 if (kk10 != 0U)
777 {
778 ite = 128U;
779 }
780 else
781 {
782 ite = 0U;
783 }
784 Hacl_Hash_Blake2b_state_t
785 s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)ite };
786 Hacl_Hash_Blake2b_state_t
787 *p = (Hacl_Hash_Blake2b_state_t *)KRML_HOST_MALLOC(sizeof (Hacl_Hash_Blake2b_state_t));
788 if (p != NULL)
789 {
790 p[0U] = s;
791 }
792 if (p == NULL)
793 {
794 uint64_t *b = block_state1.f3.snd;
795 uint64_t *wv = block_state1.f3.fst;
796 KRML_HOST_FREE(wv);
797 KRML_HOST_FREE(b);
798 KRML_HOST_FREE(buf1);
799 return NULL;
800 }
801 Hacl_Hash_Blake2b_blake2_params *p1 = key.fst;
802 uint8_t kk1 = p1->key_length;
803 uint8_t nn = p1->digest_length;
804 bool last_node = block_state1.thd;
805 Hacl_Hash_Blake2b_index
806 i = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
807 uint64_t *h = block_state1.f3.snd;
808 uint32_t kk20 = (uint32_t)i.key_length;
809 uint8_t *k_2 = key.snd;
810 if (!(kk20 == 0U))
811 {
812 uint8_t *sub_b = buf1 + kk20;
813 memset(sub_b, 0U, (128U - kk20) * sizeof (uint8_t));
814 memcpy(buf1, k_2, kk20 * sizeof (uint8_t));
815 }
816 Hacl_Hash_Blake2b_blake2_params pv = p1[0U];
817 uint64_t tmp[8U] = { 0U };
818 uint64_t *r0 = h;
819 uint64_t *r1 = h + 4U;
820 uint64_t *r2 = h + 8U;
821 uint64_t *r3 = h + 12U;
822 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
823 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
824 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
825 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
826 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
827 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
828 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
829 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
830 r2[0U] = iv0;
831 r2[1U] = iv1;
832 r2[2U] = iv2;
833 r2[3U] = iv3;
834 r3[0U] = iv4;
835 r3[1U] = iv5;
836 r3[2U] = iv6;
837 r3[3U] = iv7;
838 uint8_t kk2 = pv.key_length;
839 uint8_t nn1 = pv.digest_length;
840 KRML_MAYBE_FOR2(i0,
841 0U,
842 2U,
843 1U,
844 uint64_t *os = tmp + 4U;
845 uint8_t *bj = pv.salt + i0 * 8U;
846 uint64_t u = load64_le(bj);
847 uint64_t r4 = u;
848 uint64_t x = r4;
849 os[i0] = x;);
850 KRML_MAYBE_FOR2(i0,
851 0U,
852 2U,
853 1U,
854 uint64_t *os = tmp + 6U;
855 uint8_t *bj = pv.personal + i0 * 8U;
856 uint64_t u = load64_le(bj);
857 uint64_t r4 = u;
858 uint64_t x = r4;
859 os[i0] = x;);
860 tmp[0U] =
861 (uint64_t)nn1 ^
862 ((uint64_t)kk2 << 8U ^
863 ((uint64_t)pv.fanout << 16U ^
864 ((uint64_t)pv.depth << 24U ^ (uint64_t)pv.leaf_length << 32U)));
865 tmp[1U] = pv.node_offset;
866 tmp[2U] = (uint64_t)pv.node_depth ^ (uint64_t)pv.inner_length << 8U;
867 tmp[3U] = 0ULL;
868 uint64_t tmp0 = tmp[0U];
869 uint64_t tmp1 = tmp[1U];
870 uint64_t tmp2 = tmp[2U];
871 uint64_t tmp3 = tmp[3U];
872 uint64_t tmp4 = tmp[4U];
873 uint64_t tmp5 = tmp[5U];
874 uint64_t tmp6 = tmp[6U];
875 uint64_t tmp7 = tmp[7U];
876 uint64_t iv0_ = iv0 ^ tmp0;
877 uint64_t iv1_ = iv1 ^ tmp1;
878 uint64_t iv2_ = iv2 ^ tmp2;
879 uint64_t iv3_ = iv3 ^ tmp3;
880 uint64_t iv4_ = iv4 ^ tmp4;
881 uint64_t iv5_ = iv5 ^ tmp5;
882 uint64_t iv6_ = iv6 ^ tmp6;
883 uint64_t iv7_ = iv7 ^ tmp7;
884 r0[0U] = iv0_;
885 r0[1U] = iv1_;
886 r0[2U] = iv2_;
887 r0[3U] = iv3_;
888 r1[0U] = iv4_;
889 r1[1U] = iv5_;
890 r1[2U] = iv6_;
891 r1[3U] = iv7_;
892 return p;
893 }
894 default:
895 {
896 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
897 KRML_HOST_EXIT(253U);
898 }
899 }
900 }
901 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
902 __FILE__,
903 __LINE__,
904 "unreachable (pattern matches are exhaustive in F*)");
905 KRML_HOST_EXIT(255U);
906 }
907
908 /**
909 General-purpose allocation function that gives control over all
910 Blake2 parameters, including the key. Further resettings of the state SHALL be
911 done with `reset_with_params_and_key`, and SHALL feature the exact same values
912 for the `key_length` and `digest_length` fields as passed here. In other words,
913 once you commit to a digest and key length, the only way to change these
914 parameters is to allocate a new object.
915
916 The caller must satisfy the following requirements.
917 - The length of the key k MUST match the value of the field key_length in the
918 parameters.
919 - The key_length must not exceed 32 for S, 64 for B.
920 - The digest_length must not exceed 32 for S, 64 for B.
921
922 */
923 Hacl_Hash_Blake2b_state_t
924 *Hacl_Hash_Blake2b_malloc_with_params_and_key(
925 Hacl_Hash_Blake2b_blake2_params *p,
926 bool last_node,
927 uint8_t *k
928 )
929 {
930 Hacl_Hash_Blake2b_blake2_params pv = p[0U];
931 Hacl_Hash_Blake2b_index
932 i1 = { .key_length = pv.key_length, .digest_length = pv.digest_length, .last_node = last_node };
933 return malloc_raw(i1, ((Hacl_Hash_Blake2b_params_and_key){ .fst = p, .snd = k }));
934 }
935
936 /**
937 Specialized allocation function that picks default values for all
938 parameters, except for the key_length. Further resettings of the state SHALL be
939 done with `reset_with_key`, and SHALL feature the exact same key length `kk` as
940 passed here. In other words, once you commit to a key length, the only way to
941 change this parameter is to allocate a new object.
942
943 The caller must satisfy the following requirements.
944 - The key_length must not exceed 32 for S, 64 for B.
945
946 */
947 Hacl_Hash_Blake2b_state_t *Hacl_Hash_Blake2b_malloc_with_key(uint8_t *k, uint8_t kk)
948 {
949 uint8_t nn = 64U;
950 Hacl_Hash_Blake2b_index i = { .key_length = kk, .digest_length = nn, .last_node = false };
951 uint8_t salt[16U] = { 0U };
952 uint8_t personal[16U] = { 0U };
953 Hacl_Hash_Blake2b_blake2_params
954 p =
955 {
956 .digest_length = i.digest_length, .key_length = i.key_length, .fanout = 1U, .depth = 1U,
957 .leaf_length = 0U, .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt,
958 .personal = personal
959 };
960 Hacl_Hash_Blake2b_blake2_params p0 = p;
961 Hacl_Hash_Blake2b_state_t *s = Hacl_Hash_Blake2b_malloc_with_params_and_key(&p0, false, k);
962 return s;
963 }
964
965 /**
966 Specialized allocation function that picks default values for all
967 parameters, and has no key. Effectively, this is what you want if you intend to
968 use Blake2 as a hash function. Further resettings of the state SHALL be done with `reset`.
969 */
970 Hacl_Hash_Blake2b_state_t *Hacl_Hash_Blake2b_malloc(void)
971 {
972 return Hacl_Hash_Blake2b_malloc_with_key(NULL, 0U);
973 }
974
975 static Hacl_Hash_Blake2b_index index_of_state(Hacl_Hash_Blake2b_state_t *s)
976 {
977 Hacl_Hash_Blake2b_block_state_t block_state = (*s).block_state;
978 bool last_node = block_state.thd;
979 uint8_t nn = block_state.snd;
980 uint8_t kk1 = block_state.fst;
981 return
982 ((Hacl_Hash_Blake2b_index){ .key_length = kk1, .digest_length = nn, .last_node = last_node });
983 }
984
985 static void reset_raw(Hacl_Hash_Blake2b_state_t *state, Hacl_Hash_Blake2b_params_and_key key)
986 {
987 Hacl_Hash_Blake2b_state_t scrut = *state;
988 uint8_t *buf = scrut.buf;
989 Hacl_Hash_Blake2b_block_state_t block_state = scrut.block_state;
990 bool last_node0 = block_state.thd;
991 uint8_t nn0 = block_state.snd;
992 uint8_t kk10 = block_state.fst;
993 Hacl_Hash_Blake2b_index
994 i = { .key_length = kk10, .digest_length = nn0, .last_node = last_node0 };
995 KRML_MAYBE_UNUSED_VAR(i);
996 Hacl_Hash_Blake2b_blake2_params *p = key.fst;
997 uint8_t kk1 = p->key_length;
998 uint8_t nn = p->digest_length;
999 bool last_node = block_state.thd;
1000 Hacl_Hash_Blake2b_index
1001 i1 = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
1002 uint64_t *h = block_state.f3.snd;
1003 uint32_t kk20 = (uint32_t)i1.key_length;
1004 uint8_t *k_1 = key.snd;
1005 if (!(kk20 == 0U))
1006 {
1007 uint8_t *sub_b = buf + kk20;
1008 memset(sub_b, 0U, (128U - kk20) * sizeof (uint8_t));
1009 memcpy(buf, k_1, kk20 * sizeof (uint8_t));
1010 }
1011 Hacl_Hash_Blake2b_blake2_params pv = p[0U];
1012 uint64_t tmp[8U] = { 0U };
1013 uint64_t *r0 = h;
1014 uint64_t *r1 = h + 4U;
1015 uint64_t *r2 = h + 8U;
1016 uint64_t *r3 = h + 12U;
1017 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
1018 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
1019 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
1020 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
1021 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
1022 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
1023 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
1024 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
1025 r2[0U] = iv0;
1026 r2[1U] = iv1;
1027 r2[2U] = iv2;
1028 r2[3U] = iv3;
1029 r3[0U] = iv4;
1030 r3[1U] = iv5;
1031 r3[2U] = iv6;
1032 r3[3U] = iv7;
1033 uint8_t kk2 = pv.key_length;
1034 uint8_t nn1 = pv.digest_length;
1035 KRML_MAYBE_FOR2(i0,
1036 0U,
1037 2U,
1038 1U,
1039 uint64_t *os = tmp + 4U;
1040 uint8_t *bj = pv.salt + i0 * 8U;
1041 uint64_t u = load64_le(bj);
1042 uint64_t r = u;
1043 uint64_t x = r;
1044 os[i0] = x;);
1045 KRML_MAYBE_FOR2(i0,
1046 0U,
1047 2U,
1048 1U,
1049 uint64_t *os = tmp + 6U;
1050 uint8_t *bj = pv.personal + i0 * 8U;
1051 uint64_t u = load64_le(bj);
1052 uint64_t r = u;
1053 uint64_t x = r;
1054 os[i0] = x;);
1055 tmp[0U] =
1056 (uint64_t)nn1 ^
1057 ((uint64_t)kk2 << 8U ^
1058 ((uint64_t)pv.fanout << 16U ^ ((uint64_t)pv.depth << 24U ^ (uint64_t)pv.leaf_length << 32U)));
1059 tmp[1U] = pv.node_offset;
1060 tmp[2U] = (uint64_t)pv.node_depth ^ (uint64_t)pv.inner_length << 8U;
1061 tmp[3U] = 0ULL;
1062 uint64_t tmp0 = tmp[0U];
1063 uint64_t tmp1 = tmp[1U];
1064 uint64_t tmp2 = tmp[2U];
1065 uint64_t tmp3 = tmp[3U];
1066 uint64_t tmp4 = tmp[4U];
1067 uint64_t tmp5 = tmp[5U];
1068 uint64_t tmp6 = tmp[6U];
1069 uint64_t tmp7 = tmp[7U];
1070 uint64_t iv0_ = iv0 ^ tmp0;
1071 uint64_t iv1_ = iv1 ^ tmp1;
1072 uint64_t iv2_ = iv2 ^ tmp2;
1073 uint64_t iv3_ = iv3 ^ tmp3;
1074 uint64_t iv4_ = iv4 ^ tmp4;
1075 uint64_t iv5_ = iv5 ^ tmp5;
1076 uint64_t iv6_ = iv6 ^ tmp6;
1077 uint64_t iv7_ = iv7 ^ tmp7;
1078 r0[0U] = iv0_;
1079 r0[1U] = iv1_;
1080 r0[2U] = iv2_;
1081 r0[3U] = iv3_;
1082 r1[0U] = iv4_;
1083 r1[1U] = iv5_;
1084 r1[2U] = iv6_;
1085 r1[3U] = iv7_;
1086 uint8_t kk11 = i.key_length;
1087 uint32_t ite;
1088 if (kk11 != 0U)
1089 {
1090 ite = 128U;
1091 }
1092 else
1093 {
1094 ite = 0U;
1095 }
1096 Hacl_Hash_Blake2b_state_t
1097 tmp8 = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)ite };
1098 state[0U] = tmp8;
1099 }
1100
1101 /**
1102 General-purpose re-initialization function with parameters and
1103 key. You cannot change digest_length, key_length, or last_node, meaning those values in
1104 the parameters object must be the same as originally decided via one of the
1105 malloc functions. All other values of the parameter can be changed. The behavior
1106 is unspecified if you violate this precondition.
1107 */
1108 void
1109 Hacl_Hash_Blake2b_reset_with_key_and_params(
1110 Hacl_Hash_Blake2b_state_t *s,
1111 Hacl_Hash_Blake2b_blake2_params *p,
1112 uint8_t *k
1113 )
1114 {
1115 Hacl_Hash_Blake2b_index i1 = index_of_state(s);
1116 KRML_MAYBE_UNUSED_VAR(i1);
1117 reset_raw(s, ((Hacl_Hash_Blake2b_params_and_key){ .fst = p, .snd = k }));
1118 }
1119
1120 /**
1121 Specialized-purpose re-initialization function with no parameters,
1122 and a key. The key length must be the same as originally decided via your choice
1123 of malloc function. All other parameters are reset to their default values. The
1124 original call to malloc MUST have set digest_length to the default value. The
1125 behavior is unspecified if you violate this precondition.
1126 */
1127 void Hacl_Hash_Blake2b_reset_with_key(Hacl_Hash_Blake2b_state_t *s, uint8_t *k)
1128 {
1129 Hacl_Hash_Blake2b_index idx = index_of_state(s);
1130 uint8_t salt[16U] = { 0U };
1131 uint8_t personal[16U] = { 0U };
1132 Hacl_Hash_Blake2b_blake2_params
1133 p =
1134 {
1135 .digest_length = idx.digest_length, .key_length = idx.key_length, .fanout = 1U, .depth = 1U,
1136 .leaf_length = 0U, .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt,
1137 .personal = personal
1138 };
1139 Hacl_Hash_Blake2b_blake2_params p0 = p;
1140 reset_raw(s, ((Hacl_Hash_Blake2b_params_and_key){ .fst = &p0, .snd = k }));
1141 }
1142
1143 /**
1144 Specialized-purpose re-initialization function with no parameters
1145 and no key. This is what you want if you intend to use Blake2 as a hash
1146 function. The key length and digest length must have been set to their
1147 respective default values via your choice of malloc function (always true if you
1148 used `malloc`). All other parameters are reset to their default values. The
1149 behavior is unspecified if you violate this precondition.
1150 */
1151 void Hacl_Hash_Blake2b_reset(Hacl_Hash_Blake2b_state_t *s)
1152 {
1153 Hacl_Hash_Blake2b_reset_with_key(s, NULL);
1154 }
1155
1156 /**
1157 Update function; 0 = success, 1 = max length exceeded
1158 */
1159 Hacl_Streaming_Types_error_code
1160 Hacl_Hash_Blake2b_update(Hacl_Hash_Blake2b_state_t *state, uint8_t *chunk, uint32_t chunk_len)
1161 {
1162 Hacl_Hash_Blake2b_state_t s = *state;
1163 uint64_t total_len = s.total_len;
1164 if ((uint64_t)chunk_len > 0xffffffffffffffffULL - total_len)
1165 {
1166 return Hacl_Streaming_Types_MaximumLengthExceeded;
1167 }
1168 uint32_t sz;
1169 if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
1170 {
1171 sz = 128U;
1172 }
1173 else
1174 {
1175 sz = (uint32_t)(total_len % (uint64_t)128U);
1176 }
1177 if (chunk_len <= 128U - sz)
1178 {
1179 Hacl_Hash_Blake2b_state_t s1 = *state;
1180 Hacl_Hash_Blake2b_block_state_t block_state1 = s1.block_state;
1181 uint8_t *buf = s1.buf;
1182 uint64_t total_len1 = s1.total_len;
1183 uint32_t sz1;
1184 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1185 {
1186 sz1 = 128U;
1187 }
1188 else
1189 {
1190 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1191 }
1192 uint8_t *buf2 = buf + sz1;
1193 memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
1194 uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
1195 *state =
1196 (
1197 (Hacl_Hash_Blake2b_state_t){
1198 .block_state = block_state1,
1199 .buf = buf,
1200 .total_len = total_len2
1201 }
1202 );
1203 }
1204 else if (sz == 0U)
1205 {
1206 Hacl_Hash_Blake2b_state_t s1 = *state;
1207 Hacl_Hash_Blake2b_block_state_t block_state1 = s1.block_state;
1208 uint8_t *buf = s1.buf;
1209 uint64_t total_len1 = s1.total_len;
1210 uint32_t sz1;
1211 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1212 {
1213 sz1 = 128U;
1214 }
1215 else
1216 {
1217 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1218 }
1219 if (!(sz1 == 0U))
1220 {
1221 uint64_t prevlen = total_len1 - (uint64_t)sz1;
1222 Hacl_Streaming_Types_two_pointers acc = block_state1.f3;
1223 uint64_t *wv = acc.fst;
1224 uint64_t *hash = acc.snd;
1225 uint32_t nb = 1U;
1226 Hacl_Hash_Blake2b_update_multi(128U,
1227 wv,
1228 hash,
1229 FStar_UInt128_uint64_to_uint128(prevlen),
1230 buf,
1231 nb);
1232 }
1233 uint32_t ite;
1234 if ((uint64_t)chunk_len % (uint64_t)128U == 0ULL && (uint64_t)chunk_len > 0ULL)
1235 {
1236 ite = 128U;
1237 }
1238 else
1239 {
1240 ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)128U);
1241 }
1242 uint32_t n_blocks = (chunk_len - ite) / 128U;
1243 uint32_t data1_len = n_blocks * 128U;
1244 uint32_t data2_len = chunk_len - data1_len;
1245 uint8_t *data1 = chunk;
1246 uint8_t *data2 = chunk + data1_len;
1247 Hacl_Streaming_Types_two_pointers acc = block_state1.f3;
1248 uint64_t *wv = acc.fst;
1249 uint64_t *hash = acc.snd;
1250 uint32_t nb = data1_len / 128U;
1251 Hacl_Hash_Blake2b_update_multi(data1_len,
1252 wv,
1253 hash,
1254 FStar_UInt128_uint64_to_uint128(total_len1),
1255 data1,
1256 nb);
1257 uint8_t *dst = buf;
1258 memcpy(dst, data2, data2_len * sizeof (uint8_t));
1259 *state =
1260 (
1261 (Hacl_Hash_Blake2b_state_t){
1262 .block_state = block_state1,
1263 .buf = buf,
1264 .total_len = total_len1 + (uint64_t)chunk_len
1265 }
1266 );
1267 }
1268 else
1269 {
1270 uint32_t diff = 128U - sz;
1271 uint8_t *chunk1 = chunk;
1272 uint8_t *chunk2 = chunk + diff;
1273 Hacl_Hash_Blake2b_state_t s1 = *state;
1274 Hacl_Hash_Blake2b_block_state_t block_state10 = s1.block_state;
1275 uint8_t *buf0 = s1.buf;
1276 uint64_t total_len10 = s1.total_len;
1277 uint32_t sz10;
1278 if (total_len10 % (uint64_t)128U == 0ULL && total_len10 > 0ULL)
1279 {
1280 sz10 = 128U;
1281 }
1282 else
1283 {
1284 sz10 = (uint32_t)(total_len10 % (uint64_t)128U);
1285 }
1286 uint8_t *buf2 = buf0 + sz10;
1287 memcpy(buf2, chunk1, diff * sizeof (uint8_t));
1288 uint64_t total_len2 = total_len10 + (uint64_t)diff;
1289 *state =
1290 (
1291 (Hacl_Hash_Blake2b_state_t){
1292 .block_state = block_state10,
1293 .buf = buf0,
1294 .total_len = total_len2
1295 }
1296 );
1297 Hacl_Hash_Blake2b_state_t s10 = *state;
1298 Hacl_Hash_Blake2b_block_state_t block_state1 = s10.block_state;
1299 uint8_t *buf = s10.buf;
1300 uint64_t total_len1 = s10.total_len;
1301 uint32_t sz1;
1302 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1303 {
1304 sz1 = 128U;
1305 }
1306 else
1307 {
1308 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1309 }
1310 if (!(sz1 == 0U))
1311 {
1312 uint64_t prevlen = total_len1 - (uint64_t)sz1;
1313 Hacl_Streaming_Types_two_pointers acc = block_state1.f3;
1314 uint64_t *wv = acc.fst;
1315 uint64_t *hash = acc.snd;
1316 uint32_t nb = 1U;
1317 Hacl_Hash_Blake2b_update_multi(128U,
1318 wv,
1319 hash,
1320 FStar_UInt128_uint64_to_uint128(prevlen),
1321 buf,
1322 nb);
1323 }
1324 uint32_t ite;
1325 if
1326 ((uint64_t)(chunk_len - diff) % (uint64_t)128U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
1327 {
1328 ite = 128U;
1329 }
1330 else
1331 {
1332 ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)128U);
1333 }
1334 uint32_t n_blocks = (chunk_len - diff - ite) / 128U;
1335 uint32_t data1_len = n_blocks * 128U;
1336 uint32_t data2_len = chunk_len - diff - data1_len;
1337 uint8_t *data1 = chunk2;
1338 uint8_t *data2 = chunk2 + data1_len;
1339 Hacl_Streaming_Types_two_pointers acc = block_state1.f3;
1340 uint64_t *wv = acc.fst;
1341 uint64_t *hash = acc.snd;
1342 uint32_t nb = data1_len / 128U;
1343 Hacl_Hash_Blake2b_update_multi(data1_len,
1344 wv,
1345 hash,
1346 FStar_UInt128_uint64_to_uint128(total_len1),
1347 data1,
1348 nb);
1349 uint8_t *dst = buf;
1350 memcpy(dst, data2, data2_len * sizeof (uint8_t));
1351 *state =
1352 (
1353 (Hacl_Hash_Blake2b_state_t){
1354 .block_state = block_state1,
1355 .buf = buf,
1356 .total_len = total_len1 + (uint64_t)(chunk_len - diff)
1357 }
1358 );
1359 }
1360 return Hacl_Streaming_Types_Success;
1361 }
1362
1363 /**
1364 Digest function. This function expects the `output` array to hold
1365 at least `digest_length` bytes, where `digest_length` was determined by your
1366 choice of `malloc` function. Concretely, if you used `malloc` or
1367 `malloc_with_key`, then the expected length is 32 for S, or 64 for B (default
1368 digest length). If you used `malloc_with_params_and_key`, then the expected
1369 length is whatever you chose for the `digest_length` field of your parameters.
1370 For convenience, this function returns `digest_length`. When in doubt, callers
1371 can pass an array of size HACL_BLAKE2B_32_OUT_BYTES, then use the return value
1372 to see how many bytes were actually written.
1373 */
1374 uint8_t Hacl_Hash_Blake2b_digest(Hacl_Hash_Blake2b_state_t *s, uint8_t *dst)
1375 {
1376 Hacl_Hash_Blake2b_block_state_t block_state0 = (*s).block_state;
1377 bool last_node0 = block_state0.thd;
1378 uint8_t nn0 = block_state0.snd;
1379 uint8_t kk0 = block_state0.fst;
1380 Hacl_Hash_Blake2b_index
1381 i1 = { .key_length = kk0, .digest_length = nn0, .last_node = last_node0 };
1382 Hacl_Hash_Blake2b_state_t scrut = *s;
1383 Hacl_Hash_Blake2b_block_state_t block_state = scrut.block_state;
1384 uint8_t *buf_ = scrut.buf;
1385 uint64_t total_len = scrut.total_len;
1386 uint32_t r;
1387 if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
1388 {
1389 r = 128U;
1390 }
1391 else
1392 {
1393 r = (uint32_t)(total_len % (uint64_t)128U);
1394 }
1395 uint8_t *buf_1 = buf_;
1396 uint64_t wv0[16U] = { 0U };
1397 uint64_t b[16U] = { 0U };
1398 Hacl_Hash_Blake2b_block_state_t
1399 tmp_block_state =
1400 {
1401 .fst = i1.key_length,
1402 .snd = i1.digest_length,
1403 .thd = i1.last_node,
1404 .f3 = { .fst = wv0, .snd = b }
1405 };
1406 uint64_t *src_b = block_state.f3.snd;
1407 uint64_t *dst_b = tmp_block_state.f3.snd;
1408 memcpy(dst_b, src_b, 16U * sizeof (uint64_t));
1409 uint64_t prev_len = total_len - (uint64_t)r;
1410 uint32_t ite;
1411 if (r % 128U == 0U && r > 0U)
1412 {
1413 ite = 128U;
1414 }
1415 else
1416 {
1417 ite = r % 128U;
1418 }
1419 uint8_t *buf_last = buf_1 + r - ite;
1420 uint8_t *buf_multi = buf_1;
1421 Hacl_Streaming_Types_two_pointers acc0 = tmp_block_state.f3;
1422 uint64_t *wv1 = acc0.fst;
1423 uint64_t *hash0 = acc0.snd;
1424 uint32_t nb = 0U;
1425 Hacl_Hash_Blake2b_update_multi(0U,
1426 wv1,
1427 hash0,
1428 FStar_UInt128_uint64_to_uint128(prev_len),
1429 buf_multi,
1430 nb);
1431 uint64_t prev_len_last = total_len - (uint64_t)r;
1432 Hacl_Streaming_Types_two_pointers acc = tmp_block_state.f3;
1433 bool last_node1 = tmp_block_state.thd;
1434 uint64_t *wv = acc.fst;
1435 uint64_t *hash = acc.snd;
1436 Hacl_Hash_Blake2b_update_last(r,
1437 wv,
1438 hash,
1439 last_node1,
1440 FStar_UInt128_uint64_to_uint128(prev_len_last),
1441 r,
1442 buf_last);
1443 uint8_t nn1 = tmp_block_state.snd;
1444 Hacl_Hash_Blake2b_finish((uint32_t)nn1, dst, tmp_block_state.f3.snd);
1445 Hacl_Hash_Blake2b_block_state_t block_state1 = (*s).block_state;
1446 bool last_node = block_state1.thd;
1447 uint8_t nn = block_state1.snd;
1448 uint8_t kk = block_state1.fst;
1449 return
1450 ((Hacl_Hash_Blake2b_index){ .key_length = kk, .digest_length = nn, .last_node = last_node }).digest_length;
1451 }
1452
1453 Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_info(Hacl_Hash_Blake2b_state_t *s)
1454 {
1455 Hacl_Hash_Blake2b_block_state_t block_state = (*s).block_state;
1456 bool last_node = block_state.thd;
1457 uint8_t nn = block_state.snd;
1458 uint8_t kk = block_state.fst;
1459 return
1460 ((Hacl_Hash_Blake2b_index){ .key_length = kk, .digest_length = nn, .last_node = last_node });
1461 }
1462
1463 /**
1464 Free state function when there is no key
1465 */
1466 void Hacl_Hash_Blake2b_free(Hacl_Hash_Blake2b_state_t *state)
1467 {
1468 Hacl_Hash_Blake2b_state_t scrut = *state;
1469 uint8_t *buf = scrut.buf;
1470 Hacl_Hash_Blake2b_block_state_t block_state = scrut.block_state;
1471 uint64_t *b = block_state.f3.snd;
1472 uint64_t *wv = block_state.f3.fst;
1473 KRML_HOST_FREE(wv);
1474 KRML_HOST_FREE(b);
1475 KRML_HOST_FREE(buf);
1476 KRML_HOST_FREE(state);
1477 }
1478
1479 /**
1480 Copying. This preserves all parameters.
1481 */
1482 Hacl_Hash_Blake2b_state_t *Hacl_Hash_Blake2b_copy(Hacl_Hash_Blake2b_state_t *state)
1483 {
1484 Hacl_Hash_Blake2b_state_t scrut = *state;
1485 Hacl_Hash_Blake2b_block_state_t block_state0 = scrut.block_state;
1486 uint8_t *buf0 = scrut.buf;
1487 uint64_t total_len0 = scrut.total_len;
1488 bool last_node = block_state0.thd;
1489 uint8_t nn = block_state0.snd;
1490 uint8_t kk1 = block_state0.fst;
1491 Hacl_Hash_Blake2b_index i = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
1492 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
1493 if (buf == NULL)
1494 {
1495 return NULL;
1496 }
1497 memcpy(buf, buf0, 128U * sizeof (uint8_t));
1498 uint64_t *wv0 = (uint64_t *)KRML_HOST_CALLOC(16U, sizeof (uint64_t));
1499 option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___ block_state;
1500 if (wv0 == NULL)
1501 {
1502 block_state =
1503 (
1504 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
1505 .tag = Hacl_Streaming_Types_None
1506 }
1507 );
1508 }
1509 else
1510 {
1511 uint64_t *b = (uint64_t *)KRML_HOST_CALLOC(16U, sizeof (uint64_t));
1512 if (b == NULL)
1513 {
1514 KRML_HOST_FREE(wv0);
1515 block_state =
1516 (
1517 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
1518 .tag = Hacl_Streaming_Types_None
1519 }
1520 );
1521 }
1522 else
1523 {
1524 block_state =
1525 (
1526 (option___uint8_t___uint8_t___bool_____uint64_t_____uint64_t___){
1527 .tag = Hacl_Streaming_Types_Some,
1528 .v = {
1529 .fst = i.key_length,
1530 .snd = i.digest_length,
1531 .thd = i.last_node,
1532 .f3 = { .fst = wv0, .snd = b }
1533 }
1534 }
1535 );
1536 }
1537 }
1538 if (block_state.tag == Hacl_Streaming_Types_None)
1539 {
1540 KRML_HOST_FREE(buf);
1541 return NULL;
1542 }
1543 if (block_state.tag == Hacl_Streaming_Types_Some)
1544 {
1545 Hacl_Hash_Blake2b_block_state_t block_state1 = block_state.v;
1546 uint64_t *src_b = block_state0.f3.snd;
1547 uint64_t *dst_b = block_state1.f3.snd;
1548 memcpy(dst_b, src_b, 16U * sizeof (uint64_t));
1549 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
1550 switch (k_)
1551 {
1552 case Hacl_Streaming_Types_None:
1553 {
1554 return NULL;
1555 }
1556 case Hacl_Streaming_Types_Some:
1557 {
1558 Hacl_Hash_Blake2b_state_t
1559 s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 };
1560 Hacl_Hash_Blake2b_state_t
1561 *p = (Hacl_Hash_Blake2b_state_t *)KRML_HOST_MALLOC(sizeof (Hacl_Hash_Blake2b_state_t));
1562 if (p != NULL)
1563 {
1564 p[0U] = s;
1565 }
1566 if (p == NULL)
1567 {
1568 uint64_t *b = block_state1.f3.snd;
1569 uint64_t *wv = block_state1.f3.fst;
1570 KRML_HOST_FREE(wv);
1571 KRML_HOST_FREE(b);
1572 KRML_HOST_FREE(buf);
1573 return NULL;
1574 }
1575 return p;
1576 }
1577 default:
1578 {
1579 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1580 KRML_HOST_EXIT(253U);
1581 }
1582 }
1583 }
1584 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1585 __FILE__,
1586 __LINE__,
1587 "unreachable (pattern matches are exhaustive in F*)");
1588 KRML_HOST_EXIT(255U);
1589 }
1590
1591 /**
1592 Write the BLAKE2b digest of message `input` using key `key` into `output`.
1593
1594 @param output Pointer to `output_len` bytes of memory where the digest is written to.
1595 @param output_len Length of the to-be-generated digest with 1 <= `output_len` <= 64.
1596 @param input Pointer to `input_len` bytes of memory where the input message is read from.
1597 @param input_len Length of the input message.
1598 @param key Pointer to `key_len` bytes of memory where the key is read from.
1599 @param key_len Length of the key. Can be 0.
1600 */
1601 void
1602 Hacl_Hash_Blake2b_hash_with_key(
1603 uint8_t *output,
1604 uint32_t output_len,
1605 uint8_t *input,
1606 uint32_t input_len,
1607 uint8_t *key,
1608 uint32_t key_len
1609 )
1610 {
1611 uint64_t b[16U] = { 0U };
1612 uint64_t b1[16U] = { 0U };
1613 Hacl_Hash_Blake2b_init(b, key_len, output_len);
1614 update(b1, b, key_len, key, input_len, input);
1615 Hacl_Hash_Blake2b_finish(output_len, output, b);
1616 Lib_Memzero0_memzero(b1, 16U, uint64_t, void *);
1617 Lib_Memzero0_memzero(b, 16U, uint64_t, void *);
1618 }
1619
1620 /**
1621 Write the BLAKE2b digest of message `input` using key `key` and
1622 parameters `params` into `output`. The `key` array must be of length
1623 `params.key_length`. The `output` array must be of length
1624 `params.digest_length`.
1625 */
1626 void
1627 Hacl_Hash_Blake2b_hash_with_key_and_params(
1628 uint8_t *output,
1629 uint8_t *input,
1630 uint32_t input_len,
1631 Hacl_Hash_Blake2b_blake2_params params,
1632 uint8_t *key
1633 )
1634 {
1635 uint64_t b[16U] = { 0U };
1636 uint64_t b1[16U] = { 0U };
1637 uint64_t tmp[8U] = { 0U };
1638 uint64_t *r0 = b;
1639 uint64_t *r1 = b + 4U;
1640 uint64_t *r2 = b + 8U;
1641 uint64_t *r3 = b + 12U;
1642 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
1643 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
1644 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
1645 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
1646 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
1647 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
1648 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
1649 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
1650 r2[0U] = iv0;
1651 r2[1U] = iv1;
1652 r2[2U] = iv2;
1653 r2[3U] = iv3;
1654 r3[0U] = iv4;
1655 r3[1U] = iv5;
1656 r3[2U] = iv6;
1657 r3[3U] = iv7;
1658 uint8_t kk = params.key_length;
1659 uint8_t nn = params.digest_length;
1660 KRML_MAYBE_FOR2(i,
1661 0U,
1662 2U,
1663 1U,
1664 uint64_t *os = tmp + 4U;
1665 uint8_t *bj = params.salt + i * 8U;
1666 uint64_t u = load64_le(bj);
1667 uint64_t r = u;
1668 uint64_t x = r;
1669 os[i] = x;);
1670 KRML_MAYBE_FOR2(i,
1671 0U,
1672 2U,
1673 1U,
1674 uint64_t *os = tmp + 6U;
1675 uint8_t *bj = params.personal + i * 8U;
1676 uint64_t u = load64_le(bj);
1677 uint64_t r = u;
1678 uint64_t x = r;
1679 os[i] = x;);
1680 tmp[0U] =
1681 (uint64_t)nn ^
1682 ((uint64_t)kk << 8U ^
1683 ((uint64_t)params.fanout << 16U ^
1684 ((uint64_t)params.depth << 24U ^ (uint64_t)params.leaf_length << 32U)));
1685 tmp[1U] = params.node_offset;
1686 tmp[2U] = (uint64_t)params.node_depth ^ (uint64_t)params.inner_length << 8U;
1687 tmp[3U] = 0ULL;
1688 uint64_t tmp0 = tmp[0U];
1689 uint64_t tmp1 = tmp[1U];
1690 uint64_t tmp2 = tmp[2U];
1691 uint64_t tmp3 = tmp[3U];
1692 uint64_t tmp4 = tmp[4U];
1693 uint64_t tmp5 = tmp[5U];
1694 uint64_t tmp6 = tmp[6U];
1695 uint64_t tmp7 = tmp[7U];
1696 uint64_t iv0_ = iv0 ^ tmp0;
1697 uint64_t iv1_ = iv1 ^ tmp1;
1698 uint64_t iv2_ = iv2 ^ tmp2;
1699 uint64_t iv3_ = iv3 ^ tmp3;
1700 uint64_t iv4_ = iv4 ^ tmp4;
1701 uint64_t iv5_ = iv5 ^ tmp5;
1702 uint64_t iv6_ = iv6 ^ tmp6;
1703 uint64_t iv7_ = iv7 ^ tmp7;
1704 r0[0U] = iv0_;
1705 r0[1U] = iv1_;
1706 r0[2U] = iv2_;
1707 r0[3U] = iv3_;
1708 r1[0U] = iv4_;
1709 r1[1U] = iv5_;
1710 r1[2U] = iv6_;
1711 r1[3U] = iv7_;
1712 update(b1, b, (uint32_t)params.key_length, key, input_len, input);
1713 Hacl_Hash_Blake2b_finish((uint32_t)params.digest_length, output, b);
1714 Lib_Memzero0_memzero(b1, 16U, uint64_t, void *);
1715 Lib_Memzero0_memzero(b, 16U, uint64_t, void *);
1716 }
1717