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