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