sha256.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #include <string.h>
  15. #include "sha2.h"
  16. #include "blockwise.h"
  17. #include "bitops.h"
  18. #include "handy.h"
  19. #include "tassert.h"
  20. static const uint32_t K[64] = {
  21. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  22. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  23. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  24. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  25. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  26. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  27. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  28. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  29. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  30. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  31. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  32. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  33. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  34. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  35. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  36. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  37. };
  38. # define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  39. # define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  40. # define BSIG0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
  41. # define BSIG1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
  42. # define SSIG0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
  43. # define SSIG1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
  44. void cf_sha256_init(cf_sha256_context *ctx)
  45. {
  46. memset(ctx, 0, sizeof *ctx);
  47. ctx->H[0] = 0x6a09e667;
  48. ctx->H[1] = 0xbb67ae85;
  49. ctx->H[2] = 0x3c6ef372;
  50. ctx->H[3] = 0xa54ff53a;
  51. ctx->H[4] = 0x510e527f;
  52. ctx->H[5] = 0x9b05688c;
  53. ctx->H[6] = 0x1f83d9ab;
  54. ctx->H[7] = 0x5be0cd19;
  55. }
  56. void cf_sha224_init(cf_sha256_context *ctx)
  57. {
  58. memset(ctx, 0, sizeof *ctx);
  59. ctx->H[0] = 0xc1059ed8;
  60. ctx->H[1] = 0x367cd507;
  61. ctx->H[2] = 0x3070dd17;
  62. ctx->H[3] = 0xf70e5939;
  63. ctx->H[4] = 0xffc00b31;
  64. ctx->H[5] = 0x68581511;
  65. ctx->H[6] = 0x64f98fa7;
  66. ctx->H[7] = 0xbefa4fa4;
  67. }
  68. static void sha256_update_block(void *vctx, const uint8_t *inp)
  69. {
  70. cf_sha256_context *ctx = vctx;
  71. /* This is a 16-word window into the whole W array. */
  72. uint32_t W[16];
  73. uint32_t a = ctx->H[0],
  74. b = ctx->H[1],
  75. c = ctx->H[2],
  76. d = ctx->H[3],
  77. e = ctx->H[4],
  78. f = ctx->H[5],
  79. g = ctx->H[6],
  80. h = ctx->H[7],
  81. Wt;
  82. size_t t;
  83. for (t = 0; t < 64; t++)
  84. {
  85. /* For W[0..16] we process the input into W.
  86. * For W[16..64] we compute the next W value:
  87. *
  88. * W[t] = SSIG1(W[t - 2]) + W[t - 7] + SSIG0(W[t - 15]) + W[t - 16];
  89. *
  90. * But all W indices are reduced mod 16 into our window.
  91. */
  92. if (t < 16)
  93. {
  94. W[t] = Wt = read32_be(inp);
  95. inp += 4;
  96. } else {
  97. Wt = SSIG1(W[(t - 2) % 16]) +
  98. W[(t - 7) % 16] +
  99. SSIG0(W[(t - 15) % 16]) +
  100. W[(t - 16) % 16];
  101. W[t % 16] = Wt;
  102. }
  103. uint32_t T1 = h + BSIG1(e) + CH(e, f, g) + K[t] + Wt;
  104. uint32_t T2 = BSIG0(a) + MAJ(a, b, c);
  105. h = g;
  106. g = f;
  107. f = e;
  108. e = d + T1;
  109. d = c;
  110. c = b;
  111. b = a;
  112. a = T1 + T2;
  113. }
  114. ctx->H[0] += a;
  115. ctx->H[1] += b;
  116. ctx->H[2] += c;
  117. ctx->H[3] += d;
  118. ctx->H[4] += e;
  119. ctx->H[5] += f;
  120. ctx->H[6] += g;
  121. ctx->H[7] += h;
  122. ctx->blocks++;
  123. }
  124. void cf_sha256_update(cf_sha256_context *ctx, const void *data, size_t nbytes)
  125. {
  126. cf_blockwise_accumulate(ctx->partial, &ctx->npartial, sizeof ctx->partial,
  127. data, nbytes,
  128. sha256_update_block, ctx);
  129. }
  130. void cf_sha224_update(cf_sha256_context *ctx, const void *data, size_t nbytes)
  131. {
  132. cf_sha256_update(ctx, data, nbytes);
  133. }
  134. void cf_sha256_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ])
  135. {
  136. /* We copy the context, so the finalisation doesn't effect the caller's
  137. * context. This means the caller can do:
  138. *
  139. * x = init()
  140. * x.update('hello')
  141. * h1 = x.digest()
  142. * x.update(' world')
  143. * h2 = x.digest()
  144. *
  145. * to get h1 = H('hello') and h2 = H('hello world')
  146. *
  147. * This wouldn't work if we applied MD-padding to *ctx.
  148. */
  149. cf_sha256_context ours = *ctx;
  150. cf_sha256_digest_final(&ours, hash);
  151. }
  152. void cf_sha256_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ])
  153. {
  154. uint64_t digested_bytes = ctx->blocks;
  155. digested_bytes = digested_bytes * CF_SHA256_BLOCKSZ + ctx->npartial;
  156. uint64_t digested_bits = digested_bytes * 8;
  157. size_t padbytes = CF_SHA256_BLOCKSZ - ((digested_bytes + 8) % CF_SHA256_BLOCKSZ);
  158. /* Hash 0x80 00 ... block first. */
  159. cf_blockwise_acc_pad(ctx->partial, &ctx->npartial, sizeof ctx->partial,
  160. 0x80, 0x00, 0x00, padbytes,
  161. sha256_update_block, ctx);
  162. /* Now hash length. */
  163. uint8_t buf[8];
  164. write64_be(digested_bits, buf);
  165. cf_sha256_update(ctx, buf, 8);
  166. /* We ought to have got our padding calculation right! */
  167. assert(ctx->npartial == 0);
  168. write32_be(ctx->H[0], hash + 0);
  169. write32_be(ctx->H[1], hash + 4);
  170. write32_be(ctx->H[2], hash + 8);
  171. write32_be(ctx->H[3], hash + 12);
  172. write32_be(ctx->H[4], hash + 16);
  173. write32_be(ctx->H[5], hash + 20);
  174. write32_be(ctx->H[6], hash + 24);
  175. write32_be(ctx->H[7], hash + 28);
  176. memset(ctx, 0, sizeof *ctx);
  177. }
  178. void cf_sha224_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ])
  179. {
  180. uint8_t full[CF_SHA256_HASHSZ];
  181. cf_sha256_digest(ctx, full);
  182. memcpy(hash, full, CF_SHA224_HASHSZ);
  183. }
  184. void cf_sha224_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ])
  185. {
  186. uint8_t full[CF_SHA256_HASHSZ];
  187. cf_sha256_digest_final(ctx, full);
  188. memcpy(hash, full, CF_SHA224_HASHSZ);
  189. }
  190. const cf_chash cf_sha224 = {
  191. .hashsz = CF_SHA224_HASHSZ,
  192. .blocksz = CF_SHA256_BLOCKSZ,
  193. .init = (cf_chash_init) cf_sha224_init,
  194. .update = (cf_chash_update) cf_sha224_update,
  195. .digest = (cf_chash_digest) cf_sha224_digest
  196. };
  197. const cf_chash cf_sha256 = {
  198. .hashsz = CF_SHA256_HASHSZ,
  199. .blocksz = CF_SHA256_BLOCKSZ,
  200. .init = (cf_chash_init) cf_sha256_init,
  201. .update = (cf_chash_update) cf_sha256_update,
  202. .digest = (cf_chash_digest) cf_sha256_digest
  203. };