X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2Fcrypto%2Fkey%2FKeyCheckSmallFactors.java;fp=src%2Fclub%2Fwpia%2Fgigi%2Fcrypto%2Fkey%2FKeyCheckSmallFactors.java;h=a0965344fc103674f2e561741a421a623220f785;hb=4b371730607ed659a4a8b40d8a221b5e6ed42746;hp=0000000000000000000000000000000000000000;hpb=376ad64d4bafc4b2db6990604758addf8fabbd3c;p=gigi.git diff --git a/src/club/wpia/gigi/crypto/key/KeyCheckSmallFactors.java b/src/club/wpia/gigi/crypto/key/KeyCheckSmallFactors.java new file mode 100644 index 00000000..a0965344 --- /dev/null +++ b/src/club/wpia/gigi/crypto/key/KeyCheckSmallFactors.java @@ -0,0 +1,52 @@ +package club.wpia.gigi.crypto.key; + +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; + +public class KeyCheckSmallFactors extends KeyCheck { + + private static final long MAX_CHECKED_SMALL_PRIME_BOUNDARY = 10000; + + private static final BigInteger[] primes; + + static { + ArrayList prims = new ArrayList<>(1024); + + 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; + } + } + + prims.add(BigInteger.valueOf(i)); + } + + primes = prims.toArray(new BigInteger[0]); + + register(new KeyCheckSmallFactors()); + } + + @Override + public void check(PublicKey key) throws GigiApiException { + if ( !(key instanceof RSAPublicKey)) { + return; + } + + 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())); + } + } + } + +}