| 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_HMAC.h" |
| 27 |
|
| 28 |
#include "Hacl_Streaming_Types.h" |
| 29 |
|
| 30 |
#include "Hacl_Hash_SHA3.h" |
| 31 |
#include "Hacl_Hash_SHA2.h" |
| 32 |
#include "Hacl_Hash_Blake2s.h" |
| 33 |
#include "Hacl_Hash_Blake2b.h" |
| 34 |
|
| 35 |
#include "internal/Hacl_Hash_SHA3.h" |
| 36 |
#include "internal/Hacl_Hash_SHA2.h" |
| 37 |
#include "internal/Hacl_Hash_SHA1.h" |
| 38 |
#include "internal/Hacl_Hash_MD5.h" |
| 39 |
#include "internal/Hacl_Hash_Blake2s.h" |
| 40 |
#include "internal/Hacl_Hash_Blake2b.h" |
| 41 |
|
| 42 |
/** |
| 43 |
Write the HMAC-MD5 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 44 |
|
| 45 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 64 byte. |
| 46 |
`dst` must point to 16 bytes of memory. |
| 47 |
*/ |
| 48 |
void |
| 49 |
Hacl_HMAC_compute_md5( |
| 50 |
uint8_t *dst, |
| 51 |
uint8_t *key, |
| 52 |
uint32_t key_len, |
| 53 |
uint8_t *data, |
| 54 |
uint32_t data_len |
| 55 |
) |
| 56 |
{ |
| 57 |
uint8_t key_block[64U]; |
| 58 |
memset(key_block, 0U, 64U * sizeof (uint8_t)); |
| 59 |
uint8_t *nkey = key_block; |
| 60 |
uint32_t ite; |
| 61 |
if (key_len <= 64U) |
| 62 |
{ |
| 63 |
ite = key_len; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
ite = 16U; |
| 68 |
} |
| 69 |
uint8_t *zeroes = key_block + ite; |
| 70 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 71 |
if (key_len <= 64U) |
| 72 |
{ |
| 73 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
Hacl_Hash_MD5_hash_oneshot(nkey, key, key_len); |
| 78 |
} |
| 79 |
uint8_t ipad[64U]; |
| 80 |
memset(ipad, 0x36U, 64U * sizeof (uint8_t)); |
| 81 |
for (uint32_t i = 0U; i < 64U; i++) |
| 82 |
{ |
| 83 |
uint8_t xi = ipad[i]; |
| 84 |
uint8_t yi = key_block[i]; |
| 85 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 86 |
} |
| 87 |
uint8_t opad[64U]; |
| 88 |
memset(opad, 0x5cU, 64U * sizeof (uint8_t)); |
| 89 |
for (uint32_t i = 0U; i < 64U; i++) |
| 90 |
{ |
| 91 |
uint8_t xi = opad[i]; |
| 92 |
uint8_t yi = key_block[i]; |
| 93 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 94 |
} |
| 95 |
uint32_t s[4U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U }; |
| 96 |
uint8_t *dst1 = ipad; |
| 97 |
if (data_len == 0U) |
| 98 |
{ |
| 99 |
Hacl_Hash_MD5_update_last(s, 0ULL, ipad, 64U); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
uint32_t block_len = 64U; |
| 104 |
uint32_t n_blocks0 = data_len / block_len; |
| 105 |
uint32_t rem0 = data_len % block_len; |
| 106 |
K___uint32_t_uint32_t scrut; |
| 107 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 108 |
{ |
| 109 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 110 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 115 |
} |
| 116 |
uint32_t n_blocks = scrut.fst; |
| 117 |
uint32_t rem_len = scrut.snd; |
| 118 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 119 |
uint8_t *full_blocks = data; |
| 120 |
uint8_t *rem = data + full_blocks_len; |
| 121 |
Hacl_Hash_MD5_update_multi(s, ipad, 1U); |
| 122 |
Hacl_Hash_MD5_update_multi(s, full_blocks, n_blocks); |
| 123 |
Hacl_Hash_MD5_update_last(s, (uint64_t)64U + (uint64_t)full_blocks_len, rem, rem_len); |
| 124 |
} |
| 125 |
Hacl_Hash_MD5_finish(s, dst1); |
| 126 |
uint8_t *hash1 = ipad; |
| 127 |
Hacl_Hash_MD5_init(s); |
| 128 |
uint32_t block_len = 64U; |
| 129 |
uint32_t n_blocks0 = 16U / block_len; |
| 130 |
uint32_t rem0 = 16U % block_len; |
| 131 |
K___uint32_t_uint32_t scrut; |
| 132 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 133 |
{ |
| 134 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 135 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 16U - n_blocks_ * block_len }); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 140 |
} |
| 141 |
uint32_t n_blocks = scrut.fst; |
| 142 |
uint32_t rem_len = scrut.snd; |
| 143 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 144 |
uint8_t *full_blocks = hash1; |
| 145 |
uint8_t *rem = hash1 + full_blocks_len; |
| 146 |
Hacl_Hash_MD5_update_multi(s, opad, 1U); |
| 147 |
Hacl_Hash_MD5_update_multi(s, full_blocks, n_blocks); |
| 148 |
Hacl_Hash_MD5_update_last(s, (uint64_t)64U + (uint64_t)full_blocks_len, rem, rem_len); |
| 149 |
Hacl_Hash_MD5_finish(s, dst); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
Write the HMAC-SHA-1 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 154 |
|
| 155 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 64 byte. |
| 156 |
`dst` must point to 20 bytes of memory. |
| 157 |
*/ |
| 158 |
void |
| 159 |
Hacl_HMAC_compute_sha1( |
| 160 |
uint8_t *dst, |
| 161 |
uint8_t *key, |
| 162 |
uint32_t key_len, |
| 163 |
uint8_t *data, |
| 164 |
uint32_t data_len |
| 165 |
) |
| 166 |
{ |
| 167 |
uint8_t key_block[64U]; |
| 168 |
memset(key_block, 0U, 64U * sizeof (uint8_t)); |
| 169 |
uint8_t *nkey = key_block; |
| 170 |
uint32_t ite; |
| 171 |
if (key_len <= 64U) |
| 172 |
{ |
| 173 |
ite = key_len; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
ite = 20U; |
| 178 |
} |
| 179 |
uint8_t *zeroes = key_block + ite; |
| 180 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 181 |
if (key_len <= 64U) |
| 182 |
{ |
| 183 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
Hacl_Hash_SHA1_hash_oneshot(nkey, key, key_len); |
| 188 |
} |
| 189 |
uint8_t ipad[64U]; |
| 190 |
memset(ipad, 0x36U, 64U * sizeof (uint8_t)); |
| 191 |
for (uint32_t i = 0U; i < 64U; i++) |
| 192 |
{ |
| 193 |
uint8_t xi = ipad[i]; |
| 194 |
uint8_t yi = key_block[i]; |
| 195 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 196 |
} |
| 197 |
uint8_t opad[64U]; |
| 198 |
memset(opad, 0x5cU, 64U * sizeof (uint8_t)); |
| 199 |
for (uint32_t i = 0U; i < 64U; i++) |
| 200 |
{ |
| 201 |
uint8_t xi = opad[i]; |
| 202 |
uint8_t yi = key_block[i]; |
| 203 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 204 |
} |
| 205 |
uint32_t s[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U }; |
| 206 |
uint8_t *dst1 = ipad; |
| 207 |
if (data_len == 0U) |
| 208 |
{ |
| 209 |
Hacl_Hash_SHA1_update_last(s, 0ULL, ipad, 64U); |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
uint32_t block_len = 64U; |
| 214 |
uint32_t n_blocks0 = data_len / block_len; |
| 215 |
uint32_t rem0 = data_len % block_len; |
| 216 |
K___uint32_t_uint32_t scrut; |
| 217 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 218 |
{ |
| 219 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 220 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 225 |
} |
| 226 |
uint32_t n_blocks = scrut.fst; |
| 227 |
uint32_t rem_len = scrut.snd; |
| 228 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 229 |
uint8_t *full_blocks = data; |
| 230 |
uint8_t *rem = data + full_blocks_len; |
| 231 |
Hacl_Hash_SHA1_update_multi(s, ipad, 1U); |
| 232 |
Hacl_Hash_SHA1_update_multi(s, full_blocks, n_blocks); |
| 233 |
Hacl_Hash_SHA1_update_last(s, (uint64_t)64U + (uint64_t)full_blocks_len, rem, rem_len); |
| 234 |
} |
| 235 |
Hacl_Hash_SHA1_finish(s, dst1); |
| 236 |
uint8_t *hash1 = ipad; |
| 237 |
Hacl_Hash_SHA1_init(s); |
| 238 |
uint32_t block_len = 64U; |
| 239 |
uint32_t n_blocks0 = 20U / block_len; |
| 240 |
uint32_t rem0 = 20U % block_len; |
| 241 |
K___uint32_t_uint32_t scrut; |
| 242 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 243 |
{ |
| 244 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 245 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 20U - n_blocks_ * block_len }); |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 250 |
} |
| 251 |
uint32_t n_blocks = scrut.fst; |
| 252 |
uint32_t rem_len = scrut.snd; |
| 253 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 254 |
uint8_t *full_blocks = hash1; |
| 255 |
uint8_t *rem = hash1 + full_blocks_len; |
| 256 |
Hacl_Hash_SHA1_update_multi(s, opad, 1U); |
| 257 |
Hacl_Hash_SHA1_update_multi(s, full_blocks, n_blocks); |
| 258 |
Hacl_Hash_SHA1_update_last(s, (uint64_t)64U + (uint64_t)full_blocks_len, rem, rem_len); |
| 259 |
Hacl_Hash_SHA1_finish(s, dst); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
Write the HMAC-SHA-2-224 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 264 |
|
| 265 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 64 bytes. |
| 266 |
`dst` must point to 28 bytes of memory. |
| 267 |
*/ |
| 268 |
void |
| 269 |
Hacl_HMAC_compute_sha2_224( |
| 270 |
uint8_t *dst, |
| 271 |
uint8_t *key, |
| 272 |
uint32_t key_len, |
| 273 |
uint8_t *data, |
| 274 |
uint32_t data_len |
| 275 |
) |
| 276 |
{ |
| 277 |
uint8_t key_block[64U]; |
| 278 |
memset(key_block, 0U, 64U * sizeof (uint8_t)); |
| 279 |
uint8_t *nkey = key_block; |
| 280 |
uint32_t ite; |
| 281 |
if (key_len <= 64U) |
| 282 |
{ |
| 283 |
ite = key_len; |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
ite = 28U; |
| 288 |
} |
| 289 |
uint8_t *zeroes = key_block + ite; |
| 290 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 291 |
if (key_len <= 64U) |
| 292 |
{ |
| 293 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
Hacl_Hash_SHA2_hash_224(nkey, key, key_len); |
| 298 |
} |
| 299 |
uint8_t ipad[64U]; |
| 300 |
memset(ipad, 0x36U, 64U * sizeof (uint8_t)); |
| 301 |
for (uint32_t i = 0U; i < 64U; i++) |
| 302 |
{ |
| 303 |
uint8_t xi = ipad[i]; |
| 304 |
uint8_t yi = key_block[i]; |
| 305 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 306 |
} |
| 307 |
uint8_t opad[64U]; |
| 308 |
memset(opad, 0x5cU, 64U * sizeof (uint8_t)); |
| 309 |
for (uint32_t i = 0U; i < 64U; i++) |
| 310 |
{ |
| 311 |
uint8_t xi = opad[i]; |
| 312 |
uint8_t yi = key_block[i]; |
| 313 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 314 |
} |
| 315 |
uint32_t st[8U] = { 0U }; |
| 316 |
KRML_MAYBE_FOR8(i, |
| 317 |
0U, |
| 318 |
8U, |
| 319 |
1U, |
| 320 |
uint32_t *os = st; |
| 321 |
uint32_t x = Hacl_Hash_SHA2_h224[i]; |
| 322 |
os[i] = x;); |
| 323 |
uint32_t *s = st; |
| 324 |
uint8_t *dst1 = ipad; |
| 325 |
if (data_len == 0U) |
| 326 |
{ |
| 327 |
Hacl_Hash_SHA2_sha224_update_last(0ULL + (uint64_t)64U, 64U, ipad, s); |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
uint32_t block_len = 64U; |
| 332 |
uint32_t n_blocks0 = data_len / block_len; |
| 333 |
uint32_t rem0 = data_len % block_len; |
| 334 |
K___uint32_t_uint32_t scrut; |
| 335 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 336 |
{ |
| 337 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 338 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 343 |
} |
| 344 |
uint32_t n_blocks = scrut.fst; |
| 345 |
uint32_t rem_len = scrut.snd; |
| 346 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 347 |
uint8_t *full_blocks = data; |
| 348 |
uint8_t *rem = data + full_blocks_len; |
| 349 |
Hacl_Hash_SHA2_sha224_update_nblocks(64U, ipad, s); |
| 350 |
Hacl_Hash_SHA2_sha224_update_nblocks(n_blocks * 64U, full_blocks, s); |
| 351 |
Hacl_Hash_SHA2_sha224_update_last((uint64_t)64U + (uint64_t)full_blocks_len + (uint64_t)rem_len, |
| 352 |
rem_len, |
| 353 |
rem, |
| 354 |
s); |
| 355 |
} |
| 356 |
Hacl_Hash_SHA2_sha224_finish(s, dst1); |
| 357 |
uint8_t *hash1 = ipad; |
| 358 |
Hacl_Hash_SHA2_sha224_init(s); |
| 359 |
uint32_t block_len = 64U; |
| 360 |
uint32_t n_blocks0 = 28U / block_len; |
| 361 |
uint32_t rem0 = 28U % block_len; |
| 362 |
K___uint32_t_uint32_t scrut; |
| 363 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 364 |
{ |
| 365 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 366 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 28U - n_blocks_ * block_len }); |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 371 |
} |
| 372 |
uint32_t n_blocks = scrut.fst; |
| 373 |
uint32_t rem_len = scrut.snd; |
| 374 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 375 |
uint8_t *full_blocks = hash1; |
| 376 |
uint8_t *rem = hash1 + full_blocks_len; |
| 377 |
Hacl_Hash_SHA2_sha224_update_nblocks(64U, opad, s); |
| 378 |
Hacl_Hash_SHA2_sha224_update_nblocks(n_blocks * 64U, full_blocks, s); |
| 379 |
Hacl_Hash_SHA2_sha224_update_last((uint64_t)64U + (uint64_t)full_blocks_len + (uint64_t)rem_len, |
| 380 |
rem_len, |
| 381 |
rem, |
| 382 |
s); |
| 383 |
Hacl_Hash_SHA2_sha224_finish(s, dst); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
Write the HMAC-SHA-2-256 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 388 |
|
| 389 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 64 bytes. |
| 390 |
`dst` must point to 32 bytes of memory. |
| 391 |
*/ |
| 392 |
void |
| 393 |
Hacl_HMAC_compute_sha2_256( |
| 394 |
uint8_t *dst, |
| 395 |
uint8_t *key, |
| 396 |
uint32_t key_len, |
| 397 |
uint8_t *data, |
| 398 |
uint32_t data_len |
| 399 |
) |
| 400 |
{ |
| 401 |
uint8_t key_block[64U]; |
| 402 |
memset(key_block, 0U, 64U * sizeof (uint8_t)); |
| 403 |
uint8_t *nkey = key_block; |
| 404 |
uint32_t ite; |
| 405 |
if (key_len <= 64U) |
| 406 |
{ |
| 407 |
ite = key_len; |
| 408 |
} |
| 409 |
else |
| 410 |
{ |
| 411 |
ite = 32U; |
| 412 |
} |
| 413 |
uint8_t *zeroes = key_block + ite; |
| 414 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 415 |
if (key_len <= 64U) |
| 416 |
{ |
| 417 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
Hacl_Hash_SHA2_hash_256(nkey, key, key_len); |
| 422 |
} |
| 423 |
uint8_t ipad[64U]; |
| 424 |
memset(ipad, 0x36U, 64U * sizeof (uint8_t)); |
| 425 |
for (uint32_t i = 0U; i < 64U; i++) |
| 426 |
{ |
| 427 |
uint8_t xi = ipad[i]; |
| 428 |
uint8_t yi = key_block[i]; |
| 429 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 430 |
} |
| 431 |
uint8_t opad[64U]; |
| 432 |
memset(opad, 0x5cU, 64U * sizeof (uint8_t)); |
| 433 |
for (uint32_t i = 0U; i < 64U; i++) |
| 434 |
{ |
| 435 |
uint8_t xi = opad[i]; |
| 436 |
uint8_t yi = key_block[i]; |
| 437 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 438 |
} |
| 439 |
uint32_t st[8U] = { 0U }; |
| 440 |
KRML_MAYBE_FOR8(i, |
| 441 |
0U, |
| 442 |
8U, |
| 443 |
1U, |
| 444 |
uint32_t *os = st; |
| 445 |
uint32_t x = Hacl_Hash_SHA2_h256[i]; |
| 446 |
os[i] = x;); |
| 447 |
uint32_t *s = st; |
| 448 |
uint8_t *dst1 = ipad; |
| 449 |
if (data_len == 0U) |
| 450 |
{ |
| 451 |
Hacl_Hash_SHA2_sha256_update_last(0ULL + (uint64_t)64U, 64U, ipad, s); |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
uint32_t block_len = 64U; |
| 456 |
uint32_t n_blocks0 = data_len / block_len; |
| 457 |
uint32_t rem0 = data_len % block_len; |
| 458 |
K___uint32_t_uint32_t scrut; |
| 459 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 460 |
{ |
| 461 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 462 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 467 |
} |
| 468 |
uint32_t n_blocks = scrut.fst; |
| 469 |
uint32_t rem_len = scrut.snd; |
| 470 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 471 |
uint8_t *full_blocks = data; |
| 472 |
uint8_t *rem = data + full_blocks_len; |
| 473 |
Hacl_Hash_SHA2_sha256_update_nblocks(64U, ipad, s); |
| 474 |
Hacl_Hash_SHA2_sha256_update_nblocks(n_blocks * 64U, full_blocks, s); |
| 475 |
Hacl_Hash_SHA2_sha256_update_last((uint64_t)64U + (uint64_t)full_blocks_len + (uint64_t)rem_len, |
| 476 |
rem_len, |
| 477 |
rem, |
| 478 |
s); |
| 479 |
} |
| 480 |
Hacl_Hash_SHA2_sha256_finish(s, dst1); |
| 481 |
uint8_t *hash1 = ipad; |
| 482 |
Hacl_Hash_SHA2_sha256_init(s); |
| 483 |
uint32_t block_len = 64U; |
| 484 |
uint32_t n_blocks0 = 32U / block_len; |
| 485 |
uint32_t rem0 = 32U % block_len; |
| 486 |
K___uint32_t_uint32_t scrut; |
| 487 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 488 |
{ |
| 489 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 490 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 32U - n_blocks_ * block_len }); |
| 491 |
} |
| 492 |
else |
| 493 |
{ |
| 494 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 495 |
} |
| 496 |
uint32_t n_blocks = scrut.fst; |
| 497 |
uint32_t rem_len = scrut.snd; |
| 498 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 499 |
uint8_t *full_blocks = hash1; |
| 500 |
uint8_t *rem = hash1 + full_blocks_len; |
| 501 |
Hacl_Hash_SHA2_sha256_update_nblocks(64U, opad, s); |
| 502 |
Hacl_Hash_SHA2_sha256_update_nblocks(n_blocks * 64U, full_blocks, s); |
| 503 |
Hacl_Hash_SHA2_sha256_update_last((uint64_t)64U + (uint64_t)full_blocks_len + (uint64_t)rem_len, |
| 504 |
rem_len, |
| 505 |
rem, |
| 506 |
s); |
| 507 |
Hacl_Hash_SHA2_sha256_finish(s, dst); |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
Write the HMAC-SHA-2-384 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 512 |
|
| 513 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 128 bytes. |
| 514 |
`dst` must point to 48 bytes of memory. |
| 515 |
*/ |
| 516 |
void |
| 517 |
Hacl_HMAC_compute_sha2_384( |
| 518 |
uint8_t *dst, |
| 519 |
uint8_t *key, |
| 520 |
uint32_t key_len, |
| 521 |
uint8_t *data, |
| 522 |
uint32_t data_len |
| 523 |
) |
| 524 |
{ |
| 525 |
uint8_t key_block[128U]; |
| 526 |
memset(key_block, 0U, 128U * sizeof (uint8_t)); |
| 527 |
uint8_t *nkey = key_block; |
| 528 |
uint32_t ite; |
| 529 |
if (key_len <= 128U) |
| 530 |
{ |
| 531 |
ite = key_len; |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
ite = 48U; |
| 536 |
} |
| 537 |
uint8_t *zeroes = key_block + ite; |
| 538 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 539 |
if (key_len <= 128U) |
| 540 |
{ |
| 541 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
Hacl_Hash_SHA2_hash_384(nkey, key, key_len); |
| 546 |
} |
| 547 |
uint8_t ipad[128U]; |
| 548 |
memset(ipad, 0x36U, 128U * sizeof (uint8_t)); |
| 549 |
for (uint32_t i = 0U; i < 128U; i++) |
| 550 |
{ |
| 551 |
uint8_t xi = ipad[i]; |
| 552 |
uint8_t yi = key_block[i]; |
| 553 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 554 |
} |
| 555 |
uint8_t opad[128U]; |
| 556 |
memset(opad, 0x5cU, 128U * sizeof (uint8_t)); |
| 557 |
for (uint32_t i = 0U; i < 128U; i++) |
| 558 |
{ |
| 559 |
uint8_t xi = opad[i]; |
| 560 |
uint8_t yi = key_block[i]; |
| 561 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 562 |
} |
| 563 |
uint64_t st[8U] = { 0U }; |
| 564 |
KRML_MAYBE_FOR8(i, |
| 565 |
0U, |
| 566 |
8U, |
| 567 |
1U, |
| 568 |
uint64_t *os = st; |
| 569 |
uint64_t x = Hacl_Hash_SHA2_h384[i]; |
| 570 |
os[i] = x;); |
| 571 |
uint64_t *s = st; |
| 572 |
uint8_t *dst1 = ipad; |
| 573 |
if (data_len == 0U) |
| 574 |
{ |
| 575 |
Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(0ULL), |
| 576 |
FStar_UInt128_uint64_to_uint128((uint64_t)128U)), |
| 577 |
128U, |
| 578 |
ipad, |
| 579 |
s); |
| 580 |
} |
| 581 |
else |
| 582 |
{ |
| 583 |
uint32_t block_len = 128U; |
| 584 |
uint32_t n_blocks0 = data_len / block_len; |
| 585 |
uint32_t rem0 = data_len % block_len; |
| 586 |
K___uint32_t_uint32_t scrut; |
| 587 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 588 |
{ |
| 589 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 590 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 591 |
} |
| 592 |
else |
| 593 |
{ |
| 594 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 595 |
} |
| 596 |
uint32_t n_blocks = scrut.fst; |
| 597 |
uint32_t rem_len = scrut.snd; |
| 598 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 599 |
uint8_t *full_blocks = data; |
| 600 |
uint8_t *rem = data + full_blocks_len; |
| 601 |
Hacl_Hash_SHA2_sha384_update_nblocks(128U, ipad, s); |
| 602 |
Hacl_Hash_SHA2_sha384_update_nblocks(n_blocks * 128U, full_blocks, s); |
| 603 |
Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 604 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 605 |
FStar_UInt128_uint64_to_uint128((uint64_t)rem_len)), |
| 606 |
rem_len, |
| 607 |
rem, |
| 608 |
s); |
| 609 |
} |
| 610 |
Hacl_Hash_SHA2_sha384_finish(s, dst1); |
| 611 |
uint8_t *hash1 = ipad; |
| 612 |
Hacl_Hash_SHA2_sha384_init(s); |
| 613 |
uint32_t block_len = 128U; |
| 614 |
uint32_t n_blocks0 = 48U / block_len; |
| 615 |
uint32_t rem0 = 48U % block_len; |
| 616 |
K___uint32_t_uint32_t scrut; |
| 617 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 618 |
{ |
| 619 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 620 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 48U - n_blocks_ * block_len }); |
| 621 |
} |
| 622 |
else |
| 623 |
{ |
| 624 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 625 |
} |
| 626 |
uint32_t n_blocks = scrut.fst; |
| 627 |
uint32_t rem_len = scrut.snd; |
| 628 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 629 |
uint8_t *full_blocks = hash1; |
| 630 |
uint8_t *rem = hash1 + full_blocks_len; |
| 631 |
Hacl_Hash_SHA2_sha384_update_nblocks(128U, opad, s); |
| 632 |
Hacl_Hash_SHA2_sha384_update_nblocks(n_blocks * 128U, full_blocks, s); |
| 633 |
Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 634 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 635 |
FStar_UInt128_uint64_to_uint128((uint64_t)rem_len)), |
| 636 |
rem_len, |
| 637 |
rem, |
| 638 |
s); |
| 639 |
Hacl_Hash_SHA2_sha384_finish(s, dst); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
Write the HMAC-SHA-2-512 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 644 |
|
| 645 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 128 bytes. |
| 646 |
`dst` must point to 64 bytes of memory. |
| 647 |
*/ |
| 648 |
void |
| 649 |
Hacl_HMAC_compute_sha2_512( |
| 650 |
uint8_t *dst, |
| 651 |
uint8_t *key, |
| 652 |
uint32_t key_len, |
| 653 |
uint8_t *data, |
| 654 |
uint32_t data_len |
| 655 |
) |
| 656 |
{ |
| 657 |
uint8_t key_block[128U]; |
| 658 |
memset(key_block, 0U, 128U * sizeof (uint8_t)); |
| 659 |
uint8_t *nkey = key_block; |
| 660 |
uint32_t ite; |
| 661 |
if (key_len <= 128U) |
| 662 |
{ |
| 663 |
ite = key_len; |
| 664 |
} |
| 665 |
else |
| 666 |
{ |
| 667 |
ite = 64U; |
| 668 |
} |
| 669 |
uint8_t *zeroes = key_block + ite; |
| 670 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 671 |
if (key_len <= 128U) |
| 672 |
{ |
| 673 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 674 |
} |
| 675 |
else |
| 676 |
{ |
| 677 |
Hacl_Hash_SHA2_hash_512(nkey, key, key_len); |
| 678 |
} |
| 679 |
uint8_t ipad[128U]; |
| 680 |
memset(ipad, 0x36U, 128U * sizeof (uint8_t)); |
| 681 |
for (uint32_t i = 0U; i < 128U; i++) |
| 682 |
{ |
| 683 |
uint8_t xi = ipad[i]; |
| 684 |
uint8_t yi = key_block[i]; |
| 685 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 686 |
} |
| 687 |
uint8_t opad[128U]; |
| 688 |
memset(opad, 0x5cU, 128U * sizeof (uint8_t)); |
| 689 |
for (uint32_t i = 0U; i < 128U; i++) |
| 690 |
{ |
| 691 |
uint8_t xi = opad[i]; |
| 692 |
uint8_t yi = key_block[i]; |
| 693 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 694 |
} |
| 695 |
uint64_t st[8U] = { 0U }; |
| 696 |
KRML_MAYBE_FOR8(i, |
| 697 |
0U, |
| 698 |
8U, |
| 699 |
1U, |
| 700 |
uint64_t *os = st; |
| 701 |
uint64_t x = Hacl_Hash_SHA2_h512[i]; |
| 702 |
os[i] = x;); |
| 703 |
uint64_t *s = st; |
| 704 |
uint8_t *dst1 = ipad; |
| 705 |
if (data_len == 0U) |
| 706 |
{ |
| 707 |
Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(0ULL), |
| 708 |
FStar_UInt128_uint64_to_uint128((uint64_t)128U)), |
| 709 |
128U, |
| 710 |
ipad, |
| 711 |
s); |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
uint32_t block_len = 128U; |
| 716 |
uint32_t n_blocks0 = data_len / block_len; |
| 717 |
uint32_t rem0 = data_len % block_len; |
| 718 |
K___uint32_t_uint32_t scrut; |
| 719 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 720 |
{ |
| 721 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 722 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 727 |
} |
| 728 |
uint32_t n_blocks = scrut.fst; |
| 729 |
uint32_t rem_len = scrut.snd; |
| 730 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 731 |
uint8_t *full_blocks = data; |
| 732 |
uint8_t *rem = data + full_blocks_len; |
| 733 |
Hacl_Hash_SHA2_sha512_update_nblocks(128U, ipad, s); |
| 734 |
Hacl_Hash_SHA2_sha512_update_nblocks(n_blocks * 128U, full_blocks, s); |
| 735 |
Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 736 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 737 |
FStar_UInt128_uint64_to_uint128((uint64_t)rem_len)), |
| 738 |
rem_len, |
| 739 |
rem, |
| 740 |
s); |
| 741 |
} |
| 742 |
Hacl_Hash_SHA2_sha512_finish(s, dst1); |
| 743 |
uint8_t *hash1 = ipad; |
| 744 |
Hacl_Hash_SHA2_sha512_init(s); |
| 745 |
uint32_t block_len = 128U; |
| 746 |
uint32_t n_blocks0 = 64U / block_len; |
| 747 |
uint32_t rem0 = 64U % block_len; |
| 748 |
K___uint32_t_uint32_t scrut; |
| 749 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 750 |
{ |
| 751 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 752 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 64U - n_blocks_ * block_len }); |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 757 |
} |
| 758 |
uint32_t n_blocks = scrut.fst; |
| 759 |
uint32_t rem_len = scrut.snd; |
| 760 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 761 |
uint8_t *full_blocks = hash1; |
| 762 |
uint8_t *rem = hash1 + full_blocks_len; |
| 763 |
Hacl_Hash_SHA2_sha512_update_nblocks(128U, opad, s); |
| 764 |
Hacl_Hash_SHA2_sha512_update_nblocks(n_blocks * 128U, full_blocks, s); |
| 765 |
Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 766 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 767 |
FStar_UInt128_uint64_to_uint128((uint64_t)rem_len)), |
| 768 |
rem_len, |
| 769 |
rem, |
| 770 |
s); |
| 771 |
Hacl_Hash_SHA2_sha512_finish(s, dst); |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
Write the HMAC-SHA-3-224 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 776 |
|
| 777 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 144 bytes. |
| 778 |
`dst` must point to 28 bytes of memory. |
| 779 |
*/ |
| 780 |
void |
| 781 |
Hacl_HMAC_compute_sha3_224( |
| 782 |
uint8_t *dst, |
| 783 |
uint8_t *key, |
| 784 |
uint32_t key_len, |
| 785 |
uint8_t *data, |
| 786 |
uint32_t data_len |
| 787 |
) |
| 788 |
{ |
| 789 |
uint8_t key_block[144U]; |
| 790 |
memset(key_block, 0U, 144U * sizeof (uint8_t)); |
| 791 |
uint8_t *nkey = key_block; |
| 792 |
uint32_t ite; |
| 793 |
if (key_len <= 144U) |
| 794 |
{ |
| 795 |
ite = key_len; |
| 796 |
} |
| 797 |
else |
| 798 |
{ |
| 799 |
ite = 28U; |
| 800 |
} |
| 801 |
uint8_t *zeroes = key_block + ite; |
| 802 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 803 |
if (key_len <= 144U) |
| 804 |
{ |
| 805 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 806 |
} |
| 807 |
else |
| 808 |
{ |
| 809 |
Hacl_Hash_SHA3_sha3_224(nkey, key, key_len); |
| 810 |
} |
| 811 |
uint8_t ipad[144U]; |
| 812 |
memset(ipad, 0x36U, 144U * sizeof (uint8_t)); |
| 813 |
for (uint32_t i = 0U; i < 144U; i++) |
| 814 |
{ |
| 815 |
uint8_t xi = ipad[i]; |
| 816 |
uint8_t yi = key_block[i]; |
| 817 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 818 |
} |
| 819 |
uint8_t opad[144U]; |
| 820 |
memset(opad, 0x5cU, 144U * sizeof (uint8_t)); |
| 821 |
for (uint32_t i = 0U; i < 144U; i++) |
| 822 |
{ |
| 823 |
uint8_t xi = opad[i]; |
| 824 |
uint8_t yi = key_block[i]; |
| 825 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 826 |
} |
| 827 |
uint64_t s[25U] = { 0U }; |
| 828 |
uint8_t *dst1 = ipad; |
| 829 |
if (data_len == 0U) |
| 830 |
{ |
| 831 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_224, s, ipad, 144U); |
| 832 |
} |
| 833 |
else |
| 834 |
{ |
| 835 |
uint32_t block_len = 144U; |
| 836 |
uint32_t n_blocks0 = data_len / block_len; |
| 837 |
uint32_t rem0 = data_len % block_len; |
| 838 |
K___uint32_t_uint32_t scrut; |
| 839 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 840 |
{ |
| 841 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 842 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 843 |
} |
| 844 |
else |
| 845 |
{ |
| 846 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 847 |
} |
| 848 |
uint32_t n_blocks = scrut.fst; |
| 849 |
uint32_t rem_len = scrut.snd; |
| 850 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 851 |
uint8_t *full_blocks = data; |
| 852 |
uint8_t *rem = data + full_blocks_len; |
| 853 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_224, s, ipad, 1U); |
| 854 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_224, s, full_blocks, n_blocks); |
| 855 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_224, s, rem, rem_len); |
| 856 |
} |
| 857 |
uint32_t remOut = 28U; |
| 858 |
uint8_t hbuf0[256U] = { 0U }; |
| 859 |
uint64_t ws0[32U] = { 0U }; |
| 860 |
memcpy(ws0, s, 25U * sizeof (uint64_t)); |
| 861 |
for (uint32_t i = 0U; i < 32U; i++) |
| 862 |
{ |
| 863 |
store64_le(hbuf0 + i * 8U, ws0[i]); |
| 864 |
} |
| 865 |
memcpy(dst1 + 28U - remOut, hbuf0, remOut * sizeof (uint8_t)); |
| 866 |
uint8_t *hash1 = ipad; |
| 867 |
Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_224, s); |
| 868 |
uint32_t block_len = 144U; |
| 869 |
uint32_t n_blocks0 = 28U / block_len; |
| 870 |
uint32_t rem0 = 28U % block_len; |
| 871 |
K___uint32_t_uint32_t scrut; |
| 872 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 873 |
{ |
| 874 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 875 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 28U - n_blocks_ * block_len }); |
| 876 |
} |
| 877 |
else |
| 878 |
{ |
| 879 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 880 |
} |
| 881 |
uint32_t n_blocks = scrut.fst; |
| 882 |
uint32_t rem_len = scrut.snd; |
| 883 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 884 |
uint8_t *full_blocks = hash1; |
| 885 |
uint8_t *rem = hash1 + full_blocks_len; |
| 886 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_224, s, opad, 1U); |
| 887 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_224, s, full_blocks, n_blocks); |
| 888 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_224, s, rem, rem_len); |
| 889 |
uint32_t remOut0 = 28U; |
| 890 |
uint8_t hbuf[256U] = { 0U }; |
| 891 |
uint64_t ws[32U] = { 0U }; |
| 892 |
memcpy(ws, s, 25U * sizeof (uint64_t)); |
| 893 |
for (uint32_t i = 0U; i < 32U; i++) |
| 894 |
{ |
| 895 |
store64_le(hbuf + i * 8U, ws[i]); |
| 896 |
} |
| 897 |
memcpy(dst + 28U - remOut0, hbuf, remOut0 * sizeof (uint8_t)); |
| 898 |
} |
| 899 |
|
| 900 |
/** |
| 901 |
Write the HMAC-SHA-3-256 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 902 |
|
| 903 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 136 bytes. |
| 904 |
`dst` must point to 32 bytes of memory. |
| 905 |
*/ |
| 906 |
void |
| 907 |
Hacl_HMAC_compute_sha3_256( |
| 908 |
uint8_t *dst, |
| 909 |
uint8_t *key, |
| 910 |
uint32_t key_len, |
| 911 |
uint8_t *data, |
| 912 |
uint32_t data_len |
| 913 |
) |
| 914 |
{ |
| 915 |
uint8_t key_block[136U]; |
| 916 |
memset(key_block, 0U, 136U * sizeof (uint8_t)); |
| 917 |
uint8_t *nkey = key_block; |
| 918 |
uint32_t ite; |
| 919 |
if (key_len <= 136U) |
| 920 |
{ |
| 921 |
ite = key_len; |
| 922 |
} |
| 923 |
else |
| 924 |
{ |
| 925 |
ite = 32U; |
| 926 |
} |
| 927 |
uint8_t *zeroes = key_block + ite; |
| 928 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 929 |
if (key_len <= 136U) |
| 930 |
{ |
| 931 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 932 |
} |
| 933 |
else |
| 934 |
{ |
| 935 |
Hacl_Hash_SHA3_sha3_256(nkey, key, key_len); |
| 936 |
} |
| 937 |
uint8_t ipad[136U]; |
| 938 |
memset(ipad, 0x36U, 136U * sizeof (uint8_t)); |
| 939 |
for (uint32_t i = 0U; i < 136U; i++) |
| 940 |
{ |
| 941 |
uint8_t xi = ipad[i]; |
| 942 |
uint8_t yi = key_block[i]; |
| 943 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 944 |
} |
| 945 |
uint8_t opad[136U]; |
| 946 |
memset(opad, 0x5cU, 136U * sizeof (uint8_t)); |
| 947 |
for (uint32_t i = 0U; i < 136U; i++) |
| 948 |
{ |
| 949 |
uint8_t xi = opad[i]; |
| 950 |
uint8_t yi = key_block[i]; |
| 951 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 952 |
} |
| 953 |
uint64_t s[25U] = { 0U }; |
| 954 |
uint8_t *dst1 = ipad; |
| 955 |
if (data_len == 0U) |
| 956 |
{ |
| 957 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_256, s, ipad, 136U); |
| 958 |
} |
| 959 |
else |
| 960 |
{ |
| 961 |
uint32_t block_len = 136U; |
| 962 |
uint32_t n_blocks0 = data_len / block_len; |
| 963 |
uint32_t rem0 = data_len % block_len; |
| 964 |
K___uint32_t_uint32_t scrut; |
| 965 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 966 |
{ |
| 967 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 968 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 969 |
} |
| 970 |
else |
| 971 |
{ |
| 972 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 973 |
} |
| 974 |
uint32_t n_blocks = scrut.fst; |
| 975 |
uint32_t rem_len = scrut.snd; |
| 976 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 977 |
uint8_t *full_blocks = data; |
| 978 |
uint8_t *rem = data + full_blocks_len; |
| 979 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_256, s, ipad, 1U); |
| 980 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_256, s, full_blocks, n_blocks); |
| 981 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_256, s, rem, rem_len); |
| 982 |
} |
| 983 |
uint32_t remOut = 32U; |
| 984 |
uint8_t hbuf0[256U] = { 0U }; |
| 985 |
uint64_t ws0[32U] = { 0U }; |
| 986 |
memcpy(ws0, s, 25U * sizeof (uint64_t)); |
| 987 |
for (uint32_t i = 0U; i < 32U; i++) |
| 988 |
{ |
| 989 |
store64_le(hbuf0 + i * 8U, ws0[i]); |
| 990 |
} |
| 991 |
memcpy(dst1 + 32U - remOut, hbuf0, remOut * sizeof (uint8_t)); |
| 992 |
uint8_t *hash1 = ipad; |
| 993 |
Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_256, s); |
| 994 |
uint32_t block_len = 136U; |
| 995 |
uint32_t n_blocks0 = 32U / block_len; |
| 996 |
uint32_t rem0 = 32U % block_len; |
| 997 |
K___uint32_t_uint32_t scrut; |
| 998 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 999 |
{ |
| 1000 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1001 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 32U - n_blocks_ * block_len }); |
| 1002 |
} |
| 1003 |
else |
| 1004 |
{ |
| 1005 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1006 |
} |
| 1007 |
uint32_t n_blocks = scrut.fst; |
| 1008 |
uint32_t rem_len = scrut.snd; |
| 1009 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1010 |
uint8_t *full_blocks = hash1; |
| 1011 |
uint8_t *rem = hash1 + full_blocks_len; |
| 1012 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_256, s, opad, 1U); |
| 1013 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_256, s, full_blocks, n_blocks); |
| 1014 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_256, s, rem, rem_len); |
| 1015 |
uint32_t remOut0 = 32U; |
| 1016 |
uint8_t hbuf[256U] = { 0U }; |
| 1017 |
uint64_t ws[32U] = { 0U }; |
| 1018 |
memcpy(ws, s, 25U * sizeof (uint64_t)); |
| 1019 |
for (uint32_t i = 0U; i < 32U; i++) |
| 1020 |
{ |
| 1021 |
store64_le(hbuf + i * 8U, ws[i]); |
| 1022 |
} |
| 1023 |
memcpy(dst + 32U - remOut0, hbuf, remOut0 * sizeof (uint8_t)); |
| 1024 |
} |
| 1025 |
|
| 1026 |
/** |
| 1027 |
Write the HMAC-SHA-3-384 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 1028 |
|
| 1029 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 104 bytes. |
| 1030 |
`dst` must point to 48 bytes of memory. |
| 1031 |
*/ |
| 1032 |
void |
| 1033 |
Hacl_HMAC_compute_sha3_384( |
| 1034 |
uint8_t *dst, |
| 1035 |
uint8_t *key, |
| 1036 |
uint32_t key_len, |
| 1037 |
uint8_t *data, |
| 1038 |
uint32_t data_len |
| 1039 |
) |
| 1040 |
{ |
| 1041 |
uint8_t key_block[104U]; |
| 1042 |
memset(key_block, 0U, 104U * sizeof (uint8_t)); |
| 1043 |
uint8_t *nkey = key_block; |
| 1044 |
uint32_t ite; |
| 1045 |
if (key_len <= 104U) |
| 1046 |
{ |
| 1047 |
ite = key_len; |
| 1048 |
} |
| 1049 |
else |
| 1050 |
{ |
| 1051 |
ite = 48U; |
| 1052 |
} |
| 1053 |
uint8_t *zeroes = key_block + ite; |
| 1054 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 1055 |
if (key_len <= 104U) |
| 1056 |
{ |
| 1057 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 1058 |
} |
| 1059 |
else |
| 1060 |
{ |
| 1061 |
Hacl_Hash_SHA3_sha3_384(nkey, key, key_len); |
| 1062 |
} |
| 1063 |
uint8_t ipad[104U]; |
| 1064 |
memset(ipad, 0x36U, 104U * sizeof (uint8_t)); |
| 1065 |
for (uint32_t i = 0U; i < 104U; i++) |
| 1066 |
{ |
| 1067 |
uint8_t xi = ipad[i]; |
| 1068 |
uint8_t yi = key_block[i]; |
| 1069 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1070 |
} |
| 1071 |
uint8_t opad[104U]; |
| 1072 |
memset(opad, 0x5cU, 104U * sizeof (uint8_t)); |
| 1073 |
for (uint32_t i = 0U; i < 104U; i++) |
| 1074 |
{ |
| 1075 |
uint8_t xi = opad[i]; |
| 1076 |
uint8_t yi = key_block[i]; |
| 1077 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1078 |
} |
| 1079 |
uint64_t s[25U] = { 0U }; |
| 1080 |
uint8_t *dst1 = ipad; |
| 1081 |
if (data_len == 0U) |
| 1082 |
{ |
| 1083 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_384, s, ipad, 104U); |
| 1084 |
} |
| 1085 |
else |
| 1086 |
{ |
| 1087 |
uint32_t block_len = 104U; |
| 1088 |
uint32_t n_blocks0 = data_len / block_len; |
| 1089 |
uint32_t rem0 = data_len % block_len; |
| 1090 |
K___uint32_t_uint32_t scrut; |
| 1091 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1092 |
{ |
| 1093 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1094 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 1095 |
} |
| 1096 |
else |
| 1097 |
{ |
| 1098 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1099 |
} |
| 1100 |
uint32_t n_blocks = scrut.fst; |
| 1101 |
uint32_t rem_len = scrut.snd; |
| 1102 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1103 |
uint8_t *full_blocks = data; |
| 1104 |
uint8_t *rem = data + full_blocks_len; |
| 1105 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_384, s, ipad, 1U); |
| 1106 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_384, s, full_blocks, n_blocks); |
| 1107 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_384, s, rem, rem_len); |
| 1108 |
} |
| 1109 |
uint32_t remOut = 48U; |
| 1110 |
uint8_t hbuf0[256U] = { 0U }; |
| 1111 |
uint64_t ws0[32U] = { 0U }; |
| 1112 |
memcpy(ws0, s, 25U * sizeof (uint64_t)); |
| 1113 |
for (uint32_t i = 0U; i < 32U; i++) |
| 1114 |
{ |
| 1115 |
store64_le(hbuf0 + i * 8U, ws0[i]); |
| 1116 |
} |
| 1117 |
memcpy(dst1 + 48U - remOut, hbuf0, remOut * sizeof (uint8_t)); |
| 1118 |
uint8_t *hash1 = ipad; |
| 1119 |
Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_384, s); |
| 1120 |
uint32_t block_len = 104U; |
| 1121 |
uint32_t n_blocks0 = 48U / block_len; |
| 1122 |
uint32_t rem0 = 48U % block_len; |
| 1123 |
K___uint32_t_uint32_t scrut; |
| 1124 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1125 |
{ |
| 1126 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1127 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 48U - n_blocks_ * block_len }); |
| 1128 |
} |
| 1129 |
else |
| 1130 |
{ |
| 1131 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1132 |
} |
| 1133 |
uint32_t n_blocks = scrut.fst; |
| 1134 |
uint32_t rem_len = scrut.snd; |
| 1135 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1136 |
uint8_t *full_blocks = hash1; |
| 1137 |
uint8_t *rem = hash1 + full_blocks_len; |
| 1138 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_384, s, opad, 1U); |
| 1139 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_384, s, full_blocks, n_blocks); |
| 1140 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_384, s, rem, rem_len); |
| 1141 |
uint32_t remOut0 = 48U; |
| 1142 |
uint8_t hbuf[256U] = { 0U }; |
| 1143 |
uint64_t ws[32U] = { 0U }; |
| 1144 |
memcpy(ws, s, 25U * sizeof (uint64_t)); |
| 1145 |
for (uint32_t i = 0U; i < 32U; i++) |
| 1146 |
{ |
| 1147 |
store64_le(hbuf + i * 8U, ws[i]); |
| 1148 |
} |
| 1149 |
memcpy(dst + 48U - remOut0, hbuf, remOut0 * sizeof (uint8_t)); |
| 1150 |
} |
| 1151 |
|
| 1152 |
/** |
| 1153 |
Write the HMAC-SHA-3-512 MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 1154 |
|
| 1155 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 72 bytes. |
| 1156 |
`dst` must point to 64 bytes of memory. |
| 1157 |
*/ |
| 1158 |
void |
| 1159 |
Hacl_HMAC_compute_sha3_512( |
| 1160 |
uint8_t *dst, |
| 1161 |
uint8_t *key, |
| 1162 |
uint32_t key_len, |
| 1163 |
uint8_t *data, |
| 1164 |
uint32_t data_len |
| 1165 |
) |
| 1166 |
{ |
| 1167 |
uint8_t key_block[72U]; |
| 1168 |
memset(key_block, 0U, 72U * sizeof (uint8_t)); |
| 1169 |
uint8_t *nkey = key_block; |
| 1170 |
uint32_t ite; |
| 1171 |
if (key_len <= 72U) |
| 1172 |
{ |
| 1173 |
ite = key_len; |
| 1174 |
} |
| 1175 |
else |
| 1176 |
{ |
| 1177 |
ite = 64U; |
| 1178 |
} |
| 1179 |
uint8_t *zeroes = key_block + ite; |
| 1180 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 1181 |
if (key_len <= 72U) |
| 1182 |
{ |
| 1183 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 1184 |
} |
| 1185 |
else |
| 1186 |
{ |
| 1187 |
Hacl_Hash_SHA3_sha3_512(nkey, key, key_len); |
| 1188 |
} |
| 1189 |
uint8_t ipad[72U]; |
| 1190 |
memset(ipad, 0x36U, 72U * sizeof (uint8_t)); |
| 1191 |
for (uint32_t i = 0U; i < 72U; i++) |
| 1192 |
{ |
| 1193 |
uint8_t xi = ipad[i]; |
| 1194 |
uint8_t yi = key_block[i]; |
| 1195 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1196 |
} |
| 1197 |
uint8_t opad[72U]; |
| 1198 |
memset(opad, 0x5cU, 72U * sizeof (uint8_t)); |
| 1199 |
for (uint32_t i = 0U; i < 72U; i++) |
| 1200 |
{ |
| 1201 |
uint8_t xi = opad[i]; |
| 1202 |
uint8_t yi = key_block[i]; |
| 1203 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1204 |
} |
| 1205 |
uint64_t s[25U] = { 0U }; |
| 1206 |
uint8_t *dst1 = ipad; |
| 1207 |
if (data_len == 0U) |
| 1208 |
{ |
| 1209 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_512, s, ipad, 72U); |
| 1210 |
} |
| 1211 |
else |
| 1212 |
{ |
| 1213 |
uint32_t block_len = 72U; |
| 1214 |
uint32_t n_blocks0 = data_len / block_len; |
| 1215 |
uint32_t rem0 = data_len % block_len; |
| 1216 |
K___uint32_t_uint32_t scrut; |
| 1217 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1218 |
{ |
| 1219 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1220 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1225 |
} |
| 1226 |
uint32_t n_blocks = scrut.fst; |
| 1227 |
uint32_t rem_len = scrut.snd; |
| 1228 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1229 |
uint8_t *full_blocks = data; |
| 1230 |
uint8_t *rem = data + full_blocks_len; |
| 1231 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_512, s, ipad, 1U); |
| 1232 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_512, s, full_blocks, n_blocks); |
| 1233 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_512, s, rem, rem_len); |
| 1234 |
} |
| 1235 |
uint32_t remOut = 64U; |
| 1236 |
uint8_t hbuf0[256U] = { 0U }; |
| 1237 |
uint64_t ws0[32U] = { 0U }; |
| 1238 |
memcpy(ws0, s, 25U * sizeof (uint64_t)); |
| 1239 |
for (uint32_t i = 0U; i < 32U; i++) |
| 1240 |
{ |
| 1241 |
store64_le(hbuf0 + i * 8U, ws0[i]); |
| 1242 |
} |
| 1243 |
memcpy(dst1 + 64U - remOut, hbuf0, remOut * sizeof (uint8_t)); |
| 1244 |
uint8_t *hash1 = ipad; |
| 1245 |
Hacl_Hash_SHA3_init_(Spec_Hash_Definitions_SHA3_512, s); |
| 1246 |
uint32_t block_len = 72U; |
| 1247 |
uint32_t n_blocks0 = 64U / block_len; |
| 1248 |
uint32_t rem0 = 64U % block_len; |
| 1249 |
K___uint32_t_uint32_t scrut; |
| 1250 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1251 |
{ |
| 1252 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1253 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 64U - n_blocks_ * block_len }); |
| 1254 |
} |
| 1255 |
else |
| 1256 |
{ |
| 1257 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1258 |
} |
| 1259 |
uint32_t n_blocks = scrut.fst; |
| 1260 |
uint32_t rem_len = scrut.snd; |
| 1261 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1262 |
uint8_t *full_blocks = hash1; |
| 1263 |
uint8_t *rem = hash1 + full_blocks_len; |
| 1264 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_512, s, opad, 1U); |
| 1265 |
Hacl_Hash_SHA3_update_multi_sha3(Spec_Hash_Definitions_SHA3_512, s, full_blocks, n_blocks); |
| 1266 |
Hacl_Hash_SHA3_update_last_sha3(Spec_Hash_Definitions_SHA3_512, s, rem, rem_len); |
| 1267 |
uint32_t remOut0 = 64U; |
| 1268 |
uint8_t hbuf[256U] = { 0U }; |
| 1269 |
uint64_t ws[32U] = { 0U }; |
| 1270 |
memcpy(ws, s, 25U * sizeof (uint64_t)); |
| 1271 |
for (uint32_t i = 0U; i < 32U; i++) |
| 1272 |
{ |
| 1273 |
store64_le(hbuf + i * 8U, ws[i]); |
| 1274 |
} |
| 1275 |
memcpy(dst + 64U - remOut0, hbuf, remOut0 * sizeof (uint8_t)); |
| 1276 |
} |
| 1277 |
|
| 1278 |
/** |
| 1279 |
Write the HMAC-BLAKE2s MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 1280 |
|
| 1281 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 64 bytes. |
| 1282 |
`dst` must point to 32 bytes of memory. |
| 1283 |
*/ |
| 1284 |
void |
| 1285 |
Hacl_HMAC_compute_blake2s_32( |
| 1286 |
uint8_t *dst, |
| 1287 |
uint8_t *key, |
| 1288 |
uint32_t key_len, |
| 1289 |
uint8_t *data, |
| 1290 |
uint32_t data_len |
| 1291 |
) |
| 1292 |
{ |
| 1293 |
uint8_t key_block[64U]; |
| 1294 |
memset(key_block, 0U, 64U * sizeof (uint8_t)); |
| 1295 |
uint8_t *nkey = key_block; |
| 1296 |
uint32_t ite; |
| 1297 |
if (key_len <= 64U) |
| 1298 |
{ |
| 1299 |
ite = key_len; |
| 1300 |
} |
| 1301 |
else |
| 1302 |
{ |
| 1303 |
ite = 32U; |
| 1304 |
} |
| 1305 |
uint8_t *zeroes = key_block + ite; |
| 1306 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 1307 |
if (key_len <= 64U) |
| 1308 |
{ |
| 1309 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 1310 |
} |
| 1311 |
else |
| 1312 |
{ |
| 1313 |
Hacl_Hash_Blake2s_hash_with_key(nkey, 32U, key, key_len, NULL, 0U); |
| 1314 |
} |
| 1315 |
uint8_t ipad[64U]; |
| 1316 |
memset(ipad, 0x36U, 64U * sizeof (uint8_t)); |
| 1317 |
for (uint32_t i = 0U; i < 64U; i++) |
| 1318 |
{ |
| 1319 |
uint8_t xi = ipad[i]; |
| 1320 |
uint8_t yi = key_block[i]; |
| 1321 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1322 |
} |
| 1323 |
uint8_t opad[64U]; |
| 1324 |
memset(opad, 0x5cU, 64U * sizeof (uint8_t)); |
| 1325 |
for (uint32_t i = 0U; i < 64U; i++) |
| 1326 |
{ |
| 1327 |
uint8_t xi = opad[i]; |
| 1328 |
uint8_t yi = key_block[i]; |
| 1329 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1330 |
} |
| 1331 |
uint32_t s[16U] = { 0U }; |
| 1332 |
Hacl_Hash_Blake2s_init(s, 0U, 32U); |
| 1333 |
uint32_t *s0 = s; |
| 1334 |
uint8_t *dst1 = ipad; |
| 1335 |
if (data_len == 0U) |
| 1336 |
{ |
| 1337 |
uint32_t wv[16U] = { 0U }; |
| 1338 |
Hacl_Hash_Blake2s_update_last(64U, wv, s0, false, 0ULL, 64U, ipad); |
| 1339 |
} |
| 1340 |
else |
| 1341 |
{ |
| 1342 |
uint32_t block_len = 64U; |
| 1343 |
uint32_t n_blocks0 = data_len / block_len; |
| 1344 |
uint32_t rem0 = data_len % block_len; |
| 1345 |
K___uint32_t_uint32_t scrut; |
| 1346 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1347 |
{ |
| 1348 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1349 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 1350 |
} |
| 1351 |
else |
| 1352 |
{ |
| 1353 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1354 |
} |
| 1355 |
uint32_t n_blocks = scrut.fst; |
| 1356 |
uint32_t rem_len = scrut.snd; |
| 1357 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1358 |
uint8_t *full_blocks = data; |
| 1359 |
uint8_t *rem = data + full_blocks_len; |
| 1360 |
uint32_t wv[16U] = { 0U }; |
| 1361 |
Hacl_Hash_Blake2s_update_multi(64U, wv, s0, 0ULL, ipad, 1U); |
| 1362 |
uint32_t wv0[16U] = { 0U }; |
| 1363 |
Hacl_Hash_Blake2s_update_multi(n_blocks * 64U, |
| 1364 |
wv0, |
| 1365 |
s0, |
| 1366 |
(uint64_t)block_len, |
| 1367 |
full_blocks, |
| 1368 |
n_blocks); |
| 1369 |
uint32_t wv1[16U] = { 0U }; |
| 1370 |
Hacl_Hash_Blake2s_update_last(rem_len, |
| 1371 |
wv1, |
| 1372 |
s0, |
| 1373 |
false, |
| 1374 |
(uint64_t)64U + (uint64_t)full_blocks_len, |
| 1375 |
rem_len, |
| 1376 |
rem); |
| 1377 |
} |
| 1378 |
Hacl_Hash_Blake2s_finish(32U, dst1, s0); |
| 1379 |
uint8_t *hash1 = ipad; |
| 1380 |
Hacl_Hash_Blake2s_init(s0, 0U, 32U); |
| 1381 |
uint32_t block_len = 64U; |
| 1382 |
uint32_t n_blocks0 = 32U / block_len; |
| 1383 |
uint32_t rem0 = 32U % block_len; |
| 1384 |
K___uint32_t_uint32_t scrut; |
| 1385 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1386 |
{ |
| 1387 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1388 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 32U - n_blocks_ * block_len }); |
| 1389 |
} |
| 1390 |
else |
| 1391 |
{ |
| 1392 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1393 |
} |
| 1394 |
uint32_t n_blocks = scrut.fst; |
| 1395 |
uint32_t rem_len = scrut.snd; |
| 1396 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1397 |
uint8_t *full_blocks = hash1; |
| 1398 |
uint8_t *rem = hash1 + full_blocks_len; |
| 1399 |
uint32_t wv[16U] = { 0U }; |
| 1400 |
Hacl_Hash_Blake2s_update_multi(64U, wv, s0, 0ULL, opad, 1U); |
| 1401 |
uint32_t wv0[16U] = { 0U }; |
| 1402 |
Hacl_Hash_Blake2s_update_multi(n_blocks * 64U, |
| 1403 |
wv0, |
| 1404 |
s0, |
| 1405 |
(uint64_t)block_len, |
| 1406 |
full_blocks, |
| 1407 |
n_blocks); |
| 1408 |
uint32_t wv1[16U] = { 0U }; |
| 1409 |
Hacl_Hash_Blake2s_update_last(rem_len, |
| 1410 |
wv1, |
| 1411 |
s0, |
| 1412 |
false, |
| 1413 |
(uint64_t)64U + (uint64_t)full_blocks_len, |
| 1414 |
rem_len, |
| 1415 |
rem); |
| 1416 |
Hacl_Hash_Blake2s_finish(32U, dst, s0); |
| 1417 |
} |
| 1418 |
|
| 1419 |
/** |
| 1420 |
Write the HMAC-BLAKE2b MAC of a message (`data`) by using a key (`key`) into `dst`. |
| 1421 |
|
| 1422 |
The key can be any length and will be hashed if it is longer and padded if it is shorter than 128 bytes. |
| 1423 |
`dst` must point to 64 bytes of memory. |
| 1424 |
*/ |
| 1425 |
void |
| 1426 |
Hacl_HMAC_compute_blake2b_32( |
| 1427 |
uint8_t *dst, |
| 1428 |
uint8_t *key, |
| 1429 |
uint32_t key_len, |
| 1430 |
uint8_t *data, |
| 1431 |
uint32_t data_len |
| 1432 |
) |
| 1433 |
{ |
| 1434 |
uint8_t key_block[128U]; |
| 1435 |
memset(key_block, 0U, 128U * sizeof (uint8_t)); |
| 1436 |
uint8_t *nkey = key_block; |
| 1437 |
uint32_t ite; |
| 1438 |
if (key_len <= 128U) |
| 1439 |
{ |
| 1440 |
ite = key_len; |
| 1441 |
} |
| 1442 |
else |
| 1443 |
{ |
| 1444 |
ite = 64U; |
| 1445 |
} |
| 1446 |
uint8_t *zeroes = key_block + ite; |
| 1447 |
KRML_MAYBE_UNUSED_VAR(zeroes); |
| 1448 |
if (key_len <= 128U) |
| 1449 |
{ |
| 1450 |
memcpy(nkey, key, key_len * sizeof (uint8_t)); |
| 1451 |
} |
| 1452 |
else |
| 1453 |
{ |
| 1454 |
Hacl_Hash_Blake2b_hash_with_key(nkey, 64U, key, key_len, NULL, 0U); |
| 1455 |
} |
| 1456 |
uint8_t ipad[128U]; |
| 1457 |
memset(ipad, 0x36U, 128U * sizeof (uint8_t)); |
| 1458 |
for (uint32_t i = 0U; i < 128U; i++) |
| 1459 |
{ |
| 1460 |
uint8_t xi = ipad[i]; |
| 1461 |
uint8_t yi = key_block[i]; |
| 1462 |
ipad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1463 |
} |
| 1464 |
uint8_t opad[128U]; |
| 1465 |
memset(opad, 0x5cU, 128U * sizeof (uint8_t)); |
| 1466 |
for (uint32_t i = 0U; i < 128U; i++) |
| 1467 |
{ |
| 1468 |
uint8_t xi = opad[i]; |
| 1469 |
uint8_t yi = key_block[i]; |
| 1470 |
opad[i] = (uint32_t)xi ^ (uint32_t)yi; |
| 1471 |
} |
| 1472 |
uint64_t s[16U] = { 0U }; |
| 1473 |
Hacl_Hash_Blake2b_init(s, 0U, 64U); |
| 1474 |
uint64_t *s0 = s; |
| 1475 |
uint8_t *dst1 = ipad; |
| 1476 |
if (data_len == 0U) |
| 1477 |
{ |
| 1478 |
uint64_t wv[16U] = { 0U }; |
| 1479 |
Hacl_Hash_Blake2b_update_last(128U, |
| 1480 |
wv, |
| 1481 |
s0, |
| 1482 |
false, |
| 1483 |
FStar_UInt128_uint64_to_uint128(0ULL), |
| 1484 |
128U, |
| 1485 |
ipad); |
| 1486 |
} |
| 1487 |
else |
| 1488 |
{ |
| 1489 |
uint32_t block_len = 128U; |
| 1490 |
uint32_t n_blocks0 = data_len / block_len; |
| 1491 |
uint32_t rem0 = data_len % block_len; |
| 1492 |
K___uint32_t_uint32_t scrut; |
| 1493 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1494 |
{ |
| 1495 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1496 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = data_len - n_blocks_ * block_len }); |
| 1497 |
} |
| 1498 |
else |
| 1499 |
{ |
| 1500 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1501 |
} |
| 1502 |
uint32_t n_blocks = scrut.fst; |
| 1503 |
uint32_t rem_len = scrut.snd; |
| 1504 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1505 |
uint8_t *full_blocks = data; |
| 1506 |
uint8_t *rem = data + full_blocks_len; |
| 1507 |
uint64_t wv[16U] = { 0U }; |
| 1508 |
Hacl_Hash_Blake2b_update_multi(128U, wv, s0, FStar_UInt128_uint64_to_uint128(0ULL), ipad, 1U); |
| 1509 |
uint64_t wv0[16U] = { 0U }; |
| 1510 |
Hacl_Hash_Blake2b_update_multi(n_blocks * 128U, |
| 1511 |
wv0, |
| 1512 |
s0, |
| 1513 |
FStar_UInt128_uint64_to_uint128((uint64_t)block_len), |
| 1514 |
full_blocks, |
| 1515 |
n_blocks); |
| 1516 |
uint64_t wv1[16U] = { 0U }; |
| 1517 |
Hacl_Hash_Blake2b_update_last(rem_len, |
| 1518 |
wv1, |
| 1519 |
s0, |
| 1520 |
false, |
| 1521 |
FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 1522 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 1523 |
rem_len, |
| 1524 |
rem); |
| 1525 |
} |
| 1526 |
Hacl_Hash_Blake2b_finish(64U, dst1, s0); |
| 1527 |
uint8_t *hash1 = ipad; |
| 1528 |
Hacl_Hash_Blake2b_init(s0, 0U, 64U); |
| 1529 |
uint32_t block_len = 128U; |
| 1530 |
uint32_t n_blocks0 = 64U / block_len; |
| 1531 |
uint32_t rem0 = 64U % block_len; |
| 1532 |
K___uint32_t_uint32_t scrut; |
| 1533 |
if (n_blocks0 > 0U && rem0 == 0U) |
| 1534 |
{ |
| 1535 |
uint32_t n_blocks_ = n_blocks0 - 1U; |
| 1536 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks_, .snd = 64U - n_blocks_ * block_len }); |
| 1537 |
} |
| 1538 |
else |
| 1539 |
{ |
| 1540 |
scrut = ((K___uint32_t_uint32_t){ .fst = n_blocks0, .snd = rem0 }); |
| 1541 |
} |
| 1542 |
uint32_t n_blocks = scrut.fst; |
| 1543 |
uint32_t rem_len = scrut.snd; |
| 1544 |
uint32_t full_blocks_len = n_blocks * block_len; |
| 1545 |
uint8_t *full_blocks = hash1; |
| 1546 |
uint8_t *rem = hash1 + full_blocks_len; |
| 1547 |
uint64_t wv[16U] = { 0U }; |
| 1548 |
Hacl_Hash_Blake2b_update_multi(128U, wv, s0, FStar_UInt128_uint64_to_uint128(0ULL), opad, 1U); |
| 1549 |
uint64_t wv0[16U] = { 0U }; |
| 1550 |
Hacl_Hash_Blake2b_update_multi(n_blocks * 128U, |
| 1551 |
wv0, |
| 1552 |
s0, |
| 1553 |
FStar_UInt128_uint64_to_uint128((uint64_t)block_len), |
| 1554 |
full_blocks, |
| 1555 |
n_blocks); |
| 1556 |
uint64_t wv1[16U] = { 0U }; |
| 1557 |
Hacl_Hash_Blake2b_update_last(rem_len, |
| 1558 |
wv1, |
| 1559 |
s0, |
| 1560 |
false, |
| 1561 |
FStar_UInt128_add(FStar_UInt128_uint64_to_uint128((uint64_t)128U), |
| 1562 |
FStar_UInt128_uint64_to_uint128((uint64_t)full_blocks_len)), |
| 1563 |
rem_len, |
| 1564 |
rem); |
| 1565 |
Hacl_Hash_Blake2b_finish(64U, dst, s0); |
| 1566 |
} |
| 1567 |
|