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