jdk/src/share/classes/sun/jkernel/StandaloneSHA.java
changeset 3111 fefdeafb7ab9
child 5506 202f599c92aa
equal deleted inserted replaced
2790:e9771c308d06 3111:fefdeafb7ab9
       
     1 /*
       
     2  * Copyright 2008 - 2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.jkernel;
       
    27 
       
    28 import static sun.jkernel.StandaloneByteArrayAccess.*;
       
    29 
       
    30 /**
       
    31  * This is a slightly modified subset of the
       
    32  * <code>sun.security.provider.SHA</code> class that
       
    33  * is not dependent on the regular Java Security framework classes. It
       
    34  * implements the Secure Hash Algorithm (SHA-1) developed by
       
    35  * the National Institute of Standards and Technology along with the
       
    36  * National Security Agency.  This is the updated version of SHA
       
    37  * fip-180 as superseded by fip-180-1.
       
    38  * <p>
       
    39  * The <code>sun.security.provider.SHA.clonde()</code> method is not
       
    40  * implemented and other, formerly public methods, are package private.
       
    41  *
       
    42  */
       
    43 final class StandaloneSHA extends StandaloneMessageDigest {
       
    44 
       
    45     static final boolean debug = false;
       
    46 
       
    47     // Buffer of int's and count of characters accumulated
       
    48     // 64 bytes are included in each hash block so the low order
       
    49     // bits of count are used to know how to pack the bytes into ints
       
    50     // and to know when to compute the block and start the next one.
       
    51     private final int[] W;
       
    52 
       
    53     // state of this
       
    54     private final int[] state;
       
    55 
       
    56     /**
       
    57      * Creates a new StandaloneSHA object.
       
    58      */
       
    59     StandaloneSHA() {
       
    60         super("SHA-1", 20, 64);
       
    61         state = new int[5];
       
    62         W = new int[80];
       
    63         implReset();
       
    64     }
       
    65 
       
    66     /**
       
    67      * Resets the buffers and hash value to start a new hash.
       
    68      */
       
    69     void implReset() {
       
    70         if (debug) {
       
    71             System.out.print("StandaloneSHA.implR: " );
       
    72         }
       
    73         state[0] = 0x67452301;
       
    74         state[1] = 0xefcdab89;
       
    75         state[2] = 0x98badcfe;
       
    76         state[3] = 0x10325476;
       
    77         state[4] = 0xc3d2e1f0;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Computes the final hash and copies the 20 bytes to the output array.
       
    82      */
       
    83     void implDigest(byte[] out, int ofs) {
       
    84         if (debug) {
       
    85             System.out.print("StandaloneSHA.implD: " );
       
    86         }
       
    87         long bitsProcessed = bytesProcessed << 3;
       
    88 
       
    89         int index = (int)bytesProcessed & 0x3f;
       
    90         int padLen = (index < 56) ? (56 - index) : (120 - index);
       
    91 
       
    92         engineUpdate(padding, 0, padLen);
       
    93 
       
    94         // System.out.println("Inserting: " + bitsProcessed);
       
    95         StandaloneByteArrayAccess.i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);
       
    96         StandaloneByteArrayAccess.i2bBig4((int)bitsProcessed, buffer, 60);
       
    97         implCompress(buffer, 0);
       
    98 
       
    99         StandaloneByteArrayAccess.i2bBig(state, 0, out, ofs, 20);
       
   100     }
       
   101 
       
   102     // Constants for each round
       
   103     private final static int round1_kt = 0x5a827999;
       
   104     private final static int round2_kt = 0x6ed9eba1;
       
   105     private final static int round3_kt = 0x8f1bbcdc;
       
   106     private final static int round4_kt = 0xca62c1d6;
       
   107 
       
   108     /**
       
   109      * Compute a the hash for the current block.
       
   110      *
       
   111      * This is in the same vein as Peter Gutmann's algorithm listed in
       
   112      * the back of Applied Cryptography, Compact implementation of
       
   113      * "old" NIST Secure Hash Algorithm.
       
   114      */
       
   115     void implCompress(byte[] buf, int ofs) {
       
   116 
       
   117         if (debug) {
       
   118             System.out.print("StandaloneSHA.implC: " );
       
   119             for (int i=ofs; i<buf.length; i++) {
       
   120                 System.out.format("%02X",buf[i]);
       
   121             }
       
   122             System.out.println();
       
   123         }
       
   124 
       
   125         StandaloneByteArrayAccess.b2iBig(buf, ofs, W, 0, 64);
       
   126 
       
   127         // The first 16 ints have the byte stream, compute the rest of
       
   128         // the buffer
       
   129         for (int t = 16; t <= 79; t++) {
       
   130             int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
       
   131             W[t] = (temp << 1) | (temp >>> 31);
       
   132         }
       
   133 
       
   134         int a = state[0];
       
   135         int b = state[1];
       
   136         int c = state[2];
       
   137         int d = state[3];
       
   138         int e = state[4];
       
   139 
       
   140         // Round 1
       
   141         for (int i = 0; i < 20; i++) {
       
   142             int temp = ((a<<5) | (a>>>(32-5))) +
       
   143                 ((b&c)|((~b)&d))+ e + W[i] + round1_kt;
       
   144             e = d;
       
   145             d = c;
       
   146             c = ((b<<30) | (b>>>(32-30)));
       
   147             b = a;
       
   148             a = temp;
       
   149         }
       
   150 
       
   151         // Round 2
       
   152         for (int i = 20; i < 40; i++) {
       
   153             int temp = ((a<<5) | (a>>>(32-5))) +
       
   154                 (b ^ c ^ d) + e + W[i] + round2_kt;
       
   155             e = d;
       
   156             d = c;
       
   157             c = ((b<<30) | (b>>>(32-30)));
       
   158             b = a;
       
   159             a = temp;
       
   160         }
       
   161 
       
   162         // Round 3
       
   163         for (int i = 40; i < 60; i++) {
       
   164             int temp = ((a<<5) | (a>>>(32-5))) +
       
   165                 ((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;
       
   166             e = d;
       
   167             d = c;
       
   168             c = ((b<<30) | (b>>>(32-30)));
       
   169             b = a;
       
   170             a = temp;
       
   171         }
       
   172 
       
   173         // Round 4
       
   174         for (int i = 60; i < 80; i++) {
       
   175             int temp = ((a<<5) | (a>>>(32-5))) +
       
   176                 (b ^ c ^ d) + e + W[i] + round4_kt;
       
   177             e = d;
       
   178             d = c;
       
   179             c = ((b<<30) | (b>>>(32-30)));
       
   180             b = a;
       
   181             a = temp;
       
   182         }
       
   183         state[0] += a;
       
   184         state[1] += b;
       
   185         state[2] += c;
       
   186         state[3] += d;
       
   187         state[4] += e;
       
   188     }
       
   189 
       
   190 }