Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/NSASuiteB/TestAESOids.java
41155 views
1
/*
2
* Copyright (c) 2015, 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
import static javax.crypto.Cipher.ENCRYPT_MODE;
25
import static javax.crypto.Cipher.getMaxAllowedKeyLength;
26
27
import java.security.InvalidAlgorithmParameterException;
28
import java.security.InvalidKeyException;
29
import java.security.NoSuchAlgorithmException;
30
import java.security.NoSuchProviderException;
31
import java.security.spec.AlgorithmParameterSpec;
32
import java.util.Arrays;
33
import java.util.List;
34
35
import javax.crypto.BadPaddingException;
36
import javax.crypto.Cipher;
37
import javax.crypto.IllegalBlockSizeException;
38
import javax.crypto.KeyGenerator;
39
import javax.crypto.NoSuchPaddingException;
40
import javax.crypto.SecretKey;
41
import javax.crypto.ShortBufferException;
42
import javax.crypto.spec.IvParameterSpec;
43
44
/*
45
* @test
46
* @bug 8075286
47
* @summary Test the AES algorithm OIDs in JDK.
48
* OID and Algorithm transformation string should match.
49
* Both could be able to be used to generate the algorithm instance.
50
* @run main TestAESOids
51
*/
52
public class TestAESOids {
53
54
private static final String PROVIDER_NAME = "SunJCE";
55
private static final byte[] INPUT = "1234567890123456".getBytes();
56
57
private static final List<DataTuple> DATA = Arrays.asList(
58
new DataTuple("2.16.840.1.101.3.4.1.1", "AES_128/ECB/NoPadding",
59
128, "ECB"),
60
new DataTuple("2.16.840.1.101.3.4.1.2", "AES_128/CBC/NoPadding",
61
128, "CBC"),
62
new DataTuple("2.16.840.1.101.3.4.1.3", "AES_128/OFB/NoPadding",
63
128, "OFB"),
64
new DataTuple("2.16.840.1.101.3.4.1.4", "AES_128/CFB/NoPadding",
65
128, "CFB"),
66
new DataTuple("2.16.840.1.101.3.4.1.21", "AES_192/ECB/NoPadding",
67
192, "ECB"),
68
new DataTuple("2.16.840.1.101.3.4.1.22", "AES_192/CBC/NoPadding",
69
192, "CBC"),
70
new DataTuple("2.16.840.1.101.3.4.1.23", "AES_192/OFB/NoPadding",
71
192, "OFB"),
72
new DataTuple("2.16.840.1.101.3.4.1.24", "AES_192/CFB/NoPadding",
73
192, "CFB"),
74
new DataTuple("2.16.840.1.101.3.4.1.41", "AES_256/ECB/NoPadding",
75
256, "ECB"),
76
new DataTuple("2.16.840.1.101.3.4.1.42", "AES_256/CBC/NoPadding",
77
256, "CBC"),
78
new DataTuple("2.16.840.1.101.3.4.1.43", "AES_256/OFB/NoPadding",
79
256, "OFB"),
80
new DataTuple("2.16.840.1.101.3.4.1.44", "AES_256/CFB/NoPadding",
81
256, "CFB"));
82
83
public static void main(String[] args) throws Exception {
84
for (DataTuple dataTuple : DATA) {
85
int maxAllowedKeyLength =
86
getMaxAllowedKeyLength(dataTuple.algorithm);
87
boolean supportedKeyLength =
88
maxAllowedKeyLength >= dataTuple.keyLength;
89
90
try {
91
runTest(dataTuple, supportedKeyLength);
92
System.out.println("passed");
93
} catch (InvalidKeyException ike) {
94
if (supportedKeyLength) {
95
throw new RuntimeException(String.format(
96
"The key length %d is supported, but test failed.",
97
dataTuple.keyLength), ike);
98
} else {
99
System.out.printf(
100
"Catch expected InvalidKeyException due "
101
+ "to the key length %d is greater than "
102
+ "max supported key length %d%n",
103
dataTuple.keyLength, maxAllowedKeyLength);
104
}
105
}
106
}
107
}
108
109
private static void runTest(DataTuple dataTuple,
110
boolean supportedKeyLength) throws NoSuchAlgorithmException,
111
NoSuchProviderException, NoSuchPaddingException,
112
InvalidKeyException, ShortBufferException,
113
IllegalBlockSizeException, BadPaddingException,
114
InvalidAlgorithmParameterException {
115
Cipher algorithmCipher = Cipher.getInstance(dataTuple.algorithm,
116
PROVIDER_NAME);
117
Cipher oidCipher = Cipher.getInstance(dataTuple.oid, PROVIDER_NAME);
118
119
if (algorithmCipher == null) {
120
throw new RuntimeException(
121
String.format("Test failed: algorithm string %s getInstance"
122
+ " failed.%n", dataTuple.algorithm));
123
}
124
125
if (oidCipher == null) {
126
throw new RuntimeException(
127
String.format("Test failed: OID %s getInstance failed.%n",
128
dataTuple.oid));
129
}
130
131
if (!algorithmCipher.getAlgorithm().equals(dataTuple.algorithm)) {
132
throw new RuntimeException(String.format(
133
"Test failed: algorithm string %s getInstance "
134
+ "doesn't generate expected algorithm.%n",
135
dataTuple.algorithm));
136
}
137
138
KeyGenerator kg = KeyGenerator.getInstance("AES");
139
kg.init(dataTuple.keyLength);
140
SecretKey key = kg.generateKey();
141
142
// encrypt
143
algorithmCipher.init(ENCRYPT_MODE, key);
144
if (!supportedKeyLength) {
145
throw new RuntimeException(String.format(
146
"The key length %d is not supported, so the initialization "
147
+ "of algorithmCipher should fail.%n",
148
dataTuple.keyLength));
149
}
150
151
byte[] cipherText = new byte[algorithmCipher.getOutputSize(INPUT.length)];
152
int offset = algorithmCipher.update(INPUT, 0, INPUT.length,
153
cipherText, 0);
154
algorithmCipher.doFinal(cipherText, offset);
155
156
AlgorithmParameterSpec aps = null;
157
if (!dataTuple.mode.equalsIgnoreCase("ECB")) {
158
aps = new IvParameterSpec(algorithmCipher.getIV());
159
}
160
161
oidCipher.init(Cipher.DECRYPT_MODE, key, aps);
162
if (!supportedKeyLength) {
163
throw new RuntimeException(String.format(
164
"The key length %d is not supported, so the "
165
+ "initialization of oidCipher should fail.%n",
166
dataTuple.keyLength));
167
}
168
169
byte[] recoveredText = new byte[oidCipher.getOutputSize(cipherText.length)];
170
oidCipher.doFinal(cipherText, 0, cipherText.length, recoveredText);
171
172
// Comparison
173
if (!Arrays.equals(INPUT, recoveredText)) {
174
throw new RuntimeException(
175
"Decrypted data is not the same as the original text");
176
}
177
}
178
179
private static class DataTuple {
180
181
private final String oid;
182
private final String algorithm;
183
private final int keyLength;
184
private final String mode;
185
186
private DataTuple(String oid, String algorithm, int keyLength,
187
String mode) {
188
this.oid = oid;
189
this.algorithm = algorithm;
190
this.keyLength = keyLength;
191
this.mode = mode;
192
}
193
}
194
}
195
196