RipeMD128Digest.cs 13 KB

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