xref: /openssl/crypto/evp/ec_support.c (revision fecb3aae)
1 /*
2  * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <string.h>
11 #include <openssl/ec.h>
12 #include "crypto/ec.h"
13 #include "internal/nelem.h"
14 
15 typedef struct ec_name2nid_st {
16     const char *name;
17     int nid;
18 } EC_NAME2NID;
19 
20 static const EC_NAME2NID curve_list[] = {
21     /* prime field curves */
22     /* secg curves */
23     {"secp112r1", NID_secp112r1 },
24     {"secp112r2", NID_secp112r2 },
25     {"secp128r1", NID_secp128r1 },
26     {"secp128r2", NID_secp128r2 },
27     {"secp160k1", NID_secp160k1 },
28     {"secp160r1", NID_secp160r1 },
29     {"secp160r2", NID_secp160r2 },
30     {"secp192k1", NID_secp192k1 },
31     {"secp224k1", NID_secp224k1 },
32     {"secp224r1", NID_secp224r1 },
33     {"secp256k1", NID_secp256k1 },
34     {"secp384r1", NID_secp384r1 },
35     {"secp521r1", NID_secp521r1 },
36     /* X9.62 curves */
37     {"prime192v1", NID_X9_62_prime192v1 },
38     {"prime192v2", NID_X9_62_prime192v2 },
39     {"prime192v3", NID_X9_62_prime192v3 },
40     {"prime239v1", NID_X9_62_prime239v1 },
41     {"prime239v2", NID_X9_62_prime239v2 },
42     {"prime239v3", NID_X9_62_prime239v3 },
43     {"prime256v1", NID_X9_62_prime256v1 },
44     /* characteristic two field curves */
45     /* NIST/SECG curves */
46     {"sect113r1", NID_sect113r1 },
47     {"sect113r2", NID_sect113r2 },
48     {"sect131r1", NID_sect131r1 },
49     {"sect131r2", NID_sect131r2 },
50     {"sect163k1", NID_sect163k1 },
51     {"sect163r1", NID_sect163r1 },
52     {"sect163r2", NID_sect163r2 },
53     {"sect193r1", NID_sect193r1 },
54     {"sect193r2", NID_sect193r2 },
55     {"sect233k1", NID_sect233k1 },
56     {"sect233r1", NID_sect233r1 },
57     {"sect239k1", NID_sect239k1 },
58     {"sect283k1", NID_sect283k1 },
59     {"sect283r1", NID_sect283r1 },
60     {"sect409k1", NID_sect409k1 },
61     {"sect409r1", NID_sect409r1 },
62     {"sect571k1", NID_sect571k1 },
63     {"sect571r1", NID_sect571r1 },
64     /* X9.62 curves */
65     {"c2pnb163v1", NID_X9_62_c2pnb163v1 },
66     {"c2pnb163v2", NID_X9_62_c2pnb163v2 },
67     {"c2pnb163v3", NID_X9_62_c2pnb163v3 },
68     {"c2pnb176v1", NID_X9_62_c2pnb176v1 },
69     {"c2tnb191v1", NID_X9_62_c2tnb191v1 },
70     {"c2tnb191v2", NID_X9_62_c2tnb191v2 },
71     {"c2tnb191v3", NID_X9_62_c2tnb191v3 },
72     {"c2pnb208w1", NID_X9_62_c2pnb208w1 },
73     {"c2tnb239v1", NID_X9_62_c2tnb239v1 },
74     {"c2tnb239v2", NID_X9_62_c2tnb239v2 },
75     {"c2tnb239v3", NID_X9_62_c2tnb239v3 },
76     {"c2pnb272w1", NID_X9_62_c2pnb272w1 },
77     {"c2pnb304w1", NID_X9_62_c2pnb304w1 },
78     {"c2tnb359v1", NID_X9_62_c2tnb359v1 },
79     {"c2pnb368w1", NID_X9_62_c2pnb368w1 },
80     {"c2tnb431r1", NID_X9_62_c2tnb431r1 },
81     /*
82      * the WAP/WTLS curves [unlike SECG, spec has its own OIDs for curves
83      * from X9.62]
84      */
85     {"wap-wsg-idm-ecid-wtls1", NID_wap_wsg_idm_ecid_wtls1 },
86     {"wap-wsg-idm-ecid-wtls3", NID_wap_wsg_idm_ecid_wtls3 },
87     {"wap-wsg-idm-ecid-wtls4", NID_wap_wsg_idm_ecid_wtls4 },
88     {"wap-wsg-idm-ecid-wtls5", NID_wap_wsg_idm_ecid_wtls5 },
89     {"wap-wsg-idm-ecid-wtls6", NID_wap_wsg_idm_ecid_wtls6 },
90     {"wap-wsg-idm-ecid-wtls7", NID_wap_wsg_idm_ecid_wtls7 },
91     {"wap-wsg-idm-ecid-wtls8", NID_wap_wsg_idm_ecid_wtls8 },
92     {"wap-wsg-idm-ecid-wtls9", NID_wap_wsg_idm_ecid_wtls9 },
93     {"wap-wsg-idm-ecid-wtls10", NID_wap_wsg_idm_ecid_wtls10 },
94     {"wap-wsg-idm-ecid-wtls11", NID_wap_wsg_idm_ecid_wtls11 },
95     {"wap-wsg-idm-ecid-wtls12", NID_wap_wsg_idm_ecid_wtls12 },
96     /* IPSec curves */
97     {"Oakley-EC2N-3", NID_ipsec3 },
98     {"Oakley-EC2N-4", NID_ipsec4 },
99     /* brainpool curves */
100     {"brainpoolP160r1", NID_brainpoolP160r1 },
101     {"brainpoolP160t1", NID_brainpoolP160t1 },
102     {"brainpoolP192r1", NID_brainpoolP192r1 },
103     {"brainpoolP192t1", NID_brainpoolP192t1 },
104     {"brainpoolP224r1", NID_brainpoolP224r1 },
105     {"brainpoolP224t1", NID_brainpoolP224t1 },
106     {"brainpoolP256r1", NID_brainpoolP256r1 },
107     {"brainpoolP256t1", NID_brainpoolP256t1 },
108     {"brainpoolP320r1", NID_brainpoolP320r1 },
109     {"brainpoolP320t1", NID_brainpoolP320t1 },
110     {"brainpoolP384r1", NID_brainpoolP384r1 },
111     {"brainpoolP384t1", NID_brainpoolP384t1 },
112     {"brainpoolP512r1", NID_brainpoolP512r1 },
113     {"brainpoolP512t1", NID_brainpoolP512t1 },
114     /* SM2 curve */
115     {"SM2", NID_sm2 },
116 };
117 
OSSL_EC_curve_nid2name(int nid)118 const char *OSSL_EC_curve_nid2name(int nid)
119 {
120     size_t i;
121 
122     if (nid <= 0)
123         return NULL;
124 
125     for (i = 0; i < OSSL_NELEM(curve_list); i++) {
126         if (curve_list[i].nid == nid)
127             return curve_list[i].name;
128     }
129     return NULL;
130 }
131 
ossl_ec_curve_name2nid(const char * name)132 int ossl_ec_curve_name2nid(const char *name)
133 {
134     size_t i;
135     int nid;
136 
137     if (name != NULL) {
138         if ((nid = ossl_ec_curve_nist2nid_int(name)) != NID_undef)
139             return nid;
140 
141         for (i = 0; i < OSSL_NELEM(curve_list); i++) {
142             if (OPENSSL_strcasecmp(curve_list[i].name, name) == 0)
143                 return curve_list[i].nid;
144         }
145     }
146 
147     return NID_undef;
148 }
149 
150 /* Functions to translate between common NIST curve names and NIDs */
151 
152 static const EC_NAME2NID nist_curves[] = {
153     {"B-163", NID_sect163r2},
154     {"B-233", NID_sect233r1},
155     {"B-283", NID_sect283r1},
156     {"B-409", NID_sect409r1},
157     {"B-571", NID_sect571r1},
158     {"K-163", NID_sect163k1},
159     {"K-233", NID_sect233k1},
160     {"K-283", NID_sect283k1},
161     {"K-409", NID_sect409k1},
162     {"K-571", NID_sect571k1},
163     {"P-192", NID_X9_62_prime192v1},
164     {"P-224", NID_secp224r1},
165     {"P-256", NID_X9_62_prime256v1},
166     {"P-384", NID_secp384r1},
167     {"P-521", NID_secp521r1}
168 };
169 
ossl_ec_curve_nid2nist_int(int nid)170 const char *ossl_ec_curve_nid2nist_int(int nid)
171 {
172     size_t i;
173     for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
174         if (nist_curves[i].nid == nid)
175             return nist_curves[i].name;
176     }
177     return NULL;
178 }
179 
ossl_ec_curve_nist2nid_int(const char * name)180 int ossl_ec_curve_nist2nid_int(const char *name)
181 {
182     size_t i;
183     for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
184         if (strcmp(nist_curves[i].name, name) == 0)
185             return nist_curves[i].nid;
186     }
187     return NID_undef;
188 }
189