summary refs log tree commit diff
path: root/crypto/src/asn1/anssi
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-24 20:37:41 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-24 20:37:41 +0700
commit110f936da13a1639a83edc512c5c104d2f5694a7 (patch)
tree6edf3f33b1b173348ccf2dfa639fe03979d336c3 /crypto/src/asn1/anssi
parentRemove bridging version of Curve25519 (diff)
downloadBouncyCastle.NET-ed25519-110f936da13a1639a83edc512c5c104d2f5694a7.tar.xz
Update EC curve registry classes
Diffstat (limited to 'crypto/src/asn1/anssi')
-rw-r--r--crypto/src/asn1/anssi/ANSSINamedCurves.cs86
1 files changed, 42 insertions, 44 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); }
         }
     }
 }