package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RSABlindedEngine implements org.bouncycastle.crypto.AsymmetricBlockCipher {
    private static final java.math.BigInteger d = java.math.BigInteger.valueOf(1);
    private org.bouncycastle.crypto.engines.RSACoreEngine a = new org.bouncycastle.crypto.engines.RSACoreEngine();
    private org.bouncycastle.crypto.params.RSAKeyParameters b;
    private java.security.SecureRandom c;

    private java.math.BigInteger b(java.math.BigInteger bigInteger) {
        org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters rSAPrivateCrtKeyParameters;
        java.math.BigInteger bigIntegerO;
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters = this.b;
        if (!(rSAKeyParameters instanceof org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters) || (bigIntegerO = (rSAPrivateCrtKeyParameters = (org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters) rSAKeyParameters).o()) == null) {
            return this.a.g(bigInteger);
        }
        java.math.BigInteger bigIntegerI = rSAPrivateCrtKeyParameters.i();
        java.math.BigInteger bigInteger2 = d;
        java.math.BigInteger bigIntegerG = org.bouncycastle.util.BigIntegers.g(bigInteger2, bigIntegerI.subtract(bigInteger2), this.c);
        return org.bouncycastle.util.BigIntegers.n(bigIntegerI, bigIntegerG).multiply(this.a.g(bigIntegerG.modPow(bigIntegerO, bigIntegerI).multiply(bigInteger).mod(bigIntegerI))).mod(bigIntegerI);
    }

    protected java.security.SecureRandom a(boolean z, java.security.SecureRandom secureRandom) {
        if (z) {
            return org.bouncycastle.crypto.CryptoServicesRegistrar.e(secureRandom);
        }
        return null;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getInputBlockSize() {
        return this.a.c();
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getOutputBlockSize() {
        return this.a.d();
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        java.security.SecureRandom secureRandomB;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            secureRandomB = parametersWithRandom.b();
            cipherParameters = parametersWithRandom.a();
        } else {
            secureRandomB = null;
        }
        this.a.f(z, cipherParameters);
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) cipherParameters;
        this.b = rSAKeyParameters;
        this.c = a(rSAKeyParameters instanceof org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters, secureRandomB);
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public byte[] processBlock(byte[] bArr, int i, int i2) {
        if (this.b == null) {
            throw new java.lang.IllegalStateException("RSA engine not initialised");
        }
        return this.a.b(b(this.a.a(bArr, i, i2)));
    }
}
