package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class EAXBlockCipher implements org.bouncycastle.crypto.modes.AEADBlockCipher {
    private org.bouncycastle.crypto.modes.CTRModeCipher a;
    private boolean b;
    private int c;
    private org.bouncycastle.crypto.Mac d;
    private byte[] e;
    private byte[] f;
    private byte[] g;
    private int h;
    private byte[] i;
    private int j;
    private boolean k;
    private byte[] l;

    public EAXBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.c = blockCipher.b();
        org.bouncycastle.crypto.macs.CMac cMac = new org.bouncycastle.crypto.macs.CMac(blockCipher);
        this.d = cMac;
        this.g = new byte[this.c];
        this.f = new byte[cMac.getMacSize()];
        this.e = new byte[this.d.getMacSize()];
        this.a = org.bouncycastle.crypto.modes.SICBlockCipher.n(blockCipher);
    }

    private void a() {
        byte[] bArr = new byte[this.c];
        int i = 0;
        this.d.doFinal(bArr, 0);
        while (true) {
            byte[] bArr2 = this.g;
            if (i >= bArr2.length) {
                return;
            }
            bArr2[i] = (byte) ((this.e[i] ^ this.f[i]) ^ bArr[i]);
            i++;
        }
    }

    private void e() {
        if (this.k) {
            return;
        }
        this.k = true;
        this.d.doFinal(this.f, 0);
        int i = this.c;
        byte[] bArr = new byte[i];
        bArr[i - 1] = 2;
        this.d.update(bArr, 0, i);
    }

    private int f(byte b, byte[] bArr, int i) {
        int iC;
        byte[] bArr2 = this.i;
        int i2 = this.j;
        int i3 = i2 + 1;
        this.j = i3;
        bArr2[i2] = b;
        if (i3 != bArr2.length) {
            return 0;
        }
        int length = bArr.length;
        int i4 = this.c;
        if (length < i + i4) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer is too short");
        }
        if (this.b) {
            iC = this.a.c(bArr2, 0, bArr, i);
            this.d.update(bArr, i, this.c);
        } else {
            this.d.update(bArr2, 0, i4);
            iC = this.a.c(this.i, 0, bArr, i);
        }
        this.j = 0;
        if (!this.b) {
            byte[] bArr3 = this.i;
            java.lang.System.arraycopy(bArr3, this.c, bArr3, 0, this.h);
            this.j = this.h;
        }
        return iC;
    }

    private void g(boolean z) {
        this.a.reset();
        this.d.reset();
        this.j = 0;
        org.bouncycastle.util.Arrays.G(this.i, (byte) 0);
        if (z) {
            org.bouncycastle.util.Arrays.G(this.g, (byte) 0);
        }
        int i = this.c;
        byte[] bArr = new byte[i];
        bArr[i - 1] = 1;
        this.d.update(bArr, 0, i);
        this.k = false;
        byte[] bArr2 = this.l;
        if (bArr2 != null) {
            c(bArr2, 0, bArr2.length);
        }
    }

    private boolean h(byte[] bArr, int i) {
        int i2 = 0;
        for (int i3 = 0; i3 < this.h; i3++) {
            i2 |= this.g[i3] ^ bArr[i + i3];
        }
        return i2 == 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        int i = this.h;
        byte[] bArr = new byte[i];
        java.lang.System.arraycopy(this.g, 0, bArr, 0, i);
        return bArr;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        if (this.k) {
            throw new java.lang.IllegalStateException("AAD data cannot be added after encryption/decryption processing has begun.");
        }
        this.d.update(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void d(byte b) {
        if (this.k) {
            throw new java.lang.IllegalStateException("AAD data cannot be added after encryption/decryption processing has begun.");
        }
        this.d.update(b);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        e();
        int i2 = this.j;
        byte[] bArr2 = this.i;
        byte[] bArr3 = new byte[bArr2.length];
        this.j = 0;
        if (this.b) {
            int i3 = i + i2;
            if (bArr.length < this.h + i3) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
            this.a.c(bArr2, 0, bArr3, 0);
            java.lang.System.arraycopy(bArr3, 0, bArr, i, i2);
            this.d.update(bArr3, 0, i2);
            a();
            java.lang.System.arraycopy(this.g, 0, bArr, i3, this.h);
            g(false);
            return i2 + this.h;
        }
        int i4 = this.h;
        if (i2 < i4) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
        }
        if (bArr.length < (i + i2) - i4) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        if (i2 > i4) {
            this.d.update(bArr2, 0, i2 - i4);
            this.a.c(this.i, 0, bArr3, 0);
            java.lang.System.arraycopy(bArr3, 0, bArr, i, i2 - this.h);
        }
        a();
        if (!h(this.i, i2 - this.h)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in EAX failed");
        }
        g(false);
        return i2 - this.h;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return this.a.getUnderlyingCipher().getAlgorithmName() + "/EAX";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int i2 = i + this.j;
        boolean z = this.b;
        int i3 = this.h;
        if (z) {
            return i2 + i3;
        }
        if (i2 < i3) {
            return 0;
        }
        return i2 - i3;
    }

    @Override // org.bouncycastle.crypto.modes.AEADBlockCipher
    public org.bouncycastle.crypto.BlockCipher getUnderlyingCipher() {
        return this.a.getUnderlyingCipher();
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        int i2 = i + this.j;
        if (!this.b) {
            int i3 = this.h;
            if (i2 < i3) {
                return 0;
            }
            i2 -= i3;
        }
        return i2 - (i2 % this.c);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArrA;
        org.bouncycastle.crypto.CipherParameters cipherParametersB;
        this.b = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            bArrA = aEADParameters.d();
            this.l = aEADParameters.a();
            this.h = aEADParameters.c() / 8;
            cipherParametersB = aEADParameters.b();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to EAX");
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            bArrA = parametersWithIV.a();
            this.l = null;
            this.h = this.d.getMacSize() / 2;
            cipherParametersB = parametersWithIV.b();
        }
        this.i = new byte[z ? this.c : this.c + this.h];
        byte[] bArr = new byte[this.c];
        this.d.init(cipherParametersB);
        int i = this.c;
        bArr[i - 1] = 0;
        this.d.update(bArr, 0, i);
        this.d.update(bArrA, 0, bArrA.length);
        this.d.doFinal(this.e, 0);
        this.a.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(cipherParametersB, this.e));
        reset();
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) {
        e();
        return f(b, bArr, i);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        e();
        if (bArr.length < i + i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        int iF = 0;
        for (int i4 = 0; i4 != i2; i4++) {
            iF += f(bArr[i + i4], bArr2, i3 + iF);
        }
        return iF;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void reset() {
        g(true);
    }
}
