jdk/test/com/sun/crypto/provider/CICO/PBEFunc/PBKDF2Wrapper.java
author tyan
Wed, 23 Sep 2015 21:28:14 +0000
changeset 32766 2322e6c2a032
permissions -rw-r--r--
8048604: Tests for strong crypto ciphers Summary: SQE test co-location effort Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32766
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     1
/*
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     4
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     8
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    13
 * accompanied this code).
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    14
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    18
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    21
 * questions.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    22
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    23
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    24
import java.security.GeneralSecurityException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    25
import java.security.Provider;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    26
import java.security.Security;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    27
import javax.crypto.Cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    28
import javax.crypto.SecretKey;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    29
import javax.crypto.SecretKeyFactory;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    30
import javax.crypto.spec.IvParameterSpec;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    31
import javax.crypto.spec.PBEKeySpec;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    32
import javax.crypto.spec.SecretKeySpec;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    33
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    34
/**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    35
 * Wrapper class to test a given SecretKeyFactory.PBKDF2 algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    36
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    37
public class PBKDF2Wrapper extends AbstractPBEWrapper {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    38
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    39
     * Default salt size.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    40
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    41
    public static final int PBKDF2_SALT_SIZE = 64;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    42
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    43
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    44
     * Default key length.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    45
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    46
    public static final int PKDF2_DEFAULT_KEY_LEN = 128;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    47
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    48
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    49
     * Default transformation.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    50
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    51
    public static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    52
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    53
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    54
     * Algorithm name.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    55
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    56
    public static final String KEY_ALGORITHM = "AES";
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    57
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    58
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    59
     * Initialization vector length.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    60
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    61
    private static final int IV_LENGTH = 16;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    62
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    63
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    64
     * The buffer with the IV.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    65
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    66
    private final byte[] iv;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    67
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    68
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    69
     * PBKDF2Wrapper constructor. Instantiate Cipher using
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    70
     * "AES/CBC/PKCS5Padding" transformation. Generate a secret key using PKDF2
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    71
     * algorithms given in the "algo" parameter.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    72
     *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    73
     * @param algo AES-based PBE algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    74
     * @param passwd password phrase.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    75
     * @throws GeneralSecurityException all security exceptions are thrown.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    76
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    77
    public PBKDF2Wrapper(PBEAlgorithm algo, String passwd)
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    78
            throws GeneralSecurityException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    79
        super(algo, passwd, PBKDF2_SALT_SIZE);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    80
        iv = TestUtilities.generateBytes(IV_LENGTH);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    81
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    82
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    83
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    84
     * Initiate the Cipher object for PBKDF2 algorithm using given "mode".
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    85
     *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    86
     * @param mode Cipher mode: encrypt or decrypt
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    87
     * @return Cipher object for PBKDF2 algorithm
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    88
     * @throws GeneralSecurityException all security exceptions are thrown.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    89
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    90
    @Override
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    91
    protected Cipher initCipher(int mode) throws GeneralSecurityException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    92
        Provider provider = Security.getProvider("SunJCE");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    93
        if (provider == null) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    94
            throw new RuntimeException("SunJCE provider does not exist.");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    95
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    96
        // Generate secret key
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    97
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    98
                salt, DEFAULT_ITERATION, PKDF2_DEFAULT_KEY_LEN);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    99
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(baseAlgo);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   100
        SecretKey key = keyFactory.generateSecret(pbeKeySpec);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   101
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   102
        // get Cipher instance
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   103
        Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION, provider);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   104
        cipher.init(mode,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   105
                new SecretKeySpec(key.getEncoded(),KEY_ALGORITHM),
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   106
                new IvParameterSpec(iv));
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   107
        return cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   108
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   109
}