MD4Digest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
  9. * Computer Science and RSA Data Security, Inc.
  10. * <p>
  11. * <b>NOTE</b>: This algorithm is only included for backwards compatibility
  12. * with legacy applications, it's not secure, don't use it for anything new!</p>
  13. */
  14. public class MD4Digest
  15. : GeneralDigest
  16. {
  17. private const int DigestLength = 16;
  18. private int H1, H2, H3, H4; // IV's
  19. private int[] X = new int[16];
  20. private int xOff;
  21. /**
  22. * Standard constructor
  23. */
  24. public MD4Digest()
  25. {
  26. Reset();
  27. }
  28. /**
  29. * Copy constructor. This will copy the state of the provided
  30. * message digest.
  31. */
  32. public MD4Digest(MD4Digest t) : base(t)
  33. {
  34. CopyIn(t);
  35. }
  36. private void CopyIn(MD4Digest t)
  37. {
  38. base.CopyIn(t);
  39. H1 = t.H1;
  40. H2 = t.H2;
  41. H3 = t.H3;
  42. H4 = t.H4;
  43. Array.Copy(t.X, 0, X, 0, t.X.Length);
  44. xOff = t.xOff;
  45. }
  46. public override string AlgorithmName
  47. {
  48. get { return "MD4"; }
  49. }
  50. public override int GetDigestSize()
  51. {
  52. return DigestLength;
  53. }
  54. internal override void ProcessWord(
  55. byte[] input,
  56. int inOff)
  57. {
  58. X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8)
  59. | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24);
  60. if (xOff == 16)
  61. {
  62. ProcessBlock();
  63. }
  64. }
  65. internal override void ProcessLength(
  66. long bitLength)
  67. {
  68. if (xOff > 14)
  69. {
  70. ProcessBlock();
  71. }
  72. X[14] = (int)(bitLength & 0xffffffff);
  73. X[15] = (int)((ulong) bitLength >> 32);
  74. }
  75. private void UnpackWord(
  76. int word,
  77. byte[] outBytes,
  78. int outOff)
  79. {
  80. outBytes[outOff] = (byte)word;
  81. outBytes[outOff + 1] = (byte)((uint) word >> 8);
  82. outBytes[outOff + 2] = (byte)((uint) word >> 16);
  83. outBytes[outOff + 3] = (byte)((uint) word >> 24);
  84. }
  85. public override int DoFinal(
  86. byte[] output,
  87. int outOff)
  88. {
  89. Finish();
  90. UnpackWord(H1, output, outOff);
  91. UnpackWord(H2, output, outOff + 4);
  92. UnpackWord(H3, output, outOff + 8);
  93. UnpackWord(H4, output, outOff + 12);
  94. Reset();
  95. return DigestLength;
  96. }
  97. /**
  98. * reset the chaining variables to the IV values.
  99. */
  100. public override void Reset()
  101. {
  102. base.Reset();
  103. H1 = unchecked((int) 0x67452301);
  104. H2 = unchecked((int) 0xefcdab89);
  105. H3 = unchecked((int) 0x98badcfe);
  106. H4 = unchecked((int) 0x10325476);
  107. xOff = 0;
  108. for (int i = 0; i != X.Length; i++)
  109. {
  110. X[i] = 0;
  111. }
  112. }
  113. //
  114. // round 1 left rotates
  115. //
  116. private const int S11 = 3;
  117. private const int S12 = 7;
  118. private const int S13 = 11;
  119. private const int S14 = 19;
  120. //
  121. // round 2 left rotates
  122. //
  123. private const int S21 = 3;
  124. private const int S22 = 5;
  125. private const int S23 = 9;
  126. private const int S24 = 13;
  127. //
  128. // round 3 left rotates
  129. //
  130. private const int S31 = 3;
  131. private const int S32 = 9;
  132. private const int S33 = 11;
  133. private const int S34 = 15;
  134. /*
  135. * rotate int x left n bits.
  136. */
  137. private int RotateLeft(
  138. int x,
  139. int n)
  140. {
  141. return (x << n) | (int) ((uint) x >> (32 - n));
  142. }
  143. /*
  144. * F, G, H and I are the basic MD4 functions.
  145. */
  146. private int F(
  147. int u,
  148. int v,
  149. int w)
  150. {
  151. return (u & v) | (~u & w);
  152. }
  153. private int G(
  154. int u,
  155. int v,
  156. int w)
  157. {
  158. return (u & v) | (u & w) | (v & w);
  159. }
  160. private int H(
  161. int u,
  162. int v,
  163. int w)
  164. {
  165. return u ^ v ^ w;
  166. }
  167. internal override void ProcessBlock()
  168. {
  169. int a = H1;
  170. int b = H2;
  171. int c = H3;
  172. int d = H4;
  173. //
  174. // Round 1 - F cycle, 16 times.
  175. //
  176. a = RotateLeft((a + F(b, c, d) + X[ 0]), S11);
  177. d = RotateLeft((d + F(a, b, c) + X[ 1]), S12);
  178. c = RotateLeft((c + F(d, a, b) + X[ 2]), S13);
  179. b = RotateLeft((b + F(c, d, a) + X[ 3]), S14);
  180. a = RotateLeft((a + F(b, c, d) + X[ 4]), S11);
  181. d = RotateLeft((d + F(a, b, c) + X[ 5]), S12);
  182. c = RotateLeft((c + F(d, a, b) + X[ 6]), S13);
  183. b = RotateLeft((b + F(c, d, a) + X[ 7]), S14);
  184. a = RotateLeft((a + F(b, c, d) + X[ 8]), S11);
  185. d = RotateLeft((d + F(a, b, c) + X[ 9]), S12);
  186. c = RotateLeft((c + F(d, a, b) + X[10]), S13);
  187. b = RotateLeft((b + F(c, d, a) + X[11]), S14);
  188. a = RotateLeft((a + F(b, c, d) + X[12]), S11);
  189. d = RotateLeft((d + F(a, b, c) + X[13]), S12);
  190. c = RotateLeft((c + F(d, a, b) + X[14]), S13);
  191. b = RotateLeft((b + F(c, d, a) + X[15]), S14);
  192. //
  193. // Round 2 - G cycle, 16 times.
  194. //
  195. a = RotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
  196. d = RotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
  197. c = RotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
  198. b = RotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
  199. a = RotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
  200. d = RotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
  201. c = RotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
  202. b = RotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
  203. a = RotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
  204. d = RotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
  205. c = RotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
  206. b = RotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
  207. a = RotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
  208. d = RotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
  209. c = RotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
  210. b = RotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
  211. //
  212. // Round 3 - H cycle, 16 times.
  213. //
  214. a = RotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
  215. d = RotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
  216. c = RotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
  217. b = RotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
  218. a = RotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
  219. d = RotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
  220. c = RotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
  221. b = RotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
  222. a = RotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
  223. d = RotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
  224. c = RotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
  225. b = RotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
  226. a = RotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
  227. d = RotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
  228. c = RotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
  229. b = RotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
  230. H1 += a;
  231. H2 += b;
  232. H3 += c;
  233. H4 += d;
  234. //
  235. // reset the offset and clean out the word buffer.
  236. //
  237. xOff = 0;
  238. for (int i = 0; i != X.Length; i++)
  239. {
  240. X[i] = 0;
  241. }
  242. }
  243. public override IMemoable Copy()
  244. {
  245. return new MD4Digest(this);
  246. }
  247. public override void Reset(IMemoable other)
  248. {
  249. MD4Digest d = (MD4Digest)other;
  250. CopyIn(d);
  251. }
  252. }
  253. }
  254. #pragma warning restore
  255. #endif