Path: blob/master/src/java.base/share/classes/sun/security/provider/SHA.java
41159 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.util.Arrays;28import java.util.Objects;2930import static sun.security.provider.ByteArrayAccess.*;31import jdk.internal.vm.annotation.IntrinsicCandidate;3233/**34* This class implements the Secure Hash Algorithm (SHA) developed by35* the National Institute of Standards and Technology along with the36* National Security Agency. This is the updated version of SHA37* fip-180 as superseded by fip-180-1.38*39* <p>It implement JavaSecurity MessageDigest, and can be used by in40* the Java Security framework, as a pluggable implementation, as a41* filter for the digest stream classes.42*43* @author Roger Riggs44* @author Benjamin Renaud45* @author Andreas Sterbenz46*/47public final class SHA extends DigestBase {4849// Buffer of int's and count of characters accumulated50// 64 bytes are included in each hash block so the low order51// bits of count are used to know how to pack the bytes into ints52// and to know when to compute the block and start the next one.53private int[] W;5455// state of this56private int[] state;5758/**59* Creates a new SHA object.60*/61public SHA() {62super("SHA-1", 20, 64);63state = new int[5];64resetHashes();65}6667/*68* Clones this object.69*/70public Object clone() throws CloneNotSupportedException {71SHA copy = (SHA) super.clone();72copy.state = copy.state.clone();73copy.W = null;74return copy;75}7677/**78* Resets the buffers and hash value to start a new hash.79*/80void implReset() {81// Load magic initialization constants.82resetHashes();83// clear out old data84if (W != null) {85Arrays.fill(W, 0);86}87}8889private void resetHashes() {90state[0] = 0x67452301;91state[1] = 0xefcdab89;92state[2] = 0x98badcfe;93state[3] = 0x10325476;94state[4] = 0xc3d2e1f0;95}9697/**98* Computes the final hash and copies the 20 bytes to the output array.99*/100void implDigest(byte[] out, int ofs) {101long bitsProcessed = bytesProcessed << 3;102103int index = (int)bytesProcessed & 0x3f;104int padLen = (index < 56) ? (56 - index) : (120 - index);105engineUpdate(padding, 0, padLen);106107i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);108i2bBig4((int)bitsProcessed, buffer, 60);109implCompress(buffer, 0);110111i2bBig(state, 0, out, ofs, 20);112}113114// Constants for each round115private static final int round1_kt = 0x5a827999;116private static final int round2_kt = 0x6ed9eba1;117private static final int round3_kt = 0x8f1bbcdc;118private static final int round4_kt = 0xca62c1d6;119120/**121* Compute a the hash for the current block.122*123* This is in the same vein as Peter Gutmann's algorithm listed in124* the back of Applied Cryptography, Compact implementation of125* "old" NIST Secure Hash Algorithm.126*/127void implCompress(byte[] buf, int ofs) {128implCompressCheck(buf, ofs);129implCompress0(buf, ofs);130}131132private void implCompressCheck(byte[] buf, int ofs) {133Objects.requireNonNull(buf);134135// Checks similar to those performed by the method 'b2iBig64'136// are sufficient for the case when the method 'implCompress0' is137// replaced with a compiler intrinsic.138if (ofs < 0 || (buf.length - ofs) < 64) {139throw new ArrayIndexOutOfBoundsException();140}141}142143// The method 'implCompress0 seems not to use its parameters.144// The method can, however, be replaced with a compiler intrinsic145// that operates directly on the array 'buf' (starting from146// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'147// must be passed as parameter to the method.148@IntrinsicCandidate149private void implCompress0(byte[] buf, int ofs) {150if (W == null) {151W = new int[80];152}153b2iBig64(buf, ofs, W);154// The first 16 ints have the byte stream, compute the rest of155// the buffer156for (int t = 16; t <= 79; t++) {157int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];158W[t] = (temp << 1) | (temp >>> 31);159}160161int a = state[0];162int b = state[1];163int c = state[2];164int d = state[3];165int e = state[4];166167// Round 1168for (int i = 0; i < 20; i++) {169int temp = ((a<<5) | (a>>>(32-5))) +170((b&c)|((~b)&d))+ e + W[i] + round1_kt;171e = d;172d = c;173c = ((b<<30) | (b>>>(32-30)));174b = a;175a = temp;176}177178// Round 2179for (int i = 20; i < 40; i++) {180int temp = ((a<<5) | (a>>>(32-5))) +181(b ^ c ^ d) + e + W[i] + round2_kt;182e = d;183d = c;184c = ((b<<30) | (b>>>(32-30)));185b = a;186a = temp;187}188189// Round 3190for (int i = 40; i < 60; i++) {191int temp = ((a<<5) | (a>>>(32-5))) +192((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;193e = d;194d = c;195c = ((b<<30) | (b>>>(32-30)));196b = a;197a = temp;198}199200// Round 4201for (int i = 60; i < 80; i++) {202int temp = ((a<<5) | (a>>>(32-5))) +203(b ^ c ^ d) + e + W[i] + round4_kt;204e = d;205d = c;206c = ((b<<30) | (b>>>(32-30)));207b = a;208a = temp;209}210state[0] += a;211state[1] += b;212state[2] += c;213state[3] += d;214state[4] += e;215}216217}218219220