summaryrefslogtreecommitdiff
path: root/gnu/java/security/key/rsa/RSAKeyPairGenerator.java
diff options
context:
space:
mode:
authorRaif S. Naffah <raif@swiftdsl.com.au>2006-06-20 11:24:41 +0000
committerRaif S. Naffah <raif@swiftdsl.com.au>2006-06-20 11:24:41 +0000
commitc2396fba423be31a4de8db3dbe09d42a04db32dc (patch)
tree09a4cc2b18491b62d6d60663b6b82e1f76856234 /gnu/java/security/key/rsa/RSAKeyPairGenerator.java
parent4d800c50a7257f4febca55b7c94b0901e816916a (diff)
downloadclasspath-c2396fba423be31a4de8db3dbe09d42a04db32dc.tar.gz
2006-06-20 Raif S. Naffah <raif@swiftdsl.com.au>
* gnu/java/security/key/dss/DSSKey.java: Source formatting. * gnu/java/security/key/dss/DSSKeyPairGenerator.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairPKCS8Codec.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairRawCodec.java: Likewise. * gnu/java/security/key/dss/DSSKeyPairX509Codec.java: Likewise. * gnu/java/security/key/dss/DSSPrivateKey.java: Likewise. * gnu/java/security/key/dss/DSSPublicKey.java: Likewise. * gnu/java/security/key/dss/FIPS186.java: Likewise. * gnu/java/security/key/rsa/GnuRSAKey.java: Likewise. * gnu/java/security/key/rsa/GnuRSAPrivateKey.java: Likewise. * gnu/java/security/key/rsa/GnuRSAPublicKey.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairGenerator.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairRawCodec.java: Likewise. * gnu/java/security/key/rsa/RSAKeyPairX509Codec.java: Likewise. * gnu/java/security/key/IKeyPairCodec.java: Likewise. * gnu/java/security/key/IKeyPairGenerator.java: Likewise. * gnu/java/security/key/KeyPairCodecFactory.java: Likewise. * gnu/java/security/key/KeyPairGeneratorFactory.java: Likewise.
Diffstat (limited to 'gnu/java/security/key/rsa/RSAKeyPairGenerator.java')
-rw-r--r--gnu/java/security/key/rsa/RSAKeyPairGenerator.java92
1 files changed, 31 insertions, 61 deletions
diff --git a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java
index d8f575846..5c2db61e2 100644
--- a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java
+++ b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java
@@ -53,25 +53,23 @@ import java.util.Map;
import java.util.logging.Logger;
/**
- * <p>A key-pair generator for asymetric keys to use in conjunction with the RSA
- * scheme.</p>
- *
- * <p>Reference:</p>
+ * A key-pair generator for asymetric keys to use in conjunction with the RSA
+ * scheme.
+ * <p>
+ * Reference:
* <ol>
- * <li><a href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip">
- * RSA-PSS Signature Scheme with Appendix</a>, part B. Primitive
- * specification and supporting documentation. Jakob Jonsson and Burt Kaliski.
- * </li>
- * <li><a href="http://www.cacr.math.uwaterloo.ca/hac/">Handbook of Applied
- * Cryptography</a>, Alfred J. Menezes, Paul C. van Oorschot and Scott A.
- * Vanstone. Section 11.3 RSA and related signature schemes.</li>
+ * <li><a
+ * href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip">
+ * RSA-PSS Signature Scheme with Appendix</a>, part B. Primitive specification
+ * and supporting documentation. Jakob Jonsson and Burt Kaliski. </li>
+ * <li><a href="http://www.cacr.math.uwaterloo.ca/hac/">Handbook of Applied
+ * Cryptography</a>, Alfred J. Menezes, Paul C. van Oorschot and Scott A.
+ * Vanstone. Section 11.3 RSA and related signature schemes.</li>
* </ol>
*/
-public class RSAKeyPairGenerator implements IKeyPairGenerator
+public class RSAKeyPairGenerator
+ implements IKeyPairGenerator
{
- // Constants and variables
- // -------------------------------------------------------------------------
-
private static final Logger log = Logger.getLogger(RSAKeyPairGenerator.class.getName());
/** The BigInteger constant 1. */
@@ -90,8 +88,8 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
public static final String SOURCE_OF_RANDOMNESS = "gnu.crypto.rsa.prng";
/**
- * Property name of an optional {@link RSAKeyGenParameterSpec} instance to
- * use for this generator's <code>n</code>, and <code>e</code> values. The
+ * Property name of an optional {@link RSAKeyGenParameterSpec} instance to use
+ * for this generator's <code>n</code>, and <code>e</code> values. The
* default is to generate <code>n</code> and use a fixed value for
* <code>e</.code> (Fermat's F4 number).
*/
@@ -128,27 +126,19 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
/** Preferred encoding format of generated keys. */
private int preferredFormat;
- // Constructor(s)
- // -------------------------------------------------------------------------
-
// implicit 0-arguments constructor
- // Class methods
- // -------------------------------------------------------------------------
-
- // gnu.crypto.key.IKeyPairGenerator interface implementation ---------------
-
public String name()
{
return Registry.RSA_KPG;
}
/**
- * <p>Configures this instance.</p>
- *
+ * Configures this instance.
+ *
* @param attributes the map of name/value pairs to use.
- * @exception IllegalArgumentException if the designated MODULUS_LENGTH
- * value is less than 1024.
+ * @exception IllegalArgumentException if the designated MODULUS_LENGTH value
+ * is less than 1024.
*/
public void setup(Map attributes)
{
@@ -156,10 +146,8 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
log.entering(this.getClass().getName(), "setup", attributes);
// do we have a SecureRandom, or should we use our own?
rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
-
// are we given a set of RSA params or we shall use our own?
RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) attributes.get(RSA_PARAMETERS);
-
// find out the modulus length
if (params != null)
{
@@ -171,11 +159,8 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
Integer l = (Integer) attributes.get(MODULUS_LENGTH);
L = (l == null ? DEFAULT_MODULUS_LENGTH : l.intValue());
}
-
if (L < 1024)
- {
- throw new IllegalArgumentException(MODULUS_LENGTH);
- }
+ throw new IllegalArgumentException(MODULUS_LENGTH);
// what is the preferred encoding format
Integer formatID = (Integer) attributes.get(PREFERRED_ENCODING_FORMAT);
@@ -186,18 +171,18 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
}
/**
- * <p>The algorithm used here is described in <i>nessie-pss-B.pdf</i>
- * document which is part of the RSA-PSS submission to NESSIE.</p>
- *
+ * <p>
+ * The algorithm used here is described in <i>nessie-pss-B.pdf</i> document
+ * which is part of the RSA-PSS submission to NESSIE.
+ * </p>
+ *
* @return an RSA keypair.
*/
public KeyPair generate()
{
if (Configuration.DEBUG)
log.entering(this.getClass().getName(), "generate");
-
BigInteger p, q, n, d;
-
// 1. Generate a prime p in the interval [2**(M-1), 2**M - 1], where
// M = CEILING(L/2), and such that GCD(p, e) = 1
int M = (L + 1) / 2;
@@ -210,11 +195,8 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
p = new BigInteger(1, kb).setBit(0);
if (p.compareTo(lower) >= 0 && p.compareTo(upper) <= 0
&& p.isProbablePrime(80) && p.gcd(e).equals(ONE))
- {
- break step1;
- }
+ break step1;
}
-
// 2. Generate a prime q such that the product of p and q is an L-bit
// number, and such that GCD(q, e) = 1
step2: while (true)
@@ -222,46 +204,34 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator
nextRandomBytes(kb);
q = new BigInteger(1, kb).setBit(0);
n = p.multiply(q);
- if (n.bitLength() == L && q.isProbablePrime(80)
- && q.gcd(e).equals(ONE))
- {
- break step2;
- }
-
+ if (n.bitLength() == L && q.isProbablePrime(80) && q.gcd(e).equals(ONE))
+ break step2;
// TODO: test for p != q
}
-
// TODO: ensure p < q
-
// 3. Put n = pq. The public key is (n, e).
// 4. Compute the parameters necessary for the private key K (see
// Section 2.2).
BigInteger phi = p.subtract(ONE).multiply(q.subtract(ONE));
d = e.modInverse(phi);
-
// 5. Output the public key and the private key.
PublicKey pubK = new GnuRSAPublicKey(preferredFormat, n, e);
PrivateKey secK = new GnuRSAPrivateKey(preferredFormat, p, q, e, d);
-
KeyPair result = new KeyPair(pubK, secK);
if (Configuration.DEBUG)
log.exiting(this.getClass().getName(), "generate", result);
return result;
}
- // helper methods ----------------------------------------------------------
-
/**
- * <p>Fills the designated byte array with random data.</p>
- *
+ * Fills the designated byte array with random data.
+ *
* @param buffer the byte array to fill with random data.
*/
private void nextRandomBytes(byte[] buffer)
{
if (rnd != null)
- {
- rnd.nextBytes(buffer);
- }
+ rnd.nextBytes(buffer);
else
getDefaultPRNG().nextBytes(buffer);
}