Sha1Digest.cs 7.9 KB

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