src/java.base/share/classes/javax/crypto/Mac.java
author mullan
Wed, 13 Nov 2019 13:43:06 -0500
changeset 59059 27a266d5fb13
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214483: Remove algorithms that use MD5 or DES from security requirements Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59059
27a266d5fb13 8214483: Remove algorithms that use MD5 or DES from security requirements
mullan
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, 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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class provides the functionality of a "Message Authentication Code"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (MAC) algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> A MAC provides a way to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the integrity of information transmitted over or stored in an unreliable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * medium, based on a secret key. Typically, message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * authentication codes are used between two parties that share a secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * key in order to validate information transmitted between these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> A MAC mechanism that is based on cryptographic hash functions is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * referred to as HMAC. HMAC can be used with any cryptographic hash function,
46053
6f7e93cb432a 8169080: Improve documentation examples for crypto applications
wetmore
parents: 45118
diff changeset
    53
 * e.g., SHA256 or SHA384, in combination with a secret shared key. HMAC is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * specified in RFC 2104.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    56
 * <p> Every implementation of the Java platform is required to support
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
    57
 * the following standard {@code Mac} algorithms:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    58
 * <ul>
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
    59
 * <li>{@code HmacSHA1}</li>
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
    60
 * <li>{@code HmacSHA256}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    61
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    62
 * These algorithms are described in the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
    63
 * <a href="{@docRoot}/../specs/security/standard-names.html#mac-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    64
 * Mac section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
    65
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    66
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    67
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    68
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class Mac implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        Debug.getInstance("jca", "Mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    79
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    80
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    81
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    82
        Debug.isOn("engine=") && !Debug.isOn("mac");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    83
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private MacSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // The name of the MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // Has this object been initialized?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // next service to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // null once provider is selected
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   102
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private final Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Creates a MAC object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param macSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected Mac(MacSpi macSpi, Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.spi = macSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   121
    private Mac(Service s, Iterator<Service> t, String algorithm) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        firstService = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        serviceIterator = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   129
     * Returns the algorithm name of this {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p>This is the same name that was specified in one of the
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   132
     * {@code getInstance} calls that created this
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   133
     * {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   135
     * @return the algorithm name of this {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   142
     * Returns a {@code Mac} object that implements the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * MacSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   154
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   155
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   156
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   157
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   158
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   159
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   160
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   161
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param algorithm the standard name of the requested MAC algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   163
     * See the Mac section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   164
     *   "{@docRoot}/../specs/security/standard-names.html#mac-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   165
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   168
     * @return the new {@code Mac} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   170
     * @throws NoSuchAlgorithmException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   171
     *         {@code MacSpi} implementation for the specified algorithm
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   172
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   173
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static final Mac getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   179
        Objects.requireNonNull(algorithm, "null algorithm name");
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   180
        List<Service> services = GetInstance.getServices("Mac", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // make sure there is at least one service from a signed provider
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   182
        Iterator<Service> t = services.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        while (t.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   184
            Service s = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return new Mac(s, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                ("Algorithm " + algorithm + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   195
     * Returns a {@code Mac} object that implements the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * MacSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param algorithm the standard name of the requested MAC algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   207
     * See the Mac section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   208
     *   "{@docRoot}/../specs/security/standard-names.html#mac-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   209
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   214
     * @return the new {@code Mac} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   216
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   217
     *         is {@code null} or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   219
     * @throws NoSuchAlgorithmException if a {@code MacSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   220
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   221
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   223
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   224
     *         registered in the security provider list
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   225
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   226
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public static final Mac getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throws NoSuchAlgorithmException, NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   232
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   239
     * Returns a {@code Mac} object that implements the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * MacSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param algorithm the standard name of the requested MAC algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   248
     * See the Mac section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   249
     *   "{@docRoot}/../specs/security/standard-names.html#mac-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   250
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   255
     * @return the new {@code Mac} object
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   256
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   257
     * @throws IllegalArgumentException if the {@code provider} is
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   258
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   260
     * @throws NoSuchAlgorithmException if a {@code MacSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   261
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   262
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   264
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public static final Mac getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   270
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * delayed provider selection is not possible because init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if ((spi != null) || (serviceIterator == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    debug.println("Mac.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   311
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    if (obj instanceof MacSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    spi = (MacSpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    ("Could not construct MacSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private void chooseProvider(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   354
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    MacSpi spi = (MacSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (lastException instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                throw (InvalidAlgorithmParameterException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            String kName = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                + kName, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   398
     * Returns the provider of this {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   400
     * @return the provider of this {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Returns the length of the MAC in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return the MAC length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public final int getMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return spi.engineGetMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   417
    private String getProviderName() {
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   418
        return (provider == null) ? "(no provider)" : provider.getName();
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   419
    }
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   420
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   422
     * Initializes this {@code Mac} object with the given key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public final void init(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                spi.engineInit(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                chooseProvider(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        initialized = true;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   440
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   441
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   442
            pdebug.println("Mac." + algorithm + " algorithm from: " +
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   443
                getProviderName());
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   444
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   448
     * Initializes this {@code Mac} object with the given key and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * parameters are inappropriate for this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public final void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            chooseProvider(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        initialized = true;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   467
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   468
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   469
            pdebug.println("Mac." + algorithm + " algorithm from: " +
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   470
                getProviderName());
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   471
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Processes the given byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param input the input byte to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   479
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public final void update(byte input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Processes the given array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param input the array of bytes to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   495
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public final void update(byte[] input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            spi.engineUpdate(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   509
     * Processes the first {@code len} bytes in {@code input},
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   510
     * starting at {@code offset} inclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param input the input buffer.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   513
     * @param offset the offset in {@code input} where the input starts.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param len the number of bytes to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   516
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public final void update(byte[] input, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if ((offset < 0) || (len > (input.length - offset)) || (len < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            spi.engineUpdate(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   534
     * Processes {@code input.remaining()} bytes in the ByteBuffer
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   535
     * {@code input}, starting at {@code input.position()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   541
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public final void update(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            throw new IllegalArgumentException("Buffer must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   559
     * <p>A call to this method resets this {@code Mac} object to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * state it was in when previously initialized via a call to
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   561
     * {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   562
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * That is, the object is reset and available to generate another MAC from
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   564
     * the same key, if desired, via new calls to {@code update} and
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   565
     * {@code doFinal}.
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   566
     * (In order to reuse this {@code Mac} object with a different key,
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   567
     * it must be reinitialized via a call to {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   568
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   572
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public final byte[] doFinal() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        byte[] mac = spi.engineDoFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   588
     * <p>A call to this method resets this {@code Mac} object to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * state it was in when previously initialized via a call to
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   590
     * {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   591
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * That is, the object is reset and available to generate another MAC from
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   593
     * the same key, if desired, via new calls to {@code update} and
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   594
     * {@code doFinal}.
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   595
     * (In order to reuse this {@code Mac} object with a different key,
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   596
     * it must be reinitialized via a call to {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   597
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   599
     * <p>The MAC result is stored in {@code output}, starting at
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   600
     * {@code outOffset} inclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param output the buffer where the MAC result is stored
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   603
     * @param outOffset the offset in {@code output} where the MAC is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * to hold the result
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   608
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public final void doFinal(byte[] output, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        throws ShortBufferException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int macLen = getMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (output == null || output.length-outOffset < macLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                ("Cannot store MAC in output buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        byte[] mac = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        System.arraycopy(mac, 0, output, outOffset, macLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Processes the given array of bytes and finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   631
     * <p>A call to this method resets this {@code Mac} object to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * state it was in when previously initialized via a call to
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   633
     * {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   634
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * That is, the object is reset and available to generate another MAC from
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   636
     * the same key, if desired, via new calls to {@code update} and
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   637
     * {@code doFinal}.
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   638
     * (In order to reuse this {@code Mac} object with a different key,
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   639
     * it must be reinitialized via a call to {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   640
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param input data in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   645
     * @exception IllegalStateException if this {@code Mac} has not been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public final byte[] doFinal(byte[] input) throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   659
     * Resets this {@code Mac} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   661
     * <p>A call to this method resets this {@code Mac} object to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * state it was in when previously initialized via a call to
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   663
     * {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   664
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * That is, the object is reset and available to generate another MAC from
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   666
     * the same key, if desired, via new calls to {@code update} and
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   667
     * {@code doFinal}.
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   668
     * (In order to reuse this {@code Mac} object with a different key,
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   669
     * it must be reinitialized via a call to {@code init(Key)} or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   670
     * {@code init(Key, AlgorithmParameterSpec)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public final void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Returns a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @return a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @exception CloneNotSupportedException if this is called on a
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26736
diff changeset
   683
     * delegate that does not support {@code Cloneable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Mac that = (Mac)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        that.spi = (MacSpi)this.spi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}