]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_lib.c
To avoid possible time_t overflow use X509_time_adj_ex()
[thirdparty/openssl.git] / crypto / ec / ec_lib.c
CommitLineData
65e81670 1/* crypto/ec/ec_lib.c */
35b73a1f
BM
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
65e81670 5/* ====================================================================
37c660ff 6 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
65e81670
BM
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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
65e81670
BM
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 */
7793f30e
BM
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
0f113f3e 60 * Binary polynomial ECC support in OpenSSL originally developed by
7793f30e
BM
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */
65e81670 63
c4b36ff4
BM
64#include <string.h>
65
0657bf9c 66#include <openssl/err.h>
bb62a8b0 67#include <openssl/opensslv.h>
0657bf9c 68
65e81670 69#include "ec_lcl.h"
0657bf9c 70
0657bf9c
BM
71/* functions for EC_GROUP objects */
72
73EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
0f113f3e
MC
74{
75 EC_GROUP *ret;
76
77 if (meth == NULL) {
78 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
79 return NULL;
80 }
81 if (meth->group_init == 0) {
82 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
83 return NULL;
84 }
85
64b25758 86 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
87 if (ret == NULL) {
88 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
89 return NULL;
90 }
91
92 ret->meth = meth;
0f113f3e 93 ret->order = BN_new();
90945fa3 94 if (ret->order == NULL)
0f113f3e
MC
95 goto err;
96 ret->cofactor = BN_new();
90945fa3 97 if (ret->cofactor == NULL)
0f113f3e 98 goto err;
86f300d3 99 ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
0f113f3e 100 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
0f113f3e
MC
101 if (!meth->group_init(ret))
102 goto err;
0f113f3e 103 return ret;
64b25758 104
0f113f3e 105 err:
23a1d5e9
RS
106 BN_free(ret->order);
107 BN_free(ret->cofactor);
0f113f3e
MC
108 OPENSSL_free(ret);
109 return NULL;
110}
0657bf9c 111
3aef36ff
RS
112static void ec_group_free_precomp(EC_GROUP *group)
113{
114 switch (group->pre_comp_type) {
115 default:
116 break;
e69aa800 117#ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
3aef36ff
RS
118 case pct_nistz256:
119 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
120 break;
e69aa800 121#endif
3aef36ff
RS
122#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
123 case pct_nistp224:
124 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
125 break;
126 case pct_nistp256:
127 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
128 break;
129 case pct_nistp521:
130 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
131 break;
132#endif
133 case pct_ec:
134 EC_ec_pre_comp_free(group->pre_comp.ec);
135 break;
136 }
137 group->pre_comp.ec = NULL;
138}
139
0657bf9c 140void EC_GROUP_free(EC_GROUP *group)
0f113f3e
MC
141{
142 if (!group)
143 return;
7711de24 144
0f113f3e
MC
145 if (group->meth->group_finish != 0)
146 group->meth->group_finish(group);
df9cc153 147
3aef36ff 148 ec_group_free_precomp(group);
23a1d5e9 149 BN_MONT_CTX_free(group->mont_data);
8fdc3734 150 EC_POINT_free(group->generator);
0f113f3e
MC
151 BN_free(group->order);
152 BN_free(group->cofactor);
25aaa98a 153 OPENSSL_free(group->seed);
0f113f3e
MC
154 OPENSSL_free(group);
155}
0657bf9c
BM
156
157void EC_GROUP_clear_free(EC_GROUP *group)
0f113f3e
MC
158{
159 if (!group)
160 return;
df9cc153 161
0f113f3e
MC
162 if (group->meth->group_clear_finish != 0)
163 group->meth->group_clear_finish(group);
164 else if (group->meth->group_finish != 0)
165 group->meth->group_finish(group);
df9cc153 166
3aef36ff 167 ec_group_free_precomp(group);
23a1d5e9 168 BN_MONT_CTX_free(group->mont_data);
8fdc3734 169 EC_POINT_clear_free(group->generator);
0f113f3e
MC
170 BN_clear_free(group->order);
171 BN_clear_free(group->cofactor);
4b45c6e5 172 OPENSSL_clear_free(group->seed, group->seed_len);
b4faea50 173 OPENSSL_clear_free(group, sizeof(*group));
0f113f3e 174}
0657bf9c
BM
175
176int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
0f113f3e 177{
0f113f3e
MC
178 if (dest->meth->group_copy == 0) {
179 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
180 return 0;
181 }
182 if (dest->meth != src->meth) {
183 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
184 return 0;
185 }
186 if (dest == src)
187 return 1;
188
3aef36ff
RS
189 /* Copy precomputed */
190 dest->pre_comp_type = src->pre_comp_type;
191 switch (src->pre_comp_type) {
192 default:
193 dest->pre_comp.ec = NULL;
194 break;
e69aa800 195#ifdef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
3aef36ff
RS
196 case pct_nistz256:
197 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
198 break;
e69aa800 199#endif
3aef36ff
RS
200#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
201 case pct_nistp224:
202 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
203 break;
204 case pct_nistp256:
205 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
206 break;
207 case pct_nistp521:
208 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
209 break;
210#endif
211 case pct_ec:
212 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
213 break;
0f113f3e
MC
214 }
215
216 if (src->mont_data != NULL) {
217 if (dest->mont_data == NULL) {
218 dest->mont_data = BN_MONT_CTX_new();
219 if (dest->mont_data == NULL)
220 return 0;
221 }
222 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
223 return 0;
224 } else {
225 /* src->generator == NULL */
23a1d5e9
RS
226 BN_MONT_CTX_free(dest->mont_data);
227 dest->mont_data = NULL;
0f113f3e 228 }
0657bf9c 229
0f113f3e
MC
230 if (src->generator != NULL) {
231 if (dest->generator == NULL) {
232 dest->generator = EC_POINT_new(dest);
233 if (dest->generator == NULL)
234 return 0;
235 }
236 if (!EC_POINT_copy(dest->generator, src->generator))
237 return 0;
238 } else {
239 /* src->generator == NULL */
8fdc3734
RS
240 EC_POINT_clear_free(dest->generator);
241 dest->generator = NULL;
0f113f3e
MC
242 }
243
244 if (!BN_copy(dest->order, src->order))
245 return 0;
246 if (!BN_copy(dest->cofactor, src->cofactor))
247 return 0;
248
249 dest->curve_name = src->curve_name;
250 dest->asn1_flag = src->asn1_flag;
251 dest->asn1_form = src->asn1_form;
252
253 if (src->seed) {
b548a1f1 254 OPENSSL_free(dest->seed);
0f113f3e
MC
255 dest->seed = OPENSSL_malloc(src->seed_len);
256 if (dest->seed == NULL)
257 return 0;
258 if (!memcpy(dest->seed, src->seed, src->seed_len))
259 return 0;
260 dest->seed_len = src->seed_len;
261 } else {
b548a1f1 262 OPENSSL_free(dest->seed);
0f113f3e
MC
263 dest->seed = NULL;
264 dest->seed_len = 0;
265 }
266
267 return dest->meth->group_copy(dest, src);
268}
0657bf9c 269
7793f30e 270EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
0f113f3e
MC
271{
272 EC_GROUP *t = NULL;
273 int ok = 0;
7793f30e 274
0f113f3e
MC
275 if (a == NULL)
276 return NULL;
7793f30e 277
0f113f3e
MC
278 if ((t = EC_GROUP_new(a->meth)) == NULL)
279 return (NULL);
280 if (!EC_GROUP_copy(t, a))
281 goto err;
7793f30e 282
0f113f3e 283 ok = 1;
7793f30e 284
0f113f3e
MC
285 err:
286 if (!ok) {
8fdc3734 287 EC_GROUP_free(t);
0f113f3e 288 return NULL;
8fdc3734 289 }
0f113f3e
MC
290 return t;
291}
7793f30e 292
48fe4d62 293const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
0f113f3e
MC
294{
295 return group->meth;
296}
48fe4d62 297
458c2917 298int EC_METHOD_get_field_type(const EC_METHOD *meth)
0f113f3e
MC
299{
300 return meth->field_type;
301}
302
303int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
304 const BIGNUM *order, const BIGNUM *cofactor)
305{
306 if (generator == NULL) {
307 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
308 return 0;
309 }
310
311 if (group->generator == NULL) {
312 group->generator = EC_POINT_new(group);
313 if (group->generator == NULL)
314 return 0;
315 }
316 if (!EC_POINT_copy(group->generator, generator))
317 return 0;
318
319 if (order != NULL) {
320 if (!BN_copy(group->order, order))
321 return 0;
322 } else
323 BN_zero(group->order);
324
325 if (cofactor != NULL) {
326 if (!BN_copy(group->cofactor, cofactor))
327 return 0;
328 } else
329 BN_zero(group->cofactor);
330
331 /*
332 * We ignore the return value because some groups have an order with
333 * factors of two, which makes the Montgomery setup fail.
334 * |group->mont_data| will be NULL in this case.
335 */
336 ec_precompute_mont_data(group);
337
338 return 1;
339}
bb62a8b0 340
9dd84053 341const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
0f113f3e
MC
342{
343 return group->generator;
344}
bb62a8b0 345
f54be179 346BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
0f113f3e
MC
347{
348 return group->mont_data;
349}
bb62a8b0 350
b6db386f 351int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
0f113f3e
MC
352{
353 if (!BN_copy(order, group->order))
354 return 0;
0657bf9c 355
0f113f3e
MC
356 return !BN_is_zero(order);
357}
0657bf9c 358
0f113f3e
MC
359int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
360 BN_CTX *ctx)
361{
362 if (!BN_copy(cofactor, group->cofactor))
363 return 0;
48fe4d62 364
0f113f3e
MC
365 return !BN_is_zero(group->cofactor);
366}
48fe4d62 367
7dc17a6c 368void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
0f113f3e
MC
369{
370 group->curve_name = nid;
371}
b6db386f 372
7dc17a6c 373int EC_GROUP_get_curve_name(const EC_GROUP *group)
0f113f3e
MC
374{
375 return group->curve_name;
376}
b6db386f 377
458c2917 378void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
0f113f3e
MC
379{
380 group->asn1_flag = flag;
381}
458c2917
BM
382
383int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
0f113f3e
MC
384{
385 return group->asn1_flag;
386}
458c2917 387
0f113f3e 388void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
254ef80d 389 point_conversion_form_t form)
0f113f3e
MC
390{
391 group->asn1_form = form;
392}
254ef80d 393
0f113f3e
MC
394point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
395 *group)
396{
397 return group->asn1_form;
398}
254ef80d 399
5f3d6f70 400size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
0f113f3e 401{
b548a1f1
RS
402 OPENSSL_free(group->seed);
403 group->seed = NULL;
404 group->seed_len = 0;
5f3d6f70 405
0f113f3e
MC
406 if (!len || !p)
407 return 1;
5f3d6f70 408
0f113f3e
MC
409 if ((group->seed = OPENSSL_malloc(len)) == NULL)
410 return 0;
411 memcpy(group->seed, p, len);
412 group->seed_len = len;
5f3d6f70 413
0f113f3e
MC
414 return len;
415}
5f3d6f70
BM
416
417unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
0f113f3e
MC
418{
419 return group->seed;
420}
5f3d6f70
BM
421
422size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
0f113f3e
MC
423{
424 return group->seed_len;
425}
426
427int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
428 const BIGNUM *b, BN_CTX *ctx)
429{
430 if (group->meth->group_set_curve == 0) {
431 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
432 return 0;
433 }
434 return group->meth->group_set_curve(group, p, a, b, ctx);
435}
436
437int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
438 BIGNUM *b, BN_CTX *ctx)
439{
440 if (group->meth->group_get_curve == 0) {
441 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
442 return 0;
443 }
444 return group->meth->group_get_curve(group, p, a, b, ctx);
445}
7793f30e 446
b3310161 447#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
448int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
449 const BIGNUM *b, BN_CTX *ctx)
450{
451 if (group->meth->group_set_curve == 0) {
452 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
453 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
454 return 0;
455 }
456 return group->meth->group_set_curve(group, p, a, b, ctx);
457}
458
459int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
460 BIGNUM *b, BN_CTX *ctx)
461{
462 if (group->meth->group_get_curve == 0) {
463 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
464 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
465 return 0;
466 }
467 return group->meth->group_get_curve(group, p, a, b, ctx);
468}
b3310161 469#endif
7793f30e
BM
470
471int EC_GROUP_get_degree(const EC_GROUP *group)
0f113f3e
MC
472{
473 if (group->meth->group_get_degree == 0) {
474 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
475 return 0;
476 }
477 return group->meth->group_get_degree(group);
478}
48fe4d62 479
17d6bb81 480int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
481{
482 if (group->meth->group_check_discriminant == 0) {
483 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
484 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
485 return 0;
486 }
487 return group->meth->group_check_discriminant(group, ctx);
488}
af28dd6c 489
ada0e717 490int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
0f113f3e
MC
491{
492 int r = 0;
493 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
494 BN_CTX *ctx_new = NULL;
495
496 /* compare the field types */
497 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
498 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
499 return 1;
500 /* compare the curve name (if present in both) */
501 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
502 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
503 return 1;
504
90945fa3 505 if (ctx == NULL)
0f113f3e 506 ctx_new = ctx = BN_CTX_new();
90945fa3 507 if (ctx == NULL)
0f113f3e
MC
508 return -1;
509
510 BN_CTX_start(ctx);
511 a1 = BN_CTX_get(ctx);
512 a2 = BN_CTX_get(ctx);
513 a3 = BN_CTX_get(ctx);
514 b1 = BN_CTX_get(ctx);
515 b2 = BN_CTX_get(ctx);
516 b3 = BN_CTX_get(ctx);
90945fa3 517 if (b3 == NULL) {
0f113f3e 518 BN_CTX_end(ctx);
23a1d5e9 519 BN_CTX_free(ctx_new);
0f113f3e
MC
520 return -1;
521 }
522
523 /*
524 * XXX This approach assumes that the external representation of curves
525 * over the same field type is the same.
526 */
527 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
528 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
529 r = 1;
530
531 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
532 r = 1;
533
534 /* XXX EC_POINT_cmp() assumes that the methods are equal */
535 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
536 EC_GROUP_get0_generator(b), ctx))
537 r = 1;
538
539 if (!r) {
540 /* compare the order and cofactor */
541 if (!EC_GROUP_get_order(a, a1, ctx) ||
542 !EC_GROUP_get_order(b, b1, ctx) ||
543 !EC_GROUP_get_cofactor(a, a2, ctx) ||
544 !EC_GROUP_get_cofactor(b, b2, ctx)) {
545 BN_CTX_end(ctx);
23a1d5e9 546 BN_CTX_free(ctx_new);
0f113f3e
MC
547 return -1;
548 }
549 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
550 r = 1;
551 }
552
553 BN_CTX_end(ctx);
23a1d5e9 554 BN_CTX_free(ctx_new);
ada0e717 555
0f113f3e
MC
556 return r;
557}
ada0e717 558
0657bf9c
BM
559/* functions for EC_POINT objects */
560
561EC_POINT *EC_POINT_new(const EC_GROUP *group)
0f113f3e
MC
562{
563 EC_POINT *ret;
564
565 if (group == NULL) {
566 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
567 return NULL;
568 }
569 if (group->meth->point_init == 0) {
570 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
571 return NULL;
572 }
573
b4faea50 574 ret = OPENSSL_malloc(sizeof(*ret));
0f113f3e
MC
575 if (ret == NULL) {
576 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
577 return NULL;
578 }
579
580 ret->meth = group->meth;
581
582 if (!ret->meth->point_init(ret)) {
583 OPENSSL_free(ret);
584 return NULL;
585 }
586
587 return ret;
588}
0657bf9c
BM
589
590void EC_POINT_free(EC_POINT *point)
0f113f3e
MC
591{
592 if (!point)
593 return;
7711de24 594
0f113f3e
MC
595 if (point->meth->point_finish != 0)
596 point->meth->point_finish(point);
597 OPENSSL_free(point);
598}
0657bf9c
BM
599
600void EC_POINT_clear_free(EC_POINT *point)
0f113f3e
MC
601{
602 if (!point)
603 return;
604
605 if (point->meth->point_clear_finish != 0)
606 point->meth->point_clear_finish(point);
607 else if (point->meth->point_finish != 0)
608 point->meth->point_finish(point);
b4faea50 609 OPENSSL_clear_free(point, sizeof(*point));
0f113f3e 610}
0657bf9c
BM
611
612int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
0f113f3e
MC
613{
614 if (dest->meth->point_copy == 0) {
615 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
616 return 0;
617 }
618 if (dest->meth != src->meth) {
619 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
620 return 0;
621 }
622 if (dest == src)
623 return 1;
624 return dest->meth->point_copy(dest, src);
625}
0657bf9c 626
7793f30e 627EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
0f113f3e
MC
628{
629 EC_POINT *t;
630 int r;
631
632 if (a == NULL)
633 return NULL;
634
635 t = EC_POINT_new(group);
636 if (t == NULL)
637 return (NULL);
638 r = EC_POINT_copy(t, a);
639 if (!r) {
640 EC_POINT_free(t);
641 return NULL;
8fdc3734
RS
642 }
643 return t;
0f113f3e 644}
7793f30e 645
48fe4d62 646const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
0f113f3e
MC
647{
648 return point->meth;
649}
48fe4d62 650
226cc7de 651int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
0f113f3e
MC
652{
653 if (group->meth->point_set_to_infinity == 0) {
654 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
655 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
656 return 0;
657 }
658 if (group->meth != point->meth) {
659 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
660 return 0;
661 }
662 return group->meth->point_set_to_infinity(group, point);
663}
664
665int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
666 EC_POINT *point, const BIGNUM *x,
667 const BIGNUM *y, const BIGNUM *z,
668 BN_CTX *ctx)
669{
670 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
671 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
672 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
673 return 0;
674 }
675 if (group->meth != point->meth) {
676 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
677 EC_R_INCOMPATIBLE_OBJECTS);
678 return 0;
679 }
680 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
681 y, z, ctx);
682}
683
684int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
685 const EC_POINT *point, BIGNUM *x,
686 BIGNUM *y, BIGNUM *z,
687 BN_CTX *ctx)
688{
689 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
690 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
691 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
692 return 0;
693 }
694 if (group->meth != point->meth) {
695 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
696 EC_R_INCOMPATIBLE_OBJECTS);
697 return 0;
698 }
699 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
700 y, z, ctx);
701}
702
703int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
704 EC_POINT *point, const BIGNUM *x,
705 const BIGNUM *y, BN_CTX *ctx)
706{
707 if (group->meth->point_set_affine_coordinates == 0) {
708 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
709 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
710 return 0;
711 }
712 if (group->meth != point->meth) {
713 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
714 EC_R_INCOMPATIBLE_OBJECTS);
715 return 0;
716 }
717 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
718}
226cc7de 719
b3310161 720#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
721int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
722 EC_POINT *point, const BIGNUM *x,
723 const BIGNUM *y, BN_CTX *ctx)
724{
725 if (group->meth->point_set_affine_coordinates == 0) {
726 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
727 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
728 return 0;
729 }
730 if (group->meth != point->meth) {
731 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
732 EC_R_INCOMPATIBLE_OBJECTS);
733 return 0;
734 }
735 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
736}
b3310161 737#endif
7793f30e 738
0f113f3e
MC
739int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
740 const EC_POINT *point, BIGNUM *x,
741 BIGNUM *y, BN_CTX *ctx)
742{
743 if (group->meth->point_get_affine_coordinates == 0) {
744 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
745 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
746 return 0;
747 }
748 if (group->meth != point->meth) {
749 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
750 EC_R_INCOMPATIBLE_OBJECTS);
751 return 0;
752 }
753 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
754}
7793f30e 755
b3310161 756#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
757int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
758 const EC_POINT *point, BIGNUM *x,
759 BIGNUM *y, BN_CTX *ctx)
760{
761 if (group->meth->point_get_affine_coordinates == 0) {
762 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
763 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
764 return 0;
765 }
766 if (group->meth != point->meth) {
767 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
768 EC_R_INCOMPATIBLE_OBJECTS);
769 return 0;
770 }
771 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
772}
b3310161 773#endif
7793f30e 774
0f113f3e
MC
775int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
776 const EC_POINT *b, BN_CTX *ctx)
777{
778 if (group->meth->add == 0) {
779 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
780 return 0;
781 }
782 if ((group->meth != r->meth) || (r->meth != a->meth)
783 || (a->meth != b->meth)) {
784 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
785 return 0;
786 }
787 return group->meth->add(group, r, a, b, ctx);
788}
789
790int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
791 BN_CTX *ctx)
792{
793 if (group->meth->dbl == 0) {
794 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
795 return 0;
796 }
797 if ((group->meth != r->meth) || (r->meth != a->meth)) {
798 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
799 return 0;
800 }
801 return group->meth->dbl(group, r, a, ctx);
802}
0657bf9c 803
1d5bd6cf 804int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
0f113f3e
MC
805{
806 if (group->meth->invert == 0) {
807 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
808 return 0;
809 }
810 if (group->meth != a->meth) {
811 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
812 return 0;
813 }
814 return group->meth->invert(group, a, ctx);
815}
1d5bd6cf 816
0657bf9c 817int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
0f113f3e
MC
818{
819 if (group->meth->is_at_infinity == 0) {
820 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
821 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
822 return 0;
823 }
824 if (group->meth != point->meth) {
825 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
826 return 0;
827 }
828 return group->meth->is_at_infinity(group, point);
829}
830
68886be7
MC
831/*
832 * Check whether an EC_POINT is on the curve or not. Note that the return
833 * value for this function should NOT be treated as a boolean. Return values:
834 * 1: The point is on the curve
835 * 0: The point is not on the curve
836 * -1: An error occurred
837 */
0f113f3e
MC
838int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
839 BN_CTX *ctx)
840{
841 if (group->meth->is_on_curve == 0) {
842 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
843 return 0;
844 }
845 if (group->meth != point->meth) {
846 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
847 return 0;
848 }
849 return group->meth->is_on_curve(group, point, ctx);
850}
851
852int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
853 BN_CTX *ctx)
854{
855 if (group->meth->point_cmp == 0) {
856 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
857 return -1;
858 }
859 if ((group->meth != a->meth) || (a->meth != b->meth)) {
860 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
861 return -1;
862 }
863 return group->meth->point_cmp(group, a, b, ctx);
864}
1d5bd6cf 865
e869d4bd 866int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
0f113f3e
MC
867{
868 if (group->meth->make_affine == 0) {
869 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
870 return 0;
871 }
872 if (group->meth != point->meth) {
873 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
874 return 0;
875 }
876 return group->meth->make_affine(group, point, ctx);
877}
878
879int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
880 EC_POINT *points[], BN_CTX *ctx)
881{
882 size_t i;
883
884 if (group->meth->points_make_affine == 0) {
885 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
886 return 0;
887 }
888 for (i = 0; i < num; i++) {
889 if (group->meth != points[i]->meth) {
890 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
891 return 0;
892 }
893 }
894 return group->meth->points_make_affine(group, num, points, ctx);
895}
896
897/*
898 * Functions for point multiplication. If group->meth->mul is 0, we use the
899 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
900 * methods.
37c660ff
BM
901 */
902
903int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
0f113f3e
MC
904 size_t num, const EC_POINT *points[],
905 const BIGNUM *scalars[], BN_CTX *ctx)
906{
907 if (group->meth->mul == 0)
908 /* use default */
909 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
37c660ff 910
0f113f3e
MC
911 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
912}
37c660ff
BM
913
914int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
0f113f3e
MC
915 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
916{
917 /* just a convenient interface to EC_POINTs_mul() */
37c660ff 918
0f113f3e
MC
919 const EC_POINT *points[1];
920 const BIGNUM *scalars[1];
37c660ff 921
0f113f3e
MC
922 points[0] = point;
923 scalars[0] = p_scalar;
37c660ff 924
0f113f3e
MC
925 return EC_POINTs_mul(group, r, g_scalar,
926 (point != NULL
927 && p_scalar != NULL), points, scalars, ctx);
928}
37c660ff
BM
929
930int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
931{
932 if (group->meth->mul == 0)
933 /* use default */
934 return ec_wNAF_precompute_mult(group, ctx);
37c660ff 935
0f113f3e
MC
936 if (group->meth->precompute_mult != 0)
937 return group->meth->precompute_mult(group, ctx);
938 else
939 return 1; /* nothing to do, so report success */
940}
37c660ff
BM
941
942int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
0f113f3e
MC
943{
944 if (group->meth->mul == 0)
945 /* use default */
946 return ec_wNAF_have_precompute_mult(group);
947
948 if (group->meth->have_precompute_mult != 0)
949 return group->meth->have_precompute_mult(group);
950 else
951 return 0; /* cannot tell whether precomputation has
952 * been performed */
953}
954
955/*
956 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
957 * returns one on success. On error it returns zero.
958 */
f54be179 959int ec_precompute_mont_data(EC_GROUP *group)
0f113f3e
MC
960{
961 BN_CTX *ctx = BN_CTX_new();
962 int ret = 0;
963
23a1d5e9
RS
964 BN_MONT_CTX_free(group->mont_data);
965 group->mont_data = NULL;
0f113f3e
MC
966
967 if (ctx == NULL)
968 goto err;
969
970 group->mont_data = BN_MONT_CTX_new();
90945fa3 971 if (group->mont_data == NULL)
0f113f3e
MC
972 goto err;
973
974 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
975 BN_MONT_CTX_free(group->mont_data);
976 group->mont_data = NULL;
977 goto err;
978 }
979
980 ret = 1;
981
982 err:
983
23a1d5e9 984 BN_CTX_free(ctx);
0f113f3e
MC
985 return ret;
986}
3aef36ff
RS
987
988int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
989{
990 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
991}
992
993void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
994{
995 return CRYPTO_get_ex_data(&key->ex_data, idx);
996}