1 package club.wpia.gigi.crypto.key;
3 import java.math.BigInteger;
4 import java.security.PublicKey;
5 import java.security.interfaces.RSAPublicKey;
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.output.template.SprintfCommand;
10 public class KeyCheckSmallFactors extends KeyCheck {
12 private static final long MAX_CHECKED_SMALL_PRIME_BOUNDARY = 10000;
14 private static final BigInteger primeProduct;
17 BigInteger prod = BigInteger.ONE;
20 for (long i = 2; i < MAX_CHECKED_SMALL_PRIME_BOUNDARY; i++) {
21 if ( !BigInteger.ONE.equals(BigInteger.valueOf(i).gcd(prod))) {
25 prod = prod.multiply(BigInteger.valueOf(i));
30 register(new KeyCheckSmallFactors());
34 public void check(PublicKey key) throws GigiApiException {
35 if ( !(key instanceof RSAPublicKey)) {
39 BigInteger modulus = ((RSAPublicKey) key).getModulus();
41 // Check for small prime factors below 10000
42 BigInteger n = modulus.gcd(primeProduct);
43 if ( !BigInteger.ONE.equals(n)) {
44 throw new GigiApiException(SprintfCommand.createSimple("Small factors check of public key: Key has known factor of {0}.", n.toString()));