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