Sha256Digest.cs 8.9 KB

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