Path: blob/master/src/java.base/share/classes/sun/security/provider/DigestBase.java
41159 views
/*1* Copyright (c) 2003, 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 java.security.MessageDigestSpi;28import java.security.DigestException;29import java.security.ProviderException;30import java.util.Arrays;31import java.util.Objects;3233import jdk.internal.vm.annotation.IntrinsicCandidate;3435/**36* Common base message digest implementation for the Sun provider.37* It implements all the JCA methods as suitable for a Java message digest38* implementation of an algorithm based on a compression function (as all39* commonly used algorithms are). The individual digest subclasses only need to40* implement the following methods:41*42* . abstract void implCompress(byte[] b, int ofs);43* . abstract void implDigest(byte[] out, int ofs);44* . abstract void implReset();45*46* See the inline documentation for details.47*48* @since 1.549* @author Andreas Sterbenz50*/51abstract class DigestBase extends MessageDigestSpi implements Cloneable {5253// one element byte array, temporary storage for update(byte)54private byte[] oneByte;5556// algorithm name to use in the exception message57private final String algorithm;58// length of the message digest in bytes59private final int digestLength;6061// size of the input to the compression function in bytes62private final int blockSize;63// buffer to store partial blocks, blockSize bytes large64// Subclasses should not access this array directly except possibly in their65// implDigest() method. See MD5.java as an example.66byte[] buffer;67// offset into buffer68private int bufOfs;6970// number of bytes processed so far. subclasses should not modify71// this value.72// also used as a flag to indicate reset status73// -1: need to call engineReset() before next call to update()74// 0: is already reset75long bytesProcessed;7677/**78* Main constructor.79*/80DigestBase(String algorithm, int digestLength, int blockSize) {81super();82this.algorithm = algorithm;83this.digestLength = digestLength;84this.blockSize = blockSize;85buffer = new byte[blockSize];86}8788// return digest length. See JCA doc.89protected final int engineGetDigestLength() {90return digestLength;91}9293// single byte update. See JCA doc.94protected final void engineUpdate(byte b) {95if (oneByte == null) {96oneByte = new byte[1];97}98oneByte[0] = b;99engineUpdate(oneByte, 0, 1);100}101102// array update. See JCA doc.103protected final void engineUpdate(byte[] b, int ofs, int len) {104if (len == 0) {105return;106}107if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {108throw new ArrayIndexOutOfBoundsException();109}110if (bytesProcessed < 0) {111engineReset();112}113bytesProcessed += len;114// if buffer is not empty, we need to fill it before proceeding115if (bufOfs != 0) {116int n = Math.min(len, blockSize - bufOfs);117System.arraycopy(b, ofs, buffer, bufOfs, n);118bufOfs += n;119ofs += n;120len -= n;121if (bufOfs >= blockSize) {122// compress completed block now123implCompress(buffer, 0);124bufOfs = 0;125}126}127// compress complete blocks128if (len >= blockSize) {129int limit = ofs + len;130ofs = implCompressMultiBlock(b, ofs, limit - blockSize);131len = limit - ofs;132}133// copy remainder to buffer134if (len > 0) {135System.arraycopy(b, ofs, buffer, 0, len);136bufOfs = len;137}138}139140// compress complete blocks141private int implCompressMultiBlock(byte[] b, int ofs, int limit) {142implCompressMultiBlockCheck(b, ofs, limit);143return implCompressMultiBlock0(b, ofs, limit);144}145146@IntrinsicCandidate147private int implCompressMultiBlock0(byte[] b, int ofs, int limit) {148for (; ofs <= limit; ofs += blockSize) {149implCompress(b, ofs);150}151return ofs;152}153154private void implCompressMultiBlockCheck(byte[] b, int ofs, int limit) {155if (limit < 0) {156return; // not an error because implCompressMultiBlockImpl won't execute if limit < 0157// and an exception is thrown if ofs < 0.158}159160Objects.requireNonNull(b);161162if (ofs < 0 || ofs >= b.length) {163throw new ArrayIndexOutOfBoundsException(ofs);164}165166int endIndex = (limit / blockSize) * blockSize + blockSize - 1;167if (endIndex >= b.length) {168throw new ArrayIndexOutOfBoundsException(endIndex);169}170}171172// reset this object. See JCA doc.173protected final void engineReset() {174if (bytesProcessed == 0) {175// already reset, ignore176return;177}178implReset();179bufOfs = 0;180bytesProcessed = 0;181Arrays.fill(buffer, (byte) 0x00);182}183184// return the digest. See JCA doc.185protected final byte[] engineDigest() {186byte[] b = new byte[digestLength];187try {188engineDigest(b, 0, b.length);189} catch (DigestException e) {190throw (ProviderException)191new ProviderException("Internal error").initCause(e);192}193return b;194}195196// return the digest in the specified array. See JCA doc.197protected final int engineDigest(byte[] out, int ofs, int len)198throws DigestException {199if (len < digestLength) {200throw new DigestException("Length must be at least "201+ digestLength + " for " + algorithm + "digests");202}203if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) {204throw new DigestException("Buffer too short to store digest");205}206if (bytesProcessed < 0) {207engineReset();208}209implDigest(out, ofs);210bytesProcessed = -1;211return digestLength;212}213214/**215* Core compression function. Processes blockSize bytes at a time216* and updates the state of this object.217*/218abstract void implCompress(byte[] b, int ofs);219220/**221* Return the digest. Subclasses do not need to reset() themselves,222* DigestBase calls implReset() when necessary.223*/224abstract void implDigest(byte[] out, int ofs);225226/**227* Reset subclass specific state to their initial values. DigestBase228* calls this method when necessary.229*/230abstract void implReset();231232public Object clone() throws CloneNotSupportedException {233DigestBase copy = (DigestBase) super.clone();234copy.buffer = copy.buffer.clone();235copy.oneByte = null;236return copy;237}238239// padding used for the MD5, and SHA-* message digests240static final byte[] padding;241242static {243// we need 128 byte padding for SHA-384/512244// and an additional 8 bytes for the high 8 bytes of the 16245// byte bit counter in SHA-384/512246padding = new byte[136];247padding[0] = (byte)0x80;248}249}250251252