Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/TestGeneral.java
66649 views
1
/*
2
* Copyright (c) 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 8248268 8268621
27
* @summary Verify general properties of the AES/KW/NoPadding,
28
* AES/KW/PKCS5Padding, and AES/KWP/NoPadding.
29
* @run main TestGeneral
30
*/
31
import java.util.Arrays;
32
import java.security.Key;
33
import java.security.InvalidAlgorithmParameterException;
34
import javax.crypto.*;
35
import javax.crypto.spec.*;
36
37
public class TestGeneral {
38
39
private static final byte[] DATA_128 =
40
Arrays.copyOf("1234567890123456789012345678901234".getBytes(), 128);
41
private static final SecretKey KEY =
42
new SecretKeySpec(DATA_128, 0, 16, "AES");
43
private static final int KW_IV_LEN = 8;
44
private static final int KWP_IV_LEN = 4;
45
private static final int MAX_KW_PKCS5PAD_LEN = 16; // 1-16
46
private static final int MAX_KWP_PAD_LEN = 7; // 0...7
47
48
public static void testEnc(Cipher c, byte[] in, int inLen, int ivLen,
49
int maxPadLen) throws Exception {
50
51
System.out.println("input len: " + inLen);
52
c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));
53
54
int estOutLen = c.getOutputSize(inLen);
55
56
byte[] out = c.doFinal(in, 0, inLen);
57
58
// for encryption output, the estimate should match the actual
59
if (estOutLen != out.length) {
60
System.out.println("=> estimated: " + estOutLen);
61
System.out.println("=> actual enc out length: " + out.length);
62
throw new RuntimeException("Failed enc output len check");
63
}
64
65
// encryption outout should always be multiple of 8 and at least 8-byte
66
// longer than input
67
if ((out.length % 8 != 0) || (out.length - inLen < 8)) {
68
throw new RuntimeException("Invalid length of encrypted data: " +
69
out.length);
70
}
71
72
c.init(Cipher.DECRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));
73
estOutLen = c.getOutputSize(out.length);
74
75
byte[] in2 = c.doFinal(out);
76
77
// for decryption output, the estimate should match the actual for
78
// AES/KW/NoPadding and slightly larger than the actual for the rest
79
if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {
80
System.out.println("=> estimated: " + estOutLen);
81
System.out.println("=> actual dec out length: " + in2.length);
82
throw new RuntimeException("Failed dec output len check");
83
}
84
85
if (!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {
86
throw new RuntimeException("Failed decrypted data check");
87
}
88
}
89
90
public static void testWrap(Cipher c, byte[] in, int inLen, int ivLen,
91
int maxPadLen) throws Exception {
92
93
System.out.println("key len: " + inLen);
94
c.init(Cipher.WRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));
95
96
int estOutLen = c.getOutputSize(inLen);
97
98
byte[] out = c.wrap(new SecretKeySpec(in, 0, inLen, "Any"));
99
100
// for encryption output, the estimate should match the actual
101
if (estOutLen != out.length) {
102
System.out.println("=> estimated: " + estOutLen);
103
System.out.println("=> actual wrap out length: " + out.length);
104
throw new RuntimeException("Failed wrap output len check");
105
}
106
107
// encryption outout should always be multiple of 8 and at least 8-byte
108
// longer than input
109
if ((out.length % 8 != 0) || (out.length - inLen < 8)) {
110
throw new RuntimeException("Invalid length of encrypted data: " +
111
out.length);
112
}
113
c.init(Cipher.UNWRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));
114
estOutLen = c.getOutputSize(out.length);
115
116
Key key2 = c.unwrap(out, "Any", Cipher.SECRET_KEY);
117
118
if (!(key2 instanceof SecretKey)) {
119
throw new RuntimeException("Failed unwrap output type check");
120
}
121
122
byte[] in2 = key2.getEncoded();
123
// for decryption output, the estimate should match the actual for
124
// AES/KW/NoPadding and slightly larger than the actual for the rest
125
if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {
126
System.out.println("=> estimated: " + estOutLen);
127
System.out.println("=> actual unwrap out length: " + in2.length);
128
throw new RuntimeException("Failed unwrap output len check");
129
}
130
131
if (inLen != in2.length ||
132
!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {
133
throw new RuntimeException("Failed unwrap data check");
134
}
135
}
136
137
public static void testIv(Cipher c) throws Exception {
138
// get a fresh Cipher instance so we can test iv with pre-init state
139
Cipher c2 = Cipher.getInstance(c.getAlgorithm(), c.getProvider());
140
if (c2.getIV() != null) {
141
throw new RuntimeException("Expects null iv");
142
}
143
if (c2.getParameters() == null) {
144
throw new RuntimeException("Expects non-null default parameters");
145
}
146
147
c2.init(Cipher.ENCRYPT_MODE, KEY);
148
byte[] defIv2 = c2.getIV();
149
c.init(Cipher.ENCRYPT_MODE, KEY);
150
byte[] defIv = c.getIV();
151
if (!Arrays.equals(defIv, defIv2)) {
152
throw new RuntimeException("Failed default iv check");
153
}
154
// try init w/ an iv w/ invalid length
155
try {
156
c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv, 0,
157
defIv.length/2));
158
throw new RuntimeException("Invalid iv accepted");
159
} catch (InvalidAlgorithmParameterException iape) {
160
System.out.println("Invalid IV rejected as expected");
161
}
162
Arrays.fill(defIv, (byte) 0xFF);
163
c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv));
164
byte[] newIv = c.getIV();
165
if (!Arrays.equals(newIv, defIv)) {
166
throw new RuntimeException("Failed set iv check");
167
}
168
byte[] newIv2 = c.getIV();
169
if (newIv == newIv2) {
170
throw new RuntimeException("Failed getIV copy check");
171
}
172
}
173
174
public static void main(String[] argv) throws Exception {
175
byte[] data = DATA_128;
176
177
String ALGO = "AES/KW/PKCS5Padding";
178
System.out.println("Testing " + ALGO);
179
Cipher c = Cipher.getInstance(ALGO, "SunJCE");
180
181
// test all possible pad lengths, i.e. 1 - 16
182
for (int i = 1; i <= MAX_KW_PKCS5PAD_LEN; i++) {
183
testEnc(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);
184
testWrap(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);
185
}
186
testIv(c);
187
188
ALGO = "AES/KW/NoPadding";
189
System.out.println("Testing " + ALGO);
190
c = Cipher.getInstance(ALGO, "SunJCE");
191
testEnc(c, data, data.length, KW_IV_LEN, 0);
192
testEnc(c, data, data.length >> 1, KW_IV_LEN, 0);
193
testWrap(c, data, data.length, KW_IV_LEN, 0);
194
testWrap(c, data, data.length >> 1, KW_IV_LEN, 0);
195
testIv(c);
196
197
ALGO = "AES/KWP/NoPadding";
198
System.out.println("Testing " + ALGO);
199
c = Cipher.getInstance(ALGO, "SunJCE");
200
201
// test all possible pad lengths, i.e. 0 - 7
202
for (int i = 0; i <= MAX_KWP_PAD_LEN; i++) {
203
testEnc(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);
204
testWrap(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);
205
}
206
testIv(c);
207
208
System.out.println("All Tests Passed");
209
}
210
}
211
212