SerpentEngineBase.cs 14 KB

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