Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/crypto/Cipher/TestCipherMode.java
66644 views
1
/*
2
* Copyright (c) 2004, 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 4953556 8210838 8248268
27
* @summary ensure that IllegalStateException is thrown if the
28
* Cipher object is initialized with a wrong mode, e.g. WRAP_MODE
29
* for update()/doFinal() calls.
30
* @author Valerie Peng
31
*/
32
33
34
import java.security.*;
35
import java.security.spec.*;
36
import java.util.Arrays;
37
38
import javax.crypto.*;
39
import javax.crypto.spec.SecretKeySpec;
40
41
public class TestCipherMode {
42
43
private static final String[] TRANSFORMATIONS = {
44
"DES/ECB/PKCS5Padding", // CipherCore
45
"AES/GCM/NoPadding", // GaloisCounterMode
46
"AES/KW/NoPadding", // KeyWrapCipher
47
"AES/KW/PKCS5Padding", // KeyWrapCipher
48
"AES/KWP/NoPadding", // KeyWrapCipher
49
"RSA/ECB/NoPadding", // RSACipher
50
"DESedeWrap/CBC/NoPadding", // DESedeWrapCipher
51
"ChaCha20-Poly1305", // ChaCha20Cipher
52
};
53
54
private static final byte[] BYTES32 =
55
Arrays.copyOf(TRANSFORMATIONS[0].getBytes(), 32);
56
private static final SecretKey DES_KEY =
57
new SecretKeySpec(BYTES32, 0, 8, "DES");
58
private static final SecretKey AES_KEY =
59
new SecretKeySpec(BYTES32, 0, 16, "AES");
60
61
private static enum CipherMode {
62
ENCRYPT(Cipher.ENCRYPT_MODE),
63
DECRYPT(Cipher.DECRYPT_MODE),
64
WRAP(Cipher.WRAP_MODE),
65
UNWRAP(Cipher.UNWRAP_MODE),
66
NONEXISTENT(100);
67
68
int value;
69
70
CipherMode(int value) {
71
this.value = value;
72
}
73
}
74
75
private static Key getKey(String t, CipherMode m)
76
throws NoSuchAlgorithmException, NoSuchProviderException {
77
Key key;
78
String algo = t.split("/")[0];
79
switch (algo) {
80
case "AES":
81
key = AES_KEY;
82
break;
83
case "RSA":
84
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
85
KeyPair kp = kpg.generateKeyPair();
86
key = ((m == CipherMode.ENCRYPT || m == CipherMode.UNWRAP)?
87
kp.getPrivate() : kp.getPublic());
88
break;
89
case "ChaCha20-Poly1305":
90
key = new SecretKeySpec(BYTES32, 0, 32, "ChaCha20");
91
break;
92
case "DES":
93
key = new SecretKeySpec(BYTES32, 0, 8, algo);
94
break;
95
case "DESedeWrap":
96
key = new SecretKeySpec(BYTES32, 0, 24, "DESede");
97
break;
98
default:
99
throw new RuntimeException("Unknown transformation: " + t);
100
}
101
return key;
102
}
103
104
public static void main(String[] argv) throws Exception {
105
106
TestCipherMode test = new TestCipherMode("SunJCE", TRANSFORMATIONS);
107
System.out.println("All Tests Passed");
108
}
109
110
private Cipher c = null;
111
private SecretKey key = null;
112
113
private TestCipherMode(String provName, String... transformations)
114
throws Exception {
115
116
System.out.println("Testing " + provName);
117
118
for (String t : transformations) {
119
for (CipherMode m : CipherMode.values()) {
120
checkMode(t, m, provName);
121
}
122
}
123
}
124
125
private void checkMode(String t, CipherMode mode, String provName)
126
throws Exception {
127
Cipher c = Cipher.getInstance(t, provName);
128
Key key = getKey(t, mode);
129
130
System.out.println(c.getAlgorithm() + " with " + mode.name());
131
try {
132
c.init(mode.value, key, c.getParameters());
133
if (mode == CipherMode.NONEXISTENT) {
134
throw new Exception("ERROR: should throw IPE for init()");
135
}
136
} catch (UnsupportedOperationException uoe) {
137
// some may not support wrap/unwrap or enc/dec
138
if (mode != CipherMode.NONEXISTENT) {
139
System.out.println("Expected UOE thrown with init()");
140
return;
141
}
142
throw uoe;
143
} catch (InvalidParameterException ipe) {
144
if (mode == CipherMode.NONEXISTENT) {
145
System.out.println("=> expected IPE thrown for init()");
146
return;
147
}
148
throw ipe;
149
}
150
151
switch (mode) {
152
case ENCRYPT:
153
case DECRYPT:
154
// call wrap()/unwrap() and see if ISE is thrown.
155
try {
156
c.wrap(key);
157
throw new Exception("ERROR: should throw ISE for wrap()");
158
} catch (IllegalStateException ise) {
159
System.out.println("=> expected ISE thrown for wrap()");
160
}
161
try {
162
c.unwrap(new byte[16], key.getAlgorithm(), Cipher.SECRET_KEY);
163
throw new Exception("ERROR: should throw ISE for unwrap()");
164
} catch (IllegalStateException ise) {
165
System.out.println("=> expected ISE thrown for unwrap()");
166
}
167
break;
168
case WRAP:
169
case UNWRAP:
170
try {
171
c.update(new byte[16]);
172
throw new Exception("ERROR: should throw ISE for update()");
173
} catch (IllegalStateException ise) {
174
System.out.println("=> expected ISE thrown for update()");
175
}
176
try {
177
c.doFinal();
178
throw new Exception("ERROR: should throw ISE for doFinal()");
179
} catch (IllegalStateException ise) {
180
System.out.println("=> expected ISE thrown for doFinal()");
181
}
182
break;
183
}
184
}
185
}
186
187