jdk/src/share/classes/com/sun/crypto/provider/PBMAC1Core.java
author valeriep
Tue, 08 Jan 2013 11:55:21 -0800
changeset 15010 ec6b49ce42b1
parent 14405 e7fff80005c1
child 17918 37620367ceb7
permissions -rw-r--r--
8004044: Lazily instantiate SunJCE.RANDOM Summary: Replace the static initialization of SunJCE.RANDOM object w/ lazy initialization Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     1
/*
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     4
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    10
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    16
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    20
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    23
 * questions.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    24
 */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    25
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    26
package com.sun.crypto.provider;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    27
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    28
import java.util.Arrays;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    29
import java.nio.ByteBuffer;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    30
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    31
import javax.crypto.MacSpi;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    32
import javax.crypto.SecretKey;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    33
import javax.crypto.spec.SecretKeySpec;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    34
import javax.crypto.spec.PBEKeySpec;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    35
import javax.crypto.spec.PBEParameterSpec;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    36
import java.security.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    37
import java.security.spec.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    38
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    39
/**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    40
 * This is an implementation of the PBMAC1 algorithms as defined
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    41
 * in PKCS#5 v2.1 standard.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    42
 */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    43
abstract class PBMAC1Core extends HmacCore {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    44
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    45
    private static final int DEFAULT_SALT_LENGTH = 20;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    46
    private static final int DEFAULT_COUNT = 4096;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    47
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    48
    private final String kdfAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    49
    private final String hashAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    50
    private final PBKDF2Core kdf;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    51
    private final int blockLength; // in octets
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    52
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    53
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    54
     * Creates an instance of PBMAC1 according to the selected
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    55
     * password-based key derivation function.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    56
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    57
    PBMAC1Core(String kdfAlgo, String hashAlgo, int blockLength)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    58
        throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    59
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    60
        super(hashAlgo, blockLength);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    61
        this.kdfAlgo = kdfAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    62
        this.hashAlgo = hashAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    63
        this.blockLength = blockLength;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    64
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    65
        switch(kdfAlgo) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    66
        case "HmacSHA1":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    67
                kdf = new PBKDF2Core.HmacSHA1();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    68
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    69
        case "HmacSHA224":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    70
                kdf = new PBKDF2Core.HmacSHA224();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    71
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    72
        case "HmacSHA256":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    73
                kdf = new PBKDF2Core.HmacSHA256();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    74
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    75
        case "HmacSHA384":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    76
                kdf = new PBKDF2Core.HmacSHA384();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    77
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    78
        case "HmacSHA512":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    79
                kdf = new PBKDF2Core.HmacSHA512();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    80
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    81
        default:
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    82
                throw new NoSuchAlgorithmException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    83
                    "No MAC implementation for " + kdfAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    84
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    85
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    86
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    87
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    88
     * Initializes the HMAC with the given secret key and algorithm parameters.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    89
     *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    90
     * @param key the secret key.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    91
     * @param params the algorithm parameters.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    92
     *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    93
     * @exception InvalidKeyException if the given key is inappropriate for
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    94
     * initializing this MAC.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    95
     * @exception InvalidAlgorithmParameterException if the given algorithm
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    96
     * parameters are inappropriate for this MAC.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    97
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    98
    protected void engineInit(Key key, AlgorithmParameterSpec params)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    99
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   100
        char[] passwdChars;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   101
        byte[] salt = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   102
        int iCount = 0;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   103
        if (key instanceof javax.crypto.interfaces.PBEKey) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   104
            javax.crypto.interfaces.PBEKey pbeKey =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   105
                (javax.crypto.interfaces.PBEKey) key;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   106
            passwdChars = pbeKey.getPassword();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   107
            salt = pbeKey.getSalt(); // maybe null if unspecified
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   108
            iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   109
        } else if (key instanceof SecretKey) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   110
            byte[] passwdBytes = key.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   111
            if ((passwdBytes == null) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   112
                !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   113
                throw new InvalidKeyException("Missing password");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   114
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   115
            passwdChars = new char[passwdBytes.length];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   116
            for (int i=0; i<passwdChars.length; i++) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   117
                passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   118
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   119
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   120
            throw new InvalidKeyException("SecretKey of PBE type required");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   121
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   122
        if (params == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   123
            // generate default for salt and iteration count if necessary
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   124
            if (salt == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   125
                salt = new byte[DEFAULT_SALT_LENGTH];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   126
                SunJCE.getRandom().nextBytes(salt);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   127
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   128
            if (iCount == 0) iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   129
        } else if (!(params instanceof PBEParameterSpec)) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   130
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   131
                ("PBEParameterSpec type required");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   132
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   133
            PBEParameterSpec pbeParams = (PBEParameterSpec) params;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   134
            // make sure the parameter values are consistent
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   135
            if (salt != null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   136
                if (!Arrays.equals(salt, pbeParams.getSalt())) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   137
                    throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   138
                        ("Inconsistent value of salt between key and params");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   139
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   140
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   141
                salt = pbeParams.getSalt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   142
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   143
            if (iCount != 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   144
                if (iCount != pbeParams.getIterationCount()) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   145
                    throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   146
                        ("Different iteration count between key and params");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   147
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   148
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   149
                iCount = pbeParams.getIterationCount();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   150
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   151
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   152
        // For security purpose, we need to enforce a minimum length
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   153
        // for salt; just require the minimum salt length to be 8-byte
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   154
        // which is what PKCS#5 recommends and openssl does.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   155
        if (salt.length < 8) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   156
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   157
                ("Salt must be at least 8 bytes long");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   158
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   159
        if (iCount <= 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   160
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   161
                ("IterationCount must be a positive number");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   162
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   163
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   164
        PBEKeySpec pbeSpec =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   165
            new PBEKeySpec(passwdChars, salt, iCount, blockLength);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   166
            // password char[] was cloned in PBEKeySpec constructor,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   167
            // so we can zero it out here
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   168
        java.util.Arrays.fill(passwdChars, ' ');
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   169
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   170
        SecretKey s = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   171
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   172
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   173
            s = kdf.engineGenerateSecret(pbeSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   174
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   175
        } catch (InvalidKeySpecException ikse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   176
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   177
                new InvalidKeyException("Cannot construct PBE key");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   178
            ike.initCause(ikse);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   179
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   180
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   181
        byte[] derivedKey = s.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   182
        SecretKey cipherKey = new SecretKeySpec(derivedKey, kdfAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   183
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   184
        super.engineInit(cipherKey, null);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   185
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   186
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   187
    public static final class HmacSHA1 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   188
        public HmacSHA1() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   189
            super("HmacSHA1", "SHA1", 64);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   190
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   191
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   192
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   193
    public static final class HmacSHA224 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   194
        public HmacSHA224() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   195
            super("HmacSHA224", "SHA-224", 64);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   196
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   197
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   198
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   199
    public static final class HmacSHA256 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   200
        public HmacSHA256() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   201
            super("HmacSHA256", "SHA-256", 64);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   202
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   203
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   204
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   205
    public static final class HmacSHA384 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   206
        public HmacSHA384() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   207
            super("HmacSHA384", "SHA-384", 128);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   208
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   209
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   210
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   211
    public static final class HmacSHA512 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   212
        public HmacSHA512() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   213
            super("HmacSHA512", "SHA-512", 128);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   214
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   215
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   216
}