summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java
diff options
context:
space:
mode:
Diffstat (limited to 'pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java')
-rwxr-xr-xpki/base/util/src/netscape/security/provider/DSAParameterGenerator.java281
1 files changed, 140 insertions, 141 deletions
diff --git a/pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java b/pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java
index cec2b97b..cd7b8de3 100755
--- a/pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java
+++ b/pki/base/util/src/netscape/security/provider/DSAParameterGenerator.java
@@ -46,7 +46,7 @@ import java.security.spec.InvalidParameterSpecException;
*/
public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
-
+
// the modulus length
private int modLen = 1024; // default
@@ -62,83 +62,82 @@ public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
private SHA sha;
public DSAParameterGenerator() {
- this.sha = new SHA();
+ this.sha = new SHA();
}
/**
* Initializes this parameter generator for a certain strength
* and source of randomness.
- *
+ *
* @param strength the strength (size of prime) in bits
* @param random the source of randomness
*/
protected void engineInit(int strength, SecureRandom random) {
- /*
- * Bruce Schneier, "Applied Cryptography", 2nd Edition,
- * Description of DSA:
- * [...] The algorithm uses the following parameter:
- * p=a prime number L bits long, when L ranges from 512 to 1024 and is
- * a multiple of 64. [...]
- */
- if ((strength < 512) || (strength > 1024) || (strength % 64 != 0)) {
- throw new InvalidParameterException
- ("Prime size must range from 512 to 1024 "
- + "and be a multiple of 64");
- }
- this.modLen = strength;
- this.random = random;
+ /*
+ * Bruce Schneier, "Applied Cryptography", 2nd Edition,
+ * Description of DSA:
+ * [...] The algorithm uses the following parameter:
+ * p=a prime number L bits long, when L ranges from 512 to 1024 and is
+ * a multiple of 64. [...]
+ */
+ if ((strength < 512) || (strength > 1024) || (strength % 64 != 0)) {
+ throw new InvalidParameterException("Prime size must range from 512 to 1024 "
+ + "and be a multiple of 64");
+ }
+ this.modLen = strength;
+ this.random = random;
}
/**
* Initializes this parameter generator with a set of
* algorithm-specific parameter generation values.
- *
+ *
* @param params the set of algorithm-specific parameter generation values
* @param random the source of randomness
- *
+ *
* @exception InvalidAlgorithmParameterException if the given parameter
- * generation values are inappropriate for this parameter generator
+ * generation values are inappropriate for this parameter generator
*/
protected void engineInit(AlgorithmParameterSpec genParamSpec,
- SecureRandom random)
- throws InvalidAlgorithmParameterException {
- throw new InvalidAlgorithmParameterException("Invalid parameter");
+ SecureRandom random)
+ throws InvalidAlgorithmParameterException {
+ throw new InvalidAlgorithmParameterException("Invalid parameter");
}
/**
* Generates the parameters.
- *
+ *
* @return the new AlgorithmParameters object
*/
protected AlgorithmParameters engineGenerateParameters() {
- AlgorithmParameters algParams = null;
- try {
- if (this.random == null) {
- this.random = new SecureRandom();
- }
-
- BigInteger[] pAndQ = generatePandQ(this.random, this.modLen);
- BigInteger paramP = pAndQ[0];
- BigInteger paramQ = pAndQ[1];
- BigInteger paramG = generateG(paramP, paramQ);
-
- DSAParameterSpec dsaParamSpec = new DSAParameterSpec(paramP,
- paramQ,
- paramG);
- algParams = AlgorithmParameters.getInstance("DSA", "SUN");
- algParams.init(dsaParamSpec);
- } catch (InvalidParameterSpecException e) {
- // this should never happen
- throw new RuntimeException(e.getMessage());
- } catch (NoSuchAlgorithmException e) {
- // this should never happen, because we provide it
- throw new RuntimeException(e.getMessage());
- } catch (NoSuchProviderException e) {
- // this should never happen, because we provide it
- throw new RuntimeException(e.getMessage());
- }
-
- return algParams;
+ AlgorithmParameters algParams = null;
+ try {
+ if (this.random == null) {
+ this.random = new SecureRandom();
+ }
+
+ BigInteger[] pAndQ = generatePandQ(this.random, this.modLen);
+ BigInteger paramP = pAndQ[0];
+ BigInteger paramQ = pAndQ[1];
+ BigInteger paramG = generateG(paramP, paramQ);
+
+ DSAParameterSpec dsaParamSpec = new DSAParameterSpec(paramP,
+ paramQ,
+ paramG);
+ algParams = AlgorithmParameters.getInstance("DSA", "SUN");
+ algParams.init(dsaParamSpec);
+ } catch (InvalidParameterSpecException e) {
+ // this should never happen
+ throw new RuntimeException(e.getMessage());
+ } catch (NoSuchAlgorithmException e) {
+ // this should never happen, because we provide it
+ throw new RuntimeException(e.getMessage());
+ } catch (NoSuchProviderException e) {
+ // this should never happen, because we provide it
+ throw new RuntimeException(e.getMessage());
+ }
+
+ return algParams;
}
/*
@@ -155,16 +154,16 @@ public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
* <code>q</code> at index 1.
*/
BigInteger[] generatePandQ(SecureRandom random, int L) {
- BigInteger[] result = null;
- byte[] seed = new byte[20];
-
- while(result == null) {
- for (int i = 0; i < 20; i++) {
- seed[i] = (byte)random.nextInt();
- }
- result = generatePandQ(seed, L);
- }
- return result;
+ BigInteger[] result = null;
+ byte[] seed = new byte[20];
+
+ while (result == null) {
+ for (int i = 0; i < 20; i++) {
+ seed[i] = (byte) random.nextInt();
+ }
+ result = generatePandQ(seed, L);
+ }
+ return result;
}
/*
@@ -183,68 +182,68 @@ public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
*/
BigInteger[] generatePandQ(byte[] seed, int L) {
- /* Useful variables */
- int g = seed.length * 8;
- int n = (L - 1) / 160;
- int b = (L - 1) % 160;
+ /* Useful variables */
+ int g = seed.length * 8;
+ int n = (L - 1) / 160;
+ int b = (L - 1) % 160;
- BigInteger SEED = new BigInteger(1, seed);
- BigInteger TWOG = TWO.pow(2 * g);
+ BigInteger SEED = new BigInteger(1, seed);
+ BigInteger TWOG = TWO.pow(2 * g);
- /* Step 2 (Step 1 is getting seed). */
- byte[] U1 = SHA(seed);
+ /* Step 2 (Step 1 is getting seed). */
+ byte[] U1 = SHA(seed);
byte[] U2 = SHA(toByteArray((SEED.add(ONE)).mod(TWOG)));
- xor(U1, U2);
- byte[] U = U1;
-
- /* Step 3: For q by setting the msb and lsb to 1 */
- U[0] |= 0x80;
- U[19] |= 1;
- BigInteger q = new BigInteger(1, U);
-
- /* Step 5 */
- if (!q.isProbablePrime(40)) {
- return null;
-
- } else {
- BigInteger V[] = new BigInteger[n + 1];
- BigInteger offset = TWO;
-
- /* Step 6 */
- for (int counter = 0; counter < 4096; counter++) {
-
- /* Step 7 */
- for (int k = 0; k <= n; k++) {
- BigInteger K = BigInteger.valueOf(k);
- BigInteger tmp = (SEED.add(offset).add(K)).mod(TWOG);
- V[k] = new BigInteger(1, SHA(toByteArray(tmp)));
- }
-
- /* Step 8 */
- BigInteger W = V[0];
- for (int i = 1; i < n; i++) {
- W = W.add(V[i].multiply(TWO.pow(i * 160)));
- }
- W = W.add((V[n].mod(TWO.pow(b))).multiply(TWO.pow(n * 160)));
-
- BigInteger TWOLm1 = TWO.pow(L - 1);
- BigInteger X = W.add(TWOLm1);
-
- /* Step 9 */
- BigInteger c = X.mod(q.multiply(TWO));
- BigInteger p = X.subtract(c.subtract(ONE));
-
- /* Step 10 - 13 */
- if (p.compareTo(TWOLm1) > -1 && p.isProbablePrime(15)) {
- BigInteger[] result = {p, q, SEED,
- BigInteger.valueOf(counter)};
- return result;
- }
- offset = offset.add(BigInteger.valueOf(n)).add(ONE);
- }
- return null;
- }
+ xor(U1, U2);
+ byte[] U = U1;
+
+ /* Step 3: For q by setting the msb and lsb to 1 */
+ U[0] |= 0x80;
+ U[19] |= 1;
+ BigInteger q = new BigInteger(1, U);
+
+ /* Step 5 */
+ if (!q.isProbablePrime(40)) {
+ return null;
+
+ } else {
+ BigInteger V[] = new BigInteger[n + 1];
+ BigInteger offset = TWO;
+
+ /* Step 6 */
+ for (int counter = 0; counter < 4096; counter++) {
+
+ /* Step 7 */
+ for (int k = 0; k <= n; k++) {
+ BigInteger K = BigInteger.valueOf(k);
+ BigInteger tmp = (SEED.add(offset).add(K)).mod(TWOG);
+ V[k] = new BigInteger(1, SHA(toByteArray(tmp)));
+ }
+
+ /* Step 8 */
+ BigInteger W = V[0];
+ for (int i = 1; i < n; i++) {
+ W = W.add(V[i].multiply(TWO.pow(i * 160)));
+ }
+ W = W.add((V[n].mod(TWO.pow(b))).multiply(TWO.pow(n * 160)));
+
+ BigInteger TWOLm1 = TWO.pow(L - 1);
+ BigInteger X = W.add(TWOLm1);
+
+ /* Step 9 */
+ BigInteger c = X.mod(q.multiply(TWO));
+ BigInteger p = X.subtract(c.subtract(ONE));
+
+ /* Step 10 - 13 */
+ if (p.compareTo(TWOLm1) > -1 && p.isProbablePrime(15)) {
+ BigInteger[] result = { p, q, SEED,
+ BigInteger.valueOf(counter) };
+ return result;
+ }
+ offset = offset.add(BigInteger.valueOf(n)).add(ONE);
+ }
+ return null;
+ }
}
/*
@@ -256,23 +255,23 @@ public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
* @param the <code>g</code>
*/
BigInteger generateG(BigInteger p, BigInteger q) {
- BigInteger h = ONE;
- BigInteger pMinusOneOverQ = (p.subtract(ONE)).divide(q);
- BigInteger g = ONE;
- while (g.compareTo(TWO) < 0) {
- g = h.modPow(pMinusOneOverQ, p);
- h = h.add(ONE);
- }
- return g;
+ BigInteger h = ONE;
+ BigInteger pMinusOneOverQ = (p.subtract(ONE)).divide(q);
+ BigInteger g = ONE;
+ while (g.compareTo(TWO) < 0) {
+ g = h.modPow(pMinusOneOverQ, p);
+ h = h.add(ONE);
+ }
+ return g;
}
/*
* Returns the SHA-1 digest of some data
*/
private byte[] SHA(byte[] array) {
- sha.engineReset();
- sha.engineUpdate(array, 0, array.length);
- return sha.engineDigest();
+ sha.engineReset();
+ sha.engineUpdate(array, 0, array.length);
+ return sha.engineDigest();
}
/*
@@ -280,21 +279,21 @@ public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
* signed magnitude representation for any positive number.
*/
private byte[] toByteArray(BigInteger bigInt) {
- byte[] result = bigInt.toByteArray();
- if (result[0] == 0) {
- byte[] tmp = new byte[result.length - 1];
- System.arraycopy(result, 1, tmp, 0, tmp.length);
- result = tmp;
- }
- return result;
+ byte[] result = bigInt.toByteArray();
+ if (result[0] == 0) {
+ byte[] tmp = new byte[result.length - 1];
+ System.arraycopy(result, 1, tmp, 0, tmp.length);
+ result = tmp;
+ }
+ return result;
}
/*
* XORs U2 into U1
*/
private void xor(byte[] U1, byte[] U2) {
- for (int i = 0; i < U1.length; i++) {
- U1[i] ^= U2[i];
- }
+ for (int i = 0; i < U1.length; i++) {
+ U1[i] ^= U2[i];
+ }
}
}