1 |
769 |
jeremybenn |
/* DHParametersGenerator.java -- JCE Adapter for a generator of DH parameters
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.jce.sig;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Registry;
|
42 |
|
|
import gnu.javax.crypto.jce.GnuCrypto;
|
43 |
|
|
import gnu.javax.crypto.key.dh.GnuDHKeyPairGenerator;
|
44 |
|
|
import gnu.javax.crypto.key.dh.RFC2631;
|
45 |
|
|
|
46 |
|
|
import java.math.BigInteger;
|
47 |
|
|
import java.security.AlgorithmParameterGeneratorSpi;
|
48 |
|
|
import java.security.AlgorithmParameters;
|
49 |
|
|
import java.security.InvalidAlgorithmParameterException;
|
50 |
|
|
import java.security.InvalidParameterException;
|
51 |
|
|
import java.security.NoSuchAlgorithmException;
|
52 |
|
|
import java.security.Provider;
|
53 |
|
|
import java.security.SecureRandom;
|
54 |
|
|
import java.security.spec.AlgorithmParameterSpec;
|
55 |
|
|
import java.security.spec.InvalidParameterSpecException;
|
56 |
|
|
|
57 |
|
|
import javax.crypto.spec.DHGenParameterSpec;
|
58 |
|
|
import javax.crypto.spec.DHParameterSpec;
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* A JCE Adapter for a generator of DH parameters.
|
62 |
|
|
*/
|
63 |
|
|
public class DHParametersGenerator
|
64 |
|
|
extends AlgorithmParameterGeneratorSpi
|
65 |
|
|
{
|
66 |
|
|
private static final Provider GNU_CRYPTO = new GnuCrypto();
|
67 |
|
|
|
68 |
|
|
/** Size of the prime (public) modulus in bits. */
|
69 |
|
|
private int modulusSize = -1;
|
70 |
|
|
|
71 |
|
|
/** Size of the prime (private) modulus in bits. */
|
72 |
|
|
private int exponentSize = -1;
|
73 |
|
|
|
74 |
|
|
/** User specified source of randomness. */
|
75 |
|
|
private SecureRandom rnd;
|
76 |
|
|
|
77 |
|
|
/** Our concrete DH parameters generator. */
|
78 |
|
|
private RFC2631 rfc2631;
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
protected void engineInit(int size, SecureRandom random)
|
82 |
|
|
{
|
83 |
|
|
if ((size % 256) != 0 || size < GnuDHKeyPairGenerator.DEFAULT_PRIME_SIZE)
|
84 |
|
|
throw new InvalidParameterException("Prime modulus (p) size (in bits) "
|
85 |
|
|
+ "MUST be a multiple of 256, and "
|
86 |
|
|
+ "greater than or equal to 1024");
|
87 |
|
|
this.modulusSize = size;
|
88 |
|
|
this.rnd = random;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
protected void engineInit(AlgorithmParameterSpec spec, SecureRandom random)
|
92 |
|
|
throws InvalidAlgorithmParameterException
|
93 |
|
|
{
|
94 |
|
|
if (spec instanceof DHParameterSpec)
|
95 |
|
|
{
|
96 |
|
|
DHParameterSpec dhSpec = (DHParameterSpec) spec;
|
97 |
|
|
BigInteger p = dhSpec.getP();
|
98 |
|
|
int size = p.bitLength();
|
99 |
|
|
this.engineInit(size, random);
|
100 |
|
|
}
|
101 |
|
|
else if (spec instanceof DHGenParameterSpec)
|
102 |
|
|
{
|
103 |
|
|
DHGenParameterSpec dhSpec = (DHGenParameterSpec) spec;
|
104 |
|
|
int size = dhSpec.getPrimeSize();
|
105 |
|
|
this.engineInit(size, random);
|
106 |
|
|
exponentSize = dhSpec.getExponentSize();
|
107 |
|
|
|
108 |
|
|
if ((exponentSize % 8) != 0
|
109 |
|
|
|| exponentSize < GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE)
|
110 |
|
|
throw new InvalidParameterException("Random exponent size (in bits) "
|
111 |
|
|
+ "MUST be a multiple of 8, and "
|
112 |
|
|
+ "greater than or equal to "
|
113 |
|
|
+ GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE);
|
114 |
|
|
if (exponentSize > modulusSize)
|
115 |
|
|
throw new InvalidParameterException("Random exponent size (in bits) "
|
116 |
|
|
+ "MUST be less than that of the "
|
117 |
|
|
+ "public prime modulus (p)");
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
throw new InvalidAlgorithmParameterException("Wrong AlgorithmParameterSpec type: "
|
121 |
|
|
+ spec.getClass().getName());
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
protected AlgorithmParameters engineGenerateParameters()
|
125 |
|
|
{
|
126 |
|
|
if (modulusSize < 1)
|
127 |
|
|
modulusSize = GnuDHKeyPairGenerator.DEFAULT_PRIME_SIZE;
|
128 |
|
|
|
129 |
|
|
if (exponentSize < 1)
|
130 |
|
|
exponentSize = GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE;
|
131 |
|
|
|
132 |
|
|
rfc2631 = new RFC2631(exponentSize, modulusSize, rnd);
|
133 |
|
|
BigInteger[] params = rfc2631.generateParameters();
|
134 |
|
|
BigInteger p = params[RFC2631.DH_PARAMS_P];
|
135 |
|
|
BigInteger g = params[RFC2631.DH_PARAMS_G];
|
136 |
|
|
int l = params[RFC2631.DH_PARAMS_Q].bitLength();
|
137 |
|
|
DHParameterSpec spec = new DHParameterSpec(p, g, l);
|
138 |
|
|
AlgorithmParameters result = null;
|
139 |
|
|
try
|
140 |
|
|
{
|
141 |
|
|
result = AlgorithmParameters.getInstance(Registry.DH_KPG, GNU_CRYPTO);
|
142 |
|
|
result.init(spec);
|
143 |
|
|
}
|
144 |
|
|
catch (NoSuchAlgorithmException ignore)
|
145 |
|
|
{
|
146 |
|
|
}
|
147 |
|
|
catch (InvalidParameterSpecException ignore)
|
148 |
|
|
{
|
149 |
|
|
}
|
150 |
|
|
return result;
|
151 |
|
|
}
|
152 |
|
|
}
|