1   /* $RCSfile: RSAKeyPairGeneratorEngine.java,v $
2    * $Revision: 1.10 $
3    * $Date: 2003/10/04 19:18:38 $
4    * $Author: uwe_guenther $
5    * $State: Exp $
6    *
7    * Created on November 8, 2001 11:25 AM
8    *
9    * Copyright (C) 2001  Uwe Guenther  <uwe@cscc.de >
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.provider;
33  
34  import java.security.InvalidAlgorithmParameterException;
35  import java.security.KeyPair;
36  import java.security.KeyPairGeneratorSpi;
37  import java.security.SecureRandom;
38  import java.security.spec.AlgorithmParameterSpec;
39  
40  /** 
41   * RSAKeyPairGeneratorEngine Class.
42   *
43   * <p>Defaults are:
44   * <ol>
45   * <li>public Exponent = F4 (Forth Fermat number (2^2^4)+1 = 65537 or 
46   *     0x00010001)
47   * <li><code>keysize</code> (modulus length) = 768 bit
48   *     <br><b>Note:</b> The keysize must be a positive multiple of 
49   *     256 and equals or greater than 768.</li>
50   * <li><code>random = new java.security.SecureRandom()</code></li>
51   * </ol>
52   *
53   * @author  <a href=mailto:uwe@cscc.de >Uwe G&uuml;nther </a>
54   *
55   * @version $Revision: 1.10 $
56   */
57  public final class RSAKeyPairGeneratorEngine extends KeyPairGeneratorSpi {
58      
59      /** The delegate for this wrapper object */
60      private RSAKeyPairGeneratorImpl generator = new RSAKeyPairGeneratorImpl();
61      
62      /** Creates new RSAKeyPairGeneratorEngine*/
63      public RSAKeyPairGeneratorEngine() {
64          if (JHBCI.selfIntegrityChecking() == false) {
65              throw new SecurityException("JHBCI-Provider is tampered.");
66          }        
67      }
68  
69      /**
70       * Returns a string representation of the object. 
71       *
72       * @return  a string representation of the object.
73       */
74      public String toString() {
75          return this.generator.toString();
76      }    
77      
78      /**
79       * Initializes the key pair generator for a certain keysize, using
80       * the default parameter set. If random is <code>null</code> we use
81       * the <code>SecureRandom</code> implementation of the
82       * highest-priority installed provider as the source of randomness.
83       * (If none of the installed providers supply an implementation of
84       * <code>SecureRandom</code>, a system-provided source of randomness
85       * is used.) 
86       * <p><b>Note:</b> If there a reinitialization with a <code>null</code>
87       * reference for <code>random</code> we use the existing random object,
88       * that was valid before reinitialization.
89       *
90       * @param keysize the keysize. The keysize must be a positive multiple of 
91       * 256 and equals or greater than 768.
92       * @param random the source of randomness for this generator. If random is 
93       * <code>null</code> we use the the <code>SecureRandom</code> implementation
94       * of the highest-priority installed provider as the source of randomness.
95       * (If none of the installed providers supply an implementation of
96       * <code>SecureRandom</code>, a system-provided source of randomness
97       * is used.)
98       * @throws InvalidParameterException if the <code>keysize</code> is not
99       * equals or geater than 768 and a multible of 256. Where the unit is bit.
100      */
101     public void initialize(int keysize, SecureRandom random) {
102         this.generator.initialize(keysize, random);
103     }
104     
105     /**
106      * Initializes the key pair generator using the specified parameter
107      * set and user-provided source of randomness.
108      *
109      * You have to use {@link java.security.spec.RSAKeyGenParameterSpec} as 
110      * <code>AlgorithmParamterSpec</code>. <code>RSAKeyGenParameterSpec</code>
111      * does support <code>int keysize</code> and 
112      * <code>BigInteger publicExponent</code>. The <code>keysize</code> must be 
113      * 768 or greater and a multible of 256. 
114      *
115      * <p>So that <code>keysize</code> satisfies the equation: 
116      * <ul>
117      * <li><code>keysize = x * 256, where x = 3, 4, 5, 6, ...</code></li>
118      * </ul>
119      * The possible values for <code>publicExponent</code> are:
120      * <ul>
121      * <li><code>publicExponent = </code>
122      * {@link java.security.spec.RSAKeyGenParameterSpec#F0} = 3</li>
123      * <li><code>publicExponent = </code>
124      * {@link java.security.spec.RSAKeyGenParameterSpec#F4} = 65537</li>
125      * </ul>
126      *
127      * <p><b>Note:</b> If there a reinitialization with a <code>null</code>
128      * reference for <code>random</code> we use the existing random object,
129      * that was valid before reinitialization.
130      *
131      * @param params the parameter set used to generate the keys.
132      * @param random the source of randomness for this generator.
133      * @throws InvalidAlgorithmParameterException if the <code>keysize</code>
134      * is not equals or geater than 768 and a multible of 256. Where the unit
135      * is bit. Or if the <code>publicExponent</code> isn't F0 or F4.
136      * @see java.security.spec.RSAKeyGenParameterSpec
137      */
138     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
139             throws InvalidAlgorithmParameterException {
140         this.generator.initialize(params, random);
141     } 
142     
143     /**
144      * Generates a <code>KeyPair</code>. Unless an initialization method is
145      * called using a KeyPairGenerator interface, algorithm-specific defaults
146      * will be used. This will generate a new key pair every time it
147      * is called. The defaults are <code>keysize</code> = 768 bit and
148      * random with the <code>SecureRandom</code> implementation of the
149      * highest-priority installed provider as the source of randomness.
150      * (If none of the installed providers supply an implementation of
151      * <code>SecureRandom</code>, a system-provided source of randomness
152      * is used.)
153      *
154      * @return the newly generated <code>KeyPair</code>
155      */
156     public KeyPair generateKeyPair() {    
157         return this.generator.generateKeyPair();
158     }    
159 }
160