Path: blob/master/src/java.base/share/classes/sun/security/provider/SHA3.java
41159 views
/*1* Copyright (c) 2016, 2020, 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 jdk.internal.vm.annotation.IntrinsicCandidate;28import static sun.security.provider.ByteArrayAccess.*;29import java.nio.*;30import java.util.*;31import java.security.*;3233/**34* This class implements the Secure Hash Algorithm SHA-3 developed by35* the National Institute of Standards and Technology along with the36* National Security Agency as defined in FIPS PUB 202.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 943* @author Valerie Peng44*/45abstract class SHA3 extends DigestBase {4647private static final int WIDTH = 200; // in bytes, e.g. 1600 bits48private static final int DM = 5; // dimension of lanes4950private static final int NR = 24; // number of rounds5152// precomputed round constants needed by the step mapping Iota53private static final long[] RC_CONSTANTS = {540x01L, 0x8082L, 0x800000000000808aL,550x8000000080008000L, 0x808bL, 0x80000001L,560x8000000080008081L, 0x8000000000008009L, 0x8aL,570x88L, 0x80008009L, 0x8000000aL,580x8000808bL, 0x800000000000008bL, 0x8000000000008089L,590x8000000000008003L, 0x8000000000008002L, 0x8000000000000080L,600x800aL, 0x800000008000000aL, 0x8000000080008081L,610x8000000000008080L, 0x80000001L, 0x8000000080008008L,62};6364private final byte suffix;65private byte[] state = new byte[WIDTH];66private long[] lanes = new long[DM*DM];6768/**69* Creates a new SHA-3 object.70*/71SHA3(String name, int digestLength, byte suffix, int c) {72super(name, digestLength, (WIDTH - c));73this.suffix = suffix;74}7576private void implCompressCheck(byte[] b, int ofs) {77Objects.requireNonNull(b);78}7980/**81* Core compression function. Processes blockSize bytes at a time82* and updates the state of this object.83*/84void implCompress(byte[] b, int ofs) {85implCompressCheck(b, ofs);86implCompress0(b, ofs);87}8889@IntrinsicCandidate90private void implCompress0(byte[] b, int ofs) {91for (int i = 0; i < buffer.length; i++) {92state[i] ^= b[ofs++];93}94keccak();95}9697/**98* Return the digest. Subclasses do not need to reset() themselves,99* DigestBase calls implReset() when necessary.100*/101void implDigest(byte[] out, int ofs) {102int numOfPadding =103setPaddingBytes(suffix, buffer, (int)(bytesProcessed % buffer.length));104if (numOfPadding < 1) {105throw new ProviderException("Incorrect pad size: " + numOfPadding);106}107implCompress(buffer, 0);108System.arraycopy(state, 0, out, ofs, engineGetDigestLength());109}110111/**112* Resets the internal state to start a new hash.113*/114void implReset() {115Arrays.fill(state, (byte)0);116Arrays.fill(lanes, 0L);117}118119/**120* Utility function for padding the specified data based on the121* pad10*1 algorithm (section 5.1) and the 2-bit suffix "01" required122* for SHA-3 hash (section 6.1).123*/124private static int setPaddingBytes(byte suffix, byte[] in, int len) {125if (len != in.length) {126// erase leftover values127Arrays.fill(in, len, in.length, (byte)0);128// directly store the padding bytes into the input129// as the specified buffer is allocated w/ size = rateR130in[len] |= suffix;131in[in.length - 1] |= (byte) 0x80;132}133return (in.length - len);134}135136/**137* Utility function for transforming the specified byte array 's'138* into array of lanes 'm' as defined in section 3.1.2.139*/140private static void bytes2Lanes(byte[] s, long[] m) {141int sOfs = 0;142// Conversion traverses along x-axis before y-axis143for (int y = 0; y < DM; y++, sOfs += 40) {144b2lLittle(s, sOfs, m, DM*y, 40);145}146}147148/**149* Utility function for transforming the specified array of150* lanes 'm' into a byte array 's' as defined in section 3.1.3.151*/152private static void lanes2Bytes(long[] m, byte[] s) {153int sOfs = 0;154// Conversion traverses along x-axis before y-axis155for (int y = 0; y < DM; y++, sOfs += 40) {156l2bLittle(m, DM*y, s, sOfs, 40);157}158}159160/**161* Step mapping Theta as defined in section 3.2.1 .162*/163private static long[] smTheta(long[] a) {164long c0 = a[0]^a[5]^a[10]^a[15]^a[20];165long c1 = a[1]^a[6]^a[11]^a[16]^a[21];166long c2 = a[2]^a[7]^a[12]^a[17]^a[22];167long c3 = a[3]^a[8]^a[13]^a[18]^a[23];168long c4 = a[4]^a[9]^a[14]^a[19]^a[24];169long d0 = c4 ^ Long.rotateLeft(c1, 1);170long d1 = c0 ^ Long.rotateLeft(c2, 1);171long d2 = c1 ^ Long.rotateLeft(c3, 1);172long d3 = c2 ^ Long.rotateLeft(c4, 1);173long d4 = c3 ^ Long.rotateLeft(c0, 1);174for (int y = 0; y < a.length; y += DM) {175a[y] ^= d0;176a[y+1] ^= d1;177a[y+2] ^= d2;178a[y+3] ^= d3;179a[y+4] ^= d4;180}181return a;182}183184/**185* Merged Step mapping Rho (section 3.2.2) and Pi (section 3.2.3).186* for performance. Optimization is achieved by precalculating187* shift constants for the following loop188* int xNext, yNext;189* for (int t = 0, x = 1, y = 0; t <= 23; t++, x = xNext, y = yNext) {190* int numberOfShift = ((t + 1)*(t + 2)/2) % 64;191* a[y][x] = Long.rotateLeft(a[y][x], numberOfShift);192* xNext = y;193* yNext = (2 * x + 3 * y) % DM;194* }195* and with inplace permutation.196*/197private static long[] smPiRho(long[] a) {198long tmp = Long.rotateLeft(a[10], 3);199a[10] = Long.rotateLeft(a[1], 1);200a[1] = Long.rotateLeft(a[6], 44);201a[6] = Long.rotateLeft(a[9], 20);202a[9] = Long.rotateLeft(a[22], 61);203a[22] = Long.rotateLeft(a[14], 39);204a[14] = Long.rotateLeft(a[20], 18);205a[20] = Long.rotateLeft(a[2], 62);206a[2] = Long.rotateLeft(a[12], 43);207a[12] = Long.rotateLeft(a[13], 25);208a[13] = Long.rotateLeft(a[19], 8);209a[19] = Long.rotateLeft(a[23], 56);210a[23] = Long.rotateLeft(a[15], 41);211a[15] = Long.rotateLeft(a[4], 27);212a[4] = Long.rotateLeft(a[24], 14);213a[24] = Long.rotateLeft(a[21], 2);214a[21] = Long.rotateLeft(a[8], 55);215a[8] = Long.rotateLeft(a[16], 45);216a[16] = Long.rotateLeft(a[5], 36);217a[5] = Long.rotateLeft(a[3], 28);218a[3] = Long.rotateLeft(a[18], 21);219a[18] = Long.rotateLeft(a[17], 15);220a[17] = Long.rotateLeft(a[11], 10);221a[11] = Long.rotateLeft(a[7], 6);222a[7] = tmp;223return a;224}225226/**227* Step mapping Chi as defined in section 3.2.4.228*/229private static long[] smChi(long[] a) {230for (int y = 0; y < a.length; y+=DM) {231long ay0 = a[y];232long ay1 = a[y+1];233long ay2 = a[y+2];234long ay3 = a[y+3];235long ay4 = a[y+4];236a[y] = ay0 ^ ((~ay1) & ay2);237a[y+1] = ay1 ^ ((~ay2) & ay3);238a[y+2] = ay2 ^ ((~ay3) & ay4);239a[y+3] = ay3 ^ ((~ay4) & ay0);240a[y+4] = ay4 ^ ((~ay0) & ay1);241}242return a;243}244245/**246* Step mapping Iota as defined in section 3.2.5.247*/248private static long[] smIota(long[] a, int rndIndex) {249a[0] ^= RC_CONSTANTS[rndIndex];250return a;251}252253/**254* The function Keccak as defined in section 5.2 with255* rate r = 1600 and capacity c = (digest length x 2).256*/257private void keccak() {258// convert the 200-byte state into 25 lanes259bytes2Lanes(state, lanes);260// process the lanes through step mappings261for (int ir = 0; ir < NR; ir++) {262smIota(smChi(smPiRho(smTheta(lanes))), ir);263}264// convert the resulting 25 lanes back into 200-byte state265lanes2Bytes(lanes, state);266}267268public Object clone() throws CloneNotSupportedException {269SHA3 copy = (SHA3) super.clone();270copy.state = copy.state.clone();271copy.lanes = new long[DM*DM];272return copy;273}274275/**276* SHA3-224 implementation class.277*/278public static final class SHA224 extends SHA3 {279public SHA224() {280super("SHA3-224", 28, (byte)0x06, 56);281}282}283284/**285* SHA3-256 implementation class.286*/287public static final class SHA256 extends SHA3 {288public SHA256() {289super("SHA3-256", 32, (byte)0x06, 64);290}291}292293/**294* SHAs-384 implementation class.295*/296public static final class SHA384 extends SHA3 {297public SHA384() {298super("SHA3-384", 48, (byte)0x06, 96);299}300}301302/**303* SHA3-512 implementation class.304*/305public static final class SHA512 extends SHA3 {306public SHA512() {307super("SHA3-512", 64, (byte)0x06, 128);308}309}310}311312313