package org.bouncycastle.crypto.generators;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class BaseKDFBytesGenerator implements org.bouncycastle.crypto.DigestDerivationFunction {
    private int a;
    private org.bouncycastle.crypto.Digest b;
    private byte[] c;
    private byte[] d;

    protected BaseKDFBytesGenerator(int i, org.bouncycastle.crypto.Digest digest) {
        this.a = i;
        this.b = digest;
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public int generateBytes(byte[] bArr, int i, int i2) {
        int i3 = i2;
        int i4 = i;
        if (bArr.length - i3 < i4) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too small");
        }
        long j = i3;
        int digestSize = this.b.getDigestSize();
        if (j > 8589934591L) {
            throw new java.lang.IllegalArgumentException("Output length too large");
        }
        long j2 = digestSize;
        int i5 = (int) (((j + j2) - 1) / j2);
        byte[] bArr2 = new byte[this.b.getDigestSize()];
        byte[] bArr3 = new byte[4];
        org.bouncycastle.util.Pack.h(this.a, bArr3, 0);
        int i6 = this.a & (-256);
        for (int i7 = 0; i7 < i5; i7++) {
            org.bouncycastle.crypto.Digest digest = this.b;
            byte[] bArr4 = this.c;
            digest.update(bArr4, 0, bArr4.length);
            this.b.update(bArr3, 0, 4);
            byte[] bArr5 = this.d;
            if (bArr5 != null) {
                this.b.update(bArr5, 0, bArr5.length);
            }
            this.b.doFinal(bArr2, 0);
            if (i3 > digestSize) {
                java.lang.System.arraycopy(bArr2, 0, bArr, i4, digestSize);
                i4 += digestSize;
                i3 -= digestSize;
            } else {
                java.lang.System.arraycopy(bArr2, 0, bArr, i4, i3);
            }
            byte b = (byte) (bArr3[3] + 1);
            bArr3[3] = b;
            if (b == 0) {
                i6 += 256;
                org.bouncycastle.util.Pack.h(i6, bArr3, 0);
            }
        }
        this.b.reset();
        return (int) j;
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public void init(org.bouncycastle.crypto.DerivationParameters derivationParameters) {
        if (derivationParameters instanceof org.bouncycastle.crypto.params.KDFParameters) {
            org.bouncycastle.crypto.params.KDFParameters kDFParameters = (org.bouncycastle.crypto.params.KDFParameters) derivationParameters;
            this.c = kDFParameters.b();
            this.d = kDFParameters.a();
        } else {
            if (!(derivationParameters instanceof org.bouncycastle.crypto.params.ISO18033KDFParameters)) {
                throw new java.lang.IllegalArgumentException("KDF parameters required for generator");
            }
            this.c = ((org.bouncycastle.crypto.params.ISO18033KDFParameters) derivationParameters).a();
            this.d = null;
        }
    }
}
