Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Signature/KeyAndParamCheckForPSS.java
41153 views
1
/*
2
* Copyright (c) 2019, 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
27
/**
28
* @test
29
* @bug 8080462 8226651 8242332
30
* @summary Ensure that PSS key and params check are implemented properly
31
* regardless of call sequence
32
* @library /test/lib ..
33
* @modules jdk.crypto.cryptoki
34
* @run main KeyAndParamCheckForPSS
35
*/
36
public class KeyAndParamCheckForPSS extends PKCS11Test {
37
38
/**
39
* ALGORITHM name, fixed as RSA for PKCS11
40
*/
41
private static final String KEYALG = "RSA";
42
private static final String SIGALG = "RSASSA-PSS";
43
44
public static void main(String[] args) throws Exception {
45
main(new KeyAndParamCheckForPSS(), args);
46
}
47
48
@Override
49
public void main(Provider p) throws Exception {
50
Signature sig;
51
try {
52
sig = Signature.getInstance(SIGALG, p);
53
} catch (NoSuchAlgorithmException e) {
54
System.out.println("Skip testing RSASSA-PSS" +
55
" due to no support");
56
return;
57
}
58
59
// NOTE: key length >= (digest length + 2) in bytes
60
// otherwise, even salt length = 0 would not work
61
runTest(p, 1024, "SHA-256", "SHA-256");
62
runTest(p, 1024, "SHA-256", "SHA-384");
63
runTest(p, 1024, "SHA-256", "SHA-512");
64
runTest(p, 1024, "SHA-384", "SHA-256");
65
runTest(p, 1024, "SHA-384", "SHA-384");
66
runTest(p, 1024, "SHA-384", "SHA-512");
67
runTest(p, 1040, "SHA-512", "SHA-256");
68
runTest(p, 1040, "SHA-512", "SHA-384");
69
runTest(p, 1040, "SHA-512", "SHA-512");
70
runTest(p, 1024, "SHA3-256", "SHA3-256");
71
runTest(p, 1024, "SHA3-256", "SHA3-384");
72
runTest(p, 1024, "SHA3-256", "SHA3-512");
73
runTest(p, 1024, "SHA3-384", "SHA3-256");
74
runTest(p, 1024, "SHA3-384", "SHA3-384");
75
runTest(p, 1024, "SHA3-384", "SHA3-512");
76
runTest(p, 1040, "SHA3-512", "SHA3-256");
77
runTest(p, 1040, "SHA3-512", "SHA3-384");
78
runTest(p, 1040, "SHA3-512", "SHA3-512");
79
}
80
81
private void runTest(Provider p, int keySize, String hashAlg,
82
String mgfHashAlg) throws Exception {
83
84
// skip further test if this provider does not support hashAlg or
85
// mgfHashAlg
86
try {
87
MessageDigest.getInstance(hashAlg, p);
88
MessageDigest.getInstance(mgfHashAlg, p);
89
} catch (NoSuchAlgorithmException nsae) {
90
System.out.println("No support for " + hashAlg + ", skip");
91
return;
92
}
93
94
System.out.println("Testing [" + keySize + " " + hashAlg + "]");
95
96
// create a key pair with the supplied size
97
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);
98
kpg.initialize(keySize);
99
KeyPair kp = kpg.generateKeyPair();
100
101
int bigSaltLen = keySize/8 - 14;
102
AlgorithmParameterSpec paramsBad = new PSSParameterSpec(hashAlg,
103
"MGF1", new MGF1ParameterSpec(mgfHashAlg), bigSaltLen, 1);
104
AlgorithmParameterSpec paramsGood = new PSSParameterSpec(hashAlg,
105
"MGF1", new MGF1ParameterSpec(mgfHashAlg), 0, 1);
106
107
PrivateKey priv = kp.getPrivate();
108
PublicKey pub = kp.getPublic();
109
110
// test#1 - setParameter then initSign
111
Signature sig = Signature.getInstance("RSASSA-PSS", p);
112
sig.setParameter(paramsBad);
113
try {
114
sig.initSign(priv);
115
throw new RuntimeException("Expected IKE not thrown");
116
} catch (InvalidKeyException ike) {
117
System.out.println("test#1: got expected IKE");
118
}
119
120
sig.setParameter(paramsGood);
121
sig.initSign(priv);
122
System.out.println("test#1: pass");
123
124
// test#2 - setParameter then initVerify
125
sig = Signature.getInstance("RSASSA-PSS", p);
126
sig.setParameter(paramsBad);
127
try {
128
sig.initVerify(pub);
129
throw new RuntimeException("Expected IKE not thrown");
130
} catch (InvalidKeyException ike) {
131
System.out.println("test#2: got expected IKE");
132
}
133
134
sig.setParameter(paramsGood);
135
sig.initVerify(pub);
136
137
System.out.println("test#2: pass");
138
139
// test#3 - initSign, then setParameter
140
sig = Signature.getInstance("RSASSA-PSS", p);
141
sig.initSign(priv);
142
try {
143
sig.setParameter(paramsBad);
144
throw new RuntimeException("Expected IAPE not thrown");
145
} catch (InvalidAlgorithmParameterException iape) {
146
System.out.println("test#3: got expected IAPE");
147
}
148
149
sig.setParameter(paramsGood);
150
System.out.println("test#3: pass");
151
152
// test#4 - initVerify, then setParameter
153
sig = Signature.getInstance("RSASSA-PSS", p);
154
sig.initVerify(pub);
155
try {
156
sig.setParameter(paramsBad);
157
throw new RuntimeException("Expected IAPE not thrown");
158
} catch (InvalidAlgorithmParameterException iape) {
159
System.out.println("test#4: got expected IAPE");
160
}
161
162
sig.setParameter(paramsGood);
163
System.out.println("test#4: pass");
164
}
165
}
166
167