]>
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 | * |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
65e81670 | 9 | */ |
aa6bb135 | 10 | |
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; | |
8402cd5f | 287 | } else { |
0f113f3e | 288 | BN_zero(group->order); |
8402cd5f | 289 | } |
0f113f3e | 290 | |
8402cd5f | 291 | /* The cofactor is an optional field, so it should be able to be NULL. */ |
0f113f3e MC |
292 | if (cofactor != NULL) { |
293 | if (!BN_copy(group->cofactor, cofactor)) | |
294 | return 0; | |
8402cd5f | 295 | } else { |
0f113f3e | 296 | BN_zero(group->cofactor); |
8402cd5f | 297 | } |
0f113f3e | 298 | /* |
3a6a4a93 | 299 | * Some groups have an order with |
0f113f3e MC |
300 | * factors of two, which makes the Montgomery setup fail. |
301 | * |group->mont_data| will be NULL in this case. | |
302 | */ | |
3a6a4a93 BB |
303 | if (BN_is_odd(group->order)) { |
304 | return ec_precompute_mont_data(group); | |
305 | } | |
0f113f3e | 306 | |
3a6a4a93 BB |
307 | BN_MONT_CTX_free(group->mont_data); |
308 | group->mont_data = NULL; | |
0f113f3e MC |
309 | return 1; |
310 | } | |
bb62a8b0 | 311 | |
9dd84053 | 312 | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) |
0f113f3e MC |
313 | { |
314 | return group->generator; | |
315 | } | |
bb62a8b0 | 316 | |
f54be179 | 317 | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group) |
0f113f3e MC |
318 | { |
319 | return group->mont_data; | |
320 | } | |
bb62a8b0 | 321 | |
b6db386f | 322 | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) |
0f113f3e | 323 | { |
be2e334f DSH |
324 | if (group->order == NULL) |
325 | return 0; | |
0f113f3e MC |
326 | if (!BN_copy(order, group->order)) |
327 | return 0; | |
0657bf9c | 328 | |
0f113f3e MC |
329 | return !BN_is_zero(order); |
330 | } | |
0657bf9c | 331 | |
be2e334f DSH |
332 | const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group) |
333 | { | |
334 | return group->order; | |
335 | } | |
336 | ||
337 | int EC_GROUP_order_bits(const EC_GROUP *group) | |
338 | { | |
77470e98 | 339 | return group->meth->group_order_bits(group); |
be2e334f DSH |
340 | } |
341 | ||
0f113f3e MC |
342 | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, |
343 | BN_CTX *ctx) | |
344 | { | |
be2e334f DSH |
345 | |
346 | if (group->cofactor == NULL) | |
347 | return 0; | |
0f113f3e MC |
348 | if (!BN_copy(cofactor, group->cofactor)) |
349 | return 0; | |
48fe4d62 | 350 | |
0f113f3e MC |
351 | return !BN_is_zero(group->cofactor); |
352 | } | |
48fe4d62 | 353 | |
be2e334f DSH |
354 | const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group) |
355 | { | |
356 | return group->cofactor; | |
357 | } | |
358 | ||
7dc17a6c | 359 | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) |
0f113f3e MC |
360 | { |
361 | group->curve_name = nid; | |
362 | } | |
b6db386f | 363 | |
7dc17a6c | 364 | int EC_GROUP_get_curve_name(const EC_GROUP *group) |
0f113f3e MC |
365 | { |
366 | return group->curve_name; | |
367 | } | |
b6db386f | 368 | |
fa1f0306 DA |
369 | const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group) |
370 | { | |
371 | return group->field; | |
372 | } | |
373 | ||
458c2917 | 374 | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) |
0f113f3e MC |
375 | { |
376 | group->asn1_flag = flag; | |
377 | } | |
458c2917 BM |
378 | |
379 | int EC_GROUP_get_asn1_flag(const EC_GROUP *group) | |
0f113f3e MC |
380 | { |
381 | return group->asn1_flag; | |
382 | } | |
458c2917 | 383 | |
0f113f3e | 384 | void EC_GROUP_set_point_conversion_form(EC_GROUP *group, |
254ef80d | 385 | point_conversion_form_t form) |
0f113f3e MC |
386 | { |
387 | group->asn1_form = form; | |
388 | } | |
254ef80d | 389 | |
0f113f3e MC |
390 | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP |
391 | *group) | |
392 | { | |
393 | return group->asn1_form; | |
394 | } | |
254ef80d | 395 | |
5f3d6f70 | 396 | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) |
0f113f3e | 397 | { |
b548a1f1 RS |
398 | OPENSSL_free(group->seed); |
399 | group->seed = NULL; | |
400 | group->seed_len = 0; | |
5f3d6f70 | 401 | |
0f113f3e MC |
402 | if (!len || !p) |
403 | return 1; | |
5f3d6f70 | 404 | |
f06080cb F |
405 | if ((group->seed = OPENSSL_malloc(len)) == NULL) { |
406 | ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 407 | return 0; |
f06080cb | 408 | } |
0f113f3e MC |
409 | memcpy(group->seed, p, len); |
410 | group->seed_len = len; | |
5f3d6f70 | 411 | |
0f113f3e MC |
412 | return len; |
413 | } | |
5f3d6f70 BM |
414 | |
415 | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) | |
0f113f3e MC |
416 | { |
417 | return group->seed; | |
418 | } | |
5f3d6f70 BM |
419 | |
420 | size_t EC_GROUP_get_seed_len(const EC_GROUP *group) | |
0f113f3e MC |
421 | { |
422 | return group->seed_len; | |
423 | } | |
424 | ||
8e3cced7 MC |
425 | int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
426 | const BIGNUM *b, BN_CTX *ctx) | |
0f113f3e MC |
427 | { |
428 | if (group->meth->group_set_curve == 0) { | |
8e3cced7 | 429 | ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
0f113f3e MC |
430 | return 0; |
431 | } | |
432 | return group->meth->group_set_curve(group, p, a, b, ctx); | |
433 | } | |
434 | ||
8e3cced7 MC |
435 | int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, |
436 | BN_CTX *ctx) | |
0f113f3e | 437 | { |
8e3cced7 MC |
438 | if (group->meth->group_get_curve == NULL) { |
439 | ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
0f113f3e MC |
440 | return 0; |
441 | } | |
442 | return group->meth->group_get_curve(group, p, a, b, ctx); | |
443 | } | |
7793f30e | 444 | |
fcd2d5a6 | 445 | #if !OPENSSL_API_3 |
8e3cced7 MC |
446 | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
447 | const BIGNUM *b, BN_CTX *ctx) | |
448 | { | |
449 | return EC_GROUP_set_curve(group, p, a, b, ctx); | |
450 | } | |
451 | ||
452 | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
453 | BIGNUM *b, BN_CTX *ctx) | |
454 | { | |
455 | return EC_GROUP_get_curve(group, p, a, b, ctx); | |
456 | } | |
457 | ||
50db8163 | 458 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
459 | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, |
460 | const BIGNUM *b, BN_CTX *ctx) | |
461 | { | |
8e3cced7 | 462 | return EC_GROUP_set_curve(group, p, a, b, ctx); |
0f113f3e MC |
463 | } |
464 | ||
465 | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, | |
466 | BIGNUM *b, BN_CTX *ctx) | |
467 | { | |
8e3cced7 | 468 | return EC_GROUP_get_curve(group, p, a, b, ctx); |
0f113f3e | 469 | } |
50db8163 | 470 | # endif |
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; | |
6903e2e7 DSH |
506 | if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE) |
507 | return 0; | |
0f113f3e | 508 | |
90945fa3 | 509 | if (ctx == NULL) |
0f113f3e | 510 | ctx_new = ctx = BN_CTX_new(); |
90945fa3 | 511 | if (ctx == NULL) |
0f113f3e MC |
512 | return -1; |
513 | ||
514 | BN_CTX_start(ctx); | |
515 | a1 = BN_CTX_get(ctx); | |
516 | a2 = BN_CTX_get(ctx); | |
517 | a3 = BN_CTX_get(ctx); | |
518 | b1 = BN_CTX_get(ctx); | |
519 | b2 = BN_CTX_get(ctx); | |
520 | b3 = BN_CTX_get(ctx); | |
90945fa3 | 521 | if (b3 == NULL) { |
0f113f3e | 522 | BN_CTX_end(ctx); |
23a1d5e9 | 523 | BN_CTX_free(ctx_new); |
0f113f3e MC |
524 | return -1; |
525 | } | |
526 | ||
527 | /* | |
528 | * XXX This approach assumes that the external representation of curves | |
529 | * over the same field type is the same. | |
530 | */ | |
531 | if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || | |
532 | !b->meth->group_get_curve(b, b1, b2, b3, ctx)) | |
533 | r = 1; | |
534 | ||
8402cd5f SL |
535 | /* return 1 if the curve parameters are different */ |
536 | if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0) | |
0f113f3e MC |
537 | r = 1; |
538 | ||
ac2b52c6 | 539 | /* XXX EC_POINT_cmp() assumes that the methods are equal */ |
8402cd5f | 540 | /* return 1 if the generators are different */ |
0f113f3e | 541 | if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), |
8402cd5f | 542 | EC_GROUP_get0_generator(b), ctx) != 0) |
0f113f3e MC |
543 | r = 1; |
544 | ||
545 | if (!r) { | |
be2e334f | 546 | const BIGNUM *ao, *bo, *ac, *bc; |
ac2b52c6 | 547 | /* compare the orders */ |
be2e334f DSH |
548 | ao = EC_GROUP_get0_order(a); |
549 | bo = EC_GROUP_get0_order(b); | |
be2e334f | 550 | if (ao == NULL || bo == NULL) { |
8402cd5f SL |
551 | /* return an error if either order is NULL */ |
552 | r = -1; | |
553 | goto end; | |
554 | } | |
555 | if (BN_cmp(ao, bo) != 0) { | |
556 | /* return 1 if orders are different */ | |
557 | r = 1; | |
558 | goto end; | |
0f113f3e | 559 | } |
8402cd5f SL |
560 | /* |
561 | * It gets here if the curve parameters and generator matched. | |
562 | * Now check the optional cofactors (if both are present). | |
563 | */ | |
564 | ac = EC_GROUP_get0_cofactor(a); | |
565 | bc = EC_GROUP_get0_cofactor(b); | |
566 | /* Returns 1 (mismatch) if both cofactors are specified and different */ | |
567 | if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0) | |
0f113f3e | 568 | r = 1; |
8402cd5f | 569 | /* Returns 0 if the parameters matched */ |
0f113f3e | 570 | } |
8402cd5f | 571 | end: |
0f113f3e | 572 | BN_CTX_end(ctx); |
23a1d5e9 | 573 | BN_CTX_free(ctx_new); |
ada0e717 | 574 | |
0f113f3e MC |
575 | return r; |
576 | } | |
ada0e717 | 577 | |
0657bf9c BM |
578 | /* functions for EC_POINT objects */ |
579 | ||
580 | EC_POINT *EC_POINT_new(const EC_GROUP *group) | |
0f113f3e MC |
581 | { |
582 | EC_POINT *ret; | |
583 | ||
584 | if (group == NULL) { | |
585 | ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); | |
586 | return NULL; | |
587 | } | |
f06080cb | 588 | if (group->meth->point_init == NULL) { |
0f113f3e MC |
589 | ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
590 | return NULL; | |
591 | } | |
592 | ||
1b4cf96f | 593 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
594 | if (ret == NULL) { |
595 | ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); | |
596 | return NULL; | |
597 | } | |
598 | ||
599 | ret->meth = group->meth; | |
b14e6015 | 600 | ret->curve_name = group->curve_name; |
0f113f3e MC |
601 | |
602 | if (!ret->meth->point_init(ret)) { | |
603 | OPENSSL_free(ret); | |
604 | return NULL; | |
605 | } | |
606 | ||
607 | return ret; | |
608 | } | |
0657bf9c BM |
609 | |
610 | void EC_POINT_free(EC_POINT *point) | |
0f113f3e MC |
611 | { |
612 | if (!point) | |
613 | return; | |
7711de24 | 614 | |
0f113f3e MC |
615 | if (point->meth->point_finish != 0) |
616 | point->meth->point_finish(point); | |
617 | OPENSSL_free(point); | |
618 | } | |
0657bf9c BM |
619 | |
620 | void EC_POINT_clear_free(EC_POINT *point) | |
0f113f3e MC |
621 | { |
622 | if (!point) | |
623 | return; | |
624 | ||
625 | if (point->meth->point_clear_finish != 0) | |
626 | point->meth->point_clear_finish(point); | |
627 | else if (point->meth->point_finish != 0) | |
628 | point->meth->point_finish(point); | |
b4faea50 | 629 | OPENSSL_clear_free(point, sizeof(*point)); |
0f113f3e | 630 | } |
0657bf9c BM |
631 | |
632 | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) | |
0f113f3e MC |
633 | { |
634 | if (dest->meth->point_copy == 0) { | |
635 | ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
636 | return 0; | |
637 | } | |
b14e6015 MC |
638 | if (dest->meth != src->meth |
639 | || (dest->curve_name != src->curve_name | |
8402cd5f SL |
640 | && dest->curve_name != 0 |
641 | && src->curve_name != 0)) { | |
0f113f3e MC |
642 | ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); |
643 | return 0; | |
644 | } | |
645 | if (dest == src) | |
646 | return 1; | |
647 | return dest->meth->point_copy(dest, src); | |
648 | } | |
0657bf9c | 649 | |
7793f30e | 650 | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) |
0f113f3e MC |
651 | { |
652 | EC_POINT *t; | |
653 | int r; | |
654 | ||
655 | if (a == NULL) | |
656 | return NULL; | |
657 | ||
658 | t = EC_POINT_new(group); | |
659 | if (t == NULL) | |
26a7d938 | 660 | return NULL; |
0f113f3e MC |
661 | r = EC_POINT_copy(t, a); |
662 | if (!r) { | |
663 | EC_POINT_free(t); | |
664 | return NULL; | |
8fdc3734 RS |
665 | } |
666 | return t; | |
0f113f3e | 667 | } |
7793f30e | 668 | |
48fe4d62 | 669 | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) |
0f113f3e MC |
670 | { |
671 | return point->meth; | |
672 | } | |
48fe4d62 | 673 | |
226cc7de | 674 | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) |
0f113f3e MC |
675 | { |
676 | if (group->meth->point_set_to_infinity == 0) { | |
677 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, | |
678 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
679 | return 0; | |
680 | } | |
681 | if (group->meth != point->meth) { | |
682 | ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); | |
683 | return 0; | |
684 | } | |
685 | return group->meth->point_set_to_infinity(group, point); | |
686 | } | |
687 | ||
688 | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
689 | EC_POINT *point, const BIGNUM *x, | |
690 | const BIGNUM *y, const BIGNUM *z, | |
691 | BN_CTX *ctx) | |
692 | { | |
693 | if (group->meth->point_set_Jprojective_coordinates_GFp == 0) { | |
694 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, | |
695 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
696 | return 0; | |
697 | } | |
b14e6015 | 698 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
699 | ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, |
700 | EC_R_INCOMPATIBLE_OBJECTS); | |
701 | return 0; | |
702 | } | |
703 | return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, | |
704 | y, z, ctx); | |
705 | } | |
706 | ||
707 | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | |
708 | const EC_POINT *point, BIGNUM *x, | |
709 | BIGNUM *y, BIGNUM *z, | |
710 | BN_CTX *ctx) | |
711 | { | |
712 | if (group->meth->point_get_Jprojective_coordinates_GFp == 0) { | |
713 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, | |
714 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
715 | return 0; | |
716 | } | |
b14e6015 | 717 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
718 | ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, |
719 | EC_R_INCOMPATIBLE_OBJECTS); | |
720 | return 0; | |
721 | } | |
722 | return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, | |
723 | y, z, ctx); | |
724 | } | |
725 | ||
8e3cced7 MC |
726 | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, |
727 | const BIGNUM *x, const BIGNUM *y, | |
728 | BN_CTX *ctx) | |
0f113f3e | 729 | { |
8e3cced7 MC |
730 | if (group->meth->point_set_affine_coordinates == NULL) { |
731 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, | |
0f113f3e MC |
732 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
733 | return 0; | |
734 | } | |
b14e6015 | 735 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 736 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
0f113f3e MC |
737 | return 0; |
738 | } | |
1e2012b7 EK |
739 | if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx)) |
740 | return 0; | |
741 | ||
742 | if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | |
8e3cced7 | 743 | ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE); |
1e2012b7 EK |
744 | return 0; |
745 | } | |
746 | return 1; | |
0f113f3e | 747 | } |
226cc7de | 748 | |
fcd2d5a6 | 749 | #if !OPENSSL_API_3 |
8e3cced7 MC |
750 | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, |
751 | EC_POINT *point, const BIGNUM *x, | |
752 | const BIGNUM *y, BN_CTX *ctx) | |
753 | { | |
754 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); | |
755 | } | |
756 | ||
50db8163 | 757 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
758 | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, |
759 | EC_POINT *point, const BIGNUM *x, | |
760 | const BIGNUM *y, BN_CTX *ctx) | |
761 | { | |
8e3cced7 MC |
762 | return EC_POINT_set_affine_coordinates(group, point, x, y, ctx); |
763 | } | |
50db8163 | 764 | # endif |
8e3cced7 MC |
765 | #endif |
766 | ||
767 | int EC_POINT_get_affine_coordinates(const EC_GROUP *group, | |
768 | const EC_POINT *point, BIGNUM *x, BIGNUM *y, | |
769 | BN_CTX *ctx) | |
770 | { | |
771 | if (group->meth->point_get_affine_coordinates == NULL) { | |
772 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, | |
0f113f3e MC |
773 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
774 | return 0; | |
775 | } | |
b14e6015 | 776 | if (!ec_point_is_compat(point, group)) { |
8e3cced7 | 777 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS); |
1e2012b7 EK |
778 | return 0; |
779 | } | |
bfb10b97 BB |
780 | if (EC_POINT_is_at_infinity(group, point)) { |
781 | ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY); | |
782 | return 0; | |
783 | } | |
8e3cced7 | 784 | return group->meth->point_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 785 | } |
7793f30e | 786 | |
fcd2d5a6 | 787 | #if !OPENSSL_API_3 |
0f113f3e MC |
788 | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, |
789 | const EC_POINT *point, BIGNUM *x, | |
790 | BIGNUM *y, BN_CTX *ctx) | |
791 | { | |
8e3cced7 | 792 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 793 | } |
7793f30e | 794 | |
50db8163 | 795 | # ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
796 | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, |
797 | const EC_POINT *point, BIGNUM *x, | |
798 | BIGNUM *y, BN_CTX *ctx) | |
799 | { | |
8e3cced7 | 800 | return EC_POINT_get_affine_coordinates(group, point, x, y, ctx); |
0f113f3e | 801 | } |
50db8163 | 802 | # endif |
b3310161 | 803 | #endif |
7793f30e | 804 | |
0f113f3e MC |
805 | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
806 | const EC_POINT *b, BN_CTX *ctx) | |
807 | { | |
808 | if (group->meth->add == 0) { | |
809 | ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
810 | return 0; | |
811 | } | |
b14e6015 MC |
812 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group) |
813 | || !ec_point_is_compat(b, group)) { | |
0f113f3e MC |
814 | ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS); |
815 | return 0; | |
816 | } | |
817 | return group->meth->add(group, r, a, b, ctx); | |
818 | } | |
819 | ||
820 | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | |
821 | BN_CTX *ctx) | |
822 | { | |
823 | if (group->meth->dbl == 0) { | |
824 | ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
825 | return 0; | |
826 | } | |
b14e6015 | 827 | if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { |
0f113f3e MC |
828 | ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS); |
829 | return 0; | |
830 | } | |
831 | return group->meth->dbl(group, r, a, ctx); | |
832 | } | |
0657bf9c | 833 | |
1d5bd6cf | 834 | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) |
0f113f3e MC |
835 | { |
836 | if (group->meth->invert == 0) { | |
837 | ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
838 | return 0; | |
839 | } | |
b14e6015 | 840 | if (!ec_point_is_compat(a, group)) { |
0f113f3e MC |
841 | ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS); |
842 | return 0; | |
843 | } | |
844 | return group->meth->invert(group, a, ctx); | |
845 | } | |
1d5bd6cf | 846 | |
0657bf9c | 847 | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
0f113f3e MC |
848 | { |
849 | if (group->meth->is_at_infinity == 0) { | |
850 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, | |
851 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
852 | return 0; | |
853 | } | |
b14e6015 | 854 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
855 | ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); |
856 | return 0; | |
857 | } | |
858 | return group->meth->is_at_infinity(group, point); | |
859 | } | |
860 | ||
68886be7 MC |
861 | /* |
862 | * Check whether an EC_POINT is on the curve or not. Note that the return | |
863 | * value for this function should NOT be treated as a boolean. Return values: | |
864 | * 1: The point is on the curve | |
865 | * 0: The point is not on the curve | |
866 | * -1: An error occurred | |
867 | */ | |
0f113f3e MC |
868 | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
869 | BN_CTX *ctx) | |
870 | { | |
871 | if (group->meth->is_on_curve == 0) { | |
872 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
873 | return 0; | |
874 | } | |
b14e6015 | 875 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
876 | ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS); |
877 | return 0; | |
878 | } | |
879 | return group->meth->is_on_curve(group, point, ctx); | |
880 | } | |
881 | ||
882 | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, | |
883 | BN_CTX *ctx) | |
884 | { | |
885 | if (group->meth->point_cmp == 0) { | |
886 | ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
887 | return -1; | |
888 | } | |
b14e6015 | 889 | if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { |
0f113f3e MC |
890 | ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); |
891 | return -1; | |
892 | } | |
893 | return group->meth->point_cmp(group, a, b, ctx); | |
894 | } | |
1d5bd6cf | 895 | |
e869d4bd | 896 | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
0f113f3e MC |
897 | { |
898 | if (group->meth->make_affine == 0) { | |
899 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
900 | return 0; | |
901 | } | |
b14e6015 | 902 | if (!ec_point_is_compat(point, group)) { |
0f113f3e MC |
903 | ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
904 | return 0; | |
905 | } | |
906 | return group->meth->make_affine(group, point, ctx); | |
907 | } | |
908 | ||
909 | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, | |
910 | EC_POINT *points[], BN_CTX *ctx) | |
911 | { | |
912 | size_t i; | |
913 | ||
914 | if (group->meth->points_make_affine == 0) { | |
915 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
916 | return 0; | |
917 | } | |
918 | for (i = 0; i < num; i++) { | |
b14e6015 | 919 | if (!ec_point_is_compat(points[i], group)) { |
0f113f3e MC |
920 | ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); |
921 | return 0; | |
922 | } | |
923 | } | |
924 | return group->meth->points_make_affine(group, num, points, ctx); | |
925 | } | |
926 | ||
927 | /* | |
928 | * Functions for point multiplication. If group->meth->mul is 0, we use the | |
929 | * wNAF-based implementations in ec_mult.c; otherwise we dispatch through | |
930 | * methods. | |
37c660ff BM |
931 | */ |
932 | ||
933 | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, | |
0f113f3e MC |
934 | size_t num, const EC_POINT *points[], |
935 | const BIGNUM *scalars[], BN_CTX *ctx) | |
936 | { | |
01ad66f8 NT |
937 | int ret = 0; |
938 | size_t i = 0; | |
939 | BN_CTX *new_ctx = NULL; | |
940 | ||
941 | if ((scalar == NULL) && (num == 0)) { | |
942 | return EC_POINT_set_to_infinity(group, r); | |
943 | } | |
944 | ||
945 | if (!ec_point_is_compat(r, group)) { | |
946 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
947 | return 0; | |
948 | } | |
949 | for (i = 0; i < num; i++) { | |
950 | if (!ec_point_is_compat(points[i], group)) { | |
951 | ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); | |
952 | return 0; | |
953 | } | |
954 | } | |
955 | ||
956 | if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) { | |
957 | ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR); | |
958 | return 0; | |
959 | } | |
960 | ||
961 | if (group->meth->mul != NULL) | |
962 | ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx); | |
963 | else | |
0f113f3e | 964 | /* use default */ |
01ad66f8 | 965 | ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); |
37c660ff | 966 | |
01ad66f8 NT |
967 | BN_CTX_free(new_ctx); |
968 | return ret; | |
0f113f3e | 969 | } |
37c660ff BM |
970 | |
971 | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, | |
0f113f3e MC |
972 | const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) |
973 | { | |
974 | /* just a convenient interface to EC_POINTs_mul() */ | |
37c660ff | 975 | |
0f113f3e MC |
976 | const EC_POINT *points[1]; |
977 | const BIGNUM *scalars[1]; | |
37c660ff | 978 | |
0f113f3e MC |
979 | points[0] = point; |
980 | scalars[0] = p_scalar; | |
37c660ff | 981 | |
0f113f3e MC |
982 | return EC_POINTs_mul(group, r, g_scalar, |
983 | (point != NULL | |
984 | && p_scalar != NULL), points, scalars, ctx); | |
985 | } | |
37c660ff BM |
986 | |
987 | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) | |
0f113f3e MC |
988 | { |
989 | if (group->meth->mul == 0) | |
990 | /* use default */ | |
991 | return ec_wNAF_precompute_mult(group, ctx); | |
37c660ff | 992 | |
0f113f3e MC |
993 | if (group->meth->precompute_mult != 0) |
994 | return group->meth->precompute_mult(group, ctx); | |
995 | else | |
996 | return 1; /* nothing to do, so report success */ | |
997 | } | |
37c660ff BM |
998 | |
999 | int EC_GROUP_have_precompute_mult(const EC_GROUP *group) | |
0f113f3e MC |
1000 | { |
1001 | if (group->meth->mul == 0) | |
1002 | /* use default */ | |
1003 | return ec_wNAF_have_precompute_mult(group); | |
1004 | ||
1005 | if (group->meth->have_precompute_mult != 0) | |
1006 | return group->meth->have_precompute_mult(group); | |
1007 | else | |
1008 | return 0; /* cannot tell whether precomputation has | |
1009 | * been performed */ | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * ec_precompute_mont_data sets |group->mont_data| from |group->order| and | |
1014 | * returns one on success. On error it returns zero. | |
1015 | */ | |
eb791696 | 1016 | static int ec_precompute_mont_data(EC_GROUP *group) |
0f113f3e MC |
1017 | { |
1018 | BN_CTX *ctx = BN_CTX_new(); | |
1019 | int ret = 0; | |
1020 | ||
23a1d5e9 RS |
1021 | BN_MONT_CTX_free(group->mont_data); |
1022 | group->mont_data = NULL; | |
0f113f3e MC |
1023 | |
1024 | if (ctx == NULL) | |
1025 | goto err; | |
1026 | ||
1027 | group->mont_data = BN_MONT_CTX_new(); | |
90945fa3 | 1028 | if (group->mont_data == NULL) |
0f113f3e MC |
1029 | goto err; |
1030 | ||
1031 | if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | |
1032 | BN_MONT_CTX_free(group->mont_data); | |
1033 | group->mont_data = NULL; | |
1034 | goto err; | |
1035 | } | |
1036 | ||
1037 | ret = 1; | |
1038 | ||
1039 | err: | |
1040 | ||
23a1d5e9 | 1041 | BN_CTX_free(ctx); |
0f113f3e MC |
1042 | return ret; |
1043 | } | |
3aef36ff RS |
1044 | |
1045 | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg) | |
1046 | { | |
1047 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
1048 | } | |
1049 | ||
1050 | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx) | |
1051 | { | |
1052 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
1053 | } | |
77470e98 DSH |
1054 | |
1055 | int ec_group_simple_order_bits(const EC_GROUP *group) | |
1056 | { | |
1057 | if (group->order == NULL) | |
1058 | return 0; | |
1059 | return BN_num_bits(group->order); | |
1060 | } | |
eb791696 | 1061 | |
c11d372b | 1062 | static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r, |
792546eb | 1063 | const BIGNUM *x, BN_CTX *ctx) |
c11d372b | 1064 | { |
262dccc0 | 1065 | BIGNUM *e = NULL; |
c11d372b BB |
1066 | BN_CTX *new_ctx = NULL; |
1067 | int ret = 0; | |
1068 | ||
792546eb BB |
1069 | if (group->mont_data == NULL) |
1070 | return 0; | |
1071 | ||
c11d372b BB |
1072 | if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) |
1073 | return 0; | |
1074 | ||
1075 | BN_CTX_start(ctx); | |
262dccc0 | 1076 | if ((e = BN_CTX_get(ctx)) == NULL) |
c11d372b BB |
1077 | goto err; |
1078 | ||
792546eb BB |
1079 | /*- |
1080 | * We want inverse in constant time, therefore we utilize the fact | |
1081 | * order must be prime and use Fermats Little Theorem instead. | |
1082 | */ | |
1083 | if (!BN_set_word(e, 2)) | |
1084 | goto err; | |
1085 | if (!BN_sub(e, group->order, e)) | |
1086 | goto err; | |
1087 | /*- | |
1088 | * Exponent e is public. | |
1089 | * No need for scatter-gather or BN_FLG_CONSTTIME. | |
1090 | */ | |
1091 | if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data)) | |
1092 | goto err; | |
c11d372b | 1093 | |
792546eb | 1094 | ret = 1; |
c11d372b BB |
1095 | |
1096 | err: | |
ce1415ed | 1097 | BN_CTX_end(ctx); |
c11d372b BB |
1098 | BN_CTX_free(new_ctx); |
1099 | return ret; | |
1100 | } | |
1101 | ||
792546eb BB |
1102 | /*- |
1103 | * Default behavior, if group->meth->field_inverse_mod_ord is NULL: | |
1104 | * - When group->order is even, this function returns an error. | |
1105 | * - When group->order is otherwise composite, the correctness | |
1106 | * of the output is not guaranteed. | |
1107 | * - When x is outside the range [1, group->order), the correctness | |
1108 | * of the output is not guaranteed. | |
1109 | * - Otherwise, this function returns the multiplicative inverse in the | |
1110 | * range [1, group->order). | |
1111 | * | |
1112 | * EC_METHODs must implement their own field_inverse_mod_ord for | |
1113 | * other functionality. | |
1114 | */ | |
1115 | int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res, | |
1116 | const BIGNUM *x, BN_CTX *ctx) | |
eb791696 AP |
1117 | { |
1118 | if (group->meth->field_inverse_mod_ord != NULL) | |
1119 | return group->meth->field_inverse_mod_ord(group, res, x, ctx); | |
1120 | else | |
c11d372b | 1121 | return ec_field_inverse_mod_ord(group, res, x, ctx); |
eb791696 | 1122 | } |
f667820c SH |
1123 | |
1124 | /*- | |
1125 | * Coordinate blinding for EC_POINT. | |
1126 | * | |
1127 | * The underlying EC_METHOD can optionally implement this function: | |
1128 | * underlying implementations should return 0 on errors, or 1 on | |
1129 | * success. | |
1130 | * | |
1131 | * This wrapper returns 1 in case the underlying EC_METHOD does not | |
1132 | * support coordinate blinding. | |
1133 | */ | |
1134 | int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx) | |
1135 | { | |
1136 | if (group->meth->blind_coordinates == NULL) | |
1137 | return 1; /* ignore if not implemented */ | |
1138 | ||
1139 | return group->meth->blind_coordinates(group, p, ctx); | |
1140 | } |