package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class IESEngine {
    org.bouncycastle.crypto.BasicAgreement a;
    org.bouncycastle.crypto.DerivationFunction b;
    org.bouncycastle.crypto.Mac c;
    org.bouncycastle.crypto.BufferedBlockCipher d;
    byte[] e;
    boolean f;
    org.bouncycastle.crypto.CipherParameters g;
    org.bouncycastle.crypto.CipherParameters h;
    org.bouncycastle.crypto.params.IESParameters i;
    byte[] j;
    private org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator k;
    private org.bouncycastle.crypto.KeyParser l;
    private byte[] m;

    public IESEngine(org.bouncycastle.crypto.BasicAgreement basicAgreement, org.bouncycastle.crypto.DerivationFunction derivationFunction, org.bouncycastle.crypto.Mac mac) {
        this.a = basicAgreement;
        this.b = derivationFunction;
        this.c = mac;
        this.e = new byte[mac.getMacSize()];
        this.d = null;
    }

    private byte[] a(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        byte[] bArr2;
        byte[] bArr3;
        int iH;
        byte[] bArr4;
        if (i2 < this.j.length + this.c.getMacSize()) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Length of input must be greater than the MAC and V combined");
        }
        if (this.d == null) {
            int length = (i2 - this.j.length) - this.c.getMacSize();
            byte[] bArr5 = new byte[length];
            int iC = this.i.c() / 8;
            bArr2 = new byte[iC];
            int i3 = length + iC;
            byte[] bArr6 = new byte[i3];
            this.b.generateBytes(bArr6, 0, i3);
            if (this.j.length != 0) {
                java.lang.System.arraycopy(bArr6, 0, bArr2, 0, iC);
                java.lang.System.arraycopy(bArr6, iC, bArr5, 0, length);
            } else {
                java.lang.System.arraycopy(bArr6, 0, bArr5, 0, length);
                java.lang.System.arraycopy(bArr6, length, bArr2, 0, iC);
            }
            bArr4 = new byte[length];
            for (int i4 = 0; i4 != length; i4++) {
                bArr4[i4] = (byte) (bArr[(this.j.length + i) + i4] ^ bArr5[i4]);
            }
            bArr3 = bArr;
            iH = 0;
        } else {
            int iD = ((org.bouncycastle.crypto.params.IESWithCipherParameters) this.i).d() / 8;
            byte[] bArr7 = new byte[iD];
            int iC2 = this.i.c() / 8;
            bArr2 = new byte[iC2];
            int i5 = iD + iC2;
            byte[] bArr8 = new byte[i5];
            this.b.generateBytes(bArr8, 0, i5);
            java.lang.System.arraycopy(bArr8, 0, bArr7, 0, iD);
            java.lang.System.arraycopy(bArr8, iD, bArr2, 0, iC2);
            org.bouncycastle.crypto.CipherParameters keyParameter = new org.bouncycastle.crypto.params.KeyParameter(bArr7);
            byte[] bArr9 = this.m;
            if (bArr9 != null) {
                keyParameter = new org.bouncycastle.crypto.params.ParametersWithIV(keyParameter, bArr9);
            }
            this.d.f(false, keyParameter);
            byte[] bArr10 = new byte[this.d.c((i2 - this.j.length) - this.c.getMacSize())];
            org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher = this.d;
            byte[] bArr11 = this.j;
            bArr3 = bArr;
            iH = bufferedBlockCipher.h(bArr3, i + bArr11.length, (i2 - bArr11.length) - this.c.getMacSize(), bArr10, 0);
            bArr4 = bArr10;
        }
        byte[] bArrB = this.i.b();
        byte[] bArrE = this.j.length != 0 ? e(bArrB) : null;
        int i6 = i + i2;
        byte[] bArrD = org.bouncycastle.util.Arrays.D(bArr3, i6 - this.c.getMacSize(), i6);
        int length2 = bArrD.length;
        byte[] bArr12 = new byte[length2];
        this.c.init(new org.bouncycastle.crypto.params.KeyParameter(bArr2));
        org.bouncycastle.crypto.Mac mac = this.c;
        byte[] bArr13 = this.j;
        mac.update(bArr3, i + bArr13.length, (i2 - bArr13.length) - length2);
        if (bArrB != null) {
            this.c.update(bArrB, 0, bArrB.length);
        }
        if (this.j.length != 0) {
            this.c.update(bArrE, 0, bArrE.length);
        }
        this.c.doFinal(bArr12, 0);
        if (!org.bouncycastle.util.Arrays.z(bArrD, bArr12)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("invalid MAC");
        }
        org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher2 = this.d;
        return bufferedBlockCipher2 == null ? bArr4 : org.bouncycastle.util.Arrays.D(bArr4, 0, iH + bufferedBlockCipher2.a(bArr4, iH));
    }

    private byte[] b(byte[] bArr, int i, int i2) {
        org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher;
        org.bouncycastle.crypto.CipherParameters keyParameter;
        byte[] bArr2;
        byte[] bArr3;
        if (this.d == null) {
            byte[] bArr4 = new byte[i2];
            int iC = this.i.c() / 8;
            bArr2 = new byte[iC];
            int i3 = i2 + iC;
            byte[] bArr5 = new byte[i3];
            this.b.generateBytes(bArr5, 0, i3);
            if (this.j.length != 0) {
                java.lang.System.arraycopy(bArr5, 0, bArr2, 0, iC);
                java.lang.System.arraycopy(bArr5, iC, bArr4, 0, i2);
            } else {
                java.lang.System.arraycopy(bArr5, 0, bArr4, 0, i2);
                java.lang.System.arraycopy(bArr5, i2, bArr2, 0, iC);
            }
            bArr3 = new byte[i2];
            for (int i4 = 0; i4 != i2; i4++) {
                bArr3[i4] = (byte) (bArr[i + i4] ^ bArr4[i4]);
            }
        } else {
            int iD = ((org.bouncycastle.crypto.params.IESWithCipherParameters) this.i).d() / 8;
            byte[] bArr6 = new byte[iD];
            int iC2 = this.i.c() / 8;
            byte[] bArr7 = new byte[iC2];
            int i5 = iD + iC2;
            byte[] bArr8 = new byte[i5];
            this.b.generateBytes(bArr8, 0, i5);
            java.lang.System.arraycopy(bArr8, 0, bArr6, 0, iD);
            java.lang.System.arraycopy(bArr8, iD, bArr7, 0, iC2);
            if (this.m != null) {
                bufferedBlockCipher = this.d;
                keyParameter = new org.bouncycastle.crypto.params.ParametersWithIV(new org.bouncycastle.crypto.params.KeyParameter(bArr6), this.m);
            } else {
                bufferedBlockCipher = this.d;
                keyParameter = new org.bouncycastle.crypto.params.KeyParameter(bArr6);
            }
            bufferedBlockCipher.f(true, keyParameter);
            byte[] bArr9 = new byte[this.d.c(i2)];
            int iH = this.d.h(bArr, i, i2, bArr9, 0);
            i2 = iH + this.d.a(bArr9, iH);
            bArr2 = bArr7;
            bArr3 = bArr9;
        }
        byte[] bArrB = this.i.b();
        byte[] bArrE = this.j.length != 0 ? e(bArrB) : null;
        int macSize = this.c.getMacSize();
        byte[] bArr10 = new byte[macSize];
        this.c.init(new org.bouncycastle.crypto.params.KeyParameter(bArr2));
        this.c.update(bArr3, 0, bArr3.length);
        if (bArrB != null) {
            this.c.update(bArrB, 0, bArrB.length);
        }
        if (this.j.length != 0) {
            this.c.update(bArrE, 0, bArrE.length);
        }
        this.c.doFinal(bArr10, 0);
        byte[] bArr11 = this.j;
        byte[] bArr12 = new byte[bArr11.length + i2 + macSize];
        java.lang.System.arraycopy(bArr11, 0, bArr12, 0, bArr11.length);
        java.lang.System.arraycopy(bArr3, 0, bArr12, this.j.length, i2);
        java.lang.System.arraycopy(bArr10, 0, bArr12, this.j.length + i2, macSize);
        return bArr12;
    }

    private void c(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            this.m = parametersWithIV.a();
            cipherParameters = parametersWithIV.b();
        } else {
            this.m = null;
        }
        this.i = (org.bouncycastle.crypto.params.IESParameters) cipherParameters;
    }

    public org.bouncycastle.crypto.BufferedBlockCipher d() {
        return this.d;
    }

    protected byte[] e(byte[] bArr) {
        byte[] bArr2 = new byte[8];
        if (bArr != null) {
            org.bouncycastle.util.Pack.C(((long) bArr.length) * 8, bArr2, 0);
        }
        return bArr2;
    }

    public org.bouncycastle.crypto.Mac f() {
        return this.c;
    }

    public void g(org.bouncycastle.crypto.params.AsymmetricKeyParameter asymmetricKeyParameter, org.bouncycastle.crypto.CipherParameters cipherParameters, org.bouncycastle.crypto.KeyParser keyParser) {
        this.f = false;
        this.g = asymmetricKeyParameter;
        this.l = keyParser;
        c(cipherParameters);
    }

    public void h(org.bouncycastle.crypto.params.AsymmetricKeyParameter asymmetricKeyParameter, org.bouncycastle.crypto.CipherParameters cipherParameters, org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator ephemeralKeyPairGenerator) {
        this.f = true;
        this.h = asymmetricKeyParameter;
        this.k = ephemeralKeyPairGenerator;
        c(cipherParameters);
    }

    public void i(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters, org.bouncycastle.crypto.CipherParameters cipherParameters2, org.bouncycastle.crypto.CipherParameters cipherParameters3) {
        this.f = z;
        this.g = cipherParameters;
        this.h = cipherParameters2;
        this.j = new byte[0];
        c(cipherParameters3);
    }

    public byte[] j(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        if (this.f) {
            org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator ephemeralKeyPairGenerator = this.k;
            if (ephemeralKeyPairGenerator != null) {
                org.bouncycastle.crypto.EphemeralKeyPair ephemeralKeyPairA = ephemeralKeyPairGenerator.a();
                this.g = ephemeralKeyPairA.b().a();
                this.j = ephemeralKeyPairA.a();
            }
        } else if (this.l != null) {
            java.io.ByteArrayInputStream byteArrayInputStream = new java.io.ByteArrayInputStream(bArr, i, i2);
            try {
                this.h = this.l.a(byteArrayInputStream);
                this.j = org.bouncycastle.util.Arrays.D(bArr, i, (i2 - byteArrayInputStream.available()) + i);
            } catch (java.io.IOException e) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
            } catch (java.lang.IllegalArgumentException e2) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("unable to recover ephemeral public key: " + e2.getMessage(), e2);
            }
        }
        this.a.init(this.g);
        byte[] bArrB = org.bouncycastle.util.BigIntegers.b(this.a.getFieldSize(), this.a.a(this.h));
        byte[] bArr2 = this.j;
        if (bArr2.length != 0) {
            byte[] bArrT = org.bouncycastle.util.Arrays.t(bArr2, bArrB);
            org.bouncycastle.util.Arrays.G(bArrB, (byte) 0);
            bArrB = bArrT;
        }
        try {
            this.b.init(new org.bouncycastle.crypto.params.KDFParameters(bArrB, this.i.a()));
            return this.f ? b(bArr, i, i2) : a(bArr, i, i2);
        } finally {
            org.bouncycastle.util.Arrays.G(bArrB, (byte) 0);
        }
    }

    public IESEngine(org.bouncycastle.crypto.BasicAgreement basicAgreement, org.bouncycastle.crypto.DerivationFunction derivationFunction, org.bouncycastle.crypto.Mac mac, org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher) {
        this.a = basicAgreement;
        this.b = derivationFunction;
        this.c = mac;
        this.e = new byte[mac.getMacSize()];
        this.d = bufferedBlockCipher;
    }
}
