package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class HC128Engine implements org.bouncycastle.crypto.StreamCipher {
    private byte[] d;
    private byte[] e;
    private boolean f;
    private int[] a = new int[512];
    private int[] b = new int[512];
    private int c = 0;
    private byte[] g = new byte[4];
    private int h = 0;

    private static int a(int i, int i2) {
        return l(i - i2);
    }

    private static int b(int i) {
        return (i >>> 3) ^ (n(i, 7) ^ n(i, 18));
    }

    private static int c(int i) {
        return (i >>> 10) ^ (n(i, 17) ^ n(i, 19));
    }

    private int d(int i, int i2, int i3) {
        return (n(i, 10) ^ n(i3, 23)) + n(i2, 8);
    }

    private int f(int i, int i2, int i3) {
        return (m(i, 10) ^ m(i3, 23)) + m(i2, 8);
    }

    private byte g() {
        if (this.h == 0) {
            int iO = o();
            byte[] bArr = this.g;
            bArr[0] = (byte) (iO & 255);
            bArr[1] = (byte) ((iO >> 8) & 255);
            bArr[2] = (byte) ((iO >> 16) & 255);
            bArr[3] = (byte) ((iO >> 24) & 255);
        }
        byte[] bArr2 = this.g;
        int i = this.h;
        byte b = bArr2[i];
        this.h = 3 & (i + 1);
        return b;
    }

    private int h(int i) {
        int[] iArr = this.b;
        return iArr[i & 255] + iArr[((i >> 16) & 255) + 256];
    }

    private int i(int i) {
        int[] iArr = this.a;
        return iArr[i & 255] + iArr[((i >> 16) & 255) + 256];
    }

    private void j() {
        if (this.d.length != 16) {
            throw new java.lang.IllegalArgumentException("The key must be 128 bits long");
        }
        if (this.e.length != 16) {
            throw new java.lang.IllegalArgumentException("The IV must be 128 bits long");
        }
        this.h = 0;
        this.c = 0;
        int[] iArr = new int[1280];
        for (int i = 0; i < 16; i++) {
            int i2 = i >> 2;
            iArr[i2] = ((this.d[i] & 255) << ((i & 3) * 8)) | iArr[i2];
        }
        java.lang.System.arraycopy(iArr, 0, iArr, 4, 4);
        int i3 = 0;
        while (true) {
            byte[] bArr = this.e;
            if (i3 >= bArr.length || i3 >= 16) {
                break;
            }
            int i4 = (i3 >> 2) + 8;
            iArr[i4] = ((bArr[i3] & 255) << ((i3 & 3) * 8)) | iArr[i4];
            i3++;
        }
        java.lang.System.arraycopy(iArr, 8, iArr, 12, 4);
        for (int i5 = 16; i5 < 1280; i5++) {
            iArr[i5] = c(iArr[i5 - 2]) + iArr[i5 - 7] + b(iArr[i5 - 15]) + iArr[i5 - 16] + i5;
        }
        java.lang.System.arraycopy(iArr, 256, this.a, 0, 512);
        java.lang.System.arraycopy(iArr, 768, this.b, 0, 512);
        for (int i6 = 0; i6 < 512; i6++) {
            this.a[i6] = o();
        }
        for (int i7 = 0; i7 < 512; i7++) {
            this.b[i7] = o();
        }
        this.c = 0;
    }

    private static int k(int i) {
        return i & 1023;
    }

    private static int l(int i) {
        return i & 511;
    }

    private static int m(int i, int i2) {
        return (i >>> (-i2)) | (i << i2);
    }

    private static int n(int i, int i2) {
        return (i << (-i2)) | (i >>> i2);
    }

    private int o() {
        int i;
        int i2;
        int iL = l(this.c);
        if (this.c < 512) {
            int[] iArr = this.a;
            iArr[iL] = iArr[iL] + d(iArr[a(iL, 3)], this.a[a(iL, 10)], this.a[a(iL, 511)]);
            i = h(this.a[a(iL, 12)]);
            i2 = this.a[iL];
        } else {
            int[] iArr2 = this.b;
            iArr2[iL] = iArr2[iL] + f(iArr2[a(iL, 3)], this.b[a(iL, 10)], this.b[a(iL, 511)]);
            i = i(this.b[a(iL, 12)]);
            i2 = this.b[iL];
        }
        int i3 = i2 ^ i;
        this.c = k(this.c + 1);
        return i3;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public byte e(byte b) {
        return (byte) (g() ^ b);
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public java.lang.String getAlgorithmName() {
        return "HC-128";
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("no IV passed");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        this.e = parametersWithIV.a();
        org.bouncycastle.crypto.CipherParameters cipherParametersB = parametersWithIV.b();
        if (!(cipherParametersB instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("Invalid parameter passed to HC128 init - " + cipherParameters.getClass().getName());
        }
        this.d = ((org.bouncycastle.crypto.params.KeyParameter) cipherParametersB).b();
        j();
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(getAlgorithmName(), 128, cipherParameters, org.bouncycastle.crypto.engines.Utils.a(z)));
        this.f = true;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (!this.f) {
            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++) {
            bArr2[i3 + i4] = (byte) (bArr[i + i4] ^ g());
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void reset() {
        j();
    }
}
