Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/Cipher/TestCipherMode.java
41152 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
37
import javax.crypto.*;
38
import javax.crypto.spec.SecretKeySpec;
39
40
public class TestCipherMode {
41
42
private static final String[] TRANSFORMATIONS = {
43
"DES/ECB/PKCS5Padding",
44
"AES/KW/NoPadding",
45
"AES/KW/PKCS5Padding",
46
"AES/KWP/NoPadding",
47
};
48
49
private static final SecretKey DES_KEY =
50
new SecretKeySpec(new byte[8], "DES");
51
private static final SecretKey AES_KEY =
52
new SecretKeySpec(new byte[16], "AES");
53
54
public static void main(String[] argv) throws Exception {
55
for (String t : TRANSFORMATIONS) {
56
System.out.println("Testing SunJCE provider, Cipher " + t );
57
58
TestCipherMode test = new TestCipherMode(t);
59
System.out.println("Testing ENCRYPT_MODE...");
60
test.checkMode(Cipher.ENCRYPT_MODE, "encryption");
61
System.out.println("Testing DECRYPT_MODE...");
62
test.checkMode(Cipher.DECRYPT_MODE, "decryption");
63
System.out.println("Testing WRAP_MODE...");
64
test.checkMode(Cipher.WRAP_MODE, "key wrapping");
65
System.out.println("Testing UNWRAP_MODE...");
66
test.checkMode(Cipher.UNWRAP_MODE, "key unwrapping");
67
}
68
System.out.println("All Tests Passed");
69
}
70
71
private Cipher c = null;
72
private SecretKey key = null;
73
74
private TestCipherMode(String transformation)
75
throws NoSuchAlgorithmException, NoSuchProviderException,
76
NoSuchPaddingException {
77
c = Cipher.getInstance(transformation, "SunJCE");
78
this.key = switch (transformation.split("/")[0]) {
79
case "DES" -> DES_KEY;
80
case "AES" -> AES_KEY;
81
default -> throw new RuntimeException
82
("Error: Unsupported key algorithm");
83
};
84
}
85
86
private void checkMode(int mode, String opString) throws Exception {
87
c.init(mode, key);
88
89
switch (mode) {
90
case Cipher.ENCRYPT_MODE:
91
case Cipher.DECRYPT_MODE:
92
// call wrap()/unwrap() and see if ISE is thrown.
93
try {
94
c.wrap(key);
95
throw new Exception("ERROR: should throw ISE for wrap()");
96
} catch (IllegalStateException ise) {
97
System.out.println("expected ISE is thrown for wrap()");
98
}
99
try {
100
c.unwrap(new byte[16], key.getAlgorithm(), Cipher.SECRET_KEY);
101
throw new Exception("ERROR: should throw ISE for unwrap()");
102
} catch (IllegalStateException ise) {
103
System.out.println("expected ISE is thrown for unwrap()");
104
}
105
break;
106
case Cipher.WRAP_MODE:
107
case Cipher.UNWRAP_MODE:
108
try {
109
c.update(new byte[16]);
110
throw new Exception("ERROR: should throw ISE for update()");
111
} catch (IllegalStateException ise) {
112
System.out.println("expected ISE is thrown for update()");
113
}
114
try {
115
c.doFinal();
116
throw new Exception("ERROR: should throw ISE for doFinal()");
117
} catch (IllegalStateException ise) {
118
System.out.println("expected ISE is thrown for doFinal()");
119
}
120
break;
121
}
122
}
123
}
124
125