001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security;
027
028 import java.security.spec.AlgorithmParameterSpec;
029
030 /**
031 * The <code>AlgorithmParameterGenerator</code> class is used to generate a
032 * set of
033 * parameters to be used with a certain algorithm. Parameter generators
034 * are constructed using the <code>getInstance</code> factory methods
035 * (static methods that return instances of a given class).
036 *
037 * <P>The object that will generate the parameters can be initialized
038 * in two different ways: in an algorithm-independent manner, or in an
039 * algorithm-specific manner:
040 *
041 * <ul>
042 * <li>The algorithm-independent approach uses the fact that all parameter
043 * generators share the concept of a "size" and a
044 * source of randomness. The measure of size is universally shared
045 * by all algorithm parameters, though it is interpreted differently
046 * for different algorithms. For example, in the case of parameters for
047 * the <i>DSA</i> algorithm, "size" corresponds to the size
048 * of the prime modulus (in bits).
049 * When using this approach, algorithm-specific parameter generation
050 * values - if any - default to some standard values, unless they can be
051 * derived from the specified size.<P>
052 *
053 * <li>The other approach initializes a parameter generator object
054 * using algorithm-specific semantics, which are represented by a set of
055 * algorithm-specific parameter generation values. To generate
056 * Diffie-Hellman system parameters, for example, the parameter generation
057 * values usually
058 * consist of the size of the prime modulus and the size of the
059 * random exponent, both specified in number of bits.
060 * </ul>
061 *
062 * <P>In case the client does not explicitly initialize the
063 * AlgorithmParameterGenerator
064 * (via a call to an <code>init</code> method), each provider must supply (and
065 * document) a default initialization. For example, the Sun provider uses a
066 * default modulus prime size of 1024 bits for the generation of DSA
067 * parameters.
068 *
069 * @author Jan Luehe
070 *
071 * @version 1.49, 05/05/07
072 *
073 * @see AlgorithmParameters
074 * @see java.security.spec.AlgorithmParameterSpec
075 *
076 * @since 1.2
077 */
078
079 public class AlgorithmParameterGenerator {
080
081 // The provider
082 private Provider provider;
083
084 // The provider implementation (delegate)
085 private AlgorithmParameterGeneratorSpi paramGenSpi;
086
087 // The algorithm
088 private String algorithm;
089
090 /**
091 * Creates an AlgorithmParameterGenerator object.
092 *
093 * @param paramGenSpi the delegate
094 * @param provider the provider
095 * @param algorithm the algorithm
096 */
097 protected AlgorithmParameterGenerator(
098 AlgorithmParameterGeneratorSpi paramGenSpi,
099 Provider provider, String algorithm) {
100 this .paramGenSpi = paramGenSpi;
101 this .provider = provider;
102 this .algorithm = algorithm;
103 }
104
105 /**
106 * Returns the standard name of the algorithm this parameter
107 * generator is associated with.
108 *
109 * @return the string name of the algorithm.
110 */
111 public final String getAlgorithm() {
112 return this .algorithm;
113 }
114
115 /**
116 * Returns an AlgorithmParameterGenerator object for generating
117 * a set of parameters to be used with the specified algorithm.
118 *
119 * <p> This method traverses the list of registered security Providers,
120 * starting with the most preferred Provider.
121 * A new AlgorithmParameterGenerator object encapsulating the
122 * AlgorithmParameterGeneratorSpi implementation from the first
123 * Provider that supports the specified algorithm is returned.
124 *
125 * <p> Note that the list of registered providers may be retrieved via
126 * the {@link Security#getProviders() Security.getProviders()} method.
127 *
128 * @param algorithm the name of the algorithm this
129 * parameter generator is associated with.
130 * See Appendix A in the <a href=
131 * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
132 * Java Cryptography Architecture API Specification & Reference </a>
133 * for information about standard algorithm names.
134 *
135 * @return the new AlgorithmParameterGenerator object.
136 *
137 * @exception NoSuchAlgorithmException if no Provider supports an
138 * AlgorithmParameterGeneratorSpi implementation for the
139 * specified algorithm.
140 *
141 * @see Provider
142 */
143 public static AlgorithmParameterGenerator getInstance(
144 String algorithm) throws NoSuchAlgorithmException {
145 try {
146 Object[] objs = Security.getImpl(algorithm,
147 "AlgorithmParameterGenerator", (String) null);
148 return new AlgorithmParameterGenerator(
149 (AlgorithmParameterGeneratorSpi) objs[0],
150 (Provider) objs[1], algorithm);
151 } catch (NoSuchProviderException e) {
152 throw new NoSuchAlgorithmException(algorithm + " not found");
153 }
154 }
155
156 /**
157 * Returns an AlgorithmParameterGenerator object for generating
158 * a set of parameters to be used with the specified algorithm.
159 *
160 * <p> A new AlgorithmParameterGenerator object encapsulating the
161 * AlgorithmParameterGeneratorSpi implementation from the specified provider
162 * is returned. The specified provider must be registered
163 * in the security provider list.
164 *
165 * <p> Note that the list of registered providers may be retrieved via
166 * the {@link Security#getProviders() Security.getProviders()} method.
167 *
168 * @param algorithm the name of the algorithm this
169 * parameter generator is associated with.
170 * See Appendix A in the <a href=
171 * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
172 * Java Cryptography Architecture API Specification & Reference </a>
173 * for information about standard algorithm names.
174 *
175 * @param provider the string name of the Provider.
176 *
177 * @return the new AlgorithmParameterGenerator object.
178 *
179 * @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi
180 * implementation for the specified algorithm is not
181 * available from the specified provider.
182 *
183 * @exception NoSuchProviderException if the specified provider is not
184 * registered in the security provider list.
185 *
186 * @exception IllegalArgumentException if the provider name is null
187 * or empty.
188 *
189 * @see Provider
190 */
191 public static AlgorithmParameterGenerator getInstance(
192 String algorithm, String provider)
193 throws NoSuchAlgorithmException, NoSuchProviderException {
194 if (provider == null || provider.length() == 0)
195 throw new IllegalArgumentException("missing provider");
196 Object[] objs = Security.getImpl(algorithm,
197 "AlgorithmParameterGenerator", provider);
198 return new AlgorithmParameterGenerator(
199 (AlgorithmParameterGeneratorSpi) objs[0],
200 (Provider) objs[1], algorithm);
201 }
202
203 /**
204 * Returns an AlgorithmParameterGenerator object for generating
205 * a set of parameters to be used with the specified algorithm.
206 *
207 * <p> A new AlgorithmParameterGenerator object encapsulating the
208 * AlgorithmParameterGeneratorSpi implementation from the specified Provider
209 * object is returned. Note that the specified Provider object
210 * does not have to be registered in the provider list.
211 *
212 * @param algorithm the string name of the algorithm this
213 * parameter generator is associated with.
214 * See Appendix A in the <a href=
215 * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
216 * Java Cryptography Architecture API Specification & Reference </a>
217 * for information about standard algorithm names.
218 *
219 * @param provider the Provider object.
220 *
221 * @return the new AlgorithmParameterGenerator object.
222 *
223 * @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi
224 * implementation for the specified algorithm is not available
225 * from the specified Provider object.
226 *
227 * @exception IllegalArgumentException if the specified provider is null.
228 *
229 * @see Provider
230 *
231 * @since 1.4
232 */
233 public static AlgorithmParameterGenerator getInstance(
234 String algorithm, Provider provider)
235 throws NoSuchAlgorithmException {
236 if (provider == null)
237 throw new IllegalArgumentException("missing provider");
238 Object[] objs = Security.getImpl(algorithm,
239 "AlgorithmParameterGenerator", provider);
240 return new AlgorithmParameterGenerator(
241 (AlgorithmParameterGeneratorSpi) objs[0],
242 (Provider) objs[1], algorithm);
243 }
244
245 /**
246 * Returns the provider of this algorithm parameter generator object.
247 *
248 * @return the provider of this algorithm parameter generator object
249 */
250 public final Provider getProvider() {
251 return this .provider;
252 }
253
254 /**
255 * Initializes this parameter generator for a certain size.
256 * To create the parameters, the <code>SecureRandom</code>
257 * implementation of the highest-priority installed provider is used as
258 * the source of randomness.
259 * (If none of the installed providers supply an implementation of
260 * <code>SecureRandom</code>, a system-provided source of randomness is
261 * used.)
262 *
263 * @param size the size (number of bits).
264 */
265 public final void init(int size) {
266 paramGenSpi.engineInit(size, new SecureRandom());
267 }
268
269 /**
270 * Initializes this parameter generator for a certain size and source
271 * of randomness.
272 *
273 * @param size the size (number of bits).
274 * @param random the source of randomness.
275 */
276 public final void init(int size, SecureRandom random) {
277 paramGenSpi.engineInit(size, random);
278 }
279
280 /**
281 * Initializes this parameter generator with a set of algorithm-specific
282 * parameter generation values.
283 * To generate the parameters, the <code>SecureRandom</code>
284 * implementation of the highest-priority installed provider is used as
285 * the source of randomness.
286 * (If none of the installed providers supply an implementation of
287 * <code>SecureRandom</code>, a system-provided source of randomness is
288 * used.)
289 *
290 * @param genParamSpec the set of algorithm-specific parameter generation values.
291 *
292 * @exception InvalidAlgorithmParameterException if the given parameter
293 * generation values are inappropriate for this parameter generator.
294 */
295 public final void init(AlgorithmParameterSpec genParamSpec)
296 throws InvalidAlgorithmParameterException {
297 paramGenSpi.engineInit(genParamSpec, new SecureRandom());
298 }
299
300 /**
301 * Initializes this parameter generator with a set of algorithm-specific
302 * parameter generation values.
303 *
304 * @param genParamSpec the set of algorithm-specific parameter generation values.
305 * @param random the source of randomness.
306 *
307 * @exception InvalidAlgorithmParameterException if the given parameter
308 * generation values are inappropriate for this parameter generator.
309 */
310 public final void init(AlgorithmParameterSpec genParamSpec,
311 SecureRandom random)
312 throws InvalidAlgorithmParameterException {
313 paramGenSpi.engineInit(genParamSpec, random);
314 }
315
316 /**
317 * Generates the parameters.
318 *
319 * @return the new AlgorithmParameters object.
320 */
321 public final AlgorithmParameters generateParameters() {
322 return paramGenSpi.engineGenerateParameters();
323 }
324 }
|