1. /*
  2. * @(#)AlgorithmParameterGenerator.java 1.32 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.security.spec.AlgorithmParameterSpec;
  9. /**
  10. * The <code>AlgorithmParameterGenerator</code> class is used to generate a
  11. * set of
  12. * parameters to be used with a certain algorithm. Parameter generators
  13. * are constructed using the <code>getInstance</code> factory methods
  14. * (static methods that return instances of a given class).
  15. *
  16. * <P>The object that will generate the parameters can be initialized
  17. * in two different ways: in an algorithm-independent manner, or in an
  18. * algorithm-specific manner:
  19. *
  20. * <ul>
  21. * <li>The algorithm-independent approach uses the fact that all parameter
  22. * generators share the concept of a "size" and a
  23. * source of randomness. The measure of size is universally shared
  24. * by all algorithm parameters, though it is interpreted differently
  25. * for different algorithms. For example, in the case of parameters for
  26. * the <i>DSA</i> algorithm, "size" corresponds to the size
  27. * of the prime modulus (in bits).
  28. * When using this approach, algorithm-specific parameter generation
  29. * values - if any - default to some standard values, unless they can be
  30. * derived from the specified size.<P>
  31. *
  32. * <li>The other approach initializes a parameter generator object
  33. * using algorithm-specific semantics, which are represented by a set of
  34. * algorithm-specific parameter generation values. To generate
  35. * Diffie-Hellman system parameters, for example, the parameter generation
  36. * values usually
  37. * consist of the size of the prime modulus and the size of the
  38. * random exponent, both specified in number of bits.
  39. * </ul>
  40. *
  41. * <P>In case the client does not explicitly initialize the
  42. * AlgorithmParameterGenerator
  43. * (via a call to an <code>init</code> method), each provider must supply (and
  44. * document) a default initialization. For example, the Sun provider uses a
  45. * default modulus prime size of 1024 bits for the generation of DSA
  46. * parameters.
  47. *
  48. * @author Jan Luehe
  49. *
  50. * @version 1.32 01/11/29
  51. *
  52. * @see AlgorithmParameters
  53. * @see java.security.spec.AlgorithmParameterSpec
  54. *
  55. * @since JDK1.2
  56. */
  57. public class AlgorithmParameterGenerator {
  58. // The provider
  59. private Provider provider;
  60. // The provider implementation (delegate)
  61. private AlgorithmParameterGeneratorSpi paramGenSpi;
  62. // The algorithm
  63. private String algorithm;
  64. /**
  65. * Creates an AlgorithmParameterGenerator object.
  66. *
  67. * @param keyFacSpi the delegate
  68. * @param provider the provider
  69. * @param algorithm the algorithm
  70. */
  71. protected AlgorithmParameterGenerator
  72. (AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,
  73. String algorithm) {
  74. this.paramGenSpi = paramGenSpi;
  75. this.provider = provider;
  76. this.algorithm = algorithm;
  77. }
  78. /**
  79. * Returns the standard name of the algorithm this parameter
  80. * generator is associated with.
  81. *
  82. * @return the string name of the algorithm.
  83. */
  84. public final String getAlgorithm() {
  85. return this.algorithm;
  86. }
  87. /**
  88. * Generates an AlgorithmParameterGenerator object that implements the
  89. * specified digest algorithm. If the default provider package
  90. * provides an implementation of the requested digest algorithm,
  91. * an instance of AlgorithmParameterGenerator containing that
  92. * implementation
  93. * is returned. If the algorithm is not available in the default
  94. * package, other packages are searched.
  95. *
  96. * @param algorithm the string name of the algorithm this
  97. * parameter generator is associated with.
  98. *
  99. * @return the new AlgorithmParameterGenerator object.
  100. *
  101. * @exception NoSuchAlgorithmException if the algorithm is
  102. * not available in the environment.
  103. */
  104. public static AlgorithmParameterGenerator getInstance(String algorithm)
  105. throws NoSuchAlgorithmException {
  106. try {
  107. Object[] objs = Security.getImpl(algorithm,
  108. "AlgorithmParameterGenerator",
  109. null);
  110. return new AlgorithmParameterGenerator
  111. ((AlgorithmParameterGeneratorSpi)objs[0],
  112. (Provider)objs[1],
  113. algorithm);
  114. } catch(NoSuchProviderException e) {
  115. throw new NoSuchAlgorithmException(algorithm + " not found");
  116. }
  117. }
  118. /**
  119. * Generates an AlgorithmParameterGenerator object for the requested
  120. * algorithm, as supplied from the specified provider,
  121. * if such a parameter generator is available from the provider.
  122. *
  123. * @param algorithm the string name of the algorithm.
  124. *
  125. * @param provider the string name of the provider.
  126. *
  127. * @return the new AlgorithmParameterGenerator object.
  128. *
  129. * @exception NoSuchAlgorithmException if the algorithm is
  130. * not available from the provider.
  131. *
  132. * @exception NoSuchProviderException if the provider is not
  133. * available in the environment.
  134. *
  135. * @see Provider
  136. */
  137. public static AlgorithmParameterGenerator getInstance(String algorithm,
  138. String provider)
  139. throws NoSuchAlgorithmException, NoSuchProviderException
  140. {
  141. if (provider == null || provider.length() == 0)
  142. throw new IllegalArgumentException("missing provider");
  143. Object[] objs = Security.getImpl(algorithm,
  144. "AlgorithmParameterGenerator",
  145. provider);
  146. return new AlgorithmParameterGenerator
  147. ((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],
  148. algorithm);
  149. }
  150. /**
  151. * Returns the provider of this algorithm parameter generator object.
  152. *
  153. * @return the provider of this algorithm parameter generator object
  154. */
  155. public final Provider getProvider() {
  156. return this.provider;
  157. }
  158. /**
  159. * Initializes this parameter generator for a certain size.
  160. * To create the parameters, the <code>SecureRandom</code>
  161. * implementation of the highest-priority installed provider is used as
  162. * the source of randomness.
  163. * (If none of the installed providers supply an implementation of
  164. * <code>SecureRandom</code>, a system-provided source of randomness is
  165. * used.)
  166. *
  167. * @param size the size (number of bits).
  168. */
  169. public final void init(int size) {
  170. paramGenSpi.engineInit(size, new SecureRandom());
  171. }
  172. /**
  173. * Initializes this parameter generator for a certain size and source
  174. * of randomness.
  175. *
  176. * @param size the size (number of bits).
  177. * @param random the source of randomness.
  178. */
  179. public final void init(int size, SecureRandom random) {
  180. paramGenSpi.engineInit(size, random);
  181. }
  182. /**
  183. * Initializes this parameter generator with a set of algorithm-specific
  184. * parameter generation values.
  185. * To generate the parameters, the <code>SecureRandom</code>
  186. * implementation of the highest-priority installed provider is used as
  187. * the source of randomness.
  188. * (If none of the installed providers supply an implementation of
  189. * <code>SecureRandom</code>, a system-provided source of randomness is
  190. * used.)
  191. *
  192. * @param params the set of algorithm-specific parameter generation values.
  193. *
  194. * @exception InvalidAlgorithmParameterException if the given parameter
  195. * generation values are inappropriate for this parameter generator.
  196. */
  197. public final void init(AlgorithmParameterSpec genParamSpec)
  198. throws InvalidAlgorithmParameterException {
  199. paramGenSpi.engineInit(genParamSpec, new SecureRandom());
  200. }
  201. /**
  202. * Initializes this parameter generator with a set of algorithm-specific
  203. * parameter generation values.
  204. *
  205. * @param params the set of algorithm-specific parameter generation values.
  206. * @param random the source of randomness.
  207. *
  208. * @exception InvalidAlgorithmParameterException if the given parameter
  209. * generation values are inappropriate for this parameter generator.
  210. */
  211. public final void init(AlgorithmParameterSpec genParamSpec,
  212. SecureRandom random)
  213. throws InvalidAlgorithmParameterException {
  214. paramGenSpi.engineInit(genParamSpec, random);
  215. }
  216. /**
  217. * Generates the parameters.
  218. *
  219. * @return the new AlgorithmParameters object.
  220. */
  221. public final AlgorithmParameters generateParameters() {
  222. return paramGenSpi.engineGenerateParameters();
  223. }
  224. }