?? twofish.java
字號:
/* Twofish.java -- Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.crypto.cipher;import gnu.java.security.Registry;import gnu.java.security.util.Util;//import java.io.PrintWriter;import java.security.InvalidKeyException;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;/** * <p>Twofish is a balanced 128-bit Feistel cipher, consisting of 16 rounds. In * each round, a 64-bit S-box value is computed from 64 bits of the block, and * this value is xored into the other half of the block. The two half-blocks are * then exchanged, and the next round begins. Before the first round, all input * bits are xored with key-dependent "whitening" subkeys, and after the final * round the output bits are xored with other key-dependent whitening subkeys; * these subkeys are not used anywhere else in the algorithm.</p> * * <p>Twofish is designed by Bruce Schneier, Doug Whiting, John Kelsey, Chris * Hall, David Wagner and Niels Ferguson.</p> * * <p>References:</p> * * <ol> * <li><a href="http://www.counterpane.com/twofish-paper.html">Twofish: A * 128-bit Block Cipher</a>.</li> * </ol> */public final class Twofish extends BaseCipher{ // Debugging methods and variables // ------------------------------------------------------------------------- // private static final String NAME = "twofish"; private static final boolean DEBUG = false; private static final int debuglevel = 9; // private static final PrintWriter err = new PrintWriter(System.out, true); // private static void debug(String s) { // err.println(">>> "+NAME+": "+s); // } // Constants and variables // ------------------------------------------------------------------------- private static final int DEFAULT_BLOCK_SIZE = 16; // in bytes private static final int DEFAULT_KEY_SIZE = 16; // in bytes private static final int MAX_ROUNDS = 16; // max # rounds (for allocating subkeys) private static final int ROUNDS = MAX_ROUNDS; // subkey array indices private static final int INPUT_WHITEN = 0; private static final int OUTPUT_WHITEN = INPUT_WHITEN + DEFAULT_BLOCK_SIZE / 4; private static final int ROUND_SUBKEYS = OUTPUT_WHITEN + DEFAULT_BLOCK_SIZE / 4; // private static final int TOTAL_SUBKEYS = ROUND_SUBKEYS + 2*MAX_ROUNDS; private static final int SK_STEP = 0x02020202; private static final int SK_BUMP = 0x01010101; private static final int SK_ROTL = 9; private static final String[] Pm = new String[] { // p0 "\uA967\uB3E8\u04FD\uA376\u9A92\u8078\uE4DD\uD138" + "\u0DC6\u3598\u18F7\uEC6C\u4375\u3726\uFA13\u9448" + "\uF2D0\u8B30\u8454\uDF23\u195B\u3D59\uF3AE\uA282" + "\u6301\u832E\uD951\u9B7C\uA6EB\uA5BE\u160C\uE361" + "\uC08C\u3AF5\u732C\u250B\uBB4E\u896B\u536A\uB4F1" + "\uE1E6\uBD45\uE2F4\uB666\uCC95\u0356\uD41C\u1ED7" + "\uFBC3\u8EB5\uE9CF\uBFBA\uEA77\u39AF\u33C9\u6271" + "\u8179\u09AD\u24CD\uF9D8\uE5C5\uB94D\u4408\u86E7" + "\uA11D\uAAED\u0670\uB2D2\u417B\uA011\u31C2\u2790" + "\u20F6\u60FF\u965C\uB1AB\u9E9C\u521B\u5F93\u0AEF" + "\u9185\u49EE\u2D4F\u8F3B\u4787\u6D46\uD63E\u6964" + "\u2ACE\uCB2F\uFC97\u057A\uAC7F\uD51A\u4B0E\uA75A" + "\u2814\u3F29\u883C\u4C02\uB8DA\uB017\u551F\u8A7D" + "\u57C7\u8D74\uB7C4\u9F72\u7E15\u2212\u5807\u9934" + "\u6E50\uDE68\u65BC\uDBF8\uC8A8\u2B40\uDCFE\u32A4" + "\uCA10\u21F0\uD35D\u0F00\u6F9D\u3642\u4A5E\uC1E0", // p1 "\u75F3\uC6F4\uDB7B\uFBC8\u4AD3\uE66B\u457D\uE84B" + "\uD632\uD8FD\u3771\uF1E1\u300F\uF81B\u87FA\u063F" + "\u5EBA\uAE5B\u8A00\uBC9D\u6DC1\uB10E\u805D\uD2D5" + "\uA084\u0714\uB590\u2CA3\uB273\u4C54\u9274\u3651" + "\u38B0\uBD5A\uFC60\u6296\u6C42\uF710\u7C28\u278C" + "\u1395\u9CC7\u2446\u3B70\uCAE3\u85CB\u11D0\u93B8" + "\uA683\u20FF\u9F77\uC3CC\u036F\u08BF\u40E7\u2BE2" + "\u790C\uAA82\u413A\uEAB9\uE49A\uA497\u7EDA\u7A17" + "\u6694\uA11D\u3DF0\uDEB3\u0B72\uA71C\uEFD1\u533E" + "\u8F33\u265F\uEC76\u2A49\u8188\uEE21\uC41A\uEBD9" + "\uC539\u99CD\uAD31\u8B01\u1823\uDD1F\u4E2D\uF948" + "\u4FF2\u658E\u785C\u5819\u8DE5\u9857\u677F\u0564" + "\uAF63\uB6FE\uF5B7\u3CA5\uCEE9\u6844\uE04D\u4369" + "\u292E\uAC15\u59A8\u0A9E\u6E47\uDF34\u356A\uCFDC" + "\u22C9\uC09B\u89D4\uEDAB\u12A2\u0D52\uBB02\u2FA9" + "\uD761\u1EB4\u5004\uF6C2\u1625\u8656\u5509\uBE91" }; /** Fixed 8x8 permutation S-boxes */ private static final byte[][] P = new byte[2][256]; // blank final /** * Define the fixed p0/p1 permutations used in keyed S-box lookup. By * changing the following constant definitions, the S-boxes will * automatically get changed in the Twofish engine. */ private static final int P_00 = 1; private static final int P_01 = 0; private static final int P_02 = 0; private static final int P_03 = P_01 ^ 1; private static final int P_04 = 1; private static final int P_10 = 0; private static final int P_11 = 0; private static final int P_12 = 1; private static final int P_13 = P_11 ^ 1; private static final int P_14 = 0; private static final int P_20 = 1; private static final int P_21 = 1; private static final int P_22 = 0; private static final int P_23 = P_21 ^ 1; private static final int P_24 = 0; private static final int P_30 = 0; private static final int P_31 = 1; private static final int P_32 = 1; private static final int P_33 = P_31 ^ 1; private static final int P_34 = 1; /** Primitive polynomial for GF(256) */ // private static final int GF256_FDBK = 0x169; private static final int GF256_FDBK_2 = 0x169 / 2; private static final int GF256_FDBK_4 = 0x169 / 4; /** MDS matrix */ private static final int[][] MDS = new int[4][256]; // blank final private static final int RS_GF_FDBK = 0x14D; // field generator /** * KAT vector (from ecb_vk): * I=183 * KEY=0000000000000000000000000000000000000000000002000000000000000000 * CT=F51410475B33FBD3DB2117B5C17C82D4 */ private static final byte[] KAT_KEY = Util.toBytesFromString("0000000000000000000000000000000000000000000002000000000000000000"); private static final byte[] KAT_CT = Util.toBytesFromString("F51410475B33FBD3DB2117B5C17C82D4"); /** caches the result of the correctness test, once executed. */ private static Boolean valid; // Static code - to intialise the MDS matrix and lookup tables ------------- static { long time = System.currentTimeMillis(); // expand the P arrays int i; char c; for (i = 0; i < 256; i++) { c = Pm[0].charAt(i >>> 1); P[0][i] = (byte) ((i & 1) == 0 ? c >>> 8 : c); c = Pm[1].charAt(i >>> 1); P[1][i] = (byte) ((i & 1) == 0 ? c >>> 8 : c); } // precompute the MDS matrix int[] m1 = new int[2]; int[] mX = new int[2]; int[] mY = new int[2]; int j; for (i = 0; i < 256; i++) { j = P[0][i] & 0xFF; // compute all the matrix elements m1[0] = j; mX[0] = Mx_X(j) & 0xFF; mY[0] = Mx_Y(j) & 0xFF; j = P[1][i] & 0xFF; m1[1] = j; mX[1] = Mx_X(j) & 0xFF; mY[1] = Mx_Y(j) & 0xFF; MDS[0][i] = m1[P_00] << 0 | // fill matrix w/ above elements mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24; MDS[1][i] = mY[P_10] << 0 | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24; MDS[2][i] = mX[P_20] << 0 | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24; MDS[3][i] = mX[P_30] << 0 | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24; } time = System.currentTimeMillis() - time; if (DEBUG && debuglevel > 8) { System.out.println("=========="); System.out.println(); System.out.println("Static Data"); System.out.println(); System.out.println("MDS[0][]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(MDS[0][i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("MDS[1][]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(MDS[1][i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("MDS[2][]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(MDS[2][i * 4 + j])
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -