package org.bouncycastle.crypto.agreement.kdf;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DHKEKGenerator implements org.bouncycastle.crypto.DerivationFunction {
    private final org.bouncycastle.crypto.Digest a;
    private org.bouncycastle.asn1.ASN1ObjectIdentifier b;
    private int c;
    private byte[] d;
    private byte[] e;

    public DHKEKGenerator(org.bouncycastle.crypto.Digest digest) {
        this.a = digest;
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public int generateBytes(byte[] bArr, int i, int i2) {
        boolean z;
        int i3 = i2;
        int i4 = i;
        if (bArr.length - i3 < i4) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too small");
        }
        long j = i3;
        int digestSize = this.a.getDigestSize();
        if (j > 8589934591L) {
            throw new java.lang.IllegalArgumentException("Output length too large");
        }
        long j2 = digestSize;
        int i5 = (int) (((j + j2) - 1) / j2);
        byte[] bArr2 = new byte[this.a.getDigestSize()];
        int i6 = 0;
        int i7 = 0;
        int i8 = 1;
        while (i7 < i5) {
            org.bouncycastle.crypto.Digest digest = this.a;
            byte[] bArr3 = this.d;
            digest.update(bArr3, i6, bArr3.length);
            org.bouncycastle.asn1.ASN1EncodableVector aSN1EncodableVector = new org.bouncycastle.asn1.ASN1EncodableVector();
            org.bouncycastle.asn1.ASN1EncodableVector aSN1EncodableVector2 = new org.bouncycastle.asn1.ASN1EncodableVector();
            aSN1EncodableVector2.a(this.b);
            aSN1EncodableVector2.a(new org.bouncycastle.asn1.DEROctetString(org.bouncycastle.util.Pack.j(i8)));
            aSN1EncodableVector.a(new org.bouncycastle.asn1.DERSequence(aSN1EncodableVector2));
            if (this.e != null) {
                z = true;
                aSN1EncodableVector.a(new org.bouncycastle.asn1.DERTaggedObject(true, i6, new org.bouncycastle.asn1.DEROctetString(this.e)));
            } else {
                z = true;
            }
            aSN1EncodableVector.a(new org.bouncycastle.asn1.DERTaggedObject(z, 2, new org.bouncycastle.asn1.DEROctetString(org.bouncycastle.util.Pack.j(this.c))));
            try {
                byte[] bArrM = new org.bouncycastle.asn1.DERSequence(aSN1EncodableVector).m("DER");
                this.a.update(bArrM, 0, bArrM.length);
                this.a.doFinal(bArr2, 0);
                if (i3 > digestSize) {
                    java.lang.System.arraycopy(bArr2, 0, bArr, i4, digestSize);
                    i4 += digestSize;
                    i3 -= digestSize;
                } else {
                    java.lang.System.arraycopy(bArr2, 0, bArr, i4, i3);
                }
                i8++;
                i7++;
                i6 = 0;
            } catch (java.io.IOException e) {
                throw new java.lang.IllegalArgumentException("unable to encode parameter info: " + e.getMessage());
            }
        }
        this.a.reset();
        return (int) j;
    }

    @Override // org.bouncycastle.crypto.DerivationFunction
    public void init(org.bouncycastle.crypto.DerivationParameters derivationParameters) {
        org.bouncycastle.crypto.agreement.kdf.DHKDFParameters dHKDFParameters = (org.bouncycastle.crypto.agreement.kdf.DHKDFParameters) derivationParameters;
        this.b = dHKDFParameters.a();
        this.c = dHKDFParameters.c();
        this.d = dHKDFParameters.d();
        this.e = dHKDFParameters.b();
    }
}
