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