package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ElGamalEngine implements org.bouncycastle.crypto.AsymmetricBlockCipher {
    private static final java.math.BigInteger e = java.math.BigInteger.valueOf(0);
    private static final java.math.BigInteger f = java.math.BigInteger.valueOf(1);
    private static final java.math.BigInteger g = java.math.BigInteger.valueOf(2);
    private org.bouncycastle.crypto.params.ElGamalKeyParameters a;
    private java.security.SecureRandom b;
    private boolean c;
    private int d;

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getInputBlockSize() {
        boolean z = this.c;
        int i = this.d;
        return z ? (i - 1) / 8 : ((i + 7) / 8) * 2;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getOutputBlockSize() {
        boolean z = this.c;
        int i = this.d;
        return z ? ((i + 7) / 8) * 2 : (i - 1) / 8;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        java.security.SecureRandom secureRandomD;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            this.a = (org.bouncycastle.crypto.params.ElGamalKeyParameters) parametersWithRandom.a();
            secureRandomD = parametersWithRandom.b();
        } else {
            this.a = (org.bouncycastle.crypto.params.ElGamalKeyParameters) cipherParameters;
            secureRandomD = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
        }
        this.b = secureRandomD;
        this.c = z;
        this.d = this.a.g().c().bitLength();
        org.bouncycastle.crypto.params.ElGamalKeyParameters elGamalKeyParameters = this.a;
        if (z) {
            if (!(elGamalKeyParameters instanceof org.bouncycastle.crypto.params.ElGamalPublicKeyParameters)) {
                throw new java.lang.IllegalArgumentException("ElGamalPublicKeyParameters are required for encryption.");
            }
        } else if (!(elGamalKeyParameters instanceof org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters)) {
            throw new java.lang.IllegalArgumentException("ElGamalPrivateKeyParameters are required for decryption.");
        }
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties("RSA", org.bouncycastle.crypto.constraints.ConstraintUtils.a(this.a.g().c()), this.a, org.bouncycastle.crypto.engines.Utils.a(z)));
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public byte[] processBlock(byte[] bArr, int i, int i2) {
        java.math.BigInteger bigIntegerF;
        if (this.a == null) {
            throw new java.lang.IllegalStateException("ElGamal engine not initialised");
        }
        if (i2 > (this.c ? (this.d + 6) / 8 : getInputBlockSize())) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for ElGamal cipher.\n");
        }
        java.math.BigInteger bigIntegerC = this.a.g().c();
        if (this.a instanceof org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters) {
            int i3 = i2 / 2;
            byte[] bArr2 = new byte[i3];
            byte[] bArr3 = new byte[i3];
            java.lang.System.arraycopy(bArr, i, bArr2, 0, i3);
            java.lang.System.arraycopy(bArr, i + i3, bArr3, 0, i3);
            return org.bouncycastle.util.BigIntegers.c(new java.math.BigInteger(1, bArr2).modPow(bigIntegerC.subtract(f).subtract(((org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters) this.a).h()), bigIntegerC).multiply(new java.math.BigInteger(1, bArr3)).mod(bigIntegerC));
        }
        if (i != 0 || i2 != bArr.length) {
            byte[] bArr4 = new byte[i2];
            java.lang.System.arraycopy(bArr, i, bArr4, 0, i2);
            bArr = bArr4;
        }
        java.math.BigInteger bigInteger = new java.math.BigInteger(1, bArr);
        if (bigInteger.compareTo(bigIntegerC) >= 0) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for ElGamal cipher.\n");
        }
        org.bouncycastle.crypto.params.ElGamalPublicKeyParameters elGamalPublicKeyParameters = (org.bouncycastle.crypto.params.ElGamalPublicKeyParameters) this.a;
        int iBitLength = bigIntegerC.bitLength();
        while (true) {
            bigIntegerF = org.bouncycastle.util.BigIntegers.f(iBitLength, this.b);
            if (!bigIntegerF.equals(e) && bigIntegerF.compareTo(bigIntegerC.subtract(g)) <= 0) {
                break;
            }
        }
        java.math.BigInteger bigIntegerModPow = this.a.g().a().modPow(bigIntegerF, bigIntegerC);
        java.math.BigInteger bigIntegerMod = bigInteger.multiply(elGamalPublicKeyParameters.h().modPow(bigIntegerF, bigIntegerC)).mod(bigIntegerC);
        byte[] byteArray = bigIntegerModPow.toByteArray();
        byte[] byteArray2 = bigIntegerMod.toByteArray();
        int outputBlockSize = getOutputBlockSize();
        byte[] bArr5 = new byte[outputBlockSize];
        int i4 = outputBlockSize / 2;
        if (byteArray.length > i4) {
            java.lang.System.arraycopy(byteArray, 1, bArr5, i4 - (byteArray.length - 1), byteArray.length - 1);
        } else {
            java.lang.System.arraycopy(byteArray, 0, bArr5, i4 - byteArray.length, byteArray.length);
        }
        if (byteArray2.length > i4) {
            java.lang.System.arraycopy(byteArray2, 1, bArr5, outputBlockSize - (byteArray2.length - 1), byteArray2.length - 1);
            return bArr5;
        }
        java.lang.System.arraycopy(byteArray2, 0, bArr5, outputBlockSize - byteArray2.length, byteArray2.length);
        return bArr5;
    }
}
