package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CTSBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    private int o;

    public CTSBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        if (blockCipher instanceof org.bouncycastle.crypto.StreamBlockCipher) {
            throw new java.lang.IllegalArgumentException("CTSBlockCipher can only accept ECB, or CBC ciphers");
        }
        this.k = blockCipher;
        int iB = blockCipher.b();
        this.o = iB;
        this.h = new byte[iB * 2];
        this.i = 0;
    }

    @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;
        int i3 = i2 - iB;
        byte[] bArr2 = new byte[iB];
        if (this.j) {
            if (i2 < iB) {
                throw new org.bouncycastle.crypto.DataLengthException("need at least one block of input for CTS");
            }
            this.k.c(this.h, 0, bArr2, 0);
            int i4 = this.i;
            if (i4 > iB) {
                while (true) {
                    byte[] bArr3 = this.h;
                    if (i4 == bArr3.length) {
                        break;
                    }
                    bArr3[i4] = bArr2[i4 - iB];
                    i4++;
                }
                for (int i5 = iB; i5 != this.i; i5++) {
                    byte[] bArr4 = this.h;
                    bArr4[i5] = (byte) (bArr4[i5] ^ bArr2[i5 - iB]);
                }
                org.bouncycastle.crypto.BlockCipher blockCipher = this.k;
                if (blockCipher instanceof org.bouncycastle.crypto.modes.CBCModeCipher) {
                    ((org.bouncycastle.crypto.modes.CBCModeCipher) 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, i3);
            } else {
                java.lang.System.arraycopy(bArr2, 0, bArr, i, iB);
            }
        } else {
            if (i2 < iB) {
                throw new org.bouncycastle.crypto.DataLengthException("need at least one block of input for CTS");
            }
            byte[] bArr5 = new byte[iB];
            if (i2 > iB) {
                org.bouncycastle.crypto.BlockCipher blockCipher2 = this.k;
                if (blockCipher2 instanceof org.bouncycastle.crypto.modes.CBCModeCipher) {
                    ((org.bouncycastle.crypto.modes.CBCModeCipher) blockCipher2).getUnderlyingCipher().c(this.h, 0, bArr2, 0);
                } else {
                    blockCipher2.c(this.h, 0, bArr2, 0);
                }
                for (int i6 = iB; i6 != this.i; i6++) {
                    int i7 = i6 - iB;
                    bArr5[i7] = (byte) (bArr2[i7] ^ this.h[i6]);
                }
                java.lang.System.arraycopy(this.h, iB, bArr2, 0, i3);
                this.k.c(bArr2, 0, bArr, i);
                java.lang.System.arraycopy(bArr5, 0, bArr, i + iB, i3);
            } else {
                this.k.c(this.h, 0, bArr2, 0);
                java.lang.System.arraycopy(bArr2, 0, bArr, i, iB);
            }
        }
        int i8 = this.i;
        i();
        return i8;
    }

    @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;
    }
}
