Serpent.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /* Serpent.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. * Serpent is a 32-round substitution-permutation network block cipher,
  40. * operating on 128-bit blocks and accepting keys of 128, 192, and 256 bits in
  41. * length. At each round the plaintext is XORed with a 128 bit portion of the
  42. * session key -- a 4224 bit key computed from the input key -- then one of
  43. * eight S-boxes are applied, and finally a simple linear transformation is
  44. * done. Decryption does the exact same thing in reverse order, and using the
  45. * eight inverses of the S-boxes.
  46. * <p>
  47. * Serpent was designed by Ross Anderson, Eli Biham, and Lars Knudsen as a
  48. * proposed cipher for the Advanced Encryption Standard.
  49. * <p>
  50. * Serpent can be sped up greatly by replacing S-box substitution with a
  51. * sequence of binary operations, and the optimal implementation depends upon
  52. * finding the fastest sequence of binary operations that reproduce this
  53. * substitution. This implementation uses the S-boxes discovered by <a
  54. * href="http://www.ii.uib.no/~osvik/">Dag Arne Osvik</a>, which are optimized
  55. * for the Pentium family of processors.
  56. * <p>
  57. * References:
  58. * <ol>
  59. * <li><a href="http://www.cl.cam.ac.uk/~rja14/serpent.html">Serpent: A
  60. * Candidate Block Cipher for the Advanced Encryption Standard.</a></li>
  61. * </ol>
  62. */
  63. public class Serpent
  64. extends BaseCipher
  65. {
  66. private static final int DEFAULT_KEY_SIZE = 16;
  67. private static final int DEFAULT_BLOCK_SIZE = 16;
  68. private static final int ROUNDS = 32;
  69. /** The fractional part of the golden ratio, (sqrt(5)+1)/2. */
  70. private static final int PHI = 0x9e3779b9;
  71. /**
  72. * KAT vector (from ecb_vk): I=9
  73. * KEY=008000000000000000000000000000000000000000000000
  74. * CT=5587B5BCB9EE5A28BA2BACC418005240
  75. */
  76. private static final byte[] KAT_KEY = Util.toReversedBytesFromString(
  77. "008000000000000000000000000000000000000000000000");
  78. private static final byte[] KAT_CT =
  79. Util.toReversedBytesFromString("5587B5BCB9EE5A28BA2BACC418005240");
  80. /** caches the result of the correctness test, once executed. */
  81. private static Boolean valid;
  82. private int x0, x1, x2, x3, x4;
  83. /** Trivial zero-argument constructor. */
  84. public Serpent()
  85. {
  86. super(Registry.SERPENT_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE);
  87. }
  88. public Object clone()
  89. {
  90. Serpent result = new Serpent();
  91. result.currentBlockSize = this.currentBlockSize;
  92. return result;
  93. }
  94. public Iterator blockSizes()
  95. {
  96. return Collections.singleton(Integer.valueOf(DEFAULT_BLOCK_SIZE)).iterator();
  97. }
  98. public Iterator keySizes()
  99. {
  100. ArrayList keySizes = new ArrayList();
  101. keySizes.add(Integer.valueOf(16));
  102. keySizes.add(Integer.valueOf(24));
  103. keySizes.add(Integer.valueOf(32));
  104. return Collections.unmodifiableList(keySizes).iterator();
  105. }
  106. public Object makeKey(byte[] kb, int blockSize) throws InvalidKeyException
  107. {
  108. // Not strictly true, but here to conform with the AES proposal.
  109. // This restriction can be removed if deemed necessary.
  110. if (kb.length != 16 && kb.length != 24 && kb.length != 32)
  111. throw new InvalidKeyException("Key length is not 16, 24, or 32 bytes");
  112. Key key = new Key();
  113. // Here w is our "pre-key".
  114. int[] w = new int[4 * (ROUNDS + 1)];
  115. int i, j;
  116. for (i = 0, j = 0; i < 8 && j < kb.length; i++)
  117. w[i] = (kb[j++] & 0xff)
  118. | (kb[j++] & 0xff) << 8
  119. | (kb[j++] & 0xff) << 16
  120. | (kb[j++] & 0xff) << 24;
  121. // Pad key if < 256 bits.
  122. if (i != 8)
  123. w[i] = 1;
  124. // Transform using w_i-8 ... w_i-1
  125. for (i = 8, j = 0; i < 16; i++)
  126. {
  127. int t = w[j] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ j++;
  128. w[i] = t << 11 | t >>> 21;
  129. }
  130. // Translate by 8.
  131. for (i = 0; i < 8; i++)
  132. w[i] = w[i + 8];
  133. // Transform the rest of the key.
  134. for (; i < w.length; i++)
  135. {
  136. int t = w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i;
  137. w[i] = t << 11 | t >>> 21;
  138. }
  139. // After these s-boxes the pre-key (w, above) will become the
  140. // session key (key, below).
  141. sbox3(w[0], w[1], w[2], w[3]);
  142. key.k0 = x0;
  143. key.k1 = x1;
  144. key.k2 = x2;
  145. key.k3 = x3;
  146. sbox2(w[4], w[5], w[6], w[7]);
  147. key.k4 = x0;
  148. key.k5 = x1;
  149. key.k6 = x2;
  150. key.k7 = x3;
  151. sbox1(w[8], w[9], w[10], w[11]);
  152. key.k8 = x0;
  153. key.k9 = x1;
  154. key.k10 = x2;
  155. key.k11 = x3;
  156. sbox0(w[12], w[13], w[14], w[15]);
  157. key.k12 = x0;
  158. key.k13 = x1;
  159. key.k14 = x2;
  160. key.k15 = x3;
  161. sbox7(w[16], w[17], w[18], w[19]);
  162. key.k16 = x0;
  163. key.k17 = x1;
  164. key.k18 = x2;
  165. key.k19 = x3;
  166. sbox6(w[20], w[21], w[22], w[23]);
  167. key.k20 = x0;
  168. key.k21 = x1;
  169. key.k22 = x2;
  170. key.k23 = x3;
  171. sbox5(w[24], w[25], w[26], w[27]);
  172. key.k24 = x0;
  173. key.k25 = x1;
  174. key.k26 = x2;
  175. key.k27 = x3;
  176. sbox4(w[28], w[29], w[30], w[31]);
  177. key.k28 = x0;
  178. key.k29 = x1;
  179. key.k30 = x2;
  180. key.k31 = x3;
  181. sbox3(w[32], w[33], w[34], w[35]);
  182. key.k32 = x0;
  183. key.k33 = x1;
  184. key.k34 = x2;
  185. key.k35 = x3;
  186. sbox2(w[36], w[37], w[38], w[39]);
  187. key.k36 = x0;
  188. key.k37 = x1;
  189. key.k38 = x2;
  190. key.k39 = x3;
  191. sbox1(w[40], w[41], w[42], w[43]);
  192. key.k40 = x0;
  193. key.k41 = x1;
  194. key.k42 = x2;
  195. key.k43 = x3;
  196. sbox0(w[44], w[45], w[46], w[47]);
  197. key.k44 = x0;
  198. key.k45 = x1;
  199. key.k46 = x2;
  200. key.k47 = x3;
  201. sbox7(w[48], w[49], w[50], w[51]);
  202. key.k48 = x0;
  203. key.k49 = x1;
  204. key.k50 = x2;
  205. key.k51 = x3;
  206. sbox6(w[52], w[53], w[54], w[55]);
  207. key.k52 = x0;
  208. key.k53 = x1;
  209. key.k54 = x2;
  210. key.k55 = x3;
  211. sbox5(w[56], w[57], w[58], w[59]);
  212. key.k56 = x0;
  213. key.k57 = x1;
  214. key.k58 = x2;
  215. key.k59 = x3;
  216. sbox4(w[60], w[61], w[62], w[63]);
  217. key.k60 = x0;
  218. key.k61 = x1;
  219. key.k62 = x2;
  220. key.k63 = x3;
  221. sbox3(w[64], w[65], w[66], w[67]);
  222. key.k64 = x0;
  223. key.k65 = x1;
  224. key.k66 = x2;
  225. key.k67 = x3;
  226. sbox2(w[68], w[69], w[70], w[71]);
  227. key.k68 = x0;
  228. key.k69 = x1;
  229. key.k70 = x2;
  230. key.k71 = x3;
  231. sbox1(w[72], w[73], w[74], w[75]);
  232. key.k72 = x0;
  233. key.k73 = x1;
  234. key.k74 = x2;
  235. key.k75 = x3;
  236. sbox0(w[76], w[77], w[78], w[79]);
  237. key.k76 = x0;
  238. key.k77 = x1;
  239. key.k78 = x2;
  240. key.k79 = x3;
  241. sbox7(w[80], w[81], w[82], w[83]);
  242. key.k80 = x0;
  243. key.k81 = x1;
  244. key.k82 = x2;
  245. key.k83 = x3;
  246. sbox6(w[84], w[85], w[86], w[87]);
  247. key.k84 = x0;
  248. key.k85 = x1;
  249. key.k86 = x2;
  250. key.k87 = x3;
  251. sbox5(w[88], w[89], w[90], w[91]);
  252. key.k88 = x0;
  253. key.k89 = x1;
  254. key.k90 = x2;
  255. key.k91 = x3;
  256. sbox4(w[92], w[93], w[94], w[95]);
  257. key.k92 = x0;
  258. key.k93 = x1;
  259. key.k94 = x2;
  260. key.k95 = x3;
  261. sbox3(w[96], w[97], w[98], w[99]);
  262. key.k96 = x0;
  263. key.k97 = x1;
  264. key.k98 = x2;
  265. key.k99 = x3;
  266. sbox2(w[100], w[101], w[102], w[103]);
  267. key.k100 = x0;
  268. key.k101 = x1;
  269. key.k102 = x2;
  270. key.k103 = x3;
  271. sbox1(w[104], w[105], w[106], w[107]);
  272. key.k104 = x0;
  273. key.k105 = x1;
  274. key.k106 = x2;
  275. key.k107 = x3;
  276. sbox0(w[108], w[109], w[110], w[111]);
  277. key.k108 = x0;
  278. key.k109 = x1;
  279. key.k110 = x2;
  280. key.k111 = x3;
  281. sbox7(w[112], w[113], w[114], w[115]);
  282. key.k112 = x0;
  283. key.k113 = x1;
  284. key.k114 = x2;
  285. key.k115 = x3;
  286. sbox6(w[116], w[117], w[118], w[119]);
  287. key.k116 = x0;
  288. key.k117 = x1;
  289. key.k118 = x2;
  290. key.k119 = x3;
  291. sbox5(w[120], w[121], w[122], w[123]);
  292. key.k120 = x0;
  293. key.k121 = x1;
  294. key.k122 = x2;
  295. key.k123 = x3;
  296. sbox4(w[124], w[125], w[126], w[127]);
  297. key.k124 = x0;
  298. key.k125 = x1;
  299. key.k126 = x2;
  300. key.k127 = x3;
  301. sbox3(w[128], w[129], w[130], w[131]);
  302. key.k128 = x0;
  303. key.k129 = x1;
  304. key.k130 = x2;
  305. key.k131 = x3;
  306. return key;
  307. }
  308. public synchronized void encrypt(byte[] in, int i, byte[] out, int o,
  309. Object K, int bs)
  310. {
  311. Key key = (Key) K;
  312. x0 = (in[i ] & 0xff)
  313. | (in[i + 1] & 0xff) << 8
  314. | (in[i + 2] & 0xff) << 16
  315. | (in[i + 3] & 0xff) << 24;
  316. x1 = (in[i + 4] & 0xff)
  317. | (in[i + 5] & 0xff) << 8
  318. | (in[i + 6] & 0xff) << 16
  319. | (in[i + 7] & 0xff) << 24;
  320. x2 = (in[i + 8] & 0xff)
  321. | (in[i + 9] & 0xff) << 8
  322. | (in[i + 10] & 0xff) << 16
  323. | (in[i + 11] & 0xff) << 24;
  324. x3 = (in[i + 12] & 0xff)
  325. | (in[i + 13] & 0xff) << 8
  326. | (in[i + 14] & 0xff) << 16
  327. | (in[i + 15] & 0xff) << 24;
  328. x0 ^= key.k0;
  329. x1 ^= key.k1;
  330. x2 ^= key.k2;
  331. x3 ^= key.k3;
  332. sbox0();
  333. x1 ^= key.k4;
  334. x4 ^= key.k5;
  335. x2 ^= key.k6;
  336. x0 ^= key.k7;
  337. sbox1();
  338. x0 ^= key.k8;
  339. x4 ^= key.k9;
  340. x2 ^= key.k10;
  341. x1 ^= key.k11;
  342. sbox2();
  343. x2 ^= key.k12;
  344. x1 ^= key.k13;
  345. x4 ^= key.k14;
  346. x3 ^= key.k15;
  347. sbox3();
  348. x1 ^= key.k16;
  349. x4 ^= key.k17;
  350. x3 ^= key.k18;
  351. x0 ^= key.k19;
  352. sbox4();
  353. x4 ^= key.k20;
  354. x2 ^= key.k21;
  355. x1 ^= key.k22;
  356. x0 ^= key.k23;
  357. sbox5();
  358. x2 ^= key.k24;
  359. x0 ^= key.k25;
  360. x4 ^= key.k26;
  361. x1 ^= key.k27;
  362. sbox6();
  363. x2 ^= key.k28;
  364. x0 ^= key.k29;
  365. x3 ^= key.k30;
  366. x4 ^= key.k31;
  367. sbox7();
  368. x0 = x3;
  369. x3 = x2;
  370. x2 = x4;
  371. x0 ^= key.k32;
  372. x1 ^= key.k33;
  373. x2 ^= key.k34;
  374. x3 ^= key.k35;
  375. sbox0();
  376. x1 ^= key.k36;
  377. x4 ^= key.k37;
  378. x2 ^= key.k38;
  379. x0 ^= key.k39;
  380. sbox1();
  381. x0 ^= key.k40;
  382. x4 ^= key.k41;
  383. x2 ^= key.k42;
  384. x1 ^= key.k43;
  385. sbox2();
  386. x2 ^= key.k44;
  387. x1 ^= key.k45;
  388. x4 ^= key.k46;
  389. x3 ^= key.k47;
  390. sbox3();
  391. x1 ^= key.k48;
  392. x4 ^= key.k49;
  393. x3 ^= key.k50;
  394. x0 ^= key.k51;
  395. sbox4();
  396. x4 ^= key.k52;
  397. x2 ^= key.k53;
  398. x1 ^= key.k54;
  399. x0 ^= key.k55;
  400. sbox5();
  401. x2 ^= key.k56;
  402. x0 ^= key.k57;
  403. x4 ^= key.k58;
  404. x1 ^= key.k59;
  405. sbox6();
  406. x2 ^= key.k60;
  407. x0 ^= key.k61;
  408. x3 ^= key.k62;
  409. x4 ^= key.k63;
  410. sbox7();
  411. x0 = x3;
  412. x3 = x2;
  413. x2 = x4;
  414. x0 ^= key.k64;
  415. x1 ^= key.k65;
  416. x2 ^= key.k66;
  417. x3 ^= key.k67;
  418. sbox0();
  419. x1 ^= key.k68;
  420. x4 ^= key.k69;
  421. x2 ^= key.k70;
  422. x0 ^= key.k71;
  423. sbox1();
  424. x0 ^= key.k72;
  425. x4 ^= key.k73;
  426. x2 ^= key.k74;
  427. x1 ^= key.k75;
  428. sbox2();
  429. x2 ^= key.k76;
  430. x1 ^= key.k77;
  431. x4 ^= key.k78;
  432. x3 ^= key.k79;
  433. sbox3();
  434. x1 ^= key.k80;
  435. x4 ^= key.k81;
  436. x3 ^= key.k82;
  437. x0 ^= key.k83;
  438. sbox4();
  439. x4 ^= key.k84;
  440. x2 ^= key.k85;
  441. x1 ^= key.k86;
  442. x0 ^= key.k87;
  443. sbox5();
  444. x2 ^= key.k88;
  445. x0 ^= key.k89;
  446. x4 ^= key.k90;
  447. x1 ^= key.k91;
  448. sbox6();
  449. x2 ^= key.k92;
  450. x0 ^= key.k93;
  451. x3 ^= key.k94;
  452. x4 ^= key.k95;
  453. sbox7();
  454. x0 = x3;
  455. x3 = x2;
  456. x2 = x4;
  457. x0 ^= key.k96;
  458. x1 ^= key.k97;
  459. x2 ^= key.k98;
  460. x3 ^= key.k99;
  461. sbox0();
  462. x1 ^= key.k100;
  463. x4 ^= key.k101;
  464. x2 ^= key.k102;
  465. x0 ^= key.k103;
  466. sbox1();
  467. x0 ^= key.k104;
  468. x4 ^= key.k105;
  469. x2 ^= key.k106;
  470. x1 ^= key.k107;
  471. sbox2();
  472. x2 ^= key.k108;
  473. x1 ^= key.k109;
  474. x4 ^= key.k110;
  475. x3 ^= key.k111;
  476. sbox3();
  477. x1 ^= key.k112;
  478. x4 ^= key.k113;
  479. x3 ^= key.k114;
  480. x0 ^= key.k115;
  481. sbox4();
  482. x4 ^= key.k116;
  483. x2 ^= key.k117;
  484. x1 ^= key.k118;
  485. x0 ^= key.k119;
  486. sbox5();
  487. x2 ^= key.k120;
  488. x0 ^= key.k121;
  489. x4 ^= key.k122;
  490. x1 ^= key.k123;
  491. sbox6();
  492. x2 ^= key.k124;
  493. x0 ^= key.k125;
  494. x3 ^= key.k126;
  495. x4 ^= key.k127;
  496. sbox7noLT();
  497. x0 = x3;
  498. x3 = x2;
  499. x2 = x4;
  500. x0 ^= key.k128;
  501. x1 ^= key.k129;
  502. x2 ^= key.k130;
  503. x3 ^= key.k131;
  504. out[o ] = (byte) x0;
  505. out[o + 1] = (byte)(x0 >>> 8);
  506. out[o + 2] = (byte)(x0 >>> 16);
  507. out[o + 3] = (byte)(x0 >>> 24);
  508. out[o + 4] = (byte) x1;
  509. out[o + 5] = (byte)(x1 >>> 8);
  510. out[o + 6] = (byte)(x1 >>> 16);
  511. out[o + 7] = (byte)(x1 >>> 24);
  512. out[o + 8] = (byte) x2;
  513. out[o + 9] = (byte)(x2 >>> 8);
  514. out[o + 10] = (byte)(x2 >>> 16);
  515. out[o + 11] = (byte)(x2 >>> 24);
  516. out[o + 12] = (byte) x3;
  517. out[o + 13] = (byte)(x3 >>> 8);
  518. out[o + 14] = (byte)(x3 >>> 16);
  519. out[o + 15] = (byte)(x3 >>> 24);
  520. }
  521. public synchronized void decrypt(byte[] in, int i, byte[] out, int o,
  522. Object K, int bs)
  523. {
  524. Key key = (Key) K;
  525. x0 = (in[i ] & 0xff)
  526. | (in[i + 1] & 0xff) << 8
  527. | (in[i + 2] & 0xff) << 16
  528. | (in[i + 3] & 0xff) << 24;
  529. x1 = (in[i + 4] & 0xff)
  530. | (in[i + 5] & 0xff) << 8
  531. | (in[i + 6] & 0xff) << 16
  532. | (in[i + 7] & 0xff) << 24;
  533. x2 = (in[i + 8] & 0xff)
  534. | (in[i + 9] & 0xff) << 8
  535. | (in[i + 10] & 0xff) << 16
  536. | (in[i + 11] & 0xff) << 24;
  537. x3 = (in[i + 12] & 0xff)
  538. | (in[i + 13] & 0xff) << 8
  539. | (in[i + 14] & 0xff) << 16
  540. | (in[i + 15] & 0xff) << 24;
  541. x0 ^= key.k128;
  542. x1 ^= key.k129;
  543. x2 ^= key.k130;
  544. x3 ^= key.k131;
  545. sboxI7noLT();
  546. x3 ^= key.k124;
  547. x0 ^= key.k125;
  548. x1 ^= key.k126;
  549. x4 ^= key.k127;
  550. sboxI6();
  551. x0 ^= key.k120;
  552. x1 ^= key.k121;
  553. x2 ^= key.k122;
  554. x4 ^= key.k123;
  555. sboxI5();
  556. x1 ^= key.k116;
  557. x3 ^= key.k117;
  558. x4 ^= key.k118;
  559. x2 ^= key.k119;
  560. sboxI4();
  561. x1 ^= key.k112;
  562. x2 ^= key.k113;
  563. x4 ^= key.k114;
  564. x0 ^= key.k115;
  565. sboxI3();
  566. x0 ^= key.k108;
  567. x1 ^= key.k109;
  568. x4 ^= key.k110;
  569. x2 ^= key.k111;
  570. sboxI2();
  571. x1 ^= key.k104;
  572. x3 ^= key.k105;
  573. x4 ^= key.k106;
  574. x2 ^= key.k107;
  575. sboxI1();
  576. x0 ^= key.k100;
  577. x1 ^= key.k101;
  578. x2 ^= key.k102;
  579. x4 ^= key.k103;
  580. sboxI0();
  581. x0 ^= key.k96;
  582. x3 ^= key.k97;
  583. x1 ^= key.k98;
  584. x4 ^= key.k99;
  585. sboxI7();
  586. x1 = x3;
  587. x3 = x4;
  588. x4 = x2;
  589. x3 ^= key.k92;
  590. x0 ^= key.k93;
  591. x1 ^= key.k94;
  592. x4 ^= key.k95;
  593. sboxI6();
  594. x0 ^= key.k88;
  595. x1 ^= key.k89;
  596. x2 ^= key.k90;
  597. x4 ^= key.k91;
  598. sboxI5();
  599. x1 ^= key.k84;
  600. x3 ^= key.k85;
  601. x4 ^= key.k86;
  602. x2 ^= key.k87;
  603. sboxI4();
  604. x1 ^= key.k80;
  605. x2 ^= key.k81;
  606. x4 ^= key.k82;
  607. x0 ^= key.k83;
  608. sboxI3();
  609. x0 ^= key.k76;
  610. x1 ^= key.k77;
  611. x4 ^= key.k78;
  612. x2 ^= key.k79;
  613. sboxI2();
  614. x1 ^= key.k72;
  615. x3 ^= key.k73;
  616. x4 ^= key.k74;
  617. x2 ^= key.k75;
  618. sboxI1();
  619. x0 ^= key.k68;
  620. x1 ^= key.k69;
  621. x2 ^= key.k70;
  622. x4 ^= key.k71;
  623. sboxI0();
  624. x0 ^= key.k64;
  625. x3 ^= key.k65;
  626. x1 ^= key.k66;
  627. x4 ^= key.k67;
  628. sboxI7();
  629. x1 = x3;
  630. x3 = x4;
  631. x4 = x2;
  632. x3 ^= key.k60;
  633. x0 ^= key.k61;
  634. x1 ^= key.k62;
  635. x4 ^= key.k63;
  636. sboxI6();
  637. x0 ^= key.k56;
  638. x1 ^= key.k57;
  639. x2 ^= key.k58;
  640. x4 ^= key.k59;
  641. sboxI5();
  642. x1 ^= key.k52;
  643. x3 ^= key.k53;
  644. x4 ^= key.k54;
  645. x2 ^= key.k55;
  646. sboxI4();
  647. x1 ^= key.k48;
  648. x2 ^= key.k49;
  649. x4 ^= key.k50;
  650. x0 ^= key.k51;
  651. sboxI3();
  652. x0 ^= key.k44;
  653. x1 ^= key.k45;
  654. x4 ^= key.k46;
  655. x2 ^= key.k47;
  656. sboxI2();
  657. x1 ^= key.k40;
  658. x3 ^= key.k41;
  659. x4 ^= key.k42;
  660. x2 ^= key.k43;
  661. sboxI1();
  662. x0 ^= key.k36;
  663. x1 ^= key.k37;
  664. x2 ^= key.k38;
  665. x4 ^= key.k39;
  666. sboxI0();
  667. x0 ^= key.k32;
  668. x3 ^= key.k33;
  669. x1 ^= key.k34;
  670. x4 ^= key.k35;
  671. sboxI7();
  672. x1 = x3;
  673. x3 = x4;
  674. x4 = x2;
  675. x3 ^= key.k28;
  676. x0 ^= key.k29;
  677. x1 ^= key.k30;
  678. x4 ^= key.k31;
  679. sboxI6();
  680. x0 ^= key.k24;
  681. x1 ^= key.k25;
  682. x2 ^= key.k26;
  683. x4 ^= key.k27;
  684. sboxI5();
  685. x1 ^= key.k20;
  686. x3 ^= key.k21;
  687. x4 ^= key.k22;
  688. x2 ^= key.k23;
  689. sboxI4();
  690. x1 ^= key.k16;
  691. x2 ^= key.k17;
  692. x4 ^= key.k18;
  693. x0 ^= key.k19;
  694. sboxI3();
  695. x0 ^= key.k12;
  696. x1 ^= key.k13;
  697. x4 ^= key.k14;
  698. x2 ^= key.k15;
  699. sboxI2();
  700. x1 ^= key.k8;
  701. x3 ^= key.k9;
  702. x4 ^= key.k10;
  703. x2 ^= key.k11;
  704. sboxI1();
  705. x0 ^= key.k4;
  706. x1 ^= key.k5;
  707. x2 ^= key.k6;
  708. x4 ^= key.k7;
  709. sboxI0();
  710. x2 = x1;
  711. x1 = x3;
  712. x3 = x4;
  713. x0 ^= key.k0;
  714. x1 ^= key.k1;
  715. x2 ^= key.k2;
  716. x3 ^= key.k3;
  717. out[o ] = (byte) x0;
  718. out[o + 1] = (byte)(x0 >>> 8);
  719. out[o + 2] = (byte)(x0 >>> 16);
  720. out[o + 3] = (byte)(x0 >>> 24);
  721. out[o + 4] = (byte) x1;
  722. out[o + 5] = (byte)(x1 >>> 8);
  723. out[o + 6] = (byte)(x1 >>> 16);
  724. out[o + 7] = (byte)(x1 >>> 24);
  725. out[o + 8] = (byte) x2;
  726. out[o + 9] = (byte)(x2 >>> 8);
  727. out[o + 10] = (byte)(x2 >>> 16);
  728. out[o + 11] = (byte)(x2 >>> 24);
  729. out[o + 12] = (byte) x3;
  730. out[o + 13] = (byte)(x3 >>> 8);
  731. out[o + 14] = (byte)(x3 >>> 16);
  732. out[o + 15] = (byte)(x3 >>> 24);
  733. }
  734. public boolean selfTest()
  735. {
  736. if (valid == null)
  737. {
  738. boolean result = super.selfTest(); // do symmetry tests
  739. if (result)
  740. result = testKat(KAT_KEY, KAT_CT);
  741. valid = Boolean.valueOf(result);
  742. }
  743. return valid.booleanValue();
  744. }
  745. // These first few S-boxes operate directly on the "registers",
  746. // x0..x4, and perform the linear transform.
  747. private void sbox0()
  748. {
  749. x3 ^= x0;
  750. x4 = x1;
  751. x1 &= x3;
  752. x4 ^= x2;
  753. x1 ^= x0;
  754. x0 |= x3;
  755. x0 ^= x4;
  756. x4 ^= x3;
  757. x3 ^= x2;
  758. x2 |= x1;
  759. x2 ^= x4;
  760. x4 ^= -1;
  761. x4 |= x1;
  762. x1 ^= x3;
  763. x1 ^= x4;
  764. x3 |= x0;
  765. x1 ^= x3;
  766. x4 ^= x3;
  767. x1 = (x1 << 13) | (x1 >>> 19);
  768. x4 ^= x1;
  769. x3 = x1 << 3;
  770. x2 = (x2 << 3) | (x2 >>> 29);
  771. x4 ^= x2;
  772. x0 ^= x2;
  773. x4 = (x4 << 1) | (x4 >>> 31);
  774. x0 ^= x3;
  775. x0 = (x0 << 7) | (x0 >>> 25);
  776. x3 = x4;
  777. x1 ^= x4;
  778. x3 <<= 7;
  779. x1 ^= x0;
  780. x2 ^= x0;
  781. x2 ^= x3;
  782. x1 = (x1 << 5) | (x1 >>> 27);
  783. x2 = (x2 << 22) | (x2 >>> 10);
  784. }
  785. private void sbox1()
  786. {
  787. x4 = ~x4;
  788. x3 = x1;
  789. x1 ^= x4;
  790. x3 |= x4;
  791. x3 ^= x0;
  792. x0 &= x1;
  793. x2 ^= x3;
  794. x0 ^= x4;
  795. x0 |= x2;
  796. x1 ^= x3;
  797. x0 ^= x1;
  798. x4 &= x2;
  799. x1 |= x4;
  800. x4 ^= x3;
  801. x1 ^= x2;
  802. x3 |= x0;
  803. x1 ^= x3;
  804. x3 = ~x3;
  805. x4 ^= x0;
  806. x3 &= x2;
  807. x4 = ~x4;
  808. x3 ^= x1;
  809. x4 ^= x3;
  810. x0 = (x0 << 13) | (x0 >>> 19);
  811. x4 ^= x0;
  812. x3 = x0 << 3;
  813. x2 = (x2 << 3) | (x2 >>> 29);
  814. x4 ^= x2;
  815. x1 ^= x2;
  816. x4 = (x4 << 1) | (x4 >>> 31);
  817. x1 ^= x3;
  818. x1 = (x1 << 7) | (x1 >>> 25);
  819. x3 = x4;
  820. x0 ^= x4;
  821. x3 <<= 7;
  822. x0 ^= x1;
  823. x2 ^= x1;
  824. x2 ^= x3;
  825. x0 = (x0 << 5) | (x0 >>> 27);
  826. x2 = (x2 << 22) | (x2 >>> 10);
  827. }
  828. private void sbox2()
  829. {
  830. x3 = x0;
  831. x0 = x0 & x2;
  832. x0 = x0 ^ x1;
  833. x2 = x2 ^ x4;
  834. x2 = x2 ^ x0;
  835. x1 = x1 | x3;
  836. x1 = x1 ^ x4;
  837. x3 = x3 ^ x2;
  838. x4 = x1;
  839. x1 = x1 | x3;
  840. x1 = x1 ^ x0;
  841. x0 = x0 & x4;
  842. x3 = x3 ^ x0;
  843. x4 = x4 ^ x1;
  844. x4 = x4 ^ x3;
  845. x3 = ~x3;
  846. x2 = (x2 << 13) | (x2 >>> 19);
  847. x1 ^= x2;
  848. x0 = x2 << 3;
  849. x4 = (x4 << 3) | (x4 >>> 29);
  850. x1 ^= x4;
  851. x3 ^= x4;
  852. x1 = (x1 << 1) | (x1 >>> 31);
  853. x3 ^= x0;
  854. x3 = (x3 << 7) | (x3 >>> 25);
  855. x0 = x1;
  856. x2 ^= x1;
  857. x0 <<= 7;
  858. x2 ^= x3;
  859. x4 ^= x3;
  860. x4 ^= x0;
  861. x2 = (x2 << 5) | (x2 >>> 27);
  862. x4 = (x4 << 22) | (x4 >>> 10);
  863. }
  864. private void sbox3()
  865. {
  866. x0 = x2;
  867. x2 = x2 | x3;
  868. x3 = x3 ^ x1;
  869. x1 = x1 & x0;
  870. x0 = x0 ^ x4;
  871. x4 = x4 ^ x3;
  872. x3 = x3 & x2;
  873. x0 = x0 | x1;
  874. x3 = x3 ^ x0;
  875. x2 = x2 ^ x1;
  876. x0 = x0 & x2;
  877. x1 = x1 ^ x3;
  878. x0 = x0 ^ x4;
  879. x1 = x1 | x2;
  880. x1 = x1 ^ x4;
  881. x2 = x2 ^ x3;
  882. x4 = x1;
  883. x1 = x1 | x3;
  884. x1 = x1 ^ x2;
  885. x1 = (x1 << 13) | (x1 >>> 19);
  886. x4 ^= x1;
  887. x2 = x1 << 3;
  888. x3 = (x3 << 3) | (x3 >>> 29);
  889. x4 ^= x3;
  890. x0 ^= x3;
  891. x4 = (x4 << 1) | (x4 >>> 31);
  892. x0 ^= x2;
  893. x0 = (x0 << 7) | (x0 >>> 25);
  894. x2 = x4;
  895. x1 ^= x4;
  896. x2 <<= 7;
  897. x1 ^= x0;
  898. x3 ^= x0;
  899. x3 ^= x2;
  900. x1 = (x1 << 5) | (x1 >>> 27);
  901. x3 = (x3 << 22) | (x3 >>> 10);
  902. }
  903. private void sbox4()
  904. {
  905. x4 = x4 ^ x0;
  906. x0 = ~x0;
  907. x3 = x3 ^ x0;
  908. x0 = x0 ^ x1;
  909. x2 = x4;
  910. x4 = x4 & x0;
  911. x4 = x4 ^ x3;
  912. x2 = x2 ^ x0;
  913. x1 = x1 ^ x2;
  914. x3 = x3 & x2;
  915. x3 = x3 ^ x1;
  916. x1 = x1 & x4;
  917. x0 = x0 ^ x1;
  918. x2 = x2 | x4;
  919. x2 = x2 ^ x1;
  920. x1 = x1 | x0;
  921. x1 = x1 ^ x3;
  922. x3 = x3 & x0;
  923. x1 = ~x1;
  924. x2 = x2 ^ x3;
  925. x4 = (x4 << 13) | (x4 >>> 19);
  926. x2 ^= x4;
  927. x3 = x4 << 3;
  928. x1 = (x1 << 3) | (x1 >>> 29);
  929. x2 ^= x1;
  930. x0 ^= x1;
  931. x2 = (x2 << 1) | (x2 >>> 31);
  932. x0 ^= x3;
  933. x0 = (x0 << 7) | (x0 >>> 25);
  934. x3 = x2;
  935. x4 ^= x2;
  936. x3 <<= 7;
  937. x4 ^= x0;
  938. x1 ^= x0;
  939. x1 ^= x3;
  940. x4 = (x4 << 5) | (x4 >>> 27);
  941. x1 = (x1 << 22) | (x1 >>> 10);
  942. }
  943. private void sbox5()
  944. {
  945. x4 = x4 ^ x2;
  946. x2 = x2 ^ x0;
  947. x0 = ~x0;
  948. x3 = x2;
  949. x2 = x2 & x4;
  950. x1 = x1 ^ x0;
  951. x2 = x2 ^ x1;
  952. x1 = x1 | x3;
  953. x3 = x3 ^ x0;
  954. x0 = x0 & x2;
  955. x0 = x0 ^ x4;
  956. x3 = x3 ^ x2;
  957. x3 = x3 ^ x1;
  958. x1 = x1 ^ x4;
  959. x4 = x4 & x0;
  960. x1 = ~x1;
  961. x4 = x4 ^ x3;
  962. x3 = x3 | x0;
  963. x1 = x1 ^ x3;
  964. x2 = (x2 << 13) | (x2 >>> 19);
  965. x0 ^= x2;
  966. x3 = x2 << 3;
  967. x4 = (x4 << 3) | (x4 >>> 29);
  968. x0 ^= x4;
  969. x1 ^= x4;
  970. x0 = (x0 << 1) | (x0 >>> 31);
  971. x1 ^= x3;
  972. x1 = (x1 << 7) | (x1 >>> 25);
  973. x3 = x0;
  974. x2 ^= x0;
  975. x3 <<= 7;
  976. x2 ^= x1;
  977. x4 ^= x1;
  978. x4 ^= x3;
  979. x2 = (x2 << 5) | (x2 >>> 27);
  980. x4 = (x4 << 22) | (x4 >>> 10);
  981. }
  982. private void sbox6()
  983. {
  984. x4 = ~x4;
  985. x3 = x1;
  986. x1 = x1 & x2;
  987. x2 = x2 ^ x3;
  988. x1 = x1 ^ x4;
  989. x4 = x4 | x3;
  990. x0 = x0 ^ x1;
  991. x4 = x4 ^ x2;
  992. x2 = x2 | x0;
  993. x4 = x4 ^ x0;
  994. x3 = x3 ^ x2;
  995. x2 = x2 | x1;
  996. x2 = x2 ^ x4;
  997. x3 = x3 ^ x1;
  998. x3 = x3 ^ x2;
  999. x1 = ~x1;
  1000. x4 = x4 & x3;
  1001. x4 = x4 ^ x1;
  1002. x2 = (x2 << 13) | (x2 >>> 19);
  1003. x0 ^= x2;
  1004. x1 = x2 << 3;
  1005. x3 = (x3 << 3) | (x3 >>> 29);
  1006. x0 ^= x3;
  1007. x4 ^= x3;
  1008. x0 = (x0 << 1) | (x0 >>> 31);
  1009. x4 ^= x1;
  1010. x4 = (x4 << 7) | (x4 >>> 25);
  1011. x1 = x0;
  1012. x2 ^= x0;
  1013. x1 <<= 7;
  1014. x2 ^= x4;
  1015. x3 ^= x4;
  1016. x3 ^= x1;
  1017. x2 = (x2 << 5) | (x2 >>> 27);
  1018. x3 = (x3 << 22) | (x3 >>> 10);
  1019. }
  1020. private void sbox7()
  1021. {
  1022. x1 = x3;
  1023. x3 = x3 & x0;
  1024. x3 = x3 ^ x4;
  1025. x4 = x4 & x0;
  1026. x1 = x1 ^ x3;
  1027. x3 = x3 ^ x0;
  1028. x0 = x0 ^ x2;
  1029. x2 = x2 | x1;
  1030. x2 = x2 ^ x3;
  1031. x4 = x4 ^ x0;
  1032. x3 = x3 ^ x4;
  1033. x4 = x4 & x2;
  1034. x4 = x4 ^ x1;
  1035. x1 = x1 ^ x3;
  1036. x3 = x3 & x2;
  1037. x1 = ~x1;
  1038. x3 = x3 ^ x1;
  1039. x1 = x1 & x2;
  1040. x0 = x0 ^ x4;
  1041. x1 = x1 ^ x0;
  1042. x3 = (x3 << 13) | (x3 >>> 19);
  1043. x1 ^= x3;
  1044. x0 = x3 << 3;
  1045. x4 = (x4 << 3) | (x4 >>> 29);
  1046. x1 ^= x4;
  1047. x2 ^= x4;
  1048. x1 = (x1 << 1) | (x1 >>> 31);
  1049. x2 ^= x0;
  1050. x2 = (x2 << 7) | (x2 >>> 25);
  1051. x0 = x1;
  1052. x3 ^= x1;
  1053. x0 <<= 7;
  1054. x3 ^= x2;
  1055. x4 ^= x2;
  1056. x4 ^= x0;
  1057. x3 = (x3 << 5) | (x3 >>> 27);
  1058. x4 = (x4 << 22) | (x4 >>> 10);
  1059. }
  1060. /** The final S-box, with no transform. */
  1061. private void sbox7noLT()
  1062. {
  1063. x1 = x3;
  1064. x3 = x3 & x0;
  1065. x3 = x3 ^ x4;
  1066. x4 = x4 & x0;
  1067. x1 = x1 ^ x3;
  1068. x3 = x3 ^ x0;
  1069. x0 = x0 ^ x2;
  1070. x2 = x2 | x1;
  1071. x2 = x2 ^ x3;
  1072. x4 = x4 ^ x0;
  1073. x3 = x3 ^ x4;
  1074. x4 = x4 & x2;
  1075. x4 = x4 ^ x1;
  1076. x1 = x1 ^ x3;
  1077. x3 = x3 & x2;
  1078. x1 = ~x1;
  1079. x3 = x3 ^ x1;
  1080. x1 = x1 & x2;
  1081. x0 = x0 ^ x4;
  1082. x1 = x1 ^ x0;
  1083. }
  1084. private void sboxI7noLT()
  1085. {
  1086. x4 = x2;
  1087. x2 ^= x0;
  1088. x0 &= x3;
  1089. x2 = ~x2;
  1090. x4 |= x3;
  1091. x3 ^= x1;
  1092. x1 |= x0;
  1093. x0 ^= x2;
  1094. x2 &= x4;
  1095. x1 ^= x2;
  1096. x2 ^= x0;
  1097. x0 |= x2;
  1098. x3 &= x4;
  1099. x0 ^= x3;
  1100. x4 ^= x1;
  1101. x3 ^= x4;
  1102. x4 |= x0;
  1103. x3 ^= x2;
  1104. x4 ^= x2;
  1105. }
  1106. private void sboxI6()
  1107. {
  1108. x1 = (x1 >>> 22) | (x1 << 10);
  1109. x3 = (x3 >>> 5) | (x3 << 27);
  1110. x2 = x0;
  1111. x1 ^= x4;
  1112. x2 <<= 7;
  1113. x3 ^= x4;
  1114. x1 ^= x2;
  1115. x3 ^= x0;
  1116. x4 = (x4 >>> 7) | (x4 << 25);
  1117. x0 = (x0 >>> 1) | (x0 << 31);
  1118. x0 ^= x3;
  1119. x2 = x3 << 3;
  1120. x4 ^= x2;
  1121. x3 = (x3 >>> 13) | (x3 << 19);
  1122. x0 ^= x1;
  1123. x4 ^= x1;
  1124. x1 = (x1 >>> 3) | (x1 << 29);
  1125. x3 ^= x1;
  1126. x2 = x1;
  1127. x1 &= x3;
  1128. x2 ^= x4;
  1129. x1 = ~x1;
  1130. x4 ^= x0;
  1131. x1 ^= x4;
  1132. x2 |= x3;
  1133. x3 ^= x1;
  1134. x4 ^= x2;
  1135. x2 ^= x0;
  1136. x0 &= x4;
  1137. x0 ^= x3;
  1138. x3 ^= x4;
  1139. x3 |= x1;
  1140. x4 ^= x0;
  1141. x2 ^= x3;
  1142. }
  1143. private void sboxI5()
  1144. {
  1145. x2 = (x2 >>> 22) | (x2 << 10);
  1146. x0 = (x0 >>> 5) | (x0 << 27);
  1147. x3 = x1;
  1148. x2 ^= x4;
  1149. x3 <<= 7;
  1150. x0 ^= x4;
  1151. x2 ^= x3;
  1152. x0 ^= x1;
  1153. x4 = (x4 >>> 7) | (x4 << 25);
  1154. x1 = (x1 >>> 1) | (x1 << 31);
  1155. x1 ^= x0;
  1156. x3 = x0 << 3;
  1157. x4 ^= x3;
  1158. x0 = (x0 >>> 13) | (x0 << 19);
  1159. x1 ^= x2;
  1160. x4 ^= x2;
  1161. x2 = (x2 >>> 3) | (x2 << 29);
  1162. x1 = ~x1;
  1163. x3 = x4;
  1164. x2 ^= x1;
  1165. x4 |= x0;
  1166. x4 ^= x2;
  1167. x2 |= x1;
  1168. x2 &= x0;
  1169. x3 ^= x4;
  1170. x2 ^= x3;
  1171. x3 |= x0;
  1172. x3 ^= x1;
  1173. x1 &= x2;
  1174. x1 ^= x4;
  1175. x3 ^= x2;
  1176. x4 &= x3;
  1177. x3 ^= x1;
  1178. x4 ^= x0;
  1179. x4 ^= x3;
  1180. x3 = ~x3;
  1181. }
  1182. private void sboxI4()
  1183. {
  1184. x4 = (x4 >>> 22) | (x4 << 10);
  1185. x1 = (x1 >>> 5) | (x1 << 27);
  1186. x0 = x3;
  1187. x4 ^= x2;
  1188. x0 <<= 7;
  1189. x1 ^= x2;
  1190. x4 ^= x0;
  1191. x1 ^= x3;
  1192. x2 = (x2 >>> 7) | (x2 << 25);
  1193. x3 = (x3 >>> 1) | (x3 << 31);
  1194. x3 ^= x1;
  1195. x0 = x1 << 3;
  1196. x2 ^= x0;
  1197. x1 = (x1 >>> 13) | (x1 << 19);
  1198. x3 ^= x4;
  1199. x2 ^= x4;
  1200. x4 = (x4 >>> 3) | (x4 << 29);
  1201. x0 = x4;
  1202. x4 &= x2;
  1203. x4 ^= x3;
  1204. x3 |= x2;
  1205. x3 &= x1;
  1206. x0 ^= x4;
  1207. x0 ^= x3;
  1208. x3 &= x4;
  1209. x1 = ~x1;
  1210. x2 ^= x0;
  1211. x3 ^= x2;
  1212. x2 &= x1;
  1213. x2 ^= x4;
  1214. x1 ^= x3;
  1215. x4 &= x1;
  1216. x2 ^= x1;
  1217. x4 ^= x0;
  1218. x4 |= x2;
  1219. x2 ^= x1;
  1220. x4 ^= x3;
  1221. }
  1222. private void sboxI3()
  1223. {
  1224. x4 = (x4 >>> 22) | (x4 << 10);
  1225. x1 = (x1 >>> 5) | (x1 << 27);
  1226. x3 = x2;
  1227. x4 ^= x0;
  1228. x3 <<= 7;
  1229. x1 ^= x0;
  1230. x4 ^= x3;
  1231. x1 ^= x2;
  1232. x0 = (x0 >>> 7) | (x0 << 25);
  1233. x2 = (x2 >>> 1) | (x2 << 31);
  1234. x2 ^= x1;
  1235. x3 = x1 << 3;
  1236. x0 ^= x3;
  1237. x1 = (x1 >>> 13) | (x1 << 19);
  1238. x2 ^= x4;
  1239. x0 ^= x4;
  1240. x4 = (x4 >>> 3) | (x4 << 29);
  1241. x3 = x4;
  1242. x4 ^= x2;
  1243. x2 &= x4;
  1244. x2 ^= x1;
  1245. x1 &= x3;
  1246. x3 ^= x0;
  1247. x0 |= x2;
  1248. x0 ^= x4;
  1249. x1 ^= x3;
  1250. x4 ^= x1;
  1251. x1 |= x0;
  1252. x1 ^= x2;
  1253. x3 ^= x4;
  1254. x4 &= x0;
  1255. x2 |= x0;
  1256. x2 ^= x4;
  1257. x3 ^= x1;
  1258. x4 ^= x3;
  1259. }
  1260. private void sboxI2()
  1261. {
  1262. x4 = (x4 >>> 22) | (x4 << 10);
  1263. x0 = (x0 >>> 5) | (x0 << 27);
  1264. x3 = x1;
  1265. x4 ^= x2;
  1266. x3 <<= 7;
  1267. x0 ^= x2;
  1268. x4 ^= x3;
  1269. x0 ^= x1;
  1270. x2 = (x2 >>> 7) | (x2 << 25);
  1271. x1 = (x1 >>> 1) | (x1 << 31);
  1272. x1 ^= x0;
  1273. x3 = x0 << 3;
  1274. x2 ^= x3;
  1275. x0 = (x0 >>> 13) | (x0 << 19);
  1276. x1 ^= x4;
  1277. x2 ^= x4;
  1278. x4 = (x4 >>> 3) | (x4 << 29);
  1279. x4 ^= x2;
  1280. x2 ^= x0;
  1281. x3 = x2;
  1282. x2 &= x4;
  1283. x2 ^= x1;
  1284. x1 |= x4;
  1285. x1 ^= x3;
  1286. x3 &= x2;
  1287. x4 ^= x2;
  1288. x3 &= x0;
  1289. x3 ^= x4;
  1290. x4 &= x1;
  1291. x4 |= x0;
  1292. x2 = ~x2;
  1293. x4 ^= x2;
  1294. x0 ^= x2;
  1295. x0 &= x1;
  1296. x2 ^= x3;
  1297. x2 ^= x0;
  1298. }
  1299. private void sboxI1()
  1300. {
  1301. x4 = (x4 >>> 22) | (x4 << 10);
  1302. x1 = (x1 >>> 5) | (x1 << 27);
  1303. x0 = x3;
  1304. x4 ^= x2;
  1305. x0 <<= 7;
  1306. x1 ^= x2;
  1307. x4 ^= x0;
  1308. x1 ^= x3;
  1309. x2 = (x2 >>> 7) | (x2 << 25);
  1310. x3 = (x3 >>> 1) | (x3 << 31);
  1311. x3 ^= x1;
  1312. x0 = x1 << 3;
  1313. x2 ^= x0;
  1314. x1 = (x1 >>> 13) | (x1 << 19);
  1315. x3 ^= x4;
  1316. x2 ^= x4;
  1317. x4 = (x4 >>> 3) | (x4 << 29);
  1318. x0 = x3;
  1319. x3 ^= x2;
  1320. x2 &= x3;
  1321. x0 ^= x4;
  1322. x2 ^= x1;
  1323. x1 |= x3;
  1324. x4 ^= x2;
  1325. x1 ^= x0;
  1326. x1 |= x4;
  1327. x3 ^= x2;
  1328. x1 ^= x3;
  1329. x3 |= x2;
  1330. x3 ^= x1;
  1331. x0 = ~x0;
  1332. x0 ^= x3;
  1333. x3 |= x1;
  1334. x3 ^= x1;
  1335. x3 |= x0;
  1336. x2 ^= x3;
  1337. }
  1338. private void sboxI0()
  1339. {
  1340. x2 = (x2 >>> 22) | (x2 << 10);
  1341. x0 = (x0 >>> 5) | (x0 << 27);
  1342. x3 = x1;
  1343. x2 ^= x4;
  1344. x3 <<= 7;
  1345. x0 ^= x4;
  1346. x2 ^= x3;
  1347. x0 ^= x1;
  1348. x4 = (x4 >>> 7) | (x4 << 25);
  1349. x1 = (x1 >>> 1) | (x1 << 31);
  1350. x1 ^= x0;
  1351. x3 = x0 << 3;
  1352. x4 ^= x3;
  1353. x0 = (x0 >>> 13) | (x0 << 19);
  1354. x1 ^= x2;
  1355. x4 ^= x2;
  1356. x2 = (x2 >>> 3) | (x2 << 29);
  1357. x2 = ~x2;
  1358. x3 = x1;
  1359. x1 |= x0;
  1360. x3 = ~x3;
  1361. x1 ^= x2;
  1362. x2 |= x3;
  1363. x1 ^= x4;
  1364. x0 ^= x3;
  1365. x2 ^= x0;
  1366. x0 &= x4;
  1367. x3 ^= x0;
  1368. x0 |= x1;
  1369. x0 ^= x2;
  1370. x4 ^= x3;
  1371. x2 ^= x1;
  1372. x4 ^= x0;
  1373. x4 ^= x1;
  1374. x2 &= x4;
  1375. x3 ^= x2;
  1376. }
  1377. private void sboxI7()
  1378. {
  1379. x1 = (x1 >>> 22) | (x1 << 10);
  1380. x0 = (x0 >>> 5) | (x0 << 27);
  1381. x2 = x3;
  1382. x1 ^= x4;
  1383. x2 <<= 7;
  1384. x0 ^= x4;
  1385. x1 ^= x2;
  1386. x0 ^= x3;
  1387. x4 = (x4 >>> 7) | (x4 << 25);
  1388. x3 = (x3 >>> 1) | (x3 << 31);
  1389. x3 ^= x0;
  1390. x2 = x0 << 3;
  1391. x4 ^= x2;
  1392. x0 = (x0 >>> 13) | (x0 << 19);
  1393. x3 ^= x1;
  1394. x4 ^= x1;
  1395. x1 = (x1 >>> 3) | (x1 << 29);
  1396. x2 = x1;
  1397. x1 ^= x0;
  1398. x0 &= x4;
  1399. x1 = ~x1;
  1400. x2 |= x4;
  1401. x4 ^= x3;
  1402. x3 |= x0;
  1403. x0 ^= x1;
  1404. x1 &= x2;
  1405. x3 ^= x1;
  1406. x1 ^= x0;
  1407. x0 |= x1;
  1408. x4 &= x2;
  1409. x0 ^= x4;
  1410. x2 ^= x3;
  1411. x4 ^= x2;
  1412. x2 |= x0;
  1413. x4 ^= x1;
  1414. x2 ^= x1;
  1415. }
  1416. /** S-Box 0. */
  1417. private void sbox0(int r0, int r1, int r2, int r3)
  1418. {
  1419. int r4 = r1 ^ r2;
  1420. r3 ^= r0;
  1421. r1 = r1 & r3 ^ r0;
  1422. r0 = (r0 | r3) ^ r4;
  1423. r4 ^= r3;
  1424. r3 ^= r2;
  1425. r2 = (r2 | r1) ^ r4;
  1426. r4 = ~r4 | r1;
  1427. r1 ^= r3 ^ r4;
  1428. r3 |= r0;
  1429. x0 = r1 ^ r3;
  1430. x1 = r4 ^ r3;
  1431. x2 = r2;
  1432. x3 = r0;
  1433. }
  1434. /** S-Box 1. */
  1435. private void sbox1(int r0, int r1, int r2, int r3)
  1436. {
  1437. r0 = ~r0;
  1438. int r4 = r0;
  1439. r2 = ~r2;
  1440. r0 &= r1;
  1441. r2 ^= r0;
  1442. r0 |= r3;
  1443. r3 ^= r2;
  1444. r1 ^= r0;
  1445. r0 ^= r4;
  1446. r4 |= r1;
  1447. r1 ^= r3;
  1448. r2 = (r2 | r0) & r4;
  1449. r0 ^= r1;
  1450. x0 = r2;
  1451. x1 = r0 & r2 ^ r4;
  1452. x2 = r3;
  1453. x3 = r1 & r2 ^ r0;
  1454. }
  1455. /** S-Box 2. */
  1456. private void sbox2(int r0, int r1, int r2, int r3)
  1457. {
  1458. int r4 = r0;
  1459. r0 = r0 & r2 ^ r3;
  1460. r2 = r2 ^ r1 ^ r0;
  1461. r3 = (r3 | r4) ^ r1;
  1462. r4 ^= r2;
  1463. r1 = r3;
  1464. r3 = (r3 | r4) ^ r0;
  1465. r0 &= r1;
  1466. r4 ^= r0;
  1467. x0 = r2;
  1468. x1 = r3;
  1469. x2 = r1 ^ r3 ^ r4;
  1470. x3 = ~r4;
  1471. }
  1472. /** S-Box 3. */
  1473. private void sbox3(int r0, int r1, int r2, int r3)
  1474. {
  1475. int r4 = r0;
  1476. r0 |= r3;
  1477. r3 ^= r1;
  1478. r1 &= r4;
  1479. r4 = r4 ^ r2 | r1;
  1480. r2 ^= r3;
  1481. r3 = r3 & r0 ^ r4;
  1482. r0 ^= r1;
  1483. r4 = r4 & r0 ^ r2;
  1484. r1 = (r1 ^ r3 | r0) ^ r2;
  1485. r0 ^= r3;
  1486. x0 = (r1 | r3) ^ r0;
  1487. x1 = r1;
  1488. x2 = r3;
  1489. x3 = r4;
  1490. }
  1491. /** S-Box 4. */
  1492. private void sbox4(int r0, int r1, int r2, int r3)
  1493. {
  1494. r1 ^= r3;
  1495. int r4 = r1;
  1496. r3 = ~r3;
  1497. r2 ^= r3;
  1498. r3 ^= r0;
  1499. r1 = r1 & r3 ^ r2;
  1500. r4 ^= r3;
  1501. r0 ^= r4;
  1502. r2 = r2 & r4 ^ r0;
  1503. r0 &= r1;
  1504. r3 ^= r0;
  1505. r4 = (r4 | r1) ^ r0;
  1506. x0 = r1;
  1507. x1 = r4 ^ (r2 & r3);
  1508. x2 = ~((r0 | r3) ^ r2);
  1509. x3 = r3;
  1510. }
  1511. /** S-Box 5. */
  1512. private void sbox5(int r0, int r1, int r2, int r3)
  1513. {
  1514. r0 ^= r1;
  1515. r1 ^= r3;
  1516. int r4 = r1;
  1517. r3 = ~r3;
  1518. r1 &= r0;
  1519. r2 ^= r3;
  1520. r1 ^= r2;
  1521. r2 |= r4;
  1522. r4 ^= r3;
  1523. r3 = r3 & r1 ^ r0;
  1524. r4 = r4 ^ r1 ^ r2;
  1525. x0 = r1;
  1526. x1 = r3;
  1527. x2 = r0 & r3 ^ r4;
  1528. x3 = ~(r2 ^ r0) ^ (r4 | r3);
  1529. }
  1530. /** S-Box 6. */
  1531. private void sbox6(int r0, int r1, int r2, int r3)
  1532. {
  1533. int r4 = r3;
  1534. r2 = ~r2;
  1535. r3 = r3 & r0 ^ r2;
  1536. r0 ^= r4;
  1537. r2 = (r2 | r4) ^ r0;
  1538. r1 ^= r3;
  1539. r0 |= r1;
  1540. r2 ^= r1;
  1541. r4 ^= r0;
  1542. r0 = (r0 | r3) ^ r2;
  1543. r4 = r4 ^ r3 ^ r0;
  1544. x0 = r0;
  1545. x1 = r1;
  1546. x2 = r4;
  1547. x3 = r2 & r4 ^ ~r3;
  1548. }
  1549. /** S-Box 7. */
  1550. private void sbox7(int r0, int r1, int r2, int r3)
  1551. {
  1552. int r4 = r1;
  1553. r1 = (r1 | r2) ^ r3;
  1554. r4 ^= r2;
  1555. r2 ^= r1;
  1556. r3 = (r3 | r4) & r0;
  1557. r4 ^= r2;
  1558. r3 ^= r1;
  1559. r1 = (r1 | r4) ^ r0;
  1560. r0 = (r0 | r4) ^ r2;
  1561. r1 ^= r4;
  1562. r2 ^= r1;
  1563. x0 = r4 ^ (~r2 | r0);
  1564. x1 = r3;
  1565. x2 = r1 & r0 ^ r4;
  1566. x3 = r0;
  1567. }
  1568. private class Key
  1569. implements Cloneable
  1570. {
  1571. int k0, k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15,
  1572. k16, k17, k18, k19, k20, k21, k22, k23, k24, k25, k26, k27, k28, k29,
  1573. k30, k31, k32, k33, k34, k35, k36, k37, k38, k39, k40, k41, k42, k43,
  1574. k44, k45, k46, k47, k48, k49, k50, k51, k52, k53, k54, k55, k56, k57,
  1575. k58, k59, k60, k61, k62, k63, k64, k65, k66, k67, k68, k69, k70, k71,
  1576. k72, k73, k74, k75, k76, k77, k78, k79, k80, k81, k82, k83, k84, k85,
  1577. k86, k87, k88, k89, k90, k91, k92, k93, k94, k95, k96, k97, k98, k99,
  1578. k100, k101, k102, k103, k104, k105, k106, k107, k108, k109, k110, k111,
  1579. k112, k113, k114, k115, k116, k117, k118, k119, k120, k121, k122, k123,
  1580. k124, k125, k126, k127, k128, k129, k130, k131;
  1581. /** Trivial 0-arguments constructor. */
  1582. Key()
  1583. {
  1584. }
  1585. /** Cloning constructor. */
  1586. private Key(Key that)
  1587. {
  1588. this.k0 = that.k0;
  1589. this.k1 = that.k1;
  1590. this.k2 = that.k2;
  1591. this.k3 = that.k3;
  1592. this.k4 = that.k4;
  1593. this.k5 = that.k5;
  1594. this.k6 = that.k6;
  1595. this.k7 = that.k7;
  1596. this.k8 = that.k8;
  1597. this.k9 = that.k9;
  1598. this.k10 = that.k10;
  1599. this.k11 = that.k11;
  1600. this.k12 = that.k12;
  1601. this.k13 = that.k13;
  1602. this.k14 = that.k14;
  1603. this.k15 = that.k15;
  1604. this.k16 = that.k16;
  1605. this.k17 = that.k17;
  1606. this.k18 = that.k18;
  1607. this.k19 = that.k19;
  1608. this.k20 = that.k20;
  1609. this.k21 = that.k21;
  1610. this.k22 = that.k22;
  1611. this.k23 = that.k23;
  1612. this.k24 = that.k24;
  1613. this.k25 = that.k25;
  1614. this.k26 = that.k26;
  1615. this.k27 = that.k27;
  1616. this.k28 = that.k28;
  1617. this.k29 = that.k29;
  1618. this.k30 = that.k30;
  1619. this.k31 = that.k31;
  1620. this.k32 = that.k32;
  1621. this.k33 = that.k33;
  1622. this.k34 = that.k34;
  1623. this.k35 = that.k35;
  1624. this.k36 = that.k36;
  1625. this.k37 = that.k37;
  1626. this.k38 = that.k38;
  1627. this.k39 = that.k39;
  1628. this.k40 = that.k40;
  1629. this.k41 = that.k41;
  1630. this.k42 = that.k42;
  1631. this.k43 = that.k43;
  1632. this.k44 = that.k44;
  1633. this.k45 = that.k45;
  1634. this.k46 = that.k46;
  1635. this.k47 = that.k47;
  1636. this.k48 = that.k48;
  1637. this.k49 = that.k49;
  1638. this.k50 = that.k50;
  1639. this.k51 = that.k51;
  1640. this.k52 = that.k52;
  1641. this.k53 = that.k53;
  1642. this.k54 = that.k54;
  1643. this.k55 = that.k55;
  1644. this.k56 = that.k56;
  1645. this.k57 = that.k57;
  1646. this.k58 = that.k58;
  1647. this.k59 = that.k59;
  1648. this.k60 = that.k60;
  1649. this.k61 = that.k61;
  1650. this.k62 = that.k62;
  1651. this.k63 = that.k63;
  1652. this.k64 = that.k64;
  1653. this.k65 = that.k65;
  1654. this.k66 = that.k66;
  1655. this.k67 = that.k67;
  1656. this.k68 = that.k68;
  1657. this.k69 = that.k69;
  1658. this.k70 = that.k70;
  1659. this.k71 = that.k71;
  1660. this.k72 = that.k72;
  1661. this.k73 = that.k73;
  1662. this.k74 = that.k74;
  1663. this.k75 = that.k75;
  1664. this.k76 = that.k76;
  1665. this.k77 = that.k77;
  1666. this.k78 = that.k78;
  1667. this.k79 = that.k79;
  1668. this.k80 = that.k80;
  1669. this.k81 = that.k81;
  1670. this.k82 = that.k82;
  1671. this.k83 = that.k83;
  1672. this.k84 = that.k84;
  1673. this.k85 = that.k85;
  1674. this.k86 = that.k86;
  1675. this.k87 = that.k87;
  1676. this.k88 = that.k88;
  1677. this.k89 = that.k89;
  1678. this.k90 = that.k90;
  1679. this.k91 = that.k91;
  1680. this.k92 = that.k92;
  1681. this.k93 = that.k93;
  1682. this.k94 = that.k94;
  1683. this.k95 = that.k95;
  1684. this.k96 = that.k96;
  1685. this.k97 = that.k97;
  1686. this.k98 = that.k98;
  1687. this.k99 = that.k99;
  1688. this.k100 = that.k100;
  1689. this.k101 = that.k101;
  1690. this.k102 = that.k102;
  1691. this.k103 = that.k103;
  1692. this.k104 = that.k104;
  1693. this.k105 = that.k105;
  1694. this.k106 = that.k106;
  1695. this.k107 = that.k107;
  1696. this.k108 = that.k108;
  1697. this.k109 = that.k109;
  1698. this.k110 = that.k110;
  1699. this.k111 = that.k111;
  1700. this.k112 = that.k112;
  1701. this.k113 = that.k113;
  1702. this.k114 = that.k114;
  1703. this.k115 = that.k115;
  1704. this.k116 = that.k116;
  1705. this.k117 = that.k117;
  1706. this.k118 = that.k118;
  1707. this.k119 = that.k119;
  1708. this.k120 = that.k120;
  1709. this.k121 = that.k121;
  1710. this.k122 = that.k122;
  1711. this.k123 = that.k123;
  1712. this.k124 = that.k124;
  1713. this.k125 = that.k125;
  1714. this.k126 = that.k126;
  1715. this.k127 = that.k127;
  1716. this.k128 = that.k128;
  1717. this.k129 = that.k129;
  1718. this.k130 = that.k130;
  1719. this.k131 = that.k131;
  1720. }
  1721. public Object clone()
  1722. {
  1723. return new Key(this);
  1724. }
  1725. }
  1726. }