Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/KeyGenerator/TestChaCha20.java
41154 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
* @modules jdk.crypto.cryptoki
28
* @summary Check ChaCha20 key generator.
29
* @library /test/lib ..
30
* @run main/othervm TestChaCha20
31
*/
32
import java.security.Provider;
33
import java.security.InvalidAlgorithmParameterException;
34
import java.security.InvalidParameterException;
35
import java.security.NoSuchAlgorithmException;
36
import java.util.HexFormat;
37
38
import javax.crypto.KeyGenerator;
39
import javax.crypto.SecretKey;
40
import javax.crypto.spec.ChaCha20ParameterSpec;
41
42
public class TestChaCha20 extends PKCS11Test {
43
44
private static final String ALGO = "ChaCha20";
45
46
public static void main(String[] args) throws Exception {
47
main(new TestChaCha20(), args);
48
}
49
50
@Override
51
public void main(Provider p) throws Exception {
52
System.out.println("Testing " + p.getName());
53
KeyGenerator kg;
54
try {
55
kg = KeyGenerator.getInstance(ALGO, p);
56
} catch (NoSuchAlgorithmException nsae) {
57
System.out.println("Skip; no support for " + ALGO);
58
return;
59
}
60
61
try {
62
kg.init(new ChaCha20ParameterSpec(new byte[12], 0));
63
throw new RuntimeException(
64
"ChaCha20 key generation should not need any paramSpec");
65
} catch (InvalidAlgorithmParameterException e) {
66
System.out.println("Expected IAPE: " + e.getMessage());
67
}
68
69
for (int keySize : new int[] { 32, 64, 128, 256, 512, 1024 }) {
70
try {
71
kg.init(keySize);
72
if (keySize != 256) {
73
throw new RuntimeException(keySize + " is invalid keysize");
74
}
75
} catch (InvalidParameterException e) {
76
if (keySize == 256) {
77
throw new RuntimeException("IPE thrown for valid keySize");
78
} else {
79
System.out.println("Expected IPE thrown for " + keySize);
80
}
81
}
82
}
83
84
//kg.init(256);
85
SecretKey key = kg.generateKey();
86
byte[] keyValue = key.getEncoded();
87
System.out.println("Key: " + HexFormat.of().formatHex(keyValue));
88
if (keyValue.length != 32) {
89
throw new RuntimeException("The size of generated key must be 256");
90
}
91
}
92
}
93
94