Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/pss/SignatureTestPSS.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.*;
25
import java.security.spec.*;
26
import java.util.Arrays;
27
import java.util.stream.IntStream;
28
import jdk.test.lib.SigTestUtil;
29
import static jdk.test.lib.SigTestUtil.SignatureType;
30
import static javax.crypto.Cipher.PRIVATE_KEY;
31
import static javax.crypto.Cipher.PUBLIC_KEY;
32
33
/**
34
* @test
35
* @bug 8146293 8238448
36
* @summary Create a signature for RSASSA-PSS and get its signed data.
37
* re-initiate the signature with the public key. The signature
38
* can be verified by acquired signed data.
39
* @library /test/lib
40
* @build jdk.test.lib.SigTestUtil
41
* @run main SignatureTestPSS 512
42
* @run main SignatureTestPSS 768
43
* @run main SignatureTestPSS 1024
44
* @run main SignatureTestPSS 1025
45
* @run main SignatureTestPSS 2048
46
* @run main SignatureTestPSS 2049
47
* @run main/timeout=240 SignatureTestPSS 4096
48
* @run main/timeout=240 SignatureTestPSS 5120
49
* @run main/timeout=480 SignatureTestPSS 6144
50
*/
51
public class SignatureTestPSS {
52
/**
53
* ALGORITHM name, fixed as RSASSA-PSS.
54
*/
55
private static final String KEYALG = SignatureType.RSASSA_PSS.toString();
56
57
/**
58
* JDK default RSA Provider.
59
*/
60
private static final String PROVIDER = "SunRsaSign";
61
62
/**
63
* How much times signature updated.
64
*/
65
private static final int UPDATE_TIMES_FIFTY = 50;
66
67
/**
68
* How much times signature initial updated.
69
*/
70
private static final int UPDATE_TIMES_HUNDRED = 100;
71
72
public static void main(String[] args) throws Exception {
73
int testSize = Integer.parseInt(args[0]);
74
75
Iterable<String> md_alg_pss =
76
SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, testSize);
77
78
byte[] data = new byte[100];
79
IntStream.range(0, data.length).forEach(j -> {
80
data[j] = (byte) j;
81
});
82
83
// create a key pair
84
KeyPair kpair = generateKeys(KEYALG, testSize);
85
Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
86
Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
87
88
test(md_alg_pss, privs, pubs, data);
89
}
90
91
private static void test(Iterable<String> testAlgs, Key[] privs,
92
Key[] pubs, byte[] data) throws RuntimeException {
93
// For signature algorithm, create and verify a signature
94
Arrays.stream(privs).forEach(priv
95
-> Arrays.stream(pubs).forEach(pub
96
-> testAlgs.forEach(testAlg -> {
97
try {
98
checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
99
testAlg);
100
} catch (NoSuchAlgorithmException | InvalidKeyException |
101
SignatureException | NoSuchProviderException |
102
InvalidAlgorithmParameterException ex) {
103
throw new RuntimeException(ex);
104
}
105
}
106
)));
107
108
}
109
110
private static KeyPair generateKeys(String keyalg, int size)
111
throws NoSuchAlgorithmException {
112
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);
113
kpg.initialize(size);
114
return kpg.generateKeyPair();
115
}
116
117
private static Key[] manipulateKey(int type, Key key)
118
throws NoSuchAlgorithmException, InvalidKeySpecException,
119
NoSuchProviderException {
120
KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);
121
switch (type) {
122
case PUBLIC_KEY:
123
try {
124
kf.getKeySpec(key, RSAPrivateKeySpec.class);
125
throw new RuntimeException("Expected InvalidKeySpecException"
126
+ " not thrown");
127
} catch (InvalidKeySpecException expected) {
128
System.out.println("Expected IKSE thrown for PublicKey");
129
}
130
return new Key[]{
131
kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
132
kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
133
kf.generatePublic(new RSAPublicKeySpec(
134
((RSAPublicKey)key).getModulus(),
135
((RSAPublicKey)key).getPublicExponent(),
136
((RSAPublicKey)key).getParams()))
137
};
138
case PRIVATE_KEY:
139
try {
140
kf.getKeySpec(key, RSAPublicKeySpec.class);
141
throw new RuntimeException("Expected InvalidKeySpecException"
142
+ " not thrown");
143
} catch (InvalidKeySpecException expected) {
144
System.out.println("Expected IKSE thrown for PrivateKey");
145
}
146
return new Key[]{
147
kf.generatePrivate(kf.getKeySpec(key, RSAPrivateKeySpec.class)),
148
kf.generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded())),
149
kf.generatePrivate(new RSAPrivateKeySpec(
150
((RSAPrivateKey)key).getModulus(),
151
((RSAPrivateKey)key).getPrivateExponent(),
152
((RSAPrivateKey)key).getParams()))
153
};
154
}
155
throw new RuntimeException("We shouldn't reach here");
156
}
157
158
private static void checkSignature(byte[] data, PublicKey pub,
159
PrivateKey priv, String mdAlg) throws NoSuchAlgorithmException,
160
InvalidKeyException, SignatureException, NoSuchProviderException,
161
InvalidAlgorithmParameterException {
162
System.out.println("Testing against " + mdAlg);
163
Signature sig = Signature.getInstance
164
(SignatureType.RSASSA_PSS.toString(), PROVIDER);
165
AlgorithmParameterSpec params =
166
SigTestUtil.generateDefaultParameter(SignatureType.RSASSA_PSS, mdAlg);
167
sig.setParameter(params);
168
sig.initSign(priv);
169
for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
170
sig.update(data);
171
}
172
byte[] signedData = sig.sign();
173
174
// Make sure signature verifies with original data
175
// do we need to call sig.setParameter(params) again?
176
sig.initVerify(pub);
177
for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
178
sig.update(data);
179
}
180
if (!sig.verify(signedData)) {
181
throw new RuntimeException("Failed to verify signature");
182
}
183
184
// Make sure signature does NOT verify when the original data
185
// has changed
186
sig.initVerify(pub);
187
for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {
188
sig.update(data);
189
}
190
191
if (sig.verify(signedData)) {
192
throw new RuntimeException("Failed to detect bad signature");
193
}
194
}
195
}
196
197