Path: blob/master/src/java.base/share/classes/sun/security/provider/SHA2.java
41159 views
/*1* Copyright (c) 2002, 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 jdk.internal.vm.annotation.IntrinsicCandidate;31import static sun.security.provider.ByteArrayAccess.*;3233/**34* This class implements the Secure Hash Algorithm SHA-256 developed by35* the National Institute of Standards and Technology along with the36* National Security Agency.37*38* <p>It implements java.security.MessageDigestSpi, and can be used39* through Java Cryptography Architecture (JCA), as a pluggable40* MessageDigest implementation.41*42* @since 1.4.243* @author Valerie Peng44* @author Andreas Sterbenz45*/46abstract class SHA2 extends DigestBase {4748private static final int ITERATION = 64;49// Constants for each round50private static final int[] ROUND_CONSTS = {510x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,520x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,530xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,540x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,550xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,560x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,570x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,580xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,590x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,600x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,610xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,620xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,630x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,640x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,650x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,660x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f267};6869// buffer used by implCompress()70private int[] W;7172// state of this object73private int[] state;7475// initial state value. different between SHA-224 and SHA-25676private final int[] initialHashes;7778/**79* Creates a new SHA object.80*/81SHA2(String name, int digestLength, int[] initialHashes) {82super(name, digestLength, 64);83this.initialHashes = initialHashes;84state = new int[8];85resetHashes();86}8788/**89* Resets the buffers and hash value to start a new hash.90*/91void implReset() {92resetHashes();93if (W != null) {94Arrays.fill(W, 0);95}96}9798private void resetHashes() {99System.arraycopy(initialHashes, 0, state, 0, state.length);100}101102void implDigest(byte[] out, int ofs) {103long bitsProcessed = bytesProcessed << 3;104105int index = (int)bytesProcessed & 0x3f;106int padLen = (index < 56) ? (56 - index) : (120 - index);107engineUpdate(padding, 0, padLen);108109i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);110i2bBig4((int)bitsProcessed, buffer, 60);111implCompress(buffer, 0);112113i2bBig(state, 0, out, ofs, engineGetDigestLength());114}115116/**117* Process the current block to update the state variable state.118*/119void implCompress(byte[] buf, int ofs) {120implCompressCheck(buf, ofs);121implCompress0(buf, ofs);122}123124private void implCompressCheck(byte[] buf, int ofs) {125Objects.requireNonNull(buf);126127// Checks similar to those performed by the method 'b2iBig64'128// are sufficient for the case when the method 'implCompress0' is129// replaced with a compiler intrinsic.130if (ofs < 0 || (buf.length - ofs) < 64) {131throw new ArrayIndexOutOfBoundsException();132}133}134135// The method 'implCompressImpl' seems not to use its parameters.136// The method can, however, be replaced with a compiler intrinsic137// that operates directly on the array 'buf' (starting from138// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'139// must be passed as parameter to the method.140@IntrinsicCandidate141private void implCompress0(byte[] buf, int ofs) {142if (W == null) {143W = new int[64];144}145b2iBig64(buf, ofs, W);146// The first 16 ints are from the byte stream, compute the rest of147// the W[]'s148for (int t = 16; t < ITERATION; t++) {149int W_t2 = W[t - 2];150int W_t15 = W[t - 15];151152// S(x,s) is right rotation of x by s positions:153// S(x,s) = (x >>> s) | (x << (32 - s))154// R(x,s) is right shift of x by s positions:155// R(x,s) = (x >>> s)156157// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)158int delta0_W_t15 =159((W_t15 >>> 7) | (W_t15 << 25)) ^160((W_t15 >>> 18) | (W_t15 << 14)) ^161(W_t15 >>> 3);162163// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)164int delta1_W_t2 =165((W_t2 >>> 17) | (W_t2 << 15)) ^166((W_t2 >>> 19) | (W_t2 << 13)) ^167(W_t2 >>> 10);168169W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16];170}171172int a = state[0];173int b = state[1];174int c = state[2];175int d = state[3];176int e = state[4];177int f = state[5];178int g = state[6];179int h = state[7];180181for (int i = 0; i < ITERATION; i++) {182// S(x,s) is right rotation of x by s positions:183// S(x,s) = (x >>> s) | (x << (32 - s))184185// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)186int sigma0_a =187((a >>> 2) | (a << 30)) ^188((a >>> 13) | (a << 19)) ^189((a >>> 22) | (a << 10));190191// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)192int sigma1_e =193((e >>> 6) | (e << 26)) ^194((e >>> 11) | (e << 21)) ^195((e >>> 25) | (e << 7));196197// ch(x,y,z) = (x and y) xor ((complement x) and z)198int ch_efg = (e & f) ^ ((~e) & g);199200// maj(x,y,z) = (x and y) xor (x and z) xor (y and z)201int maj_abc = (a & b) ^ (a & c) ^ (b & c);202203int T1 = h + sigma1_e + ch_efg + ROUND_CONSTS[i] + W[i];204int T2 = sigma0_a + maj_abc;205h = g;206g = f;207f = e;208e = d + T1;209d = c;210c = b;211b = a;212a = T1 + T2;213}214215state[0] += a;216state[1] += b;217state[2] += c;218state[3] += d;219state[4] += e;220state[5] += f;221state[6] += g;222state[7] += h;223}224225public Object clone() throws CloneNotSupportedException {226SHA2 copy = (SHA2) super.clone();227copy.state = copy.state.clone();228copy.W = null;229return copy;230}231232/**233* SHA-224 implementation class.234*/235public static final class SHA224 extends SHA2 {236private static final int[] INITIAL_HASHES = {2370xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,2380xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4239};240241public SHA224() {242super("SHA-224", 28, INITIAL_HASHES);243}244}245246/**247* SHA-256 implementation class.248*/249public static final class SHA256 extends SHA2 {250private static final int[] INITIAL_HASHES = {2510x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,2520x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19253};254255public SHA256() {256super("SHA-256", 32, INITIAL_HASHES);257}258}259}260261262