Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestRSACipher.java
38855 views
1
/*
2
* Copyright (c) 2003, 2016, 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
* @run main/othervm TestRSACipher
32
* @run main/othervm TestRSACipher sm
33
*/
34
35
import java.security.GeneralSecurityException;
36
import java.security.KeyPair;
37
import java.security.KeyPairGenerator;
38
import java.security.PrivateKey;
39
import java.security.Provider;
40
import java.security.PublicKey;
41
import java.util.Arrays;
42
import java.util.Random;
43
import javax.crypto.BadPaddingException;
44
import javax.crypto.Cipher;
45
import javax.crypto.IllegalBlockSizeException;
46
47
public class TestRSACipher 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("Not supported by provider, skipping");
58
return;
59
}
60
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
61
kpg.initialize(1024);
62
KeyPair kp = kpg.generateKeyPair();
63
PublicKey publicKey = kp.getPublic();
64
PrivateKey privateKey = kp.getPrivate();
65
Random random = new Random();
66
byte[] b, e, d;
67
b = new byte[16];
68
random.nextBytes(b);
69
70
for (String rsaAlgo: RSA_ALGOS) {
71
Cipher c1 = Cipher.getInstance(rsaAlgo, p);
72
Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");
73
74
c1.init(Cipher.ENCRYPT_MODE, publicKey);
75
e = c1.doFinal(b);
76
c1.init(Cipher.DECRYPT_MODE, privateKey);
77
d = c1.doFinal(e);
78
match(b, d);
79
c2.init(Cipher.DECRYPT_MODE, privateKey);
80
d = c2.doFinal(e);
81
match(b, d);
82
83
// invalid data
84
c1.init(Cipher.DECRYPT_MODE, publicKey);
85
try {
86
d = c1.doFinal(e);
87
throw new Exception("completed call");
88
} catch (BadPaddingException ee) {
89
ee.printStackTrace();
90
}
91
92
c1.init(Cipher.ENCRYPT_MODE, privateKey);
93
e = c1.doFinal(b);
94
c1.init(Cipher.DECRYPT_MODE, publicKey);
95
d = c1.doFinal(e);
96
match(b, d);
97
c2.init(Cipher.DECRYPT_MODE, publicKey);
98
d = c2.doFinal(e);
99
match(b, d);
100
101
// reinit tests
102
c1.init(Cipher.ENCRYPT_MODE, privateKey);
103
c1.init(Cipher.ENCRYPT_MODE, privateKey);
104
e = c1.doFinal(b);
105
e = c1.doFinal(b);
106
c1.update(b);
107
c1.update(b);
108
c1.init(Cipher.ENCRYPT_MODE, privateKey);
109
e = c1.doFinal();
110
e = c1.doFinal();
111
c1.update(b);
112
e = c1.doFinal();
113
114
c1.update(new byte[256]);
115
try {
116
e = c1.doFinal();
117
throw new Exception("completed call");
118
} catch (IllegalBlockSizeException ee) {
119
System.out.println(ee);
120
}
121
}
122
}
123
124
private static void match(byte[] b1, byte[] b2) throws Exception {
125
if (Arrays.equals(b1, b2) == false) {
126
System.out.println(toString(b1));
127
System.out.println(toString(b2));
128
throw new Exception("mismatch");
129
}
130
}
131
132
public static void main(String[] args) throws Exception {
133
main(new TestRSACipher(), args);
134
}
135
136
}
137
138