sha512-internal.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * SHA-512 hash implementation and interface functions
  3. * Copyright (c) 2015, Pali Rohár <pali.rohar@gmail.com>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "sha512_i.h"
  11. #include "crypto.h"
  12. /**
  13. * sha512_vector - SHA512 hash for data vector
  14. * @num_elem: Number of elements in the data vector
  15. * @addr: Pointers to the data areas
  16. * @len: Lengths of the data blocks
  17. * @mac: Buffer for the hash
  18. * Returns: 0 on success, -1 of failure
  19. */
  20. int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  21. u8 *mac)
  22. {
  23. struct sha512_state ctx;
  24. size_t i;
  25. sha512_init(&ctx);
  26. for (i = 0; i < num_elem; i++)
  27. if (sha512_process(&ctx, addr[i], len[i]))
  28. return -1;
  29. if (sha512_done(&ctx, mac))
  30. return -1;
  31. return 0;
  32. }
  33. /* ===== start - public domain SHA512 implementation ===== */
  34. /* This is based on SHA512 implementation in LibTomCrypt that was released into
  35. * public domain by Tom St Denis. */
  36. #define CONST64(n) n ## ULL
  37. /* the K array */
  38. static const u64 K[80] = {
  39. CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
  40. CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
  41. CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
  42. CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
  43. CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
  44. CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
  45. CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
  46. CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
  47. CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
  48. CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
  49. CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
  50. CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
  51. CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
  52. CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
  53. CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
  54. CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
  55. CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
  56. CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
  57. CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
  58. CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
  59. CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
  60. CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
  61. CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
  62. CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
  63. CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
  64. CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
  65. CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
  66. CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
  67. CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
  68. CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
  69. CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
  70. CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
  71. CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
  72. CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
  73. CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
  74. CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
  75. CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
  76. CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
  77. CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
  78. CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
  79. };
  80. /* Various logical functions */
  81. #define Ch(x,y,z) (z ^ (x & (y ^ z)))
  82. #define Maj(x,y,z) (((x | y) & z) | (x & y))
  83. #define S(x, n) ROR64c(x, n)
  84. #define R(x, n) (((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) n))
  85. #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
  86. #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
  87. #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
  88. #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
  89. #ifndef MIN
  90. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  91. #endif
  92. #define ROR64c(x, y) \
  93. ( ((((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) (y) & CONST64(63))) | \
  94. ((x) << ((u64) (64 - ((y) & CONST64(63)))))) & \
  95. CONST64(0xFFFFFFFFFFFFFFFF))
  96. /* compress 1024-bits */
  97. static int sha512_compress(struct sha512_state *md, unsigned char *buf)
  98. {
  99. u64 S[8], W[80], t0, t1;
  100. int i;
  101. /* copy state into S */
  102. for (i = 0; i < 8; i++) {
  103. S[i] = md->state[i];
  104. }
  105. /* copy the state into 1024-bits into W[0..15] */
  106. for (i = 0; i < 16; i++)
  107. W[i] = WPA_GET_BE64(buf + (8 * i));
  108. /* fill W[16..79] */
  109. for (i = 16; i < 80; i++) {
  110. W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
  111. W[i - 16];
  112. }
  113. /* Compress */
  114. for (i = 0; i < 80; i++) {
  115. t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
  116. t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
  117. S[7] = S[6];
  118. S[6] = S[5];
  119. S[5] = S[4];
  120. S[4] = S[3] + t0;
  121. S[3] = S[2];
  122. S[2] = S[1];
  123. S[1] = S[0];
  124. S[0] = t0 + t1;
  125. }
  126. /* feedback */
  127. for (i = 0; i < 8; i++) {
  128. md->state[i] = md->state[i] + S[i];
  129. }
  130. return 0;
  131. }
  132. /**
  133. Initialize the hash state
  134. @param md The hash state you wish to initialize
  135. @return CRYPT_OK if successful
  136. */
  137. void sha512_init(struct sha512_state *md)
  138. {
  139. md->curlen = 0;
  140. md->length = 0;
  141. md->state[0] = CONST64(0x6a09e667f3bcc908);
  142. md->state[1] = CONST64(0xbb67ae8584caa73b);
  143. md->state[2] = CONST64(0x3c6ef372fe94f82b);
  144. md->state[3] = CONST64(0xa54ff53a5f1d36f1);
  145. md->state[4] = CONST64(0x510e527fade682d1);
  146. md->state[5] = CONST64(0x9b05688c2b3e6c1f);
  147. md->state[6] = CONST64(0x1f83d9abfb41bd6b);
  148. md->state[7] = CONST64(0x5be0cd19137e2179);
  149. }
  150. /**
  151. Process a block of memory though the hash
  152. @param md The hash state
  153. @param in The data to hash
  154. @param inlen The length of the data (octets)
  155. @return CRYPT_OK if successful
  156. */
  157. int sha512_process(struct sha512_state *md, const unsigned char *in,
  158. unsigned long inlen)
  159. {
  160. unsigned long n;
  161. if (md->curlen >= sizeof(md->buf))
  162. return -1;
  163. while (inlen > 0) {
  164. if (md->curlen == 0 && inlen >= SHA512_BLOCK_SIZE) {
  165. if (sha512_compress(md, (unsigned char *) in) < 0)
  166. return -1;
  167. md->length += SHA512_BLOCK_SIZE * 8;
  168. in += SHA512_BLOCK_SIZE;
  169. inlen -= SHA512_BLOCK_SIZE;
  170. } else {
  171. n = MIN(inlen, (SHA512_BLOCK_SIZE - md->curlen));
  172. os_memcpy(md->buf + md->curlen, in, n);
  173. md->curlen += n;
  174. in += n;
  175. inlen -= n;
  176. if (md->curlen == SHA512_BLOCK_SIZE) {
  177. if (sha512_compress(md, md->buf) < 0)
  178. return -1;
  179. md->length += 8 * SHA512_BLOCK_SIZE;
  180. md->curlen = 0;
  181. }
  182. }
  183. }
  184. return 0;
  185. }
  186. /**
  187. Terminate the hash to get the digest
  188. @param md The hash state
  189. @param out [out] The destination of the hash (64 bytes)
  190. @return CRYPT_OK if successful
  191. */
  192. int sha512_done(struct sha512_state *md, unsigned char *out)
  193. {
  194. int i;
  195. if (md->curlen >= sizeof(md->buf))
  196. return -1;
  197. /* increase the length of the message */
  198. md->length += md->curlen * CONST64(8);
  199. /* append the '1' bit */
  200. md->buf[md->curlen++] = (unsigned char) 0x80;
  201. /* if the length is currently above 112 bytes we append zeros
  202. * then compress. Then we can fall back to padding zeros and length
  203. * encoding like normal.
  204. */
  205. if (md->curlen > 112) {
  206. while (md->curlen < 128) {
  207. md->buf[md->curlen++] = (unsigned char) 0;
  208. }
  209. sha512_compress(md, md->buf);
  210. md->curlen = 0;
  211. }
  212. /* pad up to 120 bytes of zeroes
  213. * note: that from 112 to 120 is the 64 MSB of the length. We assume
  214. * that you won't hash > 2^64 bits of data... :-)
  215. */
  216. while (md->curlen < 120) {
  217. md->buf[md->curlen++] = (unsigned char) 0;
  218. }
  219. /* store length */
  220. WPA_PUT_BE64(md->buf + 120, md->length);
  221. sha512_compress(md, md->buf);
  222. /* copy output */
  223. for (i = 0; i < 8; i++)
  224. WPA_PUT_BE64(out + (8 * i), md->state[i]);
  225. return 0;
  226. }
  227. /* ===== end - public domain SHA512 implementation ===== */