118 lines
6.9 KiB
Java
118 lines
6.9 KiB
Java
package org.bouncycastle.jcajce.provider.asymmetric.util;
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.spec.ECField;
|
|
import java.security.spec.ECFieldF2m;
|
|
import java.security.spec.ECFieldFp;
|
|
import java.security.spec.ECParameterSpec;
|
|
import java.security.spec.ECPoint;
|
|
import java.security.spec.EllipticCurve;
|
|
import java.util.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
|
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
|
|
import org.bouncycastle.asn1.x9.X962Parameters;
|
|
import org.bouncycastle.asn1.x9.X9ECParameters;
|
|
import org.bouncycastle.crypto.ec.CustomNamedCurves;
|
|
import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
|
|
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
|
|
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
|
import org.bouncycastle.math.ec.ECAlgorithms;
|
|
import org.bouncycastle.math.ec.ECCurve;
|
|
import org.bouncycastle.math.field.FiniteField;
|
|
import org.bouncycastle.math.field.Polynomial;
|
|
import org.bouncycastle.math.field.PolynomialExtensionField;
|
|
import org.bouncycastle.util.Arrays;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class EC5Util {
|
|
private static Map customCurves = new HashMap();
|
|
|
|
public static ECCurve getCurve(ProviderConfiguration providerConfiguration, X962Parameters x962Parameters) {
|
|
X9ECParameters x9ECParameters;
|
|
if (x962Parameters.isNamedCurve()) {
|
|
x9ECParameters = ECUtil.getNamedCurveByOid(ASN1ObjectIdentifier.getInstance(x962Parameters.getParameters()));
|
|
} else {
|
|
if (x962Parameters.isImplicitlyCA()) {
|
|
return providerConfiguration.getEcImplicitlyCa().getCurve();
|
|
}
|
|
x9ECParameters = X9ECParameters.getInstance(x962Parameters.getParameters());
|
|
}
|
|
return x9ECParameters.getCurve();
|
|
}
|
|
|
|
public static ECParameterSpec convertToSpec(X9ECParameters x9ECParameters) {
|
|
return new ECParameterSpec(convertCurve(x9ECParameters.getCurve(), null), new ECPoint(x9ECParameters.getG().getAffineXCoord().toBigInteger(), x9ECParameters.getG().getAffineYCoord().toBigInteger()), x9ECParameters.getN(), x9ECParameters.getH().intValue());
|
|
}
|
|
|
|
public static ECParameterSpec convertToSpec(X962Parameters x962Parameters, ECCurve eCCurve) {
|
|
if (x962Parameters.isNamedCurve()) {
|
|
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier) x962Parameters.getParameters();
|
|
X9ECParameters namedCurveByOid = ECUtil.getNamedCurveByOid(aSN1ObjectIdentifier);
|
|
return new ECNamedCurveSpec(ECUtil.getCurveName(aSN1ObjectIdentifier), convertCurve(eCCurve, namedCurveByOid.getSeed()), new ECPoint(namedCurveByOid.getG().getAffineXCoord().toBigInteger(), namedCurveByOid.getG().getAffineYCoord().toBigInteger()), namedCurveByOid.getN(), namedCurveByOid.getH());
|
|
}
|
|
if (x962Parameters.isImplicitlyCA()) {
|
|
return null;
|
|
}
|
|
X9ECParameters x9ECParameters = X9ECParameters.getInstance(x962Parameters.getParameters());
|
|
EllipticCurve convertCurve = convertCurve(eCCurve, x9ECParameters.getSeed());
|
|
return x9ECParameters.getH() != null ? new ECParameterSpec(convertCurve, new ECPoint(x9ECParameters.getG().getAffineXCoord().toBigInteger(), x9ECParameters.getG().getAffineYCoord().toBigInteger()), x9ECParameters.getN(), x9ECParameters.getH().intValue()) : new ECParameterSpec(convertCurve, new ECPoint(x9ECParameters.getG().getAffineXCoord().toBigInteger(), x9ECParameters.getG().getAffineYCoord().toBigInteger()), x9ECParameters.getN(), 1);
|
|
}
|
|
|
|
public static org.bouncycastle.jce.spec.ECParameterSpec convertSpec(ECParameterSpec eCParameterSpec, boolean z) {
|
|
ECCurve convertCurve = convertCurve(eCParameterSpec.getCurve());
|
|
return new org.bouncycastle.jce.spec.ECParameterSpec(convertCurve, convertPoint(convertCurve, eCParameterSpec.getGenerator(), z), eCParameterSpec.getOrder(), BigInteger.valueOf(eCParameterSpec.getCofactor()), eCParameterSpec.getCurve().getSeed());
|
|
}
|
|
|
|
public static ECParameterSpec convertSpec(EllipticCurve ellipticCurve, org.bouncycastle.jce.spec.ECParameterSpec eCParameterSpec) {
|
|
return eCParameterSpec instanceof ECNamedCurveParameterSpec ? new ECNamedCurveSpec(((ECNamedCurveParameterSpec) eCParameterSpec).getName(), ellipticCurve, new ECPoint(eCParameterSpec.getG().getAffineXCoord().toBigInteger(), eCParameterSpec.getG().getAffineYCoord().toBigInteger()), eCParameterSpec.getN(), eCParameterSpec.getH()) : new ECParameterSpec(ellipticCurve, new ECPoint(eCParameterSpec.getG().getAffineXCoord().toBigInteger(), eCParameterSpec.getG().getAffineYCoord().toBigInteger()), eCParameterSpec.getN(), eCParameterSpec.getH().intValue());
|
|
}
|
|
|
|
public static org.bouncycastle.math.ec.ECPoint convertPoint(ECCurve eCCurve, ECPoint eCPoint, boolean z) {
|
|
return eCCurve.createPoint(eCPoint.getAffineX(), eCPoint.getAffineY());
|
|
}
|
|
|
|
public static org.bouncycastle.math.ec.ECPoint convertPoint(ECParameterSpec eCParameterSpec, ECPoint eCPoint, boolean z) {
|
|
return convertPoint(convertCurve(eCParameterSpec.getCurve()), eCPoint, z);
|
|
}
|
|
|
|
public static ECField convertField(FiniteField finiteField) {
|
|
if (ECAlgorithms.isFpField(finiteField)) {
|
|
return new ECFieldFp(finiteField.getCharacteristic());
|
|
}
|
|
Polynomial minimalPolynomial = ((PolynomialExtensionField) finiteField).getMinimalPolynomial();
|
|
int[] exponentsPresent = minimalPolynomial.getExponentsPresent();
|
|
return new ECFieldF2m(minimalPolynomial.getDegree(), Arrays.reverse(Arrays.copyOfRange(exponentsPresent, 1, exponentsPresent.length - 1)));
|
|
}
|
|
|
|
public static ECCurve convertCurve(EllipticCurve ellipticCurve) {
|
|
ECField field = ellipticCurve.getField();
|
|
BigInteger a = ellipticCurve.getA();
|
|
BigInteger b = ellipticCurve.getB();
|
|
if (field instanceof ECFieldFp) {
|
|
ECCurve.Fp fp = new ECCurve.Fp(((ECFieldFp) field).getP(), a, b);
|
|
return customCurves.containsKey(fp) ? (ECCurve) customCurves.get(fp) : fp;
|
|
}
|
|
ECFieldF2m eCFieldF2m = (ECFieldF2m) field;
|
|
int m = eCFieldF2m.getM();
|
|
int[] convertMidTerms = ECUtil.convertMidTerms(eCFieldF2m.getMidTermsOfReductionPolynomial());
|
|
return new ECCurve.F2m(m, convertMidTerms[0], convertMidTerms[1], convertMidTerms[2], a, b);
|
|
}
|
|
|
|
public static EllipticCurve convertCurve(ECCurve eCCurve, byte[] bArr) {
|
|
return new EllipticCurve(convertField(eCCurve.getField()), eCCurve.getA().toBigInteger(), eCCurve.getB().toBigInteger(), null);
|
|
}
|
|
|
|
static {
|
|
Enumeration names = CustomNamedCurves.getNames();
|
|
while (names.hasMoreElements()) {
|
|
String str = (String) names.nextElement();
|
|
X9ECParameters byName = ECNamedCurveTable.getByName(str);
|
|
if (byName != null) {
|
|
customCurves.put(byName.getCurve(), CustomNamedCurves.getByName(str).getCurve());
|
|
}
|
|
}
|
|
}
|
|
}
|