TwofishEngine.cs 26 KB

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