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

PqcPrivateKeyInfoFactory

public static class PqcPrivateKeyInfoFactory
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; if (lmsPrivateKeyParameters != null) { byte[] contents = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters) .Build(); byte[] publicKey = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()) .Build(); return new PrivateKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), new DerOctetString(contents), attributes, publicKey); } HssPrivateKeyParameters hssPrivateKeyParameters = privateKey as HssPrivateKeyParameters; if (hssPrivateKeyParameters != null) { int level = hssPrivateKeyParameters.Level; byte[] contents2 = Composer.Compose().U32Str(level).Bytes(hssPrivateKeyParameters) .Build(); byte[] publicKey2 = Composer.Compose().U32Str(level).Bytes(hssPrivateKeyParameters.GetPublicKey().LmsPublicKey) .Build(); return new PrivateKeyInfo(new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig), new DerOctetString(contents2), attributes, publicKey2); } SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters = privateKey as SphincsPlusPrivateKeyParameters; if (sphincsPlusPrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.SphincsPlusOidLookup(sphincsPlusPrivateKeyParameters.Parameters)), new DerOctetString(sphincsPlusPrivateKeyParameters.GetEncoded()), attributes); CmcePrivateKeyParameters cmcePrivateKeyParameters = privateKey as 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); return new PrivateKeyInfo(privateKeyAlgorithm, privateKey2, attributes); } FrodoPrivateKeyParameters frodoPrivateKeyParameters = privateKey as FrodoPrivateKeyParameters; if (frodoPrivateKeyParameters != null) { byte[] encoded = frodoPrivateKeyParameters.GetEncoded(); return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.FrodoOidLookup(frodoPrivateKeyParameters.Parameters)), new DerOctetString(encoded), attributes); } SaberPrivateKeyParameters saberPrivateKeyParameters = privateKey as SaberPrivateKeyParameters; if (saberPrivateKeyParameters != null) { byte[] encoded2 = saberPrivateKeyParameters.GetEncoded(); return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.SaberOidLookup(saberPrivateKeyParameters.Parameters)), new DerOctetString(encoded2), attributes); } PicnicPrivateKeyParameters picnicPrivateKeyParameters = privateKey as PicnicPrivateKeyParameters; if (picnicPrivateKeyParameters != null) { byte[] encoded3 = picnicPrivateKeyParameters.GetEncoded(); return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.PicnicOidLookup(picnicPrivateKeyParameters.Parameters)), new DerOctetString(encoded3), attributes); } FalconPrivateKeyParameters falconPrivateKeyParameters = privateKey as FalconPrivateKeyParameters; if (falconPrivateKeyParameters != null) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(4); 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; if (dilithiumPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm2 = new AlgorithmIdentifier(PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); DilithiumPublicKeyParameters publicKeyParameters = dilithiumPrivateKeyParameters.GetPublicKeyParameters(); return new PrivateKeyInfo(privateKeyAlgorithm2, new DerOctetString(dilithiumPrivateKeyParameters.GetEncoded()), attributes, publicKeyParameters.GetEncoded()); } BikePrivateKeyParameters bikePrivateKeyParameters = privateKey as BikePrivateKeyParameters; if (bikePrivateKeyParameters != null) { byte[] encoded4 = bikePrivateKeyParameters.GetEncoded(); return new PrivateKeyInfo(new AlgorithmIdentifier(PqcUtilities.BikeOidLookup(bikePrivateKeyParameters.Parameters)), new DerOctetString(encoded4), attributes); } HqcPrivateKeyParameters hqcPrivateKeyParameters = privateKey as HqcPrivateKeyParameters; if (hqcPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm3 = new AlgorithmIdentifier(PqcUtilities.HqcOidLookup(hqcPrivateKeyParameters.Parameters)); byte[] privateKey3 = hqcPrivateKeyParameters.PrivateKey; return new PrivateKeyInfo(privateKeyAlgorithm3, new DerOctetString(privateKey3), attributes); } NtruPrivateKeyParameters ntruPrivateKeyParameters = privateKey as NtruPrivateKeyParameters; if (ntruPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm4 = new AlgorithmIdentifier(PqcUtilities.NtruOidLookup(ntruPrivateKeyParameters.Parameters)); byte[] encoded5 = ntruPrivateKeyParameters.GetEncoded(); return new PrivateKeyInfo(privateKeyAlgorithm4, new DerOctetString(encoded5), attributes); } throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); } } }