package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public final class Zuc128Mac implements org.bouncycastle.crypto.Mac {
    private int b;
    private org.bouncycastle.crypto.engines.Zuc128CoreEngine d;
    private int e;
    private int f;
    private final org.bouncycastle.crypto.macs.Zuc128Mac.InternalZuc128Engine a = new org.bouncycastle.crypto.macs.Zuc128Mac.InternalZuc128Engine();
    private final int[] c = new int[2];

    private static class InternalZuc128Engine extends org.bouncycastle.crypto.engines.Zuc128CoreEngine {
        private InternalZuc128Engine() {
        }

        int u() {
            return super.r();
        }
    }

    private int a() {
        if (this.f != 0) {
            return this.a.u();
        }
        int i = this.e + 1;
        int[] iArr = this.c;
        int length = i % iArr.length;
        this.e = length;
        return iArr[length];
    }

    private int b(int i) {
        int[] iArr = this.c;
        int i2 = this.e;
        int i3 = iArr[i2];
        if (i == 0) {
            return i3;
        }
        int i4 = iArr[(i2 + 1) % iArr.length];
        return (i4 >>> (32 - i)) | (i3 << i);
    }

    private void c() {
        int i = 0;
        this.b = 0;
        while (true) {
            int[] iArr = this.c;
            if (i >= iArr.length - 1) {
                this.e = iArr.length - 1;
                this.f = 3;
                return;
            } else {
                iArr[i] = this.a.u();
                i++;
            }
        }
    }

    private void d() {
        int i = (this.f + 1) % 4;
        this.f = i;
        if (i == 0) {
            this.c[this.e] = this.a.u();
            this.e = (this.e + 1) % this.c.length;
        }
    }

    private void e(int i) {
        this.b = b(i) ^ this.b;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        d();
        int iB = this.b ^ b(this.f * 8);
        this.b = iB;
        int iA = iB ^ a();
        this.b = iA;
        org.bouncycastle.crypto.engines.Zuc128CoreEngine.o(iA, bArr, i);
        reset();
        return getMacSize();
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "Zuc128Mac";
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return 4;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.a.init(true, cipherParameters);
        this.d = (org.bouncycastle.crypto.engines.Zuc128CoreEngine) this.a.a();
        c();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        org.bouncycastle.crypto.engines.Zuc128CoreEngine zuc128CoreEngine = this.d;
        if (zuc128CoreEngine != null) {
            this.a.d(zuc128CoreEngine);
        }
        c();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        d();
        int i = this.f * 8;
        int i2 = 128;
        int i3 = 0;
        while (i2 > 0) {
            if ((b & i2) != 0) {
                e(i + i3);
            }
            i2 >>= 1;
            i3++;
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        for (int i3 = 0; i3 < i2; i3++) {
            update(bArr[i + i3]);
        }
    }
}
