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