jdk/test/com/sun/crypto/provider/CICO/PBEFunc/AESPBEWrapper.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.AlgorithmParameters;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    25
import java.security.GeneralSecurityException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    26
import java.security.Provider;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    27
import java.security.Security;
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.Cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    30
import javax.crypto.SecretKeyFactory;
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
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    33
/**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    34
 * Wrapper class to test a given AES-based PBE algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    35
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    36
public class AESPBEWrapper extends AbstractPBEWrapper {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    37
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    38
     * the algorithm parameters.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    39
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    40
    private AlgorithmParameters pbeParams;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    41
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    42
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    43
     * the encryption key.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    44
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    45
    private final SecretKey key;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    46
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    47
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    48
     * The Wrapper constructor. Instantiate Cipher using the given AES-based PBE
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    49
     * algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    50
     *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    51
     * @param algo AES-based PBE algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    52
     * @param passwd password phrase.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    53
     * @throws GeneralSecurityException all security exceptions are thrown.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    54
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    55
    public AESPBEWrapper(PBEAlgorithm algo, String passwd)
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    56
            throws GeneralSecurityException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    57
        // salt and iteration count will be generated during encryption
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    58
        super(algo, passwd, 0);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    59
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    60
        // Generate secret key. We expect no mode and padding specified.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    61
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.baseAlgo);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    62
        key = skf.generateSecret(new PBEKeySpec(passwd.toCharArray()));
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    63
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    64
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    65
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    66
     * Initiate the Cipher object using given "mode".
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    67
     * @return a cipher object.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    68
     * @throws GeneralSecurityException all security exceptions are thrown.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    69
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    70
    @Override
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    71
    protected Cipher initCipher(int mode) throws GeneralSecurityException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    72
        Provider provider = Security.getProvider("SunJCE");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    73
        if (provider == null) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    74
            throw new RuntimeException("SunJCE provider does not exist.");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    75
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    76
        // get Cipher instance
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    77
        Cipher ci = Cipher.getInstance(transformation, provider);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    78
        if (Cipher.ENCRYPT_MODE == mode) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    79
            ci.init(Cipher.ENCRYPT_MODE, key);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    80
            pbeParams = ci.getParameters();
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    81
        } else {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    82
            ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    83
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    84
        return ci;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    85
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    86
}