1 |
14 |
jlechner |
/* KeyGenerator.java -- Interface to a symmetric key generator.
|
2 |
|
|
Copyright (C) 2004 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 javax.crypto;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Engine;
|
42 |
|
|
|
43 |
|
|
import java.lang.reflect.InvocationTargetException;
|
44 |
|
|
import java.security.InvalidAlgorithmParameterException;
|
45 |
|
|
import java.security.NoSuchAlgorithmException;
|
46 |
|
|
import java.security.NoSuchProviderException;
|
47 |
|
|
import java.security.Provider;
|
48 |
|
|
import java.security.SecureRandom;
|
49 |
|
|
import java.security.Security;
|
50 |
|
|
import java.security.spec.AlgorithmParameterSpec;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* A generic producer of keys for symmetric cryptography. The keys
|
54 |
|
|
* returned may be simple wrappers around byte arrays, or, if the
|
55 |
|
|
* target cipher requires them, more complex objects.
|
56 |
|
|
*
|
57 |
|
|
* @author Casey Marshall (csm@gnu.org)
|
58 |
|
|
* @since 1.4
|
59 |
|
|
* @see Cipher
|
60 |
|
|
* @see Mac
|
61 |
|
|
*/
|
62 |
|
|
public class KeyGenerator
|
63 |
|
|
{
|
64 |
|
|
|
65 |
|
|
// Constants and fields.
|
66 |
|
|
// ------------------------------------------------------------------------
|
67 |
|
|
|
68 |
|
|
private static final String SERVICE = "KeyGenerator";
|
69 |
|
|
|
70 |
|
|
/** The underlying generator implementation. */
|
71 |
|
|
private KeyGeneratorSpi kgSpi;
|
72 |
|
|
|
73 |
|
|
/** The provider of the implementation. */
|
74 |
|
|
private Provider provider;
|
75 |
|
|
|
76 |
|
|
/** The name of the algorithm. */
|
77 |
|
|
private String algorithm;
|
78 |
|
|
|
79 |
|
|
// Constructor.
|
80 |
|
|
// ------------------------------------------------------------------------
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Create a new key generator.
|
84 |
|
|
*
|
85 |
|
|
* @param kgSpi The underlying generator.
|
86 |
|
|
* @param provider The provider of this implementation.
|
87 |
|
|
* @param algorithm The algorithm's name.
|
88 |
|
|
*/
|
89 |
|
|
protected KeyGenerator(KeyGeneratorSpi kgSpi, Provider provider,
|
90 |
|
|
String algorithm)
|
91 |
|
|
{
|
92 |
|
|
this.kgSpi = kgSpi;
|
93 |
|
|
this.provider = provider;
|
94 |
|
|
this.algorithm = algorithm;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// Class methods.
|
98 |
|
|
// ------------------------------------------------------------------------
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* Create a new key generator, returning the first available
|
102 |
|
|
* implementation.
|
103 |
|
|
*
|
104 |
|
|
* @param algorithm The generator algorithm name.
|
105 |
|
|
* @throws java.security.NoSuchAlgorithmException If the specified
|
106 |
|
|
* algorithm does not exist.
|
107 |
|
|
*/
|
108 |
|
|
public static final KeyGenerator getInstance(String algorithm)
|
109 |
|
|
throws NoSuchAlgorithmException
|
110 |
|
|
{
|
111 |
|
|
Provider[] provs = Security.getProviders();
|
112 |
|
|
String msg = algorithm;
|
113 |
|
|
for (int i = 0; i < provs.length; i++)
|
114 |
|
|
{
|
115 |
|
|
try
|
116 |
|
|
{
|
117 |
|
|
return getInstance(algorithm, provs[i]);
|
118 |
|
|
}
|
119 |
|
|
catch (NoSuchAlgorithmException nsae)
|
120 |
|
|
{
|
121 |
|
|
msg = nsae.getMessage();
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
throw new NoSuchAlgorithmException(msg);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Create a new key generator from the named provider.
|
129 |
|
|
*
|
130 |
|
|
* @param algorithm The generator algorithm name.
|
131 |
|
|
* @param provider The name of the provider to use.
|
132 |
|
|
* @return An appropriate key generator, if found.
|
133 |
|
|
* @throws java.security.NoSuchAlgorithmException If the specified
|
134 |
|
|
* algorithm is not implemented by the named provider.
|
135 |
|
|
* @throws java.security.NoSuchProviderException If the named provider
|
136 |
|
|
* does not exist.
|
137 |
|
|
*/
|
138 |
|
|
public static final KeyGenerator getInstance(String algorithm, String provider)
|
139 |
|
|
throws NoSuchAlgorithmException, NoSuchProviderException
|
140 |
|
|
{
|
141 |
|
|
Provider p = Security.getProvider(provider);
|
142 |
|
|
if (p == null)
|
143 |
|
|
{
|
144 |
|
|
throw new NoSuchProviderException(provider);
|
145 |
|
|
}
|
146 |
|
|
return getInstance(algorithm, p);
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Create a new key generator from the supplied provider.
|
151 |
|
|
*
|
152 |
|
|
* @param algorithm The generator algorithm name.
|
153 |
|
|
* @param provider The provider to use.
|
154 |
|
|
* @return An appropriate key generator, if found.
|
155 |
|
|
* @throws java.security.NoSuchAlgorithmException If the specified
|
156 |
|
|
* algorithm is not implemented by the provider.
|
157 |
|
|
*/
|
158 |
|
|
public static final KeyGenerator getInstance(String algorithm, Provider provider)
|
159 |
|
|
throws NoSuchAlgorithmException
|
160 |
|
|
{
|
161 |
|
|
try
|
162 |
|
|
{
|
163 |
|
|
return new KeyGenerator((KeyGeneratorSpi)
|
164 |
|
|
Engine.getInstance(SERVICE, algorithm, provider),
|
165 |
|
|
provider, algorithm);
|
166 |
|
|
}
|
167 |
|
|
catch (InvocationTargetException ite)
|
168 |
|
|
{
|
169 |
|
|
if (ite.getCause() == null)
|
170 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
171 |
|
|
if (ite.getCause() instanceof NoSuchAlgorithmException)
|
172 |
|
|
throw (NoSuchAlgorithmException) ite.getCause();
|
173 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
174 |
|
|
}
|
175 |
|
|
catch (ClassCastException cce)
|
176 |
|
|
{
|
177 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
// Instance methods.
|
182 |
|
|
// ------------------------------------------------------------------------
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Generate a key.
|
186 |
|
|
*
|
187 |
|
|
* @return The new key.
|
188 |
|
|
*/
|
189 |
|
|
public final SecretKey generateKey()
|
190 |
|
|
{
|
191 |
|
|
return kgSpi.engineGenerateKey();
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
/**
|
195 |
|
|
* Return the name of this key generator.
|
196 |
|
|
*
|
197 |
|
|
* @return The algorithm name.
|
198 |
|
|
*/
|
199 |
|
|
public final String getAlgorithm()
|
200 |
|
|
{
|
201 |
|
|
return algorithm;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
/**
|
205 |
|
|
* Return the provider of the underlying implementation.
|
206 |
|
|
*
|
207 |
|
|
* @return The provider.
|
208 |
|
|
*/
|
209 |
|
|
public final Provider getProvider()
|
210 |
|
|
{
|
211 |
|
|
return provider;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
/**
|
215 |
|
|
* Initialize this key generator with a set of parameters; the
|
216 |
|
|
* highest-priority {@link java.security.SecureRandom} implementation
|
217 |
|
|
* will be used.
|
218 |
|
|
*
|
219 |
|
|
* @param params The algorithm parameters.
|
220 |
|
|
* @throws java.security.InvalidAlgorithmParameterException If the
|
221 |
|
|
* supplied parameters are inapproprate.
|
222 |
|
|
*/
|
223 |
|
|
public final void init(AlgorithmParameterSpec params)
|
224 |
|
|
throws InvalidAlgorithmParameterException
|
225 |
|
|
{
|
226 |
|
|
init(params, new SecureRandom());
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
/**
|
230 |
|
|
* Initialize this key generator with a set of parameters and a source
|
231 |
|
|
* of randomness.
|
232 |
|
|
*
|
233 |
|
|
* @param params The algorithm parameters.
|
234 |
|
|
* @param random The source of randomness.
|
235 |
|
|
* @throws java.security.InvalidAlgorithmParameterException If the
|
236 |
|
|
* supplied parameters are inapproprate.
|
237 |
|
|
*/
|
238 |
|
|
public final void init(AlgorithmParameterSpec params, SecureRandom random)
|
239 |
|
|
throws InvalidAlgorithmParameterException
|
240 |
|
|
{
|
241 |
|
|
kgSpi.engineInit(params, random);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Initialize this key generator with a key size (in bits); the
|
246 |
|
|
* highest-priority {@link java.security.SecureRandom} implementation
|
247 |
|
|
* will be used.
|
248 |
|
|
*
|
249 |
|
|
* @param keySize The target key size, in bits.
|
250 |
|
|
* @throws java.security.InvalidParameterException If the
|
251 |
|
|
* key size is unsupported.
|
252 |
|
|
*/
|
253 |
|
|
public final void init(int keySize)
|
254 |
|
|
{
|
255 |
|
|
init(keySize, new SecureRandom());
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/**
|
259 |
|
|
* Initialize this key generator with a key size (in bits) and a
|
260 |
|
|
* source of randomness.
|
261 |
|
|
*
|
262 |
|
|
* @param keySize The target key size, in bits.
|
263 |
|
|
* @param random The source of randomness.
|
264 |
|
|
* @throws java.security.InvalidAlgorithmParameterException If the
|
265 |
|
|
* key size is unsupported.
|
266 |
|
|
*/
|
267 |
|
|
public final void init(int keySize, SecureRandom random)
|
268 |
|
|
{
|
269 |
|
|
kgSpi.engineInit(keySize, random);
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/**
|
273 |
|
|
* Initialize this key generator with a source of randomness. The
|
274 |
|
|
* implementation-specific default parameters (such as key size) will
|
275 |
|
|
* be used.
|
276 |
|
|
*
|
277 |
|
|
* @param random The source of randomness.
|
278 |
|
|
*/
|
279 |
|
|
public final void init(SecureRandom random)
|
280 |
|
|
{
|
281 |
|
|
kgSpi.engineInit(random);
|
282 |
|
|
}
|
283 |
|
|
}
|