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