package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class KXTSBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    private final int o;
    private final long p;
    private final long[] q;
    private final long[] r;
    private int s;

    private static void j(long j, long[] jArr) {
        long j2 = 0;
        int i = 0;
        while (i < jArr.length) {
            long j3 = jArr[i];
            jArr[i] = j2 ^ (j3 << 1);
            i++;
            j2 = j3 >>> 63;
        }
        jArr[0] = (j & (-j2)) ^ jArr[0];
    }

    private void k(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3 = this.s;
        if (i3 == -1) {
            throw new java.lang.IllegalStateException("Attempt to process too many blocks");
        }
        this.s = i3 + 1;
        j(this.p, this.r);
        byte[] bArr3 = new byte[this.o];
        org.bouncycastle.util.Pack.K(this.r, bArr3, 0);
        int i4 = this.o;
        byte[] bArr4 = new byte[i4];
        java.lang.System.arraycopy(bArr3, 0, bArr4, 0, i4);
        for (int i5 = 0; i5 < this.o; i5++) {
            bArr4[i5] = (byte) (bArr4[i5] ^ bArr[i + i5]);
        }
        this.k.c(bArr4, 0, bArr4, 0);
        for (int i6 = 0; i6 < this.o; i6++) {
            bArr2[i2 + i6] = (byte) (bArr4[i6] ^ bArr3[i6]);
        }
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int a(byte[] bArr, int i) {
        i();
        return 0;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public void f(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("Invalid parameters passed");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        org.bouncycastle.crypto.CipherParameters cipherParametersB = parametersWithIV.b();
        byte[] bArrA = parametersWithIV.a();
        int length = bArrA.length;
        int i = this.o;
        if (length != i) {
            throw new java.lang.IllegalArgumentException("Currently only support IVs of exactly one block");
        }
        byte[] bArr = new byte[i];
        java.lang.System.arraycopy(bArrA, 0, bArr, 0, i);
        this.k.init(true, cipherParametersB);
        this.k.c(bArr, 0, bArr, 0);
        this.k.init(z, cipherParametersB);
        org.bouncycastle.util.Pack.x(bArr, 0, this.q);
        long[] jArr = this.q;
        java.lang.System.arraycopy(jArr, 0, this.r, 0, jArr.length);
        this.s = 0;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int g(byte b, byte[] bArr, int i) {
        throw new java.lang.IllegalStateException("unsupported operation");
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int h(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (bArr.length - i < i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        if (bArr2.length - i < i2) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        if (i2 % this.o != 0) {
            throw new java.lang.IllegalArgumentException("Partial blocks not supported");
        }
        int i4 = 0;
        while (i4 < i2) {
            k(bArr, i + i4, bArr2, i3 + i4);
            i4 += this.o;
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public void i() {
        this.k.reset();
        long[] jArr = this.q;
        java.lang.System.arraycopy(jArr, 0, this.r, 0, jArr.length);
        this.s = 0;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int c(int i) {
        return i;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int e(int i) {
        return i;
    }
}
