Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAParameterGenerator.java
67848 views
1
/*
2
* Copyright (c) 1997, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.provider;
27
28
import java.math.BigInteger;
29
import java.security.AlgorithmParameterGeneratorSpi;
30
import java.security.AlgorithmParameters;
31
import java.security.InvalidAlgorithmParameterException;
32
import java.security.NoSuchAlgorithmException;
33
import java.security.NoSuchProviderException;
34
import java.security.InvalidParameterException;
35
import java.security.MessageDigest;
36
import java.security.SecureRandom;
37
import java.security.ProviderException;
38
import java.security.spec.AlgorithmParameterSpec;
39
import java.security.spec.InvalidParameterSpecException;
40
import java.security.spec.DSAParameterSpec;
41
import java.security.spec.DSAGenParameterSpec;
42
43
import static sun.security.util.SecurityProviderConstants.DEF_DSA_KEY_SIZE;
44
import static sun.security.util.SecurityProviderConstants.getDefDSASubprimeSize;
45
46
47
/**
48
* This class generates parameters for the DSA algorithm.
49
*
50
* @author Jan Luehe
51
*
52
*
53
* @see java.security.AlgorithmParameters
54
* @see java.security.spec.AlgorithmParameterSpec
55
* @see DSAParameters
56
*
57
* @since 1.2
58
*/
59
60
public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
61
62
// the length of prime P, subPrime Q, and seed in bits
63
private int valueL = -1;
64
private int valueN = -1;
65
private int seedLen = -1;
66
67
// the source of randomness
68
private SecureRandom random;
69
70
public DSAParameterGenerator() {
71
}
72
73
/**
74
* Initializes this parameter generator for a certain strength
75
* and source of randomness.
76
*
77
* @param strength the strength (size of prime) in bits
78
* @param random the source of randomness
79
*/
80
@Override
81
protected void engineInit(int strength, SecureRandom random) {
82
if ((strength != 2048) && (strength != 3072) &&
83
((strength < 512) || (strength > 1024) || (strength % 64 != 0))) {
84
throw new InvalidParameterException(
85
"Unexpected strength (size of prime): " + strength +
86
". Prime size should be 512-1024, 2048, or 3072");
87
}
88
this.valueL = strength;
89
this.valueN = getDefDSASubprimeSize(strength);
90
this.seedLen = valueN;
91
this.random = random;
92
}
93
94
/**
95
* Initializes this parameter generator with a set of
96
* algorithm-specific parameter generation values.
97
*
98
* @param genParamSpec the set of algorithm-specific parameter
99
* generation values
100
* @param random the source of randomness
101
*
102
* @exception InvalidAlgorithmParameterException if the given parameter
103
* generation values are inappropriate for this parameter generator
104
*/
105
@Override
106
protected void engineInit(AlgorithmParameterSpec genParamSpec,
107
SecureRandom random) throws InvalidAlgorithmParameterException {
108
if (!(genParamSpec instanceof DSAGenParameterSpec)) {
109
throw new InvalidAlgorithmParameterException("Invalid parameter");
110
}
111
DSAGenParameterSpec dsaGenParams = (DSAGenParameterSpec)genParamSpec;
112
113
// directly initialize using the already validated values
114
this.valueL = dsaGenParams.getPrimePLength();
115
this.valueN = dsaGenParams.getSubprimeQLength();
116
this.seedLen = dsaGenParams.getSeedLength();
117
this.random = random;
118
}
119
120
/**
121
* Generates the parameters.
122
*
123
* @return the new AlgorithmParameters object
124
*/
125
@Override
126
protected AlgorithmParameters engineGenerateParameters() {
127
AlgorithmParameters algParams = null;
128
try {
129
if (this.random == null) {
130
this.random = new SecureRandom();
131
}
132
if (valueL == -1) {
133
engineInit(DEF_DSA_KEY_SIZE, this.random);
134
}
135
BigInteger[] pAndQ = generatePandQ(this.random, valueL,
136
valueN, seedLen);
137
BigInteger paramP = pAndQ[0];
138
BigInteger paramQ = pAndQ[1];
139
BigInteger paramG = generateG(paramP, paramQ);
140
141
DSAParameterSpec dsaParamSpec =
142
new DSAParameterSpec(paramP, paramQ, paramG);
143
algParams = AlgorithmParameters.getInstance("DSA", "SUN");
144
algParams.init(dsaParamSpec);
145
} catch (InvalidParameterSpecException e) {
146
// this should never happen
147
throw new RuntimeException(e.getMessage());
148
} catch (NoSuchAlgorithmException e) {
149
// this should never happen, because we provide it
150
throw new RuntimeException(e.getMessage());
151
} catch (NoSuchProviderException e) {
152
// this should never happen, because we provide it
153
throw new RuntimeException(e.getMessage());
154
}
155
156
return algParams;
157
}
158
159
/*
160
* Generates the prime and subprime parameters for DSA,
161
* using the provided source of randomness.
162
* This method will generate new seeds until a suitable
163
* seed has been found.
164
*
165
* @param random the source of randomness to generate the
166
* seed
167
* @param valueL the size of <code>p</code>, in bits.
168
* @param valueN the size of <code>q</code>, in bits.
169
* @param seedLen the length of <code>seed</code>, in bits.
170
*
171
* @return an array of BigInteger, with <code>p</code> at index 0 and
172
* <code>q</code> at index 1, the seed at index 2, and the counter value
173
* at index 3.
174
*/
175
private static BigInteger[] generatePandQ(SecureRandom random, int valueL,
176
int valueN, int seedLen) {
177
String hashAlg = null;
178
if (valueN == 160) {
179
hashAlg = "SHA";
180
} else if (valueN == 224) {
181
hashAlg = "SHA-224";
182
} else if (valueN == 256) {
183
hashAlg = "SHA-256";
184
}
185
MessageDigest hashObj = null;
186
try {
187
hashObj = MessageDigest.getInstance(hashAlg);
188
} catch (NoSuchAlgorithmException nsae) {
189
// should never happen
190
}
191
192
/* Step 3, 4: Useful variables */
193
int outLen = hashObj.getDigestLength()*8;
194
int n = (valueL - 1) / outLen;
195
int b = (valueL - 1) % outLen;
196
byte[] seedBytes = new byte[seedLen/8];
197
BigInteger twoSl = BigInteger.TWO.pow(seedLen);
198
int primeCertainty = -1;
199
if (valueL <= 1024) {
200
primeCertainty = 80;
201
} else if (valueL == 2048) {
202
primeCertainty = 112;
203
} else if (valueL == 3072) {
204
primeCertainty = 128;
205
}
206
if (primeCertainty < 0) {
207
throw new ProviderException("Invalid valueL: " + valueL);
208
}
209
BigInteger resultP, resultQ, seed = null;
210
int counter;
211
while (true) {
212
do {
213
/* Step 5 */
214
random.nextBytes(seedBytes);
215
seed = new BigInteger(1, seedBytes);
216
217
/* Step 6 */
218
BigInteger U = new BigInteger(1, hashObj.digest(seedBytes)).
219
mod(BigInteger.TWO.pow(valueN - 1));
220
221
/* Step 7 */
222
resultQ = BigInteger.TWO.pow(valueN - 1)
223
.add(U)
224
.add(BigInteger.ONE)
225
.subtract(U.mod(BigInteger.TWO));
226
} while (!resultQ.isProbablePrime(primeCertainty));
227
228
/* Step 10 */
229
BigInteger offset = BigInteger.ONE;
230
/* Step 11 */
231
for (counter = 0; counter < 4*valueL; counter++) {
232
BigInteger[] V = new BigInteger[n + 1];
233
/* Step 11.1 */
234
for (int j = 0; j <= n; j++) {
235
BigInteger J = BigInteger.valueOf(j);
236
BigInteger tmp = (seed.add(offset).add(J)).mod(twoSl);
237
byte[] vjBytes = hashObj.digest(toByteArray(tmp));
238
V[j] = new BigInteger(1, vjBytes);
239
}
240
/* Step 11.2 */
241
BigInteger W = V[0];
242
for (int i = 1; i < n; i++) {
243
W = W.add(V[i].multiply(BigInteger.TWO.pow(i * outLen)));
244
}
245
W = W.add((V[n].mod(BigInteger.TWO.pow(b)))
246
.multiply(BigInteger.TWO.pow(n * outLen)));
247
/* Step 11.3 */
248
BigInteger twoLm1 = BigInteger.TWO.pow(valueL - 1);
249
BigInteger X = W.add(twoLm1);
250
/* Step 11.4, 11.5 */
251
BigInteger c = X.mod(resultQ.multiply(BigInteger.TWO));
252
resultP = X.subtract(c.subtract(BigInteger.ONE));
253
/* Step 11.6, 11.7 */
254
if (resultP.compareTo(twoLm1) > -1
255
&& resultP.isProbablePrime(primeCertainty)) {
256
/* Step 11.8 */
257
BigInteger[] result = {resultP, resultQ, seed,
258
BigInteger.valueOf(counter)};
259
return result;
260
}
261
/* Step 11.9 */
262
offset = offset.add(BigInteger.valueOf(n)).add(BigInteger.ONE);
263
}
264
}
265
266
}
267
268
/*
269
* Generates the <code>g</code> parameter for DSA.
270
*
271
* @param p the prime, <code>p</code>.
272
* @param q the subprime, <code>q</code>.
273
*
274
* @param the <code>g</code>
275
*/
276
private static BigInteger generateG(BigInteger p, BigInteger q) {
277
BigInteger h = BigInteger.ONE;
278
/* Step 1 */
279
BigInteger pMinusOneOverQ = (p.subtract(BigInteger.ONE)).divide(q);
280
BigInteger resultG = BigInteger.ONE;
281
while (resultG.compareTo(BigInteger.TWO) < 0) {
282
/* Step 3 */
283
resultG = h.modPow(pMinusOneOverQ, p);
284
h = h.add(BigInteger.ONE);
285
}
286
return resultG;
287
}
288
289
/*
290
* Converts the result of a BigInteger.toByteArray call to an exact
291
* signed magnitude representation for any positive number.
292
*/
293
private static byte[] toByteArray(BigInteger bigInt) {
294
byte[] result = bigInt.toByteArray();
295
if (result[0] == 0) {
296
byte[] tmp = new byte[result.length - 1];
297
System.arraycopy(result, 1, tmp, 0, tmp.length);
298
result = tmp;
299
}
300
return result;
301
}
302
}
303
304