package org.bouncycastle.crypto.generators;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class HKDFBytesGenerator implements org.bouncycastle.crypto.DerivationFunction {
    private org.bouncycastle.crypto.macs.HMac a;
    private int b;
    private byte[] c;
    private byte[] d;
    private int e;

    public HKDFBytesGenerator(org.bouncycastle.crypto.Digest digest) {
        this.a = new org.bouncycastle.crypto.macs.HMac(digest);
        this.b = digest.getDigestSize();
    }

    private void a() {
        int i = this.e;
        int i2 = this.b;
        int i3 = (i / i2) + 1;
        if (i3 >= 256) {
            throw new org.bouncycastle.crypto.DataLengthException("HKDF cannot generate more than 255 blocks of HashLen size");
        }
        if (i != 0) {
            this.a.update(this.d, 0, i2);
        }
        org.bouncycastle.crypto.macs.HMac hMac = this.a;
        byte[] bArr = this.c;
        hMac.update(bArr, 0, bArr.length);
        this.a.update((byte) i3);
        this.a.doFinal(this.d, 0);
    }

    public byte[] b(byte[] bArr, byte[] bArr2) {
        if (bArr == null) {
            this.a.init(new org.bouncycastle.crypto.params.KeyParameter(new byte[this.b]));
        } else {
            this.a.init(new org.bouncycastle.crypto.params.KeyParameter(bArr));
        }
        this.a.update(bArr2, 0, bArr2.length);
        byte[] bArr3 = new byte[this.b];
        this.a.doFinal(bArr3, 0);
        return bArr3;
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public int generateBytes(byte[] bArr, int i, int i2) {
        int i3 = this.e;
        int i4 = i3 + i2;
        int i5 = this.b;
        if (i4 > i5 * 255) {
            throw new org.bouncycastle.crypto.DataLengthException("HKDF may only be used for 255 * HashLen bytes of output");
        }
        if (i3 % i5 == 0) {
            a();
        }
        int i6 = this.e;
        int i7 = this.b;
        int i8 = i6 % i7;
        int iMin = java.lang.Math.min(i7 - (i6 % i7), i2);
        java.lang.System.arraycopy(this.d, i8, bArr, i, iMin);
        this.e += iMin;
        int i9 = i2 - iMin;
        while (true) {
            i += iMin;
            if (i9 <= 0) {
                return i2;
            }
            a();
            iMin = java.lang.Math.min(this.b, i9);
            java.lang.System.arraycopy(this.d, 0, bArr, i, iMin);
            this.e += iMin;
            i9 -= iMin;
        }
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public void init(org.bouncycastle.crypto.DerivationParameters derivationParameters) {
        org.bouncycastle.crypto.macs.HMac hMac;
        org.bouncycastle.crypto.params.KeyParameter keyParameter;
        if (!(derivationParameters instanceof org.bouncycastle.crypto.params.HKDFParameters)) {
            throw new java.lang.IllegalArgumentException("HKDF parameters required for HKDFBytesGenerator");
        }
        org.bouncycastle.crypto.params.HKDFParameters hKDFParameters = (org.bouncycastle.crypto.params.HKDFParameters) derivationParameters;
        if (hKDFParameters.d()) {
            hMac = this.a;
            keyParameter = new org.bouncycastle.crypto.params.KeyParameter(hKDFParameters.a());
        } else {
            hMac = this.a;
            keyParameter = new org.bouncycastle.crypto.params.KeyParameter(b(hKDFParameters.c(), hKDFParameters.a()));
        }
        hMac.init(keyParameter);
        this.c = hKDFParameters.b();
        this.e = 0;
        this.d = new byte[this.b];
    }
}
