package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class NISTCTSBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    private final int o;
    private final int p;

    @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 NISTCTS");
            }
            if (i2 > iB) {
                byte[] bArr3 = new byte[iB];
                int i4 = this.o;
                if (i4 == 2 || i4 == 3) {
                    this.k.c(this.h, 0, bArr2, 0);
                    java.lang.System.arraycopy(this.h, iB, bArr3, 0, i3);
                    this.k.c(bArr3, 0, bArr3, 0);
                    if (this.o == 2 && i3 == iB) {
                        java.lang.System.arraycopy(bArr2, 0, bArr, i, iB);
                        java.lang.System.arraycopy(bArr3, 0, bArr, i + iB, i3);
                    } else {
                        java.lang.System.arraycopy(bArr3, 0, bArr, i, iB);
                        java.lang.System.arraycopy(bArr2, 0, bArr, i + iB, i3);
                    }
                } else {
                    java.lang.System.arraycopy(this.h, 0, bArr2, 0, iB);
                    this.k.c(bArr2, 0, bArr2, 0);
                    java.lang.System.arraycopy(bArr2, 0, bArr, i, i3);
                    java.lang.System.arraycopy(this.h, this.i - i3, bArr3, 0, i3);
                    this.k.c(bArr3, 0, bArr3, 0);
                    java.lang.System.arraycopy(bArr3, 0, bArr, i + i3, iB);
                }
            } else {
                this.k.c(this.h, 0, bArr2, 0);
                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[] bArr4 = new byte[iB];
            if (i2 > iB) {
                int i5 = this.o;
                if (i5 == 3 || (i5 == 2 && (this.h.length - i2) % iB != 0)) {
                    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, 0, bArr2, 0);
                    } else {
                        blockCipher.c(this.h, 0, bArr2, 0);
                    }
                    for (int i6 = iB; i6 != this.i; i6++) {
                        int i7 = i6 - iB;
                        bArr4[i7] = (byte) (bArr2[i7] ^ this.h[i6]);
                    }
                    java.lang.System.arraycopy(this.h, iB, bArr2, 0, i3);
                    this.k.c(bArr2, 0, bArr, i);
                } else {
                    ((org.bouncycastle.crypto.modes.CBCModeCipher) this.k).getUnderlyingCipher().c(this.h, this.i - iB, bArr4, 0);
                    java.lang.System.arraycopy(this.h, 0, bArr2, 0, iB);
                    if (i3 != iB) {
                        java.lang.System.arraycopy(bArr4, i3, bArr2, i3, iB - i3);
                    }
                    this.k.c(bArr2, 0, bArr2, 0);
                    java.lang.System.arraycopy(bArr2, 0, bArr, i, iB);
                    for (int i8 = 0; i8 != i3; i8++) {
                        bArr4[i8] = (byte) (bArr4[i8] ^ this.h[i8]);
                    }
                }
                java.lang.System.arraycopy(bArr4, 0, bArr, i + iB, i3);
            } else {
                this.k.c(this.h, 0, bArr2, 0);
                java.lang.System.arraycopy(bArr2, 0, bArr, i, iB);
            }
        }
        int i9 = this.i;
        i();
        return i9;
    }

    @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.p;
            java.lang.System.arraycopy(bArr3, i4, bArr3, 0, i4);
            this.i = this.p;
            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;
    }
}
