]>
Commit | Line | Data |
---|---|---|
35b73a1f | 1 | /* |
4333b89f | 2 | * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
65e81670 | 4 | * |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
65e81670 | 9 | */ |
aa6bb135 | 10 | |
579422c8 | 11 | /* |
5b5eea4b | 12 | * EC_GROUP low level APIs are deprecated for public use, but still ok for |
579422c8 P |
13 | * internal use. |
14 | */ | |
15 | #include "internal/deprecated.h" | |
16 | ||
c4b36ff4 | 17 | #include <string.h> |
c0f39ded SL |
18 | #include <openssl/params.h> |
19 | #include <openssl/core_names.h> | |
0657bf9c | 20 | #include <openssl/err.h> |
bb62a8b0 | 21 | #include <openssl/opensslv.h> |
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 | ||
b4250010 | 29 | EC_GROUP *ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq, |
d8652be0 | 30 | const EC_METHOD *meth) |
0f113f3e MC |
31 | { |
32 | EC_GROUP *ret; | |
33 | ||
34 | if (meth == NULL) { | |
9311d0c4 | 35 | ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL); |
0f113f3e MC |
36 | return NULL; |
37 | } | |
38 | if (meth->group_init == 0) { | |
9311d0c4 | 39 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
40 | return NULL; |
41 | } | |
42 | ||
64b25758 | 43 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 44 | if (ret == NULL) { |
9311d0c4 | 45 | ERR_raise(ERR_LIB_EC, 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) { | |
9311d0c4 | 53 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
2da8d4eb MC |
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 | 163 | if (dest->meth->group_copy == 0) { |
9311d0c4 | 164 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
165 | return 0; |
166 | } | |
167 | if (dest->meth != src->meth) { | |
9311d0c4 | 168 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
169 | return 0; |
170 | } | |
171 | if (dest == src) | |
172 | return 1; | |
173 | ||
a9612d6c | 174 | dest->libctx = src->libctx; |
b14e6015 MC |
175 | dest->curve_name = src->curve_name; |
176 | ||
3aef36ff RS |
177 | /* Copy precomputed */ |
178 | dest->pre_comp_type = src->pre_comp_type; | |
179 | switch (src->pre_comp_type) { | |
f3b3d7f0 | 180 | case PCT_none: |
3aef36ff RS |
181 | dest->pre_comp.ec = NULL; |
182 | break; | |
66117ab0 | 183 | case PCT_nistz256: |
f3b3d7f0 | 184 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 185 | dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256); |
e69aa800 | 186 | #endif |
f3b3d7f0 | 187 | break; |
3aef36ff | 188 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 189 | case PCT_nistp224: |
3aef36ff RS |
190 | dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224); |
191 | break; | |
66117ab0 | 192 | case PCT_nistp256: |
3aef36ff RS |
193 | dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256); |
194 | break; | |
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 | 250 | if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) { |
9311d0c4 | 251 | ERR_raise(ERR_LIB_EC, 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) { | |
9311d0c4 | 368 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
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)) { | |
9311d0c4 | 375 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
b783beea BB |
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) { | |
9311d0c4 | 386 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
b783beea BB |
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)) { | |
9311d0c4 | 396 | ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR); |
b783beea BB |
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 | 532 | if ((group->seed = OPENSSL_malloc(len)) == NULL) { |
9311d0c4 | 533 | ERR_raise(ERR_LIB_EC, 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) { | |
9311d0c4 | 556 | ERR_raise(ERR_LIB_EC, 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 | 565 | if (group->meth->group_get_curve == NULL) { |
9311d0c4 | 566 | ERR_raise(ERR_LIB_EC, 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) { | |
9311d0c4 | 603 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
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) { | |
9311d0c4 | 612 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
613 | return 0; |
614 | } | |
615 | return group->meth->group_check_discriminant(group, ctx); | |
616 | } | |
af28dd6c | 617 | |
ada0e717 | 618 | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) |
0f113f3e MC |
619 | { |
620 | int r = 0; | |
621 | BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; | |
f844f9eb | 622 | #ifndef FIPS_MODULE |
0f113f3e | 623 | BN_CTX *ctx_new = NULL; |
a9612d6c | 624 | #endif |
a9612d6c | 625 | |
0f113f3e | 626 | /* compare the field types */ |
23ccae80 | 627 | if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b)) |
0f113f3e MC |
628 | return 1; |
629 | /* compare the curve name (if present in both) */ | |
630 | if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && | |
631 | EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b)) | |
632 | return 1; | |
6903e2e7 DSH |
633 | if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE) |
634 | return 0; | |
0f113f3e | 635 | |
f844f9eb | 636 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
637 | if (ctx == NULL) |
638 | ctx_new = ctx = BN_CTX_new(); | |
639 | #endif | |
640 | if (ctx == NULL) | |
641 | return -1; | |
642 | ||
0f113f3e MC |
643 | BN_CTX_start(ctx); |
644 | a1 = BN_CTX_get(ctx); | |
645 | a2 = BN_CTX_get(ctx); | |
646 | a3 = BN_CTX_get(ctx); | |
647 | b1 = BN_CTX_get(ctx); | |
648 | b2 = BN_CTX_get(ctx); | |
649 | b3 = BN_CTX_get(ctx); | |
90945fa3 | 650 | if (b3 == NULL) { |
0f113f3e | 651 | BN_CTX_end(ctx); |
f844f9eb | 652 | #ifndef FIPS_MODULE |
23a1d5e9 | 653 | BN_CTX_free(ctx_new); |
a9612d6c | 654 | #endif |
0f113f3e MC |
655 | return -1; |
656 | } | |
657 | ||
658 | /* | |
659 | * XXX This approach assumes that the external representation of curves | |
660 | * over the same field type is the same. | |
661 | */ | |
662 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
663 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
664 | r = 1; | |
665 | ||
8402cd5f SL |
666 | /* return 1 if the curve parameters are different */ |
667 | if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0) | |
0f113f3e MC |
668 | r = 1; |
669 | ||
ac2b52c6 | 670 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ |
8402cd5f | 671 | /* return 1 if the generators are different */ |
0f113f3e | 672 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), |
8402cd5f | 673 | EC_GROUP_get0_generator(b), ctx) != 0) |
0f113f3e MC |
674 | r = 1; |
675 | ||
676 | if (!r) { | |
be2e334f | 677 | const BIGNUM *ao, *bo, *ac, *bc; |
ac2b52c6 | 678 | /* compare the orders */ |
be2e334f DSH |
679 | ao = EC_GROUP_get0_order(a); |
680 | bo = EC_GROUP_get0_order(b); | |
be2e334f | 681 | if (ao == NULL || bo == NULL) { |
8402cd5f SL |
682 | /* return an error if either order is NULL */ |
683 | r = -1; | |
684 | goto end; | |
685 | } | |
686 | if (BN_cmp(ao, bo) != 0) { | |
687 | /* return 1 if orders are different */ | |
688 | r = 1; | |
689 | goto end; | |
0f113f3e | 690 | } |
8402cd5f SL |
691 | /* |
692 | * It gets here if the curve parameters and generator matched. | |
693 | * Now check the optional cofactors (if both are present). | |
694 | */ | |
695 | ac = EC_GROUP_get0_cofactor(a); | |
696 | bc = EC_GROUP_get0_cofactor(b); | |
697 | /* Returns 1 (mismatch) if both cofactors are specified and different */ | |
698 | if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0) | |
0f113f3e | 699 | r = 1; |
8402cd5f | 700 | /* Returns 0 if the parameters matched */ |
0f113f3e | 701 | } |
8402cd5f | 702 | end: |
0f113f3e | 703 | BN_CTX_end(ctx); |
f844f9eb | 704 | #ifndef FIPS_MODULE |
23a1d5e9 | 705 | BN_CTX_free(ctx_new); |
a9612d6c | 706 | #endif |
0f113f3e MC |
707 | return r; |
708 | } | |
ada0e717 | 709 | |
0657bf9c BM |
710 | /* functions for EC_POINT objects */ |
711 | ||
712 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
713 | { |
714 | EC_POINT *ret; | |
715 | ||
716 | if (group == NULL) { | |
9311d0c4 | 717 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
718 | return NULL; |
719 | } | |
f06080cb | 720 | if (group->meth->point_init == NULL) { |
9311d0c4 | 721 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
722 | return NULL; |
723 | } | |
724 | ||
1b4cf96f | 725 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 726 | if (ret == NULL) { |
9311d0c4 | 727 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
728 | return NULL; |
729 | } | |
730 | ||
731 | ret->meth = group->meth; | |
b14e6015 | 732 | ret->curve_name = group->curve_name; |
0f113f3e MC |
733 | |
734 | if (!ret->meth->point_init(ret)) { | |
735 | OPENSSL_free(ret); | |
736 | return NULL; | |
737 | } | |
738 | ||
739 | return ret; | |
740 | } | |
0657bf9c BM |
741 | |
742 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e | 743 | { |
12a765a5 | 744 | if (point == NULL) |
0f113f3e | 745 | return; |
7711de24 | 746 | |
0f113f3e MC |
747 | if (point->meth->point_finish != 0) |
748 | point->meth->point_finish(point); | |
749 | OPENSSL_free(point); | |
750 | } | |
0657bf9c BM |
751 | |
752 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e | 753 | { |
12a765a5 | 754 | if (point == NULL) |
0f113f3e MC |
755 | return; |
756 | ||
757 | if (point->meth->point_clear_finish != 0) | |
758 | point->meth->point_clear_finish(point); | |
759 | else if (point->meth->point_finish != 0) | |
760 | point->meth->point_finish(point); | |
b4faea50 | 761 | OPENSSL_clear_free(point, sizeof(*point)); |
0f113f3e | 762 | } |
0657bf9c BM |
763 | |
764 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
765 | { |
766 | if (dest->meth->point_copy == 0) { | |
9311d0c4 | 767 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
768 | return 0; |
769 | } | |
b14e6015 MC |
770 | if (dest->meth != src->meth |
771 | || (dest->curve_name != src->curve_name | |
8402cd5f SL |
772 | && dest->curve_name != 0 |
773 | && src->curve_name != 0)) { | |
9311d0c4 | 774 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
775 | return 0; |
776 | } | |
777 | if (dest == src) | |
778 | return 1; | |
779 | return dest->meth->point_copy(dest, src); | |
780 | } | |
0657bf9c | 781 | |
7793f30e | 782 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
783 | { |
784 | EC_POINT *t; | |
785 | int r; | |
786 | ||
787 | if (a == NULL) | |
788 | return NULL; | |
789 | ||
790 | t = EC_POINT_new(group); | |
791 | if (t == NULL) | |
26a7d938 | 792 | return NULL; |
0f113f3e MC |
793 | r = EC_POINT_copy(t, a); |
794 | if (!r) { | |
795 | EC_POINT_free(t); | |
796 | return NULL; | |
8fdc3734 RS |
797 | } |
798 | return t; | |
0f113f3e | 799 | } |
7793f30e | 800 | |
23ccae80 | 801 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
48fe4d62 | 802 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
803 | { |
804 | return point->meth; | |
805 | } | |
23ccae80 | 806 | #endif |
48fe4d62 | 807 | |
226cc7de | 808 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
809 | { |
810 | if (group->meth->point_set_to_infinity == 0) { | |
9311d0c4 | 811 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
812 | return 0; |
813 | } | |
814 | if (group->meth != point->meth) { | |
9311d0c4 | 815 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
816 | return 0; |
817 | } | |
818 | return group->meth->point_set_to_infinity(group, point); | |
819 | } | |
820 | ||
07caec83 | 821 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
822 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, |
823 | EC_POINT *point, const BIGNUM *x, | |
824 | const BIGNUM *y, const BIGNUM *z, | |
825 | BN_CTX *ctx) | |
826 | { | |
07caec83 | 827 | if (group->meth->field_type != NID_X9_62_prime_field) { |
9311d0c4 | 828 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
829 | return 0; |
830 | } | |
b14e6015 | 831 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 832 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
833 | return 0; |
834 | } | |
07caec83 | 835 | return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e MC |
836 | } |
837 | ||
838 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
839 | const EC_POINT *point, BIGNUM *x, | |
840 | BIGNUM *y, BIGNUM *z, | |
841 | BN_CTX *ctx) | |
842 | { | |
07caec83 | 843 | if (group->meth->field_type != NID_X9_62_prime_field) { |
9311d0c4 | 844 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
845 | return 0; |
846 | } | |
b14e6015 | 847 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 848 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
849 | return 0; |
850 | } | |
07caec83 | 851 | return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e | 852 | } |
07caec83 | 853 | #endif |
0f113f3e | 854 | |
8e3cced7 MC |
855 | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, |
856 | const BIGNUM *x, const BIGNUM *y, | |
857 | BN_CTX *ctx) | |
0f113f3e | 858 | { |
8e3cced7 | 859 | if (group->meth->point_set_affine_coordinates == NULL) { |
9311d0c4 | 860 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
861 | return 0; |
862 | } | |
b14e6015 | 863 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 864 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
865 | return 0; |
866 | } | |
1e2012b7 EK |
867 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
868 | return 0; | |
869 | ||
870 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
9311d0c4 | 871 | ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE); |
1e2012b7 EK |
872 | return 0; |
873 | } | |
874 | return 1; | |
0f113f3e | 875 | } |
226cc7de | 876 | |
936c2b9e | 877 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
8e3cced7 MC |
878 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, |
879 | EC_POINT *point, const BIGNUM *x, | |
880 | const BIGNUM *y, BN_CTX *ctx) | |
881 | { | |
882 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); | |
883 | } | |
884 | ||
50db8163 | 885 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
886 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
887 | EC_POINT *point, const BIGNUM *x, | |
888 | const BIGNUM *y, BN_CTX *ctx) | |
889 | { | |
8e3cced7 MC |
890 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); |
891 | } | |
50db8163 | 892 | # endif |
8e3cced7 MC |
893 | #endif |
894 | ||
895 | int EC_POINT_get_affine_coordinates(const EC_GROUP *group, | |
896 | const EC_POINT *point, BIGNUM *x, BIGNUM *y, | |
897 | BN_CTX *ctx) | |
898 | { | |
899 | if (group->meth->point_get_affine_coordinates == NULL) { | |
9311d0c4 | 900 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
901 | return 0; |
902 | } | |
b14e6015 | 903 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 904 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
1e2012b7 EK |
905 | return 0; |
906 | } | |
bfb10b97 | 907 | if (EC_POINT_is_at_infinity(group, point)) { |
9311d0c4 | 908 | ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY); |
bfb10b97 BB |
909 | return 0; |
910 | } | |
8e3cced7 | 911 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 912 | } |
7793f30e | 913 | |
936c2b9e | 914 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
915 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
916 | const EC_POINT *point, BIGNUM *x, | |
917 | BIGNUM *y, BN_CTX *ctx) | |
918 | { | |
8e3cced7 | 919 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 920 | } |
7793f30e | 921 | |
50db8163 | 922 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
923 | int EC_POINT_get_affine_coordinates_GF2m(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 | } |
50db8163 | 929 | # endif |
b3310161 | 930 | #endif |
7793f30e | 931 | |
0f113f3e MC |
932 | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
933 | const EC_POINT *b, BN_CTX *ctx) | |
934 | { | |
935 | if (group->meth->add == 0) { | |
9311d0c4 | 936 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
937 | return 0; |
938 | } | |
b14e6015 MC |
939 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group) |
940 | || !ec_point_is_compat(b, group)) { | |
9311d0c4 | 941 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
942 | return 0; |
943 | } | |
944 | return group->meth->add(group, r, a, b, ctx); | |
945 | } | |
946 | ||
947 | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | |
948 | BN_CTX *ctx) | |
949 | { | |
950 | if (group->meth->dbl == 0) { | |
9311d0c4 | 951 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
952 | return 0; |
953 | } | |
b14e6015 | 954 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { |
9311d0c4 | 955 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
956 | return 0; |
957 | } | |
958 | return group->meth->dbl(group, r, a, ctx); | |
959 | } | |
0657bf9c | 960 | |
1d5bd6cf | 961 | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) |
0f113f3e MC |
962 | { |
963 | if (group->meth->invert == 0) { | |
9311d0c4 | 964 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
965 | return 0; |
966 | } | |
b14e6015 | 967 | if (!ec_point_is_compat(a, group)) { |
9311d0c4 | 968 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
969 | return 0; |
970 | } | |
971 | return group->meth->invert(group, a, ctx); | |
972 | } | |
1d5bd6cf | 973 | |
0657bf9c | 974 | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
0f113f3e MC |
975 | { |
976 | if (group->meth->is_at_infinity == 0) { | |
9311d0c4 | 977 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
978 | return 0; |
979 | } | |
b14e6015 | 980 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 981 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
982 | return 0; |
983 | } | |
984 | return group->meth->is_at_infinity(group, point); | |
985 | } | |
986 | ||
68886be7 MC |
987 | /* |
988 | * Check whether an EC_POINT is on the curve or not. Note that the return | |
989 | * value for this function should NOT be treated as a boolean. Return values: | |
990 | * 1: The point is on the curve | |
991 | * 0: The point is not on the curve | |
992 | * -1: An error occurred | |
993 | */ | |
0f113f3e MC |
994 | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
995 | BN_CTX *ctx) | |
996 | { | |
997 | if (group->meth->is_on_curve == 0) { | |
9311d0c4 | 998 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
999 | return 0; |
1000 | } | |
b14e6015 | 1001 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 1002 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
1003 | return 0; |
1004 | } | |
1005 | return group->meth->is_on_curve(group, point, ctx); | |
1006 | } | |
1007 | ||
1008 | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, | |
1009 | BN_CTX *ctx) | |
1010 | { | |
1011 | if (group->meth->point_cmp == 0) { | |
9311d0c4 | 1012 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
1013 | return -1; |
1014 | } | |
b14e6015 | 1015 | if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { |
9311d0c4 | 1016 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
1017 | return -1; |
1018 | } | |
1019 | return group->meth->point_cmp(group, a, b, ctx); | |
1020 | } | |
1d5bd6cf | 1021 | |
c2f2db9b | 1022 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
e869d4bd | 1023 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
1024 | { |
1025 | if (group->meth->make_affine == 0) { | |
9311d0c4 | 1026 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
1027 | return 0; |
1028 | } | |
b14e6015 | 1029 | if (!ec_point_is_compat(point, group)) { |
9311d0c4 | 1030 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
1031 | return 0; |
1032 | } | |
1033 | return group->meth->make_affine(group, point, ctx); | |
1034 | } | |
1035 | ||
1036 | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, | |
1037 | EC_POINT *points[], BN_CTX *ctx) | |
1038 | { | |
1039 | size_t i; | |
1040 | ||
1041 | if (group->meth->points_make_affine == 0) { | |
9311d0c4 | 1042 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
1043 | return 0; |
1044 | } | |
1045 | for (i = 0; i < num; i++) { | |
b14e6015 | 1046 | if (!ec_point_is_compat(points[i], group)) { |
9311d0c4 | 1047 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
1048 | return 0; |
1049 | } | |
1050 | } | |
1051 | return group->meth->points_make_affine(group, num, points, ctx); | |
1052 | } | |
c2f2db9b | 1053 | #endif |
0f113f3e MC |
1054 | |
1055 | /* | |
1056 | * Functions for point multiplication. If group->meth->mul is 0, we use the | |
1057 | * wNAF-based implementations in ec_mult.c; otherwise we dispatch through | |
1058 | * methods. | |
37c660ff BM |
1059 | */ |
1060 | ||
4fcd15c1 | 1061 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1062 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, |
0f113f3e MC |
1063 | size_t num, const EC_POINT *points[], |
1064 | const BIGNUM *scalars[], BN_CTX *ctx) | |
1065 | { | |
01ad66f8 NT |
1066 | int ret = 0; |
1067 | size_t i = 0; | |
f844f9eb | 1068 | #ifndef FIPS_MODULE |
01ad66f8 | 1069 | BN_CTX *new_ctx = NULL; |
a9612d6c | 1070 | #endif |
a9612d6c | 1071 | |
01ad66f8 | 1072 | if (!ec_point_is_compat(r, group)) { |
9311d0c4 | 1073 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
01ad66f8 NT |
1074 | return 0; |
1075 | } | |
1eb9b54a BE |
1076 | |
1077 | if (scalar == NULL && num == 0) | |
1078 | return EC_POINT_set_to_infinity(group, r); | |
1079 | ||
01ad66f8 NT |
1080 | for (i = 0; i < num; i++) { |
1081 | if (!ec_point_is_compat(points[i], group)) { | |
9311d0c4 | 1082 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
01ad66f8 NT |
1083 | return 0; |
1084 | } | |
1085 | } | |
1086 | ||
f844f9eb | 1087 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
1088 | if (ctx == NULL) |
1089 | ctx = new_ctx = BN_CTX_secure_new(); | |
1090 | #endif | |
1091 | if (ctx == NULL) { | |
9311d0c4 | 1092 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
0e8b6c97 AT |
1093 | return 0; |
1094 | } | |
1095 | ||
01ad66f8 NT |
1096 | if (group->meth->mul != NULL) |
1097 | ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx); | |
1098 | else | |
0f113f3e | 1099 | /* use default */ |
01ad66f8 | 1100 | ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); |
37c660ff | 1101 | |
f844f9eb | 1102 | #ifndef FIPS_MODULE |
01ad66f8 | 1103 | BN_CTX_free(new_ctx); |
a9612d6c | 1104 | #endif |
01ad66f8 | 1105 | return ret; |
0f113f3e | 1106 | } |
4fcd15c1 | 1107 | #endif |
37c660ff BM |
1108 | |
1109 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
1110 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
1111 | { | |
4fcd15c1 | 1112 | int ret = 0; |
9c47a338 | 1113 | size_t num; |
4fcd15c1 BB |
1114 | #ifndef FIPS_MODULE |
1115 | BN_CTX *new_ctx = NULL; | |
1116 | #endif | |
37c660ff | 1117 | |
4fcd15c1 BB |
1118 | if (!ec_point_is_compat(r, group) |
1119 | || (point != NULL && !ec_point_is_compat(point, group))) { | |
9311d0c4 | 1120 | ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); |
4fcd15c1 BB |
1121 | return 0; |
1122 | } | |
37c660ff | 1123 | |
4fcd15c1 BB |
1124 | if (g_scalar == NULL && p_scalar == NULL) |
1125 | return EC_POINT_set_to_infinity(group, r); | |
37c660ff | 1126 | |
4fcd15c1 BB |
1127 | #ifndef FIPS_MODULE |
1128 | if (ctx == NULL) | |
1129 | ctx = new_ctx = BN_CTX_secure_new(); | |
1130 | #endif | |
1131 | if (ctx == NULL) { | |
9311d0c4 | 1132 | ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
4fcd15c1 BB |
1133 | return 0; |
1134 | } | |
1135 | ||
9c47a338 | 1136 | num = (point != NULL && p_scalar != NULL) ? 1 : 0; |
4fcd15c1 | 1137 | if (group->meth->mul != NULL) |
9c47a338 | 1138 | ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1139 | else |
1140 | /* use default */ | |
9c47a338 | 1141 | ret = ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1142 | |
1143 | #ifndef FIPS_MODULE | |
1144 | BN_CTX_free(new_ctx); | |
1145 | #endif | |
1146 | return ret; | |
0f113f3e | 1147 | } |
37c660ff | 1148 | |
6b4eb933 | 1149 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1150 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
1151 | { |
1152 | if (group->meth->mul == 0) | |
1153 | /* use default */ | |
1154 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 1155 | |
0f113f3e MC |
1156 | if (group->meth->precompute_mult != 0) |
1157 | return group->meth->precompute_mult(group, ctx); | |
1158 | else | |
1159 | return 1; /* nothing to do, so report success */ | |
1160 | } | |
37c660ff BM |
1161 | |
1162 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
1163 | { |
1164 | if (group->meth->mul == 0) | |
1165 | /* use default */ | |
1166 | return ec_wNAF_have_precompute_mult(group); | |
1167 | ||
1168 | if (group->meth->have_precompute_mult != 0) | |
1169 | return group->meth->have_precompute_mult(group); | |
1170 | else | |
1171 | return 0; /* cannot tell whether precomputation has | |
1172 | * been performed */ | |
1173 | } | |
6b4eb933 | 1174 | #endif |
0f113f3e MC |
1175 | |
1176 | /* | |
1177 | * ec_precompute_mont_data sets |group->mont_data| from |group->order| and | |
1178 | * returns one on success. On error it returns zero. | |
1179 | */ | |
eb791696 | 1180 | static int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e | 1181 | { |
a9612d6c | 1182 | BN_CTX *ctx = BN_CTX_new_ex(group->libctx); |
0f113f3e MC |
1183 | int ret = 0; |
1184 | ||
23a1d5e9 RS |
1185 | BN_MONT_CTX_free(group->mont_data); |
1186 | group->mont_data = NULL; | |
0f113f3e MC |
1187 | |
1188 | if (ctx == NULL) | |
1189 | goto err; | |
1190 | ||
1191 | group->mont_data = BN_MONT_CTX_new(); | |
90945fa3 | 1192 | if (group->mont_data == NULL) |
0f113f3e MC |
1193 | goto err; |
1194 | ||
1195 | if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | |
1196 | BN_MONT_CTX_free(group->mont_data); | |
1197 | group->mont_data = NULL; | |
1198 | goto err; | |
1199 | } | |
1200 | ||
1201 | ret = 1; | |
1202 | ||
1203 | err: | |
1204 | ||
23a1d5e9 | 1205 | BN_CTX_free(ctx); |
0f113f3e MC |
1206 | return ret; |
1207 | } | |
3aef36ff | 1208 | |
f844f9eb | 1209 | #ifndef FIPS_MODULE |
3aef36ff RS |
1210 | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg) |
1211 | { | |
1212 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
1213 | } | |
1214 | ||
1215 | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx) | |
1216 | { | |
1217 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
1218 | } | |
a9612d6c | 1219 | #endif |
77470e98 DSH |
1220 | |
1221 | int ec_group_simple_order_bits(const EC_GROUP *group) | |
1222 | { | |
1223 | if (group->order == NULL) | |
1224 | return 0; | |
1225 | return BN_num_bits(group->order); | |
1226 | } | |
eb791696 | 1227 | |
c11d372b | 1228 | static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r, |
792546eb | 1229 | const BIGNUM *x, BN_CTX *ctx) |
c11d372b | 1230 | { |
262dccc0 | 1231 | BIGNUM *e = NULL; |
c11d372b | 1232 | int ret = 0; |
f844f9eb | 1233 | #ifndef FIPS_MODULE |
a9612d6c | 1234 | BN_CTX *new_ctx = NULL; |
0e8b6c97 AT |
1235 | #endif |
1236 | ||
1237 | if (group->mont_data == NULL) | |
1238 | return 0; | |
c11d372b | 1239 | |
f844f9eb | 1240 | #ifndef FIPS_MODULE |
a9612d6c MC |
1241 | if (ctx == NULL) |
1242 | ctx = new_ctx = BN_CTX_secure_new(); | |
1243 | #endif | |
1244 | if (ctx == NULL) | |
792546eb BB |
1245 | return 0; |
1246 | ||
c11d372b | 1247 | BN_CTX_start(ctx); |
262dccc0 | 1248 | if ((e = BN_CTX_get(ctx)) == NULL) |
c11d372b BB |
1249 | goto err; |
1250 | ||
792546eb BB |
1251 | /*- |
1252 | * We want inverse in constant time, therefore we utilize the fact | |
1253 | * order must be prime and use Fermats Little Theorem instead. | |
1254 | */ | |
1255 | if (!BN_set_word(e, 2)) | |
1256 | goto err; | |
1257 | if (!BN_sub(e, group->order, e)) | |
1258 | goto err; | |
1259 | /*- | |
1260 | * Exponent e is public. | |
1261 | * No need for scatter-gather or BN_FLG_CONSTTIME. | |
1262 | */ | |
1263 | if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data)) | |
1264 | goto err; | |
c11d372b | 1265 | |
792546eb | 1266 | ret = 1; |
c11d372b BB |
1267 | |
1268 | err: | |
ce1415ed | 1269 | BN_CTX_end(ctx); |
f844f9eb | 1270 | #ifndef FIPS_MODULE |
c11d372b | 1271 | BN_CTX_free(new_ctx); |
a9612d6c | 1272 | #endif |
c11d372b BB |
1273 | return ret; |
1274 | } | |
1275 | ||
792546eb BB |
1276 | /*- |
1277 | * Default behavior, if group->meth->field_inverse_mod_ord is NULL: | |
1278 | * - When group->order is even, this function returns an error. | |
1279 | * - When group->order is otherwise composite, the correctness | |
1280 | * of the output is not guaranteed. | |
1281 | * - When x is outside the range [1, group->order), the correctness | |
1282 | * of the output is not guaranteed. | |
1283 | * - Otherwise, this function returns the multiplicative inverse in the | |
1284 | * range [1, group->order). | |
1285 | * | |
1286 | * EC_METHODs must implement their own field_inverse_mod_ord for | |
1287 | * other functionality. | |
1288 | */ | |
1289 | int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res, | |
1290 | const BIGNUM *x, BN_CTX *ctx) | |
eb791696 AP |
1291 | { |
1292 | if (group->meth->field_inverse_mod_ord != NULL) | |
1293 | return group->meth->field_inverse_mod_ord(group, res, x, ctx); | |
1294 | else | |
c11d372b | 1295 | return ec_field_inverse_mod_ord(group, res, x, ctx); |
eb791696 | 1296 | } |
f667820c SH |
1297 | |
1298 | /*- | |
1299 | * Coordinate blinding for EC_POINT. | |
1300 | * | |
1301 | * The underlying EC_METHOD can optionally implement this function: | |
1302 | * underlying implementations should return 0 on errors, or 1 on | |
1303 | * success. | |
1304 | * | |
1305 | * This wrapper returns 1 in case the underlying EC_METHOD does not | |
1306 | * support coordinate blinding. | |
1307 | */ | |
1308 | int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx) | |
1309 | { | |
1310 | if (group->meth->blind_coordinates == NULL) | |
1311 | return 1; /* ignore if not implemented */ | |
1312 | ||
1313 | return group->meth->blind_coordinates(group, p, ctx); | |
1314 | } | |
c0f39ded SL |
1315 | |
1316 | int EC_GROUP_get_basis_type(const EC_GROUP *group) | |
1317 | { | |
1318 | int i; | |
1319 | ||
1320 | if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field) | |
1321 | /* everything else is currently not supported */ | |
1322 | return 0; | |
1323 | ||
1324 | /* Find the last non-zero element of group->poly[] */ | |
1325 | for (i = 0; | |
1326 | i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0; | |
1327 | i++) | |
1328 | continue; | |
1329 | ||
1330 | if (i == 4) | |
1331 | return NID_X9_62_ppBasis; | |
1332 | else if (i == 2) | |
1333 | return NID_X9_62_tpBasis; | |
1334 | else | |
1335 | /* everything else is currently not supported */ | |
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | #ifndef OPENSSL_NO_EC2M | |
1340 | int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k) | |
1341 | { | |
1342 | if (group == NULL) | |
1343 | return 0; | |
1344 | ||
1345 | if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field | |
1346 | || !((group->poly[0] != 0) && (group->poly[1] != 0) | |
1347 | && (group->poly[2] == 0))) { | |
9311d0c4 | 1348 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
c0f39ded SL |
1349 | return 0; |
1350 | } | |
1351 | ||
1352 | if (k) | |
1353 | *k = group->poly[1]; | |
1354 | ||
1355 | return 1; | |
1356 | } | |
1357 | ||
1358 | int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1, | |
1359 | unsigned int *k2, unsigned int *k3) | |
1360 | { | |
1361 | if (group == NULL) | |
1362 | return 0; | |
1363 | ||
1364 | if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field | |
1365 | || !((group->poly[0] != 0) && (group->poly[1] != 0) | |
1366 | && (group->poly[2] != 0) && (group->poly[3] != 0) | |
1367 | && (group->poly[4] == 0))) { | |
9311d0c4 | 1368 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
c0f39ded SL |
1369 | return 0; |
1370 | } | |
1371 | ||
1372 | if (k1) | |
1373 | *k1 = group->poly[3]; | |
1374 | if (k2) | |
1375 | *k2 = group->poly[2]; | |
1376 | if (k3) | |
1377 | *k3 = group->poly[1]; | |
1378 | ||
1379 | return 1; | |
1380 | } | |
1381 | #endif | |
1382 | ||
1383 | /* | |
1384 | * Check if the explicit parameters group matches any built-in curves. | |
1385 | * | |
1386 | * We create a copy of the group just built, so that we can remove optional | |
1387 | * fields for the lookup: we do this to avoid the possibility that one of | |
1388 | * the optional parameters is used to force the library into using a less | |
1389 | * performant and less secure EC_METHOD instead of the specialized one. | |
1390 | * In any case, `seed` is not really used in any computation, while a | |
1391 | * cofactor different from the one in the built-in table is just | |
1392 | * mathematically wrong anyway and should not be used. | |
1393 | */ | |
1394 | static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group, | |
b4250010 | 1395 | OSSL_LIB_CTX *libctx, |
c0f39ded SL |
1396 | const char *propq, |
1397 | BN_CTX *ctx) | |
1398 | { | |
1399 | EC_GROUP *ret_group = NULL, *dup = NULL; | |
1400 | int curve_name_nid; | |
1401 | ||
1402 | const EC_POINT *point = EC_GROUP_get0_generator(group); | |
1403 | const BIGNUM *order = EC_GROUP_get0_order(group); | |
1404 | int no_seed = (EC_GROUP_get0_seed(group) == NULL); | |
1405 | ||
1406 | if ((dup = EC_GROUP_dup(group)) == NULL | |
1407 | || EC_GROUP_set_seed(dup, NULL, 0) != 1 | |
1408 | || !EC_GROUP_set_generator(dup, point, order, NULL)) | |
1409 | goto err; | |
1410 | if ((curve_name_nid = ec_curve_nid_from_params(dup, ctx)) != NID_undef) { | |
1411 | /* | |
1412 | * The input explicit parameters successfully matched one of the | |
1413 | * built-in curves: often for built-in curves we have specialized | |
1414 | * methods with better performance and hardening. | |
1415 | * | |
1416 | * In this case we replace the `EC_GROUP` created through explicit | |
1417 | * parameters with one created from a named group. | |
1418 | */ | |
1419 | ||
1420 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | |
1421 | /* | |
1422 | * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for | |
1423 | * the same curve, we prefer the SECP nid when matching explicit | |
1424 | * parameters as that is associated with a specialized EC_METHOD. | |
1425 | */ | |
1426 | if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12) | |
1427 | curve_name_nid = NID_secp224r1; | |
1428 | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ | |
1429 | ||
d8652be0 | 1430 | ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid); |
c0f39ded SL |
1431 | if (ret_group == NULL) |
1432 | goto err; | |
1433 | ||
1434 | /* | |
1435 | * Set the flag so that EC_GROUPs created from explicit parameters are | |
1436 | * serialized using explicit parameters by default. | |
1437 | */ | |
1438 | EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE); | |
1439 | ||
1440 | /* | |
1441 | * If the input params do not contain the optional seed field we make | |
1442 | * sure it is not added to the returned group. | |
1443 | * | |
1444 | * The seed field is not really used inside libcrypto anyway, and | |
1445 | * adding it to parsed explicit parameter keys would alter their DER | |
1446 | * encoding output (because of the extra field) which could impact | |
1447 | * applications fingerprinting keys by their DER encoding. | |
1448 | */ | |
1449 | if (no_seed) { | |
1450 | if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1) | |
1451 | goto err; | |
1452 | } | |
1453 | } else { | |
1454 | ret_group = (EC_GROUP *)group; | |
1455 | } | |
1456 | EC_GROUP_free(dup); | |
1457 | return ret_group; | |
1458 | err: | |
1459 | EC_GROUP_free(dup); | |
1460 | EC_GROUP_free(ret_group); | |
1461 | return NULL; | |
1462 | } | |
1463 | ||
c0f39ded | 1464 | static EC_GROUP *group_new_from_name(const OSSL_PARAM *p, |
b4250010 | 1465 | OSSL_LIB_CTX *libctx, const char *propq) |
c0f39ded SL |
1466 | { |
1467 | int ok = 0, nid; | |
1468 | const char *curve_name = NULL; | |
1469 | ||
1470 | switch (p->data_type) { | |
1471 | case OSSL_PARAM_UTF8_STRING: | |
1472 | /* The OSSL_PARAM functions have no support for this */ | |
1473 | curve_name = p->data; | |
1474 | ok = (curve_name != NULL); | |
1475 | break; | |
1476 | case OSSL_PARAM_UTF8_PTR: | |
1477 | ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name); | |
1478 | break; | |
1479 | } | |
1480 | ||
1481 | if (ok) { | |
1482 | nid = ec_curve_name2nid(curve_name); | |
1483 | if (nid == NID_undef) { | |
9311d0c4 | 1484 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE); |
c0f39ded SL |
1485 | return NULL; |
1486 | } else { | |
d8652be0 | 1487 | return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid); |
c0f39ded SL |
1488 | } |
1489 | } | |
1490 | return NULL; | |
1491 | } | |
1492 | ||
5b5eea4b SL |
1493 | /* These parameters can be set directly into an EC_GROUP */ |
1494 | int ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[]) | |
1495 | { | |
1496 | int encoding_flag = -1, format = -1; | |
1497 | const OSSL_PARAM *p; | |
1498 | ||
1499 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT); | |
1500 | if (p != NULL) { | |
1501 | if (!ec_pt_format_param2id(p, &format)) { | |
1502 | ECerr(0, EC_R_INVALID_FORM); | |
1503 | return 0; | |
1504 | } | |
1505 | EC_GROUP_set_point_conversion_form(group, format); | |
1506 | } | |
1507 | ||
1508 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING); | |
1509 | if (p != NULL) { | |
1510 | if (!ec_encoding_param2id(p, &encoding_flag)) { | |
1511 | ECerr(0, EC_R_INVALID_FORM); | |
1512 | return 0; | |
1513 | } | |
1514 | EC_GROUP_set_asn1_flag(group, encoding_flag); | |
1515 | } | |
1516 | /* Optional seed */ | |
1517 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED); | |
1518 | if (p != NULL) { | |
1519 | /* The seed is allowed to be NULL */ | |
1520 | if (p->data_type != OSSL_PARAM_OCTET_STRING | |
1521 | || !EC_GROUP_set_seed(group, p->data, p->data_size)) { | |
1522 | ECerr(0, EC_R_INVALID_SEED); | |
1523 | return 0; | |
1524 | } | |
1525 | } | |
1526 | return 1; | |
1527 | } | |
1528 | ||
c0f39ded | 1529 | EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[], |
b4250010 | 1530 | OSSL_LIB_CTX *libctx, const char *propq) |
c0f39ded SL |
1531 | { |
1532 | const OSSL_PARAM *ptmp, *pa, *pb; | |
1533 | int ok = 0; | |
1534 | EC_GROUP *group = NULL, *named_group = NULL; | |
1535 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL; | |
1536 | EC_POINT *point = NULL; | |
1537 | int field_bits = 0; | |
1538 | int is_prime_field = 1; | |
1539 | BN_CTX *bnctx = NULL; | |
1540 | const unsigned char *buf = NULL; | |
1541 | int encoding_flag = -1; | |
1542 | ||
5b5eea4b | 1543 | /* This is the simple named group case */ |
c0f39ded SL |
1544 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); |
1545 | if (ptmp != NULL) { | |
1546 | group = group_new_from_name(ptmp, libctx, propq); | |
5b5eea4b SL |
1547 | if (group != NULL) { |
1548 | if (!ec_group_set_params(group, params)) { | |
1549 | EC_GROUP_free(group); | |
1550 | group = NULL; | |
1551 | } | |
1552 | } | |
c0f39ded SL |
1553 | return group; |
1554 | } | |
5b5eea4b | 1555 | /* If it gets here then we are trying explicit parameters */ |
c0f39ded SL |
1556 | bnctx = BN_CTX_new_ex(libctx); |
1557 | if (bnctx == NULL) { | |
9311d0c4 | 1558 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
1559 | return 0; |
1560 | } | |
1561 | BN_CTX_start(bnctx); | |
1562 | ||
1563 | p = BN_CTX_get(bnctx); | |
1564 | a = BN_CTX_get(bnctx); | |
1565 | b = BN_CTX_get(bnctx); | |
1566 | order = BN_CTX_get(bnctx); | |
1567 | if (order == NULL) { | |
9311d0c4 | 1568 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
c0f39ded SL |
1569 | goto err; |
1570 | } | |
1571 | ||
1572 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE); | |
1573 | if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) { | |
9311d0c4 | 1574 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
c0f39ded SL |
1575 | goto err; |
1576 | } | |
1577 | if (strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) { | |
1578 | is_prime_field = 1; | |
1579 | } else if (strcasecmp(ptmp->data, SN_X9_62_characteristic_two_field) == 0) { | |
1580 | is_prime_field = 0; | |
1581 | } else { | |
1582 | /* Invalid field */ | |
9311d0c4 | 1583 | ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); |
c0f39ded SL |
1584 | goto err; |
1585 | } | |
1586 | ||
1587 | pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A); | |
1588 | if (!OSSL_PARAM_get_BN(pa, &a)) { | |
9311d0c4 | 1589 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_A); |
c0f39ded SL |
1590 | goto err; |
1591 | } | |
1592 | pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B); | |
1593 | if (!OSSL_PARAM_get_BN(pb, &b)) { | |
9311d0c4 | 1594 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_B); |
c0f39ded SL |
1595 | goto err; |
1596 | } | |
1597 | ||
1598 | /* extract the prime number or irreducible polynomial */ | |
1599 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P); | |
1600 | if (!OSSL_PARAM_get_BN(ptmp, &p)) { | |
9311d0c4 | 1601 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_P); |
c0f39ded SL |
1602 | goto err; |
1603 | } | |
1604 | ||
1605 | if (is_prime_field) { | |
1606 | if (BN_is_negative(p) || BN_is_zero(p)) { | |
9311d0c4 | 1607 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_P); |
c0f39ded SL |
1608 | goto err; |
1609 | } | |
1610 | field_bits = BN_num_bits(p); | |
1611 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
9311d0c4 | 1612 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
c0f39ded SL |
1613 | goto err; |
1614 | } | |
1615 | ||
1616 | /* create the EC_GROUP structure */ | |
1617 | group = EC_GROUP_new_curve_GFp(p, a, b, bnctx); | |
1618 | } else { | |
1619 | #ifdef OPENSSL_NO_EC2M | |
9311d0c4 | 1620 | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
c0f39ded SL |
1621 | goto err; |
1622 | #else | |
1623 | /* create the EC_GROUP structure */ | |
1624 | group = EC_GROUP_new_curve_GF2m(p, a, b, NULL); | |
1625 | if (group != NULL) { | |
1626 | field_bits = EC_GROUP_get_degree(group); | |
1627 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
9311d0c4 | 1628 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
c0f39ded SL |
1629 | goto err; |
1630 | } | |
1631 | } | |
1632 | #endif /* OPENSSL_NO_EC2M */ | |
1633 | } | |
1634 | ||
1635 | if (group == NULL) { | |
9311d0c4 | 1636 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
c0f39ded SL |
1637 | goto err; |
1638 | } | |
1639 | ||
1640 | /* Optional seed */ | |
1641 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED); | |
1642 | if (ptmp != NULL) { | |
1643 | if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | |
9311d0c4 | 1644 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED); |
c0f39ded SL |
1645 | goto err; |
1646 | } | |
1647 | if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size)) | |
1648 | goto err; | |
1649 | } | |
1650 | ||
1651 | /* generator base point */ | |
1652 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR); | |
1653 | if (ptmp == NULL | |
1654 | || ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | |
9311d0c4 | 1655 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
c0f39ded SL |
1656 | goto err; |
1657 | } | |
1658 | buf = (const unsigned char *)(ptmp->data); | |
1659 | if ((point = EC_POINT_new(group)) == NULL) | |
1660 | goto err; | |
1661 | EC_GROUP_set_point_conversion_form(group, | |
1662 | (point_conversion_form_t)buf[0] & ~0x01); | |
1663 | if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) { | |
9311d0c4 | 1664 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
c0f39ded SL |
1665 | goto err; |
1666 | } | |
1667 | ||
1668 | /* order */ | |
1669 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER); | |
1670 | if (!OSSL_PARAM_get_BN(ptmp, &order) | |
1671 | || (BN_is_negative(order) || BN_is_zero(order)) | |
1672 | || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */ | |
9311d0c4 | 1673 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
c0f39ded SL |
1674 | goto err; |
1675 | } | |
1676 | ||
1677 | /* Optional cofactor */ | |
1678 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR); | |
1679 | if (ptmp != NULL) { | |
1680 | cofactor = BN_CTX_get(bnctx); | |
1681 | if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) { | |
9311d0c4 | 1682 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR); |
c0f39ded SL |
1683 | goto err; |
1684 | } | |
1685 | } | |
1686 | ||
1687 | /* set the generator, order and cofactor (if present) */ | |
1688 | if (!EC_GROUP_set_generator(group, point, order, cofactor)) { | |
9311d0c4 | 1689 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR); |
c0f39ded SL |
1690 | goto err; |
1691 | } | |
1692 | ||
1693 | named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx); | |
1694 | if (named_group == NULL) { | |
9311d0c4 | 1695 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION); |
c0f39ded SL |
1696 | goto err; |
1697 | } | |
1698 | if (named_group == group) { | |
1699 | /* | |
1700 | * If we did not find a named group then the encoding should be explicit | |
1701 | * if it was specified | |
1702 | */ | |
5b5eea4b SL |
1703 | ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING); |
1704 | if (ptmp != NULL | |
1705 | && !ec_encoding_param2id(ptmp, &encoding_flag)) { | |
1706 | ECerr(0, EC_R_INVALID_ENCODING); | |
1707 | return 0; | |
1708 | } | |
c0f39ded | 1709 | if (encoding_flag == OPENSSL_EC_NAMED_CURVE) { |
9311d0c4 | 1710 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
c0f39ded SL |
1711 | goto err; |
1712 | } | |
1713 | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE); | |
1714 | } else { | |
1715 | EC_GROUP_free(group); | |
1716 | group = named_group; | |
1717 | } | |
1718 | ok = 1; | |
1719 | err: | |
1720 | if (!ok) { | |
1721 | EC_GROUP_free(group); | |
1722 | group = NULL; | |
1723 | } | |
1724 | EC_POINT_free(point); | |
1725 | BN_CTX_end(bnctx); | |
1726 | BN_CTX_free(bnctx); | |
1727 | ||
1728 | return group; | |
1729 | } |