Hacl_Hash_SHA2.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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_SHA2.h"
  25. void Hacl_Hash_SHA2_sha256_init(uint32_t *hash)
  26. {
  27. KRML_MAYBE_FOR8(i,
  28. 0U,
  29. 8U,
  30. 1U,
  31. uint32_t *os = hash;
  32. uint32_t x = Hacl_Hash_SHA2_h256[i];
  33. os[i] = x;);
  34. }
  35. static inline void sha256_update(uint8_t *b, uint32_t *hash)
  36. {
  37. uint32_t hash_old[8U] = { 0U };
  38. uint32_t ws[16U] = { 0U };
  39. memcpy(hash_old, hash, 8U * sizeof (uint32_t));
  40. uint8_t *b10 = b;
  41. uint32_t u = load32_be(b10);
  42. ws[0U] = u;
  43. uint32_t u0 = load32_be(b10 + 4U);
  44. ws[1U] = u0;
  45. uint32_t u1 = load32_be(b10 + 8U);
  46. ws[2U] = u1;
  47. uint32_t u2 = load32_be(b10 + 12U);
  48. ws[3U] = u2;
  49. uint32_t u3 = load32_be(b10 + 16U);
  50. ws[4U] = u3;
  51. uint32_t u4 = load32_be(b10 + 20U);
  52. ws[5U] = u4;
  53. uint32_t u5 = load32_be(b10 + 24U);
  54. ws[6U] = u5;
  55. uint32_t u6 = load32_be(b10 + 28U);
  56. ws[7U] = u6;
  57. uint32_t u7 = load32_be(b10 + 32U);
  58. ws[8U] = u7;
  59. uint32_t u8 = load32_be(b10 + 36U);
  60. ws[9U] = u8;
  61. uint32_t u9 = load32_be(b10 + 40U);
  62. ws[10U] = u9;
  63. uint32_t u10 = load32_be(b10 + 44U);
  64. ws[11U] = u10;
  65. uint32_t u11 = load32_be(b10 + 48U);
  66. ws[12U] = u11;
  67. uint32_t u12 = load32_be(b10 + 52U);
  68. ws[13U] = u12;
  69. uint32_t u13 = load32_be(b10 + 56U);
  70. ws[14U] = u13;
  71. uint32_t u14 = load32_be(b10 + 60U);
  72. ws[15U] = u14;
  73. KRML_MAYBE_FOR4(i0,
  74. 0U,
  75. 4U,
  76. 1U,
  77. KRML_MAYBE_FOR16(i,
  78. 0U,
  79. 16U,
  80. 1U,
  81. uint32_t k_t = Hacl_Hash_SHA2_k224_256[16U * i0 + i];
  82. uint32_t ws_t = ws[i];
  83. uint32_t a0 = hash[0U];
  84. uint32_t b0 = hash[1U];
  85. uint32_t c0 = hash[2U];
  86. uint32_t d0 = hash[3U];
  87. uint32_t e0 = hash[4U];
  88. uint32_t f0 = hash[5U];
  89. uint32_t g0 = hash[6U];
  90. uint32_t h02 = hash[7U];
  91. uint32_t k_e_t = k_t;
  92. uint32_t
  93. t1 =
  94. h02
  95. + ((e0 << 26U | e0 >> 6U) ^ ((e0 << 21U | e0 >> 11U) ^ (e0 << 7U | e0 >> 25U)))
  96. + ((e0 & f0) ^ (~e0 & g0))
  97. + k_e_t
  98. + ws_t;
  99. uint32_t
  100. t2 =
  101. ((a0 << 30U | a0 >> 2U) ^ ((a0 << 19U | a0 >> 13U) ^ (a0 << 10U | a0 >> 22U)))
  102. + ((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0)));
  103. uint32_t a1 = t1 + t2;
  104. uint32_t b1 = a0;
  105. uint32_t c1 = b0;
  106. uint32_t d1 = c0;
  107. uint32_t e1 = d0 + t1;
  108. uint32_t f1 = e0;
  109. uint32_t g1 = f0;
  110. uint32_t h12 = g0;
  111. hash[0U] = a1;
  112. hash[1U] = b1;
  113. hash[2U] = c1;
  114. hash[3U] = d1;
  115. hash[4U] = e1;
  116. hash[5U] = f1;
  117. hash[6U] = g1;
  118. hash[7U] = h12;);
  119. if (i0 < 3U)
  120. {
  121. KRML_MAYBE_FOR16(i,
  122. 0U,
  123. 16U,
  124. 1U,
  125. uint32_t t16 = ws[i];
  126. uint32_t t15 = ws[(i + 1U) % 16U];
  127. uint32_t t7 = ws[(i + 9U) % 16U];
  128. uint32_t t2 = ws[(i + 14U) % 16U];
  129. uint32_t s1 = (t2 << 15U | t2 >> 17U) ^ ((t2 << 13U | t2 >> 19U) ^ t2 >> 10U);
  130. uint32_t s0 = (t15 << 25U | t15 >> 7U) ^ ((t15 << 14U | t15 >> 18U) ^ t15 >> 3U);
  131. ws[i] = s1 + t7 + s0 + t16;);
  132. });
  133. KRML_MAYBE_FOR8(i,
  134. 0U,
  135. 8U,
  136. 1U,
  137. uint32_t *os = hash;
  138. uint32_t x = hash[i] + hash_old[i];
  139. os[i] = x;);
  140. }
  141. void Hacl_Hash_SHA2_sha256_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st)
  142. {
  143. uint32_t blocks = len / 64U;
  144. for (uint32_t i = 0U; i < blocks; i++)
  145. {
  146. uint8_t *b0 = b;
  147. uint8_t *mb = b0 + i * 64U;
  148. sha256_update(mb, st);
  149. }
  150. }
  151. void
  152. Hacl_Hash_SHA2_sha256_update_last(uint64_t totlen, uint32_t len, uint8_t *b, uint32_t *hash)
  153. {
  154. uint32_t blocks;
  155. if (len + 8U + 1U <= 64U)
  156. {
  157. blocks = 1U;
  158. }
  159. else
  160. {
  161. blocks = 2U;
  162. }
  163. uint32_t fin = blocks * 64U;
  164. uint8_t last[128U] = { 0U };
  165. uint8_t totlen_buf[8U] = { 0U };
  166. uint64_t total_len_bits = totlen << 3U;
  167. store64_be(totlen_buf, total_len_bits);
  168. uint8_t *b0 = b;
  169. memcpy(last, b0, len * sizeof (uint8_t));
  170. last[len] = 0x80U;
  171. memcpy(last + fin - 8U, totlen_buf, 8U * sizeof (uint8_t));
  172. uint8_t *last00 = last;
  173. uint8_t *last10 = last + 64U;
  174. uint8_t *l0 = last00;
  175. uint8_t *l1 = last10;
  176. uint8_t *lb0 = l0;
  177. uint8_t *lb1 = l1;
  178. uint8_t *last0 = lb0;
  179. uint8_t *last1 = lb1;
  180. sha256_update(last0, hash);
  181. if (blocks > 1U)
  182. {
  183. sha256_update(last1, hash);
  184. return;
  185. }
  186. }
  187. void Hacl_Hash_SHA2_sha256_finish(uint32_t *st, uint8_t *h)
  188. {
  189. uint8_t hbuf[32U] = { 0U };
  190. KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store32_be(hbuf + i * 4U, st[i]););
  191. memcpy(h, hbuf, 32U * sizeof (uint8_t));
  192. }
  193. void Hacl_Hash_SHA2_sha224_init(uint32_t *hash)
  194. {
  195. KRML_MAYBE_FOR8(i,
  196. 0U,
  197. 8U,
  198. 1U,
  199. uint32_t *os = hash;
  200. uint32_t x = Hacl_Hash_SHA2_h224[i];
  201. os[i] = x;);
  202. }
  203. static inline void sha224_update_nblocks(uint32_t len, uint8_t *b, uint32_t *st)
  204. {
  205. Hacl_Hash_SHA2_sha256_update_nblocks(len, b, st);
  206. }
  207. void Hacl_Hash_SHA2_sha224_update_last(uint64_t totlen, uint32_t len, uint8_t *b, uint32_t *st)
  208. {
  209. Hacl_Hash_SHA2_sha256_update_last(totlen, len, b, st);
  210. }
  211. void Hacl_Hash_SHA2_sha224_finish(uint32_t *st, uint8_t *h)
  212. {
  213. uint8_t hbuf[32U] = { 0U };
  214. KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store32_be(hbuf + i * 4U, st[i]););
  215. memcpy(h, hbuf, 28U * sizeof (uint8_t));
  216. }
  217. void Hacl_Hash_SHA2_sha512_init(uint64_t *hash)
  218. {
  219. KRML_MAYBE_FOR8(i,
  220. 0U,
  221. 8U,
  222. 1U,
  223. uint64_t *os = hash;
  224. uint64_t x = Hacl_Hash_SHA2_h512[i];
  225. os[i] = x;);
  226. }
  227. static inline void sha512_update(uint8_t *b, uint64_t *hash)
  228. {
  229. uint64_t hash_old[8U] = { 0U };
  230. uint64_t ws[16U] = { 0U };
  231. memcpy(hash_old, hash, 8U * sizeof (uint64_t));
  232. uint8_t *b10 = b;
  233. uint64_t u = load64_be(b10);
  234. ws[0U] = u;
  235. uint64_t u0 = load64_be(b10 + 8U);
  236. ws[1U] = u0;
  237. uint64_t u1 = load64_be(b10 + 16U);
  238. ws[2U] = u1;
  239. uint64_t u2 = load64_be(b10 + 24U);
  240. ws[3U] = u2;
  241. uint64_t u3 = load64_be(b10 + 32U);
  242. ws[4U] = u3;
  243. uint64_t u4 = load64_be(b10 + 40U);
  244. ws[5U] = u4;
  245. uint64_t u5 = load64_be(b10 + 48U);
  246. ws[6U] = u5;
  247. uint64_t u6 = load64_be(b10 + 56U);
  248. ws[7U] = u6;
  249. uint64_t u7 = load64_be(b10 + 64U);
  250. ws[8U] = u7;
  251. uint64_t u8 = load64_be(b10 + 72U);
  252. ws[9U] = u8;
  253. uint64_t u9 = load64_be(b10 + 80U);
  254. ws[10U] = u9;
  255. uint64_t u10 = load64_be(b10 + 88U);
  256. ws[11U] = u10;
  257. uint64_t u11 = load64_be(b10 + 96U);
  258. ws[12U] = u11;
  259. uint64_t u12 = load64_be(b10 + 104U);
  260. ws[13U] = u12;
  261. uint64_t u13 = load64_be(b10 + 112U);
  262. ws[14U] = u13;
  263. uint64_t u14 = load64_be(b10 + 120U);
  264. ws[15U] = u14;
  265. KRML_MAYBE_FOR5(i0,
  266. 0U,
  267. 5U,
  268. 1U,
  269. KRML_MAYBE_FOR16(i,
  270. 0U,
  271. 16U,
  272. 1U,
  273. uint64_t k_t = Hacl_Hash_SHA2_k384_512[16U * i0 + i];
  274. uint64_t ws_t = ws[i];
  275. uint64_t a0 = hash[0U];
  276. uint64_t b0 = hash[1U];
  277. uint64_t c0 = hash[2U];
  278. uint64_t d0 = hash[3U];
  279. uint64_t e0 = hash[4U];
  280. uint64_t f0 = hash[5U];
  281. uint64_t g0 = hash[6U];
  282. uint64_t h02 = hash[7U];
  283. uint64_t k_e_t = k_t;
  284. uint64_t
  285. t1 =
  286. h02
  287. + ((e0 << 50U | e0 >> 14U) ^ ((e0 << 46U | e0 >> 18U) ^ (e0 << 23U | e0 >> 41U)))
  288. + ((e0 & f0) ^ (~e0 & g0))
  289. + k_e_t
  290. + ws_t;
  291. uint64_t
  292. t2 =
  293. ((a0 << 36U | a0 >> 28U) ^ ((a0 << 30U | a0 >> 34U) ^ (a0 << 25U | a0 >> 39U)))
  294. + ((a0 & b0) ^ ((a0 & c0) ^ (b0 & c0)));
  295. uint64_t a1 = t1 + t2;
  296. uint64_t b1 = a0;
  297. uint64_t c1 = b0;
  298. uint64_t d1 = c0;
  299. uint64_t e1 = d0 + t1;
  300. uint64_t f1 = e0;
  301. uint64_t g1 = f0;
  302. uint64_t h12 = g0;
  303. hash[0U] = a1;
  304. hash[1U] = b1;
  305. hash[2U] = c1;
  306. hash[3U] = d1;
  307. hash[4U] = e1;
  308. hash[5U] = f1;
  309. hash[6U] = g1;
  310. hash[7U] = h12;);
  311. if (i0 < 4U)
  312. {
  313. KRML_MAYBE_FOR16(i,
  314. 0U,
  315. 16U,
  316. 1U,
  317. uint64_t t16 = ws[i];
  318. uint64_t t15 = ws[(i + 1U) % 16U];
  319. uint64_t t7 = ws[(i + 9U) % 16U];
  320. uint64_t t2 = ws[(i + 14U) % 16U];
  321. uint64_t s1 = (t2 << 45U | t2 >> 19U) ^ ((t2 << 3U | t2 >> 61U) ^ t2 >> 6U);
  322. uint64_t s0 = (t15 << 63U | t15 >> 1U) ^ ((t15 << 56U | t15 >> 8U) ^ t15 >> 7U);
  323. ws[i] = s1 + t7 + s0 + t16;);
  324. });
  325. KRML_MAYBE_FOR8(i,
  326. 0U,
  327. 8U,
  328. 1U,
  329. uint64_t *os = hash;
  330. uint64_t x = hash[i] + hash_old[i];
  331. os[i] = x;);
  332. }
  333. void Hacl_Hash_SHA2_sha512_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st)
  334. {
  335. uint32_t blocks = len / 128U;
  336. for (uint32_t i = 0U; i < blocks; i++)
  337. {
  338. uint8_t *b0 = b;
  339. uint8_t *mb = b0 + i * 128U;
  340. sha512_update(mb, st);
  341. }
  342. }
  343. void
  344. Hacl_Hash_SHA2_sha512_update_last(
  345. FStar_UInt128_uint128 totlen,
  346. uint32_t len,
  347. uint8_t *b,
  348. uint64_t *hash
  349. )
  350. {
  351. uint32_t blocks;
  352. if (len + 16U + 1U <= 128U)
  353. {
  354. blocks = 1U;
  355. }
  356. else
  357. {
  358. blocks = 2U;
  359. }
  360. uint32_t fin = blocks * 128U;
  361. uint8_t last[256U] = { 0U };
  362. uint8_t totlen_buf[16U] = { 0U };
  363. FStar_UInt128_uint128 total_len_bits = FStar_UInt128_shift_left(totlen, 3U);
  364. store128_be(totlen_buf, total_len_bits);
  365. uint8_t *b0 = b;
  366. memcpy(last, b0, len * sizeof (uint8_t));
  367. last[len] = 0x80U;
  368. memcpy(last + fin - 16U, totlen_buf, 16U * sizeof (uint8_t));
  369. uint8_t *last00 = last;
  370. uint8_t *last10 = last + 128U;
  371. uint8_t *l0 = last00;
  372. uint8_t *l1 = last10;
  373. uint8_t *lb0 = l0;
  374. uint8_t *lb1 = l1;
  375. uint8_t *last0 = lb0;
  376. uint8_t *last1 = lb1;
  377. sha512_update(last0, hash);
  378. if (blocks > 1U)
  379. {
  380. sha512_update(last1, hash);
  381. return;
  382. }
  383. }
  384. void Hacl_Hash_SHA2_sha512_finish(uint64_t *st, uint8_t *h)
  385. {
  386. uint8_t hbuf[64U] = { 0U };
  387. KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store64_be(hbuf + i * 8U, st[i]););
  388. memcpy(h, hbuf, 64U * sizeof (uint8_t));
  389. }
  390. void Hacl_Hash_SHA2_sha384_init(uint64_t *hash)
  391. {
  392. KRML_MAYBE_FOR8(i,
  393. 0U,
  394. 8U,
  395. 1U,
  396. uint64_t *os = hash;
  397. uint64_t x = Hacl_Hash_SHA2_h384[i];
  398. os[i] = x;);
  399. }
  400. void Hacl_Hash_SHA2_sha384_update_nblocks(uint32_t len, uint8_t *b, uint64_t *st)
  401. {
  402. Hacl_Hash_SHA2_sha512_update_nblocks(len, b, st);
  403. }
  404. void
  405. Hacl_Hash_SHA2_sha384_update_last(
  406. FStar_UInt128_uint128 totlen,
  407. uint32_t len,
  408. uint8_t *b,
  409. uint64_t *st
  410. )
  411. {
  412. Hacl_Hash_SHA2_sha512_update_last(totlen, len, b, st);
  413. }
  414. void Hacl_Hash_SHA2_sha384_finish(uint64_t *st, uint8_t *h)
  415. {
  416. uint8_t hbuf[64U] = { 0U };
  417. KRML_MAYBE_FOR8(i, 0U, 8U, 1U, store64_be(hbuf + i * 8U, st[i]););
  418. memcpy(h, hbuf, 48U * sizeof (uint8_t));
  419. }
  420. /**
  421. Allocate initial state for the SHA2_256 hash. The state is to be freed by
  422. calling `free_256`.
  423. */
  424. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_256(void)
  425. {
  426. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
  427. uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t));
  428. Hacl_Streaming_MD_state_32
  429. s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  430. Hacl_Streaming_MD_state_32
  431. *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
  432. p[0U] = s;
  433. Hacl_Hash_SHA2_sha256_init(block_state);
  434. return p;
  435. }
  436. /**
  437. Copies the state passed as argument into a newly allocated state (deep copy).
  438. The state is to be freed by calling `free_256`. Cloning the state this way is
  439. useful, for instance, if your control-flow diverges and you need to feed
  440. more (different) data into the hash in each branch.
  441. */
  442. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_copy_256(Hacl_Streaming_MD_state_32 *state)
  443. {
  444. Hacl_Streaming_MD_state_32 scrut = *state;
  445. uint32_t *block_state0 = scrut.block_state;
  446. uint8_t *buf0 = scrut.buf;
  447. uint64_t total_len0 = scrut.total_len;
  448. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
  449. memcpy(buf, buf0, 64U * sizeof (uint8_t));
  450. uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t));
  451. memcpy(block_state, block_state0, 8U * sizeof (uint32_t));
  452. Hacl_Streaming_MD_state_32
  453. s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
  454. Hacl_Streaming_MD_state_32
  455. *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
  456. p[0U] = s;
  457. return p;
  458. }
  459. /**
  460. Reset an existing state to the initial hash state with empty data.
  461. */
  462. void Hacl_Hash_SHA2_reset_256(Hacl_Streaming_MD_state_32 *state)
  463. {
  464. Hacl_Streaming_MD_state_32 scrut = *state;
  465. uint8_t *buf = scrut.buf;
  466. uint32_t *block_state = scrut.block_state;
  467. Hacl_Hash_SHA2_sha256_init(block_state);
  468. Hacl_Streaming_MD_state_32
  469. tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  470. state[0U] = tmp;
  471. }
  472. static inline Hacl_Streaming_Types_error_code
  473. update_224_256(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len)
  474. {
  475. Hacl_Streaming_MD_state_32 s = *state;
  476. uint64_t total_len = s.total_len;
  477. if ((uint64_t)chunk_len > 2305843009213693951ULL - total_len)
  478. {
  479. return Hacl_Streaming_Types_MaximumLengthExceeded;
  480. }
  481. uint32_t sz;
  482. if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
  483. {
  484. sz = 64U;
  485. }
  486. else
  487. {
  488. sz = (uint32_t)(total_len % (uint64_t)64U);
  489. }
  490. if (chunk_len <= 64U - sz)
  491. {
  492. Hacl_Streaming_MD_state_32 s1 = *state;
  493. uint32_t *block_state1 = s1.block_state;
  494. uint8_t *buf = s1.buf;
  495. uint64_t total_len1 = s1.total_len;
  496. uint32_t sz1;
  497. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  498. {
  499. sz1 = 64U;
  500. }
  501. else
  502. {
  503. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  504. }
  505. uint8_t *buf2 = buf + sz1;
  506. memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
  507. uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
  508. *state
  509. =
  510. (
  511. (Hacl_Streaming_MD_state_32){
  512. .block_state = block_state1,
  513. .buf = buf,
  514. .total_len = total_len2
  515. }
  516. );
  517. }
  518. else if (sz == 0U)
  519. {
  520. Hacl_Streaming_MD_state_32 s1 = *state;
  521. uint32_t *block_state1 = s1.block_state;
  522. uint8_t *buf = s1.buf;
  523. uint64_t total_len1 = s1.total_len;
  524. uint32_t sz1;
  525. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  526. {
  527. sz1 = 64U;
  528. }
  529. else
  530. {
  531. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  532. }
  533. if (!(sz1 == 0U))
  534. {
  535. Hacl_Hash_SHA2_sha256_update_nblocks(64U, buf, block_state1);
  536. }
  537. uint32_t ite;
  538. if ((uint64_t)chunk_len % (uint64_t)64U == 0ULL && (uint64_t)chunk_len > 0ULL)
  539. {
  540. ite = 64U;
  541. }
  542. else
  543. {
  544. ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)64U);
  545. }
  546. uint32_t n_blocks = (chunk_len - ite) / 64U;
  547. uint32_t data1_len = n_blocks * 64U;
  548. uint32_t data2_len = chunk_len - data1_len;
  549. uint8_t *data1 = chunk;
  550. uint8_t *data2 = chunk + data1_len;
  551. Hacl_Hash_SHA2_sha256_update_nblocks(data1_len / 64U * 64U, data1, block_state1);
  552. uint8_t *dst = buf;
  553. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  554. *state
  555. =
  556. (
  557. (Hacl_Streaming_MD_state_32){
  558. .block_state = block_state1,
  559. .buf = buf,
  560. .total_len = total_len1 + (uint64_t)chunk_len
  561. }
  562. );
  563. }
  564. else
  565. {
  566. uint32_t diff = 64U - sz;
  567. uint8_t *chunk1 = chunk;
  568. uint8_t *chunk2 = chunk + diff;
  569. Hacl_Streaming_MD_state_32 s1 = *state;
  570. uint32_t *block_state10 = s1.block_state;
  571. uint8_t *buf0 = s1.buf;
  572. uint64_t total_len10 = s1.total_len;
  573. uint32_t sz10;
  574. if (total_len10 % (uint64_t)64U == 0ULL && total_len10 > 0ULL)
  575. {
  576. sz10 = 64U;
  577. }
  578. else
  579. {
  580. sz10 = (uint32_t)(total_len10 % (uint64_t)64U);
  581. }
  582. uint8_t *buf2 = buf0 + sz10;
  583. memcpy(buf2, chunk1, diff * sizeof (uint8_t));
  584. uint64_t total_len2 = total_len10 + (uint64_t)diff;
  585. *state
  586. =
  587. (
  588. (Hacl_Streaming_MD_state_32){
  589. .block_state = block_state10,
  590. .buf = buf0,
  591. .total_len = total_len2
  592. }
  593. );
  594. Hacl_Streaming_MD_state_32 s10 = *state;
  595. uint32_t *block_state1 = s10.block_state;
  596. uint8_t *buf = s10.buf;
  597. uint64_t total_len1 = s10.total_len;
  598. uint32_t sz1;
  599. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  600. {
  601. sz1 = 64U;
  602. }
  603. else
  604. {
  605. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  606. }
  607. if (!(sz1 == 0U))
  608. {
  609. Hacl_Hash_SHA2_sha256_update_nblocks(64U, buf, block_state1);
  610. }
  611. uint32_t ite;
  612. if
  613. ((uint64_t)(chunk_len - diff) % (uint64_t)64U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
  614. {
  615. ite = 64U;
  616. }
  617. else
  618. {
  619. ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)64U);
  620. }
  621. uint32_t n_blocks = (chunk_len - diff - ite) / 64U;
  622. uint32_t data1_len = n_blocks * 64U;
  623. uint32_t data2_len = chunk_len - diff - data1_len;
  624. uint8_t *data1 = chunk2;
  625. uint8_t *data2 = chunk2 + data1_len;
  626. Hacl_Hash_SHA2_sha256_update_nblocks(data1_len / 64U * 64U, data1, block_state1);
  627. uint8_t *dst = buf;
  628. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  629. *state
  630. =
  631. (
  632. (Hacl_Streaming_MD_state_32){
  633. .block_state = block_state1,
  634. .buf = buf,
  635. .total_len = total_len1 + (uint64_t)(chunk_len - diff)
  636. }
  637. );
  638. }
  639. return Hacl_Streaming_Types_Success;
  640. }
  641. /**
  642. Feed an arbitrary amount of data into the hash. This function returns 0 for
  643. success, or 1 if the combined length of all of the data passed to `update_256`
  644. (since the last call to `reset_256`) exceeds 2^61-1 bytes.
  645. This function is identical to the update function for SHA2_224.
  646. */
  647. Hacl_Streaming_Types_error_code
  648. Hacl_Hash_SHA2_update_256(
  649. Hacl_Streaming_MD_state_32 *state,
  650. uint8_t *input,
  651. uint32_t input_len
  652. )
  653. {
  654. return update_224_256(state, input, input_len);
  655. }
  656. /**
  657. Write the resulting hash into `output`, an array of 32 bytes. The state remains
  658. valid after a call to `digest_256`, meaning the user may feed more data into
  659. the hash via `update_256`. (The digest_256 function operates on an internal copy of
  660. the state and therefore does not invalidate the client-held state `p`.)
  661. */
  662. void Hacl_Hash_SHA2_digest_256(Hacl_Streaming_MD_state_32 *state, uint8_t *output)
  663. {
  664. Hacl_Streaming_MD_state_32 scrut = *state;
  665. uint32_t *block_state = scrut.block_state;
  666. uint8_t *buf_ = scrut.buf;
  667. uint64_t total_len = scrut.total_len;
  668. uint32_t r;
  669. if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
  670. {
  671. r = 64U;
  672. }
  673. else
  674. {
  675. r = (uint32_t)(total_len % (uint64_t)64U);
  676. }
  677. uint8_t *buf_1 = buf_;
  678. uint32_t tmp_block_state[8U] = { 0U };
  679. memcpy(tmp_block_state, block_state, 8U * sizeof (uint32_t));
  680. uint32_t ite;
  681. if (r % 64U == 0U && r > 0U)
  682. {
  683. ite = 64U;
  684. }
  685. else
  686. {
  687. ite = r % 64U;
  688. }
  689. uint8_t *buf_last = buf_1 + r - ite;
  690. uint8_t *buf_multi = buf_1;
  691. Hacl_Hash_SHA2_sha256_update_nblocks(0U, buf_multi, tmp_block_state);
  692. uint64_t prev_len_last = total_len - (uint64_t)r;
  693. Hacl_Hash_SHA2_sha256_update_last(prev_len_last + (uint64_t)r, r, buf_last, tmp_block_state);
  694. Hacl_Hash_SHA2_sha256_finish(tmp_block_state, output);
  695. }
  696. /**
  697. Free a state allocated with `malloc_256`.
  698. This function is identical to the free function for SHA2_224.
  699. */
  700. void Hacl_Hash_SHA2_free_256(Hacl_Streaming_MD_state_32 *state)
  701. {
  702. Hacl_Streaming_MD_state_32 scrut = *state;
  703. uint8_t *buf = scrut.buf;
  704. uint32_t *block_state = scrut.block_state;
  705. KRML_HOST_FREE(block_state);
  706. KRML_HOST_FREE(buf);
  707. KRML_HOST_FREE(state);
  708. }
  709. /**
  710. Hash `input`, of len `input_len`, into `output`, an array of 32 bytes.
  711. */
  712. void Hacl_Hash_SHA2_hash_256(uint8_t *output, uint8_t *input, uint32_t input_len)
  713. {
  714. uint8_t *ib = input;
  715. uint8_t *rb = output;
  716. uint32_t st[8U] = { 0U };
  717. Hacl_Hash_SHA2_sha256_init(st);
  718. uint32_t rem = input_len % 64U;
  719. uint64_t len_ = (uint64_t)input_len;
  720. Hacl_Hash_SHA2_sha256_update_nblocks(input_len, ib, st);
  721. uint32_t rem1 = input_len % 64U;
  722. uint8_t *b0 = ib;
  723. uint8_t *lb = b0 + input_len - rem1;
  724. Hacl_Hash_SHA2_sha256_update_last(len_, rem, lb, st);
  725. Hacl_Hash_SHA2_sha256_finish(st, rb);
  726. }
  727. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_224(void)
  728. {
  729. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
  730. uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(8U, sizeof (uint32_t));
  731. Hacl_Streaming_MD_state_32
  732. s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  733. Hacl_Streaming_MD_state_32
  734. *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
  735. p[0U] = s;
  736. Hacl_Hash_SHA2_sha224_init(block_state);
  737. return p;
  738. }
  739. void Hacl_Hash_SHA2_reset_224(Hacl_Streaming_MD_state_32 *state)
  740. {
  741. Hacl_Streaming_MD_state_32 scrut = *state;
  742. uint8_t *buf = scrut.buf;
  743. uint32_t *block_state = scrut.block_state;
  744. Hacl_Hash_SHA2_sha224_init(block_state);
  745. Hacl_Streaming_MD_state_32
  746. tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  747. state[0U] = tmp;
  748. }
  749. Hacl_Streaming_Types_error_code
  750. Hacl_Hash_SHA2_update_224(
  751. Hacl_Streaming_MD_state_32 *state,
  752. uint8_t *input,
  753. uint32_t input_len
  754. )
  755. {
  756. return update_224_256(state, input, input_len);
  757. }
  758. /**
  759. Write the resulting hash into `output`, an array of 28 bytes. The state remains
  760. valid after a call to `digest_224`, meaning the user may feed more data into
  761. the hash via `update_224`.
  762. */
  763. void Hacl_Hash_SHA2_digest_224(Hacl_Streaming_MD_state_32 *state, uint8_t *output)
  764. {
  765. Hacl_Streaming_MD_state_32 scrut = *state;
  766. uint32_t *block_state = scrut.block_state;
  767. uint8_t *buf_ = scrut.buf;
  768. uint64_t total_len = scrut.total_len;
  769. uint32_t r;
  770. if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
  771. {
  772. r = 64U;
  773. }
  774. else
  775. {
  776. r = (uint32_t)(total_len % (uint64_t)64U);
  777. }
  778. uint8_t *buf_1 = buf_;
  779. uint32_t tmp_block_state[8U] = { 0U };
  780. memcpy(tmp_block_state, block_state, 8U * sizeof (uint32_t));
  781. uint32_t ite;
  782. if (r % 64U == 0U && r > 0U)
  783. {
  784. ite = 64U;
  785. }
  786. else
  787. {
  788. ite = r % 64U;
  789. }
  790. uint8_t *buf_last = buf_1 + r - ite;
  791. uint8_t *buf_multi = buf_1;
  792. sha224_update_nblocks(0U, buf_multi, tmp_block_state);
  793. uint64_t prev_len_last = total_len - (uint64_t)r;
  794. Hacl_Hash_SHA2_sha224_update_last(prev_len_last + (uint64_t)r, r, buf_last, tmp_block_state);
  795. Hacl_Hash_SHA2_sha224_finish(tmp_block_state, output);
  796. }
  797. void Hacl_Hash_SHA2_free_224(Hacl_Streaming_MD_state_32 *state)
  798. {
  799. Hacl_Hash_SHA2_free_256(state);
  800. }
  801. /**
  802. Hash `input`, of len `input_len`, into `output`, an array of 28 bytes.
  803. */
  804. void Hacl_Hash_SHA2_hash_224(uint8_t *output, uint8_t *input, uint32_t input_len)
  805. {
  806. uint8_t *ib = input;
  807. uint8_t *rb = output;
  808. uint32_t st[8U] = { 0U };
  809. Hacl_Hash_SHA2_sha224_init(st);
  810. uint32_t rem = input_len % 64U;
  811. uint64_t len_ = (uint64_t)input_len;
  812. sha224_update_nblocks(input_len, ib, st);
  813. uint32_t rem1 = input_len % 64U;
  814. uint8_t *b0 = ib;
  815. uint8_t *lb = b0 + input_len - rem1;
  816. Hacl_Hash_SHA2_sha224_update_last(len_, rem, lb, st);
  817. Hacl_Hash_SHA2_sha224_finish(st, rb);
  818. }
  819. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_512(void)
  820. {
  821. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
  822. uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t));
  823. Hacl_Streaming_MD_state_64
  824. s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  825. Hacl_Streaming_MD_state_64
  826. *p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
  827. p[0U] = s;
  828. Hacl_Hash_SHA2_sha512_init(block_state);
  829. return p;
  830. }
  831. /**
  832. Copies the state passed as argument into a newly allocated state (deep copy).
  833. The state is to be freed by calling `free_512`. Cloning the state this way is
  834. useful, for instance, if your control-flow diverges and you need to feed
  835. more (different) data into the hash in each branch.
  836. */
  837. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_copy_512(Hacl_Streaming_MD_state_64 *state)
  838. {
  839. Hacl_Streaming_MD_state_64 scrut = *state;
  840. uint64_t *block_state0 = scrut.block_state;
  841. uint8_t *buf0 = scrut.buf;
  842. uint64_t total_len0 = scrut.total_len;
  843. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
  844. memcpy(buf, buf0, 128U * sizeof (uint8_t));
  845. uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t));
  846. memcpy(block_state, block_state0, 8U * sizeof (uint64_t));
  847. Hacl_Streaming_MD_state_64
  848. s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
  849. Hacl_Streaming_MD_state_64
  850. *p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
  851. p[0U] = s;
  852. return p;
  853. }
  854. void Hacl_Hash_SHA2_reset_512(Hacl_Streaming_MD_state_64 *state)
  855. {
  856. Hacl_Streaming_MD_state_64 scrut = *state;
  857. uint8_t *buf = scrut.buf;
  858. uint64_t *block_state = scrut.block_state;
  859. Hacl_Hash_SHA2_sha512_init(block_state);
  860. Hacl_Streaming_MD_state_64
  861. tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  862. state[0U] = tmp;
  863. }
  864. static inline Hacl_Streaming_Types_error_code
  865. update_384_512(Hacl_Streaming_MD_state_64 *state, uint8_t *chunk, uint32_t chunk_len)
  866. {
  867. Hacl_Streaming_MD_state_64 s = *state;
  868. uint64_t total_len = s.total_len;
  869. if ((uint64_t)chunk_len > 18446744073709551615ULL - total_len)
  870. {
  871. return Hacl_Streaming_Types_MaximumLengthExceeded;
  872. }
  873. uint32_t sz;
  874. if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
  875. {
  876. sz = 128U;
  877. }
  878. else
  879. {
  880. sz = (uint32_t)(total_len % (uint64_t)128U);
  881. }
  882. if (chunk_len <= 128U - sz)
  883. {
  884. Hacl_Streaming_MD_state_64 s1 = *state;
  885. uint64_t *block_state1 = s1.block_state;
  886. uint8_t *buf = s1.buf;
  887. uint64_t total_len1 = s1.total_len;
  888. uint32_t sz1;
  889. if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
  890. {
  891. sz1 = 128U;
  892. }
  893. else
  894. {
  895. sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
  896. }
  897. uint8_t *buf2 = buf + sz1;
  898. memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
  899. uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
  900. *state
  901. =
  902. (
  903. (Hacl_Streaming_MD_state_64){
  904. .block_state = block_state1,
  905. .buf = buf,
  906. .total_len = total_len2
  907. }
  908. );
  909. }
  910. else if (sz == 0U)
  911. {
  912. Hacl_Streaming_MD_state_64 s1 = *state;
  913. uint64_t *block_state1 = s1.block_state;
  914. uint8_t *buf = s1.buf;
  915. uint64_t total_len1 = s1.total_len;
  916. uint32_t sz1;
  917. if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
  918. {
  919. sz1 = 128U;
  920. }
  921. else
  922. {
  923. sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
  924. }
  925. if (!(sz1 == 0U))
  926. {
  927. Hacl_Hash_SHA2_sha512_update_nblocks(128U, buf, block_state1);
  928. }
  929. uint32_t ite;
  930. if ((uint64_t)chunk_len % (uint64_t)128U == 0ULL && (uint64_t)chunk_len > 0ULL)
  931. {
  932. ite = 128U;
  933. }
  934. else
  935. {
  936. ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)128U);
  937. }
  938. uint32_t n_blocks = (chunk_len - ite) / 128U;
  939. uint32_t data1_len = n_blocks * 128U;
  940. uint32_t data2_len = chunk_len - data1_len;
  941. uint8_t *data1 = chunk;
  942. uint8_t *data2 = chunk + data1_len;
  943. Hacl_Hash_SHA2_sha512_update_nblocks(data1_len / 128U * 128U, data1, block_state1);
  944. uint8_t *dst = buf;
  945. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  946. *state
  947. =
  948. (
  949. (Hacl_Streaming_MD_state_64){
  950. .block_state = block_state1,
  951. .buf = buf,
  952. .total_len = total_len1 + (uint64_t)chunk_len
  953. }
  954. );
  955. }
  956. else
  957. {
  958. uint32_t diff = 128U - sz;
  959. uint8_t *chunk1 = chunk;
  960. uint8_t *chunk2 = chunk + diff;
  961. Hacl_Streaming_MD_state_64 s1 = *state;
  962. uint64_t *block_state10 = s1.block_state;
  963. uint8_t *buf0 = s1.buf;
  964. uint64_t total_len10 = s1.total_len;
  965. uint32_t sz10;
  966. if (total_len10 % (uint64_t)128U == 0ULL && total_len10 > 0ULL)
  967. {
  968. sz10 = 128U;
  969. }
  970. else
  971. {
  972. sz10 = (uint32_t)(total_len10 % (uint64_t)128U);
  973. }
  974. uint8_t *buf2 = buf0 + sz10;
  975. memcpy(buf2, chunk1, diff * sizeof (uint8_t));
  976. uint64_t total_len2 = total_len10 + (uint64_t)diff;
  977. *state
  978. =
  979. (
  980. (Hacl_Streaming_MD_state_64){
  981. .block_state = block_state10,
  982. .buf = buf0,
  983. .total_len = total_len2
  984. }
  985. );
  986. Hacl_Streaming_MD_state_64 s10 = *state;
  987. uint64_t *block_state1 = s10.block_state;
  988. uint8_t *buf = s10.buf;
  989. uint64_t total_len1 = s10.total_len;
  990. uint32_t sz1;
  991. if (total_len1 % (uint64_t)128U == 0ULL && total_len1 > 0ULL)
  992. {
  993. sz1 = 128U;
  994. }
  995. else
  996. {
  997. sz1 = (uint32_t)(total_len1 % (uint64_t)128U);
  998. }
  999. if (!(sz1 == 0U))
  1000. {
  1001. Hacl_Hash_SHA2_sha512_update_nblocks(128U, buf, block_state1);
  1002. }
  1003. uint32_t ite;
  1004. if
  1005. ((uint64_t)(chunk_len - diff) % (uint64_t)128U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
  1006. {
  1007. ite = 128U;
  1008. }
  1009. else
  1010. {
  1011. ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)128U);
  1012. }
  1013. uint32_t n_blocks = (chunk_len - diff - ite) / 128U;
  1014. uint32_t data1_len = n_blocks * 128U;
  1015. uint32_t data2_len = chunk_len - diff - data1_len;
  1016. uint8_t *data1 = chunk2;
  1017. uint8_t *data2 = chunk2 + data1_len;
  1018. Hacl_Hash_SHA2_sha512_update_nblocks(data1_len / 128U * 128U, data1, block_state1);
  1019. uint8_t *dst = buf;
  1020. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  1021. *state
  1022. =
  1023. (
  1024. (Hacl_Streaming_MD_state_64){
  1025. .block_state = block_state1,
  1026. .buf = buf,
  1027. .total_len = total_len1 + (uint64_t)(chunk_len - diff)
  1028. }
  1029. );
  1030. }
  1031. return Hacl_Streaming_Types_Success;
  1032. }
  1033. /**
  1034. Feed an arbitrary amount of data into the hash. This function returns 0 for
  1035. success, or 1 if the combined length of all of the data passed to `update_512`
  1036. (since the last call to `reset_512`) exceeds 2^125-1 bytes.
  1037. This function is identical to the update function for SHA2_384.
  1038. */
  1039. Hacl_Streaming_Types_error_code
  1040. Hacl_Hash_SHA2_update_512(
  1041. Hacl_Streaming_MD_state_64 *state,
  1042. uint8_t *input,
  1043. uint32_t input_len
  1044. )
  1045. {
  1046. return update_384_512(state, input, input_len);
  1047. }
  1048. /**
  1049. Write the resulting hash into `output`, an array of 64 bytes. The state remains
  1050. valid after a call to `digest_512`, meaning the user may feed more data into
  1051. the hash via `update_512`. (The digest_512 function operates on an internal copy of
  1052. the state and therefore does not invalidate the client-held state `p`.)
  1053. */
  1054. void Hacl_Hash_SHA2_digest_512(Hacl_Streaming_MD_state_64 *state, uint8_t *output)
  1055. {
  1056. Hacl_Streaming_MD_state_64 scrut = *state;
  1057. uint64_t *block_state = scrut.block_state;
  1058. uint8_t *buf_ = scrut.buf;
  1059. uint64_t total_len = scrut.total_len;
  1060. uint32_t r;
  1061. if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
  1062. {
  1063. r = 128U;
  1064. }
  1065. else
  1066. {
  1067. r = (uint32_t)(total_len % (uint64_t)128U);
  1068. }
  1069. uint8_t *buf_1 = buf_;
  1070. uint64_t tmp_block_state[8U] = { 0U };
  1071. memcpy(tmp_block_state, block_state, 8U * sizeof (uint64_t));
  1072. uint32_t ite;
  1073. if (r % 128U == 0U && r > 0U)
  1074. {
  1075. ite = 128U;
  1076. }
  1077. else
  1078. {
  1079. ite = r % 128U;
  1080. }
  1081. uint8_t *buf_last = buf_1 + r - ite;
  1082. uint8_t *buf_multi = buf_1;
  1083. Hacl_Hash_SHA2_sha512_update_nblocks(0U, buf_multi, tmp_block_state);
  1084. uint64_t prev_len_last = total_len - (uint64_t)r;
  1085. Hacl_Hash_SHA2_sha512_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last),
  1086. FStar_UInt128_uint64_to_uint128((uint64_t)r)),
  1087. r,
  1088. buf_last,
  1089. tmp_block_state);
  1090. Hacl_Hash_SHA2_sha512_finish(tmp_block_state, output);
  1091. }
  1092. /**
  1093. Free a state allocated with `malloc_512`.
  1094. This function is identical to the free function for SHA2_384.
  1095. */
  1096. void Hacl_Hash_SHA2_free_512(Hacl_Streaming_MD_state_64 *state)
  1097. {
  1098. Hacl_Streaming_MD_state_64 scrut = *state;
  1099. uint8_t *buf = scrut.buf;
  1100. uint64_t *block_state = scrut.block_state;
  1101. KRML_HOST_FREE(block_state);
  1102. KRML_HOST_FREE(buf);
  1103. KRML_HOST_FREE(state);
  1104. }
  1105. /**
  1106. Hash `input`, of len `input_len`, into `output`, an array of 64 bytes.
  1107. */
  1108. void Hacl_Hash_SHA2_hash_512(uint8_t *output, uint8_t *input, uint32_t input_len)
  1109. {
  1110. uint8_t *ib = input;
  1111. uint8_t *rb = output;
  1112. uint64_t st[8U] = { 0U };
  1113. Hacl_Hash_SHA2_sha512_init(st);
  1114. uint32_t rem = input_len % 128U;
  1115. FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len);
  1116. Hacl_Hash_SHA2_sha512_update_nblocks(input_len, ib, st);
  1117. uint32_t rem1 = input_len % 128U;
  1118. uint8_t *b0 = ib;
  1119. uint8_t *lb = b0 + input_len - rem1;
  1120. Hacl_Hash_SHA2_sha512_update_last(len_, rem, lb, st);
  1121. Hacl_Hash_SHA2_sha512_finish(st, rb);
  1122. }
  1123. Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_384(void)
  1124. {
  1125. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(128U, sizeof (uint8_t));
  1126. uint64_t *block_state = (uint64_t *)KRML_HOST_CALLOC(8U, sizeof (uint64_t));
  1127. Hacl_Streaming_MD_state_64
  1128. s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  1129. Hacl_Streaming_MD_state_64
  1130. *p = (Hacl_Streaming_MD_state_64 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_64));
  1131. p[0U] = s;
  1132. Hacl_Hash_SHA2_sha384_init(block_state);
  1133. return p;
  1134. }
  1135. void Hacl_Hash_SHA2_reset_384(Hacl_Streaming_MD_state_64 *state)
  1136. {
  1137. Hacl_Streaming_MD_state_64 scrut = *state;
  1138. uint8_t *buf = scrut.buf;
  1139. uint64_t *block_state = scrut.block_state;
  1140. Hacl_Hash_SHA2_sha384_init(block_state);
  1141. Hacl_Streaming_MD_state_64
  1142. tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  1143. state[0U] = tmp;
  1144. }
  1145. Hacl_Streaming_Types_error_code
  1146. Hacl_Hash_SHA2_update_384(
  1147. Hacl_Streaming_MD_state_64 *state,
  1148. uint8_t *input,
  1149. uint32_t input_len
  1150. )
  1151. {
  1152. return update_384_512(state, input, input_len);
  1153. }
  1154. /**
  1155. Write the resulting hash into `output`, an array of 48 bytes. The state remains
  1156. valid after a call to `digest_384`, meaning the user may feed more data into
  1157. the hash via `update_384`.
  1158. */
  1159. void Hacl_Hash_SHA2_digest_384(Hacl_Streaming_MD_state_64 *state, uint8_t *output)
  1160. {
  1161. Hacl_Streaming_MD_state_64 scrut = *state;
  1162. uint64_t *block_state = scrut.block_state;
  1163. uint8_t *buf_ = scrut.buf;
  1164. uint64_t total_len = scrut.total_len;
  1165. uint32_t r;
  1166. if (total_len % (uint64_t)128U == 0ULL && total_len > 0ULL)
  1167. {
  1168. r = 128U;
  1169. }
  1170. else
  1171. {
  1172. r = (uint32_t)(total_len % (uint64_t)128U);
  1173. }
  1174. uint8_t *buf_1 = buf_;
  1175. uint64_t tmp_block_state[8U] = { 0U };
  1176. memcpy(tmp_block_state, block_state, 8U * sizeof (uint64_t));
  1177. uint32_t ite;
  1178. if (r % 128U == 0U && r > 0U)
  1179. {
  1180. ite = 128U;
  1181. }
  1182. else
  1183. {
  1184. ite = r % 128U;
  1185. }
  1186. uint8_t *buf_last = buf_1 + r - ite;
  1187. uint8_t *buf_multi = buf_1;
  1188. Hacl_Hash_SHA2_sha384_update_nblocks(0U, buf_multi, tmp_block_state);
  1189. uint64_t prev_len_last = total_len - (uint64_t)r;
  1190. Hacl_Hash_SHA2_sha384_update_last(FStar_UInt128_add(FStar_UInt128_uint64_to_uint128(prev_len_last),
  1191. FStar_UInt128_uint64_to_uint128((uint64_t)r)),
  1192. r,
  1193. buf_last,
  1194. tmp_block_state);
  1195. Hacl_Hash_SHA2_sha384_finish(tmp_block_state, output);
  1196. }
  1197. void Hacl_Hash_SHA2_free_384(Hacl_Streaming_MD_state_64 *state)
  1198. {
  1199. Hacl_Hash_SHA2_free_512(state);
  1200. }
  1201. /**
  1202. Hash `input`, of len `input_len`, into `output`, an array of 48 bytes.
  1203. */
  1204. void Hacl_Hash_SHA2_hash_384(uint8_t *output, uint8_t *input, uint32_t input_len)
  1205. {
  1206. uint8_t *ib = input;
  1207. uint8_t *rb = output;
  1208. uint64_t st[8U] = { 0U };
  1209. Hacl_Hash_SHA2_sha384_init(st);
  1210. uint32_t rem = input_len % 128U;
  1211. FStar_UInt128_uint128 len_ = FStar_UInt128_uint64_to_uint128((uint64_t)input_len);
  1212. Hacl_Hash_SHA2_sha384_update_nblocks(input_len, ib, st);
  1213. uint32_t rem1 = input_len % 128U;
  1214. uint8_t *b0 = ib;
  1215. uint8_t *lb = b0 + input_len - rem1;
  1216. Hacl_Hash_SHA2_sha384_update_last(len_, rem, lb, st);
  1217. Hacl_Hash_SHA2_sha384_finish(st, rb);
  1218. }