package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class KMAC implements org.bouncycastle.crypto.Mac, org.bouncycastle.crypto.Xof {
    private static final byte[] g = new byte[100];
    private final org.bouncycastle.crypto.digests.CSHAKEDigest a;
    private final int b;
    private final int c;
    private byte[] d;
    private boolean e;
    private boolean f;

    public KMAC(int i, byte[] bArr) {
        this.a = new org.bouncycastle.crypto.digests.CSHAKEDigest(i, org.bouncycastle.util.Strings.f("KMAC"), bArr);
        this.b = i;
        this.c = (i * 2) / 8;
    }

    private void a(byte[] bArr, int i) {
        byte[] bArrC = org.bouncycastle.crypto.digests.XofUtils.c(i);
        update(bArrC, 0, bArrC.length);
        byte[] bArrD = d(bArr);
        update(bArrD, 0, bArrD.length);
        int length = i - ((bArrC.length + bArrD.length) % i);
        if (length <= 0 || length == i) {
            return;
        }
        while (true) {
            byte[] bArr2 = g;
            if (length <= bArr2.length) {
                update(bArr2, 0, length);
                return;
            } else {
                update(bArr2, 0, bArr2.length);
                length -= bArr2.length;
            }
        }
    }

    private static byte[] d(byte[] bArr) {
        return org.bouncycastle.util.Arrays.t(org.bouncycastle.crypto.digests.XofUtils.c(bArr.length * 8), bArr);
    }

    @Override // org.bouncycastle.crypto.Xof
    public int b(byte[] bArr, int i, int i2) {
        if (this.f) {
            if (!this.e) {
                throw new java.lang.IllegalStateException("KMAC not initialized");
            }
            byte[] bArrD = org.bouncycastle.crypto.digests.XofUtils.d(i2 * 8);
            this.a.update(bArrD, 0, bArrD.length);
        }
        int iB = this.a.b(bArr, i, i2);
        reset();
        return iB;
    }

    @Override // org.bouncycastle.crypto.Xof
    public int c(byte[] bArr, int i, int i2) {
        if (this.f) {
            if (!this.e) {
                throw new java.lang.IllegalStateException("KMAC not initialized");
            }
            byte[] bArrD = org.bouncycastle.crypto.digests.XofUtils.d(0L);
            this.a.update(bArrD, 0, bArrD.length);
            this.f = false;
        }
        return this.a.c(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        if (this.f) {
            if (!this.e) {
                throw new java.lang.IllegalStateException("KMAC not initialized");
            }
            byte[] bArrD = org.bouncycastle.crypto.digests.XofUtils.d(getMacSize() * 8);
            this.a.update(bArrD, 0, bArrD.length);
        }
        int iB = this.a.b(bArr, i, getMacSize());
        reset();
        return iB;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "KMAC" + this.a.getAlgorithmName().substring(6);
    }

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return this.a.getByteLength();
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return this.c;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return this.c;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.d = org.bouncycastle.util.Arrays.j(((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b());
        this.e = true;
        reset();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        this.a.reset();
        byte[] bArr = this.d;
        if (bArr != null) {
            a(bArr, this.b == 128 ? 168 : 136);
        }
        this.f = true;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        if (!this.e) {
            throw new java.lang.IllegalStateException("KMAC not initialized");
        }
        this.a.update(b);
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        if (!this.e) {
            throw new java.lang.IllegalStateException("KMAC not initialized");
        }
        this.a.update(bArr, i, i2);
    }
}
