package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class EthereumIESEngine {

    public static class HandshakeKDFFunction implements org.bouncycastle.crypto.DigestDerivationFunction {
        private int a;
        private org.bouncycastle.crypto.Digest b;
        private byte[] c;
        private byte[] d;

        @Override // org.bouncycastle.crypto.DerivationFunction
        public int generateBytes(byte[] bArr, int i, int i2) {
            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.b.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.b.getDigestSize()];
            int i6 = 4;
            byte[] bArr3 = new byte[4];
            org.bouncycastle.util.Pack.h(this.a, bArr3, 0);
            int i7 = this.a & (-256);
            int i8 = 0;
            while (i8 < i5) {
                this.b.update(bArr3, 0, i6);
                org.bouncycastle.crypto.Digest digest = this.b;
                byte[] bArr4 = this.c;
                digest.update(bArr4, 0, bArr4.length);
                byte[] bArr5 = this.d;
                if (bArr5 != null) {
                    this.b.update(bArr5, 0, bArr5.length);
                }
                this.b.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);
                }
                byte b = (byte) (bArr3[3] + 1);
                bArr3[3] = b;
                if (b == 0) {
                    i7 += 256;
                    org.bouncycastle.util.Pack.h(i7, bArr3, 0);
                }
                i8++;
                i6 = 4;
            }
            this.b.reset();
            return (int) j;
        }

        @Override // org.bouncycastle.crypto.DerivationFunction
        public void init(org.bouncycastle.crypto.DerivationParameters derivationParameters) {
            if (derivationParameters instanceof org.bouncycastle.crypto.params.KDFParameters) {
                org.bouncycastle.crypto.params.KDFParameters kDFParameters = (org.bouncycastle.crypto.params.KDFParameters) derivationParameters;
                this.c = kDFParameters.b();
                this.d = kDFParameters.a();
            } else {
                if (!(derivationParameters instanceof org.bouncycastle.crypto.params.ISO18033KDFParameters)) {
                    throw new java.lang.IllegalArgumentException("KDF parameters required for generator");
                }
                this.c = ((org.bouncycastle.crypto.params.ISO18033KDFParameters) derivationParameters).a();
                this.d = null;
            }
        }
    }
}
