jdk/src/java.base/share/classes/java/security/MessageDigestSpi.java
author coleenp
Wed, 30 Aug 2017 19:18:22 -0400
changeset 47098 e704f55561c3
parent 45434 4582657c7260
permissions -rw-r--r--
8164207: Checking missing load-acquire in relation to _pd_set in dictionary.cpp Summary: Use load_acquire for accessing DictionaryEntry::_pd_set since it's accessed outside the SystemDictionary_lock Reviewed-by: zgu, twisti, dholmes, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2013, 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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    34
 * for the {@code MessageDigest} class, which provides the functionality
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of a message digest algorithm, such as MD5 or SHA. Message digests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * secure one-way hash functions that take arbitrary-sized data and output a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * fixed-length hash value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> All the abstract methods in this class must be implemented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * cryptographic service provider who wishes to supply the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of a particular message digest algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> Implementations are free to implement the Cloneable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Benjamin Renaud
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 25859
diff changeset
    46
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public abstract class MessageDigestSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // for re-use in engineUpdate(ByteBuffer input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private byte[] tempArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Returns the digest length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * abstract class. (For backwards compatibility, it cannot be abstract.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <p>The default behavior is to return 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <p>This method may be overridden by a provider to return the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @return the digest length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected int engineGetDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Updates the digest using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param input the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected abstract void engineUpdate(byte input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Updates the digest using the specified array of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param input the array of bytes to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param offset the offset to start from in the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param len the number of bytes to use, starting at
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    92
     * {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected abstract void engineUpdate(byte[] input, int offset, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Update the digest using the specified ByteBuffer. The digest is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    98
     * updated using the {@code input.remaining()} bytes starting
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    99
     * at {@code input.position()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (input.hasRemaining() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (input.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            byte[] b = input.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int ofs = input.arrayOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int pos = input.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int lim = input.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            engineUpdate(b, ofs + pos, lim - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            input.position(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int len = input.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            int n = JCAUtil.getTempArraySize(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if ((tempArray == null) || (n > tempArray.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                tempArray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                int chunk = Math.min(len, tempArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                input.get(tempArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                engineUpdate(tempArray, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                len -= chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Completes the hash computation by performing final
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   134
     * operations such as padding. Once {@code engineDigest} has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * been called, the engine should be reset (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * {@link #engineReset() engineReset}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Resetting is the responsibility of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * engine implementor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the array of bytes for the resulting hash value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected abstract byte[] engineDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Completes the hash computation by performing final
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   146
     * operations such as padding. Once {@code engineDigest} has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * been called, the engine should be reset (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@link #engineReset() engineReset}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Resetting is the responsibility of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * engine implementor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * This method should be abstract, but we leave it concrete for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * binary compatibility.  Knowledgeable providers should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param buf the output buffer in which to store the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param offset offset to start from in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param len number of bytes within buf allotted for the digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Both this default implementation and the SUN provider do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * return partial digests.  The presence of this parameter is solely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * for consistency in our API's.  If the value of this parameter is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * than the actual digest length, the method will throw a DigestException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * This parameter is ignored if its value is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * the actual digest length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the length of the digest stored in the output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @exception DigestException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected int engineDigest(byte[] buf, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                throws DigestException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] digest = engineDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (len < digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw new DigestException("partial digests not returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (buf.length - offset < digest.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throw new DigestException("insufficient space in the output "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                          + "buffer to store the digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.arraycopy(digest, 0, buf, offset, digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return digest.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Resets the digest for further use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected abstract void engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception CloneNotSupportedException if this is called on an
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   198
     * implementation that does not support {@code Cloneable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (this instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}