package org.bouncycastle.crypto.digests;

import org.bouncycastle.crypto.Xof;

/* JADX INFO: loaded from: classes.dex */
abstract class AsconXofBase extends AsconBaseDigest implements Xof {
    private final byte[] buffer = new byte[this.BlockSize];
    private int bytesInBuffer;
    private boolean m_squeezing;

    AsconXofBase() {
    }

    private void ensureNoAbsorbWhileSqueezing(boolean z) {
        if (z) {
            throw new IllegalStateException("attempt to absorb while squeezing");
        }
    }

    public int doFinal(byte[] bArr, int i, int i2) {
        int iDoOutput = doOutput(bArr, i, i2);
        reset();
        return iDoOutput;
    }

    public int doOutput(byte[] bArr, int i, int i2) {
        int iHash;
        ensureSufficientOutputBuffer(bArr, i, i2);
        if (this.bytesInBuffer != 0) {
            int i3 = this.BlockSize - this.bytesInBuffer;
            int iMin = Math.min(i2, this.bytesInBuffer);
            System.arraycopy(this.buffer, i3, bArr, i, iMin);
            this.bytesInBuffer -= iMin;
            iHash = iMin + 0;
        } else {
            iHash = 0;
        }
        int i4 = i2 - iHash;
        if (i4 >= this.BlockSize) {
            iHash += hash(bArr, i + iHash, i4 - (i4 % this.BlockSize));
        }
        if (iHash >= i2) {
            return iHash;
        }
        hash(this.buffer, 0, this.BlockSize);
        int i5 = i2 - iHash;
        System.arraycopy(this.buffer, 0, bArr, i + iHash, i5);
        this.bytesInBuffer = this.buffer.length - i5;
        return iHash + i5;
    }

    @Override // org.bouncycastle.crypto.digests.AsconBaseDigest
    protected void padAndAbsorb() {
        if (this.m_squeezing) {
            this.p.p(this.ASCON_PB_ROUNDS);
        } else {
            this.m_squeezing = true;
            super.padAndAbsorb();
        }
    }

    @Override // org.bouncycastle.crypto.digests.BufferBaseDigest, org.bouncycastle.crypto.Digest
    public void reset() {
        this.m_squeezing = false;
        this.bytesInBuffer = 0;
        super.reset();
    }

    @Override // org.bouncycastle.crypto.digests.BufferBaseDigest, org.bouncycastle.crypto.Digest
    public void update(byte b) {
        ensureNoAbsorbWhileSqueezing(this.m_squeezing);
        super.update(b);
    }

    @Override // org.bouncycastle.crypto.digests.BufferBaseDigest, org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        ensureNoAbsorbWhileSqueezing(this.m_squeezing);
        super.update(bArr, i, i2);
    }
}
