jdk/test/sun/security/pkcs11/Cipher/TestRSACipherWrap.java
author darcy
Wed, 29 Apr 2015 10:25:53 -0700
changeset 30046 cf2c86e1819e
parent 8578 f5d3509ad92b
child 35379 1e8e336ef66b
permissions -rw-r--r--
8078334: Mark regression tests using randomness Reviewed-by: xuelei, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     1
/*
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     4
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     8
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    13
 * accompanied this code).
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    14
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    21
 * questions.
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    22
 */
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    23
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    24
/**
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    25
 * @test
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    26
 * @bug 6572331 6994008
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    27
 * @summary basic test for RSA cipher key wrapping functionality
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    28
 * @author Valerie Peng
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    29
 * @library ..
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    30
 */
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    31
import java.io.*;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    32
import java.util.*;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    33
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    34
import java.security.*;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    35
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    36
import javax.crypto.*;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    37
import javax.crypto.spec.SecretKeySpec;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    38
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    39
public class TestRSACipherWrap extends PKCS11Test {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    40
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    41
    private static final String[] RSA_ALGOS =
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    42
        { "RSA/ECB/PKCS1Padding", "RSA" };
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    43
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    44
    public void main(Provider p) throws Exception {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    45
        try {
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    46
            Cipher.getInstance(RSA_ALGOS[0], p);
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    47
        } catch (GeneralSecurityException e) {
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    48
            System.out.println(RSA_ALGOS[0] + " unsupported, skipping");
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    49
            return;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    50
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    51
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    52
        kpg.initialize(1024);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    53
        KeyPair kp = kpg.generateKeyPair();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    54
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    55
        for (String rsaAlgo: RSA_ALGOS) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    56
            Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    57
            Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    58
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    59
            String algos[] = {"AES", "RC2", "Blowfish"};
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    60
            int keySizes[] = {128, 256};
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    61
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    62
            for (int j = 0; j < algos.length; j++) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    63
                String algorithm = algos[j];
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    64
                KeyGenerator keygen =
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    65
                    KeyGenerator.getInstance(algorithm);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    66
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    67
                for (int i = 0; i < keySizes.length; i++) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    68
                    SecretKey secretKey = null;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    69
                    System.out.print("Generate " + keySizes[i] + "-bit " +
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    70
                        algorithm + " key using ");
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    71
                    try {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    72
                        keygen.init(keySizes[i]);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    73
                        secretKey = keygen.generateKey();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    74
                        System.out.println(keygen.getProvider().getName());
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    75
                    } catch (InvalidParameterException ipe) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    76
                        secretKey = new SecretKeySpec(new byte[32], algorithm);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    77
                        System.out.println("SecretKeySpec class");
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    78
                    }
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    79
                    test(kp, secretKey, cipherPKCS11, cipherJce);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    80
                    test(kp, secretKey, cipherPKCS11, cipherPKCS11);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    81
                    test(kp, secretKey, cipherJce, cipherPKCS11);
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    82
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    83
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    84
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    85
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    86
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    87
    private static void test(KeyPair kp, SecretKey secretKey,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    88
            Cipher wrapCipher, Cipher unwrapCipher)
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    89
            throws Exception {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    90
        String algo = secretKey.getAlgorithm();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    91
        wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    92
        byte[] wrappedKey = wrapCipher.wrap(secretKey);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    93
        unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    94
        Key unwrappedKey =
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    95
                unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    96
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    97
        System.out.println("Test " + wrapCipher.getProvider().getName() +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    98
                "/" + unwrapCipher.getProvider().getName() + ": ");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    99
        if (!Arrays.equals(secretKey.getEncoded(),
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   100
                unwrappedKey.getEncoded())) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   101
            throw new Exception("Test Failed!");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   102
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   103
        System.out.println("Passed");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   104
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   105
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   106
    public static void main(String[] args) throws Exception {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   107
        main(new TestRSACipherWrap());
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   108
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   109
}