Sha256Digest.cs 8.0 KB

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