]>
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 BM |
17 | #include <string.h> |
18 | ||
0657bf9c | 19 | #include <openssl/err.h> |
bb62a8b0 | 20 | #include <openssl/opensslv.h> |
0657bf9c | 21 | |
706457b7 | 22 | #include "ec_local.h" |
0657bf9c | 23 | |
0657bf9c BM |
24 | /* functions for EC_GROUP objects */ |
25 | ||
23ccae80 | 26 | EC_GROUP *ec_group_new_ex(OPENSSL_CTX *libctx, const EC_METHOD *meth) |
0f113f3e MC |
27 | { |
28 | EC_GROUP *ret; | |
29 | ||
30 | if (meth == NULL) { | |
a9612d6c | 31 | ECerr(EC_F_EC_GROUP_NEW_EX, EC_R_SLOT_FULL); |
0f113f3e MC |
32 | return NULL; |
33 | } | |
34 | if (meth->group_init == 0) { | |
a9612d6c | 35 | ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
36 | return NULL; |
37 | } | |
38 | ||
64b25758 | 39 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 40 | if (ret == NULL) { |
a9612d6c | 41 | ECerr(EC_F_EC_GROUP_NEW_EX, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
42 | return NULL; |
43 | } | |
44 | ||
a9612d6c | 45 | ret->libctx = libctx; |
0f113f3e | 46 | ret->meth = meth; |
6903e2e7 DSH |
47 | if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
48 | ret->order = BN_new(); | |
49 | if (ret->order == NULL) | |
50 | goto err; | |
51 | ret->cofactor = BN_new(); | |
52 | if (ret->cofactor == NULL) | |
53 | goto err; | |
54 | } | |
86f300d3 | 55 | ret->asn1_flag = OPENSSL_EC_NAMED_CURVE; |
0f113f3e | 56 | ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED; |
0f113f3e MC |
57 | if (!meth->group_init(ret)) |
58 | goto err; | |
0f113f3e | 59 | return ret; |
64b25758 | 60 | |
0f113f3e | 61 | err: |
23a1d5e9 RS |
62 | BN_free(ret->order); |
63 | BN_free(ret->cofactor); | |
0f113f3e MC |
64 | OPENSSL_free(ret); |
65 | return NULL; | |
66 | } | |
0657bf9c | 67 | |
23ccae80 BB |
68 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
69 | # ifndef FIPS_MODULE | |
a9612d6c MC |
70 | EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) |
71 | { | |
23ccae80 | 72 | return ec_group_new_ex(NULL, meth); |
a9612d6c | 73 | } |
23ccae80 | 74 | # endif |
a9612d6c MC |
75 | #endif |
76 | ||
2c52ac9b | 77 | void EC_pre_comp_free(EC_GROUP *group) |
3aef36ff RS |
78 | { |
79 | switch (group->pre_comp_type) { | |
f3b3d7f0 | 80 | case PCT_none: |
3aef36ff | 81 | break; |
66117ab0 | 82 | case PCT_nistz256: |
f3b3d7f0 | 83 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 84 | EC_nistz256_pre_comp_free(group->pre_comp.nistz256); |
e69aa800 | 85 | #endif |
f3b3d7f0 | 86 | break; |
3aef36ff | 87 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 88 | case PCT_nistp224: |
3aef36ff RS |
89 | EC_nistp224_pre_comp_free(group->pre_comp.nistp224); |
90 | break; | |
66117ab0 | 91 | case PCT_nistp256: |
3aef36ff RS |
92 | EC_nistp256_pre_comp_free(group->pre_comp.nistp256); |
93 | break; | |
66117ab0 | 94 | case PCT_nistp521: |
3aef36ff RS |
95 | EC_nistp521_pre_comp_free(group->pre_comp.nistp521); |
96 | break; | |
f3b3d7f0 RS |
97 | #else |
98 | case PCT_nistp224: | |
99 | case PCT_nistp256: | |
100 | case PCT_nistp521: | |
101 | break; | |
3aef36ff | 102 | #endif |
66117ab0 | 103 | case PCT_ec: |
3aef36ff RS |
104 | EC_ec_pre_comp_free(group->pre_comp.ec); |
105 | break; | |
106 | } | |
107 | group->pre_comp.ec = NULL; | |
108 | } | |
109 | ||
0657bf9c | 110 | void EC_GROUP_free(EC_GROUP *group) |
0f113f3e MC |
111 | { |
112 | if (!group) | |
113 | return; | |
7711de24 | 114 | |
0f113f3e MC |
115 | if (group->meth->group_finish != 0) |
116 | group->meth->group_finish(group); | |
df9cc153 | 117 | |
2c52ac9b | 118 | EC_pre_comp_free(group); |
23a1d5e9 | 119 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 120 | EC_POINT_free(group->generator); |
0f113f3e MC |
121 | BN_free(group->order); |
122 | BN_free(group->cofactor); | |
25aaa98a | 123 | OPENSSL_free(group->seed); |
0f113f3e MC |
124 | OPENSSL_free(group); |
125 | } | |
0657bf9c | 126 | |
936c2b9e | 127 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0657bf9c | 128 | void EC_GROUP_clear_free(EC_GROUP *group) |
0f113f3e MC |
129 | { |
130 | if (!group) | |
131 | return; | |
df9cc153 | 132 | |
0f113f3e MC |
133 | if (group->meth->group_clear_finish != 0) |
134 | group->meth->group_clear_finish(group); | |
135 | else if (group->meth->group_finish != 0) | |
136 | group->meth->group_finish(group); | |
df9cc153 | 137 | |
2c52ac9b | 138 | EC_pre_comp_free(group); |
23a1d5e9 | 139 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 140 | EC_POINT_clear_free(group->generator); |
0f113f3e MC |
141 | BN_clear_free(group->order); |
142 | BN_clear_free(group->cofactor); | |
4b45c6e5 | 143 | OPENSSL_clear_free(group->seed, group->seed_len); |
b4faea50 | 144 | OPENSSL_clear_free(group, sizeof(*group)); |
0f113f3e | 145 | } |
4a7a4972 | 146 | #endif |
0657bf9c BM |
147 | |
148 | int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) | |
0f113f3e | 149 | { |
0f113f3e MC |
150 | if (dest->meth->group_copy == 0) { |
151 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
152 | return 0; | |
153 | } | |
154 | if (dest->meth != src->meth) { | |
155 | ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS); | |
156 | return 0; | |
157 | } | |
158 | if (dest == src) | |
159 | return 1; | |
160 | ||
a9612d6c | 161 | dest->libctx = src->libctx; |
b14e6015 MC |
162 | dest->curve_name = src->curve_name; |
163 | ||
3aef36ff RS |
164 | /* Copy precomputed */ |
165 | dest->pre_comp_type = src->pre_comp_type; | |
166 | switch (src->pre_comp_type) { | |
f3b3d7f0 | 167 | case PCT_none: |
3aef36ff RS |
168 | dest->pre_comp.ec = NULL; |
169 | break; | |
66117ab0 | 170 | case PCT_nistz256: |
f3b3d7f0 | 171 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 172 | dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256); |
e69aa800 | 173 | #endif |
f3b3d7f0 | 174 | break; |
3aef36ff | 175 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 176 | case PCT_nistp224: |
3aef36ff RS |
177 | dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224); |
178 | break; | |
66117ab0 | 179 | case PCT_nistp256: |
3aef36ff RS |
180 | dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256); |
181 | break; | |
66117ab0 | 182 | case PCT_nistp521: |
3aef36ff RS |
183 | dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521); |
184 | break; | |
f3b3d7f0 RS |
185 | #else |
186 | case PCT_nistp224: | |
187 | case PCT_nistp256: | |
188 | case PCT_nistp521: | |
189 | break; | |
3aef36ff | 190 | #endif |
66117ab0 | 191 | case PCT_ec: |
3aef36ff RS |
192 | dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec); |
193 | break; | |
0f113f3e MC |
194 | } |
195 | ||
196 | if (src->mont_data != NULL) { | |
197 | if (dest->mont_data == NULL) { | |
198 | dest->mont_data = BN_MONT_CTX_new(); | |
199 | if (dest->mont_data == NULL) | |
200 | return 0; | |
201 | } | |
202 | if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) | |
203 | return 0; | |
204 | } else { | |
205 | /* src->generator == NULL */ | |
23a1d5e9 RS |
206 | BN_MONT_CTX_free(dest->mont_data); |
207 | dest->mont_data = NULL; | |
0f113f3e | 208 | } |
0657bf9c | 209 | |
0f113f3e MC |
210 | if (src->generator != NULL) { |
211 | if (dest->generator == NULL) { | |
212 | dest->generator = EC_POINT_new(dest); | |
213 | if (dest->generator == NULL) | |
214 | return 0; | |
215 | } | |
216 | if (!EC_POINT_copy(dest->generator, src->generator)) | |
217 | return 0; | |
218 | } else { | |
219 | /* src->generator == NULL */ | |
8fdc3734 RS |
220 | EC_POINT_clear_free(dest->generator); |
221 | dest->generator = NULL; | |
0f113f3e MC |
222 | } |
223 | ||
6903e2e7 DSH |
224 | if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
225 | if (!BN_copy(dest->order, src->order)) | |
226 | return 0; | |
227 | if (!BN_copy(dest->cofactor, src->cofactor)) | |
228 | return 0; | |
229 | } | |
0f113f3e | 230 | |
0f113f3e MC |
231 | dest->asn1_flag = src->asn1_flag; |
232 | dest->asn1_form = src->asn1_form; | |
233 | ||
234 | if (src->seed) { | |
b548a1f1 | 235 | OPENSSL_free(dest->seed); |
cdb10bae RS |
236 | if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) { |
237 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 238 | return 0; |
cdb10bae | 239 | } |
0f113f3e MC |
240 | if (!memcpy(dest->seed, src->seed, src->seed_len)) |
241 | return 0; | |
242 | dest->seed_len = src->seed_len; | |
243 | } else { | |
b548a1f1 | 244 | OPENSSL_free(dest->seed); |
0f113f3e MC |
245 | dest->seed = NULL; |
246 | dest->seed_len = 0; | |
247 | } | |
248 | ||
249 | return dest->meth->group_copy(dest, src); | |
250 | } | |
0657bf9c | 251 | |
7793f30e | 252 | EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) |
0f113f3e MC |
253 | { |
254 | EC_GROUP *t = NULL; | |
255 | int ok = 0; | |
7793f30e | 256 | |
0f113f3e MC |
257 | if (a == NULL) |
258 | return NULL; | |
7793f30e | 259 | |
23ccae80 | 260 | if ((t = ec_group_new_ex(a->libctx, a->meth)) == NULL) |
26a7d938 | 261 | return NULL; |
0f113f3e MC |
262 | if (!EC_GROUP_copy(t, a)) |
263 | goto err; | |
7793f30e | 264 | |
0f113f3e | 265 | ok = 1; |
7793f30e | 266 | |
0f113f3e MC |
267 | err: |
268 | if (!ok) { | |
8fdc3734 | 269 | EC_GROUP_free(t); |
0f113f3e | 270 | return NULL; |
8fdc3734 | 271 | } |
0f113f3e MC |
272 | return t; |
273 | } | |
7793f30e | 274 | |
23ccae80 | 275 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
48fe4d62 | 276 | const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) |
0f113f3e MC |
277 | { |
278 | return group->meth; | |
279 | } | |
48fe4d62 | 280 | |
458c2917 | 281 | int EC_METHOD_get_field_type(const EC_METHOD *meth) |
0f113f3e MC |
282 | { |
283 | return meth->field_type; | |
284 | } | |
23ccae80 | 285 | #endif |
0f113f3e | 286 | |
eb791696 AP |
287 | static int ec_precompute_mont_data(EC_GROUP *); |
288 | ||
b783beea BB |
289 | /*- |
290 | * Try computing cofactor from the generator order (n) and field cardinality (q). | |
291 | * This works for all curves of cryptographic interest. | |
292 | * | |
293 | * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q) | |
294 | * h_min = (q + 1 - 2*sqrt(q))/n | |
295 | * h_max = (q + 1 + 2*sqrt(q))/n | |
296 | * h_max - h_min = 4*sqrt(q)/n | |
297 | * So if n > 4*sqrt(q) holds, there is only one possible value for h: | |
298 | * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil | |
299 | * | |
300 | * Otherwise, zero cofactor and return success. | |
301 | */ | |
302 | static int ec_guess_cofactor(EC_GROUP *group) { | |
303 | int ret = 0; | |
304 | BN_CTX *ctx = NULL; | |
305 | BIGNUM *q = NULL; | |
306 | ||
307 | /*- | |
308 | * If the cofactor is too large, we cannot guess it. | |
309 | * The RHS of below is a strict overestimate of lg(4 * sqrt(q)) | |
310 | */ | |
311 | if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) { | |
312 | /* default to 0 */ | |
313 | BN_zero(group->cofactor); | |
314 | /* return success */ | |
315 | return 1; | |
316 | } | |
317 | ||
318 | if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL) | |
319 | return 0; | |
320 | ||
321 | BN_CTX_start(ctx); | |
322 | if ((q = BN_CTX_get(ctx)) == NULL) | |
323 | goto err; | |
324 | ||
325 | /* set q = 2**m for binary fields; q = p otherwise */ | |
326 | if (group->meth->field_type == NID_X9_62_characteristic_two_field) { | |
327 | BN_zero(q); | |
328 | if (!BN_set_bit(q, BN_num_bits(group->field) - 1)) | |
329 | goto err; | |
330 | } else { | |
331 | if (!BN_copy(q, group->field)) | |
332 | goto err; | |
333 | } | |
334 | ||
335 | /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */ | |
336 | if (!BN_rshift1(group->cofactor, group->order) /* n/2 */ | |
337 | || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */ | |
338 | /* q + 1 + n/2 */ | |
339 | || !BN_add(group->cofactor, group->cofactor, BN_value_one()) | |
340 | /* (q + 1 + n/2)/n */ | |
341 | || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx)) | |
342 | goto err; | |
343 | ret = 1; | |
344 | err: | |
345 | BN_CTX_end(ctx); | |
346 | BN_CTX_free(ctx); | |
347 | return ret; | |
348 | } | |
349 | ||
0f113f3e MC |
350 | int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, |
351 | const BIGNUM *order, const BIGNUM *cofactor) | |
352 | { | |
353 | if (generator == NULL) { | |
354 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER); | |
355 | return 0; | |
356 | } | |
357 | ||
b783beea BB |
358 | /* require group->field >= 1 */ |
359 | if (group->field == NULL || BN_is_zero(group->field) | |
360 | || BN_is_negative(group->field)) { | |
361 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | /*- | |
366 | * - require order >= 1 | |
367 | * - enforce upper bound due to Hasse thm: order can be no more than one bit | |
368 | * longer than field cardinality | |
369 | */ | |
370 | if (order == NULL || BN_is_zero(order) || BN_is_negative(order) | |
371 | || BN_num_bits(order) > BN_num_bits(group->field) + 1) { | |
372 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER); | |
373 | return 0; | |
374 | } | |
375 | ||
376 | /*- | |
377 | * Unfortunately the cofactor is an optional field in many standards. | |
378 | * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor". | |
379 | * So accept cofactor == NULL or cofactor >= 0. | |
380 | */ | |
381 | if (cofactor != NULL && BN_is_negative(cofactor)) { | |
382 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR); | |
383 | return 0; | |
384 | } | |
385 | ||
0f113f3e MC |
386 | if (group->generator == NULL) { |
387 | group->generator = EC_POINT_new(group); | |
388 | if (group->generator == NULL) | |
389 | return 0; | |
390 | } | |
391 | if (!EC_POINT_copy(group->generator, generator)) | |
392 | return 0; | |
393 | ||
b783beea BB |
394 | if (!BN_copy(group->order, order)) |
395 | return 0; | |
0f113f3e | 396 | |
b783beea BB |
397 | /* Either take the provided positive cofactor, or try to compute it */ |
398 | if (cofactor != NULL && !BN_is_zero(cofactor)) { | |
0f113f3e MC |
399 | if (!BN_copy(group->cofactor, cofactor)) |
400 | return 0; | |
b783beea | 401 | } else if (!ec_guess_cofactor(group)) { |
0f113f3e | 402 | BN_zero(group->cofactor); |
b783beea | 403 | return 0; |
8402cd5f | 404 | } |
b783beea | 405 | |
0f113f3e | 406 | /* |
3a6a4a93 | 407 | * Some groups have an order with |
0f113f3e MC |
408 | * factors of two, which makes the Montgomery setup fail. |
409 | * |group->mont_data| will be NULL in this case. | |
410 | */ | |
3a6a4a93 BB |
411 | if (BN_is_odd(group->order)) { |
412 | return ec_precompute_mont_data(group); | |
413 | } | |
0f113f3e | 414 | |
3a6a4a93 BB |
415 | BN_MONT_CTX_free(group->mont_data); |
416 | group->mont_data = NULL; | |
0f113f3e MC |
417 | return 1; |
418 | } | |
bb62a8b0 | 419 | |
9dd84053 | 420 | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) |
0f113f3e MC |
421 | { |
422 | return group->generator; | |
423 | } | |
bb62a8b0 | 424 | |
f54be179 | 425 | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group) |
0f113f3e MC |
426 | { |
427 | return group->mont_data; | |
428 | } | |
bb62a8b0 | 429 | |
b6db386f | 430 | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) |
0f113f3e | 431 | { |
be2e334f DSH |
432 | if (group->order == NULL) |
433 | return 0; | |
0f113f3e MC |
434 | if (!BN_copy(order, group->order)) |
435 | return 0; | |
0657bf9c | 436 | |
0f113f3e MC |
437 | return !BN_is_zero(order); |
438 | } | |
0657bf9c | 439 | |
be2e334f DSH |
440 | const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) |
441 | { | |
442 | return group->order; | |
443 | } | |
444 | ||
445 | int EC_GROUP_order_bits(const EC_GROUP *group) | |
446 | { | |
77470e98 | 447 | return group->meth->group_order_bits(group); |
be2e334f DSH |
448 | } |
449 | ||
0f113f3e MC |
450 | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
451 | BN_CTX *ctx) | |
452 | { | |
be2e334f DSH |
453 | |
454 | if (group->cofactor == NULL) | |
455 | return 0; | |
0f113f3e MC |
456 | if (!BN_copy(cofactor, group->cofactor)) |
457 | return 0; | |
48fe4d62 | 458 | |
0f113f3e MC |
459 | return !BN_is_zero(group->cofactor); |
460 | } | |
48fe4d62 | 461 | |
be2e334f DSH |
462 | const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group) |
463 | { | |
464 | return group->cofactor; | |
465 | } | |
466 | ||
7dc17a6c | 467 | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) |
0f113f3e MC |
468 | { |
469 | group->curve_name = nid; | |
470 | } | |
b6db386f | 471 | |
7dc17a6c | 472 | int EC_GROUP_get_curve_name(const EC_GROUP *group) |
0f113f3e MC |
473 | { |
474 | return group->curve_name; | |
475 | } | |
b6db386f | 476 | |
fa1f0306 DA |
477 | const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group) |
478 | { | |
479 | return group->field; | |
480 | } | |
481 | ||
23ccae80 BB |
482 | int EC_GROUP_get_field_type(const EC_GROUP *group) |
483 | { | |
484 | return group->meth->field_type; | |
485 | } | |
486 | ||
458c2917 | 487 | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) |
0f113f3e MC |
488 | { |
489 | group->asn1_flag = flag; | |
490 | } | |
458c2917 BM |
491 | |
492 | int EC_GROUP_get_asn1_flag(const EC_GROUP *group) | |
0f113f3e MC |
493 | { |
494 | return group->asn1_flag; | |
495 | } | |
458c2917 | 496 | |
0f113f3e | 497 | void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
254ef80d | 498 | point_conversion_form_t form) |
0f113f3e MC |
499 | { |
500 | group->asn1_form = form; | |
501 | } | |
254ef80d | 502 | |
0f113f3e MC |
503 | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP |
504 | *group) | |
505 | { | |
506 | return group->asn1_form; | |
507 | } | |
254ef80d | 508 | |
5f3d6f70 | 509 | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) |
0f113f3e | 510 | { |
b548a1f1 RS |
511 | OPENSSL_free(group->seed); |
512 | group->seed = NULL; | |
513 | group->seed_len = 0; | |
5f3d6f70 | 514 | |
0f113f3e MC |
515 | if (!len || !p) |
516 | return 1; | |
5f3d6f70 | 517 | |
f06080cb F |
518 | if ((group->seed = OPENSSL_malloc(len)) == NULL) { |
519 | ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 520 | return 0; |
f06080cb | 521 | } |
0f113f3e MC |
522 | memcpy(group->seed, p, len); |
523 | group->seed_len = len; | |
5f3d6f70 | 524 | |
0f113f3e MC |
525 | return len; |
526 | } | |
5f3d6f70 BM |
527 | |
528 | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) | |
0f113f3e MC |
529 | { |
530 | return group->seed; | |
531 | } | |
5f3d6f70 BM |
532 | |
533 | size_t EC_GROUP_get_seed_len(const EC_GROUP *group) | |
0f113f3e MC |
534 | { |
535 | return group->seed_len; | |
536 | } | |
537 | ||
8e3cced7 MC |
538 | int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
539 | const BIGNUM *b, BN_CTX *ctx) | |
0f113f3e MC |
540 | { |
541 | if (group->meth->group_set_curve == 0) { | |
8e3cced7 | 542 | ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
543 | return 0; |
544 | } | |
545 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
546 | } | |
547 | ||
8e3cced7 MC |
548 | int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, |
549 | BN_CTX *ctx) | |
0f113f3e | 550 | { |
8e3cced7 MC |
551 | if (group->meth->group_get_curve == NULL) { |
552 | ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
0f113f3e MC |
553 | return 0; |
554 | } | |
555 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
556 | } | |
7793f30e | 557 | |
936c2b9e | 558 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
8e3cced7 MC |
559 | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
560 | const BIGNUM *b, BN_CTX *ctx) | |
561 | { | |
562 | return EC_GROUP_set_curve(group, p, a, b, ctx); | |
563 | } | |
564 | ||
565 | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
566 | BIGNUM *b, BN_CTX *ctx) | |
567 | { | |
568 | return EC_GROUP_get_curve(group, p, a, b, ctx); | |
569 | } | |
570 | ||
50db8163 | 571 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
572 | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
573 | const BIGNUM *b, BN_CTX *ctx) | |
574 | { | |
8e3cced7 | 575 | return EC_GROUP_set_curve(group, p, a, b, ctx); |
0f113f3e MC |
576 | } |
577 | ||
578 | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
579 | BIGNUM *b, BN_CTX *ctx) | |
580 | { | |
8e3cced7 | 581 | return EC_GROUP_get_curve(group, p, a, b, ctx); |
0f113f3e | 582 | } |
50db8163 | 583 | # endif |
b3310161 | 584 | #endif |
7793f30e BM |
585 | |
586 | int EC_GROUP_get_degree(const EC_GROUP *group) | |
0f113f3e MC |
587 | { |
588 | if (group->meth->group_get_degree == 0) { | |
589 | ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
590 | return 0; | |
591 | } | |
592 | return group->meth->group_get_degree(group); | |
593 | } | |
48fe4d62 | 594 | |
17d6bb81 | 595 | int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
596 | { |
597 | if (group->meth->group_check_discriminant == 0) { | |
598 | ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, | |
599 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
600 | return 0; | |
601 | } | |
602 | return group->meth->group_check_discriminant(group, ctx); | |
603 | } | |
af28dd6c | 604 | |
ada0e717 | 605 | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) |
0f113f3e MC |
606 | { |
607 | int r = 0; | |
608 | BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; | |
f844f9eb | 609 | #ifndef FIPS_MODULE |
0f113f3e | 610 | BN_CTX *ctx_new = NULL; |
a9612d6c | 611 | #endif |
a9612d6c | 612 | |
0f113f3e | 613 | /* compare the field types */ |
23ccae80 | 614 | if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b)) |
0f113f3e MC |
615 | return 1; |
616 | /* compare the curve name (if present in both) */ | |
617 | if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && | |
618 | EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b)) | |
619 | return 1; | |
6903e2e7 DSH |
620 | if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE) |
621 | return 0; | |
0f113f3e | 622 | |
f844f9eb | 623 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
624 | if (ctx == NULL) |
625 | ctx_new = ctx = BN_CTX_new(); | |
626 | #endif | |
627 | if (ctx == NULL) | |
628 | return -1; | |
629 | ||
0f113f3e MC |
630 | BN_CTX_start(ctx); |
631 | a1 = BN_CTX_get(ctx); | |
632 | a2 = BN_CTX_get(ctx); | |
633 | a3 = BN_CTX_get(ctx); | |
634 | b1 = BN_CTX_get(ctx); | |
635 | b2 = BN_CTX_get(ctx); | |
636 | b3 = BN_CTX_get(ctx); | |
90945fa3 | 637 | if (b3 == NULL) { |
0f113f3e | 638 | BN_CTX_end(ctx); |
f844f9eb | 639 | #ifndef FIPS_MODULE |
23a1d5e9 | 640 | BN_CTX_free(ctx_new); |
a9612d6c | 641 | #endif |
0f113f3e MC |
642 | return -1; |
643 | } | |
644 | ||
645 | /* | |
646 | * XXX This approach assumes that the external representation of curves | |
647 | * over the same field type is the same. | |
648 | */ | |
649 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
650 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
651 | r = 1; | |
652 | ||
8402cd5f SL |
653 | /* return 1 if the curve parameters are different */ |
654 | if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0) | |
0f113f3e MC |
655 | r = 1; |
656 | ||
ac2b52c6 | 657 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ |
8402cd5f | 658 | /* return 1 if the generators are different */ |
0f113f3e | 659 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), |
8402cd5f | 660 | EC_GROUP_get0_generator(b), ctx) != 0) |
0f113f3e MC |
661 | r = 1; |
662 | ||
663 | if (!r) { | |
be2e334f | 664 | const BIGNUM *ao, *bo, *ac, *bc; |
ac2b52c6 | 665 | /* compare the orders */ |
be2e334f DSH |
666 | ao = EC_GROUP_get0_order(a); |
667 | bo = EC_GROUP_get0_order(b); | |
be2e334f | 668 | if (ao == NULL || bo == NULL) { |
8402cd5f SL |
669 | /* return an error if either order is NULL */ |
670 | r = -1; | |
671 | goto end; | |
672 | } | |
673 | if (BN_cmp(ao, bo) != 0) { | |
674 | /* return 1 if orders are different */ | |
675 | r = 1; | |
676 | goto end; | |
0f113f3e | 677 | } |
8402cd5f SL |
678 | /* |
679 | * It gets here if the curve parameters and generator matched. | |
680 | * Now check the optional cofactors (if both are present). | |
681 | */ | |
682 | ac = EC_GROUP_get0_cofactor(a); | |
683 | bc = EC_GROUP_get0_cofactor(b); | |
684 | /* Returns 1 (mismatch) if both cofactors are specified and different */ | |
685 | if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0) | |
0f113f3e | 686 | r = 1; |
8402cd5f | 687 | /* Returns 0 if the parameters matched */ |
0f113f3e | 688 | } |
8402cd5f | 689 | end: |
0f113f3e | 690 | BN_CTX_end(ctx); |
f844f9eb | 691 | #ifndef FIPS_MODULE |
23a1d5e9 | 692 | BN_CTX_free(ctx_new); |
a9612d6c | 693 | #endif |
0f113f3e MC |
694 | return r; |
695 | } | |
ada0e717 | 696 | |
0657bf9c BM |
697 | /* functions for EC_POINT objects */ |
698 | ||
699 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
700 | { |
701 | EC_POINT *ret; | |
702 | ||
703 | if (group == NULL) { | |
704 | ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); | |
705 | return NULL; | |
706 | } | |
f06080cb | 707 | if (group->meth->point_init == NULL) { |
0f113f3e MC |
708 | ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
709 | return NULL; | |
710 | } | |
711 | ||
1b4cf96f | 712 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
713 | if (ret == NULL) { |
714 | ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); | |
715 | return NULL; | |
716 | } | |
717 | ||
718 | ret->meth = group->meth; | |
b14e6015 | 719 | ret->curve_name = group->curve_name; |
0f113f3e MC |
720 | |
721 | if (!ret->meth->point_init(ret)) { | |
722 | OPENSSL_free(ret); | |
723 | return NULL; | |
724 | } | |
725 | ||
726 | return ret; | |
727 | } | |
0657bf9c BM |
728 | |
729 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e | 730 | { |
12a765a5 | 731 | if (point == NULL) |
0f113f3e | 732 | return; |
7711de24 | 733 | |
0f113f3e MC |
734 | if (point->meth->point_finish != 0) |
735 | point->meth->point_finish(point); | |
736 | OPENSSL_free(point); | |
737 | } | |
0657bf9c BM |
738 | |
739 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e | 740 | { |
12a765a5 | 741 | if (point == NULL) |
0f113f3e MC |
742 | return; |
743 | ||
744 | if (point->meth->point_clear_finish != 0) | |
745 | point->meth->point_clear_finish(point); | |
746 | else if (point->meth->point_finish != 0) | |
747 | point->meth->point_finish(point); | |
b4faea50 | 748 | OPENSSL_clear_free(point, sizeof(*point)); |
0f113f3e | 749 | } |
0657bf9c BM |
750 | |
751 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
752 | { |
753 | if (dest->meth->point_copy == 0) { | |
754 | ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
755 | return 0; | |
756 | } | |
b14e6015 MC |
757 | if (dest->meth != src->meth |
758 | || (dest->curve_name != src->curve_name | |
8402cd5f SL |
759 | && dest->curve_name != 0 |
760 | && src->curve_name != 0)) { | |
0f113f3e MC |
761 | ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); |
762 | return 0; | |
763 | } | |
764 | if (dest == src) | |
765 | return 1; | |
766 | return dest->meth->point_copy(dest, src); | |
767 | } | |
0657bf9c | 768 | |
7793f30e | 769 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
770 | { |
771 | EC_POINT *t; | |
772 | int r; | |
773 | ||
774 | if (a == NULL) | |
775 | return NULL; | |
776 | ||
777 | t = EC_POINT_new(group); | |
778 | if (t == NULL) | |
26a7d938 | 779 | return NULL; |
0f113f3e MC |
780 | r = EC_POINT_copy(t, a); |
781 | if (!r) { | |
782 | EC_POINT_free(t); | |
783 | return NULL; | |
8fdc3734 RS |
784 | } |
785 | return t; | |
0f113f3e | 786 | } |
7793f30e | 787 | |
23ccae80 | 788 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
48fe4d62 | 789 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
790 | { |
791 | return point->meth; | |
792 | } | |
23ccae80 | 793 | #endif |
48fe4d62 | 794 | |
226cc7de | 795 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
796 | { |
797 | if (group->meth->point_set_to_infinity == 0) { | |
798 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, | |
799 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
800 | return 0; | |
801 | } | |
802 | if (group->meth != point->meth) { | |
803 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); | |
804 | return 0; | |
805 | } | |
806 | return group->meth->point_set_to_infinity(group, point); | |
807 | } | |
808 | ||
07caec83 | 809 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
810 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, |
811 | EC_POINT *point, const BIGNUM *x, | |
812 | const BIGNUM *y, const BIGNUM *z, | |
813 | BN_CTX *ctx) | |
814 | { | |
07caec83 | 815 | if (group->meth->field_type != NID_X9_62_prime_field) { |
0f113f3e MC |
816 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
817 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
818 | return 0; | |
819 | } | |
b14e6015 | 820 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
821 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
822 | EC_R_INCOMPATIBLE_OBJECTS); | |
823 | return 0; | |
824 | } | |
07caec83 | 825 | return ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e MC |
826 | } |
827 | ||
828 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
829 | const EC_POINT *point, BIGNUM *x, | |
830 | BIGNUM *y, BIGNUM *z, | |
831 | BN_CTX *ctx) | |
832 | { | |
07caec83 | 833 | if (group->meth->field_type != NID_X9_62_prime_field) { |
0f113f3e MC |
834 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
835 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
836 | return 0; | |
837 | } | |
b14e6015 | 838 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
839 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
840 | EC_R_INCOMPATIBLE_OBJECTS); | |
841 | return 0; | |
842 | } | |
07caec83 | 843 | return ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx); |
0f113f3e | 844 | } |
07caec83 | 845 | #endif |
0f113f3e | 846 | |
8e3cced7 MC |
847 | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, |
848 | const BIGNUM *x, const BIGNUM *y, | |
849 | BN_CTX *ctx) | |
0f113f3e | 850 | { |
8e3cced7 MC |
851 | if (group->meth->point_set_affine_coordinates == NULL) { |
852 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, | |
0f113f3e MC |
853 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
854 | return 0; | |
855 | } | |
b14e6015 | 856 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 857 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
858 | return 0; |
859 | } | |
1e2012b7 EK |
860 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
861 | return 0; | |
862 | ||
863 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
8e3cced7 | 864 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE); |
1e2012b7 EK |
865 | return 0; |
866 | } | |
867 | return 1; | |
0f113f3e | 868 | } |
226cc7de | 869 | |
936c2b9e | 870 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
8e3cced7 MC |
871 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, |
872 | EC_POINT *point, const BIGNUM *x, | |
873 | const BIGNUM *y, BN_CTX *ctx) | |
874 | { | |
875 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); | |
876 | } | |
877 | ||
50db8163 | 878 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
879 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
880 | EC_POINT *point, const BIGNUM *x, | |
881 | const BIGNUM *y, BN_CTX *ctx) | |
882 | { | |
8e3cced7 MC |
883 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); |
884 | } | |
50db8163 | 885 | # endif |
8e3cced7 MC |
886 | #endif |
887 | ||
888 | int EC_POINT_get_affine_coordinates(const EC_GROUP *group, | |
889 | const EC_POINT *point, BIGNUM *x, BIGNUM *y, | |
890 | BN_CTX *ctx) | |
891 | { | |
892 | if (group->meth->point_get_affine_coordinates == NULL) { | |
893 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, | |
0f113f3e MC |
894 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
895 | return 0; | |
896 | } | |
b14e6015 | 897 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 898 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
1e2012b7 EK |
899 | return 0; |
900 | } | |
bfb10b97 BB |
901 | if (EC_POINT_is_at_infinity(group, point)) { |
902 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY); | |
903 | return 0; | |
904 | } | |
8e3cced7 | 905 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 906 | } |
7793f30e | 907 | |
936c2b9e | 908 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e MC |
909 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
910 | const EC_POINT *point, BIGNUM *x, | |
911 | BIGNUM *y, BN_CTX *ctx) | |
912 | { | |
8e3cced7 | 913 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 914 | } |
7793f30e | 915 | |
50db8163 | 916 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
917 | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, |
918 | const EC_POINT *point, BIGNUM *x, | |
919 | BIGNUM *y, BN_CTX *ctx) | |
920 | { | |
8e3cced7 | 921 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 922 | } |
50db8163 | 923 | # endif |
b3310161 | 924 | #endif |
7793f30e | 925 | |
0f113f3e MC |
926 | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
927 | const EC_POINT *b, BN_CTX *ctx) | |
928 | { | |
929 | if (group->meth->add == 0) { | |
930 | ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
931 | return 0; | |
932 | } | |
b14e6015 MC |
933 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group) |
934 | || !ec_point_is_compat(b, group)) { | |
0f113f3e MC |
935 | ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS); |
936 | return 0; | |
937 | } | |
938 | return group->meth->add(group, r, a, b, ctx); | |
939 | } | |
940 | ||
941 | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | |
942 | BN_CTX *ctx) | |
943 | { | |
944 | if (group->meth->dbl == 0) { | |
945 | ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
946 | return 0; | |
947 | } | |
b14e6015 | 948 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { |
0f113f3e MC |
949 | ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS); |
950 | return 0; | |
951 | } | |
952 | return group->meth->dbl(group, r, a, ctx); | |
953 | } | |
0657bf9c | 954 | |
1d5bd6cf | 955 | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) |
0f113f3e MC |
956 | { |
957 | if (group->meth->invert == 0) { | |
958 | ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
959 | return 0; | |
960 | } | |
b14e6015 | 961 | if (!ec_point_is_compat(a, group)) { |
0f113f3e MC |
962 | ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS); |
963 | return 0; | |
964 | } | |
965 | return group->meth->invert(group, a, ctx); | |
966 | } | |
1d5bd6cf | 967 | |
0657bf9c | 968 | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
0f113f3e MC |
969 | { |
970 | if (group->meth->is_at_infinity == 0) { | |
971 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, | |
972 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
973 | return 0; | |
974 | } | |
b14e6015 | 975 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
976 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); |
977 | return 0; | |
978 | } | |
979 | return group->meth->is_at_infinity(group, point); | |
980 | } | |
981 | ||
68886be7 MC |
982 | /* |
983 | * Check whether an EC_POINT is on the curve or not. Note that the return | |
984 | * value for this function should NOT be treated as a boolean. Return values: | |
985 | * 1: The point is on the curve | |
986 | * 0: The point is not on the curve | |
987 | * -1: An error occurred | |
988 | */ | |
0f113f3e MC |
989 | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
990 | BN_CTX *ctx) | |
991 | { | |
992 | if (group->meth->is_on_curve == 0) { | |
993 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
994 | return 0; | |
995 | } | |
b14e6015 | 996 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
997 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS); |
998 | return 0; | |
999 | } | |
1000 | return group->meth->is_on_curve(group, point, ctx); | |
1001 | } | |
1002 | ||
1003 | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, | |
1004 | BN_CTX *ctx) | |
1005 | { | |
1006 | if (group->meth->point_cmp == 0) { | |
1007 | ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1008 | return -1; | |
1009 | } | |
b14e6015 | 1010 | if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { |
0f113f3e MC |
1011 | ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); |
1012 | return -1; | |
1013 | } | |
1014 | return group->meth->point_cmp(group, a, b, ctx); | |
1015 | } | |
1d5bd6cf | 1016 | |
c2f2db9b | 1017 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
e869d4bd | 1018 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
1019 | { |
1020 | if (group->meth->make_affine == 0) { | |
1021 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1022 | return 0; | |
1023 | } | |
b14e6015 | 1024 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
1025 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
1026 | return 0; | |
1027 | } | |
1028 | return group->meth->make_affine(group, point, ctx); | |
1029 | } | |
1030 | ||
1031 | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, | |
1032 | EC_POINT *points[], BN_CTX *ctx) | |
1033 | { | |
1034 | size_t i; | |
1035 | ||
1036 | if (group->meth->points_make_affine == 0) { | |
1037 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1038 | return 0; | |
1039 | } | |
1040 | for (i = 0; i < num; i++) { | |
b14e6015 | 1041 | if (!ec_point_is_compat(points[i], group)) { |
0f113f3e MC |
1042 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
1043 | return 0; | |
1044 | } | |
1045 | } | |
1046 | return group->meth->points_make_affine(group, num, points, ctx); | |
1047 | } | |
c2f2db9b | 1048 | #endif |
0f113f3e MC |
1049 | |
1050 | /* | |
1051 | * Functions for point multiplication. If group->meth->mul is 0, we use the | |
1052 | * wNAF-based implementations in ec_mult.c; otherwise we dispatch through | |
1053 | * methods. | |
37c660ff BM |
1054 | */ |
1055 | ||
4fcd15c1 | 1056 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1057 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, |
0f113f3e MC |
1058 | size_t num, const EC_POINT *points[], |
1059 | const BIGNUM *scalars[], BN_CTX *ctx) | |
1060 | { | |
01ad66f8 NT |
1061 | int ret = 0; |
1062 | size_t i = 0; | |
f844f9eb | 1063 | #ifndef FIPS_MODULE |
01ad66f8 | 1064 | BN_CTX *new_ctx = NULL; |
a9612d6c | 1065 | #endif |
a9612d6c | 1066 | |
01ad66f8 NT |
1067 | if (!ec_point_is_compat(r, group)) { |
1068 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1069 | return 0; | |
1070 | } | |
1eb9b54a BE |
1071 | |
1072 | if (scalar == NULL && num == 0) | |
1073 | return EC_POINT_set_to_infinity(group, r); | |
1074 | ||
01ad66f8 NT |
1075 | for (i = 0; i < num; i++) { |
1076 | if (!ec_point_is_compat(points[i], group)) { | |
1077 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1078 | return 0; | |
1079 | } | |
1080 | } | |
1081 | ||
f844f9eb | 1082 | #ifndef FIPS_MODULE |
0e8b6c97 AT |
1083 | if (ctx == NULL) |
1084 | ctx = new_ctx = BN_CTX_secure_new(); | |
1085 | #endif | |
1086 | if (ctx == NULL) { | |
1087 | ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR); | |
1088 | return 0; | |
1089 | } | |
1090 | ||
01ad66f8 NT |
1091 | if (group->meth->mul != NULL) |
1092 | ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx); | |
1093 | else | |
0f113f3e | 1094 | /* use default */ |
01ad66f8 | 1095 | ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); |
37c660ff | 1096 | |
f844f9eb | 1097 | #ifndef FIPS_MODULE |
01ad66f8 | 1098 | BN_CTX_free(new_ctx); |
a9612d6c | 1099 | #endif |
01ad66f8 | 1100 | return ret; |
0f113f3e | 1101 | } |
4fcd15c1 | 1102 | #endif |
37c660ff BM |
1103 | |
1104 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
1105 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
1106 | { | |
4fcd15c1 | 1107 | int ret = 0; |
9c47a338 | 1108 | size_t num; |
4fcd15c1 BB |
1109 | #ifndef FIPS_MODULE |
1110 | BN_CTX *new_ctx = NULL; | |
1111 | #endif | |
37c660ff | 1112 | |
4fcd15c1 BB |
1113 | if (!ec_point_is_compat(r, group) |
1114 | || (point != NULL && !ec_point_is_compat(point, group))) { | |
1115 | ECerr(EC_F_EC_POINT_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
1116 | return 0; | |
1117 | } | |
37c660ff | 1118 | |
4fcd15c1 BB |
1119 | if (g_scalar == NULL && p_scalar == NULL) |
1120 | return EC_POINT_set_to_infinity(group, r); | |
37c660ff | 1121 | |
4fcd15c1 BB |
1122 | #ifndef FIPS_MODULE |
1123 | if (ctx == NULL) | |
1124 | ctx = new_ctx = BN_CTX_secure_new(); | |
1125 | #endif | |
1126 | if (ctx == NULL) { | |
1127 | ECerr(EC_F_EC_POINT_MUL, ERR_R_INTERNAL_ERROR); | |
1128 | return 0; | |
1129 | } | |
1130 | ||
9c47a338 | 1131 | num = (point != NULL && p_scalar != NULL) ? 1 : 0; |
4fcd15c1 | 1132 | if (group->meth->mul != NULL) |
9c47a338 | 1133 | ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1134 | else |
1135 | /* use default */ | |
9c47a338 | 1136 | ret = ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx); |
4fcd15c1 BB |
1137 | |
1138 | #ifndef FIPS_MODULE | |
1139 | BN_CTX_free(new_ctx); | |
1140 | #endif | |
1141 | return ret; | |
0f113f3e | 1142 | } |
37c660ff | 1143 | |
6b4eb933 | 1144 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
37c660ff | 1145 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
1146 | { |
1147 | if (group->meth->mul == 0) | |
1148 | /* use default */ | |
1149 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 1150 | |
0f113f3e MC |
1151 | if (group->meth->precompute_mult != 0) |
1152 | return group->meth->precompute_mult(group, ctx); | |
1153 | else | |
1154 | return 1; /* nothing to do, so report success */ | |
1155 | } | |
37c660ff BM |
1156 | |
1157 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
1158 | { |
1159 | if (group->meth->mul == 0) | |
1160 | /* use default */ | |
1161 | return ec_wNAF_have_precompute_mult(group); | |
1162 | ||
1163 | if (group->meth->have_precompute_mult != 0) | |
1164 | return group->meth->have_precompute_mult(group); | |
1165 | else | |
1166 | return 0; /* cannot tell whether precomputation has | |
1167 | * been performed */ | |
1168 | } | |
6b4eb933 | 1169 | #endif |
0f113f3e MC |
1170 | |
1171 | /* | |
1172 | * ec_precompute_mont_data sets |group->mont_data| from |group->order| and | |
1173 | * returns one on success. On error it returns zero. | |
1174 | */ | |
eb791696 | 1175 | static int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e | 1176 | { |
a9612d6c | 1177 | BN_CTX *ctx = BN_CTX_new_ex(group->libctx); |
0f113f3e MC |
1178 | int ret = 0; |
1179 | ||
23a1d5e9 RS |
1180 | BN_MONT_CTX_free(group->mont_data); |
1181 | group->mont_data = NULL; | |
0f113f3e MC |
1182 | |
1183 | if (ctx == NULL) | |
1184 | goto err; | |
1185 | ||
1186 | group->mont_data = BN_MONT_CTX_new(); | |
90945fa3 | 1187 | if (group->mont_data == NULL) |
0f113f3e MC |
1188 | goto err; |
1189 | ||
1190 | if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | |
1191 | BN_MONT_CTX_free(group->mont_data); | |
1192 | group->mont_data = NULL; | |
1193 | goto err; | |
1194 | } | |
1195 | ||
1196 | ret = 1; | |
1197 | ||
1198 | err: | |
1199 | ||
23a1d5e9 | 1200 | BN_CTX_free(ctx); |
0f113f3e MC |
1201 | return ret; |
1202 | } | |
3aef36ff | 1203 | |
f844f9eb | 1204 | #ifndef FIPS_MODULE |
3aef36ff RS |
1205 | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg) |
1206 | { | |
1207 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
1208 | } | |
1209 | ||
1210 | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx) | |
1211 | { | |
1212 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
1213 | } | |
a9612d6c | 1214 | #endif |
77470e98 DSH |
1215 | |
1216 | int ec_group_simple_order_bits(const EC_GROUP *group) | |
1217 | { | |
1218 | if (group->order == NULL) | |
1219 | return 0; | |
1220 | return BN_num_bits(group->order); | |
1221 | } | |
eb791696 | 1222 | |
c11d372b | 1223 | static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r, |
792546eb | 1224 | const BIGNUM *x, BN_CTX *ctx) |
c11d372b | 1225 | { |
262dccc0 | 1226 | BIGNUM *e = NULL; |
c11d372b | 1227 | int ret = 0; |
f844f9eb | 1228 | #ifndef FIPS_MODULE |
a9612d6c | 1229 | BN_CTX *new_ctx = NULL; |
0e8b6c97 AT |
1230 | #endif |
1231 | ||
1232 | if (group->mont_data == NULL) | |
1233 | return 0; | |
c11d372b | 1234 | |
f844f9eb | 1235 | #ifndef FIPS_MODULE |
a9612d6c MC |
1236 | if (ctx == NULL) |
1237 | ctx = new_ctx = BN_CTX_secure_new(); | |
1238 | #endif | |
1239 | if (ctx == NULL) | |
792546eb BB |
1240 | return 0; |
1241 | ||
c11d372b | 1242 | BN_CTX_start(ctx); |
262dccc0 | 1243 | if ((e = BN_CTX_get(ctx)) == NULL) |
c11d372b BB |
1244 | goto err; |
1245 | ||
792546eb BB |
1246 | /*- |
1247 | * We want inverse in constant time, therefore we utilize the fact | |
1248 | * order must be prime and use Fermats Little Theorem instead. | |
1249 | */ | |
1250 | if (!BN_set_word(e, 2)) | |
1251 | goto err; | |
1252 | if (!BN_sub(e, group->order, e)) | |
1253 | goto err; | |
1254 | /*- | |
1255 | * Exponent e is public. | |
1256 | * No need for scatter-gather or BN_FLG_CONSTTIME. | |
1257 | */ | |
1258 | if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data)) | |
1259 | goto err; | |
c11d372b | 1260 | |
792546eb | 1261 | ret = 1; |
c11d372b BB |
1262 | |
1263 | err: | |
ce1415ed | 1264 | BN_CTX_end(ctx); |
f844f9eb | 1265 | #ifndef FIPS_MODULE |
c11d372b | 1266 | BN_CTX_free(new_ctx); |
a9612d6c | 1267 | #endif |
c11d372b BB |
1268 | return ret; |
1269 | } | |
1270 | ||
792546eb BB |
1271 | /*- |
1272 | * Default behavior, if group->meth->field_inverse_mod_ord is NULL: | |
1273 | * - When group->order is even, this function returns an error. | |
1274 | * - When group->order is otherwise composite, the correctness | |
1275 | * of the output is not guaranteed. | |
1276 | * - When x is outside the range [1, group->order), the correctness | |
1277 | * of the output is not guaranteed. | |
1278 | * - Otherwise, this function returns the multiplicative inverse in the | |
1279 | * range [1, group->order). | |
1280 | * | |
1281 | * EC_METHODs must implement their own field_inverse_mod_ord for | |
1282 | * other functionality. | |
1283 | */ | |
1284 | int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res, | |
1285 | const BIGNUM *x, BN_CTX *ctx) | |
eb791696 AP |
1286 | { |
1287 | if (group->meth->field_inverse_mod_ord != NULL) | |
1288 | return group->meth->field_inverse_mod_ord(group, res, x, ctx); | |
1289 | else | |
c11d372b | 1290 | return ec_field_inverse_mod_ord(group, res, x, ctx); |
eb791696 | 1291 | } |
f667820c SH |
1292 | |
1293 | /*- | |
1294 | * Coordinate blinding for EC_POINT. | |
1295 | * | |
1296 | * The underlying EC_METHOD can optionally implement this function: | |
1297 | * underlying implementations should return 0 on errors, or 1 on | |
1298 | * success. | |
1299 | * | |
1300 | * This wrapper returns 1 in case the underlying EC_METHOD does not | |
1301 | * support coordinate blinding. | |
1302 | */ | |
1303 | int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx) | |
1304 | { | |
1305 | if (group->meth->blind_coordinates == NULL) | |
1306 | return 1; /* ignore if not implemented */ | |
1307 | ||
1308 | return group->meth->blind_coordinates(group, p, ctx); | |
1309 | } |