1/*
2 * Copyright (c) 2003, 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 4898468 6994008
27 * @summary basic test for RSA cipher
28 * @author Andreas Sterbenz
29 * @library ..
30 * @key randomness
31 * @modules jdk.crypto.cryptoki
32 * @run main/othervm TestRSACipher
33 * @run main/othervm TestRSACipher sm
34 */
35
36import java.security.GeneralSecurityException;
37import java.security.KeyPair;
38import java.security.KeyPairGenerator;
39import java.security.PrivateKey;
40import java.security.Provider;
41import java.security.PublicKey;
42import java.util.Arrays;
43import java.util.Random;
44import javax.crypto.BadPaddingException;
45import javax.crypto.Cipher;
46import javax.crypto.IllegalBlockSizeException;
47
48public class TestRSACipher extends PKCS11Test {
49
50    private static final String[] RSA_ALGOS =
51        { "RSA/ECB/PKCS1Padding", "RSA" };
52
53    @Override
54    public void main(Provider p) throws Exception {
55        try {
56            Cipher.getInstance(RSA_ALGOS[0], p);
57        } catch (GeneralSecurityException e) {
58            System.out.println("Not supported by provider, skipping");
59            return;
60        }
61        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
62        kpg.initialize(1024);
63        KeyPair kp = kpg.generateKeyPair();
64        PublicKey publicKey = kp.getPublic();
65        PrivateKey privateKey = kp.getPrivate();
66        Random random = new Random();
67        byte[] b, e, d;
68        b = new byte[16];
69        random.nextBytes(b);
70
71        for (String rsaAlgo: RSA_ALGOS) {
72            Cipher c1 = Cipher.getInstance(rsaAlgo, p);
73            Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");
74
75            c1.init(Cipher.ENCRYPT_MODE, publicKey);
76            e = c1.doFinal(b);
77            c1.init(Cipher.DECRYPT_MODE, privateKey);
78            d = c1.doFinal(e);
79            match(b, d);
80            c2.init(Cipher.DECRYPT_MODE, privateKey);
81            d = c2.doFinal(e);
82            match(b, d);
83
84            // invalid data
85            c1.init(Cipher.DECRYPT_MODE, publicKey);
86            try {
87                d = c1.doFinal(e);
88                throw new Exception("completed call");
89            } catch (BadPaddingException ee) {
90                ee.printStackTrace();
91            }
92
93            c1.init(Cipher.ENCRYPT_MODE, privateKey);
94            e = c1.doFinal(b);
95            c1.init(Cipher.DECRYPT_MODE, publicKey);
96            d = c1.doFinal(e);
97            match(b, d);
98            c2.init(Cipher.DECRYPT_MODE, publicKey);
99            d = c2.doFinal(e);
100            match(b, d);
101
102            // reinit tests
103            c1.init(Cipher.ENCRYPT_MODE, privateKey);
104            c1.init(Cipher.ENCRYPT_MODE, privateKey);
105            e = c1.doFinal(b);
106            e = c1.doFinal(b);
107            c1.update(b);
108            c1.update(b);
109            c1.init(Cipher.ENCRYPT_MODE, privateKey);
110            e = c1.doFinal();
111            e = c1.doFinal();
112            c1.update(b);
113            e = c1.doFinal();
114
115            c1.update(new byte[256]);
116            try {
117                e = c1.doFinal();
118                throw new Exception("completed call");
119            } catch (IllegalBlockSizeException ee) {
120                System.out.println(ee);
121            }
122        }
123    }
124
125    private static void match(byte[] b1, byte[] b2) throws Exception {
126        if (Arrays.equals(b1, b2) == false) {
127            System.out.println(toString(b1));
128            System.out.println(toString(b2));
129            throw new Exception("mismatch");
130        }
131    }
132
133    public static void main(String[] args) throws Exception {
134        main(new TestRSACipher(), args);
135    }
136
137}
138