package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
class RSACoreEngine {
    private org.bouncycastle.crypto.params.RSAKeyParameters a;
    private boolean b;

    RSACoreEngine() {
    }

    private org.bouncycastle.crypto.CryptoServicePurpose e(boolean z, boolean z2) {
        boolean z3 = false;
        boolean z4 = z && z2;
        boolean z5 = !z && z2;
        if (!z && !z2) {
            z3 = true;
        }
        if (z4) {
            return org.bouncycastle.crypto.CryptoServicePurpose.SIGNING;
        }
        if (z5) {
            return org.bouncycastle.crypto.CryptoServicePurpose.ENCRYPTION;
        }
        return z3 ? org.bouncycastle.crypto.CryptoServicePurpose.VERIFYING : org.bouncycastle.crypto.CryptoServicePurpose.DECRYPTION;
    }

    public java.math.BigInteger a(byte[] bArr, int i, int i2) {
        if (i2 > c() + 1) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for RSA cipher.");
        }
        if (i2 == c() + 1 && !this.b) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for RSA cipher.");
        }
        if (i != 0 || i2 != bArr.length) {
            byte[] bArr2 = new byte[i2];
            java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
            bArr = bArr2;
        }
        java.math.BigInteger bigInteger = new java.math.BigInteger(1, bArr);
        if (bigInteger.compareTo(this.a.i()) < 0) {
            return bigInteger;
        }
        throw new org.bouncycastle.crypto.DataLengthException("input too large for RSA cipher.");
    }

    public byte[] b(java.math.BigInteger bigInteger) {
        byte[] bArr;
        byte[] byteArray = bigInteger.toByteArray();
        if (!this.b) {
            if (byteArray[0] == 0) {
                int length = byteArray.length - 1;
                bArr = new byte[length];
                java.lang.System.arraycopy(byteArray, 1, bArr, 0, length);
            } else {
                int length2 = byteArray.length;
                bArr = new byte[length2];
                java.lang.System.arraycopy(byteArray, 0, bArr, 0, length2);
            }
            org.bouncycastle.util.Arrays.G(byteArray, (byte) 0);
            return bArr;
        }
        if (byteArray[0] == 0 && byteArray.length > d()) {
            int length3 = byteArray.length - 1;
            byte[] bArr2 = new byte[length3];
            java.lang.System.arraycopy(byteArray, 1, bArr2, 0, length3);
            return bArr2;
        }
        if (byteArray.length >= d()) {
            return byteArray;
        }
        int iD = d();
        byte[] bArr3 = new byte[iD];
        java.lang.System.arraycopy(byteArray, 0, bArr3, iD - byteArray.length, byteArray.length);
        return bArr3;
    }

    public int c() {
        int iBitLength = (this.a.i().bitLength() + 7) / 8;
        return this.b ? iBitLength - 1 : iBitLength;
    }

    public int d() {
        int iBitLength = (this.a.i().bitLength() + 7) / 8;
        return this.b ? iBitLength : iBitLength - 1;
    }

    public void f(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            cipherParameters = ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a();
        }
        this.b = z;
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) cipherParameters;
        this.a = rSAKeyParameters;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties("RSA", org.bouncycastle.crypto.constraints.ConstraintUtils.a(rSAKeyParameters.i()), this.a, e(this.a.f(), z)));
    }

    public java.math.BigInteger g(java.math.BigInteger bigInteger) {
        org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters rSAPrivateCrtKeyParameters;
        java.math.BigInteger bigIntegerO;
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters = this.a;
        if (!(rSAKeyParameters instanceof org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters) || (bigIntegerO = (rSAPrivateCrtKeyParameters = (org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters) rSAKeyParameters).o()) == null) {
            return bigInteger.modPow(this.a.g(), this.a.i());
        }
        java.math.BigInteger bigIntegerN = rSAPrivateCrtKeyParameters.n();
        java.math.BigInteger bigIntegerP = rSAPrivateCrtKeyParameters.p();
        java.math.BigInteger bigIntegerL = rSAPrivateCrtKeyParameters.l();
        java.math.BigInteger bigIntegerM = rSAPrivateCrtKeyParameters.m();
        java.math.BigInteger bigIntegerQ = rSAPrivateCrtKeyParameters.q();
        java.math.BigInteger bigIntegerModPow = bigInteger.remainder(bigIntegerN).modPow(bigIntegerL, bigIntegerN);
        java.math.BigInteger bigIntegerModPow2 = bigInteger.remainder(bigIntegerP).modPow(bigIntegerM, bigIntegerP);
        java.math.BigInteger bigIntegerAdd = bigIntegerModPow.subtract(bigIntegerModPow2).multiply(bigIntegerQ).mod(bigIntegerN).multiply(bigIntegerP).add(bigIntegerModPow2);
        if (bigIntegerAdd.modPow(bigIntegerO, rSAPrivateCrtKeyParameters.i()).equals(bigInteger)) {
            return bigIntegerAdd;
        }
        throw new java.lang.IllegalStateException("RSA engine faulty decryption/signing detected");
    }
}
