use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class PrivateKeyInfoFactory method createPrivateKeyInfo.
/**
* Create a PrivateKeyInfo representation of a private key with attributes.
*
* @param privateKey the key to be encoded into the info object.
* @param attributes the set of attributes to be included.
* @return the appropriate PrivateKeyInfo
* @throws IOException on an error encoding the key
*/
public static PrivateKeyInfo createPrivateKeyInfo(AsymmetricKeyParameter privateKey, ASN1Set attributes) throws IOException {
if (privateKey instanceof RSAKeyParameters) {
RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) privateKey;
return new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPrivateKey(priv.getModulus(), priv.getPublicExponent(), priv.getExponent(), priv.getP(), priv.getQ(), priv.getDP(), priv.getDQ(), priv.getQInv()), attributes);
} else if (privateKey instanceof DSAPrivateKeyParameters) {
DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) privateKey;
DSAParameters params = priv.getParameters();
return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(params.getP(), params.getQ(), params.getG())), new ASN1Integer(priv.getX()), attributes);
} else if (privateKey instanceof ECPrivateKeyParameters) {
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) privateKey;
ECDomainParameters domainParams = priv.getParameters();
ASN1Encodable params;
int orderBitLength;
if (domainParams == null) {
// Implicitly CA
params = new X962Parameters(DERNull.INSTANCE);
orderBitLength = priv.getD().bitLength();
} else if (domainParams instanceof ECGOST3410Parameters) {
GOST3410PublicKeyAlgParameters gostParams = new GOST3410PublicKeyAlgParameters(((ECGOST3410Parameters) domainParams).getPublicKeyParamSet(), ((ECGOST3410Parameters) domainParams).getDigestParamSet(), ((ECGOST3410Parameters) domainParams).getEncryptionParamSet());
int size;
ASN1ObjectIdentifier identifier;
if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
size = 32;
identifier = CryptoProObjectIdentifiers.gostR3410_2001;
} else {
boolean is512 = priv.getD().bitLength() > 256;
identifier = (is512) ? RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512 : RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
size = (is512) ? 64 : 32;
}
byte[] encKey = new byte[size];
extractBytes(encKey, size, 0, priv.getD());
return new PrivateKeyInfo(new AlgorithmIdentifier(identifier, gostParams), new DEROctetString(encKey));
} else if (domainParams instanceof ECNamedDomainParameters) {
params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
orderBitLength = domainParams.getN().bitLength();
} else {
X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
params = new X962Parameters(ecP);
orderBitLength = domainParams.getN().bitLength();
}
ECPoint q = new FixedPointCombMultiplier().multiply(domainParams.getG(), priv.getD());
// TODO Support point compression
DERBitString publicKey = new DERBitString(q.getEncoded(false));
return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), new ECPrivateKey(orderBitLength, priv.getD(), publicKey, params), attributes);
} else if (privateKey instanceof X448PrivateKeyParameters) {
X448PrivateKeyParameters key = (X448PrivateKeyParameters) privateKey;
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
} else if (privateKey instanceof X25519PrivateKeyParameters) {
X25519PrivateKeyParameters key = (X25519PrivateKeyParameters) privateKey;
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
} else if (privateKey instanceof Ed448PrivateKeyParameters) {
Ed448PrivateKeyParameters key = (Ed448PrivateKeyParameters) privateKey;
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
} else if (privateKey instanceof Ed25519PrivateKeyParameters) {
Ed25519PrivateKeyParameters key = (Ed25519PrivateKeyParameters) privateKey;
return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), new DEROctetString(key.getEncoded()), attributes, key.generatePublicKey().getEncoded());
} else {
throw new IOException("key parameters not recognized");
}
}
use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class BCECGOST3410_2012PublicKey method getEncoded.
public byte[] getEncoded() {
ASN1Encodable params;
SubjectPublicKeyInfo info;
// ecPublicKey.getQ().
BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = this.ecPublicKey.getQ().getAffineYCoord().toBigInteger();
// need to detect key size
boolean is512 = (bX.bitLength() > 256);
params = getGostParams();
if (params == null) {
if (ecSpec instanceof ECNamedCurveSpec) {
if (is512) {
params = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512);
} else {
params = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256);
}
} 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);
}
}
int encKeySize;
int offset;
ASN1ObjectIdentifier algIdentifier;
if (is512) {
encKeySize = 128;
offset = 64;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
} else {
encKeySize = 64;
offset = 32;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
}
byte[] encKey = new byte[encKeySize];
extractBytes(encKey, encKeySize / 2, 0, bX);
extractBytes(encKey, encKeySize / 2, offset, bY);
try {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, 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 BCECGOST3410_2012PublicKey method getGostParams.
public GOST3410PublicKeyAlgParameters getGostParams() {
if (gostParams == null && ecSpec instanceof ECNamedCurveSpec) {
BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
// need to detect key size
boolean is512 = (bX.bitLength() > 256);
if (is512) {
this.gostParams = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512);
} else {
this.gostParams = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256);
}
}
return gostParams;
}
use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.
the class BCGOST3410PublicKey method getEncoded.
public byte[] getEncoded() {
SubjectPublicKeyInfo info;
byte[] keyEnc = this.getY().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) {
if (gost3410Spec.getEncryptionParamSetOID() != null) {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getEncryptionParamSetOID()))), new DEROctetString(keyBytes));
} else {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
}
} else {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
} catch (IOException e) {
return null;
}
}
Aggregations