Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java
66646 views
1
/*
2
* Copyright (c) 2021, 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 8265500
27
* @summary
28
* @library /test/lib ..
29
* @modules jdk.crypto.cryptoki
30
* @run main/othervm TestCipherMode
31
*/
32
33
import java.security.Provider;
34
import java.security.Key;
35
import java.security.KeyPair;
36
import java.security.KeyPairGenerator;
37
import java.security.PrivateKey;
38
import java.security.PublicKey;
39
import java.security.InvalidParameterException;
40
import java.security.NoSuchAlgorithmException;
41
import java.util.Arrays;
42
import javax.crypto.Cipher;
43
import javax.crypto.SecretKey;
44
import javax.crypto.spec.SecretKeySpec;
45
46
public class TestCipherMode extends PKCS11Test {
47
48
private static String[] TRANSFORMATIONS = {
49
"AES/ECB/PKCS5Padding", "AES/GCM/NoPadding",
50
"RSA/ECB/PKCS1Padding"
51
};
52
53
private static byte[] BYTES16 =
54
Arrays.copyOf(TRANSFORMATIONS[0].getBytes(), 16);
55
private static SecretKey AES_KEY = new SecretKeySpec(BYTES16, "AES");
56
private static PublicKey RSA_PUBKEY = null;
57
private static PrivateKey RSA_PRIVKEY = null;
58
59
enum CipherMode {
60
ENCRYPT(Cipher.ENCRYPT_MODE),
61
DECRYPT(Cipher.DECRYPT_MODE),
62
WRAP(Cipher.WRAP_MODE),
63
UNWRAP(Cipher.UNWRAP_MODE),
64
NONEXISTENT(100);
65
66
int value;
67
68
CipherMode(int value) {
69
this.value = value;
70
}
71
}
72
73
private static Key getKey(String t, CipherMode m, Provider p)
74
throws NoSuchAlgorithmException {
75
if (t.startsWith("AES")) {
76
return AES_KEY;
77
} else if (t.startsWith("RSA")) {
78
if (RSA_PUBKEY == null) {
79
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
80
KeyPair kp = kpg.generateKeyPair();
81
RSA_PUBKEY = kp.getPublic();
82
RSA_PRIVKEY = kp.getPrivate();
83
}
84
return ((m == CipherMode.ENCRYPT || m == CipherMode.UNWRAP)?
85
RSA_PRIVKEY : RSA_PUBKEY);
86
} else {
87
throw new RuntimeException("Unknown transformation: " + t);
88
}
89
}
90
91
public static void main(String[] args) throws Exception {
92
main(new TestCipherMode(), args);
93
}
94
95
@Override
96
public void main(Provider p) throws Exception {
97
98
// test all cipher impls, e.g. P11Cipher, P11AEADCipher, and
99
// P11RSACipher
100
for (String t : TRANSFORMATIONS) {
101
checkModes(t, p);
102
}
103
System.out.println("All tests passed");
104
}
105
106
private static void checkModes(String t, Provider p) throws Exception {
107
try {
108
Cipher.getInstance(t, p);
109
} catch (Exception e) {
110
System.out.println("Skip " + t + " due to " + e.getMessage());
111
return;
112
}
113
114
for (CipherMode m : CipherMode.values()) {
115
System.out.println("Testing " + t + " with " + m.name());
116
Cipher c;
117
try {
118
c = Cipher.getInstance(t, p);
119
// try init and see if the expected Exception is thrown
120
c.init(m.value, getKey(t, m, p), c.getParameters());
121
if (m == CipherMode.NONEXISTENT) {
122
throw new Exception("ERROR: should throw IPE with init()");
123
}
124
} catch (UnsupportedOperationException uoe) {
125
// some may not support wrap/unwrap
126
if (m == CipherMode.WRAP || m == CipherMode.UNWRAP) {
127
System.out.println("Expected UOE thrown with init()");
128
continue;
129
}
130
throw uoe;
131
} catch (InvalidParameterException ipe) {
132
if (m == CipherMode.NONEXISTENT) {
133
System.out.println("Expected IPE thrown for init()");
134
continue;
135
}
136
throw ipe;
137
}
138
switch (m) {
139
case ENCRYPT:
140
case DECRYPT:
141
// call wrap()/unwrap() and see if ISE is thrown.
142
try {
143
c.wrap(AES_KEY);
144
throw new Exception("ERROR: should throw ISE for wrap()");
145
} catch (IllegalStateException ise) {
146
System.out.println("Expected ISE thrown for wrap()");
147
}
148
try {
149
c.unwrap(BYTES16, "AES", Cipher.SECRET_KEY);
150
throw new Exception("ERROR: should throw ISE for unwrap()");
151
} catch (IllegalStateException ise) {
152
System.out.println("Expected ISE thrown for unwrap()");
153
}
154
break;
155
case WRAP:
156
case UNWRAP:
157
try {
158
c.update(BYTES16);
159
throw new Exception("ERROR: should throw ISE for update()");
160
} catch (IllegalStateException ise) {
161
System.out.println("Expected ISE thrown for update()");
162
}
163
try {
164
c.doFinal();
165
throw new Exception("ERROR: should throw ISE for" +
166
" doFinal()");
167
} catch (IllegalStateException ise) {
168
System.out.println("Expected ISE thrown for doFinal()");
169
}
170
break;
171
default:
172
throw new AssertionError();
173
}
174
}
175
}
176
}
177
178