use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class SubjectPublicKeyInfoFactory method createSubjectPublicKeyInfo.
/**
* Create a SubjectPublicKeyInfo public key.
*
* @param publicKey the key to be encoded into the info object.
* @return a SubjectPublicKeyInfo representing the key.
* @throws IOException on an error encoding the key
*/
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
if (publicKey instanceof RSAKeyParameters) {
RSAKeyParameters pub = (RSAKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(pub.getModulus(), pub.getExponent()));
} else if (publicKey instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters pub = (DSAPublicKeyParameters) publicKey;
DSAParameter params = null;
DSAParameters dsaParams = pub.getParameters();
if (dsaParams != null) {
params = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
}
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, params), new ASN1Integer(pub.getY()));
} else if (publicKey instanceof ECPublicKeyParameters) {
ECPublicKeyParameters pub = (ECPublicKeyParameters) publicKey;
ECDomainParameters domainParams = pub.getParameters();
ASN1Encodable params;
if (domainParams == null) {
// Implicitly CA
params = new X962Parameters(DERNull.INSTANCE);
} else if (domainParams instanceof ECGOST3410Parameters) {
ECGOST3410Parameters gostParams = (ECGOST3410Parameters) domainParams;
BigInteger bX = pub.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = pub.getQ().getAffineYCoord().toBigInteger();
params = new GOST3410PublicKeyAlgParameters(gostParams.getPublicKeyParamSet(), gostParams.getDigestParamSet());
int encKeySize;
int offset;
ASN1ObjectIdentifier algIdentifier;
if (cryptoProOids.contains(gostParams.getPublicKeyParamSet())) {
encKeySize = 64;
offset = 32;
algIdentifier = CryptoProObjectIdentifiers.gostR3410_2001;
} else {
boolean is512 = (bX.bitLength() > 256);
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 {
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, params), new DEROctetString(encKey));
} catch (IOException e) {
return null;
}
} else if (domainParams instanceof ECNamedDomainParameters) {
params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
} else {
X9ECParameters ecP = new X9ECParameters(domainParams.getCurve(), // TODO Support point compression
new X9ECPoint(domainParams.getG(), false), domainParams.getN(), domainParams.getH(), domainParams.getSeed());
params = new X962Parameters(ecP);
}
// TODO Support point compression
byte[] pubKeyOctets = pub.getQ().getEncoded(false);
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), pubKeyOctets);
} else if (publicKey instanceof X448PublicKeyParameters) {
X448PublicKeyParameters key = (X448PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.getEncoded());
} else if (publicKey instanceof X25519PublicKeyParameters) {
X25519PublicKeyParameters key = (X25519PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.getEncoded());
} else if (publicKey instanceof Ed448PublicKeyParameters) {
Ed448PublicKeyParameters key = (Ed448PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), key.getEncoded());
} else if (publicKey instanceof Ed25519PublicKeyParameters) {
Ed25519PublicKeyParameters key = (Ed25519PublicKeyParameters) publicKey;
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), key.getEncoded());
} else {
throw new IOException("key parameters not recognized");
}
}
use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class OpenSSHPrivateKeyUtil method encodePrivateKey.
/**
* Encode a cipher parameters into an OpenSSH private key. This does not add headers like
* ----BEGIN RSA PRIVATE KEY----
*
* @param params the cipher parameters.
* @return a byte array
*/
public static byte[] encodePrivateKey(AsymmetricKeyParameter params) throws IOException {
if (params == null) {
throw new IllegalArgumentException("param is null");
}
if (params instanceof RSAPrivateCrtKeyParameters) {
PrivateKeyInfo pInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(params);
return pInfo.parsePrivateKey().toASN1Primitive().getEncoded();
} else if (params instanceof ECPrivateKeyParameters) {
PrivateKeyInfo pInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(params);
return pInfo.parsePrivateKey().toASN1Primitive().getEncoded();
} else if (params instanceof DSAPrivateKeyParameters) {
DSAPrivateKeyParameters dsaPrivKey = (DSAPrivateKeyParameters) params;
DSAParameters dsaParams = dsaPrivKey.getParameters();
ASN1EncodableVector vec = new ASN1EncodableVector();
vec.add(new ASN1Integer(0));
vec.add(new ASN1Integer(dsaParams.getP()));
vec.add(new ASN1Integer(dsaParams.getQ()));
vec.add(new ASN1Integer(dsaParams.getG()));
// public key = g.modPow(x, p);
BigInteger pubKey = dsaParams.getG().modPow(dsaPrivKey.getX(), dsaParams.getP());
vec.add(new ASN1Integer(pubKey));
vec.add(new ASN1Integer(dsaPrivKey.getX()));
try {
return new DERSequence(vec).getEncoded();
} catch (Exception ex) {
throw new IllegalStateException("unable to encode DSAPrivateKeyParameters " + ex.getMessage());
}
} else if (params instanceof Ed25519PrivateKeyParameters) {
Ed25519PublicKeyParameters publicKeyParameters = ((Ed25519PrivateKeyParameters) params).generatePublicKey();
SSHBuilder builder = new SSHBuilder();
builder.writeBytes(AUTH_MAGIC);
// cipher name
builder.writeString("none");
// KDF name
builder.writeString("none");
// KDF options
builder.writeString("");
// Number of keys
builder.u32(1);
{
byte[] pkEncoded = OpenSSHPublicKeyUtil.encodePublicKey(publicKeyParameters);
builder.writeBlock(pkEncoded);
}
{
SSHBuilder pkBuild = new SSHBuilder();
int checkint = CryptoServicesRegistrar.getSecureRandom().nextInt();
pkBuild.u32(checkint);
pkBuild.u32(checkint);
pkBuild.writeString("ssh-ed25519");
// Public key (as part of private key pair)
byte[] pubKeyEncoded = publicKeyParameters.getEncoded();
pkBuild.writeBlock(pubKeyEncoded);
// The private key in SSH is 64 bytes long and is the concatenation of the private and the public keys
pkBuild.writeBlock(Arrays.concatenate(((Ed25519PrivateKeyParameters) params).getEncoded(), pubKeyEncoded));
// Comment for this private key (empty)
pkBuild.writeString("");
builder.writeBlock(pkBuild.getPaddedBytes());
}
return builder.getBytes();
}
throw new IllegalArgumentException("unable to convert " + params.getClass().getName() + " to openssh private key");
}
use of com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class OpenSSHPublicKeyUtil method encodePublicKey.
/**
* Encode a public key from an AsymmetricKeyParameter instance.
*
* @param cipherParameters The key to encode.
* @return the key OpenSSH encoded.
* @throws IOException
*/
public static byte[] encodePublicKey(AsymmetricKeyParameter cipherParameters) throws IOException {
if (cipherParameters == null) {
throw new IllegalArgumentException("cipherParameters was null.");
}
if (cipherParameters instanceof RSAKeyParameters) {
if (cipherParameters.isPrivate()) {
throw new IllegalArgumentException("RSAKeyParamaters was for encryption");
}
RSAKeyParameters rsaPubKey = (RSAKeyParameters) cipherParameters;
SSHBuilder builder = new SSHBuilder();
builder.writeString(RSA);
builder.writeBigNum(rsaPubKey.getExponent());
builder.writeBigNum(rsaPubKey.getModulus());
return builder.getBytes();
} else if (cipherParameters instanceof ECPublicKeyParameters) {
SSHBuilder builder = new SSHBuilder();
//
// checked for named curve parameters..
//
String name = SSHNamedCurves.getNameForParameters(((ECPublicKeyParameters) cipherParameters).getParameters());
if (name == null) {
throw new IllegalArgumentException("unable to derive ssh curve name for " + ((ECPublicKeyParameters) cipherParameters).getParameters().getCurve().getClass().getName());
}
// Magic
builder.writeString(ECDSA + "-sha2-" + name);
builder.writeString(name);
builder.writeBlock(// Uncompressed
((ECPublicKeyParameters) cipherParameters).getQ().getEncoded(false));
return builder.getBytes();
} else if (cipherParameters instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters dsaPubKey = (DSAPublicKeyParameters) cipherParameters;
DSAParameters dsaParams = dsaPubKey.getParameters();
SSHBuilder builder = new SSHBuilder();
builder.writeString(DSS);
builder.writeBigNum(dsaParams.getP());
builder.writeBigNum(dsaParams.getQ());
builder.writeBigNum(dsaParams.getG());
builder.writeBigNum(dsaPubKey.getY());
return builder.getBytes();
} else if (cipherParameters instanceof Ed25519PublicKeyParameters) {
SSHBuilder builder = new SSHBuilder();
builder.writeString(ED_25519);
builder.writeBlock(((Ed25519PublicKeyParameters) cipherParameters).getEncoded());
return builder.getBytes();
}
throw new IllegalArgumentException("unable to convert " + cipherParameters.getClass().getName() + " to private key");
}
Aggregations