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