Sha224Digest.cs 7.9 KB

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