From b55db2642539aa659d2872830c55668912bf9d93 Mon Sep 17 00:00:00 2001 From: royb Date: Mon, 24 Oct 2022 12:57:52 -0400 Subject: fixed refactored class names in pqc/utils --- crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs | 84 ++++---- .../src/pqc/crypto/utils/PrivateKeyInfoFactory.cs | 158 ++++++++------- crypto/src/pqc/crypto/utils/PublicKeyFactory.cs | 215 ++++++++------------- 3 files changed, 191 insertions(+), 266 deletions(-) diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs index 7db65dbfb..8792727b8 100644 --- a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs @@ -9,12 +9,10 @@ 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; @@ -24,8 +22,9 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public static class PrivateKeyFactory + public class PrivateKeyFactory { + /// Create a private key parameter from a PKCS8 PrivateKeyInfo encoding. /// the PrivateKeyInfo encoding /// a suitable private key parameter @@ -44,6 +43,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities 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 @@ -71,6 +71,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); } } + if (algOID.On(BCObjectIdentifiers.pqc_kem_mceliece)) { CmcePrivateKey cmceKey = CmcePrivateKey.GetInstance(keyInfo.ParsePrivateKey()); @@ -78,6 +79,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities 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(); @@ -106,24 +108,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities return new SikePrivateKeyParameters(sikeParams, keyEnc); } - 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) @@ -174,7 +158,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities int version = DerInteger.GetInstance(keyEnc[0]).Value.IntValue; if (version != 0) + { throw new IOException("unknown private key version: " + version); + } if (keyInfo.PublicKeyData != null) { @@ -201,35 +187,39 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities } } if (algOID.Equals(BCObjectIdentifiers.falcon_512) || algOID.Equals(BCObjectIdentifiers.falcon_1024)) - { - Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); - FalconParameters spParams = PqcUtilities.FalconParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm); + { + 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); + 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); + 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"); + } } -} +} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs index 806eae8b7..b43810f13 100644 --- a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs +++ b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs @@ -5,12 +5,10 @@ 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; @@ -20,8 +18,13 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public static class PrivateKeyInfoFactory + public class PrivateKeyInfoFactory { + private PrivateKeyInfoFactory() + { + + } + /// Create a PrivateKeyInfo representation of a private key. /// the key to be encoded into the info object. /// the appropriate PrivateKeyInfo @@ -38,142 +41,135 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities /// on an error encoding the key public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) { - if (privateKey is LmsPrivateKeyParameters lmsPrivateKeyParameters) + if (privateKey is LmsPrivateKeyParameters) { - byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters).Build(); - byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()).Build(); + LmsPrivateKeyParameters parameters = (LmsPrivateKeyParameters)privateKey; + + byte[] encoding = Composer.Compose().U32Str(1).Bytes(parameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(parameters.GetPublicKey()).Build(); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); } - if (privateKey is HssPrivateKeyParameters hssPrivateKeyParameters) + if (privateKey is 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(); + HssPrivateKeyParameters parameters = (HssPrivateKeyParameters)privateKey; + + byte[] encoding = Composer.Compose().U32Str(parameters.L).Bytes(parameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(parameters.L).Bytes(parameters.GetPublicKey().LmsPublicKey).Build(); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); } - if (privateKey is SphincsPlusPrivateKeyParameters sphincsPlusPrivateKeyParameters) + if (privateKey is SphincsPlusPrivateKeyParameters) { - byte[] encoding = sphincsPlusPrivateKeyParameters.GetEncoded(); - byte[] pubEncoding = sphincsPlusPrivateKeyParameters.GetEncodedPublicKey(); + SphincsPlusPrivateKeyParameters parameters = (SphincsPlusPrivateKeyParameters)privateKey; + + byte[] encoding = parameters.GetEncoded(); + byte[] pubEncoding = parameters.GetEncodedPublicKey(); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SphincsPlusOidLookup(sphincsPlusPrivateKeyParameters.Parameters)); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.SphincsPlusOidLookup(parameters.Parameters)); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); } - if (privateKey is CmcePrivateKeyParameters cmcePrivateKeyParameters) + if (privateKey is CmcePrivateKeyParameters) { - byte[] encoding = cmcePrivateKeyParameters.GetEncoded(); + CmcePrivateKeyParameters parameters = (CmcePrivateKeyParameters) privateKey; + + byte[] encoding = parameters.GetEncoded(); AlgorithmIdentifier algorithmIdentifier = - new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(cmcePrivateKeyParameters.Parameters)); + new AlgorithmIdentifier(PqcUtilities.McElieceOidLookup(parameters.Parameters)); - CmcePublicKey CmcePub = new CmcePublicKey(cmcePrivateKeyParameters.ReconstructPublicKey()); - CmcePrivateKey CmcePriv = new CmcePrivateKey(0, cmcePrivateKeyParameters.Delta, - cmcePrivateKeyParameters.C, cmcePrivateKeyParameters.G, cmcePrivateKeyParameters.Alpha, - cmcePrivateKeyParameters.S, CmcePub); + CmcePublicKey CmcePub = new CmcePublicKey(parameters.ReconstructPublicKey()); + CmcePrivateKey CmcePriv = new CmcePrivateKey(0, parameters.Delta, parameters.C, parameters.G, + parameters.Alpha, parameters.S, CmcePub); return new PrivateKeyInfo(algorithmIdentifier, CmcePriv, attributes); } - if (privateKey is SaberPrivateKeyParameters saberPrivateKeyParameters) + if (privateKey is SaberPrivateKeyParameters) { - byte[] encoding = saberPrivateKeyParameters.GetEncoded(); + SaberPrivateKeyParameters parameters = (SaberPrivateKeyParameters)privateKey; - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SaberOidLookup(saberPrivateKeyParameters.Parameters)); + byte[] encoding = parameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.SaberOidLookup(parameters.Parameters)); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); } - if (privateKey is PicnicPrivateKeyParameters picnicPrivateKeyParameters) + if (privateKey is PicnicPrivateKeyParameters) { - byte[] encoding = picnicPrivateKeyParameters.GetEncoded(); + PicnicPrivateKeyParameters parameters = (PicnicPrivateKeyParameters)privateKey; + + byte[] encoding = parameters.GetEncoded(); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.PicnicOidLookup(picnicPrivateKeyParameters.Parameters)); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.PicnicOidLookup(parameters.Parameters)); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); } - if (privateKey is SikePrivateKeyParameters sikePrivateKeyParameters) + if (privateKey is SikePrivateKeyParameters) { - byte[] encoding = sikePrivateKeyParameters.GetEncoded(); + SikePrivateKeyParameters parameters = (SikePrivateKeyParameters)privateKey; - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.SikeOidLookup(sikePrivateKeyParameters.Parameters)); + byte[] encoding = parameters.GetEncoded(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.SikeOidLookup(parameters.Parameters)); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes); } - if (privateKey is FalconPrivateKeyParameters falconPrivateKeyParameters) + if (privateKey is FalconPrivateKeyParameters) { + FalconPrivateKeyParameters parameters = (FalconPrivateKeyParameters)privateKey; + Asn1EncodableVector v = new Asn1EncodableVector(); v.Add(new DerInteger(1)); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyf())); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetG())); - v.Add(new DerOctetString(falconPrivateKeyParameters.GetSpolyF())); + v.Add(new DerOctetString(parameters.GetSpolyf())); + v.Add(new DerOctetString(parameters.GetG())); + v.Add(new DerOctetString(parameters.GetSpolyF())); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.FalconOidLookup(falconPrivateKeyParameters.Parameters)); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.FalconOidLookup(parameters.Parameters)); - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, - falconPrivateKeyParameters.GetPublicKey()); + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, parameters.GetPublicKey()); } - if (privateKey is KyberPrivateKeyParameters kyberPrivateKeyParameters) + if (privateKey is KyberPrivateKeyParameters) { + KyberPrivateKeyParameters parameters = (KyberPrivateKeyParameters)privateKey; + 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)); + v.Add(new DerOctetString(parameters.S)); + v.Add(new DerOctetString(parameters.Hpk)); + v.Add(new DerOctetString(parameters.Nonce)); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.KyberOidLookup(kyberPrivateKeyParameters.Parameters)); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.KyberOidLookup(parameters.Parameters)); Asn1EncodableVector vPub = new Asn1EncodableVector(); - vPub.Add(new DerOctetString(kyberPrivateKeyParameters.T)); - vPub.Add(new DerOctetString(kyberPrivateKeyParameters.Rho)); + vPub.Add(new DerOctetString(parameters.T)); + vPub.Add(new DerOctetString(parameters.Rho)); - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, - new DerSequence(vPub).GetEncoded()); + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, new DerSequence(vPub).GetEncoded()); } - if (privateKey is DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters) + if (privateKey is DilithiumPrivateKeyParameters) { + DilithiumPrivateKeyParameters parameters = (DilithiumPrivateKeyParameters)privateKey; + 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)); + v.Add(new DerBitString(parameters.Rho)); + v.Add(new DerBitString(parameters.K)); + v.Add(new DerBitString(parameters.Tr)); + v.Add(new DerBitString(parameters.S1)); + v.Add(new DerBitString(parameters.S2)); + v.Add(new DerBitString(parameters.T0)); - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( - PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.DilithiumOidLookup(parameters.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(); + vPub.Add(new DerOctetString(parameters.Rho)); + vPub.Add(new DerOctetString(parameters.T1)); - 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); + return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, new DerSequence(vPub).GetEncoded()); } throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); } } -} +} \ No newline at end of file diff --git a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs index db68387f3..07df84ca1 100644 --- a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs @@ -4,19 +4,15 @@ 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; @@ -25,90 +21,80 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public static class PublicKeyFactory + public class PublicKeyFactory { - private static Dictionary Converters = - new Dictionary(); + 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.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.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.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.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] = 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.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] = 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.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] = 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.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(); + 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(); } - + /// Create a public key from a SubjectPublicKeyInfo encoding /// the SubjectPublicKeyInfo encoding /// the appropriate key parameter @@ -141,10 +127,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities /// default parameters that might be needed. /// the appropriate key parameter /// on an error decoding the key - public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, object defaultParams) + public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, Object defaultParams) { AlgorithmIdentifier algId = keyInfo.AlgorithmID; - SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)Converters[algId.Algorithm]; + SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)converters[algId.Algorithm]; if (converter != null) { @@ -157,36 +143,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities } 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); - } - } + internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams); } - + private class SphincsPlusConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { byte[] keyEnc = DerOctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); @@ -199,7 +162,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities private class CmceConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { byte[] keyEnc = CmcePublicKey.GetInstance(keyInfo.ParsePublicKey()).T; @@ -212,21 +175,21 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities private class SaberConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { byte[] keyEnc = DerOctetString.GetInstance( DerSequence.GetInstance(keyInfo.ParsePublicKey())[0]).GetOctets(); SaberParameters saberParams = PqcUtilities.SaberParamsLookup(keyInfo.AlgorithmID.Algorithm); - return new SaberPublicKeyParameters(saberParams, keyEnc); + return new SaberPublicKeyParameters(saberParams, keyEnc); } } private class PicnicConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { byte[] keyEnc = DerOctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); @@ -238,7 +201,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities private class SikeConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { byte[] keyEnc = DerOctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); @@ -250,7 +213,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities private class DilithiumConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { DilithiumParameters dilithiumParams = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm); @@ -275,7 +238,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities private class KyberConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { KyberParameters kyberParameters = PqcUtilities.KyberParamsLookup(keyInfo.AlgorithmID.Algorithm); @@ -296,11 +259,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities } } } - + private class FalconConverter : SubjectPublicKeyInfoConverter { - internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams) { FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.AlgorithmID.Algorithm); @@ -324,29 +287,5 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities } } } - - 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); - } - } } -} +} \ No newline at end of file -- cgit 1.4.1