use of com.github.zhenwei.core.crypto.params.X448PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyFactorySpi method engineGeneratePublic.
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec instanceof X509EncodedKeySpec) {
byte[] enc = ((X509EncodedKeySpec) keySpec).getEncoded();
// optimise if we can
if ((specificBase == 0 || specificBase == enc[8])) {
// watch out for badly placed DER NULL - the default X509Cert will add these!
if (enc[9] == 0x05 && enc[10] == 0x00) {
SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(enc);
keyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(keyInfo.getAlgorithm().getAlgorithm()), keyInfo.getPublicKeyData().getBytes());
try {
enc = keyInfo.getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
throw new InvalidKeySpecException("attempt to reconstruct key failed: " + e.getMessage());
}
}
switch(enc[8]) {
case x448_type:
return new BCXDHPublicKey(x448Prefix, enc);
case x25519_type:
return new BCXDHPublicKey(x25519Prefix, enc);
case Ed448_type:
return new BCEdDSAPublicKey(Ed448Prefix, enc);
case Ed25519_type:
return new BCEdDSAPublicKey(Ed25519Prefix, enc);
default:
return super.engineGeneratePublic(keySpec);
}
}
} else if (keySpec instanceof RawEncodedKeySpec) {
byte[] enc = ((RawEncodedKeySpec) keySpec).getEncoded();
switch(specificBase) {
case x448_type:
return new BCXDHPublicKey(new X448PublicKeyParameters(enc));
case x25519_type:
return new BCXDHPublicKey(new X25519PublicKeyParameters(enc));
case Ed448_type:
return new BCEdDSAPublicKey(new Ed448PublicKeyParameters(enc));
case Ed25519_type:
return new BCEdDSAPublicKey(new Ed25519PublicKeyParameters(enc));
default:
throw new InvalidKeySpecException("factory not a specific type, cannot recognise raw encoding");
}
} else if (keySpec instanceof OpenSSHPublicKeySpec) {
CipherParameters parameters = OpenSSHPublicKeyUtil.parsePublicKey(((OpenSSHPublicKeySpec) keySpec).getEncoded());
if (parameters instanceof Ed25519PublicKeyParameters) {
return new BCEdDSAPublicKey(new byte[0], ((Ed25519PublicKeyParameters) parameters).getEncoded());
}
throw new IllegalStateException("openssh public key not Ed25519 public key");
}
return super.engineGeneratePublic(keySpec);
}
use of com.github.zhenwei.core.crypto.params.X448PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class X448KeyPairGenerator method generateKeyPair.
public AsymmetricCipherKeyPair generateKeyPair() {
X448PrivateKeyParameters privateKey = new X448PrivateKeyParameters(random);
X448PublicKeyParameters publicKey = privateKey.generatePublicKey();
return new AsymmetricCipherKeyPair(publicKey, privateKey);
}
use of com.github.zhenwei.core.crypto.params.X448PublicKeyParameters 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");
}
}
Aggregations