diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-02-14 18:54:23 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-02-14 18:54:23 +0700 |
commit | fce6b3a69583099700820fc6f1822081a608f468 (patch) | |
tree | 5d8ca74de8ad2f72638316a27c7a34d66d04c9e4 | |
parent | added missing SphincsPlus Oids/Params (Haraka_simple which isn't approved by ... (diff) | |
download | BouncyCastle.NET-ed25519-fce6b3a69583099700820fc6f1822081a608f468.tar.xz |
Refactoring in Pqc.Crypto.Utilities
-rw-r--r-- | crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs | 9 | ||||
-rw-r--r-- | crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs | 570 |
2 files changed, 263 insertions, 316 deletions
diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs index 1fc937e6f..3cd5ce521 100644 --- a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs @@ -7,7 +7,6 @@ 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; @@ -162,17 +161,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities Asn1OctetString keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()); DilithiumParameters spParams = PqcUtilities.DilithiumParamsLookup(algOid); + DilithiumPublicKeyParameters pubKey = null; DerBitString publicKeyData = keyInfo.PublicKey; if (publicKeyData != null) { - var pubParams = PqcPublicKeyFactory.DilithiumConverter.GetPublicKeyParameters(spParams, - publicKeyData); - - return new DilithiumPrivateKeyParameters(spParams, keyEnc.GetOctets(), pubParams); + pubKey = PqcPublicKeyFactory.GetDilithiumPublicKey(spParams, publicKeyData); } - return new DilithiumPrivateKeyParameters(spParams, keyEnc.GetOctets(), null); + return new DilithiumPrivateKeyParameters(spParams, encoding: keyEnc.GetOctets(), pubKey); } if (algOid.Equals(BCObjectIdentifiers.falcon_512) || algOid.Equals(BCObjectIdentifiers.falcon_1024)) diff --git a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs index a1c8b5172..0f16f477b 100644 --- a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs @@ -22,146 +22,147 @@ using Org.BouncyCastle.Pqc.Crypto.Saber; using Org.BouncyCastle.Pqc.Crypto.Sike; using Org.BouncyCastle.Pqc.Crypto.SphincsPlus; using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { public static class PqcPublicKeyFactory { - private static Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter> Converters = - new Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter>(); + private delegate AsymmetricKeyParameter Converter(SubjectPublicKeyInfo keyInfo, object defaultParams); + + private static Dictionary<DerObjectIdentifier, Converter> Converters = + new Dictionary<DerObjectIdentifier, Converter>(); static PqcPublicKeyFactory() { - Converters[PkcsObjectIdentifiers.IdAlgHssLmsHashsig] = new LmsConverter(); - - 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.frodokem640aes] = new FrodoConverter(); - Converters[BCObjectIdentifiers.frodokem640shake] = new FrodoConverter(); - Converters[BCObjectIdentifiers.frodokem976aes] = new FrodoConverter(); - Converters[BCObjectIdentifiers.frodokem976shake] = new FrodoConverter(); - Converters[BCObjectIdentifiers.frodokem1344aes] = new FrodoConverter(); - Converters[BCObjectIdentifiers.frodokem1344shake] = new FrodoConverter(); - - 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[PkcsObjectIdentifiers.IdAlgHssLmsHashsig] = LmsConverter; + + Converters[BCObjectIdentifiers.mceliece348864_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece348864f_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece460896_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece460896f_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece6688128_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece6688128f_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece6960119_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece6960119f_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece8192128_r3] = CmceConverter; + Converters[BCObjectIdentifiers.mceliece8192128f_r3] = CmceConverter; + + Converters[BCObjectIdentifiers.frodokem640aes] = FrodoConverter; + Converters[BCObjectIdentifiers.frodokem640shake] = FrodoConverter; + Converters[BCObjectIdentifiers.frodokem976aes] = FrodoConverter; + Converters[BCObjectIdentifiers.frodokem976shake] = FrodoConverter; + Converters[BCObjectIdentifiers.frodokem1344aes] = FrodoConverter; + Converters[BCObjectIdentifiers.frodokem1344shake] = FrodoConverter; + + Converters[BCObjectIdentifiers.lightsaberkem128r3] = SaberConverter; + Converters[BCObjectIdentifiers.saberkem128r3] = SaberConverter; + Converters[BCObjectIdentifiers.firesaberkem128r3] = SaberConverter; + Converters[BCObjectIdentifiers.lightsaberkem192r3] = SaberConverter; + Converters[BCObjectIdentifiers.saberkem192r3] = SaberConverter; + Converters[BCObjectIdentifiers.firesaberkem192r3] = SaberConverter; + Converters[BCObjectIdentifiers.lightsaberkem256r3] = SaberConverter; + Converters[BCObjectIdentifiers.saberkem256r3] = SaberConverter; + Converters[BCObjectIdentifiers.firesaberkem256r3] = SaberConverter; + Converters[BCObjectIdentifiers.ulightsaberkemr3] = SaberConverter; + Converters[BCObjectIdentifiers.usaberkemr3] = SaberConverter; + Converters[BCObjectIdentifiers.ufiresaberkemr3] = SaberConverter; + Converters[BCObjectIdentifiers.lightsaberkem90sr3] = SaberConverter; + Converters[BCObjectIdentifiers.saberkem90sr3] = SaberConverter; + Converters[BCObjectIdentifiers.firesaberkem90sr3] = SaberConverter; + Converters[BCObjectIdentifiers.ulightsaberkem90sr3] = SaberConverter; + Converters[BCObjectIdentifiers.usaberkem90sr3] = SaberConverter; + Converters[BCObjectIdentifiers.ufiresaberkem90sr3] = 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(); + Converters[BCObjectIdentifiers.picnic] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl1fs] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl1ur] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl3fs] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl3ur] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl5fs] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl5ur] = PicnicConverter; + Converters[BCObjectIdentifiers.picnic3l1] = PicnicConverter; + Converters[BCObjectIdentifiers.picnic3l3] = PicnicConverter; + Converters[BCObjectIdentifiers.picnic3l5] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl1full] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl3full] = PicnicConverter; + Converters[BCObjectIdentifiers.picnicl5full] = 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(); + Converters[BCObjectIdentifiers.sikep434] = SikeConverter; + Converters[BCObjectIdentifiers.sikep503] = SikeConverter; + Converters[BCObjectIdentifiers.sikep610] = SikeConverter; + Converters[BCObjectIdentifiers.sikep751] = SikeConverter; + Converters[BCObjectIdentifiers.sikep434_compressed] = SikeConverter; + Converters[BCObjectIdentifiers.sikep503_compressed] = SikeConverter; + Converters[BCObjectIdentifiers.sikep610_compressed] = SikeConverter; + Converters[BCObjectIdentifiers.sikep751_compressed] = 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.dilithium2] = DilithiumConverter; + Converters[BCObjectIdentifiers.dilithium3] = DilithiumConverter; + Converters[BCObjectIdentifiers.dilithium5] = DilithiumConverter; + Converters[BCObjectIdentifiers.dilithium2_aes] = DilithiumConverter; + Converters[BCObjectIdentifiers.dilithium3_aes] = DilithiumConverter; + Converters[BCObjectIdentifiers.dilithium5_aes] = DilithiumConverter; - Converters[BCObjectIdentifiers.falcon_512] = new FalconConverter(); - Converters[BCObjectIdentifiers.falcon_1024] = new FalconConverter(); + Converters[BCObjectIdentifiers.falcon_512] = FalconConverter; + Converters[BCObjectIdentifiers.falcon_1024] = 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(); - - - Converters[BCObjectIdentifiers.sphincsPlus] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_128s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_128f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_128s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_128f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_128s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_128f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_192s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_192f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_192s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_192f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_192s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_192f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_256s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_256f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_256s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_256f_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_256s_r3] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_256f_r3] = new SphincsPlusConverter(); - - Converters[BCObjectIdentifiers.sphincsPlus_haraka_128f_r3_simple] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_128s_r3_simple] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_192f_r3_simple] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_192s_r3_simple] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_256f_r3_simple] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_haraka_256s_r3_simple] = new SphincsPlusConverter(); - - Converters[BCObjectIdentifiers.sphincsPlus_sha2_128s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_128f] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_128s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_128f] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_192s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_192f] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_192s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_192f] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_256s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_sha2_256f] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_256s] = new SphincsPlusConverter(); - Converters[BCObjectIdentifiers.sphincsPlus_shake_256f] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.kyber512] = KyberConverter; + Converters[BCObjectIdentifiers.kyber512_aes] = KyberConverter; + Converters[BCObjectIdentifiers.kyber768] = KyberConverter; + Converters[BCObjectIdentifiers.kyber768_aes] = KyberConverter; + Converters[BCObjectIdentifiers.kyber1024] = KyberConverter; + Converters[BCObjectIdentifiers.kyber1024_aes] = KyberConverter; + + Converters[BCObjectIdentifiers.bike128] = BikeConverter; + Converters[BCObjectIdentifiers.bike192] = BikeConverter; + Converters[BCObjectIdentifiers.bike256] = BikeConverter; + + Converters[BCObjectIdentifiers.hqc128] = HqcConverter; + Converters[BCObjectIdentifiers.hqc192] = HqcConverter; + Converters[BCObjectIdentifiers.hqc256] = HqcConverter; + + + Converters[BCObjectIdentifiers.sphincsPlus] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_128s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_128f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_128s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_128f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_128s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_128f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_192s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_192f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_192s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_192f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_192s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_192f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_256s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_256f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_256s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_256f_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_256s_r3] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_256f_r3] = SphincsPlusConverter; + + Converters[BCObjectIdentifiers.sphincsPlus_haraka_128f_r3_simple] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_128s_r3_simple] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_192f_r3_simple] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_192s_r3_simple] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_256f_r3_simple] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_haraka_256s_r3_simple] = SphincsPlusConverter; + + Converters[BCObjectIdentifiers.sphincsPlus_sha2_128s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_128f] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_128s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_128f] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_192s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_192f] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_192s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_192f] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_256s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_sha2_256f] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_256s] = SphincsPlusConverter; + Converters[BCObjectIdentifiers.sphincsPlus_shake_256f] = SphincsPlusConverter; } /// <summary> Create a public key from a SubjectPublicKeyInfo encoding</summary> @@ -198,228 +199,172 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities /// <exception cref="IOException"> on an error decoding the key</exception> public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, object defaultParams) { - var oid = keyInfo.Algorithm.Algorithm; - - SubjectPublicKeyInfoConverter converter = CollectionUtilities.GetValueOrNull(Converters, oid) - ?? throw new IOException("algorithm identifier in public key not recognised: " + oid); + var algID = keyInfo.Algorithm; + var algOid = algID.Algorithm; - return converter.GetPublicKeyParameters(keyInfo, defaultParams); - } + if (!Converters.TryGetValue(algOid, out var converter)) + throw new IOException("algorithm identifier in public key not recognised: " + algOid); - internal abstract class SubjectPublicKeyInfoConverter - { - internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams); + return converter(keyInfo, defaultParams); } - private class LmsConverter - : SubjectPublicKeyInfoConverter + internal static DilithiumPublicKeyParameters GetDilithiumPublicKey(DilithiumParameters dilithiumParameters, + DerBitString publicKeyData) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + byte[] publicKeyOctets = publicKeyData.GetOctets(); + try { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) + Asn1Object obj = Asn1Object.FromByteArray(publicKeyOctets); + if (obj is Asn1Sequence keySeq) { - return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + return new DilithiumPublicKeyParameters(dilithiumParameters, + Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), + Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); } else { - // public key with extra tree height - if (keyEnc.Length == 64) - { - keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); - } - return HssPublicKeyParameters.GetInstance(keyEnc); + byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); + + return new DilithiumPublicKeyParameters(dilithiumParameters, encKey); } } + catch (Exception) + { + // we're a raw encoding + return new DilithiumPublicKeyParameters(dilithiumParameters, publicKeyOctets); + } } - private class SphincsPlusConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter LmsConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - try - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - - SphincsPlusParameters spParams = PqcUtilities.SphincsPlusParamsLookup(keyInfo.Algorithm.Algorithm); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - return new SphincsPlusPublicKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); - } - catch (Exception) + 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) { - byte[] keyEnc = keyInfo.PublicKey.GetOctets(); - - SphincsPlusParameters spParams = PqcUtilities.SphincsPlusParamsLookup(keyInfo.Algorithm.Algorithm); - - return new SphincsPlusPublicKeyParameters(spParams, keyEnc); + keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); } + return HssPublicKeyParameters.GetInstance(keyEnc); } } - - private class CmceConverter - : SubjectPublicKeyInfoConverter + + private static AsymmetricKeyParameter SphincsPlusConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + try { - byte[] keyEnc = CmcePublicKey.GetInstance(keyInfo.ParsePublicKey()).T; + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + SphincsPlusParameters spParams = PqcUtilities.SphincsPlusParamsLookup(keyInfo.Algorithm.Algorithm); + + return new SphincsPlusPublicKeyParameters(spParams, Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + catch (Exception) + { + byte[] keyEnc = keyInfo.PublicKey.GetOctets(); - CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.Algorithm.Algorithm); + SphincsPlusParameters spParams = PqcUtilities.SphincsPlusParamsLookup(keyInfo.Algorithm.Algorithm); - return new CmcePublicKeyParameters(spParams, keyEnc); + return new SphincsPlusPublicKeyParameters(spParams, keyEnc); } } - private class FrodoConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter CmceConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + byte[] keyEnc = CmcePublicKey.GetInstance(keyInfo.ParsePublicKey()).T; - FrodoParameters fParams = PqcUtilities.FrodoParamsLookup(keyInfo.Algorithm.Algorithm); + CmceParameters spParams = PqcUtilities.McElieceParamsLookup(keyInfo.Algorithm.Algorithm); - return new FrodoPublicKeyParameters(fParams, keyEnc); - } + return new CmcePublicKeyParameters(spParams, keyEnc); } - private class SaberConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter FrodoConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance( - Asn1Sequence.GetInstance(keyInfo.ParsePublicKey())[0]).GetOctets(); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - SaberParameters saberParams = PqcUtilities.SaberParamsLookup(keyInfo.Algorithm.Algorithm); + FrodoParameters fParams = PqcUtilities.FrodoParamsLookup(keyInfo.Algorithm.Algorithm); - return new SaberPublicKeyParameters(saberParams, keyEnc); - } + return new FrodoPublicKeyParameters(fParams, keyEnc); } - - private class PicnicConverter - : SubjectPublicKeyInfoConverter + + private static AsymmetricKeyParameter SaberConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + byte[] keyEnc = Asn1OctetString.GetInstance( + Asn1Sequence.GetInstance(keyInfo.ParsePublicKey())[0]).GetOctets(); - PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.Algorithm.Algorithm); + SaberParameters saberParams = PqcUtilities.SaberParamsLookup(keyInfo.Algorithm.Algorithm); - return new PicnicPublicKeyParameters(picnicParams, keyEnc); - } + return new SaberPublicKeyParameters(saberParams, keyEnc); } - [Obsolete("Will be removed")] - private class SikeConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter PicnicConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.Algorithm.Algorithm); + PicnicParameters picnicParams = PqcUtilities.PicnicParamsLookup(keyInfo.Algorithm.Algorithm); - return new SikePublicKeyParameters(sikeParams, keyEnc); - } + return new PicnicPublicKeyParameters(picnicParams, keyEnc); } - internal class DilithiumConverter - : SubjectPublicKeyInfoConverter + [Obsolete("Will be removed")] + private static AsymmetricKeyParameter SikeConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, - object defaultParams) - { - var dilithiumParameters = PqcUtilities.DilithiumParamsLookup(keyInfo.Algorithm.Algorithm); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - return GetPublicKeyParameters(dilithiumParameters, keyInfo.PublicKey); - } + SikeParameters sikeParams = PqcUtilities.SikeParamsLookup(keyInfo.Algorithm.Algorithm); - internal static DilithiumPublicKeyParameters GetPublicKeyParameters(DilithiumParameters dilithiumParameters, - DerBitString publicKeyData) - { - try - { - Asn1Object obj = Asn1Object.FromByteArray(publicKeyData.GetOctets()); - if (obj is Asn1Sequence keySeq) - { - return new DilithiumPublicKeyParameters(dilithiumParameters, - Asn1OctetString.GetInstance(keySeq[0]).GetOctets(), - Asn1OctetString.GetInstance(keySeq[1]).GetOctets()); - } - else - { - byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets(); + return new SikePublicKeyParameters(sikeParams, keyEnc); + } - return new DilithiumPublicKeyParameters(dilithiumParameters, encKey); - } - } - catch (Exception) - { - // we're a raw encoding - return new DilithiumPublicKeyParameters(dilithiumParameters, publicKeyData.GetOctets()); - } - } + private static AsymmetricKeyParameter DilithiumConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + var dilithiumParameters = PqcUtilities.DilithiumParamsLookup(keyInfo.Algorithm.Algorithm); + + return GetDilithiumPublicKey(dilithiumParameters, publicKeyData: keyInfo.PublicKey); } - private class KyberConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter KyberConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, - object defaultParams) - { - KyberParameters kyberParameters = PqcUtilities.KyberParamsLookup(keyInfo.Algorithm.Algorithm); + KyberParameters kyberParameters = PqcUtilities.KyberParamsLookup(keyInfo.Algorithm.Algorithm); - try - { - Asn1Object obj = keyInfo.ParsePublicKey(); + try + { + Asn1Object obj = keyInfo.ParsePublicKey(); #pragma warning disable CS0618 // Type or member is obsolete - KyberPublicKey kyberKey = KyberPublicKey.GetInstance(obj); + KyberPublicKey kyberKey = KyberPublicKey.GetInstance(obj); #pragma warning restore CS0618 // Type or member is obsolete - return new KyberPublicKeyParameters(kyberParameters, kyberKey.T, kyberKey.Rho); - } - catch (Exception) - { - // we're a raw encoding - return new KyberPublicKeyParameters(kyberParameters, keyInfo.PublicKey.GetOctets()); - } + return new KyberPublicKeyParameters(kyberParameters, kyberKey.T, kyberKey.Rho); + } + catch (Exception) + { + // we're a raw encoding + return new KyberPublicKeyParameters(kyberParameters, keyInfo.PublicKey.GetOctets()); } } - private class FalconConverter - : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter FalconConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) - { - FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.Algorithm.Algorithm); + FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.Algorithm.Algorithm); - try + try + { + Asn1Object obj = keyInfo.ParsePublicKey(); + if (obj is Asn1Sequence) { - Asn1Object obj = keyInfo.ParsePublicKey(); - if (obj is Asn1Sequence) - { - byte[] keyEnc = Asn1OctetString.GetInstance(Asn1Sequence.GetInstance(obj)[0]).GetOctets(); + 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)); - } + return new FalconPublicKeyParameters(falconParams, keyEnc); } - catch (Exception) + else { - // raw encoding - byte[] keyEnc = keyInfo.PublicKey.GetOctets(); + // header byte + h + byte[] keyEnc = Asn1OctetString.GetInstance(obj).GetOctets(); if (keyEnc[0] != (byte)(0x00 + falconParams.LogN)) { @@ -428,52 +373,57 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length)); } } + catch (Exception) + { + // raw encoding + byte[] keyEnc = keyInfo.PublicKey.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 + private static AsymmetricKeyParameter BikeConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + try { - try - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm); + BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm); - return new BikePublicKeyParameters(bikeParams, keyEnc); - } - catch (Exception) - { - byte[] keyEnc = keyInfo.PublicKey.GetOctets(); + return new BikePublicKeyParameters(bikeParams, keyEnc); + } + catch (Exception) + { + byte[] keyEnc = keyInfo.PublicKey.GetOctets(); - BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm); + BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm); - return new BikePublicKeyParameters(bikeParams, keyEnc); - } + return new BikePublicKeyParameters(bikeParams, keyEnc); } } - private class HqcConverter : SubjectPublicKeyInfoConverter + private static AsymmetricKeyParameter HqcConverter(SubjectPublicKeyInfo keyInfo, object defaultParams) { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + try { - try - { - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); - HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm); + HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm); - return new HqcPublicKeyParameters(hqcParams, keyEnc); - } - catch (Exception) - { - // raw encoding - byte[] keyEnc = keyInfo.PublicKey.GetOctets(); + return new HqcPublicKeyParameters(hqcParams, keyEnc); + } + catch (Exception) + { + // raw encoding + byte[] keyEnc = keyInfo.PublicKey.GetOctets(); - HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm); + HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm); - return new HqcPublicKeyParameters(hqcParams, keyEnc); - } + return new HqcPublicKeyParameters(hqcParams, keyEnc); } } } |