]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_lib.c
Save and restore the Windows error around TlsGetValue.
[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);
cdb10bae
RS
216 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
217 ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
0f113f3e 218 return 0;
cdb10bae 219 }
0f113f3e
MC
220 if (!memcpy(dest->seed, src->seed, src->seed_len))
221 return 0;
222 dest->seed_len = src->seed_len;
223 } else {
b548a1f1 224 OPENSSL_free(dest->seed);
0f113f3e
MC
225 dest->seed = NULL;
226 dest->seed_len = 0;
227 }
228
229 return dest->meth->group_copy(dest, src);
230}
0657bf9c 231
7793f30e 232EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
0f113f3e
MC
233{
234 EC_GROUP *t = NULL;
235 int ok = 0;
7793f30e 236
0f113f3e
MC
237 if (a == NULL)
238 return NULL;
7793f30e 239
0f113f3e 240 if ((t = EC_GROUP_new(a->meth)) == NULL)
26a7d938 241 return NULL;
0f113f3e
MC
242 if (!EC_GROUP_copy(t, a))
243 goto err;
7793f30e 244
0f113f3e 245 ok = 1;
7793f30e 246
0f113f3e
MC
247 err:
248 if (!ok) {
8fdc3734 249 EC_GROUP_free(t);
0f113f3e 250 return NULL;
8fdc3734 251 }
0f113f3e
MC
252 return t;
253}
7793f30e 254
48fe4d62 255const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
0f113f3e
MC
256{
257 return group->meth;
258}
48fe4d62 259
458c2917 260int EC_METHOD_get_field_type(const EC_METHOD *meth)
0f113f3e
MC
261{
262 return meth->field_type;
263}
264
eb791696
AP
265static int ec_precompute_mont_data(EC_GROUP *);
266
0f113f3e
MC
267int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
268 const BIGNUM *order, const BIGNUM *cofactor)
269{
270 if (generator == NULL) {
271 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
272 return 0;
273 }
274
275 if (group->generator == NULL) {
276 group->generator = EC_POINT_new(group);
277 if (group->generator == NULL)
278 return 0;
279 }
280 if (!EC_POINT_copy(group->generator, generator))
281 return 0;
282
283 if (order != NULL) {
284 if (!BN_copy(group->order, order))
285 return 0;
286 } else
287 BN_zero(group->order);
288
289 if (cofactor != NULL) {
290 if (!BN_copy(group->cofactor, cofactor))
291 return 0;
292 } else
293 BN_zero(group->cofactor);
294
295 /*
3a6a4a93 296 * Some groups have an order with
0f113f3e
MC
297 * factors of two, which makes the Montgomery setup fail.
298 * |group->mont_data| will be NULL in this case.
299 */
3a6a4a93
BB
300 if (BN_is_odd(group->order)) {
301 return ec_precompute_mont_data(group);
302 }
0f113f3e 303
3a6a4a93
BB
304 BN_MONT_CTX_free(group->mont_data);
305 group->mont_data = NULL;
0f113f3e
MC
306 return 1;
307}
bb62a8b0 308
9dd84053 309const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
0f113f3e
MC
310{
311 return group->generator;
312}
bb62a8b0 313
f54be179 314BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
0f113f3e
MC
315{
316 return group->mont_data;
317}
bb62a8b0 318
b6db386f 319int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
0f113f3e 320{
be2e334f
DSH
321 if (group->order == NULL)
322 return 0;
0f113f3e
MC
323 if (!BN_copy(order, group->order))
324 return 0;
0657bf9c 325
0f113f3e
MC
326 return !BN_is_zero(order);
327}
0657bf9c 328
be2e334f
DSH
329const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
330{
331 return group->order;
332}
333
334int EC_GROUP_order_bits(const EC_GROUP *group)
335{
77470e98 336 return group->meth->group_order_bits(group);
be2e334f
DSH
337}
338
0f113f3e
MC
339int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
340 BN_CTX *ctx)
341{
be2e334f
DSH
342
343 if (group->cofactor == NULL)
344 return 0;
0f113f3e
MC
345 if (!BN_copy(cofactor, group->cofactor))
346 return 0;
48fe4d62 347
0f113f3e
MC
348 return !BN_is_zero(group->cofactor);
349}
48fe4d62 350
be2e334f
DSH
351const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
352{
353 return group->cofactor;
354}
355
7dc17a6c 356void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
0f113f3e
MC
357{
358 group->curve_name = nid;
359}
b6db386f 360
7dc17a6c 361int EC_GROUP_get_curve_name(const EC_GROUP *group)
0f113f3e
MC
362{
363 return group->curve_name;
364}
b6db386f 365
458c2917 366void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
0f113f3e
MC
367{
368 group->asn1_flag = flag;
369}
458c2917
BM
370
371int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
0f113f3e
MC
372{
373 return group->asn1_flag;
374}
458c2917 375
0f113f3e 376void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
254ef80d 377 point_conversion_form_t form)
0f113f3e
MC
378{
379 group->asn1_form = form;
380}
254ef80d 381
0f113f3e
MC
382point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
383 *group)
384{
385 return group->asn1_form;
386}
254ef80d 387
5f3d6f70 388size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
0f113f3e 389{
b548a1f1
RS
390 OPENSSL_free(group->seed);
391 group->seed = NULL;
392 group->seed_len = 0;
5f3d6f70 393
0f113f3e
MC
394 if (!len || !p)
395 return 1;
5f3d6f70 396
f06080cb
F
397 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
398 ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
0f113f3e 399 return 0;
f06080cb 400 }
0f113f3e
MC
401 memcpy(group->seed, p, len);
402 group->seed_len = len;
5f3d6f70 403
0f113f3e
MC
404 return len;
405}
5f3d6f70
BM
406
407unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
0f113f3e
MC
408{
409 return group->seed;
410}
5f3d6f70
BM
411
412size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
0f113f3e
MC
413{
414 return group->seed_len;
415}
416
417int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
418 const BIGNUM *b, BN_CTX *ctx)
419{
420 if (group->meth->group_set_curve == 0) {
421 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
422 return 0;
423 }
424 return group->meth->group_set_curve(group, p, a, b, ctx);
425}
426
427int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
428 BIGNUM *b, BN_CTX *ctx)
429{
430 if (group->meth->group_get_curve == 0) {
431 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
432 return 0;
433 }
434 return group->meth->group_get_curve(group, p, a, b, ctx);
435}
7793f30e 436
b3310161 437#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
438int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
439 const BIGNUM *b, BN_CTX *ctx)
440{
441 if (group->meth->group_set_curve == 0) {
442 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
443 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
444 return 0;
445 }
446 return group->meth->group_set_curve(group, p, a, b, ctx);
447}
448
449int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
450 BIGNUM *b, BN_CTX *ctx)
451{
452 if (group->meth->group_get_curve == 0) {
453 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
454 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
455 return 0;
456 }
457 return group->meth->group_get_curve(group, p, a, b, ctx);
458}
b3310161 459#endif
7793f30e
BM
460
461int EC_GROUP_get_degree(const EC_GROUP *group)
0f113f3e
MC
462{
463 if (group->meth->group_get_degree == 0) {
464 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
465 return 0;
466 }
467 return group->meth->group_get_degree(group);
468}
48fe4d62 469
17d6bb81 470int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
471{
472 if (group->meth->group_check_discriminant == 0) {
473 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
474 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
475 return 0;
476 }
477 return group->meth->group_check_discriminant(group, ctx);
478}
af28dd6c 479
ada0e717 480int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
0f113f3e
MC
481{
482 int r = 0;
483 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
484 BN_CTX *ctx_new = NULL;
485
486 /* compare the field types */
487 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
488 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
489 return 1;
490 /* compare the curve name (if present in both) */
491 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
492 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
493 return 1;
6903e2e7
DSH
494 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
495 return 0;
0f113f3e 496
90945fa3 497 if (ctx == NULL)
0f113f3e 498 ctx_new = ctx = BN_CTX_new();
90945fa3 499 if (ctx == NULL)
0f113f3e
MC
500 return -1;
501
502 BN_CTX_start(ctx);
503 a1 = BN_CTX_get(ctx);
504 a2 = BN_CTX_get(ctx);
505 a3 = BN_CTX_get(ctx);
506 b1 = BN_CTX_get(ctx);
507 b2 = BN_CTX_get(ctx);
508 b3 = BN_CTX_get(ctx);
90945fa3 509 if (b3 == NULL) {
0f113f3e 510 BN_CTX_end(ctx);
23a1d5e9 511 BN_CTX_free(ctx_new);
0f113f3e
MC
512 return -1;
513 }
514
515 /*
516 * XXX This approach assumes that the external representation of curves
517 * over the same field type is the same.
518 */
519 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
520 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
521 r = 1;
522
523 if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
524 r = 1;
525
526 /* XXX EC_POINT_cmp() assumes that the methods are equal */
527 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
528 EC_GROUP_get0_generator(b), ctx))
529 r = 1;
530
531 if (!r) {
be2e334f 532 const BIGNUM *ao, *bo, *ac, *bc;
0f113f3e 533 /* compare the order and cofactor */
be2e334f
DSH
534 ao = EC_GROUP_get0_order(a);
535 bo = EC_GROUP_get0_order(b);
536 ac = EC_GROUP_get0_cofactor(a);
537 bc = EC_GROUP_get0_cofactor(b);
538 if (ao == NULL || bo == NULL) {
0f113f3e 539 BN_CTX_end(ctx);
23a1d5e9 540 BN_CTX_free(ctx_new);
0f113f3e
MC
541 return -1;
542 }
be2e334f 543 if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
0f113f3e
MC
544 r = 1;
545 }
546
547 BN_CTX_end(ctx);
23a1d5e9 548 BN_CTX_free(ctx_new);
ada0e717 549
0f113f3e
MC
550 return r;
551}
ada0e717 552
0657bf9c
BM
553/* functions for EC_POINT objects */
554
555EC_POINT *EC_POINT_new(const EC_GROUP *group)
0f113f3e
MC
556{
557 EC_POINT *ret;
558
559 if (group == NULL) {
560 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
561 return NULL;
562 }
f06080cb 563 if (group->meth->point_init == NULL) {
0f113f3e
MC
564 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
565 return NULL;
566 }
567
1b4cf96f 568 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
569 if (ret == NULL) {
570 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
571 return NULL;
572 }
573
574 ret->meth = group->meth;
575
576 if (!ret->meth->point_init(ret)) {
577 OPENSSL_free(ret);
578 return NULL;
579 }
580
581 return ret;
582}
0657bf9c
BM
583
584void EC_POINT_free(EC_POINT *point)
0f113f3e
MC
585{
586 if (!point)
587 return;
7711de24 588
0f113f3e
MC
589 if (point->meth->point_finish != 0)
590 point->meth->point_finish(point);
591 OPENSSL_free(point);
592}
0657bf9c
BM
593
594void EC_POINT_clear_free(EC_POINT *point)
0f113f3e
MC
595{
596 if (!point)
597 return;
598
599 if (point->meth->point_clear_finish != 0)
600 point->meth->point_clear_finish(point);
601 else if (point->meth->point_finish != 0)
602 point->meth->point_finish(point);
b4faea50 603 OPENSSL_clear_free(point, sizeof(*point));
0f113f3e 604}
0657bf9c
BM
605
606int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
0f113f3e
MC
607{
608 if (dest->meth->point_copy == 0) {
609 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
610 return 0;
611 }
612 if (dest->meth != src->meth) {
613 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
614 return 0;
615 }
616 if (dest == src)
617 return 1;
618 return dest->meth->point_copy(dest, src);
619}
0657bf9c 620
7793f30e 621EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
0f113f3e
MC
622{
623 EC_POINT *t;
624 int r;
625
626 if (a == NULL)
627 return NULL;
628
629 t = EC_POINT_new(group);
630 if (t == NULL)
26a7d938 631 return NULL;
0f113f3e
MC
632 r = EC_POINT_copy(t, a);
633 if (!r) {
634 EC_POINT_free(t);
635 return NULL;
8fdc3734
RS
636 }
637 return t;
0f113f3e 638}
7793f30e 639
48fe4d62 640const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
0f113f3e
MC
641{
642 return point->meth;
643}
48fe4d62 644
226cc7de 645int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
0f113f3e
MC
646{
647 if (group->meth->point_set_to_infinity == 0) {
648 ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
649 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
650 return 0;
651 }
652 if (group->meth != point->meth) {
653 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
654 return 0;
655 }
656 return group->meth->point_set_to_infinity(group, point);
657}
658
659int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
660 EC_POINT *point, const BIGNUM *x,
661 const BIGNUM *y, const BIGNUM *z,
662 BN_CTX *ctx)
663{
664 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
665 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
666 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
667 return 0;
668 }
669 if (group->meth != point->meth) {
670 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
671 EC_R_INCOMPATIBLE_OBJECTS);
672 return 0;
673 }
674 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
675 y, z, ctx);
676}
677
678int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
679 const EC_POINT *point, BIGNUM *x,
680 BIGNUM *y, BIGNUM *z,
681 BN_CTX *ctx)
682{
683 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
684 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
685 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
686 return 0;
687 }
688 if (group->meth != point->meth) {
689 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
690 EC_R_INCOMPATIBLE_OBJECTS);
691 return 0;
692 }
693 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
694 y, z, ctx);
695}
696
697int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
698 EC_POINT *point, const BIGNUM *x,
699 const BIGNUM *y, BN_CTX *ctx)
700{
701 if (group->meth->point_set_affine_coordinates == 0) {
702 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
703 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
704 return 0;
705 }
706 if (group->meth != point->meth) {
707 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
708 EC_R_INCOMPATIBLE_OBJECTS);
709 return 0;
710 }
1e2012b7
EK
711 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
712 return 0;
713
714 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
715 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
716 EC_R_POINT_IS_NOT_ON_CURVE);
717 return 0;
718 }
719 return 1;
0f113f3e 720}
226cc7de 721
b3310161 722#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
723int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
724 EC_POINT *point, const BIGNUM *x,
725 const BIGNUM *y, BN_CTX *ctx)
726{
727 if (group->meth->point_set_affine_coordinates == 0) {
728 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
729 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
730 return 0;
731 }
732 if (group->meth != point->meth) {
733 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
734 EC_R_INCOMPATIBLE_OBJECTS);
735 return 0;
736 }
1e2012b7
EK
737 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
738 return 0;
739
740 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
741 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
742 EC_R_POINT_IS_NOT_ON_CURVE);
743 return 0;
744 }
745 return 1;
0f113f3e 746}
b3310161 747#endif
7793f30e 748
0f113f3e
MC
749int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
750 const EC_POINT *point, BIGNUM *x,
751 BIGNUM *y, BN_CTX *ctx)
752{
753 if (group->meth->point_get_affine_coordinates == 0) {
754 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
755 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
756 return 0;
757 }
758 if (group->meth != point->meth) {
759 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
760 EC_R_INCOMPATIBLE_OBJECTS);
761 return 0;
762 }
763 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
764}
7793f30e 765
b3310161 766#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
767int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
768 const EC_POINT *point, BIGNUM *x,
769 BIGNUM *y, BN_CTX *ctx)
770{
771 if (group->meth->point_get_affine_coordinates == 0) {
772 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
773 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
774 return 0;
775 }
776 if (group->meth != point->meth) {
777 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
778 EC_R_INCOMPATIBLE_OBJECTS);
779 return 0;
780 }
781 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
782}
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 }
792 if ((group->meth != r->meth) || (r->meth != a->meth)
793 || (a->meth != b->meth)) {
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 }
807 if ((group->meth != r->meth) || (r->meth != a->meth)) {
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 }
820 if (group->meth != a->meth) {
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 }
834 if (group->meth != point->meth) {
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 }
855 if (group->meth != point->meth) {
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 }
869 if ((group->meth != a->meth) || (a->meth != b->meth)) {
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 }
882 if (group->meth != point->meth) {
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++) {
899 if (group->meth != points[i]->meth) {
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{
917 if (group->meth->mul == 0)
918 /* use default */
919 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
37c660ff 920
0f113f3e
MC
921 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
922}
37c660ff
BM
923
924int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
0f113f3e
MC
925 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
926{
927 /* just a convenient interface to EC_POINTs_mul() */
37c660ff 928
0f113f3e
MC
929 const EC_POINT *points[1];
930 const BIGNUM *scalars[1];
37c660ff 931
0f113f3e
MC
932 points[0] = point;
933 scalars[0] = p_scalar;
37c660ff 934
0f113f3e
MC
935 return EC_POINTs_mul(group, r, g_scalar,
936 (point != NULL
937 && p_scalar != NULL), points, scalars, ctx);
938}
37c660ff
BM
939
940int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
941{
942 if (group->meth->mul == 0)
943 /* use default */
944 return ec_wNAF_precompute_mult(group, ctx);
37c660ff 945
0f113f3e
MC
946 if (group->meth->precompute_mult != 0)
947 return group->meth->precompute_mult(group, ctx);
948 else
949 return 1; /* nothing to do, so report success */
950}
37c660ff
BM
951
952int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
0f113f3e
MC
953{
954 if (group->meth->mul == 0)
955 /* use default */
956 return ec_wNAF_have_precompute_mult(group);
957
958 if (group->meth->have_precompute_mult != 0)
959 return group->meth->have_precompute_mult(group);
960 else
961 return 0; /* cannot tell whether precomputation has
962 * been performed */
963}
964
965/*
966 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
967 * returns one on success. On error it returns zero.
968 */
eb791696 969static int ec_precompute_mont_data(EC_GROUP *group)
0f113f3e
MC
970{
971 BN_CTX *ctx = BN_CTX_new();
972 int ret = 0;
973
23a1d5e9
RS
974 BN_MONT_CTX_free(group->mont_data);
975 group->mont_data = NULL;
0f113f3e
MC
976
977 if (ctx == NULL)
978 goto err;
979
980 group->mont_data = BN_MONT_CTX_new();
90945fa3 981 if (group->mont_data == NULL)
0f113f3e
MC
982 goto err;
983
984 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
985 BN_MONT_CTX_free(group->mont_data);
986 group->mont_data = NULL;
987 goto err;
988 }
989
990 ret = 1;
991
992 err:
993
23a1d5e9 994 BN_CTX_free(ctx);
0f113f3e
MC
995 return ret;
996}
3aef36ff
RS
997
998int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
999{
1000 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1001}
1002
1003void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1004{
1005 return CRYPTO_get_ex_data(&key->ex_data, idx);
1006}
77470e98
DSH
1007
1008int ec_group_simple_order_bits(const EC_GROUP *group)
1009{
1010 if (group->order == NULL)
1011 return 0;
1012 return BN_num_bits(group->order);
1013}
eb791696
AP
1014
1015int EC_GROUP_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1016 BIGNUM *x, BN_CTX *ctx)
1017{
1018 if (group->meth->field_inverse_mod_ord != NULL)
1019 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1020 else
1021 return 0;
1022}