Square.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* Square.java --
  2. Copyright (C) 2001, 2002, 2003, 2006 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.Registry;
  33. import gnu.java.security.util.Util;
  34. import java.security.InvalidKeyException;
  35. import java.util.ArrayList;
  36. import java.util.Collections;
  37. import java.util.Iterator;
  38. /**
  39. * Square is a 128-bit key, 128-bit block cipher algorithm developed by Joan
  40. * Daemen, Lars Knudsen and Vincent Rijmen.
  41. * <p>
  42. * References:
  43. * <ol>
  44. * <li><a href="http://www.esat.kuleuven.ac.be/~rijmen/square/">The block
  45. * cipher Square</a>.<br>
  46. * <a href="mailto:daemen.j@protonworld.com">Joan Daemen</a>, <a
  47. * href="mailto:lars.knudsen@esat.kuleuven.ac.be">Lars Knudsen</a> and <a
  48. * href="mailto:vincent.rijmen@esat.kuleuven.ac.be">Vincent Rijmen</a>.</li>
  49. * </ol>
  50. */
  51. public final class Square
  52. extends BaseCipher
  53. {
  54. private static final int DEFAULT_BLOCK_SIZE = 16; // in bytes
  55. private static final int DEFAULT_KEY_SIZE = 16; // in bytes
  56. private static final int ROUNDS = 8;
  57. private static final int ROOT = 0x1F5; // for generating GF(2**8)
  58. private static final int[] OFFSET = new int[ROUNDS];
  59. private static final String Sdata =
  60. "\uB1CE\uC395\u5AAD\uE702\u4D44\uFB91\u0C87\uA150"
  61. + "\uCB67\u54DD\u468F\uE14E\uF0FD\uFCEB\uF9C4\u1A6E"
  62. + "\u5EF5\uCC8D\u1C56\u43FE\u0761\uF875\u59FF\u0322"
  63. + "\u8AD1\u13EE\u8800\u0E34\u1580\u94E3\uEDB5\u5323"
  64. + "\u4B47\u17A7\u9035\uABD8\uB8DF\u4F57\u9A92\uDB1B"
  65. + "\u3CC8\u9904\u8EE0\uD77D\u85BB\u402C\u3A45\uF142"
  66. + "\u6520\u4118\u7225\u9370\u3605\uF20B\uA379\uEC08"
  67. + "\u2731\u32B6\u7CB0\u0A73\u5B7B\uB781\uD20D\u6A26"
  68. + "\u9E58\u9C83\u74B3\uAC30\u7A69\u770F\uAE21\uDED0"
  69. + "\u2E97\u10A4\u98A8\uD468\u2D62\u296D\u1649\u76C7"
  70. + "\uE8C1\u9637\uE5CA\uF4E9\u6312\uC2A6\u14BC\uD328"
  71. + "\uAF2F\uE624\u52C6\uA009\uBD8C\uCF5D\u115F\u01C5"
  72. + "\u9F3D\uA29B\uC93B\uBE51\u191F\u3F5C\uB2EF\u4ACD"
  73. + "\uBFBA\u6F64\uD9F3\u3EB4\uAADC\uD506\uC07E\uF666"
  74. + "\u6C84\u7138\uB91D\u7F9D\u488B\u2ADA\uA533\u8239"
  75. + "\uD678\u86FA\uE42B\uA91E\u8960\u6BEA\u554C\uF7E2";
  76. /** Substitution boxes for encryption and decryption. */
  77. private static final byte[] Se = new byte[256];
  78. private static final byte[] Sd = new byte[256];
  79. /** Transposition boxes for encryption and decryption. */
  80. private static final int[] Te = new int[256];
  81. private static final int[] Td = new int[256];
  82. /**
  83. * KAT vector (from ecb_vk): I=87 KEY=00000000000000000000020000000000
  84. * CT=A9DF031B4E25E89F527EFFF89CB0BEBA
  85. */
  86. private static final byte[] KAT_KEY =
  87. Util.toBytesFromString("00000000000000000000020000000000");
  88. private static final byte[] KAT_CT =
  89. Util.toBytesFromString("A9DF031B4E25E89F527EFFF89CB0BEBA");
  90. /** caches the result of the correctness test, once executed. */
  91. private static Boolean valid;
  92. static
  93. {
  94. int i, j;
  95. // re-construct Se box values
  96. int limit = Sdata.length();
  97. char c1;
  98. for (i = 0, j = 0; i < limit; i++)
  99. {
  100. c1 = Sdata.charAt(i);
  101. Se[j++] = (byte)(c1 >>> 8);
  102. Se[j++] = (byte) c1;
  103. }
  104. // compute Sd box values
  105. for (i = 0; i < 256; i++)
  106. Sd[Se[i] & 0xFF] = (byte) i;
  107. // generate OFFSET values
  108. OFFSET[0] = 1;
  109. for (i = 1; i < ROUNDS; i++)
  110. {
  111. OFFSET[i] = mul(OFFSET[i - 1], 2);
  112. OFFSET[i - 1] <<= 24;
  113. }
  114. OFFSET[ROUNDS - 1] <<= 24;
  115. // generate Te and Td boxes if we're not reading their values
  116. // Notes:
  117. // (1) The function mul() computes the product of two elements of GF(2**8)
  118. // with ROOT as reduction polynomial.
  119. // (2) the values used in computing the Te and Td are the GF(2**8)
  120. // coefficients of the diffusion polynomial c(x) and its inverse
  121. // (modulo x**4 + 1) d(x), defined in sections 2.1 and 4 of the Square
  122. // paper.
  123. for (i = 0; i < 256; i++)
  124. {
  125. j = Se[i] & 0xFF;
  126. Te[i] = (Se[i & 3] == 0) ? 0
  127. : mul(j, 2) << 24
  128. | j << 16
  129. | j << 8
  130. | mul(j, 3);
  131. j = Sd[i] & 0xFF;
  132. Td[i] = (Sd[i & 3] == 0) ? 0
  133. : mul(j, 14) << 24
  134. | mul(j, 9) << 16
  135. | mul(j, 13) << 8
  136. | mul(j, 11);
  137. }
  138. }
  139. /** Trivial 0-arguments constructor. */
  140. public Square()
  141. {
  142. super(Registry.SQUARE_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE);
  143. }
  144. private static void square(byte[] in, int i, byte[] out, int j, int[][] K,
  145. int[] T, byte[] S)
  146. {
  147. int a = ((in[i++]) << 24
  148. | (in[i++] & 0xFF) << 16
  149. | (in[i++] & 0xFF) << 8
  150. | (in[i++] & 0xFF) ) ^ K[0][0];
  151. int b = ((in[i++]) << 24
  152. | (in[i++] & 0xFF) << 16
  153. | (in[i++] & 0xFF) << 8
  154. | (in[i++] & 0xFF) ) ^ K[0][1];
  155. int c = ((in[i++]) << 24
  156. | (in[i++] & 0xFF) << 16
  157. | (in[i++] & 0xFF) << 8
  158. | (in[i++] & 0xFF) ) ^ K[0][2];
  159. int d = ((in[i++]) << 24
  160. | (in[i++] & 0xFF) << 16
  161. | (in[i++] & 0xFF) << 8
  162. | (in[i ] & 0xFF) ) ^ K[0][3];
  163. int r, aa, bb, cc, dd;
  164. for (r = 1; r < ROUNDS; r++)
  165. { // R - 1 full rounds
  166. aa = T[(a >>> 24) ]
  167. ^ rot32R(T[(b >>> 24) ], 8)
  168. ^ rot32R(T[(c >>> 24) ], 16)
  169. ^ rot32R(T[(d >>> 24) ], 24) ^ K[r][0];
  170. bb = T[(a >>> 16) & 0xFF]
  171. ^ rot32R(T[(b >>> 16) & 0xFF], 8)
  172. ^ rot32R(T[(c >>> 16) & 0xFF], 16)
  173. ^ rot32R(T[(d >>> 16) & 0xFF], 24) ^ K[r][1];
  174. cc = T[(a >>> 8) & 0xFF]
  175. ^ rot32R(T[(b >>> 8) & 0xFF], 8)
  176. ^ rot32R(T[(c >>> 8) & 0xFF], 16)
  177. ^ rot32R(T[(d >>> 8) & 0xFF], 24) ^ K[r][2];
  178. dd = T[ a & 0xFF]
  179. ^ rot32R(T[ b & 0xFF], 8)
  180. ^ rot32R(T[ c & 0xFF], 16)
  181. ^ rot32R(T[ d & 0xFF], 24) ^ K[r][3];
  182. a = aa;
  183. b = bb;
  184. c = cc;
  185. d = dd;
  186. }
  187. // last round (diffusion becomes only transposition)
  188. aa = ((S[(a >>> 24) ] ) << 24
  189. | (S[(b >>> 24) ] & 0xFF) << 16
  190. | (S[(c >>> 24) ] & 0xFF) << 8
  191. | (S[(d >>> 24) ] & 0xFF) ) ^ K[r][0];
  192. bb = ((S[(a >>> 16) & 0xFF] ) << 24
  193. | (S[(b >>> 16) & 0xFF] & 0xFF) << 16
  194. | (S[(c >>> 16) & 0xFF] & 0xFF) << 8
  195. | (S[(d >>> 16) & 0xFF] & 0xFF) ) ^ K[r][1];
  196. cc = ((S[(a >>> 8) & 0xFF] ) << 24
  197. | (S[(b >>> 8) & 0xFF] & 0xFF) << 16
  198. | (S[(c >>> 8) & 0xFF] & 0xFF) << 8
  199. | (S[(d >>> 8) & 0xFF] & 0xFF) ) ^ K[r][2];
  200. dd = ((S[ a & 0xFF] ) << 24
  201. | (S[ b & 0xFF] & 0xFF) << 16
  202. | (S[ c & 0xFF] & 0xFF) << 8
  203. | (S[ d & 0xFF] & 0xFF) ) ^ K[r][3];
  204. out[j++] = (byte)(aa >>> 24);
  205. out[j++] = (byte)(aa >>> 16);
  206. out[j++] = (byte)(aa >>> 8);
  207. out[j++] = (byte) aa;
  208. out[j++] = (byte)(bb >>> 24);
  209. out[j++] = (byte)(bb >>> 16);
  210. out[j++] = (byte)(bb >>> 8);
  211. out[j++] = (byte) bb;
  212. out[j++] = (byte)(cc >>> 24);
  213. out[j++] = (byte)(cc >>> 16);
  214. out[j++] = (byte)(cc >>> 8);
  215. out[j++] = (byte) cc;
  216. out[j++] = (byte)(dd >>> 24);
  217. out[j++] = (byte)(dd >>> 16);
  218. out[j++] = (byte)(dd >>> 8);
  219. out[j ] = (byte) dd;
  220. }
  221. /**
  222. * Applies the Theta function to an input <i>in</i> in order to produce in
  223. * <i>out</i> an internal session sub-key.
  224. * <p>
  225. * Both <i>in</i> and <i>out</i> are arrays of four ints.
  226. * <p>
  227. * Pseudo-code is:
  228. * <pre>
  229. * for (i = 0; i &lt; 4; i++)
  230. * {
  231. * out[i] = 0;
  232. * for (j = 0, n = 24; j &lt; 4; j++, n -= 8)
  233. * {
  234. * k = mul(in[i] &gt;&gt;&gt; 24, G[0][j]) &circ; mul(in[i] &gt;&gt;&gt; 16, G[1][j])
  235. * &circ; mul(in[i] &gt;&gt;&gt; 8, G[2][j]) &circ; mul(in[i], G[3][j]);
  236. * out[i] &circ;= k &lt;&lt; n;
  237. * }
  238. * }
  239. * </pre>
  240. */
  241. private static void transform(int[] in, int[] out)
  242. {
  243. int l3, l2, l1, l0, m;
  244. for (int i = 0; i < 4; i++)
  245. {
  246. l3 = in[i];
  247. l2 = l3 >>> 8;
  248. l1 = l3 >>> 16;
  249. l0 = l3 >>> 24;
  250. m = ((mul(l0, 2) ^ mul(l1, 3) ^ l2 ^ l3) & 0xFF) << 24;
  251. m ^= ((l0 ^ mul(l1, 2) ^ mul(l2, 3) ^ l3) & 0xFF) << 16;
  252. m ^= ((l0 ^ l1 ^ mul(l2, 2) ^ mul(l3, 3)) & 0xFF) << 8;
  253. m ^= ((mul(l0, 3) ^ l1 ^ l2 ^ mul(l3, 2)) & 0xFF);
  254. out[i] = m;
  255. }
  256. }
  257. /**
  258. * Left rotate a 32-bit chunk.
  259. *
  260. * @param x the 32-bit data to rotate
  261. * @param s number of places to left-rotate by
  262. * @return the newly permutated value.
  263. */
  264. private static int rot32L(int x, int s)
  265. {
  266. return x << s | x >>> (32 - s);
  267. }
  268. /**
  269. * Right rotate a 32-bit chunk.
  270. *
  271. * @param x the 32-bit data to rotate
  272. * @param s number of places to right-rotate by
  273. * @return the newly permutated value.
  274. */
  275. private static int rot32R(int x, int s)
  276. {
  277. return x >>> s | x << (32 - s);
  278. }
  279. /**
  280. * Returns the product of two binary numbers a and b, using the generator ROOT
  281. * as the modulus: p = (a * b) mod ROOT. ROOT Generates a suitable Galois
  282. * Field in GF(2**8).
  283. * <p>
  284. * For best performance call it with abs(b) &lt; abs(a).
  285. *
  286. * @param a operand for multiply.
  287. * @param b operand for multiply.
  288. * @return the result of (a * b) % ROOT.
  289. */
  290. private static final int mul(int a, int b)
  291. {
  292. if (a == 0)
  293. return 0;
  294. a &= 0xFF;
  295. b &= 0xFF;
  296. int result = 0;
  297. while (b != 0)
  298. {
  299. if ((b & 0x01) != 0)
  300. result ^= a;
  301. b >>>= 1;
  302. a <<= 1;
  303. if (a > 0xFF)
  304. a ^= ROOT;
  305. }
  306. return result & 0xFF;
  307. }
  308. public Object clone()
  309. {
  310. Square result = new Square();
  311. result.currentBlockSize = this.currentBlockSize;
  312. return result;
  313. }
  314. public Iterator blockSizes()
  315. {
  316. ArrayList al = new ArrayList();
  317. al.add(Integer.valueOf(DEFAULT_BLOCK_SIZE));
  318. return Collections.unmodifiableList(al).iterator();
  319. }
  320. public Iterator keySizes()
  321. {
  322. ArrayList al = new ArrayList();
  323. al.add(Integer.valueOf(DEFAULT_KEY_SIZE));
  324. return Collections.unmodifiableList(al).iterator();
  325. }
  326. public Object makeKey(byte[] uk, int bs) throws InvalidKeyException
  327. {
  328. if (bs != DEFAULT_BLOCK_SIZE)
  329. throw new IllegalArgumentException();
  330. if (uk == null)
  331. throw new InvalidKeyException("Empty key");
  332. if (uk.length != DEFAULT_KEY_SIZE)
  333. throw new InvalidKeyException("Key is not 128-bit.");
  334. int[][] Ke = new int[ROUNDS + 1][4];
  335. int[][] Kd = new int[ROUNDS + 1][4];
  336. int[][] tK = new int[ROUNDS + 1][4];
  337. int i = 0;
  338. Ke[0][0] = (uk[i++] & 0xFF) << 24
  339. | (uk[i++] & 0xFF) << 16
  340. | (uk[i++] & 0xFF) << 8
  341. | (uk[i++] & 0xFF);
  342. tK[0][0] = Ke[0][0];
  343. Ke[0][1] = (uk[i++] & 0xFF) << 24
  344. | (uk[i++] & 0xFF) << 16
  345. | (uk[i++] & 0xFF) << 8
  346. | (uk[i++] & 0xFF);
  347. tK[0][1] = Ke[0][1];
  348. Ke[0][2] = (uk[i++] & 0xFF) << 24
  349. | (uk[i++] & 0xFF) << 16
  350. | (uk[i++] & 0xFF) << 8
  351. | (uk[i++] & 0xFF);
  352. tK[0][2] = Ke[0][2];
  353. Ke[0][3] = (uk[i++] & 0xFF) << 24
  354. | (uk[i++] & 0xFF) << 16
  355. | (uk[i++] & 0xFF) << 8
  356. | (uk[i ] & 0xFF);
  357. tK[0][3] = Ke[0][3];
  358. int j;
  359. for (i = 1, j = 0; i < ROUNDS + 1; i++, j++)
  360. {
  361. tK[i][0] = tK[j][0] ^ rot32L(tK[j][3], 8) ^ OFFSET[j];
  362. tK[i][1] = tK[j][1] ^ tK[i][0];
  363. tK[i][2] = tK[j][2] ^ tK[i][1];
  364. tK[i][3] = tK[j][3] ^ tK[i][2];
  365. System.arraycopy(tK[i], 0, Ke[i], 0, 4);
  366. transform(Ke[j], Ke[j]);
  367. }
  368. for (i = 0; i < ROUNDS; i++)
  369. System.arraycopy(tK[ROUNDS - i], 0, Kd[i], 0, 4);
  370. transform(tK[0], Kd[ROUNDS]);
  371. return new Object[] { Ke, Kd };
  372. }
  373. public void encrypt(byte[] in, int i, byte[] out, int j, Object k, int bs)
  374. {
  375. if (bs != DEFAULT_BLOCK_SIZE)
  376. throw new IllegalArgumentException();
  377. int[][] K = (int[][])((Object[]) k)[0];
  378. square(in, i, out, j, K, Te, Se);
  379. }
  380. public void decrypt(byte[] in, int i, byte[] out, int j, Object k, int bs)
  381. {
  382. if (bs != DEFAULT_BLOCK_SIZE)
  383. throw new IllegalArgumentException();
  384. int[][] K = (int[][])((Object[]) k)[1];
  385. square(in, i, out, j, K, Td, Sd);
  386. }
  387. public boolean selfTest()
  388. {
  389. if (valid == null)
  390. {
  391. boolean result = super.selfTest(); // do symmetry tests
  392. if (result)
  393. result = testKat(KAT_KEY, KAT_CT);
  394. valid = Boolean.valueOf(result);
  395. }
  396. return valid.booleanValue();
  397. }
  398. }