jdk/src/share/classes/sun/security/provider/DigestBase.java
author valeriep
Tue, 25 Sep 2012 11:31:17 -0700
changeset 14003 53c498ff6b0b
parent 12685 8a448b5b9006
child 14342 8435a30053c1
permissions -rw-r--r--
7199939: DSA 576 and 640 bit keys fail when initializing for No precomputed parameters Summary: Fixed initialize(int, SecureRandom) call to not error out when no precomputed params available. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2006, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Common base message digest implementation for the Sun provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * It implements all the JCA methods as suitable for a Java message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * implementation of an algorithm based on a compression function (as all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * commonly used algorithms are). The individual digest subclasses only need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implement the following methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  . abstract void implCompress(byte[] b, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  . abstract void implDigest(byte[] out, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  . abstract void implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * See the inline documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
abstract class DigestBase extends MessageDigestSpi implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // one element byte array, temporary storage for update(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte[] oneByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // algorithm name to use in the exception message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // length of the message digest in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final int digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // size of the input to the compression function in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // buffer to store partial blocks, blockSize bytes large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Subclasses should not access this array directly except possibly in their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // implDigest() method. See MD5.java as an example.
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    63
    byte[] buffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // offset into buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // number of bytes processed so far. subclasses should not modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // also used as a flag to indicate reset status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // -1: need to call engineReset() before next call to update()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    //  0: is already reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    long bytesProcessed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Main constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    DigestBase(String algorithm, int digestLength, int blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.digestLength = digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.blockSize = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        buffer = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // return digest length. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected final int engineGetDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // single byte update. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected final void engineUpdate(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (oneByte == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            oneByte = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        oneByte[0] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        engineUpdate(oneByte, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // array update. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected final void engineUpdate(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (bytesProcessed < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // if buffer is not empty, we need to fill it before proceeding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (bufOfs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int n = Math.min(len, blockSize - bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.arraycopy(b, ofs, buffer, bufOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            bufOfs += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ofs += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (bufOfs >= blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // compress completed block now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                implCompress(buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // compress complete blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (len >= blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            implCompress(b, ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            len -= blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ofs += blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // copy remainder to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.arraycopy(b, ofs, buffer, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            bufOfs = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // reset this object. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected final void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (bytesProcessed == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // already reset, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        bytesProcessed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // return the digest. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected final byte[] engineDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        byte[] b = new byte[digestLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            engineDigest(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw (ProviderException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                new ProviderException("Internal error").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // return the digest in the specified array. See JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected final int engineDigest(byte[] out, int ofs, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws DigestException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (len < digestLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new DigestException("Length must be at least "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                + digestLength + " for " + algorithm + "digests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new DigestException("Buffer too short to store digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (bytesProcessed < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        implDigest(out, ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        bytesProcessed = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Core compression function. Processes blockSize bytes at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * and updates the state of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    abstract void implCompress(byte[] b, int ofs);
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. Subclasses do not need to reset() themselves,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * DigestBase calls implReset() when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    abstract void implDigest(byte[] out, int ofs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Reset subclass specific state to their initial values. DigestBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * calls this method when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    abstract void implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   196
    public Object clone() throws CloneNotSupportedException {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   197
        DigestBase copy = (DigestBase) super.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   198
        copy.buffer = copy.buffer.clone();
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   199
        return copy;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   200
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // padding used for the MD5, and SHA-* message digests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static final byte[] padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // we need 128 byte padding for SHA-384/512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // and an additional 8 bytes for the high 8 bytes of the 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // byte bit counter in SHA-384/512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        padding = new byte[136];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        padding[0] = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}