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