1 /*
2 * Copyright 2001-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 /*
12 * EC_GROUP low level APIs are deprecated for public use, but still ok for
13 * internal use.
14 */
15 #include "internal/deprecated.h"
16
17 #include <string.h>
18 #include <openssl/params.h>
19 #include <openssl/core_names.h>
20 #include <openssl/err.h>
21 #include <openssl/opensslv.h>
22 #include <openssl/param_build.h>
23 #include "crypto/ec.h"
24 #include "crypto/bn.h"
25 #include "internal/nelem.h"
26 #include "ec_local.h"
27
28 /* functions for EC_GROUP objects */
29
ossl_ec_group_new_ex(OSSL_LIB_CTX * libctx,const char * propq,const EC_METHOD * meth)30 EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
31 const EC_METHOD *meth)
32 {
33 EC_GROUP *ret;
34
35 if (meth == NULL) {
36 ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
37 return NULL;
38 }
39 if (meth->group_init == 0) {
40 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
41 return NULL;
42 }
43
44 ret = OPENSSL_zalloc(sizeof(*ret));
45 if (ret == NULL)
46 return NULL;
47
48 ret->libctx = libctx;
49 if (propq != NULL) {
50 ret->propq = OPENSSL_strdup(propq);
51 if (ret->propq == NULL)
52 goto err;
53 }
54 ret->meth = meth;
55 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
56 ret->order = BN_new();
57 if (ret->order == NULL)
58 goto err;
59 ret->cofactor = BN_new();
60 if (ret->cofactor == NULL)
61 goto err;
62 }
63 ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
64 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
65 if (!meth->group_init(ret))
66 goto err;
67 return ret;
68
69 err:
70 BN_free(ret->order);
71 BN_free(ret->cofactor);
72 OPENSSL_free(ret->propq);
73 OPENSSL_free(ret);
74 return NULL;
75 }
76
77 #ifndef OPENSSL_NO_DEPRECATED_3_0
78 #ifndef FIPS_MODULE
EC_GROUP_new(const EC_METHOD * meth)79 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
80 {
81 return ossl_ec_group_new_ex(NULL, NULL, meth);
82 }
83 #endif
84 #endif
85
EC_pre_comp_free(EC_GROUP * group)86 void EC_pre_comp_free(EC_GROUP *group)
87 {
88 switch (group->pre_comp_type) {
89 case PCT_none:
90 break;
91 case PCT_nistz256:
92 #ifdef ECP_NISTZ256_ASM
93 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
94 #endif
95 break;
96 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
97 case PCT_nistp224:
98 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
99 break;
100 case PCT_nistp256:
101 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
102 break;
103 case PCT_nistp384:
104 ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);
105 break;
106 case PCT_nistp521:
107 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
108 break;
109 #else
110 case PCT_nistp224:
111 case PCT_nistp256:
112 case PCT_nistp384:
113 case PCT_nistp521:
114 break;
115 #endif
116 case PCT_ec:
117 EC_ec_pre_comp_free(group->pre_comp.ec);
118 break;
119 }
120 group->pre_comp.ec = NULL;
121 }
122
EC_GROUP_free(EC_GROUP * group)123 void EC_GROUP_free(EC_GROUP *group)
124 {
125 if (!group)
126 return;
127
128 if (group->meth->group_finish != 0)
129 group->meth->group_finish(group);
130
131 EC_pre_comp_free(group);
132 BN_MONT_CTX_free(group->mont_data);
133 EC_POINT_free(group->generator);
134 BN_free(group->order);
135 BN_free(group->cofactor);
136 OPENSSL_free(group->seed);
137 OPENSSL_free(group->propq);
138 OPENSSL_free(group);
139 }
140
141 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_GROUP_clear_free(EC_GROUP * group)142 void EC_GROUP_clear_free(EC_GROUP *group)
143 {
144 if (!group)
145 return;
146
147 if (group->meth->group_clear_finish != 0)
148 group->meth->group_clear_finish(group);
149 else if (group->meth->group_finish != 0)
150 group->meth->group_finish(group);
151
152 EC_pre_comp_free(group);
153 BN_MONT_CTX_free(group->mont_data);
154 EC_POINT_clear_free(group->generator);
155 BN_clear_free(group->order);
156 BN_clear_free(group->cofactor);
157 OPENSSL_clear_free(group->seed, group->seed_len);
158 OPENSSL_clear_free(group, sizeof(*group));
159 }
160 #endif
161
EC_GROUP_copy(EC_GROUP * dest,const EC_GROUP * src)162 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
163 {
164 if (dest->meth->group_copy == 0) {
165 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
166 return 0;
167 }
168 if (dest->meth != src->meth) {
169 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
170 return 0;
171 }
172 if (dest == src)
173 return 1;
174
175 dest->libctx = src->libctx;
176 dest->curve_name = src->curve_name;
177
178 /* Copy precomputed */
179 dest->pre_comp_type = src->pre_comp_type;
180 switch (src->pre_comp_type) {
181 case PCT_none:
182 dest->pre_comp.ec = NULL;
183 break;
184 case PCT_nistz256:
185 #ifdef ECP_NISTZ256_ASM
186 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
187 #endif
188 break;
189 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
190 case PCT_nistp224:
191 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
192 break;
193 case PCT_nistp256:
194 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
195 break;
196 case PCT_nistp384:
197 dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);
198 break;
199 case PCT_nistp521:
200 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
201 break;
202 #else
203 case PCT_nistp224:
204 case PCT_nistp256:
205 case PCT_nistp384:
206 case PCT_nistp521:
207 break;
208 #endif
209 case PCT_ec:
210 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
211 break;
212 }
213
214 if (src->mont_data != NULL) {
215 if (dest->mont_data == NULL) {
216 dest->mont_data = BN_MONT_CTX_new();
217 if (dest->mont_data == NULL)
218 return 0;
219 }
220 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
221 return 0;
222 } else {
223 /* src->generator == NULL */
224 BN_MONT_CTX_free(dest->mont_data);
225 dest->mont_data = NULL;
226 }
227
228 if (src->generator != NULL) {
229 if (dest->generator == NULL) {
230 dest->generator = EC_POINT_new(dest);
231 if (dest->generator == NULL)
232 return 0;
233 }
234 if (!EC_POINT_copy(dest->generator, src->generator))
235 return 0;
236 } else {
237 /* src->generator == NULL */
238 EC_POINT_clear_free(dest->generator);
239 dest->generator = NULL;
240 }
241
242 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
243 if (!BN_copy(dest->order, src->order))
244 return 0;
245 if (!BN_copy(dest->cofactor, src->cofactor))
246 return 0;
247 }
248
249 dest->asn1_flag = src->asn1_flag;
250 dest->asn1_form = src->asn1_form;
251 dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
252
253 if (src->seed) {
254 OPENSSL_free(dest->seed);
255 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)
256 return 0;
257 if (!memcpy(dest->seed, src->seed, src->seed_len))
258 return 0;
259 dest->seed_len = src->seed_len;
260 } else {
261 OPENSSL_free(dest->seed);
262 dest->seed = NULL;
263 dest->seed_len = 0;
264 }
265
266 return dest->meth->group_copy(dest, src);
267 }
268
EC_GROUP_dup(const EC_GROUP * a)269 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
270 {
271 EC_GROUP *t = NULL;
272 int ok = 0;
273
274 if (a == NULL)
275 return NULL;
276
277 if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
278 return NULL;
279 if (!EC_GROUP_copy(t, a))
280 goto err;
281
282 ok = 1;
283
284 err:
285 if (!ok) {
286 EC_GROUP_free(t);
287 return NULL;
288 }
289 return t;
290 }
291
292 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_GROUP_method_of(const EC_GROUP * group)293 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
294 {
295 return group->meth;
296 }
297
EC_METHOD_get_field_type(const EC_METHOD * meth)298 int EC_METHOD_get_field_type(const EC_METHOD *meth)
299 {
300 return meth->field_type;
301 }
302 #endif
303
304 static int ec_precompute_mont_data(EC_GROUP *);
305
306 /*-
307 * Try computing cofactor from the generator order (n) and field cardinality (q).
308 * This works for all curves of cryptographic interest.
309 *
310 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
311 * h_min = (q + 1 - 2*sqrt(q))/n
312 * h_max = (q + 1 + 2*sqrt(q))/n
313 * h_max - h_min = 4*sqrt(q)/n
314 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
315 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
316 *
317 * Otherwise, zero cofactor and return success.
318 */
ec_guess_cofactor(EC_GROUP * group)319 static int ec_guess_cofactor(EC_GROUP *group)
320 {
321 int ret = 0;
322 BN_CTX *ctx = NULL;
323 BIGNUM *q = NULL;
324
325 /*-
326 * If the cofactor is too large, we cannot guess it.
327 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
328 */
329 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
330 /* default to 0 */
331 BN_zero(group->cofactor);
332 /* return success */
333 return 1;
334 }
335
336 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
337 return 0;
338
339 BN_CTX_start(ctx);
340 if ((q = BN_CTX_get(ctx)) == NULL)
341 goto err;
342
343 /* set q = 2**m for binary fields; q = p otherwise */
344 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
345 BN_zero(q);
346 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
347 goto err;
348 } else {
349 if (!BN_copy(q, group->field))
350 goto err;
351 }
352
353 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
354 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
355 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
356 /* q + 1 + n/2 */
357 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
358 /* (q + 1 + n/2)/n */
359 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
360 goto err;
361 ret = 1;
362 err:
363 BN_CTX_end(ctx);
364 BN_CTX_free(ctx);
365 return ret;
366 }
367
EC_GROUP_set_generator(EC_GROUP * group,const EC_POINT * generator,const BIGNUM * order,const BIGNUM * cofactor)368 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
369 const BIGNUM *order, const BIGNUM *cofactor)
370 {
371 if (generator == NULL) {
372 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
373 return 0;
374 }
375
376 /* require group->field >= 1 */
377 if (group->field == NULL || BN_is_zero(group->field)
378 || BN_is_negative(group->field)) {
379 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
380 return 0;
381 }
382
383 /*-
384 * - require order >= 1
385 * - enforce upper bound due to Hasse thm: order can be no more than one bit
386 * longer than field cardinality
387 */
388 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
389 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
390 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
391 return 0;
392 }
393
394 /*-
395 * Unfortunately the cofactor is an optional field in many standards.
396 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
397 * So accept cofactor == NULL or cofactor >= 0.
398 */
399 if (cofactor != NULL && BN_is_negative(cofactor)) {
400 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
401 return 0;
402 }
403
404 if (group->generator == NULL) {
405 group->generator = EC_POINT_new(group);
406 if (group->generator == NULL)
407 return 0;
408 }
409 if (!EC_POINT_copy(group->generator, generator))
410 return 0;
411
412 if (!BN_copy(group->order, order))
413 return 0;
414
415 /* Either take the provided positive cofactor, or try to compute it */
416 if (cofactor != NULL && !BN_is_zero(cofactor)) {
417 if (!BN_copy(group->cofactor, cofactor))
418 return 0;
419 } else if (!ec_guess_cofactor(group)) {
420 BN_zero(group->cofactor);
421 return 0;
422 }
423
424 /*
425 * Some groups have an order with
426 * factors of two, which makes the Montgomery setup fail.
427 * |group->mont_data| will be NULL in this case.
428 */
429 if (BN_is_odd(group->order)) {
430 return ec_precompute_mont_data(group);
431 }
432
433 BN_MONT_CTX_free(group->mont_data);
434 group->mont_data = NULL;
435 return 1;
436 }
437
EC_GROUP_get0_generator(const EC_GROUP * group)438 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
439 {
440 return group->generator;
441 }
442
EC_GROUP_get_mont_data(const EC_GROUP * group)443 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
444 {
445 return group->mont_data;
446 }
447
EC_GROUP_get_order(const EC_GROUP * group,BIGNUM * order,BN_CTX * ctx)448 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
449 {
450 if (group->order == NULL)
451 return 0;
452 if (!BN_copy(order, group->order))
453 return 0;
454
455 return !BN_is_zero(order);
456 }
457
EC_GROUP_get0_order(const EC_GROUP * group)458 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
459 {
460 return group->order;
461 }
462
EC_GROUP_order_bits(const EC_GROUP * group)463 int EC_GROUP_order_bits(const EC_GROUP *group)
464 {
465 return group->meth->group_order_bits(group);
466 }
467
EC_GROUP_get_cofactor(const EC_GROUP * group,BIGNUM * cofactor,BN_CTX * ctx)468 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
469 BN_CTX *ctx)
470 {
471
472 if (group->cofactor == NULL)
473 return 0;
474 if (!BN_copy(cofactor, group->cofactor))
475 return 0;
476
477 return !BN_is_zero(group->cofactor);
478 }
479
EC_GROUP_get0_cofactor(const EC_GROUP * group)480 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
481 {
482 return group->cofactor;
483 }
484
EC_GROUP_set_curve_name(EC_GROUP * group,int nid)485 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
486 {
487 group->curve_name = nid;
488 group->asn1_flag = (nid != NID_undef)
489 ? OPENSSL_EC_NAMED_CURVE
490 : OPENSSL_EC_EXPLICIT_CURVE;
491 }
492
EC_GROUP_get_curve_name(const EC_GROUP * group)493 int EC_GROUP_get_curve_name(const EC_GROUP *group)
494 {
495 return group->curve_name;
496 }
497
EC_GROUP_get0_field(const EC_GROUP * group)498 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
499 {
500 return group->field;
501 }
502
EC_GROUP_get_field_type(const EC_GROUP * group)503 int EC_GROUP_get_field_type(const EC_GROUP *group)
504 {
505 return group->meth->field_type;
506 }
507
EC_GROUP_set_asn1_flag(EC_GROUP * group,int flag)508 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
509 {
510 group->asn1_flag = flag;
511 }
512
EC_GROUP_get_asn1_flag(const EC_GROUP * group)513 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
514 {
515 return group->asn1_flag;
516 }
517
EC_GROUP_set_point_conversion_form(EC_GROUP * group,point_conversion_form_t form)518 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
519 point_conversion_form_t form)
520 {
521 group->asn1_form = form;
522 }
523
EC_GROUP_get_point_conversion_form(const EC_GROUP * group)524 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
525 *group)
526 {
527 return group->asn1_form;
528 }
529
EC_GROUP_set_seed(EC_GROUP * group,const unsigned char * p,size_t len)530 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
531 {
532 OPENSSL_free(group->seed);
533 group->seed = NULL;
534 group->seed_len = 0;
535
536 if (!len || !p)
537 return 1;
538
539 if ((group->seed = OPENSSL_malloc(len)) == NULL)
540 return 0;
541 memcpy(group->seed, p, len);
542 group->seed_len = len;
543
544 return len;
545 }
546
EC_GROUP_get0_seed(const EC_GROUP * group)547 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
548 {
549 return group->seed;
550 }
551
EC_GROUP_get_seed_len(const EC_GROUP * group)552 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
553 {
554 return group->seed_len;
555 }
556
EC_GROUP_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)557 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
558 const BIGNUM *b, BN_CTX *ctx)
559 {
560 if (group->meth->group_set_curve == 0) {
561 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
562 return 0;
563 }
564 return group->meth->group_set_curve(group, p, a, b, ctx);
565 }
566
EC_GROUP_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)567 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
568 BN_CTX *ctx)
569 {
570 if (group->meth->group_get_curve == NULL) {
571 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
572 return 0;
573 }
574 return group->meth->group_get_curve(group, p, a, b, ctx);
575 }
576
577 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_GROUP_set_curve_GFp(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)578 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
579 const BIGNUM *b, BN_CTX *ctx)
580 {
581 return EC_GROUP_set_curve(group, p, a, b, ctx);
582 }
583
EC_GROUP_get_curve_GFp(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)584 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
585 BIGNUM *b, BN_CTX *ctx)
586 {
587 return EC_GROUP_get_curve(group, p, a, b, ctx);
588 }
589
590 #ifndef OPENSSL_NO_EC2M
EC_GROUP_set_curve_GF2m(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)591 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
592 const BIGNUM *b, BN_CTX *ctx)
593 {
594 return EC_GROUP_set_curve(group, p, a, b, ctx);
595 }
596
EC_GROUP_get_curve_GF2m(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)597 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
598 BIGNUM *b, BN_CTX *ctx)
599 {
600 return EC_GROUP_get_curve(group, p, a, b, ctx);
601 }
602 #endif
603 #endif
604
EC_GROUP_get_degree(const EC_GROUP * group)605 int EC_GROUP_get_degree(const EC_GROUP *group)
606 {
607 if (group->meth->group_get_degree == 0) {
608 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
609 return 0;
610 }
611 return group->meth->group_get_degree(group);
612 }
613
EC_GROUP_check_discriminant(const EC_GROUP * group,BN_CTX * ctx)614 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
615 {
616 if (group->meth->group_check_discriminant == 0) {
617 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
618 return 0;
619 }
620 return group->meth->group_check_discriminant(group, ctx);
621 }
622
EC_GROUP_cmp(const EC_GROUP * a,const EC_GROUP * b,BN_CTX * ctx)623 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
624 {
625 int r = 0;
626 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
627 #ifndef FIPS_MODULE
628 BN_CTX *ctx_new = NULL;
629 #endif
630
631 /* compare the field types */
632 if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
633 return 1;
634 /* compare the curve name (if present in both) */
635 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
636 return 1;
637 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
638 return 0;
639
640 #ifndef FIPS_MODULE
641 if (ctx == NULL)
642 ctx_new = ctx = BN_CTX_new();
643 #endif
644 if (ctx == NULL)
645 return -1;
646
647 BN_CTX_start(ctx);
648 a1 = BN_CTX_get(ctx);
649 a2 = BN_CTX_get(ctx);
650 a3 = BN_CTX_get(ctx);
651 b1 = BN_CTX_get(ctx);
652 b2 = BN_CTX_get(ctx);
653 b3 = BN_CTX_get(ctx);
654 if (b3 == NULL) {
655 BN_CTX_end(ctx);
656 #ifndef FIPS_MODULE
657 BN_CTX_free(ctx_new);
658 #endif
659 return -1;
660 }
661
662 /*
663 * XXX This approach assumes that the external representation of curves
664 * over the same field type is the same.
665 */
666 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || !b->meth->group_get_curve(b, b1, b2, b3, ctx))
667 r = 1;
668
669 /* return 1 if the curve parameters are different */
670 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
671 r = 1;
672
673 /* XXX EC_POINT_cmp() assumes that the methods are equal */
674 /* return 1 if the generators are different */
675 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), EC_GROUP_get0_generator(b), ctx) != 0)
676 r = 1;
677
678 if (!r) {
679 const BIGNUM *ao, *bo, *ac, *bc;
680 /* compare the orders */
681 ao = EC_GROUP_get0_order(a);
682 bo = EC_GROUP_get0_order(b);
683 if (ao == NULL || bo == NULL) {
684 /* return an error if either order is NULL */
685 r = -1;
686 goto end;
687 }
688 if (BN_cmp(ao, bo) != 0) {
689 /* return 1 if orders are different */
690 r = 1;
691 goto end;
692 }
693 /*
694 * It gets here if the curve parameters and generator matched.
695 * Now check the optional cofactors (if both are present).
696 */
697 ac = EC_GROUP_get0_cofactor(a);
698 bc = EC_GROUP_get0_cofactor(b);
699 /* Returns 1 (mismatch) if both cofactors are specified and different */
700 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
701 r = 1;
702 /* Returns 0 if the parameters matched */
703 }
704 end:
705 BN_CTX_end(ctx);
706 #ifndef FIPS_MODULE
707 BN_CTX_free(ctx_new);
708 #endif
709 return r;
710 }
711
712 /* functions for EC_POINT objects */
713
EC_POINT_new(const EC_GROUP * group)714 EC_POINT *EC_POINT_new(const EC_GROUP *group)
715 {
716 EC_POINT *ret;
717
718 if (group == NULL) {
719 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
720 return NULL;
721 }
722 if (group->meth->point_init == NULL) {
723 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
724 return NULL;
725 }
726
727 ret = OPENSSL_zalloc(sizeof(*ret));
728 if (ret == NULL)
729 return NULL;
730
731 ret->meth = group->meth;
732 ret->curve_name = group->curve_name;
733
734 if (!ret->meth->point_init(ret)) {
735 OPENSSL_free(ret);
736 return NULL;
737 }
738
739 return ret;
740 }
741
EC_POINT_free(EC_POINT * point)742 void EC_POINT_free(EC_POINT *point)
743 {
744 if (point == NULL)
745 return;
746
747 #ifdef OPENSSL_PEDANTIC_ZEROIZATION
748 EC_POINT_clear_free(point);
749 #else
750 if (point->meth->point_finish != 0)
751 point->meth->point_finish(point);
752 OPENSSL_free(point);
753 #endif
754 }
755
EC_POINT_clear_free(EC_POINT * point)756 void EC_POINT_clear_free(EC_POINT *point)
757 {
758 if (point == NULL)
759 return;
760
761 if (point->meth->point_clear_finish != 0)
762 point->meth->point_clear_finish(point);
763 else if (point->meth->point_finish != 0)
764 point->meth->point_finish(point);
765 OPENSSL_clear_free(point, sizeof(*point));
766 }
767
EC_POINT_copy(EC_POINT * dest,const EC_POINT * src)768 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
769 {
770 if (dest->meth->point_copy == 0) {
771 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
772 return 0;
773 }
774 if (dest->meth != src->meth
775 || (dest->curve_name != src->curve_name
776 && dest->curve_name != 0
777 && src->curve_name != 0)) {
778 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
779 return 0;
780 }
781 if (dest == src)
782 return 1;
783 return dest->meth->point_copy(dest, src);
784 }
785
EC_POINT_dup(const EC_POINT * a,const EC_GROUP * group)786 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
787 {
788 EC_POINT *t;
789 int r;
790
791 if (a == NULL)
792 return NULL;
793
794 t = EC_POINT_new(group);
795 if (t == NULL)
796 return NULL;
797 r = EC_POINT_copy(t, a);
798 if (!r) {
799 EC_POINT_free(t);
800 return NULL;
801 }
802 return t;
803 }
804
805 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_method_of(const EC_POINT * point)806 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
807 {
808 return point->meth;
809 }
810 #endif
811
EC_POINT_set_to_infinity(const EC_GROUP * group,EC_POINT * point)812 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
813 {
814 if (group->meth->point_set_to_infinity == 0) {
815 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
816 return 0;
817 }
818 if (group->meth != point->meth) {
819 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
820 return 0;
821 }
822 return group->meth->point_set_to_infinity(group, point);
823 }
824
825 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,const BIGNUM * z,BN_CTX * ctx)826 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
827 EC_POINT *point, const BIGNUM *x,
828 const BIGNUM *y, const BIGNUM *z,
829 BN_CTX *ctx)
830 {
831 if (group->meth->field_type != NID_X9_62_prime_field) {
832 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
833 return 0;
834 }
835 if (!ec_point_is_compat(point, group)) {
836 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
837 return 0;
838 }
839 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
840 x, y, z, ctx);
841 }
842
EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BIGNUM * z,BN_CTX * ctx)843 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
844 const EC_POINT *point, BIGNUM *x,
845 BIGNUM *y, BIGNUM *z,
846 BN_CTX *ctx)
847 {
848 if (group->meth->field_type != NID_X9_62_prime_field) {
849 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
850 return 0;
851 }
852 if (!ec_point_is_compat(point, group)) {
853 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
854 return 0;
855 }
856 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
857 x, y, z, ctx);
858 }
859 #endif
860
EC_POINT_set_affine_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)861 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
862 const BIGNUM *x, const BIGNUM *y,
863 BN_CTX *ctx)
864 {
865 if (group->meth->point_set_affine_coordinates == NULL) {
866 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
867 return 0;
868 }
869 if (!ec_point_is_compat(point, group)) {
870 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
871 return 0;
872 }
873 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
874 return 0;
875
876 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
877 ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
878 return 0;
879 }
880 return 1;
881 }
882
883 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_set_affine_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)884 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
885 EC_POINT *point, const BIGNUM *x,
886 const BIGNUM *y, BN_CTX *ctx)
887 {
888 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
889 }
890
891 #ifndef OPENSSL_NO_EC2M
EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)892 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
893 EC_POINT *point, const BIGNUM *x,
894 const BIGNUM *y, BN_CTX *ctx)
895 {
896 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
897 }
898 #endif
899 #endif
900
EC_POINT_get_affine_coordinates(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)901 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
902 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
903 BN_CTX *ctx)
904 {
905 if (group->meth->point_get_affine_coordinates == NULL) {
906 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
907 return 0;
908 }
909 if (!ec_point_is_compat(point, group)) {
910 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
911 return 0;
912 }
913 if (EC_POINT_is_at_infinity(group, point)) {
914 ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
915 return 0;
916 }
917 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
918 }
919
920 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_get_affine_coordinates_GFp(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)921 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
922 const EC_POINT *point, BIGNUM *x,
923 BIGNUM *y, BN_CTX *ctx)
924 {
925 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
926 }
927
928 #ifndef OPENSSL_NO_EC2M
EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)929 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
930 const EC_POINT *point, BIGNUM *x,
931 BIGNUM *y, BN_CTX *ctx)
932 {
933 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
934 }
935 #endif
936 #endif
937
EC_POINT_add(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)938 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
939 const EC_POINT *b, BN_CTX *ctx)
940 {
941 if (group->meth->add == 0) {
942 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
943 return 0;
944 }
945 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
946 || !ec_point_is_compat(b, group)) {
947 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
948 return 0;
949 }
950 return group->meth->add(group, r, a, b, ctx);
951 }
952
EC_POINT_dbl(const EC_GROUP * group,EC_POINT * r,const EC_POINT * a,BN_CTX * ctx)953 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
954 BN_CTX *ctx)
955 {
956 if (group->meth->dbl == 0) {
957 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
958 return 0;
959 }
960 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
961 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
962 return 0;
963 }
964 return group->meth->dbl(group, r, a, ctx);
965 }
966
EC_POINT_invert(const EC_GROUP * group,EC_POINT * a,BN_CTX * ctx)967 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
968 {
969 if (group->meth->invert == 0) {
970 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
971 return 0;
972 }
973 if (!ec_point_is_compat(a, group)) {
974 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
975 return 0;
976 }
977 return group->meth->invert(group, a, ctx);
978 }
979
EC_POINT_is_at_infinity(const EC_GROUP * group,const EC_POINT * point)980 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
981 {
982 if (group->meth->is_at_infinity == 0) {
983 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
984 return 0;
985 }
986 if (!ec_point_is_compat(point, group)) {
987 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
988 return 0;
989 }
990 return group->meth->is_at_infinity(group, point);
991 }
992
993 /*
994 * Check whether an EC_POINT is on the curve or not. Note that the return
995 * value for this function should NOT be treated as a boolean. Return values:
996 * 1: The point is on the curve
997 * 0: The point is not on the curve
998 * -1: An error occurred
999 */
EC_POINT_is_on_curve(const EC_GROUP * group,const EC_POINT * point,BN_CTX * ctx)1000 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1001 BN_CTX *ctx)
1002 {
1003 if (group->meth->is_on_curve == 0) {
1004 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1005 return 0;
1006 }
1007 if (!ec_point_is_compat(point, group)) {
1008 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1009 return 0;
1010 }
1011 return group->meth->is_on_curve(group, point, ctx);
1012 }
1013
EC_POINT_cmp(const EC_GROUP * group,const EC_POINT * a,const EC_POINT * b,BN_CTX * ctx)1014 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1015 BN_CTX *ctx)
1016 {
1017 if (group->meth->point_cmp == 0) {
1018 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1019 return -1;
1020 }
1021 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1022 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1023 return -1;
1024 }
1025 return group->meth->point_cmp(group, a, b, ctx);
1026 }
1027
1028 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_make_affine(const EC_GROUP * group,EC_POINT * point,BN_CTX * ctx)1029 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1030 {
1031 if (group->meth->make_affine == 0) {
1032 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1033 return 0;
1034 }
1035 if (!ec_point_is_compat(point, group)) {
1036 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1037 return 0;
1038 }
1039 return group->meth->make_affine(group, point, ctx);
1040 }
1041
EC_POINTs_make_affine(const EC_GROUP * group,size_t num,EC_POINT * points[],BN_CTX * ctx)1042 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1043 EC_POINT *points[], BN_CTX *ctx)
1044 {
1045 size_t i;
1046
1047 if (group->meth->points_make_affine == 0) {
1048 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1049 return 0;
1050 }
1051 for (i = 0; i < num; i++) {
1052 if (!ec_point_is_compat(points[i], group)) {
1053 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1054 return 0;
1055 }
1056 }
1057 return group->meth->points_make_affine(group, num, points, ctx);
1058 }
1059 #endif
1060
1061 /*
1062 * Functions for point multiplication. If group->meth->mul is 0, we use the
1063 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1064 * methods.
1065 */
1066
1067 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINTs_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * scalar,size_t num,const EC_POINT * points[],const BIGNUM * scalars[],BN_CTX * ctx)1068 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1069 size_t num, const EC_POINT *points[],
1070 const BIGNUM *scalars[], BN_CTX *ctx)
1071 {
1072 int ret = 0;
1073 size_t i = 0;
1074 #ifndef FIPS_MODULE
1075 BN_CTX *new_ctx = NULL;
1076 #endif
1077
1078 if (!ec_point_is_compat(r, group)) {
1079 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1080 return 0;
1081 }
1082
1083 if (scalar == NULL && num == 0)
1084 return EC_POINT_set_to_infinity(group, r);
1085
1086 for (i = 0; i < num; i++) {
1087 if (!ec_point_is_compat(points[i], group)) {
1088 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1089 return 0;
1090 }
1091 }
1092
1093 #ifndef FIPS_MODULE
1094 if (ctx == NULL)
1095 ctx = new_ctx = BN_CTX_secure_new();
1096 #endif
1097 if (ctx == NULL) {
1098 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1099 return 0;
1100 }
1101
1102 if (group->meth->mul != NULL)
1103 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1104 else
1105 /* use default */
1106 ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1107
1108 #ifndef FIPS_MODULE
1109 BN_CTX_free(new_ctx);
1110 #endif
1111 return ret;
1112 }
1113 #endif
1114
EC_POINT_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * g_scalar,const EC_POINT * point,const BIGNUM * p_scalar,BN_CTX * ctx)1115 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1116 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1117 {
1118 int ret = 0;
1119 size_t num;
1120 #ifndef FIPS_MODULE
1121 BN_CTX *new_ctx = NULL;
1122 #endif
1123
1124 if (!ec_point_is_compat(r, group)
1125 || (point != NULL && !ec_point_is_compat(point, group))) {
1126 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1127 return 0;
1128 }
1129
1130 if (g_scalar == NULL && p_scalar == NULL)
1131 return EC_POINT_set_to_infinity(group, r);
1132
1133 #ifndef FIPS_MODULE
1134 if (ctx == NULL)
1135 ctx = new_ctx = BN_CTX_secure_new();
1136 #endif
1137 if (ctx == NULL) {
1138 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
1139 return 0;
1140 }
1141
1142 num = (point != NULL && p_scalar != NULL) ? 1 : 0;
1143 if (group->meth->mul != NULL)
1144 ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1145 else
1146 /* use default */
1147 ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1148
1149 #ifndef FIPS_MODULE
1150 BN_CTX_free(new_ctx);
1151 #endif
1152 return ret;
1153 }
1154
1155 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_GROUP_precompute_mult(EC_GROUP * group,BN_CTX * ctx)1156 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1157 {
1158 if (group->meth->mul == 0)
1159 /* use default */
1160 return ossl_ec_wNAF_precompute_mult(group, ctx);
1161
1162 if (group->meth->precompute_mult != 0)
1163 return group->meth->precompute_mult(group, ctx);
1164 else
1165 return 1; /* nothing to do, so report success */
1166 }
1167
EC_GROUP_have_precompute_mult(const EC_GROUP * group)1168 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1169 {
1170 if (group->meth->mul == 0)
1171 /* use default */
1172 return ossl_ec_wNAF_have_precompute_mult(group);
1173
1174 if (group->meth->have_precompute_mult != 0)
1175 return group->meth->have_precompute_mult(group);
1176 else
1177 return 0; /* cannot tell whether precomputation has
1178 * been performed */
1179 }
1180 #endif
1181
1182 /*
1183 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1184 * returns one on success. On error it returns zero.
1185 */
ec_precompute_mont_data(EC_GROUP * group)1186 static int ec_precompute_mont_data(EC_GROUP *group)
1187 {
1188 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1189 int ret = 0;
1190
1191 BN_MONT_CTX_free(group->mont_data);
1192 group->mont_data = NULL;
1193
1194 if (ctx == NULL)
1195 goto err;
1196
1197 group->mont_data = BN_MONT_CTX_new();
1198 if (group->mont_data == NULL)
1199 goto err;
1200
1201 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1202 BN_MONT_CTX_free(group->mont_data);
1203 group->mont_data = NULL;
1204 goto err;
1205 }
1206
1207 ret = 1;
1208
1209 err:
1210
1211 BN_CTX_free(ctx);
1212 return ret;
1213 }
1214
1215 #ifndef FIPS_MODULE
EC_KEY_set_ex_data(EC_KEY * key,int idx,void * arg)1216 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1217 {
1218 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1219 }
1220
EC_KEY_get_ex_data(const EC_KEY * key,int idx)1221 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1222 {
1223 return CRYPTO_get_ex_data(&key->ex_data, idx);
1224 }
1225 #endif
1226
ossl_ec_group_simple_order_bits(const EC_GROUP * group)1227 int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
1228 {
1229 if (group->order == NULL)
1230 return 0;
1231 return BN_num_bits(group->order);
1232 }
1233
ec_field_inverse_mod_ord(const EC_GROUP * group,BIGNUM * r,const BIGNUM * x,BN_CTX * ctx)1234 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1235 const BIGNUM *x, BN_CTX *ctx)
1236 {
1237 BIGNUM *e = NULL;
1238 int ret = 0;
1239 #ifndef FIPS_MODULE
1240 BN_CTX *new_ctx = NULL;
1241 #endif
1242
1243 if (group->mont_data == NULL)
1244 return 0;
1245
1246 #ifndef FIPS_MODULE
1247 if (ctx == NULL)
1248 ctx = new_ctx = BN_CTX_secure_new();
1249 #endif
1250 if (ctx == NULL)
1251 return 0;
1252
1253 BN_CTX_start(ctx);
1254 if ((e = BN_CTX_get(ctx)) == NULL)
1255 goto err;
1256
1257 /*-
1258 * We want inverse in constant time, therefore we utilize the fact
1259 * order must be prime and use Fermats Little Theorem instead.
1260 */
1261 if (!BN_set_word(e, 2))
1262 goto err;
1263 if (!BN_sub(e, group->order, e))
1264 goto err;
1265 /*-
1266 * Although the exponent is public we want the result to be
1267 * fixed top.
1268 */
1269 if (!bn_mod_exp_mont_fixed_top(r, x, e, group->order, ctx, group->mont_data))
1270 goto err;
1271
1272 ret = 1;
1273
1274 err:
1275 BN_CTX_end(ctx);
1276 #ifndef FIPS_MODULE
1277 BN_CTX_free(new_ctx);
1278 #endif
1279 return ret;
1280 }
1281
1282 /*-
1283 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1284 * - When group->order is even, this function returns an error.
1285 * - When group->order is otherwise composite, the correctness
1286 * of the output is not guaranteed.
1287 * - When x is outside the range [1, group->order), the correctness
1288 * of the output is not guaranteed.
1289 * - Otherwise, this function returns the multiplicative inverse in the
1290 * range [1, group->order).
1291 *
1292 * EC_METHODs must implement their own field_inverse_mod_ord for
1293 * other functionality.
1294 */
ossl_ec_group_do_inverse_ord(const EC_GROUP * group,BIGNUM * res,const BIGNUM * x,BN_CTX * ctx)1295 int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1296 const BIGNUM *x, BN_CTX *ctx)
1297 {
1298 if (group->meth->field_inverse_mod_ord != NULL)
1299 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1300 else
1301 return ec_field_inverse_mod_ord(group, res, x, ctx);
1302 }
1303
1304 /*-
1305 * Coordinate blinding for EC_POINT.
1306 *
1307 * The underlying EC_METHOD can optionally implement this function:
1308 * underlying implementations should return 0 on errors, or 1 on
1309 * success.
1310 *
1311 * This wrapper returns 1 in case the underlying EC_METHOD does not
1312 * support coordinate blinding.
1313 */
ossl_ec_point_blind_coordinates(const EC_GROUP * group,EC_POINT * p,BN_CTX * ctx)1314 int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1315 BN_CTX *ctx)
1316 {
1317 if (group->meth->blind_coordinates == NULL)
1318 return 1; /* ignore if not implemented */
1319
1320 return group->meth->blind_coordinates(group, p, ctx);
1321 }
1322
EC_GROUP_get_basis_type(const EC_GROUP * group)1323 int EC_GROUP_get_basis_type(const EC_GROUP *group)
1324 {
1325 int i;
1326
1327 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
1328 /* everything else is currently not supported */
1329 return 0;
1330
1331 /* Find the last non-zero element of group->poly[] */
1332 for (i = 0;
1333 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
1334 i++)
1335 continue;
1336
1337 if (i == 4)
1338 return NID_X9_62_ppBasis;
1339 else if (i == 2)
1340 return NID_X9_62_tpBasis;
1341 else
1342 /* everything else is currently not supported */
1343 return 0;
1344 }
1345
1346 #ifndef OPENSSL_NO_EC2M
EC_GROUP_get_trinomial_basis(const EC_GROUP * group,unsigned int * k)1347 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
1348 {
1349 if (group == NULL)
1350 return 0;
1351
1352 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1353 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1354 && (group->poly[2] == 0))) {
1355 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1356 return 0;
1357 }
1358
1359 if (k)
1360 *k = group->poly[1];
1361
1362 return 1;
1363 }
1364
EC_GROUP_get_pentanomial_basis(const EC_GROUP * group,unsigned int * k1,unsigned int * k2,unsigned int * k3)1365 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1366 unsigned int *k2, unsigned int *k3)
1367 {
1368 if (group == NULL)
1369 return 0;
1370
1371 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1372 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1373 && (group->poly[2] != 0) && (group->poly[3] != 0)
1374 && (group->poly[4] == 0))) {
1375 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1376 return 0;
1377 }
1378
1379 if (k1)
1380 *k1 = group->poly[3];
1381 if (k2)
1382 *k2 = group->poly[2];
1383 if (k3)
1384 *k3 = group->poly[1];
1385
1386 return 1;
1387 }
1388 #endif
1389
1390 #ifndef FIPS_MODULE
1391 /*
1392 * Check if the explicit parameters group matches any built-in curves.
1393 *
1394 * We create a copy of the group just built, so that we can remove optional
1395 * fields for the lookup: we do this to avoid the possibility that one of
1396 * the optional parameters is used to force the library into using a less
1397 * performant and less secure EC_METHOD instead of the specialized one.
1398 * In any case, `seed` is not really used in any computation, while a
1399 * cofactor different from the one in the built-in table is just
1400 * mathematically wrong anyway and should not be used.
1401 */
ec_group_explicit_to_named(const EC_GROUP * group,OSSL_LIB_CTX * libctx,const char * propq,BN_CTX * ctx)1402 static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1403 OSSL_LIB_CTX *libctx,
1404 const char *propq,
1405 BN_CTX *ctx)
1406 {
1407 EC_GROUP *ret_group = NULL, *dup = NULL;
1408 int curve_name_nid;
1409
1410 const EC_POINT *point = EC_GROUP_get0_generator(group);
1411 const BIGNUM *order = EC_GROUP_get0_order(group);
1412 int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1413
1414 if ((dup = EC_GROUP_dup(group)) == NULL
1415 || EC_GROUP_set_seed(dup, NULL, 0) != 1
1416 || !EC_GROUP_set_generator(dup, point, order, NULL))
1417 goto err;
1418 if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
1419 /*
1420 * The input explicit parameters successfully matched one of the
1421 * built-in curves: often for built-in curves we have specialized
1422 * methods with better performance and hardening.
1423 *
1424 * In this case we replace the `EC_GROUP` created through explicit
1425 * parameters with one created from a named group.
1426 */
1427
1428 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1429 /*
1430 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1431 * the same curve, we prefer the SECP nid when matching explicit
1432 * parameters as that is associated with a specialized EC_METHOD.
1433 */
1434 if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1435 curve_name_nid = NID_secp224r1;
1436 #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1437
1438 ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
1439 if (ret_group == NULL)
1440 goto err;
1441
1442 /*
1443 * Set the flag so that EC_GROUPs created from explicit parameters are
1444 * serialized using explicit parameters by default.
1445 */
1446 EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1447
1448 /*
1449 * If the input params do not contain the optional seed field we make
1450 * sure it is not added to the returned group.
1451 *
1452 * The seed field is not really used inside libcrypto anyway, and
1453 * adding it to parsed explicit parameter keys would alter their DER
1454 * encoding output (because of the extra field) which could impact
1455 * applications fingerprinting keys by their DER encoding.
1456 */
1457 if (no_seed) {
1458 if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1459 goto err;
1460 }
1461 } else {
1462 ret_group = (EC_GROUP *)group;
1463 }
1464 EC_GROUP_free(dup);
1465 return ret_group;
1466 err:
1467 EC_GROUP_free(dup);
1468 EC_GROUP_free(ret_group);
1469 return NULL;
1470 }
1471 #endif /* FIPS_MODULE */
1472
group_new_from_name(const OSSL_PARAM * p,OSSL_LIB_CTX * libctx,const char * propq)1473 static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
1474 OSSL_LIB_CTX *libctx, const char *propq)
1475 {
1476 int ok = 0, nid;
1477 const char *curve_name = NULL;
1478
1479 switch (p->data_type) {
1480 case OSSL_PARAM_UTF8_STRING:
1481 /* The OSSL_PARAM functions have no support for this */
1482 curve_name = p->data;
1483 ok = (curve_name != NULL);
1484 break;
1485 case OSSL_PARAM_UTF8_PTR:
1486 ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1487 break;
1488 }
1489
1490 if (ok) {
1491 nid = ossl_ec_curve_name2nid(curve_name);
1492 if (nid == NID_undef) {
1493 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
1494 return NULL;
1495 } else {
1496 return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
1497 }
1498 }
1499 return NULL;
1500 }
1501
1502 /* These parameters can be set directly into an EC_GROUP */
ossl_ec_group_set_params(EC_GROUP * group,const OSSL_PARAM params[])1503 int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
1504 {
1505 int encoding_flag = -1, format = -1;
1506 const OSSL_PARAM *p;
1507
1508 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
1509 if (p != NULL) {
1510 if (!ossl_ec_pt_format_param2id(p, &format)) {
1511 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1512 return 0;
1513 }
1514 EC_GROUP_set_point_conversion_form(group, format);
1515 }
1516
1517 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1518 if (p != NULL) {
1519 if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
1520 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
1521 return 0;
1522 }
1523 EC_GROUP_set_asn1_flag(group, encoding_flag);
1524 }
1525 /* Optional seed */
1526 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1527 if (p != NULL) {
1528 /* The seed is allowed to be NULL */
1529 if (p->data_type != OSSL_PARAM_OCTET_STRING
1530 || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
1531 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1532 return 0;
1533 }
1534 }
1535 return 1;
1536 }
1537
EC_GROUP_new_from_params(const OSSL_PARAM params[],OSSL_LIB_CTX * libctx,const char * propq)1538 EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
1539 OSSL_LIB_CTX *libctx, const char *propq)
1540 {
1541 const OSSL_PARAM *ptmp;
1542 EC_GROUP *group = NULL;
1543
1544 #ifndef FIPS_MODULE
1545 const OSSL_PARAM *pa, *pb;
1546 int ok = 0;
1547 EC_GROUP *named_group = NULL;
1548 BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1549 EC_POINT *point = NULL;
1550 int field_bits = 0;
1551 int is_prime_field = 1;
1552 BN_CTX *bnctx = NULL;
1553 const unsigned char *buf = NULL;
1554 int encoding_flag = -1;
1555 #endif
1556
1557 /* This is the simple named group case */
1558 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1559 if (ptmp != NULL) {
1560 int decoded = 0;
1561
1562 if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
1563 return NULL;
1564 if (!ossl_ec_group_set_params(group, params)) {
1565 EC_GROUP_free(group);
1566 return NULL;
1567 }
1568
1569 ptmp = OSSL_PARAM_locate_const(params,
1570 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
1571 if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
1572 ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
1573 EC_GROUP_free(group);
1574 return NULL;
1575 }
1576 group->decoded_from_explicit_params = decoded > 0;
1577 return group;
1578 }
1579 #ifdef FIPS_MODULE
1580 ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
1581 return NULL;
1582 #else
1583 /* If it gets here then we are trying explicit parameters */
1584 bnctx = BN_CTX_new_ex(libctx);
1585 if (bnctx == NULL) {
1586 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1587 return 0;
1588 }
1589 BN_CTX_start(bnctx);
1590
1591 p = BN_CTX_get(bnctx);
1592 a = BN_CTX_get(bnctx);
1593 b = BN_CTX_get(bnctx);
1594 order = BN_CTX_get(bnctx);
1595 if (order == NULL) {
1596 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1597 goto err;
1598 }
1599
1600 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1601 if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
1602 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
1603 goto err;
1604 }
1605 if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
1606 is_prime_field = 1;
1607 } else if (OPENSSL_strcasecmp(ptmp->data,
1608 SN_X9_62_characteristic_two_field)
1609 == 0) {
1610 is_prime_field = 0;
1611 } else {
1612 /* Invalid field */
1613 ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
1614 goto err;
1615 }
1616
1617 pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1618 if (!OSSL_PARAM_get_BN(pa, &a)) {
1619 ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
1620 goto err;
1621 }
1622 pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1623 if (!OSSL_PARAM_get_BN(pb, &b)) {
1624 ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
1625 goto err;
1626 }
1627
1628 /* extract the prime number or irreducible polynomial */
1629 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1630 if (!OSSL_PARAM_get_BN(ptmp, &p)) {
1631 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1632 goto err;
1633 }
1634
1635 if (is_prime_field) {
1636 if (BN_is_negative(p) || BN_is_zero(p)) {
1637 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
1638 goto err;
1639 }
1640 field_bits = BN_num_bits(p);
1641 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1642 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1643 goto err;
1644 }
1645
1646 /* create the EC_GROUP structure */
1647 group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1648 } else {
1649 #ifdef OPENSSL_NO_EC2M
1650 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
1651 goto err;
1652 #else
1653 /* create the EC_GROUP structure */
1654 group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1655 if (group != NULL) {
1656 field_bits = EC_GROUP_get_degree(group);
1657 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1658 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
1659 goto err;
1660 }
1661 }
1662 #endif /* OPENSSL_NO_EC2M */
1663 }
1664
1665 if (group == NULL) {
1666 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1667 goto err;
1668 }
1669
1670 /* Optional seed */
1671 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1672 if (ptmp != NULL) {
1673 if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1674 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
1675 goto err;
1676 }
1677 if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1678 goto err;
1679 }
1680
1681 /* generator base point */
1682 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1683 if (ptmp == NULL
1684 || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
1685 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1686 goto err;
1687 }
1688 buf = (const unsigned char *)(ptmp->data);
1689 if ((point = EC_POINT_new(group)) == NULL)
1690 goto err;
1691 EC_GROUP_set_point_conversion_form(group,
1692 (point_conversion_form_t)buf[0] & ~0x01);
1693 if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1694 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1695 goto err;
1696 }
1697
1698 /* order */
1699 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1700 if (!OSSL_PARAM_get_BN(ptmp, &order)
1701 || (BN_is_negative(order) || BN_is_zero(order))
1702 || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
1703 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
1704 goto err;
1705 }
1706
1707 /* Optional cofactor */
1708 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1709 if (ptmp != NULL) {
1710 cofactor = BN_CTX_get(bnctx);
1711 if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
1712 ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
1713 goto err;
1714 }
1715 }
1716
1717 /* set the generator, order and cofactor (if present) */
1718 if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1719 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
1720 goto err;
1721 }
1722
1723 named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1724 if (named_group == NULL) {
1725 ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
1726 goto err;
1727 }
1728 if (named_group == group) {
1729 /*
1730 * If we did not find a named group then the encoding should be explicit
1731 * if it was specified
1732 */
1733 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1734 if (ptmp != NULL
1735 && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
1736 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1737 goto err;
1738 }
1739 if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
1740 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1741 goto err;
1742 }
1743 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1744 } else {
1745 EC_GROUP_free(group);
1746 group = named_group;
1747 }
1748 /* We've imported the group from explicit parameters, set it so. */
1749 group->decoded_from_explicit_params = 1;
1750 ok = 1;
1751 err:
1752 if (!ok) {
1753 EC_GROUP_free(group);
1754 group = NULL;
1755 }
1756 EC_POINT_free(point);
1757 BN_CTX_end(bnctx);
1758 BN_CTX_free(bnctx);
1759
1760 return group;
1761 #endif /* FIPS_MODULE */
1762 }
1763
EC_GROUP_to_params(const EC_GROUP * group,OSSL_LIB_CTX * libctx,const char * propq,BN_CTX * bnctx)1764 OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
1765 const char *propq, BN_CTX *bnctx)
1766 {
1767 OSSL_PARAM_BLD *tmpl = NULL;
1768 BN_CTX *new_bnctx = NULL;
1769 unsigned char *gen_buf = NULL;
1770 OSSL_PARAM *params = NULL;
1771
1772 if (group == NULL)
1773 goto err;
1774
1775 tmpl = OSSL_PARAM_BLD_new();
1776 if (tmpl == NULL)
1777 goto err;
1778
1779 if (bnctx == NULL)
1780 bnctx = new_bnctx = BN_CTX_new_ex(libctx);
1781 if (bnctx == NULL)
1782 goto err;
1783 BN_CTX_start(bnctx);
1784
1785 if (!ossl_ec_group_todata(
1786 group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
1787 goto err;
1788
1789 params = OSSL_PARAM_BLD_to_param(tmpl);
1790
1791 err:
1792 OSSL_PARAM_BLD_free(tmpl);
1793 OPENSSL_free(gen_buf);
1794 BN_CTX_end(bnctx);
1795 BN_CTX_free(new_bnctx);
1796 return params;
1797 }
1798