Sha256.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Crypto/Sha256.c -- SHA-256 Hash
  2. 2017-04-03 : Igor Pavlov : Public domain
  3. This code is based on public domain code from Wei Dai's Crypto++ library. */
  4. #include "Precomp.h"
  5. #include <string.h>
  6. #include "CpuArch.h"
  7. #include "RotateDefs.h"
  8. #include "Sha256.h"
  9. /* define it for speed optimization */
  10. #ifndef _SFX
  11. #define _SHA256_UNROLL
  12. #define _SHA256_UNROLL2
  13. #endif
  14. /* #define _SHA256_UNROLL2 */
  15. void Sha256_Init(CSha256 *p)
  16. {
  17. p->state[0] = 0x6a09e667;
  18. p->state[1] = 0xbb67ae85;
  19. p->state[2] = 0x3c6ef372;
  20. p->state[3] = 0xa54ff53a;
  21. p->state[4] = 0x510e527f;
  22. p->state[5] = 0x9b05688c;
  23. p->state[6] = 0x1f83d9ab;
  24. p->state[7] = 0x5be0cd19;
  25. p->count = 0;
  26. }
  27. #define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22))
  28. #define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25))
  29. #define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3))
  30. #define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10))
  31. #define blk0(i) (W[i])
  32. #define blk2(i) (W[i] += s1(W[((i)-2)&15]) + W[((i)-7)&15] + s0(W[((i)-15)&15]))
  33. #define Ch(x,y,z) (z^(x&(y^z)))
  34. #define Maj(x,y,z) ((x&y)|(z&(x|y)))
  35. #ifdef _SHA256_UNROLL2
  36. #define R(a,b,c,d,e,f,g,h, i) \
  37. h += S1(e) + Ch(e,f,g) + K[(i)+(size_t)(j)] + (j ? blk2(i) : blk0(i)); \
  38. d += h; \
  39. h += S0(a) + Maj(a, b, c)
  40. #define RX_8(i) \
  41. R(a,b,c,d,e,f,g,h, i); \
  42. R(h,a,b,c,d,e,f,g, i+1); \
  43. R(g,h,a,b,c,d,e,f, i+2); \
  44. R(f,g,h,a,b,c,d,e, i+3); \
  45. R(e,f,g,h,a,b,c,d, i+4); \
  46. R(d,e,f,g,h,a,b,c, i+5); \
  47. R(c,d,e,f,g,h,a,b, i+6); \
  48. R(b,c,d,e,f,g,h,a, i+7)
  49. #define RX_16 RX_8(0); RX_8(8);
  50. #else
  51. #define a(i) T[(0-(i))&7]
  52. #define b(i) T[(1-(i))&7]
  53. #define c(i) T[(2-(i))&7]
  54. #define d(i) T[(3-(i))&7]
  55. #define e(i) T[(4-(i))&7]
  56. #define f(i) T[(5-(i))&7]
  57. #define g(i) T[(6-(i))&7]
  58. #define h(i) T[(7-(i))&7]
  59. #define R(i) \
  60. h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[(i)+(size_t)(j)] + (j ? blk2(i) : blk0(i)); \
  61. d(i) += h(i); \
  62. h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) \
  63. #ifdef _SHA256_UNROLL
  64. #define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7);
  65. #define RX_16 RX_8(0); RX_8(8);
  66. #else
  67. #define RX_16 unsigned i; for (i = 0; i < 16; i++) { R(i); }
  68. #endif
  69. #endif
  70. static const UInt32 K[64] = {
  71. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  72. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  73. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  74. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  75. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  76. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  77. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  78. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  79. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  80. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  81. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  82. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  83. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  84. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  85. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  86. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  87. };
  88. static void Sha256_WriteByteBlock(CSha256 *p)
  89. {
  90. UInt32 W[16];
  91. unsigned j;
  92. UInt32 *state;
  93. #ifdef _SHA256_UNROLL2
  94. UInt32 a,b,c,d,e,f,g,h;
  95. #else
  96. UInt32 T[8];
  97. #endif
  98. for (j = 0; j < 16; j += 4)
  99. {
  100. const Byte *ccc = p->buffer + j * 4;
  101. W[j ] = GetBe32(ccc);
  102. W[j + 1] = GetBe32(ccc + 4);
  103. W[j + 2] = GetBe32(ccc + 8);
  104. W[j + 3] = GetBe32(ccc + 12);
  105. }
  106. state = p->state;
  107. #ifdef _SHA256_UNROLL2
  108. a = state[0];
  109. b = state[1];
  110. c = state[2];
  111. d = state[3];
  112. e = state[4];
  113. f = state[5];
  114. g = state[6];
  115. h = state[7];
  116. #else
  117. for (j = 0; j < 8; j++)
  118. T[j] = state[j];
  119. #endif
  120. for (j = 0; j < 64; j += 16)
  121. {
  122. RX_16
  123. }
  124. #ifdef _SHA256_UNROLL2
  125. state[0] += a;
  126. state[1] += b;
  127. state[2] += c;
  128. state[3] += d;
  129. state[4] += e;
  130. state[5] += f;
  131. state[6] += g;
  132. state[7] += h;
  133. #else
  134. for (j = 0; j < 8; j++)
  135. state[j] += T[j];
  136. #endif
  137. /* Wipe variables */
  138. /* memset(W, 0, sizeof(W)); */
  139. /* memset(T, 0, sizeof(T)); */
  140. }
  141. #undef S0
  142. #undef S1
  143. #undef s0
  144. #undef s1
  145. void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
  146. {
  147. if (size == 0)
  148. return;
  149. {
  150. unsigned pos = (unsigned)p->count & 0x3F;
  151. unsigned num;
  152. p->count += size;
  153. num = 64 - pos;
  154. if (num > size)
  155. {
  156. memcpy(p->buffer + pos, data, size);
  157. return;
  158. }
  159. size -= num;
  160. memcpy(p->buffer + pos, data, num);
  161. data += num;
  162. }
  163. for (;;)
  164. {
  165. Sha256_WriteByteBlock(p);
  166. if (size < 64)
  167. break;
  168. size -= 64;
  169. memcpy(p->buffer, data, 64);
  170. data += 64;
  171. }
  172. if (size != 0)
  173. memcpy(p->buffer, data, size);
  174. }
  175. void Sha256_Final(CSha256 *p, Byte *digest)
  176. {
  177. unsigned pos = (unsigned)p->count & 0x3F;
  178. unsigned i;
  179. p->buffer[pos++] = 0x80;
  180. while (pos != (64 - 8))
  181. {
  182. pos &= 0x3F;
  183. if (pos == 0)
  184. Sha256_WriteByteBlock(p);
  185. p->buffer[pos++] = 0;
  186. }
  187. {
  188. UInt64 numBits = (p->count << 3);
  189. SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32));
  190. SetBe32(p->buffer + 64 - 4, (UInt32)(numBits));
  191. }
  192. Sha256_WriteByteBlock(p);
  193. for (i = 0; i < 8; i += 2)
  194. {
  195. UInt32 v0 = p->state[i];
  196. UInt32 v1 = p->state[i + 1];
  197. SetBe32(digest , v0);
  198. SetBe32(digest + 4, v1);
  199. digest += 8;
  200. }
  201. Sha256_Init(p);
  202. }