Skip to content

Commit

Permalink
BJA-694 cleaned up primality test
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Apr 19, 2018
1 parent 4988382 commit 73780ac
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ public class RSAKeyPairGenerator
private static final BigInteger ONE = BigInteger.valueOf(1);

private RSAKeyGenerationParameters param;
private int iterations;

public void init(KeyGenerationParameters param)
{
this.param = (RSAKeyGenerationParameters)param;
this.iterations = getNumberOfIterations(this.param.getStrength(), this.param.getCertainty());
}

public AsymmetricCipherKeyPair generateKeyPair()
Expand Down Expand Up @@ -159,6 +157,8 @@ public AsymmetricCipherKeyPair generateKeyPair()
*/
protected BigInteger chooseRandomPrime(int bitlength, BigInteger e, BigInteger sqrdBound)
{
int iterations = getNumberOfIterations(bitlength, param.getCertainty());

for (int i = 0; i != 5 * bitlength; i++)
{
BigInteger p = new BigInteger(bitlength, 1, param.getRandom());
Expand All @@ -173,7 +173,7 @@ protected BigInteger chooseRandomPrime(int bitlength, BigInteger e, BigInteger s
continue;
}

if (!isProbablePrime(p))
if (!isProbablePrime(p, iterations))
{
continue;
}
Expand All @@ -189,7 +189,7 @@ protected BigInteger chooseRandomPrime(int bitlength, BigInteger e, BigInteger s
throw new IllegalStateException("unable to generate prime number for RSA key");
}

protected boolean isProbablePrime(BigInteger x)
protected boolean isProbablePrime(BigInteger x, int iterations)
{
/*
* Primes class for FIPS 186-4 C.3 primality checking
Expand Down

0 comments on commit 73780ac

Please sign in to comment.