Twofish.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /* Twofish.java --
  2. Copyright (C) 2001, 2002, 2003, 2006, 2010 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.cipher;
  32. import gnu.java.security.Configuration;
  33. import gnu.java.security.Registry;
  34. import gnu.java.security.util.Util;
  35. import java.security.InvalidKeyException;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.Iterator;
  39. import java.util.logging.Logger;
  40. /**
  41. * Twofish is a balanced 128-bit Feistel cipher, consisting of 16 rounds. In
  42. * each round, a 64-bit S-box value is computed from 64 bits of the block, and
  43. * this value is xored into the other half of the block. The two half-blocks are
  44. * then exchanged, and the next round begins. Before the first round, all input
  45. * bits are xored with key-dependent "whitening" subkeys, and after the final
  46. * round the output bits are xored with other key-dependent whitening subkeys;
  47. * these subkeys are not used anywhere else in the algorithm.
  48. * <p>
  49. * Twofish is designed by Bruce Schneier, Doug Whiting, John Kelsey, Chris
  50. * Hall, David Wagner and Niels Ferguson.
  51. * <p>
  52. * References:
  53. * <ol>
  54. * <li><a href="http://www.counterpane.com/twofish-paper.html">Twofish: A
  55. * 128-bit Block Cipher</a>.</li>
  56. * </ol>
  57. */
  58. public final class Twofish
  59. extends BaseCipher
  60. {
  61. private static final Logger log = Configuration.DEBUG ?
  62. Logger.getLogger(Twofish.class.getName()) : null;
  63. private static final int DEFAULT_BLOCK_SIZE = 16; // in bytes
  64. private static final int DEFAULT_KEY_SIZE = 16; // in bytes
  65. private static final int MAX_ROUNDS = 16; // max # rounds (for allocating subkeys)
  66. private static final int ROUNDS = MAX_ROUNDS;
  67. // subkey array indices
  68. private static final int INPUT_WHITEN = 0;
  69. private static final int OUTPUT_WHITEN = INPUT_WHITEN + DEFAULT_BLOCK_SIZE / 4;
  70. private static final int ROUND_SUBKEYS = OUTPUT_WHITEN + DEFAULT_BLOCK_SIZE / 4;
  71. private static final int SK_STEP = 0x02020202;
  72. private static final int SK_BUMP = 0x01010101;
  73. private static final int SK_ROTL = 9;
  74. private static final String[] Pm = new String[] {
  75. // p0
  76. "\uA967\uB3E8\u04FD\uA376\u9A92\u8078\uE4DD\uD138"
  77. + "\u0DC6\u3598\u18F7\uEC6C\u4375\u3726\uFA13\u9448"
  78. + "\uF2D0\u8B30\u8454\uDF23\u195B\u3D59\uF3AE\uA282"
  79. + "\u6301\u832E\uD951\u9B7C\uA6EB\uA5BE\u160C\uE361"
  80. + "\uC08C\u3AF5\u732C\u250B\uBB4E\u896B\u536A\uB4F1"
  81. + "\uE1E6\uBD45\uE2F4\uB666\uCC95\u0356\uD41C\u1ED7"
  82. + "\uFBC3\u8EB5\uE9CF\uBFBA\uEA77\u39AF\u33C9\u6271"
  83. + "\u8179\u09AD\u24CD\uF9D8\uE5C5\uB94D\u4408\u86E7"
  84. + "\uA11D\uAAED\u0670\uB2D2\u417B\uA011\u31C2\u2790"
  85. + "\u20F6\u60FF\u965C\uB1AB\u9E9C\u521B\u5F93\u0AEF"
  86. + "\u9185\u49EE\u2D4F\u8F3B\u4787\u6D46\uD63E\u6964"
  87. + "\u2ACE\uCB2F\uFC97\u057A\uAC7F\uD51A\u4B0E\uA75A"
  88. + "\u2814\u3F29\u883C\u4C02\uB8DA\uB017\u551F\u8A7D"
  89. + "\u57C7\u8D74\uB7C4\u9F72\u7E15\u2212\u5807\u9934"
  90. + "\u6E50\uDE68\u65BC\uDBF8\uC8A8\u2B40\uDCFE\u32A4"
  91. + "\uCA10\u21F0\uD35D\u0F00\u6F9D\u3642\u4A5E\uC1E0",
  92. // p1
  93. "\u75F3\uC6F4\uDB7B\uFBC8\u4AD3\uE66B\u457D\uE84B"
  94. + "\uD632\uD8FD\u3771\uF1E1\u300F\uF81B\u87FA\u063F"
  95. + "\u5EBA\uAE5B\u8A00\uBC9D\u6DC1\uB10E\u805D\uD2D5"
  96. + "\uA084\u0714\uB590\u2CA3\uB273\u4C54\u9274\u3651"
  97. + "\u38B0\uBD5A\uFC60\u6296\u6C42\uF710\u7C28\u278C"
  98. + "\u1395\u9CC7\u2446\u3B70\uCAE3\u85CB\u11D0\u93B8"
  99. + "\uA683\u20FF\u9F77\uC3CC\u036F\u08BF\u40E7\u2BE2"
  100. + "\u790C\uAA82\u413A\uEAB9\uE49A\uA497\u7EDA\u7A17"
  101. + "\u6694\uA11D\u3DF0\uDEB3\u0B72\uA71C\uEFD1\u533E"
  102. + "\u8F33\u265F\uEC76\u2A49\u8188\uEE21\uC41A\uEBD9"
  103. + "\uC539\u99CD\uAD31\u8B01\u1823\uDD1F\u4E2D\uF948"
  104. + "\u4FF2\u658E\u785C\u5819\u8DE5\u9857\u677F\u0564"
  105. + "\uAF63\uB6FE\uF5B7\u3CA5\uCEE9\u6844\uE04D\u4369"
  106. + "\u292E\uAC15\u59A8\u0A9E\u6E47\uDF34\u356A\uCFDC"
  107. + "\u22C9\uC09B\u89D4\uEDAB\u12A2\u0D52\uBB02\u2FA9"
  108. + "\uD761\u1EB4\u5004\uF6C2\u1625\u8656\u5509\uBE91" };
  109. /** Fixed 8x8 permutation S-boxes */
  110. private static final byte[][] P = new byte[2][256]; // blank final
  111. /**
  112. * Define the fixed p0/p1 permutations used in keyed S-box lookup. By
  113. * changing the following constant definitions, the S-boxes will
  114. * automatically get changed in the Twofish engine.
  115. */
  116. private static final int P_00 = 1;
  117. private static final int P_01 = 0;
  118. private static final int P_02 = 0;
  119. private static final int P_03 = P_01 ^ 1;
  120. private static final int P_04 = 1;
  121. private static final int P_10 = 0;
  122. private static final int P_11 = 0;
  123. private static final int P_12 = 1;
  124. private static final int P_13 = P_11 ^ 1;
  125. private static final int P_14 = 0;
  126. private static final int P_20 = 1;
  127. private static final int P_21 = 1;
  128. private static final int P_22 = 0;
  129. private static final int P_23 = P_21 ^ 1;
  130. private static final int P_24 = 0;
  131. private static final int P_30 = 0;
  132. private static final int P_31 = 1;
  133. private static final int P_32 = 1;
  134. private static final int P_33 = P_31 ^ 1;
  135. private static final int P_34 = 1;
  136. /** Primitive polynomial for GF(256) */
  137. private static final int GF256_FDBK_2 = 0x169 / 2;
  138. private static final int GF256_FDBK_4 = 0x169 / 4;
  139. /** MDS matrix */
  140. private static final int[][] MDS = new int[4][256]; // blank final
  141. private static final int RS_GF_FDBK = 0x14D; // field generator
  142. /**
  143. * KAT vector (from ecb_vk):
  144. * I=183
  145. * KEY=0000000000000000000000000000000000000000000002000000000000000000
  146. * CT=F51410475B33FBD3DB2117B5C17C82D4
  147. */
  148. private static final byte[] KAT_KEY = Util.toBytesFromString(
  149. "0000000000000000000000000000000000000000000002000000000000000000");
  150. private static final byte[] KAT_CT =
  151. Util.toBytesFromString("F51410475B33FBD3DB2117B5C17C82D4");
  152. /** caches the result of the correctness test, once executed. */
  153. private static Boolean valid;
  154. static
  155. {
  156. long time = System.currentTimeMillis();
  157. // expand the P arrays
  158. int i;
  159. char c;
  160. for (i = 0; i < 256; i++)
  161. {
  162. c = Pm[0].charAt(i >>> 1);
  163. P[0][i] = (byte)((i & 1) == 0 ? c >>> 8 : c);
  164. c = Pm[1].charAt(i >>> 1);
  165. P[1][i] = (byte)((i & 1) == 0 ? c >>> 8 : c);
  166. }
  167. // precompute the MDS matrix
  168. int[] m1 = new int[2];
  169. int[] mX = new int[2];
  170. int[] mY = new int[2];
  171. int j;
  172. for (i = 0; i < 256; i++)
  173. {
  174. j = P[0][i] & 0xFF; // compute all the matrix elements
  175. m1[0] = j;
  176. mX[0] = Mx_X(j) & 0xFF;
  177. mY[0] = Mx_Y(j) & 0xFF;
  178. j = P[1][i] & 0xFF;
  179. m1[1] = j;
  180. mX[1] = Mx_X(j) & 0xFF;
  181. mY[1] = Mx_Y(j) & 0xFF;
  182. MDS[0][i] = m1[P_00] << 0
  183. | mX[P_00] << 8
  184. | mY[P_00] << 16
  185. | mY[P_00] << 24;
  186. MDS[1][i] = mY[P_10] << 0
  187. | mY[P_10] << 8
  188. | mX[P_10] << 16
  189. | m1[P_10] << 24;
  190. MDS[2][i] = mX[P_20] << 0
  191. | mY[P_20] << 8
  192. | m1[P_20] << 16
  193. | mY[P_20] << 24;
  194. MDS[3][i] = mX[P_30] << 0
  195. | m1[P_30] << 8
  196. | mY[P_30] << 16
  197. | mX[P_30] << 24;
  198. }
  199. time = System.currentTimeMillis() - time;
  200. if (Configuration.DEBUG)
  201. {
  202. log.fine("Static Data");
  203. log.fine("MDS[0][]:");
  204. StringBuilder sb;
  205. for (i = 0; i < 64; i++)
  206. {
  207. sb = new StringBuilder();
  208. for (j = 0; j < 4; j++)
  209. sb.append("0x").append(Util.toString(MDS[0][i * 4 + j])).append(", ");
  210. log.fine(sb.toString());
  211. }
  212. log.fine("MDS[1][]:");
  213. for (i = 0; i < 64; i++)
  214. {
  215. sb = new StringBuilder();
  216. for (j = 0; j < 4; j++)
  217. sb.append("0x").append(Util.toString(MDS[1][i * 4 + j])).append(", ");
  218. log.fine(sb.toString());
  219. }
  220. log.fine("MDS[2][]:");
  221. for (i = 0; i < 64; i++)
  222. {
  223. sb = new StringBuilder();
  224. for (j = 0; j < 4; j++)
  225. sb.append("0x").append(Util.toString(MDS[2][i * 4 + j])).append(", ");
  226. log.fine(sb.toString());
  227. }
  228. log.fine("MDS[3][]:");
  229. for (i = 0; i < 64; i++)
  230. {
  231. sb = new StringBuilder();
  232. for (j = 0; j < 4; j++)
  233. sb.append("0x").append(Util.toString(MDS[3][i * 4 + j])).append(", ");
  234. log.fine(sb.toString());
  235. }
  236. log.fine("Total initialization time: " + time + " ms.");
  237. }
  238. }
  239. private static final int LFSR1(int x)
  240. {
  241. return (x >> 1) ^ ((x & 0x01) != 0 ? GF256_FDBK_2 : 0);
  242. }
  243. private static final int LFSR2(int x)
  244. {
  245. return (x >> 2)
  246. ^ ((x & 0x02) != 0 ? GF256_FDBK_2 : 0)
  247. ^ ((x & 0x01) != 0 ? GF256_FDBK_4 : 0);
  248. }
  249. private static final int Mx_X(int x)
  250. { // 5B
  251. return x ^ LFSR2(x);
  252. }
  253. private static final int Mx_Y(int x)
  254. { // EF
  255. return x ^ LFSR1(x) ^ LFSR2(x);
  256. }
  257. /** Trivial 0-arguments constructor. */
  258. public Twofish()
  259. {
  260. super(Registry.TWOFISH_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE);
  261. }
  262. private static final int b0(int x)
  263. {
  264. return x & 0xFF;
  265. }
  266. private static final int b1(int x)
  267. {
  268. return (x >>> 8) & 0xFF;
  269. }
  270. private static final int b2(int x)
  271. {
  272. return (x >>> 16) & 0xFF;
  273. }
  274. private static final int b3(int x)
  275. {
  276. return (x >>> 24) & 0xFF;
  277. }
  278. /**
  279. * Use (12, 8) Reed-Solomon code over GF(256) to produce a key S-box 32-bit
  280. * entity from two key material 32-bit entities.
  281. *
  282. * @param k0 1st 32-bit entity.
  283. * @param k1 2nd 32-bit entity.
  284. * @return remainder polynomial generated using RS code
  285. */
  286. private static final int RS_MDS_Encode(int k0, int k1)
  287. {
  288. int r = k1;
  289. int i;
  290. for (i = 0; i < 4; i++) // shift 1 byte at a time
  291. r = RS_rem(r);
  292. r ^= k0;
  293. for (i = 0; i < 4; i++)
  294. r = RS_rem(r);
  295. return r;
  296. }
  297. /**
  298. * Reed-Solomon code parameters: (12, 8) reversible code:<p>
  299. * <pre>
  300. * g(x) = x**4 + (a + 1/a) x**3 + a x**2 + (a + 1/a) x + 1
  301. * </pre>
  302. * where a = primitive root of field generator 0x14D
  303. */
  304. private static final int RS_rem(int x)
  305. {
  306. int b = (x >>> 24) & 0xFF;
  307. int g2 = ((b << 1) ^ ((b & 0x80) != 0 ? RS_GF_FDBK : 0)) & 0xFF;
  308. int g3 = (b >>> 1) ^ ((b & 0x01) != 0 ? (RS_GF_FDBK >>> 1) : 0) ^ g2;
  309. int result = (x << 8) ^ (g3 << 24) ^ (g2 << 16) ^ (g3 << 8) ^ b;
  310. return result;
  311. }
  312. private static final int F32(int k64Cnt, int x, int[] k32)
  313. {
  314. int b0 = b0(x);
  315. int b1 = b1(x);
  316. int b2 = b2(x);
  317. int b3 = b3(x);
  318. int k0 = k32[0];
  319. int k1 = k32[1];
  320. int k2 = k32[2];
  321. int k3 = k32[3];
  322. int result = 0;
  323. switch (k64Cnt & 3)
  324. {
  325. case 1:
  326. result = MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)]
  327. ^ MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)]
  328. ^ MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)]
  329. ^ MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)];
  330. break;
  331. case 0: // same as 4
  332. b0 = (P[P_04][b0] & 0xFF) ^ b0(k3);
  333. b1 = (P[P_14][b1] & 0xFF) ^ b1(k3);
  334. b2 = (P[P_24][b2] & 0xFF) ^ b2(k3);
  335. b3 = (P[P_34][b3] & 0xFF) ^ b3(k3);
  336. case 3:
  337. b0 = (P[P_03][b0] & 0xFF) ^ b0(k2);
  338. b1 = (P[P_13][b1] & 0xFF) ^ b1(k2);
  339. b2 = (P[P_23][b2] & 0xFF) ^ b2(k2);
  340. b3 = (P[P_33][b3] & 0xFF) ^ b3(k2);
  341. case 2: // 128-bit keys (optimize for this case)
  342. result = MDS[0][(P[P_01][(P[P_02][b0] & 0xFF) ^ b0(k1)] & 0xFF) ^ b0(k0)]
  343. ^ MDS[1][(P[P_11][(P[P_12][b1] & 0xFF) ^ b1(k1)] & 0xFF) ^ b1(k0)]
  344. ^ MDS[2][(P[P_21][(P[P_22][b2] & 0xFF) ^ b2(k1)] & 0xFF) ^ b2(k0)]
  345. ^ MDS[3][(P[P_31][(P[P_32][b3] & 0xFF) ^ b3(k1)] & 0xFF) ^ b3(k0)];
  346. break;
  347. }
  348. return result;
  349. }
  350. private static final int Fe32(int[] sBox, int x, int R)
  351. {
  352. return sBox[ 2 * _b(x, R ) ]
  353. ^ sBox[ 2 * _b(x, R + 1) + 1]
  354. ^ sBox[0x200 + 2 * _b(x, R + 2) ]
  355. ^ sBox[0x200 + 2 * _b(x, R + 3) + 1];
  356. }
  357. private static final int _b(int x, int N)
  358. {
  359. switch (N % 4)
  360. {
  361. case 0:
  362. return x & 0xFF;
  363. case 1:
  364. return (x >>> 8) & 0xFF;
  365. case 2:
  366. return (x >>> 16) & 0xFF;
  367. default:
  368. return x >>> 24;
  369. }
  370. }
  371. public Object clone()
  372. {
  373. Twofish result = new Twofish();
  374. result.currentBlockSize = this.currentBlockSize;
  375. return result;
  376. }
  377. public Iterator blockSizes()
  378. {
  379. ArrayList al = new ArrayList();
  380. al.add(Integer.valueOf(DEFAULT_BLOCK_SIZE));
  381. return Collections.unmodifiableList(al).iterator();
  382. }
  383. public Iterator keySizes()
  384. {
  385. ArrayList al = new ArrayList();
  386. al.add(Integer.valueOf(8)); // 64-bit
  387. al.add(Integer.valueOf(16)); // 128-bit
  388. al.add(Integer.valueOf(24)); // 192-bit
  389. al.add(Integer.valueOf(32)); // 256-bit
  390. return Collections.unmodifiableList(al).iterator();
  391. }
  392. /**
  393. * Expands a user-supplied key material into a session key for a designated
  394. * <i>block size</i>.
  395. *
  396. * @param k the 64/128/192/256-bit user-key to use.
  397. * @param bs the desired block size in bytes.
  398. * @return an Object encapsulating the session key.
  399. * @exception IllegalArgumentException if the block size is not 16 (128-bit).
  400. * @exception InvalidKeyException if the key data is invalid.
  401. */
  402. public Object makeKey(byte[] k, int bs) throws InvalidKeyException
  403. {
  404. if (bs != DEFAULT_BLOCK_SIZE)
  405. throw new IllegalArgumentException();
  406. if (k == null)
  407. throw new InvalidKeyException("Empty key");
  408. int length = k.length;
  409. if (! (length == 8 || length == 16 || length == 24 || length == 32))
  410. throw new InvalidKeyException("Incorrect key length");
  411. int k64Cnt = length / 8;
  412. int subkeyCnt = ROUND_SUBKEYS + 2 * ROUNDS;
  413. int[] k32e = new int[4]; // even 32-bit entities
  414. int[] k32o = new int[4]; // odd 32-bit entities
  415. int[] sBoxKey = new int[4];
  416. // split user key material into even and odd 32-bit entities and
  417. // compute S-box keys using (12, 8) Reed-Solomon code over GF(256)
  418. int i, j, offset = 0;
  419. for (i = 0, j = k64Cnt - 1; i < 4 && offset < length; i++, j--)
  420. {
  421. k32e[i] = (k[offset++] & 0xFF)
  422. | (k[offset++] & 0xFF) << 8
  423. | (k[offset++] & 0xFF) << 16
  424. | (k[offset++] & 0xFF) << 24;
  425. k32o[i] = (k[offset++] & 0xFF)
  426. | (k[offset++] & 0xFF) << 8
  427. | (k[offset++] & 0xFF) << 16
  428. | (k[offset++] & 0xFF) << 24;
  429. sBoxKey[j] = RS_MDS_Encode(k32e[i], k32o[i]); // reverse order
  430. }
  431. // compute the round decryption subkeys for PHT. these same subkeys
  432. // will be used in encryption but will be applied in reverse order.
  433. int q, A, B;
  434. int[] subKeys = new int[subkeyCnt];
  435. for (i = q = 0; i < subkeyCnt / 2; i++, q += SK_STEP)
  436. {
  437. A = F32(k64Cnt, q, k32e); // A uses even key entities
  438. B = F32(k64Cnt, q + SK_BUMP, k32o); // B uses odd key entities
  439. B = B << 8 | B >>> 24;
  440. A += B;
  441. subKeys[2 * i] = A; // combine with a PHT
  442. A += B;
  443. subKeys[2 * i + 1] = A << SK_ROTL | A >>> (32 - SK_ROTL);
  444. }
  445. // fully expand the table for speed
  446. int k0 = sBoxKey[0];
  447. int k1 = sBoxKey[1];
  448. int k2 = sBoxKey[2];
  449. int k3 = sBoxKey[3];
  450. int b0, b1, b2, b3;
  451. int[] sBox = new int[4 * 256];
  452. for (i = 0; i < 256; i++)
  453. {
  454. b0 = b1 = b2 = b3 = i;
  455. switch (k64Cnt & 3)
  456. {
  457. case 1:
  458. sBox[ 2 * i ] = MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)];
  459. sBox[ 2 * i + 1] = MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)];
  460. sBox[0x200 + 2 * i ] = MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)];
  461. sBox[0x200 + 2 * i + 1] = MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)];
  462. break;
  463. case 0: // same as 4
  464. b0 = (P[P_04][b0] & 0xFF) ^ b0(k3);
  465. b1 = (P[P_14][b1] & 0xFF) ^ b1(k3);
  466. b2 = (P[P_24][b2] & 0xFF) ^ b2(k3);
  467. b3 = (P[P_34][b3] & 0xFF) ^ b3(k3);
  468. case 3:
  469. b0 = (P[P_03][b0] & 0xFF) ^ b0(k2);
  470. b1 = (P[P_13][b1] & 0xFF) ^ b1(k2);
  471. b2 = (P[P_23][b2] & 0xFF) ^ b2(k2);
  472. b3 = (P[P_33][b3] & 0xFF) ^ b3(k2);
  473. case 2: // 128-bit keys
  474. sBox[ 2 * i ] = MDS[0][(P[P_01][(P[P_02][b0] & 0xFF)
  475. ^ b0(k1)] & 0xFF) ^ b0(k0)];
  476. sBox[ 2 * i + 1] = MDS[1][(P[P_11][(P[P_12][b1] & 0xFF)
  477. ^ b1(k1)] & 0xFF) ^ b1(k0)];
  478. sBox[0x200 + 2 * i ] = MDS[2][(P[P_21][(P[P_22][b2] & 0xFF)
  479. ^ b2(k1)] & 0xFF) ^ b2(k0)];
  480. sBox[0x200 + 2 * i + 1] = MDS[3][(P[P_31][(P[P_32][b3] & 0xFF)
  481. ^ b3(k1)] & 0xFF) ^ b3(k0)];
  482. }
  483. }
  484. if (Configuration.DEBUG)
  485. {
  486. StringBuilder sb;
  487. log.fine("S-box[]:");
  488. for (i = 0; i < 64; i++)
  489. {
  490. sb = new StringBuilder();
  491. for (j = 0; j < 4; j++)
  492. sb.append("0x").append(Util.toString(sBox[i * 4 + j])).append(", ");
  493. log.fine(sb.toString());
  494. }
  495. log.fine("");
  496. for (i = 0; i < 64; i++)
  497. {
  498. sb = new StringBuilder();
  499. for (j = 0; j < 4; j++)
  500. sb.append("0x").append(Util.toString(sBox[256 + i * 4 + j])).append(", ");
  501. log.fine(sb.toString());
  502. }
  503. log.fine("");
  504. for (i = 0; i < 64; i++)
  505. {
  506. sb = new StringBuilder();
  507. for (j = 0; j < 4; j++)
  508. sb.append("0x").append(Util.toString(sBox[512 + i * 4 + j])).append(", ");
  509. log.fine(sb.toString());
  510. }
  511. log.fine("");
  512. for (i = 0; i < 64; i++)
  513. {
  514. sb = new StringBuilder();
  515. for (j = 0; j < 4; j++)
  516. sb.append("0x").append(Util.toString(sBox[768 + i * 4 + j])).append(", ");
  517. log.fine(sb.toString());
  518. }
  519. log.fine("User (odd, even) keys --> S-Box keys:");
  520. for (i = 0; i < k64Cnt; i++)
  521. log.fine("0x" + Util.toString(k32o[i])
  522. + " 0x" + Util.toString(k32e[i])
  523. + " --> 0x" + Util.toString(sBoxKey[k64Cnt - 1 - i]));
  524. log.fine("Round keys:");
  525. for (i = 0; i < ROUND_SUBKEYS + 2 * ROUNDS; i += 2)
  526. log.fine("0x" + Util.toString(subKeys[i])
  527. + " 0x" + Util.toString(subKeys[i + 1]));
  528. }
  529. return new Object[] { sBox, subKeys };
  530. }
  531. public void encrypt(byte[] in, int inOffset, byte[] out, int outOffset,
  532. Object sessionKey, int bs)
  533. {
  534. if (bs != DEFAULT_BLOCK_SIZE)
  535. throw new IllegalArgumentException();
  536. Object[] sk = (Object[]) sessionKey; // extract S-box and session key
  537. int[] sBox = (int[]) sk[0];
  538. int[] sKey = (int[]) sk[1];
  539. if (Configuration.DEBUG)
  540. log.fine("PT=" + Util.toString(in, inOffset, bs));
  541. int x0 = (in[inOffset++] & 0xFF)
  542. | (in[inOffset++] & 0xFF) << 8
  543. | (in[inOffset++] & 0xFF) << 16
  544. | (in[inOffset++] & 0xFF) << 24;
  545. int x1 = (in[inOffset++] & 0xFF)
  546. | (in[inOffset++] & 0xFF) << 8
  547. | (in[inOffset++] & 0xFF) << 16
  548. | (in[inOffset++] & 0xFF) << 24;
  549. int x2 = (in[inOffset++] & 0xFF)
  550. | (in[inOffset++] & 0xFF) << 8
  551. | (in[inOffset++] & 0xFF) << 16
  552. | (in[inOffset++] & 0xFF) << 24;
  553. int x3 = (in[inOffset++] & 0xFF)
  554. | (in[inOffset++] & 0xFF) << 8
  555. | (in[inOffset++] & 0xFF) << 16
  556. | (in[inOffset++] & 0xFF) << 24;
  557. x0 ^= sKey[INPUT_WHITEN];
  558. x1 ^= sKey[INPUT_WHITEN + 1];
  559. x2 ^= sKey[INPUT_WHITEN + 2];
  560. x3 ^= sKey[INPUT_WHITEN + 3];
  561. if (Configuration.DEBUG)
  562. log.fine("PTw=" + Util.toString(x0) + Util.toString(x1)
  563. + Util.toString(x2) + Util.toString(x3));
  564. int t0, t1;
  565. int k = ROUND_SUBKEYS;
  566. for (int R = 0; R < ROUNDS; R += 2)
  567. {
  568. t0 = Fe32(sBox, x0, 0);
  569. t1 = Fe32(sBox, x1, 3);
  570. x2 ^= t0 + t1 + sKey[k++];
  571. x2 = x2 >>> 1 | x2 << 31;
  572. x3 = x3 << 1 | x3 >>> 31;
  573. x3 ^= t0 + 2 * t1 + sKey[k++];
  574. if (Configuration.DEBUG)
  575. log.fine("CT" + (R) + "=" + Util.toString(x0) + Util.toString(x1)
  576. + Util.toString(x2) + Util.toString(x3));
  577. t0 = Fe32(sBox, x2, 0);
  578. t1 = Fe32(sBox, x3, 3);
  579. x0 ^= t0 + t1 + sKey[k++];
  580. x0 = x0 >>> 1 | x0 << 31;
  581. x1 = x1 << 1 | x1 >>> 31;
  582. x1 ^= t0 + 2 * t1 + sKey[k++];
  583. if (Configuration.DEBUG)
  584. log.fine("CT" + (R + 1) + "=" + Util.toString(x0) + Util.toString(x1)
  585. + Util.toString(x2) + Util.toString(x3));
  586. }
  587. x2 ^= sKey[OUTPUT_WHITEN];
  588. x3 ^= sKey[OUTPUT_WHITEN + 1];
  589. x0 ^= sKey[OUTPUT_WHITEN + 2];
  590. x1 ^= sKey[OUTPUT_WHITEN + 3];
  591. if (Configuration.DEBUG)
  592. log.fine("CTw=" + Util.toString(x0) + Util.toString(x1)
  593. + Util.toString(x2) + Util.toString(x3));
  594. out[outOffset++] = (byte) x2;
  595. out[outOffset++] = (byte)(x2 >>> 8);
  596. out[outOffset++] = (byte)(x2 >>> 16);
  597. out[outOffset++] = (byte)(x2 >>> 24);
  598. out[outOffset++] = (byte) x3;
  599. out[outOffset++] = (byte)(x3 >>> 8);
  600. out[outOffset++] = (byte)(x3 >>> 16);
  601. out[outOffset++] = (byte)(x3 >>> 24);
  602. out[outOffset++] = (byte) x0;
  603. out[outOffset++] = (byte)(x0 >>> 8);
  604. out[outOffset++] = (byte)(x0 >>> 16);
  605. out[outOffset++] = (byte)(x0 >>> 24);
  606. out[outOffset++] = (byte) x1;
  607. out[outOffset++] = (byte)(x1 >>> 8);
  608. out[outOffset++] = (byte)(x1 >>> 16);
  609. out[outOffset ] = (byte)(x1 >>> 24);
  610. if (Configuration.DEBUG)
  611. log.fine("CT=" + Util.toString(out, outOffset - 15, 16) + "\n");
  612. }
  613. public void decrypt(byte[] in, int inOffset, byte[] out, int outOffset,
  614. Object sessionKey, int bs)
  615. {
  616. if (bs != DEFAULT_BLOCK_SIZE)
  617. throw new IllegalArgumentException();
  618. Object[] sk = (Object[]) sessionKey; // extract S-box and session key
  619. int[] sBox = (int[]) sk[0];
  620. int[] sKey = (int[]) sk[1];
  621. if (Configuration.DEBUG)
  622. log.fine("CT=" + Util.toString(in, inOffset, bs));
  623. int x2 = (in[inOffset++] & 0xFF)
  624. | (in[inOffset++] & 0xFF) << 8
  625. | (in[inOffset++] & 0xFF) << 16
  626. | (in[inOffset++] & 0xFF) << 24;
  627. int x3 = (in[inOffset++] & 0xFF)
  628. | (in[inOffset++] & 0xFF) << 8
  629. | (in[inOffset++] & 0xFF) << 16
  630. | (in[inOffset++] & 0xFF) << 24;
  631. int x0 = (in[inOffset++] & 0xFF)
  632. | (in[inOffset++] & 0xFF) << 8
  633. | (in[inOffset++] & 0xFF) << 16
  634. | (in[inOffset++] & 0xFF) << 24;
  635. int x1 = (in[inOffset++] & 0xFF)
  636. | (in[inOffset++] & 0xFF) << 8
  637. | (in[inOffset++] & 0xFF) << 16
  638. | (in[inOffset++] & 0xFF) << 24;
  639. x2 ^= sKey[OUTPUT_WHITEN];
  640. x3 ^= sKey[OUTPUT_WHITEN + 1];
  641. x0 ^= sKey[OUTPUT_WHITEN + 2];
  642. x1 ^= sKey[OUTPUT_WHITEN + 3];
  643. if (Configuration.DEBUG)
  644. log.fine("CTw=" + Util.toString(x2) + Util.toString(x3)
  645. + Util.toString(x0) + Util.toString(x1));
  646. int k = ROUND_SUBKEYS + 2 * ROUNDS - 1;
  647. int t0, t1;
  648. for (int R = 0; R < ROUNDS; R += 2)
  649. {
  650. t0 = Fe32(sBox, x2, 0);
  651. t1 = Fe32(sBox, x3, 3);
  652. x1 ^= t0 + 2 * t1 + sKey[k--];
  653. x1 = x1 >>> 1 | x1 << 31;
  654. x0 = x0 << 1 | x0 >>> 31;
  655. x0 ^= t0 + t1 + sKey[k--];
  656. if (Configuration.DEBUG)
  657. log.fine("PT" + (ROUNDS - R) + "=" + Util.toString(x2)
  658. + Util.toString(x3) + Util.toString(x0) + Util.toString(x1));
  659. t0 = Fe32(sBox, x0, 0);
  660. t1 = Fe32(sBox, x1, 3);
  661. x3 ^= t0 + 2 * t1 + sKey[k--];
  662. x3 = x3 >>> 1 | x3 << 31;
  663. x2 = x2 << 1 | x2 >>> 31;
  664. x2 ^= t0 + t1 + sKey[k--];
  665. if (Configuration.DEBUG)
  666. log.fine("PT" + (ROUNDS - R - 1) + "=" + Util.toString(x2)
  667. + Util.toString(x3) + Util.toString(x0) + Util.toString(x1));
  668. }
  669. x0 ^= sKey[INPUT_WHITEN];
  670. x1 ^= sKey[INPUT_WHITEN + 1];
  671. x2 ^= sKey[INPUT_WHITEN + 2];
  672. x3 ^= sKey[INPUT_WHITEN + 3];
  673. if (Configuration.DEBUG)
  674. log.fine("PTw=" + Util.toString(x2) + Util.toString(x3)
  675. + Util.toString(x0) + Util.toString(x1));
  676. out[outOffset++] = (byte) x0;
  677. out[outOffset++] = (byte)(x0 >>> 8);
  678. out[outOffset++] = (byte)(x0 >>> 16);
  679. out[outOffset++] = (byte)(x0 >>> 24);
  680. out[outOffset++] = (byte) x1;
  681. out[outOffset++] = (byte)(x1 >>> 8);
  682. out[outOffset++] = (byte)(x1 >>> 16);
  683. out[outOffset++] = (byte)(x1 >>> 24);
  684. out[outOffset++] = (byte) x2;
  685. out[outOffset++] = (byte)(x2 >>> 8);
  686. out[outOffset++] = (byte)(x2 >>> 16);
  687. out[outOffset++] = (byte)(x2 >>> 24);
  688. out[outOffset++] = (byte) x3;
  689. out[outOffset++] = (byte)(x3 >>> 8);
  690. out[outOffset++] = (byte)(x3 >>> 16);
  691. out[outOffset ] = (byte)(x3 >>> 24);
  692. if (Configuration.DEBUG)
  693. log.fine("PT=" + Util.toString(out, outOffset - 15, 16) + "\n");
  694. }
  695. public boolean selfTest()
  696. {
  697. if (valid == null)
  698. {
  699. boolean result = super.selfTest(); // do symmetry tests
  700. if (result)
  701. result = testKat(KAT_KEY, KAT_CT);
  702. valid = Boolean.valueOf(result);
  703. }
  704. return valid.booleanValue();
  705. }
  706. }