SerpentCipher.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Renci.SshNet.Security.Cryptography.Ciphers
  6. {
  7. /// <summary>
  8. /// Implements Serpent cipher algorithm.
  9. /// </summary>
  10. public class SerpentCipher : BlockCipher
  11. {
  12. private static readonly int ROUNDS = 32;
  13. private static readonly int PHI = unchecked((int)0x9E3779B9); // (Sqrt(5) - 1) * 2**31
  14. private int[] _workingKey;
  15. private int X0, X1, X2, X3; // registers
  16. /// <summary>
  17. /// Gets the size of the block in bytes.
  18. /// </summary>
  19. /// <value>
  20. /// The size of the block in bytes.
  21. /// </value>
  22. public override int BlockSize
  23. {
  24. get { return 16; }
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="SerpentCipher"/> class.
  28. /// </summary>
  29. /// <param name="key">The key.</param>
  30. /// <param name="mode">The mode.</param>
  31. /// <param name="padding">The padding.</param>
  32. public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding)
  33. : base(key, mode, padding)
  34. {
  35. // TODO: Refactor this algorithm
  36. var keySize = key.Length * 8;
  37. if (!(keySize == 128 || keySize == 192 || keySize == 256))
  38. throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
  39. this._workingKey = this.MakeWorkingKey(key);
  40. }
  41. /// <summary>
  42. /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region of the output byte array.
  43. /// </summary>
  44. /// <param name="inputBuffer">The input data to encrypt.</param>
  45. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  46. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  47. /// <param name="outputBuffer">The output to which to write encrypted data.</param>
  48. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  49. /// <returns>
  50. /// The number of bytes encrypted.
  51. /// </returns>
  52. public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  53. {
  54. if (inputCount != this.BlockSize)
  55. throw new ArgumentException("inputCount");
  56. X3 = BytesToWord(inputBuffer, inputOffset);
  57. X2 = BytesToWord(inputBuffer, inputOffset + 4);
  58. X1 = BytesToWord(inputBuffer, inputOffset + 8);
  59. X0 = BytesToWord(inputBuffer, inputOffset + 12);
  60. Sb0(this._workingKey[0] ^ X0, this._workingKey[1] ^ X1, this._workingKey[2] ^ X2, this._workingKey[3] ^ X3); LT();
  61. Sb1(this._workingKey[4] ^ X0, this._workingKey[5] ^ X1, this._workingKey[6] ^ X2, this._workingKey[7] ^ X3); LT();
  62. Sb2(this._workingKey[8] ^ X0, this._workingKey[9] ^ X1, this._workingKey[10] ^ X2, this._workingKey[11] ^ X3); LT();
  63. Sb3(this._workingKey[12] ^ X0, this._workingKey[13] ^ X1, this._workingKey[14] ^ X2, this._workingKey[15] ^ X3); LT();
  64. Sb4(this._workingKey[16] ^ X0, this._workingKey[17] ^ X1, this._workingKey[18] ^ X2, this._workingKey[19] ^ X3); LT();
  65. Sb5(this._workingKey[20] ^ X0, this._workingKey[21] ^ X1, this._workingKey[22] ^ X2, this._workingKey[23] ^ X3); LT();
  66. Sb6(this._workingKey[24] ^ X0, this._workingKey[25] ^ X1, this._workingKey[26] ^ X2, this._workingKey[27] ^ X3); LT();
  67. Sb7(this._workingKey[28] ^ X0, this._workingKey[29] ^ X1, this._workingKey[30] ^ X2, this._workingKey[31] ^ X3); LT();
  68. Sb0(this._workingKey[32] ^ X0, this._workingKey[33] ^ X1, this._workingKey[34] ^ X2, this._workingKey[35] ^ X3); LT();
  69. Sb1(this._workingKey[36] ^ X0, this._workingKey[37] ^ X1, this._workingKey[38] ^ X2, this._workingKey[39] ^ X3); LT();
  70. Sb2(this._workingKey[40] ^ X0, this._workingKey[41] ^ X1, this._workingKey[42] ^ X2, this._workingKey[43] ^ X3); LT();
  71. Sb3(this._workingKey[44] ^ X0, this._workingKey[45] ^ X1, this._workingKey[46] ^ X2, this._workingKey[47] ^ X3); LT();
  72. Sb4(this._workingKey[48] ^ X0, this._workingKey[49] ^ X1, this._workingKey[50] ^ X2, this._workingKey[51] ^ X3); LT();
  73. Sb5(this._workingKey[52] ^ X0, this._workingKey[53] ^ X1, this._workingKey[54] ^ X2, this._workingKey[55] ^ X3); LT();
  74. Sb6(this._workingKey[56] ^ X0, this._workingKey[57] ^ X1, this._workingKey[58] ^ X2, this._workingKey[59] ^ X3); LT();
  75. Sb7(this._workingKey[60] ^ X0, this._workingKey[61] ^ X1, this._workingKey[62] ^ X2, this._workingKey[63] ^ X3); LT();
  76. Sb0(this._workingKey[64] ^ X0, this._workingKey[65] ^ X1, this._workingKey[66] ^ X2, this._workingKey[67] ^ X3); LT();
  77. Sb1(this._workingKey[68] ^ X0, this._workingKey[69] ^ X1, this._workingKey[70] ^ X2, this._workingKey[71] ^ X3); LT();
  78. Sb2(this._workingKey[72] ^ X0, this._workingKey[73] ^ X1, this._workingKey[74] ^ X2, this._workingKey[75] ^ X3); LT();
  79. Sb3(this._workingKey[76] ^ X0, this._workingKey[77] ^ X1, this._workingKey[78] ^ X2, this._workingKey[79] ^ X3); LT();
  80. Sb4(this._workingKey[80] ^ X0, this._workingKey[81] ^ X1, this._workingKey[82] ^ X2, this._workingKey[83] ^ X3); LT();
  81. Sb5(this._workingKey[84] ^ X0, this._workingKey[85] ^ X1, this._workingKey[86] ^ X2, this._workingKey[87] ^ X3); LT();
  82. Sb6(this._workingKey[88] ^ X0, this._workingKey[89] ^ X1, this._workingKey[90] ^ X2, this._workingKey[91] ^ X3); LT();
  83. Sb7(this._workingKey[92] ^ X0, this._workingKey[93] ^ X1, this._workingKey[94] ^ X2, this._workingKey[95] ^ X3); LT();
  84. Sb0(this._workingKey[96] ^ X0, this._workingKey[97] ^ X1, this._workingKey[98] ^ X2, this._workingKey[99] ^ X3); LT();
  85. Sb1(this._workingKey[100] ^ X0, this._workingKey[101] ^ X1, this._workingKey[102] ^ X2, this._workingKey[103] ^ X3); LT();
  86. Sb2(this._workingKey[104] ^ X0, this._workingKey[105] ^ X1, this._workingKey[106] ^ X2, this._workingKey[107] ^ X3); LT();
  87. Sb3(this._workingKey[108] ^ X0, this._workingKey[109] ^ X1, this._workingKey[110] ^ X2, this._workingKey[111] ^ X3); LT();
  88. Sb4(this._workingKey[112] ^ X0, this._workingKey[113] ^ X1, this._workingKey[114] ^ X2, this._workingKey[115] ^ X3); LT();
  89. Sb5(this._workingKey[116] ^ X0, this._workingKey[117] ^ X1, this._workingKey[118] ^ X2, this._workingKey[119] ^ X3); LT();
  90. Sb6(this._workingKey[120] ^ X0, this._workingKey[121] ^ X1, this._workingKey[122] ^ X2, this._workingKey[123] ^ X3); LT();
  91. Sb7(this._workingKey[124] ^ X0, this._workingKey[125] ^ X1, this._workingKey[126] ^ X2, this._workingKey[127] ^ X3);
  92. WordToBytes(this._workingKey[131] ^ X3, outputBuffer, outputOffset);
  93. WordToBytes(this._workingKey[130] ^ X2, outputBuffer, outputOffset + 4);
  94. WordToBytes(this._workingKey[129] ^ X1, outputBuffer, outputOffset + 8);
  95. WordToBytes(this._workingKey[128] ^ X0, outputBuffer, outputOffset + 12);
  96. return this.BlockSize;
  97. }
  98. /// <summary>
  99. /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region of the output byte array.
  100. /// </summary>
  101. /// <param name="inputBuffer">The input data to decrypt.</param>
  102. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  103. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  104. /// <param name="outputBuffer">The output to which to write decrypted data.</param>
  105. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  106. /// <returns>
  107. /// The number of bytes decrypted.
  108. /// </returns>
  109. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  110. {
  111. if (inputCount != this.BlockSize)
  112. throw new ArgumentException("inputCount");
  113. X3 = this._workingKey[131] ^ BytesToWord(inputBuffer, inputOffset);
  114. X2 = this._workingKey[130] ^ BytesToWord(inputBuffer, inputOffset + 4);
  115. X1 = this._workingKey[129] ^ BytesToWord(inputBuffer, inputOffset + 8);
  116. X0 = this._workingKey[128] ^ BytesToWord(inputBuffer, inputOffset + 12);
  117. Ib7(X0, X1, X2, X3);
  118. X0 ^= this._workingKey[124]; X1 ^= this._workingKey[125]; X2 ^= this._workingKey[126]; X3 ^= this._workingKey[127];
  119. InverseLT(); Ib6(X0, X1, X2, X3);
  120. X0 ^= this._workingKey[120]; X1 ^= this._workingKey[121]; X2 ^= this._workingKey[122]; X3 ^= this._workingKey[123];
  121. InverseLT(); Ib5(X0, X1, X2, X3);
  122. X0 ^= this._workingKey[116]; X1 ^= this._workingKey[117]; X2 ^= this._workingKey[118]; X3 ^= this._workingKey[119];
  123. InverseLT(); Ib4(X0, X1, X2, X3);
  124. X0 ^= this._workingKey[112]; X1 ^= this._workingKey[113]; X2 ^= this._workingKey[114]; X3 ^= this._workingKey[115];
  125. InverseLT(); Ib3(X0, X1, X2, X3);
  126. X0 ^= this._workingKey[108]; X1 ^= this._workingKey[109]; X2 ^= this._workingKey[110]; X3 ^= this._workingKey[111];
  127. InverseLT(); Ib2(X0, X1, X2, X3);
  128. X0 ^= this._workingKey[104]; X1 ^= this._workingKey[105]; X2 ^= this._workingKey[106]; X3 ^= this._workingKey[107];
  129. InverseLT(); Ib1(X0, X1, X2, X3);
  130. X0 ^= this._workingKey[100]; X1 ^= this._workingKey[101]; X2 ^= this._workingKey[102]; X3 ^= this._workingKey[103];
  131. InverseLT(); Ib0(X0, X1, X2, X3);
  132. X0 ^= this._workingKey[96]; X1 ^= this._workingKey[97]; X2 ^= this._workingKey[98]; X3 ^= this._workingKey[99];
  133. InverseLT(); Ib7(X0, X1, X2, X3);
  134. X0 ^= this._workingKey[92]; X1 ^= this._workingKey[93]; X2 ^= this._workingKey[94]; X3 ^= this._workingKey[95];
  135. InverseLT(); Ib6(X0, X1, X2, X3);
  136. X0 ^= this._workingKey[88]; X1 ^= this._workingKey[89]; X2 ^= this._workingKey[90]; X3 ^= this._workingKey[91];
  137. InverseLT(); Ib5(X0, X1, X2, X3);
  138. X0 ^= this._workingKey[84]; X1 ^= this._workingKey[85]; X2 ^= this._workingKey[86]; X3 ^= this._workingKey[87];
  139. InverseLT(); Ib4(X0, X1, X2, X3);
  140. X0 ^= this._workingKey[80]; X1 ^= this._workingKey[81]; X2 ^= this._workingKey[82]; X3 ^= this._workingKey[83];
  141. InverseLT(); Ib3(X0, X1, X2, X3);
  142. X0 ^= this._workingKey[76]; X1 ^= this._workingKey[77]; X2 ^= this._workingKey[78]; X3 ^= this._workingKey[79];
  143. InverseLT(); Ib2(X0, X1, X2, X3);
  144. X0 ^= this._workingKey[72]; X1 ^= this._workingKey[73]; X2 ^= this._workingKey[74]; X3 ^= this._workingKey[75];
  145. InverseLT(); Ib1(X0, X1, X2, X3);
  146. X0 ^= this._workingKey[68]; X1 ^= this._workingKey[69]; X2 ^= this._workingKey[70]; X3 ^= this._workingKey[71];
  147. InverseLT(); Ib0(X0, X1, X2, X3);
  148. X0 ^= this._workingKey[64]; X1 ^= this._workingKey[65]; X2 ^= this._workingKey[66]; X3 ^= this._workingKey[67];
  149. InverseLT(); Ib7(X0, X1, X2, X3);
  150. X0 ^= this._workingKey[60]; X1 ^= this._workingKey[61]; X2 ^= this._workingKey[62]; X3 ^= this._workingKey[63];
  151. InverseLT(); Ib6(X0, X1, X2, X3);
  152. X0 ^= this._workingKey[56]; X1 ^= this._workingKey[57]; X2 ^= this._workingKey[58]; X3 ^= this._workingKey[59];
  153. InverseLT(); Ib5(X0, X1, X2, X3);
  154. X0 ^= this._workingKey[52]; X1 ^= this._workingKey[53]; X2 ^= this._workingKey[54]; X3 ^= this._workingKey[55];
  155. InverseLT(); Ib4(X0, X1, X2, X3);
  156. X0 ^= this._workingKey[48]; X1 ^= this._workingKey[49]; X2 ^= this._workingKey[50]; X3 ^= this._workingKey[51];
  157. InverseLT(); Ib3(X0, X1, X2, X3);
  158. X0 ^= this._workingKey[44]; X1 ^= this._workingKey[45]; X2 ^= this._workingKey[46]; X3 ^= this._workingKey[47];
  159. InverseLT(); Ib2(X0, X1, X2, X3);
  160. X0 ^= this._workingKey[40]; X1 ^= this._workingKey[41]; X2 ^= this._workingKey[42]; X3 ^= this._workingKey[43];
  161. InverseLT(); Ib1(X0, X1, X2, X3);
  162. X0 ^= this._workingKey[36]; X1 ^= this._workingKey[37]; X2 ^= this._workingKey[38]; X3 ^= this._workingKey[39];
  163. InverseLT(); Ib0(X0, X1, X2, X3);
  164. X0 ^= this._workingKey[32]; X1 ^= this._workingKey[33]; X2 ^= this._workingKey[34]; X3 ^= this._workingKey[35];
  165. InverseLT(); Ib7(X0, X1, X2, X3);
  166. X0 ^= this._workingKey[28]; X1 ^= this._workingKey[29]; X2 ^= this._workingKey[30]; X3 ^= this._workingKey[31];
  167. InverseLT(); Ib6(X0, X1, X2, X3);
  168. X0 ^= this._workingKey[24]; X1 ^= this._workingKey[25]; X2 ^= this._workingKey[26]; X3 ^= this._workingKey[27];
  169. InverseLT(); Ib5(X0, X1, X2, X3);
  170. X0 ^= this._workingKey[20]; X1 ^= this._workingKey[21]; X2 ^= this._workingKey[22]; X3 ^= this._workingKey[23];
  171. InverseLT(); Ib4(X0, X1, X2, X3);
  172. X0 ^= this._workingKey[16]; X1 ^= this._workingKey[17]; X2 ^= this._workingKey[18]; X3 ^= this._workingKey[19];
  173. InverseLT(); Ib3(X0, X1, X2, X3);
  174. X0 ^= this._workingKey[12]; X1 ^= this._workingKey[13]; X2 ^= this._workingKey[14]; X3 ^= this._workingKey[15];
  175. InverseLT(); Ib2(X0, X1, X2, X3);
  176. X0 ^= this._workingKey[8]; X1 ^= this._workingKey[9]; X2 ^= this._workingKey[10]; X3 ^= this._workingKey[11];
  177. InverseLT(); Ib1(X0, X1, X2, X3);
  178. X0 ^= this._workingKey[4]; X1 ^= this._workingKey[5]; X2 ^= this._workingKey[6]; X3 ^= this._workingKey[7];
  179. InverseLT(); Ib0(X0, X1, X2, X3);
  180. WordToBytes(X3 ^ this._workingKey[3], outputBuffer, outputOffset);
  181. WordToBytes(X2 ^ this._workingKey[2], outputBuffer, outputOffset + 4);
  182. WordToBytes(X1 ^ this._workingKey[1], outputBuffer, outputOffset + 8);
  183. WordToBytes(X0 ^ this._workingKey[0], outputBuffer, outputOffset + 12);
  184. return this.BlockSize;
  185. }
  186. /**
  187. * Expand a user-supplied key material into a session key.
  188. *
  189. * @param key The user-key bytes (multiples of 4) to use.
  190. * @exception ArgumentException
  191. */
  192. private int[] MakeWorkingKey(byte[] key)
  193. {
  194. //
  195. // pad key to 256 bits
  196. //
  197. int[] kPad = new int[16];
  198. int off = 0;
  199. int length = 0;
  200. for (off = key.Length - 4; off > 0; off -= 4)
  201. {
  202. kPad[length++] = BytesToWord(key, off);
  203. }
  204. if (off == 0)
  205. {
  206. kPad[length++] = BytesToWord(key, 0);
  207. if (length < 8)
  208. {
  209. kPad[length] = 1;
  210. }
  211. }
  212. else
  213. {
  214. throw new ArgumentException("key must be a multiple of 4 bytes");
  215. }
  216. //
  217. // expand the padded key up to 33 x 128 bits of key material
  218. //
  219. int amount = (ROUNDS + 1) * 4;
  220. int[] w = new int[amount];
  221. //
  222. // compute w0 to w7 from w-8 to w-1
  223. //
  224. for (int i = 8; i < 16; i++)
  225. {
  226. kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
  227. }
  228. Array.Copy(kPad, 8, w, 0, 8);
  229. //
  230. // compute w8 to w136
  231. //
  232. for (int i = 8; i < amount; i++)
  233. {
  234. w[i] = RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
  235. }
  236. //
  237. // create the working keys by processing w with the Sbox and IP
  238. //
  239. Sb3(w[0], w[1], w[2], w[3]);
  240. w[0] = X0; w[1] = X1; w[2] = X2; w[3] = X3;
  241. Sb2(w[4], w[5], w[6], w[7]);
  242. w[4] = X0; w[5] = X1; w[6] = X2; w[7] = X3;
  243. Sb1(w[8], w[9], w[10], w[11]);
  244. w[8] = X0; w[9] = X1; w[10] = X2; w[11] = X3;
  245. Sb0(w[12], w[13], w[14], w[15]);
  246. w[12] = X0; w[13] = X1; w[14] = X2; w[15] = X3;
  247. Sb7(w[16], w[17], w[18], w[19]);
  248. w[16] = X0; w[17] = X1; w[18] = X2; w[19] = X3;
  249. Sb6(w[20], w[21], w[22], w[23]);
  250. w[20] = X0; w[21] = X1; w[22] = X2; w[23] = X3;
  251. Sb5(w[24], w[25], w[26], w[27]);
  252. w[24] = X0; w[25] = X1; w[26] = X2; w[27] = X3;
  253. Sb4(w[28], w[29], w[30], w[31]);
  254. w[28] = X0; w[29] = X1; w[30] = X2; w[31] = X3;
  255. Sb3(w[32], w[33], w[34], w[35]);
  256. w[32] = X0; w[33] = X1; w[34] = X2; w[35] = X3;
  257. Sb2(w[36], w[37], w[38], w[39]);
  258. w[36] = X0; w[37] = X1; w[38] = X2; w[39] = X3;
  259. Sb1(w[40], w[41], w[42], w[43]);
  260. w[40] = X0; w[41] = X1; w[42] = X2; w[43] = X3;
  261. Sb0(w[44], w[45], w[46], w[47]);
  262. w[44] = X0; w[45] = X1; w[46] = X2; w[47] = X3;
  263. Sb7(w[48], w[49], w[50], w[51]);
  264. w[48] = X0; w[49] = X1; w[50] = X2; w[51] = X3;
  265. Sb6(w[52], w[53], w[54], w[55]);
  266. w[52] = X0; w[53] = X1; w[54] = X2; w[55] = X3;
  267. Sb5(w[56], w[57], w[58], w[59]);
  268. w[56] = X0; w[57] = X1; w[58] = X2; w[59] = X3;
  269. Sb4(w[60], w[61], w[62], w[63]);
  270. w[60] = X0; w[61] = X1; w[62] = X2; w[63] = X3;
  271. Sb3(w[64], w[65], w[66], w[67]);
  272. w[64] = X0; w[65] = X1; w[66] = X2; w[67] = X3;
  273. Sb2(w[68], w[69], w[70], w[71]);
  274. w[68] = X0; w[69] = X1; w[70] = X2; w[71] = X3;
  275. Sb1(w[72], w[73], w[74], w[75]);
  276. w[72] = X0; w[73] = X1; w[74] = X2; w[75] = X3;
  277. Sb0(w[76], w[77], w[78], w[79]);
  278. w[76] = X0; w[77] = X1; w[78] = X2; w[79] = X3;
  279. Sb7(w[80], w[81], w[82], w[83]);
  280. w[80] = X0; w[81] = X1; w[82] = X2; w[83] = X3;
  281. Sb6(w[84], w[85], w[86], w[87]);
  282. w[84] = X0; w[85] = X1; w[86] = X2; w[87] = X3;
  283. Sb5(w[88], w[89], w[90], w[91]);
  284. w[88] = X0; w[89] = X1; w[90] = X2; w[91] = X3;
  285. Sb4(w[92], w[93], w[94], w[95]);
  286. w[92] = X0; w[93] = X1; w[94] = X2; w[95] = X3;
  287. Sb3(w[96], w[97], w[98], w[99]);
  288. w[96] = X0; w[97] = X1; w[98] = X2; w[99] = X3;
  289. Sb2(w[100], w[101], w[102], w[103]);
  290. w[100] = X0; w[101] = X1; w[102] = X2; w[103] = X3;
  291. Sb1(w[104], w[105], w[106], w[107]);
  292. w[104] = X0; w[105] = X1; w[106] = X2; w[107] = X3;
  293. Sb0(w[108], w[109], w[110], w[111]);
  294. w[108] = X0; w[109] = X1; w[110] = X2; w[111] = X3;
  295. Sb7(w[112], w[113], w[114], w[115]);
  296. w[112] = X0; w[113] = X1; w[114] = X2; w[115] = X3;
  297. Sb6(w[116], w[117], w[118], w[119]);
  298. w[116] = X0; w[117] = X1; w[118] = X2; w[119] = X3;
  299. Sb5(w[120], w[121], w[122], w[123]);
  300. w[120] = X0; w[121] = X1; w[122] = X2; w[123] = X3;
  301. Sb4(w[124], w[125], w[126], w[127]);
  302. w[124] = X0; w[125] = X1; w[126] = X2; w[127] = X3;
  303. Sb3(w[128], w[129], w[130], w[131]);
  304. w[128] = X0; w[129] = X1; w[130] = X2; w[131] = X3;
  305. return w;
  306. }
  307. private int RotateLeft(int x, int bits)
  308. {
  309. return ((x << bits) | (int)((uint)x >> (32 - bits)));
  310. }
  311. private int RotateRight(int x, int bits)
  312. {
  313. return ((int)((uint)x >> bits) | (x << (32 - bits)));
  314. }
  315. private int BytesToWord(byte[] src, int srcOff)
  316. {
  317. return (((src[srcOff] & 0xff) << 24) | ((src[srcOff + 1] & 0xff) << 16) |
  318. ((src[srcOff + 2] & 0xff) << 8) | ((src[srcOff + 3] & 0xff)));
  319. }
  320. private void WordToBytes(int word, byte[] dst, int dstOff)
  321. {
  322. dst[dstOff + 3] = (byte)(word);
  323. dst[dstOff + 2] = (byte)((uint)word >> 8);
  324. dst[dstOff + 1] = (byte)((uint)word >> 16);
  325. dst[dstOff] = (byte)((uint)word >> 24);
  326. }
  327. /*
  328. * The sboxes below are based on the work of Brian Gladman and
  329. * Sam Simpson, whose original notice appears below.
  330. * <p>
  331. * For further details see:
  332. * http://fp.gladman.plus.com/cryptography_technology/serpent/
  333. * </p>
  334. */
  335. /* Partially optimised Serpent S Box bool functions derived */
  336. /* using a recursive descent analyser but without a full search */
  337. /* of all subtrees. This set of S boxes is the result of work */
  338. /* by Sam Simpson and Brian Gladman using the spare time on a */
  339. /* cluster of high capacity servers to search for S boxes with */
  340. /* this customised search engine. There are now an average of */
  341. /* 15.375 terms per S box. */
  342. /* */
  343. /* Copyright: Dr B. R Gladman (gladman@seven77.demon.co.uk) */
  344. /* and Sam Simpson (s.simpson@mia.co.uk) */
  345. /* 17th December 1998 */
  346. /* */
  347. /* We hereby give permission for information in this file to be */
  348. /* used freely subject only to acknowledgement of its origin. */
  349. /**
  350. * S0 - { 3, 8,15, 1,10, 6, 5,11,14,13, 4, 2, 7, 0, 9,12 } - 15 terms.
  351. */
  352. private void Sb0(int a, int b, int c, int d)
  353. {
  354. int t1 = a ^ d;
  355. int t3 = c ^ t1;
  356. int t4 = b ^ t3;
  357. X3 = (a & d) ^ t4;
  358. int t7 = a ^ (b & t1);
  359. X2 = t4 ^ (c | t7);
  360. int t12 = X3 & (t3 ^ t7);
  361. X1 = (~t3) ^ t12;
  362. X0 = t12 ^ (~t7);
  363. }
  364. /**
  365. * InvSO - {13, 3,11, 0,10, 6, 5,12, 1,14, 4, 7,15, 9, 8, 2 } - 15 terms.
  366. */
  367. private void Ib0(int a, int b, int c, int d)
  368. {
  369. int t1 = ~a;
  370. int t2 = a ^ b;
  371. int t4 = d ^ (t1 | t2);
  372. int t5 = c ^ t4;
  373. X2 = t2 ^ t5;
  374. int t8 = t1 ^ (d & t2);
  375. X1 = t4 ^ (X2 & t8);
  376. X3 = (a & t4) ^ (t5 | X1);
  377. X0 = X3 ^ (t5 ^ t8);
  378. }
  379. /**
  380. * S1 - {15,12, 2, 7, 9, 0, 5,10, 1,11,14, 8, 6,13, 3, 4 } - 14 terms.
  381. */
  382. private void Sb1(int a, int b, int c, int d)
  383. {
  384. int t2 = b ^ (~a);
  385. int t5 = c ^ (a | t2);
  386. X2 = d ^ t5;
  387. int t7 = b ^ (d | t2);
  388. int t8 = t2 ^ X2;
  389. X3 = t8 ^ (t5 & t7);
  390. int t11 = t5 ^ t7;
  391. X1 = X3 ^ t11;
  392. X0 = t5 ^ (t8 & t11);
  393. }
  394. /**
  395. * InvS1 - { 5, 8, 2,14,15, 6,12, 3,11, 4, 7, 9, 1,13,10, 0 } - 14 steps.
  396. */
  397. private void Ib1(int a, int b, int c, int d)
  398. {
  399. int t1 = b ^ d;
  400. int t3 = a ^ (b & t1);
  401. int t4 = t1 ^ t3;
  402. X3 = c ^ t4;
  403. int t7 = b ^ (t1 & t3);
  404. int t8 = X3 | t7;
  405. X1 = t3 ^ t8;
  406. int t10 = ~X1;
  407. int t11 = X3 ^ t7;
  408. X0 = t10 ^ t11;
  409. X2 = t4 ^ (t10 | t11);
  410. }
  411. /**
  412. * S2 - { 8, 6, 7, 9, 3,12,10,15,13, 1,14, 4, 0,11, 5, 2 } - 16 terms.
  413. */
  414. private void Sb2(int a, int b, int c, int d)
  415. {
  416. int t1 = ~a;
  417. int t2 = b ^ d;
  418. int t3 = c & t1;
  419. X0 = t2 ^ t3;
  420. int t5 = c ^ t1;
  421. int t6 = c ^ X0;
  422. int t7 = b & t6;
  423. X3 = t5 ^ t7;
  424. X2 = a ^ ((d | t7) & (X0 | t5));
  425. X1 = (t2 ^ X3) ^ (X2 ^ (d | t1));
  426. }
  427. /**
  428. * InvS2 - {12, 9,15, 4,11,14, 1, 2, 0, 3, 6,13, 5, 8,10, 7 } - 16 steps.
  429. */
  430. private void Ib2(int a, int b, int c, int d)
  431. {
  432. int t1 = b ^ d;
  433. int t2 = ~t1;
  434. int t3 = a ^ c;
  435. int t4 = c ^ t1;
  436. int t5 = b & t4;
  437. X0 = t3 ^ t5;
  438. int t7 = a | t2;
  439. int t8 = d ^ t7;
  440. int t9 = t3 | t8;
  441. X3 = t1 ^ t9;
  442. int t11 = ~t4;
  443. int t12 = X0 | X3;
  444. X1 = t11 ^ t12;
  445. X2 = (d & t11) ^ (t3 ^ t12);
  446. }
  447. /**
  448. * S3 - { 0,15,11, 8,12, 9, 6, 3,13, 1, 2, 4,10, 7, 5,14 } - 16 terms.
  449. */
  450. private void Sb3(int a, int b, int c, int d)
  451. {
  452. int t1 = a ^ b;
  453. int t2 = a & c;
  454. int t3 = a | d;
  455. int t4 = c ^ d;
  456. int t5 = t1 & t3;
  457. int t6 = t2 | t5;
  458. X2 = t4 ^ t6;
  459. int t8 = b ^ t3;
  460. int t9 = t6 ^ t8;
  461. int t10 = t4 & t9;
  462. X0 = t1 ^ t10;
  463. int t12 = X2 & X0;
  464. X1 = t9 ^ t12;
  465. X3 = (b | d) ^ (t4 ^ t12);
  466. }
  467. /**
  468. * InvS3 - { 0, 9,10, 7,11,14, 6,13, 3, 5,12, 2, 4, 8,15, 1 } - 15 terms
  469. */
  470. private void Ib3(int a, int b, int c, int d)
  471. {
  472. int t1 = a | b;
  473. int t2 = b ^ c;
  474. int t3 = b & t2;
  475. int t4 = a ^ t3;
  476. int t5 = c ^ t4;
  477. int t6 = d | t4;
  478. X0 = t2 ^ t6;
  479. int t8 = t2 | t6;
  480. int t9 = d ^ t8;
  481. X2 = t5 ^ t9;
  482. int t11 = t1 ^ t9;
  483. int t12 = X0 & t11;
  484. X3 = t4 ^ t12;
  485. X1 = X3 ^ (X0 ^ t11);
  486. }
  487. /**
  488. * S4 - { 1,15, 8, 3,12, 0,11, 6, 2, 5, 4,10, 9,14, 7,13 } - 15 terms.
  489. */
  490. private void Sb4(int a, int b, int c, int d)
  491. {
  492. int t1 = a ^ d;
  493. int t2 = d & t1;
  494. int t3 = c ^ t2;
  495. int t4 = b | t3;
  496. X3 = t1 ^ t4;
  497. int t6 = ~b;
  498. int t7 = t1 | t6;
  499. X0 = t3 ^ t7;
  500. int t9 = a & X0;
  501. int t10 = t1 ^ t6;
  502. int t11 = t4 & t10;
  503. X2 = t9 ^ t11;
  504. X1 = (a ^ t3) ^ (t10 & X2);
  505. }
  506. /**
  507. * InvS4 - { 5, 0, 8, 3,10, 9, 7,14, 2,12,11, 6, 4,15,13, 1 } - 15 terms.
  508. */
  509. private void Ib4(int a, int b, int c, int d)
  510. {
  511. int t1 = c | d;
  512. int t2 = a & t1;
  513. int t3 = b ^ t2;
  514. int t4 = a & t3;
  515. int t5 = c ^ t4;
  516. X1 = d ^ t5;
  517. int t7 = ~a;
  518. int t8 = t5 & X1;
  519. X3 = t3 ^ t8;
  520. int t10 = X1 | t7;
  521. int t11 = d ^ t10;
  522. X0 = X3 ^ t11;
  523. X2 = (t3 & t11) ^ (X1 ^ t7);
  524. }
  525. /**
  526. * S5 - {15, 5, 2,11, 4,10, 9,12, 0, 3,14, 8,13, 6, 7, 1 } - 16 terms.
  527. */
  528. private void Sb5(int a, int b, int c, int d)
  529. {
  530. int t1 = ~a;
  531. int t2 = a ^ b;
  532. int t3 = a ^ d;
  533. int t4 = c ^ t1;
  534. int t5 = t2 | t3;
  535. X0 = t4 ^ t5;
  536. int t7 = d & X0;
  537. int t8 = t2 ^ X0;
  538. X1 = t7 ^ t8;
  539. int t10 = t1 | X0;
  540. int t11 = t2 | t7;
  541. int t12 = t3 ^ t10;
  542. X2 = t11 ^ t12;
  543. X3 = (b ^ t7) ^ (X1 & t12);
  544. }
  545. /**
  546. * InvS5 - { 8,15, 2, 9, 4, 1,13,14,11, 6, 5, 3, 7,12,10, 0 } - 16 terms.
  547. */
  548. private void Ib5(int a, int b, int c, int d)
  549. {
  550. int t1 = ~c;
  551. int t2 = b & t1;
  552. int t3 = d ^ t2;
  553. int t4 = a & t3;
  554. int t5 = b ^ t1;
  555. X3 = t4 ^ t5;
  556. int t7 = b | X3;
  557. int t8 = a & t7;
  558. X1 = t3 ^ t8;
  559. int t10 = a | d;
  560. int t11 = t1 ^ t7;
  561. X0 = t10 ^ t11;
  562. X2 = (b & t10) ^ (t4 | (a ^ c));
  563. }
  564. /**
  565. * S6 - { 7, 2,12, 5, 8, 4, 6,11,14, 9, 1,15,13, 3,10, 0 } - 15 terms.
  566. */
  567. private void Sb6(int a, int b, int c, int d)
  568. {
  569. int t1 = ~a;
  570. int t2 = a ^ d;
  571. int t3 = b ^ t2;
  572. int t4 = t1 | t2;
  573. int t5 = c ^ t4;
  574. X1 = b ^ t5;
  575. int t7 = t2 | X1;
  576. int t8 = d ^ t7;
  577. int t9 = t5 & t8;
  578. X2 = t3 ^ t9;
  579. int t11 = t5 ^ t8;
  580. X0 = X2 ^ t11;
  581. X3 = (~t5) ^ (t3 & t11);
  582. }
  583. /**
  584. * InvS6 - {15,10, 1,13, 5, 3, 6, 0, 4, 9,14, 7, 2,12, 8,11 } - 15 terms.
  585. */
  586. private void Ib6(int a, int b, int c, int d)
  587. {
  588. int t1 = ~a;
  589. int t2 = a ^ b;
  590. int t3 = c ^ t2;
  591. int t4 = c | t1;
  592. int t5 = d ^ t4;
  593. X1 = t3 ^ t5;
  594. int t7 = t3 & t5;
  595. int t8 = t2 ^ t7;
  596. int t9 = b | t8;
  597. X3 = t5 ^ t9;
  598. int t11 = b | X3;
  599. X0 = t8 ^ t11;
  600. X2 = (d & t1) ^ (t3 ^ t11);
  601. }
  602. /**
  603. * S7 - { 1,13,15, 0,14, 8, 2,11, 7, 4,12,10, 9, 3, 5, 6 } - 16 terms.
  604. */
  605. private void Sb7(int a, int b, int c, int d)
  606. {
  607. int t1 = b ^ c;
  608. int t2 = c & t1;
  609. int t3 = d ^ t2;
  610. int t4 = a ^ t3;
  611. int t5 = d | t1;
  612. int t6 = t4 & t5;
  613. X1 = b ^ t6;
  614. int t8 = t3 | X1;
  615. int t9 = a & t4;
  616. X3 = t1 ^ t9;
  617. int t11 = t4 ^ t8;
  618. int t12 = X3 & t11;
  619. X2 = t3 ^ t12;
  620. X0 = (~t11) ^ (X3 & X2);
  621. }
  622. /**
  623. * InvS7 - { 3, 0, 6,13, 9,14,15, 8, 5,12,11, 7,10, 1, 4, 2 } - 17 terms.
  624. */
  625. private void Ib7(int a, int b, int c, int d)
  626. {
  627. int t3 = c | (a & b);
  628. int t4 = d & (a | b);
  629. X3 = t3 ^ t4;
  630. int t6 = ~d;
  631. int t7 = b ^ t4;
  632. int t9 = t7 | (X3 ^ t6);
  633. X1 = a ^ t9;
  634. X0 = (c ^ t7) ^ (d | X1);
  635. X2 = (t3 ^ X1) ^ (X0 ^ (a & X3));
  636. }
  637. /**
  638. * Apply the linear transformation to the register set.
  639. */
  640. private void LT()
  641. {
  642. int x0 = RotateLeft(X0, 13);
  643. int x2 = RotateLeft(X2, 3);
  644. int x1 = X1 ^ x0 ^ x2;
  645. int x3 = X3 ^ x2 ^ x0 << 3;
  646. X1 = RotateLeft(x1, 1);
  647. X3 = RotateLeft(x3, 7);
  648. X0 = RotateLeft(x0 ^ X1 ^ X3, 5);
  649. X2 = RotateLeft(x2 ^ X3 ^ (X1 << 7), 22);
  650. }
  651. /**
  652. * Apply the inverse of the linear transformation to the register set.
  653. */
  654. private void InverseLT()
  655. {
  656. int x2 = RotateRight(X2, 22) ^ X3 ^ (X1 << 7);
  657. int x0 = RotateRight(X0, 5) ^ X1 ^ X3;
  658. int x3 = RotateRight(X3, 7);
  659. int x1 = RotateRight(X1, 1);
  660. X3 = x3 ^ x2 ^ x0 << 3;
  661. X1 = x1 ^ x0 ^ x2;
  662. X2 = RotateRight(x2, 3);
  663. X0 = RotateRight(x0, 13);
  664. }
  665. }
  666. }