1package org.bouncycastle.asn1.nist;
2
3import java.util.Enumeration;
4import java.util.Hashtable;
5
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7import org.bouncycastle.asn1.sec.SECNamedCurves;
8import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
9import org.bouncycastle.asn1.x9.X9ECParameters;
10import org.bouncycastle.util.Strings;
11
12/**
13 * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-2
14 */
15public class NISTNamedCurves
16{
17    static final Hashtable objIds = new Hashtable();
18    static final Hashtable names = new Hashtable();
19
20    static void defineCurve(String name, ASN1ObjectIdentifier oid)
21    {
22        objIds.put(name, oid);
23        names.put(oid, name);
24    }
25
26    static
27    {
28        // TODO Missing the "K-" curves
29
30        defineCurve("B-571", SECObjectIdentifiers.sect571r1);
31        defineCurve("B-409", SECObjectIdentifiers.sect409r1);
32        defineCurve("B-283", SECObjectIdentifiers.sect283r1);
33        defineCurve("B-233", SECObjectIdentifiers.sect233r1);
34        defineCurve("B-163", SECObjectIdentifiers.sect163r2);
35        defineCurve("P-521", SECObjectIdentifiers.secp521r1);
36        defineCurve("P-384", SECObjectIdentifiers.secp384r1);
37        defineCurve("P-256", SECObjectIdentifiers.secp256r1);
38        defineCurve("P-224", SECObjectIdentifiers.secp224r1);
39        defineCurve("P-192", SECObjectIdentifiers.secp192r1);
40    }
41
42    public static X9ECParameters getByName(
43        String  name)
44    {
45        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)objIds.get(Strings.toUpperCase(name));
46
47        if (oid != null)
48        {
49            return getByOID(oid);
50        }
51
52        return null;
53    }
54
55    /**
56     * return the X9ECParameters object for the named curve represented by
57     * the passed in object identifier. Null if the curve isn't present.
58     *
59     * @param oid an object identifier representing a named curve, if present.
60     */
61    public static X9ECParameters getByOID(
62        ASN1ObjectIdentifier  oid)
63    {
64        return SECNamedCurves.getByOID(oid);
65    }
66
67    /**
68     * return the object identifier signified by the passed in name. Null
69     * if there is no object identifier associated with name.
70     *
71     * @return the object identifier associated with name, if present.
72     */
73    public static ASN1ObjectIdentifier getOID(
74        String  name)
75    {
76        return (ASN1ObjectIdentifier)objIds.get(Strings.toUpperCase(name));
77    }
78
79    /**
80     * return the named curve name represented by the given object identifier.
81     */
82    public static String getName(
83        ASN1ObjectIdentifier  oid)
84    {
85        return (String)names.get(oid);
86    }
87
88    /**
89     * returns an enumeration containing the name strings for curves
90     * contained in this structure.
91     */
92    public static Enumeration getNames()
93    {
94        return objIds.keys();
95    }
96}
97