use of org.bouncycastle.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 org.bouncycastle.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 org.bouncycastle.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 org.bouncycastle.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;
}
}
use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class JCEECPublicKey method getEncoded.
public byte[] getEncoded() {
ASN1Encodable params;
SubjectPublicKeyInfo info;
if (algorithm.equals("ECGOST3410")) {
if (gostParams != null) {
params = gostParams;
} else {
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.q.getAffineXCoord().toBigInteger();
BigInteger bY = this.q.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;
}
} else {
if (ecSpec instanceof ECNamedCurveSpec) {
ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
if (curveOid == null) {
curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
}
params = new X962Parameters(curveOid);
} else if (ecSpec == null) {
params = new X962Parameters(DERNull.INSTANCE);
} else {
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);
}
byte[] pubKeyOctets = this.getQ().getEncoded(withCompression);
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), pubKeyOctets);
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
Aggregations