Debian Codegraph @

Modules/_hacl/Hacl_Streaming_HMAC.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_Streaming_HMAC.h"
27
28 #include "Hacl_Streaming_Types.h"
29
30 #include "Hacl_Hash_SHA3.h"
31 #include "Hacl_Hash_SHA2.h"
32 #include "Hacl_Hash_Blake2s_Simd128.h"
33 #include "Hacl_Hash_Blake2s.h"
34 #include "Hacl_Hash_Blake2b_Simd256.h"
35 #include "Hacl_Hash_Blake2b.h"
36 #include "internal/Hacl_Streaming_Types.h"
37
38 #include "internal/Hacl_Hash_SHA3.h"
39 #include "internal/Hacl_Hash_SHA2.h"
40 #include "internal/Hacl_Hash_SHA1.h"
41 #include "internal/Hacl_Hash_MD5.h"
42 #include "internal/Hacl_Hash_Blake2s_Simd128.h"
43 #include "internal/Hacl_Hash_Blake2s.h"
44 #include "internal/Hacl_Hash_Blake2b_Simd256.h"
45 #include "internal/Hacl_Hash_Blake2b.h"
46
47 static Spec_Hash_Definitions_hash_alg alg_of_impl(Hacl_Agile_Hash_impl i)
48 {
49 switch (i)
50 {
51 case Hacl_Agile_Hash_MD5:
52 {
53 return Spec_Hash_Definitions_MD5;
54 }
55 case Hacl_Agile_Hash_SHA1:
56 {
57 return Spec_Hash_Definitions_SHA1;
58 }
59 case Hacl_Agile_Hash_SHA2_224:
60 {
61 return Spec_Hash_Definitions_SHA2_224;
62 }
63 case Hacl_Agile_Hash_SHA2_256:
64 {
65 return Spec_Hash_Definitions_SHA2_256;
66 }
67 case Hacl_Agile_Hash_SHA2_384:
68 {
69 return Spec_Hash_Definitions_SHA2_384;
70 }
71 case Hacl_Agile_Hash_SHA2_512:
72 {
73 return Spec_Hash_Definitions_SHA2_512;
74 }
75 case Hacl_Agile_Hash_SHA3_224:
76 {
77 return Spec_Hash_Definitions_SHA3_224;
78 }
79 case Hacl_Agile_Hash_SHA3_256:
80 {
81 return Spec_Hash_Definitions_SHA3_256;
82 }
83 case Hacl_Agile_Hash_SHA3_384:
84 {
85 return Spec_Hash_Definitions_SHA3_384;
86 }
87 case Hacl_Agile_Hash_SHA3_512:
88 {
89 return Spec_Hash_Definitions_SHA3_512;
90 }
91 case Hacl_Agile_Hash_Blake2S_32:
92 {
93 return Spec_Hash_Definitions_Blake2S;
94 }
95 case Hacl_Agile_Hash_Blake2S_128:
96 {
97 return Spec_Hash_Definitions_Blake2S;
98 }
99 case Hacl_Agile_Hash_Blake2B_32:
100 {
101 return Spec_Hash_Definitions_Blake2B;
102 }
103 case Hacl_Agile_Hash_Blake2B_256:
104 {
105 return Spec_Hash_Definitions_Blake2B;
106 }
107 default:
108 {
109 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
110 KRML_HOST_EXIT(253U);
111 }
112 }
113 }
114
115 static Hacl_Agile_Hash_impl impl_of_state_s(Hacl_Agile_Hash_state_s s)
116 {
117 if (s.tag == Hacl_Agile_Hash_MD5_a)
118 {
119 return Hacl_Agile_Hash_MD5;
120 }
121 if (s.tag == Hacl_Agile_Hash_SHA1_a)
122 {
123 return Hacl_Agile_Hash_SHA1;
124 }
125 if (s.tag == Hacl_Agile_Hash_SHA2_224_a)
126 {
127 return Hacl_Agile_Hash_SHA2_224;
128 }
129 if (s.tag == Hacl_Agile_Hash_SHA2_256_a)
130 {
131 return Hacl_Agile_Hash_SHA2_256;
132 }
133 if (s.tag == Hacl_Agile_Hash_SHA2_384_a)
134 {
135 return Hacl_Agile_Hash_SHA2_384;
136 }
137 if (s.tag == Hacl_Agile_Hash_SHA2_512_a)
138 {
139 return Hacl_Agile_Hash_SHA2_512;
140 }
141 if (s.tag == Hacl_Agile_Hash_SHA3_224_a)
142 {
143 return Hacl_Agile_Hash_SHA3_224;
144 }
145 if (s.tag == Hacl_Agile_Hash_SHA3_256_a)
146 {
147 return Hacl_Agile_Hash_SHA3_256;
148 }
149 if (s.tag == Hacl_Agile_Hash_SHA3_384_a)
150 {
151 return Hacl_Agile_Hash_SHA3_384;
152 }
153 if (s.tag == Hacl_Agile_Hash_SHA3_512_a)
154 {
155 return Hacl_Agile_Hash_SHA3_512;
156 }
157 if (s.tag == Hacl_Agile_Hash_Blake2S_a)
158 {
159 return Hacl_Agile_Hash_Blake2S_32;
160 }
161 if (s.tag == Hacl_Agile_Hash_Blake2S_128_a)
162 {
163 return Hacl_Agile_Hash_Blake2S_128;
164 }
165 if (s.tag == Hacl_Agile_Hash_Blake2B_a)
166 {
167 return Hacl_Agile_Hash_Blake2B_32;
168 }
169 if (s.tag == Hacl_Agile_Hash_Blake2B_256_a)
170 {
171 return Hacl_Agile_Hash_Blake2B_256;
172 }
173 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
174 __FILE__,
175 __LINE__,
176 "unreachable (pattern matches are exhaustive in F*)");
177 KRML_HOST_EXIT(255U);
178 }
179
180 static Hacl_Agile_Hash_impl impl_of_state(Hacl_Agile_Hash_state_s *s)
181 {
182 return impl_of_state_s(*s);
183 }
184
185 static Hacl_Agile_Hash_state_s *malloc_(Hacl_Agile_Hash_impl a)
186 {
187 switch (a)
188 {
189 case Hacl_Agile_Hash_MD5:
190 {
191 uint32_t *s = (uint32_t *)KRML_HOST_CALLOC(4U, sizeof (uint32_t));
192 if (s == NULL)
193 {
194 return NULL;
195 }
196 uint32_t *s1 = s;
197 Hacl_Agile_Hash_state_s
198 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
199 if (st != NULL)
200 {
201 st[0U] = ((Hacl_Agile_Hash_state_s){ .tag = Hacl_Agile_Hash_MD5_a, { .case_MD5_a = s1 } });
202 }
203 if (st == NULL)
204 {
205 KRML_HOST_FREE(s1);
206 return NULL;
207 }
208 return st;
209 }
210 case Hacl_Agile_Hash_SHA1:
211 {
212 uint32_t *s = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
213 if (s == NULL)
214 {
215 return NULL;
216 }
217 uint32_t *s1 = s;
218 Hacl_Agile_Hash_state_s
219 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
220 if (st != NULL)
221 {
222 st[0U] =
223 ((Hacl_Agile_Hash_state_s){ .tag = Hacl_Agile_Hash_SHA1_a, { .case_SHA1_a = s1 } });
224 }
225 if (st == NULL)
226 {
227 KRML_HOST_FREE(s1);
228 return NULL;
229 }
230 return st;
231 }
232 case Hacl_Agile_Hash_SHA2_224:
233 {
234 uint32_t *s = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t));
235 if (s == NULL)
236 {
237 return NULL;
238 }
239 uint32_t *s1 = s;
240 Hacl_Agile_Hash_state_s
241 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
242 if (st != NULL)
243 {
244 st[0U] =
245 (
246 (Hacl_Agile_Hash_state_s){
247 .tag = Hacl_Agile_Hash_SHA2_224_a,
248 { .case_SHA2_224_a = s1 }
249 }
250 );
251 }
252 if (st == NULL)
253 {
254 KRML_HOST_FREE(s1);
255 return NULL;
256 }
257 return st;
258 }
259 case Hacl_Agile_Hash_SHA2_256:
260 {
261 uint32_t *s = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t));
262 if (s == NULL)
263 {
264 return NULL;
265 }
266 uint32_t *s1 = s;
267 Hacl_Agile_Hash_state_s
268 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
269 if (st != NULL)
270 {
271 st[0U] =
272 (
273 (Hacl_Agile_Hash_state_s){
274 .tag = Hacl_Agile_Hash_SHA2_256_a,
275 { .case_SHA2_256_a = s1 }
276 }
277 );
278 }
279 if (st == NULL)
280 {
281 KRML_HOST_FREE(s1);
282 return NULL;
283 }
284 return st;
285 }
286 case Hacl_Agile_Hash_SHA2_384:
287 {
288 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t));
289 if (s == NULL)
290 {
291 return NULL;
292 }
293 uint64_t *s1 = s;
294 Hacl_Agile_Hash_state_s
295 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
296 if (st != NULL)
297 {
298 st[0U] =
299 (
300 (Hacl_Agile_Hash_state_s){
301 .tag = Hacl_Agile_Hash_SHA2_384_a,
302 { .case_SHA2_384_a = s1 }
303 }
304 );
305 }
306 if (st == NULL)
307 {
308 KRML_HOST_FREE(s1);
309 return NULL;
310 }
311 return st;
312 }
313 case Hacl_Agile_Hash_SHA2_512:
314 {
315 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t));
316 if (s == NULL)
317 {
318 return NULL;
319 }
320 uint64_t *s1 = s;
321 Hacl_Agile_Hash_state_s
322 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
323 if (st != NULL)
324 {
325 st[0U] =
326 (
327 (Hacl_Agile_Hash_state_s){
328 .tag = Hacl_Agile_Hash_SHA2_512_a,
329 { .case_SHA2_512_a = s1 }
330 }
331 );
332 }
333 if (st == NULL)
334 {
335 KRML_HOST_FREE(s1);
336 return NULL;
337 }
338 return st;
339 }
340 case Hacl_Agile_Hash_SHA3_224:
341 {
342 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(25U, sizeof (uint64_t));
343 if (s == NULL)
344 {
345 return NULL;
346 }
347 uint64_t *s1 = s;
348 Hacl_Agile_Hash_state_s
349 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
350 if (st != NULL)
351 {
352 st[0U] =
353 (
354 (Hacl_Agile_Hash_state_s){
355 .tag = Hacl_Agile_Hash_SHA3_224_a,
356 { .case_SHA3_224_a = s1 }
357 }
358 );
359 }
360 if (st == NULL)
361 {
362 KRML_HOST_FREE(s1);
363 return NULL;
364 }
365 return st;
366 }
367 case Hacl_Agile_Hash_SHA3_256:
368 {
369 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(25U, sizeof (uint64_t));
370 if (s == NULL)
371 {
372 return NULL;
373 }
374 uint64_t *s1 = s;
375 Hacl_Agile_Hash_state_s
376 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
377 if (st != NULL)
378 {
379 st[0U] =
380 (
381 (Hacl_Agile_Hash_state_s){
382 .tag = Hacl_Agile_Hash_SHA3_256_a,
383 { .case_SHA3_256_a = s1 }
384 }
385 );
386 }
387 if (st == NULL)
388 {
389 KRML_HOST_FREE(s1);
390 return NULL;
391 }
392 return st;
393 }
394 case Hacl_Agile_Hash_SHA3_384:
395 {
396 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(25U, sizeof (uint64_t));
397 if (s == NULL)
398 {
399 return NULL;
400 }
401 uint64_t *s1 = s;
402 Hacl_Agile_Hash_state_s
403 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
404 if (st != NULL)
405 {
406 st[0U] =
407 (
408 (Hacl_Agile_Hash_state_s){
409 .tag = Hacl_Agile_Hash_SHA3_384_a,
410 { .case_SHA3_384_a = s1 }
411 }
412 );
413 }
414 if (st == NULL)
415 {
416 KRML_HOST_FREE(s1);
417 return NULL;
418 }
419 return st;
420 }
421 case Hacl_Agile_Hash_SHA3_512:
422 {
423 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(25U, sizeof (uint64_t));
424 if (s == NULL)
425 {
426 return NULL;
427 }
428 uint64_t *s1 = s;
429 Hacl_Agile_Hash_state_s
430 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
431 if (st != NULL)
432 {
433 st[0U] =
434 (
435 (Hacl_Agile_Hash_state_s){
436 .tag = Hacl_Agile_Hash_SHA3_512_a,
437 { .case_SHA3_512_a = s1 }
438 }
439 );
440 }
441 if (st == NULL)
442 {
443 KRML_HOST_FREE(s1);
444 return NULL;
445 }
446 return st;
447 }
448 case Hacl_Agile_Hash_Blake2S_32:
449 {
450 uint32_t *s = (uint32_t *)KRML_HOST_CALLOC(16U, sizeof (uint32_t));
451 if (s == NULL)
452 {
453 return NULL;
454 }
455 uint32_t *s1 = s;
456 Hacl_Agile_Hash_state_s
457 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
458 if (st != NULL)
459 {
460 st[0U] =
461 (
462 (Hacl_Agile_Hash_state_s){
463 .tag = Hacl_Agile_Hash_Blake2S_a,
464 { .case_Blake2S_a = s1 }
465 }
466 );
467 }
468 if (st == NULL)
469 {
470 KRML_HOST_FREE(s1);
471 return NULL;
472 }
473 return st;
474 }
475 case Hacl_Agile_Hash_Blake2S_128:
476 {
477 #if HACL_CAN_COMPILE_VEC128
478 Lib_IntVector_Intrinsics_vec128
479 *s = Hacl_Hash_Blake2s_Simd128_malloc_internal_state_with_key();
480 if (s == NULL)
481 {
482 return NULL;
483 }
484 Hacl_Agile_Hash_state_s
485 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
486 if (st != NULL)
487 {
488 st[0U] =
489 (
490 (Hacl_Agile_Hash_state_s){
491 .tag = Hacl_Agile_Hash_Blake2S_128_a,
492 { .case_Blake2S_128_a = s }
493 }
494 );
495 }
496 if (st == NULL)
497 {
498 KRML_ALIGNED_FREE(s);
499 return NULL;
500 }
501 return st;
502 #else
503 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
504 __FILE__,
505 __LINE__,
506 "provably unreachable code: did an unverified caller violate a precondition\?");
507 KRML_HOST_EXIT(255U);
508 #endif
509 break;
510 }
511 case Hacl_Agile_Hash_Blake2B_32:
512 {
513 uint64_t *s = (uint64_t *)KRML_HOST_CALLOC(16U, sizeof (uint64_t));
514 if (s == NULL)
515 {
516 return NULL;
517 }
518 uint64_t *s1 = s;
519 Hacl_Agile_Hash_state_s
520 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
521 if (st != NULL)
522 {
523 st[0U] =
524 (
525 (Hacl_Agile_Hash_state_s){
526 .tag = Hacl_Agile_Hash_Blake2B_a,
527 { .case_Blake2B_a = s1 }
528 }
529 );
530 }
531 if (st == NULL)
532 {
533 KRML_HOST_FREE(s1);
534 return NULL;
535 }
536 return st;
537 }
538 case Hacl_Agile_Hash_Blake2B_256:
539 {
540 #if HACL_CAN_COMPILE_VEC256
541 Lib_IntVector_Intrinsics_vec256
542 *s = Hacl_Hash_Blake2b_Simd256_malloc_internal_state_with_key();
543 if (s == NULL)
544 {
545 return NULL;
546 }
547 Hacl_Agile_Hash_state_s
548 *st = (Hacl_Agile_Hash_state_s *)KRML_HOST_MALLOC(sizeof (Hacl_Agile_Hash_state_s));
549 if (st != NULL)
550 {
551 st[0U] =
552 (
553 (Hacl_Agile_Hash_state_s){
554 .tag = Hacl_Agile_Hash_Blake2B_256_a,
555 { .case_Blake2B_256_a = s }
556 }
557 );
558 }
559 if (st == NULL)
560 {
561 KRML_ALIGNED_FREE(s);
562 return NULL;
563 }
564 return st;
565 #else
566 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
567 __FILE__,
568 __LINE__,
569 "provably unreachable code: did an unverified caller violate a precondition\?");
570 KRML_HOST_EXIT(255U);
571 #endif
572 break;
573 }
574 default:
575 {
576 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
577 KRML_HOST_EXIT(253U);
578 }
579 }
580 }
581
582 static void init(Hacl_Agile_Hash_state_s *s)
583 {
584 Hacl_Agile_Hash_state_s scrut = *s;
585 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
586 {
587 uint32_t *p1 = scrut.case_MD5_a;
588 Hacl_Hash_MD5_init(p1);
589 return;
590 }
591 if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
592 {
593 uint32_t *p1 = scrut.case_SHA1_a;
594 Hacl_Hash_SHA1_init(p1);
595 return;
596 }
597 if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
598 {
599 uint32_t *p1 = scrut.case_SHA2_224_a;
600 Hacl_Hash_SHA2_sha224_init(p1);
601 return;
602 }
603 if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
604 {
605 uint32_t *p1 = scrut.case_SHA2_256_a;
606 Hacl_Hash_SHA2_sha256_init(p1);
607 return;
608 }
609 if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
610 {
611 uint64_t *p1 = scrut.case_SHA2_384_a;
612 Hacl_Hash_SHA2_sha384_init(p1);
613 return;
614 }
615 if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
616 {
617 uint64_t *p1 = scrut.case_SHA2_512_a;
618 Hacl_Hash_SHA2_sha512_init(p1);
619 return;
620 }
621 if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
622 {
623 uint64_t *p1 = scrut.case_SHA3_224_a;
624 Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_224, p1);
625 return;
626 }
627 if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
628 {
629 uint64_t *p1 = scrut.case_SHA3_256_a;
630 Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_256, p1);
631 return;
632 }
633 if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
634 {
635 uint64_t *p1 = scrut.case_SHA3_384_a;
636 Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_384, p1);
637 return;
638 }
639 if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
640 {
641 uint64_t *p1 = scrut.case_SHA3_512_a;
642 Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_512, p1);
643 return;
644 }
645 if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
646 {
647 uint32_t *p1 = scrut.case_Blake2S_a;
648 Hacl_Hash_Blake2s_init(p1, 0U, 32U);
649 return;
650 }
651 if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
652 {
653 Lib_IntVector_Intrinsics_vec128 *p1 = scrut.case_Blake2S_128_a;
654 #if HACL_CAN_COMPILE_VEC128
655 Hacl_Hash_Blake2s_Simd128_init(p1, 0U, 32U);
656 return;
657 #else
658 KRML_MAYBE_UNUSED_VAR(p1);
659 return;
660 #endif
661 }
662 if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
663 {
664 uint64_t *p1 = scrut.case_Blake2B_a;
665 Hacl_Hash_Blake2b_init(p1, 0U, 64U);
666 return;
667 }
668 if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
669 {
670 Lib_IntVector_Intrinsics_vec256 *p1 = scrut.case_Blake2B_256_a;
671 #if HACL_CAN_COMPILE_VEC256
672 Hacl_Hash_Blake2b_Simd256_init(p1, 0U, 64U);
673 return;
674 #else
675 KRML_MAYBE_UNUSED_VAR(p1);
676 return;
677 #endif
678 }
679 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
680 __FILE__,
681 __LINE__,
682 "unreachable (pattern matches are exhaustive in F*)");
683 KRML_HOST_EXIT(255U);
684 }
685
686 static void
687 update_multi(Hacl_Agile_Hash_state_s *s, uint64_t prevlen, uint8_t *blocks, uint32_t len)
688 {
689 Hacl_Agile_Hash_state_s scrut = *s;
690 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
691 {
692 uint32_t *p1 = scrut.case_MD5_a;
693 uint32_t n = len / 64U;
694 Hacl_Hash_MD5_update_multi(p1, blocks, n);
695 return;
696 }
697 if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
698 {
699 uint32_t *p1 = scrut.case_SHA1_a;
700 uint32_t n = len / 64U;
701 Hacl_Hash_SHA1_update_multi(p1, blocks, n);
702 return;
703 }
704 if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
705 {
706 uint32_t *p1 = scrut.case_SHA2_224_a;
707 uint32_t n = len / 64U;
708 Hacl_Hash_SHA2_sha224_update_nblocks(n * 64U, blocks, p1);
709 return;
710 }
711 if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
712 {
713 uint32_t *p1 = scrut.case_SHA2_256_a;
714 uint32_t n = len / 64U;
715 Hacl_Hash_SHA2_sha256_update_nblocks(n * 64U, blocks, p1);
716 return;
717 }
718 if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
719 {
720 uint64_t *p1 = scrut.case_SHA2_384_a;
721 uint32_t n = len / 128U;
722 Hacl_Hash_SHA2_sha384_update_nblocks(n * 128U, blocks, p1);
723 return;
724 }
725 if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
726 {
727 uint64_t *p1 = scrut.case_SHA2_512_a;
728 uint32_t n = len / 128U;
729 Hacl_Hash_SHA2_sha512_update_nblocks(n * 128U, blocks, p1);
730 return;
731 }
732 if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
733 {
734 uint64_t *p1 = scrut.case_SHA3_224_a;
735 uint32_t n = len / 144U;
736 Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_224, p1, blocks, n);
737 return;
738 }
739 if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
740 {
741 uint64_t *p1 = scrut.case_SHA3_256_a;
742 uint32_t n = len / 136U;
743 Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_256, p1, blocks, n);
744 return;
745 }
746 if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
747 {
748 uint64_t *p1 = scrut.case_SHA3_384_a;
749 uint32_t n = len / 104U;
750 Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_384, p1, blocks, n);
751 return;
752 }
753 if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
754 {
755 uint64_t *p1 = scrut.case_SHA3_512_a;
756 uint32_t n = len / 72U;
757 Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_512, p1, blocks, n);
758 return;
759 }
760 if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
761 {
762 uint32_t *p1 = scrut.case_Blake2S_a;
763 uint32_t n = len / 64U;
764 uint32_t wv[16U] = { 0U };
765 Hacl_Hash_Blake2s_update_multi(n * 64U, wv, p1, prevlen, blocks, n);
766 return;
767 }
768 if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
769 {
770 Lib_IntVector_Intrinsics_vec128 *p1 = scrut.case_Blake2S_128_a;
771 #if HACL_CAN_COMPILE_VEC128
772 uint32_t n = len / 64U;
773 Hacl_Hash_Blake2s_Simd128_update_multi_no_inline(p1, prevlen, blocks, n);
774 return;
775 #else
776 KRML_MAYBE_UNUSED_VAR(p1);
777 return;
778 #endif
779 }
780 if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
781 {
782 uint64_t *p1 = scrut.case_Blake2B_a;
783 uint32_t n = len / 128U;
784 uint64_t wv[16U] = { 0U };
785 Hacl_Hash_Blake2b_update_multi(n * 128U,
786 wv,
787 p1,
788 FStar_UInt128_uint64_to_uint128(prevlen),
789 blocks,
790 n);
791 return;
792 }
793 if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
794 {
795 Lib_IntVector_Intrinsics_vec256 *p1 = scrut.case_Blake2B_256_a;
796 #if HACL_CAN_COMPILE_VEC256
797 uint32_t n = len / 128U;
798 Hacl_Hash_Blake2b_Simd256_update_multi_no_inline(p1,
799 FStar_UInt128_uint64_to_uint128(prevlen),
800 blocks,
801 n);
802 return;
803 #else
804 KRML_MAYBE_UNUSED_VAR(p1);
805 return;
806 #endif
807 }
808 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
809 __FILE__,
810 __LINE__,
811 "unreachable (pattern matches are exhaustive in F*)");
812 KRML_HOST_EXIT(255U);
813 }
814
815 static void
816 update_last(Hacl_Agile_Hash_state_s *s, uint64_t prev_len, uint8_t *last, uint32_t last_len)
817 {
818 Hacl_Agile_Hash_state_s scrut = *s;
819 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
820 {
821 uint32_t *p1 = scrut.case_MD5_a;
822 Hacl_Hash_MD5_update_last(p1, prev_len, last, last_len);
823 return;
824 }
825 if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
826 {
827 uint32_t *p1 = scrut.case_SHA1_a;
828 Hacl_Hash_SHA1_update_last(p1, prev_len, last, last_len);
829 return;
830 }
831 if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
832 {
833 uint32_t *p1 = scrut.case_SHA2_224_a;
834 Hacl_Hash_SHA2_sha224_update_last(prev_len + (uint64_t)last_len, last_len, last, p1);
835 return;
836 }
837 if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
838 {
839 uint32_t *p1 = scrut.case_SHA2_256_a;
840 Hacl_Hash_SHA2_sha256_update_last(prev_len + (uint64_t)last_len, last_len, last, p1);
841 return;
842 }
843 if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
844 {
845 uint64_t *p1 = scrut.case_SHA2_384_a;
846 Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len),
847 FStar_UInt128_uint64_to_uint128((uint64_t)last_len)),
848 last_len,
849 last,
850 p1);
851 return;
852 }
853 if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
854 {
855 uint64_t *p1 = scrut.case_SHA2_512_a;
856 Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len),
857 FStar_UInt128_uint64_to_uint128((uint64_t)last_len)),
858 last_len,
859 last,
860 p1);
861 return;
862 }
863 if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
864 {
865 uint64_t *p1 = scrut.case_SHA3_224_a;
866 Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_224, p1, last, last_len);
867 return;
868 }
869 if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
870 {
871 uint64_t *p1 = scrut.case_SHA3_256_a;
872 Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_256, p1, last, last_len);
873 return;
874 }
875 if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
876 {
877 uint64_t *p1 = scrut.case_SHA3_384_a;
878 Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_384, p1, last, last_len);
879 return;
880 }
881 if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
882 {
883 uint64_t *p1 = scrut.case_SHA3_512_a;
884 Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_512, p1, last, last_len);
885 return;
886 }
887 if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
888 {
889 uint32_t *p1 = scrut.case_Blake2S_a;
890 uint32_t wv[16U] = { 0U };
891 Hacl_Hash_Blake2s_update_last(last_len, wv, p1, false, prev_len, last_len, last);
892 return;
893 }
894 if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
895 {
896 Lib_IntVector_Intrinsics_vec128 *p1 = scrut.case_Blake2S_128_a;
897 #if HACL_CAN_COMPILE_VEC128
898 Hacl_Hash_Blake2s_Simd128_update_last_no_inline(p1, prev_len, last, last_len);
899 return;
900 #else
901 KRML_MAYBE_UNUSED_VAR(p1);
902 return;
903 #endif
904 }
905 if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
906 {
907 uint64_t *p1 = scrut.case_Blake2B_a;
908 uint64_t wv[16U] = { 0U };
909 Hacl_Hash_Blake2b_update_last(last_len,
910 wv,
911 p1,
912 false,
913 FStar_UInt128_uint64_to_uint128(prev_len),
914 last_len,
915 last);
916 return;
917 }
918 if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
919 {
920 Lib_IntVector_Intrinsics_vec256 *p1 = scrut.case_Blake2B_256_a;
921 #if HACL_CAN_COMPILE_VEC256
922 Hacl_Hash_Blake2b_Simd256_update_last_no_inline(p1,
923 FStar_UInt128_uint64_to_uint128(prev_len),
924 last,
925 last_len);
926 return;
927 #else
928 KRML_MAYBE_UNUSED_VAR(p1);
929 return;
930 #endif
931 }
932 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
933 __FILE__,
934 __LINE__,
935 "unreachable (pattern matches are exhaustive in F*)");
936 KRML_HOST_EXIT(255U);
937 }
938
939 static void finish(Hacl_Agile_Hash_state_s *s, uint8_t *dst)
940 {
941 Hacl_Agile_Hash_state_s scrut = *s;
942 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
943 {
944 uint32_t *p1 = scrut.case_MD5_a;
945 Hacl_Hash_MD5_finish(p1, dst);
946 return;
947 }
948 if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
949 {
950 uint32_t *p1 = scrut.case_SHA1_a;
951 Hacl_Hash_SHA1_finish(p1, dst);
952 return;
953 }
954 if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
955 {
956 uint32_t *p1 = scrut.case_SHA2_224_a;
957 Hacl_Hash_SHA2_sha224_finish(p1, dst);
958 return;
959 }
960 if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
961 {
962 uint32_t *p1 = scrut.case_SHA2_256_a;
963 Hacl_Hash_SHA2_sha256_finish(p1, dst);
964 return;
965 }
966 if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
967 {
968 uint64_t *p1 = scrut.case_SHA2_384_a;
969 Hacl_Hash_SHA2_sha384_finish(p1, dst);
970 return;
971 }
972 if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
973 {
974 uint64_t *p1 = scrut.case_SHA2_512_a;
975 Hacl_Hash_SHA2_sha512_finish(p1, dst);
976 return;
977 }
978 if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
979 {
980 uint64_t *p1 = scrut.case_SHA3_224_a;
981 uint32_t remOut = 28U;
982 uint8_t hbuf[256U] = { 0U };
983 uint64_t ws[32U] = { 0U };
984 memcpy(ws, p1, 25U * sizeof (uint64_t));
985 for (uint32_t i = 0U; i < 32U; i++)
986 {
987 store64_le(hbuf + i * 8U, ws[i]);
988 }
989 memcpy(dst + 28U - remOut, hbuf, remOut * sizeof (uint8_t));
990 return;
991 }
992 if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
993 {
994 uint64_t *p1 = scrut.case_SHA3_256_a;
995 uint32_t remOut = 32U;
996 uint8_t hbuf[256U] = { 0U };
997 uint64_t ws[32U] = { 0U };
998 memcpy(ws, p1, 25U * sizeof (uint64_t));
999 for (uint32_t i = 0U; i < 32U; i++)
1000 {
1001 store64_le(hbuf + i * 8U, ws[i]);
1002 }
1003 memcpy(dst + 32U - remOut, hbuf, remOut * sizeof (uint8_t));
1004 return;
1005 }
1006 if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
1007 {
1008 uint64_t *p1 = scrut.case_SHA3_384_a;
1009 uint32_t remOut = 48U;
1010 uint8_t hbuf[256U] = { 0U };
1011 uint64_t ws[32U] = { 0U };
1012 memcpy(ws, p1, 25U * sizeof (uint64_t));
1013 for (uint32_t i = 0U; i < 32U; i++)
1014 {
1015 store64_le(hbuf + i * 8U, ws[i]);
1016 }
1017 memcpy(dst + 48U - remOut, hbuf, remOut * sizeof (uint8_t));
1018 return;
1019 }
1020 if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
1021 {
1022 uint64_t *p1 = scrut.case_SHA3_512_a;
1023 uint32_t remOut = 64U;
1024 uint8_t hbuf[256U] = { 0U };
1025 uint64_t ws[32U] = { 0U };
1026 memcpy(ws, p1, 25U * sizeof (uint64_t));
1027 for (uint32_t i = 0U; i < 32U; i++)
1028 {
1029 store64_le(hbuf + i * 8U, ws[i]);
1030 }
1031 memcpy(dst + 64U - remOut, hbuf, remOut * sizeof (uint8_t));
1032 return;
1033 }
1034 if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
1035 {
1036 uint32_t *p1 = scrut.case_Blake2S_a;
1037 Hacl_Hash_Blake2s_finish(32U, dst, p1);
1038 return;
1039 }
1040 if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
1041 {
1042 Lib_IntVector_Intrinsics_vec128 *p1 = scrut.case_Blake2S_128_a;
1043 #if HACL_CAN_COMPILE_VEC128
1044 Hacl_Hash_Blake2s_Simd128_finish(32U, dst, p1);
1045 return;
1046 #else
1047 KRML_MAYBE_UNUSED_VAR(p1);
1048 return;
1049 #endif
1050 }
1051 if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
1052 {
1053 uint64_t *p1 = scrut.case_Blake2B_a;
1054 Hacl_Hash_Blake2b_finish(64U, dst, p1);
1055 return;
1056 }
1057 if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
1058 {
1059 Lib_IntVector_Intrinsics_vec256 *p1 = scrut.case_Blake2B_256_a;
1060 #if HACL_CAN_COMPILE_VEC256
1061 Hacl_Hash_Blake2b_Simd256_finish(64U, dst, p1);
1062 return;
1063 #else
1064 KRML_MAYBE_UNUSED_VAR(p1);
1065 return;
1066 #endif
1067 }
1068 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1069 __FILE__,
1070 __LINE__,
1071 "unreachable (pattern matches are exhaustive in F*)");
1072 KRML_HOST_EXIT(255U);
1073 }
1074
1075 static void free_(Hacl_Agile_Hash_state_s *s)
1076 {
1077 Hacl_Agile_Hash_state_s scrut = *s;
1078 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
1079 {
1080 uint32_t *p1 = scrut.case_MD5_a;
1081 KRML_HOST_FREE(p1);
1082 }
1083 else if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
1084 {
1085 uint32_t *p1 = scrut.case_SHA1_a;
1086 KRML_HOST_FREE(p1);
1087 }
1088 else if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
1089 {
1090 uint32_t *p1 = scrut.case_SHA2_224_a;
1091 KRML_HOST_FREE(p1);
1092 }
1093 else if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
1094 {
1095 uint32_t *p1 = scrut.case_SHA2_256_a;
1096 KRML_HOST_FREE(p1);
1097 }
1098 else if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
1099 {
1100 uint64_t *p1 = scrut.case_SHA2_384_a;
1101 KRML_HOST_FREE(p1);
1102 }
1103 else if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
1104 {
1105 uint64_t *p1 = scrut.case_SHA2_512_a;
1106 KRML_HOST_FREE(p1);
1107 }
1108 else if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
1109 {
1110 uint64_t *p1 = scrut.case_SHA3_224_a;
1111 KRML_HOST_FREE(p1);
1112 }
1113 else if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
1114 {
1115 uint64_t *p1 = scrut.case_SHA3_256_a;
1116 KRML_HOST_FREE(p1);
1117 }
1118 else if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
1119 {
1120 uint64_t *p1 = scrut.case_SHA3_384_a;
1121 KRML_HOST_FREE(p1);
1122 }
1123 else if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
1124 {
1125 uint64_t *p1 = scrut.case_SHA3_512_a;
1126 KRML_HOST_FREE(p1);
1127 }
1128 else if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
1129 {
1130 uint32_t *p1 = scrut.case_Blake2S_a;
1131 KRML_HOST_FREE(p1);
1132 }
1133 else if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
1134 {
1135 Lib_IntVector_Intrinsics_vec128 *p1 = scrut.case_Blake2S_128_a;
1136 KRML_ALIGNED_FREE(p1);
1137 }
1138 else if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
1139 {
1140 uint64_t *p1 = scrut.case_Blake2B_a;
1141 KRML_HOST_FREE(p1);
1142 }
1143 else if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
1144 {
1145 Lib_IntVector_Intrinsics_vec256 *p1 = scrut.case_Blake2B_256_a;
1146 KRML_ALIGNED_FREE(p1);
1147 }
1148 else
1149 {
1150 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1151 __FILE__,
1152 __LINE__,
1153 "unreachable (pattern matches are exhaustive in F*)");
1154 KRML_HOST_EXIT(255U);
1155 }
1156 KRML_HOST_FREE(s);
1157 }
1158
1159 static void copy(Hacl_Agile_Hash_state_s *s_src, Hacl_Agile_Hash_state_s *s_dst)
1160 {
1161 Hacl_Agile_Hash_state_s scrut = *s_src;
1162 if (scrut.tag == Hacl_Agile_Hash_MD5_a)
1163 {
1164 uint32_t *p_src = scrut.case_MD5_a;
1165 Hacl_Agile_Hash_state_s x1 = *s_dst;
1166 uint32_t *p_dst;
1167 if (x1.tag == Hacl_Agile_Hash_MD5_a)
1168 {
1169 p_dst = x1.case_MD5_a;
1170 }
1171 else
1172 {
1173 p_dst = KRML_EABORT(uint32_t *, "unreachable (pattern matches are exhaustive in F*)");
1174 }
1175 memcpy(p_dst, p_src, 4U * sizeof (uint32_t));
1176 return;
1177 }
1178 if (scrut.tag == Hacl_Agile_Hash_SHA1_a)
1179 {
1180 uint32_t *p_src = scrut.case_SHA1_a;
1181 Hacl_Agile_Hash_state_s x1 = *s_dst;
1182 uint32_t *p_dst;
1183 if (x1.tag == Hacl_Agile_Hash_SHA1_a)
1184 {
1185 p_dst = x1.case_SHA1_a;
1186 }
1187 else
1188 {
1189 p_dst = KRML_EABORT(uint32_t *, "unreachable (pattern matches are exhaustive in F*)");
1190 }
1191 memcpy(p_dst, p_src, 5U * sizeof (uint32_t));
1192 return;
1193 }
1194 if (scrut.tag == Hacl_Agile_Hash_SHA2_224_a)
1195 {
1196 uint32_t *p_src = scrut.case_SHA2_224_a;
1197 Hacl_Agile_Hash_state_s x1 = *s_dst;
1198 uint32_t *p_dst;
1199 if (x1.tag == Hacl_Agile_Hash_SHA2_224_a)
1200 {
1201 p_dst = x1.case_SHA2_224_a;
1202 }
1203 else
1204 {
1205 p_dst = KRML_EABORT(uint32_t *, "unreachable (pattern matches are exhaustive in F*)");
1206 }
1207 memcpy(p_dst, p_src, 8U * sizeof (uint32_t));
1208 return;
1209 }
1210 if (scrut.tag == Hacl_Agile_Hash_SHA2_256_a)
1211 {
1212 uint32_t *p_src = scrut.case_SHA2_256_a;
1213 Hacl_Agile_Hash_state_s x1 = *s_dst;
1214 uint32_t *p_dst;
1215 if (x1.tag == Hacl_Agile_Hash_SHA2_256_a)
1216 {
1217 p_dst = x1.case_SHA2_256_a;
1218 }
1219 else
1220 {
1221 p_dst = KRML_EABORT(uint32_t *, "unreachable (pattern matches are exhaustive in F*)");
1222 }
1223 memcpy(p_dst, p_src, 8U * sizeof (uint32_t));
1224 return;
1225 }
1226 if (scrut.tag == Hacl_Agile_Hash_SHA2_384_a)
1227 {
1228 uint64_t *p_src = scrut.case_SHA2_384_a;
1229 Hacl_Agile_Hash_state_s x1 = *s_dst;
1230 uint64_t *p_dst;
1231 if (x1.tag == Hacl_Agile_Hash_SHA2_384_a)
1232 {
1233 p_dst = x1.case_SHA2_384_a;
1234 }
1235 else
1236 {
1237 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1238 }
1239 memcpy(p_dst, p_src, 8U * sizeof (uint64_t));
1240 return;
1241 }
1242 if (scrut.tag == Hacl_Agile_Hash_SHA2_512_a)
1243 {
1244 uint64_t *p_src = scrut.case_SHA2_512_a;
1245 Hacl_Agile_Hash_state_s x1 = *s_dst;
1246 uint64_t *p_dst;
1247 if (x1.tag == Hacl_Agile_Hash_SHA2_512_a)
1248 {
1249 p_dst = x1.case_SHA2_512_a;
1250 }
1251 else
1252 {
1253 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1254 }
1255 memcpy(p_dst, p_src, 8U * sizeof (uint64_t));
1256 return;
1257 }
1258 if (scrut.tag == Hacl_Agile_Hash_SHA3_224_a)
1259 {
1260 uint64_t *p_src = scrut.case_SHA3_224_a;
1261 Hacl_Agile_Hash_state_s x1 = *s_dst;
1262 uint64_t *p_dst;
1263 if (x1.tag == Hacl_Agile_Hash_SHA3_224_a)
1264 {
1265 p_dst = x1.case_SHA3_224_a;
1266 }
1267 else
1268 {
1269 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1270 }
1271 memcpy(p_dst, p_src, 25U * sizeof (uint64_t));
1272 return;
1273 }
1274 if (scrut.tag == Hacl_Agile_Hash_SHA3_256_a)
1275 {
1276 uint64_t *p_src = scrut.case_SHA3_256_a;
1277 Hacl_Agile_Hash_state_s x1 = *s_dst;
1278 uint64_t *p_dst;
1279 if (x1.tag == Hacl_Agile_Hash_SHA3_256_a)
1280 {
1281 p_dst = x1.case_SHA3_256_a;
1282 }
1283 else
1284 {
1285 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1286 }
1287 memcpy(p_dst, p_src, 25U * sizeof (uint64_t));
1288 return;
1289 }
1290 if (scrut.tag == Hacl_Agile_Hash_SHA3_384_a)
1291 {
1292 uint64_t *p_src = scrut.case_SHA3_384_a;
1293 Hacl_Agile_Hash_state_s x1 = *s_dst;
1294 uint64_t *p_dst;
1295 if (x1.tag == Hacl_Agile_Hash_SHA3_384_a)
1296 {
1297 p_dst = x1.case_SHA3_384_a;
1298 }
1299 else
1300 {
1301 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1302 }
1303 memcpy(p_dst, p_src, 25U * sizeof (uint64_t));
1304 return;
1305 }
1306 if (scrut.tag == Hacl_Agile_Hash_SHA3_512_a)
1307 {
1308 uint64_t *p_src = scrut.case_SHA3_512_a;
1309 Hacl_Agile_Hash_state_s x1 = *s_dst;
1310 uint64_t *p_dst;
1311 if (x1.tag == Hacl_Agile_Hash_SHA3_512_a)
1312 {
1313 p_dst = x1.case_SHA3_512_a;
1314 }
1315 else
1316 {
1317 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1318 }
1319 memcpy(p_dst, p_src, 25U * sizeof (uint64_t));
1320 return;
1321 }
1322 if (scrut.tag == Hacl_Agile_Hash_Blake2S_a)
1323 {
1324 uint32_t *p_src = scrut.case_Blake2S_a;
1325 Hacl_Agile_Hash_state_s x1 = *s_dst;
1326 uint32_t *p_dst;
1327 if (x1.tag == Hacl_Agile_Hash_Blake2S_a)
1328 {
1329 p_dst = x1.case_Blake2S_a;
1330 }
1331 else
1332 {
1333 p_dst = KRML_EABORT(uint32_t *, "unreachable (pattern matches are exhaustive in F*)");
1334 }
1335 memcpy(p_dst, p_src, 16U * sizeof (uint32_t));
1336 return;
1337 }
1338 if (scrut.tag == Hacl_Agile_Hash_Blake2S_128_a)
1339 {
1340 Lib_IntVector_Intrinsics_vec128 *p_src = scrut.case_Blake2S_128_a;
1341 KRML_MAYBE_UNUSED_VAR(p_src);
1342 #if HACL_CAN_COMPILE_VEC128
1343 Hacl_Agile_Hash_state_s x1 = *s_dst;
1344 Lib_IntVector_Intrinsics_vec128 *p_dst;
1345 if (x1.tag == Hacl_Agile_Hash_Blake2S_128_a)
1346 {
1347 p_dst = x1.case_Blake2S_128_a;
1348 }
1349 else
1350 {
1351 p_dst =
1352 KRML_EABORT(Lib_IntVector_Intrinsics_vec128 *,
1353 "unreachable (pattern matches are exhaustive in F*)");
1354 }
1355 Hacl_Hash_Blake2s_Simd128_copy_internal_state(p_src, p_dst);
1356 return;
1357 #else
1358 return;
1359 #endif
1360 }
1361 if (scrut.tag == Hacl_Agile_Hash_Blake2B_a)
1362 {
1363 uint64_t *p_src = scrut.case_Blake2B_a;
1364 Hacl_Agile_Hash_state_s x1 = *s_dst;
1365 uint64_t *p_dst;
1366 if (x1.tag == Hacl_Agile_Hash_Blake2B_a)
1367 {
1368 p_dst = x1.case_Blake2B_a;
1369 }
1370 else
1371 {
1372 p_dst = KRML_EABORT(uint64_t *, "unreachable (pattern matches are exhaustive in F*)");
1373 }
1374 memcpy(p_dst, p_src, 16U * sizeof (uint64_t));
1375 return;
1376 }
1377 if (scrut.tag == Hacl_Agile_Hash_Blake2B_256_a)
1378 {
1379 Lib_IntVector_Intrinsics_vec256 *p_src = scrut.case_Blake2B_256_a;
1380 KRML_MAYBE_UNUSED_VAR(p_src);
1381 #if HACL_CAN_COMPILE_VEC256
1382 Hacl_Agile_Hash_state_s x1 = *s_dst;
1383 Lib_IntVector_Intrinsics_vec256 *p_dst;
1384 if (x1.tag == Hacl_Agile_Hash_Blake2B_256_a)
1385 {
1386 p_dst = x1.case_Blake2B_256_a;
1387 }
1388 else
1389 {
1390 p_dst =
1391 KRML_EABORT(Lib_IntVector_Intrinsics_vec256 *,
1392 "unreachable (pattern matches are exhaustive in F*)");
1393 }
1394 Hacl_Hash_Blake2b_Simd256_copy_internal_state(p_src, p_dst);
1395 return;
1396 #else
1397 return;
1398 #endif
1399 }
1400 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1401 __FILE__,
1402 __LINE__,
1403 "unreachable (pattern matches are exhaustive in F*)");
1404 KRML_HOST_EXIT(255U);
1405 }
1406
1407 static void hash(Hacl_Agile_Hash_impl i, uint8_t *dst, uint8_t *input, uint32_t input_len)
1408 {
1409 switch (i)
1410 {
1411 case Hacl_Agile_Hash_MD5:
1412 {
1413 Hacl_Hash_MD5_hash_oneshot(dst, input, input_len);
1414 break;
1415 }
1416 case Hacl_Agile_Hash_SHA1:
1417 {
1418 Hacl_Hash_SHA1_hash_oneshot(dst, input, input_len);
1419 break;
1420 }
1421 case Hacl_Agile_Hash_SHA2_224:
1422 {
1423 Hacl_Hash_SHA2_hash_224(dst, input, input_len);
1424 break;
1425 }
1426 case Hacl_Agile_Hash_SHA2_256:
1427 {
1428 Hacl_Hash_SHA2_hash_256(dst, input, input_len);
1429 break;
1430 }
1431 case Hacl_Agile_Hash_SHA2_384:
1432 {
1433 Hacl_Hash_SHA2_hash_384(dst, input, input_len);
1434 break;
1435 }
1436 case Hacl_Agile_Hash_SHA2_512:
1437 {
1438 Hacl_Hash_SHA2_hash_512(dst, input, input_len);
1439 break;
1440 }
1441 case Hacl_Agile_Hash_SHA3_224:
1442 {
1443 Hacl_Hash_SHA3_sha3_224(dst, input, input_len);
1444 break;
1445 }
1446 case Hacl_Agile_Hash_SHA3_256:
1447 {
1448 Hacl_Hash_SHA3_sha3_256(dst, input, input_len);
1449 break;
1450 }
1451 case Hacl_Agile_Hash_SHA3_384:
1452 {
1453 Hacl_Hash_SHA3_sha3_384(dst, input, input_len);
1454 break;
1455 }
1456 case Hacl_Agile_Hash_SHA3_512:
1457 {
1458 Hacl_Hash_SHA3_sha3_512(dst, input, input_len);
1459 break;
1460 }
1461 case Hacl_Agile_Hash_Blake2S_32:
1462 {
1463 Hacl_Hash_Blake2s_hash_with_key(dst, 32U, input, input_len, NULL, 0U);
1464 break;
1465 }
1466 case Hacl_Agile_Hash_Blake2S_128:
1467 {
1468 #if HACL_CAN_COMPILE_VEC128
1469 Hacl_Hash_Blake2s_Simd128_hash_with_key(dst, 32U, input, input_len, NULL, 0U);
1470 #endif
1471 break;
1472 }
1473 case Hacl_Agile_Hash_Blake2B_32:
1474 {
1475 Hacl_Hash_Blake2b_hash_with_key(dst, 64U, input, input_len, NULL, 0U);
1476 break;
1477 }
1478 case Hacl_Agile_Hash_Blake2B_256:
1479 {
1480 #if HACL_CAN_COMPILE_VEC256
1481 Hacl_Hash_Blake2b_Simd256_hash_with_key(dst, 64U, input, input_len, NULL, 0U);
1482 #endif
1483 break;
1484 }
1485 default:
1486 {
1487 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1488 KRML_HOST_EXIT(253U);
1489 }
1490 }
1491 }
1492
1493 static uint32_t hash_len(Spec_Hash_Definitions_hash_alg a)
1494 {
1495 switch (a)
1496 {
1497 case Spec_Hash_Definitions_MD5:
1498 {
1499 return 16U;
1500 }
1501 case Spec_Hash_Definitions_SHA1:
1502 {
1503 return 20U;
1504 }
1505 case Spec_Hash_Definitions_SHA2_224:
1506 {
1507 return 28U;
1508 }
1509 case Spec_Hash_Definitions_SHA2_256:
1510 {
1511 return 32U;
1512 }
1513 case Spec_Hash_Definitions_SHA2_384:
1514 {
1515 return 48U;
1516 }
1517 case Spec_Hash_Definitions_SHA2_512:
1518 {
1519 return 64U;
1520 }
1521 case Spec_Hash_Definitions_Blake2S:
1522 {
1523 return 32U;
1524 }
1525 case Spec_Hash_Definitions_Blake2B:
1526 {
1527 return 64U;
1528 }
1529 case Spec_Hash_Definitions_SHA3_224:
1530 {
1531 return 28U;
1532 }
1533 case Spec_Hash_Definitions_SHA3_256:
1534 {
1535 return 32U;
1536 }
1537 case Spec_Hash_Definitions_SHA3_384:
1538 {
1539 return 48U;
1540 }
1541 case Spec_Hash_Definitions_SHA3_512:
1542 {
1543 return 64U;
1544 }
1545 default:
1546 {
1547 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1548 KRML_HOST_EXIT(253U);
1549 }
1550 }
1551 }
1552
1553 static uint32_t block_len(Spec_Hash_Definitions_hash_alg a)
1554 {
1555 switch (a)
1556 {
1557 case Spec_Hash_Definitions_MD5:
1558 {
1559 return 64U;
1560 }
1561 case Spec_Hash_Definitions_SHA1:
1562 {
1563 return 64U;
1564 }
1565 case Spec_Hash_Definitions_SHA2_224:
1566 {
1567 return 64U;
1568 }
1569 case Spec_Hash_Definitions_SHA2_256:
1570 {
1571 return 64U;
1572 }
1573 case Spec_Hash_Definitions_SHA2_384:
1574 {
1575 return 128U;
1576 }
1577 case Spec_Hash_Definitions_SHA2_512:
1578 {
1579 return 128U;
1580 }
1581 case Spec_Hash_Definitions_SHA3_224:
1582 {
1583 return 144U;
1584 }
1585 case Spec_Hash_Definitions_SHA3_256:
1586 {
1587 return 136U;
1588 }
1589 case Spec_Hash_Definitions_SHA3_384:
1590 {
1591 return 104U;
1592 }
1593 case Spec_Hash_Definitions_SHA3_512:
1594 {
1595 return 72U;
1596 }
1597 case Spec_Hash_Definitions_Shake128:
1598 {
1599 return 168U;
1600 }
1601 case Spec_Hash_Definitions_Shake256:
1602 {
1603 return 136U;
1604 }
1605 case Spec_Hash_Definitions_Blake2S:
1606 {
1607 return 64U;
1608 }
1609 case Spec_Hash_Definitions_Blake2B:
1610 {
1611 return 128U;
1612 }
1613 default:
1614 {
1615 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1616 KRML_HOST_EXIT(253U);
1617 }
1618 }
1619 }
1620
1621 static uint64_t max_input_len64(Spec_Hash_Definitions_hash_alg a)
1622 {
1623 switch (a)
1624 {
1625 case Spec_Hash_Definitions_MD5:
1626 {
1627 return 2305843009213693951ULL;
1628 }
1629 case Spec_Hash_Definitions_SHA1:
1630 {
1631 return 2305843009213693951ULL;
1632 }
1633 case Spec_Hash_Definitions_SHA2_224:
1634 {
1635 return 2305843009213693951ULL;
1636 }
1637 case Spec_Hash_Definitions_SHA2_256:
1638 {
1639 return 2305843009213693951ULL;
1640 }
1641 case Spec_Hash_Definitions_SHA2_384:
1642 {
1643 return 18446744073709551615ULL;
1644 }
1645 case Spec_Hash_Definitions_SHA2_512:
1646 {
1647 return 18446744073709551615ULL;
1648 }
1649 case Spec_Hash_Definitions_Blake2S:
1650 {
1651 return 18446744073709551615ULL;
1652 }
1653 case Spec_Hash_Definitions_Blake2B:
1654 {
1655 return 18446744073709551615ULL;
1656 }
1657 case Spec_Hash_Definitions_SHA3_224:
1658 {
1659 return 18446744073709551615ULL;
1660 }
1661 case Spec_Hash_Definitions_SHA3_256:
1662 {
1663 return 18446744073709551615ULL;
1664 }
1665 case Spec_Hash_Definitions_SHA3_384:
1666 {
1667 return 18446744073709551615ULL;
1668 }
1669 case Spec_Hash_Definitions_SHA3_512:
1670 {
1671 return 18446744073709551615ULL;
1672 }
1673 case Spec_Hash_Definitions_Shake128:
1674 {
1675 return 18446744073709551615ULL;
1676 }
1677 case Spec_Hash_Definitions_Shake256:
1678 {
1679 return 18446744073709551615ULL;
1680 }
1681 default:
1682 {
1683 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1684 KRML_HOST_EXIT(253U);
1685 }
1686 }
1687 }
1688
1689 static void wrap_key(Hacl_Agile_Hash_impl impl, uint8_t *output, uint8_t *key, uint32_t len)
1690 {
1691 uint8_t *nkey = output;
1692 uint32_t ite;
1693 if (len <= block_len(alg_of_impl(impl)))
1694 {
1695 ite = len;
1696 }
1697 else
1698 {
1699 ite = hash_len(alg_of_impl(impl));
1700 }
1701 uint8_t *zeroes = output + ite;
1702 KRML_MAYBE_UNUSED_VAR(zeroes);
1703 if (len <= block_len(alg_of_impl(impl)))
1704 {
1705 if (len > 0U)
1706 {
1707 memcpy(nkey, key, len * sizeof (uint8_t));
1708 return;
1709 }
1710 return;
1711 }
1712 hash(impl, nkey, key, len);
1713 }
1714
1715 static void init0(uint8_t *k, uint8_t *buf, Hacl_Streaming_HMAC_Definitions_two_state s)
1716 {
1717 uint32_t k_len = s.fst;
1718 Hacl_Agile_Hash_state_s *s1 = s.snd;
1719 Hacl_Agile_Hash_state_s *s2 = s.thd;
1720 init(s1);
1721 init(s2);
1722 Hacl_Agile_Hash_impl i1 = impl_of_state(s1);
1723 Spec_Hash_Definitions_hash_alg a = alg_of_impl(i1);
1724 uint8_t b0[168U] = { 0U };
1725 uint8_t *block = b0;
1726 wrap_key(i1, block, k, k_len);
1727 uint8_t b1[168U];
1728 memset(b1, 0x36U, 168U * sizeof (uint8_t));
1729 uint8_t *ipad = b1;
1730 uint8_t b[168U];
1731 memset(b, 0x5cU, 168U * sizeof (uint8_t));
1732 uint8_t *opad = b;
1733 for (uint32_t i = 0U; i < block_len(a); i++)
1734 {
1735 uint8_t xi = ipad[i];
1736 uint8_t yi = block[i];
1737 buf[i] = (uint32_t)xi ^ (uint32_t)yi;
1738 }
1739 for (uint32_t i = 0U; i < block_len(a); i++)
1740 {
1741 uint8_t xi = opad[i];
1742 uint8_t yi = block[i];
1743 opad[i] = (uint32_t)xi ^ (uint32_t)yi;
1744 }
1745 update_multi(s2, 0ULL, opad, block_len(a));
1746 }
1747
1748 static void finish0(Hacl_Streaming_HMAC_Definitions_two_state s, uint8_t *dst)
1749 {
1750 Hacl_Agile_Hash_state_s *s2 = s.thd;
1751 Hacl_Agile_Hash_state_s *s1 = s.snd;
1752 Hacl_Agile_Hash_impl i1 = impl_of_state(s1);
1753 Spec_Hash_Definitions_hash_alg a = alg_of_impl(i1);
1754 finish(s1, dst);
1755 update_last(s2, (uint64_t)block_len(a), dst, hash_len(a));
1756 finish(s2, dst);
1757 }
1758
1759 Hacl_Agile_Hash_state_s
1760 *Hacl_Streaming_HMAC_s1(
1761 Hacl_Streaming_HMAC_Definitions_index i,
1762 Hacl_Streaming_HMAC_Definitions_two_state s
1763 )
1764 {
1765 KRML_MAYBE_UNUSED_VAR(i);
1766 return s.snd;
1767 }
1768
1769 Hacl_Agile_Hash_state_s
1770 *Hacl_Streaming_HMAC_s2(
1771 Hacl_Streaming_HMAC_Definitions_index i,
1772 Hacl_Streaming_HMAC_Definitions_two_state s
1773 )
1774 {
1775 KRML_MAYBE_UNUSED_VAR(i);
1776 return s.thd;
1777 }
1778
1779 Hacl_Streaming_HMAC_Definitions_index
1780 Hacl_Streaming_HMAC_index_of_state(Hacl_Streaming_HMAC_Definitions_two_state s)
1781 {
1782 Hacl_Agile_Hash_state_s *s11 = s.snd;
1783 uint32_t kl = s.fst;
1784 Hacl_Agile_Hash_impl i1 = impl_of_state(s11);
1785 return ((Hacl_Streaming_HMAC_Definitions_index){ .fst = i1, .snd = kl });
1786 }
1787
1788 static Hacl_Agile_Hash_impl
1789 __proj__Mkdtuple2__item___1__Hacl_Agile_Hash_impl_uint32_t(
1790 Hacl_Streaming_HMAC_Definitions_index pair
1791 )
1792 {
1793 return pair.fst;
1794 }
1795
1796 static Hacl_Agile_Hash_impl
1797 dfst__Hacl_Agile_Hash_impl_uint32_t(Hacl_Streaming_HMAC_Definitions_index t)
1798 {
1799 return __proj__Mkdtuple2__item___1__Hacl_Agile_Hash_impl_uint32_t(t);
1800 }
1801
1802 static uint32_t
1803 __proj__Mkdtuple2__item___2__Hacl_Agile_Hash_impl_uint32_t(
1804 Hacl_Streaming_HMAC_Definitions_index pair
1805 )
1806 {
1807 return pair.snd;
1808 }
1809
1810 static uint32_t dsnd__Hacl_Agile_Hash_impl_uint32_t(Hacl_Streaming_HMAC_Definitions_index t)
1811 {
1812 return __proj__Mkdtuple2__item___2__Hacl_Agile_Hash_impl_uint32_t(t);
1813 }
1814
1815 typedef struct option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s___s
1816 {
1817 Hacl_Streaming_Types_optional tag;
1818 Hacl_Streaming_HMAC_Definitions_two_state v;
1819 }
1820 option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__;
1821
1822 KRML_MAYBE_UNUSED static Hacl_Streaming_HMAC_agile_state
1823 *malloc_internal(Hacl_Streaming_HMAC_Definitions_index i, uint8_t *key)
1824 {
1825 KRML_CHECK_SIZE(sizeof (uint8_t),
1826 block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i))));
1827 uint8_t
1828 *buf =
1829 (uint8_t *)KRML_HOST_CALLOC(block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i))),
1830 sizeof (uint8_t));
1831 if (buf == NULL)
1832 {
1833 return NULL;
1834 }
1835 uint8_t *buf1 = buf;
1836 Hacl_Agile_Hash_state_s *s110 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i));
1837 option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__ block_state;
1838 if (s110 == NULL)
1839 {
1840 block_state =
1841 (
1842 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
1843 .tag = Hacl_Streaming_Types_None
1844 }
1845 );
1846 }
1847 else
1848 {
1849 Hacl_Agile_Hash_state_s *s21 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i));
1850 if (s21 == NULL)
1851 {
1852 KRML_HOST_FREE(s110);
1853 block_state =
1854 (
1855 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
1856 .tag = Hacl_Streaming_Types_None
1857 }
1858 );
1859 }
1860 else
1861 {
1862 block_state =
1863 (
1864 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
1865 .tag = Hacl_Streaming_Types_Some,
1866 .v = { .fst = dsnd__Hacl_Agile_Hash_impl_uint32_t(i), .snd = s110, .thd = s21 }
1867 }
1868 );
1869 }
1870 }
1871 if (block_state.tag == Hacl_Streaming_Types_None)
1872 {
1873 KRML_HOST_FREE(buf1);
1874 return NULL;
1875 }
1876 if (block_state.tag == Hacl_Streaming_Types_Some)
1877 {
1878 Hacl_Streaming_HMAC_Definitions_two_state block_state1 = block_state.v;
1879 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
1880 switch (k_)
1881 {
1882 case Hacl_Streaming_Types_None:
1883 {
1884 return NULL;
1885 }
1886 case Hacl_Streaming_Types_Some:
1887 {
1888 Hacl_Streaming_HMAC_agile_state
1889 s =
1890 {
1891 .block_state = block_state1,
1892 .buf = buf1,
1893 .total_len = (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i)))
1894 };
1895 Hacl_Streaming_HMAC_agile_state
1896 *p =
1897 (Hacl_Streaming_HMAC_agile_state *)KRML_HOST_MALLOC(sizeof (
1898 Hacl_Streaming_HMAC_agile_state
1899 ));
1900 if (p != NULL)
1901 {
1902 p[0U] = s;
1903 }
1904 if (p == NULL)
1905 {
1906 Hacl_Agile_Hash_state_s *s21 = block_state1.thd;
1907 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
1908 free_(s11);
1909 free_(s21);
1910 KRML_HOST_FREE(buf1);
1911 return NULL;
1912 }
1913 init0(key, buf1, block_state1);
1914 return p;
1915 }
1916 default:
1917 {
1918 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1919 KRML_HOST_EXIT(253U);
1920 }
1921 }
1922 }
1923 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1924 __FILE__,
1925 __LINE__,
1926 "unreachable (pattern matches are exhaustive in F*)");
1927 KRML_HOST_EXIT(255U);
1928 }
1929
1930 KRML_MAYBE_UNUSED static bool is_blake2b_256(Hacl_Agile_Hash_impl uu___)
1931 {
1932 switch (uu___)
1933 {
1934 case Hacl_Agile_Hash_Blake2B_256:
1935 {
1936 return true;
1937 }
1938 default:
1939 {
1940 return false;
1941 }
1942 }
1943 }
1944
1945 KRML_MAYBE_UNUSED static bool is_blake2s_128(Hacl_Agile_Hash_impl uu___)
1946 {
1947 switch (uu___)
1948 {
1949 case Hacl_Agile_Hash_Blake2S_128:
1950 {
1951 return true;
1952 }
1953 default:
1954 {
1955 return false;
1956 }
1957 }
1958 }
1959
1960 Hacl_Streaming_Types_error_code
1961 Hacl_Streaming_HMAC_malloc_(
1962 Hacl_Agile_Hash_impl impl,
1963 uint8_t *key,
1964 uint32_t key_length,
1965 Hacl_Streaming_HMAC_agile_state **dst
1966 )
1967 {
1968 KRML_MAYBE_UNUSED_VAR(key);
1969 KRML_MAYBE_UNUSED_VAR(key_length);
1970 KRML_MAYBE_UNUSED_VAR(dst);
1971 #if !HACL_CAN_COMPILE_VEC256
1972 if (is_blake2b_256(impl))
1973 {
1974 return Hacl_Streaming_Types_InvalidAlgorithm;
1975 }
1976 #endif
1977 #if !HACL_CAN_COMPILE_VEC128
1978 if (is_blake2s_128(impl))
1979 {
1980 return Hacl_Streaming_Types_InvalidAlgorithm;
1981 }
1982 #endif
1983 Hacl_Streaming_HMAC_agile_state
1984 *st =
1985 malloc_internal(((Hacl_Streaming_HMAC_Definitions_index){ .fst = impl, .snd = key_length }),
1986 key);
1987 if (st == NULL)
1988 {
1989 return Hacl_Streaming_Types_OutOfMemory;
1990 }
1991 *dst = st;
1992 return Hacl_Streaming_Types_Success;
1993 }
1994
1995 Hacl_Streaming_HMAC_Definitions_index
1996 Hacl_Streaming_HMAC_get_impl(Hacl_Streaming_HMAC_agile_state *s)
1997 {
1998 Hacl_Streaming_HMAC_Definitions_two_state block_state = (*s).block_state;
1999 return Hacl_Streaming_HMAC_index_of_state(block_state);
2000 }
2001
2002 static void reset_internal(Hacl_Streaming_HMAC_agile_state *state, uint8_t *key)
2003 {
2004 Hacl_Streaming_HMAC_agile_state scrut = *state;
2005 uint8_t *buf = scrut.buf;
2006 Hacl_Streaming_HMAC_Definitions_two_state block_state = scrut.block_state;
2007 Hacl_Streaming_HMAC_Definitions_index i1 = Hacl_Streaming_HMAC_index_of_state(block_state);
2008 KRML_MAYBE_UNUSED_VAR(i1);
2009 init0(key, buf, block_state);
2010 Hacl_Streaming_HMAC_agile_state
2011 tmp =
2012 {
2013 .block_state = block_state,
2014 .buf = buf,
2015 .total_len = (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)))
2016 };
2017 state[0U] = tmp;
2018 }
2019
2020 Hacl_Streaming_Types_error_code
2021 Hacl_Streaming_HMAC_reset(
2022 Hacl_Streaming_HMAC_agile_state *state,
2023 uint8_t *key,
2024 uint32_t key_length
2025 )
2026 {
2027 uint32_t k_len = Hacl_Streaming_HMAC_get_impl(state).snd;
2028 if (key_length != k_len)
2029 {
2030 return Hacl_Streaming_Types_InvalidLength;
2031 }
2032 reset_internal(state, key);
2033 return Hacl_Streaming_Types_Success;
2034 }
2035
2036 Hacl_Streaming_Types_error_code
2037 Hacl_Streaming_HMAC_update(
2038 Hacl_Streaming_HMAC_agile_state *state,
2039 uint8_t *chunk,
2040 uint32_t chunk_len
2041 )
2042 {
2043 Hacl_Streaming_HMAC_agile_state s = *state;
2044 Hacl_Streaming_HMAC_Definitions_two_state block_state = s.block_state;
2045 uint64_t total_len = s.total_len;
2046 Hacl_Streaming_HMAC_Definitions_index i1 = Hacl_Streaming_HMAC_index_of_state(block_state);
2047 if
2048 (
2049 (uint64_t)chunk_len >
2050 max_input_len64(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) - total_len
2051 )
2052 {
2053 return Hacl_Streaming_Types_MaximumLengthExceeded;
2054 }
2055 uint32_t sz;
2056 if
2057 (
2058 total_len % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0ULL
2059 && total_len > 0ULL
2060 )
2061 {
2062 sz = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2063 }
2064 else
2065 {
2066 sz =
2067 (uint32_t)(total_len %
2068 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2069 }
2070 if (chunk_len <= block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) - sz)
2071 {
2072 Hacl_Streaming_HMAC_agile_state s3 = *state;
2073 Hacl_Streaming_HMAC_Definitions_two_state block_state1 = s3.block_state;
2074 uint8_t *buf = s3.buf;
2075 uint64_t total_len1 = s3.total_len;
2076 uint32_t sz1;
2077 if
2078 (
2079 total_len1 % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0ULL
2080 && total_len1 > 0ULL
2081 )
2082 {
2083 sz1 = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2084 }
2085 else
2086 {
2087 sz1 =
2088 (uint32_t)(total_len1 %
2089 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2090 }
2091 uint8_t *buf2 = buf + sz1;
2092 memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
2093 uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
2094 *state =
2095 (
2096 (Hacl_Streaming_HMAC_agile_state){
2097 .block_state = block_state1,
2098 .buf = buf,
2099 .total_len = total_len2
2100 }
2101 );
2102 }
2103 else if (sz == 0U)
2104 {
2105 Hacl_Streaming_HMAC_agile_state s3 = *state;
2106 Hacl_Streaming_HMAC_Definitions_two_state block_state1 = s3.block_state;
2107 uint8_t *buf = s3.buf;
2108 uint64_t total_len1 = s3.total_len;
2109 uint32_t sz1;
2110 if
2111 (
2112 total_len1 % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0ULL
2113 && total_len1 > 0ULL
2114 )
2115 {
2116 sz1 = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2117 }
2118 else
2119 {
2120 sz1 =
2121 (uint32_t)(total_len1 %
2122 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2123 }
2124 if (!(sz1 == 0U))
2125 {
2126 uint64_t prevlen = total_len1 - (uint64_t)sz1;
2127 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
2128 update_multi(s11,
2129 prevlen,
2130 buf,
2131 block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2132 }
2133 uint32_t ite;
2134 if
2135 (
2136 (uint64_t)chunk_len %
2137 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)))
2138 == 0ULL
2139 && (uint64_t)chunk_len > 0ULL
2140 )
2141 {
2142 ite = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2143 }
2144 else
2145 {
2146 ite =
2147 (uint32_t)((uint64_t)chunk_len %
2148 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2149 }
2150 uint32_t
2151 n_blocks = (chunk_len - ite) / block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2152 uint32_t
2153 data1_len = n_blocks * block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2154 uint32_t data2_len = chunk_len - data1_len;
2155 uint8_t *data1 = chunk;
2156 uint8_t *data2 = chunk + data1_len;
2157 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
2158 update_multi(s11, total_len1, data1, data1_len);
2159 uint8_t *dst = buf;
2160 memcpy(dst, data2, data2_len * sizeof (uint8_t));
2161 *state =
2162 (
2163 (Hacl_Streaming_HMAC_agile_state){
2164 .block_state = block_state1,
2165 .buf = buf,
2166 .total_len = total_len1 + (uint64_t)chunk_len
2167 }
2168 );
2169 }
2170 else
2171 {
2172 uint32_t diff = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) - sz;
2173 uint8_t *chunk1 = chunk;
2174 uint8_t *chunk2 = chunk + diff;
2175 Hacl_Streaming_HMAC_agile_state s3 = *state;
2176 Hacl_Streaming_HMAC_Definitions_two_state block_state10 = s3.block_state;
2177 uint8_t *buf0 = s3.buf;
2178 uint64_t total_len10 = s3.total_len;
2179 uint32_t sz10;
2180 if
2181 (
2182 total_len10 % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) ==
2183 0ULL
2184 && total_len10 > 0ULL
2185 )
2186 {
2187 sz10 = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2188 }
2189 else
2190 {
2191 sz10 =
2192 (uint32_t)(total_len10 %
2193 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2194 }
2195 uint8_t *buf2 = buf0 + sz10;
2196 memcpy(buf2, chunk1, diff * sizeof (uint8_t));
2197 uint64_t total_len2 = total_len10 + (uint64_t)diff;
2198 *state =
2199 (
2200 (Hacl_Streaming_HMAC_agile_state){
2201 .block_state = block_state10,
2202 .buf = buf0,
2203 .total_len = total_len2
2204 }
2205 );
2206 Hacl_Streaming_HMAC_agile_state s30 = *state;
2207 Hacl_Streaming_HMAC_Definitions_two_state block_state1 = s30.block_state;
2208 uint8_t *buf = s30.buf;
2209 uint64_t total_len1 = s30.total_len;
2210 uint32_t sz1;
2211 if
2212 (
2213 total_len1 % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0ULL
2214 && total_len1 > 0ULL
2215 )
2216 {
2217 sz1 = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2218 }
2219 else
2220 {
2221 sz1 =
2222 (uint32_t)(total_len1 %
2223 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2224 }
2225 if (!(sz1 == 0U))
2226 {
2227 uint64_t prevlen = total_len1 - (uint64_t)sz1;
2228 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
2229 update_multi(s11,
2230 prevlen,
2231 buf,
2232 block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2233 }
2234 uint32_t ite;
2235 if
2236 (
2237 (uint64_t)(chunk_len - diff) %
2238 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)))
2239 == 0ULL
2240 && (uint64_t)(chunk_len - diff) > 0ULL
2241 )
2242 {
2243 ite = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2244 }
2245 else
2246 {
2247 ite =
2248 (uint32_t)((uint64_t)(chunk_len - diff) %
2249 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2250 }
2251 uint32_t
2252 n_blocks =
2253 (chunk_len - diff - ite) / block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2254 uint32_t
2255 data1_len = n_blocks * block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2256 uint32_t data2_len = chunk_len - diff - data1_len;
2257 uint8_t *data1 = chunk2;
2258 uint8_t *data2 = chunk2 + data1_len;
2259 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
2260 update_multi(s11, total_len1, data1, data1_len);
2261 uint8_t *dst = buf;
2262 memcpy(dst, data2, data2_len * sizeof (uint8_t));
2263 *state =
2264 (
2265 (Hacl_Streaming_HMAC_agile_state){
2266 .block_state = block_state1,
2267 .buf = buf,
2268 .total_len = total_len1 + (uint64_t)(chunk_len - diff)
2269 }
2270 );
2271 }
2272 return Hacl_Streaming_Types_Success;
2273 }
2274
2275 typedef struct
2276 ___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s____uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s___s
2277 {
2278 Hacl_Streaming_HMAC_Definitions_two_state fst;
2279 Hacl_Streaming_HMAC_Definitions_two_state snd;
2280 }
2281 ___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s____uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__;
2282
2283 Hacl_Streaming_Types_error_code
2284 Hacl_Streaming_HMAC_digest(
2285 Hacl_Streaming_HMAC_agile_state *state,
2286 uint8_t *output,
2287 uint32_t digest_length
2288 )
2289 {
2290 KRML_MAYBE_UNUSED_VAR(digest_length);
2291 Hacl_Streaming_HMAC_Definitions_two_state block_state0 = (*state).block_state;
2292 Hacl_Streaming_HMAC_Definitions_index i1 = Hacl_Streaming_HMAC_index_of_state(block_state0);
2293 Hacl_Streaming_HMAC_agile_state scrut0 = *state;
2294 Hacl_Streaming_HMAC_Definitions_two_state block_state = scrut0.block_state;
2295 uint8_t *buf_ = scrut0.buf;
2296 uint64_t total_len = scrut0.total_len;
2297 uint32_t r;
2298 if
2299 (
2300 total_len % (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0ULL
2301 && total_len > 0ULL
2302 )
2303 {
2304 r = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2305 }
2306 else
2307 {
2308 r =
2309 (uint32_t)(total_len %
2310 (uint64_t)block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2311 }
2312 uint8_t *buf_1 = buf_;
2313 Hacl_Agile_Hash_state_s *s110 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i1));
2314 option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__ tmp_block_state;
2315 if (s110 == NULL)
2316 {
2317 tmp_block_state =
2318 (
2319 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2320 .tag = Hacl_Streaming_Types_None
2321 }
2322 );
2323 }
2324 else
2325 {
2326 Hacl_Agile_Hash_state_s *s21 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i1));
2327 if (s21 == NULL)
2328 {
2329 KRML_HOST_FREE(s110);
2330 tmp_block_state =
2331 (
2332 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2333 .tag = Hacl_Streaming_Types_None
2334 }
2335 );
2336 }
2337 else
2338 {
2339 tmp_block_state =
2340 (
2341 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2342 .tag = Hacl_Streaming_Types_Some,
2343 .v = { .fst = dsnd__Hacl_Agile_Hash_impl_uint32_t(i1), .snd = s110, .thd = s21 }
2344 }
2345 );
2346 }
2347 }
2348 if (tmp_block_state.tag == Hacl_Streaming_Types_None)
2349 {
2350 return Hacl_Streaming_Types_OutOfMemory;
2351 }
2352 if (tmp_block_state.tag == Hacl_Streaming_Types_Some)
2353 {
2354 Hacl_Streaming_HMAC_Definitions_two_state tmp_block_state1 = tmp_block_state.v;
2355 ___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s____uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__
2356 scrut = { .fst = block_state, .snd = tmp_block_state1 };
2357 Hacl_Agile_Hash_state_s *s2_ = scrut.snd.thd;
2358 Hacl_Agile_Hash_state_s *s1_ = scrut.snd.snd;
2359 Hacl_Agile_Hash_state_s *s21 = scrut.fst.thd;
2360 Hacl_Agile_Hash_state_s *s111 = scrut.fst.snd;
2361 copy(s111, s1_);
2362 copy(s21, s2_);
2363 uint64_t prev_len = total_len - (uint64_t)r;
2364 uint32_t ite;
2365 if (r % block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) == 0U && r > 0U)
2366 {
2367 ite = block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2368 }
2369 else
2370 {
2371 ite = r % block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1)));
2372 }
2373 uint8_t *buf_last = buf_1 + r - ite;
2374 uint8_t *buf_multi = buf_1;
2375 Hacl_Agile_Hash_state_s *s112 = tmp_block_state1.snd;
2376 update_multi(s112, prev_len, buf_multi, 0U);
2377 uint64_t prev_len_last = total_len - (uint64_t)r;
2378 Hacl_Agile_Hash_state_s *s113 = tmp_block_state1.snd;
2379 update_last(s113, prev_len_last, buf_last, r);
2380 finish0(tmp_block_state1, output);
2381 Hacl_Agile_Hash_state_s *s210 = tmp_block_state1.thd;
2382 Hacl_Agile_Hash_state_s *s11 = tmp_block_state1.snd;
2383 free_(s11);
2384 free_(s210);
2385 return Hacl_Streaming_Types_Success;
2386 }
2387 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
2388 __FILE__,
2389 __LINE__,
2390 "unreachable (pattern matches are exhaustive in F*)");
2391 KRML_HOST_EXIT(255U);
2392 }
2393
2394 void Hacl_Streaming_HMAC_free(Hacl_Streaming_HMAC_agile_state *state)
2395 {
2396 Hacl_Streaming_HMAC_Definitions_two_state block_state0 = (*state).block_state;
2397 Hacl_Streaming_HMAC_Definitions_index i1 = Hacl_Streaming_HMAC_index_of_state(block_state0);
2398 KRML_MAYBE_UNUSED_VAR(i1);
2399 Hacl_Streaming_HMAC_agile_state scrut = *state;
2400 uint8_t *buf = scrut.buf;
2401 Hacl_Streaming_HMAC_Definitions_two_state block_state = scrut.block_state;
2402 Hacl_Agile_Hash_state_s *s21 = block_state.thd;
2403 Hacl_Agile_Hash_state_s *s11 = block_state.snd;
2404 free_(s11);
2405 free_(s21);
2406 KRML_HOST_FREE(buf);
2407 KRML_HOST_FREE(state);
2408 }
2409
2410 Hacl_Streaming_HMAC_agile_state
2411 *Hacl_Streaming_HMAC_copy(Hacl_Streaming_HMAC_agile_state *state)
2412 {
2413 Hacl_Streaming_HMAC_agile_state scrut0 = *state;
2414 Hacl_Streaming_HMAC_Definitions_two_state block_state0 = scrut0.block_state;
2415 uint8_t *buf0 = scrut0.buf;
2416 uint64_t total_len0 = scrut0.total_len;
2417 Hacl_Streaming_HMAC_Definitions_index i1 = Hacl_Streaming_HMAC_index_of_state(block_state0);
2418 KRML_CHECK_SIZE(sizeof (uint8_t),
2419 block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))));
2420 uint8_t
2421 *buf =
2422 (uint8_t *)KRML_HOST_CALLOC(block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))),
2423 sizeof (uint8_t));
2424 if (buf == NULL)
2425 {
2426 return NULL;
2427 }
2428 memcpy(buf,
2429 buf0,
2430 block_len(alg_of_impl(dfst__Hacl_Agile_Hash_impl_uint32_t(i1))) * sizeof (uint8_t));
2431 Hacl_Agile_Hash_state_s *s110 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i1));
2432 option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__ block_state;
2433 if (s110 == NULL)
2434 {
2435 block_state =
2436 (
2437 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2438 .tag = Hacl_Streaming_Types_None
2439 }
2440 );
2441 }
2442 else
2443 {
2444 Hacl_Agile_Hash_state_s *s21 = malloc_(dfst__Hacl_Agile_Hash_impl_uint32_t(i1));
2445 if (s21 == NULL)
2446 {
2447 KRML_HOST_FREE(s110);
2448 block_state =
2449 (
2450 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2451 .tag = Hacl_Streaming_Types_None
2452 }
2453 );
2454 }
2455 else
2456 {
2457 block_state =
2458 (
2459 (option___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__){
2460 .tag = Hacl_Streaming_Types_Some,
2461 .v = { .fst = dsnd__Hacl_Agile_Hash_impl_uint32_t(i1), .snd = s110, .thd = s21 }
2462 }
2463 );
2464 }
2465 }
2466 if (block_state.tag == Hacl_Streaming_Types_None)
2467 {
2468 KRML_HOST_FREE(buf);
2469 return NULL;
2470 }
2471 if (block_state.tag == Hacl_Streaming_Types_Some)
2472 {
2473 Hacl_Streaming_HMAC_Definitions_two_state block_state1 = block_state.v;
2474 ___uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s____uint32_t____Hacl_Agile_Hash_state_s_____Hacl_Agile_Hash_state_s__
2475 scrut = { .fst = block_state0, .snd = block_state1 };
2476 Hacl_Agile_Hash_state_s *s2_ = scrut.snd.thd;
2477 Hacl_Agile_Hash_state_s *s1_ = scrut.snd.snd;
2478 Hacl_Agile_Hash_state_s *s21 = scrut.fst.thd;
2479 Hacl_Agile_Hash_state_s *s111 = scrut.fst.snd;
2480 copy(s111, s1_);
2481 copy(s21, s2_);
2482 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
2483 switch (k_)
2484 {
2485 case Hacl_Streaming_Types_None:
2486 {
2487 return NULL;
2488 }
2489 case Hacl_Streaming_Types_Some:
2490 {
2491 Hacl_Streaming_HMAC_agile_state
2492 s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 };
2493 Hacl_Streaming_HMAC_agile_state
2494 *p =
2495 (Hacl_Streaming_HMAC_agile_state *)KRML_HOST_MALLOC(sizeof (
2496 Hacl_Streaming_HMAC_agile_state
2497 ));
2498 if (p != NULL)
2499 {
2500 p[0U] = s;
2501 }
2502 if (p == NULL)
2503 {
2504 Hacl_Agile_Hash_state_s *s210 = block_state1.thd;
2505 Hacl_Agile_Hash_state_s *s11 = block_state1.snd;
2506 free_(s11);
2507 free_(s210);
2508 KRML_HOST_FREE(buf);
2509 return NULL;
2510 }
2511 return p;
2512 }
2513 default:
2514 {
2515 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
2516 KRML_HOST_EXIT(253U);
2517 }
2518 }
2519 }
2520 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
2521 __FILE__,
2522 __LINE__,
2523 "unreachable (pattern matches are exhaustive in F*)");
2524 KRML_HOST_EXIT(255U);
2525 }
2526