use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project jmulticard by ctt-gob-es.
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 = BouncyCastleProvider.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.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class BCECGOST3410PublicKey method getEncoded.
public byte[] getEncoded() {
ASN1Encodable params;
SubjectPublicKeyInfo info;
params = getGostParams();
if (params == null) {
if (ecSpec instanceof ECNamedCurveSpec) {
params = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
} else {
// strictly speaking this may not be applicable...
ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
X9ECParameters ecP = new X9ECParameters(curve, new X9ECPoint(EC5Util.convertPoint(curve, ecSpec.getGenerator()), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
params = new X962Parameters(ecP);
}
}
BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = this.ecPublicKey.getQ().getAffineYCoord().toBigInteger();
byte[] encKey = new byte[64];
extractBytes(encKey, 0, bX);
extractBytes(encKey, 32, bY);
try {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
} catch (IOException e) {
return null;
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class BCGOST3410PrivateKey method getEncoded.
/**
* Return a PKCS8 representation of the key. The sequence returned represents a full
* PrivateKeyInfo object.
*
* @return a PKCS8 representation of the key.
*/
public byte[] getEncoded() {
PrivateKeyInfo info;
byte[] keyEnc = this.getX().toByteArray();
byte[] keyBytes;
if (keyEnc[0] == 0) {
keyBytes = new byte[keyEnc.length - 1];
} else {
keyBytes = new byte[keyEnc.length];
}
for (int i = 0; i != keyBytes.length; i++) {
// must be little endian
keyBytes[i] = keyEnc[keyEnc.length - 1 - i];
}
try {
if (gost3410Spec instanceof GOST3410ParameterSpec) {
info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
} else {
info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
}
return info.getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
return null;
}
}
Aggregations