]>
Commit | Line | Data |
---|---|---|
35b73a1f | 1 | /* |
3c7d0945 | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
65e81670 | 4 | * |
aa6bb135 RS |
5 | * Licensed under the OpenSSL license (the "License"). You may not use |
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 | |
c4b36ff4 BM |
11 | #include <string.h> |
12 | ||
0657bf9c | 13 | #include <openssl/err.h> |
bb62a8b0 | 14 | #include <openssl/opensslv.h> |
0657bf9c | 15 | |
65e81670 | 16 | #include "ec_lcl.h" |
0657bf9c | 17 | |
0657bf9c BM |
18 | /* functions for EC_GROUP objects */ |
19 | ||
20 | EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) | |
0f113f3e MC |
21 | { |
22 | EC_GROUP *ret; | |
23 | ||
24 | if (meth == NULL) { | |
25 | ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL); | |
26 | return NULL; | |
27 | } | |
28 | if (meth->group_init == 0) { | |
29 | ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
30 | return NULL; | |
31 | } | |
32 | ||
64b25758 | 33 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
34 | if (ret == NULL) { |
35 | ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE); | |
36 | return NULL; | |
37 | } | |
38 | ||
39 | ret->meth = meth; | |
6903e2e7 DSH |
40 | if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
41 | ret->order = BN_new(); | |
42 | if (ret->order == NULL) | |
43 | goto err; | |
44 | ret->cofactor = BN_new(); | |
45 | if (ret->cofactor == NULL) | |
46 | goto err; | |
47 | } | |
86f300d3 | 48 | ret->asn1_flag = OPENSSL_EC_NAMED_CURVE; |
0f113f3e | 49 | ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED; |
0f113f3e MC |
50 | if (!meth->group_init(ret)) |
51 | goto err; | |
0f113f3e | 52 | return ret; |
64b25758 | 53 | |
0f113f3e | 54 | err: |
23a1d5e9 RS |
55 | BN_free(ret->order); |
56 | BN_free(ret->cofactor); | |
0f113f3e MC |
57 | OPENSSL_free(ret); |
58 | return NULL; | |
59 | } | |
0657bf9c | 60 | |
2c52ac9b | 61 | void EC_pre_comp_free(EC_GROUP *group) |
3aef36ff RS |
62 | { |
63 | switch (group->pre_comp_type) { | |
f3b3d7f0 | 64 | case PCT_none: |
3aef36ff | 65 | break; |
66117ab0 | 66 | case PCT_nistz256: |
f3b3d7f0 | 67 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 68 | EC_nistz256_pre_comp_free(group->pre_comp.nistz256); |
e69aa800 | 69 | #endif |
f3b3d7f0 | 70 | break; |
3aef36ff | 71 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 72 | case PCT_nistp224: |
3aef36ff RS |
73 | EC_nistp224_pre_comp_free(group->pre_comp.nistp224); |
74 | break; | |
66117ab0 | 75 | case PCT_nistp256: |
3aef36ff RS |
76 | EC_nistp256_pre_comp_free(group->pre_comp.nistp256); |
77 | break; | |
66117ab0 | 78 | case PCT_nistp521: |
3aef36ff RS |
79 | EC_nistp521_pre_comp_free(group->pre_comp.nistp521); |
80 | break; | |
f3b3d7f0 RS |
81 | #else |
82 | case PCT_nistp224: | |
83 | case PCT_nistp256: | |
84 | case PCT_nistp521: | |
85 | break; | |
3aef36ff | 86 | #endif |
66117ab0 | 87 | case PCT_ec: |
3aef36ff RS |
88 | EC_ec_pre_comp_free(group->pre_comp.ec); |
89 | break; | |
90 | } | |
91 | group->pre_comp.ec = NULL; | |
92 | } | |
93 | ||
0657bf9c | 94 | void EC_GROUP_free(EC_GROUP *group) |
0f113f3e MC |
95 | { |
96 | if (!group) | |
97 | return; | |
7711de24 | 98 | |
0f113f3e MC |
99 | if (group->meth->group_finish != 0) |
100 | group->meth->group_finish(group); | |
df9cc153 | 101 | |
2c52ac9b | 102 | EC_pre_comp_free(group); |
23a1d5e9 | 103 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 104 | EC_POINT_free(group->generator); |
0f113f3e MC |
105 | BN_free(group->order); |
106 | BN_free(group->cofactor); | |
25aaa98a | 107 | OPENSSL_free(group->seed); |
0f113f3e MC |
108 | OPENSSL_free(group); |
109 | } | |
0657bf9c BM |
110 | |
111 | void EC_GROUP_clear_free(EC_GROUP *group) | |
0f113f3e MC |
112 | { |
113 | if (!group) | |
114 | return; | |
df9cc153 | 115 | |
0f113f3e MC |
116 | if (group->meth->group_clear_finish != 0) |
117 | group->meth->group_clear_finish(group); | |
118 | else if (group->meth->group_finish != 0) | |
119 | group->meth->group_finish(group); | |
df9cc153 | 120 | |
2c52ac9b | 121 | EC_pre_comp_free(group); |
23a1d5e9 | 122 | BN_MONT_CTX_free(group->mont_data); |
8fdc3734 | 123 | EC_POINT_clear_free(group->generator); |
0f113f3e MC |
124 | BN_clear_free(group->order); |
125 | BN_clear_free(group->cofactor); | |
4b45c6e5 | 126 | OPENSSL_clear_free(group->seed, group->seed_len); |
b4faea50 | 127 | OPENSSL_clear_free(group, sizeof(*group)); |
0f113f3e | 128 | } |
0657bf9c BM |
129 | |
130 | int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) | |
0f113f3e | 131 | { |
0f113f3e MC |
132 | if (dest->meth->group_copy == 0) { |
133 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
134 | return 0; | |
135 | } | |
136 | if (dest->meth != src->meth) { | |
137 | ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS); | |
138 | return 0; | |
139 | } | |
140 | if (dest == src) | |
141 | return 1; | |
142 | ||
b14e6015 MC |
143 | dest->curve_name = src->curve_name; |
144 | ||
3aef36ff RS |
145 | /* Copy precomputed */ |
146 | dest->pre_comp_type = src->pre_comp_type; | |
147 | switch (src->pre_comp_type) { | |
f3b3d7f0 | 148 | case PCT_none: |
3aef36ff RS |
149 | dest->pre_comp.ec = NULL; |
150 | break; | |
66117ab0 | 151 | case PCT_nistz256: |
f3b3d7f0 | 152 | #ifdef ECP_NISTZ256_ASM |
3aef36ff | 153 | dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256); |
e69aa800 | 154 | #endif |
f3b3d7f0 | 155 | break; |
3aef36ff | 156 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 |
66117ab0 | 157 | case PCT_nistp224: |
3aef36ff RS |
158 | dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224); |
159 | break; | |
66117ab0 | 160 | case PCT_nistp256: |
3aef36ff RS |
161 | dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256); |
162 | break; | |
66117ab0 | 163 | case PCT_nistp521: |
3aef36ff RS |
164 | dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521); |
165 | break; | |
f3b3d7f0 RS |
166 | #else |
167 | case PCT_nistp224: | |
168 | case PCT_nistp256: | |
169 | case PCT_nistp521: | |
170 | break; | |
3aef36ff | 171 | #endif |
66117ab0 | 172 | case PCT_ec: |
3aef36ff RS |
173 | dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec); |
174 | break; | |
0f113f3e MC |
175 | } |
176 | ||
177 | if (src->mont_data != NULL) { | |
178 | if (dest->mont_data == NULL) { | |
179 | dest->mont_data = BN_MONT_CTX_new(); | |
180 | if (dest->mont_data == NULL) | |
181 | return 0; | |
182 | } | |
183 | if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) | |
184 | return 0; | |
185 | } else { | |
186 | /* src->generator == NULL */ | |
23a1d5e9 RS |
187 | BN_MONT_CTX_free(dest->mont_data); |
188 | dest->mont_data = NULL; | |
0f113f3e | 189 | } |
0657bf9c | 190 | |
0f113f3e MC |
191 | if (src->generator != NULL) { |
192 | if (dest->generator == NULL) { | |
193 | dest->generator = EC_POINT_new(dest); | |
194 | if (dest->generator == NULL) | |
195 | return 0; | |
196 | } | |
197 | if (!EC_POINT_copy(dest->generator, src->generator)) | |
198 | return 0; | |
199 | } else { | |
200 | /* src->generator == NULL */ | |
8fdc3734 RS |
201 | EC_POINT_clear_free(dest->generator); |
202 | dest->generator = NULL; | |
0f113f3e MC |
203 | } |
204 | ||
6903e2e7 DSH |
205 | if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { |
206 | if (!BN_copy(dest->order, src->order)) | |
207 | return 0; | |
208 | if (!BN_copy(dest->cofactor, src->cofactor)) | |
209 | return 0; | |
210 | } | |
0f113f3e | 211 | |
0f113f3e MC |
212 | dest->asn1_flag = src->asn1_flag; |
213 | dest->asn1_form = src->asn1_form; | |
214 | ||
215 | if (src->seed) { | |
b548a1f1 | 216 | OPENSSL_free(dest->seed); |
cdb10bae RS |
217 | if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) { |
218 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 219 | return 0; |
cdb10bae | 220 | } |
0f113f3e MC |
221 | if (!memcpy(dest->seed, src->seed, src->seed_len)) |
222 | return 0; | |
223 | dest->seed_len = src->seed_len; | |
224 | } else { | |
b548a1f1 | 225 | OPENSSL_free(dest->seed); |
0f113f3e MC |
226 | dest->seed = NULL; |
227 | dest->seed_len = 0; | |
228 | } | |
229 | ||
230 | return dest->meth->group_copy(dest, src); | |
231 | } | |
0657bf9c | 232 | |
7793f30e | 233 | EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) |
0f113f3e MC |
234 | { |
235 | EC_GROUP *t = NULL; | |
236 | int ok = 0; | |
7793f30e | 237 | |
0f113f3e MC |
238 | if (a == NULL) |
239 | return NULL; | |
7793f30e | 240 | |
0f113f3e | 241 | if ((t = EC_GROUP_new(a->meth)) == NULL) |
26a7d938 | 242 | return NULL; |
0f113f3e MC |
243 | if (!EC_GROUP_copy(t, a)) |
244 | goto err; | |
7793f30e | 245 | |
0f113f3e | 246 | ok = 1; |
7793f30e | 247 | |
0f113f3e MC |
248 | err: |
249 | if (!ok) { | |
8fdc3734 | 250 | EC_GROUP_free(t); |
0f113f3e | 251 | return NULL; |
8fdc3734 | 252 | } |
0f113f3e MC |
253 | return t; |
254 | } | |
7793f30e | 255 | |
48fe4d62 | 256 | const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) |
0f113f3e MC |
257 | { |
258 | return group->meth; | |
259 | } | |
48fe4d62 | 260 | |
458c2917 | 261 | int EC_METHOD_get_field_type(const EC_METHOD *meth) |
0f113f3e MC |
262 | { |
263 | return meth->field_type; | |
264 | } | |
265 | ||
eb791696 AP |
266 | static int ec_precompute_mont_data(EC_GROUP *); |
267 | ||
0f113f3e MC |
268 | int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, |
269 | const BIGNUM *order, const BIGNUM *cofactor) | |
270 | { | |
271 | if (generator == NULL) { | |
272 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | if (group->generator == NULL) { | |
277 | group->generator = EC_POINT_new(group); | |
278 | if (group->generator == NULL) | |
279 | return 0; | |
280 | } | |
281 | if (!EC_POINT_copy(group->generator, generator)) | |
282 | return 0; | |
283 | ||
284 | if (order != NULL) { | |
285 | if (!BN_copy(group->order, order)) | |
286 | return 0; | |
287 | } else | |
288 | BN_zero(group->order); | |
289 | ||
290 | if (cofactor != NULL) { | |
291 | if (!BN_copy(group->cofactor, cofactor)) | |
292 | return 0; | |
293 | } else | |
294 | BN_zero(group->cofactor); | |
295 | ||
296 | /* | |
3a6a4a93 | 297 | * Some groups have an order with |
0f113f3e MC |
298 | * factors of two, which makes the Montgomery setup fail. |
299 | * |group->mont_data| will be NULL in this case. | |
300 | */ | |
3a6a4a93 BB |
301 | if (BN_is_odd(group->order)) { |
302 | return ec_precompute_mont_data(group); | |
303 | } | |
0f113f3e | 304 | |
3a6a4a93 BB |
305 | BN_MONT_CTX_free(group->mont_data); |
306 | group->mont_data = NULL; | |
0f113f3e MC |
307 | return 1; |
308 | } | |
bb62a8b0 | 309 | |
9dd84053 | 310 | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) |
0f113f3e MC |
311 | { |
312 | return group->generator; | |
313 | } | |
bb62a8b0 | 314 | |
f54be179 | 315 | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group) |
0f113f3e MC |
316 | { |
317 | return group->mont_data; | |
318 | } | |
bb62a8b0 | 319 | |
b6db386f | 320 | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) |
0f113f3e | 321 | { |
be2e334f DSH |
322 | if (group->order == NULL) |
323 | return 0; | |
0f113f3e MC |
324 | if (!BN_copy(order, group->order)) |
325 | return 0; | |
0657bf9c | 326 | |
0f113f3e MC |
327 | return !BN_is_zero(order); |
328 | } | |
0657bf9c | 329 | |
be2e334f DSH |
330 | const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) |
331 | { | |
332 | return group->order; | |
333 | } | |
334 | ||
335 | int EC_GROUP_order_bits(const EC_GROUP *group) | |
336 | { | |
77470e98 | 337 | return group->meth->group_order_bits(group); |
be2e334f DSH |
338 | } |
339 | ||
0f113f3e MC |
340 | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
341 | BN_CTX *ctx) | |
342 | { | |
be2e334f DSH |
343 | |
344 | if (group->cofactor == NULL) | |
345 | return 0; | |
0f113f3e MC |
346 | if (!BN_copy(cofactor, group->cofactor)) |
347 | return 0; | |
48fe4d62 | 348 | |
0f113f3e MC |
349 | return !BN_is_zero(group->cofactor); |
350 | } | |
48fe4d62 | 351 | |
be2e334f DSH |
352 | const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group) |
353 | { | |
354 | return group->cofactor; | |
355 | } | |
356 | ||
7dc17a6c | 357 | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) |
0f113f3e MC |
358 | { |
359 | group->curve_name = nid; | |
360 | } | |
b6db386f | 361 | |
7dc17a6c | 362 | int EC_GROUP_get_curve_name(const EC_GROUP *group) |
0f113f3e MC |
363 | { |
364 | return group->curve_name; | |
365 | } | |
b6db386f | 366 | |
458c2917 | 367 | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) |
0f113f3e MC |
368 | { |
369 | group->asn1_flag = flag; | |
370 | } | |
458c2917 BM |
371 | |
372 | int EC_GROUP_get_asn1_flag(const EC_GROUP *group) | |
0f113f3e MC |
373 | { |
374 | return group->asn1_flag; | |
375 | } | |
458c2917 | 376 | |
0f113f3e | 377 | void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
254ef80d | 378 | point_conversion_form_t form) |
0f113f3e MC |
379 | { |
380 | group->asn1_form = form; | |
381 | } | |
254ef80d | 382 | |
0f113f3e MC |
383 | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP |
384 | *group) | |
385 | { | |
386 | return group->asn1_form; | |
387 | } | |
254ef80d | 388 | |
5f3d6f70 | 389 | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) |
0f113f3e | 390 | { |
b548a1f1 RS |
391 | OPENSSL_free(group->seed); |
392 | group->seed = NULL; | |
393 | group->seed_len = 0; | |
5f3d6f70 | 394 | |
0f113f3e MC |
395 | if (!len || !p) |
396 | return 1; | |
5f3d6f70 | 397 | |
f06080cb F |
398 | if ((group->seed = OPENSSL_malloc(len)) == NULL) { |
399 | ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 400 | return 0; |
f06080cb | 401 | } |
0f113f3e MC |
402 | memcpy(group->seed, p, len); |
403 | group->seed_len = len; | |
5f3d6f70 | 404 | |
0f113f3e MC |
405 | return len; |
406 | } | |
5f3d6f70 BM |
407 | |
408 | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) | |
0f113f3e MC |
409 | { |
410 | return group->seed; | |
411 | } | |
5f3d6f70 BM |
412 | |
413 | size_t EC_GROUP_get_seed_len(const EC_GROUP *group) | |
0f113f3e MC |
414 | { |
415 | return group->seed_len; | |
416 | } | |
417 | ||
418 | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, | |
419 | const BIGNUM *b, BN_CTX *ctx) | |
420 | { | |
421 | if (group->meth->group_set_curve == 0) { | |
422 | ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
423 | return 0; | |
424 | } | |
425 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
426 | } | |
427 | ||
428 | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
429 | BIGNUM *b, BN_CTX *ctx) | |
430 | { | |
431 | if (group->meth->group_get_curve == 0) { | |
432 | ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
433 | return 0; | |
434 | } | |
435 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
436 | } | |
7793f30e | 437 | |
b3310161 | 438 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
439 | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
440 | const BIGNUM *b, BN_CTX *ctx) | |
441 | { | |
442 | if (group->meth->group_set_curve == 0) { | |
443 | ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, | |
444 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
445 | return 0; | |
446 | } | |
447 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
448 | } | |
449 | ||
450 | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
451 | BIGNUM *b, BN_CTX *ctx) | |
452 | { | |
453 | if (group->meth->group_get_curve == 0) { | |
454 | ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, | |
455 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
456 | return 0; | |
457 | } | |
458 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
459 | } | |
b3310161 | 460 | #endif |
7793f30e BM |
461 | |
462 | int EC_GROUP_get_degree(const EC_GROUP *group) | |
0f113f3e MC |
463 | { |
464 | if (group->meth->group_get_degree == 0) { | |
465 | ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
466 | return 0; | |
467 | } | |
468 | return group->meth->group_get_degree(group); | |
469 | } | |
48fe4d62 | 470 | |
17d6bb81 | 471 | int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
472 | { |
473 | if (group->meth->group_check_discriminant == 0) { | |
474 | ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, | |
475 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
476 | return 0; | |
477 | } | |
478 | return group->meth->group_check_discriminant(group, ctx); | |
479 | } | |
af28dd6c | 480 | |
ada0e717 | 481 | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) |
0f113f3e MC |
482 | { |
483 | int r = 0; | |
484 | BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; | |
485 | BN_CTX *ctx_new = NULL; | |
486 | ||
487 | /* compare the field types */ | |
488 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) != | |
489 | EC_METHOD_get_field_type(EC_GROUP_method_of(b))) | |
490 | return 1; | |
491 | /* compare the curve name (if present in both) */ | |
492 | if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && | |
493 | EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b)) | |
494 | return 1; | |
6903e2e7 DSH |
495 | if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE) |
496 | return 0; | |
0f113f3e | 497 | |
90945fa3 | 498 | if (ctx == NULL) |
0f113f3e | 499 | ctx_new = ctx = BN_CTX_new(); |
90945fa3 | 500 | if (ctx == NULL) |
0f113f3e MC |
501 | return -1; |
502 | ||
503 | BN_CTX_start(ctx); | |
504 | a1 = BN_CTX_get(ctx); | |
505 | a2 = BN_CTX_get(ctx); | |
506 | a3 = BN_CTX_get(ctx); | |
507 | b1 = BN_CTX_get(ctx); | |
508 | b2 = BN_CTX_get(ctx); | |
509 | b3 = BN_CTX_get(ctx); | |
90945fa3 | 510 | if (b3 == NULL) { |
0f113f3e | 511 | BN_CTX_end(ctx); |
23a1d5e9 | 512 | BN_CTX_free(ctx_new); |
0f113f3e MC |
513 | return -1; |
514 | } | |
515 | ||
516 | /* | |
517 | * XXX This approach assumes that the external representation of curves | |
518 | * over the same field type is the same. | |
519 | */ | |
520 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
521 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
522 | r = 1; | |
523 | ||
524 | if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3)) | |
525 | r = 1; | |
526 | ||
527 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ | |
528 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), | |
529 | EC_GROUP_get0_generator(b), ctx)) | |
530 | r = 1; | |
531 | ||
532 | if (!r) { | |
be2e334f | 533 | const BIGNUM *ao, *bo, *ac, *bc; |
0f113f3e | 534 | /* compare the order and cofactor */ |
be2e334f DSH |
535 | ao = EC_GROUP_get0_order(a); |
536 | bo = EC_GROUP_get0_order(b); | |
537 | ac = EC_GROUP_get0_cofactor(a); | |
538 | bc = EC_GROUP_get0_cofactor(b); | |
539 | if (ao == NULL || bo == NULL) { | |
0f113f3e | 540 | BN_CTX_end(ctx); |
23a1d5e9 | 541 | BN_CTX_free(ctx_new); |
0f113f3e MC |
542 | return -1; |
543 | } | |
be2e334f | 544 | if (BN_cmp(ao, bo) || BN_cmp(ac, bc)) |
0f113f3e MC |
545 | r = 1; |
546 | } | |
547 | ||
548 | BN_CTX_end(ctx); | |
23a1d5e9 | 549 | BN_CTX_free(ctx_new); |
ada0e717 | 550 | |
0f113f3e MC |
551 | return r; |
552 | } | |
ada0e717 | 553 | |
0657bf9c BM |
554 | /* functions for EC_POINT objects */ |
555 | ||
556 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
557 | { |
558 | EC_POINT *ret; | |
559 | ||
560 | if (group == NULL) { | |
561 | ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); | |
562 | return NULL; | |
563 | } | |
f06080cb | 564 | if (group->meth->point_init == NULL) { |
0f113f3e MC |
565 | ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
566 | return NULL; | |
567 | } | |
568 | ||
1b4cf96f | 569 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
570 | if (ret == NULL) { |
571 | ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); | |
572 | return NULL; | |
573 | } | |
574 | ||
575 | ret->meth = group->meth; | |
b14e6015 | 576 | ret->curve_name = group->curve_name; |
0f113f3e MC |
577 | |
578 | if (!ret->meth->point_init(ret)) { | |
579 | OPENSSL_free(ret); | |
580 | return NULL; | |
581 | } | |
582 | ||
583 | return ret; | |
584 | } | |
0657bf9c BM |
585 | |
586 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e MC |
587 | { |
588 | if (!point) | |
589 | return; | |
7711de24 | 590 | |
0f113f3e MC |
591 | if (point->meth->point_finish != 0) |
592 | point->meth->point_finish(point); | |
593 | OPENSSL_free(point); | |
594 | } | |
0657bf9c BM |
595 | |
596 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e MC |
597 | { |
598 | if (!point) | |
599 | return; | |
600 | ||
601 | if (point->meth->point_clear_finish != 0) | |
602 | point->meth->point_clear_finish(point); | |
603 | else if (point->meth->point_finish != 0) | |
604 | point->meth->point_finish(point); | |
b4faea50 | 605 | OPENSSL_clear_free(point, sizeof(*point)); |
0f113f3e | 606 | } |
0657bf9c BM |
607 | |
608 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
609 | { |
610 | if (dest->meth->point_copy == 0) { | |
611 | ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
612 | return 0; | |
613 | } | |
b14e6015 MC |
614 | if (dest->meth != src->meth |
615 | || (dest->curve_name != src->curve_name | |
616 | && dest->curve_name != 0 | |
617 | && src->curve_name != 0)) { | |
0f113f3e MC |
618 | ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); |
619 | return 0; | |
620 | } | |
621 | if (dest == src) | |
622 | return 1; | |
623 | return dest->meth->point_copy(dest, src); | |
624 | } | |
0657bf9c | 625 | |
7793f30e | 626 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
627 | { |
628 | EC_POINT *t; | |
629 | int r; | |
630 | ||
631 | if (a == NULL) | |
632 | return NULL; | |
633 | ||
634 | t = EC_POINT_new(group); | |
635 | if (t == NULL) | |
26a7d938 | 636 | return NULL; |
0f113f3e MC |
637 | r = EC_POINT_copy(t, a); |
638 | if (!r) { | |
639 | EC_POINT_free(t); | |
640 | return NULL; | |
8fdc3734 RS |
641 | } |
642 | return t; | |
0f113f3e | 643 | } |
7793f30e | 644 | |
48fe4d62 | 645 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
646 | { |
647 | return point->meth; | |
648 | } | |
48fe4d62 | 649 | |
226cc7de | 650 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
651 | { |
652 | if (group->meth->point_set_to_infinity == 0) { | |
653 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, | |
654 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
655 | return 0; | |
656 | } | |
657 | if (group->meth != point->meth) { | |
658 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); | |
659 | return 0; | |
660 | } | |
661 | return group->meth->point_set_to_infinity(group, point); | |
662 | } | |
663 | ||
664 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
665 | EC_POINT *point, const BIGNUM *x, | |
666 | const BIGNUM *y, const BIGNUM *z, | |
667 | BN_CTX *ctx) | |
668 | { | |
669 | if (group->meth->point_set_Jprojective_coordinates_GFp == 0) { | |
670 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, | |
671 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
672 | return 0; | |
673 | } | |
b14e6015 | 674 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
675 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
676 | EC_R_INCOMPATIBLE_OBJECTS); | |
677 | return 0; | |
678 | } | |
679 | return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, | |
680 | y, z, ctx); | |
681 | } | |
682 | ||
683 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
684 | const EC_POINT *point, BIGNUM *x, | |
685 | BIGNUM *y, BIGNUM *z, | |
686 | BN_CTX *ctx) | |
687 | { | |
688 | if (group->meth->point_get_Jprojective_coordinates_GFp == 0) { | |
689 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, | |
690 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
691 | return 0; | |
692 | } | |
b14e6015 | 693 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
694 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
695 | EC_R_INCOMPATIBLE_OBJECTS); | |
696 | return 0; | |
697 | } | |
698 | return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, | |
699 | y, z, ctx); | |
700 | } | |
701 | ||
702 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, | |
703 | EC_POINT *point, const BIGNUM *x, | |
704 | const BIGNUM *y, BN_CTX *ctx) | |
705 | { | |
706 | if (group->meth->point_set_affine_coordinates == 0) { | |
707 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, | |
708 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
709 | return 0; | |
710 | } | |
b14e6015 | 711 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
712 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, |
713 | EC_R_INCOMPATIBLE_OBJECTS); | |
714 | return 0; | |
715 | } | |
1e2012b7 EK |
716 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
717 | return 0; | |
718 | ||
719 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
720 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, | |
721 | EC_R_POINT_IS_NOT_ON_CURVE); | |
722 | return 0; | |
723 | } | |
724 | return 1; | |
0f113f3e | 725 | } |
226cc7de | 726 | |
b3310161 | 727 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
728 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
729 | EC_POINT *point, const BIGNUM *x, | |
730 | const BIGNUM *y, BN_CTX *ctx) | |
731 | { | |
732 | if (group->meth->point_set_affine_coordinates == 0) { | |
733 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, | |
734 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
735 | return 0; | |
736 | } | |
b14e6015 | 737 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
738 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, |
739 | EC_R_INCOMPATIBLE_OBJECTS); | |
740 | return 0; | |
741 | } | |
1e2012b7 EK |
742 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
743 | return 0; | |
744 | ||
745 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
746 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, | |
747 | EC_R_POINT_IS_NOT_ON_CURVE); | |
748 | return 0; | |
749 | } | |
750 | return 1; | |
0f113f3e | 751 | } |
b3310161 | 752 | #endif |
7793f30e | 753 | |
0f113f3e MC |
754 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
755 | const EC_POINT *point, BIGNUM *x, | |
756 | BIGNUM *y, BN_CTX *ctx) | |
757 | { | |
758 | if (group->meth->point_get_affine_coordinates == 0) { | |
759 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, | |
760 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
761 | return 0; | |
762 | } | |
b14e6015 | 763 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
764 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, |
765 | EC_R_INCOMPATIBLE_OBJECTS); | |
766 | return 0; | |
767 | } | |
768 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); | |
769 | } | |
7793f30e | 770 | |
b3310161 | 771 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
772 | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, |
773 | const EC_POINT *point, BIGNUM *x, | |
774 | BIGNUM *y, BN_CTX *ctx) | |
775 | { | |
776 | if (group->meth->point_get_affine_coordinates == 0) { | |
777 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, | |
778 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
779 | return 0; | |
780 | } | |
b14e6015 | 781 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
782 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, |
783 | EC_R_INCOMPATIBLE_OBJECTS); | |
784 | return 0; | |
785 | } | |
786 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); | |
787 | } | |
b3310161 | 788 | #endif |
7793f30e | 789 | |
0f113f3e MC |
790 | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
791 | const EC_POINT *b, BN_CTX *ctx) | |
792 | { | |
793 | if (group->meth->add == 0) { | |
794 | ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
795 | return 0; | |
796 | } | |
b14e6015 MC |
797 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group) |
798 | || !ec_point_is_compat(b, group)) { | |
0f113f3e MC |
799 | ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS); |
800 | return 0; | |
801 | } | |
802 | return group->meth->add(group, r, a, b, ctx); | |
803 | } | |
804 | ||
805 | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | |
806 | BN_CTX *ctx) | |
807 | { | |
808 | if (group->meth->dbl == 0) { | |
809 | ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
810 | return 0; | |
811 | } | |
b14e6015 | 812 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { |
0f113f3e MC |
813 | ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS); |
814 | return 0; | |
815 | } | |
816 | return group->meth->dbl(group, r, a, ctx); | |
817 | } | |
0657bf9c | 818 | |
1d5bd6cf | 819 | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) |
0f113f3e MC |
820 | { |
821 | if (group->meth->invert == 0) { | |
822 | ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
823 | return 0; | |
824 | } | |
b14e6015 | 825 | if (!ec_point_is_compat(a, group)) { |
0f113f3e MC |
826 | ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS); |
827 | return 0; | |
828 | } | |
829 | return group->meth->invert(group, a, ctx); | |
830 | } | |
1d5bd6cf | 831 | |
0657bf9c | 832 | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
0f113f3e MC |
833 | { |
834 | if (group->meth->is_at_infinity == 0) { | |
835 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, | |
836 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
837 | return 0; | |
838 | } | |
b14e6015 | 839 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
840 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); |
841 | return 0; | |
842 | } | |
843 | return group->meth->is_at_infinity(group, point); | |
844 | } | |
845 | ||
68886be7 MC |
846 | /* |
847 | * Check whether an EC_POINT is on the curve or not. Note that the return | |
848 | * value for this function should NOT be treated as a boolean. Return values: | |
849 | * 1: The point is on the curve | |
850 | * 0: The point is not on the curve | |
851 | * -1: An error occurred | |
852 | */ | |
0f113f3e MC |
853 | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
854 | BN_CTX *ctx) | |
855 | { | |
856 | if (group->meth->is_on_curve == 0) { | |
857 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
858 | return 0; | |
859 | } | |
b14e6015 | 860 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
861 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS); |
862 | return 0; | |
863 | } | |
864 | return group->meth->is_on_curve(group, point, ctx); | |
865 | } | |
866 | ||
867 | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, | |
868 | BN_CTX *ctx) | |
869 | { | |
870 | if (group->meth->point_cmp == 0) { | |
871 | ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
872 | return -1; | |
873 | } | |
b14e6015 | 874 | if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { |
0f113f3e MC |
875 | ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); |
876 | return -1; | |
877 | } | |
878 | return group->meth->point_cmp(group, a, b, ctx); | |
879 | } | |
1d5bd6cf | 880 | |
e869d4bd | 881 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
882 | { |
883 | if (group->meth->make_affine == 0) { | |
884 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
885 | return 0; | |
886 | } | |
b14e6015 | 887 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
888 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
889 | return 0; | |
890 | } | |
891 | return group->meth->make_affine(group, point, ctx); | |
892 | } | |
893 | ||
894 | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, | |
895 | EC_POINT *points[], BN_CTX *ctx) | |
896 | { | |
897 | size_t i; | |
898 | ||
899 | if (group->meth->points_make_affine == 0) { | |
900 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
901 | return 0; | |
902 | } | |
903 | for (i = 0; i < num; i++) { | |
b14e6015 | 904 | if (!ec_point_is_compat(points[i], group)) { |
0f113f3e MC |
905 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
906 | return 0; | |
907 | } | |
908 | } | |
909 | return group->meth->points_make_affine(group, num, points, ctx); | |
910 | } | |
911 | ||
912 | /* | |
913 | * Functions for point multiplication. If group->meth->mul is 0, we use the | |
914 | * wNAF-based implementations in ec_mult.c; otherwise we dispatch through | |
915 | * methods. | |
37c660ff BM |
916 | */ |
917 | ||
918 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, | |
0f113f3e MC |
919 | size_t num, const EC_POINT *points[], |
920 | const BIGNUM *scalars[], BN_CTX *ctx) | |
921 | { | |
922 | if (group->meth->mul == 0) | |
923 | /* use default */ | |
924 | return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); | |
37c660ff | 925 | |
0f113f3e MC |
926 | return group->meth->mul(group, r, scalar, num, points, scalars, ctx); |
927 | } | |
37c660ff BM |
928 | |
929 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
930 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
931 | { | |
932 | /* just a convenient interface to EC_POINTs_mul() */ | |
37c660ff | 933 | |
0f113f3e MC |
934 | const EC_POINT *points[1]; |
935 | const BIGNUM *scalars[1]; | |
37c660ff | 936 | |
0f113f3e MC |
937 | points[0] = point; |
938 | scalars[0] = p_scalar; | |
37c660ff | 939 | |
0f113f3e MC |
940 | return EC_POINTs_mul(group, r, g_scalar, |
941 | (point != NULL | |
942 | && p_scalar != NULL), points, scalars, ctx); | |
943 | } | |
37c660ff BM |
944 | |
945 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) | |
0f113f3e MC |
946 | { |
947 | if (group->meth->mul == 0) | |
948 | /* use default */ | |
949 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 950 | |
0f113f3e MC |
951 | if (group->meth->precompute_mult != 0) |
952 | return group->meth->precompute_mult(group, ctx); | |
953 | else | |
954 | return 1; /* nothing to do, so report success */ | |
955 | } | |
37c660ff BM |
956 | |
957 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
958 | { |
959 | if (group->meth->mul == 0) | |
960 | /* use default */ | |
961 | return ec_wNAF_have_precompute_mult(group); | |
962 | ||
963 | if (group->meth->have_precompute_mult != 0) | |
964 | return group->meth->have_precompute_mult(group); | |
965 | else | |
966 | return 0; /* cannot tell whether precomputation has | |
967 | * been performed */ | |
968 | } | |
969 | ||
970 | /* | |
971 | * ec_precompute_mont_data sets |group->mont_data| from |group->order| and | |
972 | * returns one on success. On error it returns zero. | |
973 | */ | |
eb791696 | 974 | static int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e MC |
975 | { |
976 | BN_CTX *ctx = BN_CTX_new(); | |
977 | int ret = 0; | |
978 | ||
23a1d5e9 RS |
979 | BN_MONT_CTX_free(group->mont_data); |
980 | group->mont_data = NULL; | |
0f113f3e MC |
981 | |
982 | if (ctx == NULL) | |
983 | goto err; | |
984 | ||
985 | group->mont_data = BN_MONT_CTX_new(); | |
90945fa3 | 986 | if (group->mont_data == NULL) |
0f113f3e MC |
987 | goto err; |
988 | ||
989 | if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | |
990 | BN_MONT_CTX_free(group->mont_data); | |
991 | group->mont_data = NULL; | |
992 | goto err; | |
993 | } | |
994 | ||
995 | ret = 1; | |
996 | ||
997 | err: | |
998 | ||
23a1d5e9 | 999 | BN_CTX_free(ctx); |
0f113f3e MC |
1000 | return ret; |
1001 | } | |
3aef36ff RS |
1002 | |
1003 | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg) | |
1004 | { | |
1005 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
1006 | } | |
1007 | ||
1008 | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx) | |
1009 | { | |
1010 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
1011 | } | |
77470e98 DSH |
1012 | |
1013 | int ec_group_simple_order_bits(const EC_GROUP *group) | |
1014 | { | |
1015 | if (group->order == NULL) | |
1016 | return 0; | |
1017 | return BN_num_bits(group->order); | |
1018 | } | |
eb791696 AP |
1019 | |
1020 | int EC_GROUP_do_inverse_ord(const EC_GROUP *group, BIGNUM *res, | |
1021 | BIGNUM *x, BN_CTX *ctx) | |
1022 | { | |
1023 | if (group->meth->field_inverse_mod_ord != NULL) | |
1024 | return group->meth->field_inverse_mod_ord(group, res, x, ctx); | |
1025 | else | |
1026 | return 0; | |
1027 | } |