/src/openssl/crypto/ec/ec_backend.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-2024 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 | | /* |
11 | | * Low level APIs related to EC_KEY are deprecated for public use, |
12 | | * but still ok for internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/objects.h> |
18 | | #include <openssl/params.h> |
19 | | #include <openssl/err.h> |
20 | | #ifndef FIPS_MODULE |
21 | | #include <openssl/x509.h> |
22 | | #endif |
23 | | #include "crypto/bn.h" |
24 | | #include "crypto/ec.h" |
25 | | #include "ec_local.h" |
26 | | #include "internal/e_os.h" |
27 | | #include "internal/nelem.h" |
28 | | #include "internal/param_build_set.h" |
29 | | |
30 | | /* Mapping between a flag and a name */ |
31 | | static const OSSL_ITEM encoding_nameid_map[] = { |
32 | | { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT }, |
33 | | { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }, |
34 | | }; |
35 | | |
36 | | static const OSSL_ITEM check_group_type_nameid_map[] = { |
37 | | { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT }, |
38 | | { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED }, |
39 | | { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST }, |
40 | | }; |
41 | | |
42 | | static const OSSL_ITEM format_nameid_map[] = { |
43 | | { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED }, |
44 | | { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED }, |
45 | | { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID }, |
46 | | }; |
47 | | |
48 | | int ossl_ec_encoding_name2id(const char *name) |
49 | 102k | { |
50 | 102k | size_t i, sz; |
51 | | |
52 | | /* Return the default value if there is no name */ |
53 | 102k | if (name == NULL) |
54 | 0 | return OPENSSL_EC_NAMED_CURVE; |
55 | | |
56 | 205k | for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { |
57 | 205k | if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0) |
58 | 102k | return encoding_nameid_map[i].id; |
59 | 205k | } |
60 | 0 | return -1; |
61 | 102k | } |
62 | | |
63 | | static char *ec_param_encoding_id2name(int id) |
64 | 564k | { |
65 | 564k | size_t i, sz; |
66 | | |
67 | 1.12M | for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { |
68 | 1.12M | if (id == (int)encoding_nameid_map[i].id) |
69 | 564k | return encoding_nameid_map[i].ptr; |
70 | 1.12M | } |
71 | 0 | return NULL; |
72 | 564k | } |
73 | | |
74 | | char *ossl_ec_check_group_type_id2name(int id) |
75 | 512k | { |
76 | 512k | size_t i, sz; |
77 | | |
78 | 512k | for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { |
79 | 512k | if (id == (int)check_group_type_nameid_map[i].id) |
80 | 512k | return check_group_type_nameid_map[i].ptr; |
81 | 512k | } |
82 | 0 | return NULL; |
83 | 512k | } |
84 | | |
85 | | static int ec_check_group_type_name2id(const char *name) |
86 | 51.4k | { |
87 | 51.4k | size_t i, sz; |
88 | | |
89 | | /* Return the default value if there is no name */ |
90 | 51.4k | if (name == NULL) |
91 | 0 | return 0; |
92 | | |
93 | 51.4k | for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { |
94 | 51.4k | if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0) |
95 | 51.4k | return check_group_type_nameid_map[i].id; |
96 | 51.4k | } |
97 | 0 | return -1; |
98 | 51.4k | } |
99 | | |
100 | | int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name) |
101 | 51.4k | { |
102 | 51.4k | int flags = ec_check_group_type_name2id(name); |
103 | | |
104 | 51.4k | if (flags == -1) |
105 | 0 | return 0; |
106 | 51.4k | EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK); |
107 | 51.4k | EC_KEY_set_flags(ec, flags); |
108 | 51.4k | return 1; |
109 | 51.4k | } |
110 | | |
111 | | static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p) |
112 | 51.4k | { |
113 | 51.4k | const char *name = NULL; |
114 | 51.4k | int status = 0; |
115 | | |
116 | 51.4k | switch (p->data_type) { |
117 | 51.4k | case OSSL_PARAM_UTF8_STRING: |
118 | 51.4k | name = p->data; |
119 | 51.4k | status = (name != NULL); |
120 | 51.4k | break; |
121 | 0 | case OSSL_PARAM_UTF8_PTR: |
122 | 0 | status = OSSL_PARAM_get_utf8_ptr(p, &name); |
123 | 0 | break; |
124 | 51.4k | } |
125 | 51.4k | if (status) |
126 | 51.4k | return ossl_ec_set_check_group_type_from_name(ec, name); |
127 | 0 | return 0; |
128 | 51.4k | } |
129 | | |
130 | | int ossl_ec_pt_format_name2id(const char *name) |
131 | 205k | { |
132 | 205k | size_t i, sz; |
133 | | |
134 | | /* Return the default value if there is no name */ |
135 | 205k | if (name == NULL) |
136 | 0 | return (int)POINT_CONVERSION_UNCOMPRESSED; |
137 | | |
138 | 205k | for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { |
139 | 205k | if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0) |
140 | 205k | return format_nameid_map[i].id; |
141 | 205k | } |
142 | 0 | return -1; |
143 | 205k | } |
144 | | |
145 | | char *ossl_ec_pt_format_id2name(int id) |
146 | 1.07M | { |
147 | 1.07M | size_t i, sz; |
148 | | |
149 | 1.42M | for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { |
150 | 1.37M | if (id == (int)format_nameid_map[i].id) |
151 | 1.02M | return format_nameid_map[i].ptr; |
152 | 1.37M | } |
153 | 48.1k | return NULL; |
154 | 1.07M | } |
155 | | |
156 | | static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl, |
157 | | OSSL_PARAM params[], BN_CTX *bnctx, |
158 | | unsigned char **genbuf) |
159 | 461k | { |
160 | 461k | int ret = 0, fid; |
161 | 461k | const char *field_type; |
162 | 461k | const OSSL_PARAM *param = NULL; |
163 | 461k | const OSSL_PARAM *param_p = NULL; |
164 | 461k | const OSSL_PARAM *param_a = NULL; |
165 | 461k | const OSSL_PARAM *param_b = NULL; |
166 | | |
167 | 461k | fid = EC_GROUP_get_field_type(group); |
168 | | |
169 | 461k | if (fid == NID_X9_62_prime_field) { |
170 | 329k | field_type = SN_X9_62_prime_field; |
171 | 329k | } else if (fid == NID_X9_62_characteristic_two_field) { |
172 | | #ifdef OPENSSL_NO_EC2M |
173 | | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
174 | | goto err; |
175 | | #else |
176 | 131k | field_type = SN_X9_62_characteristic_two_field; |
177 | 131k | #endif |
178 | 131k | } else { |
179 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
180 | 0 | return 0; |
181 | 0 | } |
182 | | |
183 | 461k | param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P); |
184 | 461k | param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A); |
185 | 461k | param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B); |
186 | 461k | if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL) { |
187 | 0 | BIGNUM *p = BN_CTX_get(bnctx); |
188 | 0 | BIGNUM *a = BN_CTX_get(bnctx); |
189 | 0 | BIGNUM *b = BN_CTX_get(bnctx); |
190 | |
|
191 | 0 | if (b == NULL) { |
192 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
193 | 0 | goto err; |
194 | 0 | } |
195 | | |
196 | 0 | if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) { |
197 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE); |
198 | 0 | goto err; |
199 | 0 | } |
200 | 0 | if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p) |
201 | 0 | || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a) |
202 | 0 | || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) { |
203 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
204 | 0 | goto err; |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 461k | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER); |
209 | 461k | if (tmpl != NULL || param != NULL) { |
210 | 0 | const BIGNUM *order = EC_GROUP_get0_order(group); |
211 | |
|
212 | 0 | if (order == NULL) { |
213 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
214 | 0 | goto err; |
215 | 0 | } |
216 | 0 | if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER, |
217 | 0 | order)) { |
218 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
219 | 0 | goto err; |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 461k | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE); |
224 | 461k | if (tmpl != NULL || param != NULL) { |
225 | 104 | if (!ossl_param_build_set_utf8_string(tmpl, params, |
226 | 104 | OSSL_PKEY_PARAM_EC_FIELD_TYPE, |
227 | 104 | field_type)) { |
228 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
229 | 0 | goto err; |
230 | 0 | } |
231 | 104 | } |
232 | | |
233 | 461k | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR); |
234 | 461k | if (tmpl != NULL || param != NULL) { |
235 | 0 | size_t genbuf_len; |
236 | 0 | const EC_POINT *genpt = EC_GROUP_get0_generator(group); |
237 | 0 | point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group); |
238 | |
|
239 | 0 | if (genpt == NULL) { |
240 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
241 | 0 | goto err; |
242 | 0 | } |
243 | 0 | genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx); |
244 | 0 | if (genbuf_len == 0) { |
245 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
246 | 0 | goto err; |
247 | 0 | } |
248 | 0 | if (!ossl_param_build_set_octet_string(tmpl, params, |
249 | 0 | OSSL_PKEY_PARAM_EC_GENERATOR, |
250 | 0 | *genbuf, genbuf_len)) { |
251 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
252 | 0 | goto err; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 461k | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR); |
257 | 461k | if (tmpl != NULL || param != NULL) { |
258 | 0 | const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group); |
259 | |
|
260 | 0 | if (cofactor != NULL |
261 | 0 | && !ossl_param_build_set_bn(tmpl, params, |
262 | 0 | OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) { |
263 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
264 | 0 | goto err; |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | 461k | param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED); |
269 | 461k | if (tmpl != NULL || param != NULL) { |
270 | 0 | unsigned char *seed = EC_GROUP_get0_seed(group); |
271 | 0 | size_t seed_len = EC_GROUP_get_seed_len(group); |
272 | |
|
273 | 0 | if (seed != NULL |
274 | 0 | && seed_len > 0 |
275 | 0 | && !ossl_param_build_set_octet_string(tmpl, params, |
276 | 0 | OSSL_PKEY_PARAM_EC_SEED, |
277 | 0 | seed, seed_len)) { |
278 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB); |
279 | 0 | goto err; |
280 | 0 | } |
281 | 0 | } |
282 | 461k | ret = 1; |
283 | 461k | err: |
284 | 461k | return ret; |
285 | 461k | } |
286 | | |
287 | | int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl, |
288 | | OSSL_PARAM params[], OSSL_LIB_CTX *libctx, |
289 | | const char *propq, |
290 | | BN_CTX *bnctx, unsigned char **genbuf) |
291 | 564k | { |
292 | 564k | int ret = 0, curve_nid, encoding_flag; |
293 | 564k | const char *encoding_name, *pt_form_name; |
294 | 564k | point_conversion_form_t genform; |
295 | | |
296 | 564k | if (group == NULL) { |
297 | 0 | ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER); |
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | 564k | genform = EC_GROUP_get_point_conversion_form(group); |
302 | 564k | pt_form_name = ossl_ec_pt_format_id2name(genform); |
303 | 564k | if (pt_form_name == NULL |
304 | 564k | || !ossl_param_build_set_utf8_string( |
305 | 564k | tmpl, params, |
306 | 564k | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) { |
307 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); |
308 | 0 | return 0; |
309 | 0 | } |
310 | 564k | encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE; |
311 | 564k | encoding_name = ec_param_encoding_id2name(encoding_flag); |
312 | 564k | if (encoding_name == NULL |
313 | 564k | || !ossl_param_build_set_utf8_string(tmpl, params, |
314 | 564k | OSSL_PKEY_PARAM_EC_ENCODING, |
315 | 564k | encoding_name)) { |
316 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
317 | 0 | return 0; |
318 | 0 | } |
319 | | |
320 | 564k | if (!ossl_param_build_set_int(tmpl, params, |
321 | 564k | OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, |
322 | 564k | group->decoded_from_explicit_params)) |
323 | 0 | return 0; |
324 | | |
325 | 564k | curve_nid = EC_GROUP_get_curve_name(group); |
326 | | |
327 | | /* |
328 | | * Get the explicit parameters in these two cases: |
329 | | * - We do not have a template, i.e. specific parameters are requested |
330 | | * - The curve is not a named curve |
331 | | */ |
332 | 564k | if (tmpl == NULL || curve_nid == NID_undef) |
333 | 461k | if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf)) |
334 | 0 | goto err; |
335 | | |
336 | 564k | if (curve_nid != NID_undef) { |
337 | | /* Named curve */ |
338 | 561k | const char *curve_name = OSSL_EC_curve_nid2name(curve_nid); |
339 | | |
340 | 561k | if (curve_name == NULL |
341 | 561k | || !ossl_param_build_set_utf8_string(tmpl, params, |
342 | 561k | OSSL_PKEY_PARAM_GROUP_NAME, |
343 | 561k | curve_name)) { |
344 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE); |
345 | 0 | goto err; |
346 | 0 | } |
347 | 561k | } |
348 | 564k | ret = 1; |
349 | 564k | err: |
350 | 564k | return ret; |
351 | 564k | } |
352 | | |
353 | | /* |
354 | | * The intention with the "backend" source file is to offer backend functions |
355 | | * for legacy backends (EVP_PKEY_ASN1_METHOD) and provider implementations |
356 | | * alike. |
357 | | */ |
358 | | int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode) |
359 | 116k | { |
360 | 116k | const EC_GROUP *ecg = EC_KEY_get0_group(ec); |
361 | 116k | const BIGNUM *cofactor; |
362 | | /* |
363 | | * mode can be only 0 for disable, or 1 for enable here. |
364 | | * |
365 | | * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that |
366 | | * also supports mode == -1 with the meaning of "reset to the default for |
367 | | * the associated key". |
368 | | */ |
369 | 116k | if (mode < 0 || mode > 1) |
370 | 0 | return 0; |
371 | | |
372 | 116k | if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL) |
373 | 0 | return 0; |
374 | | |
375 | | /* ECDH cofactor mode has no effect if cofactor is 1 */ |
376 | 116k | if (BN_is_one(cofactor)) |
377 | 116k | return 1; |
378 | | |
379 | 0 | if (mode == 1) |
380 | 0 | EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH); |
381 | 0 | else if (mode == 0) |
382 | 0 | EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH); |
383 | |
|
384 | 0 | return 1; |
385 | 116k | } |
386 | | |
387 | | /* |
388 | | * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has |
389 | | * been called before! |
390 | | * |
391 | | * This function only gets the bare keypair, domain parameters and other |
392 | | * parameters are treated separately, and domain parameters are required to |
393 | | * define a keypair. |
394 | | */ |
395 | | int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private) |
396 | 19.9k | { |
397 | 19.9k | const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL; |
398 | 19.9k | BN_CTX *ctx = NULL; |
399 | 19.9k | BIGNUM *priv_key = NULL; |
400 | 19.9k | unsigned char *pub_key = NULL; |
401 | 19.9k | size_t pub_key_len; |
402 | 19.9k | const EC_GROUP *ecg = NULL; |
403 | 19.9k | EC_POINT *pub_point = NULL; |
404 | 19.9k | int ok = 0; |
405 | | |
406 | 19.9k | ecg = EC_KEY_get0_group(ec); |
407 | 19.9k | if (ecg == NULL) |
408 | 0 | return 0; |
409 | | |
410 | 19.9k | param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY); |
411 | 19.9k | if (include_private) |
412 | 19.9k | param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); |
413 | | |
414 | 19.9k | ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec)); |
415 | 19.9k | if (ctx == NULL) |
416 | 0 | goto err; |
417 | | |
418 | 19.9k | if (param_pub_key != NULL) |
419 | 19.9k | if (!OSSL_PARAM_get_octet_string(param_pub_key, |
420 | 19.9k | (void **)&pub_key, 0, &pub_key_len) |
421 | 19.9k | || (pub_point = EC_POINT_new(ecg)) == NULL |
422 | 19.9k | || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx)) |
423 | 0 | goto err; |
424 | | |
425 | 19.9k | if (param_priv_key != NULL && include_private) { |
426 | 19.9k | int fixed_words; |
427 | 19.9k | const BIGNUM *order; |
428 | | |
429 | | /* |
430 | | * Key import/export should never leak the bit length of the secret |
431 | | * scalar in the key. |
432 | | * |
433 | | * For this reason, on export we use padded BIGNUMs with fixed length. |
434 | | * |
435 | | * When importing we also should make sure that, even if short lived, |
436 | | * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as |
437 | | * soon as possible, so that any processing of this BIGNUM might opt for |
438 | | * constant time implementations in the backend. |
439 | | * |
440 | | * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have |
441 | | * to preallocate the BIGNUM internal buffer to a fixed public size big |
442 | | * enough that operations performed during the processing never trigger |
443 | | * a realloc which would leak the size of the scalar through memory |
444 | | * accesses. |
445 | | * |
446 | | * Fixed Length |
447 | | * ------------ |
448 | | * |
449 | | * The order of the large prime subgroup of the curve is our choice for |
450 | | * a fixed public size, as that is generally the upper bound for |
451 | | * generating a private key in EC cryptosystems and should fit all valid |
452 | | * secret scalars. |
453 | | * |
454 | | * For padding on export we just use the bit length of the order |
455 | | * converted to bytes (rounding up). |
456 | | * |
457 | | * For preallocating the BIGNUM storage we look at the number of "words" |
458 | | * required for the internal representation of the order, and we |
459 | | * preallocate 2 extra "words" in case any of the subsequent processing |
460 | | * might temporarily overflow the order length. |
461 | | */ |
462 | 19.9k | order = EC_GROUP_get0_order(ecg); |
463 | 19.9k | if (order == NULL || BN_is_zero(order)) |
464 | 0 | goto err; |
465 | | |
466 | 19.9k | fixed_words = bn_get_top(order) + 2; |
467 | | |
468 | 19.9k | if ((priv_key = BN_secure_new()) == NULL) |
469 | 0 | goto err; |
470 | 19.9k | if (bn_wexpand(priv_key, fixed_words) == NULL) |
471 | 0 | goto err; |
472 | 19.9k | BN_set_flags(priv_key, BN_FLG_CONSTTIME); |
473 | | |
474 | 19.9k | if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key)) |
475 | 0 | goto err; |
476 | 19.9k | } |
477 | | |
478 | 19.9k | if (priv_key != NULL |
479 | 19.9k | && !EC_KEY_set_private_key(ec, priv_key)) |
480 | 0 | goto err; |
481 | | |
482 | 19.9k | if (pub_point != NULL |
483 | 19.9k | && !EC_KEY_set_public_key(ec, pub_point)) |
484 | 0 | goto err; |
485 | | |
486 | | /* Fallback computation of public key if not provided */ |
487 | 19.9k | if (priv_key != NULL && pub_point == NULL) { |
488 | 0 | if ((pub_point = EC_POINT_new(ecg)) == NULL |
489 | 0 | || !EC_KEY_set_public_key(ec, pub_point)) |
490 | 0 | goto err; |
491 | 0 | if (!ossl_ec_key_simple_generate_public_key(ec)) |
492 | 0 | goto err; |
493 | 0 | } |
494 | | |
495 | 19.9k | ok = 1; |
496 | | |
497 | 19.9k | err: |
498 | 19.9k | BN_CTX_free(ctx); |
499 | 19.9k | BN_clear_free(priv_key); |
500 | 19.9k | OPENSSL_free(pub_key); |
501 | 19.9k | EC_POINT_free(pub_point); |
502 | 19.9k | return ok; |
503 | 19.9k | } |
504 | | |
505 | | int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
506 | 102k | { |
507 | 102k | int ok = 0; |
508 | 102k | EC_GROUP *group = NULL; |
509 | | |
510 | 102k | if (ec == NULL) |
511 | 0 | return 0; |
512 | | |
513 | 102k | group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec), |
514 | 102k | ossl_ec_key_get0_propq(ec)); |
515 | | |
516 | 102k | if (!EC_KEY_set_group(ec, group)) |
517 | 0 | goto err; |
518 | 102k | ok = 1; |
519 | 102k | err: |
520 | 102k | EC_GROUP_free(group); |
521 | 102k | return ok; |
522 | 102k | } |
523 | | |
524 | | static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
525 | 108k | { |
526 | 108k | const OSSL_PARAM *p; |
527 | 108k | int format = -1; |
528 | | |
529 | 108k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT); |
530 | 108k | if (p != NULL) { |
531 | 102k | if (!ossl_ec_pt_format_param2id(p, &format)) { |
532 | 0 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); |
533 | 0 | return 0; |
534 | 0 | } |
535 | 102k | EC_KEY_set_conv_form(ec, format); |
536 | 102k | } |
537 | 108k | return 1; |
538 | 108k | } |
539 | | |
540 | | static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
541 | 108k | { |
542 | 108k | const OSSL_PARAM *p; |
543 | | |
544 | 108k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE); |
545 | 108k | if (p != NULL) |
546 | 51.4k | return ec_set_check_group_type_from_param(ec, p); |
547 | 56.9k | return 1; |
548 | 108k | } |
549 | | |
550 | | static int ec_set_include_public(EC_KEY *ec, int include) |
551 | 0 | { |
552 | 0 | int flags = EC_KEY_get_enc_flags(ec); |
553 | |
|
554 | 0 | if (!include) |
555 | 0 | flags |= EC_PKEY_NO_PUBKEY; |
556 | 0 | else |
557 | 0 | flags &= ~EC_PKEY_NO_PUBKEY; |
558 | 0 | EC_KEY_set_enc_flags(ec, flags); |
559 | 0 | return 1; |
560 | 0 | } |
561 | | |
562 | | int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[]) |
563 | 108k | { |
564 | 108k | const OSSL_PARAM *p; |
565 | | |
566 | 108k | if (ec == NULL) |
567 | 0 | return 0; |
568 | | |
569 | 108k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH); |
570 | 108k | if (p != NULL) { |
571 | 102k | int mode; |
572 | | |
573 | 102k | if (!OSSL_PARAM_get_int(p, &mode) |
574 | 102k | || !ossl_ec_set_ecdh_cofactor_mode(ec, mode)) |
575 | 0 | return 0; |
576 | 102k | } |
577 | | |
578 | 108k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC); |
579 | 108k | if (p != NULL) { |
580 | 0 | int include = 1; |
581 | |
|
582 | 0 | if (!OSSL_PARAM_get_int(p, &include) |
583 | 0 | || !ec_set_include_public(ec, include)) |
584 | 0 | return 0; |
585 | 0 | } |
586 | 108k | if (!ec_key_point_format_fromdata(ec, params)) |
587 | 0 | return 0; |
588 | 108k | if (!ec_key_group_check_fromdata(ec, params)) |
589 | 0 | return 0; |
590 | 108k | return 1; |
591 | 108k | } |
592 | | |
593 | | int ossl_ec_key_is_foreign(const EC_KEY *ec) |
594 | 71.6k | { |
595 | 71.6k | #ifndef FIPS_MODULE |
596 | 71.6k | if (EC_KEY_get_method(ec) != EC_KEY_OpenSSL()) |
597 | 0 | return 1; |
598 | 71.6k | #endif |
599 | 71.6k | return 0; |
600 | 71.6k | } |
601 | | |
602 | | EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection) |
603 | 9.15k | { |
604 | 9.15k | EC_KEY *ret; |
605 | | |
606 | 9.15k | if (src == NULL) { |
607 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
608 | 0 | return NULL; |
609 | 0 | } |
610 | | |
611 | 9.15k | if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq)) == NULL) |
612 | 0 | return NULL; |
613 | | |
614 | | /* copy the parameters */ |
615 | 9.15k | if (src->group != NULL |
616 | 9.15k | && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
617 | 9.15k | ret->group = ossl_ec_group_new_ex(src->libctx, src->propq, |
618 | 9.15k | src->group->meth); |
619 | 9.15k | if (ret->group == NULL |
620 | 9.15k | || !EC_GROUP_copy(ret->group, src->group)) |
621 | 0 | goto err; |
622 | | |
623 | 9.15k | if (src->meth != NULL) |
624 | 9.15k | ret->meth = src->meth; |
625 | 9.15k | } |
626 | | |
627 | | /* copy the public key */ |
628 | 9.15k | if (src->pub_key != NULL |
629 | 8.53k | && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
630 | 4.36k | if (ret->group == NULL) |
631 | | /* no parameter-less keys allowed */ |
632 | 0 | goto err; |
633 | 4.36k | ret->pub_key = EC_POINT_new(ret->group); |
634 | 4.36k | if (ret->pub_key == NULL |
635 | 4.36k | || !EC_POINT_copy(ret->pub_key, src->pub_key)) |
636 | 0 | goto err; |
637 | 4.36k | } |
638 | | |
639 | | /* copy the private key */ |
640 | 9.15k | if (src->priv_key != NULL |
641 | 7.85k | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
642 | 3.67k | if (ret->group == NULL) |
643 | | /* no parameter-less keys allowed */ |
644 | 0 | goto err; |
645 | 3.67k | ret->priv_key = BN_new(); |
646 | 3.67k | if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key)) |
647 | 0 | goto err; |
648 | 3.67k | if (ret->group->meth->keycopy |
649 | 0 | && ret->group->meth->keycopy(ret, src) == 0) |
650 | 0 | goto err; |
651 | 3.67k | } |
652 | | |
653 | | /* copy the rest */ |
654 | 9.15k | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) { |
655 | 4.97k | ret->enc_flag = src->enc_flag; |
656 | 4.97k | ret->conv_form = src->conv_form; |
657 | 4.97k | } |
658 | | |
659 | 9.15k | ret->version = src->version; |
660 | 9.15k | ret->flags = src->flags; |
661 | | |
662 | 9.15k | #ifndef FIPS_MODULE |
663 | 9.15k | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, |
664 | 9.15k | &ret->ex_data, &src->ex_data)) |
665 | 0 | goto err; |
666 | 9.15k | #endif |
667 | | |
668 | 9.15k | if (ret->meth != NULL && ret->meth->copy != NULL) { |
669 | 0 | if ((selection |
670 | 0 | & OSSL_KEYMGMT_SELECT_KEYPAIR) |
671 | 0 | != OSSL_KEYMGMT_SELECT_KEYPAIR) |
672 | 0 | goto err; |
673 | 0 | if (ret->meth->copy(ret, src) == 0) |
674 | 0 | goto err; |
675 | 0 | } |
676 | | |
677 | 9.15k | return ret; |
678 | 0 | err: |
679 | 0 | EC_KEY_free(ret); |
680 | 0 | return NULL; |
681 | 9.15k | } |
682 | | |
683 | | int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id) |
684 | 102k | { |
685 | 102k | const char *name = NULL; |
686 | 102k | int status = 0; |
687 | | |
688 | 102k | switch (p->data_type) { |
689 | 102k | case OSSL_PARAM_UTF8_STRING: |
690 | | /* The OSSL_PARAM functions have no support for this */ |
691 | 102k | name = p->data; |
692 | 102k | status = (name != NULL); |
693 | 102k | break; |
694 | 0 | case OSSL_PARAM_UTF8_PTR: |
695 | 0 | status = OSSL_PARAM_get_utf8_ptr(p, &name); |
696 | 0 | break; |
697 | 102k | } |
698 | 102k | if (status) { |
699 | 102k | int i = ossl_ec_encoding_name2id(name); |
700 | | |
701 | 102k | if (i >= 0) { |
702 | 102k | *id = i; |
703 | 102k | return 1; |
704 | 102k | } |
705 | 102k | } |
706 | 0 | return 0; |
707 | 102k | } |
708 | | |
709 | | int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id) |
710 | 205k | { |
711 | 205k | const char *name = NULL; |
712 | 205k | int status = 0; |
713 | | |
714 | 205k | switch (p->data_type) { |
715 | 205k | case OSSL_PARAM_UTF8_STRING: |
716 | | /* The OSSL_PARAM functions have no support for this */ |
717 | 205k | name = p->data; |
718 | 205k | status = (name != NULL); |
719 | 205k | break; |
720 | 0 | case OSSL_PARAM_UTF8_PTR: |
721 | 0 | status = OSSL_PARAM_get_utf8_ptr(p, &name); |
722 | 0 | break; |
723 | 205k | } |
724 | 205k | if (status) { |
725 | 205k | int i = ossl_ec_pt_format_name2id(name); |
726 | | |
727 | 205k | if (i >= 0) { |
728 | 205k | *id = i; |
729 | 205k | return 1; |
730 | 205k | } |
731 | 205k | } |
732 | 0 | return 0; |
733 | 205k | } |
734 | | |
735 | | #ifndef FIPS_MODULE |
736 | | int ossl_x509_algor_is_sm2(const X509_ALGOR *palg) |
737 | 641k | { |
738 | 641k | int ptype = 0; |
739 | 641k | const void *pval = NULL; |
740 | | |
741 | 641k | X509_ALGOR_get0(NULL, &ptype, &pval, palg); |
742 | | |
743 | 641k | if (ptype == V_ASN1_OBJECT) |
744 | 598k | return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2; |
745 | | |
746 | 42.9k | if (ptype == V_ASN1_SEQUENCE) { |
747 | 32.3k | const ASN1_STRING *str = pval; |
748 | 32.3k | const unsigned char *der = str->data; |
749 | 32.3k | int derlen = str->length; |
750 | 32.3k | EC_GROUP *group; |
751 | 32.3k | int ret; |
752 | | |
753 | 32.3k | if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL) |
754 | 29.1k | ret = 0; |
755 | 3.20k | else |
756 | 3.20k | ret = (EC_GROUP_get_curve_name(group) == NID_sm2); |
757 | | |
758 | 32.3k | EC_GROUP_free(group); |
759 | 32.3k | return ret; |
760 | 32.3k | } |
761 | | |
762 | 10.5k | return 0; |
763 | 42.9k | } |
764 | | |
765 | | EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg, |
766 | | OSSL_LIB_CTX *libctx, const char *propq) |
767 | 640k | { |
768 | 640k | int ptype = 0; |
769 | 640k | const void *pval = NULL; |
770 | 640k | EC_KEY *eckey = NULL; |
771 | 640k | EC_GROUP *group = NULL; |
772 | | |
773 | 640k | X509_ALGOR_get0(NULL, &ptype, &pval, palg); |
774 | 640k | if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) { |
775 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
776 | 0 | goto ecerr; |
777 | 0 | } |
778 | | |
779 | 640k | if (ptype == V_ASN1_SEQUENCE) { |
780 | 32.6k | const ASN1_STRING *pstr = pval; |
781 | 32.6k | const unsigned char *pm = pstr->data; |
782 | 32.6k | int pmlen = pstr->length; |
783 | | |
784 | 32.6k | if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) { |
785 | 29.2k | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
786 | 29.2k | goto ecerr; |
787 | 29.2k | } |
788 | 607k | } else if (ptype == V_ASN1_OBJECT) { |
789 | 597k | const ASN1_OBJECT *poid = pval; |
790 | | |
791 | | /* |
792 | | * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID |
793 | | */ |
794 | | |
795 | 597k | group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid)); |
796 | 597k | if (group == NULL) |
797 | 39.7k | goto ecerr; |
798 | 557k | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); |
799 | 557k | if (EC_KEY_set_group(eckey, group) == 0) |
800 | 0 | goto ecerr; |
801 | 557k | EC_GROUP_free(group); |
802 | 557k | } else { |
803 | 10.3k | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
804 | 10.3k | goto ecerr; |
805 | 10.3k | } |
806 | | |
807 | 560k | return eckey; |
808 | | |
809 | 79.4k | ecerr: |
810 | 79.4k | EC_KEY_free(eckey); |
811 | 79.4k | EC_GROUP_free(group); |
812 | 79.4k | return NULL; |
813 | 640k | } |
814 | | |
815 | | EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, |
816 | | OSSL_LIB_CTX *libctx, const char *propq) |
817 | 2.19k | { |
818 | 2.19k | const unsigned char *p = NULL; |
819 | 2.19k | int pklen; |
820 | 2.19k | EC_KEY *eckey = NULL; |
821 | 2.19k | const X509_ALGOR *palg; |
822 | | |
823 | 2.19k | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf)) |
824 | 0 | return 0; |
825 | 2.19k | eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq); |
826 | 2.19k | if (eckey == NULL) |
827 | 436 | goto err; |
828 | | |
829 | | /* We have parameters now set private key */ |
830 | 1.76k | if (!d2i_ECPrivateKey(&eckey, &p, pklen)) { |
831 | 756 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
832 | 756 | goto err; |
833 | 756 | } |
834 | | |
835 | 1.00k | return eckey; |
836 | 1.19k | err: |
837 | 1.19k | EC_KEY_free(eckey); |
838 | | return NULL; |
839 | 1.76k | } |
840 | | #endif |