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