package org.bouncycastle.crypto.prng.drbg;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class HMacSP800DRBG implements org.bouncycastle.crypto.prng.drbg.SP80090DRBG {
    private byte[] a;
    private byte[] b;
    private long c;
    private org.bouncycastle.crypto.prng.EntropySource d;
    private org.bouncycastle.crypto.Mac e;
    private int f;

    public HMacSP800DRBG(org.bouncycastle.crypto.Mac mac, int i, org.bouncycastle.crypto.prng.EntropySource entropySource, byte[] bArr, byte[] bArr2) {
        if (i > org.bouncycastle.crypto.prng.drbg.Utils.b(mac)) {
            throw new java.lang.IllegalArgumentException("Requested security strength is not supported by the derivation function");
        }
        if (entropySource.entropySize() < i) {
            throw new java.lang.IllegalArgumentException("Not enough entropy for security strength required");
        }
        this.f = i;
        this.d = entropySource;
        this.e = mac;
        byte[] bArrU = org.bouncycastle.util.Arrays.u(c(), bArr2, bArr);
        byte[] bArr3 = new byte[mac.getMacSize()];
        this.a = bArr3;
        byte[] bArr4 = new byte[bArr3.length];
        this.b = bArr4;
        org.bouncycastle.util.Arrays.G(bArr4, (byte) 1);
        d(bArrU);
        this.c = 1L;
    }

    private byte[] c() {
        byte[] entropy = this.d.getEntropy();
        if (entropy.length >= (this.f + 7) / 8) {
            return entropy;
        }
        throw new java.lang.IllegalStateException("Insufficient entropy provided by entropy source");
    }

    private void d(byte[] bArr) {
        e(bArr, (byte) 0);
        if (bArr != null) {
            e(bArr, (byte) 1);
        }
    }

    private void e(byte[] bArr, byte b) {
        this.e.init(new org.bouncycastle.crypto.params.KeyParameter(this.a));
        org.bouncycastle.crypto.Mac mac = this.e;
        byte[] bArr2 = this.b;
        mac.update(bArr2, 0, bArr2.length);
        this.e.update(b);
        if (bArr != null) {
            this.e.update(bArr, 0, bArr.length);
        }
        this.e.doFinal(this.a, 0);
        this.e.init(new org.bouncycastle.crypto.params.KeyParameter(this.a));
        org.bouncycastle.crypto.Mac mac2 = this.e;
        byte[] bArr3 = this.b;
        mac2.update(bArr3, 0, bArr3.length);
        this.e.doFinal(this.b, 0);
    }

    @Override // org.bouncycastle.crypto.prng.drbg.SP80090DRBG
    public int a(byte[] bArr, byte[] bArr2, boolean z) {
        int length = bArr.length * 8;
        if (length > 262144) {
            throw new java.lang.IllegalArgumentException("Number of bits per request limited to 262144");
        }
        if (this.c > 140737488355328L) {
            return -1;
        }
        if (z) {
            b(bArr2);
            bArr2 = null;
        }
        if (bArr2 != null) {
            d(bArr2);
        }
        int length2 = bArr.length;
        byte[] bArr3 = new byte[length2];
        int length3 = bArr.length / this.b.length;
        this.e.init(new org.bouncycastle.crypto.params.KeyParameter(this.a));
        for (int i = 0; i < length3; i++) {
            org.bouncycastle.crypto.Mac mac = this.e;
            byte[] bArr4 = this.b;
            mac.update(bArr4, 0, bArr4.length);
            this.e.doFinal(this.b, 0);
            byte[] bArr5 = this.b;
            java.lang.System.arraycopy(bArr5, 0, bArr3, bArr5.length * i, bArr5.length);
        }
        byte[] bArr6 = this.b;
        if (bArr6.length * length3 < length2) {
            this.e.update(bArr6, 0, bArr6.length);
            this.e.doFinal(this.b, 0);
            byte[] bArr7 = this.b;
            java.lang.System.arraycopy(bArr7, 0, bArr3, bArr7.length * length3, length2 - (length3 * bArr7.length));
        }
        d(bArr2);
        this.c++;
        java.lang.System.arraycopy(bArr3, 0, bArr, 0, bArr.length);
        return length;
    }

    @Override // org.bouncycastle.crypto.prng.drbg.SP80090DRBG
    public void b(byte[] bArr) {
        d(org.bouncycastle.util.Arrays.t(c(), bArr));
        this.c = 1L;
    }
}
