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