package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ISAACEngine implements org.bouncycastle.crypto.StreamCipher {
    private int[] a;
    private int[] b;
    private int c;
    private int d;
    private int e;
    private int f;
    private byte[] g;
    private byte[] h;
    private boolean i;

    private void a() {
        int i;
        int i2;
        int i3 = this.d;
        int i4 = this.e + 1;
        this.e = i4;
        this.d = i3 + i4;
        for (int i5 = 0; i5 < 256; i5++) {
            int[] iArr = this.a;
            int i6 = iArr[i5];
            int i7 = i5 & 3;
            if (i7 == 0) {
                i = this.c;
                i2 = i << 13;
            } else if (i7 == 1) {
                i = this.c;
                i2 = i >>> 6;
            } else if (i7 != 2) {
                if (i7 == 3) {
                    i = this.c;
                    i2 = i >>> 16;
                }
                int i8 = this.c + iArr[(i5 + 128) & 255];
                this.c = i8;
                int i9 = iArr[(i6 >>> 2) & 255] + i8 + this.d;
                iArr[i5] = i9;
                int[] iArr2 = this.b;
                int i10 = iArr[(i9 >>> 10) & 255] + i6;
                this.d = i10;
                iArr2[i5] = i10;
            } else {
                i = this.c;
                i2 = i << 2;
            }
            this.c = i ^ i2;
            int i11 = this.c + iArr[(i5 + 128) & 255];
            this.c = i11;
            int i12 = iArr[(i6 >>> 2) & 255] + i11 + this.d;
            iArr[i5] = i12;
            int[] iArr3 = this.b;
            int i13 = iArr[(i12 >>> 10) & 255] + i6;
            this.d = i13;
            iArr3[i5] = i13;
        }
    }

    private void b(int[] iArr) {
        int i = iArr[0];
        int i2 = iArr[1];
        int i3 = i ^ (i2 << 11);
        iArr[0] = i3;
        int i4 = iArr[3] + i3;
        iArr[3] = i4;
        int i5 = iArr[2];
        int i6 = i2 + i5;
        iArr[1] = i6;
        int i7 = i6 ^ (i5 >>> 2);
        iArr[1] = i7;
        int i8 = iArr[4] + i7;
        iArr[4] = i8;
        int i9 = i5 + i4;
        iArr[2] = i9;
        int i10 = i9 ^ (i4 << 8);
        iArr[2] = i10;
        int i11 = iArr[5] + i10;
        iArr[5] = i11;
        int i12 = i4 + i8;
        iArr[3] = i12;
        int i13 = i12 ^ (i8 >>> 16);
        iArr[3] = i13;
        int i14 = iArr[6] + i13;
        iArr[6] = i14;
        int i15 = i8 + i11;
        iArr[4] = i15;
        int i16 = (i11 << 10) ^ i15;
        iArr[4] = i16;
        int i17 = iArr[7] + i16;
        iArr[7] = i17;
        int i18 = i11 + i14;
        iArr[5] = i18;
        int i19 = (i14 >>> 4) ^ i18;
        iArr[5] = i19;
        int i20 = i3 + i19;
        iArr[0] = i20;
        int i21 = i14 + i17;
        iArr[6] = i21;
        int i22 = (i17 << 8) ^ i21;
        iArr[6] = i22;
        int i23 = i7 + i22;
        iArr[1] = i23;
        int i24 = i17 + i20;
        iArr[7] = i24;
        int i25 = (i20 >>> 9) ^ i24;
        iArr[7] = i25;
        iArr[2] = i10 + i25;
        iArr[0] = i20 + i23;
    }

    private void c(byte[] bArr) {
        this.h = bArr;
        if (this.a == null) {
            this.a = new int[256];
        }
        if (this.b == null) {
            this.b = new int[256];
        }
        for (int i = 0; i < 256; i++) {
            int[] iArr = this.a;
            this.b[i] = 0;
            iArr[i] = 0;
        }
        this.e = 0;
        this.d = 0;
        this.c = 0;
        this.f = 0;
        int length = bArr.length + (bArr.length & 3);
        byte[] bArr2 = new byte[length];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
        for (int i2 = 0; i2 < length; i2 += 4) {
            this.b[i2 >>> 2] = org.bouncycastle.util.Pack.q(bArr2, i2);
        }
        int[] iArr2 = new int[8];
        for (int i3 = 0; i3 < 8; i3++) {
            iArr2[i3] = -1640531527;
        }
        for (int i4 = 0; i4 < 4; i4++) {
            b(iArr2);
        }
        int i5 = 0;
        while (i5 < 2) {
            for (int i6 = 0; i6 < 256; i6 += 8) {
                for (int i7 = 0; i7 < 8; i7++) {
                    iArr2[i7] = iArr2[i7] + (i5 < 1 ? this.b[i6 + i7] : this.a[i6 + i7]);
                }
                b(iArr2);
                for (int i8 = 0; i8 < 8; i8++) {
                    this.a[i6 + i8] = iArr2[i8];
                }
            }
            i5++;
        }
        a();
        this.i = true;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public byte e(byte b) {
        if (this.f == 0) {
            a();
            this.g = org.bouncycastle.util.Pack.k(this.b);
        }
        byte[] bArr = this.g;
        int i = this.f;
        byte b2 = (byte) (b ^ bArr[i]);
        this.f = (i + 1) & 1023;
        return b2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public java.lang.String getAlgorithmName() {
        return "ISAAC";
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter) {
            byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b();
            c(bArrB);
            org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(getAlgorithmName(), bArrB.length < 32 ? bArrB.length * 8 : 256, cipherParameters, org.bouncycastle.crypto.engines.Utils.a(z)));
        } else {
            throw new java.lang.IllegalArgumentException("invalid parameter passed to ISAAC init - " + cipherParameters.getClass().getName());
        }
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (!this.i) {
            throw new java.lang.IllegalStateException(getAlgorithmName() + " not initialised");
        }
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i3 + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            if (this.f == 0) {
                a();
                this.g = org.bouncycastle.util.Pack.k(this.b);
            }
            byte[] bArr3 = this.g;
            int i5 = this.f;
            bArr2[i4 + i3] = (byte) (bArr3[i5] ^ bArr[i4 + i]);
            this.f = (i5 + 1) & 1023;
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void reset() {
        c(this.h);
    }
}
