use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.
the class EC5Util method convertToSpec.
public static ECParameterSpec convertToSpec(X962Parameters params, ECCurve curve) {
ECParameterSpec ecSpec;
EllipticCurve ellipticCurve;
if (params.isNamedCurve()) {
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) params.getParameters();
X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
if (ecP == null) {
Map additionalECParameters = WeGooProvider.CONFIGURATION.getAdditionalECParameters();
if (!additionalECParameters.isEmpty()) {
ecP = (X9ECParameters) additionalECParameters.get(oid);
}
}
ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
} else if (params.isImplicitlyCA()) {
ecSpec = null;
} else {
ASN1Sequence pSeq = ASN1Sequence.getInstance(params.getParameters());
if (pSeq.size() > 3) {
X9ECParameters ecP = X9ECParameters.getInstance(pSeq);
ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
if (ecP.getH() != null) {
ecSpec = new ECParameterSpec(ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
} else {
ecSpec = new ECParameterSpec(ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), // TODO: not strictly correct... need to fix the test data...
1);
}
} else // GOST parameters
{
GOST3410PublicKeyAlgParameters gostParams = GOST3410PublicKeyAlgParameters.getInstance(pSeq);
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
curve = spec.getCurve();
ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
}
return ecSpec;
}
use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.
the class ECUtil method getDomainParameters.
public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, com.github.zhenwei.provider.jce.spec.ECParameterSpec params) {
ECDomainParameters domainParameters;
if (params instanceof ECNamedCurveParameterSpec) {
ECNamedCurveParameterSpec nParams = (ECNamedCurveParameterSpec) params;
ASN1ObjectIdentifier nameOid = ECUtil.getNamedCurveOid(nParams.getName());
domainParameters = new ECNamedDomainParameters(nameOid, nParams.getCurve(), nParams.getG(), nParams.getN(), nParams.getH(), nParams.getSeed());
} else if (params == null) {
com.github.zhenwei.provider.jce.spec.ECParameterSpec iSpec = configuration.getEcImplicitlyCa();
domainParameters = new ECDomainParameters(iSpec.getCurve(), iSpec.getG(), iSpec.getN(), iSpec.getH(), iSpec.getSeed());
} else {
domainParameters = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed());
}
return domainParameters;
}
use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.
the class BCECGOST3410PrivateKey method populateFromPrivKeyInfo.
private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
AlgorithmIdentifier pkAlg = info.getPrivateKeyAlgorithm();
ASN1Encodable pkParams = pkAlg.getParameters();
ASN1Primitive p = pkParams.toASN1Primitive();
if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3)) {
GOST3410PublicKeyAlgParameters gParams = GOST3410PublicKeyAlgParameters.getInstance(pkParams);
gostParams = gParams;
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gParams.getPublicKeyParamSet()));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof ASN1Integer) {
this.d = ASN1Integer.getInstance(privKey).getPositiveValue();
} else {
byte[] encVal = ASN1OctetString.getInstance(privKey).getOctets();
byte[] dVal = new byte[encVal.length];
for (int i = 0; i != encVal.length; i++) {
dVal[i] = encVal[encVal.length - 1 - i];
}
this.d = new BigInteger(1, dVal);
}
} else {
// for backwards compatibility
X962Parameters params = X962Parameters.getInstance(pkParams);
if (params.isNamedCurve()) {
ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
if (ecP == null) {
throw new IllegalStateException();
}
String curveName = ECUtil.getCurveName(oid);
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
ecSpec = new ECNamedCurveSpec(curveName, ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
} else if (params.isImplicitlyCA()) {
ecSpec = null;
} else {
X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
}
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof ASN1Integer) {
ASN1Integer derD = ASN1Integer.getInstance(privKey);
this.d = derD.getValue();
} else {
com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
this.d = ec.getKey();
this.publicKey = ec.getPublicKey();
}
}
}
use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.
the class BCECGOST3410PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "ECGOST3410";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
byte[] x9Encoding = new byte[65];
x9Encoding[0] = 0x04;
for (int i = 1; i <= 32; ++i) {
x9Encoding[i] = keyEnc[32 - i];
x9Encoding[i + 32] = keyEnc[64 - i];
}
ASN1ObjectIdentifier paramOID;
if (info.getAlgorithm().getParameters() instanceof ASN1ObjectIdentifier) {
paramOID = ASN1ObjectIdentifier.getInstance(info.getAlgorithm().getParameters());
gostParams = paramOID;
} else {
GOST3410PublicKeyAlgParameters params = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
gostParams = params;
paramOID = params.getPublicKeyParamSet();
}
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(paramOID));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(paramOID), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.
the class BCECGOST3410_2012PrivateKey method populateFromPrivKeyInfo.
private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
ASN1Primitive p = info.getPrivateKeyAlgorithm().getParameters().toASN1Primitive();
if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3)) {
gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
ASN1OctetString privEnc = info.getPrivateKey();
if (privEnc.getOctets().length == 32 || privEnc.getOctets().length == 64) {
this.d = new BigInteger(1, Arrays.reverse(privEnc.getOctets()));
} else {
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof ASN1Integer) {
this.d = ASN1Integer.getInstance(privKey).getPositiveValue();
} else {
byte[] encVal = ASN1OctetString.getInstance(privKey).getOctets();
this.d = new BigInteger(1, Arrays.reverse(encVal));
}
}
} else {
// for backwards compatibility
X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
if (params.isNamedCurve()) {
ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
if (// GOST Curve
ecP == null) {
X9ECParameters gParam = ECGOST3410NamedCurves.getByOIDX9(oid);
EllipticCurve ellipticCurve = EC5Util.convertCurve(gParam.getCurve(), gParam.getSeed());
ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(oid), ellipticCurve, EC5Util.convertPoint(gParam.getG()), gParam.getN(), gParam.getH());
} else {
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
}
} else if (params.isImplicitlyCA()) {
ecSpec = null;
} else {
X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
}
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof ASN1Integer) {
ASN1Integer derD = ASN1Integer.getInstance(privKey);
this.d = derD.getValue();
} else {
com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
this.d = ec.getKey();
this.publicKey = ec.getPublicKey();
}
}
}
Aggregations