Hacl_Hash_SHA1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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_SHA1.h"
  25. static uint32_t _h0[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
  26. void Hacl_Hash_SHA1_init(uint32_t *s)
  27. {
  28. KRML_MAYBE_FOR5(i, 0U, 5U, 1U, s[i] = _h0[i];);
  29. }
  30. static void update(uint32_t *h, uint8_t *l)
  31. {
  32. uint32_t ha = h[0U];
  33. uint32_t hb = h[1U];
  34. uint32_t hc = h[2U];
  35. uint32_t hd = h[3U];
  36. uint32_t he = h[4U];
  37. uint32_t _w[80U] = { 0U };
  38. for (uint32_t i = 0U; i < 80U; i++)
  39. {
  40. uint32_t v;
  41. if (i < 16U)
  42. {
  43. uint8_t *b = l + i * 4U;
  44. uint32_t u = load32_be(b);
  45. v = u;
  46. }
  47. else
  48. {
  49. uint32_t wmit3 = _w[i - 3U];
  50. uint32_t wmit8 = _w[i - 8U];
  51. uint32_t wmit14 = _w[i - 14U];
  52. uint32_t wmit16 = _w[i - 16U];
  53. v = (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) << 1U | (wmit3 ^ (wmit8 ^ (wmit14 ^ wmit16))) >> 31U;
  54. }
  55. _w[i] = v;
  56. }
  57. for (uint32_t i = 0U; i < 80U; i++)
  58. {
  59. uint32_t _a = h[0U];
  60. uint32_t _b = h[1U];
  61. uint32_t _c = h[2U];
  62. uint32_t _d = h[3U];
  63. uint32_t _e = h[4U];
  64. uint32_t wmit = _w[i];
  65. uint32_t ite0;
  66. if (i < 20U)
  67. {
  68. ite0 = (_b & _c) ^ (~_b & _d);
  69. }
  70. else if (39U < i && i < 60U)
  71. {
  72. ite0 = (_b & _c) ^ ((_b & _d) ^ (_c & _d));
  73. }
  74. else
  75. {
  76. ite0 = _b ^ (_c ^ _d);
  77. }
  78. uint32_t ite;
  79. if (i < 20U)
  80. {
  81. ite = 0x5a827999U;
  82. }
  83. else if (i < 40U)
  84. {
  85. ite = 0x6ed9eba1U;
  86. }
  87. else if (i < 60U)
  88. {
  89. ite = 0x8f1bbcdcU;
  90. }
  91. else
  92. {
  93. ite = 0xca62c1d6U;
  94. }
  95. uint32_t _T = (_a << 5U | _a >> 27U) + ite0 + _e + ite + wmit;
  96. h[0U] = _T;
  97. h[1U] = _a;
  98. h[2U] = _b << 30U | _b >> 2U;
  99. h[3U] = _c;
  100. h[4U] = _d;
  101. }
  102. for (uint32_t i = 0U; i < 80U; i++)
  103. {
  104. _w[i] = 0U;
  105. }
  106. uint32_t sta = h[0U];
  107. uint32_t stb = h[1U];
  108. uint32_t stc = h[2U];
  109. uint32_t std = h[3U];
  110. uint32_t ste = h[4U];
  111. h[0U] = sta + ha;
  112. h[1U] = stb + hb;
  113. h[2U] = stc + hc;
  114. h[3U] = std + hd;
  115. h[4U] = ste + he;
  116. }
  117. static void pad(uint64_t len, uint8_t *dst)
  118. {
  119. uint8_t *dst1 = dst;
  120. dst1[0U] = 0x80U;
  121. uint8_t *dst2 = dst + 1U;
  122. for (uint32_t i = 0U; i < (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U; i++)
  123. {
  124. dst2[i] = 0U;
  125. }
  126. uint8_t *dst3 = dst + 1U + (128U - (9U + (uint32_t)(len % (uint64_t)64U))) % 64U;
  127. store64_be(dst3, len << 3U);
  128. }
  129. void Hacl_Hash_SHA1_finish(uint32_t *s, uint8_t *dst)
  130. {
  131. KRML_MAYBE_FOR5(i, 0U, 5U, 1U, store32_be(dst + i * 4U, s[i]););
  132. }
  133. void Hacl_Hash_SHA1_update_multi(uint32_t *s, uint8_t *blocks, uint32_t n_blocks)
  134. {
  135. for (uint32_t i = 0U; i < n_blocks; i++)
  136. {
  137. uint32_t sz = 64U;
  138. uint8_t *block = blocks + sz * i;
  139. update(s, block);
  140. }
  141. }
  142. void
  143. Hacl_Hash_SHA1_update_last(uint32_t *s, uint64_t prev_len, uint8_t *input, uint32_t input_len)
  144. {
  145. uint32_t blocks_n = input_len / 64U;
  146. uint32_t blocks_len = blocks_n * 64U;
  147. uint8_t *blocks = input;
  148. uint32_t rest_len = input_len - blocks_len;
  149. uint8_t *rest = input + blocks_len;
  150. Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
  151. uint64_t total_input_len = prev_len + (uint64_t)input_len;
  152. uint32_t pad_len = 1U + (128U - (9U + (uint32_t)(total_input_len % (uint64_t)64U))) % 64U + 8U;
  153. uint32_t tmp_len = rest_len + pad_len;
  154. uint8_t tmp_twoblocks[128U] = { 0U };
  155. uint8_t *tmp = tmp_twoblocks;
  156. uint8_t *tmp_rest = tmp;
  157. uint8_t *tmp_pad = tmp + rest_len;
  158. memcpy(tmp_rest, rest, rest_len * sizeof (uint8_t));
  159. pad(total_input_len, tmp_pad);
  160. Hacl_Hash_SHA1_update_multi(s, tmp, tmp_len / 64U);
  161. }
  162. void Hacl_Hash_SHA1_hash_oneshot(uint8_t *output, uint8_t *input, uint32_t input_len)
  163. {
  164. uint32_t s[5U] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
  165. uint32_t blocks_n0 = input_len / 64U;
  166. uint32_t blocks_n1;
  167. if (input_len % 64U == 0U && blocks_n0 > 0U)
  168. {
  169. blocks_n1 = blocks_n0 - 1U;
  170. }
  171. else
  172. {
  173. blocks_n1 = blocks_n0;
  174. }
  175. uint32_t blocks_len0 = blocks_n1 * 64U;
  176. uint8_t *blocks0 = input;
  177. uint32_t rest_len0 = input_len - blocks_len0;
  178. uint8_t *rest0 = input + blocks_len0;
  179. uint32_t blocks_n = blocks_n1;
  180. uint32_t blocks_len = blocks_len0;
  181. uint8_t *blocks = blocks0;
  182. uint32_t rest_len = rest_len0;
  183. uint8_t *rest = rest0;
  184. Hacl_Hash_SHA1_update_multi(s, blocks, blocks_n);
  185. Hacl_Hash_SHA1_update_last(s, (uint64_t)blocks_len, rest, rest_len);
  186. Hacl_Hash_SHA1_finish(s, output);
  187. }
  188. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_malloc(void)
  189. {
  190. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
  191. uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
  192. Hacl_Streaming_MD_state_32
  193. s = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  194. Hacl_Streaming_MD_state_32
  195. *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
  196. p[0U] = s;
  197. Hacl_Hash_SHA1_init(block_state);
  198. return p;
  199. }
  200. void Hacl_Hash_SHA1_reset(Hacl_Streaming_MD_state_32 *state)
  201. {
  202. Hacl_Streaming_MD_state_32 scrut = *state;
  203. uint8_t *buf = scrut.buf;
  204. uint32_t *block_state = scrut.block_state;
  205. Hacl_Hash_SHA1_init(block_state);
  206. Hacl_Streaming_MD_state_32
  207. tmp = { .block_state = block_state, .buf = buf, .total_len = (uint64_t)0U };
  208. state[0U] = tmp;
  209. }
  210. /**
  211. 0 = success, 1 = max length exceeded
  212. */
  213. Hacl_Streaming_Types_error_code
  214. Hacl_Hash_SHA1_update(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len)
  215. {
  216. Hacl_Streaming_MD_state_32 s = *state;
  217. uint64_t total_len = s.total_len;
  218. if ((uint64_t)chunk_len > 2305843009213693951ULL - total_len)
  219. {
  220. return Hacl_Streaming_Types_MaximumLengthExceeded;
  221. }
  222. uint32_t sz;
  223. if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
  224. {
  225. sz = 64U;
  226. }
  227. else
  228. {
  229. sz = (uint32_t)(total_len % (uint64_t)64U);
  230. }
  231. if (chunk_len <= 64U - sz)
  232. {
  233. Hacl_Streaming_MD_state_32 s1 = *state;
  234. uint32_t *block_state1 = s1.block_state;
  235. uint8_t *buf = s1.buf;
  236. uint64_t total_len1 = s1.total_len;
  237. uint32_t sz1;
  238. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  239. {
  240. sz1 = 64U;
  241. }
  242. else
  243. {
  244. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  245. }
  246. uint8_t *buf2 = buf + sz1;
  247. memcpy(buf2, chunk, chunk_len * sizeof (uint8_t));
  248. uint64_t total_len2 = total_len1 + (uint64_t)chunk_len;
  249. *state
  250. =
  251. (
  252. (Hacl_Streaming_MD_state_32){
  253. .block_state = block_state1,
  254. .buf = buf,
  255. .total_len = total_len2
  256. }
  257. );
  258. }
  259. else if (sz == 0U)
  260. {
  261. Hacl_Streaming_MD_state_32 s1 = *state;
  262. uint32_t *block_state1 = s1.block_state;
  263. uint8_t *buf = s1.buf;
  264. uint64_t total_len1 = s1.total_len;
  265. uint32_t sz1;
  266. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  267. {
  268. sz1 = 64U;
  269. }
  270. else
  271. {
  272. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  273. }
  274. if (!(sz1 == 0U))
  275. {
  276. Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
  277. }
  278. uint32_t ite;
  279. if ((uint64_t)chunk_len % (uint64_t)64U == 0ULL && (uint64_t)chunk_len > 0ULL)
  280. {
  281. ite = 64U;
  282. }
  283. else
  284. {
  285. ite = (uint32_t)((uint64_t)chunk_len % (uint64_t)64U);
  286. }
  287. uint32_t n_blocks = (chunk_len - ite) / 64U;
  288. uint32_t data1_len = n_blocks * 64U;
  289. uint32_t data2_len = chunk_len - data1_len;
  290. uint8_t *data1 = chunk;
  291. uint8_t *data2 = chunk + data1_len;
  292. Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
  293. uint8_t *dst = buf;
  294. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  295. *state
  296. =
  297. (
  298. (Hacl_Streaming_MD_state_32){
  299. .block_state = block_state1,
  300. .buf = buf,
  301. .total_len = total_len1 + (uint64_t)chunk_len
  302. }
  303. );
  304. }
  305. else
  306. {
  307. uint32_t diff = 64U - sz;
  308. uint8_t *chunk1 = chunk;
  309. uint8_t *chunk2 = chunk + diff;
  310. Hacl_Streaming_MD_state_32 s1 = *state;
  311. uint32_t *block_state10 = s1.block_state;
  312. uint8_t *buf0 = s1.buf;
  313. uint64_t total_len10 = s1.total_len;
  314. uint32_t sz10;
  315. if (total_len10 % (uint64_t)64U == 0ULL && total_len10 > 0ULL)
  316. {
  317. sz10 = 64U;
  318. }
  319. else
  320. {
  321. sz10 = (uint32_t)(total_len10 % (uint64_t)64U);
  322. }
  323. uint8_t *buf2 = buf0 + sz10;
  324. memcpy(buf2, chunk1, diff * sizeof (uint8_t));
  325. uint64_t total_len2 = total_len10 + (uint64_t)diff;
  326. *state
  327. =
  328. (
  329. (Hacl_Streaming_MD_state_32){
  330. .block_state = block_state10,
  331. .buf = buf0,
  332. .total_len = total_len2
  333. }
  334. );
  335. Hacl_Streaming_MD_state_32 s10 = *state;
  336. uint32_t *block_state1 = s10.block_state;
  337. uint8_t *buf = s10.buf;
  338. uint64_t total_len1 = s10.total_len;
  339. uint32_t sz1;
  340. if (total_len1 % (uint64_t)64U == 0ULL && total_len1 > 0ULL)
  341. {
  342. sz1 = 64U;
  343. }
  344. else
  345. {
  346. sz1 = (uint32_t)(total_len1 % (uint64_t)64U);
  347. }
  348. if (!(sz1 == 0U))
  349. {
  350. Hacl_Hash_SHA1_update_multi(block_state1, buf, 1U);
  351. }
  352. uint32_t ite;
  353. if
  354. ((uint64_t)(chunk_len - diff) % (uint64_t)64U == 0ULL && (uint64_t)(chunk_len - diff) > 0ULL)
  355. {
  356. ite = 64U;
  357. }
  358. else
  359. {
  360. ite = (uint32_t)((uint64_t)(chunk_len - diff) % (uint64_t)64U);
  361. }
  362. uint32_t n_blocks = (chunk_len - diff - ite) / 64U;
  363. uint32_t data1_len = n_blocks * 64U;
  364. uint32_t data2_len = chunk_len - diff - data1_len;
  365. uint8_t *data1 = chunk2;
  366. uint8_t *data2 = chunk2 + data1_len;
  367. Hacl_Hash_SHA1_update_multi(block_state1, data1, data1_len / 64U);
  368. uint8_t *dst = buf;
  369. memcpy(dst, data2, data2_len * sizeof (uint8_t));
  370. *state
  371. =
  372. (
  373. (Hacl_Streaming_MD_state_32){
  374. .block_state = block_state1,
  375. .buf = buf,
  376. .total_len = total_len1 + (uint64_t)(chunk_len - diff)
  377. }
  378. );
  379. }
  380. return Hacl_Streaming_Types_Success;
  381. }
  382. void Hacl_Hash_SHA1_digest(Hacl_Streaming_MD_state_32 *state, uint8_t *output)
  383. {
  384. Hacl_Streaming_MD_state_32 scrut = *state;
  385. uint32_t *block_state = scrut.block_state;
  386. uint8_t *buf_ = scrut.buf;
  387. uint64_t total_len = scrut.total_len;
  388. uint32_t r;
  389. if (total_len % (uint64_t)64U == 0ULL && total_len > 0ULL)
  390. {
  391. r = 64U;
  392. }
  393. else
  394. {
  395. r = (uint32_t)(total_len % (uint64_t)64U);
  396. }
  397. uint8_t *buf_1 = buf_;
  398. uint32_t tmp_block_state[5U] = { 0U };
  399. memcpy(tmp_block_state, block_state, 5U * sizeof (uint32_t));
  400. uint32_t ite;
  401. if (r % 64U == 0U && r > 0U)
  402. {
  403. ite = 64U;
  404. }
  405. else
  406. {
  407. ite = r % 64U;
  408. }
  409. uint8_t *buf_last = buf_1 + r - ite;
  410. uint8_t *buf_multi = buf_1;
  411. Hacl_Hash_SHA1_update_multi(tmp_block_state, buf_multi, 0U);
  412. uint64_t prev_len_last = total_len - (uint64_t)r;
  413. Hacl_Hash_SHA1_update_last(tmp_block_state, prev_len_last, buf_last, r);
  414. Hacl_Hash_SHA1_finish(tmp_block_state, output);
  415. }
  416. void Hacl_Hash_SHA1_free(Hacl_Streaming_MD_state_32 *state)
  417. {
  418. Hacl_Streaming_MD_state_32 scrut = *state;
  419. uint8_t *buf = scrut.buf;
  420. uint32_t *block_state = scrut.block_state;
  421. KRML_HOST_FREE(block_state);
  422. KRML_HOST_FREE(buf);
  423. KRML_HOST_FREE(state);
  424. }
  425. Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_copy(Hacl_Streaming_MD_state_32 *state)
  426. {
  427. Hacl_Streaming_MD_state_32 scrut = *state;
  428. uint32_t *block_state0 = scrut.block_state;
  429. uint8_t *buf0 = scrut.buf;
  430. uint64_t total_len0 = scrut.total_len;
  431. uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC(64U, sizeof (uint8_t));
  432. memcpy(buf, buf0, 64U * sizeof (uint8_t));
  433. uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC(5U, sizeof (uint32_t));
  434. memcpy(block_state, block_state0, 5U * sizeof (uint32_t));
  435. Hacl_Streaming_MD_state_32
  436. s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
  437. Hacl_Streaming_MD_state_32
  438. *p = (Hacl_Streaming_MD_state_32 *)KRML_HOST_MALLOC(sizeof (Hacl_Streaming_MD_state_32));
  439. p[0U] = s;
  440. return p;
  441. }
  442. void Hacl_Hash_SHA1_hash(uint8_t *output, uint8_t *input, uint32_t input_len)
  443. {
  444. Hacl_Hash_SHA1_hash_oneshot(output, input, input_len);
  445. }