package org.bouncycastle.crypto;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DefaultBufferedBlockCipher extends org.bouncycastle.crypto.BufferedBlockCipher {
    protected byte[] h;
    protected int i;
    protected boolean j;
    protected org.bouncycastle.crypto.BlockCipher k;
    protected org.bouncycastle.crypto.MultiBlockCipher l;
    protected boolean m;
    protected boolean n;

    protected DefaultBufferedBlockCipher() {
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int a(byte[] bArr, int i) {
        try {
            int i2 = this.i;
            if (i + i2 > bArr.length) {
                throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short for doFinal()");
            }
            int i3 = 0;
            if (i2 != 0) {
                if (!this.m) {
                    throw new org.bouncycastle.crypto.DataLengthException("data not block size aligned");
                }
                org.bouncycastle.crypto.BlockCipher blockCipher = this.k;
                byte[] bArr2 = this.h;
                blockCipher.c(bArr2, 0, bArr2, 0);
                int i4 = this.i;
                this.i = 0;
                java.lang.System.arraycopy(this.h, 0, bArr, i, i4);
                i3 = i4;
            }
            i();
            return i3;
        } catch (java.lang.Throwable th) {
            i();
            throw th;
        }
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int b() {
        return this.k.b();
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int c(int i) {
        int iB;
        if (this.n && this.j) {
            i += this.i;
            iB = this.k.b() + 2;
        } else {
            iB = this.i;
        }
        return i + iB;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public org.bouncycastle.crypto.BlockCipher d() {
        return this.k;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int e(int i) {
        int length;
        int i2 = i + this.i;
        if (this.n && this.j) {
            length = (i2 % this.h.length) - (this.k.b() + 2);
        } else {
            int length2 = this.h.length;
            length = i2 % length2;
        }
        return i2 - length;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public void f(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.j = z;
        i();
        this.k.init(z, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int g(byte b, byte[] bArr, int i) {
        byte[] bArr2 = this.h;
        int i2 = this.i;
        int i3 = i2 + 1;
        this.i = i3;
        bArr2[i2] = b;
        if (i3 != bArr2.length) {
            return 0;
        }
        int iC = this.k.c(bArr2, 0, bArr, i);
        this.i = 0;
        return iC;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int h(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        int iC;
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("Can't have a negative input length!");
        }
        int iB = b();
        int iE = e(i2);
        if (iE > 0 && iE + i3 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        byte[] bArr3 = this.h;
        int length = bArr3.length;
        int i4 = this.i;
        int i5 = length - i4;
        if (i2 > i5) {
            java.lang.System.arraycopy(bArr, i, bArr3, i4, i5);
            iC = this.k.c(this.h, 0, bArr2, i3);
            this.i = 0;
            i2 -= i5;
            int i6 = i5 + i;
            org.bouncycastle.crypto.MultiBlockCipher multiBlockCipher = this.l;
            if (multiBlockCipher != null) {
                int iD = i2 / multiBlockCipher.d();
                if (iD > 0) {
                    iC += this.l.a(bArr, i6, iD, bArr2, i3 + iC);
                    int iD2 = iD * this.l.d();
                    i2 -= iD2;
                    i6 += iD2;
                }
                i = i6;
            } else {
                i = i6;
                while (i2 > this.h.length) {
                    iC += this.k.c(bArr, i, bArr2, i3 + iC);
                    i2 -= iB;
                    i += iB;
                }
            }
        } else {
            iC = 0;
        }
        java.lang.System.arraycopy(bArr, i, this.h, this.i, i2);
        int i7 = this.i + i2;
        this.i = i7;
        byte[] bArr4 = this.h;
        if (i7 != bArr4.length) {
            return iC;
        }
        int iC2 = iC + this.k.c(bArr4, 0, bArr2, i3 + iC);
        this.i = 0;
        return iC2;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public void i() {
        int i = 0;
        while (true) {
            byte[] bArr = this.h;
            if (i >= bArr.length) {
                this.i = 0;
                this.k.reset();
                return;
            } else {
                bArr[i] = 0;
                i++;
            }
        }
    }

    public DefaultBufferedBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.k = blockCipher;
        if (blockCipher instanceof org.bouncycastle.crypto.MultiBlockCipher) {
            org.bouncycastle.crypto.MultiBlockCipher multiBlockCipher = (org.bouncycastle.crypto.MultiBlockCipher) blockCipher;
            this.l = multiBlockCipher;
            this.h = new byte[multiBlockCipher.d()];
        } else {
            this.l = null;
            this.h = new byte[blockCipher.b()];
        }
        boolean z = false;
        this.i = 0;
        java.lang.String algorithmName = blockCipher.getAlgorithmName();
        int iIndexOf = algorithmName.indexOf(47) + 1;
        boolean z2 = iIndexOf > 0 && algorithmName.startsWith("PGP", iIndexOf);
        this.n = z2;
        if (z2 || (blockCipher instanceof org.bouncycastle.crypto.StreamCipher)) {
            this.m = true;
            return;
        }
        if (iIndexOf > 0 && algorithmName.startsWith("OpenPGP", iIndexOf)) {
            z = true;
        }
        this.m = z;
    }
}
