Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/SHA2.java
38830 views
/*1* Copyright (c) 2002, 2013, 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;2829import java.util.Objects;3031import 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];85W = new int[64];86resetHashes();87}8889/**90* Resets the buffers and hash value to start a new hash.91*/92void implReset() {93resetHashes();94Arrays.fill(W, 0);95}9697private void resetHashes() {98System.arraycopy(initialHashes, 0, state, 0, state.length);99}100101void implDigest(byte[] out, int ofs) {102long bitsProcessed = bytesProcessed << 3;103104int index = (int)bytesProcessed & 0x3f;105int padLen = (index < 56) ? (56 - index) : (120 - index);106engineUpdate(padding, 0, padLen);107108i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);109i2bBig4((int)bitsProcessed, buffer, 60);110implCompress(buffer, 0);111112i2bBig(state, 0, out, ofs, engineGetDigestLength());113}114115/**116* logical function ch(x,y,z) as defined in spec:117* @return (x and y) xor ((complement x) and z)118* @param x int119* @param y int120* @param z int121*/122private static int lf_ch(int x, int y, int z) {123return (x & y) ^ ((~x) & z);124}125126/**127* logical function maj(x,y,z) as defined in spec:128* @return (x and y) xor (x and z) xor (y and z)129* @param x int130* @param y int131* @param z int132*/133private static int lf_maj(int x, int y, int z) {134return (x & y) ^ (x & z) ^ (y & z);135}136137/**138* logical function R(x,s) - right shift139* @return x right shift for s times140* @param x int141* @param s int142*/143private static int lf_R( int x, int s ) {144return (x >>> s);145}146147/**148* logical function S(x,s) - right rotation149* @return x circular right shift for s times150* @param x int151* @param s int152*/153private static int lf_S(int x, int s) {154return (x >>> s) | (x << (32 - s));155}156157/**158* logical function sigma0(x) - xor of results of right rotations159* @return S(x,2) xor S(x,13) xor S(x,22)160* @param x int161*/162private static int lf_sigma0(int x) {163return lf_S(x, 2) ^ lf_S(x, 13) ^ lf_S(x, 22);164}165166/**167* logical function sigma1(x) - xor of results of right rotations168* @return S(x,6) xor S(x,11) xor S(x,25)169* @param x int170*/171private static int lf_sigma1(int x) {172return lf_S( x, 6 ) ^ lf_S( x, 11 ) ^ lf_S( x, 25 );173}174175/**176* logical function delta0(x) - xor of results of right shifts/rotations177* @return int178* @param x int179*/180private static int lf_delta0(int x) {181return lf_S(x, 7) ^ lf_S(x, 18) ^ lf_R(x, 3);182}183184/**185* logical function delta1(x) - xor of results of right shifts/rotations186* @return int187* @param x int188*/189private static int lf_delta1(int x) {190return lf_S(x, 17) ^ lf_S(x, 19) ^ lf_R(x, 10);191}192193/**194* Process the current block to update the state variable state.195*/196void implCompress(byte[] buf, int ofs) {197implCompressCheck(buf, ofs);198implCompress0(buf, ofs);199}200201private void implCompressCheck(byte[] buf, int ofs) {202Objects.requireNonNull(buf);203204// The checks performed by the method 'b2iBig64'205// are sufficient for the case when the method206// 'implCompressImpl' is replaced with a compiler207// intrinsic.208b2iBig64(buf, ofs, W);209}210211// The method 'implCompressImpl' seems not to use its parameters.212// The method can, however, be replaced with a compiler intrinsic213// that operates directly on the array 'buf' (starting from214// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'215// must be passed as parameter to the method.216private void implCompress0(byte[] buf, int ofs) {217// The first 16 ints are from the byte stream, compute the rest of218// the W[]'s219for (int t = 16; t < ITERATION; t++) {220W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])221+ W[t-16];222}223224int a = state[0];225int b = state[1];226int c = state[2];227int d = state[3];228int e = state[4];229int f = state[5];230int g = state[6];231int h = state[7];232233for (int i = 0; i < ITERATION; i++) {234int T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];235int T2 = lf_sigma0(a) + lf_maj(a,b,c);236h = g;237g = f;238f = e;239e = d + T1;240d = c;241c = b;242b = a;243a = T1 + T2;244}245state[0] += a;246state[1] += b;247state[2] += c;248state[3] += d;249state[4] += e;250state[5] += f;251state[6] += g;252state[7] += h;253}254255public Object clone() throws CloneNotSupportedException {256SHA2 copy = (SHA2) super.clone();257copy.state = copy.state.clone();258copy.W = new int[64];259return copy;260}261262/**263* SHA-224 implementation class.264*/265public static final class SHA224 extends SHA2 {266private static final int[] INITIAL_HASHES = {2670xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,2680xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4269};270271public SHA224() {272super("SHA-224", 28, INITIAL_HASHES);273}274}275276/**277* SHA-256 implementation class.278*/279public static final class SHA256 extends SHA2 {280private static final int[] INITIAL_HASHES = {2810x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,2820x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19283};284285public SHA256() {286super("SHA-256", 32, INITIAL_HASHES);287}288}289}290291292