Path: blob/master/src/java.base/share/classes/sun/security/provider/SHA5.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-384 and SHA-51235* developed by the National Institute of Standards and Technology along36* with the National Security Agency.37*38* The two algorithms are almost identical. This file contains a base39* class SHA5 and two nested static subclasses as the classes to be used40* by the JCA framework.41*42* <p>It implements java.security.MessageDigestSpi, and can be used43* through Java Cryptography Architecture (JCA), as a pluggable44* MessageDigest implementation.45*46* @since 1.4.247* @author Valerie Peng48* @author Andreas Sterbenz49*/50abstract class SHA5 extends DigestBase {5152private static final int ITERATION = 80;53// Constants for each round/iteration54private static final long[] ROUND_CONSTS = {550x428A2F98D728AE22L, 0x7137449123EF65CDL, 0xB5C0FBCFEC4D3B2FL,560xE9B5DBA58189DBBCL, 0x3956C25BF348B538L, 0x59F111F1B605D019L,570x923F82A4AF194F9BL, 0xAB1C5ED5DA6D8118L, 0xD807AA98A3030242L,580x12835B0145706FBEL, 0x243185BE4EE4B28CL, 0x550C7DC3D5FFB4E2L,590x72BE5D74F27B896FL, 0x80DEB1FE3B1696B1L, 0x9BDC06A725C71235L,600xC19BF174CF692694L, 0xE49B69C19EF14AD2L, 0xEFBE4786384F25E3L,610x0FC19DC68B8CD5B5L, 0x240CA1CC77AC9C65L, 0x2DE92C6F592B0275L,620x4A7484AA6EA6E483L, 0x5CB0A9DCBD41FBD4L, 0x76F988DA831153B5L,630x983E5152EE66DFABL, 0xA831C66D2DB43210L, 0xB00327C898FB213FL,640xBF597FC7BEEF0EE4L, 0xC6E00BF33DA88FC2L, 0xD5A79147930AA725L,650x06CA6351E003826FL, 0x142929670A0E6E70L, 0x27B70A8546D22FFCL,660x2E1B21385C26C926L, 0x4D2C6DFC5AC42AEDL, 0x53380D139D95B3DFL,670x650A73548BAF63DEL, 0x766A0ABB3C77B2A8L, 0x81C2C92E47EDAEE6L,680x92722C851482353BL, 0xA2BFE8A14CF10364L, 0xA81A664BBC423001L,690xC24B8B70D0F89791L, 0xC76C51A30654BE30L, 0xD192E819D6EF5218L,700xD69906245565A910L, 0xF40E35855771202AL, 0x106AA07032BBD1B8L,710x19A4C116B8D2D0C8L, 0x1E376C085141AB53L, 0x2748774CDF8EEB99L,720x34B0BCB5E19B48A8L, 0x391C0CB3C5C95A63L, 0x4ED8AA4AE3418ACBL,730x5B9CCA4F7763E373L, 0x682E6FF3D6B2B8A3L, 0x748F82EE5DEFB2FCL,740x78A5636F43172F60L, 0x84C87814A1F0AB72L, 0x8CC702081A6439ECL,750x90BEFFFA23631E28L, 0xA4506CEBDE82BDE9L, 0xBEF9A3F7B2C67915L,760xC67178F2E372532BL, 0xCA273ECEEA26619CL, 0xD186B8C721C0C207L,770xEADA7DD6CDE0EB1EL, 0xF57D4F7FEE6ED178L, 0x06F067AA72176FBAL,780x0A637DC5A2C898A6L, 0x113F9804BEF90DAEL, 0x1B710B35131C471BL,790x28DB77F523047D84L, 0x32CAAB7B40C72493L, 0x3C9EBE0A15C9BEBCL,800x431D67C49C100D4CL, 0x4CC5D4BECB3E42B6L, 0x597F299CFC657E2AL,810x5FCB6FAB3AD6FAECL, 0x6C44198C4A475817L82};8384// buffer used by implCompress()85private long[] W;8687// state of this object88private long[] state;8990// initial state value. different between SHA-384 and SHA-51291private final long[] initialHashes;9293/**94* Creates a new SHA object.95*/96SHA5(String name, int digestLength, long[] initialHashes) {97super(name, digestLength, 128);98this.initialHashes = initialHashes;99state = new long[8];100resetHashes();101}102103final void implReset() {104resetHashes();105if (W != null) {106Arrays.fill(W, 0L);107}108}109110private void resetHashes() {111System.arraycopy(initialHashes, 0, state, 0, state.length);112}113114final void implDigest(byte[] out, int ofs) {115long bitsProcessed = bytesProcessed << 3;116117int index = (int)bytesProcessed & 0x7f;118int padLen = (index < 112) ? (112 - index) : (240 - index);119engineUpdate(padding, 0, padLen + 8);120121i2bBig4((int)(bitsProcessed >>> 32), buffer, 120);122i2bBig4((int)bitsProcessed, buffer, 124);123implCompress(buffer, 0);124125int len = engineGetDigestLength();126if (len == 28) {127// Special case for SHA-512/224128l2bBig(state, 0, out, ofs, 24);129i2bBig4((int)(state[3] >> 32), out, ofs + 24);130} else {131l2bBig(state, 0, out, ofs, len);132}133}134135/**136* logical function ch(x,y,z) as defined in spec:137* @return (x and y) xor ((complement x) and z)138* @param x long139* @param y long140* @param z long141*/142private static long lf_ch(long x, long y, long z) {143return (x & y) ^ ((~x) & z);144}145146/**147* logical function maj(x,y,z) as defined in spec:148* @return (x and y) xor (x and z) xor (y and z)149* @param x long150* @param y long151* @param z long152*/153private static long lf_maj(long x, long y, long z) {154return (x & y) ^ (x & z) ^ (y & z);155}156157/**158* logical function R(x,s) - right shift159* @return x right shift for s times160* @param x long161* @param s int162*/163private static long lf_R(long x, int s) {164return (x >>> s);165}166167/**168* logical function S(x,s) - right rotation169* @return x circular right shift for s times170* @param x long171* @param s int172*/173private static long lf_S(long x, int s) {174return (x >>> s) | (x << (64 - s));175}176177/**178* logical function sigma0(x) - xor of results of right rotations179* @return S(x,28) xor S(x,34) xor S(x,39)180* @param x long181*/182private static long lf_sigma0(long x) {183return lf_S(x, 28) ^ lf_S(x, 34) ^ lf_S(x, 39);184}185186/**187* logical function sigma1(x) - xor of results of right rotations188* @return S(x,14) xor S(x,18) xor S(x,41)189* @param x long190*/191private static long lf_sigma1(long x) {192return lf_S(x, 14) ^ lf_S(x, 18) ^ lf_S(x, 41);193}194195/**196* logical function delta0(x) - xor of results of right shifts/rotations197* @return long198* @param x long199*/200private static long lf_delta0(long x) {201return lf_S(x, 1) ^ lf_S(x, 8) ^ lf_R(x, 7);202}203204/**205* logical function delta1(x) - xor of results of right shifts/rotations206* @return long207* @param x long208*/209private static long lf_delta1(long x) {210return lf_S(x, 19) ^ lf_S(x, 61) ^ lf_R(x, 6);211}212213/**214* Compute the hash for the current block.215*216* This is in the same vein as Peter Gutmann's algorithm listed in217* the back of Applied Cryptography, Compact implementation of218* "old" NIST Secure Hash Algorithm.219*/220final void implCompress(byte[] buf, int ofs) {221implCompressCheck(buf, ofs);222implCompress0(buf, ofs);223}224225private void implCompressCheck(byte[] buf, int ofs) {226Objects.requireNonNull(buf);227228// Checks similar to those performed by the method 'b2lBig128'229// are sufficient for the case when the method 'implCompress0' is230// replaced with a compiler intrinsic.231if (ofs < 0 || (buf.length - ofs) < 128) {232throw new ArrayIndexOutOfBoundsException();233}234}235236// The method 'implCompressImpl' seems not to use its parameters.237// The method can, however, be replaced with a compiler intrinsic238// that operates directly on the array 'buf' (starting from239// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'240// must be passed as parameter to the method.241@IntrinsicCandidate242private final void implCompress0(byte[] buf, int ofs) {243if (W == null) {244W = new long[80];245}246b2lBig128(buf, ofs, W);247// The first 16 longs are from the byte stream, compute the rest of248// the W[]'s249for (int t = 16; t < ITERATION; t++) {250W[t] = lf_delta1(W[t-2]) + W[t-7] + lf_delta0(W[t-15])251+ W[t-16];252}253254long a = state[0];255long b = state[1];256long c = state[2];257long d = state[3];258long e = state[4];259long f = state[5];260long g = state[6];261long h = state[7];262263for (int i = 0; i < ITERATION; i++) {264long T1 = h + lf_sigma1(e) + lf_ch(e,f,g) + ROUND_CONSTS[i] + W[i];265long T2 = lf_sigma0(a) + lf_maj(a,b,c);266h = g;267g = f;268f = e;269e = d + T1;270d = c;271c = b;272b = a;273a = T1 + T2;274}275state[0] += a;276state[1] += b;277state[2] += c;278state[3] += d;279state[4] += e;280state[5] += f;281state[6] += g;282state[7] += h;283}284285public Object clone() throws CloneNotSupportedException {286SHA5 copy = (SHA5) super.clone();287copy.state = copy.state.clone();288copy.W = null;289return copy;290}291292/**293* SHA-512 implementation class.294*/295public static final class SHA512 extends SHA5 {296297private static final long[] INITIAL_HASHES = {2980x6a09e667f3bcc908L, 0xbb67ae8584caa73bL,2990x3c6ef372fe94f82bL, 0xa54ff53a5f1d36f1L,3000x510e527fade682d1L, 0x9b05688c2b3e6c1fL,3010x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L302};303304public SHA512() {305super("SHA-512", 64, INITIAL_HASHES);306}307}308309/**310* SHA-384 implementation class.311*/312public static final class SHA384 extends SHA5 {313314private static final long[] INITIAL_HASHES = {3150xcbbb9d5dc1059ed8L, 0x629a292a367cd507L,3160x9159015a3070dd17L, 0x152fecd8f70e5939L,3170x67332667ffc00b31L, 0x8eb44a8768581511L,3180xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L319};320321public SHA384() {322super("SHA-384", 48, INITIAL_HASHES);323}324}325public static final class SHA512_224 extends SHA5 {326327private static final long[] INITIAL_HASHES = {3280x8C3D37C819544DA2L, 0x73E1996689DCD4D6L,3290x1DFAB7AE32FF9C82L, 0x679DD514582F9FCFL,3300x0F6D2B697BD44DA8L, 0x77E36F7304C48942L,3310x3F9D85A86A1D36C8L, 0x1112E6AD91D692A1L332};333334public SHA512_224() {335super("SHA-512/224", 28, INITIAL_HASHES);336}337}338339public static final class SHA512_256 extends SHA5 {340341private static final long[] INITIAL_HASHES = {3420x22312194FC2BF72CL, 0x9F555FA3C84C64C2L,3430x2393B86B6F53B151L, 0x963877195940EABDL,3440x96283EE2A88EFFE3L, 0xBE5E1E2553863992L,3450x2B0199FC2C85B8AAL, 0x0EB72DDC81C52CA2L346};347348public SHA512_256() {349super("SHA-512/256", 32, INITIAL_HASHES);350}351}352}353354355