// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.params;

import org.bouncycastle.math.Primes;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.util.Properties;
import java.math.BigInteger;
import org.bouncycastle.util.BigIntegers;

public class RSAKeyParameters extends AsymmetricKeyParameter
{
    private static final BigIntegers.Cache validated;
    private static final BigInteger SMALL_PRIMES_PRODUCT;
    private BigInteger modulus;
    private BigInteger exponent;
    
    public RSAKeyParameters(final boolean b, final BigInteger bigInteger, final BigInteger bigInteger2) {
        this(b, bigInteger, bigInteger2, false);
    }
    
    public RSAKeyParameters(final boolean b, final BigInteger bigInteger, final BigInteger exponent, final boolean b2) {
        super(b);
        if (!b && (exponent.intValue() & 0x1) == 0x0) {
            throw new IllegalArgumentException("RSA publicExponent is even");
        }
        this.modulus = (RSAKeyParameters.validated.contains(bigInteger) ? bigInteger : validate(bigInteger, b2));
        this.exponent = exponent;
    }
    
    private static boolean hasAnySmallFactors(final BigInteger bigInteger) {
        BigInteger small_PRIMES_PRODUCT = bigInteger;
        BigInteger small_PRIMES_PRODUCT2 = RSAKeyParameters.SMALL_PRIMES_PRODUCT;
        if (bigInteger.bitLength() < RSAKeyParameters.SMALL_PRIMES_PRODUCT.bitLength()) {
            small_PRIMES_PRODUCT = RSAKeyParameters.SMALL_PRIMES_PRODUCT;
            small_PRIMES_PRODUCT2 = bigInteger;
        }
        return !BigIntegers.modOddIsCoprimeVar(small_PRIMES_PRODUCT, small_PRIMES_PRODUCT2);
    }
    
    private static BigInteger validate(final BigInteger bigInteger, final boolean b) {
        if (b) {
            RSAKeyParameters.validated.add(bigInteger);
            return bigInteger;
        }
        if ((bigInteger.intValue() & 0x1) == 0x0) {
            throw new IllegalArgumentException("RSA modulus is even");
        }
        if (Properties.isOverrideSet("org.bouncycastle.rsa.allow_unsafe_mod")) {
            return bigInteger;
        }
        if (Properties.asInteger("org.bouncycastle.rsa.max_size", 16384) < bigInteger.bitLength()) {
            throw new IllegalArgumentException("RSA modulus out of range");
        }
        if (hasAnySmallFactors(bigInteger)) {
            throw new IllegalArgumentException("RSA modulus has a small prime factor");
        }
        final int integer = Properties.asInteger("org.bouncycastle.rsa.max_mr_tests", getMRIterations(bigInteger.bitLength() / 2));
        if (integer > 0 && !Primes.enhancedMRProbablePrimeTest(bigInteger, CryptoServicesRegistrar.getSecureRandom(), integer).isProvablyComposite()) {
            throw new IllegalArgumentException("RSA modulus is not composite");
        }
        RSAKeyParameters.validated.add(bigInteger);
        return bigInteger;
    }
    
    private static int getMRIterations(final int n) {
        return (n >= 1536) ? 3 : ((n >= 1024) ? 4 : ((n >= 512) ? 7 : 50));
    }
    
    public BigInteger getModulus() {
        return this.modulus;
    }
    
    public BigInteger getExponent() {
        return this.exponent;
    }
    
    static {
        validated = new BigIntegers.Cache();
        SMALL_PRIMES_PRODUCT = new BigInteger("8138e8a0fcf3a4e84a771d40fd305d7f4aa59306d7251de54d98af8fe95729a1f73d893fa424cd2edc8636a6c3285e022b0e3866a565ae8108eed8591cd4fe8d2ce86165a978d719ebf647f362d33fca29cd179fb42401cbaf3df0c614056f9c8f3cfd51e474afb6bc6974f78db8aba8e9e517fded658591ab7502bd41849462f", 16);
    }
}
