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