Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/TestCipherKeyWrapperTest.java
41161 views
1
/*
2
* Copyright (c) 2015, 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
import static java.lang.System.out;
25
26
import java.lang.Integer;
27
import java.lang.String;
28
import java.lang.System;
29
import java.security.AlgorithmParameters;
30
import java.security.InvalidAlgorithmParameterException;
31
import java.security.InvalidKeyException;
32
import java.security.Key;
33
import java.security.KeyPair;
34
import java.security.NoSuchAlgorithmException;
35
import java.security.KeyPairGenerator;
36
import java.security.Provider;
37
import java.security.Security;
38
import java.security.spec.AlgorithmParameterSpec;
39
import java.security.spec.InvalidKeySpecException;
40
import java.util.Arrays;
41
import java.util.HashMap;
42
import java.util.Map;
43
import java.util.Random;
44
45
import javax.crypto.IllegalBlockSizeException;
46
import javax.crypto.NoSuchPaddingException;
47
import javax.crypto.SecretKey;
48
import javax.crypto.Cipher;
49
import javax.crypto.KeyGenerator;
50
import javax.crypto.SecretKeyFactory;
51
import javax.crypto.spec.PBEKeySpec;
52
import javax.crypto.spec.PBEParameterSpec;
53
54
/*
55
* @test
56
* @bug 8048599 8248268
57
* @summary Tests for key wrap and unwrap operations
58
*/
59
60
public class TestCipherKeyWrapperTest {
61
private static final String SUN_JCE = "SunJCE";
62
// Blowfish Variable key length: 32 bits to 448 bits
63
private static final int BLOWFISH_MIN_KEYSIZE = 32;
64
private static final int BLOWFISH_MAX_KEYSIZE = 448;
65
private static final int LINIMITED_KEYSIZE = 128;
66
private static final String NOPADDING = "NoPaDDing";
67
private static final String[] PBE_ALGORITHM_AR = { "pbeWithMD5ANDdes",
68
"PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5AndTripleDES",
69
"PBEWithMD5AndTripleDES/CBC/PKCS5Padding", "PBEwithSHA1AndDESede",
70
"PBEwithSHA1AndDESede/CBC/PKCS5Padding", "PBEwithSHA1AndRC2_40",
71
"PBEwithSHA1Andrc2_40/CBC/PKCS5Padding", "PBEWithSHA1AndRC2_128",
72
"PBEWithSHA1andRC2_128/CBC/PKCS5Padding", "PBEWithSHA1AndRC4_40",
73
"PBEWithsha1AndRC4_40/ECB/NoPadding", "PBEWithSHA1AndRC4_128",
74
"pbeWithSHA1AndRC4_128/ECB/NoPadding", "PBEWithHmacSHA1AndAES_128",
75
"PBEWithHmacSHA224AndAES_128", "PBEWithHmacSHA256AndAES_128",
76
"PBEWithHmacSHA384AndAES_128", "PBEWithHmacSHA512AndAES_128",
77
"PBEWithHmacSHA1AndAES_256", "PBEWithHmacSHA224AndAES_256",
78
"PBEWithHmacSHA256AndAES_256", "PBEWithHmacSHA384AndAES_256",
79
"PBEWithHmacSHA512AndAES_256" };
80
private static final String[] MODEL_AR = { "ECb", "pCbC", "cbC", "cFB",
81
"cFB24", "cFB40", "OfB48", "OFB64" };
82
private static final String[] PADDING_AR = { NOPADDING, "PKCS5Padding" };
83
84
private enum AlgorithmWrapper {
85
AESWrap("AES", "AESWrap", -1),
86
AESWrap_128("AES", "AESWrap_128", 128),
87
AESWrap_192("AES", "AESWrap_192", 192),
88
AESWrap_256("AES", "AESWrap_256", 256),
89
AESWrapPad("AES", "AESWrapPad", -1),
90
AESWrapPad_128("AES", "AESWrapPad_128", 128),
91
AESWrapPad_192("AES", "AESWrapPad_192", 192),
92
AESWrapPad_256("AES", "AESWrapPad_256", 256),
93
DESedeWrap("desede", "DESedeWrap", -1),
94
NegtiveWrap("AES", "DESedeWrap", -1);
95
96
private final String algorithm;
97
private final String wrapper;
98
private final int keySize;
99
100
private AlgorithmWrapper(String algorithm, String wrapper, int kSize) {
101
this.algorithm = algorithm;
102
this.wrapper = wrapper;
103
this.keySize = kSize;
104
}
105
106
public String getAlgorithm() {
107
return algorithm;
108
}
109
110
public String getWrapper() {
111
return wrapper;
112
}
113
114
public int getKeySize() {
115
return keySize;
116
}
117
118
};
119
120
public static void main(String[] args) throws Exception {
121
122
TestCipherKeyWrapperTest test = new TestCipherKeyWrapperTest();
123
// AESWrap and DESedeWrap test
124
for (AlgorithmWrapper algoWrapper : AlgorithmWrapper.values()) {
125
String algo = algoWrapper.getAlgorithm();
126
String wrapper = algoWrapper.getWrapper();
127
try {
128
int keySize = algoWrapper.getKeySize();
129
// only run the tests on longer key lengths if unlimited
130
// version of JCE jurisdiction policy files are installed
131
if (!(Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE)
132
&& keySize > LINIMITED_KEYSIZE) {
133
out.println(algo + " will not run if unlimited version of"
134
+ " JCE jurisdiction policy files are installed");
135
continue;
136
}
137
test.wrapperAesDESedeKeyTest(algo, wrapper, keySize);
138
if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
139
throw new RuntimeException("Expected not throw when algo"
140
+ " and wrapAlgo are not match:" + algo);
141
}
142
} catch (InvalidKeyException e) {
143
if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
144
out.println("Expepted exception when algo"
145
+ " and wrapAlgo are not match:" + algo);
146
} else {
147
throw e;
148
}
149
}
150
}
151
test.wrapperBlowfishKeyTest();
152
// PBE and public wrapper test.
153
String[] publicPrivateAlgos = new String[] { "DiffieHellman", "DSA",
154
"RSA" };
155
Provider provider = Security.getProvider(SUN_JCE);
156
if (provider == null) {
157
throw new RuntimeException("SUN_JCE provider not exist");
158
}
159
160
test.wrapperPBEKeyTest(provider);
161
// Public and private key wrap test
162
test.wrapperPublicPriviteKeyTest(provider, publicPrivateAlgos);
163
}
164
165
private void wrapperAesDESedeKeyTest(String algo, String wrapAlgo,
166
int keySize) throws InvalidKeyException, NoSuchAlgorithmException,
167
NoSuchPaddingException, IllegalBlockSizeException,
168
InvalidAlgorithmParameterException {
169
// Initialization
170
KeyGenerator kg = KeyGenerator.getInstance(algo);
171
if (keySize != -1) {
172
kg.init(keySize);
173
}
174
SecretKey key = kg.generateKey();
175
wrapTest(algo, wrapAlgo, key, key, Cipher.SECRET_KEY, false);
176
}
177
178
private void wrapperBlowfishKeyTest() throws InvalidKeyException,
179
NoSuchAlgorithmException, NoSuchPaddingException,
180
IllegalBlockSizeException, InvalidAlgorithmParameterException {
181
// how many kinds of padding mode
182
int padKinds;
183
// Keysize should be multiple of 8 bytes.
184
int KeyCutter = 8;
185
int kSize = BLOWFISH_MIN_KEYSIZE;
186
String algorithm = "Blowfish";
187
int maxAllowKeyLength = Cipher.getMaxAllowedKeyLength(algorithm);
188
boolean unLimitPolicy = maxAllowKeyLength == Integer.MAX_VALUE;
189
SecretKey key = null;
190
while (kSize <= BLOWFISH_MAX_KEYSIZE) {
191
for (String mode : MODEL_AR) {
192
// PKCS5padding is meaningful only for ECB, CBC, PCBC
193
if (mode.equalsIgnoreCase(MODEL_AR[0])
194
|| mode.equalsIgnoreCase(MODEL_AR[1])
195
|| mode.equalsIgnoreCase(MODEL_AR[2])) {
196
padKinds = PADDING_AR.length;
197
} else {
198
padKinds = 1;
199
}
200
// Initialization
201
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
202
for (int k = 0; k < padKinds; k++) {
203
String transformation = algorithm + "/" + mode + "/"
204
+ PADDING_AR[k];
205
if (NOPADDING.equals(PADDING_AR[k]) && kSize % 64 != 0) {
206
out.println(transformation
207
+ " will not run if input length not multiple"
208
+ " of 8 bytes when padding is " + NOPADDING);
209
continue;
210
}
211
kg.init(kSize);
212
key = kg.generateKey();
213
// only run the tests on longer key lengths if unlimited
214
// version of JCE jurisdiction policy files are installed
215
if (!unLimitPolicy && kSize > LINIMITED_KEYSIZE) {
216
out.println("keyStrength > 128 within " + algorithm
217
+ " will not run under global policy");
218
} else {
219
wrapTest(transformation, transformation, key, key,
220
Cipher.SECRET_KEY, false);
221
}
222
}
223
}
224
if (kSize <= LINIMITED_KEYSIZE) {
225
KeyCutter = 8;
226
} else {
227
KeyCutter = 48;
228
}
229
kSize += KeyCutter;
230
}
231
}
232
233
private void wrapperPBEKeyTest(Provider p) throws InvalidKeySpecException,
234
InvalidKeyException, NoSuchPaddingException,
235
IllegalBlockSizeException, InvalidAlgorithmParameterException,
236
NoSuchAlgorithmException {
237
for (String alg : PBE_ALGORITHM_AR) {
238
String baseAlgo = alg.split("/")[0].toUpperCase();
239
// only run the tests on longer key lengths if unlimited version
240
// of JCE jurisdiction policy files are installed
241
242
if (Cipher.getMaxAllowedKeyLength(alg) < Integer.MAX_VALUE
243
&& (baseAlgo.endsWith("TRIPLEDES") || alg
244
.endsWith("AES_256"))) {
245
out.println("keyStrength > 128 within " + alg
246
+ " will not run under global policy");
247
continue;
248
}
249
SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
250
SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover"
251
.toCharArray()));
252
wrapTest(alg, alg, key, key, Cipher.SECRET_KEY, true);
253
}
254
}
255
256
private void wrapperPublicPriviteKeyTest(Provider p, String[] algorithms)
257
throws NoSuchAlgorithmException, InvalidKeyException,
258
NoSuchPaddingException, IllegalBlockSizeException,
259
InvalidAlgorithmParameterException {
260
for (String algo : algorithms) {
261
// Key pair generated
262
System.out.println("Generate key pair (algorithm: " + algo
263
+ ", provider: " + p.getName() + ")");
264
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
265
kpg.initialize(512);
266
KeyPair kp = kpg.genKeyPair();
267
// key generated
268
String algoWrap = "DES";
269
KeyGenerator kg = KeyGenerator.getInstance(algoWrap, p);
270
Key key = kg.generateKey();
271
wrapTest(algo, algoWrap, key, kp.getPrivate(), Cipher.PRIVATE_KEY,
272
false);
273
wrapTest(algo, algoWrap, key, kp.getPublic(), Cipher.PUBLIC_KEY,
274
false);
275
}
276
}
277
278
private void wrapTest(String transformation, String wrapAlgo, Key initKey,
279
Key wrapKey, int keyType, boolean isPBE)
280
throws NoSuchAlgorithmException, NoSuchPaddingException,
281
InvalidKeyException, IllegalBlockSizeException,
282
InvalidAlgorithmParameterException {
283
String algo = transformation.split("/")[0];
284
boolean isAESBlowfish = algo.indexOf("AES") != -1
285
|| algo.indexOf("Blowfish") != -1;
286
AlgorithmParameters aps = null;
287
AlgorithmParameterSpec pbeParams = null;
288
if (isPBE) {
289
byte[] salt = new byte[8];
290
int iterCnt = 1000;
291
new Random().nextBytes(salt);
292
pbeParams = new PBEParameterSpec(salt, iterCnt);
293
}
294
295
out.println("Testing " + wrapAlgo + " cipher wrap/unwrap");
296
// Wrap & UnWrap operation
297
Cipher wrapCI = Cipher.getInstance(wrapAlgo);
298
if (isPBE && !isAESBlowfish) {
299
wrapCI.init(Cipher.WRAP_MODE, initKey, pbeParams);
300
} else if (isAESBlowfish) {
301
wrapCI.init(Cipher.WRAP_MODE, initKey);
302
aps = wrapCI.getParameters();
303
} else {
304
wrapCI.init(Cipher.WRAP_MODE, initKey);
305
}
306
byte[] keyWrapper = wrapCI.wrap(wrapKey);
307
if (isPBE && !isAESBlowfish) {
308
wrapCI.init(Cipher.UNWRAP_MODE, initKey, pbeParams);
309
} else if (isAESBlowfish) {
310
wrapCI.init(Cipher.UNWRAP_MODE, initKey, aps);
311
} else {
312
wrapCI.init(Cipher.UNWRAP_MODE, initKey);
313
}
314
Key unwrappedKey = wrapCI.unwrap(keyWrapper, algo, keyType);
315
// Comparison
316
if (!Arrays.equals(wrapKey.getEncoded(), unwrappedKey.getEncoded())) {
317
out.println("keysize : " + wrapKey.getEncoded().length);
318
throw new RuntimeException("Comparation failed testing "
319
+ transformation + ":" + wrapAlgo + ":" + keyType);
320
}
321
}
322
}
323
324