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