RipeMD320Digest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Utilities;
  4. namespace Org.BouncyCastle.Crypto.Digests
  5. {
  6. /// <remarks>
  7. /// <p>Implementation of RipeMD 320.</p>
  8. /// <p><b>Note:</b> this algorithm offers the same level of security as RipeMD160.</p>
  9. /// </remarks>
  10. public class RipeMD320Digest
  11. : GeneralDigest
  12. {
  13. public override string AlgorithmName
  14. {
  15. get { return "RIPEMD320"; }
  16. }
  17. public override int GetDigestSize()
  18. {
  19. return DigestLength;
  20. }
  21. private const int DigestLength = 40;
  22. private int H0, H1, H2, H3, H4, H5, H6, H7, H8, H9; // IV's
  23. private int[] X = new int[16];
  24. private int xOff;
  25. /// <summary> Standard constructor</summary>
  26. public RipeMD320Digest()
  27. {
  28. Reset();
  29. }
  30. /// <summary> Copy constructor. This will copy the state of the provided
  31. /// message digest.
  32. /// </summary>
  33. public RipeMD320Digest(RipeMD320Digest t)
  34. : base(t)
  35. {
  36. CopyIn(t);
  37. }
  38. private void CopyIn(RipeMD320Digest t)
  39. {
  40. base.CopyIn(t);
  41. H0 = t.H0;
  42. H1 = t.H1;
  43. H2 = t.H2;
  44. H3 = t.H3;
  45. H4 = t.H4;
  46. H5 = t.H5;
  47. H6 = t.H6;
  48. H7 = t.H7;
  49. H8 = t.H8;
  50. H9 = t.H9;
  51. Array.Copy(t.X, 0, X, 0, t.X.Length);
  52. xOff = t.xOff;
  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(byte[] output, int outOff)
  86. {
  87. Finish();
  88. UnpackWord(H0, output, outOff);
  89. UnpackWord(H1, output, outOff + 4);
  90. UnpackWord(H2, output, outOff + 8);
  91. UnpackWord(H3, output, outOff + 12);
  92. UnpackWord(H4, output, outOff + 16);
  93. UnpackWord(H5, output, outOff + 20);
  94. UnpackWord(H6, output, outOff + 24);
  95. UnpackWord(H7, output, outOff + 28);
  96. UnpackWord(H8, output, outOff + 32);
  97. UnpackWord(H9, output, outOff + 36);
  98. Reset();
  99. return DigestLength;
  100. }
  101. /// <summary> reset the chaining variables to the IV values.</summary>
  102. public override void Reset()
  103. {
  104. base.Reset();
  105. H0 = unchecked((int) 0x67452301);
  106. H1 = unchecked((int) 0xefcdab89);
  107. H2 = unchecked((int) 0x98badcfe);
  108. H3 = unchecked((int) 0x10325476);
  109. H4 = unchecked((int) 0xc3d2e1f0);
  110. H5 = unchecked((int) 0x76543210);
  111. H6 = unchecked((int) 0xFEDCBA98);
  112. H7 = unchecked((int) 0x89ABCDEF);
  113. H8 = unchecked((int) 0x01234567);
  114. H9 = unchecked((int) 0x3C2D1E0F);
  115. xOff = 0;
  116. for (int i = 0; i != X.Length; i++)
  117. {
  118. X[i] = 0;
  119. }
  120. }
  121. /*
  122. * rotate int x left n bits.
  123. */
  124. private int RL(
  125. int x,
  126. int n)
  127. {
  128. return (x << n) | (int)(((uint)x) >> (32 - n));
  129. }
  130. /*
  131. * f1,f2,f3,f4,f5 are the basic RipeMD160 functions.
  132. */
  133. /*
  134. * rounds 0-15
  135. */
  136. private int F1(int x, int y, int z)
  137. {
  138. return x ^ y ^ z;
  139. }
  140. /*
  141. * rounds 16-31
  142. */
  143. private int F2(int x, int y, int z)
  144. {
  145. return (x & y) | (~ x & z);
  146. }
  147. /*
  148. * rounds 32-47
  149. */
  150. private int F3(int x, int y, int z)
  151. {
  152. return (x | ~ y) ^ z;
  153. }
  154. /*
  155. * rounds 48-63
  156. */
  157. private int F4(int x, int y, int z)
  158. {
  159. return (x & z) | (y & ~ z);
  160. }
  161. /*
  162. * rounds 64-79
  163. */
  164. private int F5(int x, int y, int z)
  165. {
  166. return x ^ (y | ~z);
  167. }
  168. internal override void ProcessBlock()
  169. {
  170. int a, aa;
  171. int b, bb;
  172. int c, cc;
  173. int d, dd;
  174. int e, ee;
  175. int t;
  176. a = H0;
  177. b = H1;
  178. c = H2;
  179. d = H3;
  180. e = H4;
  181. aa = H5;
  182. bb = H6;
  183. cc = H7;
  184. dd = H8;
  185. ee = H9;
  186. //
  187. // Rounds 1 - 16
  188. //
  189. // left
  190. a = RL(a + F1(b, c, d) + X[0], 11) + e; c = RL(c, 10);
  191. e = RL(e + F1(a, b, c) + X[1], 14) + d; b = RL(b, 10);
  192. d = RL(d + F1(e, a, b) + X[2], 15) + c; a = RL(a, 10);
  193. c = RL(c + F1(d, e, a) + X[3], 12) + b; e = RL(e, 10);
  194. b = RL(b + F1(c, d, e) + X[4], 5) + a; d = RL(d, 10);
  195. a = RL(a + F1(b, c, d) + X[5], 8) + e; c = RL(c, 10);
  196. e = RL(e + F1(a, b, c) + X[6], 7) + d; b = RL(b, 10);
  197. d = RL(d + F1(e, a, b) + X[7], 9) + c; a = RL(a, 10);
  198. c = RL(c + F1(d, e, a) + X[8], 11) + b; e = RL(e, 10);
  199. b = RL(b + F1(c, d, e) + X[9], 13) + a; d = RL(d, 10);
  200. a = RL(a + F1(b, c, d) + X[10], 14) + e; c = RL(c, 10);
  201. e = RL(e + F1(a, b, c) + X[11], 15) + d; b = RL(b, 10);
  202. d = RL(d + F1(e, a, b) + X[12], 6) + c; a = RL(a, 10);
  203. c = RL(c + F1(d, e, a) + X[13], 7) + b; e = RL(e, 10);
  204. b = RL(b + F1(c, d, e) + X[14], 9) + a; d = RL(d, 10);
  205. a = RL(a + F1(b, c, d) + X[15], 8) + e; c = RL(c, 10);
  206. // right
  207. aa = RL(aa + F5(bb, cc, dd) + X[5] + unchecked((int)0x50a28be6), 8) + ee; cc = RL(cc, 10);
  208. ee = RL(ee + F5(aa, bb, cc) + X[14] + unchecked((int)0x50a28be6), 9) + dd; bb = RL(bb, 10);
  209. dd = RL(dd + F5(ee, aa, bb) + X[7] + unchecked((int)0x50a28be6), 9) + cc; aa = RL(aa, 10);
  210. cc = RL(cc + F5(dd, ee, aa) + X[0] + unchecked((int)0x50a28be6), 11) + bb; ee = RL(ee, 10);
  211. bb = RL(bb + F5(cc, dd, ee) + X[9] + unchecked((int)0x50a28be6), 13) + aa; dd = RL(dd, 10);
  212. aa = RL(aa + F5(bb, cc, dd) + X[2] + unchecked((int)0x50a28be6), 15) + ee; cc = RL(cc, 10);
  213. ee = RL(ee + F5(aa, bb, cc) + X[11] + unchecked((int)0x50a28be6), 15) + dd; bb = RL(bb, 10);
  214. dd = RL(dd + F5(ee, aa, bb) + X[4] + unchecked((int)0x50a28be6), 5) + cc; aa = RL(aa, 10);
  215. cc = RL(cc + F5(dd, ee, aa) + X[13] + unchecked((int)0x50a28be6), 7) + bb; ee = RL(ee, 10);
  216. bb = RL(bb + F5(cc, dd, ee) + X[6] + unchecked((int)0x50a28be6), 7) + aa; dd = RL(dd, 10);
  217. aa = RL(aa + F5(bb, cc, dd) + X[15] + unchecked((int)0x50a28be6), 8) + ee; cc = RL(cc, 10);
  218. ee = RL(ee + F5(aa, bb, cc) + X[8] + unchecked((int)0x50a28be6), 11) + dd; bb = RL(bb, 10);
  219. dd = RL(dd + F5(ee, aa, bb) + X[1] + unchecked((int)0x50a28be6), 14) + cc; aa = RL(aa, 10);
  220. cc = RL(cc + F5(dd, ee, aa) + X[10] + unchecked((int)0x50a28be6), 14) + bb; ee = RL(ee, 10);
  221. bb = RL(bb + F5(cc, dd, ee) + X[3] + unchecked((int)0x50a28be6), 12) + aa; dd = RL(dd, 10);
  222. aa = RL(aa + F5(bb, cc, dd) + X[12] + unchecked((int)0x50a28be6), 6) + ee; cc = RL(cc, 10);
  223. t = a; a = aa; aa = t;
  224. //
  225. // Rounds 16-31
  226. //
  227. // left
  228. e = RL(e + F2(a, b, c) + X[7] + unchecked((int)0x5a827999), 7) + d; b = RL(b, 10);
  229. d = RL(d + F2(e, a, b) + X[4] + unchecked((int)0x5a827999), 6) + c; a = RL(a, 10);
  230. c = RL(c + F2(d, e, a) + X[13] + unchecked((int)0x5a827999), 8) + b; e = RL(e, 10);
  231. b = RL(b + F2(c, d, e) + X[1] + unchecked((int)0x5a827999), 13) + a; d = RL(d, 10);
  232. a = RL(a + F2(b, c, d) + X[10] + unchecked((int)0x5a827999), 11) + e; c = RL(c, 10);
  233. e = RL(e + F2(a, b, c) + X[6] + unchecked((int)0x5a827999), 9) + d; b = RL(b, 10);
  234. d = RL(d + F2(e, a, b) + X[15] + unchecked((int)0x5a827999), 7) + c; a = RL(a, 10);
  235. c = RL(c + F2(d, e, a) + X[3] + unchecked((int)0x5a827999), 15) + b; e = RL(e, 10);
  236. b = RL(b + F2(c, d, e) + X[12] + unchecked((int)0x5a827999), 7) + a; d = RL(d, 10);
  237. a = RL(a + F2(b, c, d) + X[0] + unchecked((int)0x5a827999), 12) + e; c = RL(c, 10);
  238. e = RL(e + F2(a, b, c) + X[9] + unchecked((int)0x5a827999), 15) + d; b = RL(b, 10);
  239. d = RL(d + F2(e, a, b) + X[5] + unchecked((int)0x5a827999), 9) + c; a = RL(a, 10);
  240. c = RL(c + F2(d, e, a) + X[2] + unchecked((int)0x5a827999), 11) + b; e = RL(e, 10);
  241. b = RL(b + F2(c, d, e) + X[14] + unchecked((int)0x5a827999), 7) + a; d = RL(d, 10);
  242. a = RL(a + F2(b, c, d) + X[11] + unchecked((int)0x5a827999), 13) + e; c = RL(c, 10);
  243. e = RL(e + F2(a, b, c) + X[8] + unchecked((int)0x5a827999), 12) + d; b = RL(b, 10);
  244. // right
  245. ee = RL(ee + F4(aa, bb, cc) + X[6] + unchecked((int)0x5c4dd124), 9) + dd; bb = RL(bb, 10);
  246. dd = RL(dd + F4(ee, aa, bb) + X[11] + unchecked((int)0x5c4dd124), 13) + cc; aa = RL(aa, 10);
  247. cc = RL(cc + F4(dd, ee, aa) + X[3] + unchecked((int)0x5c4dd124), 15) + bb; ee = RL(ee, 10);
  248. bb = RL(bb + F4(cc, dd, ee) + X[7] + unchecked((int)0x5c4dd124), 7) + aa; dd = RL(dd, 10);
  249. aa = RL(aa + F4(bb, cc, dd) + X[0] + unchecked((int)0x5c4dd124), 12) + ee; cc = RL(cc, 10);
  250. ee = RL(ee + F4(aa, bb, cc) + X[13] + unchecked((int)0x5c4dd124), 8) + dd; bb = RL(bb, 10);
  251. dd = RL(dd + F4(ee, aa, bb) + X[5] + unchecked((int)0x5c4dd124), 9) + cc; aa = RL(aa, 10);
  252. cc = RL(cc + F4(dd, ee, aa) + X[10] + unchecked((int)0x5c4dd124), 11) + bb; ee = RL(ee, 10);
  253. bb = RL(bb + F4(cc, dd, ee) + X[14] + unchecked((int)0x5c4dd124), 7) + aa; dd = RL(dd, 10);
  254. aa = RL(aa + F4(bb, cc, dd) + X[15] + unchecked((int)0x5c4dd124), 7) + ee; cc = RL(cc, 10);
  255. ee = RL(ee + F4(aa, bb, cc) + X[8] + unchecked((int)0x5c4dd124), 12) + dd; bb = RL(bb, 10);
  256. dd = RL(dd + F4(ee, aa, bb) + X[12] + unchecked((int)0x5c4dd124), 7) + cc; aa = RL(aa, 10);
  257. cc = RL(cc + F4(dd, ee, aa) + X[4] + unchecked((int)0x5c4dd124), 6) + bb; ee = RL(ee, 10);
  258. bb = RL(bb + F4(cc, dd, ee) + X[9] + unchecked((int)0x5c4dd124), 15) + aa; dd = RL(dd, 10);
  259. aa = RL(aa + F4(bb, cc, dd) + X[1] + unchecked((int)0x5c4dd124), 13) + ee; cc = RL(cc, 10);
  260. ee = RL(ee + F4(aa, bb, cc) + X[2] + unchecked((int)0x5c4dd124), 11) + dd; bb = RL(bb, 10);
  261. t = b; b = bb; bb = t;
  262. //
  263. // Rounds 32-47
  264. //
  265. // left
  266. d = RL(d + F3(e, a, b) + X[3] + unchecked((int)0x6ed9eba1), 11) + c; a = RL(a, 10);
  267. c = RL(c + F3(d, e, a) + X[10] + unchecked((int)0x6ed9eba1), 13) + b; e = RL(e, 10);
  268. b = RL(b + F3(c, d, e) + X[14] + unchecked((int)0x6ed9eba1), 6) + a; d = RL(d, 10);
  269. a = RL(a + F3(b, c, d) + X[4] + unchecked((int)0x6ed9eba1), 7) + e; c = RL(c, 10);
  270. e = RL(e + F3(a, b, c) + X[9] + unchecked((int)0x6ed9eba1), 14) + d; b = RL(b, 10);
  271. d = RL(d + F3(e, a, b) + X[15] + unchecked((int)0x6ed9eba1), 9) + c; a = RL(a, 10);
  272. c = RL(c + F3(d, e, a) + X[8] + unchecked((int)0x6ed9eba1), 13) + b; e = RL(e, 10);
  273. b = RL(b + F3(c, d, e) + X[1] + unchecked((int)0x6ed9eba1), 15) + a; d = RL(d, 10);
  274. a = RL(a + F3(b, c, d) + X[2] + unchecked((int)0x6ed9eba1), 14) + e; c = RL(c, 10);
  275. e = RL(e + F3(a, b, c) + X[7] + unchecked((int)0x6ed9eba1), 8) + d; b = RL(b, 10);
  276. d = RL(d + F3(e, a, b) + X[0] + unchecked((int)0x6ed9eba1), 13) + c; a = RL(a, 10);
  277. c = RL(c + F3(d, e, a) + X[6] + unchecked((int)0x6ed9eba1), 6) + b; e = RL(e, 10);
  278. b = RL(b + F3(c, d, e) + X[13] + unchecked((int)0x6ed9eba1), 5) + a; d = RL(d, 10);
  279. a = RL(a + F3(b, c, d) + X[11] + unchecked((int)0x6ed9eba1), 12) + e; c = RL(c, 10);
  280. e = RL(e + F3(a, b, c) + X[5] + unchecked((int)0x6ed9eba1), 7) + d; b = RL(b, 10);
  281. d = RL(d + F3(e, a, b) + X[12] + unchecked((int)0x6ed9eba1), 5) + c; a = RL(a, 10);
  282. // right
  283. dd = RL(dd + F3(ee, aa, bb) + X[15] + unchecked((int)0x6d703ef3), 9) + cc; aa = RL(aa, 10);
  284. cc = RL(cc + F3(dd, ee, aa) + X[5] + unchecked((int)0x6d703ef3), 7) + bb; ee = RL(ee, 10);
  285. bb = RL(bb + F3(cc, dd, ee) + X[1] + unchecked((int)0x6d703ef3), 15) + aa; dd = RL(dd, 10);
  286. aa = RL(aa + F3(bb, cc, dd) + X[3] + unchecked((int)0x6d703ef3), 11) + ee; cc = RL(cc, 10);
  287. ee = RL(ee + F3(aa, bb, cc) + X[7] + unchecked((int)0x6d703ef3), 8) + dd; bb = RL(bb, 10);
  288. dd = RL(dd + F3(ee, aa, bb) + X[14] + unchecked((int)0x6d703ef3), 6) + cc; aa = RL(aa, 10);
  289. cc = RL(cc + F3(dd, ee, aa) + X[6] + unchecked((int)0x6d703ef3), 6) + bb; ee = RL(ee, 10);
  290. bb = RL(bb + F3(cc, dd, ee) + X[9] + unchecked((int)0x6d703ef3), 14) + aa; dd = RL(dd, 10);
  291. aa = RL(aa + F3(bb, cc, dd) + X[11] + unchecked((int)0x6d703ef3), 12) + ee; cc = RL(cc, 10);
  292. ee = RL(ee + F3(aa, bb, cc) + X[8] + unchecked((int)0x6d703ef3), 13) + dd; bb = RL(bb, 10);
  293. dd = RL(dd + F3(ee, aa, bb) + X[12] + unchecked((int)0x6d703ef3), 5) + cc; aa = RL(aa, 10);
  294. cc = RL(cc + F3(dd, ee, aa) + X[2] + unchecked((int)0x6d703ef3), 14) + bb; ee = RL(ee, 10);
  295. bb = RL(bb + F3(cc, dd, ee) + X[10] + unchecked((int)0x6d703ef3), 13) + aa; dd = RL(dd, 10);
  296. aa = RL(aa + F3(bb, cc, dd) + X[0] + unchecked((int)0x6d703ef3), 13) + ee; cc = RL(cc, 10);
  297. ee = RL(ee + F3(aa, bb, cc) + X[4] + unchecked((int)0x6d703ef3), 7) + dd; bb = RL(bb, 10);
  298. dd = RL(dd + F3(ee, aa, bb) + X[13] + unchecked((int)0x6d703ef3), 5) + cc; aa = RL(aa, 10);
  299. t = c; c = cc; cc = t;
  300. //
  301. // Rounds 48-63
  302. //
  303. // left
  304. c = RL(c + F4(d, e, a) + X[1] + unchecked((int)0x8f1bbcdc), 11) + b; e = RL(e, 10);
  305. b = RL(b + F4(c, d, e) + X[9] + unchecked((int)0x8f1bbcdc), 12) + a; d = RL(d, 10);
  306. a = RL(a + F4(b, c, d) + X[11] + unchecked((int)0x8f1bbcdc), 14) + e; c = RL(c, 10);
  307. e = RL(e + F4(a, b, c) + X[10] + unchecked((int)0x8f1bbcdc), 15) + d; b = RL(b, 10);
  308. d = RL(d + F4(e, a, b) + X[0] + unchecked((int)0x8f1bbcdc), 14) + c; a = RL(a, 10);
  309. c = RL(c + F4(d, e, a) + X[8] + unchecked((int)0x8f1bbcdc), 15) + b; e = RL(e, 10);
  310. b = RL(b + F4(c, d, e) + X[12] + unchecked((int)0x8f1bbcdc), 9) + a; d = RL(d, 10);
  311. a = RL(a + F4(b, c, d) + X[4] + unchecked((int)0x8f1bbcdc), 8) + e; c = RL(c, 10);
  312. e = RL(e + F4(a, b, c) + X[13] + unchecked((int)0x8f1bbcdc), 9) + d; b = RL(b, 10);
  313. d = RL(d + F4(e, a, b) + X[3] + unchecked((int)0x8f1bbcdc), 14) + c; a = RL(a, 10);
  314. c = RL(c + F4(d, e, a) + X[7] + unchecked((int)0x8f1bbcdc), 5) + b; e = RL(e, 10);
  315. b = RL(b + F4(c, d, e) + X[15] + unchecked((int)0x8f1bbcdc), 6) + a; d = RL(d, 10);
  316. a = RL(a + F4(b, c, d) + X[14] + unchecked((int)0x8f1bbcdc), 8) + e; c = RL(c, 10);
  317. e = RL(e + F4(a, b, c) + X[5] + unchecked((int)0x8f1bbcdc), 6) + d; b = RL(b, 10);
  318. d = RL(d + F4(e, a, b) + X[6] + unchecked((int)0x8f1bbcdc), 5) + c; a = RL(a, 10);
  319. c = RL(c + F4(d, e, a) + X[2] + unchecked((int)0x8f1bbcdc), 12) + b; e = RL(e, 10);
  320. // right
  321. cc = RL(cc + F2(dd, ee, aa) + X[8] + unchecked((int)0x7a6d76e9), 15) + bb; ee = RL(ee, 10);
  322. bb = RL(bb + F2(cc, dd, ee) + X[6] + unchecked((int)0x7a6d76e9), 5) + aa; dd = RL(dd, 10);
  323. aa = RL(aa + F2(bb, cc, dd) + X[4] + unchecked((int)0x7a6d76e9), 8) + ee; cc = RL(cc, 10);
  324. ee = RL(ee + F2(aa, bb, cc) + X[1] + unchecked((int)0x7a6d76e9), 11) + dd; bb = RL(bb, 10);
  325. dd = RL(dd + F2(ee, aa, bb) + X[3] + unchecked((int)0x7a6d76e9), 14) + cc; aa = RL(aa, 10);
  326. cc = RL(cc + F2(dd, ee, aa) + X[11] + unchecked((int)0x7a6d76e9), 14) + bb; ee = RL(ee, 10);
  327. bb = RL(bb + F2(cc, dd, ee) + X[15] + unchecked((int)0x7a6d76e9), 6) + aa; dd = RL(dd, 10);
  328. aa = RL(aa + F2(bb, cc, dd) + X[0] + unchecked((int)0x7a6d76e9), 14) + ee; cc = RL(cc, 10);
  329. ee = RL(ee + F2(aa, bb, cc) + X[5] + unchecked((int)0x7a6d76e9), 6) + dd; bb = RL(bb, 10);
  330. dd = RL(dd + F2(ee, aa, bb) + X[12] + unchecked((int)0x7a6d76e9), 9) + cc; aa = RL(aa, 10);
  331. cc = RL(cc + F2(dd, ee, aa) + X[2] + unchecked((int)0x7a6d76e9), 12) + bb; ee = RL(ee, 10);
  332. bb = RL(bb + F2(cc, dd, ee) + X[13] + unchecked((int)0x7a6d76e9), 9) + aa; dd = RL(dd, 10);
  333. aa = RL(aa + F2(bb, cc, dd) + X[9] + unchecked((int)0x7a6d76e9), 12) + ee; cc = RL(cc, 10);
  334. ee = RL(ee + F2(aa, bb, cc) + X[7] + unchecked((int)0x7a6d76e9), 5) + dd; bb = RL(bb, 10);
  335. dd = RL(dd + F2(ee, aa, bb) + X[10] + unchecked((int)0x7a6d76e9), 15) + cc; aa = RL(aa, 10);
  336. cc = RL(cc + F2(dd, ee, aa) + X[14] + unchecked((int)0x7a6d76e9), 8) + bb; ee = RL(ee, 10);
  337. t = d; d = dd; dd = t;
  338. //
  339. // Rounds 64-79
  340. //
  341. // left
  342. b = RL(b + F5(c, d, e) + X[4] + unchecked((int)0xa953fd4e), 9) + a; d = RL(d, 10);
  343. a = RL(a + F5(b, c, d) + X[0] + unchecked((int)0xa953fd4e), 15) + e; c = RL(c, 10);
  344. e = RL(e + F5(a, b, c) + X[5] + unchecked((int)0xa953fd4e), 5) + d; b = RL(b, 10);
  345. d = RL(d + F5(e, a, b) + X[9] + unchecked((int)0xa953fd4e), 11) + c; a = RL(a, 10);
  346. c = RL(c + F5(d, e, a) + X[7] + unchecked((int)0xa953fd4e), 6) + b; e = RL(e, 10);
  347. b = RL(b + F5(c, d, e) + X[12] + unchecked((int)0xa953fd4e), 8) + a; d = RL(d, 10);
  348. a = RL(a + F5(b, c, d) + X[2] + unchecked((int)0xa953fd4e), 13) + e; c = RL(c, 10);
  349. e = RL(e + F5(a, b, c) + X[10] + unchecked((int)0xa953fd4e), 12) + d; b = RL(b, 10);
  350. d = RL(d + F5(e, a, b) + X[14] + unchecked((int)0xa953fd4e), 5) + c; a = RL(a, 10);
  351. c = RL(c + F5(d, e, a) + X[1] + unchecked((int)0xa953fd4e), 12) + b; e = RL(e, 10);
  352. b = RL(b + F5(c, d, e) + X[3] + unchecked((int)0xa953fd4e), 13) + a; d = RL(d, 10);
  353. a = RL(a + F5(b, c, d) + X[8] + unchecked((int)0xa953fd4e), 14) + e; c = RL(c, 10);
  354. e = RL(e + F5(a, b, c) + X[11] + unchecked((int)0xa953fd4e), 11) + d; b = RL(b, 10);
  355. d = RL(d + F5(e, a, b) + X[6] + unchecked((int)0xa953fd4e), 8) + c; a = RL(a, 10);
  356. c = RL(c + F5(d, e, a) + X[15] + unchecked((int)0xa953fd4e), 5) + b; e = RL(e, 10);
  357. b = RL(b + F5(c, d, e) + X[13] + unchecked((int)0xa953fd4e), 6) + a; d = RL(d, 10);
  358. // right
  359. bb = RL(bb + F1(cc, dd, ee) + X[12], 8) + aa; dd = RL(dd, 10);
  360. aa = RL(aa + F1(bb, cc, dd) + X[15], 5) + ee; cc = RL(cc, 10);
  361. ee = RL(ee + F1(aa, bb, cc) + X[10], 12) + dd; bb = RL(bb, 10);
  362. dd = RL(dd + F1(ee, aa, bb) + X[4], 9) + cc; aa = RL(aa, 10);
  363. cc = RL(cc + F1(dd, ee, aa) + X[1], 12) + bb; ee = RL(ee, 10);
  364. bb = RL(bb + F1(cc, dd, ee) + X[5], 5) + aa; dd = RL(dd, 10);
  365. aa = RL(aa + F1(bb, cc, dd) + X[8], 14) + ee; cc = RL(cc, 10);
  366. ee = RL(ee + F1(aa, bb, cc) + X[7], 6) + dd; bb = RL(bb, 10);
  367. dd = RL(dd + F1(ee, aa, bb) + X[6], 8) + cc; aa = RL(aa, 10);
  368. cc = RL(cc + F1(dd, ee, aa) + X[2], 13) + bb; ee = RL(ee, 10);
  369. bb = RL(bb + F1(cc, dd, ee) + X[13], 6) + aa; dd = RL(dd, 10);
  370. aa = RL(aa + F1(bb, cc, dd) + X[14], 5) + ee; cc = RL(cc, 10);
  371. ee = RL(ee + F1(aa, bb, cc) + X[0], 15) + dd; bb = RL(bb, 10);
  372. dd = RL(dd + F1(ee, aa, bb) + X[3], 13) + cc; aa = RL(aa, 10);
  373. cc = RL(cc + F1(dd, ee, aa) + X[9], 11) + bb; ee = RL(ee, 10);
  374. bb = RL(bb + F1(cc, dd, ee) + X[11], 11) + aa; dd = RL(dd, 10);
  375. //
  376. // do (e, ee) swap as part of assignment.
  377. //
  378. H0 += a;
  379. H1 += b;
  380. H2 += c;
  381. H3 += d;
  382. H4 += ee;
  383. H5 += aa;
  384. H6 += bb;
  385. H7 += cc;
  386. H8 += dd;
  387. H9 += e;
  388. //
  389. // reset the offset and clean out the word buffer.
  390. //
  391. xOff = 0;
  392. for (int i = 0; i != X.Length; i++)
  393. {
  394. X[i] = 0;
  395. }
  396. }
  397. public override IMemoable Copy()
  398. {
  399. return new RipeMD320Digest(this);
  400. }
  401. public override void Reset(IMemoable other)
  402. {
  403. RipeMD320Digest d = (RipeMD320Digest)other;
  404. CopyIn(d);
  405. }
  406. }
  407. }
  408. #endif