package org.bouncycastle.crypto.agreement;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ECMQVBasicAgreement implements org.bouncycastle.crypto.BasicAgreement {
    org.bouncycastle.crypto.params.MQVPrivateParameters a;

    private org.bouncycastle.math.ec.ECPoint b(org.bouncycastle.crypto.params.ECDomainParameters eCDomainParameters, org.bouncycastle.crypto.params.ECPrivateKeyParameters eCPrivateKeyParameters, org.bouncycastle.crypto.params.ECPrivateKeyParameters eCPrivateKeyParameters2, org.bouncycastle.crypto.params.ECPublicKeyParameters eCPublicKeyParameters, org.bouncycastle.crypto.params.ECPublicKeyParameters eCPublicKeyParameters2, org.bouncycastle.crypto.params.ECPublicKeyParameters eCPublicKeyParameters3) {
        java.math.BigInteger bigIntegerE = eCDomainParameters.e();
        int iBitLength = (bigIntegerE.bitLength() + 1) / 2;
        java.math.BigInteger bigIntegerShiftLeft = org.bouncycastle.math.ec.ECConstants.b.shiftLeft(iBitLength);
        org.bouncycastle.math.ec.ECCurve eCCurveA = eCDomainParameters.a();
        org.bouncycastle.math.ec.ECPoint eCPointA = org.bouncycastle.math.ec.ECAlgorithms.a(eCCurveA, eCPublicKeyParameters.h());
        org.bouncycastle.math.ec.ECPoint eCPointA2 = org.bouncycastle.math.ec.ECAlgorithms.a(eCCurveA, eCPublicKeyParameters2.h());
        org.bouncycastle.math.ec.ECPoint eCPointA3 = org.bouncycastle.math.ec.ECAlgorithms.a(eCCurveA, eCPublicKeyParameters3.h());
        java.math.BigInteger bigIntegerMod = eCPrivateKeyParameters.h().multiply(eCPointA.f().v().mod(bigIntegerShiftLeft).setBit(iBitLength)).add(eCPrivateKeyParameters2.h()).mod(bigIntegerE);
        java.math.BigInteger bit = eCPointA3.f().v().mod(bigIntegerShiftLeft).setBit(iBitLength);
        java.math.BigInteger bigIntegerMod2 = eCDomainParameters.c().multiply(bigIntegerMod).mod(bigIntegerE);
        return org.bouncycastle.math.ec.ECAlgorithms.r(eCPointA2, bit.multiply(bigIntegerMod2).mod(bigIntegerE), eCPointA3, bigIntegerMod2);
    }

    @Override // org.bouncycastle.crypto.BasicAgreement
    public java.math.BigInteger a(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (org.bouncycastle.util.Properties.d("org.bouncycastle.ec.disable_mqv")) {
            throw new java.lang.IllegalStateException("ECMQV explicitly disabled");
        }
        org.bouncycastle.crypto.params.MQVPublicParameters mQVPublicParameters = (org.bouncycastle.crypto.params.MQVPublicParameters) cipherParameters;
        org.bouncycastle.crypto.params.ECPrivateKeyParameters eCPrivateKeyParametersC = this.a.c();
        org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = eCPrivateKeyParametersC.g();
        if (!eCDomainParametersG.equals(mQVPublicParameters.b().g())) {
            throw new java.lang.IllegalStateException("ECMQV public key components have wrong domain parameters");
        }
        org.bouncycastle.math.ec.ECPoint eCPointA = b(eCDomainParametersG, eCPrivateKeyParametersC, this.a.a(), this.a.b(), mQVPublicParameters.b(), mQVPublicParameters.a()).A();
        if (eCPointA.u()) {
            throw new java.lang.IllegalStateException("Infinity is not a valid agreement value for MQV");
        }
        return eCPointA.f().v();
    }

    @Override // org.bouncycastle.crypto.BasicAgreement
    public int getFieldSize() {
        return this.a.c().g().a().v();
    }

    @Override // org.bouncycastle.crypto.BasicAgreement
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.MQVPrivateParameters mQVPrivateParameters = (org.bouncycastle.crypto.params.MQVPrivateParameters) cipherParameters;
        this.a = mQVPrivateParameters;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.agreement.Utils.b("ECMQV", mQVPrivateParameters.c()));
    }
}
