sha512.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * SHA-512 hash in C
  3. *
  4. * Copyright (c) 2016 Project Nayuki
  5. * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
  6. *
  7. * (MIT License)
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. * - The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  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 <stddef.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. void sha512_compress(uint64_t state[8], const uint8_t block[128]) {
  28. #define ROTR64(x, n) (((0U + (x)) << (64 - (n))) | ((x) >> (n))) // Assumes that x is uint64_t and 0 < n < 64
  29. #define LOADSCHEDULE(i) \
  30. schedule[i] = (uint64_t)block[i * 8 + 0] << 56 \
  31. | (uint64_t)block[i * 8 + 1] << 48 \
  32. | (uint64_t)block[i * 8 + 2] << 40 \
  33. | (uint64_t)block[i * 8 + 3] << 32 \
  34. | (uint64_t)block[i * 8 + 4] << 24 \
  35. | (uint64_t)block[i * 8 + 5] << 16 \
  36. | (uint64_t)block[i * 8 + 6] << 8 \
  37. | (uint64_t)block[i * 8 + 7] << 0;
  38. #define SCHEDULE(i) \
  39. schedule[i] = 0U + schedule[i - 16] + schedule[i - 7] \
  40. + (ROTR64(schedule[i - 15], 1) ^ ROTR64(schedule[i - 15], 8) ^ (schedule[i - 15] >> 7)) \
  41. + (ROTR64(schedule[i - 2], 19) ^ ROTR64(schedule[i - 2], 61) ^ (schedule[i - 2] >> 6));
  42. #define ROUND(a, b, c, d, e, f, g, h, i, k) \
  43. h = 0U + h + (ROTR64(e, 14) ^ ROTR64(e, 18) ^ ROTR64(e, 41)) + (g ^ (e & (f ^ g))) + UINT64_C(k) + schedule[i]; \
  44. d = 0U + d + h; \
  45. h = 0U + h + (ROTR64(a, 28) ^ ROTR64(a, 34) ^ ROTR64(a, 39)) + ((a & (b | c)) | (b & c));
  46. uint64_t schedule[80];
  47. LOADSCHEDULE( 0)
  48. LOADSCHEDULE( 1)
  49. LOADSCHEDULE( 2)
  50. LOADSCHEDULE( 3)
  51. LOADSCHEDULE( 4)
  52. LOADSCHEDULE( 5)
  53. LOADSCHEDULE( 6)
  54. LOADSCHEDULE( 7)
  55. LOADSCHEDULE( 8)
  56. LOADSCHEDULE( 9)
  57. LOADSCHEDULE(10)
  58. LOADSCHEDULE(11)
  59. LOADSCHEDULE(12)
  60. LOADSCHEDULE(13)
  61. LOADSCHEDULE(14)
  62. LOADSCHEDULE(15)
  63. SCHEDULE(16)
  64. SCHEDULE(17)
  65. SCHEDULE(18)
  66. SCHEDULE(19)
  67. SCHEDULE(20)
  68. SCHEDULE(21)
  69. SCHEDULE(22)
  70. SCHEDULE(23)
  71. SCHEDULE(24)
  72. SCHEDULE(25)
  73. SCHEDULE(26)
  74. SCHEDULE(27)
  75. SCHEDULE(28)
  76. SCHEDULE(29)
  77. SCHEDULE(30)
  78. SCHEDULE(31)
  79. SCHEDULE(32)
  80. SCHEDULE(33)
  81. SCHEDULE(34)
  82. SCHEDULE(35)
  83. SCHEDULE(36)
  84. SCHEDULE(37)
  85. SCHEDULE(38)
  86. SCHEDULE(39)
  87. SCHEDULE(40)
  88. SCHEDULE(41)
  89. SCHEDULE(42)
  90. SCHEDULE(43)
  91. SCHEDULE(44)
  92. SCHEDULE(45)
  93. SCHEDULE(46)
  94. SCHEDULE(47)
  95. SCHEDULE(48)
  96. SCHEDULE(49)
  97. SCHEDULE(50)
  98. SCHEDULE(51)
  99. SCHEDULE(52)
  100. SCHEDULE(53)
  101. SCHEDULE(54)
  102. SCHEDULE(55)
  103. SCHEDULE(56)
  104. SCHEDULE(57)
  105. SCHEDULE(58)
  106. SCHEDULE(59)
  107. SCHEDULE(60)
  108. SCHEDULE(61)
  109. SCHEDULE(62)
  110. SCHEDULE(63)
  111. SCHEDULE(64)
  112. SCHEDULE(65)
  113. SCHEDULE(66)
  114. SCHEDULE(67)
  115. SCHEDULE(68)
  116. SCHEDULE(69)
  117. SCHEDULE(70)
  118. SCHEDULE(71)
  119. SCHEDULE(72)
  120. SCHEDULE(73)
  121. SCHEDULE(74)
  122. SCHEDULE(75)
  123. SCHEDULE(76)
  124. SCHEDULE(77)
  125. SCHEDULE(78)
  126. SCHEDULE(79)
  127. uint64_t a = state[0];
  128. uint64_t b = state[1];
  129. uint64_t c = state[2];
  130. uint64_t d = state[3];
  131. uint64_t e = state[4];
  132. uint64_t f = state[5];
  133. uint64_t g = state[6];
  134. uint64_t h = state[7];
  135. ROUND(a, b, c, d, e, f, g, h, 0, 0x428A2F98D728AE22)
  136. ROUND(h, a, b, c, d, e, f, g, 1, 0x7137449123EF65CD)
  137. ROUND(g, h, a, b, c, d, e, f, 2, 0xB5C0FBCFEC4D3B2F)
  138. ROUND(f, g, h, a, b, c, d, e, 3, 0xE9B5DBA58189DBBC)
  139. ROUND(e, f, g, h, a, b, c, d, 4, 0x3956C25BF348B538)
  140. ROUND(d, e, f, g, h, a, b, c, 5, 0x59F111F1B605D019)
  141. ROUND(c, d, e, f, g, h, a, b, 6, 0x923F82A4AF194F9B)
  142. ROUND(b, c, d, e, f, g, h, a, 7, 0xAB1C5ED5DA6D8118)
  143. ROUND(a, b, c, d, e, f, g, h, 8, 0xD807AA98A3030242)
  144. ROUND(h, a, b, c, d, e, f, g, 9, 0x12835B0145706FBE)
  145. ROUND(g, h, a, b, c, d, e, f, 10, 0x243185BE4EE4B28C)
  146. ROUND(f, g, h, a, b, c, d, e, 11, 0x550C7DC3D5FFB4E2)
  147. ROUND(e, f, g, h, a, b, c, d, 12, 0x72BE5D74F27B896F)
  148. ROUND(d, e, f, g, h, a, b, c, 13, 0x80DEB1FE3B1696B1)
  149. ROUND(c, d, e, f, g, h, a, b, 14, 0x9BDC06A725C71235)
  150. ROUND(b, c, d, e, f, g, h, a, 15, 0xC19BF174CF692694)
  151. ROUND(a, b, c, d, e, f, g, h, 16, 0xE49B69C19EF14AD2)
  152. ROUND(h, a, b, c, d, e, f, g, 17, 0xEFBE4786384F25E3)
  153. ROUND(g, h, a, b, c, d, e, f, 18, 0x0FC19DC68B8CD5B5)
  154. ROUND(f, g, h, a, b, c, d, e, 19, 0x240CA1CC77AC9C65)
  155. ROUND(e, f, g, h, a, b, c, d, 20, 0x2DE92C6F592B0275)
  156. ROUND(d, e, f, g, h, a, b, c, 21, 0x4A7484AA6EA6E483)
  157. ROUND(c, d, e, f, g, h, a, b, 22, 0x5CB0A9DCBD41FBD4)
  158. ROUND(b, c, d, e, f, g, h, a, 23, 0x76F988DA831153B5)
  159. ROUND(a, b, c, d, e, f, g, h, 24, 0x983E5152EE66DFAB)
  160. ROUND(h, a, b, c, d, e, f, g, 25, 0xA831C66D2DB43210)
  161. ROUND(g, h, a, b, c, d, e, f, 26, 0xB00327C898FB213F)
  162. ROUND(f, g, h, a, b, c, d, e, 27, 0xBF597FC7BEEF0EE4)
  163. ROUND(e, f, g, h, a, b, c, d, 28, 0xC6E00BF33DA88FC2)
  164. ROUND(d, e, f, g, h, a, b, c, 29, 0xD5A79147930AA725)
  165. ROUND(c, d, e, f, g, h, a, b, 30, 0x06CA6351E003826F)
  166. ROUND(b, c, d, e, f, g, h, a, 31, 0x142929670A0E6E70)
  167. ROUND(a, b, c, d, e, f, g, h, 32, 0x27B70A8546D22FFC)
  168. ROUND(h, a, b, c, d, e, f, g, 33, 0x2E1B21385C26C926)
  169. ROUND(g, h, a, b, c, d, e, f, 34, 0x4D2C6DFC5AC42AED)
  170. ROUND(f, g, h, a, b, c, d, e, 35, 0x53380D139D95B3DF)
  171. ROUND(e, f, g, h, a, b, c, d, 36, 0x650A73548BAF63DE)
  172. ROUND(d, e, f, g, h, a, b, c, 37, 0x766A0ABB3C77B2A8)
  173. ROUND(c, d, e, f, g, h, a, b, 38, 0x81C2C92E47EDAEE6)
  174. ROUND(b, c, d, e, f, g, h, a, 39, 0x92722C851482353B)
  175. ROUND(a, b, c, d, e, f, g, h, 40, 0xA2BFE8A14CF10364)
  176. ROUND(h, a, b, c, d, e, f, g, 41, 0xA81A664BBC423001)
  177. ROUND(g, h, a, b, c, d, e, f, 42, 0xC24B8B70D0F89791)
  178. ROUND(f, g, h, a, b, c, d, e, 43, 0xC76C51A30654BE30)
  179. ROUND(e, f, g, h, a, b, c, d, 44, 0xD192E819D6EF5218)
  180. ROUND(d, e, f, g, h, a, b, c, 45, 0xD69906245565A910)
  181. ROUND(c, d, e, f, g, h, a, b, 46, 0xF40E35855771202A)
  182. ROUND(b, c, d, e, f, g, h, a, 47, 0x106AA07032BBD1B8)
  183. ROUND(a, b, c, d, e, f, g, h, 48, 0x19A4C116B8D2D0C8)
  184. ROUND(h, a, b, c, d, e, f, g, 49, 0x1E376C085141AB53)
  185. ROUND(g, h, a, b, c, d, e, f, 50, 0x2748774CDF8EEB99)
  186. ROUND(f, g, h, a, b, c, d, e, 51, 0x34B0BCB5E19B48A8)
  187. ROUND(e, f, g, h, a, b, c, d, 52, 0x391C0CB3C5C95A63)
  188. ROUND(d, e, f, g, h, a, b, c, 53, 0x4ED8AA4AE3418ACB)
  189. ROUND(c, d, e, f, g, h, a, b, 54, 0x5B9CCA4F7763E373)
  190. ROUND(b, c, d, e, f, g, h, a, 55, 0x682E6FF3D6B2B8A3)
  191. ROUND(a, b, c, d, e, f, g, h, 56, 0x748F82EE5DEFB2FC)
  192. ROUND(h, a, b, c, d, e, f, g, 57, 0x78A5636F43172F60)
  193. ROUND(g, h, a, b, c, d, e, f, 58, 0x84C87814A1F0AB72)
  194. ROUND(f, g, h, a, b, c, d, e, 59, 0x8CC702081A6439EC)
  195. ROUND(e, f, g, h, a, b, c, d, 60, 0x90BEFFFA23631E28)
  196. ROUND(d, e, f, g, h, a, b, c, 61, 0xA4506CEBDE82BDE9)
  197. ROUND(c, d, e, f, g, h, a, b, 62, 0xBEF9A3F7B2C67915)
  198. ROUND(b, c, d, e, f, g, h, a, 63, 0xC67178F2E372532B)
  199. ROUND(a, b, c, d, e, f, g, h, 64, 0xCA273ECEEA26619C)
  200. ROUND(h, a, b, c, d, e, f, g, 65, 0xD186B8C721C0C207)
  201. ROUND(g, h, a, b, c, d, e, f, 66, 0xEADA7DD6CDE0EB1E)
  202. ROUND(f, g, h, a, b, c, d, e, 67, 0xF57D4F7FEE6ED178)
  203. ROUND(e, f, g, h, a, b, c, d, 68, 0x06F067AA72176FBA)
  204. ROUND(d, e, f, g, h, a, b, c, 69, 0x0A637DC5A2C898A6)
  205. ROUND(c, d, e, f, g, h, a, b, 70, 0x113F9804BEF90DAE)
  206. ROUND(b, c, d, e, f, g, h, a, 71, 0x1B710B35131C471B)
  207. ROUND(a, b, c, d, e, f, g, h, 72, 0x28DB77F523047D84)
  208. ROUND(h, a, b, c, d, e, f, g, 73, 0x32CAAB7B40C72493)
  209. ROUND(g, h, a, b, c, d, e, f, 74, 0x3C9EBE0A15C9BEBC)
  210. ROUND(f, g, h, a, b, c, d, e, 75, 0x431D67C49C100D4C)
  211. ROUND(e, f, g, h, a, b, c, d, 76, 0x4CC5D4BECB3E42B6)
  212. ROUND(d, e, f, g, h, a, b, c, 77, 0x597F299CFC657E2A)
  213. ROUND(c, d, e, f, g, h, a, b, 78, 0x5FCB6FAB3AD6FAEC)
  214. ROUND(b, c, d, e, f, g, h, a, 79, 0x6C44198C4A475817)
  215. state[0] = 0U + state[0] + a;
  216. state[1] = 0U + state[1] + b;
  217. state[2] = 0U + state[2] + c;
  218. state[3] = 0U + state[3] + d;
  219. state[4] = 0U + state[4] + e;
  220. state[5] = 0U + state[5] + f;
  221. state[6] = 0U + state[6] + g;
  222. state[7] = 0U + state[7] + h;
  223. }
  224. void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]) {
  225. hash[0] = UINT64_C(0x6A09E667F3BCC908);
  226. hash[1] = UINT64_C(0xBB67AE8584CAA73B);
  227. hash[2] = UINT64_C(0x3C6EF372FE94F82B);
  228. hash[3] = UINT64_C(0xA54FF53A5F1D36F1);
  229. hash[4] = UINT64_C(0x510E527FADE682D1);
  230. hash[5] = UINT64_C(0x9B05688C2B3E6C1F);
  231. hash[6] = UINT64_C(0x1F83D9ABFB41BD6B);
  232. hash[7] = UINT64_C(0x5BE0CD19137E2179);
  233. #define BLOCK_SIZE 128 // In bytes
  234. #define LENGTH_SIZE 16 // In bytes
  235. size_t off;
  236. for (off = 0; len - off >= BLOCK_SIZE; off += BLOCK_SIZE)
  237. sha512_compress(hash, &message[off]);
  238. uint8_t block[BLOCK_SIZE] = { 0 };
  239. size_t rem = len - off;
  240. memcpy(block, &message[off], rem);
  241. block[rem] = 0x80;
  242. rem++;
  243. if (BLOCK_SIZE - rem < LENGTH_SIZE) {
  244. sha512_compress(hash, block);
  245. memset(block, 0, sizeof(block));
  246. }
  247. block[BLOCK_SIZE - 1] = (uint8_t)((len & 0x1FU) << 3);
  248. len >>= 5;
  249. int i;
  250. for (i = 1; i < LENGTH_SIZE; i++, len >>= 8)
  251. block[BLOCK_SIZE - 1 - i] = (uint8_t)(len & 0xFFU);
  252. sha512_compress(hash, block);
  253. }