Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyNoReuse.java
41152 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 8255410
27
* @library /test/lib ..
28
* @run main/othervm TestChaChaPolyNoReuse
29
* @summary Test PKCS#11 ChaCha20-Poly1305 Cipher Implementation
30
* (key/nonce reuse check)
31
*/
32
33
import java.util.*;
34
import javax.crypto.Cipher;
35
import java.security.spec.AlgorithmParameterSpec;
36
import java.security.Provider;
37
import java.security.NoSuchAlgorithmException;
38
import javax.crypto.spec.ChaCha20ParameterSpec;
39
import javax.crypto.spec.IvParameterSpec;
40
import javax.crypto.spec.SecretKeySpec;
41
import javax.crypto.AEADBadTagException;
42
import javax.crypto.SecretKey;
43
import java.security.InvalidKeyException;
44
import java.security.InvalidAlgorithmParameterException;
45
46
public class TestChaChaPolyNoReuse extends PKCS11Test {
47
48
private static final String KEY_ALGO = "ChaCha20";
49
private static final String CIPHER_ALGO = "ChaCha20-Poly1305";
50
51
/**
52
* Basic TestMethod interface definition
53
*/
54
public interface TestMethod {
55
/**
56
* Runs the actual test case
57
*
58
* @param provider the provider to provide the requested Cipher obj.
59
*
60
* @return true if the test passes, false otherwise.
61
*/
62
boolean run(Provider p);
63
}
64
65
public static class TestData {
66
public TestData(String name, String keyStr, String nonceStr, int ctr,
67
int dir, String inputStr, String aadStr, String outStr) {
68
testName = Objects.requireNonNull(name);
69
HexFormat hex = HexFormat.of();
70
key = hex.parseHex(keyStr);
71
nonce = hex.parseHex(nonceStr);
72
if ((counter = ctr) < 0) {
73
throw new IllegalArgumentException(
74
"counter must be 0 or greater");
75
}
76
direction = dir;
77
if (direction != Cipher.ENCRYPT_MODE) {
78
throw new IllegalArgumentException(
79
"Direction must be ENCRYPT_MODE");
80
}
81
input = hex.parseHex(inputStr);
82
aad = (aadStr != null) ? hex.parseHex(aadStr) : null;
83
expOutput = hex.parseHex(outStr);
84
}
85
86
public final String testName;
87
public final byte[] key;
88
public final byte[] nonce;
89
public final int counter;
90
public final int direction;
91
public final byte[] input;
92
public final byte[] aad;
93
public final byte[] expOutput;
94
}
95
96
public static final List<TestData> aeadTestList =
97
new LinkedList<TestData>() {{
98
add(new TestData("RFC 7539 Sample AEAD Test Vector",
99
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
100
"070000004041424344454647",
101
1, Cipher.ENCRYPT_MODE,
102
"4c616469657320616e642047656e746c656d656e206f662074686520636c6173" +
103
"73206f66202739393a204966204920636f756c64206f6666657220796f75206f" +
104
"6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73" +
105
"637265656e20776f756c642062652069742e",
106
"50515253c0c1c2c3c4c5c6c7",
107
"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d6" +
108
"3dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b36" +
109
"92ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc" +
110
"3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd060" +
111
"0691"));
112
}};
113
114
/**
115
* Make sure we do not use this Cipher object without initializing it
116
* at all
117
*/
118
public static final TestMethod noInitTest = new TestMethod() {
119
@Override
120
public boolean run(Provider p) {
121
System.out.println("----- No Init Test -----");
122
try {
123
Cipher cipher = Cipher.getInstance(CIPHER_ALGO, p);
124
TestData testData = aeadTestList.get(0);
125
126
// Attempting to use the cipher without initializing it
127
// should throw an IllegalStateException
128
try {
129
cipher.updateAAD(testData.aad);
130
throw new RuntimeException(
131
"Expected IllegalStateException not thrown");
132
} catch (IllegalStateException ise) {
133
// Do nothing, this is what we expected to happen
134
}
135
} catch (Exception exc) {
136
System.out.println("Unexpected exception: " + exc);
137
exc.printStackTrace();
138
return false;
139
}
140
141
return true;
142
}
143
};
144
145
/**
146
* Attempt to run two full encryption operations without an init in
147
* between.
148
*/
149
public static final TestMethod encTwiceNoInit = new TestMethod() {
150
@Override
151
public boolean run(Provider p) {
152
System.out.println("----- Encrypt 2nd time without init -----");
153
try {
154
AlgorithmParameterSpec spec;
155
Cipher cipher = Cipher.getInstance(CIPHER_ALGO, p);
156
TestData testData = aeadTestList.get(0);
157
spec = new IvParameterSpec(testData.nonce);
158
SecretKey key = new SecretKeySpec(testData.key, KEY_ALGO);
159
160
// Initialize and encrypt
161
cipher.init(testData.direction, key, spec);
162
cipher.updateAAD(testData.aad);
163
cipher.doFinal(testData.input);
164
System.out.println("First encryption complete");
165
166
// Now attempt to encrypt again without changing the key/IV
167
// This should fail.
168
try {
169
cipher.updateAAD(testData.aad);
170
} catch (IllegalStateException ise) {
171
// Do nothing, this is what we expected to happen
172
}
173
try {
174
cipher.doFinal(testData.input);
175
throw new RuntimeException(
176
"Expected IllegalStateException not thrown");
177
} catch (IllegalStateException ise) {
178
// Do nothing, this is what we expected to happen
179
}
180
} catch (Exception exc) {
181
System.out.println("Unexpected exception: " + exc);
182
exc.printStackTrace();
183
return false;
184
}
185
186
return true;
187
}
188
};
189
190
/**
191
* Encrypt once successfully, then attempt to init with the same
192
* key and nonce.
193
*/
194
public static final TestMethod encTwiceInitSameParams = new TestMethod() {
195
@Override
196
public boolean run(Provider p) {
197
System.out.println("----- Encrypt, then init with same params " +
198
"-----");
199
try {
200
AlgorithmParameterSpec spec;
201
Cipher cipher = Cipher.getInstance(CIPHER_ALGO, p);
202
TestData testData = aeadTestList.get(0);
203
spec = new IvParameterSpec(testData.nonce);
204
SecretKey key = new SecretKeySpec(testData.key, KEY_ALGO);
205
206
// Initialize then encrypt
207
cipher.init(testData.direction, key, spec);
208
cipher.updateAAD(testData.aad);
209
cipher.doFinal(testData.input);
210
System.out.println("First encryption complete");
211
212
// Initializing after the completed encryption with
213
// the same key and nonce should fail.
214
try {
215
cipher.init(testData.direction, key, spec);
216
throw new RuntimeException(
217
"Expected IKE or IAPE not thrown");
218
} catch (InvalidKeyException |
219
InvalidAlgorithmParameterException e) {
220
// Do nothing, this is what we expected to happen
221
}
222
} catch (Exception exc) {
223
System.out.println("Unexpected exception: " + exc);
224
exc.printStackTrace();
225
return false;
226
}
227
228
return true;
229
}
230
};
231
232
public static final List<TestMethod> testMethodList =
233
Arrays.asList(noInitTest, encTwiceNoInit, encTwiceInitSameParams);
234
235
@Override
236
public void main(Provider p) throws Exception {
237
System.out.println("Testing " + p.getName());
238
try {
239
Cipher.getInstance(CIPHER_ALGO, p);
240
} catch (NoSuchAlgorithmException nsae) {
241
System.out.println("Skip; no support for " + CIPHER_ALGO);
242
return;
243
}
244
245
int testsPassed = 0;
246
int testNumber = 0;
247
248
for (TestMethod tm : testMethodList) {
249
testNumber++;
250
boolean result = tm.run(p);
251
System.out.println("Result: " + (result ? "PASS" : "FAIL"));
252
if (result) {
253
testsPassed++;
254
}
255
}
256
257
System.out.println("Total Tests: " + testNumber +
258
", Tests passed: " + testsPassed);
259
if (testsPassed < testNumber) {
260
throw new RuntimeException(
261
"Not all tests passed. See output for failure info");
262
}
263
}
264
265
public static void main(String[] args) throws Exception {
266
main(new TestChaChaPolyNoReuse(), args);
267
}
268
}
269
270