Debian Codegraph @

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