src/java.base/share/classes/sun/security/provider/DigestBase.java
author coffeys
Thu, 23 Aug 2018 11:37:14 +0100
changeset 51504 c9a3e3cac9c7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209129: Further improvements to cipher buffer management Reviewed-by: weijun, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24573
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.MessageDigestSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.DigestException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.ProviderException;
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    31
import java.util.Arrays;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    32
import java.util.Objects;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    33
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    34
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Common base message digest implementation for the Sun provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * It implements all the JCA methods as suitable for a Java message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * implementation of an algorithm based on a compression function (as all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * commonly used algorithms are). The individual digest subclasses only need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * implement the following methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  . abstract void implCompress(byte[] b, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  . abstract void implDigest(byte[] out, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  . abstract void implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * See the inline documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
abstract class DigestBase extends MessageDigestSpi implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // one element byte array, temporary storage for update(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private byte[] oneByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // algorithm name to use in the exception message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // length of the message digest in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final int digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // size of the input to the compression function in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // buffer to store partial blocks, blockSize bytes large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Subclasses should not access this array directly except possibly in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // implDigest() method. See MD5.java as an example.
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    67
    byte[] buffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // offset into buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // number of bytes processed so far. subclasses should not modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // also used as a flag to indicate reset status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // -1: need to call engineReset() before next call to update()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    //  0: is already reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    long bytesProcessed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Main constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    DigestBase(String algorithm, int digestLength, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.digestLength = digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.blockSize = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        buffer = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // return digest length. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected final int engineGetDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // single byte update. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected final void engineUpdate(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (oneByte == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            oneByte = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        oneByte[0] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        engineUpdate(oneByte, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // array update. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected final void engineUpdate(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (bytesProcessed < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // if buffer is not empty, we need to fill it before proceeding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (bufOfs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int n = Math.min(len, blockSize - bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.arraycopy(b, ofs, buffer, bufOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            bufOfs += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            ofs += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (bufOfs >= blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // compress completed block now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                implCompress(buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // compress complete blocks
24573
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   129
        if (len >= blockSize) {
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   130
            int limit = ofs + len;
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   131
            ofs = implCompressMultiBlock(b, ofs, limit - blockSize);
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   132
            len = limit - ofs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // copy remainder to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.arraycopy(b, ofs, buffer, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            bufOfs = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
24573
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   141
    // compress complete blocks
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   142
    private int implCompressMultiBlock(byte[] b, int ofs, int limit) {
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   143
        implCompressMultiBlockCheck(b, ofs, limit);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   144
        return implCompressMultiBlock0(b, ofs, limit);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   145
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   146
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   147
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   148
    private int implCompressMultiBlock0(byte[] b, int ofs, int limit) {
24573
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   149
        for (; ofs <= limit; ofs += blockSize) {
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   150
            implCompress(b, ofs);
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   151
        }
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   152
        return ofs;
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   153
    }
fc2756f8dfef 8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents: 14342
diff changeset
   154
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   155
    private void implCompressMultiBlockCheck(byte[] b, int ofs, int limit) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   156
        if (limit < 0) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   157
            return;  // not an error because implCompressMultiBlockImpl won't execute if limit < 0
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   158
                     // and an exception is thrown if ofs < 0.
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   159
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   160
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   161
        Objects.requireNonNull(b);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   162
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   163
        if (ofs < 0 || ofs >= b.length) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   164
            throw new ArrayIndexOutOfBoundsException(ofs);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   165
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   166
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   167
        int endIndex = (limit / blockSize) * blockSize  + blockSize - 1;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   168
        if (endIndex >= b.length) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   169
            throw new ArrayIndexOutOfBoundsException(endIndex);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   170
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   171
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // reset this object. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected final void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (bytesProcessed == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // already reset, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        bytesProcessed = 0;
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   182
        Arrays.fill(buffer, (byte) 0x00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // return the digest. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected final byte[] engineDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        byte[] b = new byte[digestLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            engineDigest(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw (ProviderException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                new ProviderException("Internal error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // return the digest in the specified array. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected final int engineDigest(byte[] out, int ofs, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throws DigestException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (len < digestLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new DigestException("Length must be at least "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                + digestLength + " for " + algorithm + "digests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new DigestException("Buffer too short to store digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (bytesProcessed < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        implDigest(out, ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        bytesProcessed = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Core compression function. Processes blockSize bytes at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * and updates the state of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    abstract void implCompress(byte[] b, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Return the digest. Subclasses do not need to reset() themselves,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * DigestBase calls implReset() when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    abstract void implDigest(byte[] out, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Reset subclass specific state to their initial values. DigestBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * calls this method when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    abstract void implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   233
    public Object clone() throws CloneNotSupportedException {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   234
        DigestBase copy = (DigestBase) super.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   235
        copy.buffer = copy.buffer.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   236
        return copy;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   237
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // padding used for the MD5, and SHA-* message digests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    static final byte[] padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // we need 128 byte padding for SHA-384/512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // and an additional 8 bytes for the high 8 bytes of the 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // byte bit counter in SHA-384/512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        padding = new byte[136];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        padding[0] = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
}