Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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