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