Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/NSASuiteB/TestAESWrapOids.java
38867 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.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
42
* @summary Test the AESWrap 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", 128),
53
new DataTuple("2.16.840.1.101.3.4.1.25", "AESWrap_192", 192),
54
new DataTuple("2.16.840.1.101.3.4.1.45", "AESWrap_256", 256));
55
56
public static void main(String[] args) throws Exception {
57
for (DataTuple dataTuple : DATA) {
58
int maxAllowedKeyLength = getMaxAllowedKeyLength(
59
dataTuple.algorithm);
60
boolean supportedKeyLength =
61
maxAllowedKeyLength >= dataTuple.keyLength;
62
63
try {
64
runTest(dataTuple, supportedKeyLength);
65
System.out.println("passed");
66
} catch (InvalidKeyException ike) {
67
if (supportedKeyLength) {
68
throw new RuntimeException(String.format(
69
"The key length %d is supported, but test failed.",
70
dataTuple.keyLength), ike);
71
} else {
72
System.out.printf(
73
"Catch expected InvalidKeyException "
74
+ "due to the key length %d is greater "
75
+ "than max supported key length %d%n",
76
dataTuple.keyLength, maxAllowedKeyLength);
77
}
78
}
79
}
80
}
81
82
private static void runTest(DataTuple dataTuple, boolean supportedKeyLength)
83
throws NoSuchAlgorithmException, NoSuchProviderException,
84
NoSuchPaddingException, InvalidKeyException,
85
IllegalBlockSizeException {
86
Cipher algorithmCipher = Cipher.getInstance(
87
dataTuple.algorithm, PROVIDER_NAME);
88
Cipher oidCipher = Cipher.getInstance(dataTuple.oid, PROVIDER_NAME);
89
90
if (algorithmCipher == null) {
91
throw new RuntimeException(String.format(
92
"Test failed: algorithm string %s getInstance failed.%n",
93
dataTuple.algorithm));
94
}
95
96
if (oidCipher == null) {
97
throw new RuntimeException(
98
String.format("Test failed: OID %s getInstance failed.%n",
99
dataTuple.oid));
100
}
101
102
if (!algorithmCipher.getAlgorithm().equals(
103
dataTuple.algorithm)) {
104
throw new RuntimeException(String.format(
105
"Test failed: algorithm string %s getInstance "
106
+ "doesn't generate expected algorithm.%n",
107
dataTuple.oid));
108
}
109
110
KeyGenerator kg = KeyGenerator.getInstance("AES");
111
kg.init(dataTuple.keyLength);
112
SecretKey key = kg.generateKey();
113
114
// Wrap the key
115
algorithmCipher.init(Cipher.WRAP_MODE, key);
116
if (!supportedKeyLength) {
117
throw new RuntimeException(String.format(
118
"The key length %d is not supported, so the initialization"
119
+ " of algorithmCipher should fail.%n",
120
dataTuple.keyLength));
121
}
122
123
// Unwrap the key
124
oidCipher.init(Cipher.UNWRAP_MODE, key);
125
if (!supportedKeyLength) {
126
throw new RuntimeException(String.format(
127
"The key length %d is not supported, so the initialization"
128
+ " of oidCipher should fail.%n",
129
dataTuple.keyLength));
130
}
131
132
byte[] keyWrapper = algorithmCipher.wrap(key);
133
Key unwrappedKey = oidCipher.unwrap(keyWrapper, "AES",
134
Cipher.SECRET_KEY);
135
136
// Comparison
137
if (!Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded())) {
138
throw new RuntimeException("Key comparison failed");
139
}
140
}
141
142
private static class DataTuple {
143
144
private final String oid;
145
private final String algorithm;
146
private final int keyLength;
147
148
private DataTuple(String oid, String algorithm, int keyLength) {
149
this.oid = oid;
150
this.algorithm = algorithm;
151
this.keyLength = keyLength;
152
}
153
}
154
}
155
156