Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/NSASuiteB/TestDSAGenParameterSpec.java
41153 views
1
/*
2
* Copyright (c) 2015, 2021, 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
24
import java.security.AlgorithmParameterGenerator;
25
import java.security.AlgorithmParameters;
26
import java.security.InvalidAlgorithmParameterException;
27
import java.security.InvalidParameterException;
28
import java.security.KeyPairGenerator;
29
import java.security.NoSuchAlgorithmException;
30
import java.security.NoSuchProviderException;
31
import java.security.spec.DSAGenParameterSpec;
32
import java.security.spec.DSAParameterSpec;
33
import java.security.spec.InvalidParameterSpecException;
34
35
/*
36
* @test
37
* @bug 8075286 8163498
38
* @summary Verify that DSAGenParameterSpec can and can only be used to generate
39
* DSA within some certain range of key sizes as described in the class
40
* specification (L, N) as (1024, 160), (2048, 224), (2048, 256) and
41
* (3072, 256) should be OK for DSAGenParameterSpec.
42
* This test has been split based on lower/higher key sizes in order to
43
* reduce individual execution times and run in parallel
44
* (see TestDSAGenParameterSpecLongKey.java)
45
* @run main TestDSAGenParameterSpec 512 160
46
* @run main TestDSAGenParameterSpec 1024 160 true
47
* @run main TestDSAGenParameterSpec 1024 224
48
* @run main TestDSAGenParameterSpec 2048 160
49
* @run main/timeout=300 TestDSAGenParameterSpec 2048 224 true
50
* @run main/timeout=300 TestDSAGenParameterSpec 2048 256 true
51
* @run main TestDSAGenParameterSpec 3072 224
52
*/
53
public class TestDSAGenParameterSpec {
54
55
private static final String ALGORITHM_NAME = "DSA";
56
private static final String PROVIDER_NAME = "SUN";
57
58
private static void testDSAGenParameterSpec(DataTuple dataTuple)
59
throws NoSuchAlgorithmException, NoSuchProviderException,
60
InvalidParameterSpecException, InvalidAlgorithmParameterException {
61
System.out.printf("Test case: primePLen=%d, " + "subprimeQLen=%d%n",
62
dataTuple.primePLen, dataTuple.subprimeQLen);
63
64
AlgorithmParameterGenerator apg
65
= AlgorithmParameterGenerator.getInstance(ALGORITHM_NAME,
66
PROVIDER_NAME);
67
68
DSAGenParameterSpec genParamSpec = createGenParameterSpec(dataTuple);
69
// genParamSpec will be null if IllegalAE is thrown when expected.
70
if (genParamSpec == null) {
71
return;
72
}
73
74
try {
75
apg.init(genParamSpec, null);
76
AlgorithmParameters param = apg.generateParameters();
77
78
checkParam(param, genParamSpec);
79
System.out.println("Test case passed");
80
} catch (InvalidParameterException ipe) {
81
throw new RuntimeException("Test case failed.", ipe);
82
}
83
}
84
85
private static void checkParam(AlgorithmParameters param,
86
DSAGenParameterSpec genParam) throws InvalidParameterSpecException,
87
NoSuchAlgorithmException, NoSuchProviderException,
88
InvalidAlgorithmParameterException {
89
String algorithm = param.getAlgorithm();
90
if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {
91
throw new RuntimeException(
92
"Unexpected type of parameters: " + algorithm);
93
}
94
95
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
96
int valueL = spec.getP().bitLength();
97
int strengthP = genParam.getPrimePLength();
98
if (strengthP != valueL) {
99
System.out.printf("P: Expected %d but actual %d%n", strengthP,
100
valueL);
101
throw new RuntimeException("Wrong P strength");
102
}
103
104
int valueN = spec.getQ().bitLength();
105
int strengthQ = genParam.getSubprimeQLength();
106
if (strengthQ != valueN) {
107
System.out.printf("Q: Expected %d but actual %d%n", strengthQ,
108
valueN);
109
throw new RuntimeException("Wrong Q strength");
110
}
111
112
if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {
113
System.out.println("Defaut seed length should be the same as Q.");
114
throw new RuntimeException("Wrong seed length");
115
}
116
117
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,
118
PROVIDER_NAME);
119
keyGen.initialize(spec);
120
}
121
122
private static DSAGenParameterSpec createGenParameterSpec(
123
DataTuple dataTuple) {
124
DSAGenParameterSpec genParamSpec = null;
125
try {
126
genParamSpec = new DSAGenParameterSpec(dataTuple.primePLen,
127
dataTuple.subprimeQLen);
128
if (!dataTuple.isDSASpecSupported) {
129
throw new RuntimeException(
130
"Test case failed: the key length must not supported");
131
}
132
} catch (IllegalArgumentException e) {
133
if (!dataTuple.isDSASpecSupported) {
134
System.out.println("Test case passed: expected "
135
+ "IllegalArgumentException is caught");
136
} else {
137
throw new RuntimeException("Test case failed: unexpected "
138
+ "IllegalArgumentException is thrown", e);
139
}
140
}
141
142
return genParamSpec;
143
}
144
145
public static void main(String[] args) throws Exception {
146
if (args == null || args.length < 2) {
147
throw new RuntimeException("Invalid number of arguments to generate"
148
+ " DSA parameter.");
149
}
150
DataTuple dataTuple = null;
151
switch (args.length) {
152
case 3:
153
dataTuple = new DataTuple(Integer.valueOf(args[0]),
154
Integer.valueOf(args[1]), Boolean.valueOf(args[2]));
155
break;
156
case 2:
157
dataTuple = new DataTuple(Integer.valueOf(args[0]),
158
Integer.valueOf(args[1]), false);
159
break;
160
default:
161
throw new RuntimeException("Unsupported arguments found.");
162
}
163
testDSAGenParameterSpec(dataTuple);
164
165
}
166
167
private static class DataTuple {
168
169
private int primePLen;
170
private int subprimeQLen;
171
private boolean isDSASpecSupported;
172
173
private DataTuple(int primePLen, int subprimeQLen,
174
boolean isDSASpecSupported) {
175
this.primePLen = primePLen;
176
this.subprimeQLen = subprimeQLen;
177
this.isDSASpecSupported = isDSASpecSupported;
178
}
179
}
180
}
181
182