📄 serpent.java
字号:
/* Serpent.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.security.InvalidKeyException;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;/** * <p>Serpent is a 32-round substitution-permutation network block cipher, * operating on 128-bit blocks and accepting keys of 128, 192, and 256 bits in * length. At each round the plaintext is XORed with a 128 bit portion of the * session key -- a 4224 bit key computed from the input key -- then one of * eight S-boxes are applied, and finally a simple linear transformation is * done. Decryption does the exact same thing in reverse order, and using the * eight inverses of the S-boxes.</p> * * <p>Serpent was designed by Ross Anderson, Eli Biham, and Lars Knudsen as a * proposed cipher for the Advanced Encryption Standard.</p> * * <p>Serpent can be sped up greatly by replacing S-box substitution with a * sequence of binary operations, and the optimal implementation depends * upon finding the fastest sequence of binary operations that reproduce this * substitution. This implementation uses the S-boxes discovered by * <a href="http://www.ii.uib.no/~osvik/">Dag Arne Osvik</a>, which are * optimized for the Pentium family of processors.</p> * * <p>References:</p> * * <ol> * <li><a href="http://www.cl.cam.ac.uk/~rja14/serpent.html">Serpent: A * Candidate Block Cipher for the Advanced Encryption Standard.</a></li> * </ol> */public class Serpent extends BaseCipher{ // Constants and variables // ------------------------------------------------------------------------- private static final int DEFAULT_KEY_SIZE = 16; private static final int DEFAULT_BLOCK_SIZE = 16; private static final int ROUNDS = 32; /** The fractional part of the golden ratio, (sqrt(5)+1)/2. */ private static final int PHI = 0x9e3779b9; /** * KAT vector (from ecb_vk): * I=9 * KEY=008000000000000000000000000000000000000000000000 * CT=5587B5BCB9EE5A28BA2BACC418005240 */ private static final byte[] KAT_KEY = Util.toReversedBytesFromString("008000000000000000000000000000000000000000000000"); private static final byte[] KAT_CT = Util.toReversedBytesFromString("5587B5BCB9EE5A28BA2BACC418005240"); /** caches the result of the correctness test, once executed. */ private static Boolean valid; private int x0, x1, x2, x3, x4; // Constructor(s) // ------------------------------------------------------------------------- /** Trivial zero-argument constructor. */ public Serpent() { super(Registry.SERPENT_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE); } // Class methods // ------------------------------------------------------------------------- // Instance methods // ------------------------------------------------------------------------- // java.lang.Cloneable interface implementation ---------------------------- public Object clone() { Serpent result = new Serpent(); result.currentBlockSize = this.currentBlockSize; return result; } // IBlockCipherSpi interface implementation -------------------------------- public Iterator blockSizes() { return Collections.singleton(new Integer(DEFAULT_BLOCK_SIZE)).iterator(); } public Iterator keySizes() { ArrayList keySizes = new ArrayList(); keySizes.add(new Integer(16)); keySizes.add(new Integer(24)); keySizes.add(new Integer(32)); return Collections.unmodifiableList(keySizes).iterator(); } public Object makeKey(byte[] kb, int blockSize) throws InvalidKeyException { // Not strictly true, but here to conform with the AES proposal. // This restriction can be removed if deemed necessary. if (kb.length != 16 && kb.length != 24 && kb.length != 32) { throw new InvalidKeyException("Key length is not 16, 24, or 32 bytes"); } Key key = new Key(); // Here w is our "pre-key". int[] w = new int[4 * (ROUNDS + 1)]; int i, j; for (i = 0, j = 0; i < 8 && j < kb.length; i++) { w[i] = (kb[j++] & 0xff) | (kb[j++] & 0xff) << 8 | (kb[j++] & 0xff) << 16 | (kb[j++] & 0xff) << 24; } // Pad key if < 256 bits. if (i != 8) { w[i] = 1; } // Transform using w_i-8 ... w_i-1 for (i = 8, j = 0; i < 16; i++) { int t = w[j] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ j++; w[i] = t << 11 | t >>> 21; } // Translate by 8. for (i = 0; i < 8; i++) { w[i] = w[i + 8]; } // Transform the rest of the key. for (; i < w.length; i++) { int t = w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i; w[i] = t << 11 | t >>> 21; } // After these s-boxes the pre-key (w, above) will become the // session key (key, below). sbox3(w[0], w[1], w[2], w[3]); key.k0 = x0; key.k1 = x1; key.k2 = x2; key.k3 = x3; sbox2(w[4], w[5], w[6], w[7]); key.k4 = x0; key.k5 = x1; key.k6 = x2; key.k7 = x3; sbox1(w[8], w[9], w[10], w[11]); key.k8 = x0; key.k9 = x1; key.k10 = x2; key.k11 = x3; sbox0(w[12], w[13], w[14], w[15]); key.k12 = x0; key.k13 = x1; key.k14 = x2; key.k15 = x3; sbox7(w[16], w[17], w[18], w[19]); key.k16 = x0; key.k17 = x1; key.k18 = x2; key.k19 = x3; sbox6(w[20], w[21], w[22], w[23]); key.k20 = x0; key.k21 = x1; key.k22 = x2; key.k23 = x3; sbox5(w[24], w[25], w[26], w[27]); key.k24 = x0; key.k25 = x1; key.k26 = x2; key.k27 = x3; sbox4(w[28], w[29], w[30], w[31]); key.k28 = x0; key.k29 = x1; key.k30 = x2; key.k31 = x3; sbox3(w[32], w[33], w[34], w[35]); key.k32 = x0; key.k33 = x1; key.k34 = x2; key.k35 = x3; sbox2(w[36], w[37], w[38], w[39]); key.k36 = x0; key.k37 = x1; key.k38 = x2; key.k39 = x3; sbox1(w[40], w[41], w[42], w[43]); key.k40 = x0; key.k41 = x1; key.k42 = x2; key.k43 = x3; sbox0(w[44], w[45], w[46], w[47]); key.k44 = x0; key.k45 = x1; key.k46 = x2; key.k47 = x3; sbox7(w[48], w[49], w[50], w[51]); key.k48 = x0; key.k49 = x1; key.k50 = x2; key.k51 = x3; sbox6(w[52], w[53], w[54], w[55]); key.k52 = x0; key.k53 = x1; key.k54 = x2; key.k55 = x3; sbox5(w[56], w[57], w[58], w[59]); key.k56 = x0; key.k57 = x1; key.k58 = x2; key.k59 = x3; sbox4(w[60], w[61], w[62], w[63]); key.k60 = x0; key.k61 = x1; key.k62 = x2; key.k63 = x3; sbox3(w[64], w[65], w[66], w[67]); key.k64 = x0; key.k65 = x1; key.k66 = x2; key.k67 = x3; sbox2(w[68], w[69], w[70], w[71]); key.k68 = x0; key.k69 = x1; key.k70 = x2; key.k71 = x3; sbox1(w[72], w[73], w[74], w[75]); key.k72 = x0; key.k73 = x1; key.k74 = x2; key.k75 = x3; sbox0(w[76], w[77], w[78], w[79]); key.k76 = x0; key.k77 = x1; key.k78 = x2; key.k79 = x3; sbox7(w[80], w[81], w[82], w[83]); key.k80 = x0; key.k81 = x1; key.k82 = x2; key.k83 = x3; sbox6(w[84], w[85], w[86], w[87]); key.k84 = x0; key.k85 = x1; key.k86 = x2; key.k87 = x3; sbox5(w[88], w[89], w[90], w[91]); key.k88 = x0; key.k89 = x1; key.k90 = x2; key.k91 = x3; sbox4(w[92], w[93], w[94], w[95]); key.k92 = x0; key.k93 = x1; key.k94 = x2; key.k95 = x3; sbox3(w[96], w[97], w[98], w[99]); key.k96 = x0; key.k97 = x1; key.k98 = x2; key.k99 = x3; sbox2(w[100], w[101], w[102], w[103]); key.k100 = x0; key.k101 = x1; key.k102 = x2; key.k103 = x3; sbox1(w[104], w[105], w[106], w[107]); key.k104 = x0; key.k105 = x1; key.k106 = x2; key.k107 = x3; sbox0(w[108], w[109], w[110], w[111]); key.k108 = x0; key.k109 = x1; key.k110 = x2; key.k111 = x3; sbox7(w[112], w[113], w[114], w[115]); key.k112 = x0; key.k113 = x1; key.k114 = x2; key.k115 = x3; sbox6(w[116], w[117], w[118], w[119]); key.k116 = x0; key.k117 = x1; key.k118 = x2; key.k119 = x3; sbox5(w[120], w[121], w[122], w[123]); key.k120 = x0; key.k121 = x1; key.k122 = x2; key.k123 = x3; sbox4(w[124], w[125], w[126], w[127]); key.k124 = x0; key.k125 = x1; key.k126 = x2; key.k127 = x3; sbox3(w[128], w[129], w[130], w[131]); key.k128 = x0; key.k129 = x1; key.k130 = x2; key.k131 = x3; return key; } public synchronized void encrypt(byte[] in, int i, byte[] out, int o, Object K, int bs) { Key key = (Key) K; x0 = (in[i] & 0xff) | (in[i + 1] & 0xff) << 8 | (in[i + 2] & 0xff) << 16 | (in[i + 3] & 0xff) << 24; x1 = (in[i + 4] & 0xff) | (in[i + 5] & 0xff) << 8 | (in[i + 6] & 0xff) << 16 | (in[i + 7] & 0xff) << 24; x2 = (in[i + 8] & 0xff) | (in[i + 9] & 0xff) << 8 | (in[i + 10] & 0xff) << 16 | (in[i + 11] & 0xff) << 24; x3 = (in[i + 12] & 0xff) | (in[i + 13] & 0xff) << 8 | (in[i + 14] & 0xff) << 16 | (in[i + 15] & 0xff) << 24; x0 ^= key.k0; x1 ^= key.k1; x2 ^= key.k2; x3 ^= key.k3; sbox0(); x1 ^= key.k4; x4 ^= key.k5; x2 ^= key.k6; x0 ^= key.k7; sbox1(); x0 ^= key.k8; x4 ^= key.k9; x2 ^= key.k10; x1 ^= key.k11; sbox2(); x2 ^= key.k12; x1 ^= key.k13; x4 ^= key.k14; x3 ^= key.k15; sbox3(); x1 ^= key.k16; x4 ^= key.k17; x3 ^= key.k18; x0 ^= key.k19; sbox4(); x4 ^= key.k20; x2 ^= key.k21; x1 ^= key.k22; x0 ^= key.k23; sbox5(); x2 ^= key.k24; x0 ^= key.k25; x4 ^= key.k26; x1 ^= key.k27; sbox6(); x2 ^= key.k28; x0 ^= key.k29; x3 ^= key.k30; x4 ^= key.k31; sbox7(); x0 = x3; x3 = x2; x2 = x4; x0 ^= key.k32; x1 ^= key.k33; x2 ^= key.k34; x3 ^= key.k35; sbox0(); x1 ^= key.k36; x4 ^= key.k37; x2 ^= key.k38; x0 ^= key.k39; sbox1(); x0 ^= key.k40; x4 ^= key.k41; x2 ^= key.k42; x1 ^= key.k43; sbox2(); x2 ^= key.k44; x1 ^= key.k45; x4 ^= key.k46; x3 ^= key.k47; sbox3(); x1 ^= key.k48; x4 ^= key.k49; x3 ^= key.k50; x0 ^= key.k51; sbox4(); x4 ^= key.k52; x2 ^= key.k53; x1 ^= key.k54; x0 ^= key.k55; sbox5(); x2 ^= key.k56; x0 ^= key.k57; x4 ^= key.k58; x1 ^= key.k59; sbox6(); x2 ^= key.k60; x0 ^= key.k61; x3 ^= key.k62; x4 ^= key.k63; sbox7(); x0 = x3; x3 = x2; x2 = x4; x0 ^= key.k64; x1 ^= key.k65; x2 ^= key.k66; x3 ^= key.k67; sbox0(); x1 ^= key.k68; x4 ^= key.k69; x2 ^= key.k70; x0 ^= key.k71; sbox1(); x0 ^= key.k72; x4 ^= key.k73; x2 ^= key.k74; x1 ^= key.k75; sbox2(); x2 ^= key.k76; x1 ^= key.k77; x4 ^= key.k78; x3 ^= key.k79; sbox3(); x1 ^= key.k80; x4 ^= key.k81; x3 ^= key.k82; x0 ^= key.k83; sbox4(); x4 ^= key.k84; x2 ^= key.k85; x1 ^= key.k86; x0 ^= key.k87; sbox5(); x2 ^= key.k88; x0 ^= key.k89; x4 ^= key.k90; x1 ^= key.k91; sbox6(); x2 ^= key.k92; x0 ^= key.k93; x3 ^= key.k94; x4 ^= key.k95; sbox7(); x0 = x3; x3 = x2; x2 = x4; x0 ^= key.k96; x1 ^= key.k97; x2 ^= key.k98; x3 ^= key.k99; sbox0(); x1 ^= key.k100; x4 ^= key.k101; x2 ^= key.k102; x0 ^= key.k103; sbox1(); x0 ^= key.k104; x4 ^= key.k105; x2 ^= key.k106; x1 ^= key.k107; sbox2(); x2 ^= key.k108; x1 ^= key.k109; x4 ^= key.k110; x3 ^= key.k111; sbox3(); x1 ^= key.k112; x4 ^= key.k113; x3 ^= key.k114; x0 ^= key.k115; sbox4(); x4 ^= key.k116; x2 ^= key.k117; x1 ^= key.k118; x0 ^= key.k119; sbox5(); x2 ^= key.k120; x0 ^= key.k121; x4 ^= key.k122; x1 ^= key.k123; sbox6(); x2 ^= key.k124; x0 ^= key.k125; x3 ^= key.k126; x4 ^= key.k127; sbox7noLT(); x0 = x3; x3 = x2; x2 = x4; x0 ^= key.k128; x1 ^= key.k129; x2 ^= key.k130; x3 ^= key.k131; out[o] = (byte) x0; out[o + 1] = (byte) (x0 >>> 8); out[o + 2] = (byte) (x0 >>> 16); out[o + 3] = (byte) (x0 >>> 24); out[o + 4] = (byte) x1; out[o + 5] = (byte) (x1 >>> 8); out[o + 6] = (byte) (x1 >>> 16); out[o + 7] = (byte) (x1 >>> 24); out[o + 8] = (byte) x2; out[o + 9] = (byte) (x2 >>> 8); out[o + 10] = (byte) (x2 >>> 16); out[o + 11] = (byte) (x2 >>> 24); out[o + 12] = (byte) x3; out[o + 13] = (byte) (x3 >>> 8); out[o + 14] = (byte) (x3 >>> 16); out[o + 15] = (byte) (x3 >>> 24); } public synchronized void decrypt(byte[] in, int i, byte[] out, int o, Object K, int bs) { Key key = (Key) K; x0 = (in[i] & 0xff) | (in[i + 1] & 0xff) << 8 | (in[i + 2] & 0xff) << 16 | (in[i + 3] & 0xff) << 24; x1 = (in[i + 4] & 0xff) | (in[i + 5] & 0xff) << 8 | (in[i + 6] & 0xff) << 16 | (in[i + 7] & 0xff) << 24; x2 = (in[i + 8] & 0xff) | (in[i + 9] & 0xff) << 8 | (in[i + 10] & 0xff) << 16 | (in[i + 11] & 0xff) << 24; x3 = (in[i + 12] & 0xff) | (in[i + 13] & 0xff) << 8 | (in[i + 14] & 0xff) << 16 | (in[i + 15] & 0xff) << 24; x0 ^= key.k128; x1 ^= key.k129; x2 ^= key.k130; x3 ^= key.k131; sboxI7noLT(); x3 ^= key.k124; x0 ^= key.k125; x1 ^= key.k126; x4 ^= key.k127; sboxI6(); x0 ^= key.k120; x1 ^= key.k121; x2 ^= key.k122; x4 ^= key.k123; sboxI5(); x1 ^= key.k116; x3 ^= key.k117; x4 ^= key.k118; x2 ^= key.k119; sboxI4(); x1 ^= key.k112; x2 ^= key.k113; x4 ^= key.k114; x0 ^= key.k115; sboxI3(); x0 ^= key.k108; x1 ^= key.k109;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -