RipeMD256Digest.cs 13 KB

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