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