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