]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_lib.c
Mark generated functions unused (applies to safestack, lhash, sparse_array)
[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 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
b14e6015
MC
143 dest->curve_name = src->curve_name;
144
3aef36ff
RS
145 /* Copy precomputed */
146 dest->pre_comp_type = src->pre_comp_type;
147 switch (src->pre_comp_type) {
f3b3d7f0 148 case PCT_none:
3aef36ff
RS
149 dest->pre_comp.ec = NULL;
150 break;
66117ab0 151 case PCT_nistz256:
f3b3d7f0 152#ifdef ECP_NISTZ256_ASM
3aef36ff 153 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
e69aa800 154#endif
f3b3d7f0 155 break;
3aef36ff 156#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
66117ab0 157 case PCT_nistp224:
3aef36ff
RS
158 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
159 break;
66117ab0 160 case PCT_nistp256:
3aef36ff
RS
161 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
162 break;
66117ab0 163 case PCT_nistp521:
3aef36ff
RS
164 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
165 break;
f3b3d7f0
RS
166#else
167 case PCT_nistp224:
168 case PCT_nistp256:
169 case PCT_nistp521:
170 break;
3aef36ff 171#endif
66117ab0 172 case PCT_ec:
3aef36ff
RS
173 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
174 break;
0f113f3e
MC
175 }
176
177 if (src->mont_data != NULL) {
178 if (dest->mont_data == NULL) {
179 dest->mont_data = BN_MONT_CTX_new();
180 if (dest->mont_data == NULL)
181 return 0;
182 }
183 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
184 return 0;
185 } else {
186 /* src->generator == NULL */
23a1d5e9
RS
187 BN_MONT_CTX_free(dest->mont_data);
188 dest->mont_data = NULL;
0f113f3e 189 }
0657bf9c 190
0f113f3e
MC
191 if (src->generator != NULL) {
192 if (dest->generator == NULL) {
193 dest->generator = EC_POINT_new(dest);
194 if (dest->generator == NULL)
195 return 0;
196 }
197 if (!EC_POINT_copy(dest->generator, src->generator))
198 return 0;
199 } else {
200 /* src->generator == NULL */
8fdc3734
RS
201 EC_POINT_clear_free(dest->generator);
202 dest->generator = NULL;
0f113f3e
MC
203 }
204
6903e2e7
DSH
205 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
206 if (!BN_copy(dest->order, src->order))
207 return 0;
208 if (!BN_copy(dest->cofactor, src->cofactor))
209 return 0;
210 }
0f113f3e 211
0f113f3e
MC
212 dest->asn1_flag = src->asn1_flag;
213 dest->asn1_form = src->asn1_form;
214
215 if (src->seed) {
b548a1f1 216 OPENSSL_free(dest->seed);
cdb10bae
RS
217 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
218 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
0f113f3e 219 return 0;
cdb10bae 220 }
0f113f3e
MC
221 if (!memcpy(dest->seed, src->seed, src->seed_len))
222 return 0;
223 dest->seed_len = src->seed_len;
224 } else {
b548a1f1 225 OPENSSL_free(dest->seed);
0f113f3e
MC
226 dest->seed = NULL;
227 dest->seed_len = 0;
228 }
229
230 return dest->meth->group_copy(dest, src);
231}
0657bf9c 232
7793f30e 233EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
0f113f3e
MC
234{
235 EC_GROUP *t = NULL;
236 int ok = 0;
7793f30e 237
0f113f3e
MC
238 if (a == NULL)
239 return NULL;
7793f30e 240
0f113f3e 241 if ((t = EC_GROUP_new(a->meth)) == NULL)
26a7d938 242 return NULL;
0f113f3e
MC
243 if (!EC_GROUP_copy(t, a))
244 goto err;
7793f30e 245
0f113f3e 246 ok = 1;
7793f30e 247
0f113f3e
MC
248 err:
249 if (!ok) {
8fdc3734 250 EC_GROUP_free(t);
0f113f3e 251 return NULL;
8fdc3734 252 }
0f113f3e
MC
253 return t;
254}
7793f30e 255
48fe4d62 256const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
0f113f3e
MC
257{
258 return group->meth;
259}
48fe4d62 260
458c2917 261int EC_METHOD_get_field_type(const EC_METHOD *meth)
0f113f3e
MC
262{
263 return meth->field_type;
264}
265
eb791696
AP
266static int ec_precompute_mont_data(EC_GROUP *);
267
0f113f3e
MC
268int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
269 const BIGNUM *order, const BIGNUM *cofactor)
270{
271 if (generator == NULL) {
272 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
273 return 0;
274 }
275
276 if (group->generator == NULL) {
277 group->generator = EC_POINT_new(group);
278 if (group->generator == NULL)
279 return 0;
280 }
281 if (!EC_POINT_copy(group->generator, generator))
282 return 0;
283
284 if (order != NULL) {
285 if (!BN_copy(group->order, order))
286 return 0;
287 } else
288 BN_zero(group->order);
289
290 if (cofactor != NULL) {
291 if (!BN_copy(group->cofactor, cofactor))
292 return 0;
293 } else
294 BN_zero(group->cofactor);
295
296 /*
3a6a4a93 297 * Some groups have an order with
0f113f3e
MC
298 * factors of two, which makes the Montgomery setup fail.
299 * |group->mont_data| will be NULL in this case.
300 */
3a6a4a93
BB
301 if (BN_is_odd(group->order)) {
302 return ec_precompute_mont_data(group);
303 }
0f113f3e 304
3a6a4a93
BB
305 BN_MONT_CTX_free(group->mont_data);
306 group->mont_data = NULL;
0f113f3e
MC
307 return 1;
308}
bb62a8b0 309
9dd84053 310const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
0f113f3e
MC
311{
312 return group->generator;
313}
bb62a8b0 314
f54be179 315BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
0f113f3e
MC
316{
317 return group->mont_data;
318}
bb62a8b0 319
b6db386f 320int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
0f113f3e 321{
be2e334f
DSH
322 if (group->order == NULL)
323 return 0;
0f113f3e
MC
324 if (!BN_copy(order, group->order))
325 return 0;
0657bf9c 326
0f113f3e
MC
327 return !BN_is_zero(order);
328}
0657bf9c 329
be2e334f
DSH
330const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
331{
332 return group->order;
333}
334
335int EC_GROUP_order_bits(const EC_GROUP *group)
336{
77470e98 337 return group->meth->group_order_bits(group);
be2e334f
DSH
338}
339
0f113f3e
MC
340int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
341 BN_CTX *ctx)
342{
be2e334f
DSH
343
344 if (group->cofactor == NULL)
345 return 0;
0f113f3e
MC
346 if (!BN_copy(cofactor, group->cofactor))
347 return 0;
48fe4d62 348
0f113f3e
MC
349 return !BN_is_zero(group->cofactor);
350}
48fe4d62 351
be2e334f
DSH
352const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
353{
354 return group->cofactor;
355}
356
7dc17a6c 357void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
0f113f3e
MC
358{
359 group->curve_name = nid;
360}
b6db386f 361
7dc17a6c 362int EC_GROUP_get_curve_name(const EC_GROUP *group)
0f113f3e
MC
363{
364 return group->curve_name;
365}
b6db386f 366
458c2917 367void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
0f113f3e
MC
368{
369 group->asn1_flag = flag;
370}
458c2917
BM
371
372int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
0f113f3e
MC
373{
374 return group->asn1_flag;
375}
458c2917 376
0f113f3e 377void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
254ef80d 378 point_conversion_form_t form)
0f113f3e
MC
379{
380 group->asn1_form = form;
381}
254ef80d 382
0f113f3e
MC
383point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
384 *group)
385{
386 return group->asn1_form;
387}
254ef80d 388
5f3d6f70 389size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
0f113f3e 390{
b548a1f1
RS
391 OPENSSL_free(group->seed);
392 group->seed = NULL;
393 group->seed_len = 0;
5f3d6f70 394
0f113f3e
MC
395 if (!len || !p)
396 return 1;
5f3d6f70 397
f06080cb
F
398 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
399 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
0f113f3e 400 return 0;
f06080cb 401 }
0f113f3e
MC
402 memcpy(group->seed, p, len);
403 group->seed_len = len;
5f3d6f70 404
0f113f3e
MC
405 return len;
406}
5f3d6f70
BM
407
408unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
0f113f3e
MC
409{
410 return group->seed;
411}
5f3d6f70
BM
412
413size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
0f113f3e
MC
414{
415 return group->seed_len;
416}
417
8e3cced7
MC
418int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
419 const BIGNUM *b, BN_CTX *ctx)
0f113f3e
MC
420{
421 if (group->meth->group_set_curve == 0) {
8e3cced7 422 ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
423 return 0;
424 }
425 return group->meth->group_set_curve(group, p, a, b, ctx);
426}
427
8e3cced7
MC
428int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
429 BN_CTX *ctx)
0f113f3e 430{
8e3cced7
MC
431 if (group->meth->group_get_curve == NULL) {
432 ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
433 return 0;
434 }
435 return group->meth->group_get_curve(group, p, a, b, ctx);
436}
7793f30e 437
fcd2d5a6 438#if !OPENSSL_API_3
8e3cced7
MC
439int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
440 const BIGNUM *b, BN_CTX *ctx)
441{
442 return EC_GROUP_set_curve(group, p, a, b, ctx);
443}
444
445int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
446 BIGNUM *b, BN_CTX *ctx)
447{
448 return EC_GROUP_get_curve(group, p, a, b, ctx);
449}
450
50db8163 451# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
452int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
453 const BIGNUM *b, BN_CTX *ctx)
454{
8e3cced7 455 return EC_GROUP_set_curve(group, p, a, b, ctx);
0f113f3e
MC
456}
457
458int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
459 BIGNUM *b, BN_CTX *ctx)
460{
8e3cced7 461 return EC_GROUP_get_curve(group, p, a, b, ctx);
0f113f3e 462}
50db8163 463# endif
b3310161 464#endif
7793f30e
BM
465
466int EC_GROUP_get_degree(const EC_GROUP *group)
0f113f3e
MC
467{
468 if (group->meth->group_get_degree == 0) {
469 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
470 return 0;
471 }
472 return group->meth->group_get_degree(group);
473}
48fe4d62 474
17d6bb81 475int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
476{
477 if (group->meth->group_check_discriminant == 0) {
478 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
479 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
480 return 0;
481 }
482 return group->meth->group_check_discriminant(group, ctx);
483}
af28dd6c 484
ada0e717 485int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
0f113f3e
MC
486{
487 int r = 0;
488 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
489 BN_CTX *ctx_new = NULL;
490
491 /* compare the field types */
492 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
493 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
494 return 1;
495 /* compare the curve name (if present in both) */
496 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
497 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
498 return 1;
6903e2e7
DSH
499 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
500 return 0;
0f113f3e 501
90945fa3 502 if (ctx == NULL)
0f113f3e 503 ctx_new = ctx = BN_CTX_new();
90945fa3 504 if (ctx == NULL)
0f113f3e
MC
505 return -1;
506
507 BN_CTX_start(ctx);
508 a1 = BN_CTX_get(ctx);
509 a2 = BN_CTX_get(ctx);
510 a3 = BN_CTX_get(ctx);
511 b1 = BN_CTX_get(ctx);
512 b2 = BN_CTX_get(ctx);
513 b3 = BN_CTX_get(ctx);
90945fa3 514 if (b3 == NULL) {
0f113f3e 515 BN_CTX_end(ctx);
23a1d5e9 516 BN_CTX_free(ctx_new);
0f113f3e
MC
517 return -1;
518 }
519
520 /*
521 * XXX This approach assumes that the external representation of curves
522 * over the same field type is the same.
523 */
524 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
525 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
526 r = 1;
527
528 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
529 r = 1;
530
531 /* XXX EC_POINT_cmp() assumes that the methods are equal */
532 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
533 EC_GROUP_get0_generator(b), ctx))
534 r = 1;
535
536 if (!r) {
be2e334f 537 const BIGNUM *ao, *bo, *ac, *bc;
0f113f3e 538 /* compare the order and cofactor */
be2e334f
DSH
539 ao = EC_GROUP_get0_order(a);
540 bo = EC_GROUP_get0_order(b);
541 ac = EC_GROUP_get0_cofactor(a);
542 bc = EC_GROUP_get0_cofactor(b);
543 if (ao == NULL || bo == NULL) {
0f113f3e 544 BN_CTX_end(ctx);
23a1d5e9 545 BN_CTX_free(ctx_new);
0f113f3e
MC
546 return -1;
547 }
be2e334f 548 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
0f113f3e
MC
549 r = 1;
550 }
551
552 BN_CTX_end(ctx);
23a1d5e9 553 BN_CTX_free(ctx_new);
ada0e717 554
0f113f3e
MC
555 return r;
556}
ada0e717 557
0657bf9c
BM
558/* functions for EC_POINT objects */
559
560EC_POINT *EC_POINT_new(const EC_GROUP *group)
0f113f3e
MC
561{
562 EC_POINT *ret;
563
564 if (group == NULL) {
565 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
566 return NULL;
567 }
f06080cb 568 if (group->meth->point_init == NULL) {
0f113f3e
MC
569 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
570 return NULL;
571 }
572
1b4cf96f 573 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
574 if (ret == NULL) {
575 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
576 return NULL;
577 }
578
579 ret->meth = group->meth;
b14e6015 580 ret->curve_name = group->curve_name;
0f113f3e
MC
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 }
b14e6015
MC
618 if (dest->meth != src->meth
619 || (dest->curve_name != src->curve_name
620 && dest->curve_name != 0
621 && src->curve_name != 0)) {
0f113f3e
MC
622 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
623 return 0;
624 }
625 if (dest == src)
626 return 1;
627 return dest->meth->point_copy(dest, src);
628}
0657bf9c 629
7793f30e 630EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
0f113f3e
MC
631{
632 EC_POINT *t;
633 int r;
634
635 if (a == NULL)
636 return NULL;
637
638 t = EC_POINT_new(group);
639 if (t == NULL)
26a7d938 640 return NULL;
0f113f3e
MC
641 r = EC_POINT_copy(t, a);
642 if (!r) {
643 EC_POINT_free(t);
644 return NULL;
8fdc3734
RS
645 }
646 return t;
0f113f3e 647}
7793f30e 648
48fe4d62 649const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
0f113f3e
MC
650{
651 return point->meth;
652}
48fe4d62 653
226cc7de 654int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
0f113f3e
MC
655{
656 if (group->meth->point_set_to_infinity == 0) {
657 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
658 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
659 return 0;
660 }
661 if (group->meth != point->meth) {
662 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
663 return 0;
664 }
665 return group->meth->point_set_to_infinity(group, point);
666}
667
668int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
669 EC_POINT *point, const BIGNUM *x,
670 const BIGNUM *y, const BIGNUM *z,
671 BN_CTX *ctx)
672{
673 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
674 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
675 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
676 return 0;
677 }
b14e6015 678 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
679 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
680 EC_R_INCOMPATIBLE_OBJECTS);
681 return 0;
682 }
683 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
684 y, z, ctx);
685}
686
687int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
688 const EC_POINT *point, BIGNUM *x,
689 BIGNUM *y, BIGNUM *z,
690 BN_CTX *ctx)
691{
692 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
693 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
694 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
695 return 0;
696 }
b14e6015 697 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
698 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
699 EC_R_INCOMPATIBLE_OBJECTS);
700 return 0;
701 }
702 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
703 y, z, ctx);
704}
705
8e3cced7
MC
706int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
707 const BIGNUM *x, const BIGNUM *y,
708 BN_CTX *ctx)
0f113f3e 709{
8e3cced7
MC
710 if (group->meth->point_set_affine_coordinates == NULL) {
711 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
0f113f3e
MC
712 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
713 return 0;
714 }
b14e6015 715 if (!ec_point_is_compat(point, group)) {
8e3cced7 716 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
717 return 0;
718 }
1e2012b7
EK
719 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
720 return 0;
721
722 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
8e3cced7 723 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
1e2012b7
EK
724 return 0;
725 }
726 return 1;
0f113f3e 727}
226cc7de 728
fcd2d5a6 729#if !OPENSSL_API_3
8e3cced7
MC
730int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
731 EC_POINT *point, const BIGNUM *x,
732 const BIGNUM *y, BN_CTX *ctx)
733{
734 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
735}
736
50db8163 737# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
738int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
739 EC_POINT *point, const BIGNUM *x,
740 const BIGNUM *y, BN_CTX *ctx)
741{
8e3cced7
MC
742 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
743}
50db8163 744# endif
8e3cced7
MC
745#endif
746
747int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
748 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
749 BN_CTX *ctx)
750{
751 if (group->meth->point_get_affine_coordinates == NULL) {
752 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
0f113f3e
MC
753 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
754 return 0;
755 }
b14e6015 756 if (!ec_point_is_compat(point, group)) {
8e3cced7 757 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
1e2012b7
EK
758 return 0;
759 }
bfb10b97
BB
760 if (EC_POINT_is_at_infinity(group, point)) {
761 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
762 return 0;
763 }
8e3cced7 764 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 765}
7793f30e 766
fcd2d5a6 767#if !OPENSSL_API_3
0f113f3e
MC
768int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
769 const EC_POINT *point, BIGNUM *x,
770 BIGNUM *y, BN_CTX *ctx)
771{
8e3cced7 772 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 773}
7793f30e 774
50db8163 775# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
776int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
777 const EC_POINT *point, BIGNUM *x,
778 BIGNUM *y, BN_CTX *ctx)
779{
8e3cced7 780 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 781}
50db8163 782# endif
b3310161 783#endif
7793f30e 784
0f113f3e
MC
785int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
786 const EC_POINT *b, BN_CTX *ctx)
787{
788 if (group->meth->add == 0) {
789 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
790 return 0;
791 }
b14e6015
MC
792 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
793 || !ec_point_is_compat(b, group)) {
0f113f3e
MC
794 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
795 return 0;
796 }
797 return group->meth->add(group, r, a, b, ctx);
798}
799
800int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
801 BN_CTX *ctx)
802{
803 if (group->meth->dbl == 0) {
804 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
805 return 0;
806 }
b14e6015 807 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
0f113f3e
MC
808 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
809 return 0;
810 }
811 return group->meth->dbl(group, r, a, ctx);
812}
0657bf9c 813
1d5bd6cf 814int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
0f113f3e
MC
815{
816 if (group->meth->invert == 0) {
817 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
818 return 0;
819 }
b14e6015 820 if (!ec_point_is_compat(a, group)) {
0f113f3e
MC
821 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
822 return 0;
823 }
824 return group->meth->invert(group, a, ctx);
825}
1d5bd6cf 826
0657bf9c 827int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
0f113f3e
MC
828{
829 if (group->meth->is_at_infinity == 0) {
830 ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
831 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
832 return 0;
833 }
b14e6015 834 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
835 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
836 return 0;
837 }
838 return group->meth->is_at_infinity(group, point);
839}
840
68886be7
MC
841/*
842 * Check whether an EC_POINT is on the curve or not. Note that the return
843 * value for this function should NOT be treated as a boolean. Return values:
844 * 1: The point is on the curve
845 * 0: The point is not on the curve
846 * -1: An error occurred
847 */
0f113f3e
MC
848int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
849 BN_CTX *ctx)
850{
851 if (group->meth->is_on_curve == 0) {
852 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
853 return 0;
854 }
b14e6015 855 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
856 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
857 return 0;
858 }
859 return group->meth->is_on_curve(group, point, ctx);
860}
861
862int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
863 BN_CTX *ctx)
864{
865 if (group->meth->point_cmp == 0) {
866 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
867 return -1;
868 }
b14e6015 869 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
0f113f3e
MC
870 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
871 return -1;
872 }
873 return group->meth->point_cmp(group, a, b, ctx);
874}
1d5bd6cf 875
e869d4bd 876int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
0f113f3e
MC
877{
878 if (group->meth->make_affine == 0) {
879 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
880 return 0;
881 }
b14e6015 882 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
883 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
884 return 0;
885 }
886 return group->meth->make_affine(group, point, ctx);
887}
888
889int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
890 EC_POINT *points[], BN_CTX *ctx)
891{
892 size_t i;
893
894 if (group->meth->points_make_affine == 0) {
895 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
896 return 0;
897 }
898 for (i = 0; i < num; i++) {
b14e6015 899 if (!ec_point_is_compat(points[i], group)) {
0f113f3e
MC
900 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
901 return 0;
902 }
903 }
904 return group->meth->points_make_affine(group, num, points, ctx);
905}
906
907/*
908 * Functions for point multiplication. If group->meth->mul is 0, we use the
909 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
910 * methods.
37c660ff
BM
911 */
912
913int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
0f113f3e
MC
914 size_t num, const EC_POINT *points[],
915 const BIGNUM *scalars[], BN_CTX *ctx)
916{
01ad66f8
NT
917 int ret = 0;
918 size_t i = 0;
919 BN_CTX *new_ctx = NULL;
920
921 if ((scalar == NULL) && (num == 0)) {
922 return EC_POINT_set_to_infinity(group, r);
923 }
924
925 if (!ec_point_is_compat(r, group)) {
926 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
927 return 0;
928 }
929 for (i = 0; i < num; i++) {
930 if (!ec_point_is_compat(points[i], group)) {
931 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
932 return 0;
933 }
934 }
935
936 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
937 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
938 return 0;
939 }
940
941 if (group->meth->mul != NULL)
942 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
943 else
0f113f3e 944 /* use default */
01ad66f8 945 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
37c660ff 946
01ad66f8
NT
947 BN_CTX_free(new_ctx);
948 return ret;
0f113f3e 949}
37c660ff
BM
950
951int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
0f113f3e
MC
952 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
953{
954 /* just a convenient interface to EC_POINTs_mul() */
37c660ff 955
0f113f3e
MC
956 const EC_POINT *points[1];
957 const BIGNUM *scalars[1];
37c660ff 958
0f113f3e
MC
959 points[0] = point;
960 scalars[0] = p_scalar;
37c660ff 961
0f113f3e
MC
962 return EC_POINTs_mul(group, r, g_scalar,
963 (point != NULL
964 && p_scalar != NULL), points, scalars, ctx);
965}
37c660ff
BM
966
967int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
968{
969 if (group->meth->mul == 0)
970 /* use default */
971 return ec_wNAF_precompute_mult(group, ctx);
37c660ff 972
0f113f3e
MC
973 if (group->meth->precompute_mult != 0)
974 return group->meth->precompute_mult(group, ctx);
975 else
976 return 1; /* nothing to do, so report success */
977}
37c660ff
BM
978
979int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
0f113f3e
MC
980{
981 if (group->meth->mul == 0)
982 /* use default */
983 return ec_wNAF_have_precompute_mult(group);
984
985 if (group->meth->have_precompute_mult != 0)
986 return group->meth->have_precompute_mult(group);
987 else
988 return 0; /* cannot tell whether precomputation has
989 * been performed */
990}
991
992/*
993 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
994 * returns one on success. On error it returns zero.
995 */
eb791696 996static int ec_precompute_mont_data(EC_GROUP *group)
0f113f3e
MC
997{
998 BN_CTX *ctx = BN_CTX_new();
999 int ret = 0;
1000
23a1d5e9
RS
1001 BN_MONT_CTX_free(group->mont_data);
1002 group->mont_data = NULL;
0f113f3e
MC
1003
1004 if (ctx == NULL)
1005 goto err;
1006
1007 group->mont_data = BN_MONT_CTX_new();
90945fa3 1008 if (group->mont_data == NULL)
0f113f3e
MC
1009 goto err;
1010
1011 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1012 BN_MONT_CTX_free(group->mont_data);
1013 group->mont_data = NULL;
1014 goto err;
1015 }
1016
1017 ret = 1;
1018
1019 err:
1020
23a1d5e9 1021 BN_CTX_free(ctx);
0f113f3e
MC
1022 return ret;
1023}
3aef36ff
RS
1024
1025int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1026{
1027 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1028}
1029
1030void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1031{
1032 return CRYPTO_get_ex_data(&key->ex_data, idx);
1033}
77470e98
DSH
1034
1035int ec_group_simple_order_bits(const EC_GROUP *group)
1036{
1037 if (group->order == NULL)
1038 return 0;
1039 return BN_num_bits(group->order);
1040}
eb791696 1041
c11d372b 1042static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
792546eb 1043 const BIGNUM *x, BN_CTX *ctx)
c11d372b 1044{
262dccc0 1045 BIGNUM *e = NULL;
c11d372b
BB
1046 BN_CTX *new_ctx = NULL;
1047 int ret = 0;
1048
792546eb
BB
1049 if (group->mont_data == NULL)
1050 return 0;
1051
c11d372b
BB
1052 if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
1053 return 0;
1054
1055 BN_CTX_start(ctx);
262dccc0 1056 if ((e = BN_CTX_get(ctx)) == NULL)
c11d372b
BB
1057 goto err;
1058
792546eb
BB
1059 /*-
1060 * We want inverse in constant time, therefore we utilize the fact
1061 * order must be prime and use Fermats Little Theorem instead.
1062 */
1063 if (!BN_set_word(e, 2))
1064 goto err;
1065 if (!BN_sub(e, group->order, e))
1066 goto err;
1067 /*-
1068 * Exponent e is public.
1069 * No need for scatter-gather or BN_FLG_CONSTTIME.
1070 */
1071 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1072 goto err;
c11d372b 1073
792546eb 1074 ret = 1;
c11d372b
BB
1075
1076 err:
262dccc0
BB
1077 if (ctx != NULL)
1078 BN_CTX_end(ctx);
c11d372b
BB
1079 BN_CTX_free(new_ctx);
1080 return ret;
1081}
1082
792546eb
BB
1083/*-
1084 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1085 * - When group->order is even, this function returns an error.
1086 * - When group->order is otherwise composite, the correctness
1087 * of the output is not guaranteed.
1088 * - When x is outside the range [1, group->order), the correctness
1089 * of the output is not guaranteed.
1090 * - Otherwise, this function returns the multiplicative inverse in the
1091 * range [1, group->order).
1092 *
1093 * EC_METHODs must implement their own field_inverse_mod_ord for
1094 * other functionality.
1095 */
1096int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1097 const BIGNUM *x, BN_CTX *ctx)
eb791696
AP
1098{
1099 if (group->meth->field_inverse_mod_ord != NULL)
1100 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1101 else
c11d372b 1102 return ec_field_inverse_mod_ord(group, res, x, ctx);
eb791696 1103}
f667820c
SH
1104
1105/*-
1106 * Coordinate blinding for EC_POINT.
1107 *
1108 * The underlying EC_METHOD can optionally implement this function:
1109 * underlying implementations should return 0 on errors, or 1 on
1110 * success.
1111 *
1112 * This wrapper returns 1 in case the underlying EC_METHOD does not
1113 * support coordinate blinding.
1114 */
1115int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
1116{
1117 if (group->meth->blind_coordinates == NULL)
1118 return 1; /* ignore if not implemented */
1119
1120 return group->meth->blind_coordinates(group, p, ctx);
1121}