TwofishEngine.cs 26 KB

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