Khazad.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* Khazad.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. * Khazad is a 64-bit (legacy-level) block cipher that accepts a 128-bit key.
  42. * The cipher is a uniform substitution-permutation network whose inverse only
  43. * differs from the forward operation in the key schedule. The overall cipher
  44. * design follows the Wide Trail strategy, favours component reuse, and permits
  45. * a wide variety of implementation trade-offs.
  46. * <p>
  47. * References:
  48. * <ol>
  49. * <li><a
  50. * href="http://planeta.terra.com.br/informatica/paulobarreto/KhazadPage.html">The
  51. * Khazad Block Cipher</a>.<br>
  52. * <a href="mailto:paulo.barreto@terra.com.br">Paulo S.L.M. Barreto</a> and <a
  53. * href="mailto:vincent.rijmen@esat.kuleuven.ac.be">Vincent Rijmen</a>.</li>
  54. * </ol>
  55. */
  56. public final class Khazad
  57. extends BaseCipher
  58. {
  59. private static final Logger log = Configuration.DEBUG ?
  60. Logger.getLogger(Khazad.class.getName()) : null;
  61. private static final int DEFAULT_BLOCK_SIZE = 8; // in bytes
  62. private static final int DEFAULT_KEY_SIZE = 16; // in bytes
  63. private static final int R = 8; // standard number of rounds; para. 3.7
  64. private static final String Sd = // p. 20 [KHAZAD]
  65. "\uBA54\u2F74\u53D3\uD24D\u50AC\u8DBF\u7052\u9A4C"
  66. + "\uEAD5\u97D1\u3351\u5BA6\uDE48\uA899\uDB32\uB7FC"
  67. + "\uE39E\u919B\uE2BB\u416E\uA5CB\u6B95\uA1F3\uB102"
  68. + "\uCCC4\u1D14\uC363\uDA5D\u5FDC\u7DCD\u7F5A\u6C5C"
  69. + "\uF726\uFFED\uE89D\u6F8E\u19A0\uF089\u0F07\uAFFB"
  70. + "\u0815\u0D04\u0164\uDF76\u79DD\u3D16\u3F37\u6D38"
  71. + "\uB973\uE935\u5571\u7B8C\u7288\uF62A\u3E5E\u2746"
  72. + "\u0C65\u6861\u03C1\u57D6\uD958\uD866\uD73A\uC83C"
  73. + "\uFA96\uA798\uECB8\uC7AE\u694B\uABA9\u670A\u47F2"
  74. + "\uB522\uE5EE\uBE2B\u8112\u831B\u0E23\uF545\u21CE"
  75. + "\u492C\uF9E6\uB628\u1782\u1A8B\uFE8A\u09C9\u874E"
  76. + "\uE12E\uE4E0\uEB90\uA41E\u8560\u0025\uF4F1\u940B"
  77. + "\uE775\uEF34\u31D4\uD086\u7EAD\uFD29\u303B\u9FF8"
  78. + "\uC613\u0605\uC511\u777C\u7A78\u361C\u3959\u1856"
  79. + "\uB3B0\u2420\uB292\uA3C0\u4462\u10B4\u8443\u93C2"
  80. + "\u4ABD\u8F2D\uBC9C\u6A40\uCFA2\u804F\u1FCA\uAA42";
  81. private static final byte[] S = new byte[256];
  82. private static final int[] T0 = new int[256];
  83. private static final int[] T1 = new int[256];
  84. private static final int[] T2 = new int[256];
  85. private static final int[] T3 = new int[256];
  86. private static final int[] T4 = new int[256];
  87. private static final int[] T5 = new int[256];
  88. private static final int[] T6 = new int[256];
  89. private static final int[] T7 = new int[256];
  90. private static final int[][] rc = new int[R + 1][2]; // round constants
  91. /**
  92. * KAT vector (from ecb_vk): I=120 KEY=00000000000000000000000000000100
  93. * CT=A0C86A1BBE2CBF4C
  94. */
  95. private static final byte[] KAT_KEY =
  96. Util.toBytesFromString("00000000000000000000000000000100");
  97. private static final byte[] KAT_CT = Util.toBytesFromString("A0C86A1BBE2CBF4C");
  98. /** caches the result of the correctness test, once executed. */
  99. private static Boolean valid;
  100. static
  101. {
  102. long time = System.currentTimeMillis();
  103. long ROOT = 0x11d; // para. 2.1 [KHAZAD]
  104. int i, j;
  105. int s, s2, s3, s4, s5, s6, s7, s8, sb;
  106. char c;
  107. for (i = 0; i < 256; i++)
  108. {
  109. c = Sd.charAt(i >>> 1);
  110. s = ((i & 1) == 0 ? c >>> 8 : c) & 0xFF;
  111. S[i] = (byte) s;
  112. s2 = s << 1;
  113. if (s2 > 0xFF)
  114. s2 ^= ROOT;
  115. s3 = s2 ^ s;
  116. s4 = s2 << 1;
  117. if (s4 > 0xFF)
  118. s4 ^= ROOT;
  119. s5 = s4 ^ s;
  120. s6 = s4 ^ s2;
  121. s7 = s6 ^ s;
  122. s8 = s4 << 1;
  123. if (s8 > 0xFF)
  124. s8 ^= ROOT;
  125. sb = s8 ^ s2 ^ s;
  126. T0[i] = s << 24 | s3 << 16 | s4 << 8 | s5;
  127. T1[i] = s3 << 24 | s << 16 | s5 << 8 | s4;
  128. T2[i] = s4 << 24 | s5 << 16 | s << 8 | s3;
  129. T3[i] = s5 << 24 | s4 << 16 | s3 << 8 | s;
  130. T4[i] = s6 << 24 | s8 << 16 | sb << 8 | s7;
  131. T5[i] = s8 << 24 | s6 << 16 | s7 << 8 | sb;
  132. T6[i] = sb << 24 | s7 << 16 | s6 << 8 | s8;
  133. T7[i] = s7 << 24 | sb << 16 | s8 << 8 | s6;
  134. }
  135. for (i = 0, j = 0; i < R + 1; i++) // compute round constant
  136. {
  137. rc[i][0] = S[j++] << 24
  138. | (S[j++] & 0xFF) << 16
  139. | (S[j++] & 0xFF) << 8
  140. | (S[j++] & 0xFF);
  141. rc[i][1] = S[j++] << 24
  142. | (S[j++] & 0xFF) << 16
  143. | (S[j++] & 0xFF) << 8
  144. | (S[j++] & 0xFF);
  145. }
  146. time = System.currentTimeMillis() - time;
  147. if (Configuration.DEBUG)
  148. {
  149. log.fine("Static data");
  150. log.fine("T0[]:");
  151. StringBuilder b;
  152. for (i = 0; i < 64; i++)
  153. {
  154. b = new StringBuilder();
  155. for (j = 0; j < 4; j++)
  156. b.append("0x").append(Util.toString(T0[i * 4 + j])).append(", ");
  157. log.fine(b.toString());
  158. }
  159. log.fine("T1[]:");
  160. for (i = 0; i < 64; i++)
  161. {
  162. b = new StringBuilder();
  163. for (j = 0; j < 4; j++)
  164. b.append("0x").append(Util.toString(T1[i * 4 + j])).append(", ");
  165. log.fine(b.toString());
  166. }
  167. log.fine("T2[]:");
  168. for (i = 0; i < 64; i++)
  169. {
  170. b = new StringBuilder();
  171. for (j = 0; j < 4; j++)
  172. b.append("0x").append(Util.toString(T2[i * 4 + j])).append(", ");
  173. log.fine(b.toString());
  174. }
  175. log.fine("T3[]:");
  176. for (i = 0; i < 64; i++)
  177. {
  178. b = new StringBuilder();
  179. for (j = 0; j < 4; j++)
  180. b.append("0x").append(Util.toString(T3[i * 4 + j])).append(", ");
  181. log.fine(b.toString());
  182. }
  183. log.fine("T4[]:");
  184. for (i = 0; i < 64; i++)
  185. {
  186. b = new StringBuilder();
  187. for (j = 0; j < 4; j++)
  188. b.append("0x").append(Util.toString(T4[i * 4 + j])).append(", ");
  189. log.fine(b.toString());
  190. }
  191. log.fine("T5[]:");
  192. for (i = 0; i < 64; i++)
  193. {
  194. b = new StringBuilder();
  195. for (j = 0; j < 4; j++)
  196. b.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
  197. log.fine(b.toString());
  198. }
  199. log.fine("T6[]:");
  200. for (i = 0; i < 64; i++)
  201. {
  202. b = new StringBuilder();
  203. for (j = 0; j < 4; j++)
  204. b.append("0x").append(Util.toString(T6[i * 4 + j])).append(", ");
  205. log.fine(b.toString());
  206. }
  207. log.fine("T7[]:");
  208. for (i = 0; i < 64; i++)
  209. {
  210. b = new StringBuilder();
  211. for (j = 0; j < 4; j++)
  212. b.append("0x").append(Util.toString(T7[i * 4 + j])).append(", ");
  213. log.fine(b.toString());
  214. }
  215. log.fine("rc[]:");
  216. for (i = 0; i < R + 1; i++)
  217. log.fine("0x" + Util.toString(rc[i][0]) + Util.toString(rc[i][1]));
  218. log.fine("Total initialization time: " + time + " ms.");
  219. }
  220. }
  221. /** Trivial 0-arguments constructor. */
  222. public Khazad()
  223. {
  224. super(Registry.KHAZAD_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE);
  225. }
  226. private static void khazad(byte[] in, int i, byte[] out, int j, int[][] K)
  227. {
  228. // sigma(K[0])
  229. int k0 = K[0][0];
  230. int k1 = K[0][1];
  231. int a0 = (in[i++] << 24
  232. | (in[i++] & 0xFF) << 16
  233. | (in[i++] & 0xFF) << 8
  234. | (in[i++] & 0xFF) ) ^ k0;
  235. int a1 = (in[i++] << 24
  236. | (in[i++] & 0xFF) << 16
  237. | (in[i++] & 0xFF) << 8
  238. | (in[i ] & 0xFF) ) ^ k1;
  239. int b0, b1;
  240. // round function
  241. for (int r = 1; r < R; r++)
  242. {
  243. k0 = K[r][0];
  244. k1 = K[r][1];
  245. b0 = T0[ a0 >>> 24 ]
  246. ^ T1[(a0 >>> 16) & 0xFF]
  247. ^ T2[(a0 >>> 8) & 0xFF]
  248. ^ T3[ a0 & 0xFF]
  249. ^ T4[ a1 >>> 24 ]
  250. ^ T5[(a1 >>> 16) & 0xFF]
  251. ^ T6[(a1 >>> 8) & 0xFF]
  252. ^ T7[ a1 & 0xFF] ^ k0;
  253. b1 = T0[ a1 >>> 24 ]
  254. ^ T1[(a1 >>> 16) & 0xFF]
  255. ^ T2[(a1 >>> 8) & 0xFF]
  256. ^ T3[ a1 & 0xFF]
  257. ^ T4[ a0 >>> 24 ]
  258. ^ T5[(a0 >>> 16) & 0xFF]
  259. ^ T6[(a0 >>> 8) & 0xFF]
  260. ^ T7[ a0 & 0xFF] ^ k1;
  261. a0 = b0;
  262. a1 = b1;
  263. if (Configuration.DEBUG)
  264. log.fine("T" + r + "=" + Util.toString(a0) + Util.toString(a1));
  265. }
  266. // sigma(K[R]) o gamma applied to previous output
  267. k0 = K[R][0];
  268. k1 = K[R][1];
  269. out[j++] = (byte)(S[ a0 >>> 24 ] ^ (k0 >>> 24));
  270. out[j++] = (byte)(S[(a0 >>> 16) & 0xFF] ^ (k0 >>> 16));
  271. out[j++] = (byte)(S[(a0 >>> 8) & 0xFF] ^ (k0 >>> 8));
  272. out[j++] = (byte)(S[ a0 & 0xFF] ^ k0 );
  273. out[j++] = (byte)(S[ a1 >>> 24 ] ^ (k1 >>> 24));
  274. out[j++] = (byte)(S[(a1 >>> 16) & 0xFF] ^ (k1 >>> 16));
  275. out[j++] = (byte)(S[(a1 >>> 8) & 0xFF] ^ (k1 >>> 8));
  276. out[j ] = (byte)(S[ a1 & 0xFF] ^ k1 );
  277. if (Configuration.DEBUG)
  278. log.fine("T=" + Util.toString(out, j - 7, 8) + "\n");
  279. }
  280. public Object clone()
  281. {
  282. Khazad result = new Khazad();
  283. result.currentBlockSize = this.currentBlockSize;
  284. return result;
  285. }
  286. public Iterator blockSizes()
  287. {
  288. ArrayList al = new ArrayList();
  289. al.add(Integer.valueOf(DEFAULT_BLOCK_SIZE));
  290. return Collections.unmodifiableList(al).iterator();
  291. }
  292. public Iterator keySizes()
  293. {
  294. ArrayList al = new ArrayList();
  295. al.add(Integer.valueOf(DEFAULT_KEY_SIZE));
  296. return Collections.unmodifiableList(al).iterator();
  297. }
  298. /**
  299. * Expands a user-supplied key material into a session key for a designated
  300. * <i>block size</i>.
  301. *
  302. * @param uk the 128-bit user-supplied key material.
  303. * @param bs the desired block size in bytes.
  304. * @return an Object encapsulating the session key.
  305. * @exception IllegalArgumentException if the block size is not 16 (128-bit).
  306. * @exception InvalidKeyException if the key data is invalid.
  307. */
  308. public Object makeKey(byte[] uk, int bs) throws InvalidKeyException
  309. {
  310. if (bs != DEFAULT_BLOCK_SIZE)
  311. throw new IllegalArgumentException();
  312. if (uk == null)
  313. throw new InvalidKeyException("Empty key");
  314. if (uk.length != 16)
  315. throw new InvalidKeyException("Key is not 128-bit.");
  316. int[][] Ke = new int[R + 1][2]; // encryption round keys
  317. int[][] Kd = new int[R + 1][2]; // decryption round keys
  318. int r, i;
  319. int k20, k21, k10, k11, rc0, rc1, kr0, kr1;
  320. i = 0;
  321. k20 = uk[i++] << 24
  322. | (uk[i++] & 0xFF) << 16
  323. | (uk[i++] & 0xFF) << 8
  324. | (uk[i++] & 0xFF);
  325. k21 = uk[i++] << 24
  326. | (uk[i++] & 0xFF) << 16
  327. | (uk[i++] & 0xFF) << 8
  328. | (uk[i++] & 0xFF);
  329. k10 = uk[i++] << 24
  330. | (uk[i++] & 0xFF) << 16
  331. | (uk[i++] & 0xFF) << 8
  332. | (uk[i++] & 0xFF);
  333. k11 = uk[i++] << 24
  334. | (uk[i++] & 0xFF) << 16
  335. | (uk[i++] & 0xFF) << 8
  336. | (uk[i++] & 0xFF);
  337. for (r = 0, i = 0; r <= R; r++)
  338. {
  339. rc0 = rc[r][0];
  340. rc1 = rc[r][1];
  341. kr0 = T0[ k10 >>> 24 ]
  342. ^ T1[(k10 >>> 16) & 0xFF]
  343. ^ T2[(k10 >>> 8) & 0xFF]
  344. ^ T3[ k10 & 0xFF]
  345. ^ T4[(k11 >>> 24) & 0xFF]
  346. ^ T5[(k11 >>> 16) & 0xFF]
  347. ^ T6[(k11 >>> 8) & 0xFF]
  348. ^ T7[ k11 & 0xFF] ^ rc0 ^ k20;
  349. kr1 = T0[ k11 >>> 24 ]
  350. ^ T1[(k11 >>> 16) & 0xFF]
  351. ^ T2[(k11 >>> 8) & 0xFF]
  352. ^ T3[ k11 & 0xFF]
  353. ^ T4[(k10 >>> 24) & 0xFF]
  354. ^ T5[(k10 >>> 16) & 0xFF]
  355. ^ T6[(k10 >>> 8) & 0xFF]
  356. ^ T7[ k10 & 0xFF] ^ rc1 ^ k21;
  357. Ke[r][0] = kr0;
  358. Ke[r][1] = kr1;
  359. k20 = k10;
  360. k21 = k11;
  361. k10 = kr0;
  362. k11 = kr1;
  363. if (r == 0 || r == R)
  364. {
  365. Kd[R - r][0] = kr0;
  366. Kd[R - r][1] = kr1;
  367. }
  368. else
  369. {
  370. Kd[R - r][0] = T0[S[ kr0 >>> 24 ] & 0xFF]
  371. ^ T1[S[(kr0 >>> 16) & 0xFF] & 0xFF]
  372. ^ T2[S[(kr0 >>> 8) & 0xFF] & 0xFF]
  373. ^ T3[S[ kr0 & 0xFF] & 0xFF]
  374. ^ T4[S[ kr1 >>> 24 ] & 0xFF]
  375. ^ T5[S[(kr1 >>> 16) & 0xFF] & 0xFF]
  376. ^ T6[S[(kr1 >>> 8) & 0xFF] & 0xFF]
  377. ^ T7[S[ kr1 & 0xFF] & 0xFF];
  378. Kd[R - r][1] = T0[S[ kr1 >>> 24 ] & 0xFF]
  379. ^ T1[S[(kr1 >>> 16) & 0xFF] & 0xFF]
  380. ^ T2[S[(kr1 >>> 8) & 0xFF] & 0xFF]
  381. ^ T3[S[ kr1 & 0xFF] & 0xFF]
  382. ^ T4[S[ kr0 >>> 24 ] & 0xFF]
  383. ^ T5[S[(kr0 >>> 16) & 0xFF] & 0xFF]
  384. ^ T6[S[(kr0 >>> 8) & 0xFF] & 0xFF]
  385. ^ T7[S[ kr0 & 0xFF] & 0xFF];
  386. }
  387. }
  388. if (Configuration.DEBUG)
  389. {
  390. log.fine("Key schedule");
  391. log.fine("Ke[]:");
  392. for (r = 0; r < R + 1; r++)
  393. log.fine("#" + r + ": 0x" + Util.toString(Ke[r][0])
  394. + Util.toString(Ke[r][1]));
  395. log.fine("Kd[]:");
  396. for (r = 0; r < R + 1; r++)
  397. log.fine("#" + r + ": 0x" + Util.toString(Kd[r][0])
  398. + Util.toString(Kd[r][1]));
  399. }
  400. return new Object[] { Ke, Kd };
  401. }
  402. public void encrypt(byte[] in, int i, byte[] out, int j, Object k, int bs)
  403. {
  404. if (bs != DEFAULT_BLOCK_SIZE)
  405. throw new IllegalArgumentException();
  406. int[][] K = (int[][])((Object[]) k)[0];
  407. khazad(in, i, out, j, K);
  408. }
  409. public void decrypt(byte[] in, int i, byte[] out, int j, Object k, int bs)
  410. {
  411. if (bs != DEFAULT_BLOCK_SIZE)
  412. throw new IllegalArgumentException();
  413. int[][] K = (int[][])((Object[]) k)[1];
  414. khazad(in, i, out, j, K);
  415. }
  416. public boolean selfTest()
  417. {
  418. if (valid == null)
  419. {
  420. boolean result = super.selfTest(); // do symmetry tests
  421. if (result)
  422. result = testKat(KAT_KEY, KAT_CT);
  423. valid = Boolean.valueOf(result);
  424. }
  425. return valid.booleanValue();
  426. }
  427. }