Debian Codegraph @

Modules/_hacl/Hacl_Hash_Blake2b_Simd256.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_Blake2b_Simd256.h"
27
28 #include "Hacl_Streaming_Types.h"
29
30 #include "Hacl_Hash_Blake2b.h"
31 #include "internal/Hacl_Streaming_Types.h"
32 #include "internal/Hacl_Impl_Blake2_Constants.h"
33 #include "internal/Hacl_Hash_Blake2b.h"
34 #include "lib_memzero0.h"
35
36 static inline void
37 update_block(
38 Lib_IntVector_Intrinsics_vec256 *wv,
39 Lib_IntVector_Intrinsics_vec256 *hash,
40 bool flag,
41 bool last_node,
42 FStar_UInt128_uint128 totlen,
43 uint8_t *d
44 )
45 {
46 uint64_t m_w[16U] = { 0U };
47 KRML_MAYBE_FOR16(i,
48 0U,
49 16U,
50 1U,
51 uint64_t *os = m_w;
52 uint8_t *bj = d + i * 8U;
53 uint64_t u = load64_le(bj);
54 uint64_t r = u;
55 uint64_t x = r;
56 os[i] = x;);
57 Lib_IntVector_Intrinsics_vec256 mask = Lib_IntVector_Intrinsics_vec256_zero;
58 uint64_t wv_14;
59 if (flag)
60 {
61 wv_14 = 0xFFFFFFFFFFFFFFFFULL;
62 }
63 else
64 {
65 wv_14 = 0ULL;
66 }
67 uint64_t wv_15;
68 if (last_node)
69 {
70 wv_15 = 0xFFFFFFFFFFFFFFFFULL;
71 }
72 else
73 {
74 wv_15 = 0ULL;
75 }
76 mask =
77 Lib_IntVector_Intrinsics_vec256_load64s(FStar_UInt128_uint128_to_uint64(totlen),
78 FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(totlen, 64U)),
79 wv_14,
80 wv_15);
81 memcpy(wv, hash, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
82 Lib_IntVector_Intrinsics_vec256 *wv3 = wv + 3U;
83 wv3[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv3[0U], mask);
84 KRML_MAYBE_FOR12(i,
85 0U,
86 12U,
87 1U,
88 uint32_t start_idx = i % 10U * 16U;
89 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 m_st[4U] KRML_POST_ALIGN(32) = { 0U };
90 Lib_IntVector_Intrinsics_vec256 *r0 = m_st;
91 Lib_IntVector_Intrinsics_vec256 *r1 = m_st + 1U;
92 Lib_IntVector_Intrinsics_vec256 *r20 = m_st + 2U;
93 Lib_IntVector_Intrinsics_vec256 *r30 = m_st + 3U;
94 uint32_t s0 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 0U];
95 uint32_t s1 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 1U];
96 uint32_t s2 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 2U];
97 uint32_t s3 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 3U];
98 uint32_t s4 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 4U];
99 uint32_t s5 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 5U];
100 uint32_t s6 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 6U];
101 uint32_t s7 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 7U];
102 uint32_t s8 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 8U];
103 uint32_t s9 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 9U];
104 uint32_t s10 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 10U];
105 uint32_t s11 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 11U];
106 uint32_t s12 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 12U];
107 uint32_t s13 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 13U];
108 uint32_t s14 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 14U];
109 uint32_t s15 = Hacl_Hash_Blake2b_sigmaTable[start_idx + 15U];
110 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(m_w[s0], m_w[s2], m_w[s4], m_w[s6]);
111 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(m_w[s1], m_w[s3], m_w[s5], m_w[s7]);
112 r20[0U] = Lib_IntVector_Intrinsics_vec256_load64s(m_w[s8], m_w[s10], m_w[s12], m_w[s14]);
113 r30[0U] = Lib_IntVector_Intrinsics_vec256_load64s(m_w[s9], m_w[s11], m_w[s13], m_w[s15]);
114 Lib_IntVector_Intrinsics_vec256 *x = m_st;
115 Lib_IntVector_Intrinsics_vec256 *y = m_st + 1U;
116 Lib_IntVector_Intrinsics_vec256 *z = m_st + 2U;
117 Lib_IntVector_Intrinsics_vec256 *w = m_st + 3U;
118 uint32_t a = 0U;
119 uint32_t b0 = 1U;
120 uint32_t c0 = 2U;
121 uint32_t d10 = 3U;
122 Lib_IntVector_Intrinsics_vec256 *wv_a0 = wv + a * 1U;
123 Lib_IntVector_Intrinsics_vec256 *wv_b0 = wv + b0 * 1U;
124 wv_a0[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a0[0U], wv_b0[0U]);
125 wv_a0[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a0[0U], x[0U]);
126 Lib_IntVector_Intrinsics_vec256 *wv_a1 = wv + d10 * 1U;
127 Lib_IntVector_Intrinsics_vec256 *wv_b1 = wv + a * 1U;
128 wv_a1[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a1[0U], wv_b1[0U]);
129 wv_a1[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a1[0U], 32U);
130 Lib_IntVector_Intrinsics_vec256 *wv_a2 = wv + c0 * 1U;
131 Lib_IntVector_Intrinsics_vec256 *wv_b2 = wv + d10 * 1U;
132 wv_a2[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a2[0U], wv_b2[0U]);
133 Lib_IntVector_Intrinsics_vec256 *wv_a3 = wv + b0 * 1U;
134 Lib_IntVector_Intrinsics_vec256 *wv_b3 = wv + c0 * 1U;
135 wv_a3[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a3[0U], wv_b3[0U]);
136 wv_a3[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a3[0U], 24U);
137 Lib_IntVector_Intrinsics_vec256 *wv_a4 = wv + a * 1U;
138 Lib_IntVector_Intrinsics_vec256 *wv_b4 = wv + b0 * 1U;
139 wv_a4[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a4[0U], wv_b4[0U]);
140 wv_a4[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a4[0U], y[0U]);
141 Lib_IntVector_Intrinsics_vec256 *wv_a5 = wv + d10 * 1U;
142 Lib_IntVector_Intrinsics_vec256 *wv_b5 = wv + a * 1U;
143 wv_a5[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a5[0U], wv_b5[0U]);
144 wv_a5[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a5[0U], 16U);
145 Lib_IntVector_Intrinsics_vec256 *wv_a6 = wv + c0 * 1U;
146 Lib_IntVector_Intrinsics_vec256 *wv_b6 = wv + d10 * 1U;
147 wv_a6[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a6[0U], wv_b6[0U]);
148 Lib_IntVector_Intrinsics_vec256 *wv_a7 = wv + b0 * 1U;
149 Lib_IntVector_Intrinsics_vec256 *wv_b7 = wv + c0 * 1U;
150 wv_a7[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a7[0U], wv_b7[0U]);
151 wv_a7[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a7[0U], 63U);
152 Lib_IntVector_Intrinsics_vec256 *r10 = wv + 1U;
153 Lib_IntVector_Intrinsics_vec256 *r21 = wv + 2U;
154 Lib_IntVector_Intrinsics_vec256 *r31 = wv + 3U;
155 Lib_IntVector_Intrinsics_vec256 v00 = r10[0U];
156 Lib_IntVector_Intrinsics_vec256
157 v1 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v00, 1U);
158 r10[0U] = v1;
159 Lib_IntVector_Intrinsics_vec256 v01 = r21[0U];
160 Lib_IntVector_Intrinsics_vec256
161 v10 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v01, 2U);
162 r21[0U] = v10;
163 Lib_IntVector_Intrinsics_vec256 v02 = r31[0U];
164 Lib_IntVector_Intrinsics_vec256
165 v11 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v02, 3U);
166 r31[0U] = v11;
167 uint32_t a0 = 0U;
168 uint32_t b = 1U;
169 uint32_t c = 2U;
170 uint32_t d1 = 3U;
171 Lib_IntVector_Intrinsics_vec256 *wv_a = wv + a0 * 1U;
172 Lib_IntVector_Intrinsics_vec256 *wv_b8 = wv + b * 1U;
173 wv_a[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a[0U], wv_b8[0U]);
174 wv_a[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a[0U], z[0U]);
175 Lib_IntVector_Intrinsics_vec256 *wv_a8 = wv + d1 * 1U;
176 Lib_IntVector_Intrinsics_vec256 *wv_b9 = wv + a0 * 1U;
177 wv_a8[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a8[0U], wv_b9[0U]);
178 wv_a8[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a8[0U], 32U);
179 Lib_IntVector_Intrinsics_vec256 *wv_a9 = wv + c * 1U;
180 Lib_IntVector_Intrinsics_vec256 *wv_b10 = wv + d1 * 1U;
181 wv_a9[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a9[0U], wv_b10[0U]);
182 Lib_IntVector_Intrinsics_vec256 *wv_a10 = wv + b * 1U;
183 Lib_IntVector_Intrinsics_vec256 *wv_b11 = wv + c * 1U;
184 wv_a10[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a10[0U], wv_b11[0U]);
185 wv_a10[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a10[0U], 24U);
186 Lib_IntVector_Intrinsics_vec256 *wv_a11 = wv + a0 * 1U;
187 Lib_IntVector_Intrinsics_vec256 *wv_b12 = wv + b * 1U;
188 wv_a11[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a11[0U], wv_b12[0U]);
189 wv_a11[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a11[0U], w[0U]);
190 Lib_IntVector_Intrinsics_vec256 *wv_a12 = wv + d1 * 1U;
191 Lib_IntVector_Intrinsics_vec256 *wv_b13 = wv + a0 * 1U;
192 wv_a12[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a12[0U], wv_b13[0U]);
193 wv_a12[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a12[0U], 16U);
194 Lib_IntVector_Intrinsics_vec256 *wv_a13 = wv + c * 1U;
195 Lib_IntVector_Intrinsics_vec256 *wv_b14 = wv + d1 * 1U;
196 wv_a13[0U] = Lib_IntVector_Intrinsics_vec256_add64(wv_a13[0U], wv_b14[0U]);
197 Lib_IntVector_Intrinsics_vec256 *wv_a14 = wv + b * 1U;
198 Lib_IntVector_Intrinsics_vec256 *wv_b = wv + c * 1U;
199 wv_a14[0U] = Lib_IntVector_Intrinsics_vec256_xor(wv_a14[0U], wv_b[0U]);
200 wv_a14[0U] = Lib_IntVector_Intrinsics_vec256_rotate_right64(wv_a14[0U], 63U);
201 Lib_IntVector_Intrinsics_vec256 *r11 = wv + 1U;
202 Lib_IntVector_Intrinsics_vec256 *r2 = wv + 2U;
203 Lib_IntVector_Intrinsics_vec256 *r3 = wv + 3U;
204 Lib_IntVector_Intrinsics_vec256 v0 = r11[0U];
205 Lib_IntVector_Intrinsics_vec256
206 v12 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v0, 3U);
207 r11[0U] = v12;
208 Lib_IntVector_Intrinsics_vec256 v03 = r2[0U];
209 Lib_IntVector_Intrinsics_vec256
210 v13 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v03, 2U);
211 r2[0U] = v13;
212 Lib_IntVector_Intrinsics_vec256 v04 = r3[0U];
213 Lib_IntVector_Intrinsics_vec256
214 v14 = Lib_IntVector_Intrinsics_vec256_rotate_right_lanes64(v04, 1U);
215 r3[0U] = v14;);
216 Lib_IntVector_Intrinsics_vec256 *s0 = hash;
217 Lib_IntVector_Intrinsics_vec256 *s1 = hash + 1U;
218 Lib_IntVector_Intrinsics_vec256 *r0 = wv;
219 Lib_IntVector_Intrinsics_vec256 *r1 = wv + 1U;
220 Lib_IntVector_Intrinsics_vec256 *r2 = wv + 2U;
221 Lib_IntVector_Intrinsics_vec256 *r3 = wv + 3U;
222 s0[0U] = Lib_IntVector_Intrinsics_vec256_xor(s0[0U], r0[0U]);
223 s0[0U] = Lib_IntVector_Intrinsics_vec256_xor(s0[0U], r2[0U]);
224 s1[0U] = Lib_IntVector_Intrinsics_vec256_xor(s1[0U], r1[0U]);
225 s1[0U] = Lib_IntVector_Intrinsics_vec256_xor(s1[0U], r3[0U]);
226 }
227
228 void
229 Hacl_Hash_Blake2b_Simd256_init(Lib_IntVector_Intrinsics_vec256 *hash, uint32_t kk, uint32_t nn)
230 {
231 uint8_t salt[16U] = { 0U };
232 uint8_t personal[16U] = { 0U };
233 Hacl_Hash_Blake2b_blake2_params
234 p =
235 {
236 .digest_length = 64U, .key_length = 0U, .fanout = 1U, .depth = 1U, .leaf_length = 0U,
237 .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt, .personal = personal
238 };
239 uint64_t tmp[8U] = { 0U };
240 Lib_IntVector_Intrinsics_vec256 *r0 = hash;
241 Lib_IntVector_Intrinsics_vec256 *r1 = hash + 1U;
242 Lib_IntVector_Intrinsics_vec256 *r2 = hash + 2U;
243 Lib_IntVector_Intrinsics_vec256 *r3 = hash + 3U;
244 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
245 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
246 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
247 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
248 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
249 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
250 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
251 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
252 r2[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0, iv1, iv2, iv3);
253 r3[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4, iv5, iv6, iv7);
254 uint8_t kk1 = (uint8_t)kk;
255 uint8_t nn1 = (uint8_t)nn;
256 KRML_MAYBE_FOR2(i,
257 0U,
258 2U,
259 1U,
260 uint64_t *os = tmp + 4U;
261 uint8_t *bj = p.salt + i * 8U;
262 uint64_t u = load64_le(bj);
263 uint64_t r = u;
264 uint64_t x = r;
265 os[i] = x;);
266 KRML_MAYBE_FOR2(i,
267 0U,
268 2U,
269 1U,
270 uint64_t *os = tmp + 6U;
271 uint8_t *bj = p.personal + i * 8U;
272 uint64_t u = load64_le(bj);
273 uint64_t r = u;
274 uint64_t x = r;
275 os[i] = x;);
276 tmp[0U] =
277 (uint64_t)nn1 ^
278 ((uint64_t)kk1 << 8U ^
279 ((uint64_t)p.fanout << 16U ^ ((uint64_t)p.depth << 24U ^ (uint64_t)p.leaf_length << 32U)));
280 tmp[1U] = p.node_offset;
281 tmp[2U] = (uint64_t)p.node_depth ^ (uint64_t)p.inner_length << 8U;
282 tmp[3U] = 0ULL;
283 uint64_t tmp0 = tmp[0U];
284 uint64_t tmp1 = tmp[1U];
285 uint64_t tmp2 = tmp[2U];
286 uint64_t tmp3 = tmp[3U];
287 uint64_t tmp4 = tmp[4U];
288 uint64_t tmp5 = tmp[5U];
289 uint64_t tmp6 = tmp[6U];
290 uint64_t tmp7 = tmp[7U];
291 uint64_t iv0_ = iv0 ^ tmp0;
292 uint64_t iv1_ = iv1 ^ tmp1;
293 uint64_t iv2_ = iv2 ^ tmp2;
294 uint64_t iv3_ = iv3 ^ tmp3;
295 uint64_t iv4_ = iv4 ^ tmp4;
296 uint64_t iv5_ = iv5 ^ tmp5;
297 uint64_t iv6_ = iv6 ^ tmp6;
298 uint64_t iv7_ = iv7 ^ tmp7;
299 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0_, iv1_, iv2_, iv3_);
300 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4_, iv5_, iv6_, iv7_);
301 }
302
303 static void
304 update_key(
305 Lib_IntVector_Intrinsics_vec256 *wv,
306 Lib_IntVector_Intrinsics_vec256 *hash,
307 uint32_t kk,
308 uint8_t *k,
309 uint32_t ll
310 )
311 {
312 FStar_UInt128_uint128 lb = FStar_UInt128_uint64_to_uint128((uint64_t)128U);
313 uint8_t b[128U] = { 0U };
314 memcpy(b, k, kk * sizeof (uint8_t));
315 if (ll == 0U)
316 {
317 update_block(wv, hash, true, false, lb, b);
318 }
319 else
320 {
321 update_block(wv, hash, false, false, lb, b);
322 }
323 Lib_Memzero0_memzero(b, 128U, uint8_t, void *);
324 }
325
326 void
327 Hacl_Hash_Blake2b_Simd256_update_multi(
328 uint32_t len,
329 Lib_IntVector_Intrinsics_vec256 *wv,
330 Lib_IntVector_Intrinsics_vec256 *hash,
331 FStar_UInt128_uint128 prev,
332 uint8_t *blocks,
333 uint32_t nb
334 )
335 {
336 KRML_MAYBE_UNUSED_VAR(len);
337 for (uint32_t i = 0U; i < nb; i++)
338 {
339 FStar_UInt128_uint128
340 totlen =
341 FStar_UInt128_add_mod(prev,
342 FStar_UInt128_uint64_to_uint128((uint64_t)((i + 1U) * 128U)));
343 uint8_t *b = blocks + i * 128U;
344 update_block(wv, hash, false, false, totlen, b);
345 }
346 }
347
348 void
349 Hacl_Hash_Blake2b_Simd256_update_last(
350 uint32_t len,
351 Lib_IntVector_Intrinsics_vec256 *wv,
352 Lib_IntVector_Intrinsics_vec256 *hash,
353 bool last_node,
354 FStar_UInt128_uint128 prev,
355 uint32_t rem,
356 uint8_t *d
357 )
358 {
359 uint8_t b[128U] = { 0U };
360 uint8_t *last = d + len - rem;
361 memcpy(b, last, rem * sizeof (uint8_t));
362 FStar_UInt128_uint128
363 totlen = FStar_UInt128_add_mod(prev, FStar_UInt128_uint64_to_uint128((uint64_t)len));
364 update_block(wv, hash, true, last_node, totlen, b);
365 Lib_Memzero0_memzero(b, 128U, uint8_t, void *);
366 }
367
368 static inline void
369 update_blocks(
370 uint32_t len,
371 Lib_IntVector_Intrinsics_vec256 *wv,
372 Lib_IntVector_Intrinsics_vec256 *hash,
373 FStar_UInt128_uint128 prev,
374 uint8_t *blocks
375 )
376 {
377 uint32_t nb0 = len / 128U;
378 uint32_t rem0 = len % 128U;
379 uint32_t nb;
380 if (rem0 == 0U && nb0 > 0U)
381 {
382 nb = nb0 - 1U;
383 }
384 else
385 {
386 nb = nb0;
387 }
388 uint32_t rem;
389 if (rem0 == 0U && nb0 > 0U)
390 {
391 rem = 128U;
392 }
393 else
394 {
395 rem = rem0;
396 }
397 Hacl_Hash_Blake2b_Simd256_update_multi(len, wv, hash, prev, blocks, nb);
398 Hacl_Hash_Blake2b_Simd256_update_last(len, wv, hash, false, prev, rem, blocks);
399 }
400
401 static inline void
402 update(
403 Lib_IntVector_Intrinsics_vec256 *wv,
404 Lib_IntVector_Intrinsics_vec256 *hash,
405 uint32_t kk,
406 uint8_t *k,
407 uint32_t ll,
408 uint8_t *d
409 )
410 {
411 FStar_UInt128_uint128 lb = FStar_UInt128_uint64_to_uint128((uint64_t)128U);
412 if (kk > 0U)
413 {
414 update_key(wv, hash, kk, k, ll);
415 if (!(ll == 0U))
416 {
417 update_blocks(ll, wv, hash, lb, d);
418 return;
419 }
420 return;
421 }
422 update_blocks(ll, wv, hash, FStar_UInt128_uint64_to_uint128((uint64_t)0U), d);
423 }
424
425 void
426 Hacl_Hash_Blake2b_Simd256_finish(
427 uint32_t nn,
428 uint8_t *output,
429 Lib_IntVector_Intrinsics_vec256 *hash
430 )
431 {
432 uint8_t b[64U] = { 0U };
433 uint8_t *first = b;
434 uint8_t *second = b + 32U;
435 Lib_IntVector_Intrinsics_vec256 *row0 = hash;
436 Lib_IntVector_Intrinsics_vec256 *row1 = hash + 1U;
437 Lib_IntVector_Intrinsics_vec256_store64_le(first, row0[0U]);
438 Lib_IntVector_Intrinsics_vec256_store64_le(second, row1[0U]);
439 uint8_t *final = b;
440 memcpy(output, final, nn * sizeof (uint8_t));
441 Lib_Memzero0_memzero(b, 64U, uint8_t, void *);
442 }
443
444 void
445 Hacl_Hash_Blake2b_Simd256_load_state256b_from_state32(
446 Lib_IntVector_Intrinsics_vec256 *st,
447 uint64_t *st32
448 )
449 {
450 Lib_IntVector_Intrinsics_vec256 *r0 = st;
451 Lib_IntVector_Intrinsics_vec256 *r1 = st + 1U;
452 Lib_IntVector_Intrinsics_vec256 *r2 = st + 2U;
453 Lib_IntVector_Intrinsics_vec256 *r3 = st + 3U;
454 uint64_t *b0 = st32;
455 uint64_t *b1 = st32 + 4U;
456 uint64_t *b2 = st32 + 8U;
457 uint64_t *b3 = st32 + 12U;
458 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(b0[0U], b0[1U], b0[2U], b0[3U]);
459 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(b1[0U], b1[1U], b1[2U], b1[3U]);
460 r2[0U] = Lib_IntVector_Intrinsics_vec256_load64s(b2[0U], b2[1U], b2[2U], b2[3U]);
461 r3[0U] = Lib_IntVector_Intrinsics_vec256_load64s(b3[0U], b3[1U], b3[2U], b3[3U]);
462 }
463
464 void
465 Hacl_Hash_Blake2b_Simd256_store_state256b_to_state32(
466 uint64_t *st32,
467 Lib_IntVector_Intrinsics_vec256 *st
468 )
469 {
470 Lib_IntVector_Intrinsics_vec256 *r0 = st;
471 Lib_IntVector_Intrinsics_vec256 *r1 = st + 1U;
472 Lib_IntVector_Intrinsics_vec256 *r2 = st + 2U;
473 Lib_IntVector_Intrinsics_vec256 *r3 = st + 3U;
474 uint64_t *b0 = st32;
475 uint64_t *b1 = st32 + 4U;
476 uint64_t *b2 = st32 + 8U;
477 uint64_t *b3 = st32 + 12U;
478 uint8_t b8[32U] = { 0U };
479 Lib_IntVector_Intrinsics_vec256_store64_le(b8, r0[0U]);
480 KRML_MAYBE_FOR4(i,
481 0U,
482 4U,
483 1U,
484 uint64_t *os = b0;
485 uint8_t *bj = b8 + i * 8U;
486 uint64_t u = load64_le(bj);
487 uint64_t r = u;
488 uint64_t x = r;
489 os[i] = x;);
490 uint8_t b80[32U] = { 0U };
491 Lib_IntVector_Intrinsics_vec256_store64_le(b80, r1[0U]);
492 KRML_MAYBE_FOR4(i,
493 0U,
494 4U,
495 1U,
496 uint64_t *os = b1;
497 uint8_t *bj = b80 + i * 8U;
498 uint64_t u = load64_le(bj);
499 uint64_t r = u;
500 uint64_t x = r;
501 os[i] = x;);
502 uint8_t b81[32U] = { 0U };
503 Lib_IntVector_Intrinsics_vec256_store64_le(b81, r2[0U]);
504 KRML_MAYBE_FOR4(i,
505 0U,
506 4U,
507 1U,
508 uint64_t *os = b2;
509 uint8_t *bj = b81 + i * 8U;
510 uint64_t u = load64_le(bj);
511 uint64_t r = u;
512 uint64_t x = r;
513 os[i] = x;);
514 uint8_t b82[32U] = { 0U };
515 Lib_IntVector_Intrinsics_vec256_store64_le(b82, r3[0U]);
516 KRML_MAYBE_FOR4(i,
517 0U,
518 4U,
519 1U,
520 uint64_t *os = b3;
521 uint8_t *bj = b82 + i * 8U;
522 uint64_t u = load64_le(bj);
523 uint64_t r = u;
524 uint64_t x = r;
525 os[i] = x;);
526 }
527
528 Lib_IntVector_Intrinsics_vec256 *Hacl_Hash_Blake2b_Simd256_malloc_internal_state_with_key(void)
529 {
530 Lib_IntVector_Intrinsics_vec256
531 *buf =
532 (Lib_IntVector_Intrinsics_vec256 *)KRML_ALIGNED_MALLOC(32,
533 sizeof (Lib_IntVector_Intrinsics_vec256) * 4U);
534 if (buf != NULL)
535 {
536 memset(buf, 0U, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
537 }
538 return buf;
539 }
540
541 void
542 Hacl_Hash_Blake2b_Simd256_update_multi_no_inline(
543 Lib_IntVector_Intrinsics_vec256 *s,
544 FStar_UInt128_uint128 ev,
545 uint8_t *blocks,
546 uint32_t n
547 )
548 {
549 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 wv[4U] KRML_POST_ALIGN(32) = { 0U };
550 Hacl_Hash_Blake2b_Simd256_update_multi(n * 128U, wv, s, ev, blocks, n);
551 }
552
553 void
554 Hacl_Hash_Blake2b_Simd256_update_last_no_inline(
555 Lib_IntVector_Intrinsics_vec256 *s,
556 FStar_UInt128_uint128 prev,
557 uint8_t *input,
558 uint32_t input_len
559 )
560 {
561 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 wv[4U] KRML_POST_ALIGN(32) = { 0U };
562 Hacl_Hash_Blake2b_Simd256_update_last(input_len, wv, s, false, prev, input_len, input);
563 }
564
565 void
566 Hacl_Hash_Blake2b_Simd256_copy_internal_state(
567 Lib_IntVector_Intrinsics_vec256 *src,
568 Lib_IntVector_Intrinsics_vec256 *dst
569 )
570 {
571 memcpy(dst, src, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
572 }
573
574 typedef struct
575 option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256____s
576 {
577 Hacl_Streaming_Types_optional tag;
578 Hacl_Hash_Blake2b_Simd256_block_state_t v;
579 }
580 option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___;
581
582 static Hacl_Hash_Blake2b_Simd256_state_t
583 *malloc_raw(Hacl_Hash_Blake2b_index kk, Hacl_Hash_Blake2b_params_and_key key)
584 {
585 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
586 if (buf == NULL)
587 {
588 return NULL;
589 }
590 uint8_t *buf1 = buf;
591 Lib_IntVector_Intrinsics_vec256
592 *wv0 =
593 (Lib_IntVector_Intrinsics_vec256 *)KRML_ALIGNED_MALLOC(32,
594 sizeof (Lib_IntVector_Intrinsics_vec256) * 4U);
595 if (wv0 != NULL)
596 {
597 memset(wv0, 0U, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
598 }
599 option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___
600 block_state;
601 if (wv0 == NULL)
602 {
603 block_state =
604 (
605 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
606 .tag = Hacl_Streaming_Types_None
607 }
608 );
609 }
610 else
611 {
612 Lib_IntVector_Intrinsics_vec256
613 *b =
614 (Lib_IntVector_Intrinsics_vec256 *)KRML_ALIGNED_MALLOC(32,
615 sizeof (Lib_IntVector_Intrinsics_vec256) * 4U);
616 if (b != NULL)
617 {
618 memset(b, 0U, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
619 }
620 if (b == NULL)
621 {
622 KRML_ALIGNED_FREE(wv0);
623 block_state =
624 (
625 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
626 .tag = Hacl_Streaming_Types_None
627 }
628 );
629 }
630 else
631 {
632 block_state =
633 (
634 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
635 .tag = Hacl_Streaming_Types_Some,
636 .v = {
637 .fst = kk.key_length,
638 .snd = kk.digest_length,
639 .thd = kk.last_node,
640 .f3 = { .fst = wv0, .snd = b }
641 }
642 }
643 );
644 }
645 }
646 if (block_state.tag == Hacl_Streaming_Types_None)
647 {
648 KRML_HOST_FREE(buf1);
649 return NULL;
650 }
651 if (block_state.tag == Hacl_Streaming_Types_Some)
652 {
653 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = block_state.v;
654 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
655 switch (k_)
656 {
657 case Hacl_Streaming_Types_None:
658 {
659 return NULL;
660 }
661 case Hacl_Streaming_Types_Some:
662 {
663 uint8_t kk10 = kk.key_length;
664 uint32_t ite;
665 if (kk10 != 0U)
666 {
667 ite = 128U;
668 }
669 else
670 {
671 ite = 0U;
672 }
673 Hacl_Hash_Blake2b_Simd256_state_t
674 s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)ite };
675 Hacl_Hash_Blake2b_Simd256_state_t
676 *p =
677 (Hacl_Hash_Blake2b_Simd256_state_t *)KRML_HOST_MALLOC(sizeof (
678 Hacl_Hash_Blake2b_Simd256_state_t
679 ));
680 if (p != NULL)
681 {
682 p[0U] = s;
683 }
684 if (p == NULL)
685 {
686 Lib_IntVector_Intrinsics_vec256 *b = block_state1.f3.snd;
687 Lib_IntVector_Intrinsics_vec256 *wv = block_state1.f3.fst;
688 KRML_ALIGNED_FREE(wv);
689 KRML_ALIGNED_FREE(b);
690 KRML_HOST_FREE(buf1);
691 return NULL;
692 }
693 Hacl_Hash_Blake2b_blake2_params *p1 = key.fst;
694 uint8_t kk1 = p1->key_length;
695 uint8_t nn = p1->digest_length;
696 bool last_node = block_state1.thd;
697 Hacl_Hash_Blake2b_index
698 i = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
699 Lib_IntVector_Intrinsics_vec256 *h = block_state1.f3.snd;
700 uint32_t kk20 = (uint32_t)i.key_length;
701 uint8_t *k_2 = key.snd;
702 if (!(kk20 == 0U))
703 {
704 uint8_t *sub_b = buf1 + kk20;
705 memset(sub_b, 0U, (128U - kk20) * sizeof (uint8_t));
706 memcpy(buf1, k_2, kk20 * sizeof (uint8_t));
707 }
708 Hacl_Hash_Blake2b_blake2_params pv = p1[0U];
709 uint64_t tmp[8U] = { 0U };
710 Lib_IntVector_Intrinsics_vec256 *r0 = h;
711 Lib_IntVector_Intrinsics_vec256 *r1 = h + 1U;
712 Lib_IntVector_Intrinsics_vec256 *r2 = h + 2U;
713 Lib_IntVector_Intrinsics_vec256 *r3 = h + 3U;
714 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
715 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
716 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
717 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
718 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
719 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
720 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
721 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
722 r2[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0, iv1, iv2, iv3);
723 r3[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4, iv5, iv6, iv7);
724 uint8_t kk2 = pv.key_length;
725 uint8_t nn1 = pv.digest_length;
726 KRML_MAYBE_FOR2(i0,
727 0U,
728 2U,
729 1U,
730 uint64_t *os = tmp + 4U;
731 uint8_t *bj = pv.salt + i0 * 8U;
732 uint64_t u = load64_le(bj);
733 uint64_t r4 = u;
734 uint64_t x = r4;
735 os[i0] = x;);
736 KRML_MAYBE_FOR2(i0,
737 0U,
738 2U,
739 1U,
740 uint64_t *os = tmp + 6U;
741 uint8_t *bj = pv.personal + i0 * 8U;
742 uint64_t u = load64_le(bj);
743 uint64_t r4 = u;
744 uint64_t x = r4;
745 os[i0] = x;);
746 tmp[0U] =
747 (uint64_t)nn1 ^
748 ((uint64_t)kk2 << 8U ^
749 ((uint64_t)pv.fanout << 16U ^
750 ((uint64_t)pv.depth << 24U ^ (uint64_t)pv.leaf_length << 32U)));
751 tmp[1U] = pv.node_offset;
752 tmp[2U] = (uint64_t)pv.node_depth ^ (uint64_t)pv.inner_length << 8U;
753 tmp[3U] = 0ULL;
754 uint64_t tmp0 = tmp[0U];
755 uint64_t tmp1 = tmp[1U];
756 uint64_t tmp2 = tmp[2U];
757 uint64_t tmp3 = tmp[3U];
758 uint64_t tmp4 = tmp[4U];
759 uint64_t tmp5 = tmp[5U];
760 uint64_t tmp6 = tmp[6U];
761 uint64_t tmp7 = tmp[7U];
762 uint64_t iv0_ = iv0 ^ tmp0;
763 uint64_t iv1_ = iv1 ^ tmp1;
764 uint64_t iv2_ = iv2 ^ tmp2;
765 uint64_t iv3_ = iv3 ^ tmp3;
766 uint64_t iv4_ = iv4 ^ tmp4;
767 uint64_t iv5_ = iv5 ^ tmp5;
768 uint64_t iv6_ = iv6 ^ tmp6;
769 uint64_t iv7_ = iv7 ^ tmp7;
770 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0_, iv1_, iv2_, iv3_);
771 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4_, iv5_, iv6_, iv7_);
772 return p;
773 }
774 default:
775 {
776 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
777 KRML_HOST_EXIT(253U);
778 }
779 }
780 }
781 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
782 __FILE__,
783 __LINE__,
784 "unreachable (pattern matches are exhaustive in F*)");
785 KRML_HOST_EXIT(255U);
786 }
787
788 /**
789 General-purpose allocation function that gives control over all
790 Blake2 parameters, including the key. Further resettings of the state SHALL be
791 done with `reset_with_params_and_key`, and SHALL feature the exact same values
792 for the `key_length` and `digest_length` fields as passed here. In other words,
793 once you commit to a digest and key length, the only way to change these
794 parameters is to allocate a new object.
795
796 The caller must satisfy the following requirements.
797 - The length of the key k MUST match the value of the field key_length in the
798 parameters.
799 - The key_length must not exceed 256 for S, 64 for B.
800 - The digest_length must not exceed 256 for S, 64 for B.
801
802 */
803 Hacl_Hash_Blake2b_Simd256_state_t
804 *Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key(
805 Hacl_Hash_Blake2b_blake2_params *p,
806 bool last_node,
807 uint8_t *k
808 )
809 {
810 Hacl_Hash_Blake2b_blake2_params pv = p[0U];
811 Hacl_Hash_Blake2b_index
812 i1 = { .key_length = pv.key_length, .digest_length = pv.digest_length, .last_node = last_node };
813 return malloc_raw(i1, ((Hacl_Hash_Blake2b_params_and_key){ .fst = p, .snd = k }));
814 }
815
816 /**
817 Specialized allocation function that picks default values for all
818 parameters, except for the key_length. Further resettings of the state SHALL be
819 done with `reset_with_key`, and SHALL feature the exact same key length `kk` as
820 passed here. In other words, once you commit to a key length, the only way to
821 change this parameter is to allocate a new object.
822
823 The caller must satisfy the following requirements.
824 - The key_length must not exceed 256 for S, 64 for B.
825
826 */
827 Hacl_Hash_Blake2b_Simd256_state_t
828 *Hacl_Hash_Blake2b_Simd256_malloc_with_key(uint8_t *k, uint8_t kk)
829 {
830 uint8_t nn = 64U;
831 Hacl_Hash_Blake2b_index i = { .key_length = kk, .digest_length = nn, .last_node = false };
832 uint8_t salt[16U] = { 0U };
833 uint8_t personal[16U] = { 0U };
834 Hacl_Hash_Blake2b_blake2_params
835 p =
836 {
837 .digest_length = i.digest_length, .key_length = i.key_length, .fanout = 1U, .depth = 1U,
838 .leaf_length = 0U, .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt,
839 .personal = personal
840 };
841 Hacl_Hash_Blake2b_blake2_params p0 = p;
842 Hacl_Hash_Blake2b_Simd256_state_t
843 *s = Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key(&p0, false, k);
844 return s;
845 }
846
847 /**
848 Specialized allocation function that picks default values for all
849 parameters, and has no key. Effectively, this is what you want if you intend to
850 use Blake2 as a hash function. Further resettings of the state SHALL be done with `reset`.
851 */
852 Hacl_Hash_Blake2b_Simd256_state_t *Hacl_Hash_Blake2b_Simd256_malloc(void)
853 {
854 return Hacl_Hash_Blake2b_Simd256_malloc_with_key(NULL, 0U);
855 }
856
857 static Hacl_Hash_Blake2b_index index_of_state(Hacl_Hash_Blake2b_Simd256_state_t *s)
858 {
859 Hacl_Hash_Blake2b_Simd256_block_state_t block_state = (*s).block_state;
860 bool last_node = block_state.thd;
861 uint8_t nn = block_state.snd;
862 uint8_t kk1 = block_state.fst;
863 return
864 ((Hacl_Hash_Blake2b_index){ .key_length = kk1, .digest_length = nn, .last_node = last_node });
865 }
866
867 static void
868 reset_raw(Hacl_Hash_Blake2b_Simd256_state_t *state, Hacl_Hash_Blake2b_params_and_key key)
869 {
870 Hacl_Hash_Blake2b_Simd256_state_t scrut = *state;
871 uint8_t *buf = scrut.buf;
872 Hacl_Hash_Blake2b_Simd256_block_state_t block_state = scrut.block_state;
873 bool last_node0 = block_state.thd;
874 uint8_t nn0 = block_state.snd;
875 uint8_t kk10 = block_state.fst;
876 Hacl_Hash_Blake2b_index
877 i = { .key_length = kk10, .digest_length = nn0, .last_node = last_node0 };
878 KRML_MAYBE_UNUSED_VAR(i);
879 Hacl_Hash_Blake2b_blake2_params *p = key.fst;
880 uint8_t kk1 = p->key_length;
881 uint8_t nn = p->digest_length;
882 bool last_node = block_state.thd;
883 Hacl_Hash_Blake2b_index
884 i1 = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
885 Lib_IntVector_Intrinsics_vec256 *h = block_state.f3.snd;
886 uint32_t kk20 = (uint32_t)i1.key_length;
887 uint8_t *k_1 = key.snd;
888 if (!(kk20 == 0U))
889 {
890 uint8_t *sub_b = buf + kk20;
891 memset(sub_b, 0U, (128U - kk20) * sizeof (uint8_t));
892 memcpy(buf, k_1, kk20 * sizeof (uint8_t));
893 }
894 Hacl_Hash_Blake2b_blake2_params pv = p[0U];
895 uint64_t tmp[8U] = { 0U };
896 Lib_IntVector_Intrinsics_vec256 *r0 = h;
897 Lib_IntVector_Intrinsics_vec256 *r1 = h + 1U;
898 Lib_IntVector_Intrinsics_vec256 *r2 = h + 2U;
899 Lib_IntVector_Intrinsics_vec256 *r3 = h + 3U;
900 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
901 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
902 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
903 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
904 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
905 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
906 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
907 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
908 r2[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0, iv1, iv2, iv3);
909 r3[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4, iv5, iv6, iv7);
910 uint8_t kk2 = pv.key_length;
911 uint8_t nn1 = pv.digest_length;
912 KRML_MAYBE_FOR2(i0,
913 0U,
914 2U,
915 1U,
916 uint64_t *os = tmp + 4U;
917 uint8_t *bj = pv.salt + i0 * 8U;
918 uint64_t u = load64_le(bj);
919 uint64_t r = u;
920 uint64_t x = r;
921 os[i0] = x;);
922 KRML_MAYBE_FOR2(i0,
923 0U,
924 2U,
925 1U,
926 uint64_t *os = tmp + 6U;
927 uint8_t *bj = pv.personal + i0 * 8U;
928 uint64_t u = load64_le(bj);
929 uint64_t r = u;
930 uint64_t x = r;
931 os[i0] = x;);
932 tmp[0U] =
933 (uint64_t)nn1 ^
934 ((uint64_t)kk2 << 8U ^
935 ((uint64_t)pv.fanout << 16U ^ ((uint64_t)pv.depth << 24U ^ (uint64_t)pv.leaf_length << 32U)));
936 tmp[1U] = pv.node_offset;
937 tmp[2U] = (uint64_t)pv.node_depth ^ (uint64_t)pv.inner_length << 8U;
938 tmp[3U] = 0ULL;
939 uint64_t tmp0 = tmp[0U];
940 uint64_t tmp1 = tmp[1U];
941 uint64_t tmp2 = tmp[2U];
942 uint64_t tmp3 = tmp[3U];
943 uint64_t tmp4 = tmp[4U];
944 uint64_t tmp5 = tmp[5U];
945 uint64_t tmp6 = tmp[6U];
946 uint64_t tmp7 = tmp[7U];
947 uint64_t iv0_ = iv0 ^ tmp0;
948 uint64_t iv1_ = iv1 ^ tmp1;
949 uint64_t iv2_ = iv2 ^ tmp2;
950 uint64_t iv3_ = iv3 ^ tmp3;
951 uint64_t iv4_ = iv4 ^ tmp4;
952 uint64_t iv5_ = iv5 ^ tmp5;
953 uint64_t iv6_ = iv6 ^ tmp6;
954 uint64_t iv7_ = iv7 ^ tmp7;
955 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0_, iv1_, iv2_, iv3_);
956 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4_, iv5_, iv6_, iv7_);
957 uint8_t kk11 = i.key_length;
958 uint32_t ite;
959 if (kk11 != 0U)
960 {
961 ite = 128U;
962 }
963 else
964 {
965 ite = 0U;
966 }
967 Hacl_Hash_Blake2b_Simd256_state_t
968 tmp8 = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)ite };
969 state[0U] = tmp8;
970 }
971
972 /**
973 General-purpose re-initialization function with parameters and
974 key. You cannot change digest_length, key_length, or last_node, meaning those values in
975 the parameters object must be the same as originally decided via one of the
976 malloc functions. All other values of the parameter can be changed. The behavior
977 is unspecified if you violate this precondition.
978 */
979 void
980 Hacl_Hash_Blake2b_Simd256_reset_with_key_and_params(
981 Hacl_Hash_Blake2b_Simd256_state_t *s,
982 Hacl_Hash_Blake2b_blake2_params *p,
983 uint8_t *k
984 )
985 {
986 Hacl_Hash_Blake2b_index i1 = index_of_state(s);
987 KRML_MAYBE_UNUSED_VAR(i1);
988 reset_raw(s, ((Hacl_Hash_Blake2b_params_and_key){ .fst = p, .snd = k }));
989 }
990
991 /**
992 Specialized-purpose re-initialization function with no parameters,
993 and a key. The key length must be the same as originally decided via your choice
994 of malloc function. All other parameters are reset to their default values. The
995 original call to malloc MUST have set digest_length to the default value. The
996 behavior is unspecified if you violate this precondition.
997 */
998 void Hacl_Hash_Blake2b_Simd256_reset_with_key(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *k)
999 {
1000 Hacl_Hash_Blake2b_index idx = index_of_state(s);
1001 uint8_t salt[16U] = { 0U };
1002 uint8_t personal[16U] = { 0U };
1003 Hacl_Hash_Blake2b_blake2_params
1004 p =
1005 {
1006 .digest_length = idx.digest_length, .key_length = idx.key_length, .fanout = 1U, .depth = 1U,
1007 .leaf_length = 0U, .node_offset = 0ULL, .node_depth = 0U, .inner_length = 0U, .salt = salt,
1008 .personal = personal
1009 };
1010 Hacl_Hash_Blake2b_blake2_params p0 = p;
1011 reset_raw(s, ((Hacl_Hash_Blake2b_params_and_key){ .fst = &p0, .snd = k }));
1012 }
1013
1014 /**
1015 Specialized-purpose re-initialization function with no parameters
1016 and no key. This is what you want if you intend to use Blake2 as a hash
1017 function. The key length and digest length must have been set to their
1018 respective default values via your choice of malloc function (always true if you
1019 used `malloc`). All other parameters are reset to their default values. The
1020 behavior is unspecified if you violate this precondition.
1021 */
1022 void Hacl_Hash_Blake2b_Simd256_reset(Hacl_Hash_Blake2b_Simd256_state_t *s)
1023 {
1024 Hacl_Hash_Blake2b_Simd256_reset_with_key(s, NULL);
1025 }
1026
1027 /**
1028 Update function; 0 = success, 1 = max length exceeded
1029 */
1030 Hacl_Streaming_Types_error_code
1031 Hacl_Hash_Blake2b_Simd256_update(
1032 Hacl_Hash_Blake2b_Simd256_state_t *state,
1033 uint8_t *chunk,
1034 uint32_t chunk_len
1035 )
1036 {
1037 Hacl_Hash_Blake2b_Simd256_state_t s = *state;
1038 uint64_t total_len = s.total_len;
1039 if ((uint64_t)chunk_len > 0xffffffffffffffffULL - total_len)
1040 {
1041 return Hacl_Streaming_Types_MaximumLengthExceeded;
1042 }
1043 uint32_t sz;
1044 if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
1045 {
1046 sz = 128U;
1047 }
1048 else
1049 {
1050 sz = (uint32_t)(total_len % (uint64_t)128U);
1051 }
1052 if (chunk_len <= 128U - sz)
1053 {
1054 Hacl_Hash_Blake2b_Simd256_state_t s1 = *state;
1055 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = s1.block_state;
1056 uint8_t *buf = s1.buf;
1057 uint64_t total_len1 = s1.total_len;
1058 uint32_t sz1;
1059 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1060 {
1061 sz1 = 128U;
1062 }
1063 else
1064 {
1065 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1066 }
1067 uint8_t *buf2 = buf + sz1;
1068 memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
1069 uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
1070 *state =
1071 (
1072 (Hacl_Hash_Blake2b_Simd256_state_t){
1073 .block_state = block_state1,
1074 .buf = buf,
1075 .total_len = total_len2
1076 }
1077 );
1078 }
1079 else if (sz == 0U)
1080 {
1081 Hacl_Hash_Blake2b_Simd256_state_t s1 = *state;
1082 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = s1.block_state;
1083 uint8_t *buf = s1.buf;
1084 uint64_t total_len1 = s1.total_len;
1085 uint32_t sz1;
1086 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1087 {
1088 sz1 = 128U;
1089 }
1090 else
1091 {
1092 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1093 }
1094 if (!(sz1 == 0U))
1095 {
1096 uint64_t prevlen = total_len1 - (uint64_t)sz1;
1097 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc = block_state1.f3;
1098 Lib_IntVector_Intrinsics_vec256 *wv = acc.fst;
1099 Lib_IntVector_Intrinsics_vec256 *hash = acc.snd;
1100 uint32_t nb = 1U;
1101 Hacl_Hash_Blake2b_Simd256_update_multi(128U,
1102 wv,
1103 hash,
1104 FStar_UInt128_uint64_to_uint128(prevlen),
1105 buf,
1106 nb);
1107 }
1108 uint32_t ite;
1109 if ((uint64_t)chunk_len % (uint64_t)128U == 0ULL && (uint64_t)chunk_len > 0ULL)
1110 {
1111 ite = 128U;
1112 }
1113 else
1114 {
1115 ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)128U);
1116 }
1117 uint32_t n_blocks = (chunk_len - ite) / 128U;
1118 uint32_t data1_len = n_blocks * 128U;
1119 uint32_t data2_len = chunk_len - data1_len;
1120 uint8_t *data1 = chunk;
1121 uint8_t *data2 = chunk + data1_len;
1122 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc = block_state1.f3;
1123 Lib_IntVector_Intrinsics_vec256 *wv = acc.fst;
1124 Lib_IntVector_Intrinsics_vec256 *hash = acc.snd;
1125 uint32_t nb = data1_len / 128U;
1126 Hacl_Hash_Blake2b_Simd256_update_multi(data1_len,
1127 wv,
1128 hash,
1129 FStar_UInt128_uint64_to_uint128(total_len1),
1130 data1,
1131 nb);
1132 uint8_t *dst = buf;
1133 memcpy(dst, data2, data2_len * sizeof (uint8_t));
1134 *state =
1135 (
1136 (Hacl_Hash_Blake2b_Simd256_state_t){
1137 .block_state = block_state1,
1138 .buf = buf,
1139 .total_len = total_len1 + (uint64_t)chunk_len
1140 }
1141 );
1142 }
1143 else
1144 {
1145 uint32_t diff = 128U - sz;
1146 uint8_t *chunk1 = chunk;
1147 uint8_t *chunk2 = chunk + diff;
1148 Hacl_Hash_Blake2b_Simd256_state_t s1 = *state;
1149 Hacl_Hash_Blake2b_Simd256_block_state_t block_state10 = s1.block_state;
1150 uint8_t *buf0 = s1.buf;
1151 uint64_t total_len10 = s1.total_len;
1152 uint32_t sz10;
1153 if (total_len10 % (uint64_t)128U == 0ULL && total_len10 > 0ULL)
1154 {
1155 sz10 = 128U;
1156 }
1157 else
1158 {
1159 sz10 = (uint32_t)(total_len10 % (uint64_t)128U);
1160 }
1161 uint8_t *buf2 = buf0 + sz10;
1162 memcpy(buf2, chunk1, diff * sizeof (uint8_t));
1163 uint64_t total_len2 = total_len10 + (uint64_t)diff;
1164 *state =
1165 (
1166 (Hacl_Hash_Blake2b_Simd256_state_t){
1167 .block_state = block_state10,
1168 .buf = buf0,
1169 .total_len = total_len2
1170 }
1171 );
1172 Hacl_Hash_Blake2b_Simd256_state_t s10 = *state;
1173 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = s10.block_state;
1174 uint8_t *buf = s10.buf;
1175 uint64_t total_len1 = s10.total_len;
1176 uint32_t sz1;
1177 if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
1178 {
1179 sz1 = 128U;
1180 }
1181 else
1182 {
1183 sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
1184 }
1185 if (!(sz1 == 0U))
1186 {
1187 uint64_t prevlen = total_len1 - (uint64_t)sz1;
1188 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc = block_state1.f3;
1189 Lib_IntVector_Intrinsics_vec256 *wv = acc.fst;
1190 Lib_IntVector_Intrinsics_vec256 *hash = acc.snd;
1191 uint32_t nb = 1U;
1192 Hacl_Hash_Blake2b_Simd256_update_multi(128U,
1193 wv,
1194 hash,
1195 FStar_UInt128_uint64_to_uint128(prevlen),
1196 buf,
1197 nb);
1198 }
1199 uint32_t ite;
1200 if
1201 ((uint64_t)(chunk_len - diff) % (uint64_t)128U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
1202 {
1203 ite = 128U;
1204 }
1205 else
1206 {
1207 ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)128U);
1208 }
1209 uint32_t n_blocks = (chunk_len - diff - ite) / 128U;
1210 uint32_t data1_len = n_blocks * 128U;
1211 uint32_t data2_len = chunk_len - diff - data1_len;
1212 uint8_t *data1 = chunk2;
1213 uint8_t *data2 = chunk2 + data1_len;
1214 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc = block_state1.f3;
1215 Lib_IntVector_Intrinsics_vec256 *wv = acc.fst;
1216 Lib_IntVector_Intrinsics_vec256 *hash = acc.snd;
1217 uint32_t nb = data1_len / 128U;
1218 Hacl_Hash_Blake2b_Simd256_update_multi(data1_len,
1219 wv,
1220 hash,
1221 FStar_UInt128_uint64_to_uint128(total_len1),
1222 data1,
1223 nb);
1224 uint8_t *dst = buf;
1225 memcpy(dst, data2, data2_len * sizeof (uint8_t));
1226 *state =
1227 (
1228 (Hacl_Hash_Blake2b_Simd256_state_t){
1229 .block_state = block_state1,
1230 .buf = buf,
1231 .total_len = total_len1 + (uint64_t)(chunk_len - diff)
1232 }
1233 );
1234 }
1235 return Hacl_Streaming_Types_Success;
1236 }
1237
1238 /**
1239 Digest function. This function expects the `output` array to hold
1240 at least `digest_length` bytes, where `digest_length` was determined by your
1241 choice of `malloc` function. Concretely, if you used `malloc` or
1242 `malloc_with_key`, then the expected length is 256 for S, or 64 for B (default
1243 digest length). If you used `malloc_with_params_and_key`, then the expected
1244 length is whatever you chose for the `digest_length` field of your parameters.
1245 For convenience, this function returns `digest_length`. When in doubt, callers
1246 can pass an array of size HACL_BLAKE2B_256_OUT_BYTES, then use the return value
1247 to see how many bytes were actually written.
1248 */
1249 uint8_t Hacl_Hash_Blake2b_Simd256_digest(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *dst)
1250 {
1251 Hacl_Hash_Blake2b_Simd256_block_state_t block_state0 = (*s).block_state;
1252 bool last_node0 = block_state0.thd;
1253 uint8_t nn0 = block_state0.snd;
1254 uint8_t kk0 = block_state0.fst;
1255 Hacl_Hash_Blake2b_index
1256 i1 = { .key_length = kk0, .digest_length = nn0, .last_node = last_node0 };
1257 Hacl_Hash_Blake2b_Simd256_state_t scrut = *s;
1258 Hacl_Hash_Blake2b_Simd256_block_state_t block_state = scrut.block_state;
1259 uint8_t *buf_ = scrut.buf;
1260 uint64_t total_len = scrut.total_len;
1261 uint32_t r;
1262 if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
1263 {
1264 r = 128U;
1265 }
1266 else
1267 {
1268 r = (uint32_t)(total_len % (uint64_t)128U);
1269 }
1270 uint8_t *buf_1 = buf_;
1271 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 wv0[4U] KRML_POST_ALIGN(32) = { 0U };
1272 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 b[4U] KRML_POST_ALIGN(32) = { 0U };
1273 Hacl_Hash_Blake2b_Simd256_block_state_t
1274 tmp_block_state =
1275 {
1276 .fst = i1.key_length,
1277 .snd = i1.digest_length,
1278 .thd = i1.last_node,
1279 .f3 = { .fst = wv0, .snd = b }
1280 };
1281 Lib_IntVector_Intrinsics_vec256 *src_b = block_state.f3.snd;
1282 Lib_IntVector_Intrinsics_vec256 *dst_b = tmp_block_state.f3.snd;
1283 memcpy(dst_b, src_b, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
1284 uint64_t prev_len = total_len - (uint64_t)r;
1285 uint32_t ite;
1286 if (r % 128U == 0U && r > 0U)
1287 {
1288 ite = 128U;
1289 }
1290 else
1291 {
1292 ite = r % 128U;
1293 }
1294 uint8_t *buf_last = buf_1 + r - ite;
1295 uint8_t *buf_multi = buf_1;
1296 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc0 = tmp_block_state.f3;
1297 Lib_IntVector_Intrinsics_vec256 *wv1 = acc0.fst;
1298 Lib_IntVector_Intrinsics_vec256 *hash0 = acc0.snd;
1299 uint32_t nb = 0U;
1300 Hacl_Hash_Blake2b_Simd256_update_multi(0U,
1301 wv1,
1302 hash0,
1303 FStar_UInt128_uint64_to_uint128(prev_len),
1304 buf_multi,
1305 nb);
1306 uint64_t prev_len_last = total_len - (uint64_t)r;
1307 Hacl_Hash_Blake2b_Simd256_two_2b_256 acc = tmp_block_state.f3;
1308 bool last_node1 = tmp_block_state.thd;
1309 Lib_IntVector_Intrinsics_vec256 *wv = acc.fst;
1310 Lib_IntVector_Intrinsics_vec256 *hash = acc.snd;
1311 Hacl_Hash_Blake2b_Simd256_update_last(r,
1312 wv,
1313 hash,
1314 last_node1,
1315 FStar_UInt128_uint64_to_uint128(prev_len_last),
1316 r,
1317 buf_last);
1318 uint8_t nn1 = tmp_block_state.snd;
1319 Hacl_Hash_Blake2b_Simd256_finish((uint32_t)nn1, dst, tmp_block_state.f3.snd);
1320 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = (*s).block_state;
1321 bool last_node = block_state1.thd;
1322 uint8_t nn = block_state1.snd;
1323 uint8_t kk = block_state1.fst;
1324 return
1325 ((Hacl_Hash_Blake2b_index){ .key_length = kk, .digest_length = nn, .last_node = last_node }).digest_length;
1326 }
1327
1328 Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(Hacl_Hash_Blake2b_Simd256_state_t *s)
1329 {
1330 Hacl_Hash_Blake2b_Simd256_block_state_t block_state = (*s).block_state;
1331 bool last_node = block_state.thd;
1332 uint8_t nn = block_state.snd;
1333 uint8_t kk = block_state.fst;
1334 return
1335 ((Hacl_Hash_Blake2b_index){ .key_length = kk, .digest_length = nn, .last_node = last_node });
1336 }
1337
1338 /**
1339 Free state function when there is no key
1340 */
1341 void Hacl_Hash_Blake2b_Simd256_free(Hacl_Hash_Blake2b_Simd256_state_t *state)
1342 {
1343 Hacl_Hash_Blake2b_Simd256_state_t scrut = *state;
1344 uint8_t *buf = scrut.buf;
1345 Hacl_Hash_Blake2b_Simd256_block_state_t block_state = scrut.block_state;
1346 Lib_IntVector_Intrinsics_vec256 *b = block_state.f3.snd;
1347 Lib_IntVector_Intrinsics_vec256 *wv = block_state.f3.fst;
1348 KRML_ALIGNED_FREE(wv);
1349 KRML_ALIGNED_FREE(b);
1350 KRML_HOST_FREE(buf);
1351 KRML_HOST_FREE(state);
1352 }
1353
1354 /**
1355 Copying. This preserves all parameters.
1356 */
1357 Hacl_Hash_Blake2b_Simd256_state_t
1358 *Hacl_Hash_Blake2b_Simd256_copy(Hacl_Hash_Blake2b_Simd256_state_t *state)
1359 {
1360 Hacl_Hash_Blake2b_Simd256_state_t scrut = *state;
1361 Hacl_Hash_Blake2b_Simd256_block_state_t block_state0 = scrut.block_state;
1362 uint8_t *buf0 = scrut.buf;
1363 uint64_t total_len0 = scrut.total_len;
1364 bool last_node = block_state0.thd;
1365 uint8_t nn = block_state0.snd;
1366 uint8_t kk1 = block_state0.fst;
1367 Hacl_Hash_Blake2b_index i = { .key_length = kk1, .digest_length = nn, .last_node = last_node };
1368 uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
1369 if (buf == NULL)
1370 {
1371 return NULL;
1372 }
1373 memcpy(buf, buf0, 128U * sizeof (uint8_t));
1374 Lib_IntVector_Intrinsics_vec256
1375 *wv0 =
1376 (Lib_IntVector_Intrinsics_vec256 *)KRML_ALIGNED_MALLOC(32,
1377 sizeof (Lib_IntVector_Intrinsics_vec256) * 4U);
1378 if (wv0 != NULL)
1379 {
1380 memset(wv0, 0U, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
1381 }
1382 option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___
1383 block_state;
1384 if (wv0 == NULL)
1385 {
1386 block_state =
1387 (
1388 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
1389 .tag = Hacl_Streaming_Types_None
1390 }
1391 );
1392 }
1393 else
1394 {
1395 Lib_IntVector_Intrinsics_vec256
1396 *b =
1397 (Lib_IntVector_Intrinsics_vec256 *)KRML_ALIGNED_MALLOC(32,
1398 sizeof (Lib_IntVector_Intrinsics_vec256) * 4U);
1399 if (b != NULL)
1400 {
1401 memset(b, 0U, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
1402 }
1403 if (b == NULL)
1404 {
1405 KRML_ALIGNED_FREE(wv0);
1406 block_state =
1407 (
1408 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
1409 .tag = Hacl_Streaming_Types_None
1410 }
1411 );
1412 }
1413 else
1414 {
1415 block_state =
1416 (
1417 (option___uint8_t___uint8_t___bool_____Lib_IntVector_Intrinsics_vec256_____Lib_IntVector_Intrinsics_vec256___){
1418 .tag = Hacl_Streaming_Types_Some,
1419 .v = {
1420 .fst = i.key_length,
1421 .snd = i.digest_length,
1422 .thd = i.last_node,
1423 .f3 = { .fst = wv0, .snd = b }
1424 }
1425 }
1426 );
1427 }
1428 }
1429 if (block_state.tag == Hacl_Streaming_Types_None)
1430 {
1431 KRML_HOST_FREE(buf);
1432 return NULL;
1433 }
1434 if (block_state.tag == Hacl_Streaming_Types_Some)
1435 {
1436 Hacl_Hash_Blake2b_Simd256_block_state_t block_state1 = block_state.v;
1437 Lib_IntVector_Intrinsics_vec256 *src_b = block_state0.f3.snd;
1438 Lib_IntVector_Intrinsics_vec256 *dst_b = block_state1.f3.snd;
1439 memcpy(dst_b, src_b, 4U * sizeof (Lib_IntVector_Intrinsics_vec256));
1440 Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some;
1441 switch (k_)
1442 {
1443 case Hacl_Streaming_Types_None:
1444 {
1445 return NULL;
1446 }
1447 case Hacl_Streaming_Types_Some:
1448 {
1449 Hacl_Hash_Blake2b_Simd256_state_t
1450 s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 };
1451 Hacl_Hash_Blake2b_Simd256_state_t
1452 *p =
1453 (Hacl_Hash_Blake2b_Simd256_state_t *)KRML_HOST_MALLOC(sizeof (
1454 Hacl_Hash_Blake2b_Simd256_state_t
1455 ));
1456 if (p != NULL)
1457 {
1458 p[0U] = s;
1459 }
1460 if (p == NULL)
1461 {
1462 Lib_IntVector_Intrinsics_vec256 *b = block_state1.f3.snd;
1463 Lib_IntVector_Intrinsics_vec256 *wv = block_state1.f3.fst;
1464 KRML_ALIGNED_FREE(wv);
1465 KRML_ALIGNED_FREE(b);
1466 KRML_HOST_FREE(buf);
1467 return NULL;
1468 }
1469 return p;
1470 }
1471 default:
1472 {
1473 KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__);
1474 KRML_HOST_EXIT(253U);
1475 }
1476 }
1477 }
1478 KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n",
1479 __FILE__,
1480 __LINE__,
1481 "unreachable (pattern matches are exhaustive in F*)");
1482 KRML_HOST_EXIT(255U);
1483 }
1484
1485 /**
1486 Write the BLAKE2b digest of message `input` using key `key` into `output`.
1487
1488 @param output Pointer to `output_len` bytes of memory where the digest is written to.
1489 @param output_len Length of the to-be-generated digest with 1 <= `output_len` <= 64.
1490 @param input Pointer to `input_len` bytes of memory where the input message is read from.
1491 @param input_len Length of the input message.
1492 @param key Pointer to `key_len` bytes of memory where the key is read from.
1493 @param key_len Length of the key. Can be 0.
1494 */
1495 void
1496 Hacl_Hash_Blake2b_Simd256_hash_with_key(
1497 uint8_t *output,
1498 uint32_t output_len,
1499 uint8_t *input,
1500 uint32_t input_len,
1501 uint8_t *key,
1502 uint32_t key_len
1503 )
1504 {
1505 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 b[4U] KRML_POST_ALIGN(32) = { 0U };
1506 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 b1[4U] KRML_POST_ALIGN(32) = { 0U };
1507 Hacl_Hash_Blake2b_Simd256_init(b, key_len, output_len);
1508 update(b1, b, key_len, key, input_len, input);
1509 Hacl_Hash_Blake2b_Simd256_finish(output_len, output, b);
1510 Lib_Memzero0_memzero(b1, 4U, Lib_IntVector_Intrinsics_vec256, void *);
1511 Lib_Memzero0_memzero(b, 4U, Lib_IntVector_Intrinsics_vec256, void *);
1512 }
1513
1514 /**
1515 Write the BLAKE2b digest of message `input` using key `key` and
1516 parameters `params` into `output`. The `key` array must be of length
1517 `params.key_length`. The `output` array must be of length
1518 `params.digest_length`.
1519 */
1520 void
1521 Hacl_Hash_Blake2b_Simd256_hash_with_key_and_params(
1522 uint8_t *output,
1523 uint8_t *input,
1524 uint32_t input_len,
1525 Hacl_Hash_Blake2b_blake2_params params,
1526 uint8_t *key
1527 )
1528 {
1529 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 b[4U] KRML_POST_ALIGN(32) = { 0U };
1530 KRML_PRE_ALIGN(32) Lib_IntVector_Intrinsics_vec256 b1[4U] KRML_POST_ALIGN(32) = { 0U };
1531 uint64_t tmp[8U] = { 0U };
1532 Lib_IntVector_Intrinsics_vec256 *r0 = b;
1533 Lib_IntVector_Intrinsics_vec256 *r1 = b + 1U;
1534 Lib_IntVector_Intrinsics_vec256 *r2 = b + 2U;
1535 Lib_IntVector_Intrinsics_vec256 *r3 = b + 3U;
1536 uint64_t iv0 = Hacl_Hash_Blake2b_ivTable_B[0U];
1537 uint64_t iv1 = Hacl_Hash_Blake2b_ivTable_B[1U];
1538 uint64_t iv2 = Hacl_Hash_Blake2b_ivTable_B[2U];
1539 uint64_t iv3 = Hacl_Hash_Blake2b_ivTable_B[3U];
1540 uint64_t iv4 = Hacl_Hash_Blake2b_ivTable_B[4U];
1541 uint64_t iv5 = Hacl_Hash_Blake2b_ivTable_B[5U];
1542 uint64_t iv6 = Hacl_Hash_Blake2b_ivTable_B[6U];
1543 uint64_t iv7 = Hacl_Hash_Blake2b_ivTable_B[7U];
1544 r2[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0, iv1, iv2, iv3);
1545 r3[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4, iv5, iv6, iv7);
1546 uint8_t kk = params.key_length;
1547 uint8_t nn = params.digest_length;
1548 KRML_MAYBE_FOR2(i,
1549 0U,
1550 2U,
1551 1U,
1552 uint64_t *os = tmp + 4U;
1553 uint8_t *bj = params.salt + i * 8U;
1554 uint64_t u = load64_le(bj);
1555 uint64_t r = u;
1556 uint64_t x = r;
1557 os[i] = x;);
1558 KRML_MAYBE_FOR2(i,
1559 0U,
1560 2U,
1561 1U,
1562 uint64_t *os = tmp + 6U;
1563 uint8_t *bj = params.personal + i * 8U;
1564 uint64_t u = load64_le(bj);
1565 uint64_t r = u;
1566 uint64_t x = r;
1567 os[i] = x;);
1568 tmp[0U] =
1569 (uint64_t)nn ^
1570 ((uint64_t)kk << 8U ^
1571 ((uint64_t)params.fanout << 16U ^
1572 ((uint64_t)params.depth << 24U ^ (uint64_t)params.leaf_length << 32U)));
1573 tmp[1U] = params.node_offset;
1574 tmp[2U] = (uint64_t)params.node_depth ^ (uint64_t)params.inner_length << 8U;
1575 tmp[3U] = 0ULL;
1576 uint64_t tmp0 = tmp[0U];
1577 uint64_t tmp1 = tmp[1U];
1578 uint64_t tmp2 = tmp[2U];
1579 uint64_t tmp3 = tmp[3U];
1580 uint64_t tmp4 = tmp[4U];
1581 uint64_t tmp5 = tmp[5U];
1582 uint64_t tmp6 = tmp[6U];
1583 uint64_t tmp7 = tmp[7U];
1584 uint64_t iv0_ = iv0 ^ tmp0;
1585 uint64_t iv1_ = iv1 ^ tmp1;
1586 uint64_t iv2_ = iv2 ^ tmp2;
1587 uint64_t iv3_ = iv3 ^ tmp3;
1588 uint64_t iv4_ = iv4 ^ tmp4;
1589 uint64_t iv5_ = iv5 ^ tmp5;
1590 uint64_t iv6_ = iv6 ^ tmp6;
1591 uint64_t iv7_ = iv7 ^ tmp7;
1592 r0[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv0_, iv1_, iv2_, iv3_);
1593 r1[0U] = Lib_IntVector_Intrinsics_vec256_load64s(iv4_, iv5_, iv6_, iv7_);
1594 update(b1, b, (uint32_t)params.key_length, key, input_len, input);
1595 Hacl_Hash_Blake2b_Simd256_finish((uint32_t)params.digest_length, output, b);
1596 Lib_Memzero0_memzero(b1, 4U, Lib_IntVector_Intrinsics_vec256, void *);
1597 Lib_Memzero0_memzero(b, 4U, Lib_IntVector_Intrinsics_vec256, void *);
1598 }
1599