package org.bouncycastle.pqc.crypto.util;
import java.io.IOException;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.asn1.PQCObjectIdentifiers;
import org.bouncycastle.pqc.asn1.SPHINCS256KeyParams;
import org.bouncycastle.pqc.asn1.XMSSKeyParams;
import org.bouncycastle.pqc.asn1.XMSSMTKeyParams;
import org.bouncycastle.pqc.asn1.XMSSMTPublicKey;
import org.bouncycastle.pqc.asn1.XMSSPublicKey;
import org.bouncycastle.pqc.crypto.newhope.NHPublicKeyParameters;
import org.bouncycastle.pqc.crypto.qtesla.QTESLAPublicKeyParameters;
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSPublicKeyParameters;
import org.bouncycastle.pqc.crypto.xmss.XMSSMTPublicKeyParameters;
import org.bouncycastle.pqc.crypto.xmss.XMSSPublicKeyParameters;
public class SubjectPublicKeyInfoFactory
{
private SubjectPublicKeyInfoFactory()
{
}
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 XMSSPublicKeyParameters)
{
XMSSPublicKeyParameters keyParams = (XMSSPublicKeyParameters)publicKey;
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss,
new XMSSKeyParams(keyParams.getParameters().getHeight(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
return new SubjectPublicKeyInfo(algorithmIdentifier, new XMSSPublicKey(keyParams.getPublicSeed(), keyParams.getRoot()));
}
else if (publicKey instanceof XMSSMTPublicKeyParameters)
{
XMSSMTPublicKeyParameters keyParams = (XMSSMTPublicKeyParameters)publicKey;
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
{
throw new IOException("key parameters not recognized");
}
}
}