what-the-bank/sources/org/bouncycastle/crypto/agreement/ECMQVBasicAgreement.java

65 lines
3.7 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.agreement;
import java.math.BigInteger;
import org.bouncycastle.crypto.BasicAgreement;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.MQVPrivateParameters;
import org.bouncycastle.crypto.params.MQVPublicParameters;
import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.math.ec.ECConstants;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.Properties;
/* loaded from: classes6.dex */
public class ECMQVBasicAgreement implements BasicAgreement {
MQVPrivateParameters privParams;
@Override // org.bouncycastle.crypto.BasicAgreement
public void init(CipherParameters cipherParameters) {
this.privParams = (MQVPrivateParameters) cipherParameters;
}
@Override // org.bouncycastle.crypto.BasicAgreement
public int getFieldSize() {
return (this.privParams.getStaticPrivateKey().getParameters().getCurve().getFieldSize() + 7) / 8;
}
@Override // org.bouncycastle.crypto.BasicAgreement
public BigInteger calculateAgreement(CipherParameters cipherParameters) {
if (Properties.isOverrideSet("org.bouncycastle.ec.disable_mqv")) {
throw new IllegalStateException("ECMQV explicitly disabled");
}
MQVPublicParameters mQVPublicParameters = (MQVPublicParameters) cipherParameters;
ECPrivateKeyParameters staticPrivateKey = this.privParams.getStaticPrivateKey();
ECDomainParameters parameters = staticPrivateKey.getParameters();
if (!parameters.equals(mQVPublicParameters.getStaticPublicKey().getParameters())) {
throw new IllegalStateException("ECMQV public key components have wrong domain parameters");
}
ECPoint normalize = calculateMqvAgreement(parameters, staticPrivateKey, this.privParams.getEphemeralPrivateKey(), this.privParams.getEphemeralPublicKey(), mQVPublicParameters.getStaticPublicKey(), mQVPublicParameters.getEphemeralPublicKey()).normalize();
if (normalize.isInfinity()) {
throw new IllegalStateException("Infinity is not a valid agreement value for MQV");
}
return normalize.getAffineXCoord().toBigInteger();
}
private ECPoint calculateMqvAgreement(ECDomainParameters eCDomainParameters, ECPrivateKeyParameters eCPrivateKeyParameters, ECPrivateKeyParameters eCPrivateKeyParameters2, ECPublicKeyParameters eCPublicKeyParameters, ECPublicKeyParameters eCPublicKeyParameters2, ECPublicKeyParameters eCPublicKeyParameters3) {
BigInteger n = eCDomainParameters.getN();
int bitLength = (n.bitLength() + 1) / 2;
BigInteger shiftLeft = ECConstants.ONE.shiftLeft(bitLength);
ECCurve curve = eCDomainParameters.getCurve();
ECPoint[] eCPointArr = {ECAlgorithms.importPoint(curve, eCPublicKeyParameters.getQ()), ECAlgorithms.importPoint(curve, eCPublicKeyParameters2.getQ()), ECAlgorithms.importPoint(curve, eCPublicKeyParameters3.getQ())};
curve.normalizeAll(eCPointArr);
ECPoint eCPoint = eCPointArr[0];
ECPoint eCPoint2 = eCPointArr[1];
ECPoint eCPoint3 = eCPointArr[2];
BigInteger mod = eCPrivateKeyParameters.getD().multiply(eCPoint.getAffineXCoord().toBigInteger().mod(shiftLeft).setBit(bitLength)).add(eCPrivateKeyParameters2.getD()).mod(n);
BigInteger bit = eCPoint3.getAffineXCoord().toBigInteger().mod(shiftLeft).setBit(bitLength);
BigInteger mod2 = eCDomainParameters.getH().multiply(mod).mod(n);
return ECAlgorithms.sumOfTwoMultiplies(eCPoint2, bit.multiply(mod2).mod(n), eCPoint3, mod2);
}
}