1/* crypto/ec/ec_lib.c */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 *    software must display the following acknowledgment:
22 *    "This product includes software developed by the OpenSSL Project
23 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 *    endorse or promote products derived from this software without
27 *    prior written permission. For written permission, please contact
28 *    openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 *    nor may "OpenSSL" appear in their names without prior written
32 *    permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by the OpenSSL Project
37 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com).  This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Binary polynomial ECC support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */
63
64#include <string.h>
65
66#include <openssl/err.h>
67#include <openssl/opensslv.h>
68
69#include "ec_lcl.h"
70
71static const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
72
73/* functions for EC_GROUP objects */
74
75EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
76{
77    EC_GROUP *ret;
78
79    if (meth == NULL) {
80        ECerr(EC_F_EC_GROUP_NEW, ERR_R_PASSED_NULL_PARAMETER);
81        return NULL;
82    }
83    if (meth->group_init == 0) {
84        ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
85        return NULL;
86    }
87
88    ret = OPENSSL_malloc(sizeof *ret);
89    if (ret == NULL) {
90        ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
91        return NULL;
92    }
93
94    ret->meth = meth;
95
96    ret->extra_data = NULL;
97
98    ret->generator = NULL;
99    BN_init(&ret->order);
100    BN_init(&ret->cofactor);
101
102    ret->curve_name = 0;
103    ret->asn1_flag = 0;
104    ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
105
106    ret->seed = NULL;
107    ret->seed_len = 0;
108
109    if (!meth->group_init(ret)) {
110        OPENSSL_free(ret);
111        return NULL;
112    }
113
114    return ret;
115}
116
117void EC_GROUP_free(EC_GROUP *group)
118{
119    if (!group)
120        return;
121
122    if (group->meth->group_finish != 0)
123        group->meth->group_finish(group);
124
125    EC_EX_DATA_free_all_data(&group->extra_data);
126
127    if (group->generator != NULL)
128        EC_POINT_free(group->generator);
129    BN_free(&group->order);
130    BN_free(&group->cofactor);
131
132    if (group->seed)
133        OPENSSL_free(group->seed);
134
135    OPENSSL_free(group);
136}
137
138void EC_GROUP_clear_free(EC_GROUP *group)
139{
140    if (!group)
141        return;
142
143    if (group->meth->group_clear_finish != 0)
144        group->meth->group_clear_finish(group);
145    else if (group->meth->group_finish != 0)
146        group->meth->group_finish(group);
147
148    EC_EX_DATA_clear_free_all_data(&group->extra_data);
149
150    if (group->generator != NULL)
151        EC_POINT_clear_free(group->generator);
152    BN_clear_free(&group->order);
153    BN_clear_free(&group->cofactor);
154
155    if (group->seed) {
156        OPENSSL_cleanse(group->seed, group->seed_len);
157        OPENSSL_free(group->seed);
158    }
159
160    OPENSSL_cleanse(group, sizeof *group);
161    OPENSSL_free(group);
162}
163
164int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
165{
166    EC_EXTRA_DATA *d;
167
168    if (dest->meth->group_copy == 0) {
169        ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
170        return 0;
171    }
172    if (dest->meth != src->meth) {
173        ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
174        return 0;
175    }
176    if (dest == src)
177        return 1;
178
179    EC_EX_DATA_free_all_data(&dest->extra_data);
180
181    for (d = src->extra_data; d != NULL; d = d->next) {
182        void *t = d->dup_func(d->data);
183
184        if (t == NULL)
185            return 0;
186        if (!EC_EX_DATA_set_data
187            (&dest->extra_data, t, d->dup_func, d->free_func,
188             d->clear_free_func))
189            return 0;
190    }
191
192    if (src->generator != NULL) {
193        if (dest->generator == NULL) {
194            dest->generator = EC_POINT_new(dest);
195            if (dest->generator == NULL)
196                return 0;
197        }
198        if (!EC_POINT_copy(dest->generator, src->generator))
199            return 0;
200    } else {
201        /* src->generator == NULL */
202        if (dest->generator != NULL) {
203            EC_POINT_clear_free(dest->generator);
204            dest->generator = NULL;
205        }
206    }
207
208    if (!BN_copy(&dest->order, &src->order))
209        return 0;
210    if (!BN_copy(&dest->cofactor, &src->cofactor))
211        return 0;
212
213    dest->curve_name = src->curve_name;
214    dest->asn1_flag = src->asn1_flag;
215    dest->asn1_form = src->asn1_form;
216
217    if (src->seed) {
218        if (dest->seed)
219            OPENSSL_free(dest->seed);
220        dest->seed = OPENSSL_malloc(src->seed_len);
221        if (dest->seed == NULL)
222            return 0;
223        if (!memcpy(dest->seed, src->seed, src->seed_len))
224            return 0;
225        dest->seed_len = src->seed_len;
226    } else {
227        if (dest->seed)
228            OPENSSL_free(dest->seed);
229        dest->seed = NULL;
230        dest->seed_len = 0;
231    }
232
233    return dest->meth->group_copy(dest, src);
234}
235
236EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
237{
238    EC_GROUP *t = NULL;
239    int ok = 0;
240
241    if (a == NULL)
242        return NULL;
243
244    if ((t = EC_GROUP_new(a->meth)) == NULL)
245        return (NULL);
246    if (!EC_GROUP_copy(t, a))
247        goto err;
248
249    ok = 1;
250
251 err:
252    if (!ok) {
253        if (t)
254            EC_GROUP_free(t);
255        return NULL;
256    } else
257        return t;
258}
259
260const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
261{
262    return group->meth;
263}
264
265int EC_METHOD_get_field_type(const EC_METHOD *meth)
266{
267    return meth->field_type;
268}
269
270int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
271                           const BIGNUM *order, const BIGNUM *cofactor)
272{
273    if (generator == NULL) {
274        ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
275        return 0;
276    }
277
278    if (group->generator == NULL) {
279        group->generator = EC_POINT_new(group);
280        if (group->generator == NULL)
281            return 0;
282    }
283    if (!EC_POINT_copy(group->generator, generator))
284        return 0;
285
286    if (order != NULL) {
287        if (!BN_copy(&group->order, order))
288            return 0;
289    } else
290        BN_zero(&group->order);
291
292    if (cofactor != NULL) {
293        if (!BN_copy(&group->cofactor, cofactor))
294            return 0;
295    } else
296        BN_zero(&group->cofactor);
297
298    return 1;
299}
300
301const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
302{
303    return group->generator;
304}
305
306int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
307{
308    if (!BN_copy(order, &group->order))
309        return 0;
310
311    return !BN_is_zero(order);
312}
313
314int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
315                          BN_CTX *ctx)
316{
317    if (!BN_copy(cofactor, &group->cofactor))
318        return 0;
319
320    return !BN_is_zero(&group->cofactor);
321}
322
323void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
324{
325    group->curve_name = nid;
326}
327
328int EC_GROUP_get_curve_name(const EC_GROUP *group)
329{
330    return group->curve_name;
331}
332
333void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
334{
335    group->asn1_flag = flag;
336}
337
338int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
339{
340    return group->asn1_flag;
341}
342
343void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
344                                        point_conversion_form_t form)
345{
346    group->asn1_form = form;
347}
348
349point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
350                                                           *group)
351{
352    return group->asn1_form;
353}
354
355size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
356{
357    if (group->seed) {
358        OPENSSL_free(group->seed);
359        group->seed = NULL;
360        group->seed_len = 0;
361    }
362
363    if (!len || !p)
364        return 1;
365
366    if ((group->seed = OPENSSL_malloc(len)) == NULL)
367        return 0;
368    memcpy(group->seed, p, len);
369    group->seed_len = len;
370
371    return len;
372}
373
374unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
375{
376    return group->seed;
377}
378
379size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
380{
381    return group->seed_len;
382}
383
384int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
385                           const BIGNUM *b, BN_CTX *ctx)
386{
387    if (group->meth->group_set_curve == 0) {
388        ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
389        return 0;
390    }
391    return group->meth->group_set_curve(group, p, a, b, ctx);
392}
393
394int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
395                           BIGNUM *b, BN_CTX *ctx)
396{
397    if (group->meth->group_get_curve == 0) {
398        ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
399        return 0;
400    }
401    return group->meth->group_get_curve(group, p, a, b, ctx);
402}
403
404int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
405                            const BIGNUM *b, BN_CTX *ctx)
406{
407    if (group->meth->group_set_curve == 0) {
408        ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
409              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
410        return 0;
411    }
412    return group->meth->group_set_curve(group, p, a, b, ctx);
413}
414
415int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
416                            BIGNUM *b, BN_CTX *ctx)
417{
418    if (group->meth->group_get_curve == 0) {
419        ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
420              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
421        return 0;
422    }
423    return group->meth->group_get_curve(group, p, a, b, ctx);
424}
425
426int EC_GROUP_get_degree(const EC_GROUP *group)
427{
428    if (group->meth->group_get_degree == 0) {
429        ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
430        return 0;
431    }
432    return group->meth->group_get_degree(group);
433}
434
435int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
436{
437    if (group->meth->group_check_discriminant == 0) {
438        ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
439              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
440        return 0;
441    }
442    return group->meth->group_check_discriminant(group, ctx);
443}
444
445int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
446{
447    int r = 0;
448    BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
449    BN_CTX *ctx_new = NULL;
450
451    /* compare the field types */
452    if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
453        EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
454        return 1;
455    /* compare the curve name (if present in both) */
456    if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
457        EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
458        return 1;
459
460    if (!ctx)
461        ctx_new = ctx = BN_CTX_new();
462    if (!ctx)
463        return -1;
464
465    BN_CTX_start(ctx);
466    a1 = BN_CTX_get(ctx);
467    a2 = BN_CTX_get(ctx);
468    a3 = BN_CTX_get(ctx);
469    b1 = BN_CTX_get(ctx);
470    b2 = BN_CTX_get(ctx);
471    b3 = BN_CTX_get(ctx);
472    if (!b3) {
473        BN_CTX_end(ctx);
474        if (ctx_new)
475            BN_CTX_free(ctx);
476        return -1;
477    }
478
479    /*
480     * XXX This approach assumes that the external representation of curves
481     * over the same field type is the same.
482     */
483    if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
484        !b->meth->group_get_curve(b, b1, b2, b3, ctx))
485        r = 1;
486
487    if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
488        r = 1;
489
490    /* XXX EC_POINT_cmp() assumes that the methods are equal */
491    if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
492                          EC_GROUP_get0_generator(b), ctx))
493        r = 1;
494
495    if (!r) {
496        /* compare the order and cofactor */
497        if (!EC_GROUP_get_order(a, a1, ctx) ||
498            !EC_GROUP_get_order(b, b1, ctx) ||
499            !EC_GROUP_get_cofactor(a, a2, ctx) ||
500            !EC_GROUP_get_cofactor(b, b2, ctx)) {
501            BN_CTX_end(ctx);
502            if (ctx_new)
503                BN_CTX_free(ctx);
504            return -1;
505        }
506        if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
507            r = 1;
508    }
509
510    BN_CTX_end(ctx);
511    if (ctx_new)
512        BN_CTX_free(ctx);
513
514    return r;
515}
516
517/* this has 'package' visibility */
518int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
519                        void *(*dup_func) (void *),
520                        void (*free_func) (void *),
521                        void (*clear_free_func) (void *))
522{
523    EC_EXTRA_DATA *d;
524
525    if (ex_data == NULL)
526        return 0;
527
528    for (d = *ex_data; d != NULL; d = d->next) {
529        if (d->dup_func == dup_func && d->free_func == free_func
530            && d->clear_free_func == clear_free_func) {
531            ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
532            return 0;
533        }
534    }
535
536    if (data == NULL)
537        /* no explicit entry needed */
538        return 1;
539
540    d = OPENSSL_malloc(sizeof *d);
541    if (d == NULL)
542        return 0;
543
544    d->data = data;
545    d->dup_func = dup_func;
546    d->free_func = free_func;
547    d->clear_free_func = clear_free_func;
548
549    d->next = *ex_data;
550    *ex_data = d;
551
552    return 1;
553}
554
555/* this has 'package' visibility */
556void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
557                          void *(*dup_func) (void *),
558                          void (*free_func) (void *),
559                          void (*clear_free_func) (void *))
560{
561    const EC_EXTRA_DATA *d;
562
563    for (d = ex_data; d != NULL; d = d->next) {
564        if (d->dup_func == dup_func && d->free_func == free_func
565            && d->clear_free_func == clear_free_func)
566            return d->data;
567    }
568
569    return NULL;
570}
571
572/* this has 'package' visibility */
573void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
574                          void *(*dup_func) (void *),
575                          void (*free_func) (void *),
576                          void (*clear_free_func) (void *))
577{
578    EC_EXTRA_DATA **p;
579
580    if (ex_data == NULL)
581        return;
582
583    for (p = ex_data; *p != NULL; p = &((*p)->next)) {
584        if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
585            && (*p)->clear_free_func == clear_free_func) {
586            EC_EXTRA_DATA *next = (*p)->next;
587
588            (*p)->free_func((*p)->data);
589            OPENSSL_free(*p);
590
591            *p = next;
592            return;
593        }
594    }
595}
596
597/* this has 'package' visibility */
598void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
599                                void *(*dup_func) (void *),
600                                void (*free_func) (void *),
601                                void (*clear_free_func) (void *))
602{
603    EC_EXTRA_DATA **p;
604
605    if (ex_data == NULL)
606        return;
607
608    for (p = ex_data; *p != NULL; p = &((*p)->next)) {
609        if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
610            && (*p)->clear_free_func == clear_free_func) {
611            EC_EXTRA_DATA *next = (*p)->next;
612
613            (*p)->clear_free_func((*p)->data);
614            OPENSSL_free(*p);
615
616            *p = next;
617            return;
618        }
619    }
620}
621
622/* this has 'package' visibility */
623void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
624{
625    EC_EXTRA_DATA *d;
626
627    if (ex_data == NULL)
628        return;
629
630    d = *ex_data;
631    while (d) {
632        EC_EXTRA_DATA *next = d->next;
633
634        d->free_func(d->data);
635        OPENSSL_free(d);
636
637        d = next;
638    }
639    *ex_data = NULL;
640}
641
642/* this has 'package' visibility */
643void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
644{
645    EC_EXTRA_DATA *d;
646
647    if (ex_data == NULL)
648        return;
649
650    d = *ex_data;
651    while (d) {
652        EC_EXTRA_DATA *next = d->next;
653
654        d->clear_free_func(d->data);
655        OPENSSL_free(d);
656
657        d = next;
658    }
659    *ex_data = NULL;
660}
661
662/* functions for EC_POINT objects */
663
664EC_POINT *EC_POINT_new(const EC_GROUP *group)
665{
666    EC_POINT *ret;
667
668    if (group == NULL) {
669        ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
670        return NULL;
671    }
672    if (group->meth->point_init == 0) {
673        ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
674        return NULL;
675    }
676
677    ret = OPENSSL_malloc(sizeof *ret);
678    if (ret == NULL) {
679        ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
680        return NULL;
681    }
682
683    ret->meth = group->meth;
684
685    if (!ret->meth->point_init(ret)) {
686        OPENSSL_free(ret);
687        return NULL;
688    }
689
690    return ret;
691}
692
693void EC_POINT_free(EC_POINT *point)
694{
695    if (!point)
696        return;
697
698    if (point->meth->point_finish != 0)
699        point->meth->point_finish(point);
700    OPENSSL_free(point);
701}
702
703void EC_POINT_clear_free(EC_POINT *point)
704{
705    if (!point)
706        return;
707
708    if (point->meth->point_clear_finish != 0)
709        point->meth->point_clear_finish(point);
710    else if (point->meth != NULL && point->meth->point_finish != 0)
711        point->meth->point_finish(point);
712    OPENSSL_cleanse(point, sizeof *point);
713    OPENSSL_free(point);
714}
715
716int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
717{
718    if (dest->meth->point_copy == 0) {
719        ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
720        return 0;
721    }
722    if (dest->meth != src->meth) {
723        ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
724        return 0;
725    }
726    if (dest == src)
727        return 1;
728    return dest->meth->point_copy(dest, src);
729}
730
731EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
732{
733    EC_POINT *t;
734    int r;
735
736    if (a == NULL)
737        return NULL;
738
739    t = EC_POINT_new(group);
740    if (t == NULL)
741        return (NULL);
742    r = EC_POINT_copy(t, a);
743    if (!r) {
744        EC_POINT_free(t);
745        return NULL;
746    } else
747        return t;
748}
749
750const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
751{
752    return point->meth;
753}
754
755int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
756{
757    if (group->meth->point_set_to_infinity == 0) {
758        ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
759              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
760        return 0;
761    }
762    if (group->meth != point->meth) {
763        ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
764        return 0;
765    }
766    return group->meth->point_set_to_infinity(group, point);
767}
768
769int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
770                                             EC_POINT *point, const BIGNUM *x,
771                                             const BIGNUM *y, const BIGNUM *z,
772                                             BN_CTX *ctx)
773{
774    if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
775        ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
776              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
777        return 0;
778    }
779    if (group->meth != point->meth) {
780        ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
781              EC_R_INCOMPATIBLE_OBJECTS);
782        return 0;
783    }
784    return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
785                                                              y, z, ctx);
786}
787
788int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
789                                             const EC_POINT *point, BIGNUM *x,
790                                             BIGNUM *y, BIGNUM *z,
791                                             BN_CTX *ctx)
792{
793    if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
794        ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
795              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
796        return 0;
797    }
798    if (group->meth != point->meth) {
799        ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
800              EC_R_INCOMPATIBLE_OBJECTS);
801        return 0;
802    }
803    return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
804                                                              y, z, ctx);
805}
806
807int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
808                                        EC_POINT *point, const BIGNUM *x,
809                                        const BIGNUM *y, BN_CTX *ctx)
810{
811    if (group->meth->point_set_affine_coordinates == 0) {
812        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
813              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
814        return 0;
815    }
816    if (group->meth != point->meth) {
817        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
818              EC_R_INCOMPATIBLE_OBJECTS);
819        return 0;
820    }
821    return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
822}
823
824int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
825                                         EC_POINT *point, const BIGNUM *x,
826                                         const BIGNUM *y, BN_CTX *ctx)
827{
828    if (group->meth->point_set_affine_coordinates == 0) {
829        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
830              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
831        return 0;
832    }
833    if (group->meth != point->meth) {
834        ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
835              EC_R_INCOMPATIBLE_OBJECTS);
836        return 0;
837    }
838    return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
839}
840
841int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
842                                        const EC_POINT *point, BIGNUM *x,
843                                        BIGNUM *y, BN_CTX *ctx)
844{
845    if (group->meth->point_get_affine_coordinates == 0) {
846        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
847              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
848        return 0;
849    }
850    if (group->meth != point->meth) {
851        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
852              EC_R_INCOMPATIBLE_OBJECTS);
853        return 0;
854    }
855    return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
856}
857
858int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
859                                         const EC_POINT *point, BIGNUM *x,
860                                         BIGNUM *y, BN_CTX *ctx)
861{
862    if (group->meth->point_get_affine_coordinates == 0) {
863        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
864              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
865        return 0;
866    }
867    if (group->meth != point->meth) {
868        ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
869              EC_R_INCOMPATIBLE_OBJECTS);
870        return 0;
871    }
872    return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
873}
874
875int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
876                                            EC_POINT *point, const BIGNUM *x,
877                                            int y_bit, BN_CTX *ctx)
878{
879    if (group->meth->point_set_compressed_coordinates == 0) {
880        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
881              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
882        return 0;
883    }
884    if (group->meth != point->meth) {
885        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
886              EC_R_INCOMPATIBLE_OBJECTS);
887        return 0;
888    }
889    return group->meth->point_set_compressed_coordinates(group, point, x,
890                                                         y_bit, ctx);
891}
892
893int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
894                                             EC_POINT *point, const BIGNUM *x,
895                                             int y_bit, BN_CTX *ctx)
896{
897    if (group->meth->point_set_compressed_coordinates == 0) {
898        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
899              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
900        return 0;
901    }
902    if (group->meth != point->meth) {
903        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
904              EC_R_INCOMPATIBLE_OBJECTS);
905        return 0;
906    }
907    return group->meth->point_set_compressed_coordinates(group, point, x,
908                                                         y_bit, ctx);
909}
910
911size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
912                          point_conversion_form_t form, unsigned char *buf,
913                          size_t len, BN_CTX *ctx)
914{
915    if (group->meth->point2oct == 0) {
916        ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
917        return 0;
918    }
919    if (group->meth != point->meth) {
920        ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
921        return 0;
922    }
923    return group->meth->point2oct(group, point, form, buf, len, ctx);
924}
925
926int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
927                       const unsigned char *buf, size_t len, BN_CTX *ctx)
928{
929    if (group->meth->oct2point == 0) {
930        ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
931        return 0;
932    }
933    if (group->meth != point->meth) {
934        ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
935        return 0;
936    }
937    return group->meth->oct2point(group, point, buf, len, ctx);
938}
939
940int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
941                 const EC_POINT *b, BN_CTX *ctx)
942{
943    if (group->meth->add == 0) {
944        ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
945        return 0;
946    }
947    if ((group->meth != r->meth) || (r->meth != a->meth)
948        || (a->meth != b->meth)) {
949        ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
950        return 0;
951    }
952    return group->meth->add(group, r, a, b, ctx);
953}
954
955int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
956                 BN_CTX *ctx)
957{
958    if (group->meth->dbl == 0) {
959        ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
960        return 0;
961    }
962    if ((group->meth != r->meth) || (r->meth != a->meth)) {
963        ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
964        return 0;
965    }
966    return group->meth->dbl(group, r, a, ctx);
967}
968
969int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
970{
971    if (group->meth->invert == 0) {
972        ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
973        return 0;
974    }
975    if (group->meth != a->meth) {
976        ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
977        return 0;
978    }
979    return group->meth->invert(group, a, ctx);
980}
981
982int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
983{
984    if (group->meth->is_at_infinity == 0) {
985        ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
986              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
987        return 0;
988    }
989    if (group->meth != point->meth) {
990        ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
991        return 0;
992    }
993    return group->meth->is_at_infinity(group, point);
994}
995
996/*
997 * Check whether an EC_POINT is on the curve or not. Note that the return
998 * value for this function should NOT be treated as a boolean. Return values:
999 *  1: The point is on the curve
1000 *  0: The point is not on the curve
1001 * -1: An error occurred
1002 */
1003int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1004                         BN_CTX *ctx)
1005{
1006    if (group->meth->is_on_curve == 0) {
1007        ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1008        return 0;
1009    }
1010    if (group->meth != point->meth) {
1011        ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
1012        return 0;
1013    }
1014    return group->meth->is_on_curve(group, point, ctx);
1015}
1016
1017int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1018                 BN_CTX *ctx)
1019{
1020    if (group->meth->point_cmp == 0) {
1021        ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1022        return -1;
1023    }
1024    if ((group->meth != a->meth) || (a->meth != b->meth)) {
1025        ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1026        return -1;
1027    }
1028    return group->meth->point_cmp(group, a, b, ctx);
1029}
1030
1031int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1032{
1033    if (group->meth->make_affine == 0) {
1034        ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1035        return 0;
1036    }
1037    if (group->meth != point->meth) {
1038        ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1039        return 0;
1040    }
1041    return group->meth->make_affine(group, point, ctx);
1042}
1043
1044int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1045                          EC_POINT *points[], BN_CTX *ctx)
1046{
1047    size_t i;
1048
1049    if (group->meth->points_make_affine == 0) {
1050        ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1051        return 0;
1052    }
1053    for (i = 0; i < num; i++) {
1054        if (group->meth != points[i]->meth) {
1055            ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1056            return 0;
1057        }
1058    }
1059    return group->meth->points_make_affine(group, num, points, ctx);
1060}
1061
1062/*
1063 * Functions for point multiplication. If group->meth->mul is 0, we use the
1064 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1065 * methods.
1066 */
1067
1068int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1069                  size_t num, const EC_POINT *points[],
1070                  const BIGNUM *scalars[], BN_CTX *ctx)
1071{
1072    if (group->meth->mul == 0)
1073        /* use default */
1074        return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1075
1076    return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1077}
1078
1079int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1080                 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1081{
1082    /* just a convenient interface to EC_POINTs_mul() */
1083
1084    const EC_POINT *points[1];
1085    const BIGNUM *scalars[1];
1086
1087    points[0] = point;
1088    scalars[0] = p_scalar;
1089
1090    return EC_POINTs_mul(group, r, g_scalar,
1091                         (point != NULL
1092                          && p_scalar != NULL), points, scalars, ctx);
1093}
1094
1095int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1096{
1097    if (group->meth->mul == 0)
1098        /* use default */
1099        return ec_wNAF_precompute_mult(group, ctx);
1100
1101    if (group->meth->precompute_mult != 0)
1102        return group->meth->precompute_mult(group, ctx);
1103    else
1104        return 1;               /* nothing to do, so report success */
1105}
1106
1107int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1108{
1109    if (group->meth->mul == 0)
1110        /* use default */
1111        return ec_wNAF_have_precompute_mult(group);
1112
1113    if (group->meth->have_precompute_mult != 0)
1114        return group->meth->have_precompute_mult(group);
1115    else
1116        return 0;               /* cannot tell whether precomputation has
1117                                 * been performed */
1118}
1119