Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/SecretKeyFactory/TestGeneral.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
* @summary test the general SecretKeyFactory functionality
28
* @library /test/lib ..
29
* @modules jdk.crypto.cryptoki
30
* @run main/othervm TestGeneral
31
*/
32
33
import java.security.NoSuchAlgorithmException;
34
import java.security.Provider;
35
import java.security.SecureRandom;
36
import java.util.Arrays;
37
import javax.crypto.SecretKeyFactory;
38
import javax.crypto.SecretKey;
39
import javax.crypto.spec.SecretKeySpec;
40
41
public class TestGeneral extends PKCS11Test {
42
43
private enum TestResult {
44
PASS,
45
FAIL,
46
TBD // unused for now
47
}
48
49
public static void main(String[] args) throws Exception {
50
main(new TestGeneral(), args);
51
}
52
53
private void test(String algorithm, SecretKey key, Provider p,
54
TestResult expected) throws RuntimeException {
55
System.out.println("Testing " + algorithm + " SKF from " + p.getName());
56
SecretKeyFactory skf;
57
try {
58
skf = SecretKeyFactory.getInstance(algorithm, p);
59
} catch (NoSuchAlgorithmException e) {
60
System.out.println("Not supported, skipping: " + e);
61
return;
62
}
63
try {
64
SecretKey key2 = skf.translateKey(key);
65
if (expected == TestResult.FAIL) {
66
throw new RuntimeException("translateKey() should FAIL");
67
}
68
System.out.println("=> translated key");
69
if (!key2.getAlgorithm().equalsIgnoreCase(algorithm)) {
70
throw new RuntimeException("translated key algorithm mismatch");
71
}
72
System.out.println("=> checked key algorithm");
73
74
// proceed to check encodings if format match
75
if (key2.getFormat().equalsIgnoreCase(key.getFormat())) {
76
if (key2.getEncoded() != null &&
77
!Arrays.equals(key.getEncoded(), key2.getEncoded())) {
78
throw new RuntimeException(
79
"translated key encoding mismatch");
80
}
81
System.out.println("=> checked key format and encoding");
82
}
83
} catch (Exception e) {
84
if (expected == TestResult.PASS) {
85
e.printStackTrace();
86
throw new RuntimeException("translateKey() should pass", e);
87
}
88
}
89
}
90
91
@Override
92
public void main(Provider p) throws Exception {
93
94
byte[] rawBytes = new byte[32];
95
new SecureRandom().nextBytes(rawBytes);
96
97
SecretKey aes_128Key = new SecretKeySpec(rawBytes, 0, 16, "AES");
98
SecretKey aes_256Key = new SecretKeySpec(rawBytes, 0, 32, "AES");
99
SecretKey bf_128Key = new SecretKeySpec(rawBytes, 0, 16, "Blowfish");
100
SecretKey cc20Key = new SecretKeySpec(rawBytes, 0, 32, "ChaCha20");
101
102
// fixed key length
103
test("AES", aes_128Key, p, TestResult.PASS);
104
test("AES", aes_256Key, p, TestResult.PASS);
105
test("AES", cc20Key, p, TestResult.FAIL);
106
107
test("ChaCha20", aes_128Key, p, TestResult.FAIL);
108
test("ChaCha20", aes_256Key, p, TestResult.FAIL);
109
test("ChaCha20", cc20Key, p, TestResult.PASS);
110
111
// variable key length
112
// Different PKCS11 impls may have different ranges
113
// of supported key sizes for variable-key-length
114
// algorithms.
115
test("Blowfish", aes_128Key, p, TestResult.FAIL);
116
test("Blowfish", cc20Key, p, TestResult.FAIL);
117
test("Blowfish", bf_128Key, p, TestResult.PASS);
118
}
119
}
120
121