Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/DSAParameterGenerator.java
38830 views
1
/*
2
* Copyright (c) 1997, 2017, 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
// useful constants
71
private static final BigInteger TWO = BigInteger.valueOf(2);
72
73
public DSAParameterGenerator() {
74
}
75
76
/**
77
* Initializes this parameter generator for a certain strength
78
* and source of randomness.
79
*
80
* @param strength the strength (size of prime) in bits
81
* @param random the source of randomness
82
*/
83
@Override
84
protected void engineInit(int strength, SecureRandom random) {
85
if ((strength != 2048) && (strength != 3072) &&
86
((strength < 512) || (strength > 1024) || (strength % 64 != 0))) {
87
throw new InvalidParameterException(
88
"Unexpected strength (size of prime): " + strength +
89
". Prime size should be 512-1024, 2048, or 3072");
90
}
91
this.valueL = strength;
92
this.valueN = getDefDSASubprimeSize(strength);
93
this.seedLen = valueN;
94
this.random = random;
95
}
96
97
/**
98
* Initializes this parameter generator with a set of
99
* algorithm-specific parameter generation values.
100
*
101
* @param genParamSpec the set of algorithm-specific parameter
102
* generation values
103
* @param random the source of randomness
104
*
105
* @exception InvalidAlgorithmParameterException if the given parameter
106
* generation values are inappropriate for this parameter generator
107
*/
108
@Override
109
protected void engineInit(AlgorithmParameterSpec genParamSpec,
110
SecureRandom random) throws InvalidAlgorithmParameterException {
111
if (!(genParamSpec instanceof DSAGenParameterSpec)) {
112
throw new InvalidAlgorithmParameterException("Invalid parameter");
113
}
114
DSAGenParameterSpec dsaGenParams = (DSAGenParameterSpec)genParamSpec;
115
116
// directly initialize using the already validated values
117
this.valueL = dsaGenParams.getPrimePLength();
118
this.valueN = dsaGenParams.getSubprimeQLength();
119
this.seedLen = dsaGenParams.getSeedLength();
120
this.random = random;
121
}
122
123
/**
124
* Generates the parameters.
125
*
126
* @return the new AlgorithmParameters object
127
*/
128
@Override
129
protected AlgorithmParameters engineGenerateParameters() {
130
AlgorithmParameters algParams = null;
131
try {
132
if (this.random == null) {
133
this.random = new SecureRandom();
134
}
135
if (valueL == -1) {
136
engineInit(DEF_DSA_KEY_SIZE, this.random);
137
}
138
BigInteger[] pAndQ = generatePandQ(this.random, valueL,
139
valueN, seedLen);
140
BigInteger paramP = pAndQ[0];
141
BigInteger paramQ = pAndQ[1];
142
BigInteger paramG = generateG(paramP, paramQ);
143
144
DSAParameterSpec dsaParamSpec =
145
new DSAParameterSpec(paramP, paramQ, paramG);
146
algParams = AlgorithmParameters.getInstance("DSA", "SUN");
147
algParams.init(dsaParamSpec);
148
} catch (InvalidParameterSpecException e) {
149
// this should never happen
150
throw new RuntimeException(e.getMessage());
151
} catch (NoSuchAlgorithmException e) {
152
// this should never happen, because we provide it
153
throw new RuntimeException(e.getMessage());
154
} catch (NoSuchProviderException e) {
155
// this should never happen, because we provide it
156
throw new RuntimeException(e.getMessage());
157
}
158
159
return algParams;
160
}
161
162
/*
163
* Generates the prime and subprime parameters for DSA,
164
* using the provided source of randomness.
165
* This method will generate new seeds until a suitable
166
* seed has been found.
167
*
168
* @param random the source of randomness to generate the
169
* seed
170
* @param valueL the size of <code>p</code>, in bits.
171
* @param valueN the size of <code>q</code>, in bits.
172
* @param seedLen the length of <code>seed</code>, in bits.
173
*
174
* @return an array of BigInteger, with <code>p</code> at index 0 and
175
* <code>q</code> at index 1, the seed at index 2, and the counter value
176
* at index 3.
177
*/
178
private static BigInteger[] generatePandQ(SecureRandom random, int valueL,
179
int valueN, int seedLen) {
180
String hashAlg = null;
181
if (valueN == 160) {
182
hashAlg = "SHA";
183
} else if (valueN == 224) {
184
hashAlg = "SHA-224";
185
} else if (valueN == 256) {
186
hashAlg = "SHA-256";
187
}
188
MessageDigest hashObj = null;
189
try {
190
hashObj = MessageDigest.getInstance(hashAlg);
191
} catch (NoSuchAlgorithmException nsae) {
192
// should never happen
193
nsae.printStackTrace();
194
}
195
196
/* Step 3, 4: Useful variables */
197
int outLen = hashObj.getDigestLength()*8;
198
int n = (valueL - 1) / outLen;
199
int b = (valueL - 1) % outLen;
200
byte[] seedBytes = new byte[seedLen/8];
201
BigInteger twoSl = TWO.pow(seedLen);
202
int primeCertainty = -1;
203
if (valueL <= 1024) {
204
primeCertainty = 80;
205
} else if (valueL == 2048) {
206
primeCertainty = 112;
207
} else if (valueL == 3072) {
208
primeCertainty = 128;
209
}
210
if (primeCertainty < 0) {
211
throw new ProviderException("Invalid valueL: " + valueL);
212
}
213
BigInteger resultP, resultQ, seed = null;
214
int counter;
215
while (true) {
216
do {
217
/* Step 5 */
218
random.nextBytes(seedBytes);
219
seed = new BigInteger(1, seedBytes);
220
221
/* Step 6 */
222
BigInteger U = new BigInteger(1, hashObj.digest(seedBytes)).
223
mod(TWO.pow(valueN - 1));
224
225
/* Step 7 */
226
resultQ = TWO.pow(valueN - 1)
227
.add(U)
228
.add(BigInteger.ONE)
229
.subtract(U.mod(TWO));
230
} while (!resultQ.isProbablePrime(primeCertainty));
231
232
/* Step 10 */
233
BigInteger offset = BigInteger.ONE;
234
/* Step 11 */
235
for (counter = 0; counter < 4*valueL; counter++) {
236
BigInteger V[] = new BigInteger[n + 1];
237
/* Step 11.1 */
238
for (int j = 0; j <= n; j++) {
239
BigInteger J = BigInteger.valueOf(j);
240
BigInteger tmp = (seed.add(offset).add(J)).mod(twoSl);
241
byte[] vjBytes = hashObj.digest(toByteArray(tmp));
242
V[j] = new BigInteger(1, vjBytes);
243
}
244
/* Step 11.2 */
245
BigInteger W = V[0];
246
for (int i = 1; i < n; i++) {
247
W = W.add(V[i].multiply(TWO.pow(i * outLen)));
248
}
249
W = W.add((V[n].mod(TWO.pow(b)))
250
.multiply(TWO.pow(n * outLen)));
251
/* Step 11.3 */
252
BigInteger twoLm1 = TWO.pow(valueL - 1);
253
BigInteger X = W.add(twoLm1);
254
/* Step 11.4, 11.5 */
255
BigInteger c = X.mod(resultQ.multiply(TWO));
256
resultP = X.subtract(c.subtract(BigInteger.ONE));
257
/* Step 11.6, 11.7 */
258
if (resultP.compareTo(twoLm1) > -1
259
&& resultP.isProbablePrime(primeCertainty)) {
260
/* Step 11.8 */
261
BigInteger[] result = {resultP, resultQ, seed,
262
BigInteger.valueOf(counter)};
263
return result;
264
}
265
/* Step 11.9 */
266
offset = offset.add(BigInteger.valueOf(n)).add(BigInteger.ONE);
267
}
268
}
269
270
}
271
272
/*
273
* Generates the <code>g</code> parameter for DSA.
274
*
275
* @param p the prime, <code>p</code>.
276
* @param q the subprime, <code>q</code>.
277
*
278
* @param the <code>g</code>
279
*/
280
private static BigInteger generateG(BigInteger p, BigInteger q) {
281
BigInteger h = BigInteger.ONE;
282
/* Step 1 */
283
BigInteger pMinusOneOverQ = (p.subtract(BigInteger.ONE)).divide(q);
284
BigInteger resultG = BigInteger.ONE;
285
while (resultG.compareTo(TWO) < 0) {
286
/* Step 3 */
287
resultG = h.modPow(pMinusOneOverQ, p);
288
h = h.add(BigInteger.ONE);
289
}
290
return resultG;
291
}
292
293
/*
294
* Converts the result of a BigInteger.toByteArray call to an exact
295
* signed magnitude representation for any positive number.
296
*/
297
private static byte[] toByteArray(BigInteger bigInt) {
298
byte[] result = bigInt.toByteArray();
299
if (result[0] == 0) {
300
byte[] tmp = new byte[result.length - 1];
301
System.arraycopy(result, 1, tmp, 0, tmp.length);
302
result = tmp;
303
}
304
return result;
305
}
306
}
307
308