Coverage Report

Created: 2025-11-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/freebl/verified/Hacl_Hash_SHA3.c
Line
Count
Source
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
#include "internal/Hacl_Hash_SHA3.h"
26
27
static uint32_t
28
block_len(Spec_Hash_Definitions_hash_alg a)
29
17.8M
{
30
17.8M
    switch (a) {
31
98.4k
        case Spec_Hash_Definitions_SHA3_224: {
32
98.4k
            return (uint32_t)144U;
33
0
        }
34
11.2M
        case Spec_Hash_Definitions_SHA3_256: {
35
11.2M
            return (uint32_t)136U;
36
0
        }
37
3.76M
        case Spec_Hash_Definitions_SHA3_384: {
38
3.76M
            return (uint32_t)104U;
39
0
        }
40
2.75M
        case Spec_Hash_Definitions_SHA3_512: {
41
2.75M
            return (uint32_t)72U;
42
0
        }
43
0
        case Spec_Hash_Definitions_Shake128: {
44
0
            return (uint32_t)168U;
45
0
        }
46
0
        case Spec_Hash_Definitions_Shake256: {
47
0
            return (uint32_t)136U;
48
0
        }
49
0
        default: {
50
0
            KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
51
0
            KRML_HOST_EXIT(253U);
52
0
        }
53
17.8M
    }
54
17.8M
}
55
56
static uint32_t
57
hash_len(Spec_Hash_Definitions_hash_alg a)
58
26.6k
{
59
26.6k
    switch (a) {
60
1.46k
        case Spec_Hash_Definitions_SHA3_224: {
61
1.46k
            return (uint32_t)28U;
62
0
        }
63
8.79k
        case Spec_Hash_Definitions_SHA3_256: {
64
8.79k
            return (uint32_t)32U;
65
0
        }
66
15.6k
        case Spec_Hash_Definitions_SHA3_384: {
67
15.6k
            return (uint32_t)48U;
68
0
        }
69
820
        case Spec_Hash_Definitions_SHA3_512: {
70
820
            return (uint32_t)64U;
71
0
        }
72
0
        default: {
73
0
            KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
74
0
            KRML_HOST_EXIT(253U);
75
0
        }
76
26.6k
    }
77
26.6k
}
78
79
void
80
Hacl_Hash_SHA3_update_multi_sha3(
81
    Spec_Hash_Definitions_hash_alg a,
82
    uint64_t *s,
83
    uint8_t *blocks,
84
    uint32_t n_blocks)
85
316k
{
86
828k
    for (uint32_t i = (uint32_t)0U; i < n_blocks; i++) {
87
512k
        uint8_t *block = blocks + i * block_len(a);
88
512k
        Hacl_Impl_SHA3_absorb_inner(block_len(a), block, s);
89
512k
    }
90
316k
}
91
92
void
93
Hacl_Hash_SHA3_update_last_sha3(
94
    Spec_Hash_Definitions_hash_alg a,
95
    uint64_t *s,
96
    uint8_t *input,
97
    uint32_t input_len)
98
13.3k
{
99
13.3k
    uint8_t suffix;
100
13.3k
    if (a == Spec_Hash_Definitions_Shake128 || a == Spec_Hash_Definitions_Shake256) {
101
0
        suffix = (uint8_t)0x1fU;
102
13.3k
    } else {
103
13.3k
        suffix = (uint8_t)0x06U;
104
13.3k
    }
105
13.3k
    uint32_t len = block_len(a);
106
13.3k
    if (input_len == len) {
107
286
        Hacl_Impl_SHA3_absorb_inner(len, input, s);
108
286
        uint8_t lastBlock_[200U] = { 0U };
109
286
        uint8_t *lastBlock = lastBlock_;
110
286
        memcpy(lastBlock, input + input_len, (uint32_t)0U * sizeof(uint8_t));
111
286
        lastBlock[0U] = suffix;
112
286
        Hacl_Impl_SHA3_loadState(len, lastBlock, s);
113
286
        if (!((suffix & (uint8_t)0x80U) == (uint8_t)0U) && (uint32_t)0U == len - (uint32_t)1U) {
114
0
            Hacl_Impl_SHA3_state_permute(s);
115
0
        }
116
286
        uint8_t nextBlock_[200U] = { 0U };
117
286
        uint8_t *nextBlock = nextBlock_;
118
286
        nextBlock[len - (uint32_t)1U] = (uint8_t)0x80U;
119
286
        Hacl_Impl_SHA3_loadState(len, nextBlock, s);
120
286
        Hacl_Impl_SHA3_state_permute(s);
121
286
        return;
122
286
    }
123
13.0k
    uint8_t lastBlock_[200U] = { 0U };
124
13.0k
    uint8_t *lastBlock = lastBlock_;
125
13.0k
    memcpy(lastBlock, input, input_len * sizeof(uint8_t));
126
13.0k
    lastBlock[input_len] = suffix;
127
13.0k
    Hacl_Impl_SHA3_loadState(len, lastBlock, s);
128
13.0k
    if (!((suffix & (uint8_t)0x80U) == (uint8_t)0U) && input_len == len - (uint32_t)1U) {
129
0
        Hacl_Impl_SHA3_state_permute(s);
130
0
    }
131
13.0k
    uint8_t nextBlock_[200U] = { 0U };
132
13.0k
    uint8_t *nextBlock = nextBlock_;
133
13.0k
    nextBlock[len - (uint32_t)1U] = (uint8_t)0x80U;
134
13.0k
    Hacl_Impl_SHA3_loadState(len, nextBlock, s);
135
13.0k
    Hacl_Impl_SHA3_state_permute(s);
136
13.0k
}
137
138
typedef struct hash_buf2_s {
139
    Hacl_Streaming_Keccak_hash_buf fst;
140
    Hacl_Streaming_Keccak_hash_buf snd;
141
} hash_buf2;
142
143
Spec_Hash_Definitions_hash_alg
144
Hacl_Streaming_Keccak_get_alg(Hacl_Streaming_Keccak_state *s)
145
13.3k
{
146
13.3k
    Hacl_Streaming_Keccak_hash_buf block_state = (*s).block_state;
147
13.3k
    return block_state.fst;
148
13.3k
}
149
150
Hacl_Streaming_Keccak_state *
151
Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_hash_alg a)
152
33.0k
{
153
33.0k
    KRML_CHECK_SIZE(sizeof(uint8_t), block_len(a));
154
33.0k
    uint8_t *buf0 = (uint8_t *)KRML_HOST_CALLOC(block_len(a), sizeof(uint8_t));
155
33.0k
    uint64_t *buf = (uint64_t *)KRML_HOST_CALLOC((uint32_t)25U, sizeof(uint64_t));
156
33.0k
    Hacl_Streaming_Keccak_hash_buf block_state = { .fst = a, .snd = buf };
157
33.0k
    Hacl_Streaming_Keccak_state
158
33.0k
        s = { .block_state = block_state, .buf = buf0, .total_len = (uint64_t)(uint32_t)0U };
159
33.0k
    Hacl_Streaming_Keccak_state
160
33.0k
        *p = (Hacl_Streaming_Keccak_state *)KRML_HOST_MALLOC(sizeof(Hacl_Streaming_Keccak_state));
161
33.0k
    p[0U] = s;
162
33.0k
    uint64_t *s1 = block_state.snd;
163
33.0k
    memset(s1, 0U, (uint32_t)25U * sizeof(uint64_t));
164
33.0k
    return p;
165
33.0k
}
166
167
void
168
Hacl_Streaming_Keccak_free(Hacl_Streaming_Keccak_state *s)
169
33.0k
{
170
33.0k
    Hacl_Streaming_Keccak_state scrut = *s;
171
33.0k
    uint8_t *buf = scrut.buf;
172
33.0k
    Hacl_Streaming_Keccak_hash_buf block_state = scrut.block_state;
173
33.0k
    uint64_t *s1 = block_state.snd;
174
33.0k
    KRML_HOST_FREE(s1);
175
33.0k
    KRML_HOST_FREE(buf);
176
33.0k
    KRML_HOST_FREE(s);
177
33.0k
}
178
179
Hacl_Streaming_Keccak_state *
180
Hacl_Streaming_Keccak_copy(Hacl_Streaming_Keccak_state *s0)
181
0
{
182
0
    Hacl_Streaming_Keccak_state scrut0 = *s0;
183
0
    Hacl_Streaming_Keccak_hash_buf block_state0 = scrut0.block_state;
184
0
    uint8_t *buf0 = scrut0.buf;
185
0
    uint64_t total_len0 = scrut0.total_len;
186
0
    Spec_Hash_Definitions_hash_alg i = block_state0.fst;
187
0
    KRML_CHECK_SIZE(sizeof(uint8_t), block_len(i));
188
0
    uint8_t *buf1 = (uint8_t *)KRML_HOST_CALLOC(block_len(i), sizeof(uint8_t));
189
0
    memcpy(buf1, buf0, block_len(i) * sizeof(uint8_t));
190
0
    uint64_t *buf = (uint64_t *)KRML_HOST_CALLOC((uint32_t)25U, sizeof(uint64_t));
191
0
    Hacl_Streaming_Keccak_hash_buf block_state = { .fst = i, .snd = buf };
192
0
    hash_buf2 scrut = { .fst = block_state0, .snd = block_state };
193
0
    uint64_t *s_dst = scrut.snd.snd;
194
0
    uint64_t *s_src = scrut.fst.snd;
195
0
    memcpy(s_dst, s_src, (uint32_t)25U * sizeof(uint64_t));
196
0
    Hacl_Streaming_Keccak_state
197
0
        s = { .block_state = block_state, .buf = buf1, .total_len = total_len0 };
198
0
    Hacl_Streaming_Keccak_state
199
0
        *p = (Hacl_Streaming_Keccak_state *)KRML_HOST_MALLOC(sizeof(Hacl_Streaming_Keccak_state));
200
0
    p[0U] = s;
201
0
    return p;
202
0
}
203
204
void
205
Hacl_Streaming_Keccak_reset(Hacl_Streaming_Keccak_state *s)
206
66.0k
{
207
66.0k
    Hacl_Streaming_Keccak_state scrut = *s;
208
66.0k
    uint8_t *buf = scrut.buf;
209
66.0k
    Hacl_Streaming_Keccak_hash_buf block_state = scrut.block_state;
210
66.0k
    Spec_Hash_Definitions_hash_alg i = block_state.fst;
211
66.0k
    KRML_HOST_IGNORE(i);
212
66.0k
    uint64_t *s1 = block_state.snd;
213
66.0k
    memset(s1, 0U, (uint32_t)25U * sizeof(uint64_t));
214
66.0k
    Hacl_Streaming_Keccak_state
215
66.0k
        tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)(uint32_t)0U };
216
66.0k
    s[0U] = tmp;
217
66.0k
}
218
219
Hacl_Streaming_Types_error_code
220
Hacl_Streaming_Keccak_update(Hacl_Streaming_Keccak_state *p, uint8_t *data, uint32_t len)
221
3.03M
{
222
3.03M
    Hacl_Streaming_Keccak_state s = *p;
223
3.03M
    Hacl_Streaming_Keccak_hash_buf block_state = s.block_state;
224
3.03M
    uint64_t total_len = s.total_len;
225
3.03M
    Spec_Hash_Definitions_hash_alg i = block_state.fst;
226
3.03M
    if ((uint64_t)len > (uint64_t)0xFFFFFFFFFFFFFFFFU - total_len) {
227
0
        return Hacl_Streaming_Types_MaximumLengthExceeded;
228
0
    }
229
3.03M
    uint32_t sz;
230
3.03M
    if (total_len % (uint64_t)block_len(i) == (uint64_t)0U && total_len > (uint64_t)0U) {
231
22.2k
        sz = block_len(i);
232
3.01M
    } else {
233
3.01M
        sz = (uint32_t)(total_len % (uint64_t)block_len(i));
234
3.01M
    }
235
3.03M
    if (len <= block_len(i) - sz) {
236
2.87M
        Hacl_Streaming_Keccak_state s1 = *p;
237
2.87M
        Hacl_Streaming_Keccak_hash_buf block_state1 = s1.block_state;
238
2.87M
        uint8_t *buf = s1.buf;
239
2.87M
        uint64_t total_len1 = s1.total_len;
240
2.87M
        uint32_t sz1;
241
2.87M
        if (total_len1 % (uint64_t)block_len(i) == (uint64_t)0U && total_len1 > (uint64_t)0U) {
242
0
            sz1 = block_len(i);
243
2.87M
        } else {
244
2.87M
            sz1 = (uint32_t)(total_len1 % (uint64_t)block_len(i));
245
2.87M
        }
246
2.87M
        uint8_t *buf2 = buf + sz1;
247
2.87M
        memcpy(buf2, data, len * sizeof(uint8_t));
248
2.87M
        uint64_t total_len2 = total_len1 + (uint64_t)len;
249
2.87M
        *p =
250
2.87M
            ((Hacl_Streaming_Keccak_state){
251
2.87M
                .block_state = block_state1,
252
2.87M
                .buf = buf,
253
2.87M
                .total_len = total_len2 });
254
2.87M
    } else if (sz == (uint32_t)0U) {
255
28.5k
        Hacl_Streaming_Keccak_state s1 = *p;
256
28.5k
        Hacl_Streaming_Keccak_hash_buf block_state1 = s1.block_state;
257
28.5k
        uint8_t *buf = s1.buf;
258
28.5k
        uint64_t total_len1 = s1.total_len;
259
28.5k
        uint32_t sz1;
260
28.5k
        if (total_len1 % (uint64_t)block_len(i) == (uint64_t)0U && total_len1 > (uint64_t)0U) {
261
0
            sz1 = block_len(i);
262
28.5k
        } else {
263
28.5k
            sz1 = (uint32_t)(total_len1 % (uint64_t)block_len(i));
264
28.5k
        }
265
28.5k
        if (!(sz1 == (uint32_t)0U)) {
266
0
            Spec_Hash_Definitions_hash_alg a1 = block_state1.fst;
267
0
            uint64_t *s2 = block_state1.snd;
268
0
            Hacl_Hash_SHA3_update_multi_sha3(a1, s2, buf, block_len(i) / block_len(a1));
269
0
        }
270
28.5k
        uint32_t ite;
271
28.5k
        if ((uint64_t)len % (uint64_t)block_len(i) == (uint64_t)0U && (uint64_t)len > (uint64_t)0U) {
272
200
            ite = block_len(i);
273
28.3k
        } else {
274
28.3k
            ite = (uint32_t)((uint64_t)len % (uint64_t)block_len(i));
275
28.3k
        }
276
28.5k
        uint32_t n_blocks = (len - ite) / block_len(i);
277
28.5k
        uint32_t data1_len = n_blocks * block_len(i);
278
28.5k
        uint32_t data2_len = len - data1_len;
279
28.5k
        uint8_t *data1 = data;
280
28.5k
        uint8_t *data2 = data + data1_len;
281
28.5k
        Spec_Hash_Definitions_hash_alg a1 = block_state1.fst;
282
28.5k
        uint64_t *s2 = block_state1.snd;
283
28.5k
        Hacl_Hash_SHA3_update_multi_sha3(a1, s2, data1, data1_len / block_len(a1));
284
28.5k
        uint8_t *dst = buf;
285
28.5k
        memcpy(dst, data2, data2_len * sizeof(uint8_t));
286
28.5k
        *p =
287
28.5k
            ((Hacl_Streaming_Keccak_state){
288
28.5k
                .block_state = block_state1,
289
28.5k
                .buf = buf,
290
28.5k
                .total_len = total_len1 + (uint64_t)len });
291
137k
    } else {
292
137k
        uint32_t diff = block_len(i) - sz;
293
137k
        uint8_t *data1 = data;
294
137k
        uint8_t *data2 = data + diff;
295
137k
        Hacl_Streaming_Keccak_state s1 = *p;
296
137k
        Hacl_Streaming_Keccak_hash_buf block_state10 = s1.block_state;
297
137k
        uint8_t *buf0 = s1.buf;
298
137k
        uint64_t total_len10 = s1.total_len;
299
137k
        uint32_t sz10;
300
137k
        if (total_len10 % (uint64_t)block_len(i) == (uint64_t)0U && total_len10 > (uint64_t)0U) {
301
22.2k
            sz10 = block_len(i);
302
114k
        } else {
303
114k
            sz10 = (uint32_t)(total_len10 % (uint64_t)block_len(i));
304
114k
        }
305
137k
        uint8_t *buf2 = buf0 + sz10;
306
137k
        memcpy(buf2, data1, diff * sizeof(uint8_t));
307
137k
        uint64_t total_len2 = total_len10 + (uint64_t)diff;
308
137k
        *p =
309
137k
            ((Hacl_Streaming_Keccak_state){
310
137k
                .block_state = block_state10,
311
137k
                .buf = buf0,
312
137k
                .total_len = total_len2 });
313
137k
        Hacl_Streaming_Keccak_state s10 = *p;
314
137k
        Hacl_Streaming_Keccak_hash_buf block_state1 = s10.block_state;
315
137k
        uint8_t *buf = s10.buf;
316
137k
        uint64_t total_len1 = s10.total_len;
317
137k
        uint32_t sz1;
318
137k
        if (total_len1 % (uint64_t)block_len(i) == (uint64_t)0U && total_len1 > (uint64_t)0U) {
319
137k
            sz1 = block_len(i);
320
137k
        } else {
321
0
            sz1 = (uint32_t)(total_len1 % (uint64_t)block_len(i));
322
0
        }
323
137k
        if (!(sz1 == (uint32_t)0U)) {
324
137k
            Spec_Hash_Definitions_hash_alg a1 = block_state1.fst;
325
137k
            uint64_t *s2 = block_state1.snd;
326
137k
            Hacl_Hash_SHA3_update_multi_sha3(a1, s2, buf, block_len(i) / block_len(a1));
327
137k
        }
328
137k
        uint32_t ite;
329
137k
        if (
330
137k
            (uint64_t)(len - diff) % (uint64_t)block_len(i) == (uint64_t)0U && (uint64_t)(len - diff) > (uint64_t)0U) {
331
596
            ite = block_len(i);
332
136k
        } else {
333
136k
            ite = (uint32_t)((uint64_t)(len - diff) % (uint64_t)block_len(i));
334
136k
        }
335
137k
        uint32_t n_blocks = (len - diff - ite) / block_len(i);
336
137k
        uint32_t data1_len = n_blocks * block_len(i);
337
137k
        uint32_t data2_len = len - diff - data1_len;
338
137k
        uint8_t *data11 = data2;
339
137k
        uint8_t *data21 = data2 + data1_len;
340
137k
        Spec_Hash_Definitions_hash_alg a1 = block_state1.fst;
341
137k
        uint64_t *s2 = block_state1.snd;
342
137k
        Hacl_Hash_SHA3_update_multi_sha3(a1, s2, data11, data1_len / block_len(a1));
343
137k
        uint8_t *dst = buf;
344
137k
        memcpy(dst, data21, data2_len * sizeof(uint8_t));
345
137k
        *p =
346
137k
            ((Hacl_Streaming_Keccak_state){
347
137k
                .block_state = block_state1,
348
137k
                .buf = buf,
349
137k
                .total_len = total_len1 + (uint64_t)(len - diff) });
350
137k
    }
351
3.03M
    return Hacl_Streaming_Types_Success;
352
3.03M
}
353
354
static void
355
finish_(
356
    Spec_Hash_Definitions_hash_alg a,
357
    Hacl_Streaming_Keccak_state *p,
358
    uint8_t *dst,
359
    uint32_t l)
360
13.3k
{
361
13.3k
    Hacl_Streaming_Keccak_state scrut0 = *p;
362
13.3k
    Hacl_Streaming_Keccak_hash_buf block_state = scrut0.block_state;
363
13.3k
    uint8_t *buf_ = scrut0.buf;
364
13.3k
    uint64_t total_len = scrut0.total_len;
365
13.3k
    uint32_t r;
366
13.3k
    if (total_len % (uint64_t)block_len(a) == (uint64_t)0U && total_len > (uint64_t)0U) {
367
286
        r = block_len(a);
368
13.0k
    } else {
369
13.0k
        r = (uint32_t)(total_len % (uint64_t)block_len(a));
370
13.0k
    }
371
13.3k
    uint8_t *buf_1 = buf_;
372
13.3k
    uint64_t buf[25U] = { 0U };
373
13.3k
    Hacl_Streaming_Keccak_hash_buf tmp_block_state = { .fst = a, .snd = buf };
374
13.3k
    hash_buf2 scrut = { .fst = block_state, .snd = tmp_block_state };
375
13.3k
    uint64_t *s_dst = scrut.snd.snd;
376
13.3k
    uint64_t *s_src = scrut.fst.snd;
377
13.3k
    memcpy(s_dst, s_src, (uint32_t)25U * sizeof(uint64_t));
378
13.3k
    uint32_t ite;
379
13.3k
    if (r % block_len(a) == (uint32_t)0U && r > (uint32_t)0U) {
380
286
        ite = block_len(a);
381
13.0k
    } else {
382
13.0k
        ite = r % block_len(a);
383
13.0k
    }
384
13.3k
    uint8_t *buf_last = buf_1 + r - ite;
385
13.3k
    uint8_t *buf_multi = buf_1;
386
13.3k
    Spec_Hash_Definitions_hash_alg a1 = tmp_block_state.fst;
387
13.3k
    uint64_t *s0 = tmp_block_state.snd;
388
13.3k
    Hacl_Hash_SHA3_update_multi_sha3(a1, s0, buf_multi, (uint32_t)0U / block_len(a1));
389
13.3k
    Spec_Hash_Definitions_hash_alg a10 = tmp_block_state.fst;
390
13.3k
    uint64_t *s1 = tmp_block_state.snd;
391
13.3k
    Hacl_Hash_SHA3_update_last_sha3(a10, s1, buf_last, r);
392
13.3k
    Spec_Hash_Definitions_hash_alg a11 = tmp_block_state.fst;
393
13.3k
    uint64_t *s = tmp_block_state.snd;
394
13.3k
    if (a11 == Spec_Hash_Definitions_Shake128 || a11 == Spec_Hash_Definitions_Shake256) {
395
0
        Hacl_Impl_SHA3_squeeze(s, block_len(a11), l, dst);
396
0
        return;
397
0
    }
398
13.3k
    Hacl_Impl_SHA3_squeeze(s, block_len(a11), hash_len(a11), dst);
399
13.3k
}
400
401
Hacl_Streaming_Types_error_code
402
Hacl_Streaming_Keccak_finish(Hacl_Streaming_Keccak_state *s, uint8_t *dst)
403
13.3k
{
404
13.3k
    Spec_Hash_Definitions_hash_alg a1 = Hacl_Streaming_Keccak_get_alg(s);
405
13.3k
    if (a1 == Spec_Hash_Definitions_Shake128 || a1 == Spec_Hash_Definitions_Shake256) {
406
0
        return Hacl_Streaming_Types_InvalidAlgorithm;
407
0
    }
408
13.3k
    finish_(a1, s, dst, hash_len(a1));
409
13.3k
    return Hacl_Streaming_Types_Success;
410
13.3k
}
411
412
Hacl_Streaming_Types_error_code
413
Hacl_Streaming_Keccak_squeeze(Hacl_Streaming_Keccak_state *s, uint8_t *dst, uint32_t l)
414
0
{
415
0
    Spec_Hash_Definitions_hash_alg a1 = Hacl_Streaming_Keccak_get_alg(s);
416
0
    if (!(a1 == Spec_Hash_Definitions_Shake128 || a1 == Spec_Hash_Definitions_Shake256)) {
417
0
        return Hacl_Streaming_Types_InvalidAlgorithm;
418
0
    }
419
0
    if (l == (uint32_t)0U) {
420
0
        return Hacl_Streaming_Types_InvalidLength;
421
0
    }
422
0
    finish_(a1, s, dst, l);
423
0
    return Hacl_Streaming_Types_Success;
424
0
}
425
426
uint32_t
427
Hacl_Streaming_Keccak_block_len(Hacl_Streaming_Keccak_state *s)
428
0
{
429
0
    Spec_Hash_Definitions_hash_alg a1 = Hacl_Streaming_Keccak_get_alg(s);
430
0
    return block_len(a1);
431
0
}
432
433
uint32_t
434
Hacl_Streaming_Keccak_hash_len(Hacl_Streaming_Keccak_state *s)
435
0
{
436
0
    Spec_Hash_Definitions_hash_alg a1 = Hacl_Streaming_Keccak_get_alg(s);
437
0
    return hash_len(a1);
438
0
}
439
440
bool
441
Hacl_Streaming_Keccak_is_shake(Hacl_Streaming_Keccak_state *s)
442
0
{
443
0
    Spec_Hash_Definitions_hash_alg uu____0 = Hacl_Streaming_Keccak_get_alg(s);
444
0
    return uu____0 == Spec_Hash_Definitions_Shake128 || uu____0 == Spec_Hash_Definitions_Shake256;
445
0
}
446
447
void
448
Hacl_SHA3_shake128_hacl(
449
    uint32_t inputByteLen,
450
    uint8_t *input,
451
    uint32_t outputByteLen,
452
    uint8_t *output)
453
0
{
454
0
    Hacl_Impl_SHA3_keccak((uint32_t)1344U,
455
0
                          (uint32_t)256U,
456
0
                          inputByteLen,
457
0
                          input,
458
0
                          (uint8_t)0x1FU,
459
0
                          outputByteLen,
460
0
                          output);
461
0
}
462
463
void
464
Hacl_SHA3_shake256_hacl(
465
    uint32_t inputByteLen,
466
    uint8_t *input,
467
    uint32_t outputByteLen,
468
    uint8_t *output)
469
0
{
470
0
    Hacl_Impl_SHA3_keccak((uint32_t)1088U,
471
0
                          (uint32_t)512U,
472
0
                          inputByteLen,
473
0
                          input,
474
0
                          (uint8_t)0x1FU,
475
0
                          outputByteLen,
476
0
                          output);
477
0
}
478
479
void
480
Hacl_SHA3_sha3_224(uint32_t inputByteLen, uint8_t *input, uint8_t *output)
481
0
{
482
0
    Hacl_Impl_SHA3_keccak((uint32_t)1152U,
483
0
                          (uint32_t)448U,
484
0
                          inputByteLen,
485
0
                          input,
486
0
                          (uint8_t)0x06U,
487
0
                          (uint32_t)28U,
488
0
                          output);
489
0
}
490
491
void
492
Hacl_SHA3_sha3_256(uint32_t inputByteLen, uint8_t *input, uint8_t *output)
493
0
{
494
0
    Hacl_Impl_SHA3_keccak((uint32_t)1088U,
495
0
                          (uint32_t)512U,
496
0
                          inputByteLen,
497
0
                          input,
498
0
                          (uint8_t)0x06U,
499
0
                          (uint32_t)32U,
500
0
                          output);
501
0
}
502
503
void
504
Hacl_SHA3_sha3_384(uint32_t inputByteLen, uint8_t *input, uint8_t *output)
505
0
{
506
0
    Hacl_Impl_SHA3_keccak((uint32_t)832U,
507
0
                          (uint32_t)768U,
508
0
                          inputByteLen,
509
0
                          input,
510
0
                          (uint8_t)0x06U,
511
0
                          (uint32_t)48U,
512
0
                          output);
513
0
}
514
515
void
516
Hacl_SHA3_sha3_512(uint32_t inputByteLen, uint8_t *input, uint8_t *output)
517
0
{
518
0
    Hacl_Impl_SHA3_keccak((uint32_t)576U,
519
0
                          (uint32_t)1024U,
520
0
                          inputByteLen,
521
0
                          input,
522
0
                          (uint8_t)0x06U,
523
0
                          (uint32_t)64U,
524
0
                          output);
525
0
}
526
527
static const uint32_t
528
    keccak_rotc[24U] = {
529
        (uint32_t)1U, (uint32_t)3U, (uint32_t)6U, (uint32_t)10U, (uint32_t)15U, (uint32_t)21U,
530
        (uint32_t)28U, (uint32_t)36U, (uint32_t)45U, (uint32_t)55U, (uint32_t)2U, (uint32_t)14U,
531
        (uint32_t)27U, (uint32_t)41U, (uint32_t)56U, (uint32_t)8U, (uint32_t)25U, (uint32_t)43U,
532
        (uint32_t)62U, (uint32_t)18U, (uint32_t)39U, (uint32_t)61U, (uint32_t)20U, (uint32_t)44U
533
    };
534
535
static const uint32_t
536
    keccak_piln[24U] = {
537
        (uint32_t)10U, (uint32_t)7U, (uint32_t)11U, (uint32_t)17U, (uint32_t)18U, (uint32_t)3U,
538
        (uint32_t)5U, (uint32_t)16U, (uint32_t)8U, (uint32_t)21U, (uint32_t)24U, (uint32_t)4U,
539
        (uint32_t)15U, (uint32_t)23U, (uint32_t)19U, (uint32_t)13U, (uint32_t)12U, (uint32_t)2U,
540
        (uint32_t)20U, (uint32_t)14U, (uint32_t)22U, (uint32_t)9U, (uint32_t)6U, (uint32_t)1U
541
    };
542
543
static const uint64_t
544
    keccak_rndc[24U] = {
545
        (uint64_t)0x0000000000000001U, (uint64_t)0x0000000000008082U, (uint64_t)0x800000000000808aU,
546
        (uint64_t)0x8000000080008000U, (uint64_t)0x000000000000808bU, (uint64_t)0x0000000080000001U,
547
        (uint64_t)0x8000000080008081U, (uint64_t)0x8000000000008009U, (uint64_t)0x000000000000008aU,
548
        (uint64_t)0x0000000000000088U, (uint64_t)0x0000000080008009U, (uint64_t)0x000000008000000aU,
549
        (uint64_t)0x000000008000808bU, (uint64_t)0x800000000000008bU, (uint64_t)0x8000000000008089U,
550
        (uint64_t)0x8000000000008003U, (uint64_t)0x8000000000008002U, (uint64_t)0x8000000000000080U,
551
        (uint64_t)0x000000000000800aU, (uint64_t)0x800000008000000aU, (uint64_t)0x8000000080008081U,
552
        (uint64_t)0x8000000000008080U, (uint64_t)0x0000000080000001U, (uint64_t)0x8000000080008008U
553
    };
554
555
void
556
Hacl_Impl_SHA3_state_permute(uint64_t *s)
557
525k
{
558
13.1M
    for (uint32_t i0 = (uint32_t)0U; i0 < (uint32_t)24U; i0++) {
559
12.6M
        uint64_t _C[5U] = { 0U };
560
12.6M
        KRML_MAYBE_FOR5(i,
561
12.6M
                        (uint32_t)0U,
562
12.6M
                        (uint32_t)5U,
563
12.6M
                        (uint32_t)1U,
564
12.6M
                        _C[i] =
565
12.6M
                            s[i + (uint32_t)0U] ^
566
12.6M
                            (s[i + (uint32_t)5U] ^ (s[i + (uint32_t)10U] ^ (s[i + (uint32_t)15U] ^ s[i + (uint32_t)20U]))););
567
12.6M
        KRML_MAYBE_FOR5(i1,
568
12.6M
                        (uint32_t)0U,
569
12.6M
                        (uint32_t)5U,
570
12.6M
                        (uint32_t)1U,
571
12.6M
                        uint64_t uu____0 = _C[(i1 + (uint32_t)1U) % (uint32_t)5U];
572
12.6M
                        uint64_t
573
12.6M
                            _D =
574
12.6M
                                _C[(i1 + (uint32_t)4U) % (uint32_t)5U] ^ (uu____0 << (uint32_t)1U | uu____0 >> (uint32_t)63U);
575
12.6M
                        KRML_MAYBE_FOR5(i,
576
12.6M
                                        (uint32_t)0U,
577
12.6M
                                        (uint32_t)5U,
578
12.6M
                                        (uint32_t)1U,
579
12.6M
                                        s[i1 + (uint32_t)5U * i] = s[i1 + (uint32_t)5U * i] ^ _D;););
580
12.6M
        uint64_t x = s[1U];
581
12.6M
        uint64_t current = x;
582
315M
        for (uint32_t i = (uint32_t)0U; i < (uint32_t)24U; i++) {
583
302M
            uint32_t _Y = keccak_piln[i];
584
302M
            uint32_t r = keccak_rotc[i];
585
302M
            uint64_t temp = s[_Y];
586
302M
            uint64_t uu____1 = current;
587
302M
            s[_Y] = uu____1 << r | uu____1 >> ((uint32_t)64U - r);
588
302M
            current = temp;
589
302M
        }
590
12.6M
        KRML_MAYBE_FOR5(i,
591
12.6M
                        (uint32_t)0U,
592
12.6M
                        (uint32_t)5U,
593
12.6M
                        (uint32_t)1U,
594
12.6M
                        uint64_t
595
12.6M
                            v0 =
596
12.6M
                                s[(uint32_t)0U + (uint32_t)5U * i] ^ (~s[(uint32_t)1U + (uint32_t)5U * i] & s[(uint32_t)2U + (uint32_t)5U * i]);
597
12.6M
                        uint64_t
598
12.6M
                            v1 =
599
12.6M
                                s[(uint32_t)1U + (uint32_t)5U * i] ^ (~s[(uint32_t)2U + (uint32_t)5U * i] & s[(uint32_t)3U + (uint32_t)5U * i]);
600
12.6M
                        uint64_t
601
12.6M
                            v2 =
602
12.6M
                                s[(uint32_t)2U + (uint32_t)5U * i] ^ (~s[(uint32_t)3U + (uint32_t)5U * i] & s[(uint32_t)4U + (uint32_t)5U * i]);
603
12.6M
                        uint64_t
604
12.6M
                            v3 =
605
12.6M
                                s[(uint32_t)3U + (uint32_t)5U * i] ^ (~s[(uint32_t)4U + (uint32_t)5U * i] & s[(uint32_t)0U + (uint32_t)5U * i]);
606
12.6M
                        uint64_t
607
12.6M
                            v4 =
608
12.6M
                                s[(uint32_t)4U + (uint32_t)5U * i] ^ (~s[(uint32_t)0U + (uint32_t)5U * i] & s[(uint32_t)1U + (uint32_t)5U * i]);
609
12.6M
                        s[(uint32_t)0U + (uint32_t)5U * i] = v0;
610
12.6M
                        s[(uint32_t)1U + (uint32_t)5U * i] = v1;
611
12.6M
                        s[(uint32_t)2U + (uint32_t)5U * i] = v2;
612
12.6M
                        s[(uint32_t)3U + (uint32_t)5U * i] = v3;
613
12.6M
                        s[(uint32_t)4U + (uint32_t)5U * i] = v4;);
614
12.6M
        uint64_t c = keccak_rndc[i0];
615
12.6M
        s[0U] = s[0U] ^ c;
616
12.6M
    }
617
525k
}
618
619
void
620
Hacl_Impl_SHA3_loadState(uint32_t rateInBytes, uint8_t *input, uint64_t *s)
621
539k
{
622
539k
    uint8_t block[200U] = { 0U };
623
539k
    memcpy(block, input, rateInBytes * sizeof(uint8_t));
624
14.0M
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)25U; i++) {
625
13.4M
        uint64_t u = load64_le(block + i * (uint32_t)8U);
626
13.4M
        uint64_t x = u;
627
13.4M
        s[i] = s[i] ^ x;
628
13.4M
    }
629
539k
}
630
631
static void
632
storeState(uint32_t rateInBytes, uint64_t *s, uint8_t *res)
633
13.3k
{
634
13.3k
    uint8_t block[200U] = { 0U };
635
347k
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)25U; i++) {
636
333k
        uint64_t sj = s[i];
637
333k
        store64_le(block + i * (uint32_t)8U, sj);
638
333k
    }
639
13.3k
    memcpy(res, block, rateInBytes * sizeof(uint8_t));
640
13.3k
}
641
642
void
643
Hacl_Impl_SHA3_absorb_inner(uint32_t rateInBytes, uint8_t *block, uint64_t *s)
644
512k
{
645
512k
    Hacl_Impl_SHA3_loadState(rateInBytes, block, s);
646
512k
    Hacl_Impl_SHA3_state_permute(s);
647
512k
}
648
649
static void
650
absorb(
651
    uint64_t *s,
652
    uint32_t rateInBytes,
653
    uint32_t inputByteLen,
654
    uint8_t *input,
655
    uint8_t delimitedSuffix)
656
0
{
657
0
    uint32_t n_blocks = inputByteLen / rateInBytes;
658
0
    uint32_t rem = inputByteLen % rateInBytes;
659
0
    for (uint32_t i = (uint32_t)0U; i < n_blocks; i++) {
660
0
        uint8_t *block = input + i * rateInBytes;
661
0
        Hacl_Impl_SHA3_absorb_inner(rateInBytes, block, s);
662
0
    }
663
0
    uint8_t *last = input + n_blocks * rateInBytes;
664
0
    uint8_t lastBlock_[200U] = { 0U };
665
0
    uint8_t *lastBlock = lastBlock_;
666
0
    memcpy(lastBlock, last, rem * sizeof(uint8_t));
667
0
    lastBlock[rem] = delimitedSuffix;
668
0
    Hacl_Impl_SHA3_loadState(rateInBytes, lastBlock, s);
669
0
    if (!((delimitedSuffix & (uint8_t)0x80U) == (uint8_t)0U) && rem == rateInBytes - (uint32_t)1U) {
670
0
        Hacl_Impl_SHA3_state_permute(s);
671
0
    }
672
0
    uint8_t nextBlock_[200U] = { 0U };
673
0
    uint8_t *nextBlock = nextBlock_;
674
0
    nextBlock[rateInBytes - (uint32_t)1U] = (uint8_t)0x80U;
675
0
    Hacl_Impl_SHA3_loadState(rateInBytes, nextBlock, s);
676
0
    Hacl_Impl_SHA3_state_permute(s);
677
0
}
678
679
void
680
Hacl_Impl_SHA3_squeeze(
681
    uint64_t *s,
682
    uint32_t rateInBytes,
683
    uint32_t outputByteLen,
684
    uint8_t *output)
685
13.3k
{
686
13.3k
    uint32_t outBlocks = outputByteLen / rateInBytes;
687
13.3k
    uint32_t remOut = outputByteLen % rateInBytes;
688
13.3k
    uint8_t *last = output + outputByteLen - remOut;
689
13.3k
    uint8_t *blocks = output;
690
13.3k
    for (uint32_t i = (uint32_t)0U; i < outBlocks; i++) {
691
0
        storeState(rateInBytes, s, blocks + i * rateInBytes);
692
0
        Hacl_Impl_SHA3_state_permute(s);
693
0
    }
694
13.3k
    storeState(remOut, s, last);
695
13.3k
}
696
697
void
698
Hacl_Impl_SHA3_keccak(
699
    uint32_t rate,
700
    uint32_t capacity,
701
    uint32_t inputByteLen,
702
    uint8_t *input,
703
    uint8_t delimitedSuffix,
704
    uint32_t outputByteLen,
705
    uint8_t *output)
706
0
{
707
0
    KRML_HOST_IGNORE(capacity);
708
0
    uint32_t rateInBytes = rate / (uint32_t)8U;
709
0
    uint64_t s[25U] = { 0U };
710
0
    absorb(s, rateInBytes, inputByteLen, input, delimitedSuffix);
711
0
    Hacl_Impl_SHA3_squeeze(s, rateInBytes, outputByteLen, output);
712
0
}