0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.security.MessageDigestSpi;
0N/Aimport java.security.DigestException;
0N/Aimport java.security.ProviderException;
0N/A
0N/A/**
0N/A * Common base message digest implementation for the Sun provider.
0N/A * It implements all the JCA methods as suitable for a Java message digest
0N/A * implementation of an algorithm based on a compression function (as all
0N/A * commonly used algorithms are). The individual digest subclasses only need to
0N/A * implement the following methods:
0N/A *
0N/A * . abstract void implCompress(byte[] b, int ofs);
0N/A * . abstract void implDigest(byte[] out, int ofs);
0N/A * . abstract void implReset();
0N/A * . public abstract Object clone();
0N/A *
0N/A * See the inline documentation for details.
0N/A *
0N/A * @since 1.5
0N/A * @author Andreas Sterbenz
0N/A */
0N/Aabstract class DigestBase extends MessageDigestSpi implements Cloneable {
0N/A
0N/A // one element byte array, temporary storage for update(byte)
0N/A private byte[] oneByte;
0N/A
0N/A // algorithm name to use in the exception message
0N/A private final String algorithm;
0N/A // length of the message digest in bytes
0N/A private final int digestLength;
0N/A
0N/A // size of the input to the compression function in bytes
0N/A private final int blockSize;
0N/A // buffer to store partial blocks, blockSize bytes large
0N/A // Subclasses should not access this array directly except possibly in their
0N/A // implDigest() method. See MD5.java as an example.
0N/A final byte[] buffer;
0N/A // offset into buffer
0N/A private int bufOfs;
0N/A
0N/A // number of bytes processed so far. subclasses should not modify
0N/A // this value.
0N/A // also used as a flag to indicate reset status
0N/A // -1: need to call engineReset() before next call to update()
0N/A // 0: is already reset
0N/A long bytesProcessed;
0N/A
0N/A /**
0N/A * Main constructor.
0N/A */
0N/A DigestBase(String algorithm, int digestLength, int blockSize) {
0N/A super();
0N/A this.algorithm = algorithm;
0N/A this.digestLength = digestLength;
0N/A this.blockSize = blockSize;
0N/A buffer = new byte[blockSize];
0N/A }
0N/A
0N/A /**
0N/A * Constructor for cloning. Replicates common data.
0N/A */
0N/A DigestBase(DigestBase base) {
0N/A this.algorithm = base.algorithm;
0N/A this.digestLength = base.digestLength;
0N/A this.blockSize = base.blockSize;
0N/A this.buffer = base.buffer.clone();
0N/A this.bufOfs = base.bufOfs;
0N/A this.bytesProcessed = base.bytesProcessed;
0N/A }
0N/A
0N/A // return digest length. See JCA doc.
0N/A protected final int engineGetDigestLength() {
0N/A return digestLength;
0N/A }
0N/A
0N/A // single byte update. See JCA doc.
0N/A protected final void engineUpdate(byte b) {
0N/A if (oneByte == null) {
0N/A oneByte = new byte[1];
0N/A }
0N/A oneByte[0] = b;
0N/A engineUpdate(oneByte, 0, 1);
0N/A }
0N/A
0N/A // array update. See JCA doc.
0N/A protected final void engineUpdate(byte[] b, int ofs, int len) {
0N/A if (len == 0) {
0N/A return;
0N/A }
0N/A if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
0N/A if (bytesProcessed < 0) {
0N/A engineReset();
0N/A }
0N/A bytesProcessed += len;
0N/A // if buffer is not empty, we need to fill it before proceeding
0N/A if (bufOfs != 0) {
0N/A int n = Math.min(len, blockSize - bufOfs);
0N/A System.arraycopy(b, ofs, buffer, bufOfs, n);
0N/A bufOfs += n;
0N/A ofs += n;
0N/A len -= n;
0N/A if (bufOfs >= blockSize) {
0N/A // compress completed block now
0N/A implCompress(buffer, 0);
0N/A bufOfs = 0;
0N/A }
0N/A }
0N/A // compress complete blocks
0N/A while (len >= blockSize) {
0N/A implCompress(b, ofs);
0N/A len -= blockSize;
0N/A ofs += blockSize;
0N/A }
0N/A // copy remainder to buffer
0N/A if (len > 0) {
0N/A System.arraycopy(b, ofs, buffer, 0, len);
0N/A bufOfs = len;
0N/A }
0N/A }
0N/A
0N/A // reset this object. See JCA doc.
0N/A protected final void engineReset() {
0N/A if (bytesProcessed == 0) {
0N/A // already reset, ignore
0N/A return;
0N/A }
0N/A implReset();
0N/A bufOfs = 0;
0N/A bytesProcessed = 0;
0N/A }
0N/A
0N/A // return the digest. See JCA doc.
0N/A protected final byte[] engineDigest() {
0N/A byte[] b = new byte[digestLength];
0N/A try {
0N/A engineDigest(b, 0, b.length);
0N/A } catch (DigestException e) {
0N/A throw (ProviderException)
0N/A new ProviderException("Internal error").initCause(e);
0N/A }
0N/A return b;
0N/A }
0N/A
0N/A // return the digest in the specified array. See JCA doc.
0N/A protected final int engineDigest(byte[] out, int ofs, int len)
0N/A throws DigestException {
0N/A if (len < digestLength) {
0N/A throw new DigestException("Length must be at least "
0N/A + digestLength + " for " + algorithm + "digests");
0N/A }
0N/A if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) {
0N/A throw new DigestException("Buffer too short to store digest");
0N/A }
0N/A if (bytesProcessed < 0) {
0N/A engineReset();
0N/A }
0N/A implDigest(out, ofs);
0N/A bytesProcessed = -1;
0N/A return digestLength;
0N/A }
0N/A
0N/A /**
0N/A * Core compression function. Processes blockSize bytes at a time
0N/A * and updates the state of this object.
0N/A */
0N/A abstract void implCompress(byte[] b, int ofs);
0N/A
0N/A /**
0N/A * Return the digest. Subclasses do not need to reset() themselves,
0N/A * DigestBase calls implReset() when necessary.
0N/A */
0N/A abstract void implDigest(byte[] out, int ofs);
0N/A
0N/A /**
0N/A * Reset subclass specific state to their initial values. DigestBase
0N/A * calls this method when necessary.
0N/A */
0N/A abstract void implReset();
0N/A
0N/A /**
0N/A * Clone this digest. Should be implemented as "return new MyDigest(this)".
0N/A * That constructor should first call "super(baseDigest)" and then copy
0N/A * subclass specific data.
0N/A */
0N/A public abstract Object clone();
0N/A
0N/A // padding used for the MD5, and SHA-* message digests
0N/A static final byte[] padding;
0N/A
0N/A static {
0N/A // we need 128 byte padding for SHA-384/512
0N/A // and an additional 8 bytes for the high 8 bytes of the 16
0N/A // byte bit counter in SHA-384/512
0N/A padding = new byte[136];
0N/A padding[0] = (byte)0x80;
0N/A }
0N/A
0N/A}