Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_hacl/Hacl_Hash_SHA2.c
12 views
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_SHA2.h"
27
28
29
30
void Hacl_SHA2_Scalar32_sha256_init(uint32_t *hash)
31
{
32
KRML_MAYBE_FOR8(i,
33
(uint32_t)0U,
34
(uint32_t)8U,
35
(uint32_t)1U,
36
uint32_t *os = hash;
37
uint32_t x = Hacl_Impl_SHA2_Generic_h256[i];
38
os[i] = x;);
39
}
40
41
static inline void sha256_update(uint8_t *b, uint32_t *hash)
42
{
43
uint32_t hash_old[8U] = { 0U };
44
uint32_t ws[16U] = { 0U };
45
memcpy(hash_old, hash, (uint32_t)8U * sizeof (uint32_t));
46
uint8_t *b10 = b;
47
uint32_t u = load32_be(b10);
48
ws[0U] = u;
49
uint32_t u0 = load32_be(b10 + (uint32_t)4U);
50
ws[1U] = u0;
51
uint32_t u1 = load32_be(b10 + (uint32_t)8U);
52
ws[2U] = u1;
53
uint32_t u2 = load32_be(b10 + (uint32_t)12U);
54
ws[3U] = u2;
55
uint32_t u3 = load32_be(b10 + (uint32_t)16U);
56
ws[4U] = u3;
57
uint32_t u4 = load32_be(b10 + (uint32_t)20U);
58
ws[5U] = u4;
59
uint32_t u5 = load32_be(b10 + (uint32_t)24U);
60
ws[6U] = u5;
61
uint32_t u6 = load32_be(b10 + (uint32_t)28U);
62
ws[7U] = u6;
63
uint32_t u7 = load32_be(b10 + (uint32_t)32U);
64
ws[8U] = u7;
65
uint32_t u8 = load32_be(b10 + (uint32_t)36U);
66
ws[9U] = u8;
67
uint32_t u9 = load32_be(b10 + (uint32_t)40U);
68
ws[10U] = u9;
69
uint32_t u10 = load32_be(b10 + (uint32_t)44U);
70
ws[11U] = u10;
71
uint32_t u11 = load32_be(b10 + (uint32_t)48U);
72
ws[12U] = u11;
73
uint32_t u12 = load32_be(b10 + (uint32_t)52U);
74
ws[13U] = u12;
75
uint32_t u13 = load32_be(b10 + (uint32_t)56U);
76
ws[14U] = u13;
77
uint32_t u14 = load32_be(b10 + (uint32_t)60U);
78
ws[15U] = u14;
79
KRML_MAYBE_FOR4(i0,
80
(uint32_t)0U,
81
(uint32_t)4U,
82
(uint32_t)1U,
83
KRML_MAYBE_FOR16(i,
84
(uint32_t)0U,
85
(uint32_t)16U,
86
(uint32_t)1U,
87
uint32_t k_t = Hacl_Impl_SHA2_Generic_k224_256[(uint32_t)16U * i0 + i];
88
uint32_t ws_t = ws[i];
89
uint32_t a0 = hash[0U];
90
uint32_t b0 = hash[1U];
91
uint32_t c0 = hash[2U];
92
uint32_t d0 = hash[3U];
93
uint32_t e0 = hash[4U];
94
uint32_t f0 = hash[5U];
95
uint32_t g0 = hash[6U];
96
uint32_t h02 = hash[7U];
97
uint32_t k_e_t = k_t;
98
uint32_t
99
t1 =
100
h02
101
+
102
((e0 << (uint32_t)26U | e0 >> (uint32_t)6U)
103
^
104
((e0 << (uint32_t)21U | e0 >> (uint32_t)11U)
105
^ (e0 << (uint32_t)7U | e0 >> (uint32_t)25U)))
106
+ ((e0 & f0) ^ (~e0 & g0))
107
+ k_e_t
108
+ ws_t;
109
uint32_t
110
t2 =
111
((a0 << (uint32_t)30U | a0 >> (uint32_t)2U)
112
^
113
((a0 << (uint32_t)19U | a0 >> (uint32_t)13U)
114
^ (a0 << (uint32_t)10U | a0 >> (uint32_t)22U)))
115
+ ((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0)));
116
uint32_t a1 = t1 + t2;
117
uint32_t b1 = a0;
118
uint32_t c1 = b0;
119
uint32_t d1 = c0;
120
uint32_t e1 = d0 + t1;
121
uint32_t f1 = e0;
122
uint32_t g1 = f0;
123
uint32_t h12 = g0;
124
hash[0U] = a1;
125
hash[1U] = b1;
126
hash[2U] = c1;
127
hash[3U] = d1;
128
hash[4U] = e1;
129
hash[5U] = f1;
130
hash[6U] = g1;
131
hash[7U] = h12;);
132
if (i0 < (uint32_t)3U)
133
{
134
KRML_MAYBE_FOR16(i,
135
(uint32_t)0U,
136
(uint32_t)16U,
137
(uint32_t)1U,
138
uint32_t t16 = ws[i];
139
uint32_t t15 = ws[(i + (uint32_t)1U) % (uint32_t)16U];
140
uint32_t t7 = ws[(i + (uint32_t)9U) % (uint32_t)16U];
141
uint32_t t2 = ws[(i + (uint32_t)14U) % (uint32_t)16U];
142
uint32_t
143
s1 =
144
(t2 << (uint32_t)15U | t2 >> (uint32_t)17U)
145
^ ((t2 << (uint32_t)13U | t2 >> (uint32_t)19U) ^ t2 >> (uint32_t)10U);
146
uint32_t
147
s0 =
148
(t15 << (uint32_t)25U | t15 >> (uint32_t)7U)
149
^ ((t15 << (uint32_t)14U | t15 >> (uint32_t)18U) ^ t15 >> (uint32_t)3U);
150
ws[i] = s1 + t7 + s0 + t16;);
151
});
152
KRML_MAYBE_FOR8(i,
153
(uint32_t)0U,
154
(uint32_t)8U,
155
(uint32_t)1U,
156
uint32_t *os = hash;
157
uint32_t x = hash[i] + hash_old[i];
158
os[i] = x;);
159
}
160
161
void Hacl_SHA2_Scalar32_sha256_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st)
162
{
163
uint32_t blocks = len / (uint32_t)64U;
164
for (uint32_t i = (uint32_t)0U; i < blocks; i++)
165
{
166
uint8_t *b0 = b;
167
uint8_t *mb = b0 + i * (uint32_t)64U;
168
sha256_update(mb, st);
169
}
170
}
171
172
void
173
Hacl_SHA2_Scalar32_sha256_update_last(
174
uint64_t totlen,
175
uint32_t len,
176
uint8_t *b,
177
uint32_t *hash
178
)
179
{
180
uint32_t blocks;
181
if (len + (uint32_t)8U + (uint32_t)1U <= (uint32_t)64U)
182
{
183
blocks = (uint32_t)1U;
184
}
185
else
186
{
187
blocks = (uint32_t)2U;
188
}
189
uint32_t fin = blocks * (uint32_t)64U;
190
uint8_t last[128U] = { 0U };
191
uint8_t totlen_buf[8U] = { 0U };
192
uint64_t total_len_bits = totlen << (uint32_t)3U;
193
store64_be(totlen_buf, total_len_bits);
194
uint8_t *b0 = b;
195
memcpy(last, b0, len * sizeof (uint8_t));
196
last[len] = (uint8_t)0x80U;
197
memcpy(last + fin - (uint32_t)8U, totlen_buf, (uint32_t)8U * sizeof (uint8_t));
198
uint8_t *last00 = last;
199
uint8_t *last10 = last + (uint32_t)64U;
200
uint8_t *l0 = last00;
201
uint8_t *l1 = last10;
202
uint8_t *lb0 = l0;
203
uint8_t *lb1 = l1;
204
uint8_t *last0 = lb0;
205
uint8_t *last1 = lb1;
206
sha256_update(last0, hash);
207
if (blocks > (uint32_t)1U)
208
{
209
sha256_update(last1, hash);
210
return;
211
}
212
}
213
214
void Hacl_SHA2_Scalar32_sha256_finish(uint32_t *st, uint8_t *h)
215
{
216
uint8_t hbuf[32U] = { 0U };
217
KRML_MAYBE_FOR8(i,
218
(uint32_t)0U,
219
(uint32_t)8U,
220
(uint32_t)1U,
221
store32_be(hbuf + i * (uint32_t)4U, st[i]););
222
memcpy(h, hbuf, (uint32_t)32U * sizeof (uint8_t));
223
}
224
225
void Hacl_SHA2_Scalar32_sha224_init(uint32_t *hash)
226
{
227
KRML_MAYBE_FOR8(i,
228
(uint32_t)0U,
229
(uint32_t)8U,
230
(uint32_t)1U,
231
uint32_t *os = hash;
232
uint32_t x = Hacl_Impl_SHA2_Generic_h224[i];
233
os[i] = x;);
234
}
235
236
static inline void sha224_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st)
237
{
238
Hacl_SHA2_Scalar32_sha256_update_nblocks(len, b, st);
239
}
240
241
void
242
Hacl_SHA2_Scalar32_sha224_update_last(uint64_t totlen, uint32_t len, uint8_t *b, uint32_t *st)
243
{
244
Hacl_SHA2_Scalar32_sha256_update_last(totlen, len, b, st);
245
}
246
247
void Hacl_SHA2_Scalar32_sha224_finish(uint32_t *st, uint8_t *h)
248
{
249
uint8_t hbuf[32U] = { 0U };
250
KRML_MAYBE_FOR8(i,
251
(uint32_t)0U,
252
(uint32_t)8U,
253
(uint32_t)1U,
254
store32_be(hbuf + i * (uint32_t)4U, st[i]););
255
memcpy(h, hbuf, (uint32_t)28U * sizeof (uint8_t));
256
}
257
258
void Hacl_SHA2_Scalar32_sha512_init(uint64_t *hash)
259
{
260
KRML_MAYBE_FOR8(i,
261
(uint32_t)0U,
262
(uint32_t)8U,
263
(uint32_t)1U,
264
uint64_t *os = hash;
265
uint64_t x = Hacl_Impl_SHA2_Generic_h512[i];
266
os[i] = x;);
267
}
268
269
static inline void sha512_update(uint8_t *b, uint64_t *hash)
270
{
271
uint64_t hash_old[8U] = { 0U };
272
uint64_t ws[16U] = { 0U };
273
memcpy(hash_old, hash, (uint32_t)8U * sizeof (uint64_t));
274
uint8_t *b10 = b;
275
uint64_t u = load64_be(b10);
276
ws[0U] = u;
277
uint64_t u0 = load64_be(b10 + (uint32_t)8U);
278
ws[1U] = u0;
279
uint64_t u1 = load64_be(b10 + (uint32_t)16U);
280
ws[2U] = u1;
281
uint64_t u2 = load64_be(b10 + (uint32_t)24U);
282
ws[3U] = u2;
283
uint64_t u3 = load64_be(b10 + (uint32_t)32U);
284
ws[4U] = u3;
285
uint64_t u4 = load64_be(b10 + (uint32_t)40U);
286
ws[5U] = u4;
287
uint64_t u5 = load64_be(b10 + (uint32_t)48U);
288
ws[6U] = u5;
289
uint64_t u6 = load64_be(b10 + (uint32_t)56U);
290
ws[7U] = u6;
291
uint64_t u7 = load64_be(b10 + (uint32_t)64U);
292
ws[8U] = u7;
293
uint64_t u8 = load64_be(b10 + (uint32_t)72U);
294
ws[9U] = u8;
295
uint64_t u9 = load64_be(b10 + (uint32_t)80U);
296
ws[10U] = u9;
297
uint64_t u10 = load64_be(b10 + (uint32_t)88U);
298
ws[11U] = u10;
299
uint64_t u11 = load64_be(b10 + (uint32_t)96U);
300
ws[12U] = u11;
301
uint64_t u12 = load64_be(b10 + (uint32_t)104U);
302
ws[13U] = u12;
303
uint64_t u13 = load64_be(b10 + (uint32_t)112U);
304
ws[14U] = u13;
305
uint64_t u14 = load64_be(b10 + (uint32_t)120U);
306
ws[15U] = u14;
307
KRML_MAYBE_FOR5(i0,
308
(uint32_t)0U,
309
(uint32_t)5U,
310
(uint32_t)1U,
311
KRML_MAYBE_FOR16(i,
312
(uint32_t)0U,
313
(uint32_t)16U,
314
(uint32_t)1U,
315
uint64_t k_t = Hacl_Impl_SHA2_Generic_k384_512[(uint32_t)16U * i0 + i];
316
uint64_t ws_t = ws[i];
317
uint64_t a0 = hash[0U];
318
uint64_t b0 = hash[1U];
319
uint64_t c0 = hash[2U];
320
uint64_t d0 = hash[3U];
321
uint64_t e0 = hash[4U];
322
uint64_t f0 = hash[5U];
323
uint64_t g0 = hash[6U];
324
uint64_t h02 = hash[7U];
325
uint64_t k_e_t = k_t;
326
uint64_t
327
t1 =
328
h02
329
+
330
((e0 << (uint32_t)50U | e0 >> (uint32_t)14U)
331
^
332
((e0 << (uint32_t)46U | e0 >> (uint32_t)18U)
333
^ (e0 << (uint32_t)23U | e0 >> (uint32_t)41U)))
334
+ ((e0 & f0) ^ (~e0 & g0))
335
+ k_e_t
336
+ ws_t;
337
uint64_t
338
t2 =
339
((a0 << (uint32_t)36U | a0 >> (uint32_t)28U)
340
^
341
((a0 << (uint32_t)30U | a0 >> (uint32_t)34U)
342
^ (a0 << (uint32_t)25U | a0 >> (uint32_t)39U)))
343
+ ((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0)));
344
uint64_t a1 = t1 + t2;
345
uint64_t b1 = a0;
346
uint64_t c1 = b0;
347
uint64_t d1 = c0;
348
uint64_t e1 = d0 + t1;
349
uint64_t f1 = e0;
350
uint64_t g1 = f0;
351
uint64_t h12 = g0;
352
hash[0U] = a1;
353
hash[1U] = b1;
354
hash[2U] = c1;
355
hash[3U] = d1;
356
hash[4U] = e1;
357
hash[5U] = f1;
358
hash[6U] = g1;
359
hash[7U] = h12;);
360
if (i0 < (uint32_t)4U)
361
{
362
KRML_MAYBE_FOR16(i,
363
(uint32_t)0U,
364
(uint32_t)16U,
365
(uint32_t)1U,
366
uint64_t t16 = ws[i];
367
uint64_t t15 = ws[(i + (uint32_t)1U) % (uint32_t)16U];
368
uint64_t t7 = ws[(i + (uint32_t)9U) % (uint32_t)16U];
369
uint64_t t2 = ws[(i + (uint32_t)14U) % (uint32_t)16U];
370
uint64_t
371
s1 =
372
(t2 << (uint32_t)45U | t2 >> (uint32_t)19U)
373
^ ((t2 << (uint32_t)3U | t2 >> (uint32_t)61U) ^ t2 >> (uint32_t)6U);
374
uint64_t
375
s0 =
376
(t15 << (uint32_t)63U | t15 >> (uint32_t)1U)
377
^ ((t15 << (uint32_t)56U | t15 >> (uint32_t)8U) ^ t15 >> (uint32_t)7U);
378
ws[i] = s1 + t7 + s0 + t16;);
379
});
380
KRML_MAYBE_FOR8(i,
381
(uint32_t)0U,
382
(uint32_t)8U,
383
(uint32_t)1U,
384
uint64_t *os = hash;
385
uint64_t x = hash[i] + hash_old[i];
386
os[i] = x;);
387
}
388
389
void Hacl_SHA2_Scalar32_sha512_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st)
390
{
391
uint32_t blocks = len / (uint32_t)128U;
392
for (uint32_t i = (uint32_t)0U; i < blocks; i++)
393
{
394
uint8_t *b0 = b;
395
uint8_t *mb = b0 + i * (uint32_t)128U;
396
sha512_update(mb, st);
397
}
398
}
399
400
void
401
Hacl_SHA2_Scalar32_sha512_update_last(
402
FStar_UInt128_uint128 totlen,
403
uint32_t len,
404
uint8_t *b,
405
uint64_t *hash
406
)
407
{
408
uint32_t blocks;
409
if (len + (uint32_t)16U + (uint32_t)1U <= (uint32_t)128U)
410
{
411
blocks = (uint32_t)1U;
412
}
413
else
414
{
415
blocks = (uint32_t)2U;
416
}
417
uint32_t fin = blocks * (uint32_t)128U;
418
uint8_t last[256U] = { 0U };
419
uint8_t totlen_buf[16U] = { 0U };
420
FStar_UInt128_uint128 total_len_bits = FStar_UInt128_shift_left(totlen, (uint32_t)3U);
421
store128_be(totlen_buf, total_len_bits);
422
uint8_t *b0 = b;
423
memcpy(last, b0, len * sizeof (uint8_t));
424
last[len] = (uint8_t)0x80U;
425
memcpy(last + fin - (uint32_t)16U, totlen_buf, (uint32_t)16U * sizeof (uint8_t));
426
uint8_t *last00 = last;
427
uint8_t *last10 = last + (uint32_t)128U;
428
uint8_t *l0 = last00;
429
uint8_t *l1 = last10;
430
uint8_t *lb0 = l0;
431
uint8_t *lb1 = l1;
432
uint8_t *last0 = lb0;
433
uint8_t *last1 = lb1;
434
sha512_update(last0, hash);
435
if (blocks > (uint32_t)1U)
436
{
437
sha512_update(last1, hash);
438
return;
439
}
440
}
441
442
void Hacl_SHA2_Scalar32_sha512_finish(uint64_t *st, uint8_t *h)
443
{
444
uint8_t hbuf[64U] = { 0U };
445
KRML_MAYBE_FOR8(i,
446
(uint32_t)0U,
447
(uint32_t)8U,
448
(uint32_t)1U,
449
store64_be(hbuf + i * (uint32_t)8U, st[i]););
450
memcpy(h, hbuf, (uint32_t)64U * sizeof (uint8_t));
451
}
452
453
void Hacl_SHA2_Scalar32_sha384_init(uint64_t *hash)
454
{
455
KRML_MAYBE_FOR8(i,
456
(uint32_t)0U,
457
(uint32_t)8U,
458
(uint32_t)1U,
459
uint64_t *os = hash;
460
uint64_t x = Hacl_Impl_SHA2_Generic_h384[i];
461
os[i] = x;);
462
}
463
464
void Hacl_SHA2_Scalar32_sha384_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st)
465
{
466
Hacl_SHA2_Scalar32_sha512_update_nblocks(len, b, st);
467
}
468
469
void
470
Hacl_SHA2_Scalar32_sha384_update_last(
471
FStar_UInt128_uint128 totlen,
472
uint32_t len,
473
uint8_t *b,
474
uint64_t *st
475
)
476
{
477
Hacl_SHA2_Scalar32_sha512_update_last(totlen, len, b, st);
478
}
479
480
void Hacl_SHA2_Scalar32_sha384_finish(uint64_t *st, uint8_t *h)
481
{
482
uint8_t hbuf[64U] = { 0U };
483
KRML_MAYBE_FOR8(i,
484
(uint32_t)0U,
485
(uint32_t)8U,
486
(uint32_t)1U,
487
store64_be(hbuf + i * (uint32_t)8U, st[i]););
488
memcpy(h, hbuf, (uint32_t)48U * sizeof (uint8_t));
489
}
490
491
/**
492
Allocate initial state for the SHA2_256 hash. The state is to be freed by
493
calling `free_256`.
494
*/
495
Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_create_in_256(void)
496
{
497
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
498
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
499
Hacl_Streaming_MD_state_32
500
s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
501
Hacl_Streaming_MD_state_32
502
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
503
p[0U] = s;
504
Hacl_SHA2_Scalar32_sha256_init(block_state);
505
return p;
506
}
507
508
/**
509
Copies the state passed as argument into a newly allocated state (deep copy).
510
The state is to be freed by calling `free_256`. Cloning the state this way is
511
useful, for instance, if your control-flow diverges and you need to feed
512
more (different) data into the hash in each branch.
513
*/
514
Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_copy_256(Hacl_Streaming_MD_state_32 *s0)
515
{
516
Hacl_Streaming_MD_state_32 scrut = *s0;
517
uint32_t *block_state0 = scrut.block_state;
518
uint8_t *buf0 = scrut.buf;
519
uint64_t total_len0 = scrut.total_len;
520
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
521
memcpy(buf, buf0, (uint32_t)64U * sizeof (uint8_t));
522
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
523
memcpy(block_state, block_state0, (uint32_t)8U * sizeof (uint32_t));
524
Hacl_Streaming_MD_state_32
525
s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
526
Hacl_Streaming_MD_state_32
527
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
528
p[0U] = s;
529
return p;
530
}
531
532
/**
533
Reset an existing state to the initial hash state with empty data.
534
*/
535
void Hacl_Streaming_SHA2_init_256(Hacl_Streaming_MD_state_32 *s)
536
{
537
Hacl_Streaming_MD_state_32 scrut = *s;
538
uint8_t *buf = scrut.buf;
539
uint32_t *block_state = scrut.block_state;
540
Hacl_SHA2_Scalar32_sha256_init(block_state);
541
Hacl_Streaming_MD_state_32
542
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
543
s[0U] = tmp;
544
}
545
546
static inline Hacl_Streaming_Types_error_code
547
update_224_256(Hacl_Streaming_MD_state_32 *p, uint8_t *data, uint32_t len)
548
{
549
Hacl_Streaming_MD_state_32 s = *p;
550
uint64_t total_len = s.total_len;
551
if ((uint64_t)len > (uint64_t)2305843009213693951U - total_len)
552
{
553
return Hacl_Streaming_Types_MaximumLengthExceeded;
554
}
555
uint32_t sz;
556
if (total_len % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len > (uint64_t)0U)
557
{
558
sz = (uint32_t)64U;
559
}
560
else
561
{
562
sz = (uint32_t)(total_len % (uint64_t)(uint32_t)64U);
563
}
564
if (len <= (uint32_t)64U - sz)
565
{
566
Hacl_Streaming_MD_state_32 s1 = *p;
567
uint32_t *block_state1 = s1.block_state;
568
uint8_t *buf = s1.buf;
569
uint64_t total_len1 = s1.total_len;
570
uint32_t sz1;
571
if (total_len1 % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len1 > (uint64_t)0U)
572
{
573
sz1 = (uint32_t)64U;
574
}
575
else
576
{
577
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)64U);
578
}
579
uint8_t *buf2 = buf + sz1;
580
memcpy(buf2, data, len * sizeof (uint8_t));
581
uint64_t total_len2 = total_len1 + (uint64_t)len;
582
*p
583
=
584
(
585
(Hacl_Streaming_MD_state_32){
586
.block_state = block_state1,
587
.buf = buf,
588
.total_len = total_len2
589
}
590
);
591
}
592
else if (sz == (uint32_t)0U)
593
{
594
Hacl_Streaming_MD_state_32 s1 = *p;
595
uint32_t *block_state1 = s1.block_state;
596
uint8_t *buf = s1.buf;
597
uint64_t total_len1 = s1.total_len;
598
uint32_t sz1;
599
if (total_len1 % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len1 > (uint64_t)0U)
600
{
601
sz1 = (uint32_t)64U;
602
}
603
else
604
{
605
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)64U);
606
}
607
if (!(sz1 == (uint32_t)0U))
608
{
609
Hacl_SHA2_Scalar32_sha256_update_nblocks((uint32_t)64U, buf, block_state1);
610
}
611
uint32_t ite;
612
if ((uint64_t)len % (uint64_t)(uint32_t)64U == (uint64_t)0U && (uint64_t)len > (uint64_t)0U)
613
{
614
ite = (uint32_t)64U;
615
}
616
else
617
{
618
ite = (uint32_t)((uint64_t)len % (uint64_t)(uint32_t)64U);
619
}
620
uint32_t n_blocks = (len - ite) / (uint32_t)64U;
621
uint32_t data1_len = n_blocks * (uint32_t)64U;
622
uint32_t data2_len = len - data1_len;
623
uint8_t *data1 = data;
624
uint8_t *data2 = data + data1_len;
625
Hacl_SHA2_Scalar32_sha256_update_nblocks(data1_len / (uint32_t)64U * (uint32_t)64U,
626
data1,
627
block_state1);
628
uint8_t *dst = buf;
629
memcpy(dst, data2, data2_len * sizeof (uint8_t));
630
*p
631
=
632
(
633
(Hacl_Streaming_MD_state_32){
634
.block_state = block_state1,
635
.buf = buf,
636
.total_len = total_len1 + (uint64_t)len
637
}
638
);
639
}
640
else
641
{
642
uint32_t diff = (uint32_t)64U - sz;
643
uint8_t *data1 = data;
644
uint8_t *data2 = data + diff;
645
Hacl_Streaming_MD_state_32 s1 = *p;
646
uint32_t *block_state10 = s1.block_state;
647
uint8_t *buf0 = s1.buf;
648
uint64_t total_len10 = s1.total_len;
649
uint32_t sz10;
650
if (total_len10 % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len10 > (uint64_t)0U)
651
{
652
sz10 = (uint32_t)64U;
653
}
654
else
655
{
656
sz10 = (uint32_t)(total_len10 % (uint64_t)(uint32_t)64U);
657
}
658
uint8_t *buf2 = buf0 + sz10;
659
memcpy(buf2, data1, diff * sizeof (uint8_t));
660
uint64_t total_len2 = total_len10 + (uint64_t)diff;
661
*p
662
=
663
(
664
(Hacl_Streaming_MD_state_32){
665
.block_state = block_state10,
666
.buf = buf0,
667
.total_len = total_len2
668
}
669
);
670
Hacl_Streaming_MD_state_32 s10 = *p;
671
uint32_t *block_state1 = s10.block_state;
672
uint8_t *buf = s10.buf;
673
uint64_t total_len1 = s10.total_len;
674
uint32_t sz1;
675
if (total_len1 % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len1 > (uint64_t)0U)
676
{
677
sz1 = (uint32_t)64U;
678
}
679
else
680
{
681
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)64U);
682
}
683
if (!(sz1 == (uint32_t)0U))
684
{
685
Hacl_SHA2_Scalar32_sha256_update_nblocks((uint32_t)64U, buf, block_state1);
686
}
687
uint32_t ite;
688
if
689
(
690
(uint64_t)(len - diff)
691
% (uint64_t)(uint32_t)64U
692
== (uint64_t)0U
693
&& (uint64_t)(len - diff) > (uint64_t)0U
694
)
695
{
696
ite = (uint32_t)64U;
697
}
698
else
699
{
700
ite = (uint32_t)((uint64_t)(len - diff) % (uint64_t)(uint32_t)64U);
701
}
702
uint32_t n_blocks = (len - diff - ite) / (uint32_t)64U;
703
uint32_t data1_len = n_blocks * (uint32_t)64U;
704
uint32_t data2_len = len - diff - data1_len;
705
uint8_t *data11 = data2;
706
uint8_t *data21 = data2 + data1_len;
707
Hacl_SHA2_Scalar32_sha256_update_nblocks(data1_len / (uint32_t)64U * (uint32_t)64U,
708
data11,
709
block_state1);
710
uint8_t *dst = buf;
711
memcpy(dst, data21, data2_len * sizeof (uint8_t));
712
*p
713
=
714
(
715
(Hacl_Streaming_MD_state_32){
716
.block_state = block_state1,
717
.buf = buf,
718
.total_len = total_len1 + (uint64_t)(len - diff)
719
}
720
);
721
}
722
return Hacl_Streaming_Types_Success;
723
}
724
725
/**
726
Feed an arbitrary amount of data into the hash. This function returns 0 for
727
success, or 1 if the combined length of all of the data passed to `update_256`
728
(since the last call to `init_256`) exceeds 2^61-1 bytes.
729
730
This function is identical to the update function for SHA2_224.
731
*/
732
Hacl_Streaming_Types_error_code
733
Hacl_Streaming_SHA2_update_256(
734
Hacl_Streaming_MD_state_32 *p,
735
uint8_t *input,
736
uint32_t input_len
737
)
738
{
739
return update_224_256(p, input, input_len);
740
}
741
742
/**
743
Write the resulting hash into `dst`, an array of 32 bytes. The state remains
744
valid after a call to `finish_256`, meaning the user may feed more data into
745
the hash via `update_256`. (The finish_256 function operates on an internal copy of
746
the state and therefore does not invalidate the client-held state `p`.)
747
*/
748
void Hacl_Streaming_SHA2_finish_256(Hacl_Streaming_MD_state_32 *p, uint8_t *dst)
749
{
750
Hacl_Streaming_MD_state_32 scrut = *p;
751
uint32_t *block_state = scrut.block_state;
752
uint8_t *buf_ = scrut.buf;
753
uint64_t total_len = scrut.total_len;
754
uint32_t r;
755
if (total_len % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len > (uint64_t)0U)
756
{
757
r = (uint32_t)64U;
758
}
759
else
760
{
761
r = (uint32_t)(total_len % (uint64_t)(uint32_t)64U);
762
}
763
uint8_t *buf_1 = buf_;
764
uint32_t tmp_block_state[8U] = { 0U };
765
memcpy(tmp_block_state, block_state, (uint32_t)8U * sizeof (uint32_t));
766
uint32_t ite;
767
if (r % (uint32_t)64U == (uint32_t)0U && r > (uint32_t)0U)
768
{
769
ite = (uint32_t)64U;
770
}
771
else
772
{
773
ite = r % (uint32_t)64U;
774
}
775
uint8_t *buf_last = buf_1 + r - ite;
776
uint8_t *buf_multi = buf_1;
777
Hacl_SHA2_Scalar32_sha256_update_nblocks((uint32_t)0U, buf_multi, tmp_block_state);
778
uint64_t prev_len_last = total_len - (uint64_t)r;
779
Hacl_SHA2_Scalar32_sha256_update_last(prev_len_last + (uint64_t)r,
780
r,
781
buf_last,
782
tmp_block_state);
783
Hacl_SHA2_Scalar32_sha256_finish(tmp_block_state, dst);
784
}
785
786
/**
787
Free a state allocated with `create_in_256`.
788
789
This function is identical to the free function for SHA2_224.
790
*/
791
void Hacl_Streaming_SHA2_free_256(Hacl_Streaming_MD_state_32 *s)
792
{
793
Hacl_Streaming_MD_state_32 scrut = *s;
794
uint8_t *buf = scrut.buf;
795
uint32_t *block_state = scrut.block_state;
796
KRML_HOST_FREE(block_state);
797
KRML_HOST_FREE(buf);
798
KRML_HOST_FREE(s);
799
}
800
801
/**
802
Hash `input`, of len `input_len`, into `dst`, an array of 32 bytes.
803
*/
804
void Hacl_Streaming_SHA2_hash_256(uint8_t *input, uint32_t input_len, uint8_t *dst)
805
{
806
uint8_t *ib = input;
807
uint8_t *rb = dst;
808
uint32_t st[8U] = { 0U };
809
Hacl_SHA2_Scalar32_sha256_init(st);
810
uint32_t rem = input_len % (uint32_t)64U;
811
uint64_t len_ = (uint64_t)input_len;
812
Hacl_SHA2_Scalar32_sha256_update_nblocks(input_len, ib, st);
813
uint32_t rem1 = input_len % (uint32_t)64U;
814
uint8_t *b0 = ib;
815
uint8_t *lb = b0 + input_len - rem1;
816
Hacl_SHA2_Scalar32_sha256_update_last(len_, rem, lb, st);
817
Hacl_SHA2_Scalar32_sha256_finish(st, rb);
818
}
819
820
Hacl_Streaming_MD_state_32 *Hacl_Streaming_SHA2_create_in_224(void)
821
{
822
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
823
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
824
Hacl_Streaming_MD_state_32
825
s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
826
Hacl_Streaming_MD_state_32
827
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
828
p[0U] = s;
829
Hacl_SHA2_Scalar32_sha224_init(block_state);
830
return p;
831
}
832
833
void Hacl_Streaming_SHA2_init_224(Hacl_Streaming_MD_state_32 *s)
834
{
835
Hacl_Streaming_MD_state_32 scrut = *s;
836
uint8_t *buf = scrut.buf;
837
uint32_t *block_state = scrut.block_state;
838
Hacl_SHA2_Scalar32_sha224_init(block_state);
839
Hacl_Streaming_MD_state_32
840
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
841
s[0U] = tmp;
842
}
843
844
Hacl_Streaming_Types_error_code
845
Hacl_Streaming_SHA2_update_224(
846
Hacl_Streaming_MD_state_32 *p,
847
uint8_t *input,
848
uint32_t input_len
849
)
850
{
851
return update_224_256(p, input, input_len);
852
}
853
854
/**
855
Write the resulting hash into `dst`, an array of 28 bytes. The state remains
856
valid after a call to `finish_224`, meaning the user may feed more data into
857
the hash via `update_224`.
858
*/
859
void Hacl_Streaming_SHA2_finish_224(Hacl_Streaming_MD_state_32 *p, uint8_t *dst)
860
{
861
Hacl_Streaming_MD_state_32 scrut = *p;
862
uint32_t *block_state = scrut.block_state;
863
uint8_t *buf_ = scrut.buf;
864
uint64_t total_len = scrut.total_len;
865
uint32_t r;
866
if (total_len % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len > (uint64_t)0U)
867
{
868
r = (uint32_t)64U;
869
}
870
else
871
{
872
r = (uint32_t)(total_len % (uint64_t)(uint32_t)64U);
873
}
874
uint8_t *buf_1 = buf_;
875
uint32_t tmp_block_state[8U] = { 0U };
876
memcpy(tmp_block_state, block_state, (uint32_t)8U * sizeof (uint32_t));
877
uint32_t ite;
878
if (r % (uint32_t)64U == (uint32_t)0U && r > (uint32_t)0U)
879
{
880
ite = (uint32_t)64U;
881
}
882
else
883
{
884
ite = r % (uint32_t)64U;
885
}
886
uint8_t *buf_last = buf_1 + r - ite;
887
uint8_t *buf_multi = buf_1;
888
sha224_update_nblocks((uint32_t)0U, buf_multi, tmp_block_state);
889
uint64_t prev_len_last = total_len - (uint64_t)r;
890
Hacl_SHA2_Scalar32_sha224_update_last(prev_len_last + (uint64_t)r,
891
r,
892
buf_last,
893
tmp_block_state);
894
Hacl_SHA2_Scalar32_sha224_finish(tmp_block_state, dst);
895
}
896
897
void Hacl_Streaming_SHA2_free_224(Hacl_Streaming_MD_state_32 *p)
898
{
899
Hacl_Streaming_SHA2_free_256(p);
900
}
901
902
/**
903
Hash `input`, of len `input_len`, into `dst`, an array of 28 bytes.
904
*/
905
void Hacl_Streaming_SHA2_hash_224(uint8_t *input, uint32_t input_len, uint8_t *dst)
906
{
907
uint8_t *ib = input;
908
uint8_t *rb = dst;
909
uint32_t st[8U] = { 0U };
910
Hacl_SHA2_Scalar32_sha224_init(st);
911
uint32_t rem = input_len % (uint32_t)64U;
912
uint64_t len_ = (uint64_t)input_len;
913
sha224_update_nblocks(input_len, ib, st);
914
uint32_t rem1 = input_len % (uint32_t)64U;
915
uint8_t *b0 = ib;
916
uint8_t *lb = b0 + input_len - rem1;
917
Hacl_SHA2_Scalar32_sha224_update_last(len_, rem, lb, st);
918
Hacl_SHA2_Scalar32_sha224_finish(st, rb);
919
}
920
921
Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_create_in_512(void)
922
{
923
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)128U, sizeof (uint8_t));
924
uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint64_t));
925
Hacl_Streaming_MD_state_64
926
s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
927
Hacl_Streaming_MD_state_64
928
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
929
p[0U] = s;
930
Hacl_SHA2_Scalar32_sha512_init(block_state);
931
return p;
932
}
933
934
/**
935
Copies the state passed as argument into a newly allocated state (deep copy).
936
The state is to be freed by calling `free_512`. Cloning the state this way is
937
useful, for instance, if your control-flow diverges and you need to feed
938
more (different) data into the hash in each branch.
939
*/
940
Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_copy_512(Hacl_Streaming_MD_state_64 *s0)
941
{
942
Hacl_Streaming_MD_state_64 scrut = *s0;
943
uint64_t *block_state0 = scrut.block_state;
944
uint8_t *buf0 = scrut.buf;
945
uint64_t total_len0 = scrut.total_len;
946
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)128U, sizeof (uint8_t));
947
memcpy(buf, buf0, (uint32_t)128U * sizeof (uint8_t));
948
uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint64_t));
949
memcpy(block_state, block_state0, (uint32_t)8U * sizeof (uint64_t));
950
Hacl_Streaming_MD_state_64
951
s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
952
Hacl_Streaming_MD_state_64
953
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
954
p[0U] = s;
955
return p;
956
}
957
958
void Hacl_Streaming_SHA2_init_512(Hacl_Streaming_MD_state_64 *s)
959
{
960
Hacl_Streaming_MD_state_64 scrut = *s;
961
uint8_t *buf = scrut.buf;
962
uint64_t *block_state = scrut.block_state;
963
Hacl_SHA2_Scalar32_sha512_init(block_state);
964
Hacl_Streaming_MD_state_64
965
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
966
s[0U] = tmp;
967
}
968
969
static inline Hacl_Streaming_Types_error_code
970
update_384_512(Hacl_Streaming_MD_state_64 *p, uint8_t *data, uint32_t len)
971
{
972
Hacl_Streaming_MD_state_64 s = *p;
973
uint64_t total_len = s.total_len;
974
if ((uint64_t)len > (uint64_t)18446744073709551615U - total_len)
975
{
976
return Hacl_Streaming_Types_MaximumLengthExceeded;
977
}
978
uint32_t sz;
979
if (total_len % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len > (uint64_t)0U)
980
{
981
sz = (uint32_t)128U;
982
}
983
else
984
{
985
sz = (uint32_t)(total_len % (uint64_t)(uint32_t)128U);
986
}
987
if (len <= (uint32_t)128U - sz)
988
{
989
Hacl_Streaming_MD_state_64 s1 = *p;
990
uint64_t *block_state1 = s1.block_state;
991
uint8_t *buf = s1.buf;
992
uint64_t total_len1 = s1.total_len;
993
uint32_t sz1;
994
if (total_len1 % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len1 > (uint64_t)0U)
995
{
996
sz1 = (uint32_t)128U;
997
}
998
else
999
{
1000
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)128U);
1001
}
1002
uint8_t *buf2 = buf + sz1;
1003
memcpy(buf2, data, len * sizeof (uint8_t));
1004
uint64_t total_len2 = total_len1 + (uint64_t)len;
1005
*p
1006
=
1007
(
1008
(Hacl_Streaming_MD_state_64){
1009
.block_state = block_state1,
1010
.buf = buf,
1011
.total_len = total_len2
1012
}
1013
);
1014
}
1015
else if (sz == (uint32_t)0U)
1016
{
1017
Hacl_Streaming_MD_state_64 s1 = *p;
1018
uint64_t *block_state1 = s1.block_state;
1019
uint8_t *buf = s1.buf;
1020
uint64_t total_len1 = s1.total_len;
1021
uint32_t sz1;
1022
if (total_len1 % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len1 > (uint64_t)0U)
1023
{
1024
sz1 = (uint32_t)128U;
1025
}
1026
else
1027
{
1028
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)128U);
1029
}
1030
if (!(sz1 == (uint32_t)0U))
1031
{
1032
Hacl_SHA2_Scalar32_sha512_update_nblocks((uint32_t)128U, buf, block_state1);
1033
}
1034
uint32_t ite;
1035
if ((uint64_t)len % (uint64_t)(uint32_t)128U == (uint64_t)0U && (uint64_t)len > (uint64_t)0U)
1036
{
1037
ite = (uint32_t)128U;
1038
}
1039
else
1040
{
1041
ite = (uint32_t)((uint64_t)len % (uint64_t)(uint32_t)128U);
1042
}
1043
uint32_t n_blocks = (len - ite) / (uint32_t)128U;
1044
uint32_t data1_len = n_blocks * (uint32_t)128U;
1045
uint32_t data2_len = len - data1_len;
1046
uint8_t *data1 = data;
1047
uint8_t *data2 = data + data1_len;
1048
Hacl_SHA2_Scalar32_sha512_update_nblocks(data1_len / (uint32_t)128U * (uint32_t)128U,
1049
data1,
1050
block_state1);
1051
uint8_t *dst = buf;
1052
memcpy(dst, data2, data2_len * sizeof (uint8_t));
1053
*p
1054
=
1055
(
1056
(Hacl_Streaming_MD_state_64){
1057
.block_state = block_state1,
1058
.buf = buf,
1059
.total_len = total_len1 + (uint64_t)len
1060
}
1061
);
1062
}
1063
else
1064
{
1065
uint32_t diff = (uint32_t)128U - sz;
1066
uint8_t *data1 = data;
1067
uint8_t *data2 = data + diff;
1068
Hacl_Streaming_MD_state_64 s1 = *p;
1069
uint64_t *block_state10 = s1.block_state;
1070
uint8_t *buf0 = s1.buf;
1071
uint64_t total_len10 = s1.total_len;
1072
uint32_t sz10;
1073
if (total_len10 % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len10 > (uint64_t)0U)
1074
{
1075
sz10 = (uint32_t)128U;
1076
}
1077
else
1078
{
1079
sz10 = (uint32_t)(total_len10 % (uint64_t)(uint32_t)128U);
1080
}
1081
uint8_t *buf2 = buf0 + sz10;
1082
memcpy(buf2, data1, diff * sizeof (uint8_t));
1083
uint64_t total_len2 = total_len10 + (uint64_t)diff;
1084
*p
1085
=
1086
(
1087
(Hacl_Streaming_MD_state_64){
1088
.block_state = block_state10,
1089
.buf = buf0,
1090
.total_len = total_len2
1091
}
1092
);
1093
Hacl_Streaming_MD_state_64 s10 = *p;
1094
uint64_t *block_state1 = s10.block_state;
1095
uint8_t *buf = s10.buf;
1096
uint64_t total_len1 = s10.total_len;
1097
uint32_t sz1;
1098
if (total_len1 % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len1 > (uint64_t)0U)
1099
{
1100
sz1 = (uint32_t)128U;
1101
}
1102
else
1103
{
1104
sz1 = (uint32_t)(total_len1 % (uint64_t)(uint32_t)128U);
1105
}
1106
if (!(sz1 == (uint32_t)0U))
1107
{
1108
Hacl_SHA2_Scalar32_sha512_update_nblocks((uint32_t)128U, buf, block_state1);
1109
}
1110
uint32_t ite;
1111
if
1112
(
1113
(uint64_t)(len - diff)
1114
% (uint64_t)(uint32_t)128U
1115
== (uint64_t)0U
1116
&& (uint64_t)(len - diff) > (uint64_t)0U
1117
)
1118
{
1119
ite = (uint32_t)128U;
1120
}
1121
else
1122
{
1123
ite = (uint32_t)((uint64_t)(len - diff) % (uint64_t)(uint32_t)128U);
1124
}
1125
uint32_t n_blocks = (len - diff - ite) / (uint32_t)128U;
1126
uint32_t data1_len = n_blocks * (uint32_t)128U;
1127
uint32_t data2_len = len - diff - data1_len;
1128
uint8_t *data11 = data2;
1129
uint8_t *data21 = data2 + data1_len;
1130
Hacl_SHA2_Scalar32_sha512_update_nblocks(data1_len / (uint32_t)128U * (uint32_t)128U,
1131
data11,
1132
block_state1);
1133
uint8_t *dst = buf;
1134
memcpy(dst, data21, data2_len * sizeof (uint8_t));
1135
*p
1136
=
1137
(
1138
(Hacl_Streaming_MD_state_64){
1139
.block_state = block_state1,
1140
.buf = buf,
1141
.total_len = total_len1 + (uint64_t)(len - diff)
1142
}
1143
);
1144
}
1145
return Hacl_Streaming_Types_Success;
1146
}
1147
1148
/**
1149
Feed an arbitrary amount of data into the hash. This function returns 0 for
1150
success, or 1 if the combined length of all of the data passed to `update_512`
1151
(since the last call to `init_512`) exceeds 2^125-1 bytes.
1152
1153
This function is identical to the update function for SHA2_384.
1154
*/
1155
Hacl_Streaming_Types_error_code
1156
Hacl_Streaming_SHA2_update_512(
1157
Hacl_Streaming_MD_state_64 *p,
1158
uint8_t *input,
1159
uint32_t input_len
1160
)
1161
{
1162
return update_384_512(p, input, input_len);
1163
}
1164
1165
/**
1166
Write the resulting hash into `dst`, an array of 64 bytes. The state remains
1167
valid after a call to `finish_512`, meaning the user may feed more data into
1168
the hash via `update_512`. (The finish_512 function operates on an internal copy of
1169
the state and therefore does not invalidate the client-held state `p`.)
1170
*/
1171
void Hacl_Streaming_SHA2_finish_512(Hacl_Streaming_MD_state_64 *p, uint8_t *dst)
1172
{
1173
Hacl_Streaming_MD_state_64 scrut = *p;
1174
uint64_t *block_state = scrut.block_state;
1175
uint8_t *buf_ = scrut.buf;
1176
uint64_t total_len = scrut.total_len;
1177
uint32_t r;
1178
if (total_len % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len > (uint64_t)0U)
1179
{
1180
r = (uint32_t)128U;
1181
}
1182
else
1183
{
1184
r = (uint32_t)(total_len % (uint64_t)(uint32_t)128U);
1185
}
1186
uint8_t *buf_1 = buf_;
1187
uint64_t tmp_block_state[8U] = { 0U };
1188
memcpy(tmp_block_state, block_state, (uint32_t)8U * sizeof (uint64_t));
1189
uint32_t ite;
1190
if (r % (uint32_t)128U == (uint32_t)0U && r > (uint32_t)0U)
1191
{
1192
ite = (uint32_t)128U;
1193
}
1194
else
1195
{
1196
ite = r % (uint32_t)128U;
1197
}
1198
uint8_t *buf_last = buf_1 + r - ite;
1199
uint8_t *buf_multi = buf_1;
1200
Hacl_SHA2_Scalar32_sha512_update_nblocks((uint32_t)0U, buf_multi, tmp_block_state);
1201
uint64_t prev_len_last = total_len - (uint64_t)r;
1202
Hacl_SHA2_Scalar32_sha512_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last),
1203
FStar_UInt128_uint64_to_uint128((uint64_t)r)),
1204
r,
1205
buf_last,
1206
tmp_block_state);
1207
Hacl_SHA2_Scalar32_sha512_finish(tmp_block_state, dst);
1208
}
1209
1210
/**
1211
Free a state allocated with `create_in_512`.
1212
1213
This function is identical to the free function for SHA2_384.
1214
*/
1215
void Hacl_Streaming_SHA2_free_512(Hacl_Streaming_MD_state_64 *s)
1216
{
1217
Hacl_Streaming_MD_state_64 scrut = *s;
1218
uint8_t *buf = scrut.buf;
1219
uint64_t *block_state = scrut.block_state;
1220
KRML_HOST_FREE(block_state);
1221
KRML_HOST_FREE(buf);
1222
KRML_HOST_FREE(s);
1223
}
1224
1225
/**
1226
Hash `input`, of len `input_len`, into `dst`, an array of 64 bytes.
1227
*/
1228
void Hacl_Streaming_SHA2_hash_512(uint8_t *input, uint32_t input_len, uint8_t *dst)
1229
{
1230
uint8_t *ib = input;
1231
uint8_t *rb = dst;
1232
uint64_t st[8U] = { 0U };
1233
Hacl_SHA2_Scalar32_sha512_init(st);
1234
uint32_t rem = input_len % (uint32_t)128U;
1235
FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len);
1236
Hacl_SHA2_Scalar32_sha512_update_nblocks(input_len, ib, st);
1237
uint32_t rem1 = input_len % (uint32_t)128U;
1238
uint8_t *b0 = ib;
1239
uint8_t *lb = b0 + input_len - rem1;
1240
Hacl_SHA2_Scalar32_sha512_update_last(len_, rem, lb, st);
1241
Hacl_SHA2_Scalar32_sha512_finish(st, rb);
1242
}
1243
1244
Hacl_Streaming_MD_state_64 *Hacl_Streaming_SHA2_create_in_384(void)
1245
{
1246
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)128U, sizeof (uint8_t));
1247
uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint64_t));
1248
Hacl_Streaming_MD_state_64
1249
s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
1250
Hacl_Streaming_MD_state_64
1251
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
1252
p[0U] = s;
1253
Hacl_SHA2_Scalar32_sha384_init(block_state);
1254
return p;
1255
}
1256
1257
void Hacl_Streaming_SHA2_init_384(Hacl_Streaming_MD_state_64 *s)
1258
{
1259
Hacl_Streaming_MD_state_64 scrut = *s;
1260
uint8_t *buf = scrut.buf;
1261
uint64_t *block_state = scrut.block_state;
1262
Hacl_SHA2_Scalar32_sha384_init(block_state);
1263
Hacl_Streaming_MD_state_64
1264
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
1265
s[0U] = tmp;
1266
}
1267
1268
Hacl_Streaming_Types_error_code
1269
Hacl_Streaming_SHA2_update_384(
1270
Hacl_Streaming_MD_state_64 *p,
1271
uint8_t *input,
1272
uint32_t input_len
1273
)
1274
{
1275
return update_384_512(p, input, input_len);
1276
}
1277
1278
/**
1279
Write the resulting hash into `dst`, an array of 48 bytes. The state remains
1280
valid after a call to `finish_384`, meaning the user may feed more data into
1281
the hash via `update_384`.
1282
*/
1283
void Hacl_Streaming_SHA2_finish_384(Hacl_Streaming_MD_state_64 *p, uint8_t *dst)
1284
{
1285
Hacl_Streaming_MD_state_64 scrut = *p;
1286
uint64_t *block_state = scrut.block_state;
1287
uint8_t *buf_ = scrut.buf;
1288
uint64_t total_len = scrut.total_len;
1289
uint32_t r;
1290
if (total_len % (uint64_t)(uint32_t)128U == (uint64_t)0U && total_len > (uint64_t)0U)
1291
{
1292
r = (uint32_t)128U;
1293
}
1294
else
1295
{
1296
r = (uint32_t)(total_len % (uint64_t)(uint32_t)128U);
1297
}
1298
uint8_t *buf_1 = buf_;
1299
uint64_t tmp_block_state[8U] = { 0U };
1300
memcpy(tmp_block_state, block_state, (uint32_t)8U * sizeof (uint64_t));
1301
uint32_t ite;
1302
if (r % (uint32_t)128U == (uint32_t)0U && r > (uint32_t)0U)
1303
{
1304
ite = (uint32_t)128U;
1305
}
1306
else
1307
{
1308
ite = r % (uint32_t)128U;
1309
}
1310
uint8_t *buf_last = buf_1 + r - ite;
1311
uint8_t *buf_multi = buf_1;
1312
Hacl_SHA2_Scalar32_sha384_update_nblocks((uint32_t)0U, buf_multi, tmp_block_state);
1313
uint64_t prev_len_last = total_len - (uint64_t)r;
1314
Hacl_SHA2_Scalar32_sha384_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last),
1315
FStar_UInt128_uint64_to_uint128((uint64_t)r)),
1316
r,
1317
buf_last,
1318
tmp_block_state);
1319
Hacl_SHA2_Scalar32_sha384_finish(tmp_block_state, dst);
1320
}
1321
1322
void Hacl_Streaming_SHA2_free_384(Hacl_Streaming_MD_state_64 *p)
1323
{
1324
Hacl_Streaming_SHA2_free_512(p);
1325
}
1326
1327
/**
1328
Hash `input`, of len `input_len`, into `dst`, an array of 48 bytes.
1329
*/
1330
void Hacl_Streaming_SHA2_hash_384(uint8_t *input, uint32_t input_len, uint8_t *dst)
1331
{
1332
uint8_t *ib = input;
1333
uint8_t *rb = dst;
1334
uint64_t st[8U] = { 0U };
1335
Hacl_SHA2_Scalar32_sha384_init(st);
1336
uint32_t rem = input_len % (uint32_t)128U;
1337
FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len);
1338
Hacl_SHA2_Scalar32_sha384_update_nblocks(input_len, ib, st);
1339
uint32_t rem1 = input_len % (uint32_t)128U;
1340
uint8_t *b0 = ib;
1341
uint8_t *lb = b0 + input_len - rem1;
1342
Hacl_SHA2_Scalar32_sha384_update_last(len_, rem, lb, st);
1343
Hacl_SHA2_Scalar32_sha384_finish(st, rb);
1344
}
1345
1346
1347