sha512.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* SHA512
  17. * Daniel Beer <dlbeer@gmail.com>, 22 Apr 2014
  18. *
  19. * This file is in the public domain.
  20. */
  21. #include "sha512.h"
  22. static const uint64_t sha512_initial_state[8] = {
  23. 0x6a09e667f3bcc908LL, 0xbb67ae8584caa73bLL,
  24. 0x3c6ef372fe94f82bLL, 0xa54ff53a5f1d36f1LL,
  25. 0x510e527fade682d1LL, 0x9b05688c2b3e6c1fLL,
  26. 0x1f83d9abfb41bd6bLL, 0x5be0cd19137e2179LL,
  27. };
  28. static const uint64_t round_k[80] = {
  29. 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL,
  30. 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL,
  31. 0x3956c25bf348b538LL, 0x59f111f1b605d019LL,
  32. 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL,
  33. 0xd807aa98a3030242LL, 0x12835b0145706fbeLL,
  34. 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL,
  35. 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL,
  36. 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL,
  37. 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL,
  38. 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL,
  39. 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL,
  40. 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL,
  41. 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL,
  42. 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL,
  43. 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL,
  44. 0x06ca6351e003826fLL, 0x142929670a0e6e70LL,
  45. 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL,
  46. 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL,
  47. 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL,
  48. 0x81c2c92e47edaee6LL, 0x92722c851482353bLL,
  49. 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL,
  50. 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL,
  51. 0xd192e819d6ef5218LL, 0xd69906245565a910LL,
  52. 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL,
  53. 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL,
  54. 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL,
  55. 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL,
  56. 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL,
  57. 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL,
  58. 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL,
  59. 0x90befffa23631e28LL, 0xa4506cebde82bde9LL,
  60. 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL,
  61. 0xca273eceea26619cLL, 0xd186b8c721c0c207LL,
  62. 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL,
  63. 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL,
  64. 0x113f9804bef90daeLL, 0x1b710b35131c471bLL,
  65. 0x28db77f523047d84LL, 0x32caab7b40c72493LL,
  66. 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL,
  67. 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL,
  68. 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL,
  69. };
  70. static inline uint64_t load64(const uint8_t *x)
  71. {
  72. uint64_t r;
  73. r = *(x++);
  74. r = (r << 8) | *(x++);
  75. r = (r << 8) | *(x++);
  76. r = (r << 8) | *(x++);
  77. r = (r << 8) | *(x++);
  78. r = (r << 8) | *(x++);
  79. r = (r << 8) | *(x++);
  80. r = (r << 8) | *(x++);
  81. return r;
  82. }
  83. static inline void store64(uint8_t *x, uint64_t v)
  84. {
  85. x += 7;
  86. *(x--) = v;
  87. v >>= 8;
  88. *(x--) = v;
  89. v >>= 8;
  90. *(x--) = v;
  91. v >>= 8;
  92. *(x--) = v;
  93. v >>= 8;
  94. *(x--) = v;
  95. v >>= 8;
  96. *(x--) = v;
  97. v >>= 8;
  98. *(x--) = v;
  99. v >>= 8;
  100. *(x--) = v;
  101. }
  102. static inline uint64_t rot64(uint64_t x, int bits)
  103. {
  104. return (x >> bits) | (x << (64 - bits));
  105. }
  106. static void
  107. sha512_block(struct sha512_state *s, const uint8_t *blk)
  108. {
  109. uint64_t w[16];
  110. uint64_t a, b, c, d, e, f, g, h;
  111. int i;
  112. for (i = 0; i < 16; i++) {
  113. w[i] = load64(blk);
  114. blk += 8;
  115. }
  116. /* Load state */
  117. a = s->h[0];
  118. b = s->h[1];
  119. c = s->h[2];
  120. d = s->h[3];
  121. e = s->h[4];
  122. f = s->h[5];
  123. g = s->h[6];
  124. h = s->h[7];
  125. for (i = 0; i < 80; i++) {
  126. /* Compute value of w[i + 16]. w[wrap(i)] is currently w[i] */
  127. const uint64_t wi = w[i & 15];
  128. const uint64_t wi15 = w[(i + 1) & 15];
  129. const uint64_t wi2 = w[(i + 14) & 15];
  130. const uint64_t wi7 = w[(i + 9) & 15];
  131. const uint64_t s0 =
  132. rot64(wi15, 1) ^ rot64(wi15, 8) ^ (wi15 >> 7);
  133. const uint64_t s1 =
  134. rot64(wi2, 19) ^ rot64(wi2, 61) ^ (wi2 >> 6);
  135. /* Round calculations */
  136. const uint64_t S0 = rot64(a, 28) ^ rot64(a, 34) ^ rot64(a, 39);
  137. const uint64_t S1 = rot64(e, 14) ^ rot64(e, 18) ^ rot64(e, 41);
  138. const uint64_t ch = (e & f) ^ ((~e) & g);
  139. const uint64_t temp1 = h + S1 + ch + round_k[i] + wi;
  140. const uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
  141. const uint64_t temp2 = S0 + maj;
  142. /* Update round state */
  143. h = g;
  144. g = f;
  145. f = e;
  146. e = d + temp1;
  147. d = c;
  148. c = b;
  149. b = a;
  150. a = temp1 + temp2;
  151. /* w[wrap(i)] becomes w[i + 16] */
  152. w[i & 15] = wi + s0 + wi7 + s1;
  153. }
  154. /* Store state */
  155. s->h[0] += a;
  156. s->h[1] += b;
  157. s->h[2] += c;
  158. s->h[3] += d;
  159. s->h[4] += e;
  160. s->h[5] += f;
  161. s->h[6] += g;
  162. s->h[7] += h;
  163. }
  164. void sha512_init(struct sha512_state *s)
  165. {
  166. memcpy(s->h, &sha512_initial_state, sizeof(s->h));
  167. s->len = 0;
  168. }
  169. void sha512_add(struct sha512_state *s, const void *data, size_t len)
  170. {
  171. unsigned int partial = s->len & (SHA512_BLOCK_SIZE - 1);
  172. if (partial) {
  173. unsigned int cur = SHA512_BLOCK_SIZE - partial;
  174. if (cur > len)
  175. cur = len;
  176. memcpy(&s->partial[partial], data, cur);
  177. s->len += cur;
  178. data += cur;
  179. len -= cur;
  180. partial = s->len & (SHA512_BLOCK_SIZE - 1);
  181. if (!partial)
  182. sha512_block(s, s->partial);
  183. }
  184. while (len >= SHA512_BLOCK_SIZE) {
  185. sha512_block(s, data);
  186. s->len += SHA512_BLOCK_SIZE;
  187. data += SHA512_BLOCK_SIZE;
  188. len -= SHA512_BLOCK_SIZE;
  189. }
  190. if (!len)
  191. return;
  192. memcpy(s->partial, data, len);
  193. s->len += len;
  194. }
  195. void sha512_final(struct sha512_state *s, uint8_t *hash)
  196. {
  197. size_t last_size = s->len & (SHA512_BLOCK_SIZE - 1);
  198. unsigned int len = SHA512_HASH_SIZE;
  199. int i = 0;
  200. s->partial[last_size++] = 0x80;
  201. if (last_size < SHA512_BLOCK_SIZE)
  202. memset(&s->partial[last_size], 0,
  203. SHA512_BLOCK_SIZE - last_size);
  204. if (last_size > (SHA512_BLOCK_SIZE - 16)) {
  205. sha512_block(s, s->partial);
  206. memset(s->partial, 0, sizeof(s->partial));
  207. }
  208. /* Note: we assume total_size fits in 61 bits */
  209. store64(s->partial + SHA512_BLOCK_SIZE - 8, s->len << 3);
  210. sha512_block(s, s->partial);
  211. /* Read out whole words */
  212. while (len >= 8) {
  213. store64(hash, s->h[i++]);
  214. hash += 8;
  215. len -= 8;
  216. }
  217. /* Read out bytes */
  218. if (len) {
  219. uint8_t tmp[8];
  220. store64(tmp, s->h[i]);
  221. memcpy(hash, tmp, len);
  222. }
  223. }