Sha256Digest.cs 15 KB

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