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