author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 13672 | 604588823b5a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, 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 com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.spec.*; |
|
30 |
import javax.crypto.spec.DHParameterSpec; |
|
31 |
import javax.crypto.spec.DHGenParameterSpec; |
|
32 |
||
33 |
/* |
|
34 |
* This class generates parameters for the Diffie-Hellman algorithm. |
|
35 |
* The parameters are a prime, a base, and optionally the length in bits of |
|
36 |
* the private value. |
|
37 |
* |
|
38 |
* <p>The Diffie-Hellman parameter generation accepts the size in bits of the |
|
39 |
* prime modulus and the size in bits of the random exponent as input. |
|
40 |
* The size of the prime modulus defaults to 1024 bits. |
|
41 |
* |
|
42 |
* @author Jan Luehe |
|
43 |
* |
|
44 |
* |
|
45 |
* @see java.security.AlgorithmParameters |
|
46 |
* @see java.security.spec.AlgorithmParameterSpec |
|
47 |
* @see DHParameters |
|
48 |
*/ |
|
49 |
public final class DHParameterGenerator |
|
50 |
extends AlgorithmParameterGeneratorSpi { |
|
51 |
||
52 |
// The size in bits of the prime modulus |
|
53 |
private int primeSize = 1024; |
|
54 |
||
55 |
// The size in bits of the random exponent (private value) |
|
56 |
private int exponentSize = 0; |
|
57 |
||
58 |
// The source of randomness |
|
59 |
private SecureRandom random = null; |
|
60 |
||
61 |
/** |
|
62 |
* Initializes this parameter generator for a certain keysize |
|
63 |
* and source of randomness. |
|
64 |
* The keysize is specified as the size in bits of the prime modulus. |
|
65 |
* |
|
66 |
* @param keysize the keysize (size of prime modulus) in bits |
|
67 |
* @param random the source of randomness |
|
68 |
*/ |
|
69 |
protected void engineInit(int keysize, SecureRandom random) { |
|
70 |
if ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0)) { |
|
71 |
throw new InvalidParameterException("Keysize must be multiple " |
|
72 |
+ "of 64, and can only range " |
|
73 |
+ "from 512 to 1024 " |
|
74 |
+ "(inclusive)"); |
|
75 |
} |
|
76 |
this.primeSize = keysize; |
|
77 |
this.random = random; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Initializes this parameter generator with a set of parameter |
|
82 |
* generation values, which specify the size of the prime modulus and |
|
83 |
* the size of the random exponent, both in bits. |
|
84 |
* |
|
85 |
* @param params the set of parameter generation values |
|
86 |
* @param random the source of randomness |
|
87 |
* |
|
88 |
* @exception InvalidAlgorithmParameterException if the given parameter |
|
89 |
* generation values are inappropriate for this parameter generator |
|
90 |
*/ |
|
91 |
protected void engineInit(AlgorithmParameterSpec genParamSpec, |
|
92 |
SecureRandom random) |
|
93 |
throws InvalidAlgorithmParameterException { |
|
94 |
if (!(genParamSpec instanceof DHGenParameterSpec)) { |
|
95 |
throw new InvalidAlgorithmParameterException |
|
96 |
("Inappropriate parameter type"); |
|
97 |
} |
|
98 |
||
99 |
DHGenParameterSpec dhParamSpec = (DHGenParameterSpec)genParamSpec; |
|
100 |
||
101 |
primeSize = dhParamSpec.getPrimeSize(); |
|
102 |
if ((primeSize<512) || (primeSize>1024) || (primeSize%64 != 0)) { |
|
103 |
throw new InvalidAlgorithmParameterException |
|
104 |
("Modulus size must be multiple of 64, and can only range " |
|
105 |
+ "from 512 to 1024 (inclusive)"); |
|
106 |
} |
|
107 |
||
108 |
exponentSize = dhParamSpec.getExponentSize(); |
|
109 |
if (exponentSize <= 0) { |
|
110 |
throw new InvalidAlgorithmParameterException |
|
111 |
("Exponent size must be greater than zero"); |
|
112 |
} |
|
113 |
||
114 |
// Require exponentSize < primeSize |
|
115 |
if (exponentSize >= primeSize) { |
|
116 |
throw new InvalidAlgorithmParameterException |
|
117 |
("Exponent size must be less than modulus size"); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Generates the parameters. |
|
123 |
* |
|
124 |
* @return the new AlgorithmParameters object |
|
125 |
*/ |
|
126 |
protected AlgorithmParameters engineGenerateParameters() { |
|
127 |
AlgorithmParameters algParams = null; |
|
128 |
||
129 |
if (this.exponentSize == 0) { |
|
130 |
this.exponentSize = this.primeSize - 1; |
|
131 |
} |
|
132 |
||
133 |
if (this.random == null) |
|
134 |
this.random = SunJCE.RANDOM; |
|
135 |
||
136 |
try { |
|
137 |
AlgorithmParameterGenerator paramGen; |
|
138 |
DSAParameterSpec dsaParamSpec; |
|
139 |
||
140 |
paramGen = AlgorithmParameterGenerator.getInstance("DSA"); |
|
141 |
paramGen.init(this.primeSize, random); |
|
142 |
algParams = paramGen.generateParameters(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
143 |
dsaParamSpec = algParams.getParameterSpec(DSAParameterSpec.class); |
2 | 144 |
|
145 |
DHParameterSpec dhParamSpec; |
|
146 |
if (this.exponentSize > 0) { |
|
147 |
dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), |
|
148 |
dsaParamSpec.getG(), |
|
149 |
this.exponentSize); |
|
150 |
} else { |
|
151 |
dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), |
|
152 |
dsaParamSpec.getG()); |
|
153 |
} |
|
154 |
algParams = AlgorithmParameters.getInstance("DH", "SunJCE"); |
|
155 |
algParams.init(dhParamSpec); |
|
156 |
} catch (InvalidParameterSpecException e) { |
|
157 |
// this should never happen |
|
158 |
throw new RuntimeException(e.getMessage()); |
|
159 |
} catch (NoSuchAlgorithmException e) { |
|
160 |
// this should never happen, because we provide it |
|
161 |
throw new RuntimeException(e.getMessage()); |
|
162 |
} catch (NoSuchProviderException e) { |
|
163 |
// this should never happen, because we provide it |
|
164 |
throw new RuntimeException(e.getMessage()); |
|
165 |
} |
|
166 |
||
167 |
return algParams; |
|
168 |
} |
|
169 |
} |