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