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

GeneratorUtilities

public static class GeneratorUtilities
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.EdEC; using Org.BouncyCastle.Asn1.Iana; using Org.BouncyCastle.Asn1.Kisa; using Org.BouncyCastle.Asn1.Misc; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Nsri; using Org.BouncyCastle.Asn1.Ntt; using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using System; using System.Collections.Generic; namespace Org.BouncyCastle.Security { public static class GeneratorUtilities { private static readonly IDictionary<string, string> KgAlgorithms; private static readonly IDictionary<string, string> KpgAlgorithms; private static readonly IDictionary<string, int> DefaultKeySizes; static GeneratorUtilities() { KgAlgorithms = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); KpgAlgorithms = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); DefaultKeySizes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); AddKgAlgorithm("AES", "AESWRAP"); AddKgAlgorithm("AES128", SecurityUtilities.WrongAes128, NistObjectIdentifiers.IdAes128Cbc, NistObjectIdentifiers.IdAes128Ccm, NistObjectIdentifiers.IdAes128Cfb, NistObjectIdentifiers.IdAes128Ecb, NistObjectIdentifiers.IdAes128Gcm, NistObjectIdentifiers.IdAes128Ofb, NistObjectIdentifiers.IdAes128Wrap, NistObjectIdentifiers.IdAes128WrapPad); AddKgAlgorithm("AES192", SecurityUtilities.WrongAes192, NistObjectIdentifiers.IdAes192Cbc, NistObjectIdentifiers.IdAes192Ccm, NistObjectIdentifiers.IdAes192Cfb, NistObjectIdentifiers.IdAes192Ecb, NistObjectIdentifiers.IdAes192Gcm, NistObjectIdentifiers.IdAes192Ofb, NistObjectIdentifiers.IdAes192Wrap, NistObjectIdentifiers.IdAes192WrapPad); AddKgAlgorithm("AES256", SecurityUtilities.WrongAes256, NistObjectIdentifiers.IdAes256Cbc, NistObjectIdentifiers.IdAes256Ccm, NistObjectIdentifiers.IdAes256Cfb, NistObjectIdentifiers.IdAes256Ecb, NistObjectIdentifiers.IdAes256Gcm, NistObjectIdentifiers.IdAes256Ofb, NistObjectIdentifiers.IdAes256Wrap, NistObjectIdentifiers.IdAes256WrapPad); AddKgAlgorithm("BLOWFISH", "1.3.6.1.4.1.3029.1.2", MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC); AddKgAlgorithm("CAMELLIA", "CAMELLIAWRAP"); AddKgAlgorithm("ARIA", Array.Empty<object>()); AddKgAlgorithm("ARIA128", NsriObjectIdentifiers.id_aria128_cbc, NsriObjectIdentifiers.id_aria128_ccm, NsriObjectIdentifiers.id_aria128_cfb, NsriObjectIdentifiers.id_aria128_ctr, NsriObjectIdentifiers.id_aria128_ecb, NsriObjectIdentifiers.id_aria128_gcm, NsriObjectIdentifiers.id_aria128_kw, NsriObjectIdentifiers.id_aria128_kwp, NsriObjectIdentifiers.id_aria128_ocb2, NsriObjectIdentifiers.id_aria128_ofb); AddKgAlgorithm("ARIA192", NsriObjectIdentifiers.id_aria192_cbc, NsriObjectIdentifiers.id_aria192_ccm, NsriObjectIdentifiers.id_aria192_cfb, NsriObjectIdentifiers.id_aria192_ctr, NsriObjectIdentifiers.id_aria192_ecb, NsriObjectIdentifiers.id_aria192_gcm, NsriObjectIdentifiers.id_aria192_kw, NsriObjectIdentifiers.id_aria192_kwp, NsriObjectIdentifiers.id_aria192_ocb2, NsriObjectIdentifiers.id_aria192_ofb); AddKgAlgorithm("ARIA256", NsriObjectIdentifiers.id_aria256_cbc, NsriObjectIdentifiers.id_aria256_ccm, NsriObjectIdentifiers.id_aria256_cfb, NsriObjectIdentifiers.id_aria256_ctr, NsriObjectIdentifiers.id_aria256_ecb, NsriObjectIdentifiers.id_aria256_gcm, NsriObjectIdentifiers.id_aria256_kw, NsriObjectIdentifiers.id_aria256_kwp, NsriObjectIdentifiers.id_aria256_ocb2, NsriObjectIdentifiers.id_aria256_ofb); AddKgAlgorithm("CAMELLIA128", NttObjectIdentifiers.IdCamellia128Cbc, NttObjectIdentifiers.IdCamellia128Wrap); AddKgAlgorithm("CAMELLIA192", NttObjectIdentifiers.IdCamellia192Cbc, NttObjectIdentifiers.IdCamellia192Wrap); AddKgAlgorithm("CAMELLIA256", NttObjectIdentifiers.IdCamellia256Cbc, NttObjectIdentifiers.IdCamellia256Wrap); AddKgAlgorithm("CAST5", MiscObjectIdentifiers.cast5CBC); AddKgAlgorithm("CAST6", Array.Empty<object>()); AddKgAlgorithm("CHACHA", Array.Empty<object>()); AddKgAlgorithm("CHACHA7539", "CHACHA20", "CHACHA20-POLY1305", PkcsObjectIdentifiers.IdAlgAeadChaCha20Poly1305); AddKgAlgorithm("DES", OiwObjectIdentifiers.DesCbc, OiwObjectIdentifiers.DesCfb, OiwObjectIdentifiers.DesEcb, OiwObjectIdentifiers.DesOfb); AddKgAlgorithm("DESEDE", "DESEDEWRAP", "TDEA", OiwObjectIdentifiers.DesEde); AddKgAlgorithm("DESEDE3", PkcsObjectIdentifiers.DesEde3Cbc, PkcsObjectIdentifiers.IdAlgCms3DesWrap); AddKgAlgorithm("GOST28147", "GOST", "GOST-28147", CryptoProObjectIdentifiers.GostR28147Gcfb); AddKgAlgorithm("HC128", Array.Empty<object>()); AddKgAlgorithm("HC256", Array.Empty<object>()); AddKgAlgorithm("IDEA", MiscObjectIdentifiers.as_sys_sec_alg_ideaCBC); AddKgAlgorithm("NOEKEON", Array.Empty<object>()); AddKgAlgorithm("RC2", PkcsObjectIdentifiers.RC2Cbc, PkcsObjectIdentifiers.IdAlgCmsRC2Wrap); AddKgAlgorithm("RC4", "ARC4", PkcsObjectIdentifiers.rc4); AddKgAlgorithm("RC5", "RC5-32"); AddKgAlgorithm("RC5-64", Array.Empty<object>()); AddKgAlgorithm("RC6", Array.Empty<object>()); AddKgAlgorithm("RIJNDAEL", Array.Empty<object>()); AddKgAlgorithm("SALSA20", Array.Empty<object>()); AddKgAlgorithm("SEED", KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap, KisaObjectIdentifiers.IdSeedCbc); AddKgAlgorithm("SERPENT", Array.Empty<object>()); AddKgAlgorithm("SKIPJACK", Array.Empty<object>()); AddKgAlgorithm("SM4", Array.Empty<object>()); AddKgAlgorithm("TEA", Array.Empty<object>()); AddKgAlgorithm("THREEFISH-256", Array.Empty<object>()); AddKgAlgorithm("THREEFISH-512", Array.Empty<object>()); AddKgAlgorithm("THREEFISH-1024", Array.Empty<object>()); AddKgAlgorithm("TNEPRES", Array.Empty<object>()); AddKgAlgorithm("TWOFISH", Array.Empty<object>()); AddKgAlgorithm("VMPC", Array.Empty<object>()); AddKgAlgorithm("VMPC-KSA3", Array.Empty<object>()); AddKgAlgorithm("XTEA", Array.Empty<object>()); AddHMacKeyGenerator("MD2", Array.Empty<object>()); AddHMacKeyGenerator("MD4", Array.Empty<object>()); AddHMacKeyGenerator("MD5", IanaObjectIdentifiers.HmacMD5); AddHMacKeyGenerator("SHA1", PkcsObjectIdentifiers.IdHmacWithSha1, IanaObjectIdentifiers.HmacSha1); AddHMacKeyGenerator("SHA224", PkcsObjectIdentifiers.IdHmacWithSha224); AddHMacKeyGenerator("SHA256", PkcsObjectIdentifiers.IdHmacWithSha256); AddHMacKeyGenerator("SHA384", PkcsObjectIdentifiers.IdHmacWithSha384); AddHMacKeyGenerator("SHA512", PkcsObjectIdentifiers.IdHmacWithSha512); AddHMacKeyGenerator("SHA512/224", PkcsObjectIdentifiers.IdHmacWithSha512_224); AddHMacKeyGenerator("SHA512-224", Array.Empty<object>()); AddHMacKeyGenerator("SHA512/256", PkcsObjectIdentifiers.IdHmacWithSha512_256); AddHMacKeyGenerator("SHA512-256", Array.Empty<object>()); AddHMacKeyGenerator("KECCAK224", Array.Empty<object>()); AddHMacKeyGenerator("KECCAK256", Array.Empty<object>()); AddHMacKeyGenerator("KECCAK288", Array.Empty<object>()); AddHMacKeyGenerator("KECCAK384", Array.Empty<object>()); AddHMacKeyGenerator("KECCAK512", Array.Empty<object>()); AddHMacKeyGenerator("SHA3-224", NistObjectIdentifiers.IdHMacWithSha3_224); AddHMacKeyGenerator("SHA3-256", NistObjectIdentifiers.IdHMacWithSha3_256); AddHMacKeyGenerator("SHA3-384", NistObjectIdentifiers.IdHMacWithSha3_384); AddHMacKeyGenerator("SHA3-512", NistObjectIdentifiers.IdHMacWithSha3_512); AddHMacKeyGenerator("RIPEMD128", Array.Empty<object>()); AddHMacKeyGenerator("RIPEMD160", IanaObjectIdentifiers.HmacRipeMD160); AddHMacKeyGenerator("TIGER", IanaObjectIdentifiers.HmacTiger); AddHMacKeyGenerator("GOST3411-2012-256", RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_256); AddHMacKeyGenerator("GOST3411-2012-512", RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_512); AddKpgAlgorithm("DH", "DIFFIEHELLMAN"); AddKpgAlgorithm("DSA", Array.Empty<object>()); AddKpgAlgorithm("EC", X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme, SecObjectIdentifiers.dhSinglePass_stdDH_sha224kdf_scheme, SecObjectIdentifiers.dhSinglePass_stdDH_sha256kdf_scheme, SecObjectIdentifiers.dhSinglePass_stdDH_sha384kdf_scheme, SecObjectIdentifiers.dhSinglePass_stdDH_sha512kdf_scheme, X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme, SecObjectIdentifiers.dhSinglePass_cofactorDH_sha224kdf_scheme, SecObjectIdentifiers.dhSinglePass_cofactorDH_sha256kdf_scheme, SecObjectIdentifiers.dhSinglePass_cofactorDH_sha384kdf_scheme, SecObjectIdentifiers.dhSinglePass_cofactorDH_sha512kdf_scheme); AddKpgAlgorithm("ECDH", "ECIES"); AddKpgAlgorithm("ECDHC", Array.Empty<object>()); AddKpgAlgorithm("ECMQV", X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme, SecObjectIdentifiers.mqvSinglePass_sha224kdf_scheme, SecObjectIdentifiers.mqvSinglePass_sha256kdf_scheme, SecObjectIdentifiers.mqvSinglePass_sha384kdf_scheme, SecObjectIdentifiers.mqvSinglePass_sha512kdf_scheme); AddKpgAlgorithm("ECDSA", Array.Empty<object>()); AddKpgAlgorithm("ECGOST3410", "ECGOST-3410", "GOST-3410-2001"); AddKpgAlgorithm("ECGOST3410-2012", "GOST-3410-2012"); AddKpgAlgorithm("Ed25519", "Ed25519ctx", "Ed25519ph", EdECObjectIdentifiers.id_Ed25519); AddKpgAlgorithm("Ed448", "Ed448ph", EdECObjectIdentifiers.id_Ed448); AddKpgAlgorithm("ELGAMAL", Array.Empty<object>()); AddKpgAlgorithm("GOST3410", "GOST-3410", "GOST-3410-94"); AddKpgAlgorithm("ML-DSA", Array.Empty<object>()); AddKpgAlgorithm("ML-KEM", Array.Empty<object>()); AddKpgAlgorithm("RSA", PkcsObjectIdentifiers.RsaEncryption); AddKpgAlgorithm("RSASSA-PSS", Array.Empty<object>()); AddKpgAlgorithm("SLH-DSA", Array.Empty<object>()); AddKpgAlgorithm("X25519", EdECObjectIdentifiers.id_X25519); AddKpgAlgorithm("X448", EdECObjectIdentifiers.id_X448); AddDefaultKeySizeEntries(64, "DES"); AddDefaultKeySizeEntries(80, "SKIPJACK"); AddDefaultKeySizeEntries(128, "AES128", "ARIA128", "BLOWFISH", "CAMELLIA128", "CAST5", "CHACHA", "DESEDE", "HC128", "HMACMD2", "HMACMD4", "HMACMD5", "HMACRIPEMD128", "IDEA", "NOEKEON", "RC2", "RC4", "RC5", "SALSA20", "SEED", "SM4", "TEA", "XTEA", "VMPC", "VMPC-KSA3"); AddDefaultKeySizeEntries(160, "HMACRIPEMD160", "HMACSHA1"); AddDefaultKeySizeEntries(192, "AES", "AES192", "ARIA192", "CAMELLIA192", "DESEDE3", "HMACTIGER", "RIJNDAEL", "SERPENT", "TNEPRES"); AddDefaultKeySizeEntries(224, "HMACSHA3-224", "HMACKECCAK224", "HMACSHA224", "HMACSHA512/224", "HMACSHA512-224"); AddDefaultKeySizeEntries(256, "AES256", "ARIA", "ARIA256", "CAMELLIA", "CAMELLIA256", "CAST6", "CHACHA7539", "GOST28147", "HC256", "HMACGOST3411-2012-256", "HMACSHA3-256", "HMACKECCAK256", "HMACSHA256", "HMACSHA512/256", "HMACSHA512-256", "RC5-64", "RC6", "THREEFISH-256", "TWOFISH"); AddDefaultKeySizeEntries(288, "HMACKECCAK288"); AddDefaultKeySizeEntries(384, "HMACSHA3-384", "HMACKECCAK384", "HMACSHA384"); AddDefaultKeySizeEntries(512, "HMACGOST3411-2012-512", "HMACSHA3-512", "HMACKECCAK512", "HMACSHA512", "THREEFISH-512"); AddDefaultKeySizeEntries(1024, "THREEFISH-1024"); } private static void AddDefaultKeySizeEntries(int size, params string[] algorithms) { foreach (string key in algorithms) { DefaultKeySizes.Add(key, size); } } private static void AddKgAlgorithm(string canonicalName, params object[] aliases) { KgAlgorithms[canonicalName] = canonicalName; foreach (object obj in aliases) { KgAlgorithms[obj.ToString()] = canonicalName; } } private static void AddKpgAlgorithm(string canonicalName, params object[] aliases) { KpgAlgorithms[canonicalName] = canonicalName; foreach (object obj in aliases) { KpgAlgorithms[obj.ToString()] = canonicalName; } } private static void AddHMacKeyGenerator(string algorithm, params object[] aliases) { string text = "HMAC" + algorithm; KgAlgorithms[text] = text; KgAlgorithms["HMAC-" + algorithm] = text; KgAlgorithms["HMAC/" + algorithm] = text; foreach (object obj in aliases) { KgAlgorithms[obj.ToString()] = text; } } internal static string GetCanonicalKeyGeneratorAlgorithm(string algorithm) { return CollectionUtilities.GetValueOrNull(KgAlgorithms, algorithm); } internal static string GetCanonicalKeyPairGeneratorAlgorithm(string algorithm) { return CollectionUtilities.GetValueOrNull(KpgAlgorithms, algorithm); } public static CipherKeyGenerator GetKeyGenerator(DerObjectIdentifier oid) { return GetKeyGenerator(oid.Id); } public static CipherKeyGenerator GetKeyGenerator(string algorithm) { string canonicalKeyGeneratorAlgorithm = GetCanonicalKeyGeneratorAlgorithm(algorithm); if (canonicalKeyGeneratorAlgorithm == null) throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised."); int num = FindDefaultKeySize(canonicalKeyGeneratorAlgorithm); if (num == -1) throw new SecurityUtilityException("KeyGenerator " + algorithm + " (" + canonicalKeyGeneratorAlgorithm + ") not supported."); if (canonicalKeyGeneratorAlgorithm == "DES") return new DesKeyGenerator(num); if (canonicalKeyGeneratorAlgorithm == "DESEDE" || canonicalKeyGeneratorAlgorithm == "DESEDE3") return new DesEdeKeyGenerator(num); return new CipherKeyGenerator(num); } public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(DerObjectIdentifier oid) { return GetKeyPairGenerator(oid.Id); } public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(string algorithm) { string canonicalKeyPairGeneratorAlgorithm = GetCanonicalKeyPairGeneratorAlgorithm(algorithm); if (canonicalKeyPairGeneratorAlgorithm == null) throw new SecurityUtilityException("KeyPairGenerator " + algorithm + " not recognised."); if (canonicalKeyPairGeneratorAlgorithm == "DH") return new DHKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "DSA") return new DsaKeyPairGenerator(); if (Platform.StartsWith(canonicalKeyPairGeneratorAlgorithm, "EC")) return new ECKeyPairGenerator(canonicalKeyPairGeneratorAlgorithm); if (canonicalKeyPairGeneratorAlgorithm == "Ed25519") return new Ed25519KeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "Ed448") return new Ed448KeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "ELGAMAL") return new ElGamalKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "GOST3410") return new Gost3410KeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "ML-DSA") return new MLDsaKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "ML-KEM") return new MLKemKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "RSA" || canonicalKeyPairGeneratorAlgorithm == "RSASSA-PSS") return new RsaKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "SLH-DSA") return new SlhDsaKeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "X25519") return new X25519KeyPairGenerator(); if (canonicalKeyPairGeneratorAlgorithm == "X448") return new X448KeyPairGenerator(); throw new SecurityUtilityException("KeyPairGenerator " + algorithm + " (" + canonicalKeyPairGeneratorAlgorithm + ") not supported."); } internal static int GetDefaultKeySize(DerObjectIdentifier oid) { return GetDefaultKeySize(oid.Id); } internal static int GetDefaultKeySize(string algorithm) { string canonicalKeyGeneratorAlgorithm = GetCanonicalKeyGeneratorAlgorithm(algorithm); if (canonicalKeyGeneratorAlgorithm == null) throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised."); int num = FindDefaultKeySize(canonicalKeyGeneratorAlgorithm); if (num == -1) throw new SecurityUtilityException("KeyGenerator " + algorithm + " (" + canonicalKeyGeneratorAlgorithm + ") not supported."); return num; } private static int FindDefaultKeySize(string canonicalName) { if (!DefaultKeySizes.TryGetValue(canonicalName, out int value)) return -1; return value; } } }