SerpentEngineBase.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  7. {
  8. public abstract class SerpentEngineBase
  9. : IBlockCipher
  10. {
  11. protected static readonly int BlockSize = 16;
  12. internal const int ROUNDS = 32;
  13. internal const int PHI = unchecked((int)0x9E3779B9); // (sqrt(5) - 1) * 2**31
  14. protected bool encrypting;
  15. protected int[] wKey;
  16. protected int X0, X1, X2, X3; // registers
  17. internal SerpentEngineBase()
  18. {
  19. }
  20. /**
  21. * initialise a Serpent cipher.
  22. *
  23. * @param encrypting whether or not we are for encryption.
  24. * @param params the parameters required to set up the cipher.
  25. * @throws IllegalArgumentException if the params argument is
  26. * inappropriate.
  27. */
  28. public virtual void Init(bool encrypting, ICipherParameters parameters)
  29. {
  30. if (!(parameters is KeyParameter))
  31. throw new ArgumentException("invalid parameter passed to " + AlgorithmName + " init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  32. this.encrypting = encrypting;
  33. this.wKey = MakeWorkingKey(((KeyParameter)parameters).GetKey());
  34. }
  35. public virtual string AlgorithmName
  36. {
  37. get { return "Serpent"; }
  38. }
  39. public virtual int GetBlockSize()
  40. {
  41. return BlockSize;
  42. }
  43. /**
  44. * Process one block of input from the array in and write it to
  45. * the out array.
  46. *
  47. * @param in the array containing the input data.
  48. * @param inOff offset into the in array the data starts at.
  49. * @param out the array the output data will be copied into.
  50. * @param outOff the offset into the out array the output will start at.
  51. * @return the number of bytes processed and produced.
  52. * @throws DataLengthException if there isn't enough data in in, or
  53. * space in out.
  54. * @throws IllegalStateException if the cipher isn't initialised.
  55. */
  56. public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  57. {
  58. if (wKey == null)
  59. throw new InvalidOperationException(AlgorithmName + " not initialised");
  60. Check.DataLength(input, inOff, BlockSize, "input buffer too short");
  61. Check.OutputLength(output, outOff, BlockSize, "output buffer too short");
  62. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  63. if (encrypting)
  64. {
  65. EncryptBlock(input.AsSpan(inOff), output.AsSpan(outOff));
  66. }
  67. else
  68. {
  69. DecryptBlock(input.AsSpan(inOff), output.AsSpan(outOff));
  70. }
  71. #else
  72. if (encrypting)
  73. {
  74. EncryptBlock(input, inOff, output, outOff);
  75. }
  76. else
  77. {
  78. DecryptBlock(input, inOff, output, outOff);
  79. }
  80. #endif
  81. return BlockSize;
  82. }
  83. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  84. public int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  85. {
  86. if (wKey == null)
  87. throw new InvalidOperationException(AlgorithmName + " not initialised");
  88. Check.DataLength(input, BlockSize, "input buffer too short");
  89. Check.OutputLength(output, BlockSize, "output buffer too short");
  90. if (encrypting)
  91. {
  92. EncryptBlock(input, output);
  93. }
  94. else
  95. {
  96. DecryptBlock(input, output);
  97. }
  98. return BlockSize;
  99. }
  100. #endif
  101. /*
  102. * The sboxes below are based on the work of Brian Gladman and
  103. * Sam Simpson, whose original notice appears below.
  104. * <p>
  105. * For further details see:
  106. * http://fp.gladman.plus.com/cryptography_technology/serpent/
  107. * </p>
  108. */
  109. /* Partially optimised Serpent S Box boolean functions derived */
  110. /* using a recursive descent analyser but without a full search */
  111. /* of all subtrees. This set of S boxes is the result of work */
  112. /* by Sam Simpson and Brian Gladman using the spare time on a */
  113. /* cluster of high capacity servers to search for S boxes with */
  114. /* this customised search engine. There are now an average of */
  115. /* 15.375 terms per S box. */
  116. /* */
  117. /* Copyright: Dr B. R Gladman (gladman@seven77.demon.co.uk) */
  118. /* and Sam Simpson (s.simpson@mia.co.uk) */
  119. /* 17th December 1998 */
  120. /* */
  121. /* We hereby give permission for information in this file to be */
  122. /* used freely subject only to acknowledgement of its origin. */
  123. /*
  124. * S0 - { 3, 8,15, 1,10, 6, 5,11,14,13, 4, 2, 7, 0, 9,12 } - 15 terms.
  125. */
  126. protected void Sb0(int a, int b, int c, int d)
  127. {
  128. int t1 = a ^ d;
  129. int t3 = c ^ t1;
  130. int t4 = b ^ t3;
  131. X3 = (a & d) ^ t4;
  132. int t7 = a ^ (b & t1);
  133. X2 = t4 ^ (c | t7);
  134. int t12 = X3 & (t3 ^ t7);
  135. X1 = (~t3) ^ t12;
  136. X0 = t12 ^ (~t7);
  137. }
  138. /**
  139. * InvSO - {13, 3,11, 0,10, 6, 5,12, 1,14, 4, 7,15, 9, 8, 2 } - 15 terms.
  140. */
  141. protected void Ib0(int a, int b, int c, int d)
  142. {
  143. int t1 = ~a;
  144. int t2 = a ^ b;
  145. int t4 = d ^ (t1 | t2);
  146. int t5 = c ^ t4;
  147. X2 = t2 ^ t5;
  148. int t8 = t1 ^ (d & t2);
  149. X1 = t4 ^ (X2 & t8);
  150. X3 = (a & t4) ^ (t5 | X1);
  151. X0 = X3 ^ (t5 ^ t8);
  152. }
  153. /**
  154. * S1 - {15,12, 2, 7, 9, 0, 5,10, 1,11,14, 8, 6,13, 3, 4 } - 14 terms.
  155. */
  156. protected void Sb1(int a, int b, int c, int d)
  157. {
  158. int t2 = b ^ (~a);
  159. int t5 = c ^ (a | t2);
  160. X2 = d ^ t5;
  161. int t7 = b ^ (d | t2);
  162. int t8 = t2 ^ X2;
  163. X3 = t8 ^ (t5 & t7);
  164. int t11 = t5 ^ t7;
  165. X1 = X3 ^ t11;
  166. X0 = t5 ^ (t8 & t11);
  167. }
  168. /**
  169. * InvS1 - { 5, 8, 2,14,15, 6,12, 3,11, 4, 7, 9, 1,13,10, 0 } - 14 steps.
  170. */
  171. protected void Ib1(int a, int b, int c, int d)
  172. {
  173. int t1 = b ^ d;
  174. int t3 = a ^ (b & t1);
  175. int t4 = t1 ^ t3;
  176. X3 = c ^ t4;
  177. int t7 = b ^ (t1 & t3);
  178. int t8 = X3 | t7;
  179. X1 = t3 ^ t8;
  180. int t10 = ~X1;
  181. int t11 = X3 ^ t7;
  182. X0 = t10 ^ t11;
  183. X2 = t4 ^ (t10 | t11);
  184. }
  185. /**
  186. * S2 - { 8, 6, 7, 9, 3,12,10,15,13, 1,14, 4, 0,11, 5, 2 } - 16 terms.
  187. */
  188. protected void Sb2(int a, int b, int c, int d)
  189. {
  190. int t1 = ~a;
  191. int t2 = b ^ d;
  192. int t3 = c & t1;
  193. X0 = t2 ^ t3;
  194. int t5 = c ^ t1;
  195. int t6 = c ^ X0;
  196. int t7 = b & t6;
  197. X3 = t5 ^ t7;
  198. X2 = a ^ ((d | t7) & (X0 | t5));
  199. X1 = (t2 ^ X3) ^ (X2 ^ (d | t1));
  200. }
  201. /**
  202. * InvS2 - {12, 9,15, 4,11,14, 1, 2, 0, 3, 6,13, 5, 8,10, 7 } - 16 steps.
  203. */
  204. protected void Ib2(int a, int b, int c, int d)
  205. {
  206. int t1 = b ^ d;
  207. int t2 = ~t1;
  208. int t3 = a ^ c;
  209. int t4 = c ^ t1;
  210. int t5 = b & t4;
  211. X0 = t3 ^ t5;
  212. int t7 = a | t2;
  213. int t8 = d ^ t7;
  214. int t9 = t3 | t8;
  215. X3 = t1 ^ t9;
  216. int t11 = ~t4;
  217. int t12 = X0 | X3;
  218. X1 = t11 ^ t12;
  219. X2 = (d & t11) ^ (t3 ^ t12);
  220. }
  221. /**
  222. * S3 - { 0,15,11, 8,12, 9, 6, 3,13, 1, 2, 4,10, 7, 5,14 } - 16 terms.
  223. */
  224. protected void Sb3(int a, int b, int c, int d)
  225. {
  226. int t1 = a ^ b;
  227. int t2 = a & c;
  228. int t3 = a | d;
  229. int t4 = c ^ d;
  230. int t5 = t1 & t3;
  231. int t6 = t2 | t5;
  232. X2 = t4 ^ t6;
  233. int t8 = b ^ t3;
  234. int t9 = t6 ^ t8;
  235. int t10 = t4 & t9;
  236. X0 = t1 ^ t10;
  237. int t12 = X2 & X0;
  238. X1 = t9 ^ t12;
  239. X3 = (b | d) ^ (t4 ^ t12);
  240. }
  241. /**
  242. * InvS3 - { 0, 9,10, 7,11,14, 6,13, 3, 5,12, 2, 4, 8,15, 1 } - 15 terms
  243. */
  244. protected void Ib3(int a, int b, int c, int d)
  245. {
  246. int t1 = a | b;
  247. int t2 = b ^ c;
  248. int t3 = b & t2;
  249. int t4 = a ^ t3;
  250. int t5 = c ^ t4;
  251. int t6 = d | t4;
  252. X0 = t2 ^ t6;
  253. int t8 = t2 | t6;
  254. int t9 = d ^ t8;
  255. X2 = t5 ^ t9;
  256. int t11 = t1 ^ t9;
  257. int t12 = X0 & t11;
  258. X3 = t4 ^ t12;
  259. X1 = X3 ^ (X0 ^ t11);
  260. }
  261. /**
  262. * S4 - { 1,15, 8, 3,12, 0,11, 6, 2, 5, 4,10, 9,14, 7,13 } - 15 terms.
  263. */
  264. protected void Sb4(int a, int b, int c, int d)
  265. {
  266. int t1 = a ^ d;
  267. int t2 = d & t1;
  268. int t3 = c ^ t2;
  269. int t4 = b | t3;
  270. X3 = t1 ^ t4;
  271. int t6 = ~b;
  272. int t7 = t1 | t6;
  273. X0 = t3 ^ t7;
  274. int t9 = a & X0;
  275. int t10 = t1 ^ t6;
  276. int t11 = t4 & t10;
  277. X2 = t9 ^ t11;
  278. X1 = (a ^ t3) ^ (t10 & X2);
  279. }
  280. /**
  281. * InvS4 - { 5, 0, 8, 3,10, 9, 7,14, 2,12,11, 6, 4,15,13, 1 } - 15 terms.
  282. */
  283. protected void Ib4(int a, int b, int c, int d)
  284. {
  285. int t1 = c | d;
  286. int t2 = a & t1;
  287. int t3 = b ^ t2;
  288. int t4 = a & t3;
  289. int t5 = c ^ t4;
  290. X1 = d ^ t5;
  291. int t7 = ~a;
  292. int t8 = t5 & X1;
  293. X3 = t3 ^ t8;
  294. int t10 = X1 | t7;
  295. int t11 = d ^ t10;
  296. X0 = X3 ^ t11;
  297. X2 = (t3 & t11) ^ (X1 ^ t7);
  298. }
  299. /**
  300. * S5 - {15, 5, 2,11, 4,10, 9,12, 0, 3,14, 8,13, 6, 7, 1 } - 16 terms.
  301. */
  302. protected void Sb5(int a, int b, int c, int d)
  303. {
  304. int t1 = ~a;
  305. int t2 = a ^ b;
  306. int t3 = a ^ d;
  307. int t4 = c ^ t1;
  308. int t5 = t2 | t3;
  309. X0 = t4 ^ t5;
  310. int t7 = d & X0;
  311. int t8 = t2 ^ X0;
  312. X1 = t7 ^ t8;
  313. int t10 = t1 | X0;
  314. int t11 = t2 | t7;
  315. int t12 = t3 ^ t10;
  316. X2 = t11 ^ t12;
  317. X3 = (b ^ t7) ^ (X1 & t12);
  318. }
  319. /**
  320. * InvS5 - { 8,15, 2, 9, 4, 1,13,14,11, 6, 5, 3, 7,12,10, 0 } - 16 terms.
  321. */
  322. protected void Ib5(int a, int b, int c, int d)
  323. {
  324. int t1 = ~c;
  325. int t2 = b & t1;
  326. int t3 = d ^ t2;
  327. int t4 = a & t3;
  328. int t5 = b ^ t1;
  329. X3 = t4 ^ t5;
  330. int t7 = b | X3;
  331. int t8 = a & t7;
  332. X1 = t3 ^ t8;
  333. int t10 = a | d;
  334. int t11 = t1 ^ t7;
  335. X0 = t10 ^ t11;
  336. X2 = (b & t10) ^ (t4 | (a ^ c));
  337. }
  338. /**
  339. * S6 - { 7, 2,12, 5, 8, 4, 6,11,14, 9, 1,15,13, 3,10, 0 } - 15 terms.
  340. */
  341. protected void Sb6(int a, int b, int c, int d)
  342. {
  343. int t1 = ~a;
  344. int t2 = a ^ d;
  345. int t3 = b ^ t2;
  346. int t4 = t1 | t2;
  347. int t5 = c ^ t4;
  348. X1 = b ^ t5;
  349. int t7 = t2 | X1;
  350. int t8 = d ^ t7;
  351. int t9 = t5 & t8;
  352. X2 = t3 ^ t9;
  353. int t11 = t5 ^ t8;
  354. X0 = X2 ^ t11;
  355. X3 = (~t5) ^ (t3 & t11);
  356. }
  357. /**
  358. * InvS6 - {15,10, 1,13, 5, 3, 6, 0, 4, 9,14, 7, 2,12, 8,11 } - 15 terms.
  359. */
  360. protected void Ib6(int a, int b, int c, int d)
  361. {
  362. int t1 = ~a;
  363. int t2 = a ^ b;
  364. int t3 = c ^ t2;
  365. int t4 = c | t1;
  366. int t5 = d ^ t4;
  367. X1 = t3 ^ t5;
  368. int t7 = t3 & t5;
  369. int t8 = t2 ^ t7;
  370. int t9 = b | t8;
  371. X3 = t5 ^ t9;
  372. int t11 = b | X3;
  373. X0 = t8 ^ t11;
  374. X2 = (d & t1) ^ (t3 ^ t11);
  375. }
  376. /**
  377. * S7 - { 1,13,15, 0,14, 8, 2,11, 7, 4,12,10, 9, 3, 5, 6 } - 16 terms.
  378. */
  379. protected void Sb7(int a, int b, int c, int d)
  380. {
  381. int t1 = b ^ c;
  382. int t2 = c & t1;
  383. int t3 = d ^ t2;
  384. int t4 = a ^ t3;
  385. int t5 = d | t1;
  386. int t6 = t4 & t5;
  387. X1 = b ^ t6;
  388. int t8 = t3 | X1;
  389. int t9 = a & t4;
  390. X3 = t1 ^ t9;
  391. int t11 = t4 ^ t8;
  392. int t12 = X3 & t11;
  393. X2 = t3 ^ t12;
  394. X0 = (~t11) ^ (X3 & X2);
  395. }
  396. /**
  397. * InvS7 - { 3, 0, 6,13, 9,14,15, 8, 5,12,11, 7,10, 1, 4, 2 } - 17 terms.
  398. */
  399. protected void Ib7(int a, int b, int c, int d)
  400. {
  401. int t3 = c | (a & b);
  402. int t4 = d & (a | b);
  403. X3 = t3 ^ t4;
  404. int t6 = ~d;
  405. int t7 = b ^ t4;
  406. int t9 = t7 | (X3 ^ t6);
  407. X1 = a ^ t9;
  408. X0 = (c ^ t7) ^ (d | X1);
  409. X2 = (t3 ^ X1) ^ (X0 ^ (a & X3));
  410. }
  411. /**
  412. * Apply the linear transformation to the register set.
  413. */
  414. protected void LT()
  415. {
  416. int x0 = Integers.RotateLeft(X0, 13);
  417. int x2 = Integers.RotateLeft(X2, 3);
  418. int x1 = X1 ^ x0 ^ x2;
  419. int x3 = X3 ^ x2 ^ x0 << 3;
  420. X1 = Integers.RotateLeft(x1, 1);
  421. X3 = Integers.RotateLeft(x3, 7);
  422. X0 = Integers.RotateLeft(x0 ^ X1 ^ X3, 5);
  423. X2 = Integers.RotateLeft(x2 ^ X3 ^ (X1 << 7), 22);
  424. }
  425. /**
  426. * Apply the inverse of the linear transformation to the register set.
  427. */
  428. protected void InverseLT()
  429. {
  430. int x2 = Integers.RotateRight(X2, 22) ^ X3 ^ (X1 << 7);
  431. int x0 = Integers.RotateRight(X0, 5) ^ X1 ^ X3;
  432. int x3 = Integers.RotateRight(X3, 7);
  433. int x1 = Integers.RotateRight(X1, 1);
  434. X3 = x3 ^ x2 ^ x0 << 3;
  435. X1 = x1 ^ x0 ^ x2;
  436. X2 = Integers.RotateRight(x2, 3);
  437. X0 = Integers.RotateRight(x0, 13);
  438. }
  439. internal abstract int[] MakeWorkingKey(byte[] key);
  440. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  441. internal abstract void EncryptBlock(ReadOnlySpan<byte> input, Span<byte> output);
  442. internal abstract void DecryptBlock(ReadOnlySpan<byte> input, Span<byte> output);
  443. #else
  444. internal abstract void EncryptBlock(byte[] input, int inOff, byte[] output, int outOff);
  445. internal abstract void DecryptBlock(byte[] input, int inOff, byte[] output, int outOff);
  446. #endif
  447. }
  448. }
  449. #pragma warning restore
  450. #endif