]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_lib.c
d30504de65558c105eef2885138ac493a86b97ea
[thirdparty/openssl.git] / crypto / ec / ec_lib.c
1 /*
2 * Copyright 2001-2018 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 #include <string.h>
12
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
15
16 #include "ec_lcl.h"
17
18 /* functions for EC_GROUP objects */
19
20 EC_GROUP *EC_GROUP_new_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth)
21 {
22 EC_GROUP *ret;
23
24 if (meth == NULL) {
25 ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL);
26 return NULL;
27 }
28 if (meth->group_init == 0) {
29 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
30 return NULL;
31 }
32
33 ret = OPENSSL_zalloc(sizeof(*ret));
34 if (ret == NULL) {
35 ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE);
36 return NULL;
37 }
38
39 ret->libctx = libctx;
40 ret->meth = meth;
41 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
42 ret->order = BN_new();
43 if (ret->order == NULL)
44 goto err;
45 ret->cofactor = BN_new();
46 if (ret->cofactor == NULL)
47 goto err;
48 }
49 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
50 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
51 if (!meth->group_init(ret))
52 goto err;
53 return ret;
54
55 err:
56 BN_free(ret->order);
57 BN_free(ret->cofactor);
58 OPENSSL_free(ret);
59 return NULL;
60 }
61
62 #ifndef FIPS_MODE
63 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
64 {
65 return EC_GROUP_new_ex(NULL, meth);
66 }
67 #endif
68
69 void EC_pre_comp_free(EC_GROUP *group)
70 {
71 switch (group->pre_comp_type) {
72 case PCT_none:
73 break;
74 case PCT_nistz256:
75 #ifdef ECP_NISTZ256_ASM
76 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
77 #endif
78 break;
79 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
80 case PCT_nistp224:
81 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
82 break;
83 case PCT_nistp256:
84 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
85 break;
86 case PCT_nistp521:
87 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
88 break;
89 #else
90 case PCT_nistp224:
91 case PCT_nistp256:
92 case PCT_nistp521:
93 break;
94 #endif
95 case PCT_ec:
96 EC_ec_pre_comp_free(group->pre_comp.ec);
97 break;
98 }
99 group->pre_comp.ec = NULL;
100 }
101
102 void EC_GROUP_free(EC_GROUP *group)
103 {
104 if (!group)
105 return;
106
107 if (group->meth->group_finish != 0)
108 group->meth->group_finish(group);
109
110 EC_pre_comp_free(group);
111 BN_MONT_CTX_free(group->mont_data);
112 EC_POINT_free(group->generator);
113 BN_free(group->order);
114 BN_free(group->cofactor);
115 OPENSSL_free(group->seed);
116 OPENSSL_free(group);
117 }
118
119 void EC_GROUP_clear_free(EC_GROUP *group)
120 {
121 if (!group)
122 return;
123
124 if (group->meth->group_clear_finish != 0)
125 group->meth->group_clear_finish(group);
126 else if (group->meth->group_finish != 0)
127 group->meth->group_finish(group);
128
129 EC_pre_comp_free(group);
130 BN_MONT_CTX_free(group->mont_data);
131 EC_POINT_clear_free(group->generator);
132 BN_clear_free(group->order);
133 BN_clear_free(group->cofactor);
134 OPENSSL_clear_free(group->seed, group->seed_len);
135 OPENSSL_clear_free(group, sizeof(*group));
136 }
137
138 int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
139 {
140 if (dest->meth->group_copy == 0) {
141 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142 return 0;
143 }
144 if (dest->meth != src->meth) {
145 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
146 return 0;
147 }
148 if (dest == src)
149 return 1;
150
151 dest->libctx = src->libctx;
152 dest->curve_name = src->curve_name;
153
154 /* Copy precomputed */
155 dest->pre_comp_type = src->pre_comp_type;
156 switch (src->pre_comp_type) {
157 case PCT_none:
158 dest->pre_comp.ec = NULL;
159 break;
160 case PCT_nistz256:
161 #ifdef ECP_NISTZ256_ASM
162 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
163 #endif
164 break;
165 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
166 case PCT_nistp224:
167 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
168 break;
169 case PCT_nistp256:
170 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
171 break;
172 case PCT_nistp521:
173 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
174 break;
175 #else
176 case PCT_nistp224:
177 case PCT_nistp256:
178 case PCT_nistp521:
179 break;
180 #endif
181 case PCT_ec:
182 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
183 break;
184 }
185
186 if (src->mont_data != NULL) {
187 if (dest->mont_data == NULL) {
188 dest->mont_data = BN_MONT_CTX_new();
189 if (dest->mont_data == NULL)
190 return 0;
191 }
192 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
193 return 0;
194 } else {
195 /* src->generator == NULL */
196 BN_MONT_CTX_free(dest->mont_data);
197 dest->mont_data = NULL;
198 }
199
200 if (src->generator != NULL) {
201 if (dest->generator == NULL) {
202 dest->generator = EC_POINT_new(dest);
203 if (dest->generator == NULL)
204 return 0;
205 }
206 if (!EC_POINT_copy(dest->generator, src->generator))
207 return 0;
208 } else {
209 /* src->generator == NULL */
210 EC_POINT_clear_free(dest->generator);
211 dest->generator = NULL;
212 }
213
214 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
215 if (!BN_copy(dest->order, src->order))
216 return 0;
217 if (!BN_copy(dest->cofactor, src->cofactor))
218 return 0;
219 }
220
221 dest->asn1_flag = src->asn1_flag;
222 dest->asn1_form = src->asn1_form;
223
224 if (src->seed) {
225 OPENSSL_free(dest->seed);
226 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
227 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
228 return 0;
229 }
230 if (!memcpy(dest->seed, src->seed, src->seed_len))
231 return 0;
232 dest->seed_len = src->seed_len;
233 } else {
234 OPENSSL_free(dest->seed);
235 dest->seed = NULL;
236 dest->seed_len = 0;
237 }
238
239 return dest->meth->group_copy(dest, src);
240 }
241
242 EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
243 {
244 EC_GROUP *t = NULL;
245 int ok = 0;
246
247 if (a == NULL)
248 return NULL;
249
250 if ((t = EC_GROUP_new_ex(a->libctx, a->meth)) == NULL)
251 return NULL;
252 if (!EC_GROUP_copy(t, a))
253 goto err;
254
255 ok = 1;
256
257 err:
258 if (!ok) {
259 EC_GROUP_free(t);
260 return NULL;
261 }
262 return t;
263 }
264
265 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
266 {
267 return group->meth;
268 }
269
270 int EC_METHOD_get_field_type(const EC_METHOD *meth)
271 {
272 return meth->field_type;
273 }
274
275 static int ec_precompute_mont_data(EC_GROUP *);
276
277 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
278 const BIGNUM *order, const BIGNUM *cofactor)
279 {
280 if (generator == NULL) {
281 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
282 return 0;
283 }
284
285 if (group->generator == NULL) {
286 group->generator = EC_POINT_new(group);
287 if (group->generator == NULL)
288 return 0;
289 }
290 if (!EC_POINT_copy(group->generator, generator))
291 return 0;
292
293 if (order != NULL) {
294 if (!BN_copy(group->order, order))
295 return 0;
296 } else {
297 BN_zero(group->order);
298 }
299
300 /* The cofactor is an optional field, so it should be able to be NULL. */
301 if (cofactor != NULL) {
302 if (!BN_copy(group->cofactor, cofactor))
303 return 0;
304 } else {
305 BN_zero(group->cofactor);
306 }
307 /*
308 * Some groups have an order with
309 * factors of two, which makes the Montgomery setup fail.
310 * |group->mont_data| will be NULL in this case.
311 */
312 if (BN_is_odd(group->order)) {
313 return ec_precompute_mont_data(group);
314 }
315
316 BN_MONT_CTX_free(group->mont_data);
317 group->mont_data = NULL;
318 return 1;
319 }
320
321 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
322 {
323 return group->generator;
324 }
325
326 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
327 {
328 return group->mont_data;
329 }
330
331 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
332 {
333 if (group->order == NULL)
334 return 0;
335 if (!BN_copy(order, group->order))
336 return 0;
337
338 return !BN_is_zero(order);
339 }
340
341 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
342 {
343 return group->order;
344 }
345
346 int EC_GROUP_order_bits(const EC_GROUP *group)
347 {
348 return group->meth->group_order_bits(group);
349 }
350
351 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
352 BN_CTX *ctx)
353 {
354
355 if (group->cofactor == NULL)
356 return 0;
357 if (!BN_copy(cofactor, group->cofactor))
358 return 0;
359
360 return !BN_is_zero(group->cofactor);
361 }
362
363 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
364 {
365 return group->cofactor;
366 }
367
368 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
369 {
370 group->curve_name = nid;
371 }
372
373 int EC_GROUP_get_curve_name(const EC_GROUP *group)
374 {
375 return group->curve_name;
376 }
377
378 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
379 {
380 return group->field;
381 }
382
383 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
384 {
385 group->asn1_flag = flag;
386 }
387
388 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
389 {
390 return group->asn1_flag;
391 }
392
393 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
394 point_conversion_form_t form)
395 {
396 group->asn1_form = form;
397 }
398
399 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
400 *group)
401 {
402 return group->asn1_form;
403 }
404
405 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
406 {
407 OPENSSL_free(group->seed);
408 group->seed = NULL;
409 group->seed_len = 0;
410
411 if (!len || !p)
412 return 1;
413
414 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
415 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
416 return 0;
417 }
418 memcpy(group->seed, p, len);
419 group->seed_len = len;
420
421 return len;
422 }
423
424 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
425 {
426 return group->seed;
427 }
428
429 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
430 {
431 return group->seed_len;
432 }
433
434 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
435 const BIGNUM *b, BN_CTX *ctx)
436 {
437 if (group->meth->group_set_curve == 0) {
438 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
439 return 0;
440 }
441 return group->meth->group_set_curve(group, p, a, b, ctx);
442 }
443
444 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
445 BN_CTX *ctx)
446 {
447 if (group->meth->group_get_curve == NULL) {
448 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
449 return 0;
450 }
451 return group->meth->group_get_curve(group, p, a, b, ctx);
452 }
453
454 #if !OPENSSL_API_3
455 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
456 const BIGNUM *b, BN_CTX *ctx)
457 {
458 return EC_GROUP_set_curve(group, p, a, b, ctx);
459 }
460
461 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
462 BIGNUM *b, BN_CTX *ctx)
463 {
464 return EC_GROUP_get_curve(group, p, a, b, ctx);
465 }
466
467 # ifndef OPENSSL_NO_EC2M
468 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
469 const BIGNUM *b, BN_CTX *ctx)
470 {
471 return EC_GROUP_set_curve(group, p, a, b, ctx);
472 }
473
474 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
475 BIGNUM *b, BN_CTX *ctx)
476 {
477 return EC_GROUP_get_curve(group, p, a, b, ctx);
478 }
479 # endif
480 #endif
481
482 int EC_GROUP_get_degree(const EC_GROUP *group)
483 {
484 if (group->meth->group_get_degree == 0) {
485 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
486 return 0;
487 }
488 return group->meth->group_get_degree(group);
489 }
490
491 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
492 {
493 if (group->meth->group_check_discriminant == 0) {
494 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
495 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
496 return 0;
497 }
498 return group->meth->group_check_discriminant(group, ctx);
499 }
500
501 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
502 {
503 int r = 0;
504 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
505 #ifndef FIPS_MODE
506 BN_CTX *ctx_new = NULL;
507
508 if (ctx == NULL)
509 ctx_new = ctx = BN_CTX_new();
510 #endif
511 if (ctx == NULL)
512 return -1;
513
514 /* compare the field types */
515 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
516 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
517 return 1;
518 /* compare the curve name (if present in both) */
519 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
520 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
521 return 1;
522 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
523 return 0;
524
525 BN_CTX_start(ctx);
526 a1 = BN_CTX_get(ctx);
527 a2 = BN_CTX_get(ctx);
528 a3 = BN_CTX_get(ctx);
529 b1 = BN_CTX_get(ctx);
530 b2 = BN_CTX_get(ctx);
531 b3 = BN_CTX_get(ctx);
532 if (b3 == NULL) {
533 BN_CTX_end(ctx);
534 #ifndef FIPS_MODE
535 BN_CTX_free(ctx_new);
536 #endif
537 return -1;
538 }
539
540 /*
541 * XXX This approach assumes that the external representation of curves
542 * over the same field type is the same.
543 */
544 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
545 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
546 r = 1;
547
548 /* return 1 if the curve parameters are different */
549 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
550 r = 1;
551
552 /* XXX EC_POINT_cmp() assumes that the methods are equal */
553 /* return 1 if the generators are different */
554 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
555 EC_GROUP_get0_generator(b), ctx) != 0)
556 r = 1;
557
558 if (!r) {
559 const BIGNUM *ao, *bo, *ac, *bc;
560 /* compare the orders */
561 ao = EC_GROUP_get0_order(a);
562 bo = EC_GROUP_get0_order(b);
563 if (ao == NULL || bo == NULL) {
564 /* return an error if either order is NULL */
565 r = -1;
566 goto end;
567 }
568 if (BN_cmp(ao, bo) != 0) {
569 /* return 1 if orders are different */
570 r = 1;
571 goto end;
572 }
573 /*
574 * It gets here if the curve parameters and generator matched.
575 * Now check the optional cofactors (if both are present).
576 */
577 ac = EC_GROUP_get0_cofactor(a);
578 bc = EC_GROUP_get0_cofactor(b);
579 /* Returns 1 (mismatch) if both cofactors are specified and different */
580 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
581 r = 1;
582 /* Returns 0 if the parameters matched */
583 }
584 end:
585 BN_CTX_end(ctx);
586 #ifndef FIPS_MODE
587 BN_CTX_free(ctx_new);
588 #endif
589 return r;
590 }
591
592 /* functions for EC_POINT objects */
593
594 EC_POINT *EC_POINT_new(const EC_GROUP *group)
595 {
596 EC_POINT *ret;
597
598 if (group == NULL) {
599 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
600 return NULL;
601 }
602 if (group->meth->point_init == NULL) {
603 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
604 return NULL;
605 }
606
607 ret = OPENSSL_zalloc(sizeof(*ret));
608 if (ret == NULL) {
609 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
610 return NULL;
611 }
612
613 ret->meth = group->meth;
614 ret->curve_name = group->curve_name;
615
616 if (!ret->meth->point_init(ret)) {
617 OPENSSL_free(ret);
618 return NULL;
619 }
620
621 return ret;
622 }
623
624 void EC_POINT_free(EC_POINT *point)
625 {
626 if (!point)
627 return;
628
629 if (point->meth->point_finish != 0)
630 point->meth->point_finish(point);
631 OPENSSL_free(point);
632 }
633
634 void EC_POINT_clear_free(EC_POINT *point)
635 {
636 if (!point)
637 return;
638
639 if (point->meth->point_clear_finish != 0)
640 point->meth->point_clear_finish(point);
641 else if (point->meth->point_finish != 0)
642 point->meth->point_finish(point);
643 OPENSSL_clear_free(point, sizeof(*point));
644 }
645
646 int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
647 {
648 if (dest->meth->point_copy == 0) {
649 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
650 return 0;
651 }
652 if (dest->meth != src->meth
653 || (dest->curve_name != src->curve_name
654 && dest->curve_name != 0
655 && src->curve_name != 0)) {
656 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
657 return 0;
658 }
659 if (dest == src)
660 return 1;
661 return dest->meth->point_copy(dest, src);
662 }
663
664 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
665 {
666 EC_POINT *t;
667 int r;
668
669 if (a == NULL)
670 return NULL;
671
672 t = EC_POINT_new(group);
673 if (t == NULL)
674 return NULL;
675 r = EC_POINT_copy(t, a);
676 if (!r) {
677 EC_POINT_free(t);
678 return NULL;
679 }
680 return t;
681 }
682
683 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
684 {
685 return point->meth;
686 }
687
688 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
689 {
690 if (group->meth->point_set_to_infinity == 0) {
691 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
692 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
693 return 0;
694 }
695 if (group->meth != point->meth) {
696 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
697 return 0;
698 }
699 return group->meth->point_set_to_infinity(group, point);
700 }
701
702 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
703 EC_POINT *point, const BIGNUM *x,
704 const BIGNUM *y, const BIGNUM *z,
705 BN_CTX *ctx)
706 {
707 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
708 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
709 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
710 return 0;
711 }
712 if (!ec_point_is_compat(point, group)) {
713 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
714 EC_R_INCOMPATIBLE_OBJECTS);
715 return 0;
716 }
717 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
718 y, z, ctx);
719 }
720
721 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
722 const EC_POINT *point, BIGNUM *x,
723 BIGNUM *y, BIGNUM *z,
724 BN_CTX *ctx)
725 {
726 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
727 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
728 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
729 return 0;
730 }
731 if (!ec_point_is_compat(point, group)) {
732 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
733 EC_R_INCOMPATIBLE_OBJECTS);
734 return 0;
735 }
736 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
737 y, z, ctx);
738 }
739
740 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
741 const BIGNUM *x, const BIGNUM *y,
742 BN_CTX *ctx)
743 {
744 if (group->meth->point_set_affine_coordinates == NULL) {
745 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
746 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
747 return 0;
748 }
749 if (!ec_point_is_compat(point, group)) {
750 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
751 return 0;
752 }
753 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
754 return 0;
755
756 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
757 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
758 return 0;
759 }
760 return 1;
761 }
762
763 #if !OPENSSL_API_3
764 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
765 EC_POINT *point, const BIGNUM *x,
766 const BIGNUM *y, BN_CTX *ctx)
767 {
768 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
769 }
770
771 # ifndef OPENSSL_NO_EC2M
772 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
773 EC_POINT *point, const BIGNUM *x,
774 const BIGNUM *y, BN_CTX *ctx)
775 {
776 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
777 }
778 # endif
779 #endif
780
781 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
782 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
783 BN_CTX *ctx)
784 {
785 if (group->meth->point_get_affine_coordinates == NULL) {
786 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
787 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
788 return 0;
789 }
790 if (!ec_point_is_compat(point, group)) {
791 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
792 return 0;
793 }
794 if (EC_POINT_is_at_infinity(group, point)) {
795 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
796 return 0;
797 }
798 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
799 }
800
801 #if !OPENSSL_API_3
802 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
803 const EC_POINT *point, BIGNUM *x,
804 BIGNUM *y, BN_CTX *ctx)
805 {
806 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
807 }
808
809 # ifndef OPENSSL_NO_EC2M
810 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
811 const EC_POINT *point, BIGNUM *x,
812 BIGNUM *y, BN_CTX *ctx)
813 {
814 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
815 }
816 # endif
817 #endif
818
819 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
820 const EC_POINT *b, BN_CTX *ctx)
821 {
822 if (group->meth->add == 0) {
823 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
824 return 0;
825 }
826 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
827 || !ec_point_is_compat(b, group)) {
828 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
829 return 0;
830 }
831 return group->meth->add(group, r, a, b, ctx);
832 }
833
834 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
835 BN_CTX *ctx)
836 {
837 if (group->meth->dbl == 0) {
838 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
839 return 0;
840 }
841 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
842 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
843 return 0;
844 }
845 return group->meth->dbl(group, r, a, ctx);
846 }
847
848 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
849 {
850 if (group->meth->invert == 0) {
851 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
852 return 0;
853 }
854 if (!ec_point_is_compat(a, group)) {
855 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
856 return 0;
857 }
858 return group->meth->invert(group, a, ctx);
859 }
860
861 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
862 {
863 if (group->meth->is_at_infinity == 0) {
864 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
865 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
866 return 0;
867 }
868 if (!ec_point_is_compat(point, group)) {
869 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
870 return 0;
871 }
872 return group->meth->is_at_infinity(group, point);
873 }
874
875 /*
876 * Check whether an EC_POINT is on the curve or not. Note that the return
877 * value for this function should NOT be treated as a boolean. Return values:
878 * 1: The point is on the curve
879 * 0: The point is not on the curve
880 * -1: An error occurred
881 */
882 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
883 BN_CTX *ctx)
884 {
885 if (group->meth->is_on_curve == 0) {
886 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
887 return 0;
888 }
889 if (!ec_point_is_compat(point, group)) {
890 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
891 return 0;
892 }
893 return group->meth->is_on_curve(group, point, ctx);
894 }
895
896 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
897 BN_CTX *ctx)
898 {
899 if (group->meth->point_cmp == 0) {
900 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
901 return -1;
902 }
903 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
904 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
905 return -1;
906 }
907 return group->meth->point_cmp(group, a, b, ctx);
908 }
909
910 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
911 {
912 if (group->meth->make_affine == 0) {
913 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
914 return 0;
915 }
916 if (!ec_point_is_compat(point, group)) {
917 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
918 return 0;
919 }
920 return group->meth->make_affine(group, point, ctx);
921 }
922
923 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
924 EC_POINT *points[], BN_CTX *ctx)
925 {
926 size_t i;
927
928 if (group->meth->points_make_affine == 0) {
929 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
930 return 0;
931 }
932 for (i = 0; i < num; i++) {
933 if (!ec_point_is_compat(points[i], group)) {
934 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
935 return 0;
936 }
937 }
938 return group->meth->points_make_affine(group, num, points, ctx);
939 }
940
941 /*
942 * Functions for point multiplication. If group->meth->mul is 0, we use the
943 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
944 * methods.
945 */
946
947 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
948 size_t num, const EC_POINT *points[],
949 const BIGNUM *scalars[], BN_CTX *ctx)
950 {
951 int ret = 0;
952 size_t i = 0;
953 #ifndef FIPS_MODE
954 BN_CTX *new_ctx = NULL;
955
956 if (ctx == NULL)
957 ctx = new_ctx = BN_CTX_secure_new();
958 #endif
959 if (ctx == NULL) {
960 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
961 return 0;
962 }
963
964 if ((scalar == NULL) && (num == 0)) {
965 return EC_POINT_set_to_infinity(group, r);
966 }
967
968 if (!ec_point_is_compat(r, group)) {
969 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
970 return 0;
971 }
972 for (i = 0; i < num; i++) {
973 if (!ec_point_is_compat(points[i], group)) {
974 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
975 return 0;
976 }
977 }
978
979 if (group->meth->mul != NULL)
980 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
981 else
982 /* use default */
983 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
984
985 #ifndef FIPS_MODE
986 BN_CTX_free(new_ctx);
987 #endif
988 return ret;
989 }
990
991 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
992 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
993 {
994 /* just a convenient interface to EC_POINTs_mul() */
995
996 const EC_POINT *points[1];
997 const BIGNUM *scalars[1];
998
999 points[0] = point;
1000 scalars[0] = p_scalar;
1001
1002 return EC_POINTs_mul(group, r, g_scalar,
1003 (point != NULL
1004 && p_scalar != NULL), points, scalars, ctx);
1005 }
1006
1007 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1008 {
1009 if (group->meth->mul == 0)
1010 /* use default */
1011 return ec_wNAF_precompute_mult(group, ctx);
1012
1013 if (group->meth->precompute_mult != 0)
1014 return group->meth->precompute_mult(group, ctx);
1015 else
1016 return 1; /* nothing to do, so report success */
1017 }
1018
1019 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1020 {
1021 if (group->meth->mul == 0)
1022 /* use default */
1023 return ec_wNAF_have_precompute_mult(group);
1024
1025 if (group->meth->have_precompute_mult != 0)
1026 return group->meth->have_precompute_mult(group);
1027 else
1028 return 0; /* cannot tell whether precomputation has
1029 * been performed */
1030 }
1031
1032 /*
1033 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1034 * returns one on success. On error it returns zero.
1035 */
1036 static int ec_precompute_mont_data(EC_GROUP *group)
1037 {
1038 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1039 int ret = 0;
1040
1041 BN_MONT_CTX_free(group->mont_data);
1042 group->mont_data = NULL;
1043
1044 if (ctx == NULL)
1045 goto err;
1046
1047 group->mont_data = BN_MONT_CTX_new();
1048 if (group->mont_data == NULL)
1049 goto err;
1050
1051 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1052 BN_MONT_CTX_free(group->mont_data);
1053 group->mont_data = NULL;
1054 goto err;
1055 }
1056
1057 ret = 1;
1058
1059 err:
1060
1061 BN_CTX_free(ctx);
1062 return ret;
1063 }
1064
1065 #ifndef FIPS_MODE
1066 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1067 {
1068 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1069 }
1070
1071 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1072 {
1073 return CRYPTO_get_ex_data(&key->ex_data, idx);
1074 }
1075 #endif
1076
1077 int ec_group_simple_order_bits(const EC_GROUP *group)
1078 {
1079 if (group->order == NULL)
1080 return 0;
1081 return BN_num_bits(group->order);
1082 }
1083
1084 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1085 const BIGNUM *x, BN_CTX *ctx)
1086 {
1087 BIGNUM *e = NULL;
1088 int ret = 0;
1089 #ifndef FIPS_MODE
1090 BN_CTX *new_ctx = NULL;
1091
1092 if (ctx == NULL)
1093 ctx = new_ctx = BN_CTX_secure_new();
1094 #endif
1095 if (ctx == NULL)
1096 return 0;
1097
1098 if (group->mont_data == NULL)
1099 goto err;
1100
1101 BN_CTX_start(ctx);
1102 if ((e = BN_CTX_get(ctx)) == NULL)
1103 goto err;
1104
1105 /*-
1106 * We want inverse in constant time, therefore we utilize the fact
1107 * order must be prime and use Fermats Little Theorem instead.
1108 */
1109 if (!BN_set_word(e, 2))
1110 goto err;
1111 if (!BN_sub(e, group->order, e))
1112 goto err;
1113 /*-
1114 * Exponent e is public.
1115 * No need for scatter-gather or BN_FLG_CONSTTIME.
1116 */
1117 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1118 goto err;
1119
1120 ret = 1;
1121
1122 err:
1123 BN_CTX_end(ctx);
1124 #ifndef FIPS_MODE
1125 BN_CTX_free(new_ctx);
1126 #endif
1127 return ret;
1128 }
1129
1130 /*-
1131 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1132 * - When group->order is even, this function returns an error.
1133 * - When group->order is otherwise composite, the correctness
1134 * of the output is not guaranteed.
1135 * - When x is outside the range [1, group->order), the correctness
1136 * of the output is not guaranteed.
1137 * - Otherwise, this function returns the multiplicative inverse in the
1138 * range [1, group->order).
1139 *
1140 * EC_METHODs must implement their own field_inverse_mod_ord for
1141 * other functionality.
1142 */
1143 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1144 const BIGNUM *x, BN_CTX *ctx)
1145 {
1146 if (group->meth->field_inverse_mod_ord != NULL)
1147 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1148 else
1149 return ec_field_inverse_mod_ord(group, res, x, ctx);
1150 }
1151
1152 /*-
1153 * Coordinate blinding for EC_POINT.
1154 *
1155 * The underlying EC_METHOD can optionally implement this function:
1156 * underlying implementations should return 0 on errors, or 1 on
1157 * success.
1158 *
1159 * This wrapper returns 1 in case the underlying EC_METHOD does not
1160 * support coordinate blinding.
1161 */
1162 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1163 {
1164 if (group->meth->blind_coordinates == NULL)
1165 return 1; /* ignore if not implemented */
1166
1167 return group->meth->blind_coordinates(group, p, ctx);
1168 }