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

package org.bouncycastle.crypto.engines;

import org.bouncycastle.crypto.CryptoServicePurpose;
import org.bouncycastle.util.Properties;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.DataLengthException;
import java.math.BigInteger;
import org.bouncycastle.crypto.CryptoServiceProperties;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
import org.bouncycastle.crypto.constraints.ConstraintUtils;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;

class RSACoreEngine
{
    static final String NO_LENSTRA_CHECK = "org.bouncycastle.rsa.no_lenstra_check";
    private RSAKeyParameters key;
    private boolean forEncryption;
    
    public void init(final boolean forEncryption, CipherParameters parameters) {
        if (parameters instanceof ParametersWithRandom) {
            parameters = ((ParametersWithRandom)parameters).getParameters();
        }
        this.forEncryption = forEncryption;
        this.key = (RSAKeyParameters)parameters;
        CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties("RSA", ConstraintUtils.bitsOfSecurityFor(this.key.getModulus()), this.key, this.getPurpose(this.key.isPrivate(), forEncryption)));
    }
    
    public int getInputBlockSize() {
        final int bitLength = this.key.getModulus().bitLength();
        if (this.forEncryption) {
            return (bitLength + 7) / 8 - 1;
        }
        return (bitLength + 7) / 8;
    }
    
    public int getOutputBlockSize() {
        final int bitLength = this.key.getModulus().bitLength();
        if (this.forEncryption) {
            return (bitLength + 7) / 8;
        }
        return (bitLength + 7) / 8 - 1;
    }
    
    public BigInteger convertInput(final byte[] array, final int n, final int n2) {
        if (n2 > this.getInputBlockSize() + 1) {
            throw new DataLengthException("input too large for RSA cipher.");
        }
        if (n2 == this.getInputBlockSize() + 1 && !this.forEncryption) {
            throw new DataLengthException("input too large for RSA cipher.");
        }
        byte[] magnitude;
        if (n != 0 || n2 != array.length) {
            magnitude = new byte[n2];
            System.arraycopy(array, n, magnitude, 0, n2);
        }
        else {
            magnitude = array;
        }
        final BigInteger bigInteger = new BigInteger(1, magnitude);
        if (bigInteger.compareTo(this.key.getModulus()) >= 0) {
            throw new DataLengthException("input too large for RSA cipher.");
        }
        return bigInteger;
    }
    
    public byte[] convertOutput(final BigInteger bigInteger) {
        final byte[] byteArray = bigInteger.toByteArray();
        if (!this.forEncryption) {
            byte[] array;
            if (byteArray[0] == 0) {
                array = new byte[byteArray.length - 1];
                System.arraycopy(byteArray, 1, array, 0, array.length);
            }
            else {
                array = new byte[byteArray.length];
                System.arraycopy(byteArray, 0, array, 0, array.length);
            }
            Arrays.fill(byteArray, (byte)0);
            return array;
        }
        if (byteArray[0] == 0 && byteArray.length > this.getOutputBlockSize()) {
            final byte[] array2 = new byte[byteArray.length - 1];
            System.arraycopy(byteArray, 1, array2, 0, array2.length);
            return array2;
        }
        if (byteArray.length < this.getOutputBlockSize()) {
            final byte[] array3 = new byte[this.getOutputBlockSize()];
            System.arraycopy(byteArray, 0, array3, array3.length - byteArray.length, byteArray.length);
            return array3;
        }
        return byteArray;
    }
    
    public BigInteger processBlock(final BigInteger x) {
        if (this.key instanceof RSAPrivateCrtKeyParameters) {
            final RSAPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = (RSAPrivateCrtKeyParameters)this.key;
            final BigInteger publicExponent = rsaPrivateCrtKeyParameters.getPublicExponent();
            if (publicExponent != null || Properties.isOverrideSet("org.bouncycastle.rsa.no_lenstra_check")) {
                final BigInteger p = rsaPrivateCrtKeyParameters.getP();
                final BigInteger q = rsaPrivateCrtKeyParameters.getQ();
                final BigInteger dp = rsaPrivateCrtKeyParameters.getDP();
                final BigInteger dq = rsaPrivateCrtKeyParameters.getDQ();
                final BigInteger qInv = rsaPrivateCrtKeyParameters.getQInv();
                final BigInteger modPow = x.remainder(p).modPow(dp, p);
                final BigInteger modPow2 = x.remainder(q).modPow(dq, q);
                final BigInteger add = modPow.subtract(modPow2).multiply(qInv).mod(p).multiply(q).add(modPow2);
                if (publicExponent != null && !add.modPow(publicExponent, rsaPrivateCrtKeyParameters.getModulus()).equals(x)) {
                    throw new IllegalStateException("RSA engine faulty decryption/signing detected");
                }
                return add;
            }
        }
        return x.modPow(this.key.getExponent(), this.key.getModulus());
    }
    
    private CryptoServicePurpose getPurpose(final boolean b, final boolean b2) {
        final boolean b3 = b && b2;
        final boolean b4 = !b && b2;
        final boolean b5 = !b && !b2;
        if (b3) {
            return CryptoServicePurpose.SIGNING;
        }
        if (b4) {
            return CryptoServicePurpose.ENCRYPTION;
        }
        if (b5) {
            return CryptoServicePurpose.VERIFYING;
        }
        return CryptoServicePurpose.DECRYPTION;
    }
}
