| 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_SHA2.h" |
| 27 |
|
| 28 |
#include "Hacl_Streaming_Types.h" |
| 29 |
|
| 30 |
#include "internal/Hacl_Streaming_Types.h" |
| 31 |
|
| 32 |
|
| 33 |
void Hacl_Hash_SHA2_sha256_init(uint32_t *hash) |
| 34 |
{ |
| 35 |
KRML_MAYBE_FOR8(i, |
| 36 |
0U, |
| 37 |
8U, |
| 38 |
1U, |
| 39 |
uint32_t *os = hash; |
| 40 |
uint32_t x = Hacl_Hash_SHA2_h256[i]; |
| 41 |
os[i] = x;); |
| 42 |
} |
| 43 |
|
| 44 |
static inline void sha256_update(uint8_t *b, uint32_t *hash) |
| 45 |
{ |
| 46 |
uint32_t hash_old[8U] = { 0U }; |
| 47 |
uint32_t ws[16U] = { 0U }; |
| 48 |
memcpy(hash_old, hash, 8U * sizeof (uint32_t)); |
| 49 |
uint8_t *b10 = b; |
| 50 |
uint32_t u = load32_be(b10); |
| 51 |
ws[0U] = u; |
| 52 |
uint32_t u0 = load32_be(b10 + 4U); |
| 53 |
ws[1U] = u0; |
| 54 |
uint32_t u1 = load32_be(b10 + 8U); |
| 55 |
ws[2U] = u1; |
| 56 |
uint32_t u2 = load32_be(b10 + 12U); |
| 57 |
ws[3U] = u2; |
| 58 |
uint32_t u3 = load32_be(b10 + 16U); |
| 59 |
ws[4U] = u3; |
| 60 |
uint32_t u4 = load32_be(b10 + 20U); |
| 61 |
ws[5U] = u4; |
| 62 |
uint32_t u5 = load32_be(b10 + 24U); |
| 63 |
ws[6U] = u5; |
| 64 |
uint32_t u6 = load32_be(b10 + 28U); |
| 65 |
ws[7U] = u6; |
| 66 |
uint32_t u7 = load32_be(b10 + 32U); |
| 67 |
ws[8U] = u7; |
| 68 |
uint32_t u8 = load32_be(b10 + 36U); |
| 69 |
ws[9U] = u8; |
| 70 |
uint32_t u9 = load32_be(b10 + 40U); |
| 71 |
ws[10U] = u9; |
| 72 |
uint32_t u10 = load32_be(b10 + 44U); |
| 73 |
ws[11U] = u10; |
| 74 |
uint32_t u11 = load32_be(b10 + 48U); |
| 75 |
ws[12U] = u11; |
| 76 |
uint32_t u12 = load32_be(b10 + 52U); |
| 77 |
ws[13U] = u12; |
| 78 |
uint32_t u13 = load32_be(b10 + 56U); |
| 79 |
ws[14U] = u13; |
| 80 |
uint32_t u14 = load32_be(b10 + 60U); |
| 81 |
ws[15U] = u14; |
| 82 |
KRML_MAYBE_FOR4(i0, |
| 83 |
0U, |
| 84 |
4U, |
| 85 |
1U, |
| 86 |
KRML_MAYBE_FOR16(i, |
| 87 |
0U, |
| 88 |
16U, |
| 89 |
1U, |
| 90 |
uint32_t k_t = Hacl_Hash_SHA2_k224_256[16U * i0 + i]; |
| 91 |
uint32_t ws_t = ws[i]; |
| 92 |
uint32_t a0 = hash[0U]; |
| 93 |
uint32_t b0 = hash[1U]; |
| 94 |
uint32_t c0 = hash[2U]; |
| 95 |
uint32_t d0 = hash[3U]; |
| 96 |
uint32_t e0 = hash[4U]; |
| 97 |
uint32_t f0 = hash[5U]; |
| 98 |
uint32_t g0 = hash[6U]; |
| 99 |
uint32_t h02 = hash[7U]; |
| 100 |
uint32_t k_e_t = k_t; |
| 101 |
uint32_t |
| 102 |
t1 = |
| 103 |
h02 + ((e0 << 26U | e0 >> 6U) ^ ((e0 << 21U | e0 >> 11U) ^ (e0 << 7U | e0 >> 25U))) + |
| 104 |
((e0 & f0) ^ (~e0 & g0)) |
| 105 |
+ k_e_t |
| 106 |
+ ws_t; |
| 107 |
uint32_t |
| 108 |
t2 = |
| 109 |
((a0 << 30U | a0 >> 2U) ^ ((a0 << 19U | a0 >> 13U) ^ (a0 << 10U | a0 >> 22U))) + |
| 110 |
((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0))); |
| 111 |
uint32_t a1 = t1 + t2; |
| 112 |
uint32_t b1 = a0; |
| 113 |
uint32_t c1 = b0; |
| 114 |
uint32_t d1 = c0; |
| 115 |
uint32_t e1 = d0 + t1; |
| 116 |
uint32_t f1 = e0; |
| 117 |
uint32_t g1 = f0; |
| 118 |
uint32_t h12 = g0; |
| 119 |
hash[0U] = a1; |
| 120 |
hash[1U] = b1; |
| 121 |
hash[2U] = c1; |
| 122 |
hash[3U] = d1; |
| 123 |
hash[4U] = e1; |
| 124 |
hash[5U] = f1; |
| 125 |
hash[6U] = g1; |
| 126 |
hash[7U] = h12;); |
| 127 |
if (i0 < 3U) |
| 128 |
{ |
| 129 |
KRML_MAYBE_FOR16(i, |
| 130 |
0U, |
| 131 |
16U, |
| 132 |
1U, |
| 133 |
uint32_t t16 = ws[i]; |
| 134 |
uint32_t t15 = ws[(i + 1U) % 16U]; |
| 135 |
uint32_t t7 = ws[(i + 9U) % 16U]; |
| 136 |
uint32_t t2 = ws[(i + 14U) % 16U]; |
| 137 |
uint32_t s1 = (t2 << 15U | t2 >> 17U) ^ ((t2 << 13U | t2 >> 19U) ^ t2 >> 10U); |
| 138 |
uint32_t s0 = (t15 << 25U | t15 >> 7U) ^ ((t15 << 14U | t15 >> 18U) ^ t15 >> 3U); |
| 139 |
ws[i] = s1 + t7 + s0 + t16;); |
| 140 |
}); |
| 141 |
KRML_MAYBE_FOR8(i, |
| 142 |
0U, |
| 143 |
8U, |
| 144 |
1U, |
| 145 |
uint32_t *os = hash; |
| 146 |
uint32_t x = hash[i] + hash_old[i]; |
| 147 |
os[i] = x;); |
| 148 |
} |
| 149 |
|
| 150 |
void Hacl_Hash_SHA2_sha256_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st) |
| 151 |
{ |
| 152 |
uint32_t blocks = len / 64U; |
| 153 |
for (uint32_t i = 0U; i < blocks; i++) |
| 154 |
{ |
| 155 |
uint8_t *b0 = b; |
| 156 |
uint8_t *mb = b0 + i * 64U; |
| 157 |
sha256_update(mb, st); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
Hacl_Hash_SHA2_sha256_update_last(uint64_t totlen, uint32_t len, uint8_t *b, uint32_t *hash) |
| 163 |
{ |
| 164 |
uint32_t blocks; |
| 165 |
if (len + 8U + 1U <= 64U) |
| 166 |
{ |
| 167 |
blocks = 1U; |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
blocks = 2U; |
| 172 |
} |
| 173 |
uint32_t fin = blocks * 64U; |
| 174 |
uint8_t last[128U] = { 0U }; |
| 175 |
uint8_t totlen_buf[8U] = { 0U }; |
| 176 |
uint64_t total_len_bits = totlen << 3U; |
| 177 |
store64_be(totlen_buf, total_len_bits); |
| 178 |
uint8_t *b0 = b; |
| 179 |
memcpy(last, b0, len * sizeof (uint8_t)); |
| 180 |
last[len] = 0x80U; |
| 181 |
memcpy(last + fin - 8U, totlen_buf, 8U * sizeof (uint8_t)); |
| 182 |
uint8_t *last00 = last; |
| 183 |
uint8_t *last10 = last + 64U; |
| 184 |
uint8_t *l0 = last00; |
| 185 |
uint8_t *l1 = last10; |
| 186 |
uint8_t *lb0 = l0; |
| 187 |
uint8_t *lb1 = l1; |
| 188 |
uint8_t *last0 = lb0; |
| 189 |
uint8_t *last1 = lb1; |
| 190 |
sha256_update(last0, hash); |
| 191 |
if (blocks > 1U) |
| 192 |
{ |
| 193 |
sha256_update(last1, hash); |
| 194 |
return; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void Hacl_Hash_SHA2_sha256_finish(uint32_t *st, uint8_t *h) |
| 199 |
{ |
| 200 |
uint8_t hbuf[32U] = { 0U }; |
| 201 |
KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store32_be(hbuf + i * 4U, st[i]);); |
| 202 |
memcpy(h, hbuf, 32U * sizeof (uint8_t)); |
| 203 |
} |
| 204 |
|
| 205 |
void Hacl_Hash_SHA2_sha224_init(uint32_t *hash) |
| 206 |
{ |
| 207 |
KRML_MAYBE_FOR8(i, |
| 208 |
0U, |
| 209 |
8U, |
| 210 |
1U, |
| 211 |
uint32_t *os = hash; |
| 212 |
uint32_t x = Hacl_Hash_SHA2_h224[i]; |
| 213 |
os[i] = x;); |
| 214 |
} |
| 215 |
|
| 216 |
void Hacl_Hash_SHA2_sha224_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st) |
| 217 |
{ |
| 218 |
Hacl_Hash_SHA2_sha256_update_nblocks(len, b, st); |
| 219 |
} |
| 220 |
|
| 221 |
void Hacl_Hash_SHA2_sha224_update_last(uint64_t totlen, uint32_t len, uint8_t *b, uint32_t *st) |
| 222 |
{ |
| 223 |
Hacl_Hash_SHA2_sha256_update_last(totlen, len, b, st); |
| 224 |
} |
| 225 |
|
| 226 |
void Hacl_Hash_SHA2_sha224_finish(uint32_t *st, uint8_t *h) |
| 227 |
{ |
| 228 |
uint8_t hbuf[32U] = { 0U }; |
| 229 |
KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store32_be(hbuf + i * 4U, st[i]);); |
| 230 |
memcpy(h, hbuf, 28U * sizeof (uint8_t)); |
| 231 |
} |
| 232 |
|
| 233 |
void Hacl_Hash_SHA2_sha512_init(uint64_t *hash) |
| 234 |
{ |
| 235 |
KRML_MAYBE_FOR8(i, |
| 236 |
0U, |
| 237 |
8U, |
| 238 |
1U, |
| 239 |
uint64_t *os = hash; |
| 240 |
uint64_t x = Hacl_Hash_SHA2_h512[i]; |
| 241 |
os[i] = x;); |
| 242 |
} |
| 243 |
|
| 244 |
static inline void sha512_update(uint8_t *b, uint64_t *hash) |
| 245 |
{ |
| 246 |
uint64_t hash_old[8U] = { 0U }; |
| 247 |
uint64_t ws[16U] = { 0U }; |
| 248 |
memcpy(hash_old, hash, 8U * sizeof (uint64_t)); |
| 249 |
uint8_t *b10 = b; |
| 250 |
uint64_t u = load64_be(b10); |
| 251 |
ws[0U] = u; |
| 252 |
uint64_t u0 = load64_be(b10 + 8U); |
| 253 |
ws[1U] = u0; |
| 254 |
uint64_t u1 = load64_be(b10 + 16U); |
| 255 |
ws[2U] = u1; |
| 256 |
uint64_t u2 = load64_be(b10 + 24U); |
| 257 |
ws[3U] = u2; |
| 258 |
uint64_t u3 = load64_be(b10 + 32U); |
| 259 |
ws[4U] = u3; |
| 260 |
uint64_t u4 = load64_be(b10 + 40U); |
| 261 |
ws[5U] = u4; |
| 262 |
uint64_t u5 = load64_be(b10 + 48U); |
| 263 |
ws[6U] = u5; |
| 264 |
uint64_t u6 = load64_be(b10 + 56U); |
| 265 |
ws[7U] = u6; |
| 266 |
uint64_t u7 = load64_be(b10 + 64U); |
| 267 |
ws[8U] = u7; |
| 268 |
uint64_t u8 = load64_be(b10 + 72U); |
| 269 |
ws[9U] = u8; |
| 270 |
uint64_t u9 = load64_be(b10 + 80U); |
| 271 |
ws[10U] = u9; |
| 272 |
uint64_t u10 = load64_be(b10 + 88U); |
| 273 |
ws[11U] = u10; |
| 274 |
uint64_t u11 = load64_be(b10 + 96U); |
| 275 |
ws[12U] = u11; |
| 276 |
uint64_t u12 = load64_be(b10 + 104U); |
| 277 |
ws[13U] = u12; |
| 278 |
uint64_t u13 = load64_be(b10 + 112U); |
| 279 |
ws[14U] = u13; |
| 280 |
uint64_t u14 = load64_be(b10 + 120U); |
| 281 |
ws[15U] = u14; |
| 282 |
KRML_MAYBE_FOR5(i0, |
| 283 |
0U, |
| 284 |
5U, |
| 285 |
1U, |
| 286 |
KRML_MAYBE_FOR16(i, |
| 287 |
0U, |
| 288 |
16U, |
| 289 |
1U, |
| 290 |
uint64_t k_t = Hacl_Hash_SHA2_k384_512[16U * i0 + i]; |
| 291 |
uint64_t ws_t = ws[i]; |
| 292 |
uint64_t a0 = hash[0U]; |
| 293 |
uint64_t b0 = hash[1U]; |
| 294 |
uint64_t c0 = hash[2U]; |
| 295 |
uint64_t d0 = hash[3U]; |
| 296 |
uint64_t e0 = hash[4U]; |
| 297 |
uint64_t f0 = hash[5U]; |
| 298 |
uint64_t g0 = hash[6U]; |
| 299 |
uint64_t h02 = hash[7U]; |
| 300 |
uint64_t k_e_t = k_t; |
| 301 |
uint64_t |
| 302 |
t1 = |
| 303 |
h02 + ((e0 << 50U | e0 >> 14U) ^ ((e0 << 46U | e0 >> 18U) ^ (e0 << 23U | e0 >> 41U))) + |
| 304 |
((e0 & f0) ^ (~e0 & g0)) |
| 305 |
+ k_e_t |
| 306 |
+ ws_t; |
| 307 |
uint64_t |
| 308 |
t2 = |
| 309 |
((a0 << 36U | a0 >> 28U) ^ ((a0 << 30U | a0 >> 34U) ^ (a0 << 25U | a0 >> 39U))) + |
| 310 |
((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0))); |
| 311 |
uint64_t a1 = t1 + t2; |
| 312 |
uint64_t b1 = a0; |
| 313 |
uint64_t c1 = b0; |
| 314 |
uint64_t d1 = c0; |
| 315 |
uint64_t e1 = d0 + t1; |
| 316 |
uint64_t f1 = e0; |
| 317 |
uint64_t g1 = f0; |
| 318 |
uint64_t h12 = g0; |
| 319 |
hash[0U] = a1; |
| 320 |
hash[1U] = b1; |
| 321 |
hash[2U] = c1; |
| 322 |
hash[3U] = d1; |
| 323 |
hash[4U] = e1; |
| 324 |
hash[5U] = f1; |
| 325 |
hash[6U] = g1; |
| 326 |
hash[7U] = h12;); |
| 327 |
if (i0 < 4U) |
| 328 |
{ |
| 329 |
KRML_MAYBE_FOR16(i, |
| 330 |
0U, |
| 331 |
16U, |
| 332 |
1U, |
| 333 |
uint64_t t16 = ws[i]; |
| 334 |
uint64_t t15 = ws[(i + 1U) % 16U]; |
| 335 |
uint64_t t7 = ws[(i + 9U) % 16U]; |
| 336 |
uint64_t t2 = ws[(i + 14U) % 16U]; |
| 337 |
uint64_t s1 = (t2 << 45U | t2 >> 19U) ^ ((t2 << 3U | t2 >> 61U) ^ t2 >> 6U); |
| 338 |
uint64_t s0 = (t15 << 63U | t15 >> 1U) ^ ((t15 << 56U | t15 >> 8U) ^ t15 >> 7U); |
| 339 |
ws[i] = s1 + t7 + s0 + t16;); |
| 340 |
}); |
| 341 |
KRML_MAYBE_FOR8(i, |
| 342 |
0U, |
| 343 |
8U, |
| 344 |
1U, |
| 345 |
uint64_t *os = hash; |
| 346 |
uint64_t x = hash[i] + hash_old[i]; |
| 347 |
os[i] = x;); |
| 348 |
} |
| 349 |
|
| 350 |
void Hacl_Hash_SHA2_sha512_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st) |
| 351 |
{ |
| 352 |
uint32_t blocks = len / 128U; |
| 353 |
for (uint32_t i = 0U; i < blocks; i++) |
| 354 |
{ |
| 355 |
uint8_t *b0 = b; |
| 356 |
uint8_t *mb = b0 + i * 128U; |
| 357 |
sha512_update(mb, st); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void |
| 362 |
Hacl_Hash_SHA2_sha512_update_last( |
| 363 |
FStar_UInt128_uint128 totlen, |
| 364 |
uint32_t len, |
| 365 |
uint8_t *b, |
| 366 |
uint64_t *hash |
| 367 |
) |
| 368 |
{ |
| 369 |
uint32_t blocks; |
| 370 |
if (len + 16U + 1U <= 128U) |
| 371 |
{ |
| 372 |
blocks = 1U; |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
blocks = 2U; |
| 377 |
} |
| 378 |
uint32_t fin = blocks * 128U; |
| 379 |
uint8_t last[256U] = { 0U }; |
| 380 |
uint8_t totlen_buf[16U] = { 0U }; |
| 381 |
FStar_UInt128_uint128 total_len_bits = FStar_UInt128_shift_left(totlen, 3U); |
| 382 |
store128_be(totlen_buf, total_len_bits); |
| 383 |
uint8_t *b0 = b; |
| 384 |
memcpy(last, b0, len * sizeof (uint8_t)); |
| 385 |
last[len] = 0x80U; |
| 386 |
memcpy(last + fin - 16U, totlen_buf, 16U * sizeof (uint8_t)); |
| 387 |
uint8_t *last00 = last; |
| 388 |
uint8_t *last10 = last + 128U; |
| 389 |
uint8_t *l0 = last00; |
| 390 |
uint8_t *l1 = last10; |
| 391 |
uint8_t *lb0 = l0; |
| 392 |
uint8_t *lb1 = l1; |
| 393 |
uint8_t *last0 = lb0; |
| 394 |
uint8_t *last1 = lb1; |
| 395 |
sha512_update(last0, hash); |
| 396 |
if (blocks > 1U) |
| 397 |
{ |
| 398 |
sha512_update(last1, hash); |
| 399 |
return; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
void Hacl_Hash_SHA2_sha512_finish(uint64_t *st, uint8_t *h) |
| 404 |
{ |
| 405 |
uint8_t hbuf[64U] = { 0U }; |
| 406 |
KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store64_be(hbuf + i * 8U, st[i]);); |
| 407 |
memcpy(h, hbuf, 64U * sizeof (uint8_t)); |
| 408 |
} |
| 409 |
|
| 410 |
void Hacl_Hash_SHA2_sha384_init(uint64_t *hash) |
| 411 |
{ |
| 412 |
KRML_MAYBE_FOR8(i, |
| 413 |
0U, |
| 414 |
8U, |
| 415 |
1U, |
| 416 |
uint64_t *os = hash; |
| 417 |
uint64_t x = Hacl_Hash_SHA2_h384[i]; |
| 418 |
os[i] = x;); |
| 419 |
} |
| 420 |
|
| 421 |
void Hacl_Hash_SHA2_sha384_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st) |
| 422 |
{ |
| 423 |
Hacl_Hash_SHA2_sha512_update_nblocks(len, b, st); |
| 424 |
} |
| 425 |
|
| 426 |
void |
| 427 |
Hacl_Hash_SHA2_sha384_update_last( |
| 428 |
FStar_UInt128_uint128 totlen, |
| 429 |
uint32_t len, |
| 430 |
uint8_t *b, |
| 431 |
uint64_t *st |
| 432 |
) |
| 433 |
{ |
| 434 |
Hacl_Hash_SHA2_sha512_update_last(totlen, len, b, st); |
| 435 |
} |
| 436 |
|
| 437 |
void Hacl_Hash_SHA2_sha384_finish(uint64_t *st, uint8_t *h) |
| 438 |
{ |
| 439 |
uint8_t hbuf[64U] = { 0U }; |
| 440 |
KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store64_be(hbuf + i * 8U, st[i]);); |
| 441 |
memcpy(h, hbuf, 48U * sizeof (uint8_t)); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
Allocate initial state for the SHA2_256 hash. The state is to be freed by |
| 446 |
calling `free_256`. |
| 447 |
*/ |
| 448 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_256(void) |
| 449 |
{ |
| 450 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t)); |
| 451 |
if (buf == NULL) |
| 452 |
{ |
| 453 |
return NULL; |
| 454 |
} |
| 455 |
uint8_t *buf1 = buf; |
| 456 |
uint32_t *b = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t)); |
| 457 |
Hacl_Streaming_Types_optional_32 block_state; |
| 458 |
if (b == NULL) |
| 459 |
{ |
| 460 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_None }); |
| 461 |
} |
| 462 |
else |
| 463 |
{ |
| 464 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 465 |
} |
| 466 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 467 |
{ |
| 468 |
KRML_HOST_FREE(buf1); |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 472 |
{ |
| 473 |
uint32_t *block_state1 = block_state.v; |
| 474 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 475 |
switch (k_) |
| 476 |
{ |
| 477 |
case Hacl_Streaming_Types_None: |
| 478 |
{ |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
case Hacl_Streaming_Types_Some: |
| 482 |
{ |
| 483 |
Hacl_Streaming_MD_state_32 |
| 484 |
s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)0U }; |
| 485 |
Hacl_Streaming_MD_state_32 |
| 486 |
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32)); |
| 487 |
if (p != NULL) |
| 488 |
{ |
| 489 |
p[0U] = s; |
| 490 |
} |
| 491 |
if (p == NULL) |
| 492 |
{ |
| 493 |
KRML_HOST_FREE(block_state1); |
| 494 |
KRML_HOST_FREE(buf1); |
| 495 |
return NULL; |
| 496 |
} |
| 497 |
Hacl_Hash_SHA2_sha256_init(block_state1); |
| 498 |
return p; |
| 499 |
} |
| 500 |
default: |
| 501 |
{ |
| 502 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 503 |
KRML_HOST_EXIT(253U); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 508 |
__FILE__, |
| 509 |
__LINE__, |
| 510 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 511 |
KRML_HOST_EXIT(255U); |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
Copies the state passed as argument into a newly allocated state (deep copy). |
| 516 |
The state is to be freed by calling `free_256`. Cloning the state this way is |
| 517 |
useful, for instance, if your control-flow diverges and you need to feed |
| 518 |
more (different) data into the hash in each branch. |
| 519 |
*/ |
| 520 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_copy_256(Hacl_Streaming_MD_state_32 *state) |
| 521 |
{ |
| 522 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 523 |
uint32_t *block_state0 = scrut.block_state; |
| 524 |
uint8_t *buf0 = scrut.buf; |
| 525 |
uint64_t total_len0 = scrut.total_len; |
| 526 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t)); |
| 527 |
if (buf == NULL) |
| 528 |
{ |
| 529 |
return NULL; |
| 530 |
} |
| 531 |
memcpy(buf, buf0, 64U * sizeof (uint8_t)); |
| 532 |
uint32_t *b = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t)); |
| 533 |
Hacl_Streaming_Types_optional_32 block_state; |
| 534 |
if (b == NULL) |
| 535 |
{ |
| 536 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_None }); |
| 537 |
} |
| 538 |
else |
| 539 |
{ |
| 540 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 541 |
} |
| 542 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 543 |
{ |
| 544 |
KRML_HOST_FREE(buf); |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 548 |
{ |
| 549 |
uint32_t *block_state1 = block_state.v; |
| 550 |
memcpy(block_state1, block_state0, 8U * sizeof (uint32_t)); |
| 551 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 552 |
switch (k_) |
| 553 |
{ |
| 554 |
case Hacl_Streaming_Types_None: |
| 555 |
{ |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
case Hacl_Streaming_Types_Some: |
| 559 |
{ |
| 560 |
Hacl_Streaming_MD_state_32 |
| 561 |
s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 }; |
| 562 |
Hacl_Streaming_MD_state_32 |
| 563 |
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32)); |
| 564 |
if (p != NULL) |
| 565 |
{ |
| 566 |
p[0U] = s; |
| 567 |
} |
| 568 |
if (p == NULL) |
| 569 |
{ |
| 570 |
KRML_HOST_FREE(block_state1); |
| 571 |
KRML_HOST_FREE(buf); |
| 572 |
return NULL; |
| 573 |
} |
| 574 |
return p; |
| 575 |
} |
| 576 |
default: |
| 577 |
{ |
| 578 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 579 |
KRML_HOST_EXIT(253U); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 584 |
__FILE__, |
| 585 |
__LINE__, |
| 586 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 587 |
KRML_HOST_EXIT(255U); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
Reset an existing state to the initial hash state with empty data. |
| 592 |
*/ |
| 593 |
void Hacl_Hash_SHA2_reset_256(Hacl_Streaming_MD_state_32 *state) |
| 594 |
{ |
| 595 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 596 |
uint8_t *buf = scrut.buf; |
| 597 |
uint32_t *block_state = scrut.block_state; |
| 598 |
Hacl_Hash_SHA2_sha256_init(block_state); |
| 599 |
Hacl_Streaming_MD_state_32 |
| 600 |
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U }; |
| 601 |
state[0U] = tmp; |
| 602 |
} |
| 603 |
|
| 604 |
static inline Hacl_Streaming_Types_error_code |
| 605 |
update_224_256(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len) |
| 606 |
{ |
| 607 |
Hacl_Streaming_MD_state_32 s = *state; |
| 608 |
uint64_t total_len = s.total_len; |
| 609 |
if ((uint64_t)chunk_len > 2305843009213693951ULL - total_len) |
| 610 |
{ |
| 611 |
return Hacl_Streaming_Types_MaximumLengthExceeded; |
| 612 |
} |
| 613 |
uint32_t sz; |
| 614 |
if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL) |
| 615 |
{ |
| 616 |
sz = 64U; |
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
sz = (uint32_t)(total_len % (uint64_t)64U); |
| 621 |
} |
| 622 |
if (chunk_len <= 64U - sz) |
| 623 |
{ |
| 624 |
Hacl_Streaming_MD_state_32 s1 = *state; |
| 625 |
uint32_t *block_state1 = s1.block_state; |
| 626 |
uint8_t *buf = s1.buf; |
| 627 |
uint64_t total_len1 = s1.total_len; |
| 628 |
uint32_t sz1; |
| 629 |
if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL) |
| 630 |
{ |
| 631 |
sz1 = 64U; |
| 632 |
} |
| 633 |
else |
| 634 |
{ |
| 635 |
sz1 = (uint32_t)(total_len1 % (uint64_t)64U); |
| 636 |
} |
| 637 |
uint8_t *buf2 = buf + sz1; |
| 638 |
memcpy(buf2, chunk, chunk_len * sizeof (uint8_t)); |
| 639 |
uint64_t total_len2 = total_len1 + (uint64_t)chunk_len; |
| 640 |
*state = |
| 641 |
( |
| 642 |
(Hacl_Streaming_MD_state_32){ |
| 643 |
.block_state = block_state1, |
| 644 |
.buf = buf, |
| 645 |
.total_len = total_len2 |
| 646 |
} |
| 647 |
); |
| 648 |
} |
| 649 |
else if (sz == 0U) |
| 650 |
{ |
| 651 |
Hacl_Streaming_MD_state_32 s1 = *state; |
| 652 |
uint32_t *block_state1 = s1.block_state; |
| 653 |
uint8_t *buf = s1.buf; |
| 654 |
uint64_t total_len1 = s1.total_len; |
| 655 |
uint32_t sz1; |
| 656 |
if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL) |
| 657 |
{ |
| 658 |
sz1 = 64U; |
| 659 |
} |
| 660 |
else |
| 661 |
{ |
| 662 |
sz1 = (uint32_t)(total_len1 % (uint64_t)64U); |
| 663 |
} |
| 664 |
if (!(sz1 == 0U)) |
| 665 |
{ |
| 666 |
Hacl_Hash_SHA2_sha256_update_nblocks(64U, buf, block_state1); |
| 667 |
} |
| 668 |
uint32_t ite; |
| 669 |
if ((uint64_t)chunk_len % (uint64_t)64U == 0ULL && (uint64_t)chunk_len > 0ULL) |
| 670 |
{ |
| 671 |
ite = 64U; |
| 672 |
} |
| 673 |
else |
| 674 |
{ |
| 675 |
ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)64U); |
| 676 |
} |
| 677 |
uint32_t n_blocks = (chunk_len - ite) / 64U; |
| 678 |
uint32_t data1_len = n_blocks * 64U; |
| 679 |
uint32_t data2_len = chunk_len - data1_len; |
| 680 |
uint8_t *data1 = chunk; |
| 681 |
uint8_t *data2 = chunk + data1_len; |
| 682 |
Hacl_Hash_SHA2_sha256_update_nblocks(data1_len / 64U * 64U, data1, block_state1); |
| 683 |
uint8_t *dst = buf; |
| 684 |
memcpy(dst, data2, data2_len * sizeof (uint8_t)); |
| 685 |
*state = |
| 686 |
( |
| 687 |
(Hacl_Streaming_MD_state_32){ |
| 688 |
.block_state = block_state1, |
| 689 |
.buf = buf, |
| 690 |
.total_len = total_len1 + (uint64_t)chunk_len |
| 691 |
} |
| 692 |
); |
| 693 |
} |
| 694 |
else |
| 695 |
{ |
| 696 |
uint32_t diff = 64U - sz; |
| 697 |
uint8_t *chunk1 = chunk; |
| 698 |
uint8_t *chunk2 = chunk + diff; |
| 699 |
Hacl_Streaming_MD_state_32 s1 = *state; |
| 700 |
uint32_t *block_state10 = s1.block_state; |
| 701 |
uint8_t *buf0 = s1.buf; |
| 702 |
uint64_t total_len10 = s1.total_len; |
| 703 |
uint32_t sz10; |
| 704 |
if (total_len10 % (uint64_t)64U == 0ULL && total_len10 > 0ULL) |
| 705 |
{ |
| 706 |
sz10 = 64U; |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
sz10 = (uint32_t)(total_len10 % (uint64_t)64U); |
| 711 |
} |
| 712 |
uint8_t *buf2 = buf0 + sz10; |
| 713 |
memcpy(buf2, chunk1, diff * sizeof (uint8_t)); |
| 714 |
uint64_t total_len2 = total_len10 + (uint64_t)diff; |
| 715 |
*state = |
| 716 |
( |
| 717 |
(Hacl_Streaming_MD_state_32){ |
| 718 |
.block_state = block_state10, |
| 719 |
.buf = buf0, |
| 720 |
.total_len = total_len2 |
| 721 |
} |
| 722 |
); |
| 723 |
Hacl_Streaming_MD_state_32 s10 = *state; |
| 724 |
uint32_t *block_state1 = s10.block_state; |
| 725 |
uint8_t *buf = s10.buf; |
| 726 |
uint64_t total_len1 = s10.total_len; |
| 727 |
uint32_t sz1; |
| 728 |
if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL) |
| 729 |
{ |
| 730 |
sz1 = 64U; |
| 731 |
} |
| 732 |
else |
| 733 |
{ |
| 734 |
sz1 = (uint32_t)(total_len1 % (uint64_t)64U); |
| 735 |
} |
| 736 |
if (!(sz1 == 0U)) |
| 737 |
{ |
| 738 |
Hacl_Hash_SHA2_sha256_update_nblocks(64U, buf, block_state1); |
| 739 |
} |
| 740 |
uint32_t ite; |
| 741 |
if |
| 742 |
((uint64_t)(chunk_len - diff) % (uint64_t)64U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL) |
| 743 |
{ |
| 744 |
ite = 64U; |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)64U); |
| 749 |
} |
| 750 |
uint32_t n_blocks = (chunk_len - diff - ite) / 64U; |
| 751 |
uint32_t data1_len = n_blocks * 64U; |
| 752 |
uint32_t data2_len = chunk_len - diff - data1_len; |
| 753 |
uint8_t *data1 = chunk2; |
| 754 |
uint8_t *data2 = chunk2 + data1_len; |
| 755 |
Hacl_Hash_SHA2_sha256_update_nblocks(data1_len / 64U * 64U, data1, block_state1); |
| 756 |
uint8_t *dst = buf; |
| 757 |
memcpy(dst, data2, data2_len * sizeof (uint8_t)); |
| 758 |
*state = |
| 759 |
( |
| 760 |
(Hacl_Streaming_MD_state_32){ |
| 761 |
.block_state = block_state1, |
| 762 |
.buf = buf, |
| 763 |
.total_len = total_len1 + (uint64_t)(chunk_len - diff) |
| 764 |
} |
| 765 |
); |
| 766 |
} |
| 767 |
return Hacl_Streaming_Types_Success; |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
Feed an arbitrary amount of data into the hash. This function returns 0 for |
| 772 |
success, or 1 if the combined length of all of the data passed to `update_256` |
| 773 |
(since the last call to `reset_256`) exceeds 2^61-1 bytes. |
| 774 |
|
| 775 |
This function is identical to the update function for SHA2_224. |
| 776 |
*/ |
| 777 |
Hacl_Streaming_Types_error_code |
| 778 |
Hacl_Hash_SHA2_update_256( |
| 779 |
Hacl_Streaming_MD_state_32 *state, |
| 780 |
uint8_t *input, |
| 781 |
uint32_t input_len |
| 782 |
) |
| 783 |
{ |
| 784 |
return update_224_256(state, input, input_len); |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
Write the resulting hash into `output`, an array of 32 bytes. The state remains |
| 789 |
valid after a call to `digest_256`, meaning the user may feed more data into |
| 790 |
the hash via `update_256`. (The digest_256 function operates on an internal copy of |
| 791 |
the state and therefore does not invalidate the client-held state `p`.) |
| 792 |
*/ |
| 793 |
void Hacl_Hash_SHA2_digest_256(Hacl_Streaming_MD_state_32 *state, uint8_t *output) |
| 794 |
{ |
| 795 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 796 |
uint32_t *block_state = scrut.block_state; |
| 797 |
uint8_t *buf_ = scrut.buf; |
| 798 |
uint64_t total_len = scrut.total_len; |
| 799 |
uint32_t r; |
| 800 |
if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL) |
| 801 |
{ |
| 802 |
r = 64U; |
| 803 |
} |
| 804 |
else |
| 805 |
{ |
| 806 |
r = (uint32_t)(total_len % (uint64_t)64U); |
| 807 |
} |
| 808 |
uint8_t *buf_1 = buf_; |
| 809 |
uint32_t tmp_block_state[8U] = { 0U }; |
| 810 |
memcpy(tmp_block_state, block_state, 8U * sizeof (uint32_t)); |
| 811 |
uint32_t ite; |
| 812 |
if (r % 64U == 0U && r > 0U) |
| 813 |
{ |
| 814 |
ite = 64U; |
| 815 |
} |
| 816 |
else |
| 817 |
{ |
| 818 |
ite = r % 64U; |
| 819 |
} |
| 820 |
uint8_t *buf_last = buf_1 + r - ite; |
| 821 |
uint8_t *buf_multi = buf_1; |
| 822 |
Hacl_Hash_SHA2_sha256_update_nblocks(0U, buf_multi, tmp_block_state); |
| 823 |
uint64_t prev_len_last = total_len - (uint64_t)r; |
| 824 |
Hacl_Hash_SHA2_sha256_update_last(prev_len_last + (uint64_t)r, r, buf_last, tmp_block_state); |
| 825 |
Hacl_Hash_SHA2_sha256_finish(tmp_block_state, output); |
| 826 |
} |
| 827 |
|
| 828 |
/** |
| 829 |
Free a state allocated with `malloc_256`. |
| 830 |
|
| 831 |
This function is identical to the free function for SHA2_224. |
| 832 |
*/ |
| 833 |
void Hacl_Hash_SHA2_free_256(Hacl_Streaming_MD_state_32 *state) |
| 834 |
{ |
| 835 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 836 |
uint8_t *buf = scrut.buf; |
| 837 |
uint32_t *block_state = scrut.block_state; |
| 838 |
KRML_HOST_FREE(block_state); |
| 839 |
KRML_HOST_FREE(buf); |
| 840 |
KRML_HOST_FREE(state); |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
Hash `input`, of len `input_len`, into `output`, an array of 32 bytes. |
| 845 |
*/ |
| 846 |
void Hacl_Hash_SHA2_hash_256(uint8_t *output, uint8_t *input, uint32_t input_len) |
| 847 |
{ |
| 848 |
uint8_t *ib = input; |
| 849 |
uint8_t *rb = output; |
| 850 |
uint32_t st[8U] = { 0U }; |
| 851 |
Hacl_Hash_SHA2_sha256_init(st); |
| 852 |
uint32_t rem = input_len % 64U; |
| 853 |
uint64_t len_ = (uint64_t)input_len; |
| 854 |
Hacl_Hash_SHA2_sha256_update_nblocks(input_len, ib, st); |
| 855 |
uint32_t rem1 = input_len % 64U; |
| 856 |
uint8_t *b0 = ib; |
| 857 |
uint8_t *lb = b0 + input_len - rem1; |
| 858 |
Hacl_Hash_SHA2_sha256_update_last(len_, rem, lb, st); |
| 859 |
Hacl_Hash_SHA2_sha256_finish(st, rb); |
| 860 |
} |
| 861 |
|
| 862 |
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_224(void) |
| 863 |
{ |
| 864 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t)); |
| 865 |
if (buf == NULL) |
| 866 |
{ |
| 867 |
return NULL; |
| 868 |
} |
| 869 |
uint8_t *buf1 = buf; |
| 870 |
uint32_t *b = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t)); |
| 871 |
Hacl_Streaming_Types_optional_32 block_state; |
| 872 |
if (b == NULL) |
| 873 |
{ |
| 874 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_None }); |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
block_state = ((Hacl_Streaming_Types_optional_32){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 879 |
} |
| 880 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 881 |
{ |
| 882 |
KRML_HOST_FREE(buf1); |
| 883 |
return NULL; |
| 884 |
} |
| 885 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 886 |
{ |
| 887 |
uint32_t *block_state1 = block_state.v; |
| 888 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 889 |
switch (k_) |
| 890 |
{ |
| 891 |
case Hacl_Streaming_Types_None: |
| 892 |
{ |
| 893 |
return NULL; |
| 894 |
} |
| 895 |
case Hacl_Streaming_Types_Some: |
| 896 |
{ |
| 897 |
Hacl_Streaming_MD_state_32 |
| 898 |
s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)0U }; |
| 899 |
Hacl_Streaming_MD_state_32 |
| 900 |
*p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32)); |
| 901 |
if (p != NULL) |
| 902 |
{ |
| 903 |
p[0U] = s; |
| 904 |
} |
| 905 |
if (p == NULL) |
| 906 |
{ |
| 907 |
KRML_HOST_FREE(block_state1); |
| 908 |
KRML_HOST_FREE(buf1); |
| 909 |
return NULL; |
| 910 |
} |
| 911 |
Hacl_Hash_SHA2_sha224_init(block_state1); |
| 912 |
return p; |
| 913 |
} |
| 914 |
default: |
| 915 |
{ |
| 916 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 917 |
KRML_HOST_EXIT(253U); |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 922 |
__FILE__, |
| 923 |
__LINE__, |
| 924 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 925 |
KRML_HOST_EXIT(255U); |
| 926 |
} |
| 927 |
|
| 928 |
void Hacl_Hash_SHA2_reset_224(Hacl_Streaming_MD_state_32 *state) |
| 929 |
{ |
| 930 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 931 |
uint8_t *buf = scrut.buf; |
| 932 |
uint32_t *block_state = scrut.block_state; |
| 933 |
Hacl_Hash_SHA2_sha224_init(block_state); |
| 934 |
Hacl_Streaming_MD_state_32 |
| 935 |
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U }; |
| 936 |
state[0U] = tmp; |
| 937 |
} |
| 938 |
|
| 939 |
Hacl_Streaming_Types_error_code |
| 940 |
Hacl_Hash_SHA2_update_224( |
| 941 |
Hacl_Streaming_MD_state_32 *state, |
| 942 |
uint8_t *input, |
| 943 |
uint32_t input_len |
| 944 |
) |
| 945 |
{ |
| 946 |
return update_224_256(state, input, input_len); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
Write the resulting hash into `output`, an array of 28 bytes. The state remains |
| 951 |
valid after a call to `digest_224`, meaning the user may feed more data into |
| 952 |
the hash via `update_224`. |
| 953 |
*/ |
| 954 |
void Hacl_Hash_SHA2_digest_224(Hacl_Streaming_MD_state_32 *state, uint8_t *output) |
| 955 |
{ |
| 956 |
Hacl_Streaming_MD_state_32 scrut = *state; |
| 957 |
uint32_t *block_state = scrut.block_state; |
| 958 |
uint8_t *buf_ = scrut.buf; |
| 959 |
uint64_t total_len = scrut.total_len; |
| 960 |
uint32_t r; |
| 961 |
if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL) |
| 962 |
{ |
| 963 |
r = 64U; |
| 964 |
} |
| 965 |
else |
| 966 |
{ |
| 967 |
r = (uint32_t)(total_len % (uint64_t)64U); |
| 968 |
} |
| 969 |
uint8_t *buf_1 = buf_; |
| 970 |
uint32_t tmp_block_state[8U] = { 0U }; |
| 971 |
memcpy(tmp_block_state, block_state, 8U * sizeof (uint32_t)); |
| 972 |
uint32_t ite; |
| 973 |
if (r % 64U == 0U && r > 0U) |
| 974 |
{ |
| 975 |
ite = 64U; |
| 976 |
} |
| 977 |
else |
| 978 |
{ |
| 979 |
ite = r % 64U; |
| 980 |
} |
| 981 |
uint8_t *buf_last = buf_1 + r - ite; |
| 982 |
uint8_t *buf_multi = buf_1; |
| 983 |
Hacl_Hash_SHA2_sha224_update_nblocks(0U, buf_multi, tmp_block_state); |
| 984 |
uint64_t prev_len_last = total_len - (uint64_t)r; |
| 985 |
Hacl_Hash_SHA2_sha224_update_last(prev_len_last + (uint64_t)r, r, buf_last, tmp_block_state); |
| 986 |
Hacl_Hash_SHA2_sha224_finish(tmp_block_state, output); |
| 987 |
} |
| 988 |
|
| 989 |
void Hacl_Hash_SHA2_free_224(Hacl_Streaming_MD_state_32 *state) |
| 990 |
{ |
| 991 |
Hacl_Hash_SHA2_free_256(state); |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
Hash `input`, of len `input_len`, into `output`, an array of 28 bytes. |
| 996 |
*/ |
| 997 |
void Hacl_Hash_SHA2_hash_224(uint8_t *output, uint8_t *input, uint32_t input_len) |
| 998 |
{ |
| 999 |
uint8_t *ib = input; |
| 1000 |
uint8_t *rb = output; |
| 1001 |
uint32_t st[8U] = { 0U }; |
| 1002 |
Hacl_Hash_SHA2_sha224_init(st); |
| 1003 |
uint32_t rem = input_len % 64U; |
| 1004 |
uint64_t len_ = (uint64_t)input_len; |
| 1005 |
Hacl_Hash_SHA2_sha224_update_nblocks(input_len, ib, st); |
| 1006 |
uint32_t rem1 = input_len % 64U; |
| 1007 |
uint8_t *b0 = ib; |
| 1008 |
uint8_t *lb = b0 + input_len - rem1; |
| 1009 |
Hacl_Hash_SHA2_sha224_update_last(len_, rem, lb, st); |
| 1010 |
Hacl_Hash_SHA2_sha224_finish(st, rb); |
| 1011 |
} |
| 1012 |
|
| 1013 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_512(void) |
| 1014 |
{ |
| 1015 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t)); |
| 1016 |
if (buf == NULL) |
| 1017 |
{ |
| 1018 |
return NULL; |
| 1019 |
} |
| 1020 |
uint8_t *buf1 = buf; |
| 1021 |
uint64_t *b = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t)); |
| 1022 |
Hacl_Streaming_Types_optional_64 block_state; |
| 1023 |
if (b == NULL) |
| 1024 |
{ |
| 1025 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_None }); |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 1030 |
} |
| 1031 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 1032 |
{ |
| 1033 |
KRML_HOST_FREE(buf1); |
| 1034 |
return NULL; |
| 1035 |
} |
| 1036 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 1037 |
{ |
| 1038 |
uint64_t *block_state1 = block_state.v; |
| 1039 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 1040 |
switch (k_) |
| 1041 |
{ |
| 1042 |
case Hacl_Streaming_Types_None: |
| 1043 |
{ |
| 1044 |
return NULL; |
| 1045 |
} |
| 1046 |
case Hacl_Streaming_Types_Some: |
| 1047 |
{ |
| 1048 |
Hacl_Streaming_MD_state_64 |
| 1049 |
s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)0U }; |
| 1050 |
Hacl_Streaming_MD_state_64 |
| 1051 |
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64)); |
| 1052 |
if (p != NULL) |
| 1053 |
{ |
| 1054 |
p[0U] = s; |
| 1055 |
} |
| 1056 |
if (p == NULL) |
| 1057 |
{ |
| 1058 |
KRML_HOST_FREE(block_state1); |
| 1059 |
KRML_HOST_FREE(buf1); |
| 1060 |
return NULL; |
| 1061 |
} |
| 1062 |
Hacl_Hash_SHA2_sha512_init(block_state1); |
| 1063 |
return p; |
| 1064 |
} |
| 1065 |
default: |
| 1066 |
{ |
| 1067 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 1068 |
KRML_HOST_EXIT(253U); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 1073 |
__FILE__, |
| 1074 |
__LINE__, |
| 1075 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 1076 |
KRML_HOST_EXIT(255U); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/** |
| 1080 |
Copies the state passed as argument into a newly allocated state (deep copy). |
| 1081 |
The state is to be freed by calling `free_512`. Cloning the state this way is |
| 1082 |
useful, for instance, if your control-flow diverges and you need to feed |
| 1083 |
more (different) data into the hash in each branch. |
| 1084 |
*/ |
| 1085 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_copy_512(Hacl_Streaming_MD_state_64 *state) |
| 1086 |
{ |
| 1087 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1088 |
uint64_t *block_state0 = scrut.block_state; |
| 1089 |
uint8_t *buf0 = scrut.buf; |
| 1090 |
uint64_t total_len0 = scrut.total_len; |
| 1091 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t)); |
| 1092 |
if (buf == NULL) |
| 1093 |
{ |
| 1094 |
return NULL; |
| 1095 |
} |
| 1096 |
memcpy(buf, buf0, 128U * sizeof (uint8_t)); |
| 1097 |
uint64_t *b = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t)); |
| 1098 |
Hacl_Streaming_Types_optional_64 block_state; |
| 1099 |
if (b == NULL) |
| 1100 |
{ |
| 1101 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_None }); |
| 1102 |
} |
| 1103 |
else |
| 1104 |
{ |
| 1105 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 1106 |
} |
| 1107 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 1108 |
{ |
| 1109 |
KRML_HOST_FREE(buf); |
| 1110 |
return NULL; |
| 1111 |
} |
| 1112 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 1113 |
{ |
| 1114 |
uint64_t *block_state1 = block_state.v; |
| 1115 |
memcpy(block_state1, block_state0, 8U * sizeof (uint64_t)); |
| 1116 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 1117 |
switch (k_) |
| 1118 |
{ |
| 1119 |
case Hacl_Streaming_Types_None: |
| 1120 |
{ |
| 1121 |
return NULL; |
| 1122 |
} |
| 1123 |
case Hacl_Streaming_Types_Some: |
| 1124 |
{ |
| 1125 |
Hacl_Streaming_MD_state_64 |
| 1126 |
s = { .block_state = block_state1, .buf = buf, .total_len = total_len0 }; |
| 1127 |
Hacl_Streaming_MD_state_64 |
| 1128 |
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64)); |
| 1129 |
if (p != NULL) |
| 1130 |
{ |
| 1131 |
p[0U] = s; |
| 1132 |
} |
| 1133 |
if (p == NULL) |
| 1134 |
{ |
| 1135 |
KRML_HOST_FREE(block_state1); |
| 1136 |
KRML_HOST_FREE(buf); |
| 1137 |
return NULL; |
| 1138 |
} |
| 1139 |
return p; |
| 1140 |
} |
| 1141 |
default: |
| 1142 |
{ |
| 1143 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 1144 |
KRML_HOST_EXIT(253U); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
} |
| 1148 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 1149 |
__FILE__, |
| 1150 |
__LINE__, |
| 1151 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 1152 |
KRML_HOST_EXIT(255U); |
| 1153 |
} |
| 1154 |
|
| 1155 |
void Hacl_Hash_SHA2_reset_512(Hacl_Streaming_MD_state_64 *state) |
| 1156 |
{ |
| 1157 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1158 |
uint8_t *buf = scrut.buf; |
| 1159 |
uint64_t *block_state = scrut.block_state; |
| 1160 |
Hacl_Hash_SHA2_sha512_init(block_state); |
| 1161 |
Hacl_Streaming_MD_state_64 |
| 1162 |
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U }; |
| 1163 |
state[0U] = tmp; |
| 1164 |
} |
| 1165 |
|
| 1166 |
static inline Hacl_Streaming_Types_error_code |
| 1167 |
update_384_512(Hacl_Streaming_MD_state_64 *state, uint8_t *chunk, uint32_t chunk_len) |
| 1168 |
{ |
| 1169 |
Hacl_Streaming_MD_state_64 s = *state; |
| 1170 |
uint64_t total_len = s.total_len; |
| 1171 |
if ((uint64_t)chunk_len > 18446744073709551615ULL - total_len) |
| 1172 |
{ |
| 1173 |
return Hacl_Streaming_Types_MaximumLengthExceeded; |
| 1174 |
} |
| 1175 |
uint32_t sz; |
| 1176 |
if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL) |
| 1177 |
{ |
| 1178 |
sz = 128U; |
| 1179 |
} |
| 1180 |
else |
| 1181 |
{ |
| 1182 |
sz = (uint32_t)(total_len % (uint64_t)128U); |
| 1183 |
} |
| 1184 |
if (chunk_len <= 128U - sz) |
| 1185 |
{ |
| 1186 |
Hacl_Streaming_MD_state_64 s1 = *state; |
| 1187 |
uint64_t *block_state1 = s1.block_state; |
| 1188 |
uint8_t *buf = s1.buf; |
| 1189 |
uint64_t total_len1 = s1.total_len; |
| 1190 |
uint32_t sz1; |
| 1191 |
if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL) |
| 1192 |
{ |
| 1193 |
sz1 = 128U; |
| 1194 |
} |
| 1195 |
else |
| 1196 |
{ |
| 1197 |
sz1 = (uint32_t)(total_len1 % (uint64_t)128U); |
| 1198 |
} |
| 1199 |
uint8_t *buf2 = buf + sz1; |
| 1200 |
memcpy(buf2, chunk, chunk_len * sizeof (uint8_t)); |
| 1201 |
uint64_t total_len2 = total_len1 + (uint64_t)chunk_len; |
| 1202 |
*state = |
| 1203 |
( |
| 1204 |
(Hacl_Streaming_MD_state_64){ |
| 1205 |
.block_state = block_state1, |
| 1206 |
.buf = buf, |
| 1207 |
.total_len = total_len2 |
| 1208 |
} |
| 1209 |
); |
| 1210 |
} |
| 1211 |
else if (sz == 0U) |
| 1212 |
{ |
| 1213 |
Hacl_Streaming_MD_state_64 s1 = *state; |
| 1214 |
uint64_t *block_state1 = s1.block_state; |
| 1215 |
uint8_t *buf = s1.buf; |
| 1216 |
uint64_t total_len1 = s1.total_len; |
| 1217 |
uint32_t sz1; |
| 1218 |
if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL) |
| 1219 |
{ |
| 1220 |
sz1 = 128U; |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
sz1 = (uint32_t)(total_len1 % (uint64_t)128U); |
| 1225 |
} |
| 1226 |
if (!(sz1 == 0U)) |
| 1227 |
{ |
| 1228 |
Hacl_Hash_SHA2_sha512_update_nblocks(128U, buf, block_state1); |
| 1229 |
} |
| 1230 |
uint32_t ite; |
| 1231 |
if ((uint64_t)chunk_len % (uint64_t)128U == 0ULL && (uint64_t)chunk_len > 0ULL) |
| 1232 |
{ |
| 1233 |
ite = 128U; |
| 1234 |
} |
| 1235 |
else |
| 1236 |
{ |
| 1237 |
ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)128U); |
| 1238 |
} |
| 1239 |
uint32_t n_blocks = (chunk_len - ite) / 128U; |
| 1240 |
uint32_t data1_len = n_blocks * 128U; |
| 1241 |
uint32_t data2_len = chunk_len - data1_len; |
| 1242 |
uint8_t *data1 = chunk; |
| 1243 |
uint8_t *data2 = chunk + data1_len; |
| 1244 |
Hacl_Hash_SHA2_sha512_update_nblocks(data1_len / 128U * 128U, data1, block_state1); |
| 1245 |
uint8_t *dst = buf; |
| 1246 |
memcpy(dst, data2, data2_len * sizeof (uint8_t)); |
| 1247 |
*state = |
| 1248 |
( |
| 1249 |
(Hacl_Streaming_MD_state_64){ |
| 1250 |
.block_state = block_state1, |
| 1251 |
.buf = buf, |
| 1252 |
.total_len = total_len1 + (uint64_t)chunk_len |
| 1253 |
} |
| 1254 |
); |
| 1255 |
} |
| 1256 |
else |
| 1257 |
{ |
| 1258 |
uint32_t diff = 128U - sz; |
| 1259 |
uint8_t *chunk1 = chunk; |
| 1260 |
uint8_t *chunk2 = chunk + diff; |
| 1261 |
Hacl_Streaming_MD_state_64 s1 = *state; |
| 1262 |
uint64_t *block_state10 = s1.block_state; |
| 1263 |
uint8_t *buf0 = s1.buf; |
| 1264 |
uint64_t total_len10 = s1.total_len; |
| 1265 |
uint32_t sz10; |
| 1266 |
if (total_len10 % (uint64_t)128U == 0ULL && total_len10 > 0ULL) |
| 1267 |
{ |
| 1268 |
sz10 = 128U; |
| 1269 |
} |
| 1270 |
else |
| 1271 |
{ |
| 1272 |
sz10 = (uint32_t)(total_len10 % (uint64_t)128U); |
| 1273 |
} |
| 1274 |
uint8_t *buf2 = buf0 + sz10; |
| 1275 |
memcpy(buf2, chunk1, diff * sizeof (uint8_t)); |
| 1276 |
uint64_t total_len2 = total_len10 + (uint64_t)diff; |
| 1277 |
*state = |
| 1278 |
( |
| 1279 |
(Hacl_Streaming_MD_state_64){ |
| 1280 |
.block_state = block_state10, |
| 1281 |
.buf = buf0, |
| 1282 |
.total_len = total_len2 |
| 1283 |
} |
| 1284 |
); |
| 1285 |
Hacl_Streaming_MD_state_64 s10 = *state; |
| 1286 |
uint64_t *block_state1 = s10.block_state; |
| 1287 |
uint8_t *buf = s10.buf; |
| 1288 |
uint64_t total_len1 = s10.total_len; |
| 1289 |
uint32_t sz1; |
| 1290 |
if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL) |
| 1291 |
{ |
| 1292 |
sz1 = 128U; |
| 1293 |
} |
| 1294 |
else |
| 1295 |
{ |
| 1296 |
sz1 = (uint32_t)(total_len1 % (uint64_t)128U); |
| 1297 |
} |
| 1298 |
if (!(sz1 == 0U)) |
| 1299 |
{ |
| 1300 |
Hacl_Hash_SHA2_sha512_update_nblocks(128U, buf, block_state1); |
| 1301 |
} |
| 1302 |
uint32_t ite; |
| 1303 |
if |
| 1304 |
((uint64_t)(chunk_len - diff) % (uint64_t)128U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL) |
| 1305 |
{ |
| 1306 |
ite = 128U; |
| 1307 |
} |
| 1308 |
else |
| 1309 |
{ |
| 1310 |
ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)128U); |
| 1311 |
} |
| 1312 |
uint32_t n_blocks = (chunk_len - diff - ite) / 128U; |
| 1313 |
uint32_t data1_len = n_blocks * 128U; |
| 1314 |
uint32_t data2_len = chunk_len - diff - data1_len; |
| 1315 |
uint8_t *data1 = chunk2; |
| 1316 |
uint8_t *data2 = chunk2 + data1_len; |
| 1317 |
Hacl_Hash_SHA2_sha512_update_nblocks(data1_len / 128U * 128U, data1, block_state1); |
| 1318 |
uint8_t *dst = buf; |
| 1319 |
memcpy(dst, data2, data2_len * sizeof (uint8_t)); |
| 1320 |
*state = |
| 1321 |
( |
| 1322 |
(Hacl_Streaming_MD_state_64){ |
| 1323 |
.block_state = block_state1, |
| 1324 |
.buf = buf, |
| 1325 |
.total_len = total_len1 + (uint64_t)(chunk_len - diff) |
| 1326 |
} |
| 1327 |
); |
| 1328 |
} |
| 1329 |
return Hacl_Streaming_Types_Success; |
| 1330 |
} |
| 1331 |
|
| 1332 |
/** |
| 1333 |
Feed an arbitrary amount of data into the hash. This function returns 0 for |
| 1334 |
success, or 1 if the combined length of all of the data passed to `update_512` |
| 1335 |
(since the last call to `reset_512`) exceeds 2^125-1 bytes. |
| 1336 |
|
| 1337 |
This function is identical to the update function for SHA2_384. |
| 1338 |
*/ |
| 1339 |
Hacl_Streaming_Types_error_code |
| 1340 |
Hacl_Hash_SHA2_update_512( |
| 1341 |
Hacl_Streaming_MD_state_64 *state, |
| 1342 |
uint8_t *input, |
| 1343 |
uint32_t input_len |
| 1344 |
) |
| 1345 |
{ |
| 1346 |
return update_384_512(state, input, input_len); |
| 1347 |
} |
| 1348 |
|
| 1349 |
/** |
| 1350 |
Write the resulting hash into `output`, an array of 64 bytes. The state remains |
| 1351 |
valid after a call to `digest_512`, meaning the user may feed more data into |
| 1352 |
the hash via `update_512`. (The digest_512 function operates on an internal copy of |
| 1353 |
the state and therefore does not invalidate the client-held state `p`.) |
| 1354 |
*/ |
| 1355 |
void Hacl_Hash_SHA2_digest_512(Hacl_Streaming_MD_state_64 *state, uint8_t *output) |
| 1356 |
{ |
| 1357 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1358 |
uint64_t *block_state = scrut.block_state; |
| 1359 |
uint8_t *buf_ = scrut.buf; |
| 1360 |
uint64_t total_len = scrut.total_len; |
| 1361 |
uint32_t r; |
| 1362 |
if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL) |
| 1363 |
{ |
| 1364 |
r = 128U; |
| 1365 |
} |
| 1366 |
else |
| 1367 |
{ |
| 1368 |
r = (uint32_t)(total_len % (uint64_t)128U); |
| 1369 |
} |
| 1370 |
uint8_t *buf_1 = buf_; |
| 1371 |
uint64_t tmp_block_state[8U] = { 0U }; |
| 1372 |
memcpy(tmp_block_state, block_state, 8U * sizeof (uint64_t)); |
| 1373 |
uint32_t ite; |
| 1374 |
if (r % 128U == 0U && r > 0U) |
| 1375 |
{ |
| 1376 |
ite = 128U; |
| 1377 |
} |
| 1378 |
else |
| 1379 |
{ |
| 1380 |
ite = r % 128U; |
| 1381 |
} |
| 1382 |
uint8_t *buf_last = buf_1 + r - ite; |
| 1383 |
uint8_t *buf_multi = buf_1; |
| 1384 |
Hacl_Hash_SHA2_sha512_update_nblocks(0U, buf_multi, tmp_block_state); |
| 1385 |
uint64_t prev_len_last = total_len - (uint64_t)r; |
| 1386 |
Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last), |
| 1387 |
FStar_UInt128_uint64_to_uint128((uint64_t)r)), |
| 1388 |
r, |
| 1389 |
buf_last, |
| 1390 |
tmp_block_state); |
| 1391 |
Hacl_Hash_SHA2_sha512_finish(tmp_block_state, output); |
| 1392 |
} |
| 1393 |
|
| 1394 |
/** |
| 1395 |
Free a state allocated with `malloc_512`. |
| 1396 |
|
| 1397 |
This function is identical to the free function for SHA2_384. |
| 1398 |
*/ |
| 1399 |
void Hacl_Hash_SHA2_free_512(Hacl_Streaming_MD_state_64 *state) |
| 1400 |
{ |
| 1401 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1402 |
uint8_t *buf = scrut.buf; |
| 1403 |
uint64_t *block_state = scrut.block_state; |
| 1404 |
KRML_HOST_FREE(block_state); |
| 1405 |
KRML_HOST_FREE(buf); |
| 1406 |
KRML_HOST_FREE(state); |
| 1407 |
} |
| 1408 |
|
| 1409 |
/** |
| 1410 |
Hash `input`, of len `input_len`, into `output`, an array of 64 bytes. |
| 1411 |
*/ |
| 1412 |
void Hacl_Hash_SHA2_hash_512(uint8_t *output, uint8_t *input, uint32_t input_len) |
| 1413 |
{ |
| 1414 |
uint8_t *ib = input; |
| 1415 |
uint8_t *rb = output; |
| 1416 |
uint64_t st[8U] = { 0U }; |
| 1417 |
Hacl_Hash_SHA2_sha512_init(st); |
| 1418 |
uint32_t rem = input_len % 128U; |
| 1419 |
FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len); |
| 1420 |
Hacl_Hash_SHA2_sha512_update_nblocks(input_len, ib, st); |
| 1421 |
uint32_t rem1 = input_len % 128U; |
| 1422 |
uint8_t *b0 = ib; |
| 1423 |
uint8_t *lb = b0 + input_len - rem1; |
| 1424 |
Hacl_Hash_SHA2_sha512_update_last(len_, rem, lb, st); |
| 1425 |
Hacl_Hash_SHA2_sha512_finish(st, rb); |
| 1426 |
} |
| 1427 |
|
| 1428 |
Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_384(void) |
| 1429 |
{ |
| 1430 |
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t)); |
| 1431 |
if (buf == NULL) |
| 1432 |
{ |
| 1433 |
return NULL; |
| 1434 |
} |
| 1435 |
uint8_t *buf1 = buf; |
| 1436 |
uint64_t *b = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t)); |
| 1437 |
Hacl_Streaming_Types_optional_64 block_state; |
| 1438 |
if (b == NULL) |
| 1439 |
{ |
| 1440 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_None }); |
| 1441 |
} |
| 1442 |
else |
| 1443 |
{ |
| 1444 |
block_state = ((Hacl_Streaming_Types_optional_64){ .tag = Hacl_Streaming_Types_Some, .v = b }); |
| 1445 |
} |
| 1446 |
if (block_state.tag == Hacl_Streaming_Types_None) |
| 1447 |
{ |
| 1448 |
KRML_HOST_FREE(buf1); |
| 1449 |
return NULL; |
| 1450 |
} |
| 1451 |
if (block_state.tag == Hacl_Streaming_Types_Some) |
| 1452 |
{ |
| 1453 |
uint64_t *block_state1 = block_state.v; |
| 1454 |
Hacl_Streaming_Types_optional k_ = Hacl_Streaming_Types_Some; |
| 1455 |
switch (k_) |
| 1456 |
{ |
| 1457 |
case Hacl_Streaming_Types_None: |
| 1458 |
{ |
| 1459 |
return NULL; |
| 1460 |
} |
| 1461 |
case Hacl_Streaming_Types_Some: |
| 1462 |
{ |
| 1463 |
Hacl_Streaming_MD_state_64 |
| 1464 |
s = { .block_state = block_state1, .buf = buf1, .total_len = (uint64_t)0U }; |
| 1465 |
Hacl_Streaming_MD_state_64 |
| 1466 |
*p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64)); |
| 1467 |
if (p != NULL) |
| 1468 |
{ |
| 1469 |
p[0U] = s; |
| 1470 |
} |
| 1471 |
if (p == NULL) |
| 1472 |
{ |
| 1473 |
KRML_HOST_FREE(block_state1); |
| 1474 |
KRML_HOST_FREE(buf1); |
| 1475 |
return NULL; |
| 1476 |
} |
| 1477 |
Hacl_Hash_SHA2_sha384_init(block_state1); |
| 1478 |
return p; |
| 1479 |
} |
| 1480 |
default: |
| 1481 |
{ |
| 1482 |
KRML_HOST_EPRINTF("KaRaMeL incomplete match at %s:%d\n", __FILE__, __LINE__); |
| 1483 |
KRML_HOST_EXIT(253U); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
KRML_HOST_EPRINTF("KaRaMeL abort at %s:%d\n%s\n", |
| 1488 |
__FILE__, |
| 1489 |
__LINE__, |
| 1490 |
"unreachable (pattern matches are exhaustive in F*)"); |
| 1491 |
KRML_HOST_EXIT(255U); |
| 1492 |
} |
| 1493 |
|
| 1494 |
void Hacl_Hash_SHA2_reset_384(Hacl_Streaming_MD_state_64 *state) |
| 1495 |
{ |
| 1496 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1497 |
uint8_t *buf = scrut.buf; |
| 1498 |
uint64_t *block_state = scrut.block_state; |
| 1499 |
Hacl_Hash_SHA2_sha384_init(block_state); |
| 1500 |
Hacl_Streaming_MD_state_64 |
| 1501 |
tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U }; |
| 1502 |
state[0U] = tmp; |
| 1503 |
} |
| 1504 |
|
| 1505 |
Hacl_Streaming_Types_error_code |
| 1506 |
Hacl_Hash_SHA2_update_384( |
| 1507 |
Hacl_Streaming_MD_state_64 *state, |
| 1508 |
uint8_t *input, |
| 1509 |
uint32_t input_len |
| 1510 |
) |
| 1511 |
{ |
| 1512 |
return update_384_512(state, input, input_len); |
| 1513 |
} |
| 1514 |
|
| 1515 |
/** |
| 1516 |
Write the resulting hash into `output`, an array of 48 bytes. The state remains |
| 1517 |
valid after a call to `digest_384`, meaning the user may feed more data into |
| 1518 |
the hash via `update_384`. |
| 1519 |
*/ |
| 1520 |
void Hacl_Hash_SHA2_digest_384(Hacl_Streaming_MD_state_64 *state, uint8_t *output) |
| 1521 |
{ |
| 1522 |
Hacl_Streaming_MD_state_64 scrut = *state; |
| 1523 |
uint64_t *block_state = scrut.block_state; |
| 1524 |
uint8_t *buf_ = scrut.buf; |
| 1525 |
uint64_t total_len = scrut.total_len; |
| 1526 |
uint32_t r; |
| 1527 |
if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL) |
| 1528 |
{ |
| 1529 |
r = 128U; |
| 1530 |
} |
| 1531 |
else |
| 1532 |
{ |
| 1533 |
r = (uint32_t)(total_len % (uint64_t)128U); |
| 1534 |
} |
| 1535 |
uint8_t *buf_1 = buf_; |
| 1536 |
uint64_t tmp_block_state[8U] = { 0U }; |
| 1537 |
memcpy(tmp_block_state, block_state, 8U * sizeof (uint64_t)); |
| 1538 |
uint32_t ite; |
| 1539 |
if (r % 128U == 0U && r > 0U) |
| 1540 |
{ |
| 1541 |
ite = 128U; |
| 1542 |
} |
| 1543 |
else |
| 1544 |
{ |
| 1545 |
ite = r % 128U; |
| 1546 |
} |
| 1547 |
uint8_t *buf_last = buf_1 + r - ite; |
| 1548 |
uint8_t *buf_multi = buf_1; |
| 1549 |
Hacl_Hash_SHA2_sha384_update_nblocks(0U, buf_multi, tmp_block_state); |
| 1550 |
uint64_t prev_len_last = total_len - (uint64_t)r; |
| 1551 |
Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last), |
| 1552 |
FStar_UInt128_uint64_to_uint128((uint64_t)r)), |
| 1553 |
r, |
| 1554 |
buf_last, |
| 1555 |
tmp_block_state); |
| 1556 |
Hacl_Hash_SHA2_sha384_finish(tmp_block_state, output); |
| 1557 |
} |
| 1558 |
|
| 1559 |
void Hacl_Hash_SHA2_free_384(Hacl_Streaming_MD_state_64 *state) |
| 1560 |
{ |
| 1561 |
Hacl_Hash_SHA2_free_512(state); |
| 1562 |
} |
| 1563 |
|
| 1564 |
/** |
| 1565 |
Hash `input`, of len `input_len`, into `output`, an array of 48 bytes. |
| 1566 |
*/ |
| 1567 |
void Hacl_Hash_SHA2_hash_384(uint8_t *output, uint8_t *input, uint32_t input_len) |
| 1568 |
{ |
| 1569 |
uint8_t *ib = input; |
| 1570 |
uint8_t *rb = output; |
| 1571 |
uint64_t st[8U] = { 0U }; |
| 1572 |
Hacl_Hash_SHA2_sha384_init(st); |
| 1573 |
uint32_t rem = input_len % 128U; |
| 1574 |
FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len); |
| 1575 |
Hacl_Hash_SHA2_sha384_update_nblocks(input_len, ib, st); |
| 1576 |
uint32_t rem1 = input_len % 128U; |
| 1577 |
uint8_t *b0 = ib; |
| 1578 |
uint8_t *lb = b0 + input_len - rem1; |
| 1579 |
Hacl_Hash_SHA2_sha384_update_last(len_, rem, lb, st); |
| 1580 |
Hacl_Hash_SHA2_sha384_finish(st, rb); |
| 1581 |
} |
| 1582 |
|