1. /*
  2. * @(#)AlgorithmParameterGenerator.java 1.39 03/01/23
  3. *
  4. * Copyright 2003 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.39, 01/23/03
  51. *
  52. * @see AlgorithmParameters
  53. * @see java.security.spec.AlgorithmParameterSpec
  54. *
  55. * @since 1.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 paramGenSpi 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. (String)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. * @exception IllegalArgumentException if the provider name is null
  136. * or empty.
  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. * Generates an AlgorithmParameterGenerator object for the requested
  155. * algorithm, as supplied from the specified provider,
  156. * if such a parameter generator is available from the provider.
  157. * Note: the <code>provider</code> doesn't have to be registered.
  158. *
  159. * @param algorithm the string name of the algorithm.
  160. *
  161. * @param provider the provider.
  162. *
  163. * @return the new AlgorithmParameterGenerator object.
  164. *
  165. * @exception NoSuchAlgorithmException if the algorithm is
  166. * not available from the provider.
  167. *
  168. * @exception IllegalArgumentException if the <code>provider</code> is
  169. * null.
  170. *
  171. * @see Provider
  172. *
  173. * @since 1.4
  174. */
  175. public static AlgorithmParameterGenerator getInstance(String algorithm,
  176. Provider provider)
  177. throws NoSuchAlgorithmException
  178. {
  179. if (provider == null)
  180. throw new IllegalArgumentException("missing provider");
  181. Object[] objs = Security.getImpl(algorithm,
  182. "AlgorithmParameterGenerator",
  183. provider);
  184. return new AlgorithmParameterGenerator
  185. ((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],
  186. algorithm);
  187. }
  188. /**
  189. * Returns the provider of this algorithm parameter generator object.
  190. *
  191. * @return the provider of this algorithm parameter generator object
  192. */
  193. public final Provider getProvider() {
  194. return this.provider;
  195. }
  196. /**
  197. * Initializes this parameter generator for a certain size.
  198. * To create the parameters, the <code>SecureRandom</code>
  199. * implementation of the highest-priority installed provider is used as
  200. * the source of randomness.
  201. * (If none of the installed providers supply an implementation of
  202. * <code>SecureRandom</code>, a system-provided source of randomness is
  203. * used.)
  204. *
  205. * @param size the size (number of bits).
  206. */
  207. public final void init(int size) {
  208. paramGenSpi.engineInit(size, new SecureRandom());
  209. }
  210. /**
  211. * Initializes this parameter generator for a certain size and source
  212. * of randomness.
  213. *
  214. * @param size the size (number of bits).
  215. * @param random the source of randomness.
  216. */
  217. public final void init(int size, SecureRandom random) {
  218. paramGenSpi.engineInit(size, random);
  219. }
  220. /**
  221. * Initializes this parameter generator with a set of algorithm-specific
  222. * parameter generation values.
  223. * To generate the parameters, the <code>SecureRandom</code>
  224. * implementation of the highest-priority installed provider is used as
  225. * the source of randomness.
  226. * (If none of the installed providers supply an implementation of
  227. * <code>SecureRandom</code>, a system-provided source of randomness is
  228. * used.)
  229. *
  230. * @param genParamSpec the set of algorithm-specific parameter generation values.
  231. *
  232. * @exception InvalidAlgorithmParameterException if the given parameter
  233. * generation values are inappropriate for this parameter generator.
  234. */
  235. public final void init(AlgorithmParameterSpec genParamSpec)
  236. throws InvalidAlgorithmParameterException {
  237. paramGenSpi.engineInit(genParamSpec, new SecureRandom());
  238. }
  239. /**
  240. * Initializes this parameter generator with a set of algorithm-specific
  241. * parameter generation values.
  242. *
  243. * @param genParamSpec the set of algorithm-specific parameter generation values.
  244. * @param random the source of randomness.
  245. *
  246. * @exception InvalidAlgorithmParameterException if the given parameter
  247. * generation values are inappropriate for this parameter generator.
  248. */
  249. public final void init(AlgorithmParameterSpec genParamSpec,
  250. SecureRandom random)
  251. throws InvalidAlgorithmParameterException {
  252. paramGenSpi.engineInit(genParamSpec, random);
  253. }
  254. /**
  255. * Generates the parameters.
  256. *
  257. * @return the new AlgorithmParameters object.
  258. */
  259. public final AlgorithmParameters generateParameters() {
  260. return paramGenSpi.engineGenerateParameters();
  261. }
  262. }