GcmUtilities.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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.Raw;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm
  8. {
  9. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.NullChecks, false)]
  10. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.ArrayBoundsChecks, false)]
  11. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.DivideByZeroChecks, false)]
  12. [BestHTTP.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  13. internal static class GcmUtilities
  14. {
  15. private const uint E1 = 0xe1000000;
  16. private const ulong E1UL = (ulong)E1 << 32;
  17. internal static byte[] OneAsBytes()
  18. {
  19. byte[] tmp = new byte[16];
  20. tmp[0] = 0x80;
  21. return tmp;
  22. }
  23. internal static uint[] OneAsUints()
  24. {
  25. uint[] tmp = new uint[4];
  26. tmp[0] = 0x80000000;
  27. return tmp;
  28. }
  29. internal static ulong[] OneAsUlongs()
  30. {
  31. ulong[] tmp = new ulong[2];
  32. tmp[0] = 1UL << 63;
  33. return tmp;
  34. }
  35. internal static byte[] AsBytes(uint[] x)
  36. {
  37. return Pack.UInt32_To_BE(x);
  38. }
  39. internal static void AsBytes(uint[] x, byte[] z)
  40. {
  41. Pack.UInt32_To_BE(x, z, 0);
  42. }
  43. internal static byte[] AsBytes(ulong[] x)
  44. {
  45. byte[] z = new byte[16];
  46. Pack.UInt64_To_BE(x, z, 0);
  47. return z;
  48. }
  49. internal static void AsBytes(ulong[] x, byte[] z)
  50. {
  51. Pack.UInt64_To_BE(x, z, 0);
  52. }
  53. internal static uint[] AsUints(byte[] bs)
  54. {
  55. uint[] output = new uint[4];
  56. Pack.BE_To_UInt32(bs, 0, output);
  57. return output;
  58. }
  59. internal static void AsUints(byte[] bs, uint[] output)
  60. {
  61. Pack.BE_To_UInt32(bs, 0, output);
  62. }
  63. internal static ulong[] AsUlongs(byte[] x)
  64. {
  65. ulong[] z = new ulong[2];
  66. Pack.BE_To_UInt64(x, 0, z);
  67. return z;
  68. }
  69. public static void AsUlongs(byte[] x, ulong[] z)
  70. {
  71. Pack.BE_To_UInt64(x, 0, z);
  72. }
  73. internal static void AsUlongs(byte[] x, ulong[] z, int zOff)
  74. {
  75. Pack.BE_To_UInt64(x, 0, z, zOff, 2);
  76. }
  77. internal static void Copy(uint[] x, uint[] z)
  78. {
  79. z[0] = x[0];
  80. z[1] = x[1];
  81. z[2] = x[2];
  82. z[3] = x[3];
  83. }
  84. internal static void Copy(ulong[] x, ulong[] z)
  85. {
  86. z[0] = x[0];
  87. z[1] = x[1];
  88. }
  89. internal static void Copy(ulong[] x, int xOff, ulong[] z, int zOff)
  90. {
  91. z[zOff + 0] = x[xOff + 0];
  92. z[zOff + 1] = x[xOff + 1];
  93. }
  94. internal static void DivideP(ulong[] x, ulong[] z)
  95. {
  96. ulong x0 = x[0], x1 = x[1];
  97. ulong m = (ulong)((long)x0 >> 63);
  98. x0 ^= (m & E1UL);
  99. z[0] = (x0 << 1) | (x1 >> 63);
  100. z[1] = (x1 << 1) | (ulong)(-(long)m);
  101. }
  102. internal static void DivideP(ulong[] x, int xOff, ulong[] z, int zOff)
  103. {
  104. ulong x0 = x[xOff + 0], x1 = x[xOff + 1];
  105. ulong m = (ulong)((long)x0 >> 63);
  106. x0 ^= (m & E1UL);
  107. z[zOff + 0] = (x0 << 1) | (x1 >> 63);
  108. z[zOff + 1] = (x1 << 1) | (ulong)(-(long)m);
  109. }
  110. internal static void Multiply(byte[] x, byte[] y)
  111. {
  112. ulong[] t1 = AsUlongs(x);
  113. ulong[] t2 = AsUlongs(y);
  114. Multiply(t1, t2);
  115. AsBytes(t1, x);
  116. }
  117. internal static void Multiply(uint[] x, uint[] y)
  118. {
  119. uint y0 = y[0], y1 = y[1], y2 = y[2], y3 = y[3];
  120. uint z0 = 0, z1 = 0, z2 = 0, z3 = 0;
  121. for (int i = 0; i < 4; ++i)
  122. {
  123. int bits = (int)x[i];
  124. for (int j = 0; j < 32; ++j)
  125. {
  126. uint m1 = (uint)(bits >> 31); bits <<= 1;
  127. z0 ^= y0 & m1;
  128. z1 ^= y1 & m1;
  129. z2 ^= y2 & m1;
  130. z3 ^= y3 & m1;
  131. uint m2 = (uint)((int)(y3 << 31) >> 8);
  132. y3 = (y3 >> 1) | (y2 << 31);
  133. y2 = (y2 >> 1) | (y1 << 31);
  134. y1 = (y1 >> 1) | (y0 << 31);
  135. y0 = (y0 >> 1) ^ (m2 & E1);
  136. }
  137. }
  138. x[0] = z0;
  139. x[1] = z1;
  140. x[2] = z2;
  141. x[3] = z3;
  142. }
  143. internal static void Multiply(ulong[] x, ulong[] y)
  144. {
  145. //ulong x0 = x[0], x1 = x[1];
  146. //ulong y0 = y[0], y1 = y[1];
  147. //ulong z0 = 0, z1 = 0, z2 = 0;
  148. //for (int j = 0; j < 64; ++j)
  149. //{
  150. // ulong m0 = (ulong)((long)x0 >> 63); x0 <<= 1;
  151. // z0 ^= y0 & m0;
  152. // z1 ^= y1 & m0;
  153. // ulong m1 = (ulong)((long)x1 >> 63); x1 <<= 1;
  154. // z1 ^= y0 & m1;
  155. // z2 ^= y1 & m1;
  156. // ulong c = (ulong)((long)(y1 << 63) >> 8);
  157. // y1 = (y1 >> 1) | (y0 << 63);
  158. // y0 = (y0 >> 1) ^ (c & E1UL);
  159. //}
  160. //z0 ^= z2 ^ (z2 >> 1) ^ (z2 >> 2) ^ (z2 >> 7);
  161. //z1 ^= (z2 << 63) ^ (z2 << 62) ^ (z2 << 57);
  162. //x[0] = z0;
  163. //x[1] = z1;
  164. /*
  165. * "Three-way recursion" as described in "Batch binary Edwards", Daniel J. Bernstein.
  166. *
  167. * Without access to the high part of a 64x64 product x * y, we use a bit reversal to calculate it:
  168. * rev(x) * rev(y) == rev((x * y) << 1)
  169. */
  170. ulong x0 = x[0], x1 = x[1];
  171. ulong y0 = y[0], y1 = y[1];
  172. ulong x0r = Longs.Reverse(x0), x1r = Longs.Reverse(x1);
  173. ulong y0r = Longs.Reverse(y0), y1r = Longs.Reverse(y1);
  174. ulong h0 = Longs.Reverse(ImplMul64(x0r, y0r));
  175. ulong h1 = ImplMul64(x0, y0) << 1;
  176. ulong h2 = Longs.Reverse(ImplMul64(x1r, y1r));
  177. ulong h3 = ImplMul64(x1, y1) << 1;
  178. ulong h4 = Longs.Reverse(ImplMul64(x0r ^ x1r, y0r ^ y1r));
  179. ulong h5 = ImplMul64(x0 ^ x1, y0 ^ y1) << 1;
  180. ulong z0 = h0;
  181. ulong z1 = h1 ^ h0 ^ h2 ^ h4;
  182. ulong z2 = h2 ^ h1 ^ h3 ^ h5;
  183. ulong z3 = h3;
  184. z1 ^= z3 ^ (z3 >> 1) ^ (z3 >> 2) ^ (z3 >> 7);
  185. // z2 ^= (z3 << 63) ^ (z3 << 62) ^ (z3 << 57);
  186. z2 ^= (z3 << 62) ^ (z3 << 57);
  187. z0 ^= z2 ^ (z2 >> 1) ^ (z2 >> 2) ^ (z2 >> 7);
  188. z1 ^= (z2 << 63) ^ (z2 << 62) ^ (z2 << 57);
  189. x[0] = z0;
  190. x[1] = z1;
  191. }
  192. internal static void MultiplyP(uint[] x)
  193. {
  194. uint x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
  195. uint m = (uint)((int)(x3 << 31) >> 31);
  196. x[0] = (x0 >> 1) ^ (m & E1);
  197. x[1] = (x1 >> 1) | (x0 << 31);
  198. x[2] = (x2 >> 1) | (x1 << 31);
  199. x[3] = (x3 >> 1) | (x2 << 31);
  200. }
  201. internal static void MultiplyP(uint[] x, uint[] z)
  202. {
  203. uint x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
  204. uint m = (uint)((int)(x3 << 31) >> 31);
  205. z[0] = (x0 >> 1) ^ (m & E1);
  206. z[1] = (x1 >> 1) | (x0 << 31);
  207. z[2] = (x2 >> 1) | (x1 << 31);
  208. z[3] = (x3 >> 1) | (x2 << 31);
  209. }
  210. internal static void MultiplyP(ulong[] x)
  211. {
  212. ulong x0 = x[0], x1 = x[1];
  213. ulong m = (ulong)((long)(x1 << 63) >> 63);
  214. x[0] = (x0 >> 1) ^ (m & E1UL);
  215. x[1] = (x1 >> 1) | (x0 << 63);
  216. }
  217. internal static void MultiplyP(ulong[] x, ulong[] z)
  218. {
  219. ulong x0 = x[0], x1 = x[1];
  220. ulong m = (ulong)((long)(x1 << 63) >> 63);
  221. z[0] = (x0 >> 1) ^ (m & E1UL);
  222. z[1] = (x1 >> 1) | (x0 << 63);
  223. }
  224. internal static void MultiplyP3(ulong[] x, ulong[] z)
  225. {
  226. ulong x0 = x[0], x1 = x[1];
  227. ulong c = x1 << 61;
  228. z[0] = (x0 >> 3) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  229. z[1] = (x1 >> 3) | (x0 << 61);
  230. }
  231. internal static void MultiplyP3(ulong[] x, int xOff, ulong[] z, int zOff)
  232. {
  233. ulong x0 = x[xOff + 0], x1 = x[xOff + 1];
  234. ulong c = x1 << 61;
  235. z[zOff + 0] = (x0 >> 3) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  236. z[zOff + 1] = (x1 >> 3) | (x0 << 61);
  237. }
  238. internal static void MultiplyP4(ulong[] x, ulong[] z)
  239. {
  240. ulong x0 = x[0], x1 = x[1];
  241. ulong c = x1 << 60;
  242. z[0] = (x0 >> 4) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  243. z[1] = (x1 >> 4) | (x0 << 60);
  244. }
  245. internal static void MultiplyP4(ulong[] x, int xOff, ulong[] z, int zOff)
  246. {
  247. ulong x0 = x[xOff + 0], x1 = x[xOff + 1];
  248. ulong c = x1 << 60;
  249. z[zOff + 0] = (x0 >> 4) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  250. z[zOff + 1] = (x1 >> 4) | (x0 << 60);
  251. }
  252. internal static void MultiplyP7(ulong[] x, ulong[] z)
  253. {
  254. ulong x0 = x[0], x1 = x[1];
  255. ulong c = x1 << 57;
  256. z[0] = (x0 >> 7) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  257. z[1] = (x1 >> 7) | (x0 << 57);
  258. }
  259. internal static void MultiplyP7(ulong[] x, int xOff, ulong[] z, int zOff)
  260. {
  261. ulong x0 = x[xOff + 0], x1 = x[xOff + 1];
  262. ulong c = x1 << 57;
  263. z[zOff + 0] = (x0 >> 7) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  264. z[zOff + 1] = (x1 >> 7) | (x0 << 57);
  265. }
  266. internal static void MultiplyP8(uint[] x)
  267. {
  268. uint x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
  269. uint c = x3 << 24;
  270. x[0] = (x0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  271. x[1] = (x1 >> 8) | (x0 << 24);
  272. x[2] = (x2 >> 8) | (x1 << 24);
  273. x[3] = (x3 >> 8) | (x2 << 24);
  274. }
  275. internal static void MultiplyP8(uint[] x, uint[] y)
  276. {
  277. uint x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
  278. uint c = x3 << 24;
  279. y[0] = (x0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  280. y[1] = (x1 >> 8) | (x0 << 24);
  281. y[2] = (x2 >> 8) | (x1 << 24);
  282. y[3] = (x3 >> 8) | (x2 << 24);
  283. }
  284. internal static void MultiplyP8(ulong[] x)
  285. {
  286. ulong x0 = x[0], x1 = x[1];
  287. ulong c = x1 << 56;
  288. x[0] = (x0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  289. x[1] = (x1 >> 8) | (x0 << 56);
  290. }
  291. internal static void MultiplyP8(ulong[] x, ulong[] y)
  292. {
  293. ulong x0 = x[0], x1 = x[1];
  294. ulong c = x1 << 56;
  295. y[0] = (x0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  296. y[1] = (x1 >> 8) | (x0 << 56);
  297. }
  298. internal static void MultiplyP8(ulong[] x, int xOff, ulong[] y, int yOff)
  299. {
  300. ulong x0 = x[xOff + 0], x1 = x[xOff + 1];
  301. ulong c = x1 << 56;
  302. y[yOff + 0] = (x0 >> 8) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
  303. y[yOff + 1] = (x1 >> 8) | (x0 << 56);
  304. }
  305. internal static void Square(ulong[] x, ulong[] z)
  306. {
  307. ulong[] t = new ulong[4];
  308. Interleave.Expand64To128Rev(x[0], t, 0);
  309. Interleave.Expand64To128Rev(x[1], t, 2);
  310. ulong z0 = t[0], z1 = t[1], z2 = t[2], z3 = t[3];
  311. z1 ^= z3 ^ (z3 >> 1) ^ (z3 >> 2) ^ (z3 >> 7);
  312. // z2 ^= (z3 << 63) ^ (z3 << 62) ^ (z3 << 57);
  313. z2 ^= (z3 << 62) ^ (z3 << 57);
  314. z0 ^= z2 ^ (z2 >> 1) ^ (z2 >> 2) ^ (z2 >> 7);
  315. z1 ^= (z2 << 63) ^ (z2 << 62) ^ (z2 << 57);
  316. z[0] = z0;
  317. z[1] = z1;
  318. }
  319. internal static void Xor(byte[] x, byte[] y)
  320. {
  321. int i = 0;
  322. do
  323. {
  324. x[i] ^= y[i]; ++i;
  325. x[i] ^= y[i]; ++i;
  326. x[i] ^= y[i]; ++i;
  327. x[i] ^= y[i]; ++i;
  328. }
  329. while (i < 16);
  330. }
  331. internal static void Xor(byte[] x, byte[] y, int yOff)
  332. {
  333. int i = 0;
  334. do
  335. {
  336. x[i] ^= y[yOff + i]; ++i;
  337. x[i] ^= y[yOff + i]; ++i;
  338. x[i] ^= y[yOff + i]; ++i;
  339. x[i] ^= y[yOff + i]; ++i;
  340. }
  341. while (i < 16);
  342. }
  343. internal static void Xor(byte[] x, int xOff, byte[] y, int yOff, byte[] z, int zOff)
  344. {
  345. int i = 0;
  346. do
  347. {
  348. z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]); ++i;
  349. z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]); ++i;
  350. z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]); ++i;
  351. z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]); ++i;
  352. }
  353. while (i < 16);
  354. }
  355. internal static void Xor(byte[] x, byte[] y, int yOff, int yLen)
  356. {
  357. while (--yLen >= 0)
  358. {
  359. x[yLen] ^= y[yOff + yLen];
  360. }
  361. }
  362. internal static void Xor(byte[] x, int xOff, byte[] y, int yOff, int len)
  363. {
  364. while (--len >= 0)
  365. {
  366. x[xOff + len] ^= y[yOff + len];
  367. }
  368. }
  369. internal static void Xor(byte[] x, byte[] y, byte[] z)
  370. {
  371. int i = 0;
  372. do
  373. {
  374. z[i] = (byte)(x[i] ^ y[i]); ++i;
  375. z[i] = (byte)(x[i] ^ y[i]); ++i;
  376. z[i] = (byte)(x[i] ^ y[i]); ++i;
  377. z[i] = (byte)(x[i] ^ y[i]); ++i;
  378. }
  379. while (i < 16);
  380. }
  381. internal static void Xor(uint[] x, uint[] y)
  382. {
  383. x[0] ^= y[0];
  384. x[1] ^= y[1];
  385. x[2] ^= y[2];
  386. x[3] ^= y[3];
  387. }
  388. internal static void Xor(uint[] x, uint[] y, uint[] z)
  389. {
  390. z[0] = x[0] ^ y[0];
  391. z[1] = x[1] ^ y[1];
  392. z[2] = x[2] ^ y[2];
  393. z[3] = x[3] ^ y[3];
  394. }
  395. internal static void Xor(ulong[] x, ulong[] y)
  396. {
  397. x[0] ^= y[0];
  398. x[1] ^= y[1];
  399. }
  400. internal static void Xor(ulong[] x, int xOff, ulong[] y, int yOff)
  401. {
  402. x[xOff + 0] ^= y[yOff + 0];
  403. x[xOff + 1] ^= y[yOff + 1];
  404. }
  405. internal static void Xor(ulong[] x, ulong[] y, ulong[] z)
  406. {
  407. z[0] = x[0] ^ y[0];
  408. z[1] = x[1] ^ y[1];
  409. }
  410. internal static void Xor(ulong[] x, int xOff, ulong[] y, int yOff, ulong[] z, int zOff)
  411. {
  412. z[zOff + 0] = x[xOff + 0] ^ y[yOff + 0];
  413. z[zOff + 1] = x[xOff + 1] ^ y[yOff + 1];
  414. }
  415. private static ulong ImplMul64(ulong x, ulong y)
  416. {
  417. ulong x0 = x & 0x1111111111111111UL;
  418. ulong x1 = x & 0x2222222222222222UL;
  419. ulong x2 = x & 0x4444444444444444UL;
  420. ulong x3 = x & 0x8888888888888888UL;
  421. ulong y0 = y & 0x1111111111111111UL;
  422. ulong y1 = y & 0x2222222222222222UL;
  423. ulong y2 = y & 0x4444444444444444UL;
  424. ulong y3 = y & 0x8888888888888888UL;
  425. ulong z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
  426. ulong z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
  427. ulong z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
  428. ulong z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
  429. z0 &= 0x1111111111111111UL;
  430. z1 &= 0x2222222222222222UL;
  431. z2 &= 0x4444444444444444UL;
  432. z3 &= 0x8888888888888888UL;
  433. return z0 | z1 | z2 | z3;
  434. }
  435. }
  436. }
  437. #pragma warning restore
  438. #endif