Hacl_Hash_SHA3.c 21 KB

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