AesLightEngine.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  9. {
  10. /**
  11. * an implementation of the AES (Rijndael), from FIPS-197.
  12. * <p>
  13. * For further details see: <a href="http://csrc.nist.gov/encryption/aes/">http://csrc.nist.gov/encryption/aes/</a>.
  14. *
  15. * This implementation is based on optimizations from Dr. Brian Gladman's paper and C code at
  16. * <a href="http://fp.gladman.plus.com/cryptography_technology/rijndael/">http://fp.gladman.plus.com/cryptography_technology/rijndael/</a>
  17. *
  18. * There are three levels of tradeoff of speed vs memory
  19. * Because java has no preprocessor, they are written as three separate classes from which to choose
  20. *
  21. * The fastest uses 8Kbytes of static tables to precompute round calculations, 4 256 word tables for encryption
  22. * and 4 for decryption.
  23. *
  24. * The middle performance version uses only one 256 word table for each, for a total of 2Kbytes,
  25. * adding 12 rotate operations per round to compute the values contained in the other tables from
  26. * the contents of the first
  27. *
  28. * The slowest version uses no static tables at all and computes the values
  29. * in each round.
  30. * </p>
  31. * <p>
  32. * This file contains the slowest performance version with no static tables
  33. * for round precomputation, but it has the smallest foot print.
  34. * </p>
  35. */
  36. public class AesLightEngine
  37. : IBlockCipher
  38. {
  39. // The S box
  40. private static readonly byte[] S =
  41. {
  42. 99, 124, 119, 123, 242, 107, 111, 197,
  43. 48, 1, 103, 43, 254, 215, 171, 118,
  44. 202, 130, 201, 125, 250, 89, 71, 240,
  45. 173, 212, 162, 175, 156, 164, 114, 192,
  46. 183, 253, 147, 38, 54, 63, 247, 204,
  47. 52, 165, 229, 241, 113, 216, 49, 21,
  48. 4, 199, 35, 195, 24, 150, 5, 154,
  49. 7, 18, 128, 226, 235, 39, 178, 117,
  50. 9, 131, 44, 26, 27, 110, 90, 160,
  51. 82, 59, 214, 179, 41, 227, 47, 132,
  52. 83, 209, 0, 237, 32, 252, 177, 91,
  53. 106, 203, 190, 57, 74, 76, 88, 207,
  54. 208, 239, 170, 251, 67, 77, 51, 133,
  55. 69, 249, 2, 127, 80, 60, 159, 168,
  56. 81, 163, 64, 143, 146, 157, 56, 245,
  57. 188, 182, 218, 33, 16, 255, 243, 210,
  58. 205, 12, 19, 236, 95, 151, 68, 23,
  59. 196, 167, 126, 61, 100, 93, 25, 115,
  60. 96, 129, 79, 220, 34, 42, 144, 136,
  61. 70, 238, 184, 20, 222, 94, 11, 219,
  62. 224, 50, 58, 10, 73, 6, 36, 92,
  63. 194, 211, 172, 98, 145, 149, 228, 121,
  64. 231, 200, 55, 109, 141, 213, 78, 169,
  65. 108, 86, 244, 234, 101, 122, 174, 8,
  66. 186, 120, 37, 46, 28, 166, 180, 198,
  67. 232, 221, 116, 31, 75, 189, 139, 138,
  68. 112, 62, 181, 102, 72, 3, 246, 14,
  69. 97, 53, 87, 185, 134, 193, 29, 158,
  70. 225, 248, 152, 17, 105, 217, 142, 148,
  71. 155, 30, 135, 233, 206, 85, 40, 223,
  72. 140, 161, 137, 13, 191, 230, 66, 104,
  73. 65, 153, 45, 15, 176, 84, 187, 22,
  74. };
  75. // The inverse S-box
  76. private static readonly byte[] Si =
  77. {
  78. 82, 9, 106, 213, 48, 54, 165, 56,
  79. 191, 64, 163, 158, 129, 243, 215, 251,
  80. 124, 227, 57, 130, 155, 47, 255, 135,
  81. 52, 142, 67, 68, 196, 222, 233, 203,
  82. 84, 123, 148, 50, 166, 194, 35, 61,
  83. 238, 76, 149, 11, 66, 250, 195, 78,
  84. 8, 46, 161, 102, 40, 217, 36, 178,
  85. 118, 91, 162, 73, 109, 139, 209, 37,
  86. 114, 248, 246, 100, 134, 104, 152, 22,
  87. 212, 164, 92, 204, 93, 101, 182, 146,
  88. 108, 112, 72, 80, 253, 237, 185, 218,
  89. 94, 21, 70, 87, 167, 141, 157, 132,
  90. 144, 216, 171, 0, 140, 188, 211, 10,
  91. 247, 228, 88, 5, 184, 179, 69, 6,
  92. 208, 44, 30, 143, 202, 63, 15, 2,
  93. 193, 175, 189, 3, 1, 19, 138, 107,
  94. 58, 145, 17, 65, 79, 103, 220, 234,
  95. 151, 242, 207, 206, 240, 180, 230, 115,
  96. 150, 172, 116, 34, 231, 173, 53, 133,
  97. 226, 249, 55, 232, 28, 117, 223, 110,
  98. 71, 241, 26, 113, 29, 41, 197, 137,
  99. 111, 183, 98, 14, 170, 24, 190, 27,
  100. 252, 86, 62, 75, 198, 210, 121, 32,
  101. 154, 219, 192, 254, 120, 205, 90, 244,
  102. 31, 221, 168, 51, 136, 7, 199, 49,
  103. 177, 18, 16, 89, 39, 128, 236, 95,
  104. 96, 81, 127, 169, 25, 181, 74, 13,
  105. 45, 229, 122, 159, 147, 201, 156, 239,
  106. 160, 224, 59, 77, 174, 42, 245, 176,
  107. 200, 235, 187, 60, 131, 83, 153, 97,
  108. 23, 43, 4, 126, 186, 119, 214, 38,
  109. 225, 105, 20, 99, 85, 33, 12, 125,
  110. };
  111. // vector used in calculating key schedule (powers of x in GF(256))
  112. private static readonly byte[] rcon =
  113. {
  114. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
  115. 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91
  116. };
  117. private static uint Shift(uint r, int shift)
  118. {
  119. return (r >> shift) | (r << (32 - shift));
  120. }
  121. /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
  122. private const uint m1 = 0x80808080;
  123. private const uint m2 = 0x7f7f7f7f;
  124. private const uint m3 = 0x0000001b;
  125. private const uint m4 = 0xC0C0C0C0;
  126. private const uint m5 = 0x3f3f3f3f;
  127. private static uint FFmulX(uint x)
  128. {
  129. return ((x & m2) << 1) ^ (((x & m1) >> 7) * m3);
  130. }
  131. private static uint FFmulX2(uint x)
  132. {
  133. uint t0 = (x & m5) << 2;
  134. uint t1 = (x & m4);
  135. t1 ^= (t1 >> 1);
  136. return t0 ^ (t1 >> 2) ^ (t1 >> 5);
  137. }
  138. /*
  139. The following defines provide alternative definitions of FFmulX that might
  140. give improved performance if a fast 32-bit multiply is not available.
  141. private int FFmulX(int x) { int u = x & m1; u |= (u >> 1); return ((x & m2) << 1) ^ ((u >>> 3) | (u >>> 6)); }
  142. private static final int m4 = 0x1b1b1b1b;
  143. private int FFmulX(int x) { int u = x & m1; return ((x & m2) << 1) ^ ((u - (u >>> 7)) & m4); }
  144. */
  145. private static uint Mcol(uint x)
  146. {
  147. uint t0, t1;
  148. t0 = Shift(x, 8);
  149. t1 = x ^ t0;
  150. return Shift(t1, 16) ^ t0 ^ FFmulX(t1);
  151. }
  152. private static uint Inv_Mcol(uint x)
  153. {
  154. uint t0, t1;
  155. t0 = x;
  156. t1 = t0 ^ Shift(t0, 8);
  157. t0 ^= FFmulX(t1);
  158. t1 ^= FFmulX2(t0);
  159. t0 ^= t1 ^ Shift(t1, 16);
  160. return t0;
  161. }
  162. private static uint SubWord(uint x)
  163. {
  164. return (uint)S[x&255]
  165. | (((uint)S[(x>>8)&255]) << 8)
  166. | (((uint)S[(x>>16)&255]) << 16)
  167. | (((uint)S[(x>>24)&255]) << 24);
  168. }
  169. /**
  170. * Calculate the necessary round keys
  171. * The number of calculations depends on key size and block size
  172. * AES specified a fixed block size of 128 bits and key sizes 128/192/256 bits
  173. * This code is written assuming those are the only possible values
  174. */
  175. private uint[][] GenerateWorkingKey(byte[] key, bool forEncryption)
  176. {
  177. int keyLen = key.Length;
  178. if (keyLen < 16 || keyLen > 32 || (keyLen & 7) != 0)
  179. throw new ArgumentException("Key length not 128/192/256 bits.");
  180. int KC = keyLen >> 2;
  181. this.ROUNDS = KC + 6; // This is not always true for the generalized Rijndael that allows larger block sizes
  182. uint[][] W = new uint[ROUNDS + 1][]; // 4 words in a block
  183. for (int i = 0; i <= ROUNDS; ++i)
  184. {
  185. W[i] = new uint[4];
  186. }
  187. switch (KC)
  188. {
  189. case 4:
  190. {
  191. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  192. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  193. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  194. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  195. for (int i = 1; i <= 10; ++i)
  196. {
  197. uint u = SubWord(Shift(t3, 8)) ^ rcon[i - 1];
  198. t0 ^= u; W[i][0] = t0;
  199. t1 ^= t0; W[i][1] = t1;
  200. t2 ^= t1; W[i][2] = t2;
  201. t3 ^= t2; W[i][3] = t3;
  202. }
  203. break;
  204. }
  205. case 6:
  206. {
  207. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  208. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  209. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  210. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  211. uint t4 = Pack.LE_To_UInt32(key, 16); W[1][0] = t4;
  212. uint t5 = Pack.LE_To_UInt32(key, 20); W[1][1] = t5;
  213. uint rcon = 1;
  214. uint u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  215. t0 ^= u; W[1][2] = t0;
  216. t1 ^= t0; W[1][3] = t1;
  217. t2 ^= t1; W[2][0] = t2;
  218. t3 ^= t2; W[2][1] = t3;
  219. t4 ^= t3; W[2][2] = t4;
  220. t5 ^= t4; W[2][3] = t5;
  221. for (int i = 3; i < 12; i += 3)
  222. {
  223. u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  224. t0 ^= u; W[i ][0] = t0;
  225. t1 ^= t0; W[i ][1] = t1;
  226. t2 ^= t1; W[i ][2] = t2;
  227. t3 ^= t2; W[i ][3] = t3;
  228. t4 ^= t3; W[i + 1][0] = t4;
  229. t5 ^= t4; W[i + 1][1] = t5;
  230. u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  231. t0 ^= u; W[i + 1][2] = t0;
  232. t1 ^= t0; W[i + 1][3] = t1;
  233. t2 ^= t1; W[i + 2][0] = t2;
  234. t3 ^= t2; W[i + 2][1] = t3;
  235. t4 ^= t3; W[i + 2][2] = t4;
  236. t5 ^= t4; W[i + 2][3] = t5;
  237. }
  238. u = SubWord(Shift(t5, 8)) ^ rcon;
  239. t0 ^= u; W[12][0] = t0;
  240. t1 ^= t0; W[12][1] = t1;
  241. t2 ^= t1; W[12][2] = t2;
  242. t3 ^= t2; W[12][3] = t3;
  243. break;
  244. }
  245. case 8:
  246. {
  247. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  248. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  249. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  250. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  251. uint t4 = Pack.LE_To_UInt32(key, 16); W[1][0] = t4;
  252. uint t5 = Pack.LE_To_UInt32(key, 20); W[1][1] = t5;
  253. uint t6 = Pack.LE_To_UInt32(key, 24); W[1][2] = t6;
  254. uint t7 = Pack.LE_To_UInt32(key, 28); W[1][3] = t7;
  255. uint u, rcon = 1;
  256. for (int i = 2; i < 14; i += 2)
  257. {
  258. u = SubWord(Shift(t7, 8)) ^ rcon; rcon <<= 1;
  259. t0 ^= u; W[i ][0] = t0;
  260. t1 ^= t0; W[i ][1] = t1;
  261. t2 ^= t1; W[i ][2] = t2;
  262. t3 ^= t2; W[i ][3] = t3;
  263. u = SubWord(t3);
  264. t4 ^= u; W[i + 1][0] = t4;
  265. t5 ^= t4; W[i + 1][1] = t5;
  266. t6 ^= t5; W[i + 1][2] = t6;
  267. t7 ^= t6; W[i + 1][3] = t7;
  268. }
  269. u = SubWord(Shift(t7, 8)) ^ rcon;
  270. t0 ^= u; W[14][0] = t0;
  271. t1 ^= t0; W[14][1] = t1;
  272. t2 ^= t1; W[14][2] = t2;
  273. t3 ^= t2; W[14][3] = t3;
  274. break;
  275. }
  276. default:
  277. {
  278. throw new InvalidOperationException("Should never get here");
  279. }
  280. }
  281. if (!forEncryption)
  282. {
  283. for (int j = 1; j < ROUNDS; j++)
  284. {
  285. uint[] w = W[j];
  286. for (int i = 0; i < 4; i++)
  287. {
  288. w[i] = Inv_Mcol(w[i]);
  289. }
  290. }
  291. }
  292. return W;
  293. }
  294. private int ROUNDS;
  295. private uint[][] WorkingKey;
  296. private bool forEncryption;
  297. private const int BLOCK_SIZE = 16;
  298. /**
  299. * default constructor - 128 bit block size.
  300. */
  301. public AesLightEngine()
  302. {
  303. }
  304. /**
  305. * initialise an AES cipher.
  306. *
  307. * @param forEncryption whether or not we are for encryption.
  308. * @param parameters the parameters required to set up the cipher.
  309. * @exception ArgumentException if the parameters argument is
  310. * inappropriate.
  311. */
  312. public virtual void Init(
  313. bool forEncryption,
  314. ICipherParameters parameters)
  315. {
  316. KeyParameter keyParameter = parameters as KeyParameter;
  317. if (keyParameter == null)
  318. throw new ArgumentException("invalid parameter passed to AES init - "
  319. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  320. WorkingKey = GenerateWorkingKey(keyParameter.GetKey(), forEncryption);
  321. this.forEncryption = forEncryption;
  322. }
  323. public virtual string AlgorithmName
  324. {
  325. get { return "AES"; }
  326. }
  327. public virtual bool IsPartialBlockOkay
  328. {
  329. get { return false; }
  330. }
  331. public virtual int GetBlockSize()
  332. {
  333. return BLOCK_SIZE;
  334. }
  335. public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  336. {
  337. if (WorkingKey == null)
  338. throw new InvalidOperationException("AES engine not initialised");
  339. Check.DataLength(input, inOff, 16, "input buffer too short");
  340. Check.OutputLength(output, outOff, 16, "output buffer too short");
  341. if (forEncryption)
  342. {
  343. EncryptBlock(input, inOff, output, outOff, WorkingKey);
  344. }
  345. else
  346. {
  347. DecryptBlock(input, inOff, output, outOff, WorkingKey);
  348. }
  349. return BLOCK_SIZE;
  350. }
  351. public virtual void Reset()
  352. {
  353. }
  354. private void EncryptBlock(byte[] input, int inOff, byte[] output, int outOff, uint[][] KW)
  355. {
  356. uint C0 = Pack.LE_To_UInt32(input, inOff + 0);
  357. uint C1 = Pack.LE_To_UInt32(input, inOff + 4);
  358. uint C2 = Pack.LE_To_UInt32(input, inOff + 8);
  359. uint C3 = Pack.LE_To_UInt32(input, inOff + 12);
  360. uint[] kw = KW[0];
  361. uint t0 = C0 ^ kw[0];
  362. uint t1 = C1 ^ kw[1];
  363. uint t2 = C2 ^ kw[2];
  364. uint r0, r1, r2, r3 = C3 ^ kw[3];
  365. int r = 1;
  366. while (r < ROUNDS - 1)
  367. {
  368. kw = KW[r++];
  369. r0 = Mcol((uint)S[t0 & 255] ^ (((uint)S[(t1 >> 8) & 255]) << 8) ^ (((uint)S[(t2 >> 16) & 255]) << 16) ^ (((uint)S[(r3 >> 24) & 255]) << 24)) ^ kw[0];
  370. r1 = Mcol((uint)S[t1 & 255] ^ (((uint)S[(t2 >> 8) & 255]) << 8) ^ (((uint)S[(r3 >> 16) & 255]) << 16) ^ (((uint)S[(t0 >> 24) & 255]) << 24)) ^ kw[1];
  371. r2 = Mcol((uint)S[t2 & 255] ^ (((uint)S[(r3 >> 8) & 255]) << 8) ^ (((uint)S[(t0 >> 16) & 255]) << 16) ^ (((uint)S[(t1 >> 24) & 255]) << 24)) ^ kw[2];
  372. r3 = Mcol((uint)S[r3 & 255] ^ (((uint)S[(t0 >> 8) & 255]) << 8) ^ (((uint)S[(t1 >> 16) & 255]) << 16) ^ (((uint)S[(t2 >> 24) & 255]) << 24)) ^ kw[3];
  373. kw = KW[r++];
  374. t0 = Mcol((uint)S[r0 & 255] ^ (((uint)S[(r1 >> 8) & 255]) << 8) ^ (((uint)S[(r2 >> 16) & 255]) << 16) ^ (((uint)S[(r3 >> 24) & 255]) << 24)) ^ kw[0];
  375. t1 = Mcol((uint)S[r1 & 255] ^ (((uint)S[(r2 >> 8) & 255]) << 8) ^ (((uint)S[(r3 >> 16) & 255]) << 16) ^ (((uint)S[(r0 >> 24) & 255]) << 24)) ^ kw[1];
  376. t2 = Mcol((uint)S[r2 & 255] ^ (((uint)S[(r3 >> 8) & 255]) << 8) ^ (((uint)S[(r0 >> 16) & 255]) << 16) ^ (((uint)S[(r1 >> 24) & 255]) << 24)) ^ kw[2];
  377. r3 = Mcol((uint)S[r3 & 255] ^ (((uint)S[(r0 >> 8) & 255]) << 8) ^ (((uint)S[(r1 >> 16) & 255]) << 16) ^ (((uint)S[(r2 >> 24) & 255]) << 24)) ^ kw[3];
  378. }
  379. kw = KW[r++];
  380. r0 = Mcol((uint)S[t0 & 255] ^ (((uint)S[(t1 >> 8) & 255]) << 8) ^ (((uint)S[(t2 >> 16) & 255]) << 16) ^ (((uint)S[(r3 >> 24) & 255]) << 24)) ^ kw[0];
  381. r1 = Mcol((uint)S[t1 & 255] ^ (((uint)S[(t2 >> 8) & 255]) << 8) ^ (((uint)S[(r3 >> 16) & 255]) << 16) ^ (((uint)S[(t0 >> 24) & 255]) << 24)) ^ kw[1];
  382. r2 = Mcol((uint)S[t2 & 255] ^ (((uint)S[(r3 >> 8) & 255]) << 8) ^ (((uint)S[(t0 >> 16) & 255]) << 16) ^ (((uint)S[(t1 >> 24) & 255]) << 24)) ^ kw[2];
  383. r3 = Mcol((uint)S[r3 & 255] ^ (((uint)S[(t0 >> 8) & 255]) << 8) ^ (((uint)S[(t1 >> 16) & 255]) << 16) ^ (((uint)S[(t2 >> 24) & 255]) << 24)) ^ kw[3];
  384. // the final round is a simple function of S
  385. kw = KW[r];
  386. C0 = (uint)S[r0 & 255] ^ (((uint)S[(r1 >> 8) & 255]) << 8) ^ (((uint)S[(r2 >> 16) & 255]) << 16) ^ (((uint)S[(r3 >> 24) & 255]) << 24) ^ kw[0];
  387. C1 = (uint)S[r1 & 255] ^ (((uint)S[(r2 >> 8) & 255]) << 8) ^ (((uint)S[(r3 >> 16) & 255]) << 16) ^ (((uint)S[(r0 >> 24) & 255]) << 24) ^ kw[1];
  388. C2 = (uint)S[r2 & 255] ^ (((uint)S[(r3 >> 8) & 255]) << 8) ^ (((uint)S[(r0 >> 16) & 255]) << 16) ^ (((uint)S[(r1 >> 24) & 255]) << 24) ^ kw[2];
  389. C3 = (uint)S[r3 & 255] ^ (((uint)S[(r0 >> 8) & 255]) << 8) ^ (((uint)S[(r1 >> 16) & 255]) << 16) ^ (((uint)S[(r2 >> 24) & 255]) << 24) ^ kw[3];
  390. Pack.UInt32_To_LE(C0, output, outOff + 0);
  391. Pack.UInt32_To_LE(C1, output, outOff + 4);
  392. Pack.UInt32_To_LE(C2, output, outOff + 8);
  393. Pack.UInt32_To_LE(C3, output, outOff + 12);
  394. }
  395. private void DecryptBlock(byte[] input, int inOff, byte[] output, int outOff, uint[][] KW)
  396. {
  397. uint C0 = Pack.LE_To_UInt32(input, inOff + 0);
  398. uint C1 = Pack.LE_To_UInt32(input, inOff + 4);
  399. uint C2 = Pack.LE_To_UInt32(input, inOff + 8);
  400. uint C3 = Pack.LE_To_UInt32(input, inOff + 12);
  401. uint[] kw = KW[ROUNDS];
  402. uint t0 = C0 ^ kw[0];
  403. uint t1 = C1 ^ kw[1];
  404. uint t2 = C2 ^ kw[2];
  405. uint r0, r1, r2, r3 = C3 ^ kw[3];
  406. int r = ROUNDS - 1;
  407. while (r > 1)
  408. {
  409. kw = KW[r--];
  410. r0 = Inv_Mcol((uint)Si[t0 & 255] ^ (((uint)Si[(r3 >> 8) & 255]) << 8) ^ (((uint)Si[(t2 >> 16) & 255]) << 16) ^ ((uint)Si[(t1 >> 24) & 255] << 24)) ^ kw[0];
  411. r1 = Inv_Mcol((uint)Si[t1 & 255] ^ (((uint)Si[(t0 >> 8) & 255]) << 8) ^ (((uint)Si[(r3 >> 16) & 255]) << 16) ^ ((uint)Si[(t2 >> 24) & 255] << 24)) ^ kw[1];
  412. r2 = Inv_Mcol((uint)Si[t2 & 255] ^ (((uint)Si[(t1 >> 8) & 255]) << 8) ^ (((uint)Si[(t0 >> 16) & 255]) << 16) ^ ((uint)Si[(r3 >> 24) & 255] << 24)) ^ kw[2];
  413. r3 = Inv_Mcol((uint)Si[r3 & 255] ^ (((uint)Si[(t2 >> 8) & 255]) << 8) ^ (((uint)Si[(t1 >> 16) & 255]) << 16) ^ ((uint)Si[(t0 >> 24) & 255] << 24)) ^ kw[3];
  414. kw = KW[r--];
  415. t0 = Inv_Mcol((uint)Si[r0 & 255] ^ (((uint)Si[(r3 >> 8) & 255]) << 8) ^ (((uint)Si[(r2 >> 16) & 255]) << 16) ^ ((uint)Si[(r1 >> 24) & 255] << 24)) ^ kw[0];
  416. t1 = Inv_Mcol((uint)Si[r1 & 255] ^ (((uint)Si[(r0 >> 8) & 255]) << 8) ^ (((uint)Si[(r3 >> 16) & 255]) << 16) ^ ((uint)Si[(r2 >> 24) & 255] << 24)) ^ kw[1];
  417. t2 = Inv_Mcol((uint)Si[r2 & 255] ^ (((uint)Si[(r1 >> 8) & 255]) << 8) ^ (((uint)Si[(r0 >> 16) & 255]) << 16) ^ ((uint)Si[(r3 >> 24) & 255] << 24)) ^ kw[2];
  418. r3 = Inv_Mcol((uint)Si[r3 & 255] ^ (((uint)Si[(r2 >> 8) & 255]) << 8) ^ (((uint)Si[(r1 >> 16) & 255]) << 16) ^ ((uint)Si[(r0 >> 24) & 255] << 24)) ^ kw[3];
  419. }
  420. kw = KW[1];
  421. r0 = Inv_Mcol((uint)Si[t0 & 255] ^ (((uint)Si[(r3 >> 8) & 255]) << 8) ^ (((uint)Si[(t2 >> 16) & 255]) << 16) ^ ((uint)Si[(t1 >> 24) & 255] << 24)) ^ kw[0];
  422. r1 = Inv_Mcol((uint)Si[t1 & 255] ^ (((uint)Si[(t0 >> 8) & 255]) << 8) ^ (((uint)Si[(r3 >> 16) & 255]) << 16) ^ ((uint)Si[(t2 >> 24) & 255] << 24)) ^ kw[1];
  423. r2 = Inv_Mcol((uint)Si[t2 & 255] ^ (((uint)Si[(t1 >> 8) & 255]) << 8) ^ (((uint)Si[(t0 >> 16) & 255]) << 16) ^ ((uint)Si[(r3 >> 24) & 255] << 24)) ^ kw[2];
  424. r3 = Inv_Mcol((uint)Si[r3 & 255] ^ (((uint)Si[(t2 >> 8) & 255]) << 8) ^ (((uint)Si[(t1 >> 16) & 255]) << 16) ^ ((uint)Si[(t0 >> 24) & 255] << 24)) ^ kw[3];
  425. // the final round's table is a simple function of Si
  426. kw = KW[0];
  427. C0 = (uint)Si[r0 & 255] ^ (((uint)Si[(r3 >> 8) & 255]) << 8) ^ (((uint)Si[(r2 >> 16) & 255]) << 16) ^ (((uint)Si[(r1 >> 24) & 255]) << 24) ^ kw[0];
  428. C1 = (uint)Si[r1 & 255] ^ (((uint)Si[(r0 >> 8) & 255]) << 8) ^ (((uint)Si[(r3 >> 16) & 255]) << 16) ^ (((uint)Si[(r2 >> 24) & 255]) << 24) ^ kw[1];
  429. C2 = (uint)Si[r2 & 255] ^ (((uint)Si[(r1 >> 8) & 255]) << 8) ^ (((uint)Si[(r0 >> 16) & 255]) << 16) ^ (((uint)Si[(r3 >> 24) & 255]) << 24) ^ kw[2];
  430. C3 = (uint)Si[r3 & 255] ^ (((uint)Si[(r2 >> 8) & 255]) << 8) ^ (((uint)Si[(r1 >> 16) & 255]) << 16) ^ (((uint)Si[(r0 >> 24) & 255]) << 24) ^ kw[3];
  431. Pack.UInt32_To_LE(C0, output, outOff + 0);
  432. Pack.UInt32_To_LE(C1, output, outOff + 4);
  433. Pack.UInt32_To_LE(C2, output, outOff + 8);
  434. Pack.UInt32_To_LE(C3, output, outOff + 12);
  435. }
  436. }
  437. }
  438. #pragma warning restore
  439. #endif