jdk/src/share/classes/com/sun/crypto/provider/HmacCore.java
author wetmore
Mon, 03 Aug 2009 18:06:51 -0700
changeset 3353 ddbd63234844
parent 2 90ce3da70b43
child 4348 5b1eb97d243a
permissions -rw-r--r--
6647452: Remove obfuscation, framework and provider self-verification checking Reviewed-by: valeriep, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
     2
 * Copyright 2002-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.MacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class constitutes the core of HMAC-<MD> algorithms, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <MD> can be SHA1 or MD5, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * It also contains the implementation classes for the SHA-256,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SHA-384, and SHA-512 HMACs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class HmacCore implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final byte[] k_ipad; // inner padding - key XORd with ipad
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final byte[] k_opad; // outer padding - key XORd with opad
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean first;       // Is this the first data to be processed?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final int blockLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Standard constructor, creates a new HmacCore instance using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * specified MessageDigest object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    HmacCore(MessageDigest md, int bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.md = md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.blockLen = bl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.k_ipad = new byte[blockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.k_opad = new byte[blockLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Standard constructor, creates a new HmacCore instance instantiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * a MessageDigest of the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    HmacCore(String digestAlgorithm, int bl) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this(MessageDigest.getInstance(digestAlgorithm), bl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Constructor used for cloning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private HmacCore(HmacCore other) throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.md = (MessageDigest)other.md.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.blockLen = other.blockLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.k_ipad = (byte[])other.k_ipad.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.k_opad = (byte[])other.k_opad.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.first = other.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Returns the length of the HMAC in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return the HMAC length in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int getDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return this.md.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Initializes the HMAC with the given secret key and algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param key the secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param params the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * initializing this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * parameters are inappropriate for this MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                ("HMAC does not use parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!(key instanceof SecretKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new InvalidKeyException("Secret key expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        byte[] secret = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (secret == null || secret.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new InvalidKeyException("Missing key data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // if key is longer than the block length, reset it using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // the message digest object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (secret.length > blockLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            byte[] tmp = md.digest(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // now erase the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            Arrays.fill(secret, (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            secret = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // XOR k with ipad and opad, respectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < blockLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int si = (i < secret.length) ? secret[i] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            k_ipad[i] = (byte)(si ^ 0x36);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            k_opad[i] = (byte)(si ^ 0x5c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // now erase the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Arrays.fill(secret, (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        secret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Processes the given byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param input the input byte to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void update(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (first == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // compute digest for 1st pass; start with inner pad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            md.update(k_ipad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // add the passed byte to the inner digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        md.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Processes the first <code>len</code> bytes in <code>input</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * starting at <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param input the input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param offset the offset in <code>input</code> where the input starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param len the number of bytes to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void update(byte input[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (first == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // compute digest for 1st pass; start with inner pad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            md.update(k_ipad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // add the selected part of an array of bytes to the inner digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        md.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    void update(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (first == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // compute digest for 1st pass; start with inner pad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            md.update(k_ipad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        md.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Completes the HMAC computation and resets the HMAC for further use,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * maintaining the secret key that the HMAC was initialized with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the HMAC result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    byte[] doFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (first == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // compute digest for 1st pass; start with inner pad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            md.update(k_ipad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // finish the inner digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            byte[] tmp = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // compute digest for 2nd pass; start with outer pad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            md.update(k_opad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // add result of 1st hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            md.update(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            md.digest(tmp, 0, tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new ProviderException(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Resets the HMAC for further use, maintaining the secret key that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * HMAC was initialized with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (first == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Clones this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return new HmacCore(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // nested static class for the HmacSHA256 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static final class HmacSHA256 extends MacSpi implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        private final HmacCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        public HmacSHA256() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            core = new HmacCore("SHA-256", 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        private HmacSHA256(HmacSHA256 base) throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            core = (HmacCore)base.core.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        protected int engineGetMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return core.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        protected void engineInit(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            core.init(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        protected void engineUpdate(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        protected void engineUpdate(byte input[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            core.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        protected byte[] engineDoFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return core.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        protected void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            core.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return new HmacSHA256(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // nested static class for the HmacSHA384 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public static final class HmacSHA384 extends MacSpi implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        private final HmacCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public HmacSHA384() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            core = new HmacCore("SHA-384", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        private HmacSHA384(HmacSHA384 base) throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            core = (HmacCore)base.core.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        protected int engineGetMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return core.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        protected void engineInit(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            core.init(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        protected void engineUpdate(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        protected void engineUpdate(byte input[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            core.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        protected byte[] engineDoFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return core.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        protected void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            core.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return new HmacSHA384(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    // nested static class for the HmacSHA512 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public static final class HmacSHA512 extends MacSpi implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        private final HmacCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        public HmacSHA512() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            core = new HmacCore("SHA-512", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        private HmacSHA512(HmacSHA512 base) throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            core = (HmacCore)base.core.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        protected int engineGetMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return core.getDigestLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        protected void engineInit(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            core.init(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        protected void engineUpdate(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        protected void engineUpdate(byte input[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            core.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            core.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        protected byte[] engineDoFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return core.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        protected void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            core.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return new HmacSHA512(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}