Sha224Digest.cs 6.9 KB

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