Showing the changes to java/pkg/sun/security/provider/SHA.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,187 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.security.provider; | |
| 27 | + | |
| 28 | +import static sun.security.provider.ByteArrayAccess.*; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * This class implements the Secure Hash Algorithm (SHA) developed by | |
| 32 | + * the National Institute of Standards and Technology along with the | |
| 33 | + * National Security Agency. This is the updated version of SHA | |
| 34 | + * fip-180 as superseded by fip-180-1. | |
| 35 | + * | |
| 36 | + * <p>It implement JavaSecurity MessageDigest, and can be used by in | |
| 37 | + * the Java Security framework, as a pluggable implementation, as a | |
| 38 | + * filter for the digest stream classes. | |
| 39 | + * | |
| 40 | + * @author Roger Riggs | |
| 41 | + * @author Benjamin Renaud | |
| 42 | + * @author Andreas Sterbenz | |
| 43 | + */ | |
| 44 | +public final class SHA extends DigestBase { | |
| 45 | + | |
| 46 | + // Buffer of int's and count of characters accumulated | |
| 47 | + // 64 bytes are included in each hash block so the low order | |
| 48 | + // bits of count are used to know how to pack the bytes into ints | |
| 49 | + // and to know when to compute the block and start the next one. | |
| 50 | + private final int[] W; | |
| 51 | + | |
| 52 | + // state of this | |
| 53 | + private final int[] state; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Creates a new SHA object. | |
| 57 | + */ | |
| 58 | + public SHA() { | |
| 59 | + super("SHA-1", 20, 64); | |
| 60 | + state = new int[5]; | |
| 61 | + W = new int[80]; | |
| 62 | + implReset(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Creates a SHA object.with state (for cloning) */ | |
| 67 | + private SHA(SHA base) { | |
| 68 | + super(base); | |
| 69 | + this.state = base.state.clone(); | |
| 70 | + this.W = new int[80]; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /* | |
| 74 | + * Clones this object. | |
| 75 | + */ | |
| 76 | + public Object clone() { | |
| 77 | + return new SHA(this); | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Resets the buffers and hash value to start a new hash. | |
| 82 | + */ | |
| 83 | + void implReset() { | |
| 84 | + state[0] = 0x67452301; | |
| 85 | + state[1] = 0xefcdab89; | |
| 86 | + state[2] = 0x98badcfe; | |
| 87 | + state[3] = 0x10325476; | |
| 88 | + state[4] = 0xc3d2e1f0; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Computes the final hash and copies the 20 bytes to the output array. | |
| 93 | + */ | |
| 94 | + void implDigest(byte[] out, int ofs) { | |
| 95 | + long bitsProcessed = bytesProcessed << 3; | |
| 96 | + | |
| 97 | + int index = (int)bytesProcessed & 0x3f; | |
| 98 | + int padLen = (index < 56) ? (56 - index) : (120 - index); | |
| 99 | + engineUpdate(padding, 0, padLen); | |
| 100 | + | |
| 101 | + i2bBig4((int)(bitsProcessed >>> 32), buffer, 56); | |
| 102 | + i2bBig4((int)bitsProcessed, buffer, 60); | |
| 103 | + implCompress(buffer, 0); | |
| 104 | + | |
| 105 | + i2bBig(state, 0, out, ofs, 20); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Constants for each round | |
| 109 | + private final static int round1_kt = 0x5a827999; | |
| 110 | + private final static int round2_kt = 0x6ed9eba1; | |
| 111 | + private final static int round3_kt = 0x8f1bbcdc; | |
| 112 | + private final static int round4_kt = 0xca62c1d6; | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Compute a the hash for the current block. | |
| 116 | + * | |
| 117 | + * This is in the same vein as Peter Gutmann's algorithm listed in | |
| 118 | + * the back of Applied Cryptography, Compact implementation of | |
| 119 | + * "old" NIST Secure Hash Algorithm. | |
| 120 | + */ | |
| 121 | + void implCompress(byte[] buf, int ofs) { | |
| 122 | + b2iBig64(buf, ofs, W); | |
| 123 | + | |
| 124 | + // The first 16 ints have the byte stream, compute the rest of | |
| 125 | + // the buffer | |
| 126 | + for (int t = 16; t <= 79; t++) { | |
| 127 | + int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; | |
| 128 | + W[t] = (temp << 1) | (temp >>> 31); | |
| 129 | + } | |
| 130 | + | |
| 131 | + int a = state[0]; | |
| 132 | + int b = state[1]; | |
| 133 | + int c = state[2]; | |
| 134 | + int d = state[3]; | |
| 135 | + int e = state[4]; | |
| 136 | + | |
| 137 | + // Round 1 | |
| 138 | + for (int i = 0; i < 20; i++) { | |
| 139 | + int temp = ((a<<5) | (a>>>(32-5))) + | |
| 140 | + ((b&c)|((~b)&d))+ e + W[i] + round1_kt; | |
| 141 | + e = d; | |
| 142 | + d = c; | |
| 143 | + c = ((b<<30) | (b>>>(32-30))); | |
| 144 | + b = a; | |
| 145 | + a = temp; | |
| 146 | + } | |
| 147 | + | |
| 148 | + // Round 2 | |
| 149 | + for (int i = 20; i < 40; i++) { | |
| 150 | + int temp = ((a<<5) | (a>>>(32-5))) + | |
| 151 | + (b ^ c ^ d) + e + W[i] + round2_kt; | |
| 152 | + e = d; | |
| 153 | + d = c; | |
| 154 | + c = ((b<<30) | (b>>>(32-30))); | |
| 155 | + b = a; | |
| 156 | + a = temp; | |
| 157 | + } | |
| 158 | + | |
| 159 | + // Round 3 | |
| 160 | + for (int i = 40; i < 60; i++) { | |
| 161 | + int temp = ((a<<5) | (a>>>(32-5))) + | |
| 162 | + ((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt; | |
| 163 | + e = d; | |
| 164 | + d = c; | |
| 165 | + c = ((b<<30) | (b>>>(32-30))); | |
| 166 | + b = a; | |
| 167 | + a = temp; | |
| 168 | + } | |
| 169 | + | |
| 170 | + // Round 4 | |
| 171 | + for (int i = 60; i < 80; i++) { | |
| 172 | + int temp = ((a<<5) | (a>>>(32-5))) + | |
| 173 | + (b ^ c ^ d) + e + W[i] + round4_kt; | |
| 174 | + e = d; | |
| 175 | + d = c; | |
| 176 | + c = ((b<<30) | (b>>>(32-30))); | |
| 177 | + b = a; | |
| 178 | + a = temp; | |
| 179 | + } | |
| 180 | + state[0] += a; | |
| 181 | + state[1] += b; | |
| 182 | + state[2] += c; | |
| 183 | + state[3] += d; | |
| 184 | + state[4] += e; | |
| 185 | + } | |
| 186 | + | |
| 187 | +} | |