]>
Commit | Line | Data |
---|---|---|
35b73a1f | 1 | /* |
33388b44 | 2 | * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
65e81670 | 4 | * |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
65e81670 | 9 | */ |
aa6bb135 | 10 | |
579422c8 P |
11 | /* |
12 | * ECDSA low level APIs are deprecated for public use, but still ok for | |
13 | * internal use. | |
14 | */ | |
15 | #include "internal/deprecated.h" | |
16 | ||
c4b36ff4 | 17 | #include <string.h> |
c0f39ded SL |
18 | #include <openssl/params.h> |
19 | #include <openssl/core_names.h> | |
0657bf9c | 20 | #include <openssl/err.h> |
bb62a8b0 | 21 | #include <openssl/opensslv.h> |
c0f39ded SL |
22 | #include "crypto/ec.h" |
23 | #include "internal/nelem.h" | |
706457b7 | 24 | #include "ec_local.h" |
c0f39ded | 25 | #include "e_os.h" /* strcasecmp */ |
0657bf9c | 26 | |
0657bf9c BM |
27 | /* functions for EC_GROUP objects */ |
28 | ||
d8652be0 MC |
29 | EC_GROUP *ec_group_new_ex(OPENSSL_CTX *libctx, const char *propq, |
30 | const EC_METHOD *meth) | |
0f113f3e MC |
31 | { |
32 | EC_GROUP *ret; | |
33 | ||
34 | if (meth == NULL) { | |
2da8d4eb | 35 | ECerr(0, EC_R_SLOT_FULL); |
0f113f3e MC |
36 | return NULL; |
37 | } | |
38 | if (meth->group_init == 0) { | |
2da8d4eb | 39 | ECerr(0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
40 | return NULL; |
41 | } | |
42 | ||
64b25758 | 43 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 44 | if (ret == NULL) { |
2da8d4eb | 45 | ECerr(0, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
46 | return NULL; |
47 | } | |
48 | ||
a9612d6c | 49 | ret->libctx = libctx; |
2da8d4eb MC |
50 | if (propq != NULL) { |
51 | ret->propq = OPENSSL_strdup(propq); | |
52 | if (ret->propq == NULL) { | |
53 | ECerr(0, ERR_R_MALLOC_FAILURE); | |
54 | goto err; | |
55 | } | |
56 | } | |
0f113f3e | 57 | ret->meth = meth; |
6903e2e7 DSH |
58 | if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
59 | ret->order = BN_new(); | |
60 | if (ret->order == NULL) | |
61 | goto err; | |
62 | ret->cofactor = BN_new(); | |
63 | if (ret->cofactor == NULL) | |
64 | goto err; | |
65 | } | |
86f300d3 | 66 | ret->asn1_flag = OPENSSL_EC_NAMED_CURVE; |
0f113f3e | 67 | ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED; |
0f113f3e MC |
68 | if (!meth->group_init(ret)) |
69 | goto err; | |
0f113f3e | 70 | return ret; |
64b25758 | 71 | |
0f113f3e | 72 | err: |
23a1d5e9 RS |
73 | BN_free(ret->order); |
74 | BN_free(ret->cofactor); | |
2da8d4eb | 75 | OPENSSL_free(ret->propq); |
0f113f3e MC |
76 | OPENSSL_free(ret); |
77 | return NULL; | |
78 | } | |
0657bf9c | 79 | |
23ccae80 BB |
80 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
81 | # ifndef FIPS_MODULE | |
a9612d6c MC |
82 | EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) |
83 | { | |
d8652be0 | 84 | return ec_group_new_ex(NULL, NULL, meth); |
a9612d6c | 85 | } |
23ccae80 | 86 | # endif |
a9612d6c MC |
87 | #endif |
88 | ||
2c52ac9b | 89 | void EC_pre_comp_free(EC_GROUP *group) |
3aef36ff RS |
90 | { |
91 | switch (group->pre_comp_type) { | |
f3b3d7f0 | 92 | case PCT_none: |
3aef36ff | 93 | break; |
66117ab0 | 94 | case PCT_nistz256: |
f3b3d7f0 | 95 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 96 | EC_nistz256_pre_comp_free(group->pre_comp.nistz256); |
e69aa800 | 97 | #endif |
f3b3d7f0 | 98 | break; |
3aef36ff | 99 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 100 | case PCT_nistp224: |
3aef36ff RS |
101 | EC_nistp224_pre_comp_free(group->pre_comp.nistp224); |
102 | break; | |
66117ab0 | 103 | case PCT_nistp256: |
3aef36ff RS |
104 | EC_nistp256_pre_comp_free(group->pre_comp.nistp256); |
105 | break; | |
66117ab0 | 106 | case PCT_nistp521: |
3aef36ff RS |
107 | EC_nistp521_pre_comp_free(group->pre_comp.nistp521); |
108 | break; | |
f3b3d7f0 RS |
109 | #else |
110 | case PCT_nistp224: | |
111 | case PCT_nistp256: | |
112 | case PCT_nistp521: | |
113 | break; | |
3aef36ff | 114 | #endif |
66117ab0 | 115 | case PCT_ec: |
3aef36ff RS |
116 | EC_ec_pre_comp_free(group->pre_comp.ec); |
117 | break; | |
118 | } | |
119 | group->pre_comp.ec = NULL; | |
120 | } | |
121 | ||
0657bf9c | 122 | void EC_GROUP_free(EC_GROUP *group) |
0f113f3e MC |
123 | { |
124 | if (!group) | |
125 | return; | |
7711de24 | 126 | |
0f113f3e MC |
127 | if (group->meth->group_finish != 0) |
128 | group->meth->group_finish(group); | |
df9cc153 | 129 | |
2c52ac9b | 130 | EC_pre_comp_free(group); |
23a1d5e9 | 131 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 132 | EC_POINT_free(group->generator); |
0f113f3e MC |
133 | BN_free(group->order); |
134 | BN_free(group->cofactor); | |
25aaa98a | 135 | OPENSSL_free(group->seed); |
2da8d4eb | 136 | OPENSSL_free(group->propq); |
0f113f3e MC |
137 | OPENSSL_free(group); |
138 | } | |
0657bf9c | 139 | |
936c2b9e | 140 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0657bf9c | 141 | void EC_GROUP_clear_free(EC_GROUP *group) |
0f113f3e MC |
142 | { |
143 | if (!group) | |
144 | return; | |
df9cc153 | 145 | |
0f113f3e MC |
146 | if (group->meth->group_clear_finish != 0) |
147 | group->meth->group_clear_finish(group); | |
148 | else if (group->meth->group_finish != 0) | |
149 | group->meth->group_finish(group); | |
df9cc153 | 150 | |
2c52ac9b | 151 | EC_pre_comp_free(group); |
23a1d5e9 | 152 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 153 | EC_POINT_clear_free(group->generator); |
0f113f3e MC |
154 | BN_clear_free(group->order); |
155 | BN_clear_free(group->cofactor); | |
4b45c6e5 | 156 | OPENSSL_clear_free(group->seed, group->seed_len); |
b4faea50 | 157 | OPENSSL_clear_free(group, sizeof(*group)); |
0f113f3e | 158 | } |
4a7a4972 | 159 | #endif |
0657bf9c BM |
160 | |
161 | int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) | |
0f113f3e | 162 | { |
0f113f3e MC |
163 | if (dest->meth->group_copy == 0) { |
164 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
165 | return 0; | |
166 | } | |
167 | if (dest->meth != src->meth) { | |
168 | ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS); | |
169 | return 0; | |
170 | } | |
171 | if (dest == src) | |
172 | return 1; | |
173 | ||
a9612d6c | 174 | dest->libctx = src->libctx; |
b14e6015 MC |
175 | dest->curve_name = src->curve_name; |
176 | ||
3aef36ff RS |
177 | /* Copy precomputed */ |
178 | dest->pre_comp_type = src->pre_comp_type; | |
179 | switch (src->pre_comp_type) { | |
f3b3d7f0 | 180 | case PCT_none: |
3aef36ff RS |
181 | dest->pre_comp.ec = NULL; |
182 | break; | |
66117ab0 | 183 | case PCT_nistz256: |
f3b3d7f0 | 184 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 185 | dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256); |
e69aa800 | 186 | #endif |
f3b3d7f0 | 187 | break; |
3aef36ff | 188 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 189 | case PCT_nistp224: |
3aef36ff RS |
190 | dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224); |
191 | break; | |
66117ab0 | 192 | case PCT_nistp256: |
3aef36ff RS |
193 | dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256); |
194 | break; | |
66117ab0 | 195 | case PCT_nistp521: |
3aef36ff RS |
196 | dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521); |
197 | break; | |
f3b3d7f0 RS |
198 | #else |
199 | case PCT_nistp224: | |
200 | case PCT_nistp256: | |
201 | case PCT_nistp521: | |
202 | break; | |
3aef36ff | 203 | #endif |
66117ab0 | 204 | case PCT_ec: |
3aef36ff RS |
205 | dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec); |
206 | break; | |
0f113f3e MC |
207 | } |
208 | ||
209 | if (src->mont_data != NULL) { | |
210 | if (dest->mont_data == NULL) { | |
211 | dest->mont_data = BN_MONT_CTX_new(); | |
212 | if (dest->mont_data == NULL) | |
213 | return 0; | |
214 | } | |
215 | if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) | |
216 | return 0; | |
217 | } else { | |
218 | /* src->generator == NULL */ | |
23a1d5e9 RS |
219 | BN_MONT_CTX_free(dest->mont_data); |
220 | dest->mont_data = NULL; | |
0f113f3e | 221 | } |
0657bf9c | 222 | |
0f113f3e MC |
223 | if (src->generator != NULL) { |
224 | if (dest->generator == NULL) { | |
225 | dest->generator = EC_POINT_new(dest); | |
226 | if (dest->generator == NULL) | |
227 | return 0; | |
228 | } | |
229 | if (!EC_POINT_copy(dest->generator, src->generator)) | |
230 | return 0; | |
231 | } else { | |
232 | /* src->generator == NULL */ | |
8fdc3734 RS |
233 | EC_POINT_clear_free(dest->generator); |
234 | dest->generator = NULL; | |
0f113f3e MC |
235 | } |
236 | ||
6903e2e7 DSH |
237 | if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
238 | if (!BN_copy(dest->order, src->order)) | |
239 | return 0; | |
240 | if (!BN_copy(dest->cofactor, src->cofactor)) | |
241 | return 0; | |
242 | } | |
0f113f3e | 243 | |
0f113f3e MC |
244 | dest->asn1_flag = src->asn1_flag; |
245 | dest->asn1_form = src->asn1_form; | |
fe2f8aec | 246 | dest->decoded_from_explicit_params = src->decoded_from_explicit_params; |
0f113f3e MC |
247 | |
248 | if (src->seed) { | |
b548a1f1 | 249 | OPENSSL_free(dest->seed); |
cdb10bae RS |
250 | if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) { |
251 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 252 | return 0; |
cdb10bae | 253 | } |
0f113f3e MC |
254 | if (!memcpy(dest->seed, src->seed, src->seed_len)) |
255 | return 0; | |
256 | dest->seed_len = src->seed_len; | |
257 | } else { | |
b548a1f1 | 258 | OPENSSL_free(dest->seed); |
0f113f3e MC |
259 | dest->seed = NULL; |
260 | dest->seed_len = 0; | |
261 | } | |
262 | ||
263 | return dest->meth->group_copy(dest, src); | |
264 | } | |
0657bf9c | 265 | |
7793f30e | 266 | EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) |
0f113f3e MC |
267 | { |
268 | EC_GROUP *t = NULL; | |
269 | int ok = 0; | |
7793f30e | 270 | |
0f113f3e MC |
271 | if (a == NULL) |
272 | return NULL; | |
7793f30e | 273 | |
d8652be0 | 274 | if ((t = ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL) |
26a7d938 | 275 | return NULL; |
0f113f3e MC |
276 | if (!EC_GROUP_copy(t, a)) |
277 | goto err; | |
7793f30e | 278 | |
0f113f3e | 279 | ok = 1; |
7793f30e | 280 | |
0f113f3e MC |
281 | err: |
282 | if (!ok) { | |
8fdc3734 | 283 | EC_GROUP_free(t); |
0f113f3e | 284 | return NULL; |
8fdc3734 | 285 | } |
0f113f3e MC |
286 | return t; |
287 | } | |
7793f30e | 288 | |
23ccae80 | 289 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
48fe4d62 | 290 | const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) |
0f113f3e MC |
291 | { |
292 | return group->meth; | |
293 | } | |
48fe4d62 | 294 | |
458c2917 | 295 | int EC_METHOD_get_field_type(const EC_METHOD *meth) |
0f113f3e MC |
296 | { |
297 | return meth->field_type; | |
298 | } | |
23ccae80 | 299 | #endif |
0f113f3e | 300 | |
eb791696 AP |
301 | static int ec_precompute_mont_data(EC_GROUP *); |
302 | ||
b783beea BB |
303 | /*- |
304 | * Try computing cofactor from the generator order (n) and field cardinality (q). | |
305 | * This works for all curves of cryptographic interest. | |
306 | * | |
307 | * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q) | |
308 | * h_min = (q + 1 - 2*sqrt(q))/n | |
309 | * h_max = (q + 1 + 2*sqrt(q))/n | |
310 | * h_max - h_min = 4*sqrt(q)/n | |
311 | * So if n > 4*sqrt(q) holds, there is only one possible value for h: | |
312 | * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil | |
313 | * | |
314 | * Otherwise, zero cofactor and return success. | |
315 | */ | |
316 | static int ec_guess_cofactor(EC_GROUP *group) { | |
317 | int ret = 0; | |
318 | BN_CTX *ctx = NULL; | |
319 | BIGNUM *q = NULL; | |
320 | ||
321 | /*- | |
322 | * If the cofactor is too large, we cannot guess it. | |
323 | * The RHS of below is a strict overestimate of lg(4 * sqrt(q)) | |
324 | */ | |
325 | if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) { | |
326 | /* default to 0 */ | |
327 | BN_zero(group->cofactor); | |
328 | /* return success */ | |
329 | return 1; | |
330 | } | |
331 | ||
332 | if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL) | |
333 | return 0; | |
334 | ||
335 | BN_CTX_start(ctx); | |
336 | if ((q = BN_CTX_get(ctx)) == NULL) | |
337 | goto err; | |
338 | ||
339 | /* set q = 2**m for binary fields; q = p otherwise */ | |
340 | if (group->meth->field_type == NID_X9_62_characteristic_two_field) { | |
341 | BN_zero(q); | |
342 | if (!BN_set_bit(q, BN_num_bits(group->field) - 1)) | |
343 | goto err; | |
344 | } else { | |
345 | if (!BN_copy(q, group->field)) | |
346 | goto err; | |
347 | } | |
348 | ||
349 | /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */ | |
350 | if (!BN_rshift1(group->cofactor, group->order) /* n/2 */ | |
351 | || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */ | |
352 | /* q + 1 + n/2 */ | |
353 | || !BN_add(group->cofactor, group->cofactor, BN_value_one()) | |
354 | /* (q + 1 + n/2)/n */ | |
355 | || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx)) | |
356 | goto err; | |
357 | ret = 1; | |
358 | err: | |
359 | BN_CTX_end(ctx); | |
360 | BN_CTX_free(ctx); | |
361 | return ret; | |
362 | } | |
363 | ||
0f113f3e MC |
364 | int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, |
365 | const BIGNUM *order, const BIGNUM *cofactor) | |
366 | { | |
367 | if (generator == NULL) { | |
368 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER); | |
369 | return 0; | |
370 | } | |
371 | ||
b783beea BB |
372 | /* require group->field >= 1 */ |
373 | if (group->field == NULL || BN_is_zero(group->field) | |
374 | || BN_is_negative(group->field)) { | |
375 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | /*- | |
380 | * - require order >= 1 | |
381 | * - enforce upper bound due to Hasse thm: order can be no more than one bit | |
382 | * longer than field cardinality | |
383 | */ | |
384 | if (order == NULL || BN_is_zero(order) || BN_is_negative(order) | |
385 | || BN_num_bits(order) > BN_num_bits(group->field) + 1) { | |
386 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER); | |
387 | return 0; | |
388 | } | |
389 | ||
390 | /*- | |
391 | * Unfortunately the cofactor is an optional field in many standards. | |
392 | * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor". | |
393 | * So accept cofactor == NULL or cofactor >= 0. | |
394 | */ | |
395 | if (cofactor != NULL && BN_is_negative(cofactor)) { | |
396 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR); | |
397 | return 0; | |
398 | } | |
399 | ||
0f113f3e MC |
400 | if (group->generator == NULL) { |
401 | group->generator = EC_POINT_new(group); | |
402 | if (group->generator == NULL) | |
403 | return 0; | |
404 | } | |
405 | if (!EC_POINT_copy(group->generator, generator)) | |
406 | return 0; | |
407 | ||
b783beea BB |
408 | if (!BN_copy(group->order, order)) |
409 | return 0; | |
0f113f3e | 410 | |
b783beea BB |
411 | /* Either take the provided positive cofactor, or try to compute it */ |
412 | if (cofactor != NULL && !BN_is_zero(cofactor)) { | |
0f113f3e MC |
413 | if (!BN_copy(group->cofactor, cofactor)) |
414 | return 0; | |
b783beea | 415 | } else if (!ec_guess_cofactor(group)) { |
0f113f3e | 416 | BN_zero(group->cofactor); |
b783beea | 417 | return 0; |
8402cd5f | 418 | } |
b783beea | 419 | |
0f113f3e | 420 | /* |
3a6a4a93 | 421 | * Some groups have an order with |
0f113f3e MC |
422 | * factors of two, which makes the Montgomery setup fail. |
423 | * |group->mont_data| will be NULL in this case. | |
424 | */ | |
3a6a4a93 BB |
425 | if (BN_is_odd(group->order)) { |
426 | return ec_precompute_mont_data(group); | |
427 | } | |
0f113f3e | 428 | |
3a6a4a93 BB |
429 | BN_MONT_CTX_free(group->mont_data); |
430 | group->mont_data = NULL; | |
0f113f3e MC |
431 | return 1; |
432 | } | |
bb62a8b0 | 433 | |
9dd84053 | 434 | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) |
0f113f3e MC |
435 | { |
436 | return group->generator; | |
437 | } | |
bb62a8b0 | 438 | |
f54be179 | 439 | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group) |
0f113f3e MC |
440 | { |
441 | return group->mont_data; | |
442 | } | |
bb62a8b0 | 443 | |
b6db386f | 444 | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) |
0f113f3e | 445 | { |
be2e334f DSH |
446 | if (group->order == NULL) |
447 | return 0; | |
0f113f3e MC |
448 | if (!BN_copy(order, group->order)) |
449 | return 0; | |
0657bf9c | 450 | |
0f113f3e MC |
451 | return !BN_is_zero(order); |
452 | } | |
0657bf9c | 453 | |
be2e334f DSH |
454 | const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) |
455 | { | |
456 | return group->order; | |
457 | } | |
458 | ||
459 | int EC_GROUP_order_bits(const EC_GROUP *group) | |
460 | { | |
77470e98 | 461 | return group->meth->group_order_bits(group); |
be2e334f DSH |
462 | } |
463 | ||
0f113f3e MC |
464 | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
465 | BN_CTX *ctx) | |
466 | { | |
be2e334f DSH |
467 | |
468 | if (group->cofactor == NULL) | |
469 | return 0; | |
0f113f3e MC |
470 | if (!BN_copy(cofactor, group->cofactor)) |
471 | return 0; | |
48fe4d62 | 472 | |
0f113f3e MC |
473 | return !BN_is_zero(group->cofactor); |
474 | } | |
48fe4d62 | 475 | |
be2e334f DSH |
476 | const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group) |
477 | { | |
478 | return group->cofactor; | |
479 | } | |
480 | ||
7dc17a6c | 481 | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) |
0f113f3e MC |
482 | { |
483 | group->curve_name = nid; | |
484 | } | |
b6db386f | 485 | |
7dc17a6c | 486 | int EC_GROUP_get_curve_name(const EC_GROUP *group) |
0f113f3e MC |
487 | { |
488 | return group->curve_name; | |
489 | } | |
b6db386f | 490 | |
fa1f0306 DA |
491 | const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group) |
492 | { | |
493 | return group->field; | |
494 | } | |
495 | ||
23ccae80 BB |
496 | int EC_GROUP_get_field_type(const EC_GROUP *group) |
497 | { | |
498 | return group->meth->field_type; | |
499 | } | |
500 | ||
458c2917 | 501 | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) |
0f113f3e MC |
502 | { |
503 | group->asn1_flag = flag; | |
504 | } | |
458c2917 BM |
505 | |
506 | int EC_GROUP_get_asn1_flag(const EC_GROUP *group) | |
0f113f3e MC |
507 | { |
508 | return group->asn1_flag; | |
509 | } | |
458c2917 | 510 | |
0f113f3e | 511 | void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
254ef80d | 512 | point_conversion_form_t form) |
0f113f3e MC |
513 | { |
514 | group->asn1_form = form; | |
515 | } | |
254ef80d | 516 | |
0f113f3e MC |
517 | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP |
518 | *group) | |
519 | { | |
520 | return group->asn1_form; | |
521 | } | |
254ef80d | 522 | |
5f3d6f70 | 523 | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) |
0f113f3e | 524 | { |
b548a1f1 RS |
525 | OPENSSL_free(group->seed); |
526 | group->seed = NULL; | |
527 | group->seed_len = 0; | |
5f3d6f70 | 528 | |
0f113f3e MC |
529 | if (!len || !p) |
530 | return 1; | |
5f3d6f70 | 531 | |
f06080cb F |
532 | if ((group->seed = OPENSSL_malloc(len)) == NULL) { |
533 | ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 534 | return 0; |
f06080cb | 535 | } |
0f113f3e MC |
536 | memcpy(group->seed, p, len); |
537 | group->seed_len = len; | |
5f3d6f70 | 538 | |
0f113f3e MC |
539 | return len; |
540 | } | |
5f3d6f70 BM |
541 | |
542 | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) | |
0f113f3e MC |
543 | { |
544 | return group->seed; | |
545 | } | |
5f3d6f70 BM |
546 | |
547 | size_t EC_GROUP_get_seed_len(const EC_GROUP *group) | |
0f113f3e MC |
548 | { |
549 | return group->seed_len; | |
550 | } | |
551 | ||
8e3cced7 MC |
552 | int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
553 | const BIGNUM *b, BN_CTX *ctx) | |
0f113f3e MC |
554 | { |
555 | if (group->meth->group_set_curve == 0) { | |
8e3cced7 | 556 | ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
557 | return 0; |
558 | } | |
559 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
560 | } | |
561 | ||
8e3cced7 MC |
562 | int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, |
563 | BN_CTX *ctx) | |
0f113f3e | 564 | { |
8e3cced7 MC |
565 | if (group->meth->group_get_curve == NULL) { |
566 | ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
0f113f3e MC |
567 | return 0; |
568 | } | |
569 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
570 | } | |
7793f30e | 571 | |
936c2b9e | 572 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
8e3cced7 MC |
573 | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
574 | const BIGNUM *b, BN_CTX *ctx) | |
575 | { | |
576 | return EC_GROUP_set_curve(group, p, a, b, ctx); | |
577 | } | |
578 | ||
579 | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
580 | BIGNUM *b, BN_CTX *ctx) | |
581 | { | |
582 | return EC_GROUP_get_curve(group, p, a, b, ctx); | |
583 | } | |
584 | ||
50db8163 | 585 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
586 | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
587 | const BIGNUM *b, BN_CTX *ctx) | |
588 | { | |
8e3cced7 | 589 | return EC_GROUP_set_curve(group, p, a, b, ctx); |
0f113f3e MC |
590 | } |
591 | ||
592 | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
593 | BIGNUM *b, BN_CTX *ctx) | |
594 | { | |
8e3cced7 | 595 | return EC_GROUP_get_curve(group, p, a, b, ctx); |
0f113f3e | 596 | } |
50db8163 | 597 | # endif |
b3310161 | 598 | #endif |
7793f30e BM |
599 | |
600 | int EC_GROUP_get_degree(const EC_GROUP *group) | |
0f113f3e MC |
601 | { |
602 | if (group->meth->group_get_degree == 0) { | |
603 | ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
604 | return 0; | |
605 | } | |
606 | return group->meth->group_get_degree(group); | |
607 | } | |
48fe4d62 | 608 | |
17d6bb81 | 609 | int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
610 | { |
611 | if (group->meth->group_check_discriminant == 0) { | |
612 | ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, | |
613 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
614 | return 0; | |
615 | } | |
616 | return group->meth->group_check_discriminant(group, ctx); | |
617 | } | |
af28dd6c | 618 | |
ada0e717 | 619 | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) |
0f113f3e MC |
620 | { |
621 | int r = 0; | |
622 | BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; | |
f844f9eb | 623 | #ifndef FIPS_MODULE |
0f113f3e | 624 | BN_CTX *ctx_new = NULL; |
a9612d6c | 625 | #endif |
a9612d6c | 626 | |
0f113f3e | 627 | /* compare the field types */ |
23ccae80 | 628 | if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b)) |
0f113f3e MC |
629 | return 1; |
630 | /* compare the curve name (if present in both) */ | |
631 | if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && | |
632 | EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b)) | |
633 | return 1; | |
6903e2e7 DSH |
634 | if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE) |
635 | return 0; | |
0f113f3e | 636 | |
f844f9eb | 637 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
638 | if (ctx == NULL) |
639 | ctx_new = ctx = BN_CTX_new(); | |
640 | #endif | |
641 | if (ctx == NULL) | |
642 | return -1; | |
643 | ||
0f113f3e MC |
644 | BN_CTX_start(ctx); |
645 | a1 = BN_CTX_get(ctx); | |
646 | a2 = BN_CTX_get(ctx); | |
647 | a3 = BN_CTX_get(ctx); | |
648 | b1 = BN_CTX_get(ctx); | |
649 | b2 = BN_CTX_get(ctx); | |
650 | b3 = BN_CTX_get(ctx); | |
90945fa3 | 651 | if (b3 == NULL) { |
0f113f3e | 652 | BN_CTX_end(ctx); |
f844f9eb | 653 | #ifndef FIPS_MODULE |
23a1d5e9 | 654 | BN_CTX_free(ctx_new); |
a9612d6c | 655 | #endif |
0f113f3e MC |
656 | return -1; |
657 | } | |
658 | ||
659 | /* | |
660 | * XXX This approach assumes that the external representation of curves | |
661 | * over the same field type is the same. | |
662 | */ | |
663 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
664 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
665 | r = 1; | |
666 | ||
8402cd5f SL |
667 | /* return 1 if the curve parameters are different */ |
668 | if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0) | |
0f113f3e MC |
669 | r = 1; |
670 | ||
ac2b52c6 | 671 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ |
8402cd5f | 672 | /* return 1 if the generators are different */ |
0f113f3e | 673 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), |
8402cd5f | 674 | EC_GROUP_get0_generator(b), ctx) != 0) |
0f113f3e MC |
675 | r = 1; |
676 | ||
677 | if (!r) { | |
be2e334f | 678 | const BIGNUM *ao, *bo, *ac, *bc; |
ac2b52c6 | 679 | /* compare the orders */ |
be2e334f DSH |
680 | ao = EC_GROUP_get0_order(a); |
681 | bo = EC_GROUP_get0_order(b); | |
be2e334f | 682 | if (ao == NULL || bo == NULL) { |
8402cd5f SL |
683 | /* return an error if either order is NULL */ |
684 | r = -1; | |
685 | goto end; | |
686 | } | |
687 | if (BN_cmp(ao, bo) != 0) { | |
688 | /* return 1 if orders are different */ | |
689 | r = 1; | |
690 | goto end; | |
0f113f3e | 691 | } |
8402cd5f SL |
692 | /* |
693 | * It gets here if the curve parameters and generator matched. | |
694 | * Now check the optional cofactors (if both are present). | |
695 | */ | |
696 | ac = EC_GROUP_get0_cofactor(a); | |
697 | bc = EC_GROUP_get0_cofactor(b); | |
698 | /* Returns 1 (mismatch) if both cofactors are specified and different */ | |
699 | if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0) | |
0f113f3e | 700 | r = 1; |
8402cd5f | 701 | /* Returns 0 if the parameters matched */ |
0f113f3e | 702 | } |
8402cd5f | 703 | end: |
0f113f3e | 704 | BN_CTX_end(ctx); |
f844f9eb | 705 | #ifndef FIPS_MODULE |
23a1d5e9 | 706 | BN_CTX_free(ctx_new); |
a9612d6c | 707 | #endif |
0f113f3e MC |
708 | return r; |
709 | } | |
ada0e717 | 710 | |
0657bf9c BM |
711 | /* functions for EC_POINT objects */ |
712 | ||
713 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
714 | { |
715 | EC_POINT *ret; | |
716 | ||
717 | if (group == NULL) { | |
718 | ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); | |
719 | return NULL; | |
720 | } | |
f06080cb | 721 | if (group->meth->point_init == NULL) { |
0f113f3e MC |
722 | ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
723 | return NULL; | |
724 | } | |
725 | ||
1b4cf96f | 726 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
727 | if (ret == NULL) { |
728 | ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); | |
729 | return NULL; | |
730 | } | |
731 | ||
732 | ret->meth = group->meth; | |
b14e6015 | 733 | ret->curve_name = group->curve_name; |
0f113f3e MC |
734 | |
735 | if (!ret->meth->point_init(ret)) { | |
736 | OPENSSL_free(ret); | |
737 | return NULL; | |
738 | } | |
739 | ||
740 | return ret; | |
741 | } | |
0657bf9c BM |
742 | |
743 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e | 744 | { |
12a765a5 | 745 | if (point == NULL) |
0f113f3e | 746 | return; |
7711de24 | 747 | |
0f113f3e MC |
748 | if (point->meth->point_finish != 0) |
749 | point->meth->point_finish(point); | |
750 | OPENSSL_free(point); | |
751 | } | |
0657bf9c BM |
752 | |
753 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e | 754 | { |
12a765a5 | 755 | if (point == NULL) |
0f113f3e MC |
756 | return; |
757 | ||
758 | if (point->meth->point_clear_finish != 0) | |
759 | point->meth->point_clear_finish(point); | |
760 | else if (point->meth->point_finish != 0) | |
761 | point->meth->point_finish(point); | |
b4faea50 | 762 | OPENSSL_clear_free(point, sizeof(*point)); |
0f113f3e | 763 | } |
0657bf9c BM |
764 | |
765 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
766 | { |
767 | if (dest->meth->point_copy == 0) { | |
768 | ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
769 | return 0; | |
770 | } | |
b14e6015 MC |
771 | if (dest->meth != src->meth |
772 | || (dest->curve_name != src->curve_name | |
8402cd5f SL |
773 | && dest->curve_name != 0 |
774 | && src->curve_name != 0)) { | |
0f113f3e MC |
775 | ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); |
776 | return 0; | |
777 | } | |
778 | if (dest == src) | |
779 | return 1; | |
780 | return dest->meth->point_copy(dest, src); | |
781 | } | |
0657bf9c | 782 | |
7793f30e | 783 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
784 | { |
785 | EC_POINT *t; | |
786 | int r; | |
787 | ||
788 | if (a == NULL) | |
789 | return NULL; | |
790 | ||
791 | t = EC_POINT_new(group); | |
792 | if (t == NULL) | |
26a7d938 | 793 | return NULL; |
0f113f3e MC |
794 | r = EC_POINT_copy(t, a); |
795 | if (!r) { | |
796 | EC_POINT_free(t); | |
797 | return NULL; | |
8fdc3734 RS |
798 | } |
799 | return t; | |
0f113f3e | 800 | } |
7793f30e | 801 | |
23ccae80 | 802 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
48fe4d62 | 803 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
804 | { |
805 | return point->meth; | |
806 | } | |
23ccae80 | 807 | #endif |
48fe4d62 | 808 | |
226cc7de | 809 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
810 | { |
811 | if (group->meth->point_set_to_infinity == 0) { | |
812 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, | |
813 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
814 | return 0; | |
815 | } | |
816 | if (group->meth != point->meth) { | |
817 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); | |
818 | return 0; | |
819 | } | |
820 | return group->meth->point_set_to_infinity(group, point); | |
821 | } | |
822 | ||
07caec83 | 823 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
824 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, |
825 | EC_POINT *point, const BIGNUM *x, | |
826 | const BIGNUM *y, const BIGNUM *z, | |
827 | BN_CTX *ctx) | |
828 | { | |
07caec83 | 829 | if (group->meth->field_type != NID_X9_62_prime_field) { |
0f113f3e MC |
830 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
831 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
832 | return 0; | |
833 | } | |
b14e6015 | 834 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
835 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
836 | EC_R_INCOMPATIBLE_OBJECTS); | |
837 | return 0; | |
838 | } | |
07caec83 | 839 | return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e MC |
840 | } |
841 | ||
842 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
843 | const EC_POINT *point, BIGNUM *x, | |
844 | BIGNUM *y, BIGNUM *z, | |
845 | BN_CTX *ctx) | |
846 | { | |
07caec83 | 847 | if (group->meth->field_type != NID_X9_62_prime_field) { |
0f113f3e MC |
848 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
849 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
850 | return 0; | |
851 | } | |
b14e6015 | 852 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
853 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
854 | EC_R_INCOMPATIBLE_OBJECTS); | |
855 | return 0; | |
856 | } | |
07caec83 | 857 | return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e | 858 | } |
07caec83 | 859 | #endif |
0f113f3e | 860 | |
8e3cced7 MC |
861 | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, |
862 | const BIGNUM *x, const BIGNUM *y, | |
863 | BN_CTX *ctx) | |
0f113f3e | 864 | { |
8e3cced7 MC |
865 | if (group->meth->point_set_affine_coordinates == NULL) { |
866 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, | |
0f113f3e MC |
867 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
868 | return 0; | |
869 | } | |
b14e6015 | 870 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 871 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
872 | return 0; |
873 | } | |
1e2012b7 EK |
874 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
875 | return 0; | |
876 | ||
877 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
8e3cced7 | 878 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE); |
1e2012b7 EK |
879 | return 0; |
880 | } | |
881 | return 1; | |
0f113f3e | 882 | } |
226cc7de | 883 | |
936c2b9e | 884 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
8e3cced7 MC |
885 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, |
886 | EC_POINT *point, const BIGNUM *x, | |
887 | const BIGNUM *y, BN_CTX *ctx) | |
888 | { | |
889 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); | |
890 | } | |
891 | ||
50db8163 | 892 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
893 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
894 | EC_POINT *point, const BIGNUM *x, | |
895 | const BIGNUM *y, BN_CTX *ctx) | |
896 | { | |
8e3cced7 MC |
897 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); |
898 | } | |
50db8163 | 899 | # endif |
8e3cced7 MC |
900 | #endif |
901 | ||
902 | int EC_POINT_get_affine_coordinates(const EC_GROUP *group, | |
903 | const EC_POINT *point, BIGNUM *x, BIGNUM *y, | |
904 | BN_CTX *ctx) | |
905 | { | |
906 | if (group->meth->point_get_affine_coordinates == NULL) { | |
907 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, | |
0f113f3e MC |
908 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
909 | return 0; | |
910 | } | |
b14e6015 | 911 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 912 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
1e2012b7 EK |
913 | return 0; |
914 | } | |
bfb10b97 BB |
915 | if (EC_POINT_is_at_infinity(group, point)) { |
916 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY); | |
917 | return 0; | |
918 | } | |
8e3cced7 | 919 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 920 | } |
7793f30e | 921 | |
936c2b9e | 922 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
923 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
924 | const EC_POINT *point, BIGNUM *x, | |
925 | BIGNUM *y, BN_CTX *ctx) | |
926 | { | |
8e3cced7 | 927 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 928 | } |
7793f30e | 929 | |
50db8163 | 930 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
931 | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, |
932 | const EC_POINT *point, BIGNUM *x, | |
933 | BIGNUM *y, BN_CTX *ctx) | |
934 | { | |
8e3cced7 | 935 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 936 | } |
50db8163 | 937 | # endif |
b3310161 | 938 | #endif |
7793f30e | 939 | |
0f113f3e MC |
940 | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
941 | const EC_POINT *b, BN_CTX *ctx) | |
942 | { | |
943 | if (group->meth->add == 0) { | |
944 | ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
945 | return 0; | |
946 | } | |
b14e6015 MC |
947 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group) |
948 | || !ec_point_is_compat(b, group)) { | |
0f113f3e MC |
949 | ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS); |
950 | return 0; | |
951 | } | |
952 | return group->meth->add(group, r, a, b, ctx); | |
953 | } | |
954 | ||
955 | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | |
956 | BN_CTX *ctx) | |
957 | { | |
958 | if (group->meth->dbl == 0) { | |
959 | ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
960 | return 0; | |
961 | } | |
b14e6015 | 962 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { |
0f113f3e MC |
963 | ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS); |
964 | return 0; | |
965 | } | |
966 | return group->meth->dbl(group, r, a, ctx); | |
967 | } | |
0657bf9c | 968 | |
1d5bd6cf | 969 | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) |
0f113f3e MC |
970 | { |
971 | if (group->meth->invert == 0) { | |
972 | ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
973 | return 0; | |
974 | } | |
b14e6015 | 975 | if (!ec_point_is_compat(a, group)) { |
0f113f3e MC |
976 | ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS); |
977 | return 0; | |
978 | } | |
979 | return group->meth->invert(group, a, ctx); | |
980 | } | |
1d5bd6cf | 981 | |
0657bf9c | 982 | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
0f113f3e MC |
983 | { |
984 | if (group->meth->is_at_infinity == 0) { | |
985 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, | |
986 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
987 | return 0; | |
988 | } | |
b14e6015 | 989 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
990 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); |
991 | return 0; | |
992 | } | |
993 | return group->meth->is_at_infinity(group, point); | |
994 | } | |
995 | ||
68886be7 MC |
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 | */ | |
0f113f3e MC |
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 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1008 | return 0; | |
1009 | } | |
b14e6015 | 1010 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
1011 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, 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 | ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1022 | return -1; | |
1023 | } | |
b14e6015 | 1024 | if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { |
0f113f3e MC |
1025 | ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); |
1026 | return -1; | |
1027 | } | |
1028 | return group->meth->point_cmp(group, a, b, ctx); | |
1029 | } | |
1d5bd6cf | 1030 | |
c2f2db9b | 1031 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
e869d4bd | 1032 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
1033 | { |
1034 | if (group->meth->make_affine == 0) { | |
1035 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1036 | return 0; | |
1037 | } | |
b14e6015 | 1038 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
1039 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, 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 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1052 | return 0; | |
1053 | } | |
1054 | for (i = 0; i < num; i++) { | |
b14e6015 | 1055 | if (!ec_point_is_compat(points[i], group)) { |
0f113f3e MC |
1056 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
1057 | return 0; | |
1058 | } | |
1059 | } | |
1060 | return group->meth->points_make_affine(group, num, points, ctx); | |
1061 | } | |
c2f2db9b | 1062 | #endif |
0f113f3e MC |
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. | |
37c660ff BM |
1068 | */ |
1069 | ||
4fcd15c1 | 1070 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1071 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, |
0f113f3e MC |
1072 | size_t num, const EC_POINT *points[], |
1073 | const BIGNUM *scalars[], BN_CTX *ctx) | |
1074 | { | |
01ad66f8 NT |
1075 | int ret = 0; |
1076 | size_t i = 0; | |
f844f9eb | 1077 | #ifndef FIPS_MODULE |
01ad66f8 | 1078 | BN_CTX *new_ctx = NULL; |
a9612d6c | 1079 | #endif |
a9612d6c | 1080 | |
01ad66f8 NT |
1081 | if (!ec_point_is_compat(r, group)) { |
1082 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1083 | return 0; | |
1084 | } | |
1eb9b54a BE |
1085 | |
1086 | if (scalar == NULL && num == 0) | |
1087 | return EC_POINT_set_to_infinity(group, r); | |
1088 | ||
01ad66f8 NT |
1089 | for (i = 0; i < num; i++) { |
1090 | if (!ec_point_is_compat(points[i], group)) { | |
1091 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1092 | return 0; | |
1093 | } | |
1094 | } | |
1095 | ||
f844f9eb | 1096 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
1097 | if (ctx == NULL) |
1098 | ctx = new_ctx = BN_CTX_secure_new(); | |
1099 | #endif | |
1100 | if (ctx == NULL) { | |
1101 | ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR); | |
1102 | return 0; | |
1103 | } | |
1104 | ||
01ad66f8 NT |
1105 | if (group->meth->mul != NULL) |
1106 | ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx); | |
1107 | else | |
0f113f3e | 1108 | /* use default */ |
01ad66f8 | 1109 | ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); |
37c660ff | 1110 | |
f844f9eb | 1111 | #ifndef FIPS_MODULE |
01ad66f8 | 1112 | BN_CTX_free(new_ctx); |
a9612d6c | 1113 | #endif |
01ad66f8 | 1114 | return ret; |
0f113f3e | 1115 | } |
4fcd15c1 | 1116 | #endif |
37c660ff BM |
1117 | |
1118 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
1119 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
1120 | { | |
4fcd15c1 | 1121 | int ret = 0; |
9c47a338 | 1122 | size_t num; |
4fcd15c1 BB |
1123 | #ifndef FIPS_MODULE |
1124 | BN_CTX *new_ctx = NULL; | |
1125 | #endif | |
37c660ff | 1126 | |
4fcd15c1 BB |
1127 | if (!ec_point_is_compat(r, group) |
1128 | || (point != NULL && !ec_point_is_compat(point, group))) { | |
1129 | ECerr(EC_F_EC_POINT_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1130 | return 0; | |
1131 | } | |
37c660ff | 1132 | |
4fcd15c1 BB |
1133 | if (g_scalar == NULL && p_scalar == NULL) |
1134 | return EC_POINT_set_to_infinity(group, r); | |
37c660ff | 1135 | |
4fcd15c1 BB |
1136 | #ifndef FIPS_MODULE |
1137 | if (ctx == NULL) | |
1138 | ctx = new_ctx = BN_CTX_secure_new(); | |
1139 | #endif | |
1140 | if (ctx == NULL) { | |
1141 | ECerr(EC_F_EC_POINT_MUL, ERR_R_INTERNAL_ERROR); | |
1142 | return 0; | |
1143 | } | |
1144 | ||
9c47a338 | 1145 | num = (point != NULL && p_scalar != NULL) ? 1 : 0; |
4fcd15c1 | 1146 | if (group->meth->mul != NULL) |
9c47a338 | 1147 | ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1148 | else |
1149 | /* use default */ | |
9c47a338 | 1150 | ret = ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1151 | |
1152 | #ifndef FIPS_MODULE | |
1153 | BN_CTX_free(new_ctx); | |
1154 | #endif | |
1155 | return ret; | |
0f113f3e | 1156 | } |
37c660ff | 1157 | |
6b4eb933 | 1158 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1159 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
1160 | { |
1161 | if (group->meth->mul == 0) | |
1162 | /* use default */ | |
1163 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 1164 | |
0f113f3e MC |
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 | } | |
37c660ff BM |
1170 | |
1171 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
1172 | { |
1173 | if (group->meth->mul == 0) | |
1174 | /* use default */ | |
1175 | return 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 | } | |
6b4eb933 | 1183 | #endif |
0f113f3e MC |
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 | */ | |
eb791696 | 1189 | static int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e | 1190 | { |
a9612d6c | 1191 | BN_CTX *ctx = BN_CTX_new_ex(group->libctx); |
0f113f3e MC |
1192 | int ret = 0; |
1193 | ||
23a1d5e9 RS |
1194 | BN_MONT_CTX_free(group->mont_data); |
1195 | group->mont_data = NULL; | |
0f113f3e MC |
1196 | |
1197 | if (ctx == NULL) | |
1198 | goto err; | |
1199 | ||
1200 | group->mont_data = BN_MONT_CTX_new(); | |
90945fa3 | 1201 | if (group->mont_data == NULL) |
0f113f3e MC |
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 | ||
23a1d5e9 | 1214 | BN_CTX_free(ctx); |
0f113f3e MC |
1215 | return ret; |
1216 | } | |
3aef36ff | 1217 | |
f844f9eb | 1218 | #ifndef FIPS_MODULE |
3aef36ff RS |
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 | } | |
a9612d6c | 1228 | #endif |
77470e98 DSH |
1229 | |
1230 | int 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 | } | |
eb791696 | 1236 | |
c11d372b | 1237 | static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r, |
792546eb | 1238 | const BIGNUM *x, BN_CTX *ctx) |
c11d372b | 1239 | { |
262dccc0 | 1240 | BIGNUM *e = NULL; |
c11d372b | 1241 | int ret = 0; |
f844f9eb | 1242 | #ifndef FIPS_MODULE |
a9612d6c | 1243 | BN_CTX *new_ctx = NULL; |
0e8b6c97 AT |
1244 | #endif |
1245 | ||
1246 | if (group->mont_data == NULL) | |
1247 | return 0; | |
c11d372b | 1248 | |
f844f9eb | 1249 | #ifndef FIPS_MODULE |
a9612d6c MC |
1250 | if (ctx == NULL) |
1251 | ctx = new_ctx = BN_CTX_secure_new(); | |
1252 | #endif | |
1253 | if (ctx == NULL) | |
792546eb BB |
1254 | return 0; |
1255 | ||
c11d372b | 1256 | BN_CTX_start(ctx); |
262dccc0 | 1257 | if ((e = BN_CTX_get(ctx)) == NULL) |
c11d372b BB |
1258 | goto err; |
1259 | ||
792546eb BB |
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 | * Exponent e is public. | |
1270 | * No need for scatter-gather or BN_FLG_CONSTTIME. | |
1271 | */ | |
1272 | if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data)) | |
1273 | goto err; | |
c11d372b | 1274 | |
792546eb | 1275 | ret = 1; |
c11d372b BB |
1276 | |
1277 | err: | |
ce1415ed | 1278 | BN_CTX_end(ctx); |
f844f9eb | 1279 | #ifndef FIPS_MODULE |
c11d372b | 1280 | BN_CTX_free(new_ctx); |
a9612d6c | 1281 | #endif |
c11d372b BB |
1282 | return ret; |
1283 | } | |
1284 | ||
792546eb BB |
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 ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res, | |
1299 | const BIGNUM *x, BN_CTX *ctx) | |
eb791696 AP |
1300 | { |
1301 | if (group->meth->field_inverse_mod_ord != NULL) | |
1302 | return group->meth->field_inverse_mod_ord(group, res, x, ctx); | |
1303 | else | |
c11d372b | 1304 | return ec_field_inverse_mod_ord(group, res, x, ctx); |
eb791696 | 1305 | } |
f667820c SH |
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 ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx) | |
1318 | { | |
1319 | if (group->meth->blind_coordinates == NULL) | |
1320 | return 1; /* ignore if not implemented */ | |
1321 | ||
1322 | return group->meth->blind_coordinates(group, p, ctx); | |
1323 | } | |
c0f39ded SL |
1324 | |
1325 | int EC_GROUP_get_basis_type(const EC_GROUP *group) | |
1326 | { | |
1327 | int i; | |
1328 | ||
1329 | if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field) | |
1330 | /* everything else is currently not supported */ | |
1331 | return 0; | |
1332 | ||
1333 | /* Find the last non-zero element of group->poly[] */ | |
1334 | for (i = 0; | |
1335 | i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0; | |
1336 | i++) | |
1337 | continue; | |
1338 | ||
1339 | if (i == 4) | |
1340 | return NID_X9_62_ppBasis; | |
1341 | else if (i == 2) | |
1342 | return NID_X9_62_tpBasis; | |
1343 | else | |
1344 | /* everything else is currently not supported */ | |
1345 | return 0; | |
1346 | } | |
1347 | ||
1348 | #ifndef OPENSSL_NO_EC2M | |
1349 | int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k) | |
1350 | { | |
1351 | if (group == NULL) | |
1352 | return 0; | |
1353 | ||
1354 | if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field | |
1355 | || !((group->poly[0] != 0) && (group->poly[1] != 0) | |
1356 | && (group->poly[2] == 0))) { | |
1357 | ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, | |
1358 | 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 | ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, | |
1379 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1380 | return 0; | |
1381 | } | |
1382 | ||
1383 | if (k1) | |
1384 | *k1 = group->poly[3]; | |
1385 | if (k2) | |
1386 | *k2 = group->poly[2]; | |
1387 | if (k3) | |
1388 | *k3 = group->poly[1]; | |
1389 | ||
1390 | return 1; | |
1391 | } | |
1392 | #endif | |
1393 | ||
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 | OPENSSL_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 = 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 | ||
d8652be0 | 1441 | ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid); |
c0f39ded SL |
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 | ||
1475 | static int ec_encoding_param2id(const OSSL_PARAM *p, int *id) | |
1476 | { | |
1477 | const char *name = NULL; | |
1478 | int status = 0; | |
1479 | ||
1480 | switch (p->data_type) { | |
1481 | case OSSL_PARAM_UTF8_STRING: | |
1482 | /* The OSSL_PARAM functions have no support for this */ | |
1483 | name = p->data; | |
1484 | status = (name != NULL); | |
1485 | break; | |
1486 | case OSSL_PARAM_UTF8_PTR: | |
1487 | status = OSSL_PARAM_get_utf8_ptr(p, &name); | |
1488 | break; | |
1489 | } | |
1490 | if (status) { | |
1491 | int i = ec_encoding_name2id(name); | |
1492 | ||
1493 | if (i >= 0) { | |
1494 | *id = i; | |
1495 | return 1; | |
1496 | } | |
1497 | } | |
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | static EC_GROUP *group_new_from_name(const OSSL_PARAM *p, | |
1502 | OPENSSL_CTX *libctx, const char *propq) | |
1503 | { | |
1504 | int ok = 0, nid; | |
1505 | const char *curve_name = NULL; | |
1506 | ||
1507 | switch (p->data_type) { | |
1508 | case OSSL_PARAM_UTF8_STRING: | |
1509 | /* The OSSL_PARAM functions have no support for this */ | |
1510 | curve_name = p->data; | |
1511 | ok = (curve_name != NULL); | |
1512 | break; | |
1513 | case OSSL_PARAM_UTF8_PTR: | |
1514 | ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name); | |
1515 | break; | |
1516 | } | |
1517 | ||
1518 | if (ok) { | |
1519 | nid = ec_curve_name2nid(curve_name); | |
1520 | if (nid == NID_undef) { | |
1521 | ECerr(0, EC_R_INVALID_CURVE); | |
1522 | return NULL; | |
1523 | } else { | |
d8652be0 | 1524 | return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid); |
c0f39ded SL |
1525 | } |
1526 | } | |
1527 | return NULL; | |
1528 | } | |
1529 | ||
1530 | EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[], | |
1531 | OPENSSL_CTX *libctx, const char *propq) | |
1532 | { | |
1533 | const OSSL_PARAM *ptmp, *pa, *pb; | |
1534 | int ok = 0; | |
1535 | EC_GROUP *group = NULL, *named_group = NULL; | |
1536 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL; | |
1537 | EC_POINT *point = NULL; | |
1538 | int field_bits = 0; | |
1539 | int is_prime_field = 1; | |
1540 | BN_CTX *bnctx = NULL; | |
1541 | const unsigned char *buf = NULL; | |
1542 | int encoding_flag = -1; | |
1543 | ||
1544 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING); | |
1545 | if (ptmp != NULL && !ec_encoding_param2id(ptmp, &encoding_flag)) { | |
1546 | ECerr(0, EC_R_INVALID_ENCODING); | |
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); | |
1551 | if (ptmp != NULL) { | |
1552 | group = group_new_from_name(ptmp, libctx, propq); | |
1553 | if (group != NULL) | |
1554 | EC_GROUP_set_asn1_flag(group, encoding_flag); | |
c0f39ded SL |
1555 | return group; |
1556 | } | |
1557 | bnctx = BN_CTX_new_ex(libctx); | |
1558 | if (bnctx == NULL) { | |
1559 | ECerr(0, ERR_R_MALLOC_FAILURE); | |
1560 | return 0; | |
1561 | } | |
1562 | BN_CTX_start(bnctx); | |
1563 | ||
1564 | p = BN_CTX_get(bnctx); | |
1565 | a = BN_CTX_get(bnctx); | |
1566 | b = BN_CTX_get(bnctx); | |
1567 | order = BN_CTX_get(bnctx); | |
1568 | if (order == NULL) { | |
1569 | ECerr(0, ERR_R_MALLOC_FAILURE); | |
1570 | goto err; | |
1571 | } | |
1572 | ||
1573 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE); | |
1574 | if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) { | |
1575 | ECerr(0, EC_R_INVALID_FIELD); | |
1576 | goto err; | |
1577 | } | |
1578 | if (strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) { | |
1579 | is_prime_field = 1; | |
1580 | } else if (strcasecmp(ptmp->data, SN_X9_62_characteristic_two_field) == 0) { | |
1581 | is_prime_field = 0; | |
1582 | } else { | |
1583 | /* Invalid field */ | |
1584 | ECerr(0, EC_R_UNSUPPORTED_FIELD); | |
1585 | goto err; | |
1586 | } | |
1587 | ||
1588 | pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A); | |
1589 | if (!OSSL_PARAM_get_BN(pa, &a)) { | |
1590 | ECerr(0, EC_R_INVALID_A); | |
1591 | goto err; | |
1592 | } | |
1593 | pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B); | |
1594 | if (!OSSL_PARAM_get_BN(pb, &b)) { | |
1595 | ECerr(0, EC_R_INVALID_B); | |
1596 | goto err; | |
1597 | } | |
1598 | ||
1599 | /* extract the prime number or irreducible polynomial */ | |
1600 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P); | |
1601 | if (!OSSL_PARAM_get_BN(ptmp, &p)) { | |
1602 | ECerr(0, EC_R_INVALID_P); | |
1603 | goto err; | |
1604 | } | |
1605 | ||
1606 | if (is_prime_field) { | |
1607 | if (BN_is_negative(p) || BN_is_zero(p)) { | |
1608 | ECerr(0, EC_R_INVALID_P); | |
1609 | goto err; | |
1610 | } | |
1611 | field_bits = BN_num_bits(p); | |
1612 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
1613 | ECerr(0, EC_R_FIELD_TOO_LARGE); | |
1614 | goto err; | |
1615 | } | |
1616 | ||
1617 | /* create the EC_GROUP structure */ | |
1618 | group = EC_GROUP_new_curve_GFp(p, a, b, bnctx); | |
1619 | } else { | |
1620 | #ifdef OPENSSL_NO_EC2M | |
1621 | ECerr(0, EC_R_GF2M_NOT_SUPPORTED); | |
1622 | goto err; | |
1623 | #else | |
1624 | /* create the EC_GROUP structure */ | |
1625 | group = EC_GROUP_new_curve_GF2m(p, a, b, NULL); | |
1626 | if (group != NULL) { | |
1627 | field_bits = EC_GROUP_get_degree(group); | |
1628 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
1629 | ECerr(0, EC_R_FIELD_TOO_LARGE); | |
1630 | goto err; | |
1631 | } | |
1632 | } | |
1633 | #endif /* OPENSSL_NO_EC2M */ | |
1634 | } | |
1635 | ||
1636 | if (group == NULL) { | |
1637 | ECerr(0, ERR_R_EC_LIB); | |
1638 | goto err; | |
1639 | } | |
1640 | ||
1641 | /* Optional seed */ | |
1642 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED); | |
1643 | if (ptmp != NULL) { | |
1644 | if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | |
1645 | ECerr(0, EC_R_INVALID_SEED); | |
1646 | goto err; | |
1647 | } | |
1648 | if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size)) | |
1649 | goto err; | |
1650 | } | |
1651 | ||
1652 | /* generator base point */ | |
1653 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR); | |
1654 | if (ptmp == NULL | |
1655 | || ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | |
1656 | ECerr(0, EC_R_INVALID_GENERATOR); | |
1657 | goto err; | |
1658 | } | |
1659 | buf = (const unsigned char *)(ptmp->data); | |
1660 | if ((point = EC_POINT_new(group)) == NULL) | |
1661 | goto err; | |
1662 | EC_GROUP_set_point_conversion_form(group, | |
1663 | (point_conversion_form_t)buf[0] & ~0x01); | |
1664 | if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) { | |
1665 | ECerr(0, EC_R_INVALID_GENERATOR); | |
1666 | goto err; | |
1667 | } | |
1668 | ||
1669 | /* order */ | |
1670 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER); | |
1671 | if (!OSSL_PARAM_get_BN(ptmp, &order) | |
1672 | || (BN_is_negative(order) || BN_is_zero(order)) | |
1673 | || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */ | |
1674 | ECerr(0, EC_R_INVALID_GROUP_ORDER); | |
1675 | goto err; | |
1676 | } | |
1677 | ||
1678 | /* Optional cofactor */ | |
1679 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR); | |
1680 | if (ptmp != NULL) { | |
1681 | cofactor = BN_CTX_get(bnctx); | |
1682 | if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) { | |
1683 | ECerr(0, EC_R_INVALID_COFACTOR); | |
1684 | goto err; | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | /* set the generator, order and cofactor (if present) */ | |
1689 | if (!EC_GROUP_set_generator(group, point, order, cofactor)) { | |
1690 | ECerr(0, EC_R_INVALID_GENERATOR); | |
1691 | goto err; | |
1692 | } | |
1693 | ||
1694 | named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx); | |
1695 | if (named_group == NULL) { | |
1696 | ECerr(0, EC_R_INVALID_NAMED_GROUP_CONVERSION); | |
1697 | goto err; | |
1698 | } | |
1699 | if (named_group == group) { | |
1700 | /* | |
1701 | * If we did not find a named group then the encoding should be explicit | |
1702 | * if it was specified | |
1703 | */ | |
1704 | if (encoding_flag == OPENSSL_EC_NAMED_CURVE) { | |
1705 | ECerr(0, EC_R_INVALID_ENCODING); | |
1706 | goto err; | |
1707 | } | |
1708 | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE); | |
1709 | } else { | |
1710 | EC_GROUP_free(group); | |
1711 | group = named_group; | |
1712 | } | |
1713 | ok = 1; | |
1714 | err: | |
1715 | if (!ok) { | |
1716 | EC_GROUP_free(group); | |
1717 | group = NULL; | |
1718 | } | |
1719 | EC_POINT_free(point); | |
1720 | BN_CTX_end(bnctx); | |
1721 | BN_CTX_free(bnctx); | |
1722 | ||
1723 | return group; | |
1724 | } |