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