package org.bouncycastle.crypto.prng;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class X931RNG {
    private final org.bouncycastle.crypto.BlockCipher a;
    private final org.bouncycastle.crypto.prng.EntropySource b;
    private final byte[] c;
    private final byte[] d;
    private final byte[] e;
    private byte[] f;
    private long g;

    private void c(byte[] bArr) {
        for (int length = bArr.length - 1; length >= 0; length--) {
            byte b = (byte) (bArr[length] + 1);
            bArr[length] = b;
            if (b != 0) {
                return;
            }
        }
    }

    private static boolean d(byte[] bArr, int i) {
        return bArr != null && bArr.length > i;
    }

    private void e(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        for (int i = 0; i != bArr.length; i++) {
            bArr[i] = (byte) (bArr2[i] ^ bArr3[i]);
        }
        this.a.c(bArr, 0, bArr, 0);
    }

    int a(byte[] bArr, boolean z) {
        int length = this.e.length;
        long j = this.g;
        if (length == 8) {
            if (j > 32768) {
                return -1;
            }
            if (d(bArr, 512)) {
                throw new java.lang.IllegalArgumentException("Number of bits per request limited to 4096");
            }
        } else {
            if (j > 8388608) {
                return -1;
            }
            if (d(bArr, 32768)) {
                throw new java.lang.IllegalArgumentException("Number of bits per request limited to 262144");
            }
        }
        if (z || this.f == null) {
            byte[] entropy = this.b.getEntropy();
            this.f = entropy;
            if (entropy.length != this.a.b()) {
                throw new java.lang.IllegalStateException("Insufficient entropy returned");
            }
        }
        int length2 = bArr.length / this.e.length;
        for (int i = 0; i < length2; i++) {
            this.a.c(this.c, 0, this.d, 0);
            e(this.e, this.d, this.f);
            e(this.f, this.e, this.d);
            byte[] bArr2 = this.e;
            java.lang.System.arraycopy(bArr2, 0, bArr, bArr2.length * i, bArr2.length);
            c(this.c);
        }
        int length3 = bArr.length - (this.e.length * length2);
        if (length3 > 0) {
            this.a.c(this.c, 0, this.d, 0);
            e(this.e, this.d, this.f);
            e(this.f, this.e, this.d);
            byte[] bArr3 = this.e;
            java.lang.System.arraycopy(bArr3, 0, bArr, length2 * bArr3.length, length3);
            c(this.c);
        }
        this.g++;
        return bArr.length * 8;
    }

    org.bouncycastle.crypto.prng.EntropySource b() {
        return this.b;
    }

    void f() {
        byte[] entropy = this.b.getEntropy();
        this.f = entropy;
        if (entropy.length != this.a.b()) {
            throw new java.lang.IllegalStateException("Insufficient entropy returned");
        }
        this.g = 1L;
    }
}
