diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-24 20:37:41 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-24 20:37:41 +0700 |
commit | 110f936da13a1639a83edc512c5c104d2f5694a7 (patch) | |
tree | 6edf3f33b1b173348ccf2dfa639fe03979d336c3 | |
parent | Remove bridging version of Curve25519 (diff) | |
download | BouncyCastle.NET-ed25519-110f936da13a1639a83edc512c5c104d2f5694a7.tar.xz |
Update EC curve registry classes
-rw-r--r-- | crypto/src/asn1/anssi/ANSSINamedCurves.cs | 86 | ||||
-rw-r--r-- | crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs | 130 | ||||
-rw-r--r-- | crypto/src/asn1/gm/GMNamedCurves.cs | 94 | ||||
-rw-r--r-- | crypto/src/asn1/nist/NISTNamedCurves.cs | 93 | ||||
-rw-r--r-- | crypto/src/asn1/sec/SECNamedCurves.cs | 188 | ||||
-rw-r--r-- | crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs | 97 | ||||
-rw-r--r-- | crypto/src/asn1/x9/ECNamedCurveTable.cs | 194 | ||||
-rw-r--r-- | crypto/src/asn1/x9/X962NamedCurves.cs | 94 | ||||
-rw-r--r-- | crypto/src/crypto/ec/CustomNamedCurves.cs | 197 | ||||
-rw-r--r-- | crypto/src/security/PrivateKeyFactory.cs | 4 | ||||
-rw-r--r-- | crypto/src/security/PublicKeyFactory.cs | 4 | ||||
-rw-r--r-- | crypto/src/util/collections/CollectionUtilities.cs | 11 | ||||
-rw-r--r-- | crypto/src/util/collections/EnumerableProxy.cs | 25 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/ECGOST3410_2012Test.cs | 8 | ||||
-rw-r--r-- | crypto/test/src/math/ec/test/ECPointPerformanceTest.cs | 18 | ||||
-rw-r--r-- | crypto/test/src/math/ec/test/ECPointTest.cs | 9 |
16 files changed, 517 insertions, 735 deletions
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 + /// <summary>Elliptic curve registry for ANSSI.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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 { - /// <summary> - /// Table of the available named parameters for GOST 3410-2001 / 2012. - /// </summary> - public sealed class ECGost3410NamedCurves + /// <summary>Elliptic curve registry for GOST 3410-2001 / 2012.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); 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) + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : GetByOidX9(oid); + return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - public static X9ECParameters GetByOidX9(DerObjectIdentifier oid) + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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 + /// <summary>Elliptic curve registry for GM.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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 + /// <summary>Elliptic curve registry for NIST curves.</summary> + public static class NistNamedCurves { - private NistNamedCurves() - { - } + private static readonly Dictionary<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : SecNamedCurves.GetByOid(oid); + return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - return names.Contains(oid) ? SecNamedCurves.GetByOid(oid) : null; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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 + /// <summary>Elliptic curve registry for the SEC standard.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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 + /// <summary>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 .</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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 + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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) + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> 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. - */ + /// <summary>A unified elliptic curve registry of the various standard-specific registries.</summary> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> 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; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> public static X9ECParametersHolder GetByNameLazy(string name) { X9ECParametersHolder holder = X962NamedCurves.GetByNameLazy(name); @@ -84,6 +81,76 @@ namespace Org.BouncyCastle.Asn1.X9 return holder; } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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; + } + + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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; + } + + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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. - */ + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> 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 + /// <summary>Enumerate the available curve names in all the registries.</summary> + public static IEnumerable<string> 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<string>(); + 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 + /// <summary>Elliptic curve registry for the curves defined in X.962 EC-DSA.</summary> + 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<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); - 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); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> 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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> 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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + 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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + 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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs index bf2b7f486..b1b97551c 100644 --- a/crypto/src/crypto/ec/CustomNamedCurves.cs +++ b/crypto/src/crypto/ec/CustomNamedCurves.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.GM; @@ -11,18 +11,14 @@ using Org.BouncyCastle.Math.EC.Custom.GM; using Org.BouncyCastle.Math.EC.Custom.Sec; 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.Crypto.EC { - public sealed class CustomNamedCurves + /// <summary>Elliptic curve registry for various customized curve implementations.</summary> + public static class CustomNamedCurves { - private CustomNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -40,9 +36,6 @@ namespace Org.BouncyCastle.Crypto.EC return c.Configure().SetEndomorphism(new GlvTypeBEndomorphism(c, p)).Create(); } - /* - * secp128r1 - */ internal class SecP128R1Holder : X9ECParametersHolder { @@ -65,9 +58,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160k1 - */ internal class SecP160K1Holder : X9ECParametersHolder { @@ -103,9 +93,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160r1 - */ internal class SecP160R1Holder : X9ECParametersHolder { @@ -128,9 +115,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160r2 - */ internal class SecP160R2Holder : X9ECParametersHolder { @@ -153,9 +137,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp192k1 - */ internal class SecP192K1Holder : X9ECParametersHolder { @@ -191,9 +172,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp192r1 - */ internal class SecP192R1Holder : X9ECParametersHolder { @@ -216,9 +194,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp224k1 - */ internal class SecP224K1Holder : X9ECParametersHolder { @@ -254,9 +229,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp224r1 - */ internal class SecP224R1Holder : X9ECParametersHolder { @@ -279,9 +251,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp256k1 - */ internal class SecP256K1Holder : X9ECParametersHolder { @@ -317,9 +286,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp256r1 - */ internal class SecP256R1Holder : X9ECParametersHolder { @@ -342,9 +308,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp384r1 - */ internal class SecP384R1Holder : X9ECParametersHolder { @@ -368,9 +331,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp521r1 - */ internal class SecP521R1Holder : X9ECParametersHolder { @@ -394,9 +354,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * sect113r1 - */ internal class SecT113R1Holder : X9ECParametersHolder { @@ -419,9 +376,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect113r2 - */ internal class SecT113R2Holder : X9ECParametersHolder { @@ -444,9 +398,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect131r1 - */ internal class SecT131R1Holder : X9ECParametersHolder { @@ -469,9 +420,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect131r2 - */ internal class SecT131R2Holder : X9ECParametersHolder { @@ -494,9 +442,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163k1 - */ internal class SecT163K1Holder : X9ECParametersHolder { @@ -519,9 +464,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163r1 - */ internal class SecT163R1Holder : X9ECParametersHolder { @@ -544,9 +486,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163r2 - */ internal class SecT163R2Holder : X9ECParametersHolder { @@ -569,9 +508,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect193r1 - */ internal class SecT193R1Holder : X9ECParametersHolder { @@ -594,9 +530,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect193r2 - */ internal class SecT193R2Holder : X9ECParametersHolder { @@ -619,9 +552,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect233k1 - */ internal class SecT233K1Holder : X9ECParametersHolder { @@ -644,9 +574,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect233r1 - */ internal class SecT233R1Holder : X9ECParametersHolder { @@ -669,9 +596,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect239k1 - */ internal class SecT239K1Holder : X9ECParametersHolder { @@ -694,9 +618,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect283k1 - */ internal class SecT283K1Holder : X9ECParametersHolder { @@ -720,9 +641,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect283r1 - */ internal class SecT283R1Holder : X9ECParametersHolder { @@ -746,9 +664,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect409k1 - */ internal class SecT409K1Holder : X9ECParametersHolder { @@ -772,9 +687,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect409r1 - */ internal class SecT409R1Holder : X9ECParametersHolder { @@ -798,9 +710,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect571k1 - */ internal class SecT571K1Holder : X9ECParametersHolder { @@ -824,9 +733,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect571r1 - */ internal class SecT571R1Holder : X9ECParametersHolder { @@ -850,9 +756,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sm2p256v1 - */ internal class SM2P256V1Holder : X9ECParametersHolder { @@ -875,32 +778,26 @@ namespace Org.BouncyCastle.Crypto.EC } } - - private static readonly IDictionary nameToCurve = Platform.CreateHashtable(); - private static readonly IDictionary nameToOid = Platform.CreateHashtable(); - private static readonly IDictionary oidToCurve = Platform.CreateHashtable(); - private static readonly IDictionary oidToName = Platform.CreateHashtable(); - private static readonly IList names = Platform.CreateArrayList(); + private static readonly Dictionary<string, DerObjectIdentifier> objIds = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves = + new Dictionary<DerObjectIdentifier, X9ECParametersHolder>(); + private static readonly Dictionary<DerObjectIdentifier, string> names = + new Dictionary<DerObjectIdentifier, string>(); private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - names.Add(name); - oidToName.Add(oid, name); - oidToCurve.Add(oid, holder); - name = Platform.ToUpperInvariant(name); - nameToOid.Add(name, oid); - nameToCurve.Add(name, holder); + objIds.Add(name, oid); + names.Add(oid, name); + curves.Add(oid, holder); } private static void DefineCurveAlias(string name, DerObjectIdentifier oid) { - object curve = oidToCurve[oid]; - if (curve == null) + if (!curves.ContainsKey(oid)) throw new InvalidOperationException(); - name = Platform.ToUpperInvariant(name); - nameToOid.Add(name, oid); - nameToCurve.Add(name, curve); + objIds.Add(name, oid); } static CustomNamedCurves() @@ -961,60 +858,64 @@ namespace Org.BouncyCastle.Crypto.EC DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1); } + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> public static X9ECParameters GetByName(string name) { - X9ECParametersHolder holder = GetByNameLazy(name); - return holder == null ? null : holder.Parameters; + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOid(oid); } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="name">The name of the curve.</param> public static X9ECParametersHolder GetByNameLazy(string name) { - return (X9ECParametersHolder)nameToCurve[Platform.ToUpperInvariant(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. - */ + /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given + /// <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <remarks> + /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the + /// full <see cref="X9ECParameters"/>. + /// </remarks> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)oidToCurve[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) + /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary> + /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param> + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)nameToOid[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) + /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary> + /// <param name="name">The name of the curve.</param> + public static DerObjectIdentifier GetOid(string name) { - return (string)oidToName[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 + /// <summary>Enumerate the available curve names in this registry.</summary> + public static IEnumerable<string> Names { - get { return new EnumerableProxy(names); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index a8c4d94d0..93f8a2260 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -135,7 +135,7 @@ namespace Org.BouncyCastle.Security Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance( algID.Parameters.ToAsn1Object()); - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet); if (ecP == null) throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key"); @@ -198,7 +198,7 @@ namespace Org.BouncyCastle.Security if (p is Asn1Sequence && (Asn1Sequence.GetInstance(p).Count == 2 || Asn1Sequence.GetInstance(p).Count == 3)) { - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet); ecSpec = new ECGost3410Parameters( new ECNamedDomainParameters( diff --git a/crypto/src/security/PublicKeyFactory.cs b/crypto/src/security/PublicKeyFactory.cs index 10b2aacdc..65baf003c 100644 --- a/crypto/src/security/PublicKeyFactory.cs +++ b/crypto/src/security/PublicKeyFactory.cs @@ -162,7 +162,7 @@ namespace Org.BouncyCastle.Security Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters); DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet; - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(publicKeyParamSet); if (ecP == null) return null; @@ -238,7 +238,7 @@ namespace Org.BouncyCastle.Security DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet; ECGost3410Parameters ecDomainParameters =new ECGost3410Parameters( - new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet)), + new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOid(publicKeyParamSet)), publicKeyParamSet, gostParams.DigestParamSet, gostParams.EncryptionParamSet); diff --git a/crypto/src/util/collections/CollectionUtilities.cs b/crypto/src/util/collections/CollectionUtilities.cs index 35ee60e41..37551e5b3 100644 --- a/crypto/src/util/collections/CollectionUtilities.cs +++ b/crypto/src/util/collections/CollectionUtilities.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Text; namespace Org.BouncyCastle.Utilities.Collections @@ -14,6 +15,16 @@ namespace Org.BouncyCastle.Utilities.Collections } } + public static IEnumerable Proxy(IEnumerable e) + { + return new EnumerableProxy(e); + } + + public static IEnumerable<T> Proxy<T>(IEnumerable<T> e) + { + return new EnumerableProxy<T>(e); + } + public static IDictionary ReadOnly(IDictionary d) { return new UnmodifiableDictionaryProxy(d); diff --git a/crypto/src/util/collections/EnumerableProxy.cs b/crypto/src/util/collections/EnumerableProxy.cs index 9eec4af21..196b4d9df 100644 --- a/crypto/src/util/collections/EnumerableProxy.cs +++ b/crypto/src/util/collections/EnumerableProxy.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; namespace Org.BouncyCastle.Utilities.Collections { @@ -22,4 +23,28 @@ namespace Org.BouncyCastle.Utilities.Collections return inner.GetEnumerator(); } } + + internal sealed class EnumerableProxy<T> + : IEnumerable<T> + { + private readonly IEnumerable<T> m_inner; + + internal EnumerableProxy(IEnumerable<T> inner) + { + if (inner == null) + throw new ArgumentNullException("inner"); + + m_inner = inner; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return m_inner.GetEnumerator(); + } + + IEnumerator<T> IEnumerable<T>.GetEnumerator() + { + return m_inner.GetEnumerator(); + } + } } diff --git a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs index 0e7e90611..7e0a84cbe 100644 --- a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs +++ b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs @@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeRecodePublicKey() { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); ECKeyGenerationParameters paramameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Crypto.Tests private SimpleTestResult EncodeRecodePrivateKey() { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -338,7 +338,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeDecodePrivateLW(string oidStr, DerObjectIdentifier digest) { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -421,7 +421,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeDecodePublicLW(string oidStr, DerObjectIdentifier digest) { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); diff --git a/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs b/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs index e36947b11..059416427 100644 --- a/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs +++ b/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs @@ -1,17 +1,13 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Text; using NUnit.Framework; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.EC; -using Org.BouncyCastle.Math; -using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Date; @@ -98,6 +94,8 @@ namespace Org.BouncyCastle.Math.EC.Tests Console.WriteLine(sb.ToString()); } } + + Console.Out.Flush(); } private double RandMult(SecureRandom random, ECPoint g, BigInteger n) @@ -175,12 +173,12 @@ namespace Org.BouncyCastle.Math.EC.Tests [Test, Explicit] public void TestMultiply() { - ArrayList nameList = new ArrayList(); - CollectionUtilities.AddRange(nameList, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(nameList, CustomNamedCurves.Names); + var names = new List<string>(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); + + names.Sort(); - string[] names = (string[])nameList.ToArray(typeof(string)); - Array.Sort(names); ISet oids = new HashSet(); foreach (string name in names) { diff --git a/crypto/test/src/math/ec/test/ECPointTest.cs b/crypto/test/src/math/ec/test/ECPointTest.cs index f3fc6e592..4e3fc5832 100644 --- a/crypto/test/src/math/ec/test/ECPointTest.cs +++ b/crypto/test/src/math/ec/test/ECPointTest.cs @@ -1,11 +1,10 @@ using System; -using System.Collections; +using System.Collections.Generic; using NUnit.Framework; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.EC; -using Org.BouncyCastle.Math.EC.Multiplier; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; @@ -535,9 +534,9 @@ namespace Org.BouncyCastle.Math.EC.Tests [Test] public void TestAddSubtractMultiplyTwiceEncoding() { - ArrayList names = new ArrayList(); - CollectionUtilities.AddRange(names, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(names, CustomNamedCurves.Names); + var names = new List<string>(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); ISet uniqNames = new HashSet(names); |