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