Change-Id: Iae10d67814bed36a8864cccf4d7e33ad3dbefeab
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
-import java.util.ArrayList;
import club.wpia.gigi.GigiApiException;
import club.wpia.gigi.output.template.SprintfCommand;
import club.wpia.gigi.GigiApiException;
import club.wpia.gigi.output.template.SprintfCommand;
private static final long MAX_CHECKED_SMALL_PRIME_BOUNDARY = 10000;
private static final long MAX_CHECKED_SMALL_PRIME_BOUNDARY = 10000;
- private static final BigInteger[] primes;
+ private static final BigInteger primeProduct;
- ArrayList<BigInteger> prims = new ArrayList<>(1024);
+ BigInteger prod = BigInteger.ONE;
NextPrime:
for (long i = 2; i < MAX_CHECKED_SMALL_PRIME_BOUNDARY; i++) {
NextPrime:
for (long i = 2; i < MAX_CHECKED_SMALL_PRIME_BOUNDARY; i++) {
- for (BigInteger p : prims) {
- if (BigInteger.ZERO.equals(BigInteger.valueOf(i).mod(p))) {
- continue NextPrime;
- }
+ if ( !BigInteger.ONE.equals(BigInteger.valueOf(i).gcd(prod))) {
+ continue NextPrime;
- prims.add(BigInteger.valueOf(i));
+ prod = prod.multiply(BigInteger.valueOf(i));
- primes = prims.toArray(new BigInteger[0]);
register(new KeyCheckSmallFactors());
}
register(new KeyCheckSmallFactors());
}
BigInteger modulus = ((RSAPublicKey) key).getModulus();
// Check for small prime factors below 10000
BigInteger modulus = ((RSAPublicKey) key).getModulus();
// Check for small prime factors below 10000
- for (BigInteger n : primes) {
- if (BigInteger.ZERO.equals(modulus.mod(n))) {
- throw new GigiApiException(SprintfCommand.createSimple("Small factors check of public key: Key is divisible by {0}.", n.toString()));
- }
+ BigInteger n = modulus.gcd(primeProduct);
+ if ( !BigInteger.ONE.equals(n)) {
+ throw new GigiApiException(SprintfCommand.createSimple("Small factors check of public key: Key has known factor of {0}.", n.toString()));