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