RipeMD128Digest.cs 13 KB

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