author | jjiang |
Wed, 15 Aug 2018 18:41:18 +0800 | |
changeset 51460 | 97e361fe3433 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
1 |
/* |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
4 |
* |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
8 |
* |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
13 |
* accompanied this code). |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
14 |
* |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
18 |
* |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
21 |
* questions. |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
22 |
*/ |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
23 |
|
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
24 |
/* |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
25 |
* @test |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
26 |
* @bug 6994008 |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
27 |
* @summary basic test for RSA/ECB/NoPadding cipher |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
28 |
* @author Valerie Peng |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
29 |
* @library /test/lib .. |
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 TestRawRSACipher |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
33 |
* @run main/othervm TestRawRSACipher sm |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
34 |
*/ |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
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.Provider; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
40 |
import java.util.Arrays; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
41 |
import java.util.Random; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
42 |
import javax.crypto.Cipher; |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
43 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
44 |
public class TestRawRSACipher extends PKCS11Test { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
45 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
46 |
@Override |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
47 |
public void main(Provider p) throws Exception { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
48 |
try { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
49 |
Cipher.getInstance("RSA/ECB/NoPadding", p); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
50 |
} catch (GeneralSecurityException e) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
51 |
System.out.println("Not supported by provider, skipping"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
52 |
return; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
53 |
} |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
54 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
55 |
final int KEY_LEN = 1024; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
56 |
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", p); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
57 |
kpGen.initialize(KEY_LEN); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
58 |
KeyPair kp = kpGen.generateKeyPair(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
59 |
Random random = new Random(); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
60 |
byte[] plainText, cipherText, recoveredText; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
61 |
plainText = new byte[KEY_LEN/8]; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
62 |
random.nextBytes(plainText); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
63 |
plainText[0] = 0; // to ensure that it's less than modulus |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
64 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
65 |
Cipher c1 = Cipher.getInstance("RSA/ECB/NoPadding", p); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
66 |
Cipher c2 = Cipher.getInstance("RSA/ECB/NoPadding", "SunJCE"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
67 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
68 |
c1.init(Cipher.ENCRYPT_MODE, kp.getPublic()); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
69 |
c2.init(Cipher.DECRYPT_MODE, kp.getPrivate()); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
70 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
71 |
cipherText = c1.doFinal(plainText); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
72 |
recoveredText = c2.doFinal(cipherText); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
73 |
if (!Arrays.equals(plainText, recoveredText)) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
74 |
throw new RuntimeException("E/D Test against SunJCE Failed!"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
75 |
} |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
76 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
77 |
c2.init(Cipher.ENCRYPT_MODE, kp.getPublic()); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
78 |
c1.init(Cipher.DECRYPT_MODE, kp.getPrivate()); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
79 |
cipherText = c2.doFinal(plainText); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
80 |
recoveredText = c1.doFinal(cipherText); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
81 |
if (!Arrays.equals(plainText, recoveredText)) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
82 |
throw new RuntimeException("D/E Test against SunJCE Failed!"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
83 |
} |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
84 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
85 |
System.out.println("Test Passed"); |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
86 |
} |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
87 |
|
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
88 |
public static void main(String[] args) throws Exception { |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
89 |
main(new TestRawRSACipher(), args); |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
90 |
} |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
diff
changeset
|
91 |
} |