hmacsha256.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "hmacsha256.h"
  2. #include <string.h>
  3. static uint32_t ror(uint32_t n, uint32_t k)
  4. {
  5. return (n >> k) | (n << (32 - k));
  6. }
  7. #define ROR(n,k) ror(n,k)
  8. #define CH(x,y,z) (z ^ (x & (y ^ z)))
  9. #define MAJ(x,y,z) ((x & y) | (z & (x | y)))
  10. #define S0(x) (ROR(x, 2) ^ ROR(x,13) ^ ROR(x,22))
  11. #define S1(x) (ROR(x, 6) ^ ROR(x,11) ^ ROR(x,25))
  12. #define R0(x) (ROR(x, 7) ^ ROR(x,18) ^ (x>>3))
  13. #define R1(x) (ROR(x,17) ^ ROR(x,19) ^ (x>>10))
  14. static const uint32_t K[64] =
  15. {
  16. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  17. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  18. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  19. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  20. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  21. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  22. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  23. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  24. };
  25. static void sha256_transform(SHA256_CTX* s, const uint8_t* buf)
  26. {
  27. uint32_t t1, t2, a, b, c, d, e, f, g, h, m[64];
  28. uint32_t i, j;
  29. for (i = 0, j = 0; i < 16; i++, j += 4)
  30. {
  31. m[i] = (uint32_t)buf[j] << 24 | (uint32_t)buf[j + 1] << 16 |
  32. (uint32_t)buf[j + 2] << 8 | (uint32_t)buf[j + 3];
  33. }
  34. for (; i < 64; i++)
  35. {
  36. m[i] = R1(m[i - 2]) + m[i - 7] + R0(m[i - 15]) + m[i - 16];
  37. }
  38. a = s->h[0];
  39. b = s->h[1];
  40. c = s->h[2];
  41. d = s->h[3];
  42. e = s->h[4];
  43. f = s->h[5];
  44. g = s->h[6];
  45. h = s->h[7];
  46. for (i = 0; i < 64; i++)
  47. {
  48. t1 = h + S1(e) + CH(e, f, g) + K[i] + m[i];
  49. t2 = S0(a) + MAJ(a, b, c);
  50. h = g;
  51. g = f;
  52. f = e;
  53. e = d + t1;
  54. d = c;
  55. c = b;
  56. b = a;
  57. a = t1 + t2;
  58. }
  59. s->h[0] += a;
  60. s->h[1] += b;
  61. s->h[2] += c;
  62. s->h[3] += d;
  63. s->h[4] += e;
  64. s->h[5] += f;
  65. s->h[6] += g;
  66. s->h[7] += h;
  67. }
  68. void sha256_init(SHA256_CTX* s)
  69. {
  70. s->len = 0;
  71. s->h[0] = 0x6a09e667;
  72. s->h[1] = 0xbb67ae85;
  73. s->h[2] = 0x3c6ef372;
  74. s->h[3] = 0xa54ff53a;
  75. s->h[4] = 0x510e527f;
  76. s->h[5] = 0x9b05688c;
  77. s->h[6] = 0x1f83d9ab;
  78. s->h[7] = 0x5be0cd19;
  79. }
  80. void sha256_final(SHA256_CTX* s, uint8_t* md)
  81. {
  82. uint32_t r = s->len % SHA256_BLOCKLEN;
  83. int i;
  84. //pad
  85. s->buf[r++] = 0x80;
  86. if (r > 56)
  87. {
  88. memset(s->buf + r, 0, SHA256_BLOCKLEN - r);
  89. r = 0;
  90. sha256_transform(s, s->buf);
  91. }
  92. memset(s->buf + r, 0, 56 - r);
  93. s->len *= 8;
  94. s->buf[56] = s->len >> 56;
  95. s->buf[57] = s->len >> 48;
  96. s->buf[58] = s->len >> 40;
  97. s->buf[59] = s->len >> 32;
  98. s->buf[60] = s->len >> 24;
  99. s->buf[61] = s->len >> 16;
  100. s->buf[62] = s->len >> 8;
  101. s->buf[63] = s->len;
  102. sha256_transform(s, s->buf);
  103. for (i = 0; i < SHA256_DIGESTINT; i++)
  104. {
  105. md[4 * i] = s->h[i] >> 24;
  106. md[4 * i + 1] = s->h[i] >> 16;
  107. md[4 * i + 2] = s->h[i] >> 8;
  108. md[4 * i + 3] = s->h[i];
  109. }
  110. sha256_init(s);
  111. }
  112. void sha256_update(SHA256_CTX* s, const uint8_t* m, uint32_t len)
  113. {
  114. const uint8_t* p = m;
  115. uint32_t r = s->len % SHA256_BLOCKLEN;
  116. s->len += len;
  117. if (r)
  118. {
  119. if (len + r < SHA256_BLOCKLEN)
  120. {
  121. memcpy(s->buf + r, p, len);
  122. return;
  123. }
  124. memcpy(s->buf + r, p, SHA256_BLOCKLEN - r);
  125. len -= SHA256_BLOCKLEN - r;
  126. p += SHA256_BLOCKLEN - r;
  127. sha256_transform(s, s->buf);
  128. }
  129. for (; len >= SHA256_BLOCKLEN; len -= SHA256_BLOCKLEN, p += SHA256_BLOCKLEN)
  130. {
  131. sha256_transform(s, p);
  132. }
  133. memcpy(s->buf, p, len);
  134. }
  135. #define INNER_PAD '\x36'
  136. #define OUTER_PAD '\x5c'
  137. void hmac_sha256_init(HMAC_SHA256_CTX* hmac, const uint8_t* key, uint32_t keylen)
  138. {
  139. SHA256_CTX* sha = &hmac->sha;
  140. uint32_t i;
  141. if (keylen <= SHA256_BLOCKLEN)
  142. {
  143. memcpy(hmac->buf, key, keylen);
  144. memset(hmac->buf + keylen, '\0', SHA256_BLOCKLEN - keylen);
  145. }
  146. else
  147. {
  148. sha256_init(sha);
  149. sha256_update(sha, key, keylen);
  150. sha256_final(sha, hmac->buf);
  151. memset(hmac->buf + SHA256_DIGESTLEN, '\0', SHA256_BLOCKLEN - SHA256_DIGESTLEN);
  152. }
  153. for (i = 0; i < SHA256_BLOCKLEN; i++)
  154. {
  155. hmac->buf[i] = hmac->buf[i] ^ OUTER_PAD;
  156. }
  157. sha256_init(sha);
  158. sha256_update(sha, hmac->buf, SHA256_BLOCKLEN);
  159. // copy outer state
  160. memcpy(hmac->h_outer, sha->h, SHA256_DIGESTLEN);
  161. for (i = 0; i < SHA256_BLOCKLEN; i++)
  162. {
  163. hmac->buf[i] = (hmac->buf[i] ^ OUTER_PAD) ^ INNER_PAD;
  164. }
  165. sha256_init(sha);
  166. sha256_update(sha, hmac->buf, SHA256_BLOCKLEN);
  167. // copy inner state
  168. memcpy(hmac->h_inner, sha->h, SHA256_DIGESTLEN);
  169. }
  170. void hmac_sha256_update(HMAC_SHA256_CTX* hmac, const uint8_t* m, uint32_t mlen)
  171. {
  172. sha256_update(&hmac->sha, m, mlen);
  173. }
  174. void hmac_sha256_final(HMAC_SHA256_CTX* hmac, uint8_t* md)
  175. {
  176. SHA256_CTX* sha = &hmac->sha;
  177. sha256_final(sha, md);
  178. // reset sha to outer state
  179. memcpy(sha->h, hmac->h_outer, SHA256_DIGESTLEN);
  180. sha->len = SHA256_BLOCKLEN;
  181. sha256_update(sha, md, SHA256_DIGESTLEN);
  182. sha256_final(sha, md); // md = D(outer || D(inner || msg))
  183. // reset sha to inner state -> reset hmac
  184. memcpy(sha->h, hmac->h_inner, SHA256_DIGESTLEN);
  185. sha->len = SHA256_BLOCKLEN;
  186. }
  187. void pbkdf2_sha256(HMAC_SHA256_CTX* hmac,
  188. const uint8_t* key, uint32_t keylen, const uint8_t* salt, uint32_t saltlen, uint32_t rounds,
  189. uint8_t* dk, uint32_t dklen)
  190. {
  191. uint8_t* U;
  192. uint8_t* T;
  193. uint8_t count[4];
  194. uint32_t i, j, k;
  195. uint32_t len;
  196. uint32_t hlen = SHA256_DIGESTLEN;
  197. uint32_t l = dklen / hlen + ((dklen % hlen) ? 1 : 0);
  198. uint32_t r = dklen - (l - 1) * hlen;
  199. hmac_sha256_init(hmac, key, keylen);
  200. U = hmac->buf;
  201. T = dk;
  202. len = hlen;
  203. for (i = 1; i <= l; i++)
  204. {
  205. if (i == l) { len = r; }
  206. count[0] = (i >> 24) & 0xFF;
  207. count[1] = (i >> 16) & 0xFF;
  208. count[2] = (i >> 8) & 0xFF;
  209. count[3] = (i) & 0xFF;
  210. hmac_sha256_update(hmac, salt, saltlen);
  211. hmac_sha256_update(hmac, count, 4);
  212. hmac_sha256_final(hmac, U);
  213. memcpy(T, U, len);
  214. for (j = 1; j < rounds; j++)
  215. {
  216. hmac_sha256_update(hmac, U, hlen);
  217. hmac_sha256_final(hmac, U);
  218. for (k = 0; k < len; k++)
  219. {
  220. T[k] ^= U[k];
  221. }
  222. }
  223. T += len;
  224. }
  225. }