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/TestRSACipherWrap.java
38855 views
1
/*
2
* Copyright (c) 2008, 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 6572331 6994008
27
* @summary basic test for RSA cipher key wrapping functionality
28
* @author Valerie Peng
29
* @library ..
30
* @run main/othervm TestRSACipherWrap
31
* @run main/othervm TestRSACipherWrap sm
32
*/
33
34
import java.security.GeneralSecurityException;
35
import java.security.InvalidParameterException;
36
import java.security.Key;
37
import java.security.KeyPair;
38
import java.security.KeyPairGenerator;
39
import java.security.Provider;
40
import java.util.Arrays;
41
import javax.crypto.Cipher;
42
import javax.crypto.KeyGenerator;
43
import javax.crypto.SecretKey;
44
import javax.crypto.spec.SecretKeySpec;
45
46
public class TestRSACipherWrap extends PKCS11Test {
47
48
private static final String[] RSA_ALGOS =
49
{ "RSA/ECB/PKCS1Padding", "RSA" };
50
51
@Override
52
public void main(Provider p) throws Exception {
53
try {
54
Cipher.getInstance(RSA_ALGOS[0], p);
55
} catch (GeneralSecurityException e) {
56
System.out.println(RSA_ALGOS[0] + " unsupported, skipping");
57
return;
58
}
59
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
60
kpg.initialize(1024);
61
KeyPair kp = kpg.generateKeyPair();
62
63
for (String rsaAlgo: RSA_ALGOS) {
64
Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);
65
Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");
66
67
String algos[] = {"AES", "RC2", "Blowfish"};
68
int keySizes[] = {128, 256};
69
70
for (int j = 0; j < algos.length; j++) {
71
String algorithm = algos[j];
72
KeyGenerator keygen =
73
KeyGenerator.getInstance(algorithm);
74
75
for (int i = 0; i < keySizes.length; i++) {
76
SecretKey secretKey = null;
77
System.out.print("Generate " + keySizes[i] + "-bit " +
78
algorithm + " key using ");
79
try {
80
keygen.init(keySizes[i]);
81
secretKey = keygen.generateKey();
82
System.out.println(keygen.getProvider().getName());
83
} catch (InvalidParameterException ipe) {
84
secretKey = new SecretKeySpec(new byte[32], algorithm);
85
System.out.println("SecretKeySpec class");
86
}
87
test(kp, secretKey, cipherPKCS11, cipherJce);
88
test(kp, secretKey, cipherPKCS11, cipherPKCS11);
89
test(kp, secretKey, cipherJce, cipherPKCS11);
90
}
91
}
92
}
93
}
94
95
private static void test(KeyPair kp, SecretKey secretKey,
96
Cipher wrapCipher, Cipher unwrapCipher)
97
throws Exception {
98
String algo = secretKey.getAlgorithm();
99
wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
100
byte[] wrappedKey = wrapCipher.wrap(secretKey);
101
unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
102
Key unwrappedKey =
103
unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
104
105
System.out.println("Test " + wrapCipher.getProvider().getName() +
106
"/" + unwrapCipher.getProvider().getName() + ": ");
107
if (!Arrays.equals(secretKey.getEncoded(),
108
unwrappedKey.getEncoded())) {
109
throw new Exception("Test Failed!");
110
}
111
System.out.println("Passed");
112
}
113
114
public static void main(String[] args) throws Exception {
115
main(new TestRSACipherWrap(), args);
116
}
117
}
118
119