package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSTU7624Mac implements org.bouncycastle.crypto.Mac {
    private byte[] a;
    private int b;
    private int c;
    private int d;
    private org.bouncycastle.crypto.engines.DSTU7624Engine e;
    private byte[] f;
    private byte[] g;
    private byte[] h;
    private boolean i;

    private void a(byte[] bArr, int i) {
        b(this.f, 0, bArr, i, this.g);
        this.e.c(this.g, 0, this.f, 0);
    }

    private void b(byte[] bArr, int i, byte[] bArr2, int i2, byte[] bArr3) {
        int length = bArr.length - i;
        int i3 = this.d;
        if (length < i3 || bArr2.length - i2 < i3 || bArr3.length < i3) {
            throw new java.lang.IllegalArgumentException("some of input buffers too short");
        }
        for (int i4 = 0; i4 < this.d; i4++) {
            bArr3[i4] = (byte) (bArr[i4 + i] ^ bArr2[i4 + i2]);
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        int i2 = this.b;
        byte[] bArr2 = this.a;
        if (i2 % bArr2.length != 0) {
            throw new org.bouncycastle.crypto.DataLengthException("input must be a multiple of blocksize");
        }
        b(this.f, 0, bArr2, 0, this.g);
        b(this.g, 0, this.h, 0, this.f);
        org.bouncycastle.crypto.engines.DSTU7624Engine dSTU7624Engine = this.e;
        byte[] bArr3 = this.f;
        dSTU7624Engine.c(bArr3, 0, bArr3, 0);
        int i3 = this.c;
        if (i3 + i > bArr.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        java.lang.System.arraycopy(this.f, 0, bArr, i, i3);
        reset();
        return this.c;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "DSTU7624Mac";
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return this.c;
    }

    @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("Invalid parameter passed to DSTU7624Mac");
        }
        this.e.init(true, cipherParameters);
        this.i = true;
        reset();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        org.bouncycastle.util.Arrays.G(this.f, (byte) 0);
        org.bouncycastle.util.Arrays.G(this.g, (byte) 0);
        org.bouncycastle.util.Arrays.G(this.h, (byte) 0);
        org.bouncycastle.util.Arrays.G(this.a, (byte) 0);
        this.e.reset();
        if (this.i) {
            org.bouncycastle.crypto.engines.DSTU7624Engine dSTU7624Engine = this.e;
            byte[] bArr = this.h;
            dSTU7624Engine.c(bArr, 0, bArr, 0);
        }
        this.b = 0;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        int i = this.b;
        byte[] bArr = this.a;
        if (i == bArr.length) {
            a(bArr, 0);
            this.b = 0;
        }
        byte[] bArr2 = this.a;
        int i2 = this.b;
        this.b = i2 + 1;
        bArr2[i2] = b;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("can't have a negative input length!");
        }
        int iB = this.e.b();
        int i3 = this.b;
        int i4 = iB - i3;
        if (i2 > i4) {
            java.lang.System.arraycopy(bArr, i, this.a, i3, i4);
            a(this.a, 0);
            this.b = 0;
            i2 -= i4;
            i += i4;
            while (i2 > iB) {
                a(bArr, i);
                i2 -= iB;
                i += iB;
            }
        }
        java.lang.System.arraycopy(bArr, i, this.a, this.b, i2);
        this.b += i2;
    }
}
