From 110f936da13a1639a83edc512c5c104d2f5694a7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 20:37:41 +0700 Subject: Update EC curve registry classes --- crypto/src/asn1/anssi/ANSSINamedCurves.cs | 86 +++++---- crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs | 130 +++++++------- crypto/src/asn1/gm/GMNamedCurves.cs | 94 +++++----- crypto/src/asn1/nist/NISTNamedCurves.cs | 93 +++++----- crypto/src/asn1/sec/SECNamedCurves.cs | 188 +++++--------------- crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs | 97 +++++------ crypto/src/asn1/x9/ECNamedCurveTable.cs | 194 ++++++++++----------- crypto/src/asn1/x9/X962NamedCurves.cs | 94 +++++----- 8 files changed, 412 insertions(+), 564 deletions(-) (limited to 'crypto/src/asn1') diff --git a/crypto/src/asn1/anssi/ANSSINamedCurves.cs b/crypto/src/asn1/anssi/ANSSINamedCurves.cs index 3a9e4dd03..ed1faa75c 100644 --- a/crypto/src/asn1/anssi/ANSSINamedCurves.cs +++ b/crypto/src/asn1/anssi/ANSSINamedCurves.cs @@ -1,17 +1,17 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.Anssi { - public class AnssiNamedCurves + /// Elliptic curve registry for ANSSI. + public static class AnssiNamedCurves { private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { @@ -30,9 +30,6 @@ namespace Org.BouncyCastle.Asn1.Anssi return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * FRP256v1 - */ internal class Frp256v1Holder : X9ECParametersHolder { @@ -63,16 +60,16 @@ namespace Org.BouncyCastle.Asn1.Anssi } } - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -82,63 +79,64 @@ namespace Org.BouncyCastle.Asn1.Anssi DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs index 5ac8cadfe..ec297f7a1 100644 --- a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs +++ b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs @@ -1,27 +1,19 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.CryptoPro { - /// - /// Table of the available named parameters for GOST 3410-2001 / 2012. - /// - public sealed class ECGost3410NamedCurves + /// Elliptic curve registry for GOST 3410-2001 / 2012. + public static class ECGost3410NamedCurves { - private ECGost3410NamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, BigInteger x, BigInteger y) { ECPoint G = curve.CreatePoint(x, y); @@ -39,9 +31,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * GostR3410-2001-CryptoPro-A (and GostR3410-2001-CryptoPro-XchA) - */ internal class Holder_gostR3410_2001_CryptoPro_A : X9ECParametersHolder { @@ -74,9 +63,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-B - */ internal class Holder_gostR3410_2001_CryptoPro_B : X9ECParametersHolder { @@ -109,9 +95,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-C - */ internal class Holder_gostR3410_2001_CryptoPro_C : X9ECParametersHolder { @@ -144,9 +127,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-XchB - */ internal class Holder_gostR3410_2001_CryptoPro_XchB : X9ECParametersHolder { @@ -179,9 +159,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-256-paramSetA - */ internal class Holder_id_tc26_gost_3410_12_256_paramSetA : X9ECParametersHolder { @@ -214,9 +191,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetA - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetA : X9ECParametersHolder { @@ -249,9 +223,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetB - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetB : X9ECParametersHolder { @@ -284,9 +255,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetC - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetC : X9ECParametersHolder { @@ -319,10 +287,12 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { @@ -333,71 +303,93 @@ namespace Org.BouncyCastle.Asn1.CryptoPro static ECGost3410NamedCurves() { - DefineCurve("GostR3410-2001-CryptoPro-A", CryptoProObjectIdentifiers.GostR3410x2001CryptoProA, + DefineCurve("GostR3410-2001-CryptoPro-A", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProA, Holder_gostR3410_2001_CryptoPro_A.Instance); - DefineCurve("GostR3410-2001-CryptoPro-B", CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, + DefineCurve("GostR3410-2001-CryptoPro-B", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, Holder_gostR3410_2001_CryptoPro_B.Instance); - DefineCurve("GostR3410-2001-CryptoPro-C", CryptoProObjectIdentifiers.GostR3410x2001CryptoProC, + DefineCurve("GostR3410-2001-CryptoPro-C", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProC, Holder_gostR3410_2001_CryptoPro_C.Instance); - DefineCurve("GostR3410-2001-CryptoPro-XchA", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA, + DefineCurve("GostR3410-2001-CryptoPro-XchA", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA, Holder_gostR3410_2001_CryptoPro_A.Instance); - DefineCurve("GostR3410-2001-CryptoPro-XchB", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB, + DefineCurve("GostR3410-2001-CryptoPro-XchB", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB, Holder_gostR3410_2001_CryptoPro_XchB.Instance); - DefineCurve("Tc26-Gost-3410-12-256-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA, + DefineCurve("Tc26-Gost-3410-12-256-paramSetA", + RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA, Holder_id_tc26_gost_3410_12_256_paramSetA.Instance); - DefineCurve("Tc26-Gost-3410-12-512-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA, + DefineCurve("Tc26-Gost-3410-12-512-paramSetA", + RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA, Holder_id_tc26_gost_3410_12_512_paramSetA.Instance); - DefineCurve("Tc26-Gost-3410-12-512-paramSetB", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB, + DefineCurve("Tc26-Gost-3410-12-512-paramSetB", + RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB, Holder_id_tc26_gost_3410_12_512_paramSetB.Instance); - DefineCurve("Tc26-Gost-3410-12-512-paramSetC", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC, + DefineCurve("Tc26-Gost-3410-12-512-paramSetC", + RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC, Holder_id_tc26_gost_3410_12_512_paramSetC.Instance); } - public static X9ECParameters GetByNameX9(string name) + /// Look up the for the curve with the given name. + /// The name of the curve. + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : GetByOidX9(oid); + return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - public static X9ECParameters GetByOidX9(DerObjectIdentifier oid) + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[name]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/gm/GMNamedCurves.cs b/crypto/src/asn1/gm/GMNamedCurves.cs index f906a2b95..fec0c1401 100644 --- a/crypto/src/asn1/gm/GMNamedCurves.cs +++ b/crypto/src/asn1/gm/GMNamedCurves.cs @@ -1,22 +1,18 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.GM { - public sealed class GMNamedCurves + /// Elliptic curve registry for GM. + public static class GMNamedCurves { - private GMNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -34,9 +30,6 @@ namespace Org.BouncyCastle.Asn1.GM return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * sm2p256v1 - */ internal class SM2P256V1Holder : X9ECParametersHolder { @@ -67,9 +60,6 @@ namespace Org.BouncyCastle.Asn1.GM } } - /* - * wapip192v1 - */ internal class WapiP192V1Holder : X9ECParametersHolder { @@ -100,17 +90,16 @@ namespace Org.BouncyCastle.Asn1.GM } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -121,63 +110,64 @@ namespace Org.BouncyCastle.Asn1.GM DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/nist/NISTNamedCurves.cs b/crypto/src/asn1/nist/NISTNamedCurves.cs index ee256cc2b..a8bc56549 100644 --- a/crypto/src/asn1/nist/NISTNamedCurves.cs +++ b/crypto/src/asn1/nist/NISTNamedCurves.cs @@ -1,31 +1,26 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.Nist { - /** - * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-3 - */ - public sealed class NistNamedCurves + /// Elliptic curve registry for NIST curves. + public static class NistNamedCurves { - private NistNamedCurves() - { - } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurveAlias( - string name, - DerObjectIdentifier oid) + private static void DefineCurveAlias(string name, DerObjectIdentifier oid) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + if (SecNamedCurves.GetByOidLazy(oid) == null) + throw new InvalidOperationException(); + + objIds.Add(name, oid); names.Add(oid, name); } @@ -50,62 +45,64 @@ namespace Org.BouncyCastle.Asn1.Nist DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : SecNamedCurves.GetByOid(oid); + return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : SecNamedCurves.GetByOidLazy(oid); + return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - return names.Contains(oid) ? SecNamedCurves.GetByOid(oid) : null; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return names.Contains(oid) ? SecNamedCurves.GetByOidLazy(oid) : null; + return names.ContainsKey(oid) ? SecNamedCurves.GetByOidLazy(oid) : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier) objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string) names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/sec/SECNamedCurves.cs b/crypto/src/asn1/sec/SECNamedCurves.cs index ad2f3e333..c0a783ec6 100644 --- a/crypto/src/asn1/sec/SECNamedCurves.cs +++ b/crypto/src/asn1/sec/SECNamedCurves.cs @@ -1,24 +1,19 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Endo; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.Sec { - public sealed class SecNamedCurves + /// Elliptic curve registry for the SEC standard. + public static class SecNamedCurves { - private SecNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -41,9 +36,6 @@ namespace Org.BouncyCastle.Asn1.Sec return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * secp112r1 - */ internal class Secp112r1Holder : X9ECParametersHolder { @@ -75,9 +67,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp112r2 - */ internal class Secp112r2Holder : X9ECParametersHolder { @@ -109,9 +98,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r1 - */ internal class Secp128r1Holder : X9ECParametersHolder { @@ -143,9 +129,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r2 - */ internal class Secp128r2Holder : X9ECParametersHolder { @@ -177,9 +160,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160k1 - */ internal class Secp160k1Holder : X9ECParametersHolder { @@ -225,9 +205,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r1 - */ internal class Secp160r1Holder : X9ECParametersHolder { @@ -259,9 +236,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r2 - */ internal class Secp160r2Holder : X9ECParametersHolder { @@ -293,9 +267,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192k1 - */ internal class Secp192k1Holder : X9ECParametersHolder { @@ -341,9 +312,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192r1 - */ internal class Secp192r1Holder : X9ECParametersHolder { @@ -375,9 +343,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224k1 - */ internal class Secp224k1Holder : X9ECParametersHolder { @@ -423,9 +388,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224r1 - */ internal class Secp224r1Holder : X9ECParametersHolder { @@ -457,9 +419,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256k1 - */ internal class Secp256k1Holder : X9ECParametersHolder { @@ -505,9 +464,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256r1 - */ internal class Secp256r1Holder : X9ECParametersHolder { @@ -539,9 +495,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp384r1 - */ internal class Secp384r1Holder : X9ECParametersHolder { @@ -574,9 +527,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp521r1 - */ internal class Secp521r1Holder : X9ECParametersHolder { @@ -609,9 +559,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r1 - */ internal class Sect113r1Holder : X9ECParametersHolder { @@ -644,9 +591,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r2 - */ internal class Sect113r2Holder : X9ECParametersHolder { @@ -679,9 +623,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r1 - */ internal class Sect131r1Holder : X9ECParametersHolder { @@ -716,9 +657,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r2 - */ internal class Sect131r2Holder : X9ECParametersHolder { @@ -753,9 +691,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163k1 - */ internal class Sect163k1Holder : X9ECParametersHolder { @@ -790,9 +725,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r1 - */ internal class Sect163r1Holder : X9ECParametersHolder { @@ -827,9 +759,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r2 - */ internal class Sect163r2Holder : X9ECParametersHolder { @@ -864,9 +793,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r1 - */ internal class Sect193r1Holder : X9ECParametersHolder { @@ -899,9 +825,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r2 - */ internal class Sect193r2Holder : X9ECParametersHolder { @@ -934,9 +857,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233k1 - */ internal class Sect233k1Holder : X9ECParametersHolder { @@ -969,9 +889,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233r1 - */ internal class Sect233r1Holder : X9ECParametersHolder { @@ -1004,9 +921,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect239k1 - */ internal class Sect239k1Holder : X9ECParametersHolder { @@ -1039,9 +953,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283k1 - */ internal class Sect283k1Holder : X9ECParametersHolder { @@ -1077,9 +988,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283r1 - */ internal class Sect283r1Holder : X9ECParametersHolder { @@ -1115,9 +1023,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409k1 - */ internal class Sect409k1Holder : X9ECParametersHolder { @@ -1151,9 +1056,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409r1 - */ internal class Sect409r1Holder : X9ECParametersHolder { @@ -1187,9 +1089,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571k1 - */ internal class Sect571k1Holder : X9ECParametersHolder { @@ -1225,9 +1124,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571r1 - */ internal class Sect571r1Holder : X9ECParametersHolder { @@ -1263,17 +1159,16 @@ namespace Org.BouncyCastle.Asn1.Sec } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -1316,63 +1211,64 @@ namespace Org.BouncyCastle.Asn1.Sec DefineCurve("sect571r1", SecObjectIdentifiers.SecT571r1, Sect571r1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs index dcc160016..b863babce 100644 --- a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs +++ b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs @@ -1,20 +1,18 @@ -using System.Collections; +using System; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.TeleTrust { - /** - * elliptic curves defined in "ECC Brainpool Standard Curves and Curve Generation" - * http://www.ecc-brainpool.org/download/draft_pkix_additional_ecc_dp.txt - */ - public class TeleTrusTNamedCurves + /// Elliptic curve registry for curves defined in "ECC Brainpool Standard Curves and Curve Generation" + /// http://www.ecc-brainpool.org/download/draft_pkix_additional_ecc_dp.txt . + public static class TeleTrusTNamedCurves { private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { @@ -460,17 +458,16 @@ namespace Org.BouncyCastle.Asn1.TeleTrust } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -493,70 +490,64 @@ namespace Org.BouncyCastle.Asn1.TeleTrust DefineCurve("brainpoolP512t1", TeleTrusTObjectIdentifiers.BrainpoolP512T1, BrainpoolP512t1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; - } - - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) - { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (string)names[oid]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - get { return new EnumerableProxy(names.Values); } + return objIds.TryGetValue(name, out var oid) ? oid : null; } - public static DerObjectIdentifier GetOid( - short curvesize, - bool twisted) + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - return GetOid("brainpoolP" + curvesize + (twisted ? "t" : "r") + "1"); + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/x9/ECNamedCurveTable.cs b/crypto/src/asn1/x9/ECNamedCurveTable.cs index f0d70272b..9243c341e 100644 --- a/crypto/src/asn1/x9/ECNamedCurveTable.cs +++ b/crypto/src/asn1/x9/ECNamedCurveTable.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.Anssi; using Org.BouncyCastle.Asn1.CryptoPro; @@ -7,23 +7,14 @@ using Org.BouncyCastle.Asn1.GM; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.TeleTrust; -using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.X9 { - /** - * A general class that reads all X9.62 style EC curve tables. - */ + /// A unified elliptic curve registry of the various standard-specific registries. public class ECNamedCurveTable { - /** - * return a X9ECParameters object representing the passed in named - * curve. The routine returns null if the curve is not present. - * - * @param name the name of the curve requested - * @return an X9ECParameters object or null if the curve is not available. - */ + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { X9ECParameters ecP = X962NamedCurves.GetByName(name); @@ -45,7 +36,7 @@ namespace Org.BouncyCastle.Asn1.X9 } if (ecP == null) { - ecP = ECGost3410NamedCurves.GetByNameX9(name); + ecP = ECGost3410NamedCurves.GetByName(name); } if (ecP == null) { @@ -54,6 +45,12 @@ namespace Org.BouncyCastle.Asn1.X9 return ecP; } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { X9ECParametersHolder holder = X962NamedCurves.GetByNameLazy(name); @@ -84,6 +81,76 @@ namespace Org.BouncyCastle.Asn1.X9 return holder; } + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. + public static X9ECParameters GetByOid(DerObjectIdentifier oid) + { + X9ECParameters ecP = X962NamedCurves.GetByOid(oid); + if (ecP == null) + { + ecP = SecNamedCurves.GetByOid(oid); + } + + // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup + + if (ecP == null) + { + ecP = TeleTrusTNamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = AnssiNamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = ECGost3410NamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = GMNamedCurves.GetByOid(oid); + } + return ecP; + } + + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + X9ECParametersHolder holder = X962NamedCurves.GetByOidLazy(oid); + if (null == holder) + { + holder = SecNamedCurves.GetByOidLazy(oid); + } + + // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup + + if (null == holder) + { + holder = TeleTrusTNamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = AnssiNamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = ECGost3410NamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = GMNamedCurves.GetByOidLazy(oid); + } + return holder; + } + + /// Look up the name of the curve with the given OID. + /// The OID for the curve. public static string GetName(DerObjectIdentifier oid) { string name = X962NamedCurves.GetName(oid); @@ -114,12 +181,8 @@ namespace Org.BouncyCastle.Asn1.X9 return name; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ + /// Look up the OID of the curve with the given name. + /// The name of the curve. public static DerObjectIdentifier GetOid(string name) { DerObjectIdentifier oid = X962NamedCurves.GetOid(name); @@ -150,89 +213,20 @@ namespace Org.BouncyCastle.Asn1.X9 return oid; } - /** - * return a X9ECParameters object representing the passed in named - * curve. - * - * @param oid the object id of the curve requested - * @return an X9ECParameters object or null if the curve is not available. - */ - public static X9ECParameters GetByOid(DerObjectIdentifier oid) - { - X9ECParameters ecP = X962NamedCurves.GetByOid(oid); - if (ecP == null) - { - ecP = SecNamedCurves.GetByOid(oid); - } - - // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup - - if (ecP == null) - { - ecP = TeleTrusTNamedCurves.GetByOid(oid); - } - if (ecP == null) - { - ecP = AnssiNamedCurves.GetByOid(oid); - } - if (ecP == null) - { - ecP = ECGost3410NamedCurves.GetByOidX9(oid); - } - if (ecP == null) - { - ecP = GMNamedCurves.GetByOid(oid); - } - return ecP; - } - - public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) - { - X9ECParametersHolder holder = X962NamedCurves.GetByOidLazy(oid); - if (null == holder) - { - holder = SecNamedCurves.GetByOidLazy(oid); - } - - // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup - - if (null == holder) - { - holder = TeleTrusTNamedCurves.GetByOidLazy(oid); - } - if (null == holder) - { - holder = AnssiNamedCurves.GetByOidLazy(oid); - } - if (null == holder) - { - holder = ECGost3410NamedCurves.GetByOidLazy(oid); - } - if (null == holder) - { - holder = GMNamedCurves.GetByOidLazy(oid); - } - return holder; - } - - /** - * return an enumeration of the names of the available curves. - * - * @return an enumeration of the names of the available curves. - */ - public static IEnumerable Names + /// Enumerate the available curve names in all the registries. + public static IEnumerable Names { get { - IList v = Platform.CreateArrayList(); - CollectionUtilities.AddRange(v, X962NamedCurves.Names); - CollectionUtilities.AddRange(v, SecNamedCurves.Names); - CollectionUtilities.AddRange(v, NistNamedCurves.Names); - CollectionUtilities.AddRange(v, TeleTrusTNamedCurves.Names); - CollectionUtilities.AddRange(v, AnssiNamedCurves.Names); - CollectionUtilities.AddRange(v, ECGost3410NamedCurves.Names); - CollectionUtilities.AddRange(v, GMNamedCurves.Names); - return v; + var result = new List(); + result.AddRange(X962NamedCurves.Names); + result.AddRange(SecNamedCurves.Names); + result.AddRange(NistNamedCurves.Names); + result.AddRange(TeleTrusTNamedCurves.Names); + result.AddRange(AnssiNamedCurves.Names); + result.AddRange(ECGost3410NamedCurves.Names); + result.AddRange(GMNamedCurves.Names); + return result; } } } diff --git a/crypto/src/asn1/x9/X962NamedCurves.cs b/crypto/src/asn1/x9/X962NamedCurves.cs index 4fd3c8d1e..e0fb625f9 100644 --- a/crypto/src/asn1/x9/X962NamedCurves.cs +++ b/crypto/src/asn1/x9/X962NamedCurves.cs @@ -1,24 +1,17 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.X9 { - /** - * table of the current named curves defined in X.962 EC-DSA. - */ - public sealed class X962NamedCurves + /// Elliptic curve registry for the curves defined in X.962 EC-DSA. + public static class X962NamedCurves { - private X962NamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -253,9 +246,6 @@ namespace Org.BouncyCastle.Asn1.X9 } } - /* - * F2m Curves - */ internal class C2pnb163v1Holder : X9ECParametersHolder { @@ -768,17 +758,16 @@ namespace Org.BouncyCastle.Asn1.X9 } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -810,63 +799,64 @@ namespace Org.BouncyCastle.Asn1.X9 DefineCurve("c2tnb431r1", X9ObjectIdentifiers.C2Tnb431r1, C2tnb431r1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } -- cgit 1.4.1