use of org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters in project jmulticard by ctt-gob-es.
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 java.io.IOException on an error encoding the key
*/
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
if (publicKey instanceof QTESLAPublicKeyParameters) {
QTESLAPublicKeyParameters keyParams = (QTESLAPublicKeyParameters) publicKey;
AlgorithmIdentifier algorithmIdentifier = Utils.qTeslaLookupAlgID(keyParams.getSecurityCategory());
return new SubjectPublicKeyInfo(algorithmIdentifier, keyParams.getPublicData());
} else if (publicKey instanceof SPHINCSPublicKeyParameters) {
SPHINCSPublicKeyParameters params = (SPHINCSPublicKeyParameters) publicKey;
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.sphincs256, new SPHINCS256KeyParams(Utils.sphincs256LookupTreeAlgID(params.getTreeDigest())));
return new SubjectPublicKeyInfo(algorithmIdentifier, params.getKeyData());
} else if (publicKey instanceof NHPublicKeyParameters) {
NHPublicKeyParameters params = (NHPublicKeyParameters) publicKey;
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.newHope);
return new SubjectPublicKeyInfo(algorithmIdentifier, params.getPubData());
} else if (publicKey instanceof LMSPublicKeyParameters) {
LMSPublicKeyParameters params = (LMSPublicKeyParameters) publicKey;
byte[] encoding = Composer.compose().u32str(1).bytes(params).build();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
} else if (publicKey instanceof HSSPublicKeyParameters) {
HSSPublicKeyParameters params = (HSSPublicKeyParameters) publicKey;
byte[] encoding = Composer.compose().u32str(params.getL()).bytes(params.getLMSPublicKey()).build();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
} else if (publicKey instanceof SPHINCSPlusPublicKeyParameters) {
SPHINCSPlusPublicKeyParameters params = (SPHINCSPlusPublicKeyParameters) publicKey;
byte[] encoding = params.getEncoded();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.sphincsPlusOidLookup(params.getParameters()));
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
} else if (publicKey instanceof CMCEPublicKeyParameters) {
CMCEPublicKeyParameters params = (CMCEPublicKeyParameters) publicKey;
byte[] encoding = params.getEncoded();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mcElieceOidLookup(params.getParameters()));
// https://datatracker.ietf.org/doc/draft-uni-qsckeys/
return new SubjectPublicKeyInfo(algorithmIdentifier, new CMCEPublicKey(encoding));
} else if (publicKey instanceof XMSSPublicKeyParameters) {
XMSSPublicKeyParameters keyParams = (XMSSPublicKeyParameters) publicKey;
byte[] publicSeed = keyParams.getPublicSeed();
byte[] root = keyParams.getRoot();
byte[] keyEnc = keyParams.getEncoded();
if (keyEnc.length > publicSeed.length + root.length) {
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(IsaraObjectIdentifiers.id_alg_xmss);
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(keyEnc));
} else {
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss, new XMSSKeyParams(keyParams.getParameters().getHeight(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
return new SubjectPublicKeyInfo(algorithmIdentifier, new XMSSPublicKey(publicSeed, root));
}
} else if (publicKey instanceof XMSSMTPublicKeyParameters) {
XMSSMTPublicKeyParameters keyParams = (XMSSMTPublicKeyParameters) publicKey;
byte[] publicSeed = keyParams.getPublicSeed();
byte[] root = keyParams.getRoot();
byte[] keyEnc = keyParams.getEncoded();
if (keyEnc.length > publicSeed.length + root.length) {
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(IsaraObjectIdentifiers.id_alg_xmssmt);
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(keyEnc));
} else {
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss_mt, new XMSSMTKeyParams(keyParams.getParameters().getHeight(), keyParams.getParameters().getLayers(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
return new SubjectPublicKeyInfo(algorithmIdentifier, new XMSSMTPublicKey(keyParams.getPublicSeed(), keyParams.getRoot()));
}
} else if (publicKey instanceof McElieceCCA2PublicKeyParameters) {
McElieceCCA2PublicKeyParameters pub = (McElieceCCA2PublicKeyParameters) publicKey;
McElieceCCA2PublicKey mcEliecePub = new McElieceCCA2PublicKey(pub.getN(), pub.getT(), pub.getG(), Utils.getAlgorithmIdentifier(pub.getDigest()));
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
return new SubjectPublicKeyInfo(algorithmIdentifier, mcEliecePub);
} else if (publicKey instanceof FrodoPublicKeyParameters) {
FrodoPublicKeyParameters params = (FrodoPublicKeyParameters) publicKey;
byte[] encoding = params.getEncoded();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.frodoOidLookup(params.getParameters()));
return new SubjectPublicKeyInfo(algorithmIdentifier, (new DEROctetString(encoding)));
} else if (publicKey instanceof SABERPublicKeyParameters) {
SABERPublicKeyParameters params = (SABERPublicKeyParameters) publicKey;
byte[] encoding = params.getEncoded();
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.saberOidLookup(params.getParameters()));
// https://datatracker.ietf.org/doc/draft-uni-qsckeys/
return new SubjectPublicKeyInfo(algorithmIdentifier, new DERSequence(new DEROctetString(encoding)));
} else {
throw new IOException("key parameters not recognized");
}
}
use of org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters in project jmulticard by ctt-gob-es.
the class McElieceCCA2KeyFactorySpi method generatePublic.
public PublicKey generatePublic(SubjectPublicKeyInfo pki) throws IOException {
// get the inner type inside the BIT STRING
ASN1Primitive innerType = pki.parsePublicKey();
McElieceCCA2PublicKey key = McElieceCCA2PublicKey.getInstance(innerType);
return new BCMcElieceCCA2PublicKey(new McElieceCCA2PublicKeyParameters(key.getN(), key.getT(), key.getG(), Utils.getDigest(key.getDigest()).getAlgorithmName()));
}
use of org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters in project jmulticard by ctt-gob-es.
the class McElieceCCA2KeyFactorySpi method engineGeneratePublic.
/**
* Converts, if possible, a key specification into a
* {@link BCMcElieceCCA2PublicKey}. Currently, the following key
* specifications are supported:
* {@link X509EncodedKeySpec}.
*
* @param keySpec the key specification
* @return the McEliece CCA2 public key
* @throws InvalidKeySpecException if the key specification is not supported.
*/
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec instanceof X509EncodedKeySpec) {
// get the DER-encoded Key according to X.509 from the spec
byte[] encKey = ((X509EncodedKeySpec) keySpec).getEncoded();
// decode the SubjectPublicKeyInfo data structure to the pki object
SubjectPublicKeyInfo pki;
try {
pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey));
} catch (IOException e) {
throw new InvalidKeySpecException(e.toString());
}
try {
if (PQCObjectIdentifiers.mcElieceCca2.equals(pki.getAlgorithm().getAlgorithm())) {
McElieceCCA2PublicKey key = McElieceCCA2PublicKey.getInstance(pki.parsePublicKey());
return new BCMcElieceCCA2PublicKey(new McElieceCCA2PublicKeyParameters(key.getN(), key.getT(), key.getG(), Utils.getDigest(key.getDigest()).getAlgorithmName()));
} else {
throw new InvalidKeySpecException("Unable to recognise OID in McEliece private key");
}
} catch (IOException cce) {
throw new InvalidKeySpecException("Unable to decode X509EncodedKeySpec: " + cce.getMessage());
}
}
throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Aggregations