using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Utilities.Collections;
namespace Org.BouncyCastle.Asn1.Nist
{
/// Elliptic curve registry for NIST curves.
public static class NistNamedCurves
{
private static readonly Dictionary objIds =
new Dictionary(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary names =
new Dictionary();
private static void DefineCurveAlias(string name, DerObjectIdentifier oid)
{
if (SecNamedCurves.GetByOidLazy(oid) == null)
throw new InvalidOperationException();
objIds.Add(name, oid);
names.Add(oid, name);
}
static NistNamedCurves()
{
DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2);
DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1);
DefineCurveAlias("B-283", SecObjectIdentifiers.SecT283r1);
DefineCurveAlias("B-409", SecObjectIdentifiers.SecT409r1);
DefineCurveAlias("B-571", SecObjectIdentifiers.SecT571r1);
DefineCurveAlias("K-163", SecObjectIdentifiers.SecT163k1);
DefineCurveAlias("K-233", SecObjectIdentifiers.SecT233k1);
DefineCurveAlias("K-283", SecObjectIdentifiers.SecT283k1);
DefineCurveAlias("K-409", SecObjectIdentifiers.SecT409k1);
DefineCurveAlias("K-571", SecObjectIdentifiers.SecT571k1);
DefineCurveAlias("P-192", SecObjectIdentifiers.SecP192r1);
DefineCurveAlias("P-224", SecObjectIdentifiers.SecP224r1);
DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1);
DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1);
DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);
}
/// Look up the for the curve with the given name.
/// The name of the curve.
public static X9ECParameters GetByName(string name)
{
DerObjectIdentifier oid = GetOid(name);
return oid == null ? null : 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);
}
/// Look up the for the curve with the given
/// OID.
/// The OID for the curve.
public static X9ECParameters GetByOid(DerObjectIdentifier oid)
{
return GetByOidLazy(oid)?.Parameters;
}
/// Look up an for the curve with the given
/// OID.
///
/// Allows accessing the curve without necessarily triggering the creation of
/// the full .
///
/// The OID for the curve.
public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid)
{
return names.ContainsKey(oid) ? SecNamedCurves.GetByOidLazy(oid) : null;
}
/// Look up the name of the curve with the given OID.
/// The OID for the curve.
public static string GetName(DerObjectIdentifier oid)
{
return CollectionUtilities.GetValueOrNull(names, oid);
}
/// Look up the OID of the curve with the given name.
/// The name of the curve.
public static DerObjectIdentifier GetOid(string name)
{
return CollectionUtilities.GetValueOrNull(objIds, name);
}
/// Enumerate the available curve names in this registry.
public static IEnumerable Names
{
get { return CollectionUtilities.Proxy(objIds.Keys); }
}
}
}