jdk/src/java.base/share/classes/javax/crypto/Mac.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 10336 jdk/src/share/classes/javax/crypto/Mac.java@0bb1999251f8
child 26736 5a93000b26cd
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 1998, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private MacSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // The name of the MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Has this object been initialized?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // next service to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // null once provider is selected
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    98
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private final Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Creates a MAC object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param macSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected Mac(MacSpi macSpi, Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.spi = macSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   117
    private Mac(Service s, Iterator<Service> t, String algorithm) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        firstService = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        serviceIterator = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the algorithm name of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the algorithm name of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns a <code>Mac</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * MacSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @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
   151
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   152
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   153
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          MacSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static final Mac getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   166
        List<Service> services = GetInstance.getServices("Mac", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // 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
   168
        Iterator<Service> t = services.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        while (t.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   170
            Service s = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return new Mac(s, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                ("Algorithm " + algorithm + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns a <code>Mac</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * MacSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @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
   193
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   194
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   195
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception NoSuchAlgorithmException if a MacSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static final Mac getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns a <code>Mac</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * specified MAC algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p> A new Mac object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * MacSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @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
   231
     * See the Mac section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   232
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   233
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return the new <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception NoSuchAlgorithmException if a MacSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static final Mac getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                ("Mac", MacSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return new Mac((MacSpi)instance.impl, instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * delayed provider selection is not possible because init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if ((spi != null) || (serviceIterator == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    debug.println("Mac.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                            + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   291
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    if (obj instanceof MacSpi == 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
                    spi = (MacSpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    ("Could not construct MacSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private void chooseProvider(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   334
                    s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    MacSpi spi = (MacSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (lastException instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw (InvalidAlgorithmParameterException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            String kName = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                + kName, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns the provider of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return the provider of this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Returns the length of the MAC in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return the MAC length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public final int getMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return spi.engineGetMacLength();
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
     * Initializes this <code>Mac</code> object with the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public final void init(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                spi.engineInit(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                chooseProvider(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Initializes this <code>Mac</code> object with the given key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * parameters are inappropriate for this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public final void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            spi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            chooseProvider(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Processes the given byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param input the input byte to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public final void update(byte input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Processes the given array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param input the array of bytes to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public final void update(byte[] input) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            spi.engineUpdate(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Processes the first <code>len</code> bytes in <code>input</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * starting at <code>offset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param input the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param offset the offset in <code>input</code> where the input starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param len the number of bytes to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public final void update(byte[] input, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if ((offset < 0) || (len > (input.length - offset)) || (len < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            spi.engineUpdate(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Processes <code>input.remaining()</code> bytes in the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <code>input</code>, starting at <code>input.position()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public final void update(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (input == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            throw new IllegalArgumentException("Buffer must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        spi.engineUpdate(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    public final byte[] doFinal() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        byte[] mac = spi.engineDoFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p>The MAC result is stored in <code>output</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <code>outOffset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param output the buffer where the MAC result is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @param outOffset the offset in <code>output</code> where the MAC is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public final void doFinal(byte[] output, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        throws ShortBufferException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        int macLen = getMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (output == null || output.length-outOffset < macLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                ("Cannot store MAC in output buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        byte[] mac = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        System.arraycopy(mac, 0, output, outOffset, macLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * Processes the given array of bytes and finishes the MAC operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param input data in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return the MAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @exception IllegalStateException if this <code>Mac</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public final byte[] doFinal(byte[] input) throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            throw new IllegalStateException("MAC not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Resets this <code>Mac</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <p>A call to this method resets this <code>Mac</code> object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * state it was in when previously initialized via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * That is, the object is reset and available to generate another MAC from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * the same key, if desired, via new calls to <code>update</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <code>doFinal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * (In order to reuse this <code>Mac</code> object with a different key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * it must be reinitialized via a call to <code>init(Key)</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <code>init(Key, AlgorithmParameterSpec)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public final void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        spi.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Returns a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @return a clone if the provider implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @exception CloneNotSupportedException if this is called on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * delegate that does not support <code>Cloneable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        Mac that = (Mac)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        that.spi = (MacSpi)this.spi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
}