sha256-internal.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * SHA-256 hash implementation and interface functions
  3. * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
  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 "sha256.h"
  11. #include "sha256_i.h"
  12. #include "crypto.h"
  13. /**
  14. * sha256_vector - SHA256 hash for data vector
  15. * @num_elem: Number of elements in the data vector
  16. * @addr: Pointers to the data areas
  17. * @len: Lengths of the data blocks
  18. * @mac: Buffer for the hash
  19. * Returns: 0 on success, -1 of failure
  20. */
  21. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  22. u8 *mac)
  23. {
  24. struct sha256_state ctx;
  25. size_t i;
  26. sha256_init(&ctx);
  27. for (i = 0; i < num_elem; i++)
  28. if (sha256_process(&ctx, addr[i], len[i]))
  29. return -1;
  30. if (sha256_done(&ctx, mac))
  31. return -1;
  32. return 0;
  33. }
  34. /* ===== start - public domain SHA256 implementation ===== */
  35. /* This is based on SHA256 implementation in LibTomCrypt that was released into
  36. * public domain by Tom St Denis. */
  37. /* the K array */
  38. static const unsigned long K[64] = {
  39. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
  40. 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
  41. 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
  42. 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  43. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
  44. 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
  45. 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
  46. 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  47. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
  48. 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
  49. 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
  50. 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  51. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  52. };
  53. /* Various logical functions */
  54. #define RORc(x, y) \
  55. ( ((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \
  56. ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
  57. #define Ch(x,y,z) (z ^ (x & (y ^ z)))
  58. #define Maj(x,y,z) (((x | y) & z) | (x & y))
  59. #define S(x, n) RORc((x), (n))
  60. #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
  61. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  62. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  63. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  64. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  65. #ifndef MIN
  66. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  67. #endif
  68. /* compress 512-bits */
  69. static int sha256_compress(struct sha256_state *md, unsigned char *buf)
  70. {
  71. u32 S[8], W[64], t0, t1;
  72. u32 t;
  73. int i;
  74. /* copy state into S */
  75. for (i = 0; i < 8; i++) {
  76. S[i] = md->state[i];
  77. }
  78. /* copy the state into 512-bits into W[0..15] */
  79. for (i = 0; i < 16; i++)
  80. W[i] = WPA_GET_BE32(buf + (4 * i));
  81. /* fill W[16..63] */
  82. for (i = 16; i < 64; i++) {
  83. W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
  84. W[i - 16];
  85. }
  86. /* Compress */
  87. #define RND(a,b,c,d,e,f,g,h,i) \
  88. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  89. t1 = Sigma0(a) + Maj(a, b, c); \
  90. d += t0; \
  91. h = t0 + t1;
  92. for (i = 0; i < 64; ++i) {
  93. RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
  94. t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
  95. S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
  96. }
  97. /* feedback */
  98. for (i = 0; i < 8; i++) {
  99. md->state[i] = md->state[i] + S[i];
  100. }
  101. return 0;
  102. }
  103. /* Initialize the hash state */
  104. void sha256_init(struct sha256_state *md)
  105. {
  106. md->curlen = 0;
  107. md->length = 0;
  108. md->state[0] = 0x6A09E667UL;
  109. md->state[1] = 0xBB67AE85UL;
  110. md->state[2] = 0x3C6EF372UL;
  111. md->state[3] = 0xA54FF53AUL;
  112. md->state[4] = 0x510E527FUL;
  113. md->state[5] = 0x9B05688CUL;
  114. md->state[6] = 0x1F83D9ABUL;
  115. md->state[7] = 0x5BE0CD19UL;
  116. }
  117. /**
  118. Process a block of memory though the hash
  119. @param md The hash state
  120. @param in The data to hash
  121. @param inlen The length of the data (octets)
  122. @return CRYPT_OK if successful
  123. */
  124. int sha256_process(struct sha256_state *md, const unsigned char *in,
  125. unsigned long inlen)
  126. {
  127. unsigned long n;
  128. if (md->curlen >= sizeof(md->buf))
  129. return -1;
  130. while (inlen > 0) {
  131. if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) {
  132. if (sha256_compress(md, (unsigned char *) in) < 0)
  133. return -1;
  134. md->length += SHA256_BLOCK_SIZE * 8;
  135. in += SHA256_BLOCK_SIZE;
  136. inlen -= SHA256_BLOCK_SIZE;
  137. } else {
  138. n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen));
  139. os_memcpy(md->buf + md->curlen, in, n);
  140. md->curlen += n;
  141. in += n;
  142. inlen -= n;
  143. if (md->curlen == SHA256_BLOCK_SIZE) {
  144. if (sha256_compress(md, md->buf) < 0)
  145. return -1;
  146. md->length += 8 * SHA256_BLOCK_SIZE;
  147. md->curlen = 0;
  148. }
  149. }
  150. }
  151. return 0;
  152. }
  153. /**
  154. Terminate the hash to get the digest
  155. @param md The hash state
  156. @param out [out] The destination of the hash (32 bytes)
  157. @return CRYPT_OK if successful
  158. */
  159. int sha256_done(struct sha256_state *md, unsigned char *out)
  160. {
  161. int i;
  162. if (md->curlen >= sizeof(md->buf))
  163. return -1;
  164. /* increase the length of the message */
  165. md->length += md->curlen * 8;
  166. /* append the '1' bit */
  167. md->buf[md->curlen++] = (unsigned char) 0x80;
  168. /* if the length is currently above 56 bytes we append zeros
  169. * then compress. Then we can fall back to padding zeros and length
  170. * encoding like normal.
  171. */
  172. if (md->curlen > 56) {
  173. while (md->curlen < SHA256_BLOCK_SIZE) {
  174. md->buf[md->curlen++] = (unsigned char) 0;
  175. }
  176. sha256_compress(md, md->buf);
  177. md->curlen = 0;
  178. }
  179. /* pad up to 56 bytes of zeroes */
  180. while (md->curlen < 56) {
  181. md->buf[md->curlen++] = (unsigned char) 0;
  182. }
  183. /* store length */
  184. WPA_PUT_BE64(md->buf + 56, md->length);
  185. sha256_compress(md, md->buf);
  186. /* copy output */
  187. for (i = 0; i < 8; i++)
  188. WPA_PUT_BE32(out + (4 * i), md->state[i]);
  189. return 0;
  190. }
  191. /* ===== end - public domain SHA256 implementation ===== */