RipeMD256Digest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 RipeMD256.</p>
  8. /// <p><b>Note:</b> this algorithm offers the same level of security as RipeMD128.</p>
  9. /// </remarks>
  10. public class RipeMD256Digest
  11. : GeneralDigest
  12. {
  13. public override string AlgorithmName
  14. {
  15. get { return "RIPEMD256"; }
  16. }
  17. public override int GetDigestSize()
  18. {
  19. return DigestLength;
  20. }
  21. private const int DigestLength = 32;
  22. private int H0, H1, H2, H3, H4, H5, H6, H7; // IV's
  23. private int[] X = new int[16];
  24. private int xOff;
  25. /// <summary> Standard constructor</summary>
  26. public RipeMD256Digest()
  27. {
  28. Reset();
  29. }
  30. /// <summary> Copy constructor. This will copy the state of the provided
  31. /// message digest.
  32. /// </summary>
  33. public RipeMD256Digest(RipeMD256Digest t):base(t)
  34. {
  35. CopyIn(t);
  36. }
  37. private void CopyIn(RipeMD256Digest t)
  38. {
  39. base.CopyIn(t);
  40. H0 = t.H0;
  41. H1 = t.H1;
  42. H2 = t.H2;
  43. H3 = t.H3;
  44. H4 = t.H4;
  45. H5 = t.H5;
  46. H6 = t.H6;
  47. H7 = t.H7;
  48. Array.Copy(t.X, 0, X, 0, t.X.Length);
  49. xOff = t.xOff;
  50. }
  51. internal override void ProcessWord(
  52. byte[] input,
  53. int inOff)
  54. {
  55. X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8)
  56. | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24);
  57. if (xOff == 16)
  58. {
  59. ProcessBlock();
  60. }
  61. }
  62. internal override void ProcessLength(
  63. long bitLength)
  64. {
  65. if (xOff > 14)
  66. {
  67. ProcessBlock();
  68. }
  69. X[14] = (int)(bitLength & 0xffffffff);
  70. X[15] = (int)((ulong)bitLength >> 32);
  71. }
  72. private void UnpackWord(
  73. int word,
  74. byte[] outBytes,
  75. int outOff)
  76. {
  77. outBytes[outOff] = (byte)(uint)word;
  78. outBytes[outOff + 1] = (byte)((uint)word >> 8);
  79. outBytes[outOff + 2] = (byte)((uint)word >> 16);
  80. outBytes[outOff + 3] = (byte)((uint)word >> 24);
  81. }
  82. public override int DoFinal(byte[] output, int outOff)
  83. {
  84. Finish();
  85. UnpackWord(H0, output, outOff);
  86. UnpackWord(H1, output, outOff + 4);
  87. UnpackWord(H2, output, outOff + 8);
  88. UnpackWord(H3, output, outOff + 12);
  89. UnpackWord(H4, output, outOff + 16);
  90. UnpackWord(H5, output, outOff + 20);
  91. UnpackWord(H6, output, outOff + 24);
  92. UnpackWord(H7, output, outOff + 28);
  93. Reset();
  94. return DigestLength;
  95. }
  96. /// <summary> reset the chaining variables to the IV values.</summary>
  97. public override void Reset()
  98. {
  99. base.Reset();
  100. H0 = unchecked((int)0x67452301);
  101. H1 = unchecked((int)0xefcdab89);
  102. H2 = unchecked((int)0x98badcfe);
  103. H3 = unchecked((int)0x10325476);
  104. H4 = unchecked((int)0x76543210);
  105. H5 = unchecked((int)0xFEDCBA98);
  106. H6 = unchecked((int)0x89ABCDEF);
  107. H7 = unchecked((int)0x01234567);
  108. xOff = 0;
  109. for (int i = 0; i != X.Length; i++)
  110. {
  111. X[i] = 0;
  112. }
  113. }
  114. /*
  115. * rotate int x left n bits.
  116. */
  117. private int RL(
  118. int x,
  119. int n)
  120. {
  121. return (x << n) | (int)((uint)x >> (32 - n));
  122. }
  123. /*
  124. * f1,f2,f3,f4 are the basic RipeMD128 functions.
  125. */
  126. /*
  127. * F
  128. */
  129. private int F1(int x, int y, int z)
  130. {
  131. return x ^ y ^ z;
  132. }
  133. /*
  134. * G
  135. */
  136. private int F2(int x, int y, int z)
  137. {
  138. return (x & y) | (~ x & z);
  139. }
  140. /*
  141. * H
  142. */
  143. private int F3(int x, int y, int z)
  144. {
  145. return (x | ~ y) ^ z;
  146. }
  147. /*
  148. * I
  149. */
  150. private int F4(int x, int y, int z)
  151. {
  152. return (x & z) | (y & ~ z);
  153. }
  154. private int F1(int a, int b, int c, int d, int x, int s)
  155. {
  156. return RL(a + F1(b, c, d) + x, s);
  157. }
  158. private int F2(int a, int b, int c, int d, int x, int s)
  159. {
  160. return RL(a + F2(b, c, d) + x + unchecked((int)0x5a827999), s);
  161. }
  162. private int F3(int a, int b, int c, int d, int x, int s)
  163. {
  164. return RL(a + F3(b, c, d) + x + unchecked((int)0x6ed9eba1), s);
  165. }
  166. private int F4(int a, int b, int c, int d, int x, int s)
  167. {
  168. return RL(a + F4(b, c, d) + x + unchecked((int)0x8f1bbcdc), s);
  169. }
  170. private int FF1(int a, int b, int c, int d, int x, int s)
  171. {
  172. return RL(a + F1(b, c, d) + x, s);
  173. }
  174. private int FF2(int a, int b, int c, int d, int x, int s)
  175. {
  176. return RL(a + F2(b, c, d) + x + unchecked((int)0x6d703ef3), s);
  177. }
  178. private int FF3(int a, int b, int c, int d, int x, int s)
  179. {
  180. return RL(a + F3(b, c, d) + x + unchecked((int)0x5c4dd124), s);
  181. }
  182. private int FF4(int a, int b, int c, int d, int x, int s)
  183. {
  184. return RL(a + F4(b, c, d) + x + unchecked((int)0x50a28be6), s);
  185. }
  186. internal override void ProcessBlock()
  187. {
  188. int a, aa;
  189. int b, bb;
  190. int c, cc;
  191. int d, dd;
  192. int t;
  193. a = H0;
  194. b = H1;
  195. c = H2;
  196. d = H3;
  197. aa = H4;
  198. bb = H5;
  199. cc = H6;
  200. dd = H7;
  201. //
  202. // Round 1
  203. //
  204. a = F1(a, b, c, d, X[0], 11);
  205. d = F1(d, a, b, c, X[1], 14);
  206. c = F1(c, d, a, b, X[2], 15);
  207. b = F1(b, c, d, a, X[3], 12);
  208. a = F1(a, b, c, d, X[4], 5);
  209. d = F1(d, a, b, c, X[5], 8);
  210. c = F1(c, d, a, b, X[6], 7);
  211. b = F1(b, c, d, a, X[7], 9);
  212. a = F1(a, b, c, d, X[8], 11);
  213. d = F1(d, a, b, c, X[9], 13);
  214. c = F1(c, d, a, b, X[10], 14);
  215. b = F1(b, c, d, a, X[11], 15);
  216. a = F1(a, b, c, d, X[12], 6);
  217. d = F1(d, a, b, c, X[13], 7);
  218. c = F1(c, d, a, b, X[14], 9);
  219. b = F1(b, c, d, a, X[15], 8);
  220. aa = FF4(aa, bb, cc, dd, X[5], 8);
  221. dd = FF4(dd, aa, bb, cc, X[14], 9);
  222. cc = FF4(cc, dd, aa, bb, X[7], 9);
  223. bb = FF4(bb, cc, dd, aa, X[0], 11);
  224. aa = FF4(aa, bb, cc, dd, X[9], 13);
  225. dd = FF4(dd, aa, bb, cc, X[2], 15);
  226. cc = FF4(cc, dd, aa, bb, X[11], 15);
  227. bb = FF4(bb, cc, dd, aa, X[4], 5);
  228. aa = FF4(aa, bb, cc, dd, X[13], 7);
  229. dd = FF4(dd, aa, bb, cc, X[6], 7);
  230. cc = FF4(cc, dd, aa, bb, X[15], 8);
  231. bb = FF4(bb, cc, dd, aa, X[8], 11);
  232. aa = FF4(aa, bb, cc, dd, X[1], 14);
  233. dd = FF4(dd, aa, bb, cc, X[10], 14);
  234. cc = FF4(cc, dd, aa, bb, X[3], 12);
  235. bb = FF4(bb, cc, dd, aa, X[12], 6);
  236. t = a; a = aa; aa = t;
  237. //
  238. // Round 2
  239. //
  240. a = F2(a, b, c, d, X[7], 7);
  241. d = F2(d, a, b, c, X[4], 6);
  242. c = F2(c, d, a, b, X[13], 8);
  243. b = F2(b, c, d, a, X[1], 13);
  244. a = F2(a, b, c, d, X[10], 11);
  245. d = F2(d, a, b, c, X[6], 9);
  246. c = F2(c, d, a, b, X[15], 7);
  247. b = F2(b, c, d, a, X[3], 15);
  248. a = F2(a, b, c, d, X[12], 7);
  249. d = F2(d, a, b, c, X[0], 12);
  250. c = F2(c, d, a, b, X[9], 15);
  251. b = F2(b, c, d, a, X[5], 9);
  252. a = F2(a, b, c, d, X[2], 11);
  253. d = F2(d, a, b, c, X[14], 7);
  254. c = F2(c, d, a, b, X[11], 13);
  255. b = F2(b, c, d, a, X[8], 12);
  256. aa = FF3(aa, bb, cc, dd, X[6], 9);
  257. dd = FF3(dd, aa, bb, cc, X[11], 13);
  258. cc = FF3(cc, dd, aa, bb, X[3], 15);
  259. bb = FF3(bb, cc, dd, aa, X[7], 7);
  260. aa = FF3(aa, bb, cc, dd, X[0], 12);
  261. dd = FF3(dd, aa, bb, cc, X[13], 8);
  262. cc = FF3(cc, dd, aa, bb, X[5], 9);
  263. bb = FF3(bb, cc, dd, aa, X[10], 11);
  264. aa = FF3(aa, bb, cc, dd, X[14], 7);
  265. dd = FF3(dd, aa, bb, cc, X[15], 7);
  266. cc = FF3(cc, dd, aa, bb, X[8], 12);
  267. bb = FF3(bb, cc, dd, aa, X[12], 7);
  268. aa = FF3(aa, bb, cc, dd, X[4], 6);
  269. dd = FF3(dd, aa, bb, cc, X[9], 15);
  270. cc = FF3(cc, dd, aa, bb, X[1], 13);
  271. bb = FF3(bb, cc, dd, aa, X[2], 11);
  272. t = b; b = bb; bb = t;
  273. //
  274. // Round 3
  275. //
  276. a = F3(a, b, c, d, X[3], 11);
  277. d = F3(d, a, b, c, X[10], 13);
  278. c = F3(c, d, a, b, X[14], 6);
  279. b = F3(b, c, d, a, X[4], 7);
  280. a = F3(a, b, c, d, X[9], 14);
  281. d = F3(d, a, b, c, X[15], 9);
  282. c = F3(c, d, a, b, X[8], 13);
  283. b = F3(b, c, d, a, X[1], 15);
  284. a = F3(a, b, c, d, X[2], 14);
  285. d = F3(d, a, b, c, X[7], 8);
  286. c = F3(c, d, a, b, X[0], 13);
  287. b = F3(b, c, d, a, X[6], 6);
  288. a = F3(a, b, c, d, X[13], 5);
  289. d = F3(d, a, b, c, X[11], 12);
  290. c = F3(c, d, a, b, X[5], 7);
  291. b = F3(b, c, d, a, X[12], 5);
  292. aa = FF2(aa, bb, cc, dd, X[15], 9);
  293. dd = FF2(dd, aa, bb, cc, X[5], 7);
  294. cc = FF2(cc, dd, aa, bb, X[1], 15);
  295. bb = FF2(bb, cc, dd, aa, X[3], 11);
  296. aa = FF2(aa, bb, cc, dd, X[7], 8);
  297. dd = FF2(dd, aa, bb, cc, X[14], 6);
  298. cc = FF2(cc, dd, aa, bb, X[6], 6);
  299. bb = FF2(bb, cc, dd, aa, X[9], 14);
  300. aa = FF2(aa, bb, cc, dd, X[11], 12);
  301. dd = FF2(dd, aa, bb, cc, X[8], 13);
  302. cc = FF2(cc, dd, aa, bb, X[12], 5);
  303. bb = FF2(bb, cc, dd, aa, X[2], 14);
  304. aa = FF2(aa, bb, cc, dd, X[10], 13);
  305. dd = FF2(dd, aa, bb, cc, X[0], 13);
  306. cc = FF2(cc, dd, aa, bb, X[4], 7);
  307. bb = FF2(bb, cc, dd, aa, X[13], 5);
  308. t = c; c = cc; cc = t;
  309. //
  310. // Round 4
  311. //
  312. a = F4(a, b, c, d, X[1], 11);
  313. d = F4(d, a, b, c, X[9], 12);
  314. c = F4(c, d, a, b, X[11], 14);
  315. b = F4(b, c, d, a, X[10], 15);
  316. a = F4(a, b, c, d, X[0], 14);
  317. d = F4(d, a, b, c, X[8], 15);
  318. c = F4(c, d, a, b, X[12], 9);
  319. b = F4(b, c, d, a, X[4], 8);
  320. a = F4(a, b, c, d, X[13], 9);
  321. d = F4(d, a, b, c, X[3], 14);
  322. c = F4(c, d, a, b, X[7], 5);
  323. b = F4(b, c, d, a, X[15], 6);
  324. a = F4(a, b, c, d, X[14], 8);
  325. d = F4(d, a, b, c, X[5], 6);
  326. c = F4(c, d, a, b, X[6], 5);
  327. b = F4(b, c, d, a, X[2], 12);
  328. aa = FF1(aa, bb, cc, dd, X[8], 15);
  329. dd = FF1(dd, aa, bb, cc, X[6], 5);
  330. cc = FF1(cc, dd, aa, bb, X[4], 8);
  331. bb = FF1(bb, cc, dd, aa, X[1], 11);
  332. aa = FF1(aa, bb, cc, dd, X[3], 14);
  333. dd = FF1(dd, aa, bb, cc, X[11], 14);
  334. cc = FF1(cc, dd, aa, bb, X[15], 6);
  335. bb = FF1(bb, cc, dd, aa, X[0], 14);
  336. aa = FF1(aa, bb, cc, dd, X[5], 6);
  337. dd = FF1(dd, aa, bb, cc, X[12], 9);
  338. cc = FF1(cc, dd, aa, bb, X[2], 12);
  339. bb = FF1(bb, cc, dd, aa, X[13], 9);
  340. aa = FF1(aa, bb, cc, dd, X[9], 12);
  341. dd = FF1(dd, aa, bb, cc, X[7], 5);
  342. cc = FF1(cc, dd, aa, bb, X[10], 15);
  343. bb = FF1(bb, cc, dd, aa, X[14], 8);
  344. t = d; d = dd; dd = t;
  345. H0 += a;
  346. H1 += b;
  347. H2 += c;
  348. H3 += d;
  349. H4 += aa;
  350. H5 += bb;
  351. H6 += cc;
  352. H7 += dd;
  353. //
  354. // reset the offset and clean out the word buffer.
  355. //
  356. xOff = 0;
  357. for (int i = 0; i != X.Length; i++)
  358. {
  359. X[i] = 0;
  360. }
  361. }
  362. public override IMemoable Copy()
  363. {
  364. return new RipeMD256Digest(this);
  365. }
  366. public override void Reset(IMemoable other)
  367. {
  368. RipeMD256Digest d = (RipeMD256Digest)other;
  369. CopyIn(d);
  370. }
  371. }
  372. }
  373. #endif