Sha1Digest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349.
  9. *
  10. * It is interesting to ponder why the, apart from the extra IV, the other difference here from MD5
  11. * is the "endianness" of the word processing!
  12. */
  13. public class Sha1Digest
  14. : GeneralDigest
  15. {
  16. private const int DigestLength = 20;
  17. private uint H1, H2, H3, H4, H5;
  18. private uint[] X = new uint[80];
  19. private int xOff;
  20. public Sha1Digest()
  21. {
  22. Reset();
  23. }
  24. /**
  25. * Copy constructor. This will copy the state of the provided
  26. * message digest.
  27. */
  28. public Sha1Digest(Sha1Digest t)
  29. : base(t)
  30. {
  31. CopyIn(t);
  32. }
  33. private void CopyIn(Sha1Digest t)
  34. {
  35. base.CopyIn(t);
  36. H1 = t.H1;
  37. H2 = t.H2;
  38. H3 = t.H3;
  39. H4 = t.H4;
  40. H5 = t.H5;
  41. Array.Copy(t.X, 0, X, 0, t.X.Length);
  42. xOff = t.xOff;
  43. }
  44. public override string AlgorithmName
  45. {
  46. get { return "SHA-1"; }
  47. }
  48. public override int GetDigestSize()
  49. {
  50. return DigestLength;
  51. }
  52. internal override void ProcessWord(
  53. byte[] input,
  54. int inOff)
  55. {
  56. X[xOff] = Pack.BE_To_UInt32(input, inOff);
  57. if (++xOff == 16)
  58. {
  59. ProcessBlock();
  60. }
  61. }
  62. internal override void ProcessLength(long bitLength)
  63. {
  64. if (xOff > 14)
  65. {
  66. ProcessBlock();
  67. }
  68. X[14] = (uint)((ulong)bitLength >> 32);
  69. X[15] = (uint)((ulong)bitLength);
  70. }
  71. public override int DoFinal(
  72. byte[] output,
  73. int outOff)
  74. {
  75. Finish();
  76. Pack.UInt32_To_BE(H1, output, outOff);
  77. Pack.UInt32_To_BE(H2, output, outOff + 4);
  78. Pack.UInt32_To_BE(H3, output, outOff + 8);
  79. Pack.UInt32_To_BE(H4, output, outOff + 12);
  80. Pack.UInt32_To_BE(H5, output, outOff + 16);
  81. Reset();
  82. return DigestLength;
  83. }
  84. /**
  85. * reset the chaining variables
  86. */
  87. public override void Reset()
  88. {
  89. base.Reset();
  90. H1 = 0x67452301;
  91. H2 = 0xefcdab89;
  92. H3 = 0x98badcfe;
  93. H4 = 0x10325476;
  94. H5 = 0xc3d2e1f0;
  95. xOff = 0;
  96. Array.Clear(X, 0, X.Length);
  97. }
  98. //
  99. // Additive constants
  100. //
  101. private const uint Y1 = 0x5a827999;
  102. private const uint Y2 = 0x6ed9eba1;
  103. private const uint Y3 = 0x8f1bbcdc;
  104. private const uint Y4 = 0xca62c1d6;
  105. private static uint F(uint u, uint v, uint w)
  106. {
  107. return (u & v) | (~u & w);
  108. }
  109. private static uint H(uint u, uint v, uint w)
  110. {
  111. return u ^ v ^ w;
  112. }
  113. private static uint G(uint u, uint v, uint w)
  114. {
  115. return (u & v) | (u & w) | (v & w);
  116. }
  117. internal override void ProcessBlock()
  118. {
  119. //
  120. // expand 16 word block into 80 word block.
  121. //
  122. for (int i = 16; i < 80; i++)
  123. {
  124. uint t = X[i - 3] ^ X[i - 8] ^ X[i - 14] ^ X[i - 16];
  125. X[i] = t << 1 | t >> 31;
  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. //
  136. // round 1
  137. //
  138. int idx = 0;
  139. for (int j = 0; j < 4; j++)
  140. {
  141. // E = rotateLeft(A, 5) + F(B, C, D) + E + X[idx++] + Y1
  142. // B = rotateLeft(B, 30)
  143. E += (A << 5 | (A >> 27)) + F(B, C, D) + X[idx++] + Y1;
  144. B = B << 30 | (B >> 2);
  145. D += (E << 5 | (E >> 27)) + F(A, B, C) + X[idx++] + Y1;
  146. A = A << 30 | (A >> 2);
  147. C += (D << 5 | (D >> 27)) + F(E, A, B) + X[idx++] + Y1;
  148. E = E << 30 | (E >> 2);
  149. B += (C << 5 | (C >> 27)) + F(D, E, A) + X[idx++] + Y1;
  150. D = D << 30 | (D >> 2);
  151. A += (B << 5 | (B >> 27)) + F(C, D, E) + X[idx++] + Y1;
  152. C = C << 30 | (C >> 2);
  153. }
  154. //
  155. // round 2
  156. //
  157. for (int j = 0; j < 4; j++)
  158. {
  159. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y2
  160. // B = rotateLeft(B, 30)
  161. E += (A << 5 | (A >> 27)) + H(B, C, D) + X[idx++] + Y2;
  162. B = B << 30 | (B >> 2);
  163. D += (E << 5 | (E >> 27)) + H(A, B, C) + X[idx++] + Y2;
  164. A = A << 30 | (A >> 2);
  165. C += (D << 5 | (D >> 27)) + H(E, A, B) + X[idx++] + Y2;
  166. E = E << 30 | (E >> 2);
  167. B += (C << 5 | (C >> 27)) + H(D, E, A) + X[idx++] + Y2;
  168. D = D << 30 | (D >> 2);
  169. A += (B << 5 | (B >> 27)) + H(C, D, E) + X[idx++] + Y2;
  170. C = C << 30 | (C >> 2);
  171. }
  172. //
  173. // round 3
  174. //
  175. for (int j = 0; j < 4; j++)
  176. {
  177. // E = rotateLeft(A, 5) + G(B, C, D) + E + X[idx++] + Y3
  178. // B = rotateLeft(B, 30)
  179. E += (A << 5 | (A >> 27)) + G(B, C, D) + X[idx++] + Y3;
  180. B = B << 30 | (B >> 2);
  181. D += (E << 5 | (E >> 27)) + G(A, B, C) + X[idx++] + Y3;
  182. A = A << 30 | (A >> 2);
  183. C += (D << 5 | (D >> 27)) + G(E, A, B) + X[idx++] + Y3;
  184. E = E << 30 | (E >> 2);
  185. B += (C << 5 | (C >> 27)) + G(D, E, A) + X[idx++] + Y3;
  186. D = D << 30 | (D >> 2);
  187. A += (B << 5 | (B >> 27)) + G(C, D, E) + X[idx++] + Y3;
  188. C = C << 30 | (C >> 2);
  189. }
  190. //
  191. // round 4
  192. //
  193. for (int j = 0; j < 4; j++)
  194. {
  195. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y4
  196. // B = rotateLeft(B, 30)
  197. E += (A << 5 | (A >> 27)) + H(B, C, D) + X[idx++] + Y4;
  198. B = B << 30 | (B >> 2);
  199. D += (E << 5 | (E >> 27)) + H(A, B, C) + X[idx++] + Y4;
  200. A = A << 30 | (A >> 2);
  201. C += (D << 5 | (D >> 27)) + H(E, A, B) + X[idx++] + Y4;
  202. E = E << 30 | (E >> 2);
  203. B += (C << 5 | (C >> 27)) + H(D, E, A) + X[idx++] + Y4;
  204. D = D << 30 | (D >> 2);
  205. A += (B << 5 | (B >> 27)) + H(C, D, E) + X[idx++] + Y4;
  206. C = C << 30 | (C >> 2);
  207. }
  208. H1 += A;
  209. H2 += B;
  210. H3 += C;
  211. H4 += D;
  212. H5 += E;
  213. //
  214. // reset start of the buffer.
  215. //
  216. xOff = 0;
  217. Array.Clear(X, 0, 16);
  218. }
  219. public override IMemoable Copy()
  220. {
  221. return new Sha1Digest(this);
  222. }
  223. public override void Reset(IMemoable other)
  224. {
  225. Sha1Digest d = (Sha1Digest)other;
  226. CopyIn(d);
  227. }
  228. }
  229. }
  230. #endif