package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class OldCTSBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    private int o;

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int a(byte[] bArr, int i) {
        if (this.i + i > bArr.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer to small in doFinal");
        }
        int iB = this.k.b();
        int i2 = this.i - iB;
        byte[] bArr2 = new byte[iB];
        if (this.j) {
            this.k.c(this.h, 0, bArr2, 0);
            int i3 = this.i;
            if (i3 < iB) {
                throw new org.bouncycastle.crypto.DataLengthException("need at least one block of input for CTS");
            }
            while (true) {
                byte[] bArr3 = this.h;
                if (i3 == bArr3.length) {
                    break;
                }
                bArr3[i3] = bArr2[i3 - iB];
                i3++;
            }
            for (int i4 = iB; i4 != this.i; i4++) {
                byte[] bArr4 = this.h;
                bArr4[i4] = (byte) (bArr4[i4] ^ bArr2[i4 - iB]);
            }
            org.bouncycastle.crypto.BlockCipher blockCipher = this.k;
            if (blockCipher instanceof org.bouncycastle.crypto.modes.CBCBlockCipher) {
                ((org.bouncycastle.crypto.modes.CBCBlockCipher) blockCipher).getUnderlyingCipher().c(this.h, iB, bArr, i);
            } else {
                blockCipher.c(this.h, iB, bArr, i);
            }
            java.lang.System.arraycopy(bArr2, 0, bArr, i + iB, i2);
        } else {
            byte[] bArr5 = new byte[iB];
            org.bouncycastle.crypto.BlockCipher blockCipher2 = this.k;
            if (blockCipher2 instanceof org.bouncycastle.crypto.modes.CBCBlockCipher) {
                ((org.bouncycastle.crypto.modes.CBCBlockCipher) blockCipher2).getUnderlyingCipher().c(this.h, 0, bArr2, 0);
            } else {
                blockCipher2.c(this.h, 0, bArr2, 0);
            }
            for (int i5 = iB; i5 != this.i; i5++) {
                int i6 = i5 - iB;
                bArr5[i6] = (byte) (bArr2[i6] ^ this.h[i5]);
            }
            java.lang.System.arraycopy(this.h, iB, bArr2, 0, i2);
            this.k.c(bArr2, 0, bArr, i);
            java.lang.System.arraycopy(bArr5, 0, bArr, i + iB, i2);
        }
        int i7 = this.i;
        i();
        return i7;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int c(int i) {
        return i + this.i;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int e(int i) {
        int i2 = i + this.i;
        byte[] bArr = this.h;
        int length = i2 % bArr.length;
        return length == 0 ? i2 - bArr.length : i2 - length;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int g(byte b, byte[] bArr, int i) {
        int i2 = this.i;
        byte[] bArr2 = this.h;
        int i3 = 0;
        if (i2 == bArr2.length) {
            int iC = this.k.c(bArr2, 0, bArr, i);
            byte[] bArr3 = this.h;
            int i4 = this.o;
            java.lang.System.arraycopy(bArr3, i4, bArr3, 0, i4);
            this.i = this.o;
            i3 = iC;
        }
        byte[] bArr4 = this.h;
        int i5 = this.i;
        this.i = i5 + 1;
        bArr4[i5] = b;
        return i3;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int h(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        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;
        int i6 = 0;
        if (i2 > i5) {
            java.lang.System.arraycopy(bArr, i, bArr3, i4, i5);
            int iC = this.k.c(this.h, 0, bArr2, i3);
            byte[] bArr4 = this.h;
            java.lang.System.arraycopy(bArr4, iB, bArr4, 0, iB);
            this.i = iB;
            i2 -= i5;
            i += i5;
            while (i2 > iB) {
                java.lang.System.arraycopy(bArr, i, this.h, this.i, iB);
                iC += this.k.c(this.h, 0, bArr2, i3 + iC);
                byte[] bArr5 = this.h;
                java.lang.System.arraycopy(bArr5, iB, bArr5, 0, iB);
                i2 -= iB;
                i += iB;
            }
            i6 = iC;
        }
        java.lang.System.arraycopy(bArr, i, this.h, this.i, i2);
        this.i += i2;
        return i6;
    }
}
