1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_MECC
7 
8 static const struct {
9    const char *OID;
10    const char *names[6];
11 } s_curve_names[] = {
12 #ifdef LTC_ECC_SECP112R1
13    {
14       "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL }
15    },
16 #endif
17 #ifdef LTC_ECC_SECP112R2
18    {
19       "1.3.132.0.7", { "SECP112R2", NULL }
20    },
21 #endif
22 #ifdef LTC_ECC_SECP128R1
23    {
24       "1.3.132.0.28", { "SECP128R1", "ECC-128", NULL }
25    },
26 #endif
27 #ifdef LTC_ECC_SECP128R2
28    {
29       "1.3.132.0.29", { "SECP128R2", NULL }
30    },
31 #endif
32 #ifdef LTC_ECC_SECP160R1
33    {
34       "1.3.132.0.8", { "SECP160R1", "ECC-160", NULL }
35    },
36 #endif
37 #ifdef LTC_ECC_SECP160R2
38    {
39       "1.3.132.0.30", { "SECP160R2", NULL }
40    },
41 #endif
42 #ifdef LTC_ECC_SECP160K1
43    {
44       "1.3.132.0.9", { "SECP160K1", NULL }
45    },
46 #endif
47 #ifdef LTC_ECC_SECP192R1
48    {
49       "1.2.840.10045.3.1.1", { "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192", NULL }
50    },
51 #endif
52 #ifdef LTC_ECC_PRIME192V2
53    {
54       "1.2.840.10045.3.1.2", { "PRIME192V2", NULL }
55    },
56 #endif
57 #ifdef LTC_ECC_PRIME192V3
58    {
59       "1.2.840.10045.3.1.3", { "PRIME192V3", NULL }
60    },
61 #endif
62 #ifdef LTC_ECC_SECP192K1
63    {
64       "1.3.132.0.31", { "SECP192K1", NULL }
65    },
66 #endif
67 #ifdef LTC_ECC_SECP224R1
68    {
69       "1.3.132.0.33", { "SECP224R1", "NISTP224", "ECC-224", "P-224", NULL }
70    },
71 #endif
72 #ifdef LTC_ECC_SECP224K1
73    {
74       "1.3.132.0.32", { "SECP224K1", NULL }
75    },
76 #endif
77 #ifdef LTC_ECC_SECP256R1
78    {
79       "1.2.840.10045.3.1.7", { "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256", NULL }
80    },
81 #endif
82 #ifdef LTC_ECC_SECP256K1
83    {
84       "1.3.132.0.10", { "SECP256K1", NULL }
85    },
86 #endif
87 #ifdef LTC_ECC_SECP384R1
88    {
89       "1.3.132.0.34", { "SECP384R1", "NISTP384", "ECC-384", "P-384", NULL }
90    },
91 #endif
92 #ifdef LTC_ECC_SECP521R1
93    {
94       "1.3.132.0.35", { "SECP521R1", "NISTP521", "ECC-521", "P-521", NULL }
95    },
96 #endif
97 #ifdef LTC_ECC_PRIME239V1
98    {
99       "1.2.840.10045.3.1.4", { "PRIME239V1", NULL }
100    },
101 #endif
102 #ifdef LTC_ECC_PRIME239V2
103    {
104       "1.2.840.10045.3.1.5", { "PRIME239V2", NULL }
105    },
106 #endif
107 #ifdef LTC_ECC_PRIME239V3
108    {
109       "1.2.840.10045.3.1.6", { "PRIME239V3", NULL }
110    },
111 #endif
112 #ifdef LTC_ECC_BRAINPOOLP160R1
113    {
114       "1.3.36.3.3.2.8.1.1.1", { "BRAINPOOLP160R1", NULL }
115    },
116 #endif
117 #ifdef LTC_ECC_BRAINPOOLP192R1
118    {
119       "1.3.36.3.3.2.8.1.1.3", { "BRAINPOOLP192R1", NULL }
120    },
121 #endif
122 #ifdef LTC_ECC_BRAINPOOLP224R1
123    {
124       "1.3.36.3.3.2.8.1.1.5", { "BRAINPOOLP224R1", NULL }
125    },
126 #endif
127 #ifdef LTC_ECC_BRAINPOOLP256R1
128    {
129       "1.3.36.3.3.2.8.1.1.7", { "BRAINPOOLP256R1", NULL }
130    },
131 #endif
132 #ifdef LTC_ECC_BRAINPOOLP320R1
133    {
134       "1.3.36.3.3.2.8.1.1.9", { "BRAINPOOLP320R1", NULL }
135    },
136 #endif
137 #ifdef LTC_ECC_BRAINPOOLP384R1
138    {
139       "1.3.36.3.3.2.8.1.1.11", { "BRAINPOOLP384R1", NULL }
140    },
141 #endif
142 #ifdef LTC_ECC_BRAINPOOLP512R1
143    {
144       "1.3.36.3.3.2.8.1.1.13", { "BRAINPOOLP512R1", NULL }
145    },
146 #endif
147 #ifdef LTC_ECC_BRAINPOOLP160T1
148    {
149       "1.3.36.3.3.2.8.1.1.2", { "BRAINPOOLP160T1", NULL }
150    },
151 #endif
152 #ifdef LTC_ECC_BRAINPOOLP192T1
153    {
154       "1.3.36.3.3.2.8.1.1.4", { "BRAINPOOLP192T1", NULL }
155    },
156 #endif
157 #ifdef LTC_ECC_BRAINPOOLP224T1
158    {
159       "1.3.36.3.3.2.8.1.1.6", { "BRAINPOOLP224T1", NULL }
160    },
161 #endif
162 #ifdef LTC_ECC_BRAINPOOLP256T1
163    {
164       "1.3.36.3.3.2.8.1.1.8", { "BRAINPOOLP256T1", NULL }
165    },
166 #endif
167 #ifdef LTC_ECC_BRAINPOOLP320T1
168    {
169       "1.3.36.3.3.2.8.1.1.10", { "BRAINPOOLP320T1", NULL }
170    },
171 #endif
172 #ifdef LTC_ECC_BRAINPOOLP384T1
173    {
174       "1.3.36.3.3.2.8.1.1.12", { "BRAINPOOLP384T1", NULL }
175    },
176 #endif
177 #ifdef LTC_ECC_BRAINPOOLP512T1
178    {
179       "1.3.36.3.3.2.8.1.1.14", { "BRAINPOOLP512T1", NULL }
180    },
181 #endif
182 #ifdef LTC_ECC_SM2
183    {
184       "1.2.156.10197.1.301", { "SM2", NULL }
185    },
186 #endif
187    {
188       NULL, { NULL }
189    }
190 };
191 
192 /* case-insensitive match + ignore '-', '_', ' ' */
s_name_match(const char * left,const char * right)193 static int s_name_match(const char *left, const char *right)
194 {
195    char lc_r, lc_l;
196 
197    while ((*left != '\0') && (*right != '\0')) {
198       while ((*left  == ' ') || (*left  == '-') || (*left  == '_')) left++;
199       while ((*right == ' ') || (*right == '-') || (*right == '_')) right++;
200       if (*left == '\0' || *right == '\0') break;
201       lc_r = *right;
202       lc_l = *left;
203       if ((lc_r >= 'A') && (lc_r <= 'Z')) lc_r += 32;
204       if ((lc_l >= 'A') && (lc_l <= 'Z')) lc_l += 32;
205       if (lc_l != lc_r) return 0;
206       left++;
207       right++;
208    }
209 
210    if ((*left == '\0') && (*right == '\0')) return 1;
211    return 0;
212 }
213 
ecc_find_curve(const char * name_or_oid,const ltc_ecc_curve ** cu)214 int ecc_find_curve(const char *name_or_oid, const ltc_ecc_curve **cu)
215 {
216    int i, j;
217    const char *OID = NULL;
218 
219    LTC_ARGCHK(cu != NULL);
220    LTC_ARGCHK(name_or_oid != NULL);
221 
222    *cu = NULL;
223 
224    for (i = 0; s_curve_names[i].OID != NULL && !OID; i++) {
225       if (XSTRCMP(s_curve_names[i].OID, name_or_oid) == 0) {
226          OID = s_curve_names[i].OID;
227       }
228       for (j = 0; s_curve_names[i].names[j] != NULL && !OID; j++) {
229          if (s_name_match(s_curve_names[i].names[j], name_or_oid)) {
230             OID = s_curve_names[i].OID;
231          }
232       }
233    }
234 
235    if (OID != NULL) {
236       for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) {
237          if (XSTRCMP(ltc_ecc_curves[i].OID, OID) == 0) {
238             *cu = &ltc_ecc_curves[i];
239             return CRYPT_OK;
240          }
241       }
242    }
243 
244    return CRYPT_INVALID_ARG; /* not found */
245 }
246 
247 #endif
248