package org.bouncycastle.crypto.engines; import java.math.BigInteger; import java.security.SecureRandom; import org.bouncycastle.crypto.AsymmetricBlockCipher; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.crypto.params.RSAKeyParameters; import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters; import org.bouncycastle.util.BigIntegers; /* loaded from: classes6.dex */ public class RSABlindedEngine implements AsymmetricBlockCipher { private static final BigInteger ONE = BigInteger.valueOf(1); private RSACoreEngine core = new RSACoreEngine(); private RSAKeyParameters key; private SecureRandom random; @Override // org.bouncycastle.crypto.AsymmetricBlockCipher public byte[] processBlock(byte[] bArr, int i, int i2) { BigInteger processBlock; RSAPrivateCrtKeyParameters rSAPrivateCrtKeyParameters; BigInteger publicExponent; if (this.key == null) { throw new IllegalStateException("RSA engine not initialised"); } BigInteger convertInput = this.core.convertInput(bArr, i, i2); RSAKeyParameters rSAKeyParameters = this.key; if (!(rSAKeyParameters instanceof RSAPrivateCrtKeyParameters) || (publicExponent = (rSAPrivateCrtKeyParameters = (RSAPrivateCrtKeyParameters) rSAKeyParameters).getPublicExponent()) == null) { processBlock = this.core.processBlock(convertInput); } else { BigInteger modulus = rSAPrivateCrtKeyParameters.getModulus(); BigInteger bigInteger = ONE; BigInteger createRandomInRange = BigIntegers.createRandomInRange(bigInteger, modulus.subtract(bigInteger), this.random); processBlock = this.core.processBlock(createRandomInRange.modPow(publicExponent, modulus).multiply(convertInput).mod(modulus)).multiply(createRandomInRange.modInverse(modulus)).mod(modulus); if (!convertInput.equals(processBlock.modPow(publicExponent, modulus))) { throw new IllegalStateException("RSA engine faulty decryption/signing detected"); } } return this.core.convertOutput(processBlock); } @Override // org.bouncycastle.crypto.AsymmetricBlockCipher public void init(boolean z, CipherParameters cipherParameters) { SecureRandom secureRandom; this.core.init(z, cipherParameters); if (cipherParameters instanceof ParametersWithRandom) { ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters; this.key = (RSAKeyParameters) parametersWithRandom.getParameters(); secureRandom = parametersWithRandom.getRandom(); } else { this.key = (RSAKeyParameters) cipherParameters; secureRandom = new SecureRandom(); } this.random = secureRandom; } @Override // org.bouncycastle.crypto.AsymmetricBlockCipher public int getOutputBlockSize() { return this.core.getOutputBlockSize(); } @Override // org.bouncycastle.crypto.AsymmetricBlockCipher public int getInputBlockSize() { return this.core.getInputBlockSize(); } }