]>
Commit | Line | Data |
---|---|---|
65e81670 | 1 | /* crypto/ec/ec_lib.c */ |
35b73a1f BM |
2 | /* |
3 | * Originally written by Bodo Moeller for the OpenSSL project. | |
4 | */ | |
65e81670 | 5 | /* ==================================================================== |
37c660ff | 6 | * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. |
65e81670 BM |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
65e81670 BM |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * openssl-core@openssl.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
7793f30e BM |
58 | /* ==================================================================== |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 60 | * Binary polynomial ECC support in OpenSSL originally developed by |
7793f30e BM |
61 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
62 | */ | |
65e81670 | 63 | |
c4b36ff4 BM |
64 | #include <string.h> |
65 | ||
0657bf9c | 66 | #include <openssl/err.h> |
bb62a8b0 | 67 | #include <openssl/opensslv.h> |
0657bf9c | 68 | |
65e81670 | 69 | #include "ec_lcl.h" |
0657bf9c | 70 | |
b597aab8 | 71 | const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT; |
bb62a8b0 | 72 | |
0657bf9c BM |
73 | /* functions for EC_GROUP objects */ |
74 | ||
75 | EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) | |
0f113f3e MC |
76 | { |
77 | EC_GROUP *ret; | |
78 | ||
79 | if (meth == NULL) { | |
80 | ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL); | |
81 | return NULL; | |
82 | } | |
83 | if (meth->group_init == 0) { | |
84 | ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | ret = OPENSSL_malloc(sizeof *ret); | |
89 | if (ret == NULL) { | |
90 | ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE); | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | ret->meth = meth; | |
95 | ||
96 | ret->extra_data = NULL; | |
97 | ret->mont_data = NULL; | |
98 | ||
99 | ret->generator = NULL; | |
100 | ret->order = BN_new(); | |
101 | ret->cofactor = NULL; | |
102 | if (!ret->order) | |
103 | goto err; | |
104 | ret->cofactor = BN_new(); | |
105 | if (!ret->cofactor) | |
106 | goto err; | |
107 | ||
108 | ret->curve_name = 0; | |
109 | ret->asn1_flag = 0; | |
110 | ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED; | |
111 | ||
112 | ret->seed = NULL; | |
113 | ret->seed_len = 0; | |
114 | ||
115 | if (!meth->group_init(ret)) | |
116 | goto err; | |
117 | ||
118 | return ret; | |
119 | err: | |
120 | if (ret->order) | |
121 | BN_free(ret->order); | |
122 | if (ret->cofactor) | |
123 | BN_free(ret->cofactor); | |
124 | OPENSSL_free(ret); | |
125 | return NULL; | |
126 | } | |
0657bf9c | 127 | |
0657bf9c | 128 | void EC_GROUP_free(EC_GROUP *group) |
0f113f3e MC |
129 | { |
130 | if (!group) | |
131 | return; | |
7711de24 | 132 | |
0f113f3e MC |
133 | if (group->meth->group_finish != 0) |
134 | group->meth->group_finish(group); | |
df9cc153 | 135 | |
0f113f3e | 136 | EC_EX_DATA_free_all_data(&group->extra_data); |
df9cc153 | 137 | |
0f113f3e MC |
138 | if (group->mont_data) |
139 | BN_MONT_CTX_free(group->mont_data); | |
f54be179 | 140 | |
0f113f3e MC |
141 | if (group->generator != NULL) |
142 | EC_POINT_free(group->generator); | |
143 | BN_free(group->order); | |
144 | BN_free(group->cofactor); | |
b6db386f | 145 | |
0f113f3e MC |
146 | if (group->seed) |
147 | OPENSSL_free(group->seed); | |
458c2917 | 148 | |
0f113f3e MC |
149 | OPENSSL_free(group); |
150 | } | |
0657bf9c BM |
151 | |
152 | void EC_GROUP_clear_free(EC_GROUP *group) | |
0f113f3e MC |
153 | { |
154 | if (!group) | |
155 | return; | |
df9cc153 | 156 | |
0f113f3e MC |
157 | if (group->meth->group_clear_finish != 0) |
158 | group->meth->group_clear_finish(group); | |
159 | else if (group->meth->group_finish != 0) | |
160 | group->meth->group_finish(group); | |
df9cc153 | 161 | |
0f113f3e | 162 | EC_EX_DATA_clear_free_all_data(&group->extra_data); |
f54be179 | 163 | |
0f113f3e MC |
164 | if (group->mont_data) |
165 | BN_MONT_CTX_free(group->mont_data); | |
b6db386f | 166 | |
0f113f3e MC |
167 | if (group->generator != NULL) |
168 | EC_POINT_clear_free(group->generator); | |
169 | BN_clear_free(group->order); | |
170 | BN_clear_free(group->cofactor); | |
458c2917 | 171 | |
0f113f3e MC |
172 | if (group->seed) { |
173 | OPENSSL_cleanse(group->seed, group->seed_len); | |
174 | OPENSSL_free(group->seed); | |
175 | } | |
0657bf9c | 176 | |
0f113f3e MC |
177 | OPENSSL_cleanse(group, sizeof *group); |
178 | OPENSSL_free(group); | |
179 | } | |
0657bf9c BM |
180 | |
181 | int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) | |
0f113f3e MC |
182 | { |
183 | EC_EXTRA_DATA *d; | |
184 | ||
185 | if (dest->meth->group_copy == 0) { | |
186 | ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
187 | return 0; | |
188 | } | |
189 | if (dest->meth != src->meth) { | |
190 | ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS); | |
191 | return 0; | |
192 | } | |
193 | if (dest == src) | |
194 | return 1; | |
195 | ||
196 | EC_EX_DATA_free_all_data(&dest->extra_data); | |
197 | ||
198 | for (d = src->extra_data; d != NULL; d = d->next) { | |
199 | void *t = d->dup_func(d->data); | |
200 | ||
201 | if (t == NULL) | |
202 | return 0; | |
203 | if (!EC_EX_DATA_set_data | |
204 | (&dest->extra_data, t, d->dup_func, d->free_func, | |
205 | d->clear_free_func)) | |
206 | return 0; | |
207 | } | |
208 | ||
209 | if (src->mont_data != NULL) { | |
210 | if (dest->mont_data == NULL) { | |
211 | dest->mont_data = BN_MONT_CTX_new(); | |
212 | if (dest->mont_data == NULL) | |
213 | return 0; | |
214 | } | |
215 | if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) | |
216 | return 0; | |
217 | } else { | |
218 | /* src->generator == NULL */ | |
219 | if (dest->mont_data != NULL) { | |
220 | BN_MONT_CTX_free(dest->mont_data); | |
221 | dest->mont_data = NULL; | |
222 | } | |
223 | } | |
0657bf9c | 224 | |
0f113f3e MC |
225 | if (src->generator != NULL) { |
226 | if (dest->generator == NULL) { | |
227 | dest->generator = EC_POINT_new(dest); | |
228 | if (dest->generator == NULL) | |
229 | return 0; | |
230 | } | |
231 | if (!EC_POINT_copy(dest->generator, src->generator)) | |
232 | return 0; | |
233 | } else { | |
234 | /* src->generator == NULL */ | |
235 | if (dest->generator != NULL) { | |
236 | EC_POINT_clear_free(dest->generator); | |
237 | dest->generator = NULL; | |
238 | } | |
239 | } | |
240 | ||
241 | if (!BN_copy(dest->order, src->order)) | |
242 | return 0; | |
243 | if (!BN_copy(dest->cofactor, src->cofactor)) | |
244 | return 0; | |
245 | ||
246 | dest->curve_name = src->curve_name; | |
247 | dest->asn1_flag = src->asn1_flag; | |
248 | dest->asn1_form = src->asn1_form; | |
249 | ||
250 | if (src->seed) { | |
251 | if (dest->seed) | |
252 | OPENSSL_free(dest->seed); | |
253 | dest->seed = OPENSSL_malloc(src->seed_len); | |
254 | if (dest->seed == NULL) | |
255 | return 0; | |
256 | if (!memcpy(dest->seed, src->seed, src->seed_len)) | |
257 | return 0; | |
258 | dest->seed_len = src->seed_len; | |
259 | } else { | |
260 | if (dest->seed) | |
261 | OPENSSL_free(dest->seed); | |
262 | dest->seed = NULL; | |
263 | dest->seed_len = 0; | |
264 | } | |
265 | ||
266 | return dest->meth->group_copy(dest, src); | |
267 | } | |
0657bf9c | 268 | |
7793f30e | 269 | EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) |
0f113f3e MC |
270 | { |
271 | EC_GROUP *t = NULL; | |
272 | int ok = 0; | |
7793f30e | 273 | |
0f113f3e MC |
274 | if (a == NULL) |
275 | return NULL; | |
7793f30e | 276 | |
0f113f3e MC |
277 | if ((t = EC_GROUP_new(a->meth)) == NULL) |
278 | return (NULL); | |
279 | if (!EC_GROUP_copy(t, a)) | |
280 | goto err; | |
7793f30e | 281 | |
0f113f3e | 282 | ok = 1; |
7793f30e | 283 | |
0f113f3e MC |
284 | err: |
285 | if (!ok) { | |
286 | if (t) | |
287 | EC_GROUP_free(t); | |
288 | return NULL; | |
289 | } else | |
290 | return t; | |
291 | } | |
7793f30e | 292 | |
48fe4d62 | 293 | const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) |
0f113f3e MC |
294 | { |
295 | return group->meth; | |
296 | } | |
48fe4d62 | 297 | |
458c2917 | 298 | int EC_METHOD_get_field_type(const EC_METHOD *meth) |
0f113f3e MC |
299 | { |
300 | return meth->field_type; | |
301 | } | |
302 | ||
303 | int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, | |
304 | const BIGNUM *order, const BIGNUM *cofactor) | |
305 | { | |
306 | if (generator == NULL) { | |
307 | ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | if (group->generator == NULL) { | |
312 | group->generator = EC_POINT_new(group); | |
313 | if (group->generator == NULL) | |
314 | return 0; | |
315 | } | |
316 | if (!EC_POINT_copy(group->generator, generator)) | |
317 | return 0; | |
318 | ||
319 | if (order != NULL) { | |
320 | if (!BN_copy(group->order, order)) | |
321 | return 0; | |
322 | } else | |
323 | BN_zero(group->order); | |
324 | ||
325 | if (cofactor != NULL) { | |
326 | if (!BN_copy(group->cofactor, cofactor)) | |
327 | return 0; | |
328 | } else | |
329 | BN_zero(group->cofactor); | |
330 | ||
331 | /* | |
332 | * We ignore the return value because some groups have an order with | |
333 | * factors of two, which makes the Montgomery setup fail. | |
334 | * |group->mont_data| will be NULL in this case. | |
335 | */ | |
336 | ec_precompute_mont_data(group); | |
337 | ||
338 | return 1; | |
339 | } | |
bb62a8b0 | 340 | |
9dd84053 | 341 | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) |
0f113f3e MC |
342 | { |
343 | return group->generator; | |
344 | } | |
bb62a8b0 | 345 | |
f54be179 | 346 | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group) |
0f113f3e MC |
347 | { |
348 | return group->mont_data; | |
349 | } | |
bb62a8b0 | 350 | |
b6db386f | 351 | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) |
0f113f3e MC |
352 | { |
353 | if (!BN_copy(order, group->order)) | |
354 | return 0; | |
0657bf9c | 355 | |
0f113f3e MC |
356 | return !BN_is_zero(order); |
357 | } | |
0657bf9c | 358 | |
0f113f3e MC |
359 | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
360 | BN_CTX *ctx) | |
361 | { | |
362 | if (!BN_copy(cofactor, group->cofactor)) | |
363 | return 0; | |
48fe4d62 | 364 | |
0f113f3e MC |
365 | return !BN_is_zero(group->cofactor); |
366 | } | |
48fe4d62 | 367 | |
7dc17a6c | 368 | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) |
0f113f3e MC |
369 | { |
370 | group->curve_name = nid; | |
371 | } | |
b6db386f | 372 | |
7dc17a6c | 373 | int EC_GROUP_get_curve_name(const EC_GROUP *group) |
0f113f3e MC |
374 | { |
375 | return group->curve_name; | |
376 | } | |
b6db386f | 377 | |
458c2917 | 378 | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) |
0f113f3e MC |
379 | { |
380 | group->asn1_flag = flag; | |
381 | } | |
458c2917 BM |
382 | |
383 | int EC_GROUP_get_asn1_flag(const EC_GROUP *group) | |
0f113f3e MC |
384 | { |
385 | return group->asn1_flag; | |
386 | } | |
458c2917 | 387 | |
0f113f3e | 388 | void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
254ef80d | 389 | point_conversion_form_t form) |
0f113f3e MC |
390 | { |
391 | group->asn1_form = form; | |
392 | } | |
254ef80d | 393 | |
0f113f3e MC |
394 | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP |
395 | *group) | |
396 | { | |
397 | return group->asn1_form; | |
398 | } | |
254ef80d | 399 | |
5f3d6f70 | 400 | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) |
0f113f3e MC |
401 | { |
402 | if (group->seed) { | |
403 | OPENSSL_free(group->seed); | |
404 | group->seed = NULL; | |
405 | group->seed_len = 0; | |
406 | } | |
5f3d6f70 | 407 | |
0f113f3e MC |
408 | if (!len || !p) |
409 | return 1; | |
5f3d6f70 | 410 | |
0f113f3e MC |
411 | if ((group->seed = OPENSSL_malloc(len)) == NULL) |
412 | return 0; | |
413 | memcpy(group->seed, p, len); | |
414 | group->seed_len = len; | |
5f3d6f70 | 415 | |
0f113f3e MC |
416 | return len; |
417 | } | |
5f3d6f70 BM |
418 | |
419 | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) | |
0f113f3e MC |
420 | { |
421 | return group->seed; | |
422 | } | |
5f3d6f70 BM |
423 | |
424 | size_t EC_GROUP_get_seed_len(const EC_GROUP *group) | |
0f113f3e MC |
425 | { |
426 | return group->seed_len; | |
427 | } | |
428 | ||
429 | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, | |
430 | const BIGNUM *b, BN_CTX *ctx) | |
431 | { | |
432 | if (group->meth->group_set_curve == 0) { | |
433 | ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
434 | return 0; | |
435 | } | |
436 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
437 | } | |
438 | ||
439 | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
440 | BIGNUM *b, BN_CTX *ctx) | |
441 | { | |
442 | if (group->meth->group_get_curve == 0) { | |
443 | ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
444 | return 0; | |
445 | } | |
446 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
447 | } | |
7793f30e | 448 | |
b3310161 | 449 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
450 | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
451 | const BIGNUM *b, BN_CTX *ctx) | |
452 | { | |
453 | if (group->meth->group_set_curve == 0) { | |
454 | ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, | |
455 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
456 | return 0; | |
457 | } | |
458 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
459 | } | |
460 | ||
461 | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
462 | BIGNUM *b, BN_CTX *ctx) | |
463 | { | |
464 | if (group->meth->group_get_curve == 0) { | |
465 | ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, | |
466 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
467 | return 0; | |
468 | } | |
469 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
470 | } | |
b3310161 | 471 | #endif |
7793f30e BM |
472 | |
473 | int EC_GROUP_get_degree(const EC_GROUP *group) | |
0f113f3e MC |
474 | { |
475 | if (group->meth->group_get_degree == 0) { | |
476 | ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
477 | return 0; | |
478 | } | |
479 | return group->meth->group_get_degree(group); | |
480 | } | |
48fe4d62 | 481 | |
17d6bb81 | 482 | int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
0f113f3e MC |
483 | { |
484 | if (group->meth->group_check_discriminant == 0) { | |
485 | ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, | |
486 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
487 | return 0; | |
488 | } | |
489 | return group->meth->group_check_discriminant(group, ctx); | |
490 | } | |
af28dd6c | 491 | |
ada0e717 | 492 | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) |
0f113f3e MC |
493 | { |
494 | int r = 0; | |
495 | BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; | |
496 | BN_CTX *ctx_new = NULL; | |
497 | ||
498 | /* compare the field types */ | |
499 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) != | |
500 | EC_METHOD_get_field_type(EC_GROUP_method_of(b))) | |
501 | return 1; | |
502 | /* compare the curve name (if present in both) */ | |
503 | if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && | |
504 | EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b)) | |
505 | return 1; | |
506 | ||
507 | if (!ctx) | |
508 | ctx_new = ctx = BN_CTX_new(); | |
509 | if (!ctx) | |
510 | return -1; | |
511 | ||
512 | BN_CTX_start(ctx); | |
513 | a1 = BN_CTX_get(ctx); | |
514 | a2 = BN_CTX_get(ctx); | |
515 | a3 = BN_CTX_get(ctx); | |
516 | b1 = BN_CTX_get(ctx); | |
517 | b2 = BN_CTX_get(ctx); | |
518 | b3 = BN_CTX_get(ctx); | |
519 | if (!b3) { | |
520 | BN_CTX_end(ctx); | |
521 | if (ctx_new) | |
522 | BN_CTX_free(ctx); | |
523 | return -1; | |
524 | } | |
525 | ||
526 | /* | |
527 | * XXX This approach assumes that the external representation of curves | |
528 | * over the same field type is the same. | |
529 | */ | |
530 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
531 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
532 | r = 1; | |
533 | ||
534 | if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3)) | |
535 | r = 1; | |
536 | ||
537 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ | |
538 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), | |
539 | EC_GROUP_get0_generator(b), ctx)) | |
540 | r = 1; | |
541 | ||
542 | if (!r) { | |
543 | /* compare the order and cofactor */ | |
544 | if (!EC_GROUP_get_order(a, a1, ctx) || | |
545 | !EC_GROUP_get_order(b, b1, ctx) || | |
546 | !EC_GROUP_get_cofactor(a, a2, ctx) || | |
547 | !EC_GROUP_get_cofactor(b, b2, ctx)) { | |
548 | BN_CTX_end(ctx); | |
549 | if (ctx_new) | |
550 | BN_CTX_free(ctx); | |
551 | return -1; | |
552 | } | |
553 | if (BN_cmp(a1, b1) || BN_cmp(a2, b2)) | |
554 | r = 1; | |
555 | } | |
556 | ||
557 | BN_CTX_end(ctx); | |
558 | if (ctx_new) | |
559 | BN_CTX_free(ctx); | |
ada0e717 | 560 | |
0f113f3e MC |
561 | return r; |
562 | } | |
ada0e717 | 563 | |
df9cc153 | 564 | /* this has 'package' visibility */ |
9dd84053 | 565 | int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data, |
0f113f3e MC |
566 | void *(*dup_func) (void *), |
567 | void (*free_func) (void *), | |
568 | void (*clear_free_func) (void *)) | |
569 | { | |
570 | EC_EXTRA_DATA *d; | |
571 | ||
572 | if (ex_data == NULL) | |
573 | return 0; | |
574 | ||
575 | for (d = *ex_data; d != NULL; d = d->next) { | |
576 | if (d->dup_func == dup_func && d->free_func == free_func | |
577 | && d->clear_free_func == clear_free_func) { | |
578 | ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL); | |
579 | return 0; | |
580 | } | |
581 | } | |
df9cc153 | 582 | |
0f113f3e MC |
583 | if (data == NULL) |
584 | /* no explicit entry needed */ | |
585 | return 1; | |
ba729265 | 586 | |
0f113f3e MC |
587 | d = OPENSSL_malloc(sizeof *d); |
588 | if (d == NULL) | |
589 | return 0; | |
ba729265 | 590 | |
0f113f3e MC |
591 | d->data = data; |
592 | d->dup_func = dup_func; | |
593 | d->free_func = free_func; | |
594 | d->clear_free_func = clear_free_func; | |
ba729265 | 595 | |
0f113f3e MC |
596 | d->next = *ex_data; |
597 | *ex_data = d; | |
ba729265 | 598 | |
0f113f3e MC |
599 | return 1; |
600 | } | |
df9cc153 | 601 | |
df9cc153 | 602 | /* this has 'package' visibility */ |
9dd84053 | 603 | void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data, |
0f113f3e MC |
604 | void *(*dup_func) (void *), |
605 | void (*free_func) (void *), | |
606 | void (*clear_free_func) (void *)) | |
607 | { | |
608 | const EC_EXTRA_DATA *d; | |
609 | ||
610 | for (d = ex_data; d != NULL; d = d->next) { | |
611 | if (d->dup_func == dup_func && d->free_func == free_func | |
612 | && d->clear_free_func == clear_free_func) | |
613 | return d->data; | |
614 | } | |
615 | ||
616 | return NULL; | |
617 | } | |
df9cc153 | 618 | |
ba729265 | 619 | /* this has 'package' visibility */ |
9dd84053 | 620 | void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data, |
0f113f3e MC |
621 | void *(*dup_func) (void *), |
622 | void (*free_func) (void *), | |
623 | void (*clear_free_func) (void *)) | |
624 | { | |
625 | EC_EXTRA_DATA **p; | |
626 | ||
627 | if (ex_data == NULL) | |
628 | return; | |
629 | ||
630 | for (p = ex_data; *p != NULL; p = &((*p)->next)) { | |
631 | if ((*p)->dup_func == dup_func && (*p)->free_func == free_func | |
632 | && (*p)->clear_free_func == clear_free_func) { | |
633 | EC_EXTRA_DATA *next = (*p)->next; | |
634 | ||
635 | (*p)->free_func((*p)->data); | |
636 | OPENSSL_free(*p); | |
637 | ||
638 | *p = next; | |
639 | return; | |
640 | } | |
641 | } | |
642 | } | |
df9cc153 | 643 | |
ba729265 | 644 | /* this has 'package' visibility */ |
bbbd6710 | 645 | void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data, |
0f113f3e MC |
646 | void *(*dup_func) (void *), |
647 | void (*free_func) (void *), | |
648 | void (*clear_free_func) (void *)) | |
649 | { | |
650 | EC_EXTRA_DATA **p; | |
651 | ||
652 | if (ex_data == NULL) | |
653 | return; | |
654 | ||
655 | for (p = ex_data; *p != NULL; p = &((*p)->next)) { | |
656 | if ((*p)->dup_func == dup_func && (*p)->free_func == free_func | |
657 | && (*p)->clear_free_func == clear_free_func) { | |
658 | EC_EXTRA_DATA *next = (*p)->next; | |
659 | ||
660 | (*p)->clear_free_func((*p)->data); | |
661 | OPENSSL_free(*p); | |
662 | ||
663 | *p = next; | |
664 | return; | |
665 | } | |
666 | } | |
667 | } | |
df9cc153 BM |
668 | |
669 | /* this has 'package' visibility */ | |
9dd84053 | 670 | void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data) |
0f113f3e MC |
671 | { |
672 | EC_EXTRA_DATA *d; | |
673 | ||
674 | if (ex_data == NULL) | |
675 | return; | |
676 | ||
677 | d = *ex_data; | |
678 | while (d) { | |
679 | EC_EXTRA_DATA *next = d->next; | |
680 | ||
681 | d->free_func(d->data); | |
682 | OPENSSL_free(d); | |
683 | ||
684 | d = next; | |
685 | } | |
686 | *ex_data = NULL; | |
687 | } | |
df9cc153 | 688 | |
df9cc153 | 689 | /* this has 'package' visibility */ |
9dd84053 | 690 | void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data) |
0f113f3e MC |
691 | { |
692 | EC_EXTRA_DATA *d; | |
693 | ||
694 | if (ex_data == NULL) | |
695 | return; | |
696 | ||
697 | d = *ex_data; | |
698 | while (d) { | |
699 | EC_EXTRA_DATA *next = d->next; | |
700 | ||
701 | d->clear_free_func(d->data); | |
702 | OPENSSL_free(d); | |
df9cc153 | 703 | |
0f113f3e MC |
704 | d = next; |
705 | } | |
706 | *ex_data = NULL; | |
707 | } | |
0657bf9c BM |
708 | |
709 | /* functions for EC_POINT objects */ | |
710 | ||
711 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
712 | { |
713 | EC_POINT *ret; | |
714 | ||
715 | if (group == NULL) { | |
716 | ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); | |
717 | return NULL; | |
718 | } | |
719 | if (group->meth->point_init == 0) { | |
720 | ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
721 | return NULL; | |
722 | } | |
723 | ||
724 | ret = OPENSSL_malloc(sizeof *ret); | |
725 | if (ret == NULL) { | |
726 | ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | ret->meth = group->meth; | |
731 | ||
732 | if (!ret->meth->point_init(ret)) { | |
733 | OPENSSL_free(ret); | |
734 | return NULL; | |
735 | } | |
736 | ||
737 | return ret; | |
738 | } | |
0657bf9c BM |
739 | |
740 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e MC |
741 | { |
742 | if (!point) | |
743 | return; | |
7711de24 | 744 | |
0f113f3e MC |
745 | if (point->meth->point_finish != 0) |
746 | point->meth->point_finish(point); | |
747 | OPENSSL_free(point); | |
748 | } | |
0657bf9c BM |
749 | |
750 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e MC |
751 | { |
752 | if (!point) | |
753 | return; | |
754 | ||
755 | if (point->meth->point_clear_finish != 0) | |
756 | point->meth->point_clear_finish(point); | |
757 | else if (point->meth->point_finish != 0) | |
758 | point->meth->point_finish(point); | |
759 | OPENSSL_cleanse(point, sizeof *point); | |
760 | OPENSSL_free(point); | |
761 | } | |
0657bf9c BM |
762 | |
763 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
764 | { |
765 | if (dest->meth->point_copy == 0) { | |
766 | ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
767 | return 0; | |
768 | } | |
769 | if (dest->meth != src->meth) { | |
770 | ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); | |
771 | return 0; | |
772 | } | |
773 | if (dest == src) | |
774 | return 1; | |
775 | return dest->meth->point_copy(dest, src); | |
776 | } | |
0657bf9c | 777 | |
7793f30e | 778 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
779 | { |
780 | EC_POINT *t; | |
781 | int r; | |
782 | ||
783 | if (a == NULL) | |
784 | return NULL; | |
785 | ||
786 | t = EC_POINT_new(group); | |
787 | if (t == NULL) | |
788 | return (NULL); | |
789 | r = EC_POINT_copy(t, a); | |
790 | if (!r) { | |
791 | EC_POINT_free(t); | |
792 | return NULL; | |
793 | } else | |
794 | return t; | |
795 | } | |
7793f30e | 796 | |
48fe4d62 | 797 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
798 | { |
799 | return point->meth; | |
800 | } | |
48fe4d62 | 801 | |
226cc7de | 802 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
803 | { |
804 | if (group->meth->point_set_to_infinity == 0) { | |
805 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, | |
806 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
807 | return 0; | |
808 | } | |
809 | if (group->meth != point->meth) { | |
810 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); | |
811 | return 0; | |
812 | } | |
813 | return group->meth->point_set_to_infinity(group, point); | |
814 | } | |
815 | ||
816 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
817 | EC_POINT *point, const BIGNUM *x, | |
818 | const BIGNUM *y, const BIGNUM *z, | |
819 | BN_CTX *ctx) | |
820 | { | |
821 | if (group->meth->point_set_Jprojective_coordinates_GFp == 0) { | |
822 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, | |
823 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
824 | return 0; | |
825 | } | |
826 | if (group->meth != point->meth) { | |
827 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, | |
828 | EC_R_INCOMPATIBLE_OBJECTS); | |
829 | return 0; | |
830 | } | |
831 | return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, | |
832 | y, z, ctx); | |
833 | } | |
834 | ||
835 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
836 | const EC_POINT *point, BIGNUM *x, | |
837 | BIGNUM *y, BIGNUM *z, | |
838 | BN_CTX *ctx) | |
839 | { | |
840 | if (group->meth->point_get_Jprojective_coordinates_GFp == 0) { | |
841 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, | |
842 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
843 | return 0; | |
844 | } | |
845 | if (group->meth != point->meth) { | |
846 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, | |
847 | EC_R_INCOMPATIBLE_OBJECTS); | |
848 | return 0; | |
849 | } | |
850 | return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, | |
851 | y, z, ctx); | |
852 | } | |
853 | ||
854 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, | |
855 | EC_POINT *point, const BIGNUM *x, | |
856 | const BIGNUM *y, BN_CTX *ctx) | |
857 | { | |
858 | if (group->meth->point_set_affine_coordinates == 0) { | |
859 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, | |
860 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
861 | return 0; | |
862 | } | |
863 | if (group->meth != point->meth) { | |
864 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, | |
865 | EC_R_INCOMPATIBLE_OBJECTS); | |
866 | return 0; | |
867 | } | |
868 | return group->meth->point_set_affine_coordinates(group, point, x, y, ctx); | |
869 | } | |
226cc7de | 870 | |
b3310161 | 871 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
872 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
873 | EC_POINT *point, const BIGNUM *x, | |
874 | const BIGNUM *y, BN_CTX *ctx) | |
875 | { | |
876 | if (group->meth->point_set_affine_coordinates == 0) { | |
877 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, | |
878 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
879 | return 0; | |
880 | } | |
881 | if (group->meth != point->meth) { | |
882 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, | |
883 | EC_R_INCOMPATIBLE_OBJECTS); | |
884 | return 0; | |
885 | } | |
886 | return group->meth->point_set_affine_coordinates(group, point, x, y, ctx); | |
887 | } | |
b3310161 | 888 | #endif |
7793f30e | 889 | |
0f113f3e MC |
890 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
891 | const EC_POINT *point, BIGNUM *x, | |
892 | BIGNUM *y, BN_CTX *ctx) | |
893 | { | |
894 | if (group->meth->point_get_affine_coordinates == 0) { | |
895 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, | |
896 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
897 | return 0; | |
898 | } | |
899 | if (group->meth != point->meth) { | |
900 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, | |
901 | EC_R_INCOMPATIBLE_OBJECTS); | |
902 | return 0; | |
903 | } | |
904 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); | |
905 | } | |
7793f30e | 906 | |
b3310161 | 907 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
908 | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, |
909 | const EC_POINT *point, BIGNUM *x, | |
910 | BIGNUM *y, BN_CTX *ctx) | |
911 | { | |
912 | if (group->meth->point_get_affine_coordinates == 0) { | |
913 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, | |
914 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
915 | return 0; | |
916 | } | |
917 | if (group->meth != point->meth) { | |
918 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, | |
919 | EC_R_INCOMPATIBLE_OBJECTS); | |
920 | return 0; | |
921 | } | |
922 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); | |
923 | } | |
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 | } | |
933 | if ((group->meth != r->meth) || (r->meth != a->meth) | |
934 | || (a->meth != b->meth)) { | |
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 | } | |
948 | if ((group->meth != r->meth) || (r->meth != a->meth)) { | |
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 | } | |
961 | if (group->meth != a->meth) { | |
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 | } | |
975 | if (group->meth != point->meth) { | |
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 | ||
982 | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, | |
983 | BN_CTX *ctx) | |
984 | { | |
985 | if (group->meth->is_on_curve == 0) { | |
986 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
987 | return 0; | |
988 | } | |
989 | if (group->meth != point->meth) { | |
990 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS); | |
991 | return 0; | |
992 | } | |
993 | return group->meth->is_on_curve(group, point, ctx); | |
994 | } | |
995 | ||
996 | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, | |
997 | BN_CTX *ctx) | |
998 | { | |
999 | if (group->meth->point_cmp == 0) { | |
1000 | ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1001 | return -1; | |
1002 | } | |
1003 | if ((group->meth != a->meth) || (a->meth != b->meth)) { | |
1004 | ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); | |
1005 | return -1; | |
1006 | } | |
1007 | return group->meth->point_cmp(group, a, b, ctx); | |
1008 | } | |
1d5bd6cf | 1009 | |
e869d4bd | 1010 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
1011 | { |
1012 | if (group->meth->make_affine == 0) { | |
1013 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1014 | return 0; | |
1015 | } | |
1016 | if (group->meth != point->meth) { | |
1017 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); | |
1018 | return 0; | |
1019 | } | |
1020 | return group->meth->make_affine(group, point, ctx); | |
1021 | } | |
1022 | ||
1023 | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, | |
1024 | EC_POINT *points[], BN_CTX *ctx) | |
1025 | { | |
1026 | size_t i; | |
1027 | ||
1028 | if (group->meth->points_make_affine == 0) { | |
1029 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
1030 | return 0; | |
1031 | } | |
1032 | for (i = 0; i < num; i++) { | |
1033 | if (group->meth != points[i]->meth) { | |
1034 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); | |
1035 | return 0; | |
1036 | } | |
1037 | } | |
1038 | return group->meth->points_make_affine(group, num, points, ctx); | |
1039 | } | |
1040 | ||
1041 | /* | |
1042 | * Functions for point multiplication. If group->meth->mul is 0, we use the | |
1043 | * wNAF-based implementations in ec_mult.c; otherwise we dispatch through | |
1044 | * methods. | |
37c660ff BM |
1045 | */ |
1046 | ||
1047 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, | |
0f113f3e MC |
1048 | size_t num, const EC_POINT *points[], |
1049 | const BIGNUM *scalars[], BN_CTX *ctx) | |
1050 | { | |
1051 | if (group->meth->mul == 0) | |
1052 | /* use default */ | |
1053 | return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); | |
37c660ff | 1054 | |
0f113f3e MC |
1055 | return group->meth->mul(group, r, scalar, num, points, scalars, ctx); |
1056 | } | |
37c660ff BM |
1057 | |
1058 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
1059 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
1060 | { | |
1061 | /* just a convenient interface to EC_POINTs_mul() */ | |
37c660ff | 1062 | |
0f113f3e MC |
1063 | const EC_POINT *points[1]; |
1064 | const BIGNUM *scalars[1]; | |
37c660ff | 1065 | |
0f113f3e MC |
1066 | points[0] = point; |
1067 | scalars[0] = p_scalar; | |
37c660ff | 1068 | |
0f113f3e MC |
1069 | return EC_POINTs_mul(group, r, g_scalar, |
1070 | (point != NULL | |
1071 | && p_scalar != NULL), points, scalars, ctx); | |
1072 | } | |
37c660ff BM |
1073 | |
1074 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) | |
0f113f3e MC |
1075 | { |
1076 | if (group->meth->mul == 0) | |
1077 | /* use default */ | |
1078 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 1079 | |
0f113f3e MC |
1080 | if (group->meth->precompute_mult != 0) |
1081 | return group->meth->precompute_mult(group, ctx); | |
1082 | else | |
1083 | return 1; /* nothing to do, so report success */ | |
1084 | } | |
37c660ff BM |
1085 | |
1086 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
1087 | { |
1088 | if (group->meth->mul == 0) | |
1089 | /* use default */ | |
1090 | return ec_wNAF_have_precompute_mult(group); | |
1091 | ||
1092 | if (group->meth->have_precompute_mult != 0) | |
1093 | return group->meth->have_precompute_mult(group); | |
1094 | else | |
1095 | return 0; /* cannot tell whether precomputation has | |
1096 | * been performed */ | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * ec_precompute_mont_data sets |group->mont_data| from |group->order| and | |
1101 | * returns one on success. On error it returns zero. | |
1102 | */ | |
f54be179 | 1103 | int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e MC |
1104 | { |
1105 | BN_CTX *ctx = BN_CTX_new(); | |
1106 | int ret = 0; | |
1107 | ||
1108 | if (group->mont_data) { | |
1109 | BN_MONT_CTX_free(group->mont_data); | |
1110 | group->mont_data = NULL; | |
1111 | } | |
1112 | ||
1113 | if (ctx == NULL) | |
1114 | goto err; | |
1115 | ||
1116 | group->mont_data = BN_MONT_CTX_new(); | |
1117 | if (!group->mont_data) | |
1118 | goto err; | |
1119 | ||
1120 | if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | |
1121 | BN_MONT_CTX_free(group->mont_data); | |
1122 | group->mont_data = NULL; | |
1123 | goto err; | |
1124 | } | |
1125 | ||
1126 | ret = 1; | |
1127 | ||
1128 | err: | |
1129 | ||
1130 | if (ctx) | |
1131 | BN_CTX_free(ctx); | |
1132 | return ret; | |
1133 | } |