author | weijun |
Wed, 01 Aug 2018 13:35:08 +0800 | |
changeset 51272 | 9d92ff04a29c |
parent 47421 | f9e03aef3a49 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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; |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
35 |
import java.security.MessageDigest; |
2 | 36 |
import java.security.SecureRandom; |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
37 |
import java.security.ProviderException; |
2 | 38 |
import java.security.spec.AlgorithmParameterSpec; |
39 |
import java.security.spec.InvalidParameterSpecException; |
|
40 |
import java.security.spec.DSAParameterSpec; |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
41 |
import java.security.spec.DSAGenParameterSpec; |
2 | 42 |
|
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
43 |
import static sun.security.util.SecurityProviderConstants.DEF_DSA_KEY_SIZE; |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
44 |
import static sun.security.util.SecurityProviderConstants.getDefDSASubprimeSize; |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
45 |
|
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
46 |
|
2 | 47 |
/** |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
48 |
* This class generates parameters for the DSA algorithm. |
2 | 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 |
||
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
62 |
// the length of prime P, subPrime Q, and seed in bits |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
63 |
private int valueL = -1; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
64 |
private int valueN = -1; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
65 |
private int seedLen = -1; |
2 | 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 |
*/ |
|
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
80 |
@Override |
2 | 81 |
protected void engineInit(int strength, SecureRandom random) { |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
82 |
if ((strength != 2048) && (strength != 3072) && |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
83 |
((strength < 512) || (strength > 1024) || (strength % 64 != 0))) { |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
84 |
throw new InvalidParameterException( |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
85 |
"Unexpected strength (size of prime): " + strength + |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
86 |
". Prime size should be 512-1024, 2048, or 3072"); |
2 | 87 |
} |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
88 |
this.valueL = strength; |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
89 |
this.valueN = getDefDSASubprimeSize(strength); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
90 |
this.seedLen = valueN; |
2 | 91 |
this.random = random; |
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Initializes this parameter generator with a set of |
|
96 |
* algorithm-specific parameter generation values. |
|
97 |
* |
|
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
98 |
* @param genParamSpec the set of algorithm-specific parameter |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
99 |
* generation values |
2 | 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 |
*/ |
|
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
105 |
@Override |
2 | 106 |
protected void engineInit(AlgorithmParameterSpec genParamSpec, |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
107 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
108 |
if (!(genParamSpec instanceof DSAGenParameterSpec)) { |
2 | 109 |
throw new InvalidAlgorithmParameterException("Invalid parameter"); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
110 |
} |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
111 |
DSAGenParameterSpec dsaGenParams = (DSAGenParameterSpec)genParamSpec; |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
112 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
113 |
// directly initialize using the already validated values |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
114 |
this.valueL = dsaGenParams.getPrimePLength(); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
115 |
this.valueN = dsaGenParams.getSubprimeQLength(); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
116 |
this.seedLen = dsaGenParams.getSeedLength(); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
117 |
this.random = random; |
2 | 118 |
} |
119 |
||
120 |
/** |
|
121 |
* Generates the parameters. |
|
122 |
* |
|
123 |
* @return the new AlgorithmParameters object |
|
124 |
*/ |
|
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
125 |
@Override |
2 | 126 |
protected AlgorithmParameters engineGenerateParameters() { |
127 |
AlgorithmParameters algParams = null; |
|
128 |
try { |
|
129 |
if (this.random == null) { |
|
130 |
this.random = new SecureRandom(); |
|
131 |
} |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
132 |
if (valueL == -1) { |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
133 |
engineInit(DEF_DSA_KEY_SIZE, this.random); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
134 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
135 |
BigInteger[] pAndQ = generatePandQ(this.random, valueL, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
136 |
valueN, seedLen); |
2 | 137 |
BigInteger paramP = pAndQ[0]; |
138 |
BigInteger paramQ = pAndQ[1]; |
|
139 |
BigInteger paramG = generateG(paramP, paramQ); |
|
140 |
||
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
141 |
DSAParameterSpec dsaParamSpec = |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
142 |
new DSAParameterSpec(paramP, paramQ, paramG); |
2 | 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 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
167 |
* @param valueL the size of <code>p</code>, in bits. |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
168 |
* @param valueN the size of <code>q</code>, in bits. |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
169 |
* @param seedLen the length of <code>seed</code>, in bits. |
2 | 170 |
* |
171 |
* @return an array of BigInteger, with <code>p</code> at index 0 and |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
172 |
* <code>q</code> at index 1, the seed at index 2, and the counter value |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
173 |
* at index 3. |
2 | 174 |
*/ |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
175 |
private static BigInteger[] generatePandQ(SecureRandom random, int valueL, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
176 |
int valueN, int seedLen) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
177 |
String hashAlg = null; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
178 |
if (valueN == 160) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
179 |
hashAlg = "SHA"; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
180 |
} else if (valueN == 224) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
181 |
hashAlg = "SHA-224"; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
182 |
} else if (valueN == 256) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
183 |
hashAlg = "SHA-256"; |
2 | 184 |
} |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
185 |
MessageDigest hashObj = null; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
186 |
try { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
187 |
hashObj = MessageDigest.getInstance(hashAlg); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
188 |
} catch (NoSuchAlgorithmException nsae) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
189 |
// should never happen |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
190 |
nsae.printStackTrace(); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
191 |
} |
2 | 192 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
193 |
/* Step 3, 4: Useful variables */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
194 |
int outLen = hashObj.getDigestLength()*8; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
195 |
int n = (valueL - 1) / outLen; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
196 |
int b = (valueL - 1) % outLen; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
197 |
byte[] seedBytes = new byte[seedLen/8]; |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
198 |
BigInteger twoSl = BigInteger.TWO.pow(seedLen); |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
199 |
int primeCertainty = -1; |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
200 |
if (valueL <= 1024) { |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
201 |
primeCertainty = 80; |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
202 |
} else if (valueL == 2048) { |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
203 |
primeCertainty = 112; |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
204 |
} else if (valueL == 3072) { |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
205 |
primeCertainty = 128; |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
206 |
} |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
207 |
if (primeCertainty < 0) { |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
208 |
throw new ProviderException("Invalid valueL: " + valueL); |
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
209 |
} |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
210 |
BigInteger resultP, resultQ, seed = null; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
211 |
int counter; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
212 |
while (true) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
213 |
do { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
214 |
/* Step 5 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
215 |
random.nextBytes(seedBytes); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
216 |
seed = new BigInteger(1, seedBytes); |
2 | 217 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
218 |
/* Step 6 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
219 |
BigInteger U = new BigInteger(1, hashObj.digest(seedBytes)). |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
220 |
mod(BigInteger.TWO.pow(valueN - 1)); |
2 | 221 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
222 |
/* Step 7 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
223 |
resultQ = BigInteger.TWO.pow(valueN - 1) |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
224 |
.add(U) |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
225 |
.add(BigInteger.ONE) |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
226 |
.subtract(U.mod(BigInteger.TWO)); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
227 |
} while (!resultQ.isProbablePrime(primeCertainty)); |
2 | 228 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
229 |
/* Step 10 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
230 |
BigInteger offset = BigInteger.ONE; |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
231 |
/* Step 11 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
232 |
for (counter = 0; counter < 4*valueL; counter++) { |
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
25859
diff
changeset
|
233 |
BigInteger[] V = new BigInteger[n + 1]; |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
234 |
/* Step 11.1 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
235 |
for (int j = 0; j <= n; j++) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
236 |
BigInteger J = BigInteger.valueOf(j); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
237 |
BigInteger tmp = (seed.add(offset).add(J)).mod(twoSl); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
238 |
byte[] vjBytes = hashObj.digest(toByteArray(tmp)); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
239 |
V[j] = new BigInteger(1, vjBytes); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
240 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
241 |
/* Step 11.2 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
242 |
BigInteger W = V[0]; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
243 |
for (int i = 1; i < n; i++) { |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
244 |
W = W.add(V[i].multiply(BigInteger.TWO.pow(i * outLen))); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
245 |
} |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
246 |
W = W.add((V[n].mod(BigInteger.TWO.pow(b))) |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
247 |
.multiply(BigInteger.TWO.pow(n * outLen))); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
248 |
/* Step 11.3 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
249 |
BigInteger twoLm1 = BigInteger.TWO.pow(valueL - 1); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
250 |
BigInteger X = W.add(twoLm1); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
251 |
/* Step 11.4, 11.5 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
252 |
BigInteger c = X.mod(resultQ.multiply(BigInteger.TWO)); |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
253 |
resultP = X.subtract(c.subtract(BigInteger.ONE)); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
254 |
/* Step 11.6, 11.7 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
255 |
if (resultP.compareTo(twoLm1) > -1 |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
256 |
&& resultP.isProbablePrime(primeCertainty)) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
257 |
/* Step 11.8 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
258 |
BigInteger[] result = {resultP, resultQ, seed, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
259 |
BigInteger.valueOf(counter)}; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
260 |
return result; |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
261 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
262 |
/* Step 11.9 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
263 |
offset = offset.add(BigInteger.valueOf(n)).add(BigInteger.ONE); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
264 |
} |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
265 |
} |
2 | 266 |
|
267 |
} |
|
268 |
||
269 |
/* |
|
270 |
* Generates the <code>g</code> parameter for DSA. |
|
271 |
* |
|
272 |
* @param p the prime, <code>p</code>. |
|
273 |
* @param q the subprime, <code>q</code>. |
|
274 |
* |
|
275 |
* @param the <code>g</code> |
|
276 |
*/ |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
277 |
private static BigInteger generateG(BigInteger p, BigInteger q) { |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
278 |
BigInteger h = BigInteger.ONE; |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
279 |
/* Step 1 */ |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
280 |
BigInteger pMinusOneOverQ = (p.subtract(BigInteger.ONE)).divide(q); |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
281 |
BigInteger resultG = BigInteger.ONE; |
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
282 |
while (resultG.compareTo(BigInteger.TWO) < 0) { |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
283 |
/* Step 3 */ |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
284 |
resultG = h.modPow(pMinusOneOverQ, p); |
37361
a790f7bc3878
8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents:
31538
diff
changeset
|
285 |
h = h.add(BigInteger.ONE); |
2 | 286 |
} |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
287 |
return resultG; |
2 | 288 |
} |
289 |
||
290 |
/* |
|
291 |
* Converts the result of a BigInteger.toByteArray call to an exact |
|
292 |
* signed magnitude representation for any positive number. |
|
293 |
*/ |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
5506
diff
changeset
|
294 |
private static byte[] toByteArray(BigInteger bigInt) { |
2 | 295 |
byte[] result = bigInt.toByteArray(); |
296 |
if (result[0] == 0) { |
|
297 |
byte[] tmp = new byte[result.length - 1]; |
|
298 |
System.arraycopy(result, 1, tmp, 0, tmp.length); |
|
299 |
result = tmp; |
|
300 |
} |
|
301 |
return result; |
|
302 |
} |
|
303 |
} |