SerpentEngine.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Engines
  6. {
  7. /**
  8. * Serpent is a 128-bit 32-round block cipher with variable key lengths,
  9. * including 128, 192 and 256 bit keys conjectured to be at least as
  10. * secure as three-key triple-DES.
  11. * <p>
  12. * Serpent was designed by Ross Anderson, Eli Biham and Lars Knudsen as a
  13. * candidate algorithm for the NIST AES Quest.
  14. * </p>
  15. * <p>
  16. * For full details see <a href="http://www.cl.cam.ac.uk/~rja14/serpent.html">The Serpent home page</a>
  17. * </p>
  18. */
  19. public sealed class SerpentEngine
  20. : SerpentEngineBase
  21. {
  22. /**
  23. * Expand a user-supplied key material into a session key.
  24. *
  25. * @param key The user-key bytes (multiples of 4) to use.
  26. * @exception ArgumentException
  27. */
  28. protected override int[] MakeWorkingKey(byte[] key)
  29. {
  30. //
  31. // pad key to 256 bits
  32. //
  33. int[] kPad = new int[16];
  34. int off = 0;
  35. int length = 0;
  36. for (off = 0; (off + 4) < key.Length; off += 4)
  37. {
  38. kPad[length++] = (int)Pack.LE_To_UInt32(key, off);
  39. }
  40. if (off % 4 == 0)
  41. {
  42. kPad[length++] = (int)Pack.LE_To_UInt32(key, off);
  43. if (length < 8)
  44. {
  45. kPad[length] = 1;
  46. }
  47. }
  48. else
  49. {
  50. throw new ArgumentException("key must be a multiple of 4 bytes");
  51. }
  52. //
  53. // expand the padded key up to 33 x 128 bits of key material
  54. //
  55. int amount = (ROUNDS + 1) * 4;
  56. int[] w = new int[amount];
  57. //
  58. // compute w0 to w7 from w-8 to w-1
  59. //
  60. for (int i = 8; i < 16; i++)
  61. {
  62. kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ PHI ^ (i - 8), 11);
  63. }
  64. Array.Copy(kPad, 8, w, 0, 8);
  65. //
  66. // compute w8 to w136
  67. //
  68. for (int i = 8; i < amount; i++)
  69. {
  70. w[i] = RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
  71. }
  72. //
  73. // create the working keys by processing w with the Sbox and IP
  74. //
  75. Sb3(w[0], w[1], w[2], w[3]);
  76. w[0] = X0; w[1] = X1; w[2] = X2; w[3] = X3;
  77. Sb2(w[4], w[5], w[6], w[7]);
  78. w[4] = X0; w[5] = X1; w[6] = X2; w[7] = X3;
  79. Sb1(w[8], w[9], w[10], w[11]);
  80. w[8] = X0; w[9] = X1; w[10] = X2; w[11] = X3;
  81. Sb0(w[12], w[13], w[14], w[15]);
  82. w[12] = X0; w[13] = X1; w[14] = X2; w[15] = X3;
  83. Sb7(w[16], w[17], w[18], w[19]);
  84. w[16] = X0; w[17] = X1; w[18] = X2; w[19] = X3;
  85. Sb6(w[20], w[21], w[22], w[23]);
  86. w[20] = X0; w[21] = X1; w[22] = X2; w[23] = X3;
  87. Sb5(w[24], w[25], w[26], w[27]);
  88. w[24] = X0; w[25] = X1; w[26] = X2; w[27] = X3;
  89. Sb4(w[28], w[29], w[30], w[31]);
  90. w[28] = X0; w[29] = X1; w[30] = X2; w[31] = X3;
  91. Sb3(w[32], w[33], w[34], w[35]);
  92. w[32] = X0; w[33] = X1; w[34] = X2; w[35] = X3;
  93. Sb2(w[36], w[37], w[38], w[39]);
  94. w[36] = X0; w[37] = X1; w[38] = X2; w[39] = X3;
  95. Sb1(w[40], w[41], w[42], w[43]);
  96. w[40] = X0; w[41] = X1; w[42] = X2; w[43] = X3;
  97. Sb0(w[44], w[45], w[46], w[47]);
  98. w[44] = X0; w[45] = X1; w[46] = X2; w[47] = X3;
  99. Sb7(w[48], w[49], w[50], w[51]);
  100. w[48] = X0; w[49] = X1; w[50] = X2; w[51] = X3;
  101. Sb6(w[52], w[53], w[54], w[55]);
  102. w[52] = X0; w[53] = X1; w[54] = X2; w[55] = X3;
  103. Sb5(w[56], w[57], w[58], w[59]);
  104. w[56] = X0; w[57] = X1; w[58] = X2; w[59] = X3;
  105. Sb4(w[60], w[61], w[62], w[63]);
  106. w[60] = X0; w[61] = X1; w[62] = X2; w[63] = X3;
  107. Sb3(w[64], w[65], w[66], w[67]);
  108. w[64] = X0; w[65] = X1; w[66] = X2; w[67] = X3;
  109. Sb2(w[68], w[69], w[70], w[71]);
  110. w[68] = X0; w[69] = X1; w[70] = X2; w[71] = X3;
  111. Sb1(w[72], w[73], w[74], w[75]);
  112. w[72] = X0; w[73] = X1; w[74] = X2; w[75] = X3;
  113. Sb0(w[76], w[77], w[78], w[79]);
  114. w[76] = X0; w[77] = X1; w[78] = X2; w[79] = X3;
  115. Sb7(w[80], w[81], w[82], w[83]);
  116. w[80] = X0; w[81] = X1; w[82] = X2; w[83] = X3;
  117. Sb6(w[84], w[85], w[86], w[87]);
  118. w[84] = X0; w[85] = X1; w[86] = X2; w[87] = X3;
  119. Sb5(w[88], w[89], w[90], w[91]);
  120. w[88] = X0; w[89] = X1; w[90] = X2; w[91] = X3;
  121. Sb4(w[92], w[93], w[94], w[95]);
  122. w[92] = X0; w[93] = X1; w[94] = X2; w[95] = X3;
  123. Sb3(w[96], w[97], w[98], w[99]);
  124. w[96] = X0; w[97] = X1; w[98] = X2; w[99] = X3;
  125. Sb2(w[100], w[101], w[102], w[103]);
  126. w[100] = X0; w[101] = X1; w[102] = X2; w[103] = X3;
  127. Sb1(w[104], w[105], w[106], w[107]);
  128. w[104] = X0; w[105] = X1; w[106] = X2; w[107] = X3;
  129. Sb0(w[108], w[109], w[110], w[111]);
  130. w[108] = X0; w[109] = X1; w[110] = X2; w[111] = X3;
  131. Sb7(w[112], w[113], w[114], w[115]);
  132. w[112] = X0; w[113] = X1; w[114] = X2; w[115] = X3;
  133. Sb6(w[116], w[117], w[118], w[119]);
  134. w[116] = X0; w[117] = X1; w[118] = X2; w[119] = X3;
  135. Sb5(w[120], w[121], w[122], w[123]);
  136. w[120] = X0; w[121] = X1; w[122] = X2; w[123] = X3;
  137. Sb4(w[124], w[125], w[126], w[127]);
  138. w[124] = X0; w[125] = X1; w[126] = X2; w[127] = X3;
  139. Sb3(w[128], w[129], w[130], w[131]);
  140. w[128] = X0; w[129] = X1; w[130] = X2; w[131] = X3;
  141. return w;
  142. }
  143. /**
  144. * Encrypt one block of plaintext.
  145. *
  146. * @param input the array containing the input data.
  147. * @param inOff offset into the in array the data starts at.
  148. * @param output the array the output data will be copied into.
  149. * @param outOff the offset into the out array the output will start at.
  150. */
  151. protected override void EncryptBlock(byte[] input, int inOff, byte[] output, int outOff)
  152. {
  153. X0 = (int)Pack.LE_To_UInt32(input, inOff);
  154. X1 = (int)Pack.LE_To_UInt32(input, inOff + 4);
  155. X2 = (int)Pack.LE_To_UInt32(input, inOff + 8);
  156. X3 = (int)Pack.LE_To_UInt32(input, inOff + 12);
  157. Sb0(wKey[0] ^ X0, wKey[1] ^ X1, wKey[2] ^ X2, wKey[3] ^ X3); LT();
  158. Sb1(wKey[4] ^ X0, wKey[5] ^ X1, wKey[6] ^ X2, wKey[7] ^ X3); LT();
  159. Sb2(wKey[8] ^ X0, wKey[9] ^ X1, wKey[10] ^ X2, wKey[11] ^ X3); LT();
  160. Sb3(wKey[12] ^ X0, wKey[13] ^ X1, wKey[14] ^ X2, wKey[15] ^ X3); LT();
  161. Sb4(wKey[16] ^ X0, wKey[17] ^ X1, wKey[18] ^ X2, wKey[19] ^ X3); LT();
  162. Sb5(wKey[20] ^ X0, wKey[21] ^ X1, wKey[22] ^ X2, wKey[23] ^ X3); LT();
  163. Sb6(wKey[24] ^ X0, wKey[25] ^ X1, wKey[26] ^ X2, wKey[27] ^ X3); LT();
  164. Sb7(wKey[28] ^ X0, wKey[29] ^ X1, wKey[30] ^ X2, wKey[31] ^ X3); LT();
  165. Sb0(wKey[32] ^ X0, wKey[33] ^ X1, wKey[34] ^ X2, wKey[35] ^ X3); LT();
  166. Sb1(wKey[36] ^ X0, wKey[37] ^ X1, wKey[38] ^ X2, wKey[39] ^ X3); LT();
  167. Sb2(wKey[40] ^ X0, wKey[41] ^ X1, wKey[42] ^ X2, wKey[43] ^ X3); LT();
  168. Sb3(wKey[44] ^ X0, wKey[45] ^ X1, wKey[46] ^ X2, wKey[47] ^ X3); LT();
  169. Sb4(wKey[48] ^ X0, wKey[49] ^ X1, wKey[50] ^ X2, wKey[51] ^ X3); LT();
  170. Sb5(wKey[52] ^ X0, wKey[53] ^ X1, wKey[54] ^ X2, wKey[55] ^ X3); LT();
  171. Sb6(wKey[56] ^ X0, wKey[57] ^ X1, wKey[58] ^ X2, wKey[59] ^ X3); LT();
  172. Sb7(wKey[60] ^ X0, wKey[61] ^ X1, wKey[62] ^ X2, wKey[63] ^ X3); LT();
  173. Sb0(wKey[64] ^ X0, wKey[65] ^ X1, wKey[66] ^ X2, wKey[67] ^ X3); LT();
  174. Sb1(wKey[68] ^ X0, wKey[69] ^ X1, wKey[70] ^ X2, wKey[71] ^ X3); LT();
  175. Sb2(wKey[72] ^ X0, wKey[73] ^ X1, wKey[74] ^ X2, wKey[75] ^ X3); LT();
  176. Sb3(wKey[76] ^ X0, wKey[77] ^ X1, wKey[78] ^ X2, wKey[79] ^ X3); LT();
  177. Sb4(wKey[80] ^ X0, wKey[81] ^ X1, wKey[82] ^ X2, wKey[83] ^ X3); LT();
  178. Sb5(wKey[84] ^ X0, wKey[85] ^ X1, wKey[86] ^ X2, wKey[87] ^ X3); LT();
  179. Sb6(wKey[88] ^ X0, wKey[89] ^ X1, wKey[90] ^ X2, wKey[91] ^ X3); LT();
  180. Sb7(wKey[92] ^ X0, wKey[93] ^ X1, wKey[94] ^ X2, wKey[95] ^ X3); LT();
  181. Sb0(wKey[96] ^ X0, wKey[97] ^ X1, wKey[98] ^ X2, wKey[99] ^ X3); LT();
  182. Sb1(wKey[100] ^ X0, wKey[101] ^ X1, wKey[102] ^ X2, wKey[103] ^ X3); LT();
  183. Sb2(wKey[104] ^ X0, wKey[105] ^ X1, wKey[106] ^ X2, wKey[107] ^ X3); LT();
  184. Sb3(wKey[108] ^ X0, wKey[109] ^ X1, wKey[110] ^ X2, wKey[111] ^ X3); LT();
  185. Sb4(wKey[112] ^ X0, wKey[113] ^ X1, wKey[114] ^ X2, wKey[115] ^ X3); LT();
  186. Sb5(wKey[116] ^ X0, wKey[117] ^ X1, wKey[118] ^ X2, wKey[119] ^ X3); LT();
  187. Sb6(wKey[120] ^ X0, wKey[121] ^ X1, wKey[122] ^ X2, wKey[123] ^ X3); LT();
  188. Sb7(wKey[124] ^ X0, wKey[125] ^ X1, wKey[126] ^ X2, wKey[127] ^ X3);
  189. Pack.UInt32_To_LE((uint)(wKey[128] ^ X0), output, outOff);
  190. Pack.UInt32_To_LE((uint)(wKey[129] ^ X1), output, outOff + 4);
  191. Pack.UInt32_To_LE((uint)(wKey[130] ^ X2), output, outOff + 8);
  192. Pack.UInt32_To_LE((uint)(wKey[131] ^ X3), output, outOff + 12);
  193. }
  194. /**
  195. * Decrypt one block of ciphertext.
  196. *
  197. * @param input the array containing the input data.
  198. * @param inOff offset into the in array the data starts at.
  199. * @param output the array the output data will be copied into.
  200. * @param outOff the offset into the out array the output will start at.
  201. */
  202. protected override void DecryptBlock(byte[] input, int inOff, byte[] output, int outOff)
  203. {
  204. X0 = wKey[128] ^ (int)Pack.LE_To_UInt32(input, inOff);
  205. X1 = wKey[129] ^ (int)Pack.LE_To_UInt32(input, inOff + 4);
  206. X2 = wKey[130] ^ (int)Pack.LE_To_UInt32(input, inOff + 8);
  207. X3 = wKey[131] ^ (int)Pack.LE_To_UInt32(input, inOff + 12);
  208. Ib7(X0, X1, X2, X3);
  209. X0 ^= wKey[124]; X1 ^= wKey[125]; X2 ^= wKey[126]; X3 ^= wKey[127];
  210. InverseLT(); Ib6(X0, X1, X2, X3);
  211. X0 ^= wKey[120]; X1 ^= wKey[121]; X2 ^= wKey[122]; X3 ^= wKey[123];
  212. InverseLT(); Ib5(X0, X1, X2, X3);
  213. X0 ^= wKey[116]; X1 ^= wKey[117]; X2 ^= wKey[118]; X3 ^= wKey[119];
  214. InverseLT(); Ib4(X0, X1, X2, X3);
  215. X0 ^= wKey[112]; X1 ^= wKey[113]; X2 ^= wKey[114]; X3 ^= wKey[115];
  216. InverseLT(); Ib3(X0, X1, X2, X3);
  217. X0 ^= wKey[108]; X1 ^= wKey[109]; X2 ^= wKey[110]; X3 ^= wKey[111];
  218. InverseLT(); Ib2(X0, X1, X2, X3);
  219. X0 ^= wKey[104]; X1 ^= wKey[105]; X2 ^= wKey[106]; X3 ^= wKey[107];
  220. InverseLT(); Ib1(X0, X1, X2, X3);
  221. X0 ^= wKey[100]; X1 ^= wKey[101]; X2 ^= wKey[102]; X3 ^= wKey[103];
  222. InverseLT(); Ib0(X0, X1, X2, X3);
  223. X0 ^= wKey[96]; X1 ^= wKey[97]; X2 ^= wKey[98]; X3 ^= wKey[99];
  224. InverseLT(); Ib7(X0, X1, X2, X3);
  225. X0 ^= wKey[92]; X1 ^= wKey[93]; X2 ^= wKey[94]; X3 ^= wKey[95];
  226. InverseLT(); Ib6(X0, X1, X2, X3);
  227. X0 ^= wKey[88]; X1 ^= wKey[89]; X2 ^= wKey[90]; X3 ^= wKey[91];
  228. InverseLT(); Ib5(X0, X1, X2, X3);
  229. X0 ^= wKey[84]; X1 ^= wKey[85]; X2 ^= wKey[86]; X3 ^= wKey[87];
  230. InverseLT(); Ib4(X0, X1, X2, X3);
  231. X0 ^= wKey[80]; X1 ^= wKey[81]; X2 ^= wKey[82]; X3 ^= wKey[83];
  232. InverseLT(); Ib3(X0, X1, X2, X3);
  233. X0 ^= wKey[76]; X1 ^= wKey[77]; X2 ^= wKey[78]; X3 ^= wKey[79];
  234. InverseLT(); Ib2(X0, X1, X2, X3);
  235. X0 ^= wKey[72]; X1 ^= wKey[73]; X2 ^= wKey[74]; X3 ^= wKey[75];
  236. InverseLT(); Ib1(X0, X1, X2, X3);
  237. X0 ^= wKey[68]; X1 ^= wKey[69]; X2 ^= wKey[70]; X3 ^= wKey[71];
  238. InverseLT(); Ib0(X0, X1, X2, X3);
  239. X0 ^= wKey[64]; X1 ^= wKey[65]; X2 ^= wKey[66]; X3 ^= wKey[67];
  240. InverseLT(); Ib7(X0, X1, X2, X3);
  241. X0 ^= wKey[60]; X1 ^= wKey[61]; X2 ^= wKey[62]; X3 ^= wKey[63];
  242. InverseLT(); Ib6(X0, X1, X2, X3);
  243. X0 ^= wKey[56]; X1 ^= wKey[57]; X2 ^= wKey[58]; X3 ^= wKey[59];
  244. InverseLT(); Ib5(X0, X1, X2, X3);
  245. X0 ^= wKey[52]; X1 ^= wKey[53]; X2 ^= wKey[54]; X3 ^= wKey[55];
  246. InverseLT(); Ib4(X0, X1, X2, X3);
  247. X0 ^= wKey[48]; X1 ^= wKey[49]; X2 ^= wKey[50]; X3 ^= wKey[51];
  248. InverseLT(); Ib3(X0, X1, X2, X3);
  249. X0 ^= wKey[44]; X1 ^= wKey[45]; X2 ^= wKey[46]; X3 ^= wKey[47];
  250. InverseLT(); Ib2(X0, X1, X2, X3);
  251. X0 ^= wKey[40]; X1 ^= wKey[41]; X2 ^= wKey[42]; X3 ^= wKey[43];
  252. InverseLT(); Ib1(X0, X1, X2, X3);
  253. X0 ^= wKey[36]; X1 ^= wKey[37]; X2 ^= wKey[38]; X3 ^= wKey[39];
  254. InverseLT(); Ib0(X0, X1, X2, X3);
  255. X0 ^= wKey[32]; X1 ^= wKey[33]; X2 ^= wKey[34]; X3 ^= wKey[35];
  256. InverseLT(); Ib7(X0, X1, X2, X3);
  257. X0 ^= wKey[28]; X1 ^= wKey[29]; X2 ^= wKey[30]; X3 ^= wKey[31];
  258. InverseLT(); Ib6(X0, X1, X2, X3);
  259. X0 ^= wKey[24]; X1 ^= wKey[25]; X2 ^= wKey[26]; X3 ^= wKey[27];
  260. InverseLT(); Ib5(X0, X1, X2, X3);
  261. X0 ^= wKey[20]; X1 ^= wKey[21]; X2 ^= wKey[22]; X3 ^= wKey[23];
  262. InverseLT(); Ib4(X0, X1, X2, X3);
  263. X0 ^= wKey[16]; X1 ^= wKey[17]; X2 ^= wKey[18]; X3 ^= wKey[19];
  264. InverseLT(); Ib3(X0, X1, X2, X3);
  265. X0 ^= wKey[12]; X1 ^= wKey[13]; X2 ^= wKey[14]; X3 ^= wKey[15];
  266. InverseLT(); Ib2(X0, X1, X2, X3);
  267. X0 ^= wKey[8]; X1 ^= wKey[9]; X2 ^= wKey[10]; X3 ^= wKey[11];
  268. InverseLT(); Ib1(X0, X1, X2, X3);
  269. X0 ^= wKey[4]; X1 ^= wKey[5]; X2 ^= wKey[6]; X3 ^= wKey[7];
  270. InverseLT(); Ib0(X0, X1, X2, X3);
  271. Pack.UInt32_To_LE((uint)(X0 ^ wKey[0]), output, outOff);
  272. Pack.UInt32_To_LE((uint)(X1 ^ wKey[1]), output, outOff + 4);
  273. Pack.UInt32_To_LE((uint)(X2 ^ wKey[2]), output, outOff + 8);
  274. Pack.UInt32_To_LE((uint)(X3 ^ wKey[3]), output, outOff + 12);
  275. }
  276. }
  277. }
  278. #endif