author | ascarpino |
Mon, 23 Jan 2017 11:49:01 -0800 | |
changeset 43248 | 5e15de85a1a0 |
parent 42693 | 6645de32a866 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43248
5e15de85a1a0
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents:
42693
diff
changeset
|
2 |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
24 |
/* |
2 | 25 |
* @test |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
26 |
* @bug 4898468 6994008 |
2 | 27 |
* @summary basic test for RSA cipher |
28 |
* @author Andreas Sterbenz |
|
29 |
* @library .. |
|
30046 | 30 |
* @key randomness |
43248
5e15de85a1a0
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents:
42693
diff
changeset
|
31 |
* @modules jdk.crypto.cryptoki |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
32 |
* @run main/othervm TestRSACipher |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
33 |
* @run main/othervm TestRSACipher sm |
2 | 34 |
*/ |
35 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
36 |
import java.security.GeneralSecurityException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
37 |
import java.security.KeyPair; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
38 |
import java.security.KeyPairGenerator; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
39 |
import java.security.PrivateKey; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
40 |
import java.security.Provider; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
41 |
import java.security.PublicKey; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
42 |
import java.util.Arrays; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
43 |
import java.util.Random; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
44 |
import javax.crypto.BadPaddingException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
45 |
import javax.crypto.Cipher; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
46 |
import javax.crypto.IllegalBlockSizeException; |
2 | 47 |
|
48 |
public class TestRSACipher extends PKCS11Test { |
|
49 |
||
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
50 |
private static final String[] RSA_ALGOS = |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
51 |
{ "RSA/ECB/PKCS1Padding", "RSA" }; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
52 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
53 |
@Override |
2 | 54 |
public void main(Provider p) throws Exception { |
55 |
try { |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
56 |
Cipher.getInstance(RSA_ALGOS[0], p); |
2 | 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 |
||
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
71 |
for (String rsaAlgo: RSA_ALGOS) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
72 |
Cipher c1 = Cipher.getInstance(rsaAlgo, p); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
73 |
Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE"); |
2 | 74 |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
75 |
c1.init(Cipher.ENCRYPT_MODE, publicKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
76 |
e = c1.doFinal(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
77 |
c1.init(Cipher.DECRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
78 |
d = c1.doFinal(e); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
79 |
match(b, d); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
80 |
c2.init(Cipher.DECRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
81 |
d = c2.doFinal(e); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
82 |
match(b, d); |
2 | 83 |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
84 |
// invalid data |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
85 |
c1.init(Cipher.DECRYPT_MODE, publicKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
86 |
try { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
87 |
d = c1.doFinal(e); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
88 |
throw new Exception("completed call"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
89 |
} catch (BadPaddingException ee) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
90 |
ee.printStackTrace(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
91 |
} |
2 | 92 |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
93 |
c1.init(Cipher.ENCRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
94 |
e = c1.doFinal(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
95 |
c1.init(Cipher.DECRYPT_MODE, publicKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
96 |
d = c1.doFinal(e); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
97 |
match(b, d); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
98 |
c2.init(Cipher.DECRYPT_MODE, publicKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
99 |
d = c2.doFinal(e); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
100 |
match(b, d); |
2 | 101 |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
102 |
// reinit tests |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
103 |
c1.init(Cipher.ENCRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
104 |
c1.init(Cipher.ENCRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
105 |
e = c1.doFinal(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
106 |
e = c1.doFinal(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
107 |
c1.update(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
108 |
c1.update(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
109 |
c1.init(Cipher.ENCRYPT_MODE, privateKey); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
110 |
e = c1.doFinal(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
111 |
e = c1.doFinal(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
112 |
c1.update(b); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
113 |
e = c1.doFinal(); |
2 | 114 |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
115 |
c1.update(new byte[256]); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
116 |
try { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
117 |
e = c1.doFinal(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
118 |
throw new Exception("completed call"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
119 |
} catch (IllegalBlockSizeException ee) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
120 |
System.out.println(ee); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
5506
diff
changeset
|
121 |
} |
2 | 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 { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
134 |
main(new TestRSACipher(), args); |
2 | 135 |
} |
136 |
||
137 |
} |