jdk/src/java.base/share/classes/com/sun/crypto/provider/PBMAC1Core.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 17918 jdk/src/share/classes/com/sun/crypto/provider/PBMAC1Core.java@37620367ceb7
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    45
    // NOTE: this class inherits the Cloneable interface from HmacCore
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    46
    // Need to override clone() if mutable fields are added.
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    47
    private final String kdfAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    48
    private final String hashAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    49
    private final int blockLength; // in octets
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    50
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    51
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    52
     * Creates an instance of PBMAC1 according to the selected
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    53
     * password-based key derivation function.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    54
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    55
    PBMAC1Core(String kdfAlgo, String hashAlgo, int blockLength)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    56
        throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    57
        super(hashAlgo, blockLength);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    58
        this.kdfAlgo = kdfAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    59
        this.hashAlgo = hashAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    60
        this.blockLength = blockLength;
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    61
    }
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    62
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    63
    private static PBKDF2Core getKDFImpl(String algo) {
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    64
        PBKDF2Core kdf = null;
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    65
        switch(algo) {
14405
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:
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    82
                throw new ProviderException(
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    83
                    "No MAC implementation for " + algo);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    84
        }
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
    85
        return kdf;
14405
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
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    89
     * 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
    90
     *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    91
     * @param key the secret key.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    92
     * @param params the algorithm parameters.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    93
     *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    94
     * @exception InvalidKeyException if the given key is inappropriate for
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    95
     * initializing this MAC.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    96
     * @exception InvalidAlgorithmParameterException if the given algorithm
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    97
     * parameters are inappropriate for this MAC.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    98
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    99
    protected void engineInit(Key key, AlgorithmParameterSpec params)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   100
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   101
        char[] passwdChars;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   102
        byte[] salt = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   103
        int iCount = 0;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   104
        if (key instanceof javax.crypto.interfaces.PBEKey) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   105
            javax.crypto.interfaces.PBEKey pbeKey =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   106
                (javax.crypto.interfaces.PBEKey) key;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   107
            passwdChars = pbeKey.getPassword();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   108
            salt = pbeKey.getSalt(); // maybe null if unspecified
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   109
            iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   110
        } else if (key instanceof SecretKey) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   111
            byte[] passwdBytes = key.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   112
            if ((passwdBytes == null) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   113
                !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   114
                throw new InvalidKeyException("Missing password");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   115
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   116
            passwdChars = new char[passwdBytes.length];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   117
            for (int i=0; i<passwdChars.length; i++) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   118
                passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   119
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   120
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   121
            throw new InvalidKeyException("SecretKey of PBE type required");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   122
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   123
        if (params == null) {
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   124
            // should not auto-generate default values since current
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   125
            // javax.crypto.Mac api does not have any method for caller to
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   126
            // retrieve the generated defaults.
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   127
            if ((salt == null) || (iCount == 0)) {
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   128
                throw new InvalidAlgorithmParameterException
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   129
                    ("PBEParameterSpec required for salt and iteration count");
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   130
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   131
        } else if (!(params instanceof PBEParameterSpec)) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   132
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   133
                ("PBEParameterSpec type required");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   134
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   135
            PBEParameterSpec pbeParams = (PBEParameterSpec) params;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   136
            // make sure the parameter values are consistent
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   137
            if (salt != null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   138
                if (!Arrays.equals(salt, pbeParams.getSalt())) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   139
                    throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   140
                        ("Inconsistent value of salt between key and params");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   141
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   142
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   143
                salt = pbeParams.getSalt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   144
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   145
            if (iCount != 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   146
                if (iCount != pbeParams.getIterationCount()) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   147
                    throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   148
                        ("Different iteration count between key and params");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   149
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   150
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   151
                iCount = pbeParams.getIterationCount();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   152
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   153
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   154
        // 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
   155
        // 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
   156
        // which is what PKCS#5 recommends and openssl does.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   157
        if (salt.length < 8) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   158
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   159
                ("Salt must be at least 8 bytes long");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   160
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   161
        if (iCount <= 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   162
            throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   163
                ("IterationCount must be a positive number");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   164
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   165
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   166
        PBEKeySpec pbeSpec =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   167
            new PBEKeySpec(passwdChars, salt, iCount, blockLength);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   168
            // password char[] was cloned in PBEKeySpec constructor,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   169
            // so we can zero it out here
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   170
        java.util.Arrays.fill(passwdChars, ' ');
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
        SecretKey s = null;
17918
37620367ceb7 8013069: javax.crypto tests fail with new PBE algorithm names
valeriep
parents: 15010
diff changeset
   173
        PBKDF2Core kdf = getKDFImpl(kdfAlgo);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   174
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   175
            s = kdf.engineGenerateSecret(pbeSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   176
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   177
        } catch (InvalidKeySpecException ikse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   178
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   179
                new InvalidKeyException("Cannot construct PBE key");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   180
            ike.initCause(ikse);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   181
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   182
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   183
        byte[] derivedKey = s.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   184
        SecretKey cipherKey = new SecretKeySpec(derivedKey, kdfAlgo);
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
        super.engineInit(cipherKey, null);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   187
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   188
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   189
    public static final class HmacSHA1 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   190
        public HmacSHA1() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   191
            super("HmacSHA1", "SHA1", 64);
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   194
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   195
    public static final class HmacSHA224 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   196
        public HmacSHA224() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   197
            super("HmacSHA224", "SHA-224", 64);
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   200
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   201
    public static final class HmacSHA256 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   202
        public HmacSHA256() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   203
            super("HmacSHA256", "SHA-256", 64);
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   206
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   207
    public static final class HmacSHA384 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   208
        public HmacSHA384() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   209
            super("HmacSHA384", "SHA-384", 128);
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   212
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   213
    public static final class HmacSHA512 extends PBMAC1Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   214
        public HmacSHA512() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   215
            super("HmacSHA512", "SHA-512", 128);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   216
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   217
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   218
}