<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.0" />

PqcPublicKeyFactory

public static class PqcPublicKeyFactory
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; 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 PqcPrivateKeyInfoFactory { public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey) { return CreatePrivateKeyInfo(privateKey, null); } public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) { LmsPrivateKeyParameters lmsPrivateKeyParameters = privateKey as LmsPrivateKeyParameters; LmsPrivateKeyParameters lmsPrivateKeyParameters; if (lmsPrivateKeyParameters != null) { byte[] contents = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters) .Build(); byte[] publicKey = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()) .Build(); byte[] contents; byte[] publicKey; return new PrivateKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), new DerOctetString(contents), attributes, publicKey); } HssPrivateKeyParameters hssPrivateKeyParameters = privateKey as HssPrivateKeyParameters; HssPrivateKeyParameters hssPrivateKeyParameters; if (hssPrivateKeyParameters != null) { int level = hssPrivateKeyParameters.Level; int level; byte[] contents2 = Composer.Compose().U32Str(level).Bytes(hssPrivateKeyParameters) .Build(); byte[] publicKey2 = Composer.Compose().U32Str(level).Bytes(hssPrivateKeyParameters.GetPublicKey().LmsPublicKey) .Build(); byte[] contents2; byte[] publicKey2; return new PrivateKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), new DerOctetString(contents2), attributes, publicKey2); } SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters = privateKey as SphincsPlusPrivateKeyParameters; SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters; if (sphincsPlusPrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.SphincsPlusOidLookup(sphincsPlusPrivateKeyParameters.Parameters)), new DerOctetString(sphincsPlusPrivateKeyParameters.GetEncoded()), attributes); CmcePrivateKeyParameters cmcePrivateKeyParameters = privateKey as CmcePrivateKeyParameters; CmcePrivateKeyParameters cmcePrivateKeyParameters; if (cmcePrivateKeyParameters != null) { cmcePrivateKeyParameters.GetEncoded(); AlgorithmIdentifier privateKeyAlgorithm = new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(cmcePrivateKeyParameters.Parameters)); CmcePrivateKey privateKey2 = new CmcePrivateKey(pubKey: new CmcePublicKey(cmcePrivateKeyParameters.ReconstructPublicKey()), version: 0, delta: cmcePrivateKeyParameters.Delta, c: cmcePrivateKeyParameters.C, g: cmcePrivateKeyParameters.G, alpha: cmcePrivateKeyParameters.Alpha, s: cmcePrivateKeyParameters.S); AlgorithmIdentifier privateKeyAlgorithm; CmcePrivateKey privateKey2; return new PrivateKeyInfo(privateKeyAlgorithm, privateKey2, attributes); } FrodoPrivateKeyParameters frodoPrivateKeyParameters = privateKey as FrodoPrivateKeyParameters; FrodoPrivateKeyParameters frodoPrivateKeyParameters; if (frodoPrivateKeyParameters != null) { byte[] encoded = frodoPrivateKeyParameters.GetEncoded(); byte[] encoded; return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.FrodoOidLookup(frodoPrivateKeyParameters.Parameters)), new DerOctetString(encoded), attributes); } SaberPrivateKeyParameters saberPrivateKeyParameters = privateKey as SaberPrivateKeyParameters; SaberPrivateKeyParameters saberPrivateKeyParameters; if (saberPrivateKeyParameters != null) { byte[] encoded2 = saberPrivateKeyParameters.GetEncoded(); byte[] encoded2; return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.SaberOidLookup(saberPrivateKeyParameters.Parameters)), new DerOctetString(encoded2), attributes); } PicnicPrivateKeyParameters picnicPrivateKeyParameters = privateKey as PicnicPrivateKeyParameters; PicnicPrivateKeyParameters picnicPrivateKeyParameters; if (picnicPrivateKeyParameters != null) { byte[] encoded3 = picnicPrivateKeyParameters.GetEncoded(); byte[] encoded3; return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.PicnicOidLookup(picnicPrivateKeyParameters.Parameters)), new DerOctetString(encoded3), attributes); } FalconPrivateKeyParameters falconPrivateKeyParameters = privateKey as FalconPrivateKeyParameters; FalconPrivateKeyParameters falconPrivateKeyParameters; if (falconPrivateKeyParameters != null) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(4); Asn1EncodableVector asn1EncodableVector; asn1EncodableVector.Add(DerInteger.One); asn1EncodableVector.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyLittleF())); asn1EncodableVector.Add(new DerOctetString(falconPrivateKeyParameters.GetG())); asn1EncodableVector.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyBigF())); return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.FalconOidLookup(falconPrivateKeyParameters.Parameters)), new DerSequence(asn1EncodableVector), attributes, falconPrivateKeyParameters.GetPublicKey()); } DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters = privateKey as DilithiumPrivateKeyParameters; DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters; if (dilithiumPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm2 = new AlgorithmIdentifier(PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); DilithiumPublicKeyParameters publicKeyParameters = dilithiumPrivateKeyParameters.GetPublicKeyParameters(); AlgorithmIdentifier privateKeyAlgorithm2; DilithiumPublicKeyParameters publicKeyParameters; return new PrivateKeyInfo(privateKeyAlgorithm2, new DerOctetString(dilithiumPrivateKeyParameters.GetEncoded()), attributes, publicKeyParameters.GetEncoded()); } BikePrivateKeyParameters bikePrivateKeyParameters = privateKey as BikePrivateKeyParameters; BikePrivateKeyParameters bikePrivateKeyParameters; if (bikePrivateKeyParameters != null) { byte[] encoded4 = bikePrivateKeyParameters.GetEncoded(); byte[] encoded4; return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.BikeOidLookup(bikePrivateKeyParameters.Parameters)), new DerOctetString(encoded4), attributes); } HqcPrivateKeyParameters hqcPrivateKeyParameters = privateKey as HqcPrivateKeyParameters; HqcPrivateKeyParameters hqcPrivateKeyParameters; if (hqcPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm3 = new AlgorithmIdentifier(PqcUtilities.HqcOidLookup(hqcPrivateKeyParameters.Parameters)); byte[] privateKey3 = hqcPrivateKeyParameters.PrivateKey; AlgorithmIdentifier privateKeyAlgorithm3; byte[] privateKey3; return new PrivateKeyInfo(privateKeyAlgorithm3, new DerOctetString(privateKey3), attributes); } NtruPrivateKeyParameters ntruPrivateKeyParameters = privateKey as NtruPrivateKeyParameters; NtruPrivateKeyParameters ntruPrivateKeyParameters; if (ntruPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm4 = new AlgorithmIdentifier(PqcUtilities.NtruOidLookup(ntruPrivateKeyParameters.Parameters)); byte[] encoded5 = ntruPrivateKeyParameters.GetEncoded(); AlgorithmIdentifier privateKeyAlgorithm4; byte[] encoded5; return new PrivateKeyInfo(privateKeyAlgorithm4, new DerOctetString(encoded5), attributes); } throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); } } }