package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSTU7564Mac implements org.bouncycastle.crypto.Mac {
    private org.bouncycastle.crypto.digests.DSTU7564Digest a;
    private int b;
    private byte[] c = null;
    private byte[] d = null;
    private long e;

    public DSTU7564Mac(int i) {
        this.a = new org.bouncycastle.crypto.digests.DSTU7564Digest(i);
        this.b = i / 8;
    }

    private void a() {
        int byteLength = this.a.getByteLength() - ((int) (this.e % ((long) this.a.getByteLength())));
        if (byteLength < 13) {
            byteLength += this.a.getByteLength();
        }
        byte[] bArr = new byte[byteLength];
        bArr[0] = -128;
        org.bouncycastle.util.Pack.H(this.e * 8, bArr, byteLength - 12);
        this.a.update(bArr, 0, byteLength);
    }

    private byte[] b(byte[] bArr) {
        int length = (((bArr.length + this.a.getByteLength()) - 1) / this.a.getByteLength()) * this.a.getByteLength();
        if (length - bArr.length < 13) {
            length += this.a.getByteLength();
        }
        byte[] bArr2 = new byte[length];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
        bArr2[bArr.length] = -128;
        org.bouncycastle.util.Pack.l(bArr.length * 8, bArr2, length - 12);
        return bArr2;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        if (this.c == null) {
            throw new java.lang.IllegalStateException(getAlgorithmName() + " not initialised");
        }
        if (bArr.length - i < this.b) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        a();
        org.bouncycastle.crypto.digests.DSTU7564Digest dSTU7564Digest = this.a;
        byte[] bArr2 = this.d;
        dSTU7564Digest.update(bArr2, 0, bArr2.length);
        this.e = 0L;
        int iDoFinal = this.a.doFinal(bArr, i);
        reset();
        return iDoFinal;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "DSTU7564Mac";
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return this.b;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.c = null;
        reset();
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("Bad parameter passed");
        }
        byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b();
        this.d = new byte[bArrB.length];
        this.c = b(bArrB);
        int i = 0;
        while (true) {
            byte[] bArr = this.d;
            if (i >= bArr.length) {
                org.bouncycastle.crypto.digests.DSTU7564Digest dSTU7564Digest = this.a;
                byte[] bArr2 = this.c;
                dSTU7564Digest.update(bArr2, 0, bArr2.length);
                return;
            }
            bArr[i] = (byte) (~bArrB[i]);
            i++;
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        this.e = 0L;
        this.a.reset();
        byte[] bArr = this.c;
        if (bArr != null) {
            this.a.update(bArr, 0, bArr.length);
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        this.a.update(b);
        this.e++;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        if (bArr.length - i < i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        if (this.c != null) {
            this.a.update(bArr, i, i2);
            this.e += (long) i2;
        } else {
            throw new java.lang.IllegalStateException(getAlgorithmName() + " not initialised");
        }
    }
}
