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