Sha224Digest.cs 7.0 KB

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