package org.bouncycastle.crypto.generators;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class KDFCounterBytesGenerator implements org.bouncycastle.crypto.MacDerivationFunction {
    private static final java.math.BigInteger i = java.math.BigInteger.valueOf(2147483647L);
    private static final java.math.BigInteger j = java.math.BigInteger.valueOf(2);
    private final org.bouncycastle.crypto.Mac a;
    private final int b;
    private byte[] c;
    private byte[] d;
    private int e;
    private byte[] f;
    private int g;
    private byte[] h;

    private void a() {
        int i2 = (this.g / this.b) + 1;
        byte[] bArr = this.f;
        int length = bArr.length;
        if (length != 1) {
            if (length != 2) {
                if (length != 3) {
                    if (length != 4) {
                        throw new java.lang.IllegalStateException("Unsupported size of counter i");
                    }
                    bArr[0] = (byte) (i2 >>> 24);
                }
                bArr[bArr.length - 3] = (byte) (i2 >>> 16);
            }
            bArr[bArr.length - 2] = (byte) (i2 >>> 8);
        }
        bArr[bArr.length - 1] = (byte) i2;
        org.bouncycastle.crypto.Mac mac = this.a;
        byte[] bArr2 = this.c;
        mac.update(bArr2, 0, bArr2.length);
        org.bouncycastle.crypto.Mac mac2 = this.a;
        byte[] bArr3 = this.f;
        mac2.update(bArr3, 0, bArr3.length);
        org.bouncycastle.crypto.Mac mac3 = this.a;
        byte[] bArr4 = this.d;
        mac3.update(bArr4, 0, bArr4.length);
        this.a.doFinal(this.h, 0);
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public int generateBytes(byte[] bArr, int i2, int i3) {
        int i4 = this.g;
        int i5 = i4 + i3;
        if (i5 < 0 || i5 >= this.e) {
            throw new org.bouncycastle.crypto.DataLengthException("Current KDFCTR may only be used for " + this.e + " bytes");
        }
        if (i4 % this.b == 0) {
            a();
        }
        int i6 = this.g;
        int i7 = this.b;
        int i8 = i6 % i7;
        int iMin = java.lang.Math.min(i7 - (i6 % i7), i3);
        java.lang.System.arraycopy(this.h, i8, bArr, i2, iMin);
        this.g += iMin;
        int i9 = i3 - iMin;
        while (true) {
            i2 += iMin;
            if (i9 <= 0) {
                return i3;
            }
            a();
            iMin = java.lang.Math.min(this.b, i9);
            java.lang.System.arraycopy(this.h, 0, bArr, i2, iMin);
            this.g += iMin;
            i9 -= iMin;
        }
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public void init(org.bouncycastle.crypto.DerivationParameters derivationParameters) {
        if (!(derivationParameters instanceof org.bouncycastle.crypto.params.KDFCounterParameters)) {
            throw new java.lang.IllegalArgumentException("Wrong type of arguments given");
        }
        org.bouncycastle.crypto.params.KDFCounterParameters kDFCounterParameters = (org.bouncycastle.crypto.params.KDFCounterParameters) derivationParameters;
        this.a.init(new org.bouncycastle.crypto.params.KeyParameter(kDFCounterParameters.c()));
        this.c = kDFCounterParameters.a();
        this.d = kDFCounterParameters.b();
        int iD = kDFCounterParameters.d();
        this.f = new byte[iD / 8];
        java.math.BigInteger bigIntegerMultiply = j.pow(iD).multiply(java.math.BigInteger.valueOf(this.b));
        this.e = bigIntegerMultiply.compareTo(i) == 1 ? Integer.MAX_VALUE : bigIntegerMultiply.intValue();
        this.g = 0;
    }
}
