Debian Codegraph @

Modules/_hacl/Hacl_Hash_SHA1.c

raw
1 /* MIT License
2 *
3 * Copyright (c) 2016-2022 INRIA, CMU and Microsoft Corporation
4 * Copyright (c) 2022-2023 HACL* Contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25
26 #include "internal/Hacl_Hash_SHA1.h"
27
28 #include "Hacl_Streaming_Types.h"
29 #include "internal/Hacl_Streaming_Types.h"
30
31 static uint32_t _h0[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
32
33 void Hacl_Hash_SHA1_init(uint32_t *s)
34 {
35 KRML_MAYBE_FOR5(i, 0U, 5U, 1U, s[i] = _h0[i];);
36 }
37
38 static void update(uint32_t *h, uint8_t *l)
39 {
40 uint32_t ha = h[0U];
41 uint32_t hb = h[1U];
42 uint32_t hc = h[2U];
43 uint32_t hd = h[3U];
44 uint32_t he = h[4U];
45 uint32_t _w[80U] = { 0U };
46 for (uint32_t i = 0U; i < 80U; i++)
47 {
48 uint32_t v;
49 if (i < 16U)
50 {
51 uint8_t *b = l + i * 4U;
52 uint32_t u = load32_be(b);
53 v = u;
54 }
55 else
56 {
57 uint32_t wmit3 = _w[i - 3U];
58 uint32_t wmit8 = _w[i - 8U];
59 uint32_t wmit14 = _w[i - 14U];
60 uint32_t wmit16 = _w[i - 16U];
61 v = (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) << 1U | (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) >> 31U;
62 }
63 _w[i] = v;
64 }
65 for (uint32_t i = 0U; i < 80U; i++)
66 {
67 uint32_t _a = h[0U];
68 uint32_t _b = h[1U];
69 uint32_t _c = h[2U];
70 uint32_t _d = h[3U];
71 uint32_t _e = h[4U];
72 uint32_t wmit = _w[i];
73 uint32_t ite0;
74 if (i < 20U)
75 {
76 ite0 = (_b & _c) ^ (~_b & _d);
77 }
78 else if (39U < i && i < 60U)
79 {
80 ite0 = (_b & _c) ^ ((_b & _d) ^ (_c & _d));
81 }
82 else
83 {
84 ite0 = _b ^ (_c ^ _d);
85 }
86 uint32_t ite;
87 if (i < 20U)
88 {
89 ite = 0x5a827999U;
90 }
91 else if (i < 40U)
92 {
93 ite = 0x6ed9eba1U;
94 }
95 else if (i < 60U)
96 {
97 ite = 0x8f1bbcdcU;
98 }
99 else
100 {
101 ite = 0xca62c1d6U;
102 }
103 uint32_t _T = (_a << 5U | _a >> 27U) + ite0 + _e + ite + wmit;
104 h[0U] = _T;
105 h[1U] = _a;
106 h[2U] = _b << 30U | _b >> 2U;
107 h[3U] = _c;
108 h[4U] = _d;
109 }
110 for (uint32_t i = 0U; i < 80U; i++)
111 {
112 _w[i] = 0U;
113 }
114 uint32_t sta = h[0U];
115 uint32_t stb = h[1U];
116 uint32_t stc = h[2U];
117 uint32_t std = h[3U];
118 uint32_t ste = h[4U];
119 h[0U] = sta + ha;
120 h[1U] = stb + hb;
121 h[2U] = stc + hc;
122 h[3U] = std + hd;
123 h[4U] = ste + he;
124 }
125
126 static void pad(uint64_t len, uint8_t *dst)
127 {
128 uint8_t *dst1 = dst;
129 dst1[0U] = 0x80U;
130 uint8_t *dst2 = dst + 1U;
131 for (uint32_t i = 0U; i < (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U; i++)
132 {
133 dst2[i] = 0U;
134 }
135 uint8_t *dst3 = dst + 1U + (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U;
136 store64_be(dst3, len << 3U);
137 }
138
139 void Hacl_Hash_SHA1_finish(uint32_t *s, uint8_t *dst)
140 {
141 KRML_MAYBE_FOR5(i, 0U, 5U, 1U, store32_be(dst + i * 4U, s[i]););
142 }
143
144 void Hacl_Hash_SHA1_update_multi(uint32_t *s, uint8_t *blocks, uint32_t n_blocks)
145 {
146 for (uint32_t i = 0U; i < n_blocks; i++)
147 {
148 uint32_t sz = 64U;
149 uint8_t *block = blocks + sz * i;
150 update(s, block);
151 }
152 }
153
154 void
155 Hacl_Hash_SHA1_update_last(uint32_t *s, uint64_t prev_len, uint8_t *input, uint32_t input_len)
156 {
157 uint32_t blocks_n = input_len / 64U;
158 uint32_t blocks_len = blocks_n * 64U;
159 uint8_t *blocks = input;
160 uint32_t rest_len = input_len - blocks_len;
161 uint8_t *rest = input + blocks_len;
162 Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
163 uint64_t total_input_len = prev_len + (uint64_t)input_len;
164 uint32_t pad_len = 1U + (128U - (9U + (uint32_t)(total_input_len % (uint64_t)64U))) % 64U + 8U;
165 uint32_t tmp_len = rest_len + pad_len;
166 uint8_t tmp_twoblocks[128U] = { 0U };
167 uint8_t *tmp = tmp_twoblocks;
168 uint8_t *tmp_rest = tmp;
169 uint8_t *tmp_pad = tmp + rest_len;
170 memcpy(tmp_rest, rest, rest_len * sizeof (uint8_t));
171 pad(total_input_len, tmp_pad);
172 Hacl_Hash_SHA1_update_multi(s, tmp, tmp_len / 64U);
173 }
174
175 void Hacl_Hash_SHA1_hash_oneshot(uint8_t *output, uint8_t *input, uint32_t input_len)
176 {
177 uint32_t s[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
178 uint32_t blocks_n0 = input_len / 64U;
179 uint32_t blocks_n1;
180 if (input_len % 64U == 0U && blocks_n0 > 0U)
181 {
182 blocks_n1 = blocks_n0 - 1U;
183 }
184 else
185 {
186 blocks_n1 = blocks_n0;
187 }
188 uint32_t blocks_len0 = blocks_n1 * 64U;
189 uint8_t *blocks0 = input;
190 uint32_t rest_len0 = input_len - blocks_len0;
191 uint8_t *rest0 = input + blocks_len0;
192 uint32_t blocks_n = blocks_n1;
193 uint32_t blocks_len = blocks_len0;
194 uint8_t *blocks = blocks0;
195 uint32_t rest_len = rest_len0;
196 uint8_t *rest = rest0;
197 Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
198 Hacl_Hash_SHA1_update_last(s, (uint64_t)blocks_len, rest, rest_len);
199 Hacl_Hash_SHA1_finish(s, output);
200 }
201
202 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_malloc(void)
203 {
204 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
205 if (buf == NULL)
206 {
207 return NULL;
208 }
209 uint8_t *buf1 = buf;
210 uint32_t *b = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
211 Hacl_Streaming_Types_optional_32 block_state;
212 if (b == NULL)
213 {
214 block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_None });
215 }
216 else
217 {
218 block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_Some, .v = b });
219 }
220 if (block_state.tag == Hacl_Streaming_Types_None)
221 {
222 KRML_HOST_FREE(buf1);
223 return NULL;
224 }
225 if (block_state.tag == Hacl_Streaming_Types_Some)
226 {
227 uint32_t *block_state1 = block_state.v;
228 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
229 switch (k_)
230 {
231 case Hacl_Streaming_Types_None:
232 {
233 return NULL;
234 }
235 case Hacl_Streaming_Types_Some:
236 {
237 Hacl_Streaming_MD_state_32
238 s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)0U };
239 Hacl_Streaming_MD_state_32
240 *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
241 if (p != NULL)
242 {
243 p[0U] = s;
244 }
245 if (p == NULL)
246 {
247 KRML_HOST_FREE(block_state1);
248 KRML_HOST_FREE(buf1);
249 return NULL;
250 }
251 Hacl_Hash_SHA1_init(block_state1);
252 return p;
253 }
254 default:
255 {
256 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
257 KRML_HOST_EXIT(253U);
258 }
259 }
260 }
261 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
262 __FILE__,
263 __LINE__,
264 "unreachable (pattern matches are exhaustive in F*)");
265 KRML_HOST_EXIT(255U);
266 }
267
268 void Hacl_Hash_SHA1_reset(Hacl_Streaming_MD_state_32 *state)
269 {
270 Hacl_Streaming_MD_state_32 scrut = *state;
271 uint8_t *buf = scrut.buf;
272 uint32_t *block_state = scrut.block_state;
273 Hacl_Hash_SHA1_init(block_state);
274 Hacl_Streaming_MD_state_32
275 tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
276 state[0U] = tmp;
277 }
278
279 /**
280 0 = success, 1 = max length exceeded
281 */
282 Hacl_Streaming_Types_error_code
283 Hacl_Hash_SHA1_update(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len)
284 {
285 Hacl_Streaming_MD_state_32 s = *state;
286 uint64_t total_len = s.total_len;
287 if ((uint64_t)chunk_len > 2305843009213693951ULL - total_len)
288 {
289 return Hacl_Streaming_Types_MaximumLengthExceeded;
290 }
291 uint32_t sz;
292 if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
293 {
294 sz = 64U;
295 }
296 else
297 {
298 sz = (uint32_t)(total_len % (uint64_t)64U);
299 }
300 if (chunk_len <= 64U - sz)
301 {
302 Hacl_Streaming_MD_state_32 s1 = *state;
303 uint32_t *block_state1 = s1.block_state;
304 uint8_t *buf = s1.buf;
305 uint64_t total_len1 = s1.total_len;
306 uint32_t sz1;
307 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
308 {
309 sz1 = 64U;
310 }
311 else
312 {
313 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
314 }
315 uint8_t *buf2 = buf + sz1;
316 memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
317 uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
318 *state =
319 (
320 (Hacl_Streaming_MD_state_32){
321 .block_state = block_state1,
322 .buf = buf,
323 .total_len = total_len2
324 }
325 );
326 }
327 else if (sz == 0U)
328 {
329 Hacl_Streaming_MD_state_32 s1 = *state;
330 uint32_t *block_state1 = s1.block_state;
331 uint8_t *buf = s1.buf;
332 uint64_t total_len1 = s1.total_len;
333 uint32_t sz1;
334 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
335 {
336 sz1 = 64U;
337 }
338 else
339 {
340 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
341 }
342 if (!(sz1 == 0U))
343 {
344 Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
345 }
346 uint32_t ite;
347 if ((uint64_t)chunk_len % (uint64_t)64U == 0ULL && (uint64_t)chunk_len > 0ULL)
348 {
349 ite = 64U;
350 }
351 else
352 {
353 ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)64U);
354 }
355 uint32_t n_blocks = (chunk_len - ite) / 64U;
356 uint32_t data1_len = n_blocks * 64U;
357 uint32_t data2_len = chunk_len - data1_len;
358 uint8_t *data1 = chunk;
359 uint8_t *data2 = chunk + data1_len;
360 Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
361 uint8_t *dst = buf;
362 memcpy(dst, data2, data2_len * sizeof (uint8_t));
363 *state =
364 (
365 (Hacl_Streaming_MD_state_32){
366 .block_state = block_state1,
367 .buf = buf,
368 .total_len = total_len1 + (uint64_t)chunk_len
369 }
370 );
371 }
372 else
373 {
374 uint32_t diff = 64U - sz;
375 uint8_t *chunk1 = chunk;
376 uint8_t *chunk2 = chunk + diff;
377 Hacl_Streaming_MD_state_32 s1 = *state;
378 uint32_t *block_state10 = s1.block_state;
379 uint8_t *buf0 = s1.buf;
380 uint64_t total_len10 = s1.total_len;
381 uint32_t sz10;
382 if (total_len10 % (uint64_t)64U == 0ULL && total_len10 > 0ULL)
383 {
384 sz10 = 64U;
385 }
386 else
387 {
388 sz10 = (uint32_t)(total_len10 % (uint64_t)64U);
389 }
390 uint8_t *buf2 = buf0 + sz10;
391 memcpy(buf2, chunk1, diff * sizeof (uint8_t));
392 uint64_t total_len2 = total_len10 + (uint64_t)diff;
393 *state =
394 (
395 (Hacl_Streaming_MD_state_32){
396 .block_state = block_state10,
397 .buf = buf0,
398 .total_len = total_len2
399 }
400 );
401 Hacl_Streaming_MD_state_32 s10 = *state;
402 uint32_t *block_state1 = s10.block_state;
403 uint8_t *buf = s10.buf;
404 uint64_t total_len1 = s10.total_len;
405 uint32_t sz1;
406 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
407 {
408 sz1 = 64U;
409 }
410 else
411 {
412 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
413 }
414 if (!(sz1 == 0U))
415 {
416 Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
417 }
418 uint32_t ite;
419 if
420 ((uint64_t)(chunk_len - diff) % (uint64_t)64U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
421 {
422 ite = 64U;
423 }
424 else
425 {
426 ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)64U);
427 }
428 uint32_t n_blocks = (chunk_len - diff - ite) / 64U;
429 uint32_t data1_len = n_blocks * 64U;
430 uint32_t data2_len = chunk_len - diff - data1_len;
431 uint8_t *data1 = chunk2;
432 uint8_t *data2 = chunk2 + data1_len;
433 Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
434 uint8_t *dst = buf;
435 memcpy(dst, data2, data2_len * sizeof (uint8_t));
436 *state =
437 (
438 (Hacl_Streaming_MD_state_32){
439 .block_state = block_state1,
440 .buf = buf,
441 .total_len = total_len1 + (uint64_t)(chunk_len - diff)
442 }
443 );
444 }
445 return Hacl_Streaming_Types_Success;
446 }
447
448 void Hacl_Hash_SHA1_digest(Hacl_Streaming_MD_state_32 *state, uint8_t *output)
449 {
450 Hacl_Streaming_MD_state_32 scrut = *state;
451 uint32_t *block_state = scrut.block_state;
452 uint8_t *buf_ = scrut.buf;
453 uint64_t total_len = scrut.total_len;
454 uint32_t r;
455 if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
456 {
457 r = 64U;
458 }
459 else
460 {
461 r = (uint32_t)(total_len % (uint64_t)64U);
462 }
463 uint8_t *buf_1 = buf_;
464 uint32_t tmp_block_state[5U] = { 0U };
465 memcpy(tmp_block_state, block_state, 5U * sizeof (uint32_t));
466 uint32_t ite;
467 if (r % 64U == 0U && r > 0U)
468 {
469 ite = 64U;
470 }
471 else
472 {
473 ite = r % 64U;
474 }
475 uint8_t *buf_last = buf_1 + r - ite;
476 uint8_t *buf_multi = buf_1;
477 Hacl_Hash_SHA1_update_multi(tmp_block_state, buf_multi, 0U);
478 uint64_t prev_len_last = total_len - (uint64_t)r;
479 Hacl_Hash_SHA1_update_last(tmp_block_state, prev_len_last, buf_last, r);
480 Hacl_Hash_SHA1_finish(tmp_block_state, output);
481 }
482
483 void Hacl_Hash_SHA1_free(Hacl_Streaming_MD_state_32 *state)
484 {
485 Hacl_Streaming_MD_state_32 scrut = *state;
486 uint8_t *buf = scrut.buf;
487 uint32_t *block_state = scrut.block_state;
488 KRML_HOST_FREE(block_state);
489 KRML_HOST_FREE(buf);
490 KRML_HOST_FREE(state);
491 }
492
493 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_copy(Hacl_Streaming_MD_state_32 *state)
494 {
495 Hacl_Streaming_MD_state_32 scrut = *state;
496 uint32_t *block_state0 = scrut.block_state;
497 uint8_t *buf0 = scrut.buf;
498 uint64_t total_len0 = scrut.total_len;
499 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
500 if (buf == NULL)
501 {
502 return NULL;
503 }
504 memcpy(buf, buf0, 64U * sizeof (uint8_t));
505 uint32_t *b = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
506 Hacl_Streaming_Types_optional_32 block_state;
507 if (b == NULL)
508 {
509 block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_None });
510 }
511 else
512 {
513 block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_Some, .v = b });
514 }
515 if (block_state.tag == Hacl_Streaming_Types_None)
516 {
517 KRML_HOST_FREE(buf);
518 return NULL;
519 }
520 if (block_state.tag == Hacl_Streaming_Types_Some)
521 {
522 uint32_t *block_state1 = block_state.v;
523 memcpy(block_state1, block_state0, 5U * sizeof (uint32_t));
524 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
525 switch (k_)
526 {
527 case Hacl_Streaming_Types_None:
528 {
529 return NULL;
530 }
531 case Hacl_Streaming_Types_Some:
532 {
533 Hacl_Streaming_MD_state_32
534 s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 };
535 Hacl_Streaming_MD_state_32
536 *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
537 if (p != NULL)
538 {
539 p[0U] = s;
540 }
541 if (p == NULL)
542 {
543 KRML_HOST_FREE(block_state1);
544 KRML_HOST_FREE(buf);
545 return NULL;
546 }
547 return p;
548 }
549 default:
550 {
551 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
552 KRML_HOST_EXIT(253U);
553 }
554 }
555 }
556 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
557 __FILE__,
558 __LINE__,
559 "unreachable (pattern matches are exhaustive in F*)");
560 KRML_HOST_EXIT(255U);
561 }
562
563 void Hacl_Hash_SHA1_hash(uint8_t *output, uint8_t *input, uint32_t input_len)
564 {
565 Hacl_Hash_SHA1_hash_oneshot(output, input, input_len);
566 }
567