PqcSubjectPublicKeyInfoFactory
A factory to produce Public Key Info Objects.
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pqc.Asn1;
using Org.BouncyCastle.Pqc.Crypto.Bike;
using Org.BouncyCastle.Pqc.Crypto.Cmce;
using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium;
using Org.BouncyCastle.Pqc.Crypto.Falcon;
using Org.BouncyCastle.Pqc.Crypto.Frodo;
using Org.BouncyCastle.Pqc.Crypto.Hqc;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using Org.BouncyCastle.Pqc.Crypto.Ntru;
using Org.BouncyCastle.Pqc.Crypto.Picnic;
using Org.BouncyCastle.Pqc.Crypto.Saber;
using Org.BouncyCastle.Pqc.Crypto.SphincsPlus;
using Org.BouncyCastle.Utilities;
using System;
namespace Org.BouncyCastle.Pqc.Crypto.Utilities
{
public static class PqcSubjectPublicKeyInfoFactory
{
public static SubjectPublicKeyInfo CreateSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey)
{
if (publicKey == null)
throw new ArgumentNullException("publicKey");
if (publicKey.IsPrivate)
throw new ArgumentException("Private key passed - public key expected.", "publicKey");
LmsPublicKeyParameters lmsPublicKeyParameters = publicKey as LmsPublicKeyParameters;
if (lmsPublicKeyParameters != null) {
byte[] publicKey2 = Composer.Compose().U32Str(1).Bytes(lmsPublicKeyParameters)
.Build();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), publicKey2);
}
HssPublicKeyParameters hssPublicKeyParameters = publicKey as HssPublicKeyParameters;
if (hssPublicKeyParameters != null) {
int level = hssPublicKeyParameters.Level;
byte[] publicKey3 = Composer.Compose().U32Str(level).Bytes(hssPublicKeyParameters.LmsPublicKey)
.Build();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), publicKey3);
}
SphincsPlusPublicKeyParameters sphincsPlusPublicKeyParameters = publicKey as SphincsPlusPublicKeyParameters;
if (sphincsPlusPublicKeyParameters != null) {
byte[] encoded = sphincsPlusPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.SphincsPlusOidLookup(sphincsPlusPublicKeyParameters.Parameters)), encoded);
}
CmcePublicKeyParameters cmcePublicKeyParameters = publicKey as CmcePublicKeyParameters;
if (cmcePublicKeyParameters != null) {
byte[] encoded2 = cmcePublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(cmcePublicKeyParameters.Parameters)), new CmcePublicKey(encoded2));
}
FrodoPublicKeyParameters frodoPublicKeyParameters = publicKey as FrodoPublicKeyParameters;
if (frodoPublicKeyParameters != null) {
byte[] encoded3 = frodoPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.FrodoOidLookup(frodoPublicKeyParameters.Parameters)), new DerOctetString(encoded3));
}
SaberPublicKeyParameters saberPublicKeyParameters = publicKey as SaberPublicKeyParameters;
if (saberPublicKeyParameters != null) {
byte[] encoded4 = saberPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.SaberOidLookup(saberPublicKeyParameters.Parameters)), new DerSequence(new DerOctetString(encoded4)));
}
PicnicPublicKeyParameters picnicPublicKeyParameters = publicKey as PicnicPublicKeyParameters;
if (picnicPublicKeyParameters != null) {
byte[] encoded5 = picnicPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.PicnicOidLookup(picnicPublicKeyParameters.Parameters)), new DerOctetString(encoded5));
}
FalconPublicKeyParameters falconPublicKeyParameters = publicKey as FalconPublicKeyParameters;
if (falconPublicKeyParameters != null) {
byte[] encoded6 = falconPublicKeyParameters.GetEncoded();
byte[] array = new byte[encoded6.Length + 1];
array[0] = (byte)falconPublicKeyParameters.Parameters.LogN;
Array.Copy(encoded6, 0, array, 1, encoded6.Length);
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.FalconOidLookup(falconPublicKeyParameters.Parameters)), array);
}
DilithiumPublicKeyParameters dilithiumPublicKeyParameters = publicKey as DilithiumPublicKeyParameters;
if (dilithiumPublicKeyParameters != null)
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.DilithiumOidLookup(dilithiumPublicKeyParameters.Parameters)), dilithiumPublicKeyParameters.GetEncoded());
BikePublicKeyParameters bikePublicKeyParameters = publicKey as BikePublicKeyParameters;
if (bikePublicKeyParameters != null) {
byte[] encoded7 = bikePublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.BikeOidLookup(bikePublicKeyParameters.Parameters)), encoded7);
}
HqcPublicKeyParameters hqcPublicKeyParameters = publicKey as HqcPublicKeyParameters;
if (hqcPublicKeyParameters != null) {
byte[] encoded8 = hqcPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.HqcOidLookup(hqcPublicKeyParameters.Parameters)), encoded8);
}
NtruPublicKeyParameters ntruPublicKeyParameters = publicKey as NtruPublicKeyParameters;
if (ntruPublicKeyParameters != null) {
byte[] encoded9 = ntruPublicKeyParameters.GetEncoded();
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PqcUtilities.NtruOidLookup(ntruPublicKeyParameters.Parameters)), encoded9);
}
throw new ArgumentException("Class provided no convertible: " + Platform.GetTypeName(publicKey));
}
}
}