Reference implementations of PQC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

385 lines
12 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 uint64_t load_bigendian(const unsigned char *x) {
  8. return (uint64_t)(x[7]) | (((uint64_t)(x[6])) << 8) | (((uint64_t)(x[5])) << 16) |
  9. (((uint64_t)(x[4])) << 24) | (((uint64_t)(x[3])) << 32) |
  10. (((uint64_t)(x[2])) << 40) | (((uint64_t)(x[1])) << 48) |
  11. (((uint64_t)(x[0])) << 56);
  12. }
  13. static void store_bigendian(uint8_t *x, uint64_t u) {
  14. x[7] = (uint8_t) u;
  15. u >>= 8;
  16. x[6] = (uint8_t) u;
  17. u >>= 8;
  18. x[5] = (uint8_t) u;
  19. u >>= 8;
  20. x[4] = (uint8_t) u;
  21. u >>= 8;
  22. x[3] = (uint8_t) u;
  23. u >>= 8;
  24. x[2] = (uint8_t) u;
  25. u >>= 8;
  26. x[1] = (uint8_t) u;
  27. u >>= 8;
  28. x[0] = (uint8_t) u;
  29. }
  30. #define SHR(x, c) ((x) >> (c))
  31. #define ROTR(x, c) (((x) >> (c)) | ((x) << (64 - (c))))
  32. #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  33. #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  34. #define Sigma0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
  35. #define Sigma1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
  36. #define sigma0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
  37. #define sigma1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
  38. #define M(w0, w14, w9, w1) w0 = sigma1(w14) + (w9) + sigma0(w1) + (w0);
  39. #define EXPAND \
  40. M(w0, w14, w9, w1) \
  41. M(w1, w15, w10, w2) \
  42. M(w2, w0, w11, w3) \
  43. M(w3, w1, w12, w4) \
  44. M(w4, w2, w13, w5) \
  45. M(w5, w3, w14, w6) \
  46. M(w6, w4, w15, w7) \
  47. M(w7, w5, w0, w8) \
  48. M(w8, w6, w1, w9) \
  49. M(w9, w7, w2, w10) \
  50. M(w10, w8, w3, w11) \
  51. M(w11, w9, w4, w12) \
  52. M(w12, w10, w5, w13) \
  53. M(w13, w11, w6, w14) \
  54. M(w14, w12, w7, w15) \
  55. M(w15, w13, w8, w0)
  56. #define F(w, k) \
  57. T1 = h + Sigma1(e) + Ch(e, f, g) + (k) + (w); \
  58. T2 = Sigma0(a) + Maj(a, b, c); \
  59. h = g; \
  60. g = f; \
  61. f = e; \
  62. e = d + T1; \
  63. d = c; \
  64. c = b; \
  65. b = a; \
  66. a = T1 + T2;
  67. static size_t crypto_hashblocks_sha512(uint8_t *statebytes,
  68. const uint8_t *in,
  69. size_t inlen) {
  70. uint64_t state[8];
  71. uint64_t a;
  72. uint64_t b;
  73. uint64_t c;
  74. uint64_t d;
  75. uint64_t e;
  76. uint64_t f;
  77. uint64_t g;
  78. uint64_t h;
  79. uint64_t T1;
  80. uint64_t T2;
  81. a = load_bigendian(statebytes + 0);
  82. state[0] = a;
  83. b = load_bigendian(statebytes + 8);
  84. state[1] = b;
  85. c = load_bigendian(statebytes + 16);
  86. state[2] = c;
  87. d = load_bigendian(statebytes + 24);
  88. state[3] = d;
  89. e = load_bigendian(statebytes + 32);
  90. state[4] = e;
  91. f = load_bigendian(statebytes + 40);
  92. state[5] = f;
  93. g = load_bigendian(statebytes + 48);
  94. state[6] = g;
  95. h = load_bigendian(statebytes + 56);
  96. state[7] = h;
  97. while (inlen >= 128) {
  98. uint64_t w0 = load_bigendian(in + 0);
  99. uint64_t w1 = load_bigendian(in + 8);
  100. uint64_t w2 = load_bigendian(in + 16);
  101. uint64_t w3 = load_bigendian(in + 24);
  102. uint64_t w4 = load_bigendian(in + 32);
  103. uint64_t w5 = load_bigendian(in + 40);
  104. uint64_t w6 = load_bigendian(in + 48);
  105. uint64_t w7 = load_bigendian(in + 56);
  106. uint64_t w8 = load_bigendian(in + 64);
  107. uint64_t w9 = load_bigendian(in + 72);
  108. uint64_t w10 = load_bigendian(in + 80);
  109. uint64_t w11 = load_bigendian(in + 88);
  110. uint64_t w12 = load_bigendian(in + 96);
  111. uint64_t w13 = load_bigendian(in + 104);
  112. uint64_t w14 = load_bigendian(in + 112);
  113. uint64_t w15 = load_bigendian(in + 120);
  114. F(w0, 0x428a2f98d728ae22ULL)
  115. F(w1, 0x7137449123ef65cdULL)
  116. F(w2, 0xb5c0fbcfec4d3b2fULL)
  117. F(w3, 0xe9b5dba58189dbbcULL)
  118. F(w4, 0x3956c25bf348b538ULL)
  119. F(w5, 0x59f111f1b605d019ULL)
  120. F(w6, 0x923f82a4af194f9bULL)
  121. F(w7, 0xab1c5ed5da6d8118ULL)
  122. F(w8, 0xd807aa98a3030242ULL)
  123. F(w9, 0x12835b0145706fbeULL)
  124. F(w10, 0x243185be4ee4b28cULL)
  125. F(w11, 0x550c7dc3d5ffb4e2ULL)
  126. F(w12, 0x72be5d74f27b896fULL)
  127. F(w13, 0x80deb1fe3b1696b1ULL)
  128. F(w14, 0x9bdc06a725c71235ULL)
  129. F(w15, 0xc19bf174cf692694ULL)
  130. EXPAND
  131. F(w0, 0xe49b69c19ef14ad2ULL)
  132. F(w1, 0xefbe4786384f25e3ULL)
  133. F(w2, 0x0fc19dc68b8cd5b5ULL)
  134. F(w3, 0x240ca1cc77ac9c65ULL)
  135. F(w4, 0x2de92c6f592b0275ULL)
  136. F(w5, 0x4a7484aa6ea6e483ULL)
  137. F(w6, 0x5cb0a9dcbd41fbd4ULL)
  138. F(w7, 0x76f988da831153b5ULL)
  139. F(w8, 0x983e5152ee66dfabULL)
  140. F(w9, 0xa831c66d2db43210ULL)
  141. F(w10, 0xb00327c898fb213fULL)
  142. F(w11, 0xbf597fc7beef0ee4ULL)
  143. F(w12, 0xc6e00bf33da88fc2ULL)
  144. F(w13, 0xd5a79147930aa725ULL)
  145. F(w14, 0x06ca6351e003826fULL)
  146. F(w15, 0x142929670a0e6e70ULL)
  147. EXPAND
  148. F(w0, 0x27b70a8546d22ffcULL)
  149. F(w1, 0x2e1b21385c26c926ULL)
  150. F(w2, 0x4d2c6dfc5ac42aedULL)
  151. F(w3, 0x53380d139d95b3dfULL)
  152. F(w4, 0x650a73548baf63deULL)
  153. F(w5, 0x766a0abb3c77b2a8ULL)
  154. F(w6, 0x81c2c92e47edaee6ULL)
  155. F(w7, 0x92722c851482353bULL)
  156. F(w8, 0xa2bfe8a14cf10364ULL)
  157. F(w9, 0xa81a664bbc423001ULL)
  158. F(w10, 0xc24b8b70d0f89791ULL)
  159. F(w11, 0xc76c51a30654be30ULL)
  160. F(w12, 0xd192e819d6ef5218ULL)
  161. F(w13, 0xd69906245565a910ULL)
  162. F(w14, 0xf40e35855771202aULL)
  163. F(w15, 0x106aa07032bbd1b8ULL)
  164. EXPAND
  165. F(w0, 0x19a4c116b8d2d0c8ULL)
  166. F(w1, 0x1e376c085141ab53ULL)
  167. F(w2, 0x2748774cdf8eeb99ULL)
  168. F(w3, 0x34b0bcb5e19b48a8ULL)
  169. F(w4, 0x391c0cb3c5c95a63ULL)
  170. F(w5, 0x4ed8aa4ae3418acbULL)
  171. F(w6, 0x5b9cca4f7763e373ULL)
  172. F(w7, 0x682e6ff3d6b2b8a3ULL)
  173. F(w8, 0x748f82ee5defb2fcULL)
  174. F(w9, 0x78a5636f43172f60ULL)
  175. F(w10, 0x84c87814a1f0ab72ULL)
  176. F(w11, 0x8cc702081a6439ecULL)
  177. F(w12, 0x90befffa23631e28ULL)
  178. F(w13, 0xa4506cebde82bde9ULL)
  179. F(w14, 0xbef9a3f7b2c67915ULL)
  180. F(w15, 0xc67178f2e372532bULL)
  181. EXPAND
  182. F(w0, 0xca273eceea26619cULL)
  183. F(w1, 0xd186b8c721c0c207ULL)
  184. F(w2, 0xeada7dd6cde0eb1eULL)
  185. F(w3, 0xf57d4f7fee6ed178ULL)
  186. F(w4, 0x06f067aa72176fbaULL)
  187. F(w5, 0x0a637dc5a2c898a6ULL)
  188. F(w6, 0x113f9804bef90daeULL)
  189. F(w7, 0x1b710b35131c471bULL)
  190. F(w8, 0x28db77f523047d84ULL)
  191. F(w9, 0x32caab7b40c72493ULL)
  192. F(w10, 0x3c9ebe0a15c9bebcULL)
  193. F(w11, 0x431d67c49c100d4cULL)
  194. F(w12, 0x4cc5d4becb3e42b6ULL)
  195. F(w13, 0x597f299cfc657e2aULL)
  196. F(w14, 0x5fcb6fab3ad6faecULL)
  197. F(w15, 0x6c44198c4a475817ULL)
  198. a += state[0];
  199. b += state[1];
  200. c += state[2];
  201. d += state[3];
  202. e += state[4];
  203. f += state[5];
  204. g += state[6];
  205. h += state[7];
  206. state[0] = a;
  207. state[1] = b;
  208. state[2] = c;
  209. state[3] = d;
  210. state[4] = e;
  211. state[5] = f;
  212. state[6] = g;
  213. state[7] = h;
  214. in += 128;
  215. inlen -= 128;
  216. }
  217. store_bigendian(statebytes + 0, state[0]);
  218. store_bigendian(statebytes + 8, state[1]);
  219. store_bigendian(statebytes + 16, state[2]);
  220. store_bigendian(statebytes + 24, state[3]);
  221. store_bigendian(statebytes + 32, state[4]);
  222. store_bigendian(statebytes + 40, state[5]);
  223. store_bigendian(statebytes + 48, state[6]);
  224. store_bigendian(statebytes + 56, state[7]);
  225. return inlen;
  226. }
  227. #define blocks crypto_hashblocks_sha512
  228. static const uint8_t iv_384[64] = {
  229. 0xcb, 0xbb, 0x9d, 0x5d, 0xc1, 0x05, 0x9e, 0xd8, 0x62, 0x9a, 0x29,
  230. 0x2a, 0x36, 0x7c, 0xd5, 0x07, 0x91, 0x59, 0x01, 0x5a, 0x30, 0x70,
  231. 0xdd, 0x17, 0x15, 0x2f, 0xec, 0xd8, 0xf7, 0x0e, 0x59, 0x39, 0x67,
  232. 0x33, 0x26, 0x67, 0xff, 0xc0, 0x0b, 0x31, 0x8e, 0xb4, 0x4a, 0x87,
  233. 0x68, 0x58, 0x15, 0x11, 0xdb, 0x0c, 0x2e, 0x0d, 0x64, 0xf9, 0x8f,
  234. 0xa7, 0x47, 0xb5, 0x48, 0x1d, 0xbe, 0xfa, 0x4f, 0xa4
  235. };
  236. static const uint8_t iv_512[64] = {
  237. 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08, 0xbb, 0x67, 0xae,
  238. 0x85, 0x84, 0xca, 0xa7, 0x3b, 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94,
  239. 0xf8, 0x2b, 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1, 0x51,
  240. 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1, 0x9b, 0x05, 0x68, 0x8c,
  241. 0x2b, 0x3e, 0x6c, 0x1f, 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd,
  242. 0x6b, 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79
  243. };
  244. int sha384(uint8_t *out, const uint8_t *in, size_t inlen) {
  245. uint8_t h[64];
  246. uint8_t padded[256];
  247. uint64_t bytes = inlen;
  248. for (size_t i = 0; i < 64; ++i) {
  249. h[i] = iv_384[i];
  250. }
  251. blocks(h, in, inlen);
  252. in += inlen;
  253. inlen &= 127;
  254. in -= inlen;
  255. for (size_t i = 0; i < inlen; ++i) {
  256. padded[i] = in[i];
  257. }
  258. padded[inlen] = 0x80;
  259. if (inlen < 112) {
  260. for (size_t i = inlen + 1; i < 119; ++i) {
  261. padded[i] = 0;
  262. }
  263. padded[119] = (uint8_t) (bytes >> 61);
  264. padded[120] = (uint8_t) (bytes >> 53);
  265. padded[121] = (uint8_t) (bytes >> 45);
  266. padded[122] = (uint8_t) (bytes >> 37);
  267. padded[123] = (uint8_t) (bytes >> 29);
  268. padded[124] = (uint8_t) (bytes >> 21);
  269. padded[125] = (uint8_t) (bytes >> 13);
  270. padded[126] = (uint8_t) (bytes >> 5);
  271. padded[127] = (uint8_t) (bytes << 3);
  272. blocks(h, padded, 128);
  273. } else {
  274. for (size_t i = inlen + 1; i < 247; ++i) {
  275. padded[i] = 0;
  276. }
  277. padded[247] = (uint8_t) (bytes >> 61);
  278. padded[248] = (uint8_t) (bytes >> 53);
  279. padded[249] = (uint8_t) (bytes >> 45);
  280. padded[250] = (uint8_t) (bytes >> 37);
  281. padded[251] = (uint8_t) (bytes >> 29);
  282. padded[252] = (uint8_t) (bytes >> 21);
  283. padded[253] = (uint8_t) (bytes >> 13);
  284. padded[254] = (uint8_t) (bytes >> 5);
  285. padded[255] = (uint8_t) (bytes << 3);
  286. blocks(h, padded, 256);
  287. }
  288. for (size_t i = 0; i < 48; ++i) {
  289. out[i] = h[i];
  290. }
  291. return 0;
  292. }
  293. int sha512(uint8_t *out, const uint8_t *in, size_t inlen) {
  294. uint8_t h[64];
  295. uint8_t padded[256];
  296. uint64_t bytes = inlen;
  297. for (size_t i = 0; i < 64; ++i) {
  298. h[i] = iv_512[i];
  299. }
  300. blocks(h, in, inlen);
  301. in += inlen;
  302. inlen &= 127;
  303. in -= inlen;
  304. for (size_t i = 0; i < inlen; ++i) {
  305. padded[i] = in[i];
  306. }
  307. padded[inlen] = 0x80;
  308. if (inlen < 112) {
  309. for (size_t i = inlen + 1; i < 119; ++i) {
  310. padded[i] = 0;
  311. }
  312. padded[119] = (uint8_t) (bytes >> 61);
  313. padded[120] = (uint8_t) (bytes >> 53);
  314. padded[121] = (uint8_t) (bytes >> 45);
  315. padded[122] = (uint8_t) (bytes >> 37);
  316. padded[123] = (uint8_t) (bytes >> 29);
  317. padded[124] = (uint8_t) (bytes >> 21);
  318. padded[125] = (uint8_t) (bytes >> 13);
  319. padded[126] = (uint8_t) (bytes >> 5);
  320. padded[127] = (uint8_t) (bytes << 3);
  321. blocks(h, padded, 128);
  322. } else {
  323. for (size_t i = inlen + 1; i < 247; ++i) {
  324. padded[i] = 0;
  325. }
  326. padded[247] = (uint8_t) (bytes >> 61);
  327. padded[248] = (uint8_t) (bytes >> 53);
  328. padded[249] = (uint8_t) (bytes >> 45);
  329. padded[250] = (uint8_t) (bytes >> 37);
  330. padded[251] = (uint8_t) (bytes >> 29);
  331. padded[252] = (uint8_t) (bytes >> 21);
  332. padded[253] = (uint8_t) (bytes >> 13);
  333. padded[254] = (uint8_t) (bytes >> 5);
  334. padded[255] = (uint8_t) (bytes << 3);
  335. blocks(h, padded, 256);
  336. }
  337. for (size_t i = 0; i < 64; ++i) {
  338. out[i] = h[i];
  339. }
  340. return 0;
  341. }