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