package org.bouncycastle.crypto.prng.drbg;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class HashSP800DRBG implements org.bouncycastle.crypto.prng.drbg.SP80090DRBG {
    private static final byte[] h = {1};
    private static final java.util.Hashtable i;
    private org.bouncycastle.crypto.Digest a;
    private byte[] b;
    private byte[] c;
    private long d;
    private org.bouncycastle.crypto.prng.EntropySource e;
    private int f;
    private int g;

    static {
        java.util.Hashtable hashtable = new java.util.Hashtable();
        i = hashtable;
        hashtable.put("SHA-1", org.bouncycastle.util.Integers.f(440));
        hashtable.put("SHA-224", org.bouncycastle.util.Integers.f(440));
        hashtable.put("SHA-256", org.bouncycastle.util.Integers.f(440));
        hashtable.put("SHA-512/256", org.bouncycastle.util.Integers.f(440));
        hashtable.put("SHA-512/224", org.bouncycastle.util.Integers.f(440));
        hashtable.put("SHA-384", org.bouncycastle.util.Integers.f(888));
        hashtable.put("SHA-512", org.bouncycastle.util.Integers.f(888));
    }

    public HashSP800DRBG(org.bouncycastle.crypto.Digest digest, int i2, org.bouncycastle.crypto.prng.EntropySource entropySource, byte[] bArr, byte[] bArr2) {
        if (i2 > org.bouncycastle.crypto.prng.drbg.Utils.a(digest)) {
            throw new java.lang.IllegalArgumentException("Requested security strength is not supported by the derivation function");
        }
        if (entropySource.entropySize() < i2) {
            throw new java.lang.IllegalArgumentException("Not enough entropy for security strength required");
        }
        this.a = digest;
        this.e = entropySource;
        this.f = i2;
        this.g = ((java.lang.Integer) i.get(digest.getAlgorithmName())).intValue();
        byte[] bArrC = org.bouncycastle.crypto.prng.drbg.Utils.c(this.a, org.bouncycastle.util.Arrays.u(e(), bArr2, bArr), this.g);
        this.b = bArrC;
        byte[] bArr3 = new byte[bArrC.length + 1];
        java.lang.System.arraycopy(bArrC, 0, bArr3, 1, bArrC.length);
        this.c = org.bouncycastle.crypto.prng.drbg.Utils.c(this.a, bArr3, this.g);
        this.d = 1L;
    }

    private void c(byte[] bArr, byte[] bArr2) {
        int i2 = 0;
        for (int i3 = 1; i3 <= bArr2.length; i3++) {
            int i4 = (bArr[bArr.length - i3] & 255) + (bArr2[bArr2.length - i3] & 255) + i2;
            i2 = i4 > 255 ? 1 : 0;
            bArr[bArr.length - i3] = (byte) i4;
        }
        for (int length = bArr2.length + 1; length <= bArr.length; length++) {
            int i5 = (bArr[bArr.length - length] & 255) + i2;
            i2 = i5 > 255 ? 1 : 0;
            bArr[bArr.length - length] = (byte) i5;
        }
    }

    private void d(byte[] bArr, byte[] bArr2) {
        this.a.update(bArr, 0, bArr.length);
        this.a.doFinal(bArr2, 0);
    }

    private byte[] e() {
        byte[] entropy = this.e.getEntropy();
        if (entropy.length >= (this.f + 7) / 8) {
            return entropy;
        }
        throw new java.lang.IllegalStateException("Insufficient entropy provided by entropy source");
    }

    private byte[] f(byte[] bArr) {
        byte[] bArr2 = new byte[this.a.getDigestSize()];
        d(bArr, bArr2);
        return bArr2;
    }

    private byte[] g(byte[] bArr, int i2) {
        int i3 = i2 / 8;
        int digestSize = i3 / this.a.getDigestSize();
        byte[] bArr2 = new byte[bArr.length];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
        byte[] bArr3 = new byte[i3];
        int digestSize2 = this.a.getDigestSize();
        byte[] bArr4 = new byte[digestSize2];
        for (int i4 = 0; i4 <= digestSize; i4++) {
            d(bArr2, bArr4);
            int i5 = i4 * digestSize2;
            int i6 = i3 - i5;
            if (i6 > digestSize2) {
                i6 = digestSize2;
            }
            java.lang.System.arraycopy(bArr4, 0, bArr3, i5, i6);
            c(bArr2, h);
        }
        return bArr3;
    }

    @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.d > 140737488355328L) {
            return -1;
        }
        if (z) {
            b(bArr2);
            bArr2 = null;
        }
        if (bArr2 != null) {
            byte[] bArr3 = this.b;
            byte[] bArr4 = new byte[bArr3.length + 1 + bArr2.length];
            bArr4[0] = 2;
            java.lang.System.arraycopy(bArr3, 0, bArr4, 1, bArr3.length);
            java.lang.System.arraycopy(bArr2, 0, bArr4, this.b.length + 1, bArr2.length);
            c(this.b, f(bArr4));
        }
        byte[] bArrG = g(this.b, length);
        byte[] bArr5 = this.b;
        byte[] bArr6 = new byte[bArr5.length + 1];
        java.lang.System.arraycopy(bArr5, 0, bArr6, 1, bArr5.length);
        bArr6[0] = 3;
        c(this.b, f(bArr6));
        c(this.b, this.c);
        long j = this.d;
        c(this.b, new byte[]{(byte) (j >> 24), (byte) (j >> 16), (byte) (j >> 8), (byte) j});
        this.d++;
        java.lang.System.arraycopy(bArrG, 0, bArr, 0, bArr.length);
        return length;
    }

    @Override // org.bouncycastle.crypto.prng.drbg.SP80090DRBG
    public void b(byte[] bArr) {
        byte[] bArrC = org.bouncycastle.crypto.prng.drbg.Utils.c(this.a, org.bouncycastle.util.Arrays.v(h, this.b, e(), bArr), this.g);
        this.b = bArrC;
        byte[] bArr2 = new byte[bArrC.length + 1];
        bArr2[0] = 0;
        java.lang.System.arraycopy(bArrC, 0, bArr2, 1, bArrC.length);
        this.c = org.bouncycastle.crypto.prng.drbg.Utils.c(this.a, bArr2, this.g);
        this.d = 1L;
    }
}
