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