TwofishEngine.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. /**
  9. * A class that provides Twofish encryption operations.
  10. *
  11. * This Java implementation is based on the Java reference
  12. * implementation provided by Bruce Schneier and developed
  13. * by Raif S. Naffah.
  14. */
  15. public sealed class TwofishEngine
  16. : IBlockCipher
  17. {
  18. private static readonly byte[,] P = {
  19. { // p0
  20. (byte) 0xA9, (byte) 0x67, (byte) 0xB3, (byte) 0xE8,
  21. (byte) 0x04, (byte) 0xFD, (byte) 0xA3, (byte) 0x76,
  22. (byte) 0x9A, (byte) 0x92, (byte) 0x80, (byte) 0x78,
  23. (byte) 0xE4, (byte) 0xDD, (byte) 0xD1, (byte) 0x38,
  24. (byte) 0x0D, (byte) 0xC6, (byte) 0x35, (byte) 0x98,
  25. (byte) 0x18, (byte) 0xF7, (byte) 0xEC, (byte) 0x6C,
  26. (byte) 0x43, (byte) 0x75, (byte) 0x37, (byte) 0x26,
  27. (byte) 0xFA, (byte) 0x13, (byte) 0x94, (byte) 0x48,
  28. (byte) 0xF2, (byte) 0xD0, (byte) 0x8B, (byte) 0x30,
  29. (byte) 0x84, (byte) 0x54, (byte) 0xDF, (byte) 0x23,
  30. (byte) 0x19, (byte) 0x5B, (byte) 0x3D, (byte) 0x59,
  31. (byte) 0xF3, (byte) 0xAE, (byte) 0xA2, (byte) 0x82,
  32. (byte) 0x63, (byte) 0x01, (byte) 0x83, (byte) 0x2E,
  33. (byte) 0xD9, (byte) 0x51, (byte) 0x9B, (byte) 0x7C,
  34. (byte) 0xA6, (byte) 0xEB, (byte) 0xA5, (byte) 0xBE,
  35. (byte) 0x16, (byte) 0x0C, (byte) 0xE3, (byte) 0x61,
  36. (byte) 0xC0, (byte) 0x8C, (byte) 0x3A, (byte) 0xF5,
  37. (byte) 0x73, (byte) 0x2C, (byte) 0x25, (byte) 0x0B,
  38. (byte) 0xBB, (byte) 0x4E, (byte) 0x89, (byte) 0x6B,
  39. (byte) 0x53, (byte) 0x6A, (byte) 0xB4, (byte) 0xF1,
  40. (byte) 0xE1, (byte) 0xE6, (byte) 0xBD, (byte) 0x45,
  41. (byte) 0xE2, (byte) 0xF4, (byte) 0xB6, (byte) 0x66,
  42. (byte) 0xCC, (byte) 0x95, (byte) 0x03, (byte) 0x56,
  43. (byte) 0xD4, (byte) 0x1C, (byte) 0x1E, (byte) 0xD7,
  44. (byte) 0xFB, (byte) 0xC3, (byte) 0x8E, (byte) 0xB5,
  45. (byte) 0xE9, (byte) 0xCF, (byte) 0xBF, (byte) 0xBA,
  46. (byte) 0xEA, (byte) 0x77, (byte) 0x39, (byte) 0xAF,
  47. (byte) 0x33, (byte) 0xC9, (byte) 0x62, (byte) 0x71,
  48. (byte) 0x81, (byte) 0x79, (byte) 0x09, (byte) 0xAD,
  49. (byte) 0x24, (byte) 0xCD, (byte) 0xF9, (byte) 0xD8,
  50. (byte) 0xE5, (byte) 0xC5, (byte) 0xB9, (byte) 0x4D,
  51. (byte) 0x44, (byte) 0x08, (byte) 0x86, (byte) 0xE7,
  52. (byte) 0xA1, (byte) 0x1D, (byte) 0xAA, (byte) 0xED,
  53. (byte) 0x06, (byte) 0x70, (byte) 0xB2, (byte) 0xD2,
  54. (byte) 0x41, (byte) 0x7B, (byte) 0xA0, (byte) 0x11,
  55. (byte) 0x31, (byte) 0xC2, (byte) 0x27, (byte) 0x90,
  56. (byte) 0x20, (byte) 0xF6, (byte) 0x60, (byte) 0xFF,
  57. (byte) 0x96, (byte) 0x5C, (byte) 0xB1, (byte) 0xAB,
  58. (byte) 0x9E, (byte) 0x9C, (byte) 0x52, (byte) 0x1B,
  59. (byte) 0x5F, (byte) 0x93, (byte) 0x0A, (byte) 0xEF,
  60. (byte) 0x91, (byte) 0x85, (byte) 0x49, (byte) 0xEE,
  61. (byte) 0x2D, (byte) 0x4F, (byte) 0x8F, (byte) 0x3B,
  62. (byte) 0x47, (byte) 0x87, (byte) 0x6D, (byte) 0x46,
  63. (byte) 0xD6, (byte) 0x3E, (byte) 0x69, (byte) 0x64,
  64. (byte) 0x2A, (byte) 0xCE, (byte) 0xCB, (byte) 0x2F,
  65. (byte) 0xFC, (byte) 0x97, (byte) 0x05, (byte) 0x7A,
  66. (byte) 0xAC, (byte) 0x7F, (byte) 0xD5, (byte) 0x1A,
  67. (byte) 0x4B, (byte) 0x0E, (byte) 0xA7, (byte) 0x5A,
  68. (byte) 0x28, (byte) 0x14, (byte) 0x3F, (byte) 0x29,
  69. (byte) 0x88, (byte) 0x3C, (byte) 0x4C, (byte) 0x02,
  70. (byte) 0xB8, (byte) 0xDA, (byte) 0xB0, (byte) 0x17,
  71. (byte) 0x55, (byte) 0x1F, (byte) 0x8A, (byte) 0x7D,
  72. (byte) 0x57, (byte) 0xC7, (byte) 0x8D, (byte) 0x74,
  73. (byte) 0xB7, (byte) 0xC4, (byte) 0x9F, (byte) 0x72,
  74. (byte) 0x7E, (byte) 0x15, (byte) 0x22, (byte) 0x12,
  75. (byte) 0x58, (byte) 0x07, (byte) 0x99, (byte) 0x34,
  76. (byte) 0x6E, (byte) 0x50, (byte) 0xDE, (byte) 0x68,
  77. (byte) 0x65, (byte) 0xBC, (byte) 0xDB, (byte) 0xF8,
  78. (byte) 0xC8, (byte) 0xA8, (byte) 0x2B, (byte) 0x40,
  79. (byte) 0xDC, (byte) 0xFE, (byte) 0x32, (byte) 0xA4,
  80. (byte) 0xCA, (byte) 0x10, (byte) 0x21, (byte) 0xF0,
  81. (byte) 0xD3, (byte) 0x5D, (byte) 0x0F, (byte) 0x00,
  82. (byte) 0x6F, (byte) 0x9D, (byte) 0x36, (byte) 0x42,
  83. (byte) 0x4A, (byte) 0x5E, (byte) 0xC1, (byte) 0xE0 },
  84. { // p1
  85. (byte) 0x75, (byte) 0xF3, (byte) 0xC6, (byte) 0xF4,
  86. (byte) 0xDB, (byte) 0x7B, (byte) 0xFB, (byte) 0xC8,
  87. (byte) 0x4A, (byte) 0xD3, (byte) 0xE6, (byte) 0x6B,
  88. (byte) 0x45, (byte) 0x7D, (byte) 0xE8, (byte) 0x4B,
  89. (byte) 0xD6, (byte) 0x32, (byte) 0xD8, (byte) 0xFD,
  90. (byte) 0x37, (byte) 0x71, (byte) 0xF1, (byte) 0xE1,
  91. (byte) 0x30, (byte) 0x0F, (byte) 0xF8, (byte) 0x1B,
  92. (byte) 0x87, (byte) 0xFA, (byte) 0x06, (byte) 0x3F,
  93. (byte) 0x5E, (byte) 0xBA, (byte) 0xAE, (byte) 0x5B,
  94. (byte) 0x8A, (byte) 0x00, (byte) 0xBC, (byte) 0x9D,
  95. (byte) 0x6D, (byte) 0xC1, (byte) 0xB1, (byte) 0x0E,
  96. (byte) 0x80, (byte) 0x5D, (byte) 0xD2, (byte) 0xD5,
  97. (byte) 0xA0, (byte) 0x84, (byte) 0x07, (byte) 0x14,
  98. (byte) 0xB5, (byte) 0x90, (byte) 0x2C, (byte) 0xA3,
  99. (byte) 0xB2, (byte) 0x73, (byte) 0x4C, (byte) 0x54,
  100. (byte) 0x92, (byte) 0x74, (byte) 0x36, (byte) 0x51,
  101. (byte) 0x38, (byte) 0xB0, (byte) 0xBD, (byte) 0x5A,
  102. (byte) 0xFC, (byte) 0x60, (byte) 0x62, (byte) 0x96,
  103. (byte) 0x6C, (byte) 0x42, (byte) 0xF7, (byte) 0x10,
  104. (byte) 0x7C, (byte) 0x28, (byte) 0x27, (byte) 0x8C,
  105. (byte) 0x13, (byte) 0x95, (byte) 0x9C, (byte) 0xC7,
  106. (byte) 0x24, (byte) 0x46, (byte) 0x3B, (byte) 0x70,
  107. (byte) 0xCA, (byte) 0xE3, (byte) 0x85, (byte) 0xCB,
  108. (byte) 0x11, (byte) 0xD0, (byte) 0x93, (byte) 0xB8,
  109. (byte) 0xA6, (byte) 0x83, (byte) 0x20, (byte) 0xFF,
  110. (byte) 0x9F, (byte) 0x77, (byte) 0xC3, (byte) 0xCC,
  111. (byte) 0x03, (byte) 0x6F, (byte) 0x08, (byte) 0xBF,
  112. (byte) 0x40, (byte) 0xE7, (byte) 0x2B, (byte) 0xE2,
  113. (byte) 0x79, (byte) 0x0C, (byte) 0xAA, (byte) 0x82,
  114. (byte) 0x41, (byte) 0x3A, (byte) 0xEA, (byte) 0xB9,
  115. (byte) 0xE4, (byte) 0x9A, (byte) 0xA4, (byte) 0x97,
  116. (byte) 0x7E, (byte) 0xDA, (byte) 0x7A, (byte) 0x17,
  117. (byte) 0x66, (byte) 0x94, (byte) 0xA1, (byte) 0x1D,
  118. (byte) 0x3D, (byte) 0xF0, (byte) 0xDE, (byte) 0xB3,
  119. (byte) 0x0B, (byte) 0x72, (byte) 0xA7, (byte) 0x1C,
  120. (byte) 0xEF, (byte) 0xD1, (byte) 0x53, (byte) 0x3E,
  121. (byte) 0x8F, (byte) 0x33, (byte) 0x26, (byte) 0x5F,
  122. (byte) 0xEC, (byte) 0x76, (byte) 0x2A, (byte) 0x49,
  123. (byte) 0x81, (byte) 0x88, (byte) 0xEE, (byte) 0x21,
  124. (byte) 0xC4, (byte) 0x1A, (byte) 0xEB, (byte) 0xD9,
  125. (byte) 0xC5, (byte) 0x39, (byte) 0x99, (byte) 0xCD,
  126. (byte) 0xAD, (byte) 0x31, (byte) 0x8B, (byte) 0x01,
  127. (byte) 0x18, (byte) 0x23, (byte) 0xDD, (byte) 0x1F,
  128. (byte) 0x4E, (byte) 0x2D, (byte) 0xF9, (byte) 0x48,
  129. (byte) 0x4F, (byte) 0xF2, (byte) 0x65, (byte) 0x8E,
  130. (byte) 0x78, (byte) 0x5C, (byte) 0x58, (byte) 0x19,
  131. (byte) 0x8D, (byte) 0xE5, (byte) 0x98, (byte) 0x57,
  132. (byte) 0x67, (byte) 0x7F, (byte) 0x05, (byte) 0x64,
  133. (byte) 0xAF, (byte) 0x63, (byte) 0xB6, (byte) 0xFE,
  134. (byte) 0xF5, (byte) 0xB7, (byte) 0x3C, (byte) 0xA5,
  135. (byte) 0xCE, (byte) 0xE9, (byte) 0x68, (byte) 0x44,
  136. (byte) 0xE0, (byte) 0x4D, (byte) 0x43, (byte) 0x69,
  137. (byte) 0x29, (byte) 0x2E, (byte) 0xAC, (byte) 0x15,
  138. (byte) 0x59, (byte) 0xA8, (byte) 0x0A, (byte) 0x9E,
  139. (byte) 0x6E, (byte) 0x47, (byte) 0xDF, (byte) 0x34,
  140. (byte) 0x35, (byte) 0x6A, (byte) 0xCF, (byte) 0xDC,
  141. (byte) 0x22, (byte) 0xC9, (byte) 0xC0, (byte) 0x9B,
  142. (byte) 0x89, (byte) 0xD4, (byte) 0xED, (byte) 0xAB,
  143. (byte) 0x12, (byte) 0xA2, (byte) 0x0D, (byte) 0x52,
  144. (byte) 0xBB, (byte) 0x02, (byte) 0x2F, (byte) 0xA9,
  145. (byte) 0xD7, (byte) 0x61, (byte) 0x1E, (byte) 0xB4,
  146. (byte) 0x50, (byte) 0x04, (byte) 0xF6, (byte) 0xC2,
  147. (byte) 0x16, (byte) 0x25, (byte) 0x86, (byte) 0x56,
  148. (byte) 0x55, (byte) 0x09, (byte) 0xBE, (byte) 0x91 }
  149. };
  150. /**
  151. * Define the fixed p0/p1 permutations used in keyed S-box lookup.
  152. * By changing the following constant definitions, the S-boxes will
  153. * automatically Get changed in the Twofish engine.
  154. */
  155. private const int P_00 = 1;
  156. private const int P_01 = 0;
  157. private const int P_02 = 0;
  158. private const int P_03 = P_01 ^ 1;
  159. private const int P_04 = 1;
  160. private const int P_10 = 0;
  161. private const int P_11 = 0;
  162. private const int P_12 = 1;
  163. private const int P_13 = P_11 ^ 1;
  164. private const int P_14 = 0;
  165. private const int P_20 = 1;
  166. private const int P_21 = 1;
  167. private const int P_22 = 0;
  168. private const int P_23 = P_21 ^ 1;
  169. private const int P_24 = 0;
  170. private const int P_30 = 0;
  171. private const int P_31 = 1;
  172. private const int P_32 = 1;
  173. private const int P_33 = P_31 ^ 1;
  174. private const int P_34 = 1;
  175. /* Primitive polynomial for GF(256) */
  176. private const int GF256_FDBK = 0x169;
  177. private const int GF256_FDBK_2 = GF256_FDBK / 2;
  178. private const int GF256_FDBK_4 = GF256_FDBK / 4;
  179. private const int RS_GF_FDBK = 0x14D; // field generator
  180. //====================================
  181. // Useful constants
  182. //====================================
  183. private const int ROUNDS = 16;
  184. private const int MAX_ROUNDS = 16; // bytes = 128 bits
  185. private const int BLOCK_SIZE = 16; // bytes = 128 bits
  186. private const int MAX_KEY_BITS = 256;
  187. private const int INPUT_WHITEN=0;
  188. private const int OUTPUT_WHITEN=INPUT_WHITEN+BLOCK_SIZE/4; // 4
  189. private const int ROUND_SUBKEYS=OUTPUT_WHITEN+BLOCK_SIZE/4;// 8
  190. private const int TOTAL_SUBKEYS=ROUND_SUBKEYS+2*MAX_ROUNDS;// 40
  191. private const int SK_STEP = 0x02020202;
  192. private const int SK_BUMP = 0x01010101;
  193. private const int SK_ROTL = 9;
  194. private bool encrypting;
  195. private int[] gMDS0 = new int[MAX_KEY_BITS];
  196. private int[] gMDS1 = new int[MAX_KEY_BITS];
  197. private int[] gMDS2 = new int[MAX_KEY_BITS];
  198. private int[] gMDS3 = new int[MAX_KEY_BITS];
  199. /**
  200. * gSubKeys[] and gSBox[] are eventually used in the
  201. * encryption and decryption methods.
  202. */
  203. private int[] gSubKeys;
  204. private int[] gSBox;
  205. private int k64Cnt;
  206. private byte[] workingKey;
  207. public TwofishEngine()
  208. {
  209. // calculate the MDS matrix
  210. int[] m1 = new int[2];
  211. int[] mX = new int[2];
  212. int[] mY = new int[2];
  213. int j;
  214. for (int i=0; i< MAX_KEY_BITS ; i++)
  215. {
  216. j = P[0,i] & 0xff;
  217. m1[0] = j;
  218. mX[0] = Mx_X(j) & 0xff;
  219. mY[0] = Mx_Y(j) & 0xff;
  220. j = P[1,i] & 0xff;
  221. m1[1] = j;
  222. mX[1] = Mx_X(j) & 0xff;
  223. mY[1] = Mx_Y(j) & 0xff;
  224. gMDS0[i] = m1[P_00] | mX[P_00] << 8 |
  225. mY[P_00] << 16 | mY[P_00] << 24;
  226. gMDS1[i] = mY[P_10] | mY[P_10] << 8 |
  227. mX[P_10] << 16 | m1[P_10] << 24;
  228. gMDS2[i] = mX[P_20] | mY[P_20] << 8 |
  229. m1[P_20] << 16 | mY[P_20] << 24;
  230. gMDS3[i] = mX[P_30] | m1[P_30] << 8 |
  231. mY[P_30] << 16 | mX[P_30] << 24;
  232. }
  233. }
  234. /**
  235. * initialise a Twofish cipher.
  236. *
  237. * @param forEncryption whether or not we are for encryption.
  238. * @param parameters the parameters required to set up the cipher.
  239. * @exception ArgumentException if the parameters argument is
  240. * inappropriate.
  241. */
  242. public void Init(
  243. bool forEncryption,
  244. ICipherParameters parameters)
  245. {
  246. if (!(parameters is KeyParameter))
  247. throw new ArgumentException("invalid parameter passed to Twofish init - " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  248. this.encrypting = forEncryption;
  249. this.workingKey = ((KeyParameter)parameters).GetKey();
  250. this.k64Cnt = (this.workingKey.Length / 8); // pre-padded ?
  251. SetKey(this.workingKey);
  252. }
  253. public string AlgorithmName
  254. {
  255. get { return "Twofish"; }
  256. }
  257. public bool IsPartialBlockOkay
  258. {
  259. get { return false; }
  260. }
  261. public int ProcessBlock(
  262. byte[] input,
  263. int inOff,
  264. byte[] output,
  265. int outOff)
  266. {
  267. if (workingKey == null)
  268. throw new InvalidOperationException("Twofish not initialised");
  269. Check.DataLength(input, inOff, BLOCK_SIZE, "input buffer too short");
  270. Check.OutputLength(output, outOff, BLOCK_SIZE, "output buffer too short");
  271. if (encrypting)
  272. {
  273. EncryptBlock(input, inOff, output, outOff);
  274. }
  275. else
  276. {
  277. DecryptBlock(input, inOff, output, outOff);
  278. }
  279. return BLOCK_SIZE;
  280. }
  281. public void Reset()
  282. {
  283. if (this.workingKey != null)
  284. {
  285. SetKey(this.workingKey);
  286. }
  287. }
  288. public int GetBlockSize()
  289. {
  290. return BLOCK_SIZE;
  291. }
  292. //==================================
  293. // Private Implementation
  294. //==================================
  295. private void SetKey(byte[] key)
  296. {
  297. int[] k32e = new int[MAX_KEY_BITS/64]; // 4
  298. int[] k32o = new int[MAX_KEY_BITS/64]; // 4
  299. int[] sBoxKeys = new int[MAX_KEY_BITS/64]; // 4
  300. gSubKeys = new int[TOTAL_SUBKEYS];
  301. if (k64Cnt < 1)
  302. {
  303. throw new ArgumentException("Key size less than 64 bits");
  304. }
  305. if (k64Cnt > 4)
  306. {
  307. throw new ArgumentException("Key size larger than 256 bits");
  308. }
  309. /*
  310. * k64Cnt is the number of 8 byte blocks (64 chunks)
  311. * that are in the input key. The input key is a
  312. * maximum of 32 bytes ( 256 bits ), so the range
  313. * for k64Cnt is 1..4
  314. */
  315. for (int i=0,p=0; i<k64Cnt ; i++)
  316. {
  317. p = i* 8;
  318. k32e[i] = BytesTo32Bits(key, p);
  319. k32o[i] = BytesTo32Bits(key, p+4);
  320. sBoxKeys[k64Cnt-1-i] = RS_MDS_Encode(k32e[i], k32o[i]);
  321. }
  322. int q,A,B;
  323. for (int i=0; i < TOTAL_SUBKEYS / 2 ; i++)
  324. {
  325. q = i*SK_STEP;
  326. A = F32(q, k32e);
  327. B = F32(q+SK_BUMP, k32o);
  328. B = B << 8 | (int)((uint)B >> 24);
  329. A += B;
  330. gSubKeys[i*2] = A;
  331. A += B;
  332. gSubKeys[i*2 + 1] = A << SK_ROTL | (int)((uint)A >> (32-SK_ROTL));
  333. }
  334. /*
  335. * fully expand the table for speed
  336. */
  337. int k0 = sBoxKeys[0];
  338. int k1 = sBoxKeys[1];
  339. int k2 = sBoxKeys[2];
  340. int k3 = sBoxKeys[3];
  341. int b0, b1, b2, b3;
  342. gSBox = new int[4*MAX_KEY_BITS];
  343. for (int i=0; i<MAX_KEY_BITS; i++)
  344. {
  345. b0 = b1 = b2 = b3 = i;
  346. switch (k64Cnt & 3)
  347. {
  348. case 1:
  349. gSBox[i*2] = gMDS0[(P[P_01,b0] & 0xff) ^ M_b0(k0)];
  350. gSBox[i*2+1] = gMDS1[(P[P_11,b1] & 0xff) ^ M_b1(k0)];
  351. gSBox[i*2+0x200] = gMDS2[(P[P_21,b2] & 0xff) ^ M_b2(k0)];
  352. gSBox[i*2+0x201] = gMDS3[(P[P_31,b3] & 0xff) ^ M_b3(k0)];
  353. break;
  354. case 0: // 256 bits of key
  355. b0 = (P[P_04,b0] & 0xff) ^ M_b0(k3);
  356. b1 = (P[P_14,b1] & 0xff) ^ M_b1(k3);
  357. b2 = (P[P_24,b2] & 0xff) ^ M_b2(k3);
  358. b3 = (P[P_34,b3] & 0xff) ^ M_b3(k3);
  359. // fall through, having pre-processed b[0]..b[3] with k32[3]
  360. goto case 3;
  361. case 3: // 192 bits of key
  362. b0 = (P[P_03,b0] & 0xff) ^ M_b0(k2);
  363. b1 = (P[P_13,b1] & 0xff) ^ M_b1(k2);
  364. b2 = (P[P_23,b2] & 0xff) ^ M_b2(k2);
  365. b3 = (P[P_33,b3] & 0xff) ^ M_b3(k2);
  366. // fall through, having pre-processed b[0]..b[3] with k32[2]
  367. goto case 2;
  368. case 2: // 128 bits of key
  369. gSBox[i * 2] = gMDS0[(P[P_01, (P[P_02, b0] & 0xff) ^ M_b0(k1)] & 0xff) ^ M_b0(k0)];
  370. gSBox[i*2+1] = gMDS1[(P[P_11,(P[P_12,b1] & 0xff) ^ M_b1(k1)] & 0xff) ^ M_b1(k0)];
  371. gSBox[i*2+0x200] = gMDS2[(P[P_21,(P[P_22,b2] & 0xff) ^ M_b2(k1)] & 0xff) ^ M_b2(k0)];
  372. gSBox[i * 2 + 0x201] = gMDS3[(P[P_31, (P[P_32, b3] & 0xff) ^ M_b3(k1)] & 0xff) ^ M_b3(k0)];
  373. break;
  374. }
  375. }
  376. /*
  377. * the function exits having setup the gSBox with the
  378. * input key material.
  379. */
  380. }
  381. /**
  382. * Encrypt the given input starting at the given offset and place
  383. * the result in the provided buffer starting at the given offset.
  384. * The input will be an exact multiple of our blocksize.
  385. *
  386. * encryptBlock uses the pre-calculated gSBox[] and subKey[]
  387. * arrays.
  388. */
  389. private void EncryptBlock(
  390. byte[] src,
  391. int srcIndex,
  392. byte[] dst,
  393. int dstIndex)
  394. {
  395. int x0 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[INPUT_WHITEN];
  396. int x1 = BytesTo32Bits(src, srcIndex + 4) ^ gSubKeys[INPUT_WHITEN + 1];
  397. int x2 = BytesTo32Bits(src, srcIndex + 8) ^ gSubKeys[INPUT_WHITEN + 2];
  398. int x3 = BytesTo32Bits(src, srcIndex + 12) ^ gSubKeys[INPUT_WHITEN + 3];
  399. int k = ROUND_SUBKEYS;
  400. int t0, t1;
  401. for (int r = 0; r < ROUNDS; r +=2)
  402. {
  403. t0 = Fe32_0(x0);
  404. t1 = Fe32_3(x1);
  405. x2 ^= t0 + t1 + gSubKeys[k++];
  406. x2 = (int)((uint)x2 >>1) | x2 << 31;
  407. x3 = (x3 << 1 | (int) ((uint)x3 >> 31)) ^ (t0 + 2*t1 + gSubKeys[k++]);
  408. t0 = Fe32_0(x2);
  409. t1 = Fe32_3(x3);
  410. x0 ^= t0 + t1 + gSubKeys[k++];
  411. x0 = (int) ((uint)x0 >>1) | x0 << 31;
  412. x1 = (x1 << 1 | (int)((uint)x1 >> 31)) ^ (t0 + 2*t1 + gSubKeys[k++]);
  413. }
  414. Bits32ToBytes(x2 ^ gSubKeys[OUTPUT_WHITEN], dst, dstIndex);
  415. Bits32ToBytes(x3 ^ gSubKeys[OUTPUT_WHITEN + 1], dst, dstIndex + 4);
  416. Bits32ToBytes(x0 ^ gSubKeys[OUTPUT_WHITEN + 2], dst, dstIndex + 8);
  417. Bits32ToBytes(x1 ^ gSubKeys[OUTPUT_WHITEN + 3], dst, dstIndex + 12);
  418. }
  419. /**
  420. * Decrypt the given input starting at the given offset and place
  421. * the result in the provided buffer starting at the given offset.
  422. * The input will be an exact multiple of our blocksize.
  423. */
  424. private void DecryptBlock(
  425. byte[] src,
  426. int srcIndex,
  427. byte[] dst,
  428. int dstIndex)
  429. {
  430. int x2 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[OUTPUT_WHITEN];
  431. int x3 = BytesTo32Bits(src, srcIndex+4) ^ gSubKeys[OUTPUT_WHITEN + 1];
  432. int x0 = BytesTo32Bits(src, srcIndex+8) ^ gSubKeys[OUTPUT_WHITEN + 2];
  433. int x1 = BytesTo32Bits(src, srcIndex+12) ^ gSubKeys[OUTPUT_WHITEN + 3];
  434. int k = ROUND_SUBKEYS + 2 * ROUNDS -1 ;
  435. int t0, t1;
  436. for (int r = 0; r< ROUNDS ; r +=2)
  437. {
  438. t0 = Fe32_0(x2);
  439. t1 = Fe32_3(x3);
  440. x1 ^= t0 + 2*t1 + gSubKeys[k--];
  441. x0 = (x0 << 1 | (int)((uint) x0 >> 31)) ^ (t0 + t1 + gSubKeys[k--]);
  442. x1 = (int) ((uint)x1 >>1) | x1 << 31;
  443. t0 = Fe32_0(x0);
  444. t1 = Fe32_3(x1);
  445. x3 ^= t0 + 2*t1 + gSubKeys[k--];
  446. x2 = (x2 << 1 | (int)((uint)x2 >> 31)) ^ (t0 + t1 + gSubKeys[k--]);
  447. x3 = (int)((uint)x3 >>1) | x3 << 31;
  448. }
  449. Bits32ToBytes(x0 ^ gSubKeys[INPUT_WHITEN], dst, dstIndex);
  450. Bits32ToBytes(x1 ^ gSubKeys[INPUT_WHITEN + 1], dst, dstIndex + 4);
  451. Bits32ToBytes(x2 ^ gSubKeys[INPUT_WHITEN + 2], dst, dstIndex + 8);
  452. Bits32ToBytes(x3 ^ gSubKeys[INPUT_WHITEN + 3], dst, dstIndex + 12);
  453. }
  454. /*
  455. * TODO: This can be optimised and made cleaner by combining
  456. * the functionality in this function and applying it appropriately
  457. * to the creation of the subkeys during key setup.
  458. */
  459. private int F32(int x, int[] k32)
  460. {
  461. int b0 = M_b0(x);
  462. int b1 = M_b1(x);
  463. int b2 = M_b2(x);
  464. int b3 = M_b3(x);
  465. int k0 = k32[0];
  466. int k1 = k32[1];
  467. int k2 = k32[2];
  468. int k3 = k32[3];
  469. int result = 0;
  470. switch (k64Cnt & 3)
  471. {
  472. case 1:
  473. result = gMDS0[(P[P_01,b0] & 0xff) ^ M_b0(k0)] ^
  474. gMDS1[(P[P_11,b1] & 0xff) ^ M_b1(k0)] ^
  475. gMDS2[(P[P_21,b2] & 0xff) ^ M_b2(k0)] ^
  476. gMDS3[(P[P_31,b3] & 0xff) ^ M_b3(k0)];
  477. break;
  478. case 0: /* 256 bits of key */
  479. b0 = (P[P_04,b0] & 0xff) ^ M_b0(k3);
  480. b1 = (P[P_14,b1] & 0xff) ^ M_b1(k3);
  481. b2 = (P[P_24,b2] & 0xff) ^ M_b2(k3);
  482. b3 = (P[P_34,b3] & 0xff) ^ M_b3(k3);
  483. goto case 3;
  484. case 3:
  485. b0 = (P[P_03,b0] & 0xff) ^ M_b0(k2);
  486. b1 = (P[P_13,b1] & 0xff) ^ M_b1(k2);
  487. b2 = (P[P_23,b2] & 0xff) ^ M_b2(k2);
  488. b3 = (P[P_33,b3] & 0xff) ^ M_b3(k2);
  489. goto case 2;
  490. case 2:
  491. result =
  492. gMDS0[(P[P_01,(P[P_02,b0]&0xff)^M_b0(k1)]&0xff)^M_b0(k0)] ^
  493. gMDS1[(P[P_11,(P[P_12,b1]&0xff)^M_b1(k1)]&0xff)^M_b1(k0)] ^
  494. gMDS2[(P[P_21,(P[P_22,b2]&0xff)^M_b2(k1)]&0xff)^M_b2(k0)] ^
  495. gMDS3[(P[P_31,(P[P_32,b3]&0xff)^M_b3(k1)]&0xff)^M_b3(k0)];
  496. break;
  497. }
  498. return result;
  499. }
  500. /**
  501. * Use (12, 8) Reed-Solomon code over GF(256) to produce
  502. * a key S-box 32-bit entity from 2 key material 32-bit
  503. * entities.
  504. *
  505. * @param k0 first 32-bit entity
  506. * @param k1 second 32-bit entity
  507. * @return Remainder polynomial Generated using RS code
  508. */
  509. private int RS_MDS_Encode(int k0, int k1)
  510. {
  511. int r = k1;
  512. for (int i = 0 ; i < 4 ; i++) // shift 1 byte at a time
  513. {
  514. r = RS_rem(r);
  515. }
  516. r ^= k0;
  517. for (int i=0 ; i < 4 ; i++)
  518. {
  519. r = RS_rem(r);
  520. }
  521. return r;
  522. }
  523. /**
  524. * Reed-Solomon code parameters: (12,8) reversible code:
  525. * <p>
  526. * <pre>
  527. * G(x) = x^4 + (a+1/a)x^3 + ax^2 + (a+1/a)x + 1
  528. * </pre>
  529. * where a = primitive root of field generator 0x14D
  530. * </p>
  531. */
  532. private int RS_rem(int x)
  533. {
  534. int b = (int) (((uint)x >> 24) & 0xff);
  535. int g2 = ((b << 1) ^
  536. ((b & 0x80) != 0 ? RS_GF_FDBK : 0)) & 0xff;
  537. int g3 = ( (int)((uint)b >> 1) ^
  538. ((b & 0x01) != 0 ? (int)((uint)RS_GF_FDBK >> 1) : 0)) ^ g2 ;
  539. return ((x << 8) ^ (g3 << 24) ^ (g2 << 16) ^ (g3 << 8) ^ b);
  540. }
  541. private int LFSR1(int x)
  542. {
  543. return (x >> 1) ^
  544. (((x & 0x01) != 0) ? GF256_FDBK_2 : 0);
  545. }
  546. private int LFSR2(int x)
  547. {
  548. return (x >> 2) ^
  549. (((x & 0x02) != 0) ? GF256_FDBK_2 : 0) ^
  550. (((x & 0x01) != 0) ? GF256_FDBK_4 : 0);
  551. }
  552. private int Mx_X(int x)
  553. {
  554. return x ^ LFSR2(x);
  555. } // 5B
  556. private int Mx_Y(int x)
  557. {
  558. return x ^ LFSR1(x) ^ LFSR2(x);
  559. } // EF
  560. private int M_b0(int x)
  561. {
  562. return x & 0xff;
  563. }
  564. private int M_b1(int x)
  565. {
  566. return (int)((uint)x >> 8) & 0xff;
  567. }
  568. private int M_b2(int x)
  569. {
  570. return (int)((uint)x >> 16) & 0xff;
  571. }
  572. private int M_b3(int x)
  573. {
  574. return (int)((uint)x >> 24) & 0xff;
  575. }
  576. private int Fe32_0(int x)
  577. {
  578. return gSBox[ 0x000 + 2*(x & 0xff) ] ^
  579. gSBox[ 0x001 + 2*((int)((uint)x >> 8) & 0xff) ] ^
  580. gSBox[ 0x200 + 2*((int)((uint)x >> 16) & 0xff) ] ^
  581. gSBox[ 0x201 + 2*((int)((uint)x >> 24) & 0xff) ];
  582. }
  583. private int Fe32_3(int x)
  584. {
  585. return gSBox[ 0x000 + 2*((int)((uint)x >> 24) & 0xff) ] ^
  586. gSBox[ 0x001 + 2*(x & 0xff) ] ^
  587. gSBox[ 0x200 + 2*((int)((uint)x >> 8) & 0xff) ] ^
  588. gSBox[ 0x201 + 2*((int)((uint)x >> 16) & 0xff) ];
  589. }
  590. private int BytesTo32Bits(byte[] b, int p)
  591. {
  592. return ((b[p] & 0xff) ) |
  593. ((b[p+1] & 0xff) << 8) |
  594. ((b[p+2] & 0xff) << 16) |
  595. ((b[p+3] & 0xff) << 24);
  596. }
  597. private void Bits32ToBytes(int inData, byte[] b, int offset)
  598. {
  599. b[offset] = (byte)inData;
  600. b[offset + 1] = (byte)(inData >> 8);
  601. b[offset + 2] = (byte)(inData >> 16);
  602. b[offset + 3] = (byte)(inData >> 24);
  603. }
  604. }
  605. }
  606. #pragma warning restore
  607. #endif