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 static uint32_t _h0[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
29
Hacl_Hash_SHA1_init(uint32_t * s)30 void Hacl_Hash_SHA1_init(uint32_t *s)
31 {
32 KRML_MAYBE_FOR5(i, 0U, 5U, 1U, s[i] = _h0[i];);
33 }
34
update(uint32_t * h,uint8_t * l)35 static void update(uint32_t *h, uint8_t *l)
36 {
37 uint32_t ha = h[0U];
38 uint32_t hb = h[1U];
39 uint32_t hc = h[2U];
40 uint32_t hd = h[3U];
41 uint32_t he = h[4U];
42 uint32_t _w[80U] = { 0U };
43 for (uint32_t i = 0U; i < 80U; i++)
44 {
45 uint32_t v;
46 if (i < 16U)
47 {
48 uint8_t *b = l + i * 4U;
49 uint32_t u = load32_be(b);
50 v = u;
51 }
52 else
53 {
54 uint32_t wmit3 = _w[i - 3U];
55 uint32_t wmit8 = _w[i - 8U];
56 uint32_t wmit14 = _w[i - 14U];
57 uint32_t wmit16 = _w[i - 16U];
58 v = (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) << 1U | (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) >> 31U;
59 }
60 _w[i] = v;
61 }
62 for (uint32_t i = 0U; i < 80U; i++)
63 {
64 uint32_t _a = h[0U];
65 uint32_t _b = h[1U];
66 uint32_t _c = h[2U];
67 uint32_t _d = h[3U];
68 uint32_t _e = h[4U];
69 uint32_t wmit = _w[i];
70 uint32_t ite0;
71 if (i < 20U)
72 {
73 ite0 = (_b & _c) ^ (~_b & _d);
74 }
75 else if (39U < i && i < 60U)
76 {
77 ite0 = (_b & _c) ^ ((_b & _d) ^ (_c & _d));
78 }
79 else
80 {
81 ite0 = _b ^ (_c ^ _d);
82 }
83 uint32_t ite;
84 if (i < 20U)
85 {
86 ite = 0x5a827999U;
87 }
88 else if (i < 40U)
89 {
90 ite = 0x6ed9eba1U;
91 }
92 else if (i < 60U)
93 {
94 ite = 0x8f1bbcdcU;
95 }
96 else
97 {
98 ite = 0xca62c1d6U;
99 }
100 uint32_t _T = (_a << 5U | _a >> 27U) + ite0 + _e + ite + wmit;
101 h[0U] = _T;
102 h[1U] = _a;
103 h[2U] = _b << 30U | _b >> 2U;
104 h[3U] = _c;
105 h[4U] = _d;
106 }
107 for (uint32_t i = 0U; i < 80U; i++)
108 {
109 _w[i] = 0U;
110 }
111 uint32_t sta = h[0U];
112 uint32_t stb = h[1U];
113 uint32_t stc = h[2U];
114 uint32_t std = h[3U];
115 uint32_t ste = h[4U];
116 h[0U] = sta + ha;
117 h[1U] = stb + hb;
118 h[2U] = stc + hc;
119 h[3U] = std + hd;
120 h[4U] = ste + he;
121 }
122
pad(uint64_t len,uint8_t * dst)123 static void pad(uint64_t len, uint8_t *dst)
124 {
125 uint8_t *dst1 = dst;
126 dst1[0U] = 0x80U;
127 uint8_t *dst2 = dst + 1U;
128 for (uint32_t i = 0U; i < (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U; i++)
129 {
130 dst2[i] = 0U;
131 }
132 uint8_t *dst3 = dst + 1U + (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U;
133 store64_be(dst3, len << 3U);
134 }
135
Hacl_Hash_SHA1_finish(uint32_t * s,uint8_t * dst)136 void Hacl_Hash_SHA1_finish(uint32_t *s, uint8_t *dst)
137 {
138 KRML_MAYBE_FOR5(i, 0U, 5U, 1U, store32_be(dst + i * 4U, s[i]););
139 }
140
Hacl_Hash_SHA1_update_multi(uint32_t * s,uint8_t * blocks,uint32_t n_blocks)141 void Hacl_Hash_SHA1_update_multi(uint32_t *s, uint8_t *blocks, uint32_t n_blocks)
142 {
143 for (uint32_t i = 0U; i < n_blocks; i++)
144 {
145 uint32_t sz = 64U;
146 uint8_t *block = blocks + sz * i;
147 update(s, block);
148 }
149 }
150
151 void
Hacl_Hash_SHA1_update_last(uint32_t * s,uint64_t prev_len,uint8_t * input,uint32_t input_len)152 Hacl_Hash_SHA1_update_last(uint32_t *s, uint64_t prev_len, uint8_t *input, uint32_t input_len)
153 {
154 uint32_t blocks_n = input_len / 64U;
155 uint32_t blocks_len = blocks_n * 64U;
156 uint8_t *blocks = input;
157 uint32_t rest_len = input_len - blocks_len;
158 uint8_t *rest = input + blocks_len;
159 Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
160 uint64_t total_input_len = prev_len + (uint64_t)input_len;
161 uint32_t pad_len = 1U + (128U - (9U + (uint32_t)(total_input_len % (uint64_t)64U))) % 64U + 8U;
162 uint32_t tmp_len = rest_len + pad_len;
163 uint8_t tmp_twoblocks[128U] = { 0U };
164 uint8_t *tmp = tmp_twoblocks;
165 uint8_t *tmp_rest = tmp;
166 uint8_t *tmp_pad = tmp + rest_len;
167 memcpy(tmp_rest, rest, rest_len * sizeof (uint8_t));
168 pad(total_input_len, tmp_pad);
169 Hacl_Hash_SHA1_update_multi(s, tmp, tmp_len / 64U);
170 }
171
Hacl_Hash_SHA1_hash_oneshot(uint8_t * output,uint8_t * input,uint32_t input_len)172 void Hacl_Hash_SHA1_hash_oneshot(uint8_t *output, uint8_t *input, uint32_t input_len)
173 {
174 uint32_t s[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
175 uint32_t blocks_n0 = input_len / 64U;
176 uint32_t blocks_n1;
177 if (input_len % 64U == 0U && blocks_n0 > 0U)
178 {
179 blocks_n1 = blocks_n0 - 1U;
180 }
181 else
182 {
183 blocks_n1 = blocks_n0;
184 }
185 uint32_t blocks_len0 = blocks_n1 * 64U;
186 uint8_t *blocks0 = input;
187 uint32_t rest_len0 = input_len - blocks_len0;
188 uint8_t *rest0 = input + blocks_len0;
189 uint32_t blocks_n = blocks_n1;
190 uint32_t blocks_len = blocks_len0;
191 uint8_t *blocks = blocks0;
192 uint32_t rest_len = rest_len0;
193 uint8_t *rest = rest0;
194 Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
195 Hacl_Hash_SHA1_update_last(s, (uint64_t)blocks_len, rest, rest_len);
196 Hacl_Hash_SHA1_finish(s, output);
197 }
198
Hacl_Hash_SHA1_malloc(void)199 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_malloc(void)
200 {
201 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
202 uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
203 Hacl_Streaming_MD_state_32
204 s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
205 Hacl_Streaming_MD_state_32
206 *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
207 p[0U] = s;
208 Hacl_Hash_SHA1_init(block_state);
209 return p;
210 }
211
Hacl_Hash_SHA1_reset(Hacl_Streaming_MD_state_32 * state)212 void Hacl_Hash_SHA1_reset(Hacl_Streaming_MD_state_32 *state)
213 {
214 Hacl_Streaming_MD_state_32 scrut = *state;
215 uint8_t *buf = scrut.buf;
216 uint32_t *block_state = scrut.block_state;
217 Hacl_Hash_SHA1_init(block_state);
218 Hacl_Streaming_MD_state_32
219 tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
220 state[0U] = tmp;
221 }
222
223 /**
224 0 = success, 1 = max length exceeded
225 */
226 Hacl_Streaming_Types_error_code
Hacl_Hash_SHA1_update(Hacl_Streaming_MD_state_32 * state,uint8_t * chunk,uint32_t chunk_len)227 Hacl_Hash_SHA1_update(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len)
228 {
229 Hacl_Streaming_MD_state_32 s = *state;
230 uint64_t total_len = s.total_len;
231 if ((uint64_t)chunk_len > 2305843009213693951ULL - total_len)
232 {
233 return Hacl_Streaming_Types_MaximumLengthExceeded;
234 }
235 uint32_t sz;
236 if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
237 {
238 sz = 64U;
239 }
240 else
241 {
242 sz = (uint32_t)(total_len % (uint64_t)64U);
243 }
244 if (chunk_len <= 64U - sz)
245 {
246 Hacl_Streaming_MD_state_32 s1 = *state;
247 uint32_t *block_state1 = s1.block_state;
248 uint8_t *buf = s1.buf;
249 uint64_t total_len1 = s1.total_len;
250 uint32_t sz1;
251 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
252 {
253 sz1 = 64U;
254 }
255 else
256 {
257 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
258 }
259 uint8_t *buf2 = buf + sz1;
260 memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
261 uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
262 *state
263 =
264 (
265 (Hacl_Streaming_MD_state_32){
266 .block_state = block_state1,
267 .buf = buf,
268 .total_len = total_len2
269 }
270 );
271 }
272 else if (sz == 0U)
273 {
274 Hacl_Streaming_MD_state_32 s1 = *state;
275 uint32_t *block_state1 = s1.block_state;
276 uint8_t *buf = s1.buf;
277 uint64_t total_len1 = s1.total_len;
278 uint32_t sz1;
279 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
280 {
281 sz1 = 64U;
282 }
283 else
284 {
285 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
286 }
287 if (!(sz1 == 0U))
288 {
289 Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
290 }
291 uint32_t ite;
292 if ((uint64_t)chunk_len % (uint64_t)64U == 0ULL && (uint64_t)chunk_len > 0ULL)
293 {
294 ite = 64U;
295 }
296 else
297 {
298 ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)64U);
299 }
300 uint32_t n_blocks = (chunk_len - ite) / 64U;
301 uint32_t data1_len = n_blocks * 64U;
302 uint32_t data2_len = chunk_len - data1_len;
303 uint8_t *data1 = chunk;
304 uint8_t *data2 = chunk + data1_len;
305 Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
306 uint8_t *dst = buf;
307 memcpy(dst, data2, data2_len * sizeof (uint8_t));
308 *state
309 =
310 (
311 (Hacl_Streaming_MD_state_32){
312 .block_state = block_state1,
313 .buf = buf,
314 .total_len = total_len1 + (uint64_t)chunk_len
315 }
316 );
317 }
318 else
319 {
320 uint32_t diff = 64U - sz;
321 uint8_t *chunk1 = chunk;
322 uint8_t *chunk2 = chunk + diff;
323 Hacl_Streaming_MD_state_32 s1 = *state;
324 uint32_t *block_state10 = s1.block_state;
325 uint8_t *buf0 = s1.buf;
326 uint64_t total_len10 = s1.total_len;
327 uint32_t sz10;
328 if (total_len10 % (uint64_t)64U == 0ULL && total_len10 > 0ULL)
329 {
330 sz10 = 64U;
331 }
332 else
333 {
334 sz10 = (uint32_t)(total_len10 % (uint64_t)64U);
335 }
336 uint8_t *buf2 = buf0 + sz10;
337 memcpy(buf2, chunk1, diff * sizeof (uint8_t));
338 uint64_t total_len2 = total_len10 + (uint64_t)diff;
339 *state
340 =
341 (
342 (Hacl_Streaming_MD_state_32){
343 .block_state = block_state10,
344 .buf = buf0,
345 .total_len = total_len2
346 }
347 );
348 Hacl_Streaming_MD_state_32 s10 = *state;
349 uint32_t *block_state1 = s10.block_state;
350 uint8_t *buf = s10.buf;
351 uint64_t total_len1 = s10.total_len;
352 uint32_t sz1;
353 if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
354 {
355 sz1 = 64U;
356 }
357 else
358 {
359 sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
360 }
361 if (!(sz1 == 0U))
362 {
363 Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
364 }
365 uint32_t ite;
366 if
367 ((uint64_t)(chunk_len - diff) % (uint64_t)64U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
368 {
369 ite = 64U;
370 }
371 else
372 {
373 ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)64U);
374 }
375 uint32_t n_blocks = (chunk_len - diff - ite) / 64U;
376 uint32_t data1_len = n_blocks * 64U;
377 uint32_t data2_len = chunk_len - diff - data1_len;
378 uint8_t *data1 = chunk2;
379 uint8_t *data2 = chunk2 + data1_len;
380 Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
381 uint8_t *dst = buf;
382 memcpy(dst, data2, data2_len * sizeof (uint8_t));
383 *state
384 =
385 (
386 (Hacl_Streaming_MD_state_32){
387 .block_state = block_state1,
388 .buf = buf,
389 .total_len = total_len1 + (uint64_t)(chunk_len - diff)
390 }
391 );
392 }
393 return Hacl_Streaming_Types_Success;
394 }
395
Hacl_Hash_SHA1_digest(Hacl_Streaming_MD_state_32 * state,uint8_t * output)396 void Hacl_Hash_SHA1_digest(Hacl_Streaming_MD_state_32 *state, uint8_t *output)
397 {
398 Hacl_Streaming_MD_state_32 scrut = *state;
399 uint32_t *block_state = scrut.block_state;
400 uint8_t *buf_ = scrut.buf;
401 uint64_t total_len = scrut.total_len;
402 uint32_t r;
403 if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
404 {
405 r = 64U;
406 }
407 else
408 {
409 r = (uint32_t)(total_len % (uint64_t)64U);
410 }
411 uint8_t *buf_1 = buf_;
412 uint32_t tmp_block_state[5U] = { 0U };
413 memcpy(tmp_block_state, block_state, 5U * sizeof (uint32_t));
414 uint32_t ite;
415 if (r % 64U == 0U && r > 0U)
416 {
417 ite = 64U;
418 }
419 else
420 {
421 ite = r % 64U;
422 }
423 uint8_t *buf_last = buf_1 + r - ite;
424 uint8_t *buf_multi = buf_1;
425 Hacl_Hash_SHA1_update_multi(tmp_block_state, buf_multi, 0U);
426 uint64_t prev_len_last = total_len - (uint64_t)r;
427 Hacl_Hash_SHA1_update_last(tmp_block_state, prev_len_last, buf_last, r);
428 Hacl_Hash_SHA1_finish(tmp_block_state, output);
429 }
430
Hacl_Hash_SHA1_free(Hacl_Streaming_MD_state_32 * state)431 void Hacl_Hash_SHA1_free(Hacl_Streaming_MD_state_32 *state)
432 {
433 Hacl_Streaming_MD_state_32 scrut = *state;
434 uint8_t *buf = scrut.buf;
435 uint32_t *block_state = scrut.block_state;
436 KRML_HOST_FREE(block_state);
437 KRML_HOST_FREE(buf);
438 KRML_HOST_FREE(state);
439 }
440
Hacl_Hash_SHA1_copy(Hacl_Streaming_MD_state_32 * state)441 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_copy(Hacl_Streaming_MD_state_32 *state)
442 {
443 Hacl_Streaming_MD_state_32 scrut = *state;
444 uint32_t *block_state0 = scrut.block_state;
445 uint8_t *buf0 = scrut.buf;
446 uint64_t total_len0 = scrut.total_len;
447 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
448 memcpy(buf, buf0, 64U * sizeof (uint8_t));
449 uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
450 memcpy(block_state, block_state0, 5U * sizeof (uint32_t));
451 Hacl_Streaming_MD_state_32
452 s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
453 Hacl_Streaming_MD_state_32
454 *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
455 p[0U] = s;
456 return p;
457 }
458
Hacl_Hash_SHA1_hash(uint8_t * output,uint8_t * input,uint32_t input_len)459 void Hacl_Hash_SHA1_hash(uint8_t *output, uint8_t *input, uint32_t input_len)
460 {
461 Hacl_Hash_SHA1_hash_oneshot(output, input, input_len);
462 }
463
464