From 1522a07e8ac7ee6e72b090966bf996926f345d56 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 8 Nov 2022 10:36:07 +0700 Subject: Add Pqc prefix to Pqc factory classes --- .../src/pqc/crypto/utils/PqcPrivateKeyFactory.cs | 237 +++++++++++++ .../pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs | 181 ++++++++++ crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs | 385 ++++++++++++++++++++ .../crypto/utils/PqcSubjectPublicKeyInfoFactory.cs | 151 ++++++++ crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs | 237 ------------- .../src/pqc/crypto/utils/PrivateKeyInfoFactory.cs | 181 ---------- crypto/src/pqc/crypto/utils/PublicKeyFactory.cs | 386 --------------------- .../crypto/utils/SubjectPublicKeyInfoFactory.cs | 151 -------- crypto/test/src/pqc/crypto/test/BikeVectorTest.cs | 8 +- crypto/test/src/pqc/crypto/test/CmceVectorTest.cs | 12 +- .../src/pqc/crypto/test/CrystalsDilithiumTest.cs | 16 +- crypto/test/src/pqc/crypto/test/FalconTest.cs | 12 +- crypto/test/src/pqc/crypto/test/HqcVectorTest.cs | 4 +- crypto/test/src/pqc/crypto/test/LMSTest.cs | 8 +- .../test/src/pqc/crypto/test/PicnicVectorTest.cs | 4 +- crypto/test/src/pqc/crypto/test/SaberVectorTest.cs | 8 +- crypto/test/src/pqc/crypto/test/SikeVectorTest.cs | 8 +- crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs | 12 +- 18 files changed, 1000 insertions(+), 1001 deletions(-) create mode 100644 crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs create mode 100644 crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs create mode 100644 crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs create mode 100644 crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs delete mode 100644 crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs delete mode 100644 crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs delete mode 100644 crypto/src/pqc/crypto/utils/PublicKeyFactory.cs delete mode 100644 crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs new file mode 100644 index 000000000..f01988169 --- /dev/null +++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs @@ -0,0 +1,237 @@ +using System; +using System.IO; + +using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.BC; +using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Utilities; +using Org.BouncyCastle.Math; +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.Crystals.Kyber; +using Org.BouncyCastle.Pqc.Crypto.Falcon; +using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; +using Org.BouncyCastle.Pqc.Crypto.Picnic; +using Org.BouncyCastle.Pqc.Crypto.Saber; +using Org.BouncyCastle.Pqc.Crypto.Sike; +using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.Utilities +{ + public static class PqcPrivateKeyFactory + { + /// Create a private key parameter from a PKCS8 PrivateKeyInfo encoding. + /// the PrivateKeyInfo encoding + /// a suitable private key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(byte[] privateKeyInfoData) + { + return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(privateKeyInfoData))); + } + + /// Create a private key parameter from a PKCS8 PrivateKeyInfo encoding read from a stream + /// the stream to read the PrivateKeyInfo encoding from + /// a suitable private key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(Stream inStr) + { + return CreateKey(PrivateKeyInfo.GetInstance(new Asn1InputStream(inStr).ReadObject())); + } + + /// Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object. + /// the PrivateKeyInfo object containing the key material + /// a suitable private key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(PrivateKeyInfo keyInfo) + { + AlgorithmIdentifier algId = keyInfo.PrivateKeyAlgorithm; + DerObjectIdentifier algOID = algId.Algorithm; + + if (algOID.Equals(PkcsObjectIdentifiers.IdAlgHssLmsHashsig)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + DerBitString pubKey = keyInfo.PublicKeyData; + + if (Pack.BE_To_UInt32(keyEnc, 0) == 1) + { + if (pubKey != null) + { + byte[] pubEnc = pubKey.GetOctets(); + + return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length), + Arrays.CopyOfRange(pubEnc, 4, pubEnc.Length)); + } + + return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + } + if (algOID.On(BCObjectIdentifiers.pqc_kem_mceliece)) + { + CmcePrivateKey cmceKey = CmcePrivateKey.GetInstance(keyInfo.ParsePrivateKey()); + CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + return new CmcePrivateKeyParameters(spParams, cmceKey.Delta, cmceKey.C, cmceKey.G, cmceKey.Alpha, cmceKey.S); + } + if (algOID.On(BCObjectIdentifiers.sphincsPlus)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + SphincsPlusParameters spParams = SphincsPlusParameters.GetParams(BigInteger.ValueOf(Pack.BE_To_UInt32(keyEnc, 0)).IntValue); + + return new SphincsPlusPrivateKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + if (algOID.On(BCObjectIdentifiers.pqc_kem_saber)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + SaberParameters spParams = PqcUtilities.SaberParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + return new SaberPrivateKeyParameters(spParams, keyEnc); + } + if (algOID.On(BCObjectIdentifiers.picnic)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + return new PicnicPrivateKeyParameters(picnicParams, keyEnc); + } +#pragma warning disable CS0618 // Type or member is obsolete + if (algOID.On(BCObjectIdentifiers.pqc_kem_sike)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + return new SikePrivateKeyParameters(sikeParams, keyEnc); + } +#pragma warning restore CS0618 // Type or member is obsolete + if (algOID.On(BCObjectIdentifiers.pqc_kem_bike)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + byte[] h0 = Arrays.CopyOfRange(keyEnc, 0, bikeParams.RByte); + byte[] h1 = Arrays.CopyOfRange(keyEnc, bikeParams.RByte, 2 * bikeParams.RByte); + byte[] sigma = Arrays.CopyOfRange(keyEnc, 2 * bikeParams.RByte, keyEnc.Length); + + return new BikePrivateKeyParameters(bikeParams, h0, h1, sigma); + } + if (algOID.On(BCObjectIdentifiers.pqc_kem_hqc)) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); + HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + return new HqcPrivateKeyParameters(hqcParams, keyEnc); + } + if (algOID.Equals(BCObjectIdentifiers.kyber512) + || algOID.Equals(BCObjectIdentifiers.kyber512_aes) + || algOID.Equals(BCObjectIdentifiers.kyber768) + || algOID.Equals(BCObjectIdentifiers.kyber768_aes) + || algOID.Equals(BCObjectIdentifiers.kyber1024) + || algOID.Equals(BCObjectIdentifiers.kyber1024_aes)) + { + Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); + + KyberParameters spParams = PqcUtilities.KyberParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; + if (version != 0) + { + throw new IOException("unknown private key version: " + version); + } + + if (keyInfo.PublicKeyData != null) + { + Asn1Sequence pubKey = Asn1Sequence.GetInstance(keyInfo.PublicKeyData.GetOctets()); + return new KyberPrivateKeyParameters(spParams, + Asn1OctetString.GetInstance(keyEnc[1]).GetDerEncoded(), + Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), + Asn1OctetString.GetInstance(pubKey[0]).GetOctets(), // t + Asn1OctetString.GetInstance(pubKey[1]).GetOctets()); // rho + } + else + { + return new KyberPrivateKeyParameters(spParams, + Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), + null, + null); + } + } + if (algOID.Equals(BCObjectIdentifiers.dilithium2) + || algOID.Equals(BCObjectIdentifiers.dilithium3) + || algOID.Equals(BCObjectIdentifiers.dilithium5) + || algOID.Equals(BCObjectIdentifiers.dilithium2_aes) + || algOID.Equals(BCObjectIdentifiers.dilithium3_aes) + || algOID.Equals(BCObjectIdentifiers.dilithium5_aes)) + { + Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); + + DilithiumParameters spParams = PqcUtilities.DilithiumParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; + if (version != 0) + throw new IOException("unknown private key version: " + version); + + if (keyInfo.PublicKeyData != null) + { + Asn1Sequence pubKey = Asn1Sequence.GetInstance(keyInfo.PublicKeyData.GetOctets()); + return new DilithiumPrivateKeyParameters(spParams, + DerBitString.GetInstance(keyEnc[1]).GetOctets(), + DerBitString.GetInstance(keyEnc[2]).GetOctets(), + DerBitString.GetInstance(keyEnc[3]).GetOctets(), + DerBitString.GetInstance(keyEnc[4]).GetOctets(), + DerBitString.GetInstance(keyEnc[5]).GetOctets(), + DerBitString.GetInstance(keyEnc[6]).GetOctets(), + Asn1OctetString.GetInstance(pubKey[1]).GetOctets()); // encT1 + } + else + { + return new DilithiumPrivateKeyParameters(spParams, + DerBitString.GetInstance(keyEnc[1]).GetOctets(), + DerBitString.GetInstance(keyEnc[2]).GetOctets(), + DerBitString.GetInstance(keyEnc[3]).GetOctets(), + DerBitString.GetInstance(keyEnc[4]).GetOctets(), + DerBitString.GetInstance(keyEnc[5]).GetOctets(), + DerBitString.GetInstance(keyEnc[6]).GetOctets(), + null); + } + } + if (algOID.Equals(BCObjectIdentifiers.falcon_512) || algOID.Equals(BCObjectIdentifiers.falcon_1024)) + { + Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); + FalconParameters spParams = PqcUtilities.FalconParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + + DerBitString publicKeyData = keyInfo.PublicKeyData; + int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; + if (version != 1) + throw new IOException("unknown private key version: " + version); + + if (keyInfo.PublicKeyData != null) + { + //ASN1Sequence pubKey = ASN1Sequence.getInstance(keyInfo.getPublicKeyData().getOctets()); + return new FalconPrivateKeyParameters(spParams, + Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), + publicKeyData.GetOctets()); // encT1 + } + else + { + return new FalconPrivateKeyParameters(spParams, + Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), + Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), + null); + } + } + + throw new Exception("algorithm identifier in private key not recognised"); + } + } +} diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs new file mode 100644 index 000000000..ce0300cf4 --- /dev/null +++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs @@ -0,0 +1,181 @@ +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.Crystals.Kyber; +using Org.BouncyCastle.Pqc.Crypto.Falcon; +using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; +using Org.BouncyCastle.Pqc.Crypto.Picnic; +using Org.BouncyCastle.Pqc.Crypto.Saber; +using Org.BouncyCastle.Pqc.Crypto.Sike; +using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.Utilities +{ + public static class PqcPrivateKeyInfoFactory + { + /// Create a PrivateKeyInfo representation of a private key. + /// the key to be encoded into the info object. + /// the appropriate PrivateKeyInfo + /// on an error encoding the key + public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey) + { + return CreatePrivateKeyInfo(privateKey, null); + } + + /// Create a PrivateKeyInfo representation of a private key with attributes. + /// the key to be encoded into the info object. + /// the set of attributes to be included. + /// the appropriate PrivateKeyInfo + /// on an error encoding the key + public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) + { + if (privateKey is LmsPrivateKeyParameters lmsPrivateKeyParameters) + { + byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); + } + if (privateKey is HssPrivateKeyParameters hssPrivateKeyParameters) + { + int L = hssPrivateKeyParameters.L; + byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters.GetPublicKey().LmsPublicKey).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); + } + if (privateKey is SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters) + { + byte[] encoding = sphincsPlusPrivateKeyParameters.GetEncoded(); + byte[] pubEncoding = sphincsPlusPrivateKeyParameters.GetEncodedPublicKey(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SphincsPlusOidLookup(sphincsPlusPrivateKeyParameters.Parameters)); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); + } + if (privateKey is CmcePrivateKeyParameters cmcePrivateKeyParameters) + { + byte[] encoding = cmcePrivateKeyParameters.GetEncoded(); + AlgorithmIdentifier algorithmIdentifier = + new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(cmcePrivateKeyParameters.Parameters)); + + CmcePublicKey CmcePub = new CmcePublicKey(cmcePrivateKeyParameters.ReconstructPublicKey()); + CmcePrivateKey CmcePriv = new CmcePrivateKey(0, cmcePrivateKeyParameters.Delta, + cmcePrivateKeyParameters.C, cmcePrivateKeyParameters.G, cmcePrivateKeyParameters.Alpha, + cmcePrivateKeyParameters.S, CmcePub); + return new PrivateKeyInfo(algorithmIdentifier, CmcePriv, attributes); + } + if (privateKey is SaberPrivateKeyParameters saberPrivateKeyParameters) + { + byte[] encoding = saberPrivateKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SaberOidLookup(saberPrivateKeyParameters.Parameters)); + + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); + } + if (privateKey is PicnicPrivateKeyParameters picnicPrivateKeyParameters) + { + byte[] encoding = picnicPrivateKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.PicnicOidLookup(picnicPrivateKeyParameters.Parameters)); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); + } +#pragma warning disable CS0618 // Type or member is obsolete + if (privateKey is SikePrivateKeyParameters sikePrivateKeyParameters) + { + byte[] encoding = sikePrivateKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SikeOidLookup(sikePrivateKeyParameters.Parameters)); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); + } +#pragma warning restore CS0618 // Type or member is obsolete + if (privateKey is FalconPrivateKeyParameters falconPrivateKeyParameters) + { + Asn1EncodableVector v = new Asn1EncodableVector(); + + v.Add(new DerInteger(1)); + v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyLittleF())); + v.Add(new DerOctetString(falconPrivateKeyParameters.GetG())); + v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyBigF())); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.FalconOidLookup(falconPrivateKeyParameters.Parameters)); + + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, + falconPrivateKeyParameters.GetPublicKey()); + } + if (privateKey is KyberPrivateKeyParameters kyberPrivateKeyParameters) + { + Asn1EncodableVector v = new Asn1EncodableVector(); + + v.Add(new DerInteger(0)); + v.Add(new DerOctetString(kyberPrivateKeyParameters.S)); + v.Add(new DerOctetString(kyberPrivateKeyParameters.Hpk)); + v.Add(new DerOctetString(kyberPrivateKeyParameters.Nonce)); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.KyberOidLookup(kyberPrivateKeyParameters.Parameters)); + + Asn1EncodableVector vPub = new Asn1EncodableVector(); + vPub.Add(new DerOctetString(kyberPrivateKeyParameters.T)); + vPub.Add(new DerOctetString(kyberPrivateKeyParameters.Rho)); + + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, + new DerSequence(vPub).GetEncoded()); + } + if (privateKey is DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters) + { + Asn1EncodableVector v = new Asn1EncodableVector(); + + v.Add(new DerInteger(0)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.Rho)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.K)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.Tr)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.S1)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.S2)); + v.Add(new DerBitString(dilithiumPrivateKeyParameters.T0)); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); + + Asn1EncodableVector vPub = new Asn1EncodableVector(); + vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.Rho)); + vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.T1)); + + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, + new DerSequence(vPub).GetEncoded()); + } + if (privateKey is BikePrivateKeyParameters bikePrivateKeyParameters) + { + byte[] encoding = bikePrivateKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.BikeOidLookup(bikePrivateKeyParameters.Parameters)); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); + } + else if (privateKey is HqcPrivateKeyParameters hqcPrivateKeyParameters) + { + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.HqcOidLookup(hqcPrivateKeyParameters.Parameters)); + byte[] encoding = hqcPrivateKeyParameters.PrivateKey; + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); + } + + throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); + } + } +} diff --git a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs new file mode 100644 index 000000000..c8ca11de6 --- /dev/null +++ b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs @@ -0,0 +1,385 @@ +using System; +using System.Collections.Generic; +using System.IO; + +using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.BC; +using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Utilities; +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.Crystals.Kyber; +using Org.BouncyCastle.Pqc.Crypto.Falcon; +using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; +using Org.BouncyCastle.Pqc.Crypto.Picnic; +using Org.BouncyCastle.Pqc.Crypto.Saber; +using Org.BouncyCastle.Pqc.Crypto.Sike; +using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.Utilities +{ + public static class PqcPublicKeyFactory + { + private static Dictionary Converters = + new Dictionary(); + + static PqcPublicKeyFactory() + { + Converters[PkcsObjectIdentifiers.IdAlgHssLmsHashsig] = new LmsConverter(); + + Converters[BCObjectIdentifiers.sphincsPlus] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_shake_256] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_sha_256] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_sha_512] = new SphincsPlusConverter(); + + Converters[BCObjectIdentifiers.mceliece348864_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece348864f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece460896_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece460896f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6688128_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6688128f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6960119_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6960119f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece8192128_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece8192128f_r3] = new CmceConverter(); + + Converters[BCObjectIdentifiers.lightsaberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.ulightsaberkemr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.usaberkemr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.ufiresaberkemr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem90sr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem90sr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem90sr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.ulightsaberkem90sr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.usaberkem90sr3] = new SaberConverter(); + Converters[BCObjectIdentifiers.ufiresaberkem90sr3] = new SaberConverter(); + + Converters[BCObjectIdentifiers.picnic] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l1] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l3] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l5] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1full] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3full] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5full] = new PicnicConverter(); + +#pragma warning disable CS0618 // Type or member is obsolete + Converters[BCObjectIdentifiers.sikep434] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep503] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep610] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep751] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep434_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep503_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep610_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep751_compressed] = new SikeConverter(); +#pragma warning restore CS0618 // Type or member is obsolete + + Converters[BCObjectIdentifiers.dilithium2] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium3] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium5] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium2_aes] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium3_aes] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium5_aes] = new DilithiumConverter(); + + Converters[BCObjectIdentifiers.falcon_512] = new FalconConverter(); + Converters[BCObjectIdentifiers.falcon_1024] = new FalconConverter(); + + Converters[BCObjectIdentifiers.kyber512] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber512_aes] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber768] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber768_aes] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber1024] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber1024_aes] = new KyberConverter(); + + Converters[BCObjectIdentifiers.bike128] = new BikeConverter(); + Converters[BCObjectIdentifiers.bike192] = new BikeConverter(); + Converters[BCObjectIdentifiers.bike256] = new BikeConverter(); + + Converters[BCObjectIdentifiers.hqc128] = new HqcConverter(); + Converters[BCObjectIdentifiers.hqc192] = new HqcConverter(); + Converters[BCObjectIdentifiers.hqc256] = new HqcConverter(); + } + + /// Create a public key from a SubjectPublicKeyInfo encoding + /// the SubjectPublicKeyInfo encoding + /// the appropriate key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(byte[] keyInfoData) + { + return CreateKey(SubjectPublicKeyInfo.GetInstance(Asn1Object.FromByteArray(keyInfoData))); + } + + /// Create a public key from a SubjectPublicKeyInfo encoding read from a stream + /// the stream to read the SubjectPublicKeyInfo encoding from + /// the appropriate key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(Stream inStr) + { + return CreateKey(SubjectPublicKeyInfo.GetInstance(new Asn1InputStream(inStr).ReadObject())); + } + + /// Create a public key from the passed in SubjectPublicKeyInfo + /// the SubjectPublicKeyInfo containing the key data + /// the appropriate key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo) + { + return CreateKey(keyInfo, null); + } + + /// Create a public key from the passed in SubjectPublicKeyInfo + /// the SubjectPublicKeyInfo containing the key data + /// default parameters that might be needed. + /// the appropriate key parameter + /// on an error decoding the key + public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + AlgorithmIdentifier algId = keyInfo.AlgorithmID; + SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)Converters[algId.Algorithm]; + + if (converter != null) + { + return converter.GetPublicKeyParameters(keyInfo, defaultParams); + } + else + { + throw new IOException("algorithm identifier in public key not recognised: " + algId.Algorithm); + } + } + private abstract class SubjectPublicKeyInfoConverter + { + internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams); + } + + private class LmsConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) + { + return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + else + { + // public key with extra tree height + if (keyEnc.Length == 64) + { + keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); + } + return HssPublicKeyParameters.GetInstance(keyEnc); + } + } + } + + private class SphincsPlusConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + SphincsPlusParameters spParams = SphincsPlusParameters.GetParams((int)Pack.BE_To_UInt32(keyEnc, 0)); + + return new SphincsPlusPublicKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + } + + private class CmceConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = CmcePublicKey.GetInstance(keyInfo.ParsePublicKey()).T; + + CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new CmcePublicKeyParameters(spParams, keyEnc); + } + } + + private class SaberConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance( + DerSequence.GetInstance(keyInfo.ParsePublicKey())[0]).GetOctets(); + + SaberParameters saberParams = PqcUtilities.SaberParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new SaberPublicKeyParameters(saberParams, keyEnc); + } + } + + private class PicnicConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new PicnicPublicKeyParameters(picnicParams, keyEnc); + } + } + [Obsolete("Will be removed")] + private class SikeConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new SikePublicKeyParameters(sikeParams, keyEnc); + } + } + private class DilithiumConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + DilithiumParameters dilithiumParams = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm); + + try + { + Asn1Object obj = keyInfo.ParsePublicKey(); + if (obj is Asn1Sequence) + { + Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj); + + return new DilithiumPublicKeyParameters(dilithiumParams, + Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), + Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); + } + else + { + byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); + + return new DilithiumPublicKeyParameters(dilithiumParams, encKey); + } + } + catch (Exception e) + { + // raw encoding + return new DilithiumPublicKeyParameters(dilithiumParams, keyInfo.PublicKeyData.GetOctets()); + } + } + } + + private class KyberConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + KyberParameters kyberParameters = PqcUtilities.KyberParamsLookup(keyInfo.AlgorithmID.Algorithm); + + Asn1Object obj = keyInfo.ParsePublicKey(); + if (obj is Asn1Sequence) + { + Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj); + + return new KyberPublicKeyParameters(kyberParameters, + Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), + Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); + } + else + { + byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); + + return new KyberPublicKeyParameters(kyberParameters, encKey); + } + } + } + + private class FalconConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.AlgorithmID.Algorithm); + + try + { + Asn1Object obj = keyInfo.ParsePublicKey(); + if (obj is Asn1Sequence) + { + byte[] keyEnc = Asn1OctetString.GetInstance(Asn1Sequence.GetInstance(obj)[0]).GetOctets(); + + return new FalconPublicKeyParameters(falconParams, keyEnc); + } + else + { + // header byte + h + byte[] keyEnc = Asn1OctetString.GetInstance(obj).GetOctets(); + + if (keyEnc[0] != (byte)(0x00 + falconParams.LogN)) + { + throw new ArgumentException("byte[] enc of Falcon h value not tagged correctly"); + } + return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length)); + } + } + catch (Exception e) + { + // raw encoding + byte[] keyEnc = keyInfo.PublicKeyData.GetOctets(); + + if (keyEnc[0] != (byte)(0x00 + falconParams.LogN)) + { + throw new ArgumentException("byte[] enc of Falcon h value not tagged correctly"); + } + return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length)); + } + } + } + + private class BikeConverter: SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new BikePublicKeyParameters(bikeParams, keyEnc); + } + } + + private class HqcConverter : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.AlgorithmID.Algorithm); + + return new HqcPublicKeyParameters(hqcParams, keyEnc); + } + } + } +} diff --git a/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs new file mode 100644 index 000000000..756308460 --- /dev/null +++ b/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs @@ -0,0 +1,151 @@ +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.Crystals.Kyber; +using Org.BouncyCastle.Pqc.Crypto.Falcon; +using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; +using Org.BouncyCastle.Pqc.Crypto.Picnic; +using Org.BouncyCastle.Pqc.Crypto.Saber; +using Org.BouncyCastle.Pqc.Crypto.Sike; +using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Pqc.Crypto.Utilities +{ + /// + /// A factory to produce Public Key Info Objects. + /// + public static class PqcSubjectPublicKeyInfoFactory + { + /// + /// Create a Subject Public Key Info object for a given public key. + /// + /// One of ElGammalPublicKeyParameters, DSAPublicKeyParameter, DHPublicKeyParameters, RsaKeyParameters or ECPublicKeyParameters + /// A subject public key info object. + /// Throw exception if object provided is not one of the above. + 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"); + + if (publicKey is LmsPublicKeyParameters lmsPublicKeyParameters) + { + byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPublicKeyParameters).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + if (publicKey is HssPublicKeyParameters hssPublicKeyParameters) + { + int L = hssPublicKeyParameters.L; + byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPublicKeyParameters.LmsPublicKey).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + if (publicKey is SphincsPlusPublicKeyParameters sphincsPlusPublicKeyParameters) + { + byte[] encoding = sphincsPlusPublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SphincsPlusOidLookup(sphincsPlusPublicKeyParameters.Parameters)); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + if (publicKey is CmcePublicKeyParameters cmcePublicKeyParameters) + { + byte[] encoding = cmcePublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.McElieceOidLookup(cmcePublicKeyParameters.Parameters)); + + // https://datatracker.ietf.org/doc/draft-uni-qsckeys/ + return new SubjectPublicKeyInfo(algorithmIdentifier, new CmcePublicKey(encoding)); + } + if (publicKey is SaberPublicKeyParameters saberPublicKeyParameters) + { + byte[] encoding = saberPublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SaberOidLookup(saberPublicKeyParameters.Parameters)); + + // https://datatracker.ietf.org/doc/draft-uni-qsckeys/ + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerSequence(new DerOctetString(encoding))); + } + if (publicKey is PicnicPublicKeyParameters picnicPublicKeyParameters) + { + byte[] encoding = picnicPublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.PicnicOidLookup(picnicPublicKeyParameters.Parameters)); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } +#pragma warning disable CS0618 // Type or member is obsolete + if (publicKey is SikePublicKeyParameters sikePublicKeyParameters) + { + byte[] encoding = sikePublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.SikeOidLookup(sikePublicKeyParameters.Parameters)); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } +#pragma warning restore CS0618 // Type or member is obsolete + if (publicKey is FalconPublicKeyParameters falconPublicKeyParameters) + { + byte[] keyEnc = falconPublicKeyParameters.GetEncoded(); + + byte[] encoding = new byte[keyEnc.Length + 1]; + encoding[0] = (byte)(0x00 + falconPublicKeyParameters.Parameters.LogN); + Array.Copy(keyEnc, 0, encoding, 1, keyEnc.Length); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.FalconOidLookup(falconPublicKeyParameters.Parameters)); + return new SubjectPublicKeyInfo(algorithmIdentifier, encoding); + } + if (publicKey is KyberPublicKeyParameters kyberPublicKeyParameters) + { + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.KyberOidLookup(kyberPublicKeyParameters.Parameters)); + Asn1EncodableVector v = new Asn1EncodableVector(); + v.Add(new DerOctetString(kyberPublicKeyParameters.T)); + v.Add(new DerOctetString(kyberPublicKeyParameters.Rho)); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerSequence(v)); + } + if (publicKey is DilithiumPublicKeyParameters dilithiumPublicKeyParameters) + { + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.DilithiumOidLookup(dilithiumPublicKeyParameters.Parameters)); + + return new SubjectPublicKeyInfo(algorithmIdentifier, Arrays.Concatenate(dilithiumPublicKeyParameters.Rho, dilithiumPublicKeyParameters.T1)); + } + if (publicKey is BikePublicKeyParameters bikePublicKeyParameters) + { + byte[] encoding = bikePublicKeyParameters.GetEncoded(); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.BikeOidLookup(bikePublicKeyParameters.Parameters)); + + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + if (publicKey is HqcPublicKeyParameters hqcPublicKeyParameters) + { + byte[] encoding = hqcPublicKeyParameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + PqcUtilities.HqcOidLookup(hqcPublicKeyParameters.Parameters)); + + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + + throw new ArgumentException("Class provided no convertible: " + Platform.GetTypeName(publicKey)); + } + } +} diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs deleted file mode 100644 index 6ad9bdb0c..000000000 --- a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs +++ /dev/null @@ -1,237 +0,0 @@ -using System; -using System.IO; - -using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.BC; -using Org.BouncyCastle.Asn1.Pkcs; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Utilities; -using Org.BouncyCastle.Math; -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.Crystals.Kyber; -using Org.BouncyCastle.Pqc.Crypto.Falcon; -using Org.BouncyCastle.Pqc.Crypto.Hqc; -using Org.BouncyCastle.Pqc.Crypto.Lms; -using Org.BouncyCastle.Pqc.Crypto.Picnic; -using Org.BouncyCastle.Pqc.Crypto.Saber; -using Org.BouncyCastle.Pqc.Crypto.Sike; -using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Pqc.Crypto.Utilities -{ - public static class PrivateKeyFactory - { - /// Create a private key parameter from a PKCS8 PrivateKeyInfo encoding. - /// the PrivateKeyInfo encoding - /// a suitable private key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(byte[] privateKeyInfoData) - { - return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(privateKeyInfoData))); - } - - /// Create a private key parameter from a PKCS8 PrivateKeyInfo encoding read from a stream - /// the stream to read the PrivateKeyInfo encoding from - /// a suitable private key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(Stream inStr) - { - return CreateKey(PrivateKeyInfo.GetInstance(new Asn1InputStream(inStr).ReadObject())); - } - - /// Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object. - /// the PrivateKeyInfo object containing the key material - /// a suitable private key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(PrivateKeyInfo keyInfo) - { - AlgorithmIdentifier algId = keyInfo.PrivateKeyAlgorithm; - DerObjectIdentifier algOID = algId.Algorithm; - - if (algOID.Equals(PkcsObjectIdentifiers.IdAlgHssLmsHashsig)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - DerBitString pubKey = keyInfo.PublicKeyData; - - if (Pack.BE_To_UInt32(keyEnc, 0) == 1) - { - if (pubKey != null) - { - byte[] pubEnc = pubKey.GetOctets(); - - return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length), - Arrays.CopyOfRange(pubEnc, 4, pubEnc.Length)); - } - - return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); - } - } - if (algOID.On(BCObjectIdentifiers.pqc_kem_mceliece)) - { - CmcePrivateKey cmceKey = CmcePrivateKey.GetInstance(keyInfo.ParsePrivateKey()); - CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - return new CmcePrivateKeyParameters(spParams, cmceKey.Delta, cmceKey.C, cmceKey.G, cmceKey.Alpha, cmceKey.S); - } - if (algOID.On(BCObjectIdentifiers.sphincsPlus)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - SphincsPlusParameters spParams = SphincsPlusParameters.GetParams(BigInteger.ValueOf(Pack.BE_To_UInt32(keyEnc, 0)).IntValue); - - return new SphincsPlusPrivateKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); - } - if (algOID.On(BCObjectIdentifiers.pqc_kem_saber)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - SaberParameters spParams = PqcUtilities.SaberParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - return new SaberPrivateKeyParameters(spParams, keyEnc); - } - if (algOID.On(BCObjectIdentifiers.picnic)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - return new PicnicPrivateKeyParameters(picnicParams, keyEnc); - } -#pragma warning disable CS0618 // Type or member is obsolete - if (algOID.On(BCObjectIdentifiers.pqc_kem_sike)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - return new SikePrivateKeyParameters(sikeParams, keyEnc); - } -#pragma warning restore CS0618 // Type or member is obsolete - if (algOID.On(BCObjectIdentifiers.pqc_kem_bike)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - byte[] h0 = Arrays.CopyOfRange(keyEnc, 0, bikeParams.RByte); - byte[] h1 = Arrays.CopyOfRange(keyEnc, bikeParams.RByte, 2 * bikeParams.RByte); - byte[] sigma = Arrays.CopyOfRange(keyEnc, 2 * bikeParams.RByte, keyEnc.Length); - - return new BikePrivateKeyParameters(bikeParams, h0, h1, sigma); - } - if (algOID.On(BCObjectIdentifiers.pqc_kem_hqc)) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); - HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - return new HqcPrivateKeyParameters(hqcParams, keyEnc); - } - if (algOID.Equals(BCObjectIdentifiers.kyber512) - || algOID.Equals(BCObjectIdentifiers.kyber512_aes) - || algOID.Equals(BCObjectIdentifiers.kyber768) - || algOID.Equals(BCObjectIdentifiers.kyber768_aes) - || algOID.Equals(BCObjectIdentifiers.kyber1024) - || algOID.Equals(BCObjectIdentifiers.kyber1024_aes)) - { - Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); - - KyberParameters spParams = PqcUtilities.KyberParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; - if (version != 0) - { - throw new IOException("unknown private key version: " + version); - } - - if (keyInfo.PublicKeyData != null) - { - Asn1Sequence pubKey = Asn1Sequence.GetInstance(keyInfo.PublicKeyData.GetOctets()); - return new KyberPrivateKeyParameters(spParams, - Asn1OctetString.GetInstance(keyEnc[1]).GetDerEncoded(), - Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), - Asn1OctetString.GetInstance(pubKey[0]).GetOctets(), // t - Asn1OctetString.GetInstance(pubKey[1]).GetOctets()); // rho - } - else - { - return new KyberPrivateKeyParameters(spParams, - Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), - null, - null); - } - } - if (algOID.Equals(BCObjectIdentifiers.dilithium2) - || algOID.Equals(BCObjectIdentifiers.dilithium3) - || algOID.Equals(BCObjectIdentifiers.dilithium5) - || algOID.Equals(BCObjectIdentifiers.dilithium2_aes) - || algOID.Equals(BCObjectIdentifiers.dilithium3_aes) - || algOID.Equals(BCObjectIdentifiers.dilithium5_aes)) - { - Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); - - DilithiumParameters spParams = PqcUtilities.DilithiumParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; - if (version != 0) - throw new IOException("unknown private key version: " + version); - - if (keyInfo.PublicKeyData != null) - { - Asn1Sequence pubKey = Asn1Sequence.GetInstance(keyInfo.PublicKeyData.GetOctets()); - return new DilithiumPrivateKeyParameters(spParams, - DerBitString.GetInstance(keyEnc[1]).GetOctets(), - DerBitString.GetInstance(keyEnc[2]).GetOctets(), - DerBitString.GetInstance(keyEnc[3]).GetOctets(), - DerBitString.GetInstance(keyEnc[4]).GetOctets(), - DerBitString.GetInstance(keyEnc[5]).GetOctets(), - DerBitString.GetInstance(keyEnc[6]).GetOctets(), - Asn1OctetString.GetInstance(pubKey[1]).GetOctets()); // encT1 - } - else - { - return new DilithiumPrivateKeyParameters(spParams, - DerBitString.GetInstance(keyEnc[1]).GetOctets(), - DerBitString.GetInstance(keyEnc[2]).GetOctets(), - DerBitString.GetInstance(keyEnc[3]).GetOctets(), - DerBitString.GetInstance(keyEnc[4]).GetOctets(), - DerBitString.GetInstance(keyEnc[5]).GetOctets(), - DerBitString.GetInstance(keyEnc[6]).GetOctets(), - null); - } - } - if (algOID.Equals(BCObjectIdentifiers.falcon_512) || algOID.Equals(BCObjectIdentifiers.falcon_1024)) - { - Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); - FalconParameters spParams = PqcUtilities.FalconParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); - - DerBitString publicKeyData = keyInfo.PublicKeyData; - int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; - if (version != 1) - throw new IOException("unknown private key version: " + version); - - if (keyInfo.PublicKeyData != null) - { - //ASN1Sequence pubKey = ASN1Sequence.getInstance(keyInfo.getPublicKeyData().getOctets()); - return new FalconPrivateKeyParameters(spParams, - Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), - publicKeyData.GetOctets()); // encT1 - } - else - { - return new FalconPrivateKeyParameters(spParams, - Asn1OctetString.GetInstance(keyEnc[1]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[2]).GetOctets(), - Asn1OctetString.GetInstance(keyEnc[3]).GetOctets(), - null); - } - } - - throw new Exception("algorithm identifier in private key not recognised"); - } - } -} diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs deleted file mode 100644 index 53b34b3f4..000000000 --- a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs +++ /dev/null @@ -1,181 +0,0 @@ -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.Crystals.Kyber; -using Org.BouncyCastle.Pqc.Crypto.Falcon; -using Org.BouncyCastle.Pqc.Crypto.Hqc; -using Org.BouncyCastle.Pqc.Crypto.Lms; -using Org.BouncyCastle.Pqc.Crypto.Picnic; -using Org.BouncyCastle.Pqc.Crypto.Saber; -using Org.BouncyCastle.Pqc.Crypto.Sike; -using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Pqc.Crypto.Utilities -{ - public static class PrivateKeyInfoFactory - { - /// Create a PrivateKeyInfo representation of a private key. - /// the key to be encoded into the info object. - /// the appropriate PrivateKeyInfo - /// on an error encoding the key - public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey) - { - return CreatePrivateKeyInfo(privateKey, null); - } - - /// Create a PrivateKeyInfo representation of a private key with attributes. - /// the key to be encoded into the info object. - /// the set of attributes to be included. - /// the appropriate PrivateKeyInfo - /// on an error encoding the key - public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) - { - if (privateKey is LmsPrivateKeyParameters lmsPrivateKeyParameters) - { - byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters).Build(); - byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()).Build(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); - } - if (privateKey is HssPrivateKeyParameters hssPrivateKeyParameters) - { - int L = hssPrivateKeyParameters.L; - byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters).Build(); - byte[] pubEncoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters.GetPublicKey().LmsPublicKey).Build(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); - } - if (privateKey is SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters) - { - byte[] encoding = sphincsPlusPrivateKeyParameters.GetEncoded(); - byte[] pubEncoding = sphincsPlusPrivateKeyParameters.GetEncodedPublicKey(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SphincsPlusOidLookup(sphincsPlusPrivateKeyParameters.Parameters)); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); - } - if (privateKey is CmcePrivateKeyParameters cmcePrivateKeyParameters) - { - byte[] encoding = cmcePrivateKeyParameters.GetEncoded(); - AlgorithmIdentifier algorithmIdentifier = - new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(cmcePrivateKeyParameters.Parameters)); - - CmcePublicKey CmcePub = new CmcePublicKey(cmcePrivateKeyParameters.ReconstructPublicKey()); - CmcePrivateKey CmcePriv = new CmcePrivateKey(0, cmcePrivateKeyParameters.Delta, - cmcePrivateKeyParameters.C, cmcePrivateKeyParameters.G, cmcePrivateKeyParameters.Alpha, - cmcePrivateKeyParameters.S, CmcePub); - return new PrivateKeyInfo(algorithmIdentifier, CmcePriv, attributes); - } - if (privateKey is SaberPrivateKeyParameters saberPrivateKeyParameters) - { - byte[] encoding = saberPrivateKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SaberOidLookup(saberPrivateKeyParameters.Parameters)); - - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); - } - if (privateKey is PicnicPrivateKeyParameters picnicPrivateKeyParameters) - { - byte[] encoding = picnicPrivateKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.PicnicOidLookup(picnicPrivateKeyParameters.Parameters)); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); - } -#pragma warning disable CS0618 // Type or member is obsolete - if (privateKey is SikePrivateKeyParameters sikePrivateKeyParameters) - { - byte[] encoding = sikePrivateKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SikeOidLookup(sikePrivateKeyParameters.Parameters)); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); - } -#pragma warning restore CS0618 // Type or member is obsolete - if (privateKey is FalconPrivateKeyParameters falconPrivateKeyParameters) - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - v.Add(new DerInteger(1)); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyLittleF())); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetG())); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyBigF())); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.FalconOidLookup(falconPrivateKeyParameters.Parameters)); - - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, - falconPrivateKeyParameters.GetPublicKey()); - } - if (privateKey is KyberPrivateKeyParameters kyberPrivateKeyParameters) - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - v.Add(new DerInteger(0)); - v.Add(new DerOctetString(kyberPrivateKeyParameters.S)); - v.Add(new DerOctetString(kyberPrivateKeyParameters.Hpk)); - v.Add(new DerOctetString(kyberPrivateKeyParameters.Nonce)); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.KyberOidLookup(kyberPrivateKeyParameters.Parameters)); - - Asn1EncodableVector vPub = new Asn1EncodableVector(); - vPub.Add(new DerOctetString(kyberPrivateKeyParameters.T)); - vPub.Add(new DerOctetString(kyberPrivateKeyParameters.Rho)); - - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, - new DerSequence(vPub).GetEncoded()); - } - if (privateKey is DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters) - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - v.Add(new DerInteger(0)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.Rho)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.K)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.Tr)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.S1)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.S2)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.T0)); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); - - Asn1EncodableVector vPub = new Asn1EncodableVector(); - vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.Rho)); - vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.T1)); - - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, - new DerSequence(vPub).GetEncoded()); - } - if (privateKey is BikePrivateKeyParameters bikePrivateKeyParameters) - { - byte[] encoding = bikePrivateKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.BikeOidLookup(bikePrivateKeyParameters.Parameters)); - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); - } - else if (privateKey is HqcPrivateKeyParameters hqcPrivateKeyParameters) - { - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.HqcOidLookup(hqcPrivateKeyParameters.Parameters)); - byte[] encoding = hqcPrivateKeyParameters.PrivateKey; - return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); - } - - throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); - } - } -} diff --git a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs deleted file mode 100644 index 792dc6f40..000000000 --- a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs +++ /dev/null @@ -1,386 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.BC; -using Org.BouncyCastle.Asn1.Pkcs; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Utilities; -using Org.BouncyCastle.Math; -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.Crystals.Kyber; -using Org.BouncyCastle.Pqc.Crypto.Falcon; -using Org.BouncyCastle.Pqc.Crypto.Hqc; -using Org.BouncyCastle.Pqc.Crypto.Lms; -using Org.BouncyCastle.Pqc.Crypto.Picnic; -using Org.BouncyCastle.Pqc.Crypto.Saber; -using Org.BouncyCastle.Pqc.Crypto.Sike; -using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Pqc.Crypto.Utilities -{ - public static class PublicKeyFactory - { - private static Dictionary Converters = - new Dictionary(); - - static PublicKeyFactory() - { - Converters[PkcsObjectIdentifiers.IdAlgHssLmsHashsig] = new LmsConverter(); - - Converters[BCObjectIdentifiers.sphincsPlus] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_256] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha_256] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha_512] = new SphincsPlusConverter(); - - Converters[BCObjectIdentifiers.mceliece348864_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece348864f_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece460896_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece460896f_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece6688128_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece6688128f_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece6960119_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece6960119f_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece8192128_r3] = new CmceConverter(); - Converters[BCObjectIdentifiers.mceliece8192128f_r3] = new CmceConverter(); - - Converters[BCObjectIdentifiers.lightsaberkem128r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.saberkem128r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.firesaberkem128r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.lightsaberkem192r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.saberkem192r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.firesaberkem192r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.lightsaberkem256r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.saberkem256r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.firesaberkem256r3] = new SaberConverter(); - Converters[BCObjectIdentifiers.ulightsaberkemr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.usaberkemr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.ufiresaberkemr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.lightsaberkem90sr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.saberkem90sr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.firesaberkem90sr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.ulightsaberkem90sr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.usaberkem90sr3] = new SaberConverter(); - Converters[BCObjectIdentifiers.ufiresaberkem90sr3] = new SaberConverter(); - - Converters[BCObjectIdentifiers.picnic] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl1fs] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl1ur] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl3fs] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl3ur] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl5fs] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl5ur] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnic3l1] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnic3l3] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnic3l5] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl1full] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl3full] = new PicnicConverter(); - Converters[BCObjectIdentifiers.picnicl5full] = new PicnicConverter(); - -#pragma warning disable CS0618 // Type or member is obsolete - Converters[BCObjectIdentifiers.sikep434] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep503] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep610] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep751] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep434_compressed] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep503_compressed] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep610_compressed] = new SikeConverter(); - Converters[BCObjectIdentifiers.sikep751_compressed] = new SikeConverter(); -#pragma warning restore CS0618 // Type or member is obsolete - - Converters[BCObjectIdentifiers.dilithium2] = new DilithiumConverter(); - Converters[BCObjectIdentifiers.dilithium3] = new DilithiumConverter(); - Converters[BCObjectIdentifiers.dilithium5] = new DilithiumConverter(); - Converters[BCObjectIdentifiers.dilithium2_aes] = new DilithiumConverter(); - Converters[BCObjectIdentifiers.dilithium3_aes] = new DilithiumConverter(); - Converters[BCObjectIdentifiers.dilithium5_aes] = new DilithiumConverter(); - - Converters[BCObjectIdentifiers.falcon_512] = new FalconConverter(); - Converters[BCObjectIdentifiers.falcon_1024] = new FalconConverter(); - - Converters[BCObjectIdentifiers.kyber512] = new KyberConverter(); - Converters[BCObjectIdentifiers.kyber512_aes] = new KyberConverter(); - Converters[BCObjectIdentifiers.kyber768] = new KyberConverter(); - Converters[BCObjectIdentifiers.kyber768_aes] = new KyberConverter(); - Converters[BCObjectIdentifiers.kyber1024] = new KyberConverter(); - Converters[BCObjectIdentifiers.kyber1024_aes] = new KyberConverter(); - - Converters[BCObjectIdentifiers.bike128] = new BikeConverter(); - Converters[BCObjectIdentifiers.bike192] = new BikeConverter(); - Converters[BCObjectIdentifiers.bike256] = new BikeConverter(); - - Converters[BCObjectIdentifiers.hqc128] = new HqcConverter(); - Converters[BCObjectIdentifiers.hqc192] = new HqcConverter(); - Converters[BCObjectIdentifiers.hqc256] = new HqcConverter(); - } - - /// Create a public key from a SubjectPublicKeyInfo encoding - /// the SubjectPublicKeyInfo encoding - /// the appropriate key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(byte[] keyInfoData) - { - return CreateKey(SubjectPublicKeyInfo.GetInstance(Asn1Object.FromByteArray(keyInfoData))); - } - - /// Create a public key from a SubjectPublicKeyInfo encoding read from a stream - /// the stream to read the SubjectPublicKeyInfo encoding from - /// the appropriate key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(Stream inStr) - { - return CreateKey(SubjectPublicKeyInfo.GetInstance(new Asn1InputStream(inStr).ReadObject())); - } - - /// Create a public key from the passed in SubjectPublicKeyInfo - /// the SubjectPublicKeyInfo containing the key data - /// the appropriate key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo) - { - return CreateKey(keyInfo, null); - } - - /// Create a public key from the passed in SubjectPublicKeyInfo - /// the SubjectPublicKeyInfo containing the key data - /// default parameters that might be needed. - /// the appropriate key parameter - /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - AlgorithmIdentifier algId = keyInfo.AlgorithmID; - SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)Converters[algId.Algorithm]; - - if (converter != null) - { - return converter.GetPublicKeyParameters(keyInfo, defaultParams); - } - else - { - throw new IOException("algorithm identifier in public key not recognised: " + algId.Algorithm); - } - } - private abstract class SubjectPublicKeyInfoConverter - { - internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams); - } - - private class LmsConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) - { - return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); - } - else - { - // public key with extra tree height - if (keyEnc.Length == 64) - { - keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); - } - return HssPublicKeyParameters.GetInstance(keyEnc); - } - } - } - - private class SphincsPlusConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - SphincsPlusParameters spParams = SphincsPlusParameters.GetParams((int)Pack.BE_To_UInt32(keyEnc, 0)); - - return new SphincsPlusPublicKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); - } - } - - private class CmceConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = CmcePublicKey.GetInstance(keyInfo.ParsePublicKey()).T; - - CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new CmcePublicKeyParameters(spParams, keyEnc); - } - } - - private class SaberConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance( - DerSequence.GetInstance(keyInfo.ParsePublicKey())[0]).GetOctets(); - - SaberParameters saberParams = PqcUtilities.SaberParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new SaberPublicKeyParameters(saberParams, keyEnc); - } - } - - private class PicnicConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new PicnicPublicKeyParameters(picnicParams, keyEnc); - } - } - [Obsolete("Will be removed")] - private class SikeConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new SikePublicKeyParameters(sikeParams, keyEnc); - } - } - private class DilithiumConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - DilithiumParameters dilithiumParams = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm); - - try - { - Asn1Object obj = keyInfo.ParsePublicKey(); - if (obj is Asn1Sequence) - { - Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj); - - return new DilithiumPublicKeyParameters(dilithiumParams, - Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), - Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); - } - else - { - byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); - - return new DilithiumPublicKeyParameters(dilithiumParams, encKey); - } - } - catch (Exception e) - { - // raw encoding - return new DilithiumPublicKeyParameters(dilithiumParams, keyInfo.PublicKeyData.GetOctets()); - } - } - } - - private class KyberConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - KyberParameters kyberParameters = PqcUtilities.KyberParamsLookup(keyInfo.AlgorithmID.Algorithm); - - Asn1Object obj = keyInfo.ParsePublicKey(); - if (obj is Asn1Sequence) - { - Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj); - - return new KyberPublicKeyParameters(kyberParameters, - Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), - Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); - } - else - { - byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); - - return new KyberPublicKeyParameters(kyberParameters, encKey); - } - } - } - - private class FalconConverter - : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.AlgorithmID.Algorithm); - - try - { - Asn1Object obj = keyInfo.ParsePublicKey(); - if (obj is Asn1Sequence) - { - byte[] keyEnc = Asn1OctetString.GetInstance(Asn1Sequence.GetInstance(obj)[0]).GetOctets(); - - return new FalconPublicKeyParameters(falconParams, keyEnc); - } - else - { - // header byte + h - byte[] keyEnc = Asn1OctetString.GetInstance(obj).GetOctets(); - - if (keyEnc[0] != (byte)(0x00 + falconParams.LogN)) - { - throw new ArgumentException("byte[] enc of Falcon h value not tagged correctly"); - } - return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length)); - } - } - catch (Exception e) - { - // raw encoding - byte[] keyEnc = keyInfo.PublicKeyData.GetOctets(); - - if (keyEnc[0] != (byte)(0x00 + falconParams.LogN)) - { - throw new ArgumentException("byte[] enc of Falcon h value not tagged correctly"); - } - return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length)); - } - } - } - - private class BikeConverter: SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new BikePublicKeyParameters(bikeParams, keyEnc); - } - } - - private class HqcConverter : SubjectPublicKeyInfoConverter - { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.AlgorithmID.Algorithm); - - return new HqcPublicKeyParameters(hqcParams, keyEnc); - } - } - } -} diff --git a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs deleted file mode 100644 index 2b16cb260..000000000 --- a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs +++ /dev/null @@ -1,151 +0,0 @@ -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.Crystals.Kyber; -using Org.BouncyCastle.Pqc.Crypto.Falcon; -using Org.BouncyCastle.Pqc.Crypto.Hqc; -using Org.BouncyCastle.Pqc.Crypto.Lms; -using Org.BouncyCastle.Pqc.Crypto.Picnic; -using Org.BouncyCastle.Pqc.Crypto.Saber; -using Org.BouncyCastle.Pqc.Crypto.Sike; -using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Pqc.Crypto.Utilities -{ - /// - /// A factory to produce Public Key Info Objects. - /// - public static class SubjectPublicKeyInfoFactory - { - /// - /// Create a Subject Public Key Info object for a given public key. - /// - /// One of ElGammalPublicKeyParameters, DSAPublicKeyParameter, DHPublicKeyParameters, RsaKeyParameters or ECPublicKeyParameters - /// A subject public key info object. - /// Throw exception if object provided is not one of the above. - 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"); - - if (publicKey is LmsPublicKeyParameters lmsPublicKeyParameters) - { - byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPublicKeyParameters).Build(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } - if (publicKey is HssPublicKeyParameters hssPublicKeyParameters) - { - int L = hssPublicKeyParameters.L; - byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPublicKeyParameters.LmsPublicKey).Build(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } - if (publicKey is SphincsPlusPublicKeyParameters sphincsPlusPublicKeyParameters) - { - byte[] encoding = sphincsPlusPublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SphincsPlusOidLookup(sphincsPlusPublicKeyParameters.Parameters)); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } - if (publicKey is CmcePublicKeyParameters cmcePublicKeyParameters) - { - byte[] encoding = cmcePublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.McElieceOidLookup(cmcePublicKeyParameters.Parameters)); - - // https://datatracker.ietf.org/doc/draft-uni-qsckeys/ - return new SubjectPublicKeyInfo(algorithmIdentifier, new CmcePublicKey(encoding)); - } - if (publicKey is SaberPublicKeyParameters saberPublicKeyParameters) - { - byte[] encoding = saberPublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SaberOidLookup(saberPublicKeyParameters.Parameters)); - - // https://datatracker.ietf.org/doc/draft-uni-qsckeys/ - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerSequence(new DerOctetString(encoding))); - } - if (publicKey is PicnicPublicKeyParameters picnicPublicKeyParameters) - { - byte[] encoding = picnicPublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.PicnicOidLookup(picnicPublicKeyParameters.Parameters)); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } -#pragma warning disable CS0618 // Type or member is obsolete - if (publicKey is SikePublicKeyParameters sikePublicKeyParameters) - { - byte[] encoding = sikePublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SikeOidLookup(sikePublicKeyParameters.Parameters)); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } -#pragma warning restore CS0618 // Type or member is obsolete - if (publicKey is FalconPublicKeyParameters falconPublicKeyParameters) - { - byte[] keyEnc = falconPublicKeyParameters.GetEncoded(); - - byte[] encoding = new byte[keyEnc.Length + 1]; - encoding[0] = (byte)(0x00 + falconPublicKeyParameters.Parameters.LogN); - Array.Copy(keyEnc, 0, encoding, 1, keyEnc.Length); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.FalconOidLookup(falconPublicKeyParameters.Parameters)); - return new SubjectPublicKeyInfo(algorithmIdentifier, encoding); - } - if (publicKey is KyberPublicKeyParameters kyberPublicKeyParameters) - { - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.KyberOidLookup(kyberPublicKeyParameters.Parameters)); - Asn1EncodableVector v = new Asn1EncodableVector(); - v.Add(new DerOctetString(kyberPublicKeyParameters.T)); - v.Add(new DerOctetString(kyberPublicKeyParameters.Rho)); - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerSequence(v)); - } - if (publicKey is DilithiumPublicKeyParameters dilithiumPublicKeyParameters) - { - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.DilithiumOidLookup(dilithiumPublicKeyParameters.Parameters)); - - return new SubjectPublicKeyInfo(algorithmIdentifier, Arrays.Concatenate(dilithiumPublicKeyParameters.Rho, dilithiumPublicKeyParameters.T1)); - } - if (publicKey is BikePublicKeyParameters bikePublicKeyParameters) - { - byte[] encoding = bikePublicKeyParameters.GetEncoded(); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.BikeOidLookup(bikePublicKeyParameters.Parameters)); - - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } - if (publicKey is HqcPublicKeyParameters hqcPublicKeyParameters) - { - byte[] encoding = hqcPublicKeyParameters.GetEncoded(); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.HqcOidLookup(hqcPublicKeyParameters.Parameters)); - - return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); - } - - throw new ArgumentException("Class provided no convertible: " + Platform.GetTypeName(publicKey)); - } - } -} diff --git a/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs index 9d954b5da..ce23542e8 100644 --- a/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/BikeVectorTest.cs @@ -59,10 +59,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests kpGen.Init(genParam); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - BikePublicKeyParameters pubParams = (BikePublicKeyParameters)PublicKeyFactory.CreateKey( - SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((BikePublicKeyParameters) kp.Public)); - BikePrivateKeyParameters privParams = (BikePrivateKeyParameters)PrivateKeyFactory.CreateKey( - PrivateKeyInfoFactory.CreatePrivateKeyInfo((BikePrivateKeyParameters) kp.Private)); + BikePublicKeyParameters pubParams = (BikePublicKeyParameters)PqcPublicKeyFactory.CreateKey( + PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((BikePublicKeyParameters) kp.Public)); + BikePrivateKeyParameters privParams = (BikePrivateKeyParameters)PqcPrivateKeyFactory.CreateKey( + PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo((BikePrivateKeyParameters) kp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs b/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs index e6b4d120c..5c6a081e8 100644 --- a/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/CmceVectorTest.cs @@ -9,10 +9,10 @@ using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; -using PrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PrivateKeyFactory; -using PrivateKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PrivateKeyInfoFactory; -using PublicKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PublicKeyFactory; -using SubjectPublicKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.SubjectPublicKeyInfoFactory; +using PqcPrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPrivateKeyFactory; +using PqcPrivateKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPrivateKeyInfoFactory; +using PqcPublicKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPublicKeyFactory; +using PqcSubjectPublicKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcSubjectPublicKeyInfoFactory; namespace Org.BouncyCastle.Pqc.Crypto.Tests { @@ -100,8 +100,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests kpGen.Init(genParam); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - CmcePublicKeyParameters pubParams = (CmcePublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((CmcePublicKeyParameters)kp.Public)); - CmcePrivateKeyParameters privParams = (CmcePrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo((CmcePrivateKeyParameters)kp.Private)); + CmcePublicKeyParameters pubParams = (CmcePublicKeyParameters)PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((CmcePublicKeyParameters)kp.Public)); + CmcePrivateKeyParameters privParams = (CmcePrivateKeyParameters)PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo((CmcePrivateKeyParameters)kp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.GetPublicKey()), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.GetPrivateKey()), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs index f4a7c338f..db1273954 100644 --- a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs +++ b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs @@ -59,7 +59,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] altEncKey = Base64.Decode("oED4ZvqhK4kChPclP5VhOPvvnwof3QeTFq/zR5UtSr6SYicBkpe1TkafnSk5whDHgEp8imVwfeP2LbSopHb5KQuUFstGq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Dxbw3Hf3oT5u37fbaS2J28ehE1vBTB7526103TXmZVPVFjuTaUlvlmxkynG1PGSQRAjePR2AYr9DgR1oxmI/IW/9YoNOZuP7sq+szhvEqPOwg0fBX2TJPt2Dn9oZm+S83UbJHDAAQznTXis/AZ3NLiLnPDKrRlrRoD4g6Z4/EddtzDt9AaWLoKUIS2ZucI1/ouGTJI9g6wWNUwJaNSUkdC057KUeCAOHk="); - AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + AsymmetricKeyParameter altPubDec = PqcPublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); Assert.AreEqual(altEncKey, ((DilithiumPublicKeyParameters)altPubDec).GetEncoded()); Security.SecureRandom random = new Security.SecureRandom(); @@ -71,8 +71,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricKeyParameter pub = ackp.Public; AsymmetricKeyParameter priv = ackp.Private; - AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); - AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + AsymmetricKeyParameter pubDec = PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); Assert.AreEqual(((DilithiumPublicKeyParameters)pub).GetEncoded(), ((DilithiumPublicKeyParameters)pubDec).GetEncoded()); Assert.AreEqual(((DilithiumPrivateKeyParameters)priv).GetEncoded(), ((DilithiumPrivateKeyParameters)privDec).GetEncoded()); @@ -84,7 +84,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] altEncKey = Base64.Decode("CKJBWPvTOpeLU/2y0fhlWLc9HKYgH2pAARm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uyUPLbsZEiEkbOsCQr/JqKxeAjNmeCKUOlVpxZuHfj8mCMT5h2fXeByDj1Ie5szCdjhuqLjnSvWu1uXAQrdtyjCTMolNWKhBsUa+L8s5agh37qhk7ZHu5B1IrzDLUxyZ89aXda+6Rp/AC/F3EDZKEreruBqzVzIgOEo92RbyGXju2I2ekSr/AqUT2JBO6rdE6OWSt/ibdXFRkRv/f7XASwqwVsFkBgnNBmioZLSOdIYw12ZcvotlFtNKfi2BMe6lj2ZY2DqA/rnnekaIPh2dBIrevzWQgLStqrn/2g59F2saBH6bg="); - AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + AsymmetricKeyParameter altPubDec = PqcPublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); Assert.AreEqual(altEncKey, ((DilithiumPublicKeyParameters)altPubDec).GetEncoded()); Security.SecureRandom random = new Security.SecureRandom(); @@ -96,8 +96,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricKeyParameter pub = ackp.Public; AsymmetricKeyParameter priv = ackp.Private; - AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); - AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + AsymmetricKeyParameter pubDec = PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); Assert.AreEqual(((DilithiumPublicKeyParameters)pub).GetEncoded(), ((DilithiumPublicKeyParameters)pubDec).GetEncoded()); Assert.AreEqual(((DilithiumPrivateKeyParameters)priv).GetEncoded(), ((DilithiumPrivateKeyParameters)privDec).GetEncoded()); @@ -134,8 +134,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests //Console.WriteLine(string.Format("{0} Expected pk = {1}", pk.Length, Convert.ToHexString(pk))); //Console.WriteLine(String.Format("{0} Actual Public key = {1}", pubParams.GetEncoded().Length, Convert.ToHexString(pubParams.GetEncoded()))); - pubParams = (DilithiumPublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(ackp.Public)); - privParams = (DilithiumPrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(ackp.Private)); + pubParams = (DilithiumPublicKeyParameters)PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(ackp.Public)); + privParams = (DilithiumPrivateKeyParameters)PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(ackp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/FalconTest.cs b/crypto/test/src/pqc/crypto/test/FalconTest.cs index 093f8e6a8..83a603365 100644 --- a/crypto/test/src/pqc/crypto/test/FalconTest.cs +++ b/crypto/test/src/pqc/crypto/test/FalconTest.cs @@ -38,7 +38,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] altEncKey = Base64.Decode("vsUwFR+onoGQjgYmVpVBEacknmQpGTqsW+kNYw4VntPxVoRVLdsl2U24SzIAgrCZgwBAOnVK57arZT+5nF1afZZB5DzjapIKfPBhLlGmnQ6RX2j/WBaEoMAX7l46ZSb1GgycmZMgUFWvsYLEZr5D4zxOhfSkkzJ38NCzuOUNiFgYQ4TRtwxGwDM8WVV0vpL0nkLVnQJ7B0ZRBz++dXetoI651kjWW91GB3mj+x6U9SYx/KTF4zyejiEF2wVxKzgum13RiivF6cbBHLBeQLGBqRbgk5Vas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ax0B8w=="); - AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + AsymmetricKeyParameter altPubDec = PqcPublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); Assert.AreEqual(altEncKey, ((FalconPublicKeyParameters)altPubDec).GetEncoded()); Security.SecureRandom random = new Security.SecureRandom(); @@ -50,8 +50,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricKeyParameter pub = ackp.Public; AsymmetricKeyParameter priv = ackp.Private; - AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); - AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + AsymmetricKeyParameter pubDec = PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); Assert.AreEqual(((FalconPublicKeyParameters)pub).GetEncoded(), ((FalconPublicKeyParameters)pubDec).GetEncoded()); Assert.AreEqual(((FalconPrivateKeyParameters)priv).GetEncoded(), ((FalconPrivateKeyParameters)privDec).GetEncoded()); @@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] altEncKey = Base64.Decode(" dzEWmS1QASD+RCFcozO0ZaxX6qSYAAiSO3AfWTQMHLX6GbwR7Ui4XZfuS1k5wWhg3oH1hoBo3r09NX1Tnt3YauWDVW1fy2rw58XW0Q5I9mrixm3qpDJgwrknl71t5NMoX7IRHtLiGM0PHuH8uqUisyiMdzjoCoxovT9wkI/lmoYYaD+GdyiY5pKPgsoYjY9q9ZWjloUiRThp7xAhJYvf1MO58GwzTFxpIjkz1UqsanwOa/dxJjREvmGjv9xVh4S1FD6K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YgA6xOisFf237u5ihgNWASmiyL8Qs2T0X1sBYP+nLJGWRCjvN8Tz6yOo9HYgbpLLLdBZ9X0F6SV/Ednx6sNIgwp+GtjhAqCfjjTa5C/xN3quYSwo15q1TZXgyLAfyieKSwYpaKE+1+LaWwvpOchrjt0CFLwnZA06tMDKAINk1GuMh5jwRM6NBcCmvkdzScFipyRJ8ADksbgIx0wiNajYj222oL1HNO1mC1J0rfQdr94g5pOhTkWLD8Ivp7PKXWhCOwX5bAaoed2XaUzgL6bcITN"); - AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + AsymmetricKeyParameter altPubDec = PqcPublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); Assert.AreEqual(altEncKey, ((FalconPublicKeyParameters)altPubDec).GetEncoded()); Security.SecureRandom random = new Security.SecureRandom(); @@ -75,8 +75,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricKeyParameter pub = ackp.Public; AsymmetricKeyParameter priv = ackp.Private; - AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); - AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + AsymmetricKeyParameter pubDec = PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); Assert.AreEqual(((FalconPublicKeyParameters)pub).GetEncoded(), ((FalconPublicKeyParameters)pubDec).GetEncoded()); Assert.AreEqual(((FalconPrivateKeyParameters)priv).GetEncoded(), ((FalconPrivateKeyParameters)privDec).GetEncoded()); diff --git a/crypto/test/src/pqc/crypto/test/HqcVectorTest.cs b/crypto/test/src/pqc/crypto/test/HqcVectorTest.cs index c836c8cb3..604a411e6 100644 --- a/crypto/test/src/pqc/crypto/test/HqcVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/HqcVectorTest.cs @@ -62,8 +62,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests kpGen.Init(genParam); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - HqcPublicKeyParameters pubParams = (HqcPublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((HqcPublicKeyParameters) kp.Public)); - HqcPrivateKeyParameters privParams = (HqcPrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo((HqcPrivateKeyParameters) kp.Private)); + HqcPublicKeyParameters pubParams = (HqcPublicKeyParameters)PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((HqcPublicKeyParameters) kp.Public)); + HqcPrivateKeyParameters privParams = (HqcPrivateKeyParameters)PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo((HqcPrivateKeyParameters) kp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.PublicKey), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.PrivateKey), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/LMSTest.cs b/crypto/test/src/pqc/crypto/test/LMSTest.cs index 4392d1851..fc6e6d43c 100644 --- a/crypto/test/src/pqc/crypto/test/LMSTest.cs +++ b/crypto/test/src/pqc/crypto/test/LMSTest.cs @@ -8,8 +8,8 @@ using Org.BouncyCastle.Pqc.Crypto.Lms; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; -using PrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PrivateKeyFactory; -using PrivateKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PrivateKeyInfoFactory; +using PqcPrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPrivateKeyFactory; +using PqcPrivateKeyInfoFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPrivateKeyInfoFactory; namespace Org.BouncyCastle.Pqc.Crypto.Tests { @@ -102,8 +102,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.True(signer.VerifySignature(msg1, sig1)); - PrivateKeyInfo pInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private);//TODO - AsymmetricKeyParameter pKey = PrivateKeyFactory.CreateKey(pInfo.GetEncoded()); + PrivateKeyInfo pInfo = PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private);//TODO + AsymmetricKeyParameter pKey = PqcPrivateKeyFactory.CreateKey(pInfo.GetEncoded()); signer.Init(false, ((LmsPrivateKeyParameters)pKey).GetPublicKey()); diff --git a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs index c83bcae0b..85f8c1f1c 100644 --- a/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/PicnicVectorTest.cs @@ -65,8 +65,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - PicnicPublicKeyParameters pubParams = (PicnicPublicKeyParameters)PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public)); - PicnicPrivateKeyParameters privParams = (PicnicPrivateKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private)); + PicnicPublicKeyParameters pubParams = (PicnicPublicKeyParameters)PqcPublicKeyFactory.CreateKey(PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public)); + PicnicPrivateKeyParameters privParams = (PicnicPrivateKeyParameters)PqcPrivateKeyFactory.CreateKey(PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.GetEncoded()), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.GetEncoded()), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs index 86e0e5299..714a9a583 100644 --- a/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/SaberVectorTest.cs @@ -78,10 +78,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests kpGen.Init(genParam); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - SaberPublicKeyParameters pubParams = (SaberPublicKeyParameters)PublicKeyFactory.CreateKey( - SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((SaberPublicKeyParameters)kp.Public)); - SaberPrivateKeyParameters privParams = (SaberPrivateKeyParameters)PrivateKeyFactory.CreateKey( - PrivateKeyInfoFactory.CreatePrivateKeyInfo((SaberPrivateKeyParameters)kp.Private)); + SaberPublicKeyParameters pubParams = (SaberPublicKeyParameters)PqcPublicKeyFactory.CreateKey( + PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo((SaberPublicKeyParameters)kp.Public)); + SaberPrivateKeyParameters privParams = (SaberPrivateKeyParameters)PqcPrivateKeyFactory.CreateKey( + PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo((SaberPrivateKeyParameters)kp.Private)); Assert.True(Arrays.AreEqual(pk, pubParams.GetPublicKey()), name + " " + count + ": public key"); Assert.True(Arrays.AreEqual(sk, privParams.GetPrivateKey()), name + " " + count + ": secret key"); diff --git a/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs b/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs index e5f4d90ba..001916c93 100644 --- a/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs +++ b/crypto/test/src/pqc/crypto/test/SikeVectorTest.cs @@ -80,10 +80,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); // todo - SikePublicKeyParameters pubParams = (SikePublicKeyParameters)PublicKeyFactory.CreateKey( - SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public)); - SikePrivateKeyParameters privParams = (SikePrivateKeyParameters)PrivateKeyFactory.CreateKey( - PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private)); + SikePublicKeyParameters pubParams = (SikePublicKeyParameters)PqcPublicKeyFactory.CreateKey( + PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public)); + SikePrivateKeyParameters privParams = (SikePrivateKeyParameters)PqcPrivateKeyFactory.CreateKey( + PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private)); // SIKEPublicKeyParameters pubParams = (SIKEPublicKeyParameters)kp.Public; // SIKEPrivateKeyParameters privParams = (SIKEPrivateKeyParameters)kp.Private; diff --git a/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs b/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs index edaf33740..efeab54bd 100644 --- a/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs +++ b/crypto/test/src/pqc/crypto/test/SphincsPlusTest.cs @@ -16,8 +16,8 @@ using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; -using PrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PrivateKeyFactory; -using PublicKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PublicKeyFactory; +using PqcPrivateKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPrivateKeyFactory; +using PqcPublicKeyFactory = Org.BouncyCastle.Pqc.Crypto.Utilities.PqcPublicKeyFactory; namespace Org.BouncyCastle.Pqc.Crypto.Tests { @@ -122,11 +122,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.True(Arrays.AreEqual(Arrays.Concatenate(pubParams.Parameters.GetEncoded(), Hex.Decode("3e784ccb7ebcdcfd45542b7f6af778742e0f4479175084aa488b3b74340678aa6ba9430051e61cb676e8449087b938a79575b3a16736ce68a3655a28001155f5")), pubParams.GetEncoded())); Assert.True(Arrays.AreEqual(Arrays.Concatenate(privParams.Parameters.GetEncoded(), Hex.Decode("7c9935a0b07694aa0c6d10e4db6b1add2fd81a25ccb148032dcd739936737f2db505d7cfad1b497499323c8686325e4792f267aafa3f87ca60d01cb54f29202a3e784ccb7ebcdcfd45542b7f6af778742e0f4479175084aa488b3b74340678aa6ba9430051e61cb676e8449087b938a79575b3a16736ce68a3655a28001155f5")), privParams.GetEncoded())); - SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubParams); - PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privParams); + SubjectPublicKeyInfo pubInfo = PqcSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubParams); + PrivateKeyInfo privInfo = PqcPrivateKeyInfoFactory.CreatePrivateKeyInfo(privParams); - pubParams = (SphincsPlusPublicKeyParameters)PublicKeyFactory.CreateKey(pubInfo.GetEncoded()); - privParams = (SphincsPlusPrivateKeyParameters)PrivateKeyFactory.CreateKey(privInfo.GetEncoded()); + pubParams = (SphincsPlusPublicKeyParameters)PqcPublicKeyFactory.CreateKey(pubInfo.GetEncoded()); + privParams = (SphincsPlusPrivateKeyParameters)PqcPrivateKeyFactory.CreateKey(privInfo.GetEncoded()); Assert.True(Arrays.AreEqual(Arrays.Concatenate(pubParams.Parameters.GetEncoded(), Hex.Decode("3e784ccb7ebcdcfd45542b7f6af778742e0f4479175084aa488b3b74340678aa6ba9430051e61cb676e8449087b938a79575b3a16736ce68a3655a28001155f5")), pubParams.GetEncoded())); Assert.True(Arrays.AreEqual(Arrays.Concatenate(privParams.Parameters.GetEncoded(), Hex.Decode("7c9935a0b07694aa0c6d10e4db6b1add2fd81a25ccb148032dcd739936737f2db505d7cfad1b497499323c8686325e4792f267aafa3f87ca60d01cb54f29202a3e784ccb7ebcdcfd45542b7f6af778742e0f4479175084aa488b3b74340678aa6ba9430051e61cb676e8449087b938a79575b3a16736ce68a3655a28001155f5")), privParams.GetEncoded())); -- cgit 1.4.1