Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Signature/SignatureTestPSS2.java
41153 views
1
/*
2
* Copyright (c) 2020, 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
import java.security.*;
24
import java.security.interfaces.*;
25
import java.security.spec.*;
26
import java.util.stream.IntStream;
27
28
/**
29
* @test
30
* @bug 8244154 8242332
31
* @summary Generate a <digest>withRSASSA-PSS signature and verify it using
32
* PKCS11 provider
33
* @library /test/lib ..
34
* @modules jdk.crypto.cryptoki
35
* @run main SignatureTestPSS2
36
*/
37
public class SignatureTestPSS2 extends PKCS11Test {
38
39
// PKCS11 does not support RSASSA-PSS keys yet
40
private static final String KEYALG = "RSA";
41
private static final String[] SIGALGS = {
42
"SHA224withRSASSA-PSS", "SHA256withRSASSA-PSS",
43
"SHA384withRSASSA-PSS", "SHA512withRSASSA-PSS",
44
"SHA3-224withRSASSA-PSS", "SHA3-256withRSASSA-PSS",
45
"SHA3-384withRSASSA-PSS", "SHA3-512withRSASSA-PSS"
46
};
47
48
private static final int[] KEYSIZES = { 2048, 3072 };
49
50
/**
51
* How much times signature updated.
52
*/
53
private static final int UPDATE_TIMES = 2;
54
55
public static void main(String[] args) throws Exception {
56
main(new SignatureTestPSS2(), args);
57
}
58
59
@Override
60
public void main(Provider p) throws Exception {
61
for (String sa : SIGALGS) {
62
Signature sig;
63
try {
64
sig = Signature.getInstance(sa, p);
65
} catch (NoSuchAlgorithmException e) {
66
System.out.println("Skip testing " + sa +
67
" due to no support");
68
return;
69
}
70
for (int i : KEYSIZES) {
71
runTest(sig, i);
72
}
73
}
74
}
75
76
private static void runTest(Signature s, int keySize) throws Exception {
77
byte[] data = new byte[100];
78
IntStream.range(0, data.length).forEach(j -> {
79
data[j] = (byte) j;
80
});
81
System.out.println("[KEYSIZE = " + keySize + "]");
82
83
// create a key pair
84
KeyPair kpair = generateKeys(KEYALG, keySize, s.getProvider());
85
test(s, kpair.getPrivate(), kpair.getPublic(), data);
86
}
87
88
private static void test(Signature sig, PrivateKey privKey,
89
PublicKey pubKey, byte[] data) throws RuntimeException {
90
// For signature algorithm, create and verify a signature
91
try {
92
checkSignature(sig, privKey, pubKey, data);
93
} catch (NoSuchAlgorithmException | InvalidKeyException |
94
SignatureException | NoSuchProviderException ex) {
95
throw new RuntimeException(ex);
96
} catch (InvalidAlgorithmParameterException ex2) {
97
System.out.println("Skip test due to " + ex2);
98
}
99
}
100
101
private static KeyPair generateKeys(String keyalg, int size, Provider p)
102
throws NoSuchAlgorithmException {
103
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg, p);
104
kpg.initialize(size);
105
return kpg.generateKeyPair();
106
}
107
108
private static void checkSignature(Signature sig, PrivateKey priv,
109
PublicKey pub, byte[] data) throws NoSuchAlgorithmException,
110
InvalidKeyException, SignatureException, NoSuchProviderException,
111
InvalidAlgorithmParameterException {
112
System.out.println("Testing against " + sig.getAlgorithm());
113
sig.initSign(priv);
114
for (int i = 0; i < UPDATE_TIMES; i++) {
115
sig.update(data);
116
}
117
byte[] signedData = sig.sign();
118
119
// Make sure signature verifies with original data
120
// do we need to call sig.setParameter(params) again?
121
sig.initVerify(pub);
122
for (int i = 0; i < UPDATE_TIMES; i++) {
123
sig.update(data);
124
}
125
if (!sig.verify(signedData)) {
126
throw new RuntimeException("Failed to verify signature");
127
}
128
129
// Make sure signature does NOT verify when the original data
130
// has changed
131
sig.initVerify(pub);
132
for (int i = 0; i < UPDATE_TIMES + 1; i++) {
133
sig.update(data);
134
}
135
136
if (sig.verify(signedData)) {
137
throw new RuntimeException("Failed to detect bad signature");
138
}
139
}
140
}
141
142