use of org.bouncycastle.pqc.crypto.xmss.XMSSPrivateKeyParameters in project jmulticard by ctt-gob-es.
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 java.io.IOException on an error encoding the key
*/
public static PrivateKeyInfo createPrivateKeyInfo(final AsymmetricKeyParameter privateKey, final ASN1Set attributes) throws IOException {
if (privateKey instanceof QTESLAPrivateKeyParameters) {
final QTESLAPrivateKeyParameters keyParams = (QTESLAPrivateKeyParameters) privateKey;
final AlgorithmIdentifier algorithmIdentifier = Utils.qTeslaLookupAlgID(keyParams.getSecurityCategory());
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(keyParams.getSecret()), attributes);
}
if (privateKey instanceof SPHINCSPrivateKeyParameters) {
final SPHINCSPrivateKeyParameters params = (SPHINCSPrivateKeyParameters) privateKey;
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.sphincs256, new SPHINCS256KeyParams(Utils.sphincs256LookupTreeAlgID(params.getTreeDigest())));
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(params.getKeyData()));
}
if (privateKey instanceof NHPrivateKeyParameters) {
final NHPrivateKeyParameters params = (NHPrivateKeyParameters) privateKey;
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.newHope);
final short[] privateKeyData = params.getSecData();
final byte[] octets = new byte[privateKeyData.length * 2];
for (int i = 0; i != privateKeyData.length; i++) {
Pack.shortToLittleEndian(privateKeyData[i], octets, i * 2);
}
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(octets));
}
if (privateKey instanceof LMSPrivateKeyParameters) {
final LMSPrivateKeyParameters params = (LMSPrivateKeyParameters) privateKey;
final byte[] encoding = Composer.compose().u32str(1).bytes(params).build();
final byte[] pubEncoding = Composer.compose().u32str(1).bytes(params.getPublicKey()).build();
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
}
if (privateKey instanceof HSSPrivateKeyParameters) {
final HSSPrivateKeyParameters params = (HSSPrivateKeyParameters) privateKey;
final byte[] encoding = Composer.compose().u32str(params.getL()).bytes(params).build();
final byte[] pubEncoding = Composer.compose().u32str(params.getL()).bytes(params.getPublicKey().getLMSPublicKey()).build();
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
}
if (privateKey instanceof SPHINCSPlusPrivateKeyParameters) {
final SPHINCSPlusPrivateKeyParameters params = (SPHINCSPlusPrivateKeyParameters) privateKey;
final byte[] encoding = params.getEncoded();
final byte[] pubEncoding = params.getEncodedPublicKey();
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.sphincsPlusOidLookup(params.getParameters()));
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
}
if (privateKey instanceof CMCEPrivateKeyParameters) {
final CMCEPrivateKeyParameters params = (CMCEPrivateKeyParameters) privateKey;
final byte[] encoding = params.getEncoded();
// todo either make CMCEPrivateKey split the parameters from the private key or
// (current) Make CMCEPrivateKey take parts of the private key splitted in the params
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mcElieceOidLookup(params.getParameters()));
final CMCEPublicKey cmcePub = new CMCEPublicKey(params.reconstructPublicKey());
final CMCEPrivateKey cmcePriv = new CMCEPrivateKey(0, params.getDelta(), params.getC(), params.getG(), params.getAlpha(), params.getS(), cmcePub);
return new PrivateKeyInfo(algorithmIdentifier, cmcePriv, attributes);
} else if (privateKey instanceof XMSSPrivateKeyParameters || privateKey instanceof XMSSMTPrivateKeyParameters) {
// $NON-NLS-1$
throw new IOException("Modificacion para JMultiCard");
} else if (privateKey instanceof McElieceCCA2PrivateKeyParameters) {
final McElieceCCA2PrivateKeyParameters priv = (McElieceCCA2PrivateKeyParameters) privateKey;
final McElieceCCA2PrivateKey mcEliecePriv = new McElieceCCA2PrivateKey(priv.getN(), priv.getK(), priv.getField(), priv.getGoppaPoly(), priv.getP(), Utils.getAlgorithmIdentifier(priv.getDigest()));
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
return new PrivateKeyInfo(algorithmIdentifier, mcEliecePriv);
} else if (privateKey instanceof FrodoPrivateKeyParameters) {
final FrodoPrivateKeyParameters params = (FrodoPrivateKeyParameters) privateKey;
final byte[] encoding = params.getEncoded();
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.frodoOidLookup(params.getParameters()));
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes);
} else if (privateKey instanceof SABERPrivateKeyParameters) {
final SABERPrivateKeyParameters params = (SABERPrivateKeyParameters) privateKey;
final byte[] encoding = params.getEncoded();
final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.saberOidLookup(params.getParameters()));
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes);
} else {
// $NON-NLS-1$
throw new IOException("key parameters not recognized");
}
}
Aggregations