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

📄 uhash32.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* UHash32.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.mac;import gnu.java.security.prng.IRandom;import gnu.java.security.prng.LimitReachedException;import gnu.javax.crypto.cipher.IBlockCipher;import gnu.javax.crypto.prng.UMacGenerator;import java.io.ByteArrayOutputStream;import java.math.BigInteger;import java.security.InvalidKeyException;import java.util.HashMap;import java.util.Map;/** * <p><i>UHASH</i> is a keyed hash function, which takes as input a string of * arbitrary length, and produces as output a string of fixed length (such as 8 * bytes). The actual output length depends on the parameter UMAC-OUTPUT-LEN.</p> * * <p><i>UHASH</i> has been shown to be <i>epsilon-ASU</i> ("Almost Strongly * Universal"), where epsilon is a small (parameter-dependent) real number. * Informally, saying that a keyed hash function is <i>epsilon-ASU</i> means * that for any two distinct fixed input strings, the two outputs of the hash * function with a random key "look almost like a pair of random strings". The * number epsilon measures how non-random the output strings may be.</p> * * <i>UHASH</i> has been designed to be fast by exploiting several architectural * features of modern commodity processors. It was specifically designed for use * in <i>UMAC</i>. But <i>UHASH</i> is useful beyond that domain, and can be * easily adopted for other purposes.</p> * * <i>UHASH</i> does its work in three layers. First, a hash function called * <code>NH</code> is used to compress input messages into strings which are * typically many times smaller than the input message. Second, the compressed * message is hashed with an optimized <i>polynomial hash function</i> into a * fixed-length 16-byte string. Finally, the 16-byte string is hashed using an * <i>inner-product hash</i> into a string of length WORD-LEN bytes. These three * layers are repeated (with a modified key) until the outputs total * UMAC-OUTPUT-LEN bytes.</p> * * <p>References:</p> * * <ol> *    <li><a href="http://www.ietf.org/internet-drafts/draft-krovetz-umac-01.txt"> *    UMAC</a>: Message Authentication Code using Universal Hashing.<br> *    T. Krovetz, J. Black, S. Halevi, A. Hevia, H. Krawczyk, and P. Rogaway.</li> * </ol> */public class UHash32 extends BaseMac{  // Constants and variables  // -------------------------------------------------------------------------  // UMAC prime values  private static final BigInteger PRIME_19 = BigInteger.valueOf(0x7FFFFL);  private static final BigInteger PRIME_32 = BigInteger.valueOf(0xFFFFFFFBL);  private static final BigInteger PRIME_36 = BigInteger.valueOf(0xFFFFFFFFBL);  private static final BigInteger PRIME_64 = new BigInteger(                                                            1,                                                            new byte[] {                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xFF,                                                                        (byte) 0xC5 });  private static final BigInteger PRIME_128 = new BigInteger(                                                             1,                                                             new byte[] {                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0xFF,                                                                         (byte) 0x61 });  static final BigInteger TWO = BigInteger.valueOf(2L);  static final long BOUNDARY = TWO.shiftLeft(17).longValue();  // 2**64 - 2**32  static final BigInteger LOWER_RANGE = TWO.pow(64).subtract(TWO.pow(32));  // 2**128 - 2**96  static final BigInteger UPPER_RANGE = TWO.pow(128).subtract(TWO.pow(96));  static final byte[] ALL_ZEROES = new byte[32];  int streams;  L1Hash32[] l1hash;  // Constructor(s)  // -------------------------------------------------------------------------  /** Trivial 0-arguments constructor. */  public UHash32()  {    super("uhash32");  }  /**   * <p>Private constructor for cloning purposes.</p>   *   * @param that the instance to clone.   */  private UHash32(UHash32 that)  {    this();    this.streams = that.streams;    if (that.l1hash != null)      {        //         this.l1hash = new L1Hash32[that.l1hash.length];        this.l1hash = new L1Hash32[that.streams];        //         for (int i = 0; i < that.l1hash.length; i++) {        for (int i = 0; i < that.streams; i++)          {            if (that.l1hash[i] != null)              {                this.l1hash[i] = (L1Hash32) that.l1hash[i].clone();              }          }      }  }  // Class methods  // -------------------------------------------------------------------------  /**   * <p>The prime numbers used in UMAC are:</p>   * <pre>   *   +-----+--------------------+---------------------------------------+   *   |  x  | prime(x) [Decimal] | prime(x) [Hexadecimal]                |   *   +-----+--------------------+---------------------------------------+   *   | 19  | 2^19  - 1          | 0x0007FFFF                            |   *   | 32  | 2^32  - 5          | 0xFFFFFFFB                            |   *   | 36  | 2^36  - 5          | 0x0000000F FFFFFFFB                   |   *   | 64  | 2^64  - 59         | 0xFFFFFFFF FFFFFFC5                   |   *   | 128 | 2^128 - 159        | 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFF61 |   *   +-----+--------------------+---------------------------------------+   *</pre>   *   * @param n a number of bits.   * @return the largest prime number less than 2**n.   */  static final BigInteger prime(int n)  {    switch (n)      {      case 19:        return PRIME_19;      case 32:        return PRIME_32;      case 36:        return PRIME_36;      case 64:        return PRIME_64;      case 128:        return PRIME_128;      default:        throw new IllegalArgumentException("Undefined prime("                                           + String.valueOf(n) + ")");      }  }  // Instance methods  // -------------------------------------------------------------------------  // java.lang.Cloneable interface implementation ----------------------------  public Object clone()  {    return new UHash32(this);  }  // gnu.crypto.mac.IMac interface implementation ----------------------------  public int macSize()  {    return UMac32.OUTPUT_LEN;  }  public void init(Map attributes) throws InvalidKeyException,      IllegalStateException  {    byte[] K = (byte[]) attributes.get(MAC_KEY_MATERIAL);    if (K == null)      {        throw new InvalidKeyException("Null Key");      }    if (K.length != UMac32.KEY_LEN)      {        throw new InvalidKeyException("Invalid Key length: "                                      + String.valueOf(K.length));      }    // Calculate iterations needed to make UMAC-OUTPUT-LEN bytes    streams = (UMac32.OUTPUT_LEN + 3) / 4;    // Define total key needed for all iterations using UMacGenerator.    // L1Key and L3Key1 both reuse most key between iterations.    IRandom kdf1 = new UMacGenerator();    IRandom kdf2 = new UMacGenerator();    IRandom kdf3 = new UMacGenerator();    IRandom kdf4 = new UMacGenerator();    Map map = new HashMap();    map.put(IBlockCipher.KEY_MATERIAL, K);    map.put(UMacGenerator.INDEX, new Integer(0));    kdf1.init(map);    map.put(UMacGenerator.INDEX, new Integer(1));    kdf2.init(map);    map.put(UMacGenerator.INDEX, new Integer(2));    kdf3.init(map);    map.put(UMacGenerator.INDEX, new Integer(3));    kdf4.init(map);    // need to generate all bytes for use later in a Toepliz construction    byte[] L1Key = new byte[UMac32.L1_KEY_LEN + (streams - 1) * 16];    try      {        kdf1.nextBytes(L1Key, 0, L1Key.length);      }    catch (LimitReachedException x)      {        x.printStackTrace(System.err);        throw new RuntimeException("KDF for L1Key reached limit");      }    l1hash = new L1Hash32[streams];    for (int i = 0; i < streams; i++)      {        byte[] k1 = new byte[UMac32.L1_KEY_LEN];        System.arraycopy(L1Key, i * 16, k1, 0, UMac32.L1_KEY_LEN);        byte[] k2 = new byte[24];        try          {            kdf2.nextBytes(k2, 0, 24);          }        catch (LimitReachedException x)          {            x.printStackTrace(System.err);            throw new RuntimeException("KDF for L2Key reached limit");          }        byte[] k31 = new byte[64];        try          {            kdf3.nextBytes(k31, 0, 64);          }        catch (LimitReachedException x)          {            x.printStackTrace(System.err);            throw new RuntimeException("KDF for L3Key1 reached limit");          }        byte[] k32 = new byte[4];        try          {            kdf4.nextBytes(k32, 0, 4);          }        catch (LimitReachedException x)          {            x.printStackTrace(System.err);

⌨️ 快捷键说明

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