Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/pss/SignatureTest2.java
41153 views
1
/*
2
* Copyright (c) 2018, 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.RSAPrivateKey;
25
import java.security.interfaces.RSAPublicKey;
26
import java.security.spec.*;
27
import java.util.Arrays;
28
import java.util.stream.IntStream;
29
import static javax.crypto.Cipher.PRIVATE_KEY;
30
import static javax.crypto.Cipher.PUBLIC_KEY;
31
32
/**
33
* @test
34
* @bug 8146293 8238448 8172366
35
* @summary Create a signature for RSASSA-PSS and get its signed data.
36
* re-initiate the signature with the public key. The signature
37
* can be verified by acquired signed data.
38
* @run main SignatureTest2 768
39
* @run main SignatureTest2 1024
40
* @run main SignatureTest2 1025
41
* @run main SignatureTest2 2048
42
* @run main SignatureTest2 2049
43
* @run main/timeout=240 SignatureTest2 4096
44
*/
45
public class SignatureTest2 {
46
/**
47
* ALGORITHM name, fixed as RSA.
48
*/
49
private static final String KEYALG = "RSASSA-PSS";
50
51
/**
52
* JDK default RSA Provider.
53
*/
54
private static final String PROVIDER = "SunRsaSign";
55
56
/**
57
* How much times signature updated.
58
*/
59
private static final int UPDATE_TIMES_TWO = 2;
60
61
/**
62
* How much times signature initial updated.
63
*/
64
private static final int UPDATE_TIMES_TEN = 10;
65
66
/**
67
* Digest algorithms to test w/ RSASSA-PSS signature algorithms
68
*/
69
private static final String[] DIGEST_ALG = {
70
"SHA-1", "SHA-224", "SHA-256", "SHA-384",
71
"SHA-512", "SHA-512/224", "SHA-512/256",
72
"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"
73
};
74
75
private static final String SIG_ALG = "RSASSA-PSS";
76
77
private static PSSParameterSpec genPSSParameter(String digestAlgo,
78
int digestLen, int keySize) {
79
// pick a salt length based on the key length and digestAlgo
80
int saltLength = keySize/8 - digestLen - 2;
81
if (saltLength < 0) {
82
System.out.println("keysize: " + keySize/8 + ", digestLen: " + digestLen);
83
return null;
84
}
85
return new PSSParameterSpec(digestAlgo, "MGF1",
86
new MGF1ParameterSpec(digestAlgo), saltLength, 1);
87
}
88
89
public static void main(String[] args) throws Exception {
90
final int testSize = Integer.parseInt(args[0]);
91
92
byte[] data = new byte[100];
93
IntStream.range(0, data.length).forEach(j -> {
94
data[j] = (byte) j;
95
});
96
97
// create a key pair
98
KeyPair kpair = generateKeys(KEYALG, testSize);
99
Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
100
Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
101
102
// For messsage digest algorithm, create and verify a RSASSA-PSS signature
103
Arrays.stream(privs).forEach(priv
104
-> Arrays.stream(pubs).forEach(pub
105
-> Arrays.stream(DIGEST_ALG).forEach(testAlg -> {
106
checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
107
testAlg, testSize);
108
}
109
)));
110
111
}
112
113
private static KeyPair generateKeys(String keyalg, int size)
114
throws NoSuchAlgorithmException {
115
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);
116
kpg.initialize(size);
117
return kpg.generateKeyPair();
118
}
119
120
private static Key[] manipulateKey(int type, Key key)
121
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
122
KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);
123
124
switch (type) {
125
case PUBLIC_KEY:
126
return new Key[]{
127
kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
128
kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
129
kf.generatePublic(new RSAPublicKeySpec(
130
((RSAPublicKey) key).getModulus(),
131
((RSAPublicKey) key).getPublicExponent()))
132
};
133
case PRIVATE_KEY:
134
return new Key[]{
135
kf.generatePrivate(kf.getKeySpec(key,
136
RSAPrivateKeySpec.class)),
137
kf.generatePrivate(new PKCS8EncodedKeySpec(
138
key.getEncoded())),
139
kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),
140
((RSAPrivateKey) key).getPrivateExponent()))
141
};
142
}
143
throw new RuntimeException("We shouldn't reach here");
144
}
145
146
private static void checkSignature(byte[] data, PublicKey pub,
147
PrivateKey priv, String digestAlg, int keySize) throws RuntimeException {
148
try {
149
Signature sig = Signature.getInstance(SIG_ALG, PROVIDER);
150
int digestLen = MessageDigest.getInstance(digestAlg).getDigestLength();
151
PSSParameterSpec params = genPSSParameter(digestAlg, digestLen, keySize);
152
if (params == null) {
153
System.out.println("Skip test due to short key size");
154
return;
155
}
156
sig.setParameter(params);
157
sig.initSign(priv);
158
for (int i = 0; i < UPDATE_TIMES_TEN; i++) {
159
sig.update(data);
160
}
161
byte[] signedDataTen = sig.sign();
162
163
// Make sure signature can be generated without re-init
164
sig.update(data);
165
byte[] signedDataOne = sig.sign();
166
167
// Make sure signature verifies with original data
168
System.out.println("Verify using params " + sig.getParameters());
169
sig.initVerify(pub);
170
sig.setParameter(params);
171
for (int i = 0; i < UPDATE_TIMES_TEN; i++) {
172
sig.update(data);
173
}
174
if (!sig.verify(signedDataTen)) {
175
throw new RuntimeException("Signature verification test#1 failed w/ "
176
+ digestAlg);
177
}
178
179
// Make sure signature can verify without re-init
180
sig.update(data);
181
if (!sig.verify(signedDataOne)) {
182
throw new RuntimeException("Signature verification test#2 failed w/ "
183
+ digestAlg);
184
}
185
186
// Make sure signature does NOT verify when the original data
187
// has changed
188
for (int i = 0; i < UPDATE_TIMES_TWO; i++) {
189
sig.update(data);
190
}
191
192
if (sig.verify(signedDataOne)) {
193
throw new RuntimeException("Bad signature accepted w/ "
194
+ digestAlg);
195
}
196
} catch (NoSuchAlgorithmException | InvalidKeyException |
197
SignatureException | NoSuchProviderException |
198
InvalidAlgorithmParameterException e) {
199
e.printStackTrace();
200
throw new RuntimeException(e);
201
}
202
}
203
}
204
205