Reference implementations of PQC
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

696 rindas
21 KiB

  1. /* Based on the public domain implementation in
  2. * crypto_hash/sha512/ref/ from http://bench.cr.yp.to/supercop.html
  3. * by D. J. Bernstein */
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include "sha2.h"
  7. static uint32_t load_bigendian_32(const uint8_t *x) {
  8. return (uint32_t)(x[3]) | (((uint32_t)(x[2])) << 8) |
  9. (((uint32_t)(x[1])) << 16) | (((uint32_t)(x[0])) << 24);
  10. }
  11. static uint64_t load_bigendian_64(const uint8_t *x) {
  12. return (uint64_t)(x[7]) | (((uint64_t)(x[6])) << 8) |
  13. (((uint64_t)(x[5])) << 16) | (((uint64_t)(x[4])) << 24) |
  14. (((uint64_t)(x[3])) << 32) | (((uint64_t)(x[2])) << 40) |
  15. (((uint64_t)(x[1])) << 48) | (((uint64_t)(x[0])) << 56);
  16. }
  17. static void store_bigendian_32(uint8_t *x, uint64_t u) {
  18. x[3] = (uint8_t) u;
  19. u >>= 8;
  20. x[2] = (uint8_t) u;
  21. u >>= 8;
  22. x[1] = (uint8_t) u;
  23. u >>= 8;
  24. x[0] = (uint8_t) u;
  25. }
  26. static void store_bigendian_64(uint8_t *x, uint64_t u) {
  27. x[7] = (uint8_t) u;
  28. u >>= 8;
  29. x[6] = (uint8_t) u;
  30. u >>= 8;
  31. x[5] = (uint8_t) u;
  32. u >>= 8;
  33. x[4] = (uint8_t) u;
  34. u >>= 8;
  35. x[3] = (uint8_t) u;
  36. u >>= 8;
  37. x[2] = (uint8_t) u;
  38. u >>= 8;
  39. x[1] = (uint8_t) u;
  40. u >>= 8;
  41. x[0] = (uint8_t) u;
  42. }
  43. #define SHR(x, c) ((x) >> (c))
  44. #define ROTR_32(x, c) (((x) >> (c)) | ((x) << (32 - (c))))
  45. #define ROTR_64(x, c) (((x) >> (c)) | ((x) << (64 - (c))))
  46. #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  47. #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  48. #define Sigma0_32(x) (ROTR_32(x, 2) ^ ROTR_32(x,13) ^ ROTR_32(x,22))
  49. #define Sigma1_32(x) (ROTR_32(x, 6) ^ ROTR_32(x,11) ^ ROTR_32(x,25))
  50. #define sigma0_32(x) (ROTR_32(x, 7) ^ ROTR_32(x,18) ^ SHR(x, 3))
  51. #define sigma1_32(x) (ROTR_32(x,17) ^ ROTR_32(x,19) ^ SHR(x,10))
  52. #define Sigma0_64(x) (ROTR_64(x, 28) ^ ROTR_64(x, 34) ^ ROTR_64(x, 39))
  53. #define Sigma1_64(x) (ROTR_64(x, 14) ^ ROTR_64(x, 18) ^ ROTR_64(x, 41))
  54. #define sigma0_64(x) (ROTR_64(x, 1) ^ ROTR_64(x, 8) ^ SHR(x, 7))
  55. #define sigma1_64(x) (ROTR_64(x, 19) ^ ROTR_64(x, 61) ^ SHR(x, 6))
  56. #define M_32(w0, w14, w9, w1) w0 = sigma1_32(w14) + (w9) + sigma0_32(w1) + (w0);
  57. #define M_64(w0, w14, w9, w1) w0 = sigma1_64(w14) + (w9) + sigma0_64(w1) + (w0);
  58. #define EXPAND_32 \
  59. M_32(w0, w14, w9, w1) \
  60. M_32(w1, w15, w10, w2) \
  61. M_32(w2, w0, w11, w3) \
  62. M_32(w3, w1, w12, w4) \
  63. M_32(w4, w2, w13, w5) \
  64. M_32(w5, w3, w14, w6) \
  65. M_32(w6, w4, w15, w7) \
  66. M_32(w7, w5, w0, w8) \
  67. M_32(w8, w6, w1, w9) \
  68. M_32(w9, w7, w2, w10) \
  69. M_32(w10, w8, w3, w11) \
  70. M_32(w11, w9, w4, w12) \
  71. M_32(w12, w10, w5, w13) \
  72. M_32(w13, w11, w6, w14) \
  73. M_32(w14, w12, w7, w15) \
  74. M_32(w15, w13, w8, w0)
  75. #define EXPAND_64 \
  76. M_64(w0, w14, w9, w1) \
  77. M_64(w1, w15, w10, w2) \
  78. M_64(w2, w0, w11, w3) \
  79. M_64(w3, w1, w12, w4) \
  80. M_64(w4, w2, w13, w5) \
  81. M_64(w5, w3, w14, w6) \
  82. M_64(w6, w4, w15, w7) \
  83. M_64(w7, w5, w0, w8) \
  84. M_64(w8, w6, w1, w9) \
  85. M_64(w9, w7, w2, w10) \
  86. M_64(w10, w8, w3, w11) \
  87. M_64(w11, w9, w4, w12) \
  88. M_64(w12, w10, w5, w13) \
  89. M_64(w13, w11, w6, w14) \
  90. M_64(w14, w12, w7, w15) \
  91. M_64(w15, w13, w8, w0)
  92. #define F_32(w, k) \
  93. T1 = h + Sigma1_32(e) + Ch(e, f, g) + (k) + (w); \
  94. T2 = Sigma0_32(a) + Maj(a, b, c); \
  95. h = g; \
  96. g = f; \
  97. f = e; \
  98. e = d + T1; \
  99. d = c; \
  100. c = b; \
  101. b = a; \
  102. a = T1 + T2;
  103. #define F_64(w, k) \
  104. T1 = h + Sigma1_64(e) + Ch(e, f, g) + (k) + (w); \
  105. T2 = Sigma0_64(a) + Maj(a, b, c); \
  106. h = g; \
  107. g = f; \
  108. f = e; \
  109. e = d + T1; \
  110. d = c; \
  111. c = b; \
  112. b = a; \
  113. a = T1 + T2;
  114. static size_t crypto_hashblocks_sha256(uint8_t *statebytes,
  115. const uint8_t *in, size_t inlen) {
  116. uint32_t state[8];
  117. uint32_t a;
  118. uint32_t b;
  119. uint32_t c;
  120. uint32_t d;
  121. uint32_t e;
  122. uint32_t f;
  123. uint32_t g;
  124. uint32_t h;
  125. uint32_t T1;
  126. uint32_t T2;
  127. a = load_bigendian_32(statebytes + 0);
  128. state[0] = a;
  129. b = load_bigendian_32(statebytes + 4);
  130. state[1] = b;
  131. c = load_bigendian_32(statebytes + 8);
  132. state[2] = c;
  133. d = load_bigendian_32(statebytes + 12);
  134. state[3] = d;
  135. e = load_bigendian_32(statebytes + 16);
  136. state[4] = e;
  137. f = load_bigendian_32(statebytes + 20);
  138. state[5] = f;
  139. g = load_bigendian_32(statebytes + 24);
  140. state[6] = g;
  141. h = load_bigendian_32(statebytes + 28);
  142. state[7] = h;
  143. while (inlen >= 64) {
  144. uint32_t w0 = load_bigendian_32(in + 0);
  145. uint32_t w1 = load_bigendian_32(in + 4);
  146. uint32_t w2 = load_bigendian_32(in + 8);
  147. uint32_t w3 = load_bigendian_32(in + 12);
  148. uint32_t w4 = load_bigendian_32(in + 16);
  149. uint32_t w5 = load_bigendian_32(in + 20);
  150. uint32_t w6 = load_bigendian_32(in + 24);
  151. uint32_t w7 = load_bigendian_32(in + 28);
  152. uint32_t w8 = load_bigendian_32(in + 32);
  153. uint32_t w9 = load_bigendian_32(in + 36);
  154. uint32_t w10 = load_bigendian_32(in + 40);
  155. uint32_t w11 = load_bigendian_32(in + 44);
  156. uint32_t w12 = load_bigendian_32(in + 48);
  157. uint32_t w13 = load_bigendian_32(in + 52);
  158. uint32_t w14 = load_bigendian_32(in + 56);
  159. uint32_t w15 = load_bigendian_32(in + 60);
  160. F_32(w0, 0x428a2f98)
  161. F_32(w1, 0x71374491)
  162. F_32(w2, 0xb5c0fbcf)
  163. F_32(w3, 0xe9b5dba5)
  164. F_32(w4, 0x3956c25b)
  165. F_32(w5, 0x59f111f1)
  166. F_32(w6, 0x923f82a4)
  167. F_32(w7, 0xab1c5ed5)
  168. F_32(w8, 0xd807aa98)
  169. F_32(w9, 0x12835b01)
  170. F_32(w10, 0x243185be)
  171. F_32(w11, 0x550c7dc3)
  172. F_32(w12, 0x72be5d74)
  173. F_32(w13, 0x80deb1fe)
  174. F_32(w14, 0x9bdc06a7)
  175. F_32(w15, 0xc19bf174)
  176. EXPAND_32
  177. F_32(w0, 0xe49b69c1)
  178. F_32(w1, 0xefbe4786)
  179. F_32(w2, 0x0fc19dc6)
  180. F_32(w3, 0x240ca1cc)
  181. F_32(w4, 0x2de92c6f)
  182. F_32(w5, 0x4a7484aa)
  183. F_32(w6, 0x5cb0a9dc)
  184. F_32(w7, 0x76f988da)
  185. F_32(w8, 0x983e5152)
  186. F_32(w9, 0xa831c66d)
  187. F_32(w10, 0xb00327c8)
  188. F_32(w11, 0xbf597fc7)
  189. F_32(w12, 0xc6e00bf3)
  190. F_32(w13, 0xd5a79147)
  191. F_32(w14, 0x06ca6351)
  192. F_32(w15, 0x14292967)
  193. EXPAND_32
  194. F_32(w0, 0x27b70a85)
  195. F_32(w1, 0x2e1b2138)
  196. F_32(w2, 0x4d2c6dfc)
  197. F_32(w3, 0x53380d13)
  198. F_32(w4, 0x650a7354)
  199. F_32(w5, 0x766a0abb)
  200. F_32(w6, 0x81c2c92e)
  201. F_32(w7, 0x92722c85)
  202. F_32(w8, 0xa2bfe8a1)
  203. F_32(w9, 0xa81a664b)
  204. F_32(w10, 0xc24b8b70)
  205. F_32(w11, 0xc76c51a3)
  206. F_32(w12, 0xd192e819)
  207. F_32(w13, 0xd6990624)
  208. F_32(w14, 0xf40e3585)
  209. F_32(w15, 0x106aa070)
  210. EXPAND_32
  211. F_32(w0, 0x19a4c116)
  212. F_32(w1, 0x1e376c08)
  213. F_32(w2, 0x2748774c)
  214. F_32(w3, 0x34b0bcb5)
  215. F_32(w4, 0x391c0cb3)
  216. F_32(w5, 0x4ed8aa4a)
  217. F_32(w6, 0x5b9cca4f)
  218. F_32(w7, 0x682e6ff3)
  219. F_32(w8, 0x748f82ee)
  220. F_32(w9, 0x78a5636f)
  221. F_32(w10, 0x84c87814)
  222. F_32(w11, 0x8cc70208)
  223. F_32(w12, 0x90befffa)
  224. F_32(w13, 0xa4506ceb)
  225. F_32(w14, 0xbef9a3f7)
  226. F_32(w15, 0xc67178f2)
  227. a += state[0];
  228. b += state[1];
  229. c += state[2];
  230. d += state[3];
  231. e += state[4];
  232. f += state[5];
  233. g += state[6];
  234. h += state[7];
  235. state[0] = a;
  236. state[1] = b;
  237. state[2] = c;
  238. state[3] = d;
  239. state[4] = e;
  240. state[5] = f;
  241. state[6] = g;
  242. state[7] = h;
  243. in += 64;
  244. inlen -= 64;
  245. }
  246. store_bigendian_32(statebytes + 0, state[0]);
  247. store_bigendian_32(statebytes + 4, state[1]);
  248. store_bigendian_32(statebytes + 8, state[2]);
  249. store_bigendian_32(statebytes + 12, state[3]);
  250. store_bigendian_32(statebytes + 16, state[4]);
  251. store_bigendian_32(statebytes + 20, state[5]);
  252. store_bigendian_32(statebytes + 24, state[6]);
  253. store_bigendian_32(statebytes + 28, state[7]);
  254. return inlen;
  255. }
  256. static size_t crypto_hashblocks_sha512(uint8_t *statebytes,
  257. const uint8_t *in, size_t inlen) {
  258. uint64_t state[8];
  259. uint64_t a;
  260. uint64_t b;
  261. uint64_t c;
  262. uint64_t d;
  263. uint64_t e;
  264. uint64_t f;
  265. uint64_t g;
  266. uint64_t h;
  267. uint64_t T1;
  268. uint64_t T2;
  269. a = load_bigendian_64(statebytes + 0);
  270. state[0] = a;
  271. b = load_bigendian_64(statebytes + 8);
  272. state[1] = b;
  273. c = load_bigendian_64(statebytes + 16);
  274. state[2] = c;
  275. d = load_bigendian_64(statebytes + 24);
  276. state[3] = d;
  277. e = load_bigendian_64(statebytes + 32);
  278. state[4] = e;
  279. f = load_bigendian_64(statebytes + 40);
  280. state[5] = f;
  281. g = load_bigendian_64(statebytes + 48);
  282. state[6] = g;
  283. h = load_bigendian_64(statebytes + 56);
  284. state[7] = h;
  285. while (inlen >= 128) {
  286. uint64_t w0 = load_bigendian_64(in + 0);
  287. uint64_t w1 = load_bigendian_64(in + 8);
  288. uint64_t w2 = load_bigendian_64(in + 16);
  289. uint64_t w3 = load_bigendian_64(in + 24);
  290. uint64_t w4 = load_bigendian_64(in + 32);
  291. uint64_t w5 = load_bigendian_64(in + 40);
  292. uint64_t w6 = load_bigendian_64(in + 48);
  293. uint64_t w7 = load_bigendian_64(in + 56);
  294. uint64_t w8 = load_bigendian_64(in + 64);
  295. uint64_t w9 = load_bigendian_64(in + 72);
  296. uint64_t w10 = load_bigendian_64(in + 80);
  297. uint64_t w11 = load_bigendian_64(in + 88);
  298. uint64_t w12 = load_bigendian_64(in + 96);
  299. uint64_t w13 = load_bigendian_64(in + 104);
  300. uint64_t w14 = load_bigendian_64(in + 112);
  301. uint64_t w15 = load_bigendian_64(in + 120);
  302. F_64(w0, 0x428a2f98d728ae22ULL)
  303. F_64(w1, 0x7137449123ef65cdULL)
  304. F_64(w2, 0xb5c0fbcfec4d3b2fULL)
  305. F_64(w3, 0xe9b5dba58189dbbcULL)
  306. F_64(w4, 0x3956c25bf348b538ULL)
  307. F_64(w5, 0x59f111f1b605d019ULL)
  308. F_64(w6, 0x923f82a4af194f9bULL)
  309. F_64(w7, 0xab1c5ed5da6d8118ULL)
  310. F_64(w8, 0xd807aa98a3030242ULL)
  311. F_64(w9, 0x12835b0145706fbeULL)
  312. F_64(w10, 0x243185be4ee4b28cULL)
  313. F_64(w11, 0x550c7dc3d5ffb4e2ULL)
  314. F_64(w12, 0x72be5d74f27b896fULL)
  315. F_64(w13, 0x80deb1fe3b1696b1ULL)
  316. F_64(w14, 0x9bdc06a725c71235ULL)
  317. F_64(w15, 0xc19bf174cf692694ULL)
  318. EXPAND_64
  319. F_64(w0, 0xe49b69c19ef14ad2ULL)
  320. F_64(w1, 0xefbe4786384f25e3ULL)
  321. F_64(w2, 0x0fc19dc68b8cd5b5ULL)
  322. F_64(w3, 0x240ca1cc77ac9c65ULL)
  323. F_64(w4, 0x2de92c6f592b0275ULL)
  324. F_64(w5, 0x4a7484aa6ea6e483ULL)
  325. F_64(w6, 0x5cb0a9dcbd41fbd4ULL)
  326. F_64(w7, 0x76f988da831153b5ULL)
  327. F_64(w8, 0x983e5152ee66dfabULL)
  328. F_64(w9, 0xa831c66d2db43210ULL)
  329. F_64(w10, 0xb00327c898fb213fULL)
  330. F_64(w11, 0xbf597fc7beef0ee4ULL)
  331. F_64(w12, 0xc6e00bf33da88fc2ULL)
  332. F_64(w13, 0xd5a79147930aa725ULL)
  333. F_64(w14, 0x06ca6351e003826fULL)
  334. F_64(w15, 0x142929670a0e6e70ULL)
  335. EXPAND_64
  336. F_64(w0, 0x27b70a8546d22ffcULL)
  337. F_64(w1, 0x2e1b21385c26c926ULL)
  338. F_64(w2, 0x4d2c6dfc5ac42aedULL)
  339. F_64(w3, 0x53380d139d95b3dfULL)
  340. F_64(w4, 0x650a73548baf63deULL)
  341. F_64(w5, 0x766a0abb3c77b2a8ULL)
  342. F_64(w6, 0x81c2c92e47edaee6ULL)
  343. F_64(w7, 0x92722c851482353bULL)
  344. F_64(w8, 0xa2bfe8a14cf10364ULL)
  345. F_64(w9, 0xa81a664bbc423001ULL)
  346. F_64(w10, 0xc24b8b70d0f89791ULL)
  347. F_64(w11, 0xc76c51a30654be30ULL)
  348. F_64(w12, 0xd192e819d6ef5218ULL)
  349. F_64(w13, 0xd69906245565a910ULL)
  350. F_64(w14, 0xf40e35855771202aULL)
  351. F_64(w15, 0x106aa07032bbd1b8ULL)
  352. EXPAND_64
  353. F_64(w0, 0x19a4c116b8d2d0c8ULL)
  354. F_64(w1, 0x1e376c085141ab53ULL)
  355. F_64(w2, 0x2748774cdf8eeb99ULL)
  356. F_64(w3, 0x34b0bcb5e19b48a8ULL)
  357. F_64(w4, 0x391c0cb3c5c95a63ULL)
  358. F_64(w5, 0x4ed8aa4ae3418acbULL)
  359. F_64(w6, 0x5b9cca4f7763e373ULL)
  360. F_64(w7, 0x682e6ff3d6b2b8a3ULL)
  361. F_64(w8, 0x748f82ee5defb2fcULL)
  362. F_64(w9, 0x78a5636f43172f60ULL)
  363. F_64(w10, 0x84c87814a1f0ab72ULL)
  364. F_64(w11, 0x8cc702081a6439ecULL)
  365. F_64(w12, 0x90befffa23631e28ULL)
  366. F_64(w13, 0xa4506cebde82bde9ULL)
  367. F_64(w14, 0xbef9a3f7b2c67915ULL)
  368. F_64(w15, 0xc67178f2e372532bULL)
  369. EXPAND_64
  370. F_64(w0, 0xca273eceea26619cULL)
  371. F_64(w1, 0xd186b8c721c0c207ULL)
  372. F_64(w2, 0xeada7dd6cde0eb1eULL)
  373. F_64(w3, 0xf57d4f7fee6ed178ULL)
  374. F_64(w4, 0x06f067aa72176fbaULL)
  375. F_64(w5, 0x0a637dc5a2c898a6ULL)
  376. F_64(w6, 0x113f9804bef90daeULL)
  377. F_64(w7, 0x1b710b35131c471bULL)
  378. F_64(w8, 0x28db77f523047d84ULL)
  379. F_64(w9, 0x32caab7b40c72493ULL)
  380. F_64(w10, 0x3c9ebe0a15c9bebcULL)
  381. F_64(w11, 0x431d67c49c100d4cULL)
  382. F_64(w12, 0x4cc5d4becb3e42b6ULL)
  383. F_64(w13, 0x597f299cfc657e2aULL)
  384. F_64(w14, 0x5fcb6fab3ad6faecULL)
  385. F_64(w15, 0x6c44198c4a475817ULL)
  386. a += state[0];
  387. b += state[1];
  388. c += state[2];
  389. d += state[3];
  390. e += state[4];
  391. f += state[5];
  392. g += state[6];
  393. h += state[7];
  394. state[0] = a;
  395. state[1] = b;
  396. state[2] = c;
  397. state[3] = d;
  398. state[4] = e;
  399. state[5] = f;
  400. state[6] = g;
  401. state[7] = h;
  402. in += 128;
  403. inlen -= 128;
  404. }
  405. store_bigendian_64(statebytes + 0, state[0]);
  406. store_bigendian_64(statebytes + 8, state[1]);
  407. store_bigendian_64(statebytes + 16, state[2]);
  408. store_bigendian_64(statebytes + 24, state[3]);
  409. store_bigendian_64(statebytes + 32, state[4]);
  410. store_bigendian_64(statebytes + 40, state[5]);
  411. store_bigendian_64(statebytes + 48, state[6]);
  412. store_bigendian_64(statebytes + 56, state[7]);
  413. return inlen;
  414. }
  415. static const uint8_t iv_224[32] = {
  416. 0xc1, 0x05, 0x9e, 0xd8, 0x36, 0x7c, 0xd5, 0x07,
  417. 0x30, 0x70, 0xdd, 0x17, 0xf7, 0x0e, 0x59, 0x39,
  418. 0xff, 0xc0, 0x0b, 0x31, 0x68, 0x58, 0x15, 0x11,
  419. 0x64, 0xf9, 0x8f, 0xa7, 0xbe, 0xfa, 0x4f, 0xa4
  420. };
  421. static const uint8_t iv_256[32] = {
  422. 0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae, 0x85,
  423. 0x3c, 0x6e, 0xf3, 0x72, 0xa5, 0x4f, 0xf5, 0x3a,
  424. 0x51, 0x0e, 0x52, 0x7f, 0x9b, 0x05, 0x68, 0x8c,
  425. 0x1f, 0x83, 0xd9, 0xab, 0x5b, 0xe0, 0xcd, 0x19
  426. };
  427. static const uint8_t iv_384[64] = {
  428. 0xcb, 0xbb, 0x9d, 0x5d, 0xc1, 0x05, 0x9e, 0xd8, 0x62, 0x9a, 0x29,
  429. 0x2a, 0x36, 0x7c, 0xd5, 0x07, 0x91, 0x59, 0x01, 0x5a, 0x30, 0x70,
  430. 0xdd, 0x17, 0x15, 0x2f, 0xec, 0xd8, 0xf7, 0x0e, 0x59, 0x39, 0x67,
  431. 0x33, 0x26, 0x67, 0xff, 0xc0, 0x0b, 0x31, 0x8e, 0xb4, 0x4a, 0x87,
  432. 0x68, 0x58, 0x15, 0x11, 0xdb, 0x0c, 0x2e, 0x0d, 0x64, 0xf9, 0x8f,
  433. 0xa7, 0x47, 0xb5, 0x48, 0x1d, 0xbe, 0xfa, 0x4f, 0xa4
  434. };
  435. static const uint8_t iv_512[64] = {
  436. 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08, 0xbb, 0x67, 0xae,
  437. 0x85, 0x84, 0xca, 0xa7, 0x3b, 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94,
  438. 0xf8, 0x2b, 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1, 0x51,
  439. 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1, 0x9b, 0x05, 0x68, 0x8c,
  440. 0x2b, 0x3e, 0x6c, 0x1f, 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd,
  441. 0x6b, 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79
  442. };
  443. void sha224_inc_init(sha224ctx *state) {
  444. for (size_t i = 0; i < 32; ++i) {
  445. state->ctx[i] = iv_224[i];
  446. }
  447. for (size_t i = 32; i < 40; ++i) {
  448. state->ctx[i] = 0;
  449. }
  450. }
  451. void sha256_inc_init(sha256ctx *state) {
  452. for (size_t i = 0; i < 32; ++i) {
  453. state->ctx[i] = iv_256[i];
  454. }
  455. for (size_t i = 32; i < 40; ++i) {
  456. state->ctx[i] = 0;
  457. }
  458. }
  459. void sha384_inc_init(sha384ctx *state) {
  460. for (size_t i = 0; i < 64; ++i) {
  461. state->ctx[i] = iv_384[i];
  462. }
  463. for (size_t i = 64; i < 72; ++i) {
  464. state->ctx[i] = 0;
  465. }
  466. }
  467. void sha512_inc_init(sha512ctx *state) {
  468. for (size_t i = 0; i < 64; ++i) {
  469. state->ctx[i] = iv_512[i];
  470. }
  471. for (size_t i = 64; i < 72; ++i) {
  472. state->ctx[i] = 0;
  473. }
  474. }
  475. void sha256_inc_blocks(sha256ctx *state, const uint8_t *in, size_t inblocks) {
  476. uint64_t bytes = load_bigendian_64(state->ctx + 32);
  477. crypto_hashblocks_sha256(state->ctx, in, 64 * inblocks);
  478. bytes += 64 * inblocks;
  479. store_bigendian_64(state->ctx + 32, bytes);
  480. }
  481. void sha224_inc_blocks(sha224ctx *state, const uint8_t *in, size_t inblocks) {
  482. sha256_inc_blocks((sha256ctx*) state, in, inblocks);
  483. }
  484. void sha512_inc_blocks(sha512ctx *state, const uint8_t *in, size_t inblocks) {
  485. uint64_t bytes = load_bigendian_64(state->ctx + 64);
  486. crypto_hashblocks_sha256(state->ctx, in, 128 * inblocks);
  487. bytes += 128 * inblocks;
  488. store_bigendian_64(state->ctx + 64, bytes);
  489. }
  490. void sha384_inc_blocks(sha384ctx *state, const uint8_t *in, size_t inblocks) {
  491. sha512_inc_blocks((sha512ctx*) state, in, inblocks);
  492. }
  493. void sha256_inc_finalize(uint8_t *out, sha256ctx *state, const uint8_t *in, size_t inlen) {
  494. uint8_t padded[128];
  495. uint64_t bytes = load_bigendian_64(state->ctx + 32) + inlen;
  496. crypto_hashblocks_sha256(state->ctx, in, inlen);
  497. in += inlen;
  498. inlen &= 63;
  499. in -= inlen;
  500. for (size_t i = 0; i < inlen; ++i) {
  501. padded[i] = in[i];
  502. }
  503. padded[inlen] = 0x80;
  504. if (inlen < 56) {
  505. for (size_t i = inlen + 1; i < 56; ++i) {
  506. padded[i] = 0;
  507. }
  508. padded[56] = (uint8_t) (bytes >> 53);
  509. padded[57] = (uint8_t) (bytes >> 45);
  510. padded[58] = (uint8_t) (bytes >> 37);
  511. padded[59] = (uint8_t) (bytes >> 29);
  512. padded[60] = (uint8_t) (bytes >> 21);
  513. padded[61] = (uint8_t) (bytes >> 13);
  514. padded[62] = (uint8_t) (bytes >> 5);
  515. padded[63] = (uint8_t) (bytes << 3);
  516. crypto_hashblocks_sha256(state->ctx, padded, 64);
  517. } else {
  518. for (size_t i = inlen + 1; i < 120; ++i) {
  519. padded[i] = 0;
  520. }
  521. padded[120] = (uint8_t) (bytes >> 53);
  522. padded[121] = (uint8_t) (bytes >> 45);
  523. padded[122] = (uint8_t) (bytes >> 37);
  524. padded[123] = (uint8_t) (bytes >> 29);
  525. padded[124] = (uint8_t) (bytes >> 21);
  526. padded[125] = (uint8_t) (bytes >> 13);
  527. padded[126] = (uint8_t) (bytes >> 5);
  528. padded[127] = (uint8_t) (bytes << 3);
  529. crypto_hashblocks_sha256(state->ctx, padded, 128);
  530. }
  531. for (size_t i = 0; i < 32; ++i) {
  532. out[i] = state->ctx[i];
  533. }
  534. }
  535. void sha224_inc_finalize(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen) {
  536. sha256_inc_finalize(state->ctx, (sha256ctx*)state, in, inlen);
  537. for (size_t i = 0; i < 28; ++i) {
  538. out[i] = state->ctx[i];
  539. }
  540. }
  541. void sha512_inc_finalize(uint8_t *out, sha512ctx *state, const uint8_t *in, size_t inlen) {
  542. uint8_t padded[256];
  543. uint64_t bytes = load_bigendian_64(state->ctx + 64) + inlen;
  544. crypto_hashblocks_sha512(state->ctx, in, inlen);
  545. in += inlen;
  546. inlen &= 127;
  547. in -= inlen;
  548. for (size_t i = 0; i < inlen; ++i) {
  549. padded[i] = in[i];
  550. }
  551. padded[inlen] = 0x80;
  552. if (inlen < 112) {
  553. for (size_t i = inlen + 1; i < 119; ++i) {
  554. padded[i] = 0;
  555. }
  556. padded[119] = (uint8_t) (bytes >> 61);
  557. padded[120] = (uint8_t) (bytes >> 53);
  558. padded[121] = (uint8_t) (bytes >> 45);
  559. padded[122] = (uint8_t) (bytes >> 37);
  560. padded[123] = (uint8_t) (bytes >> 29);
  561. padded[124] = (uint8_t) (bytes >> 21);
  562. padded[125] = (uint8_t) (bytes >> 13);
  563. padded[126] = (uint8_t) (bytes >> 5);
  564. padded[127] = (uint8_t) (bytes << 3);
  565. crypto_hashblocks_sha512(state->ctx, padded, 128);
  566. } else {
  567. for (size_t i = inlen + 1; i < 247; ++i) {
  568. padded[i] = 0;
  569. }
  570. padded[247] = (uint8_t) (bytes >> 61);
  571. padded[248] = (uint8_t) (bytes >> 53);
  572. padded[249] = (uint8_t) (bytes >> 45);
  573. padded[250] = (uint8_t) (bytes >> 37);
  574. padded[251] = (uint8_t) (bytes >> 29);
  575. padded[252] = (uint8_t) (bytes >> 21);
  576. padded[253] = (uint8_t) (bytes >> 13);
  577. padded[254] = (uint8_t) (bytes >> 5);
  578. padded[255] = (uint8_t) (bytes << 3);
  579. crypto_hashblocks_sha512(state->ctx, padded, 256);
  580. }
  581. for (size_t i = 0; i < 64; ++i) {
  582. out[i] = state->ctx[i];
  583. }
  584. }
  585. void sha384_inc_finalize(uint8_t *out, sha384ctx *state, const uint8_t *in, size_t inlen) {
  586. sha512_inc_finalize(state->ctx, (sha512ctx*)state, in, inlen);
  587. for (size_t i = 0; i < 48; ++i) {
  588. out[i] = state->ctx[i];
  589. }
  590. }
  591. void sha224(uint8_t *out, const uint8_t *in, size_t inlen) {
  592. sha224ctx state;
  593. sha224_inc_init(&state);
  594. sha224_inc_finalize(out, &state, in, inlen);
  595. }
  596. void sha256(uint8_t *out, const uint8_t *in, size_t inlen) {
  597. sha256ctx state;
  598. sha256_inc_init(&state);
  599. sha256_inc_finalize(out, &state, in, inlen);
  600. }
  601. void sha384(uint8_t *out, const uint8_t *in, size_t inlen) {
  602. sha384ctx state;
  603. sha384_inc_init(&state);
  604. sha384_inc_finalize(out, &state, in, inlen);
  605. }
  606. void sha512(uint8_t *out, const uint8_t *in, size_t inlen) {
  607. sha512ctx state;
  608. sha512_inc_init(&state);
  609. sha512_inc_finalize(out, &state, in, inlen);
  610. }