Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/NSASuiteB/TestAESWrapOids.java
41155 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 javax.crypto.Cipher.getMaxAllowedKeyLength;
25
26
import java.security.InvalidKeyException;
27
import java.security.Key;
28
import java.security.NoSuchAlgorithmException;
29
import java.security.NoSuchProviderException;
30
import java.util.Arrays;
31
import java.util.List;
32
33
import javax.crypto.Cipher;
34
import javax.crypto.IllegalBlockSizeException;
35
import javax.crypto.KeyGenerator;
36
import javax.crypto.NoSuchPaddingException;
37
import javax.crypto.SecretKey;
38
39
/*
40
* @test
41
* @bug 8075286 8248268
42
* @summary Test the AES-Key-Wrap and AES-Key-Wrap-Pad algorithm OIDs in JDK.
43
* OID and Algorithm transformation string should match.
44
* Both could be able to be used to generate the algorithm instance.
45
* @run main TestAESWrapOids
46
*/
47
public class TestAESWrapOids {
48
49
private static final String PROVIDER_NAME = "SunJCE";
50
51
private static final List<DataTuple> DATA = Arrays.asList(
52
new DataTuple("2.16.840.1.101.3.4.1.5", "AESWrap_128"),
53
new DataTuple("2.16.840.1.101.3.4.1.25", "AESWrap_192"),
54
new DataTuple("2.16.840.1.101.3.4.1.45", "AESWrap_256"),
55
new DataTuple("2.16.840.1.101.3.4.1.5", "AES_128/KW/NoPadding"),
56
new DataTuple("2.16.840.1.101.3.4.1.25", "AES_192/KW/NoPadding"),
57
new DataTuple("2.16.840.1.101.3.4.1.45", "AES_256/KW/NoPadding"),
58
new DataTuple("2.16.840.1.101.3.4.1.8", "AES_128/KWP/NoPadding"),
59
new DataTuple("2.16.840.1.101.3.4.1.28", "AES_192/KWP/NoPadding"),
60
new DataTuple("2.16.840.1.101.3.4.1.48", "AES_256/KWP/NoPadding"));
61
62
public static void main(String[] args) throws Exception {
63
for (DataTuple dataTuple : DATA) {
64
int maxAllowedKeyLength = getMaxAllowedKeyLength(
65
dataTuple.algorithm);
66
boolean supportedKeyLength =
67
maxAllowedKeyLength >= dataTuple.keyLength;
68
69
try {
70
runTest(dataTuple, supportedKeyLength);
71
System.out.println("passed");
72
} catch (InvalidKeyException ike) {
73
if (supportedKeyLength) {
74
throw new RuntimeException(String.format(
75
"The key length %d is supported, but test failed.",
76
dataTuple.keyLength), ike);
77
} else {
78
System.out.printf(
79
"Catch expected InvalidKeyException "
80
+ "due to the key length %d is greater "
81
+ "than max supported key length %d%n",
82
dataTuple.keyLength, maxAllowedKeyLength);
83
}
84
}
85
}
86
}
87
88
private static void runTest(DataTuple dataTuple, boolean supportedKeyLength)
89
throws NoSuchAlgorithmException, NoSuchProviderException,
90
NoSuchPaddingException, InvalidKeyException,
91
IllegalBlockSizeException {
92
Cipher algorithmCipher = Cipher.getInstance(
93
dataTuple.algorithm, PROVIDER_NAME);
94
Cipher oidCipher = Cipher.getInstance(dataTuple.oid, PROVIDER_NAME);
95
96
if (algorithmCipher == null) {
97
throw new RuntimeException(String.format(
98
"Test failed: algorithm string %s getInstance failed.%n",
99
dataTuple.algorithm));
100
}
101
102
if (oidCipher == null) {
103
throw new RuntimeException(
104
String.format("Test failed: OID %s getInstance failed.%n",
105
dataTuple.oid));
106
}
107
108
if (!algorithmCipher.getAlgorithm().equals(
109
dataTuple.algorithm)) {
110
throw new RuntimeException(String.format(
111
"Test failed: algorithm string %s getInstance "
112
+ "doesn't generate expected algorithm.%n",
113
dataTuple.oid));
114
}
115
116
KeyGenerator kg = KeyGenerator.getInstance("AES");
117
kg.init(dataTuple.keyLength);
118
SecretKey key = kg.generateKey();
119
120
// Wrap the key
121
algorithmCipher.init(Cipher.WRAP_MODE, key);
122
if (!supportedKeyLength) {
123
throw new RuntimeException(String.format(
124
"The key length %d is not supported, so the initialization"
125
+ " of algorithmCipher should fail.%n",
126
dataTuple.keyLength));
127
}
128
129
// Unwrap the key
130
oidCipher.init(Cipher.UNWRAP_MODE, key);
131
if (!supportedKeyLength) {
132
throw new RuntimeException(String.format(
133
"The key length %d is not supported, so the initialization"
134
+ " of oidCipher should fail.%n",
135
dataTuple.keyLength));
136
}
137
138
byte[] keyWrapper = algorithmCipher.wrap(key);
139
Key unwrappedKey = oidCipher.unwrap(keyWrapper, "AES",
140
Cipher.SECRET_KEY);
141
142
// Comparison
143
if (!Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded())) {
144
throw new RuntimeException("Key comparison failed");
145
}
146
}
147
148
private static class DataTuple {
149
150
private final String oid;
151
private final String algorithm;
152
private final int keyLength;
153
154
private DataTuple(String oid, String algorithm) {
155
this.oid = oid;
156
this.algorithm = algorithm;
157
this.keyLength = switch (oid) {
158
case "2.16.840.1.101.3.4.1.5", "2.16.840.1.101.3.4.1.8"->128;
159
case "2.16.840.1.101.3.4.1.25", "2.16.840.1.101.3.4.1.28"->192;
160
case "2.16.840.1.101.3.4.1.45", "2.16.840.1.101.3.4.1.48"->256;
161
default->throw new RuntimeException("Unrecognized oid: " + oid);
162
};
163
}
164
}
165
}
166
167