From 110f936da13a1639a83edc512c5c104d2f5694a7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 20:37:41 +0700 Subject: Update EC curve registry classes --- crypto/src/asn1/sec/SECNamedCurves.cs | 188 ++++++++-------------------------- 1 file changed, 42 insertions(+), 146 deletions(-) (limited to 'crypto/src/asn1/sec/SECNamedCurves.cs') diff --git a/crypto/src/asn1/sec/SECNamedCurves.cs b/crypto/src/asn1/sec/SECNamedCurves.cs index ad2f3e333..c0a783ec6 100644 --- a/crypto/src/asn1/sec/SECNamedCurves.cs +++ b/crypto/src/asn1/sec/SECNamedCurves.cs @@ -1,24 +1,19 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Endo; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.Sec { - public sealed class SecNamedCurves + /// Elliptic curve registry for the SEC standard. + public static class SecNamedCurves { - private SecNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -41,9 +36,6 @@ namespace Org.BouncyCastle.Asn1.Sec return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * secp112r1 - */ internal class Secp112r1Holder : X9ECParametersHolder { @@ -75,9 +67,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp112r2 - */ internal class Secp112r2Holder : X9ECParametersHolder { @@ -109,9 +98,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r1 - */ internal class Secp128r1Holder : X9ECParametersHolder { @@ -143,9 +129,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r2 - */ internal class Secp128r2Holder : X9ECParametersHolder { @@ -177,9 +160,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160k1 - */ internal class Secp160k1Holder : X9ECParametersHolder { @@ -225,9 +205,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r1 - */ internal class Secp160r1Holder : X9ECParametersHolder { @@ -259,9 +236,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r2 - */ internal class Secp160r2Holder : X9ECParametersHolder { @@ -293,9 +267,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192k1 - */ internal class Secp192k1Holder : X9ECParametersHolder { @@ -341,9 +312,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192r1 - */ internal class Secp192r1Holder : X9ECParametersHolder { @@ -375,9 +343,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224k1 - */ internal class Secp224k1Holder : X9ECParametersHolder { @@ -423,9 +388,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224r1 - */ internal class Secp224r1Holder : X9ECParametersHolder { @@ -457,9 +419,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256k1 - */ internal class Secp256k1Holder : X9ECParametersHolder { @@ -505,9 +464,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256r1 - */ internal class Secp256r1Holder : X9ECParametersHolder { @@ -539,9 +495,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp384r1 - */ internal class Secp384r1Holder : X9ECParametersHolder { @@ -574,9 +527,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp521r1 - */ internal class Secp521r1Holder : X9ECParametersHolder { @@ -609,9 +559,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r1 - */ internal class Sect113r1Holder : X9ECParametersHolder { @@ -644,9 +591,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r2 - */ internal class Sect113r2Holder : X9ECParametersHolder { @@ -679,9 +623,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r1 - */ internal class Sect131r1Holder : X9ECParametersHolder { @@ -716,9 +657,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r2 - */ internal class Sect131r2Holder : X9ECParametersHolder { @@ -753,9 +691,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163k1 - */ internal class Sect163k1Holder : X9ECParametersHolder { @@ -790,9 +725,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r1 - */ internal class Sect163r1Holder : X9ECParametersHolder { @@ -827,9 +759,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r2 - */ internal class Sect163r2Holder : X9ECParametersHolder { @@ -864,9 +793,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r1 - */ internal class Sect193r1Holder : X9ECParametersHolder { @@ -899,9 +825,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r2 - */ internal class Sect193r2Holder : X9ECParametersHolder { @@ -934,9 +857,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233k1 - */ internal class Sect233k1Holder : X9ECParametersHolder { @@ -969,9 +889,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233r1 - */ internal class Sect233r1Holder : X9ECParametersHolder { @@ -1004,9 +921,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect239k1 - */ internal class Sect239k1Holder : X9ECParametersHolder { @@ -1039,9 +953,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283k1 - */ internal class Sect283k1Holder : X9ECParametersHolder { @@ -1077,9 +988,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283r1 - */ internal class Sect283r1Holder : X9ECParametersHolder { @@ -1115,9 +1023,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409k1 - */ internal class Sect409k1Holder : X9ECParametersHolder { @@ -1151,9 +1056,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409r1 - */ internal class Sect409r1Holder : X9ECParametersHolder { @@ -1187,9 +1089,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571k1 - */ internal class Sect571k1Holder : X9ECParametersHolder { @@ -1225,9 +1124,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571r1 - */ internal class Sect571r1Holder : X9ECParametersHolder { @@ -1263,17 +1159,16 @@ namespace Org.BouncyCastle.Asn1.Sec } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -1316,63 +1211,64 @@ namespace Org.BouncyCastle.Asn1.Sec DefineCurve("sect571r1", SecObjectIdentifiers.SecT571r1, Sect571r1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - /** - * return the X9ECParameters object for the named curve represented by - * the passed in object identifier. Null if the curve isn't present. - * - * @param oid an object identifier representing a named curve, if present. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } -- cgit 1.4.1