package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class SipHash implements org.bouncycastle.crypto.Mac {
    protected final int a;
    protected final int b;
    protected long c;
    protected long d;
    protected long e;
    protected long f;
    protected long g;
    protected long h;
    protected long i;
    protected int j;
    protected int k;

    public SipHash() {
        this.i = 0L;
        this.j = 0;
        this.k = 0;
        this.a = 2;
        this.b = 4;
    }

    protected static long d(long j, int i) {
        return (j >>> (-i)) | (j << i);
    }

    protected void a(int i) {
        long jD = this.e;
        long jD2 = this.f;
        long jD3 = this.g;
        long jD4 = this.h;
        for (int i2 = 0; i2 < i; i2++) {
            long j = jD + jD2;
            long j2 = jD3 + jD4;
            long jD5 = d(jD2, 13) ^ j;
            long jD6 = d(jD4, 16) ^ j2;
            long j3 = j2 + jD5;
            jD = d(j, 32) + jD6;
            jD2 = d(jD5, 17) ^ j3;
            jD4 = d(jD6, 21) ^ jD;
            jD3 = d(j3, 32);
        }
        this.e = jD;
        this.f = jD2;
        this.g = jD3;
        this.h = jD4;
    }

    public long b() {
        long j = this.i;
        int i = this.j;
        this.i = ((j >>> ((7 - i) << 3)) >>> 8) | ((((long) ((this.k << 3) + i)) & 255) << 56);
        c();
        this.g ^= 255;
        a(this.b);
        long j2 = ((this.e ^ this.f) ^ this.g) ^ this.h;
        reset();
        return j2;
    }

    protected void c() {
        this.k++;
        this.h ^= this.i;
        a(this.a);
        this.e ^= this.i;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        org.bouncycastle.util.Pack.H(b(), bArr, i);
        return 8;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "SipHash-" + this.a + "-" + this.b;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return 8;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("'params' must be an instance of KeyParameter");
        }
        byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b();
        if (bArrB.length != 16) {
            throw new java.lang.IllegalArgumentException("'params' must be a 128-bit key");
        }
        this.c = org.bouncycastle.util.Pack.v(bArrB, 0);
        this.d = org.bouncycastle.util.Pack.v(bArrB, 8);
        reset();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        long j = this.c;
        this.e = 8317987319222330741L ^ j;
        long j2 = this.d;
        this.f = 7237128888997146477L ^ j2;
        this.g = j ^ 7816392313619706465L;
        this.h = 8387220255154660723L ^ j2;
        this.i = 0L;
        this.j = 0;
        this.k = 0;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        this.i = (this.i >>> 8) | ((((long) b) & 255) << 56);
        int i = this.j + 1;
        this.j = i;
        if (i == 8) {
            c();
            this.j = 0;
        }
    }

    public SipHash(int i, int i2) {
        this.i = 0L;
        this.j = 0;
        this.k = 0;
        this.a = i;
        this.b = i2;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        int i3 = i2 & (-8);
        int i4 = this.j;
        char c = '8';
        long j = 255;
        int i5 = 0;
        if (i4 == 0) {
            while (i5 < i3) {
                this.i = org.bouncycastle.util.Pack.v(bArr, i + i5);
                c();
                i5 += 8;
            }
            while (i5 < i2) {
                long j2 = this.i >>> 8;
                this.i = j2;
                this.i = j2 | ((((long) bArr[i + i5]) & 255) << 56);
                i5++;
            }
            this.j = i2 - i3;
            return;
        }
        int i6 = i4 << 3;
        int i7 = 0;
        while (i7 < i3) {
            long jV = org.bouncycastle.util.Pack.v(bArr, i + i7);
            this.i = (this.i >>> (-i6)) | (jV << i6);
            c();
            this.i = jV;
            i7 += 8;
            c = c;
            j = j;
        }
        char c2 = c;
        long j3 = j;
        while (i7 < i2) {
            long j4 = this.i >>> 8;
            this.i = j4;
            this.i = j4 | ((((long) bArr[i + i7]) & j3) << c2);
            int i8 = this.j + 1;
            this.j = i8;
            if (i8 == 8) {
                c();
                this.j = 0;
            }
            i7++;
        }
    }
}
