package org.bouncycastle.jcajce.provider.drbg;

import java.security.SecureRandom;
import org.bouncycastle.crypto.prng.EntropySource;
import org.bouncycastle.crypto.prng.EntropySourceProvider;

/* JADX INFO: loaded from: classes.dex */
class IncrementalEntropySourceProvider implements EntropySourceProvider {
    private final boolean predictionResistant;
    private final SecureRandom random;

    public IncrementalEntropySourceProvider(SecureRandom secureRandom, boolean z) {
        this.random = secureRandom;
        this.predictionResistant = z;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void sleep(long j) throws InterruptedException {
        if (j != 0) {
            Thread.sleep(j);
        }
    }

    @Override // org.bouncycastle.crypto.prng.EntropySourceProvider
    public EntropySource get(final int i) {
        return new IncrementalEntropySource(this) { // from class: org.bouncycastle.jcajce.provider.drbg.IncrementalEntropySourceProvider.1
            final int numBytes;
            final /* synthetic */ IncrementalEntropySourceProvider this$0;

            {
                this.this$0 = this;
                this.numBytes = (i + 7) / 8;
            }

            @Override // org.bouncycastle.crypto.prng.EntropySource
            public int entropySize() {
                return i;
            }

            @Override // org.bouncycastle.crypto.prng.EntropySource
            public byte[] getEntropy() {
                try {
                    return getEntropy(0L);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new IllegalStateException("initial entropy fetch interrupted");
                }
            }

            @Override // org.bouncycastle.jcajce.provider.drbg.IncrementalEntropySource
            public byte[] getEntropy(long j) throws InterruptedException {
                int i2 = this.numBytes;
                byte[] bArr = new byte[i2];
                for (int i3 = 0; i3 < this.numBytes / 8; i3++) {
                    IncrementalEntropySourceProvider.sleep(j);
                    byte[] bArrGenerateSeed = this.this$0.random.generateSeed(8);
                    System.arraycopy(bArrGenerateSeed, 0, bArr, i3 * 8, bArrGenerateSeed.length);
                }
                int i4 = this.numBytes - ((this.numBytes / 8) * 8);
                if (i4 != 0) {
                    IncrementalEntropySourceProvider.sleep(j);
                    byte[] bArrGenerateSeed2 = this.this$0.random.generateSeed(i4);
                    System.arraycopy(bArrGenerateSeed2, 0, bArr, i2 - bArrGenerateSeed2.length, bArrGenerateSeed2.length);
                }
                return bArr;
            }

            @Override // org.bouncycastle.crypto.prng.EntropySource
            public boolean isPredictionResistant() {
                return this.this$0.predictionResistant;
            }
        };
    }
}
