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