diff options
Diffstat (limited to 'crypto/src/asn1/gm/GMNamedCurves.cs')
-rw-r--r-- | crypto/src/asn1/gm/GMNamedCurves.cs | 94 |
1 files changed, 42 insertions, 52 deletions
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); } } } } |