Sha256Digest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /**
  9. * Draft FIPS 180-2 implementation of SHA-256. <b>Note:</b> As this is
  10. * based on a draft this implementation is subject to change.
  11. *
  12. * <pre>
  13. * block word digest
  14. * SHA-1 512 32 160
  15. * SHA-256 512 32 256
  16. * SHA-384 1024 64 384
  17. * SHA-512 1024 64 512
  18. * </pre>
  19. */
  20. public class Sha256Digest
  21. : GeneralDigest
  22. {
  23. private const int DigestLength = 32;
  24. private uint H1, H2, H3, H4, H5, H6, H7, H8;
  25. private uint[] X = new uint[64];
  26. private int xOff;
  27. public Sha256Digest()
  28. {
  29. initHs();
  30. }
  31. /**
  32. * Copy constructor. This will copy the state of the provided
  33. * message digest.
  34. */
  35. public Sha256Digest(Sha256Digest t) : base(t)
  36. {
  37. CopyIn(t);
  38. }
  39. private void CopyIn(Sha256Digest t)
  40. {
  41. base.CopyIn(t);
  42. H1 = t.H1;
  43. H2 = t.H2;
  44. H3 = t.H3;
  45. H4 = t.H4;
  46. H5 = t.H5;
  47. H6 = t.H6;
  48. H7 = t.H7;
  49. H8 = t.H8;
  50. Array.Copy(t.X, 0, X, 0, t.X.Length);
  51. xOff = t.xOff;
  52. }
  53. public override string AlgorithmName
  54. {
  55. get { return "SHA-256"; }
  56. }
  57. public override int GetDigestSize()
  58. {
  59. return DigestLength;
  60. }
  61. internal override void ProcessWord(
  62. byte[] input,
  63. int inOff)
  64. {
  65. X[xOff] = Pack.BE_To_UInt32(input, inOff);
  66. if (++xOff == 16)
  67. {
  68. ProcessBlock();
  69. }
  70. }
  71. internal override void ProcessLength(
  72. long bitLength)
  73. {
  74. if (xOff > 14)
  75. {
  76. ProcessBlock();
  77. }
  78. X[14] = (uint)((ulong)bitLength >> 32);
  79. X[15] = (uint)((ulong)bitLength);
  80. }
  81. public override int DoFinal(
  82. byte[] output,
  83. int outOff)
  84. {
  85. Finish();
  86. Pack.UInt32_To_BE((uint)H1, output, outOff);
  87. Pack.UInt32_To_BE((uint)H2, output, outOff + 4);
  88. Pack.UInt32_To_BE((uint)H3, output, outOff + 8);
  89. Pack.UInt32_To_BE((uint)H4, output, outOff + 12);
  90. Pack.UInt32_To_BE((uint)H5, output, outOff + 16);
  91. Pack.UInt32_To_BE((uint)H6, output, outOff + 20);
  92. Pack.UInt32_To_BE((uint)H7, output, outOff + 24);
  93. Pack.UInt32_To_BE((uint)H8, output, outOff + 28);
  94. Reset();
  95. return DigestLength;
  96. }
  97. /**
  98. * reset the chaining variables
  99. */
  100. public override void Reset()
  101. {
  102. base.Reset();
  103. initHs();
  104. xOff = 0;
  105. Array.Clear(X, 0, X.Length);
  106. }
  107. private void initHs()
  108. {
  109. /* SHA-256 initial hash value
  110. * The first 32 bits of the fractional parts of the square roots
  111. * of the first eight prime numbers
  112. */
  113. H1 = 0x6a09e667;
  114. H2 = 0xbb67ae85;
  115. H3 = 0x3c6ef372;
  116. H4 = 0xa54ff53a;
  117. H5 = 0x510e527f;
  118. H6 = 0x9b05688c;
  119. H7 = 0x1f83d9ab;
  120. H8 = 0x5be0cd19;
  121. }
  122. internal override void ProcessBlock()
  123. {
  124. //
  125. // expand 16 word block into 64 word blocks.
  126. //
  127. for (int ti = 16; ti <= 63; ti++)
  128. {
  129. X[ti] = Theta1(X[ti - 2]) + X[ti - 7] + Theta0(X[ti - 15]) + X[ti - 16];
  130. }
  131. //
  132. // set up working variables.
  133. //
  134. uint a = H1;
  135. uint b = H2;
  136. uint c = H3;
  137. uint d = H4;
  138. uint e = H5;
  139. uint f = H6;
  140. uint g = H7;
  141. uint h = H8;
  142. int t = 0;
  143. for(int i = 0; i < 8; ++i)
  144. {
  145. // t = 8 * i
  146. h += Sum1Ch(e, f, g) + K[t] + X[t];
  147. d += h;
  148. h += Sum0Maj(a, b, c);
  149. ++t;
  150. // t = 8 * i + 1
  151. g += Sum1Ch(d, e, f) + K[t] + X[t];
  152. c += g;
  153. g += Sum0Maj(h, a, b);
  154. ++t;
  155. // t = 8 * i + 2
  156. f += Sum1Ch(c, d, e) + K[t] + X[t];
  157. b += f;
  158. f += Sum0Maj(g, h, a);
  159. ++t;
  160. // t = 8 * i + 3
  161. e += Sum1Ch(b, c, d) + K[t] + X[t];
  162. a += e;
  163. e += Sum0Maj(f, g, h);
  164. ++t;
  165. // t = 8 * i + 4
  166. d += Sum1Ch(a, b, c) + K[t] + X[t];
  167. h += d;
  168. d += Sum0Maj(e, f, g);
  169. ++t;
  170. // t = 8 * i + 5
  171. c += Sum1Ch(h, a, b) + K[t] + X[t];
  172. g += c;
  173. c += Sum0Maj(d, e, f);
  174. ++t;
  175. // t = 8 * i + 6
  176. b += Sum1Ch(g, h, a) + K[t] + X[t];
  177. f += b;
  178. b += Sum0Maj(c, d, e);
  179. ++t;
  180. // t = 8 * i + 7
  181. a += Sum1Ch(f, g, h) + K[t] + X[t];
  182. e += a;
  183. a += Sum0Maj(b, c, d);
  184. ++t;
  185. }
  186. H1 += a;
  187. H2 += b;
  188. H3 += c;
  189. H4 += d;
  190. H5 += e;
  191. H6 += f;
  192. H7 += g;
  193. H8 += h;
  194. //
  195. // reset the offset and clean out the word buffer.
  196. //
  197. xOff = 0;
  198. Array.Clear(X, 0, 16);
  199. }
  200. private static uint Sum1Ch(
  201. uint x,
  202. uint y,
  203. uint z)
  204. {
  205. // return Sum1(x) + Ch(x, y, z);
  206. return (((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7)))
  207. + ((x & y) ^ ((~x) & z));
  208. }
  209. private static uint Sum0Maj(
  210. uint x,
  211. uint y,
  212. uint z)
  213. {
  214. // return Sum0(x) + Maj(x, y, z);
  215. return (((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10)))
  216. + ((x & y) ^ (x & z) ^ (y & z));
  217. }
  218. // /* SHA-256 functions */
  219. // private static uint Ch(
  220. // uint x,
  221. // uint y,
  222. // uint z)
  223. // {
  224. // return ((x & y) ^ ((~x) & z));
  225. // }
  226. //
  227. // private static uint Maj(
  228. // uint x,
  229. // uint y,
  230. // uint z)
  231. // {
  232. // return ((x & y) ^ (x & z) ^ (y & z));
  233. // }
  234. //
  235. // private static uint Sum0(
  236. // uint x)
  237. // {
  238. // return ((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10));
  239. // }
  240. //
  241. // private static uint Sum1(
  242. // uint x)
  243. // {
  244. // return ((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7));
  245. // }
  246. private static uint Theta0(
  247. uint x)
  248. {
  249. return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3);
  250. }
  251. private static uint Theta1(
  252. uint x)
  253. {
  254. return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ (x >> 10);
  255. }
  256. /* SHA-256 Constants
  257. * (represent the first 32 bits of the fractional parts of the
  258. * cube roots of the first sixty-four prime numbers)
  259. */
  260. private static readonly uint[] K = {
  261. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  262. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  263. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  264. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  265. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  266. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  267. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  268. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  269. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  270. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  271. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  272. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  273. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  274. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  275. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  276. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  277. };
  278. public override IMemoable Copy()
  279. {
  280. return new Sha256Digest(this);
  281. }
  282. public override void Reset(IMemoable other)
  283. {
  284. Sha256Digest d = (Sha256Digest)other;
  285. CopyIn(d);
  286. }
  287. }
  288. }
  289. #pragma warning restore
  290. #endif