SP80038G.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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.Math;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.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 = "Best.HTTP.SecureProtocol.Org.BouncyCastle.Fpe.Disable";
  21. internal static readonly string FF1_DISABLED = "Best.HTTP.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. int zeroes = -(t + b + 1) & 15;
  234. byte[] Q = new byte[t + zeroes + 1 + b];
  235. Array.Copy(T, 0, Q, 0, t);
  236. Q[t + zeroes] = (byte)round;
  237. BigInteger numAB = num(bigRadix, AB);
  238. BigIntegers.AsUnsignedByteArray(numAB, Q, Q.Length - b, b);
  239. // ii.
  240. byte[] R = prf(cipher, Arrays.Concatenate(P, Q));
  241. // iii.
  242. byte[] sBlocks = R;
  243. if (d > BLOCK_SIZE)
  244. {
  245. int sBlocksLen = (d + BLOCK_SIZE - 1) / BLOCK_SIZE;
  246. sBlocks = new byte[sBlocksLen * BLOCK_SIZE];
  247. uint j0 = Pack.BE_To_UInt32(R, BLOCK_SIZE - 4);
  248. Array.Copy(R, 0, sBlocks, 0, BLOCK_SIZE);
  249. for (uint j = 1; j < sBlocksLen; ++j)
  250. {
  251. int sOff = (int)(j * BLOCK_SIZE);
  252. Array.Copy(R, 0, sBlocks, sOff, BLOCK_SIZE - 4);
  253. Pack.UInt32_To_BE(j0 ^ j, sBlocks, sOff + BLOCK_SIZE - 4);
  254. cipher.ProcessBlock(sBlocks, sOff, sBlocks, sOff);
  255. }
  256. }
  257. // iv.
  258. return new BigInteger(1, sBlocks, 0, d);
  259. }
  260. protected static BigInteger calculateY_FF3(IBlockCipher cipher, BigInteger bigRadix, byte[] T, int wOff,
  261. uint round, ushort[] AB)
  262. {
  263. // ii.
  264. byte[] P = new byte[BLOCK_SIZE];
  265. Pack.UInt32_To_BE(Pack.BE_To_UInt32(T, wOff) ^ round, P, 0);
  266. BigInteger numAB = num(bigRadix, AB);
  267. BigIntegers.AsUnsignedByteArray(numAB, P, 4, BLOCK_SIZE - 4);
  268. // iii.
  269. Array.Reverse(P);
  270. cipher.ProcessBlock(P, 0, P, 0);
  271. Array.Reverse(P);
  272. byte[] S = P;
  273. // iv.
  274. return new BigInteger(1, S);
  275. }
  276. protected static void checkArgs(IBlockCipher cipher, bool isFF1, int radix, ushort[] buf, int off, int len)
  277. {
  278. checkCipher(cipher);
  279. if (radix < 2 || radix > (1 << 16))
  280. {
  281. throw new ArgumentException();
  282. }
  283. checkData(isFF1, radix, buf, off, len);
  284. }
  285. protected static void checkArgs(IBlockCipher cipher, bool isFF1, int radix, byte[] buf, int off, int len)
  286. {
  287. checkCipher(cipher);
  288. if (radix < 2 || radix > (1 << 8))
  289. {
  290. throw new ArgumentException();
  291. }
  292. checkData(isFF1, radix, buf, off, len);
  293. }
  294. protected static void checkCipher(IBlockCipher cipher)
  295. {
  296. if (BLOCK_SIZE != cipher.GetBlockSize())
  297. {
  298. throw new ArgumentException();
  299. }
  300. }
  301. protected static void checkData(bool isFF1, int radix, ushort[] buf, int off, int len)
  302. {
  303. checkLength(isFF1, radix, len);
  304. for (int i = 0; i < len; ++i)
  305. {
  306. int b = buf[off + i] & 0xFFFF;
  307. if (b >= radix)
  308. {
  309. throw new ArgumentException("input data outside of radix");
  310. }
  311. }
  312. }
  313. protected static void checkData(bool isFF1, int radix, byte[] buf, int off, int len)
  314. {
  315. checkLength(isFF1, radix, len);
  316. for (int i = 0; i < len; ++i)
  317. {
  318. int b = buf[off + i] & 0xFF;
  319. if (b >= radix)
  320. {
  321. throw new ArgumentException("input data outside of radix");
  322. }
  323. }
  324. }
  325. private static void checkLength(bool isFF1, int radix, int len)
  326. {
  327. if (len < 2 || System.Math.Pow(radix, len) < 1000000)
  328. {
  329. throw new ArgumentException("input too short");
  330. }
  331. if (!isFF1)
  332. {
  333. int maxLen = 2 * (int)(System.Math.Floor(System.Math.Log(TWO_TO_96) / System.Math.Log(radix)));
  334. if (len > maxLen)
  335. {
  336. throw new ArgumentException("maximum input length is " + maxLen);
  337. }
  338. }
  339. }
  340. protected static byte[] implDecryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  341. {
  342. // Algorithm 10
  343. byte[] T = tweak64;
  344. int n = len;
  345. int v = n / 2, u = n - v;
  346. ushort[] A = toShort(buf, off, u);
  347. ushort[] B = toShort(buf, off + u, v);
  348. ushort[] rv = decFF3_1(cipher, radix, T, n, v, u, A, B);
  349. return toByte(rv);
  350. }
  351. protected static ushort[] implDecryptFF3w(IBlockCipher cipher, int radix, byte[] tweak64, ushort[] buf, int off, int len)
  352. {
  353. // Algorithm 10
  354. byte[] T = tweak64;
  355. int n = len;
  356. int v = n / 2, u = n - v;
  357. ushort[] A = new ushort[u];
  358. ushort[] B = new ushort[v];
  359. Array.Copy(buf, off, A, 0, u);
  360. Array.Copy(buf, off + u, B, 0, v);
  361. return decFF3_1(cipher, radix, T, n, v, u, A, B);
  362. }
  363. private static ushort[] decFF3_1(IBlockCipher cipher, int radix, byte[] T, int n, int v, int u, ushort[] A, ushort[] B)
  364. {
  365. BigInteger bigRadix = BigInteger.ValueOf(radix);
  366. BigInteger[] modVU = calculateModUV(bigRadix, v, u);
  367. int m = u;
  368. // Note we keep A, B in reverse order throughout
  369. Array.Reverse(A);
  370. Array.Reverse(B);
  371. for (int i = 7; i >= 0; --i)
  372. {
  373. // i.
  374. m = n - m;
  375. BigInteger modulus = modVU[1 - (i & 1)];
  376. int wOff = 4 - ((i & 1) * 4);
  377. // ii. - iv.
  378. BigInteger y = calculateY_FF3(cipher, bigRadix, T, wOff, (uint)i, A);
  379. // v.
  380. BigInteger c = num(bigRadix, B).Subtract(y).Mod(modulus);
  381. // vi. - viii.
  382. ushort[] C = B;
  383. B = A;
  384. A = C;
  385. str(bigRadix, c, m, C, 0);
  386. }
  387. Array.Reverse(A);
  388. Array.Reverse(B);
  389. return Arrays.Concatenate(A, B);
  390. }
  391. protected static byte[] implEncryptFF3(IBlockCipher cipher, int radix, byte[] tweak64, byte[] buf, int off, int len)
  392. {
  393. // Algorithm 9
  394. byte[] T = tweak64;
  395. int n = len;
  396. int v = n / 2, u = n - v;
  397. ushort[] A = toShort(buf, off, u);
  398. ushort[] B = toShort(buf, off + u, v);
  399. ushort[] rv = encFF3_1(cipher, radix, T, n, v, u, A, B);
  400. return toByte(rv);
  401. }
  402. protected static ushort[] implEncryptFF3w(IBlockCipher cipher, int radix, byte[] tweak64, ushort[] buf, int off, int len)
  403. {
  404. // Algorithm 9
  405. byte[] T = tweak64;
  406. int n = len;
  407. int v = n / 2, u = n - v;
  408. ushort[] A = new ushort[u];
  409. ushort[] B = new ushort[v];
  410. Array.Copy(buf, off, A, 0, u);
  411. Array.Copy(buf, off + u, B, 0, v);
  412. return encFF3_1(cipher, radix, T, n, v, u, A, B);
  413. }
  414. private static ushort[] encFF3_1(IBlockCipher cipher, int radix, byte[] t, int n, int v, int u, ushort[] a, ushort[] b)
  415. {
  416. BigInteger bigRadix = BigInteger.ValueOf(radix);
  417. BigInteger[] modVU = calculateModUV(bigRadix, v, u);
  418. int m = v;
  419. // Note we keep A, B in reverse order throughout
  420. Array.Reverse(a);
  421. Array.Reverse(b);
  422. for (uint i = 0; i < 8; ++i)
  423. {
  424. // i.
  425. m = n - m;
  426. BigInteger modulus = modVU[1 - (i & 1)];
  427. int wOff = 4 - (int)((i & 1) * 4);
  428. // ii. - iv.
  429. BigInteger y = calculateY_FF3(cipher, bigRadix, t, wOff, i, b);
  430. // v.
  431. BigInteger c = num(bigRadix, a).Add(y).Mod(modulus);
  432. // vi. - viii.
  433. ushort[] C = a;
  434. a = b;
  435. b = C;
  436. str(bigRadix, c, m, C, 0);
  437. }
  438. Array.Reverse(a);
  439. Array.Reverse(b);
  440. return Arrays.Concatenate(a, b);
  441. }
  442. protected static BigInteger num(BigInteger R, ushort[] x)
  443. {
  444. BigInteger result = BigInteger.Zero;
  445. for (int i = 0; i < x.Length; ++i)
  446. {
  447. result = result.Multiply(R).Add(BigInteger.ValueOf(x[i] & 0xFFFF));
  448. }
  449. return result;
  450. }
  451. protected static byte[] prf(IBlockCipher c, byte[] x)
  452. {
  453. if ((x.Length % BLOCK_SIZE) != 0)
  454. throw new ArgumentException();
  455. int m = x.Length / BLOCK_SIZE;
  456. byte[] y = new byte[BLOCK_SIZE];
  457. for (int i = 0; i < m; ++i)
  458. {
  459. xor(x, i * BLOCK_SIZE, y, 0, BLOCK_SIZE);
  460. c.ProcessBlock(y, 0, y, 0);
  461. }
  462. return y;
  463. }
  464. protected static void str(BigInteger R, BigInteger x, int m, ushort[] output, int off)
  465. {
  466. if (x.SignValue < 0)
  467. throw new ArgumentException();
  468. for (int i = 1; i <= m; ++i)
  469. {
  470. BigInteger[] qr = x.DivideAndRemainder(R);
  471. output[off + m - i] = (ushort)qr[1].IntValue;
  472. x = qr[0];
  473. }
  474. if (x.SignValue != 0)
  475. throw new ArgumentException();
  476. }
  477. protected static void xor(byte[] x, int xOff, byte[] y, int yOff, int len)
  478. {
  479. for (int i = 0; i < len; ++i)
  480. {
  481. y[yOff + i] ^= x[xOff + i];
  482. }
  483. }
  484. private static byte[] toByte(ushort[] buf)
  485. {
  486. byte[] s = new byte[buf.Length];
  487. for (int i = 0; i != s.Length; i++)
  488. {
  489. s[i] = (byte)buf[i];
  490. }
  491. return s;
  492. }
  493. private static ushort[] toShort(byte[] buf, int off, int len)
  494. {
  495. ushort[] s = new ushort[len];
  496. for (int i = 0; i != s.Length; i++)
  497. {
  498. s[i] = (ushort)(buf[off + i] & 0xFF);
  499. }
  500. return s;
  501. }
  502. private static int Ceil(double v)
  503. {
  504. int rv = (int)v;
  505. if ((double)rv < v)
  506. return rv + 1;
  507. return rv;
  508. }
  509. }
  510. }
  511. #pragma warning restore
  512. #endif