SP80038G.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Fpe
  8. {
  9. /*
  10. * SP800-38G Format-Preserving Encryption
  11. *
  12. * TODOs
  13. * - Initialize the cipher internally or externally?
  14. * 1. Algs 7-10 don't appear to require forward vs. inverse transform, although sample data is forward.
  15. * 2. Algs 9-10 specify reversal of the cipher key!
  16. * - Separate construction/initialization stage for "prerequisites"
  17. */
  18. internal class SP80038G
  19. {
  20. internal static readonly string FPE_DISABLED = "BestHTTP.SecureProtocol.Org.BouncyCastle.Fpe.Disable";
  21. internal static readonly string FF1_DISABLED = "BestHTTP.SecureProtocol.Org.BouncyCastle.Fpe.Disable_Ff1";
  22. protected static readonly int BLOCK_SIZE = 16;
  23. protected static readonly double LOG2 = System.Math.Log(2.0);
  24. protected static readonly double TWO_TO_96 = System.Math.Pow(2, 96);
  25. public static byte[] DecryptFF1(IBlockCipher cipher, int radix, byte[] tweak, byte[] buf, int off, int len)
  26. {
  27. checkArgs(cipher, true, radix, buf, off, len);
  28. // Algorithm 8
  29. int n = len;
  30. int u = n / 2, v = n - u;
  31. ushort[] A = toShort(buf, off, u);
  32. ushort[] B = toShort(buf, off + u, v);
  33. ushort[] rv = decFF1(cipher, radix, tweak, n, u, v, A, B);
  34. return toByte(rv);
  35. }
  36. public static ushort[] DecryptFF1w(IBlockCipher cipher, int radix, byte[] tweak, ushort[] buf, int off, int len)
  37. {
  38. checkArgs(cipher, true, radix, buf, off, len);
  39. // Algorithm 8
  40. int n = len;
  41. int u = n / 2, v = n - u;
  42. ushort[] A = new ushort[u];
  43. ushort[] B = new ushort[v];
  44. Array.Copy(buf, off, A, 0, u);
  45. Array.Copy(buf, off + u, B, 0, v);
  46. return decFF1(cipher, radix, tweak, n, u, v, A, B);
  47. }
  48. private static ushort[] decFF1(IBlockCipher cipher, int radix, byte[] T, int n, int u, int v, ushort[] A, ushort[] B)
  49. {
  50. int t = T.Length;
  51. int b = ((int)Ceil(System.Math.Log((double)radix) * (double)v / LOG2) + 7) / 8;
  52. int d = (((b + 3) / 4) * 4) + 4;
  53. byte[] P = calculateP_FF1(radix, (byte)u, n, t);
  54. BigInteger bigRadix = BigInteger.ValueOf(radix);
  55. BigInteger[] modUV = calculateModUV(bigRadix, u, v);
  56. int m = u;
  57. for (int i = 9; i >= 0; --i)
  58. {
  59. // i. - iv.
  60. BigInteger y = calculateY_FF1(cipher, bigRadix, T, b, d, i, P, A);
  61. // v.
  62. m = n - m;
  63. BigInteger modulus = modUV[i & 1];
  64. // vi.
  65. BigInteger c = num(bigRadix, B).Subtract(y).Mod(modulus);
  66. // vii. - ix.
  67. ushort[] C = B;
  68. B = A;
  69. A = C;
  70. str(bigRadix, c, m, C, 0);
  71. }
  72. return Arrays.Concatenate(A, B);
  73. }
  74. public static byte[] DecryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  75. {
  76. checkArgs(cipher, false, radix, buf, off, len);
  77. if (tweak64.Length != 8)
  78. {
  79. throw new ArgumentException();
  80. }
  81. return implDecryptFF3(cipher, radix, tweak64, buf, off, len);
  82. }
  83. public static byte[] DecryptFF3_1(IBlockCipher cipher, int radix, byte[] tweak56, byte[] buf, int off, int len)
  84. {
  85. checkArgs(cipher, false, radix, buf, off, len);
  86. if (tweak56.Length != 7)
  87. {
  88. throw new ArgumentException("tweak should be 56 bits");
  89. }
  90. byte[] tweak64 = calculateTweak64_FF3_1(tweak56);
  91. return implDecryptFF3(cipher, radix, tweak64, buf, off, len);
  92. }
  93. public static ushort[] DecryptFF3_1w(IBlockCipher cipher, int radix, byte[] tweak56, ushort[] buf, int off, int len)
  94. {
  95. checkArgs(cipher, false, radix, buf, off, len);
  96. if (tweak56.Length != 7)
  97. {
  98. throw new ArgumentException("tweak should be 56 bits");
  99. }
  100. byte[] tweak64 = calculateTweak64_FF3_1(tweak56);
  101. return implDecryptFF3w(cipher, radix, tweak64, buf, off, len);
  102. }
  103. public static byte[] EncryptFF1(IBlockCipher cipher, int radix, byte[] tweak, byte[] buf, int off, int len)
  104. {
  105. checkArgs(cipher, true, radix, buf, off, len);
  106. // Algorithm 7
  107. int n = len;
  108. int u = n / 2, v = n - u;
  109. ushort[] A = toShort(buf, off, u);
  110. ushort[] B = toShort(buf, off + u, v);
  111. return toByte(encFF1(cipher, radix, tweak, n, u, v, A, B));
  112. }
  113. public static ushort[] EncryptFF1w(IBlockCipher cipher, int radix, byte[] tweak, ushort[] buf, int off, int len)
  114. {
  115. checkArgs(cipher, true, radix, buf, off, len);
  116. // Algorithm 7
  117. int n = len;
  118. int u = n / 2, v = n - u;
  119. ushort[] A = new ushort[u];
  120. ushort[] B = new ushort[v];
  121. Array.Copy(buf, off, A, 0, u);
  122. Array.Copy(buf, off + u, B, 0, v);
  123. return encFF1(cipher, radix, tweak, n, u, v, A, B);
  124. }
  125. private static ushort[] encFF1(IBlockCipher cipher, int radix, byte[] T, int n, int u, int v, ushort[] A, ushort[] B)
  126. {
  127. int t = T.Length;
  128. int b = ((int)Ceil(System.Math.Log((double)radix) * (double)v / LOG2) + 7) / 8;
  129. int d = (((b + 3) / 4) * 4) + 4;
  130. byte[] P = calculateP_FF1(radix, (byte)u, n, t);
  131. BigInteger bigRadix = BigInteger.ValueOf(radix);
  132. BigInteger[] modUV = calculateModUV(bigRadix, u, v);
  133. int m = v;
  134. for (int i = 0; i < 10; ++i)
  135. {
  136. // i. - iv.
  137. BigInteger y = calculateY_FF1(cipher, bigRadix, T, b, d, i, P, B);
  138. // v.
  139. m = n - m;
  140. BigInteger modulus = modUV[i & 1];
  141. // vi.
  142. BigInteger c = num(bigRadix, A).Add(y).Mod(modulus);
  143. // vii. - ix.
  144. ushort[] C = A;
  145. A = B;
  146. B = C;
  147. str(bigRadix, c, m, C, 0);
  148. }
  149. return Arrays.Concatenate(A, B);
  150. }
  151. public static byte[] EncryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  152. {
  153. checkArgs(cipher, false, radix, buf, off, len);
  154. if (tweak64.Length != 8)
  155. {
  156. throw new ArgumentException();
  157. }
  158. return implEncryptFF3(cipher, radix, tweak64, buf, off, len);
  159. }
  160. public static ushort[] EncryptFF3w(IBlockCipher cipher, int radix, byte[] tweak64, ushort[] buf, int off, int len)
  161. {
  162. checkArgs(cipher, false, radix, buf, off, len);
  163. if (tweak64.Length != 8)
  164. {
  165. throw new ArgumentException();
  166. }
  167. return implEncryptFF3w(cipher, radix, tweak64, buf, off, len);
  168. }
  169. public static ushort[] EncryptFF3_1w(IBlockCipher cipher, int radix, byte[] tweak56, ushort[] buf, int off, int len)
  170. {
  171. checkArgs(cipher, false, radix, buf, off, len);
  172. if (tweak56.Length != 7)
  173. {
  174. throw new ArgumentException("tweak should be 56 bits");
  175. }
  176. byte[] tweak64 = calculateTweak64_FF3_1(tweak56);
  177. return EncryptFF3w(cipher, radix, tweak64, buf, off, len);
  178. }
  179. public static byte[] EncryptFF3_1(IBlockCipher cipher, int radix, byte[] tweak56, byte[] buf, int off, int len)
  180. {
  181. checkArgs(cipher, false, radix, buf, off, len);
  182. if (tweak56.Length != 7)
  183. {
  184. throw new ArgumentException("tweak should be 56 bits");
  185. }
  186. byte[] tweak64 = calculateTweak64_FF3_1(tweak56);
  187. return EncryptFF3(cipher, radix, tweak64, buf, off, len);
  188. }
  189. protected static BigInteger[] calculateModUV(BigInteger bigRadix, int u, int v)
  190. {
  191. BigInteger[] modUV = new BigInteger[2];
  192. modUV[0] = bigRadix.Pow(u);
  193. modUV[1] = modUV[0];
  194. if (v != u)
  195. {
  196. modUV[1] = modUV[1].Multiply(bigRadix);
  197. }
  198. return modUV;
  199. }
  200. protected static byte[] calculateP_FF1(int radix, byte uLow, int n, int t)
  201. {
  202. byte[] P = new byte[BLOCK_SIZE];
  203. P[0] = 1;
  204. P[1] = 2;
  205. P[2] = 1;
  206. // Radix
  207. P[3] = 0;
  208. P[4] = (byte)(radix >> 8);
  209. P[5] = (byte)radix;
  210. P[6] = 10;
  211. P[7] = uLow;
  212. Pack.UInt32_To_BE((uint)n, P, 8);
  213. Pack.UInt32_To_BE((uint)t, P, 12);
  214. return P;
  215. }
  216. protected static byte[] calculateTweak64_FF3_1(byte[] tweak56)
  217. {
  218. byte[] tweak64 = new byte[8];
  219. tweak64[0] = tweak56[0];
  220. tweak64[1] = tweak56[1];
  221. tweak64[2] = tweak56[2];
  222. tweak64[3] = (byte)(tweak56[3] & 0xF0);
  223. tweak64[4] = tweak56[4];
  224. tweak64[5] = tweak56[5];
  225. tweak64[6] = tweak56[6];
  226. tweak64[7] = (byte)(tweak56[3] << 4);
  227. return tweak64;
  228. }
  229. protected static BigInteger calculateY_FF1(IBlockCipher cipher, BigInteger bigRadix, byte[] T, int b, int d, int round, byte[] P, ushort[] AB)
  230. {
  231. int t = T.Length;
  232. // i.
  233. BigInteger numAB = num(bigRadix, AB);
  234. byte[] bytesAB = BigIntegers.AsUnsignedByteArray(numAB);
  235. int zeroes = -(t + b + 1) & 15;
  236. byte[] Q = new byte[t + zeroes + 1 + b];
  237. Array.Copy(T, 0, Q, 0, t);
  238. Q[t + zeroes] = (byte)round;
  239. Array.Copy(bytesAB, 0, Q, Q.Length - bytesAB.Length, bytesAB.Length);
  240. // ii.
  241. byte[] R = prf(cipher, Arrays.Concatenate(P, Q));
  242. // iii.
  243. byte[] sBlocks = R;
  244. if (d > BLOCK_SIZE)
  245. {
  246. int sBlocksLen = (d + BLOCK_SIZE - 1) / BLOCK_SIZE;
  247. sBlocks = new byte[sBlocksLen * BLOCK_SIZE];
  248. Array.Copy(R, 0, sBlocks, 0, BLOCK_SIZE);
  249. byte[] uint32 = new byte[4];
  250. for (uint j = 1; j < sBlocksLen; ++j)
  251. {
  252. int sOff = (int)(j * BLOCK_SIZE);
  253. Array.Copy(R, 0, sBlocks, sOff, BLOCK_SIZE);
  254. Pack.UInt32_To_BE(j, uint32, 0);
  255. xor(uint32, 0, sBlocks, sOff + BLOCK_SIZE - 4, 4);
  256. cipher.ProcessBlock(sBlocks, sOff, sBlocks, sOff);
  257. }
  258. }
  259. // iv.
  260. return num(sBlocks, 0, d);
  261. }
  262. protected static BigInteger calculateY_FF3(IBlockCipher cipher, BigInteger bigRadix, byte[] T, int wOff, uint round, ushort[] AB)
  263. {
  264. // ii.
  265. byte[] P = new byte[BLOCK_SIZE];
  266. Pack.UInt32_To_BE(round, P, 0);
  267. xor(T, wOff, P, 0, 4);
  268. BigInteger numAB = num(bigRadix, AB);
  269. byte[] bytesAB = BigIntegers.AsUnsignedByteArray(numAB);
  270. if ((P.Length - bytesAB.Length) < 4) // to be sure...
  271. {
  272. throw new InvalidOperationException("input out of range");
  273. }
  274. Array.Copy(bytesAB, 0, P, P.Length - bytesAB.Length, bytesAB.Length);
  275. // iii.
  276. rev(P);
  277. cipher.ProcessBlock(P, 0, P, 0);
  278. rev(P);
  279. byte[] S = P;
  280. // iv.
  281. return num(S, 0, S.Length);
  282. }
  283. protected static void checkArgs(IBlockCipher cipher, bool isFF1, int radix, ushort[] buf, int off, int len)
  284. {
  285. checkCipher(cipher);
  286. if (radix < 2 || radix > (1 << 16))
  287. {
  288. throw new ArgumentException();
  289. }
  290. checkData(isFF1, radix, buf, off, len);
  291. }
  292. protected static void checkArgs(IBlockCipher cipher, bool isFF1, int radix, byte[] buf, int off, int len)
  293. {
  294. checkCipher(cipher);
  295. if (radix < 2 || radix > (1 << 8))
  296. {
  297. throw new ArgumentException();
  298. }
  299. checkData(isFF1, radix, buf, off, len);
  300. }
  301. protected static void checkCipher(IBlockCipher cipher)
  302. {
  303. if (BLOCK_SIZE != cipher.GetBlockSize())
  304. {
  305. throw new ArgumentException();
  306. }
  307. }
  308. protected static void checkData(bool isFF1, int radix, ushort[] buf, int off, int len)
  309. {
  310. checkLength(isFF1, radix, len);
  311. for (int i = 0; i < len; ++i)
  312. {
  313. int b = buf[off + i] & 0xFFFF;
  314. if (b >= radix)
  315. {
  316. throw new ArgumentException("input data outside of radix");
  317. }
  318. }
  319. }
  320. protected static void checkData(bool isFF1, int radix, byte[] buf, int off, int len)
  321. {
  322. checkLength(isFF1, radix, len);
  323. for (int i = 0; i < len; ++i)
  324. {
  325. int b = buf[off + i] & 0xFF;
  326. if (b >= radix)
  327. {
  328. throw new ArgumentException("input data outside of radix");
  329. }
  330. }
  331. }
  332. private static void checkLength(bool isFF1, int radix, int len)
  333. {
  334. if (len < 2 || System.Math.Pow(radix, len) < 1000000)
  335. {
  336. throw new ArgumentException("input too short");
  337. }
  338. if (!isFF1)
  339. {
  340. int maxLen = 2 * (int)(System.Math.Floor(System.Math.Log(TWO_TO_96) / System.Math.Log(radix)));
  341. if (len > maxLen)
  342. {
  343. throw new ArgumentException("maximum input length is " + maxLen);
  344. }
  345. }
  346. }
  347. protected static byte[] implDecryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  348. {
  349. // Algorithm 10
  350. byte[] T = tweak64;
  351. int n = len;
  352. int v = n / 2, u = n - v;
  353. ushort[] A = toShort(buf, off, u);
  354. ushort[] B = toShort(buf, off + u, v);
  355. ushort[] rv = decFF3_1(cipher, radix, T, n, v, u, A, B);
  356. return toByte(rv);
  357. }
  358. protected static ushort[] implDecryptFF3w(IBlockCipher cipher, int radix, byte[] tweak64, ushort[] buf, int off, int len)
  359. {
  360. // Algorithm 10
  361. byte[] T = tweak64;
  362. int n = len;
  363. int v = n / 2, u = n - v;
  364. ushort[] A = new ushort[u];
  365. ushort[] B = new ushort[v];
  366. Array.Copy(buf, off, A, 0, u);
  367. Array.Copy(buf, off + u, B, 0, v);
  368. return decFF3_1(cipher, radix, T, n, v, u, A, B);
  369. }
  370. private static ushort[] decFF3_1(IBlockCipher cipher, int radix, byte[] T, int n, int v, int u, ushort[] A, ushort[] B)
  371. {
  372. BigInteger bigRadix = BigInteger.ValueOf(radix);
  373. BigInteger[] modVU = calculateModUV(bigRadix, v, u);
  374. int m = u;
  375. // Note we keep A, B in reverse order throughout
  376. rev(A);
  377. rev(B);
  378. for (int i = 7; i >= 0; --i)
  379. {
  380. // i.
  381. m = n - m;
  382. BigInteger modulus = modVU[1 - (i & 1)];
  383. int wOff = 4 - ((i & 1) * 4);
  384. // ii. - iv.
  385. BigInteger y = calculateY_FF3(cipher, bigRadix, T, wOff, (uint)i, A);
  386. // v.
  387. BigInteger c = num(bigRadix, B).Subtract(y).Mod(modulus);
  388. // vi. - viii.
  389. ushort[] C = B;
  390. B = A;
  391. A = C;
  392. str(bigRadix, c, m, C, 0);
  393. }
  394. rev(A);
  395. rev(B);
  396. return Arrays.Concatenate(A, B);
  397. }
  398. protected static byte[] implEncryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  399. {
  400. // Algorithm 9
  401. byte[] T = tweak64;
  402. int n = len;
  403. int v = n / 2, u = n - v;
  404. ushort[] A = toShort(buf, off, u);
  405. ushort[] B = toShort(buf, off + u, v);
  406. ushort[] rv = encFF3_1(cipher, radix, T, n, v, u, A, B);
  407. return toByte(rv);
  408. }
  409. protected static ushort[] implEncryptFF3w(IBlockCipher cipher, int radix, byte[] tweak64, ushort[] buf, int off, int len)
  410. {
  411. // Algorithm 9
  412. byte[] T = tweak64;
  413. int n = len;
  414. int v = n / 2, u = n - v;
  415. ushort[] A = new ushort[u];
  416. ushort[] B = new ushort[v];
  417. Array.Copy(buf, off, A, 0, u);
  418. Array.Copy(buf, off + u, B, 0, v);
  419. return encFF3_1(cipher, radix, T, n, v, u, A, B);
  420. }
  421. private static ushort[] encFF3_1(IBlockCipher cipher, int radix, byte[] t, int n, int v, int u, ushort[] a, ushort[] b)
  422. {
  423. BigInteger bigRadix = BigInteger.ValueOf(radix);
  424. BigInteger[] modVU = calculateModUV(bigRadix, v, u);
  425. int m = v;
  426. // Note we keep A, B in reverse order throughout
  427. rev(a);
  428. rev(b);
  429. for (uint i = 0; i < 8; ++i)
  430. {
  431. // i.
  432. m = n - m;
  433. BigInteger modulus = modVU[1 - (i & 1)];
  434. int wOff = 4 - (int)((i & 1) * 4);
  435. // ii. - iv.
  436. BigInteger y = calculateY_FF3(cipher, bigRadix, t, wOff, i, b);
  437. // v.
  438. BigInteger c = num(bigRadix, a).Add(y).Mod(modulus);
  439. // vi. - viii.
  440. ushort[] C = a;
  441. a = b;
  442. b = C;
  443. str(bigRadix, c, m, C, 0);
  444. }
  445. rev(a);
  446. rev(b);
  447. return Arrays.Concatenate(a, b);
  448. }
  449. protected static BigInteger num(byte[] buf, int off, int len)
  450. {
  451. return new BigInteger(1, Arrays.CopyOfRange(buf, off, off + len));
  452. }
  453. protected static BigInteger num(BigInteger R, ushort[] x)
  454. {
  455. BigInteger result = BigInteger.Zero;
  456. for (int i = 0; i < x.Length; ++i)
  457. {
  458. result = result.Multiply(R).Add(BigInteger.ValueOf(x[i] & 0xFFFF));
  459. }
  460. return result;
  461. }
  462. protected static byte[] prf(IBlockCipher c, byte[] x)
  463. {
  464. if ((x.Length % BLOCK_SIZE) != 0)
  465. {
  466. throw new ArgumentException();
  467. }
  468. int m = x.Length / BLOCK_SIZE;
  469. byte[] y = new byte[BLOCK_SIZE];
  470. for (int i = 0; i < m; ++i)
  471. {
  472. xor(x, i * BLOCK_SIZE, y, 0, BLOCK_SIZE);
  473. c.ProcessBlock(y, 0, y, 0);
  474. }
  475. return y;
  476. }
  477. // protected static void rev(byte[] x, int xOff, byte[] y, int yOff, int len)
  478. // {
  479. // for (int i = 1; i <= len; ++i)
  480. // {
  481. // y[yOff + len - i] = x[xOff + i - 1];
  482. // }
  483. // }
  484. protected static void rev(byte[] x)
  485. {
  486. int half = x.Length / 2, end = x.Length - 1;
  487. for (int i = 0; i < half; ++i)
  488. {
  489. byte tmp = x[i];
  490. x[i] = x[end - i];
  491. x[end - i] = tmp;
  492. }
  493. }
  494. protected static void rev(ushort[] x)
  495. {
  496. int half = x.Length / 2, end = x.Length - 1;
  497. for (int i = 0; i < half; ++i)
  498. {
  499. ushort tmp = x[i];
  500. x[i] = x[end - i];
  501. x[end - i] = tmp;
  502. }
  503. }
  504. protected static void str(BigInteger R, BigInteger x, int m, ushort[] output, int off)
  505. {
  506. if (x.SignValue < 0)
  507. {
  508. throw new ArgumentException();
  509. }
  510. for (int i = 1; i <= m; ++i)
  511. {
  512. BigInteger[] qr = x.DivideAndRemainder(R);
  513. output[off + m - i] = (ushort)qr[1].IntValue;
  514. x = qr[0];
  515. }
  516. if (x.SignValue != 0)
  517. {
  518. throw new ArgumentException();
  519. }
  520. }
  521. protected static void xor(byte[] x, int xOff, byte[] y, int yOff, int len)
  522. {
  523. for (int i = 0; i < len; ++i)
  524. {
  525. y[yOff + i] ^= x[xOff + i];
  526. }
  527. }
  528. private static byte[] toByte(ushort[] buf)
  529. {
  530. byte[] s = new byte[buf.Length];
  531. for (int i = 0; i != s.Length; i++)
  532. {
  533. s[i] = (byte)buf[i];
  534. }
  535. return s;
  536. }
  537. private static ushort[] toShort(byte[] buf, int off, int len)
  538. {
  539. ushort[] s = new ushort[len];
  540. for (int i = 0; i != s.Length; i++)
  541. {
  542. s[i] = (ushort)(buf[off + i] & 0xFF);
  543. }
  544. return s;
  545. }
  546. private static int Ceil(double v)
  547. {
  548. int rv = (int)v;
  549. if ((double)rv < v)
  550. return rv + 1;
  551. return rv;
  552. }
  553. }
  554. }
  555. #pragma warning restore
  556. #endif