]> WPIA git - gigi.git/blob - src/club/wpia/gigi/crypto/key/KeyCheckSmallFactors.java
Merge "upd: remove 'browser install'"
[gigi.git] / src / club / wpia / gigi / crypto / key / KeyCheckSmallFactors.java
1 package club.wpia.gigi.crypto.key;
2
3 import java.math.BigInteger;
4 import java.security.PublicKey;
5 import java.security.interfaces.RSAPublicKey;
6
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.output.template.SprintfCommand;
9
10 public class KeyCheckSmallFactors extends KeyCheck {
11
12     private static final long MAX_CHECKED_SMALL_PRIME_BOUNDARY = 10000;
13
14     private static final BigInteger primeProduct;
15
16     static {
17         BigInteger prod = BigInteger.ONE;
18
19         NextPrime:
20         for (long i = 2; i < MAX_CHECKED_SMALL_PRIME_BOUNDARY; i++) {
21             if ( !BigInteger.ONE.equals(BigInteger.valueOf(i).gcd(prod))) {
22                 continue NextPrime;
23             }
24
25             prod = prod.multiply(BigInteger.valueOf(i));
26         }
27
28         primeProduct = prod;
29
30         register(new KeyCheckSmallFactors());
31     }
32
33     @Override
34     public void check(PublicKey key) throws GigiApiException {
35         if ( !(key instanceof RSAPublicKey)) {
36             return;
37         }
38
39         BigInteger modulus = ((RSAPublicKey) key).getModulus();
40
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()));
45         }
46     }
47
48 }