⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 square.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Square.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>Square is a 128-bit key, 128-bit block cipher algorithm developed by Joan * Daemen, Lars Knudsen and Vincent Rijmen.</p> * * <p>References:</p> * * <ol> *    <li><a href="http://www.esat.kuleuven.ac.be/~rijmen/square/">The block *    cipher Square</a>.<br> *    <a href="mailto:daemen.j@protonworld.com">Joan Daemen</a>, *    <a href="mailto:lars.knudsen@esat.kuleuven.ac.be">Lars Knudsen</a> and *    <a href="mailto:vincent.rijmen@esat.kuleuven.ac.be">Vincent Rijmen</a>.</li> * </ol> * * @version $Revision: 1.1 $ */public final class Square extends BaseCipher{  // 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 ROUNDS = 8;  private static final int ROOT = 0x1F5; // for generating GF(2**8)  private static final int[] OFFSET = new int[ROUNDS];  private static final String Sdata = "\uB1CE\uC395\u5AAD\uE702\u4D44\uFB91\u0C87\uA150"                                      + "\uCB67\u54DD\u468F\uE14E\uF0FD\uFCEB\uF9C4\u1A6E"                                      + "\u5EF5\uCC8D\u1C56\u43FE\u0761\uF875\u59FF\u0322"                                      + "\u8AD1\u13EE\u8800\u0E34\u1580\u94E3\uEDB5\u5323"                                      + "\u4B47\u17A7\u9035\uABD8\uB8DF\u4F57\u9A92\uDB1B"                                      + "\u3CC8\u9904\u8EE0\uD77D\u85BB\u402C\u3A45\uF142"                                      + "\u6520\u4118\u7225\u9370\u3605\uF20B\uA379\uEC08"                                      + "\u2731\u32B6\u7CB0\u0A73\u5B7B\uB781\uD20D\u6A26"                                      + "\u9E58\u9C83\u74B3\uAC30\u7A69\u770F\uAE21\uDED0"                                      + "\u2E97\u10A4\u98A8\uD468\u2D62\u296D\u1649\u76C7"                                      + "\uE8C1\u9637\uE5CA\uF4E9\u6312\uC2A6\u14BC\uD328"                                      + "\uAF2F\uE624\u52C6\uA009\uBD8C\uCF5D\u115F\u01C5"                                      + "\u9F3D\uA29B\uC93B\uBE51\u191F\u3F5C\uB2EF\u4ACD"                                      + "\uBFBA\u6F64\uD9F3\u3EB4\uAADC\uD506\uC07E\uF666"                                      + "\u6C84\u7138\uB91D\u7F9D\u488B\u2ADA\uA533\u8239"                                      + "\uD678\u86FA\uE42B\uA91E\u8960\u6BEA\u554C\uF7E2";  /** Substitution boxes for encryption and decryption. */  private static final byte[] Se = new byte[256];  private static final byte[] Sd = new byte[256];  /** Transposition boxes for encryption and decryption. */  private static final int[] Te = new int[256];  private static final int[] Td = new int[256];  /**   * KAT vector (from ecb_vk):   * I=87   * KEY=00000000000000000000020000000000   * CT=A9DF031B4E25E89F527EFFF89CB0BEBA   */  private static final byte[] KAT_KEY = Util.toBytesFromString("00000000000000000000020000000000");  private static final byte[] KAT_CT = Util.toBytesFromString("A9DF031B4E25E89F527EFFF89CB0BEBA");  /** caches the result of the correctness test, once executed. */  private static Boolean valid;  // Static code - to intialise lookup tables  // -------------------------------------------------------------------------  static    {      int i, j;      /*       // Generate exp and log tables used in multiplication over GF(2 ** m)       byte[] exp = new byte[256];       byte[] log = new byte[256];       exp[0] = 1;       for (i = 1; i < 256; i++) {       j = exp[i - 1] << 1;       if ((j & 0x100) != 0) {       j ^= ROOT; // reduce j (mod ROOT)       }       exp[i] = (byte) j;       log[j & 0xFF] = (byte) i;       }       // Compute the substitution box Se[] and its inverse Sd[] based on       // F(x) = x**{-1} plus affine transform of the output.       Se[0] = 0;       Se[1] = 1;       for (i = 2; i < 256; i++) {       Se[i] = exp[(255 - log[i]) & 0xFF];       }       // Let Se[i] be represented as an 8-row vector V over GF(2); the affine       // transformation is A * V + T, where the rows of the 8 x 8 matrix A are       // contained in trans[0]...trans[7] and the 8-row vector T is contained       // in 0xB1.       int[] trans = new int[] {0x01, 0x03, 0x05, 0x0F, 0x1F, 0x3D, 0x7B, 0xD6};       int u, v;       for (i = 0; i < 256; i++) {       v = 0xB1;                        // affine part of the transform       for (j = 0; j < 8; j++) {       u = Se[i] & trans[j] & 0xFF; // column-wise mult. over GF(2)       u ^= u >>> 4;                // sum of all bits of u over GF(2)       u ^= u >>> 2;       u ^= u >>> 1;       u &= 1;       v ^= u << j;                 // row alignment of the result       }       Se[i] = (byte) v;       Sd[v] = (byte) i;                // inverse substitution box       }       System.out.println("Se="+Util.toUnicodeString(Se));       System.out.println("Sd="+Util.toUnicodeString(Sd));       */      /**/      // re-construct Se box values      int limit = Sdata.length();      char c1;      for (i = 0, j = 0; i < limit; i++)        {          c1 = Sdata.charAt(i);          Se[j++] = (byte) (c1 >>> 8);          Se[j++] = (byte) c1;        }      // compute Sd box values      for (i = 0; i < 256; i++)        {          Sd[Se[i] & 0xFF] = (byte) i;        }      // generate OFFSET values      OFFSET[0] = 1;      for (i = 1; i < ROUNDS; i++)        {          OFFSET[i] = mul(OFFSET[i - 1], 2);          OFFSET[i - 1] <<= 24;        }      OFFSET[ROUNDS - 1] <<= 24;      // generate Te and Td boxes if we're not reading their values      // Notes:      // (1) The function mul() computes the product of two elements of GF(2**8)      // with ROOT as reduction polynomial.      // (2) the values used in computing the Te and Td are the GF(2**8)      // coefficients of the diffusion polynomial c(x) and its inverse      // (modulo x**4 + 1) d(x), defined in sections 2.1 and 4 of the Square      // paper.      for (i = 0; i < 256; i++)        {          j = Se[i] & 0xFF;          Te[i] = (Se[i & 3] == 0) ? 0 : mul(j, 2) << 24 | j << 16 | j << 8                                         | mul(j, 3);          j = Sd[i] & 0xFF;          Td[i] = (Sd[i & 3] == 0) ? 0 : mul(j, 14) << 24 | mul(j, 9) << 16                                         | mul(j, 13) << 8 | mul(j, 11);        }      /**/    }  // Constructor(s)  // -------------------------------------------------------------------------  /** Trivial 0-arguments constructor. */  public Square()  {    super(Registry.SQUARE_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE);  }  // Class methods  // -------------------------------------------------------------------------  private static void square(byte[] in, int i, byte[] out, int j, int[][] K,                             int[] T, byte[] S)  {    int a = ((in[i++]) << 24 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF))            ^ K[0][0];    int b = ((in[i++]) << 24 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF))            ^ K[0][1];    int c = ((in[i++]) << 24 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF))            ^ K[0][2];    int d = ((in[i++]) << 24 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 8 | (in[i] & 0xFF))            ^ K[0][3];    int r, aa, bb, cc, dd;    for (r = 1; r < ROUNDS; r++)      { // R - 1 full rounds        aa = T[(a >>> 24)] ^ rot32R(T[(b >>> 24)], 8)             ^ rot32R(T[(c >>> 24)], 16) ^ rot32R(T[(d >>> 24)], 24) ^ K[r][0];        bb = T[(a >>> 16) & 0xFF] ^ rot32R(T[(b >>> 16) & 0xFF], 8)             ^ rot32R(T[(c >>> 16) & 0xFF], 16)             ^ rot32R(T[(d >>> 16) & 0xFF], 24) ^ K[r][1];        cc = T[(a >>> 8) & 0xFF] ^ rot32R(T[(b >>> 8) & 0xFF], 8)             ^ rot32R(T[(c >>> 8) & 0xFF], 16)             ^ rot32R(T[(d >>> 8) & 0xFF], 24) ^ K[r][2];        dd = T[a & 0xFF] ^ rot32R(T[b & 0xFF], 8) ^ rot32R(T[c & 0xFF], 16)             ^ rot32R(T[d & 0xFF], 24) ^ K[r][3];        a = aa;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -