jdk/src/java.base/share/classes/javax/crypto/Mac.java
author vinnie
Thu, 25 Sep 2014 12:24:19 +0100
changeset 26736 5a93000b26cd
parent 25859 3317bb8137f4
child 32275 17eeb583a331
permissions -rw-r--r--
8056026: Debug security logging should print Provider used for each crypto operation Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * e.g., MD5 or SHA-1, in combination with a secret shared key. HMAC is
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
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    57
 * the following standard <code>Mac</code> algorithms:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    58
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    59
 * <li><tt>HmacMD5</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    60
 * <li><tt>HmacSHA1</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    61
 * <li><tt>HmacSHA256</tt></li>
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
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    64
 * <a href="{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    65
 * Mac section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    66
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the algorithm name of this <code>Mac</code> object.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the algorithm name of this <code>Mac</code> object.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Returns a <code>Mac</code> object that implements the
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @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
   156
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   157
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   158
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *          MacSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static final Mac getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   171
        List<Service> services = GetInstance.getServices("Mac", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // 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
   173
        Iterator<Service> t = services.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        while (t.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   175
            Service s = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return new Mac(s, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                ("Algorithm " + algorithm + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns a <code>Mac</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * MacSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @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
   198
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   199
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   200
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @exception NoSuchAlgorithmException if a MacSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static final Mac getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns a <code>Mac</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * MacSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @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
   236
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   237
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   238
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception NoSuchAlgorithmException if a MacSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static final Mac getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * delayed provider selection is not possible because init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if ((spi != null) || (serviceIterator == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    debug.println("Mac.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   296
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (obj instanceof MacSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    spi = (MacSpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    ("Could not construct MacSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void chooseProvider(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   339
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    MacSpi spi = (MacSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (lastException instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw (InvalidAlgorithmParameterException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            String kName = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                + kName, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Returns the provider of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return the provider of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Returns the length of the MAC in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return the MAC length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public final int getMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return spi.engineGetMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Initializes this <code>Mac</code> object with the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public final void init(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                spi.engineInit(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                chooseProvider(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        initialized = true;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   421
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   422
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   423
            pdebug.println("Mac." + algorithm + " algorithm from: " +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   424
                this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   425
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Initializes this <code>Mac</code> object with the given key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * parameters are inappropriate for this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public final void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            chooseProvider(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        initialized = true;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   448
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   449
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   450
            pdebug.println("Mac." + algorithm + " algorithm from: " +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   451
                this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   452
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Processes the given byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param input the input byte to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public final void update(byte input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Processes the given array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param input the array of bytes to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public final void update(byte[] input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            spi.engineUpdate(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Processes the first <code>len</code> bytes in <code>input</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * starting at <code>offset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param input the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param offset the offset in <code>input</code> where the input starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param len the number of bytes to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public final void update(byte[] input, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if ((offset < 0) || (len > (input.length - offset)) || (len < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            spi.engineUpdate(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Processes <code>input.remaining()</code> bytes in the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <code>input</code>, starting at <code>input.position()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public final void update(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            throw new IllegalArgumentException("Buffer must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public final byte[] doFinal() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        byte[] mac = spi.engineDoFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <p>The MAC result is stored in <code>output</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <code>outOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param output the buffer where the MAC result is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param outOffset the offset in <code>output</code> where the MAC is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public final void doFinal(byte[] output, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        throws ShortBufferException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int macLen = getMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (output == null || output.length-outOffset < macLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                ("Cannot store MAC in output buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        byte[] mac = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        System.arraycopy(mac, 0, output, outOffset, macLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Processes the given array of bytes and finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @param input data in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public final byte[] doFinal(byte[] input) throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Resets this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public final void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @return a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @exception CloneNotSupportedException if this is called on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * delegate that does not support <code>Cloneable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        Mac that = (Mac)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        that.spi = (MacSpi)this.spi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
}