]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_lib.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / crypto / ec / ec_lib.c
CommitLineData
35b73a1f 1/*
da1c088f 2 * Copyright 2001-2023 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
579422c8 11/*
5b5eea4b 12 * EC_GROUP low level APIs are deprecated for public use, but still ok for
579422c8
P
13 * internal use.
14 */
15#include "internal/deprecated.h"
16
c4b36ff4 17#include <string.h>
c0f39ded
SL
18#include <openssl/params.h>
19#include <openssl/core_names.h>
0657bf9c 20#include <openssl/err.h>
bb62a8b0 21#include <openssl/opensslv.h>
a8aad913 22#include <openssl/param_build.h>
c0f39ded
SL
23#include "crypto/ec.h"
24#include "internal/nelem.h"
706457b7 25#include "ec_local.h"
0657bf9c 26
0657bf9c
BM
27/* functions for EC_GROUP objects */
28
32ab57cb
SL
29EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
30 const EC_METHOD *meth)
0f113f3e
MC
31{
32 EC_GROUP *ret;
33
34 if (meth == NULL) {
9311d0c4 35 ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
0f113f3e
MC
36 return NULL;
37 }
38 if (meth->group_init == 0) {
9311d0c4 39 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
40 return NULL;
41 }
42
64b25758 43 ret = OPENSSL_zalloc(sizeof(*ret));
e077455e 44 if (ret == NULL)
0f113f3e 45 return NULL;
0f113f3e 46
a9612d6c 47 ret->libctx = libctx;
2da8d4eb
MC
48 if (propq != NULL) {
49 ret->propq = OPENSSL_strdup(propq);
e077455e 50 if (ret->propq == NULL)
2da8d4eb 51 goto err;
2da8d4eb 52 }
0f113f3e 53 ret->meth = meth;
6903e2e7
DSH
54 if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
55 ret->order = BN_new();
56 if (ret->order == NULL)
57 goto err;
58 ret->cofactor = BN_new();
59 if (ret->cofactor == NULL)
60 goto err;
61 }
9db6af92 62 ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
0f113f3e 63 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
0f113f3e
MC
64 if (!meth->group_init(ret))
65 goto err;
0f113f3e 66 return ret;
64b25758 67
0f113f3e 68 err:
23a1d5e9
RS
69 BN_free(ret->order);
70 BN_free(ret->cofactor);
2da8d4eb 71 OPENSSL_free(ret->propq);
0f113f3e
MC
72 OPENSSL_free(ret);
73 return NULL;
74}
0657bf9c 75
23ccae80
BB
76#ifndef OPENSSL_NO_DEPRECATED_3_0
77# ifndef FIPS_MODULE
a9612d6c
MC
78EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
79{
32ab57cb 80 return ossl_ec_group_new_ex(NULL, NULL, meth);
a9612d6c 81}
23ccae80 82# endif
a9612d6c
MC
83#endif
84
2c52ac9b 85void EC_pre_comp_free(EC_GROUP *group)
3aef36ff
RS
86{
87 switch (group->pre_comp_type) {
f3b3d7f0 88 case PCT_none:
3aef36ff 89 break;
66117ab0 90 case PCT_nistz256:
f3b3d7f0 91#ifdef ECP_NISTZ256_ASM
3aef36ff 92 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
e69aa800 93#endif
f3b3d7f0 94 break;
3aef36ff 95#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
66117ab0 96 case PCT_nistp224:
3aef36ff
RS
97 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
98 break;
66117ab0 99 case PCT_nistp256:
3aef36ff
RS
100 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
101 break;
01d901e4
RM
102 case PCT_nistp384:
103 ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);
104 break;
66117ab0 105 case PCT_nistp521:
3aef36ff
RS
106 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
107 break;
f3b3d7f0
RS
108#else
109 case PCT_nistp224:
110 case PCT_nistp256:
01d901e4 111 case PCT_nistp384:
f3b3d7f0
RS
112 case PCT_nistp521:
113 break;
3aef36ff 114#endif
66117ab0 115 case PCT_ec:
3aef36ff
RS
116 EC_ec_pre_comp_free(group->pre_comp.ec);
117 break;
118 }
119 group->pre_comp.ec = NULL;
120}
121
0657bf9c 122void EC_GROUP_free(EC_GROUP *group)
0f113f3e
MC
123{
124 if (!group)
125 return;
7711de24 126
0f113f3e
MC
127 if (group->meth->group_finish != 0)
128 group->meth->group_finish(group);
df9cc153 129
2c52ac9b 130 EC_pre_comp_free(group);
23a1d5e9 131 BN_MONT_CTX_free(group->mont_data);
8fdc3734 132 EC_POINT_free(group->generator);
0f113f3e
MC
133 BN_free(group->order);
134 BN_free(group->cofactor);
25aaa98a 135 OPENSSL_free(group->seed);
2da8d4eb 136 OPENSSL_free(group->propq);
0f113f3e
MC
137 OPENSSL_free(group);
138}
0657bf9c 139
936c2b9e 140#ifndef OPENSSL_NO_DEPRECATED_3_0
0657bf9c 141void EC_GROUP_clear_free(EC_GROUP *group)
0f113f3e
MC
142{
143 if (!group)
144 return;
df9cc153 145
0f113f3e
MC
146 if (group->meth->group_clear_finish != 0)
147 group->meth->group_clear_finish(group);
148 else if (group->meth->group_finish != 0)
149 group->meth->group_finish(group);
df9cc153 150
2c52ac9b 151 EC_pre_comp_free(group);
23a1d5e9 152 BN_MONT_CTX_free(group->mont_data);
8fdc3734 153 EC_POINT_clear_free(group->generator);
0f113f3e
MC
154 BN_clear_free(group->order);
155 BN_clear_free(group->cofactor);
4b45c6e5 156 OPENSSL_clear_free(group->seed, group->seed_len);
b4faea50 157 OPENSSL_clear_free(group, sizeof(*group));
0f113f3e 158}
4a7a4972 159#endif
0657bf9c
BM
160
161int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
0f113f3e 162{
0f113f3e 163 if (dest->meth->group_copy == 0) {
9311d0c4 164 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
165 return 0;
166 }
167 if (dest->meth != src->meth) {
9311d0c4 168 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
169 return 0;
170 }
171 if (dest == src)
172 return 1;
173
a9612d6c 174 dest->libctx = src->libctx;
b14e6015
MC
175 dest->curve_name = src->curve_name;
176
3aef36ff
RS
177 /* Copy precomputed */
178 dest->pre_comp_type = src->pre_comp_type;
179 switch (src->pre_comp_type) {
f3b3d7f0 180 case PCT_none:
3aef36ff
RS
181 dest->pre_comp.ec = NULL;
182 break;
66117ab0 183 case PCT_nistz256:
f3b3d7f0 184#ifdef ECP_NISTZ256_ASM
3aef36ff 185 dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
e69aa800 186#endif
f3b3d7f0 187 break;
3aef36ff 188#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
66117ab0 189 case PCT_nistp224:
3aef36ff
RS
190 dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
191 break;
66117ab0 192 case PCT_nistp256:
3aef36ff
RS
193 dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
194 break;
01d901e4
RM
195 case PCT_nistp384:
196 dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);
197 break;
66117ab0 198 case PCT_nistp521:
3aef36ff
RS
199 dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
200 break;
f3b3d7f0
RS
201#else
202 case PCT_nistp224:
203 case PCT_nistp256:
01d901e4 204 case PCT_nistp384:
f3b3d7f0
RS
205 case PCT_nistp521:
206 break;
3aef36ff 207#endif
66117ab0 208 case PCT_ec:
3aef36ff
RS
209 dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
210 break;
0f113f3e
MC
211 }
212
213 if (src->mont_data != NULL) {
214 if (dest->mont_data == NULL) {
215 dest->mont_data = BN_MONT_CTX_new();
216 if (dest->mont_data == NULL)
217 return 0;
218 }
219 if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
220 return 0;
221 } else {
222 /* src->generator == NULL */
23a1d5e9
RS
223 BN_MONT_CTX_free(dest->mont_data);
224 dest->mont_data = NULL;
0f113f3e 225 }
0657bf9c 226
0f113f3e
MC
227 if (src->generator != NULL) {
228 if (dest->generator == NULL) {
229 dest->generator = EC_POINT_new(dest);
230 if (dest->generator == NULL)
231 return 0;
232 }
233 if (!EC_POINT_copy(dest->generator, src->generator))
234 return 0;
235 } else {
236 /* src->generator == NULL */
8fdc3734
RS
237 EC_POINT_clear_free(dest->generator);
238 dest->generator = NULL;
0f113f3e
MC
239 }
240
6903e2e7
DSH
241 if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
242 if (!BN_copy(dest->order, src->order))
243 return 0;
244 if (!BN_copy(dest->cofactor, src->cofactor))
245 return 0;
246 }
0f113f3e 247
0f113f3e
MC
248 dest->asn1_flag = src->asn1_flag;
249 dest->asn1_form = src->asn1_form;
fe2f8aec 250 dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
0f113f3e
MC
251
252 if (src->seed) {
b548a1f1 253 OPENSSL_free(dest->seed);
e077455e 254 if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)
0f113f3e
MC
255 return 0;
256 if (!memcpy(dest->seed, src->seed, src->seed_len))
257 return 0;
258 dest->seed_len = src->seed_len;
259 } else {
b548a1f1 260 OPENSSL_free(dest->seed);
0f113f3e
MC
261 dest->seed = NULL;
262 dest->seed_len = 0;
263 }
264
265 return dest->meth->group_copy(dest, src);
266}
0657bf9c 267
7793f30e 268EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
0f113f3e
MC
269{
270 EC_GROUP *t = NULL;
271 int ok = 0;
7793f30e 272
0f113f3e
MC
273 if (a == NULL)
274 return NULL;
7793f30e 275
32ab57cb 276 if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
26a7d938 277 return NULL;
0f113f3e
MC
278 if (!EC_GROUP_copy(t, a))
279 goto err;
7793f30e 280
0f113f3e 281 ok = 1;
7793f30e 282
0f113f3e
MC
283 err:
284 if (!ok) {
8fdc3734 285 EC_GROUP_free(t);
0f113f3e 286 return NULL;
8fdc3734 287 }
0f113f3e
MC
288 return t;
289}
7793f30e 290
23ccae80 291#ifndef OPENSSL_NO_DEPRECATED_3_0
48fe4d62 292const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
0f113f3e
MC
293{
294 return group->meth;
295}
48fe4d62 296
458c2917 297int EC_METHOD_get_field_type(const EC_METHOD *meth)
0f113f3e
MC
298{
299 return meth->field_type;
300}
23ccae80 301#endif
0f113f3e 302
eb791696
AP
303static int ec_precompute_mont_data(EC_GROUP *);
304
b783beea
BB
305/*-
306 * Try computing cofactor from the generator order (n) and field cardinality (q).
307 * This works for all curves of cryptographic interest.
308 *
309 * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
310 * h_min = (q + 1 - 2*sqrt(q))/n
311 * h_max = (q + 1 + 2*sqrt(q))/n
312 * h_max - h_min = 4*sqrt(q)/n
313 * So if n > 4*sqrt(q) holds, there is only one possible value for h:
314 * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
315 *
316 * Otherwise, zero cofactor and return success.
317 */
318static int ec_guess_cofactor(EC_GROUP *group) {
319 int ret = 0;
320 BN_CTX *ctx = NULL;
321 BIGNUM *q = NULL;
322
323 /*-
324 * If the cofactor is too large, we cannot guess it.
325 * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
326 */
327 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
328 /* default to 0 */
329 BN_zero(group->cofactor);
330 /* return success */
331 return 1;
332 }
333
334 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
335 return 0;
336
337 BN_CTX_start(ctx);
338 if ((q = BN_CTX_get(ctx)) == NULL)
339 goto err;
340
341 /* set q = 2**m for binary fields; q = p otherwise */
342 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
343 BN_zero(q);
344 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
345 goto err;
346 } else {
347 if (!BN_copy(q, group->field))
348 goto err;
349 }
350
351 /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
352 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
353 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
354 /* q + 1 + n/2 */
355 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
356 /* (q + 1 + n/2)/n */
357 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
358 goto err;
359 ret = 1;
360 err:
361 BN_CTX_end(ctx);
362 BN_CTX_free(ctx);
363 return ret;
364}
365
0f113f3e
MC
366int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
367 const BIGNUM *order, const BIGNUM *cofactor)
368{
369 if (generator == NULL) {
9311d0c4 370 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
371 return 0;
372 }
373
b783beea
BB
374 /* require group->field >= 1 */
375 if (group->field == NULL || BN_is_zero(group->field)
376 || BN_is_negative(group->field)) {
9311d0c4 377 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
b783beea
BB
378 return 0;
379 }
380
381 /*-
382 * - require order >= 1
383 * - enforce upper bound due to Hasse thm: order can be no more than one bit
384 * longer than field cardinality
385 */
386 if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
387 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
9311d0c4 388 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
b783beea
BB
389 return 0;
390 }
391
392 /*-
393 * Unfortunately the cofactor is an optional field in many standards.
394 * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
395 * So accept cofactor == NULL or cofactor >= 0.
396 */
397 if (cofactor != NULL && BN_is_negative(cofactor)) {
9311d0c4 398 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
b783beea
BB
399 return 0;
400 }
401
0f113f3e
MC
402 if (group->generator == NULL) {
403 group->generator = EC_POINT_new(group);
404 if (group->generator == NULL)
405 return 0;
406 }
407 if (!EC_POINT_copy(group->generator, generator))
408 return 0;
409
b783beea
BB
410 if (!BN_copy(group->order, order))
411 return 0;
0f113f3e 412
b783beea
BB
413 /* Either take the provided positive cofactor, or try to compute it */
414 if (cofactor != NULL && !BN_is_zero(cofactor)) {
0f113f3e
MC
415 if (!BN_copy(group->cofactor, cofactor))
416 return 0;
b783beea 417 } else if (!ec_guess_cofactor(group)) {
0f113f3e 418 BN_zero(group->cofactor);
b783beea 419 return 0;
8402cd5f 420 }
b783beea 421
0f113f3e 422 /*
3a6a4a93 423 * Some groups have an order with
0f113f3e
MC
424 * factors of two, which makes the Montgomery setup fail.
425 * |group->mont_data| will be NULL in this case.
426 */
3a6a4a93
BB
427 if (BN_is_odd(group->order)) {
428 return ec_precompute_mont_data(group);
429 }
0f113f3e 430
3a6a4a93
BB
431 BN_MONT_CTX_free(group->mont_data);
432 group->mont_data = NULL;
0f113f3e
MC
433 return 1;
434}
bb62a8b0 435
9dd84053 436const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
0f113f3e
MC
437{
438 return group->generator;
439}
bb62a8b0 440
f54be179 441BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
0f113f3e
MC
442{
443 return group->mont_data;
444}
bb62a8b0 445
b6db386f 446int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
0f113f3e 447{
be2e334f
DSH
448 if (group->order == NULL)
449 return 0;
0f113f3e
MC
450 if (!BN_copy(order, group->order))
451 return 0;
0657bf9c 452
0f113f3e
MC
453 return !BN_is_zero(order);
454}
0657bf9c 455
be2e334f
DSH
456const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
457{
458 return group->order;
459}
460
461int EC_GROUP_order_bits(const EC_GROUP *group)
462{
77470e98 463 return group->meth->group_order_bits(group);
be2e334f
DSH
464}
465
0f113f3e
MC
466int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
467 BN_CTX *ctx)
468{
be2e334f
DSH
469
470 if (group->cofactor == NULL)
471 return 0;
0f113f3e
MC
472 if (!BN_copy(cofactor, group->cofactor))
473 return 0;
48fe4d62 474
0f113f3e
MC
475 return !BN_is_zero(group->cofactor);
476}
48fe4d62 477
be2e334f
DSH
478const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
479{
480 return group->cofactor;
481}
482
7dc17a6c 483void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
0f113f3e
MC
484{
485 group->curve_name = nid;
9db6af92
RL
486 group->asn1_flag =
487 (nid != NID_undef)
488 ? OPENSSL_EC_NAMED_CURVE
489 : OPENSSL_EC_EXPLICIT_CURVE;
0f113f3e 490}
b6db386f 491
7dc17a6c 492int EC_GROUP_get_curve_name(const EC_GROUP *group)
0f113f3e
MC
493{
494 return group->curve_name;
495}
b6db386f 496
fa1f0306
DA
497const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
498{
499 return group->field;
500}
501
23ccae80
BB
502int EC_GROUP_get_field_type(const EC_GROUP *group)
503{
504 return group->meth->field_type;
505}
506
458c2917 507void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
0f113f3e
MC
508{
509 group->asn1_flag = flag;
510}
458c2917
BM
511
512int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
0f113f3e
MC
513{
514 return group->asn1_flag;
515}
458c2917 516
0f113f3e 517void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
254ef80d 518 point_conversion_form_t form)
0f113f3e
MC
519{
520 group->asn1_form = form;
521}
254ef80d 522
0f113f3e
MC
523point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
524 *group)
525{
526 return group->asn1_form;
527}
254ef80d 528
5f3d6f70 529size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
0f113f3e 530{
b548a1f1
RS
531 OPENSSL_free(group->seed);
532 group->seed = NULL;
533 group->seed_len = 0;
5f3d6f70 534
0f113f3e
MC
535 if (!len || !p)
536 return 1;
5f3d6f70 537
e077455e 538 if ((group->seed = OPENSSL_malloc(len)) == NULL)
0f113f3e
MC
539 return 0;
540 memcpy(group->seed, p, len);
541 group->seed_len = len;
5f3d6f70 542
0f113f3e
MC
543 return len;
544}
5f3d6f70
BM
545
546unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
0f113f3e
MC
547{
548 return group->seed;
549}
5f3d6f70
BM
550
551size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
0f113f3e
MC
552{
553 return group->seed_len;
554}
555
8e3cced7
MC
556int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
557 const BIGNUM *b, BN_CTX *ctx)
0f113f3e
MC
558{
559 if (group->meth->group_set_curve == 0) {
9311d0c4 560 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
561 return 0;
562 }
563 return group->meth->group_set_curve(group, p, a, b, ctx);
564}
565
8e3cced7
MC
566int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
567 BN_CTX *ctx)
0f113f3e 568{
8e3cced7 569 if (group->meth->group_get_curve == NULL) {
9311d0c4 570 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
571 return 0;
572 }
573 return group->meth->group_get_curve(group, p, a, b, ctx);
574}
7793f30e 575
936c2b9e 576#ifndef OPENSSL_NO_DEPRECATED_3_0
8e3cced7
MC
577int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
578 const BIGNUM *b, BN_CTX *ctx)
579{
580 return EC_GROUP_set_curve(group, p, a, b, ctx);
581}
582
583int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
584 BIGNUM *b, BN_CTX *ctx)
585{
586 return EC_GROUP_get_curve(group, p, a, b, ctx);
587}
588
50db8163 589# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
590int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
591 const BIGNUM *b, BN_CTX *ctx)
592{
8e3cced7 593 return EC_GROUP_set_curve(group, p, a, b, ctx);
0f113f3e
MC
594}
595
596int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
597 BIGNUM *b, BN_CTX *ctx)
598{
8e3cced7 599 return EC_GROUP_get_curve(group, p, a, b, ctx);
0f113f3e 600}
50db8163 601# endif
b3310161 602#endif
7793f30e
BM
603
604int EC_GROUP_get_degree(const EC_GROUP *group)
0f113f3e
MC
605{
606 if (group->meth->group_get_degree == 0) {
9311d0c4 607 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
608 return 0;
609 }
610 return group->meth->group_get_degree(group);
611}
48fe4d62 612
17d6bb81 613int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
614{
615 if (group->meth->group_check_discriminant == 0) {
9311d0c4 616 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
617 return 0;
618 }
619 return group->meth->group_check_discriminant(group, ctx);
620}
af28dd6c 621
ada0e717 622int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
0f113f3e
MC
623{
624 int r = 0;
625 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
f844f9eb 626#ifndef FIPS_MODULE
0f113f3e 627 BN_CTX *ctx_new = NULL;
a9612d6c 628#endif
a9612d6c 629
0f113f3e 630 /* compare the field types */
23ccae80 631 if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
0f113f3e
MC
632 return 1;
633 /* compare the curve name (if present in both) */
634 if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
635 EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
636 return 1;
6903e2e7
DSH
637 if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
638 return 0;
0f113f3e 639
f844f9eb 640#ifndef FIPS_MODULE
0e8b6c97
AT
641 if (ctx == NULL)
642 ctx_new = ctx = BN_CTX_new();
643#endif
644 if (ctx == NULL)
645 return -1;
646
0f113f3e
MC
647 BN_CTX_start(ctx);
648 a1 = BN_CTX_get(ctx);
649 a2 = BN_CTX_get(ctx);
650 a3 = BN_CTX_get(ctx);
651 b1 = BN_CTX_get(ctx);
652 b2 = BN_CTX_get(ctx);
653 b3 = BN_CTX_get(ctx);
90945fa3 654 if (b3 == NULL) {
0f113f3e 655 BN_CTX_end(ctx);
f844f9eb 656#ifndef FIPS_MODULE
23a1d5e9 657 BN_CTX_free(ctx_new);
a9612d6c 658#endif
0f113f3e
MC
659 return -1;
660 }
661
662 /*
663 * XXX This approach assumes that the external representation of curves
664 * over the same field type is the same.
665 */
666 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
667 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
668 r = 1;
669
8402cd5f
SL
670 /* return 1 if the curve parameters are different */
671 if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
0f113f3e
MC
672 r = 1;
673
ac2b52c6 674 /* XXX EC_POINT_cmp() assumes that the methods are equal */
8402cd5f 675 /* return 1 if the generators are different */
0f113f3e 676 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
8402cd5f 677 EC_GROUP_get0_generator(b), ctx) != 0)
0f113f3e
MC
678 r = 1;
679
680 if (!r) {
be2e334f 681 const BIGNUM *ao, *bo, *ac, *bc;
ac2b52c6 682 /* compare the orders */
be2e334f
DSH
683 ao = EC_GROUP_get0_order(a);
684 bo = EC_GROUP_get0_order(b);
be2e334f 685 if (ao == NULL || bo == NULL) {
8402cd5f
SL
686 /* return an error if either order is NULL */
687 r = -1;
688 goto end;
689 }
690 if (BN_cmp(ao, bo) != 0) {
691 /* return 1 if orders are different */
692 r = 1;
693 goto end;
0f113f3e 694 }
8402cd5f
SL
695 /*
696 * It gets here if the curve parameters and generator matched.
697 * Now check the optional cofactors (if both are present).
698 */
699 ac = EC_GROUP_get0_cofactor(a);
700 bc = EC_GROUP_get0_cofactor(b);
701 /* Returns 1 (mismatch) if both cofactors are specified and different */
702 if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
0f113f3e 703 r = 1;
8402cd5f 704 /* Returns 0 if the parameters matched */
0f113f3e 705 }
8402cd5f 706end:
0f113f3e 707 BN_CTX_end(ctx);
f844f9eb 708#ifndef FIPS_MODULE
23a1d5e9 709 BN_CTX_free(ctx_new);
a9612d6c 710#endif
0f113f3e
MC
711 return r;
712}
ada0e717 713
0657bf9c
BM
714/* functions for EC_POINT objects */
715
716EC_POINT *EC_POINT_new(const EC_GROUP *group)
0f113f3e
MC
717{
718 EC_POINT *ret;
719
720 if (group == NULL) {
9311d0c4 721 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
722 return NULL;
723 }
f06080cb 724 if (group->meth->point_init == NULL) {
9311d0c4 725 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
726 return NULL;
727 }
728
1b4cf96f 729 ret = OPENSSL_zalloc(sizeof(*ret));
e077455e 730 if (ret == NULL)
0f113f3e 731 return NULL;
0f113f3e
MC
732
733 ret->meth = group->meth;
b14e6015 734 ret->curve_name = group->curve_name;
0f113f3e
MC
735
736 if (!ret->meth->point_init(ret)) {
737 OPENSSL_free(ret);
738 return NULL;
739 }
740
741 return ret;
742}
0657bf9c
BM
743
744void EC_POINT_free(EC_POINT *point)
0f113f3e 745{
12a765a5 746 if (point == NULL)
0f113f3e 747 return;
7711de24 748
0f113f3e
MC
749 if (point->meth->point_finish != 0)
750 point->meth->point_finish(point);
751 OPENSSL_free(point);
752}
0657bf9c
BM
753
754void EC_POINT_clear_free(EC_POINT *point)
0f113f3e 755{
12a765a5 756 if (point == NULL)
0f113f3e
MC
757 return;
758
759 if (point->meth->point_clear_finish != 0)
760 point->meth->point_clear_finish(point);
761 else if (point->meth->point_finish != 0)
762 point->meth->point_finish(point);
b4faea50 763 OPENSSL_clear_free(point, sizeof(*point));
0f113f3e 764}
0657bf9c
BM
765
766int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
0f113f3e
MC
767{
768 if (dest->meth->point_copy == 0) {
9311d0c4 769 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
770 return 0;
771 }
b14e6015
MC
772 if (dest->meth != src->meth
773 || (dest->curve_name != src->curve_name
8402cd5f
SL
774 && dest->curve_name != 0
775 && src->curve_name != 0)) {
9311d0c4 776 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
777 return 0;
778 }
779 if (dest == src)
780 return 1;
781 return dest->meth->point_copy(dest, src);
782}
0657bf9c 783
7793f30e 784EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
0f113f3e
MC
785{
786 EC_POINT *t;
787 int r;
788
789 if (a == NULL)
790 return NULL;
791
792 t = EC_POINT_new(group);
793 if (t == NULL)
26a7d938 794 return NULL;
0f113f3e
MC
795 r = EC_POINT_copy(t, a);
796 if (!r) {
797 EC_POINT_free(t);
798 return NULL;
8fdc3734
RS
799 }
800 return t;
0f113f3e 801}
7793f30e 802
23ccae80 803#ifndef OPENSSL_NO_DEPRECATED_3_0
48fe4d62 804const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
0f113f3e
MC
805{
806 return point->meth;
807}
23ccae80 808#endif
48fe4d62 809
226cc7de 810int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
0f113f3e
MC
811{
812 if (group->meth->point_set_to_infinity == 0) {
9311d0c4 813 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
814 return 0;
815 }
816 if (group->meth != point->meth) {
9311d0c4 817 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
818 return 0;
819 }
820 return group->meth->point_set_to_infinity(group, point);
821}
822
07caec83 823#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e
MC
824int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
825 EC_POINT *point, const BIGNUM *x,
826 const BIGNUM *y, const BIGNUM *z,
827 BN_CTX *ctx)
828{
07caec83 829 if (group->meth->field_type != NID_X9_62_prime_field) {
9311d0c4 830 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
831 return 0;
832 }
b14e6015 833 if (!ec_point_is_compat(point, group)) {
9311d0c4 834 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
835 return 0;
836 }
32ab57cb
SL
837 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
838 x, y, z, ctx);
0f113f3e
MC
839}
840
841int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
842 const EC_POINT *point, BIGNUM *x,
843 BIGNUM *y, BIGNUM *z,
844 BN_CTX *ctx)
845{
07caec83 846 if (group->meth->field_type != NID_X9_62_prime_field) {
9311d0c4 847 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
848 return 0;
849 }
b14e6015 850 if (!ec_point_is_compat(point, group)) {
9311d0c4 851 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
852 return 0;
853 }
32ab57cb
SL
854 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
855 x, y, z, ctx);
0f113f3e 856}
07caec83 857#endif
0f113f3e 858
8e3cced7
MC
859int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
860 const BIGNUM *x, const BIGNUM *y,
861 BN_CTX *ctx)
0f113f3e 862{
8e3cced7 863 if (group->meth->point_set_affine_coordinates == NULL) {
9311d0c4 864 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
865 return 0;
866 }
b14e6015 867 if (!ec_point_is_compat(point, group)) {
9311d0c4 868 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
869 return 0;
870 }
1e2012b7
EK
871 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
872 return 0;
873
874 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
9311d0c4 875 ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
1e2012b7
EK
876 return 0;
877 }
878 return 1;
0f113f3e 879}
226cc7de 880
936c2b9e 881#ifndef OPENSSL_NO_DEPRECATED_3_0
8e3cced7
MC
882int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
883 EC_POINT *point, const BIGNUM *x,
884 const BIGNUM *y, BN_CTX *ctx)
885{
886 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
887}
888
50db8163 889# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
890int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
891 EC_POINT *point, const BIGNUM *x,
892 const BIGNUM *y, BN_CTX *ctx)
893{
8e3cced7
MC
894 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
895}
50db8163 896# endif
8e3cced7
MC
897#endif
898
899int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
900 const EC_POINT *point, BIGNUM *x, BIGNUM *y,
901 BN_CTX *ctx)
902{
903 if (group->meth->point_get_affine_coordinates == NULL) {
9311d0c4 904 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
905 return 0;
906 }
b14e6015 907 if (!ec_point_is_compat(point, group)) {
9311d0c4 908 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
1e2012b7
EK
909 return 0;
910 }
bfb10b97 911 if (EC_POINT_is_at_infinity(group, point)) {
9311d0c4 912 ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
bfb10b97
BB
913 return 0;
914 }
8e3cced7 915 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 916}
7793f30e 917
936c2b9e 918#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e
MC
919int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
920 const EC_POINT *point, BIGNUM *x,
921 BIGNUM *y, BN_CTX *ctx)
922{
8e3cced7 923 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 924}
7793f30e 925
50db8163 926# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
927int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
928 const EC_POINT *point, BIGNUM *x,
929 BIGNUM *y, BN_CTX *ctx)
930{
8e3cced7 931 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
0f113f3e 932}
50db8163 933# endif
b3310161 934#endif
7793f30e 935
0f113f3e
MC
936int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
937 const EC_POINT *b, BN_CTX *ctx)
938{
939 if (group->meth->add == 0) {
9311d0c4 940 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
941 return 0;
942 }
b14e6015
MC
943 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
944 || !ec_point_is_compat(b, group)) {
9311d0c4 945 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
946 return 0;
947 }
948 return group->meth->add(group, r, a, b, ctx);
949}
950
951int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
952 BN_CTX *ctx)
953{
954 if (group->meth->dbl == 0) {
9311d0c4 955 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
956 return 0;
957 }
b14e6015 958 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
9311d0c4 959 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
960 return 0;
961 }
962 return group->meth->dbl(group, r, a, ctx);
963}
0657bf9c 964
1d5bd6cf 965int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
0f113f3e
MC
966{
967 if (group->meth->invert == 0) {
9311d0c4 968 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
969 return 0;
970 }
b14e6015 971 if (!ec_point_is_compat(a, group)) {
9311d0c4 972 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
973 return 0;
974 }
975 return group->meth->invert(group, a, ctx);
976}
1d5bd6cf 977
0657bf9c 978int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
0f113f3e
MC
979{
980 if (group->meth->is_at_infinity == 0) {
9311d0c4 981 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
982 return 0;
983 }
b14e6015 984 if (!ec_point_is_compat(point, group)) {
9311d0c4 985 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
986 return 0;
987 }
988 return group->meth->is_at_infinity(group, point);
989}
990
68886be7
MC
991/*
992 * Check whether an EC_POINT is on the curve or not. Note that the return
993 * value for this function should NOT be treated as a boolean. Return values:
994 * 1: The point is on the curve
995 * 0: The point is not on the curve
996 * -1: An error occurred
997 */
0f113f3e
MC
998int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
999 BN_CTX *ctx)
1000{
1001 if (group->meth->is_on_curve == 0) {
9311d0c4 1002 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
1003 return 0;
1004 }
b14e6015 1005 if (!ec_point_is_compat(point, group)) {
9311d0c4 1006 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
1007 return 0;
1008 }
1009 return group->meth->is_on_curve(group, point, ctx);
1010}
1011
1012int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1013 BN_CTX *ctx)
1014{
1015 if (group->meth->point_cmp == 0) {
9311d0c4 1016 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
1017 return -1;
1018 }
b14e6015 1019 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
9311d0c4 1020 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
1021 return -1;
1022 }
1023 return group->meth->point_cmp(group, a, b, ctx);
1024}
1d5bd6cf 1025
c2f2db9b 1026#ifndef OPENSSL_NO_DEPRECATED_3_0
e869d4bd 1027int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
0f113f3e
MC
1028{
1029 if (group->meth->make_affine == 0) {
9311d0c4 1030 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
1031 return 0;
1032 }
b14e6015 1033 if (!ec_point_is_compat(point, group)) {
9311d0c4 1034 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
1035 return 0;
1036 }
1037 return group->meth->make_affine(group, point, ctx);
1038}
1039
1040int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1041 EC_POINT *points[], BN_CTX *ctx)
1042{
1043 size_t i;
1044
1045 if (group->meth->points_make_affine == 0) {
9311d0c4 1046 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
1047 return 0;
1048 }
1049 for (i = 0; i < num; i++) {
b14e6015 1050 if (!ec_point_is_compat(points[i], group)) {
9311d0c4 1051 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
1052 return 0;
1053 }
1054 }
1055 return group->meth->points_make_affine(group, num, points, ctx);
1056}
c2f2db9b 1057#endif
0f113f3e
MC
1058
1059/*
1060 * Functions for point multiplication. If group->meth->mul is 0, we use the
1061 * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
1062 * methods.
37c660ff
BM
1063 */
1064
4fcd15c1 1065#ifndef OPENSSL_NO_DEPRECATED_3_0
37c660ff 1066int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
0f113f3e
MC
1067 size_t num, const EC_POINT *points[],
1068 const BIGNUM *scalars[], BN_CTX *ctx)
1069{
01ad66f8
NT
1070 int ret = 0;
1071 size_t i = 0;
f844f9eb 1072#ifndef FIPS_MODULE
01ad66f8 1073 BN_CTX *new_ctx = NULL;
a9612d6c 1074#endif
a9612d6c 1075
01ad66f8 1076 if (!ec_point_is_compat(r, group)) {
9311d0c4 1077 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
01ad66f8
NT
1078 return 0;
1079 }
1eb9b54a
BE
1080
1081 if (scalar == NULL && num == 0)
1082 return EC_POINT_set_to_infinity(group, r);
1083
01ad66f8
NT
1084 for (i = 0; i < num; i++) {
1085 if (!ec_point_is_compat(points[i], group)) {
9311d0c4 1086 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
01ad66f8
NT
1087 return 0;
1088 }
1089 }
1090
f844f9eb 1091#ifndef FIPS_MODULE
0e8b6c97
AT
1092 if (ctx == NULL)
1093 ctx = new_ctx = BN_CTX_secure_new();
1094#endif
1095 if (ctx == NULL) {
9311d0c4 1096 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0e8b6c97
AT
1097 return 0;
1098 }
1099
01ad66f8
NT
1100 if (group->meth->mul != NULL)
1101 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1102 else
0f113f3e 1103 /* use default */
32ab57cb 1104 ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
37c660ff 1105
f844f9eb 1106#ifndef FIPS_MODULE
01ad66f8 1107 BN_CTX_free(new_ctx);
a9612d6c 1108#endif
01ad66f8 1109 return ret;
0f113f3e 1110}
4fcd15c1 1111#endif
37c660ff
BM
1112
1113int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
0f113f3e
MC
1114 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1115{
4fcd15c1 1116 int ret = 0;
9c47a338 1117 size_t num;
4fcd15c1
BB
1118#ifndef FIPS_MODULE
1119 BN_CTX *new_ctx = NULL;
1120#endif
37c660ff 1121
4fcd15c1
BB
1122 if (!ec_point_is_compat(r, group)
1123 || (point != NULL && !ec_point_is_compat(point, group))) {
9311d0c4 1124 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
4fcd15c1
BB
1125 return 0;
1126 }
37c660ff 1127
4fcd15c1
BB
1128 if (g_scalar == NULL && p_scalar == NULL)
1129 return EC_POINT_set_to_infinity(group, r);
37c660ff 1130
4fcd15c1
BB
1131#ifndef FIPS_MODULE
1132 if (ctx == NULL)
1133 ctx = new_ctx = BN_CTX_secure_new();
1134#endif
1135 if (ctx == NULL) {
9311d0c4 1136 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
4fcd15c1
BB
1137 return 0;
1138 }
1139
9c47a338 1140 num = (point != NULL && p_scalar != NULL) ? 1 : 0;
4fcd15c1 1141 if (group->meth->mul != NULL)
9c47a338 1142 ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
4fcd15c1
BB
1143 else
1144 /* use default */
32ab57cb 1145 ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
4fcd15c1
BB
1146
1147#ifndef FIPS_MODULE
1148 BN_CTX_free(new_ctx);
1149#endif
1150 return ret;
0f113f3e 1151}
37c660ff 1152
6b4eb933 1153#ifndef OPENSSL_NO_DEPRECATED_3_0
37c660ff 1154int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
1155{
1156 if (group->meth->mul == 0)
1157 /* use default */
32ab57cb 1158 return ossl_ec_wNAF_precompute_mult(group, ctx);
37c660ff 1159
0f113f3e
MC
1160 if (group->meth->precompute_mult != 0)
1161 return group->meth->precompute_mult(group, ctx);
1162 else
1163 return 1; /* nothing to do, so report success */
1164}
37c660ff
BM
1165
1166int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
0f113f3e
MC
1167{
1168 if (group->meth->mul == 0)
1169 /* use default */
32ab57cb 1170 return ossl_ec_wNAF_have_precompute_mult(group);
0f113f3e
MC
1171
1172 if (group->meth->have_precompute_mult != 0)
1173 return group->meth->have_precompute_mult(group);
1174 else
1175 return 0; /* cannot tell whether precomputation has
1176 * been performed */
1177}
6b4eb933 1178#endif
0f113f3e
MC
1179
1180/*
1181 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1182 * returns one on success. On error it returns zero.
1183 */
eb791696 1184static int ec_precompute_mont_data(EC_GROUP *group)
0f113f3e 1185{
a9612d6c 1186 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
0f113f3e
MC
1187 int ret = 0;
1188
23a1d5e9
RS
1189 BN_MONT_CTX_free(group->mont_data);
1190 group->mont_data = NULL;
0f113f3e
MC
1191
1192 if (ctx == NULL)
1193 goto err;
1194
1195 group->mont_data = BN_MONT_CTX_new();
90945fa3 1196 if (group->mont_data == NULL)
0f113f3e
MC
1197 goto err;
1198
1199 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1200 BN_MONT_CTX_free(group->mont_data);
1201 group->mont_data = NULL;
1202 goto err;
1203 }
1204
1205 ret = 1;
1206
1207 err:
1208
23a1d5e9 1209 BN_CTX_free(ctx);
0f113f3e
MC
1210 return ret;
1211}
3aef36ff 1212
f844f9eb 1213#ifndef FIPS_MODULE
3aef36ff
RS
1214int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
1215{
1216 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
1217}
1218
1219void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
1220{
1221 return CRYPTO_get_ex_data(&key->ex_data, idx);
1222}
a9612d6c 1223#endif
77470e98 1224
32ab57cb 1225int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
77470e98
DSH
1226{
1227 if (group->order == NULL)
1228 return 0;
1229 return BN_num_bits(group->order);
1230}
eb791696 1231
c11d372b 1232static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
792546eb 1233 const BIGNUM *x, BN_CTX *ctx)
c11d372b 1234{
262dccc0 1235 BIGNUM *e = NULL;
c11d372b 1236 int ret = 0;
f844f9eb 1237#ifndef FIPS_MODULE
a9612d6c 1238 BN_CTX *new_ctx = NULL;
0e8b6c97
AT
1239#endif
1240
1241 if (group->mont_data == NULL)
1242 return 0;
c11d372b 1243
f844f9eb 1244#ifndef FIPS_MODULE
a9612d6c
MC
1245 if (ctx == NULL)
1246 ctx = new_ctx = BN_CTX_secure_new();
1247#endif
1248 if (ctx == NULL)
792546eb
BB
1249 return 0;
1250
c11d372b 1251 BN_CTX_start(ctx);
262dccc0 1252 if ((e = BN_CTX_get(ctx)) == NULL)
c11d372b
BB
1253 goto err;
1254
792546eb
BB
1255 /*-
1256 * We want inverse in constant time, therefore we utilize the fact
1257 * order must be prime and use Fermats Little Theorem instead.
1258 */
1259 if (!BN_set_word(e, 2))
1260 goto err;
1261 if (!BN_sub(e, group->order, e))
1262 goto err;
1263 /*-
1264 * Exponent e is public.
1265 * No need for scatter-gather or BN_FLG_CONSTTIME.
1266 */
1267 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1268 goto err;
c11d372b 1269
792546eb 1270 ret = 1;
c11d372b
BB
1271
1272 err:
ce1415ed 1273 BN_CTX_end(ctx);
f844f9eb 1274#ifndef FIPS_MODULE
c11d372b 1275 BN_CTX_free(new_ctx);
a9612d6c 1276#endif
c11d372b
BB
1277 return ret;
1278}
1279
792546eb
BB
1280/*-
1281 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1282 * - When group->order is even, this function returns an error.
1283 * - When group->order is otherwise composite, the correctness
1284 * of the output is not guaranteed.
1285 * - When x is outside the range [1, group->order), the correctness
1286 * of the output is not guaranteed.
1287 * - Otherwise, this function returns the multiplicative inverse in the
1288 * range [1, group->order).
1289 *
1290 * EC_METHODs must implement their own field_inverse_mod_ord for
1291 * other functionality.
1292 */
32ab57cb
SL
1293int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1294 const BIGNUM *x, BN_CTX *ctx)
eb791696
AP
1295{
1296 if (group->meth->field_inverse_mod_ord != NULL)
1297 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1298 else
c11d372b 1299 return ec_field_inverse_mod_ord(group, res, x, ctx);
eb791696 1300}
f667820c
SH
1301
1302/*-
1303 * Coordinate blinding for EC_POINT.
1304 *
1305 * The underlying EC_METHOD can optionally implement this function:
1306 * underlying implementations should return 0 on errors, or 1 on
1307 * success.
1308 *
1309 * This wrapper returns 1 in case the underlying EC_METHOD does not
1310 * support coordinate blinding.
1311 */
32ab57cb
SL
1312int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1313 BN_CTX *ctx)
f667820c
SH
1314{
1315 if (group->meth->blind_coordinates == NULL)
1316 return 1; /* ignore if not implemented */
1317
1318 return group->meth->blind_coordinates(group, p, ctx);
1319}
c0f39ded
SL
1320
1321int EC_GROUP_get_basis_type(const EC_GROUP *group)
1322{
1323 int i;
1324
1325 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
1326 /* everything else is currently not supported */
1327 return 0;
1328
1329 /* Find the last non-zero element of group->poly[] */
1330 for (i = 0;
1331 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
1332 i++)
1333 continue;
1334
1335 if (i == 4)
1336 return NID_X9_62_ppBasis;
1337 else if (i == 2)
1338 return NID_X9_62_tpBasis;
1339 else
1340 /* everything else is currently not supported */
1341 return 0;
1342}
1343
1344#ifndef OPENSSL_NO_EC2M
1345int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
1346{
1347 if (group == NULL)
1348 return 0;
1349
1350 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1351 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1352 && (group->poly[2] == 0))) {
9311d0c4 1353 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
c0f39ded
SL
1354 return 0;
1355 }
1356
1357 if (k)
1358 *k = group->poly[1];
1359
1360 return 1;
1361}
1362
1363int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1364 unsigned int *k2, unsigned int *k3)
1365{
1366 if (group == NULL)
1367 return 0;
1368
1369 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1370 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1371 && (group->poly[2] != 0) && (group->poly[3] != 0)
1372 && (group->poly[4] == 0))) {
9311d0c4 1373 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
c0f39ded
SL
1374 return 0;
1375 }
1376
1377 if (k1)
1378 *k1 = group->poly[3];
1379 if (k2)
1380 *k2 = group->poly[2];
1381 if (k3)
1382 *k3 = group->poly[1];
1383
1384 return 1;
1385}
1386#endif
1387
638c3a28 1388#ifndef FIPS_MODULE
c0f39ded
SL
1389/*
1390 * Check if the explicit parameters group matches any built-in curves.
1391 *
1392 * We create a copy of the group just built, so that we can remove optional
1393 * fields for the lookup: we do this to avoid the possibility that one of
1394 * the optional parameters is used to force the library into using a less
1395 * performant and less secure EC_METHOD instead of the specialized one.
1396 * In any case, `seed` is not really used in any computation, while a
1397 * cofactor different from the one in the built-in table is just
1398 * mathematically wrong anyway and should not be used.
1399 */
1400static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
b4250010 1401 OSSL_LIB_CTX *libctx,
c0f39ded
SL
1402 const char *propq,
1403 BN_CTX *ctx)
1404{
1405 EC_GROUP *ret_group = NULL, *dup = NULL;
1406 int curve_name_nid;
1407
1408 const EC_POINT *point = EC_GROUP_get0_generator(group);
1409 const BIGNUM *order = EC_GROUP_get0_order(group);
1410 int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1411
1412 if ((dup = EC_GROUP_dup(group)) == NULL
1413 || EC_GROUP_set_seed(dup, NULL, 0) != 1
1414 || !EC_GROUP_set_generator(dup, point, order, NULL))
1415 goto err;
32ab57cb 1416 if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
c0f39ded
SL
1417 /*
1418 * The input explicit parameters successfully matched one of the
1419 * built-in curves: often for built-in curves we have specialized
1420 * methods with better performance and hardening.
1421 *
1422 * In this case we replace the `EC_GROUP` created through explicit
1423 * parameters with one created from a named group.
1424 */
1425
638c3a28 1426# ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
c0f39ded
SL
1427 /*
1428 * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
1429 * the same curve, we prefer the SECP nid when matching explicit
1430 * parameters as that is associated with a specialized EC_METHOD.
1431 */
1432 if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
1433 curve_name_nid = NID_secp224r1;
638c3a28 1434# endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
c0f39ded 1435
d8652be0 1436 ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
c0f39ded
SL
1437 if (ret_group == NULL)
1438 goto err;
1439
1440 /*
1441 * Set the flag so that EC_GROUPs created from explicit parameters are
1442 * serialized using explicit parameters by default.
1443 */
1444 EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
1445
1446 /*
1447 * If the input params do not contain the optional seed field we make
1448 * sure it is not added to the returned group.
1449 *
1450 * The seed field is not really used inside libcrypto anyway, and
1451 * adding it to parsed explicit parameter keys would alter their DER
1452 * encoding output (because of the extra field) which could impact
1453 * applications fingerprinting keys by their DER encoding.
1454 */
1455 if (no_seed) {
1456 if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
1457 goto err;
1458 }
1459 } else {
1460 ret_group = (EC_GROUP *)group;
1461 }
1462 EC_GROUP_free(dup);
1463 return ret_group;
1464err:
1465 EC_GROUP_free(dup);
1466 EC_GROUP_free(ret_group);
1467 return NULL;
1468}
638c3a28 1469#endif /* FIPS_MODULE */
c0f39ded 1470
c0f39ded 1471static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
b4250010 1472 OSSL_LIB_CTX *libctx, const char *propq)
c0f39ded
SL
1473{
1474 int ok = 0, nid;
1475 const char *curve_name = NULL;
1476
1477 switch (p->data_type) {
1478 case OSSL_PARAM_UTF8_STRING:
1479 /* The OSSL_PARAM functions have no support for this */
1480 curve_name = p->data;
1481 ok = (curve_name != NULL);
1482 break;
1483 case OSSL_PARAM_UTF8_PTR:
1484 ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
1485 break;
1486 }
1487
1488 if (ok) {
32ab57cb 1489 nid = ossl_ec_curve_name2nid(curve_name);
c0f39ded 1490 if (nid == NID_undef) {
9311d0c4 1491 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
c0f39ded
SL
1492 return NULL;
1493 } else {
d8652be0 1494 return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
c0f39ded
SL
1495 }
1496 }
1497 return NULL;
1498}
1499
5b5eea4b 1500/* These parameters can be set directly into an EC_GROUP */
32ab57cb 1501int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
5b5eea4b
SL
1502{
1503 int encoding_flag = -1, format = -1;
1504 const OSSL_PARAM *p;
1505
1506 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
1507 if (p != NULL) {
32ab57cb 1508 if (!ossl_ec_pt_format_param2id(p, &format)) {
bd07cc1c 1509 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
5b5eea4b
SL
1510 return 0;
1511 }
1512 EC_GROUP_set_point_conversion_form(group, format);
1513 }
1514
1515 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1516 if (p != NULL) {
32ab57cb 1517 if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
bd07cc1c 1518 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
5b5eea4b
SL
1519 return 0;
1520 }
1521 EC_GROUP_set_asn1_flag(group, encoding_flag);
1522 }
1523 /* Optional seed */
1524 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1525 if (p != NULL) {
1526 /* The seed is allowed to be NULL */
1527 if (p->data_type != OSSL_PARAM_OCTET_STRING
1528 || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
bd07cc1c 1529 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
5b5eea4b
SL
1530 return 0;
1531 }
1532 }
1533 return 1;
1534}
1535
c0f39ded 1536EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
b4250010 1537 OSSL_LIB_CTX *libctx, const char *propq)
c0f39ded 1538{
638c3a28
TM
1539 const OSSL_PARAM *ptmp;
1540 EC_GROUP *group = NULL;
1541
1542#ifndef FIPS_MODULE
1543 const OSSL_PARAM *pa, *pb;
c0f39ded 1544 int ok = 0;
638c3a28 1545 EC_GROUP *named_group = NULL;
c0f39ded
SL
1546 BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
1547 EC_POINT *point = NULL;
1548 int field_bits = 0;
1549 int is_prime_field = 1;
1550 BN_CTX *bnctx = NULL;
1551 const unsigned char *buf = NULL;
1552 int encoding_flag = -1;
638c3a28 1553#endif
c0f39ded 1554
5b5eea4b 1555 /* This is the simple named group case */
c0f39ded
SL
1556 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
1557 if (ptmp != NULL) {
95a6fbdf
TM
1558 int decoded = 0;
1559
1560 if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
1561 return NULL;
1562 if (!ossl_ec_group_set_params(group, params)) {
1563 EC_GROUP_free(group);
1564 return NULL;
1565 }
1566
1567 ptmp = OSSL_PARAM_locate_const(params,
1568 OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
1569 if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
1570 ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
1571 EC_GROUP_free(group);
1572 return NULL;
5b5eea4b 1573 }
95a6fbdf 1574 group->decoded_from_explicit_params = decoded > 0;
c0f39ded
SL
1575 return group;
1576 }
638c3a28 1577#ifdef FIPS_MODULE
53137462 1578 ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
638c3a28
TM
1579 return NULL;
1580#else
5b5eea4b 1581 /* If it gets here then we are trying explicit parameters */
c0f39ded
SL
1582 bnctx = BN_CTX_new_ex(libctx);
1583 if (bnctx == NULL) {
e077455e 1584 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
c0f39ded
SL
1585 return 0;
1586 }
1587 BN_CTX_start(bnctx);
1588
1589 p = BN_CTX_get(bnctx);
1590 a = BN_CTX_get(bnctx);
1591 b = BN_CTX_get(bnctx);
1592 order = BN_CTX_get(bnctx);
1593 if (order == NULL) {
e077455e 1594 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
c0f39ded
SL
1595 goto err;
1596 }
1597
1598 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
1599 if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
9311d0c4 1600 ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
c0f39ded
SL
1601 goto err;
1602 }
fba140c7 1603 if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
c0f39ded 1604 is_prime_field = 1;
fba140c7
DB
1605 } else if (OPENSSL_strcasecmp(ptmp->data,
1606 SN_X9_62_characteristic_two_field) == 0) {
c0f39ded
SL
1607 is_prime_field = 0;
1608 } else {
1609 /* Invalid field */
9311d0c4 1610 ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
c0f39ded
SL
1611 goto err;
1612 }
1613
1614 pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
1615 if (!OSSL_PARAM_get_BN(pa, &a)) {
9311d0c4 1616 ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
c0f39ded
SL
1617 goto err;
1618 }
1619 pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
1620 if (!OSSL_PARAM_get_BN(pb, &b)) {
9311d0c4 1621 ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
c0f39ded
SL
1622 goto err;
1623 }
1624
1625 /* extract the prime number or irreducible polynomial */
1626 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
1627 if (!OSSL_PARAM_get_BN(ptmp, &p)) {
9311d0c4 1628 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
c0f39ded
SL
1629 goto err;
1630 }
1631
1632 if (is_prime_field) {
1633 if (BN_is_negative(p) || BN_is_zero(p)) {
9311d0c4 1634 ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
c0f39ded
SL
1635 goto err;
1636 }
1637 field_bits = BN_num_bits(p);
1638 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
9311d0c4 1639 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
c0f39ded
SL
1640 goto err;
1641 }
1642
1643 /* create the EC_GROUP structure */
1644 group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1645 } else {
638c3a28 1646# ifdef OPENSSL_NO_EC2M
9311d0c4 1647 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
c0f39ded 1648 goto err;
638c3a28 1649# else
c0f39ded
SL
1650 /* create the EC_GROUP structure */
1651 group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1652 if (group != NULL) {
1653 field_bits = EC_GROUP_get_degree(group);
1654 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
9311d0c4 1655 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
c0f39ded
SL
1656 goto err;
1657 }
1658 }
638c3a28 1659# endif /* OPENSSL_NO_EC2M */
c0f39ded
SL
1660 }
1661
1662 if (group == NULL) {
9311d0c4 1663 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
c0f39ded
SL
1664 goto err;
1665 }
1666
1667 /* Optional seed */
1668 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
1669 if (ptmp != NULL) {
1670 if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
9311d0c4 1671 ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
c0f39ded
SL
1672 goto err;
1673 }
1674 if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1675 goto err;
1676 }
1677
1678 /* generator base point */
1679 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
1680 if (ptmp == NULL
1681 || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
9311d0c4 1682 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
c0f39ded
SL
1683 goto err;
1684 }
1685 buf = (const unsigned char *)(ptmp->data);
1686 if ((point = EC_POINT_new(group)) == NULL)
1687 goto err;
1688 EC_GROUP_set_point_conversion_form(group,
1689 (point_conversion_form_t)buf[0] & ~0x01);
1690 if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
9311d0c4 1691 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
c0f39ded
SL
1692 goto err;
1693 }
1694
1695 /* order */
1696 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
1697 if (!OSSL_PARAM_get_BN(ptmp, &order)
1698 || (BN_is_negative(order) || BN_is_zero(order))
1699 || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
9311d0c4 1700 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
c0f39ded
SL
1701 goto err;
1702 }
1703
1704 /* Optional cofactor */
1705 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
1706 if (ptmp != NULL) {
1707 cofactor = BN_CTX_get(bnctx);
1708 if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
9311d0c4 1709 ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
c0f39ded
SL
1710 goto err;
1711 }
1712 }
1713
1714 /* set the generator, order and cofactor (if present) */
1715 if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
9311d0c4 1716 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
c0f39ded
SL
1717 goto err;
1718 }
1719
1720 named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1721 if (named_group == NULL) {
9311d0c4 1722 ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
c0f39ded
SL
1723 goto err;
1724 }
1725 if (named_group == group) {
1726 /*
1727 * If we did not find a named group then the encoding should be explicit
1728 * if it was specified
1729 */
5b5eea4b
SL
1730 ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1731 if (ptmp != NULL
32ab57cb 1732 && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
10481d33
PH
1733 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
1734 goto err;
5b5eea4b 1735 }
c0f39ded 1736 if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
9311d0c4 1737 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
c0f39ded
SL
1738 goto err;
1739 }
1740 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1741 } else {
1742 EC_GROUP_free(group);
1743 group = named_group;
1744 }
95a6fbdf
TM
1745 /* We've imported the group from explicit parameters, set it so. */
1746 group->decoded_from_explicit_params = 1;
c0f39ded
SL
1747 ok = 1;
1748 err:
1749 if (!ok) {
1750 EC_GROUP_free(group);
1751 group = NULL;
1752 }
1753 EC_POINT_free(point);
1754 BN_CTX_end(bnctx);
1755 BN_CTX_free(bnctx);
1756
1757 return group;
638c3a28 1758#endif /* FIPS_MODULE */
c0f39ded 1759}
a8aad913
OM
1760
1761OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
1762 const char *propq, BN_CTX *bnctx)
1763{
1764 OSSL_PARAM_BLD *tmpl = NULL;
1765 BN_CTX *new_bnctx = NULL;
1766 unsigned char *gen_buf = NULL;
1767 OSSL_PARAM *params = NULL;
1768
1769 if (group == NULL)
1770 goto err;
1771
1772 tmpl = OSSL_PARAM_BLD_new();
1773 if (tmpl == NULL)
1774 goto err;
1775
1776 if (bnctx == NULL)
1777 bnctx = new_bnctx = BN_CTX_new_ex(libctx);
1778 if (bnctx == NULL)
1779 goto err;
1780 BN_CTX_start(bnctx);
1781
1782 if (!ossl_ec_group_todata(
1783 group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
1784 goto err;
1785
1786 params = OSSL_PARAM_BLD_to_param(tmpl);
1787
1788 err:
1789 OSSL_PARAM_BLD_free(tmpl);
1790 OPENSSL_free(gen_buf);
1791 BN_CTX_end(bnctx);
1792 BN_CTX_free(new_bnctx);
1793 return params;
1794}