package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class SICBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher implements org.bouncycastle.crypto.modes.CTRModeCipher {
    private final org.bouncycastle.crypto.BlockCipher b;
    private final int c;
    private byte[] d;
    private byte[] e;
    private byte[] f;
    private int g;

    public SICBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        super(blockCipher);
        this.b = blockCipher;
        int iB = blockCipher.b();
        this.c = iB;
        this.d = new byte[iB];
        this.e = new byte[iB];
        this.f = new byte[iB];
        this.g = 0;
    }

    private void g(long j) {
        int i = 5;
        if (j >= 0) {
            long j2 = (((long) this.g) + j) / ((long) this.c);
            long j3 = j2;
            if (j2 > 255) {
                while (i >= 1) {
                    long j4 = 1 << (i * 8);
                    while (j3 >= j4) {
                        m(i);
                        j3 -= j4;
                    }
                    i--;
                }
            }
            l((int) j3);
            this.g = (int) ((j + ((long) this.g)) - (((long) this.c) * j2));
            return;
        }
        long j5 = ((-j) - ((long) this.g)) / ((long) this.c);
        long j6 = j5;
        if (j5 > 255) {
            while (i >= 1) {
                long j7 = 1 << (i * 8);
                while (j6 > j7) {
                    j(i);
                    j6 -= j7;
                }
                i--;
            }
        }
        for (long j8 = 0; j8 != j6; j8++) {
            j(0);
        }
        int i2 = (int) (((long) this.g) + j + (((long) this.c) * j5));
        if (i2 >= 0) {
            this.g = 0;
        } else {
            j(0);
            this.g = this.c + i2;
        }
    }

    private void h() {
        byte[] bArr = this.d;
        if (bArr.length < this.c) {
            for (int length = bArr.length - 1; length >= 0; length--) {
                if (this.e[length] != this.d[length]) {
                    throw new java.lang.IllegalStateException("Counter in CTR/SIC mode out of range.");
                }
            }
        }
    }

    private void i() {
        byte[] bArr = this.d;
        if (bArr.length < this.c && this.e[bArr.length - 1] != bArr[bArr.length - 1]) {
            throw new java.lang.IllegalStateException("Counter in CTR/SIC mode out of range.");
        }
    }

    private void j(int i) {
        byte b;
        int length = this.e.length - i;
        do {
            length--;
            if (length < 0) {
                return;
            }
            byte[] bArr = this.e;
            b = (byte) (bArr[length] - 1);
            bArr[length] = b;
        } while (b == -1);
    }

    private void k() {
        byte b;
        int length = this.e.length;
        do {
            length--;
            if (length < 0) {
                return;
            }
            byte[] bArr = this.e;
            b = (byte) (bArr[length] + 1);
            bArr[length] = b;
        } while (b == 0);
    }

    private void l(int i) {
        byte[] bArr = this.e;
        byte b = bArr[bArr.length - 1];
        int length = bArr.length - 1;
        bArr[length] = (byte) (bArr[length] + ((byte) i));
        if ((b & 255) + i > 255) {
            m(1);
        }
    }

    private void m(int i) {
        byte b;
        int length = this.e.length - i;
        do {
            length--;
            if (length < 0) {
                return;
            }
            byte[] bArr = this.e;
            b = (byte) (bArr[length] + 1);
            bArr[length] = b;
        } while (b == 0);
    }

    public static org.bouncycastle.crypto.modes.CTRModeCipher n(org.bouncycastle.crypto.BlockCipher blockCipher) {
        return new org.bouncycastle.crypto.modes.SICBlockCipher(blockCipher);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.b.b();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        org.bouncycastle.crypto.modes.SICBlockCipher sICBlockCipher;
        if (this.g != 0) {
            sICBlockCipher = this;
            sICBlockCipher.processBytes(bArr, i, this.c, bArr2, i2);
        } else {
            sICBlockCipher = this;
            int i3 = sICBlockCipher.c;
            if (i + i3 > bArr.length) {
                throw new org.bouncycastle.crypto.DataLengthException("input buffer too small");
            }
            if (i2 + i3 > bArr2.length) {
                throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
            }
            sICBlockCipher.b.c(sICBlockCipher.e, 0, sICBlockCipher.f, 0);
            for (int i4 = 0; i4 < sICBlockCipher.c; i4++) {
                bArr2[i2 + i4] = (byte) (bArr[i + i4] ^ sICBlockCipher.f[i4]);
            }
            sICBlockCipher.k();
        }
        return sICBlockCipher.c;
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        int i = this.g;
        if (i == 0) {
            i();
            this.b.c(this.e, 0, this.f, 0);
            byte[] bArr = this.f;
            int i2 = this.g;
            this.g = i2 + 1;
            return (byte) (bArr[i2] ^ b);
        }
        byte[] bArr2 = this.f;
        int i3 = i + 1;
        this.g = i3;
        byte b2 = (byte) (b ^ bArr2[i]);
        if (i3 == this.e.length) {
            this.g = 0;
            k();
        }
        return b2;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.b.getAlgorithmName() + "/SIC";
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long getPosition() {
        byte[] bArr = this.e;
        int length = bArr.length;
        byte[] bArr2 = new byte[length];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, length);
        int i = length - 1;
        while (i >= 1) {
            byte[] bArr3 = this.d;
            int i2 = i < bArr3.length ? (bArr2[i] & 255) - (bArr3[i] & 255) : bArr2[i] & 255;
            if (i2 < 0) {
                int i3 = i - 1;
                bArr2[i3] = (byte) (bArr2[i3] - 1);
                i2 += 256;
            }
            bArr2[i] = (byte) i2;
            i--;
        }
        return (org.bouncycastle.util.Pack.c(bArr2, length - 8) * ((long) this.c)) + ((long) this.g);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("CTR/SIC mode requires ParametersWithIV");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrJ = org.bouncycastle.util.Arrays.j(parametersWithIV.a());
        this.d = bArrJ;
        int i = this.c;
        if (i < bArrJ.length) {
            throw new java.lang.IllegalArgumentException("CTR/SIC mode requires IV no greater than: " + this.c + " bytes.");
        }
        int i2 = 8 > i / 2 ? i / 2 : 8;
        if (i - bArrJ.length <= i2) {
            if (parametersWithIV.b() != null) {
                this.b.init(true, parametersWithIV.b());
            }
            reset();
        } else {
            throw new java.lang.IllegalArgumentException("CTR/SIC mode requires IV of at least: " + (this.c - i2) + " bytes.");
        }
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher, org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        byte b;
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too small");
        }
        if (i3 + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            int i5 = this.g;
            if (i5 == 0) {
                i();
                this.b.c(this.e, 0, this.f, 0);
                byte b2 = bArr[i + i4];
                byte[] bArr3 = this.f;
                int i6 = this.g;
                this.g = i6 + 1;
                b = (byte) (b2 ^ bArr3[i6]);
            } else {
                byte b3 = bArr[i + i4];
                byte[] bArr4 = this.f;
                int i7 = i5 + 1;
                this.g = i7;
                b = (byte) (bArr4[i5] ^ b3);
                if (i7 == this.e.length) {
                    this.g = 0;
                    k();
                }
            }
            bArr2[i3 + i4] = b;
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        org.bouncycastle.util.Arrays.G(this.e, (byte) 0);
        byte[] bArr = this.d;
        java.lang.System.arraycopy(bArr, 0, this.e, 0, bArr.length);
        this.b.reset();
        this.g = 0;
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long seekTo(long j) {
        reset();
        return skip(j);
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long skip(long j) {
        g(j);
        h();
        this.b.c(this.e, 0, this.f, 0);
        return j;
    }
}
