263 lines
12 KiB
Java
263 lines
12 KiB
Java
|
package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.ObjectInputStream;
|
||
|
import java.io.ObjectOutputStream;
|
||
|
import java.math.BigInteger;
|
||
|
import java.security.interfaces.ECPublicKey;
|
||
|
import java.security.spec.ECParameterSpec;
|
||
|
import java.security.spec.EllipticCurve;
|
||
|
import org.bouncycastle.asn1.ASN1Encodable;
|
||
|
import org.bouncycastle.asn1.ASN1OctetString;
|
||
|
import org.bouncycastle.asn1.ASN1Primitive;
|
||
|
import org.bouncycastle.asn1.DERBitString;
|
||
|
import org.bouncycastle.asn1.DEROctetString;
|
||
|
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
|
||
|
import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
|
||
|
import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters;
|
||
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
||
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
||
|
import org.bouncycastle.asn1.x9.X962Parameters;
|
||
|
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||
|
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||
|
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||
|
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util;
|
||
|
import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
|
||
|
import org.bouncycastle.jce.ECGOST3410NamedCurveTable;
|
||
|
import org.bouncycastle.jce.interfaces.ECPointEncoder;
|
||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||
|
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
|
||
|
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
||
|
import org.bouncycastle.jce.spec.ECPublicKeySpec;
|
||
|
import org.bouncycastle.math.ec.ECCurve;
|
||
|
import org.bouncycastle.math.ec.ECPoint;
|
||
|
import org.bouncycastle.util.Strings;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class BCECGOST3410PublicKey implements ECPublicKey, org.bouncycastle.jce.interfaces.ECPublicKey, ECPointEncoder {
|
||
|
static final long serialVersionUID = 7026240464295649314L;
|
||
|
private String algorithm;
|
||
|
private transient ECParameterSpec ecSpec;
|
||
|
private transient GOST3410PublicKeyAlgParameters gostParams;
|
||
|
private transient ECPoint q;
|
||
|
private boolean withCompression;
|
||
|
|
||
|
public String toString() {
|
||
|
StringBuffer stringBuffer = new StringBuffer("EC Public Key");
|
||
|
String lineSeparator = Strings.lineSeparator();
|
||
|
stringBuffer.append(lineSeparator);
|
||
|
stringBuffer.append(" X: ").append(this.q.getAffineXCoord().toBigInteger().toString(16)).append(lineSeparator);
|
||
|
stringBuffer.append(" Y: ").append(this.q.getAffineYCoord().toBigInteger().toString(16)).append(lineSeparator);
|
||
|
return stringBuffer.toString();
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jce.interfaces.ECPointEncoder
|
||
|
public void setPointFormat(String str) {
|
||
|
this.withCompression = !"UNCOMPRESSED".equalsIgnoreCase(str);
|
||
|
}
|
||
|
|
||
|
public int hashCode() {
|
||
|
return engineGetQ().hashCode() ^ engineGetSpec().hashCode();
|
||
|
}
|
||
|
|
||
|
@Override // java.security.interfaces.ECPublicKey
|
||
|
public java.security.spec.ECPoint getW() {
|
||
|
return new java.security.spec.ECPoint(this.q.getAffineXCoord().toBigInteger(), this.q.getAffineYCoord().toBigInteger());
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jce.interfaces.ECPublicKey
|
||
|
public ECPoint getQ() {
|
||
|
return this.ecSpec == null ? this.q.getDetachedPoint() : this.q;
|
||
|
}
|
||
|
|
||
|
@Override // java.security.interfaces.ECKey
|
||
|
public ECParameterSpec getParams() {
|
||
|
return this.ecSpec;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jce.interfaces.ECKey
|
||
|
public org.bouncycastle.jce.spec.ECParameterSpec getParameters() {
|
||
|
ECParameterSpec eCParameterSpec = this.ecSpec;
|
||
|
if (eCParameterSpec == null) {
|
||
|
return null;
|
||
|
}
|
||
|
return EC5Util.convertSpec(eCParameterSpec, this.withCompression);
|
||
|
}
|
||
|
|
||
|
public GOST3410PublicKeyAlgParameters getGostParams() {
|
||
|
return this.gostParams;
|
||
|
}
|
||
|
|
||
|
@Override // java.security.Key
|
||
|
public String getFormat() {
|
||
|
return "X.509";
|
||
|
}
|
||
|
|
||
|
@Override // java.security.Key
|
||
|
public byte[] getEncoded() {
|
||
|
ASN1Encodable aSN1Encodable = this.gostParams;
|
||
|
if (aSN1Encodable == null) {
|
||
|
ECParameterSpec eCParameterSpec = this.ecSpec;
|
||
|
if (eCParameterSpec instanceof ECNamedCurveSpec) {
|
||
|
aSN1Encodable = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) eCParameterSpec).getName()), CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
|
||
|
} else {
|
||
|
ECCurve convertCurve = EC5Util.convertCurve(eCParameterSpec.getCurve());
|
||
|
aSN1Encodable = new X962Parameters(new X9ECParameters(convertCurve, EC5Util.convertPoint(convertCurve, this.ecSpec.getGenerator(), this.withCompression), this.ecSpec.getOrder(), BigInteger.valueOf(this.ecSpec.getCofactor()), this.ecSpec.getCurve().getSeed()));
|
||
|
}
|
||
|
}
|
||
|
BigInteger bigInteger = this.q.getAffineXCoord().toBigInteger();
|
||
|
BigInteger bigInteger2 = this.q.getAffineYCoord().toBigInteger();
|
||
|
byte[] bArr = new byte[64];
|
||
|
extractBytes(bArr, 0, bigInteger);
|
||
|
extractBytes(bArr, 32, bigInteger2);
|
||
|
try {
|
||
|
return KeyUtil.getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, aSN1Encodable), new DEROctetString(bArr)));
|
||
|
} catch (IOException unused) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // java.security.Key
|
||
|
public String getAlgorithm() {
|
||
|
return this.algorithm;
|
||
|
}
|
||
|
|
||
|
public boolean equals(Object obj) {
|
||
|
if (!(obj instanceof BCECGOST3410PublicKey)) {
|
||
|
return false;
|
||
|
}
|
||
|
BCECGOST3410PublicKey bCECGOST3410PublicKey = (BCECGOST3410PublicKey) obj;
|
||
|
return engineGetQ().equals(bCECGOST3410PublicKey.engineGetQ()) && engineGetSpec().equals(bCECGOST3410PublicKey.engineGetSpec());
|
||
|
}
|
||
|
|
||
|
org.bouncycastle.jce.spec.ECParameterSpec engineGetSpec() {
|
||
|
ECParameterSpec eCParameterSpec = this.ecSpec;
|
||
|
return eCParameterSpec != null ? EC5Util.convertSpec(eCParameterSpec, this.withCompression) : BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
|
||
|
}
|
||
|
|
||
|
public ECPoint engineGetQ() {
|
||
|
return this.q;
|
||
|
}
|
||
|
|
||
|
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
|
||
|
objectOutputStream.defaultWriteObject();
|
||
|
objectOutputStream.writeObject(getEncoded());
|
||
|
}
|
||
|
|
||
|
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||
|
objectInputStream.defaultReadObject();
|
||
|
populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray((byte[]) objectInputStream.readObject())));
|
||
|
}
|
||
|
|
||
|
private void populateFromPubKeyInfo(SubjectPublicKeyInfo subjectPublicKeyInfo) {
|
||
|
DERBitString publicKeyData = subjectPublicKeyInfo.getPublicKeyData();
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
try {
|
||
|
byte[] octets = ((ASN1OctetString) ASN1Primitive.fromByteArray(publicKeyData.getBytes())).getOctets();
|
||
|
byte[] bArr = new byte[32];
|
||
|
byte[] bArr2 = new byte[32];
|
||
|
for (int i = 0; i != 32; i++) {
|
||
|
bArr[i] = octets[31 - i];
|
||
|
}
|
||
|
for (int i2 = 0; i2 != 32; i2++) {
|
||
|
bArr2[i2] = octets[63 - i2];
|
||
|
}
|
||
|
GOST3410PublicKeyAlgParameters gOST3410PublicKeyAlgParameters = GOST3410PublicKeyAlgParameters.getInstance(subjectPublicKeyInfo.getAlgorithm().getParameters());
|
||
|
this.gostParams = gOST3410PublicKeyAlgParameters;
|
||
|
ECNamedCurveParameterSpec parameterSpec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gOST3410PublicKeyAlgParameters.getPublicKeyParamSet()));
|
||
|
ECCurve curve = parameterSpec.getCurve();
|
||
|
EllipticCurve convertCurve = EC5Util.convertCurve(curve, parameterSpec.getSeed());
|
||
|
this.q = curve.createPoint(new BigInteger(1, bArr), new BigInteger(1, bArr2));
|
||
|
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(this.gostParams.getPublicKeyParamSet()), convertCurve, new java.security.spec.ECPoint(parameterSpec.getG().getAffineXCoord().toBigInteger(), parameterSpec.getG().getAffineYCoord().toBigInteger()), parameterSpec.getN(), parameterSpec.getH());
|
||
|
} catch (IOException unused) {
|
||
|
throw new IllegalArgumentException("error recovering public key");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void extractBytes(byte[] bArr, int i, BigInteger bigInteger) {
|
||
|
byte[] byteArray = bigInteger.toByteArray();
|
||
|
if (byteArray.length < 32) {
|
||
|
byte[] bArr2 = new byte[32];
|
||
|
System.arraycopy(byteArray, 0, bArr2, 32 - byteArray.length, byteArray.length);
|
||
|
byteArray = bArr2;
|
||
|
}
|
||
|
for (int i2 = 0; i2 != 32; i2++) {
|
||
|
bArr[i + i2] = byteArray[(byteArray.length - 1) - i2];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters eCDomainParameters) {
|
||
|
return new ECParameterSpec(ellipticCurve, new java.security.spec.ECPoint(eCDomainParameters.getG().getAffineXCoord().toBigInteger(), eCDomainParameters.getG().getAffineYCoord().toBigInteger()), eCDomainParameters.getN(), eCDomainParameters.getH().intValue());
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(ECPublicKeySpec eCPublicKeySpec) {
|
||
|
ECParameterSpec eCParameterSpec;
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
this.q = eCPublicKeySpec.getQ();
|
||
|
if (eCPublicKeySpec.getParams() != null) {
|
||
|
eCParameterSpec = EC5Util.convertSpec(EC5Util.convertCurve(eCPublicKeySpec.getParams().getCurve(), eCPublicKeySpec.getParams().getSeed()), eCPublicKeySpec.getParams());
|
||
|
} else {
|
||
|
if (this.q.getCurve() == null) {
|
||
|
this.q = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa().getCurve().createPoint(this.q.getAffineXCoord().toBigInteger(), this.q.getAffineYCoord().toBigInteger());
|
||
|
}
|
||
|
eCParameterSpec = null;
|
||
|
}
|
||
|
this.ecSpec = eCParameterSpec;
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(BCECGOST3410PublicKey bCECGOST3410PublicKey) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
this.q = bCECGOST3410PublicKey.q;
|
||
|
this.ecSpec = bCECGOST3410PublicKey.ecSpec;
|
||
|
this.withCompression = bCECGOST3410PublicKey.withCompression;
|
||
|
this.gostParams = bCECGOST3410PublicKey.gostParams;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public BCECGOST3410PublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
populateFromPubKeyInfo(subjectPublicKeyInfo);
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(java.security.spec.ECPublicKeySpec eCPublicKeySpec) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
ECParameterSpec params = eCPublicKeySpec.getParams();
|
||
|
this.ecSpec = params;
|
||
|
this.q = EC5Util.convertPoint(params, eCPublicKeySpec.getW(), false);
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(ECPublicKey eCPublicKey) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
this.algorithm = eCPublicKey.getAlgorithm();
|
||
|
ECParameterSpec params = eCPublicKey.getParams();
|
||
|
this.ecSpec = params;
|
||
|
this.q = EC5Util.convertPoint(params, eCPublicKey.getW(), false);
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters, org.bouncycastle.jce.spec.ECParameterSpec eCParameterSpec) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
ECDomainParameters parameters = eCPublicKeyParameters.getParameters();
|
||
|
this.algorithm = str;
|
||
|
this.q = eCPublicKeyParameters.getQ();
|
||
|
this.ecSpec = eCParameterSpec == null ? createSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), parameters) : EC5Util.convertSpec(EC5Util.convertCurve(eCParameterSpec.getCurve(), eCParameterSpec.getSeed()), eCParameterSpec);
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters, ECParameterSpec eCParameterSpec) {
|
||
|
this.algorithm = "ECGOST3410";
|
||
|
ECDomainParameters parameters = eCPublicKeyParameters.getParameters();
|
||
|
this.algorithm = str;
|
||
|
this.q = eCPublicKeyParameters.getQ();
|
||
|
if (eCParameterSpec == null) {
|
||
|
this.ecSpec = createSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), parameters);
|
||
|
} else {
|
||
|
this.ecSpec = eCParameterSpec;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public BCECGOST3410PublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters) {
|
||
|
this.algorithm = str;
|
||
|
this.q = eCPublicKeyParameters.getQ();
|
||
|
this.ecSpec = null;
|
||
|
}
|
||
|
}
|