Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
4.9 KiB

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