package org.bouncycastle.crypto.digests;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class Blake2xsDigest implements org.bouncycastle.crypto.Xof {
    private int a;
    private org.bouncycastle.crypto.digests.Blake2sDigest b;
    private byte[] c;
    private byte[] d;
    private int e;
    private int f;
    private long g;
    private long h;

    private long a() {
        return ((long) this.a) * 4294967296L;
    }

    private int d() {
        int i = this.a;
        if (i == 65535) {
            return 32;
        }
        return java.lang.Math.min(32, i - this.f);
    }

    @Override // org.bouncycastle.crypto.Xof
    public int b(byte[] bArr, int i, int i2) {
        int iC = c(bArr, i, i2);
        reset();
        return iC;
    }

    @Override // org.bouncycastle.crypto.Xof
    public int c(byte[] bArr, int i, int i2) {
        if (i > bArr.length - i2) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        if (this.c == null) {
            byte[] bArr2 = new byte[this.b.getDigestSize()];
            this.c = bArr2;
            this.b.doFinal(bArr2, 0);
        }
        int i3 = this.a;
        if (i3 != 65535) {
            if (this.f + i2 > i3) {
                throw new java.lang.IllegalArgumentException("Output length is above the digest length");
            }
        } else if ((this.g << 5) >= e()) {
            throw new java.lang.IllegalArgumentException("Maximum length is 2^32 blocks of 32 bytes");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            if (this.e >= 32) {
                org.bouncycastle.crypto.digests.Blake2sDigest blake2sDigest = new org.bouncycastle.crypto.digests.Blake2sDigest(d(), 32, this.h);
                byte[] bArr3 = this.c;
                blake2sDigest.update(bArr3, 0, bArr3.length);
                org.bouncycastle.util.Arrays.G(this.d, (byte) 0);
                blake2sDigest.doFinal(this.d, 0);
                this.e = 0;
                this.h++;
                this.g++;
            }
            byte[] bArr4 = this.d;
            int i5 = this.e;
            bArr[i + i4] = bArr4[i5];
            this.e = i5 + 1;
            this.f++;
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.Digest
    public int doFinal(byte[] bArr, int i) {
        return b(bArr, i, this.a);
    }

    public long e() {
        return 137438953472L;
    }

    @Override // org.bouncycastle.crypto.Digest
    public java.lang.String getAlgorithmName() {
        return "BLAKE2xs";
    }

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return this.b.getByteLength();
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return this.a;
    }

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this.b.reset();
        this.c = null;
        this.e = 32;
        this.f = 0;
        this.g = 0L;
        this.h = a();
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        this.b.update(b);
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        this.b.update(bArr, i, i2);
    }
}
