/src/openssl/crypto/ec/ecp_smpl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* crypto/ec/ecp_smpl.c */ |
2 | | /* |
3 | | * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> |
4 | | * for the OpenSSL project. Includes code written by Bodo Moeller for the |
5 | | * OpenSSL project. |
6 | | */ |
7 | | /* ==================================================================== |
8 | | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
9 | | * |
10 | | * Redistribution and use in source and binary forms, with or without |
11 | | * modification, are permitted provided that the following conditions |
12 | | * are met: |
13 | | * |
14 | | * 1. Redistributions of source code must retain the above copyright |
15 | | * notice, this list of conditions and the following disclaimer. |
16 | | * |
17 | | * 2. Redistributions in binary form must reproduce the above copyright |
18 | | * notice, this list of conditions and the following disclaimer in |
19 | | * the documentation and/or other materials provided with the |
20 | | * distribution. |
21 | | * |
22 | | * 3. All advertising materials mentioning features or use of this |
23 | | * software must display the following acknowledgment: |
24 | | * "This product includes software developed by the OpenSSL Project |
25 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
26 | | * |
27 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
28 | | * endorse or promote products derived from this software without |
29 | | * prior written permission. For written permission, please contact |
30 | | * openssl-core@openssl.org. |
31 | | * |
32 | | * 5. Products derived from this software may not be called "OpenSSL" |
33 | | * nor may "OpenSSL" appear in their names without prior written |
34 | | * permission of the OpenSSL Project. |
35 | | * |
36 | | * 6. Redistributions of any form whatsoever must retain the following |
37 | | * acknowledgment: |
38 | | * "This product includes software developed by the OpenSSL Project |
39 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
42 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
44 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
45 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
46 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
47 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
48 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
49 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
50 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
51 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
52 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
53 | | * ==================================================================== |
54 | | * |
55 | | * This product includes cryptographic software written by Eric Young |
56 | | * (eay@cryptsoft.com). This product includes software written by Tim |
57 | | * Hudson (tjh@cryptsoft.com). |
58 | | * |
59 | | */ |
60 | | /* ==================================================================== |
61 | | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
62 | | * Portions of this software developed by SUN MICROSYSTEMS, INC., |
63 | | * and contributed to the OpenSSL project. |
64 | | */ |
65 | | |
66 | | #include <openssl/err.h> |
67 | | #include <openssl/symhacks.h> |
68 | | |
69 | | #ifdef OPENSSL_FIPS |
70 | | # include <openssl/fips.h> |
71 | | #endif |
72 | | |
73 | | #include "ec_lcl.h" |
74 | | |
75 | | const EC_METHOD *EC_GFp_simple_method(void) |
76 | 0 | { |
77 | 0 | static const EC_METHOD ret = { |
78 | 0 | EC_FLAGS_DEFAULT_OCT, |
79 | 0 | NID_X9_62_prime_field, |
80 | 0 | ec_GFp_simple_group_init, |
81 | 0 | ec_GFp_simple_group_finish, |
82 | 0 | ec_GFp_simple_group_clear_finish, |
83 | 0 | ec_GFp_simple_group_copy, |
84 | 0 | ec_GFp_simple_group_set_curve, |
85 | 0 | ec_GFp_simple_group_get_curve, |
86 | 0 | ec_GFp_simple_group_get_degree, |
87 | 0 | ec_GFp_simple_group_check_discriminant, |
88 | 0 | ec_GFp_simple_point_init, |
89 | 0 | ec_GFp_simple_point_finish, |
90 | 0 | ec_GFp_simple_point_clear_finish, |
91 | 0 | ec_GFp_simple_point_copy, |
92 | 0 | ec_GFp_simple_point_set_to_infinity, |
93 | 0 | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
94 | 0 | ec_GFp_simple_get_Jprojective_coordinates_GFp, |
95 | 0 | ec_GFp_simple_point_set_affine_coordinates, |
96 | 0 | ec_GFp_simple_point_get_affine_coordinates, |
97 | 0 | 0, 0, 0, |
98 | 0 | ec_GFp_simple_add, |
99 | 0 | ec_GFp_simple_dbl, |
100 | 0 | ec_GFp_simple_invert, |
101 | 0 | ec_GFp_simple_is_at_infinity, |
102 | 0 | ec_GFp_simple_is_on_curve, |
103 | 0 | ec_GFp_simple_cmp, |
104 | 0 | ec_GFp_simple_make_affine, |
105 | 0 | ec_GFp_simple_points_make_affine, |
106 | 0 | 0 /* mul */ , |
107 | 0 | 0 /* precompute_mult */ , |
108 | 0 | 0 /* have_precompute_mult */ , |
109 | 0 | ec_GFp_simple_field_mul, |
110 | 0 | ec_GFp_simple_field_sqr, |
111 | 0 | 0 /* field_div */ , |
112 | 0 | 0 /* field_encode */ , |
113 | 0 | 0 /* field_decode */ , |
114 | 0 | 0 /* field_set_to_one */ |
115 | 0 | }; |
116 | |
|
117 | | #ifdef OPENSSL_FIPS |
118 | | if (FIPS_mode()) |
119 | | return fips_ec_gfp_simple_method(); |
120 | | #endif |
121 | |
|
122 | 0 | return &ret; |
123 | 0 | } |
124 | | |
125 | | /* |
126 | | * Most method functions in this file are designed to work with |
127 | | * non-trivial representations of field elements if necessary |
128 | | * (see ecp_mont.c): while standard modular addition and subtraction |
129 | | * are used, the field_mul and field_sqr methods will be used for |
130 | | * multiplication, and field_encode and field_decode (if defined) |
131 | | * will be used for converting between representations. |
132 | | * |
133 | | * Functions ec_GFp_simple_points_make_affine() and |
134 | | * ec_GFp_simple_point_get_affine_coordinates() specifically assume |
135 | | * that if a non-trivial representation is used, it is a Montgomery |
136 | | * representation (i.e. 'encoding' means multiplying by some factor R). |
137 | | */ |
138 | | |
139 | | int ec_GFp_simple_group_init(EC_GROUP *group) |
140 | 0 | { |
141 | 0 | BN_init(&group->field); |
142 | 0 | BN_init(&group->a); |
143 | 0 | BN_init(&group->b); |
144 | 0 | group->a_is_minus3 = 0; |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | | void ec_GFp_simple_group_finish(EC_GROUP *group) |
149 | 0 | { |
150 | 0 | BN_free(&group->field); |
151 | 0 | BN_free(&group->a); |
152 | 0 | BN_free(&group->b); |
153 | 0 | } |
154 | | |
155 | | void ec_GFp_simple_group_clear_finish(EC_GROUP *group) |
156 | 0 | { |
157 | 0 | BN_clear_free(&group->field); |
158 | 0 | BN_clear_free(&group->a); |
159 | 0 | BN_clear_free(&group->b); |
160 | 0 | } |
161 | | |
162 | | int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
163 | 0 | { |
164 | 0 | if (!BN_copy(&dest->field, &src->field)) |
165 | 0 | return 0; |
166 | 0 | if (!BN_copy(&dest->a, &src->a)) |
167 | 0 | return 0; |
168 | 0 | if (!BN_copy(&dest->b, &src->b)) |
169 | 0 | return 0; |
170 | | |
171 | 0 | dest->a_is_minus3 = src->a_is_minus3; |
172 | |
|
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | | int ec_GFp_simple_group_set_curve(EC_GROUP *group, |
177 | | const BIGNUM *p, const BIGNUM *a, |
178 | | const BIGNUM *b, BN_CTX *ctx) |
179 | 0 | { |
180 | 0 | int ret = 0; |
181 | 0 | BN_CTX *new_ctx = NULL; |
182 | 0 | BIGNUM *tmp_a; |
183 | | |
184 | | /* p must be a prime > 3 */ |
185 | 0 | if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) { |
186 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD); |
187 | 0 | return 0; |
188 | 0 | } |
189 | | |
190 | 0 | if (ctx == NULL) { |
191 | 0 | ctx = new_ctx = BN_CTX_new(); |
192 | 0 | if (ctx == NULL) |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | 0 | BN_CTX_start(ctx); |
197 | 0 | tmp_a = BN_CTX_get(ctx); |
198 | 0 | if (tmp_a == NULL) |
199 | 0 | goto err; |
200 | | |
201 | | /* group->field */ |
202 | 0 | if (!BN_copy(&group->field, p)) |
203 | 0 | goto err; |
204 | 0 | BN_set_negative(&group->field, 0); |
205 | | |
206 | | /* group->a */ |
207 | 0 | if (!BN_nnmod(tmp_a, a, p, ctx)) |
208 | 0 | goto err; |
209 | 0 | if (group->meth->field_encode) { |
210 | 0 | if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) |
211 | 0 | goto err; |
212 | 0 | } else if (!BN_copy(&group->a, tmp_a)) |
213 | 0 | goto err; |
214 | | |
215 | | /* group->b */ |
216 | 0 | if (!BN_nnmod(&group->b, b, p, ctx)) |
217 | 0 | goto err; |
218 | 0 | if (group->meth->field_encode) |
219 | 0 | if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) |
220 | 0 | goto err; |
221 | | |
222 | | /* group->a_is_minus3 */ |
223 | 0 | if (!BN_add_word(tmp_a, 3)) |
224 | 0 | goto err; |
225 | 0 | group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field)); |
226 | |
|
227 | 0 | ret = 1; |
228 | |
|
229 | 0 | err: |
230 | 0 | BN_CTX_end(ctx); |
231 | 0 | if (new_ctx != NULL) |
232 | 0 | BN_CTX_free(new_ctx); |
233 | 0 | return ret; |
234 | 0 | } |
235 | | |
236 | | int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, |
237 | | BIGNUM *b, BN_CTX *ctx) |
238 | 0 | { |
239 | 0 | int ret = 0; |
240 | 0 | BN_CTX *new_ctx = NULL; |
241 | |
|
242 | 0 | if (p != NULL) { |
243 | 0 | if (!BN_copy(p, &group->field)) |
244 | 0 | return 0; |
245 | 0 | } |
246 | | |
247 | 0 | if (a != NULL || b != NULL) { |
248 | 0 | if (group->meth->field_decode) { |
249 | 0 | if (ctx == NULL) { |
250 | 0 | ctx = new_ctx = BN_CTX_new(); |
251 | 0 | if (ctx == NULL) |
252 | 0 | return 0; |
253 | 0 | } |
254 | 0 | if (a != NULL) { |
255 | 0 | if (!group->meth->field_decode(group, a, &group->a, ctx)) |
256 | 0 | goto err; |
257 | 0 | } |
258 | 0 | if (b != NULL) { |
259 | 0 | if (!group->meth->field_decode(group, b, &group->b, ctx)) |
260 | 0 | goto err; |
261 | 0 | } |
262 | 0 | } else { |
263 | 0 | if (a != NULL) { |
264 | 0 | if (!BN_copy(a, &group->a)) |
265 | 0 | goto err; |
266 | 0 | } |
267 | 0 | if (b != NULL) { |
268 | 0 | if (!BN_copy(b, &group->b)) |
269 | 0 | goto err; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | 0 | ret = 1; |
275 | |
|
276 | 0 | err: |
277 | 0 | if (new_ctx) |
278 | 0 | BN_CTX_free(new_ctx); |
279 | 0 | return ret; |
280 | 0 | } |
281 | | |
282 | | int ec_GFp_simple_group_get_degree(const EC_GROUP *group) |
283 | 0 | { |
284 | 0 | return BN_num_bits(&group->field); |
285 | 0 | } |
286 | | |
287 | | int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
288 | 0 | { |
289 | 0 | int ret = 0; |
290 | 0 | BIGNUM *a, *b, *order, *tmp_1, *tmp_2; |
291 | 0 | const BIGNUM *p = &group->field; |
292 | 0 | BN_CTX *new_ctx = NULL; |
293 | |
|
294 | 0 | if (ctx == NULL) { |
295 | 0 | ctx = new_ctx = BN_CTX_new(); |
296 | 0 | if (ctx == NULL) { |
297 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, |
298 | 0 | ERR_R_MALLOC_FAILURE); |
299 | 0 | goto err; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | BN_CTX_start(ctx); |
303 | 0 | a = BN_CTX_get(ctx); |
304 | 0 | b = BN_CTX_get(ctx); |
305 | 0 | tmp_1 = BN_CTX_get(ctx); |
306 | 0 | tmp_2 = BN_CTX_get(ctx); |
307 | 0 | order = BN_CTX_get(ctx); |
308 | 0 | if (order == NULL) |
309 | 0 | goto err; |
310 | | |
311 | 0 | if (group->meth->field_decode) { |
312 | 0 | if (!group->meth->field_decode(group, a, &group->a, ctx)) |
313 | 0 | goto err; |
314 | 0 | if (!group->meth->field_decode(group, b, &group->b, ctx)) |
315 | 0 | goto err; |
316 | 0 | } else { |
317 | 0 | if (!BN_copy(a, &group->a)) |
318 | 0 | goto err; |
319 | 0 | if (!BN_copy(b, &group->b)) |
320 | 0 | goto err; |
321 | 0 | } |
322 | | |
323 | | /*- |
324 | | * check the discriminant: |
325 | | * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p) |
326 | | * 0 =< a, b < p |
327 | | */ |
328 | 0 | if (BN_is_zero(a)) { |
329 | 0 | if (BN_is_zero(b)) |
330 | 0 | goto err; |
331 | 0 | } else if (!BN_is_zero(b)) { |
332 | 0 | if (!BN_mod_sqr(tmp_1, a, p, ctx)) |
333 | 0 | goto err; |
334 | 0 | if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) |
335 | 0 | goto err; |
336 | 0 | if (!BN_lshift(tmp_1, tmp_2, 2)) |
337 | 0 | goto err; |
338 | | /* tmp_1 = 4*a^3 */ |
339 | | |
340 | 0 | if (!BN_mod_sqr(tmp_2, b, p, ctx)) |
341 | 0 | goto err; |
342 | 0 | if (!BN_mul_word(tmp_2, 27)) |
343 | 0 | goto err; |
344 | | /* tmp_2 = 27*b^2 */ |
345 | | |
346 | 0 | if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) |
347 | 0 | goto err; |
348 | 0 | if (BN_is_zero(a)) |
349 | 0 | goto err; |
350 | 0 | } |
351 | 0 | ret = 1; |
352 | |
|
353 | 0 | err: |
354 | 0 | if (ctx != NULL) |
355 | 0 | BN_CTX_end(ctx); |
356 | 0 | if (new_ctx != NULL) |
357 | 0 | BN_CTX_free(new_ctx); |
358 | 0 | return ret; |
359 | 0 | } |
360 | | |
361 | | int ec_GFp_simple_point_init(EC_POINT *point) |
362 | 0 | { |
363 | 0 | BN_init(&point->X); |
364 | 0 | BN_init(&point->Y); |
365 | 0 | BN_init(&point->Z); |
366 | 0 | point->Z_is_one = 0; |
367 | |
|
368 | 0 | return 1; |
369 | 0 | } |
370 | | |
371 | | void ec_GFp_simple_point_finish(EC_POINT *point) |
372 | 0 | { |
373 | 0 | BN_free(&point->X); |
374 | 0 | BN_free(&point->Y); |
375 | 0 | BN_free(&point->Z); |
376 | 0 | } |
377 | | |
378 | | void ec_GFp_simple_point_clear_finish(EC_POINT *point) |
379 | 0 | { |
380 | 0 | BN_clear_free(&point->X); |
381 | 0 | BN_clear_free(&point->Y); |
382 | 0 | BN_clear_free(&point->Z); |
383 | 0 | point->Z_is_one = 0; |
384 | 0 | } |
385 | | |
386 | | int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) |
387 | 0 | { |
388 | 0 | if (!BN_copy(&dest->X, &src->X)) |
389 | 0 | return 0; |
390 | 0 | if (!BN_copy(&dest->Y, &src->Y)) |
391 | 0 | return 0; |
392 | 0 | if (!BN_copy(&dest->Z, &src->Z)) |
393 | 0 | return 0; |
394 | 0 | dest->Z_is_one = src->Z_is_one; |
395 | |
|
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | | int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, |
400 | | EC_POINT *point) |
401 | 0 | { |
402 | 0 | point->Z_is_one = 0; |
403 | 0 | BN_zero(&point->Z); |
404 | 0 | return 1; |
405 | 0 | } |
406 | | |
407 | | int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, |
408 | | EC_POINT *point, |
409 | | const BIGNUM *x, |
410 | | const BIGNUM *y, |
411 | | const BIGNUM *z, |
412 | | BN_CTX *ctx) |
413 | 0 | { |
414 | 0 | BN_CTX *new_ctx = NULL; |
415 | 0 | int ret = 0; |
416 | |
|
417 | 0 | if (ctx == NULL) { |
418 | 0 | ctx = new_ctx = BN_CTX_new(); |
419 | 0 | if (ctx == NULL) |
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | 0 | if (x != NULL) { |
424 | 0 | if (!BN_nnmod(&point->X, x, &group->field, ctx)) |
425 | 0 | goto err; |
426 | 0 | if (group->meth->field_encode) { |
427 | 0 | if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) |
428 | 0 | goto err; |
429 | 0 | } |
430 | 0 | } |
431 | | |
432 | 0 | if (y != NULL) { |
433 | 0 | if (!BN_nnmod(&point->Y, y, &group->field, ctx)) |
434 | 0 | goto err; |
435 | 0 | if (group->meth->field_encode) { |
436 | 0 | if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) |
437 | 0 | goto err; |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | 0 | if (z != NULL) { |
442 | 0 | int Z_is_one; |
443 | |
|
444 | 0 | if (!BN_nnmod(&point->Z, z, &group->field, ctx)) |
445 | 0 | goto err; |
446 | 0 | Z_is_one = BN_is_one(&point->Z); |
447 | 0 | if (group->meth->field_encode) { |
448 | 0 | if (Z_is_one && (group->meth->field_set_to_one != 0)) { |
449 | 0 | if (!group->meth->field_set_to_one(group, &point->Z, ctx)) |
450 | 0 | goto err; |
451 | 0 | } else { |
452 | 0 | if (!group-> |
453 | 0 | meth->field_encode(group, &point->Z, &point->Z, ctx)) |
454 | 0 | goto err; |
455 | 0 | } |
456 | 0 | } |
457 | 0 | point->Z_is_one = Z_is_one; |
458 | 0 | } |
459 | | |
460 | 0 | ret = 1; |
461 | |
|
462 | 0 | err: |
463 | 0 | if (new_ctx != NULL) |
464 | 0 | BN_CTX_free(new_ctx); |
465 | 0 | return ret; |
466 | 0 | } |
467 | | |
468 | | int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, |
469 | | const EC_POINT *point, |
470 | | BIGNUM *x, BIGNUM *y, |
471 | | BIGNUM *z, BN_CTX *ctx) |
472 | 0 | { |
473 | 0 | BN_CTX *new_ctx = NULL; |
474 | 0 | int ret = 0; |
475 | |
|
476 | 0 | if (group->meth->field_decode != 0) { |
477 | 0 | if (ctx == NULL) { |
478 | 0 | ctx = new_ctx = BN_CTX_new(); |
479 | 0 | if (ctx == NULL) |
480 | 0 | return 0; |
481 | 0 | } |
482 | | |
483 | 0 | if (x != NULL) { |
484 | 0 | if (!group->meth->field_decode(group, x, &point->X, ctx)) |
485 | 0 | goto err; |
486 | 0 | } |
487 | 0 | if (y != NULL) { |
488 | 0 | if (!group->meth->field_decode(group, y, &point->Y, ctx)) |
489 | 0 | goto err; |
490 | 0 | } |
491 | 0 | if (z != NULL) { |
492 | 0 | if (!group->meth->field_decode(group, z, &point->Z, ctx)) |
493 | 0 | goto err; |
494 | 0 | } |
495 | 0 | } else { |
496 | 0 | if (x != NULL) { |
497 | 0 | if (!BN_copy(x, &point->X)) |
498 | 0 | goto err; |
499 | 0 | } |
500 | 0 | if (y != NULL) { |
501 | 0 | if (!BN_copy(y, &point->Y)) |
502 | 0 | goto err; |
503 | 0 | } |
504 | 0 | if (z != NULL) { |
505 | 0 | if (!BN_copy(z, &point->Z)) |
506 | 0 | goto err; |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | 0 | ret = 1; |
511 | |
|
512 | 0 | err: |
513 | 0 | if (new_ctx != NULL) |
514 | 0 | BN_CTX_free(new_ctx); |
515 | 0 | return ret; |
516 | 0 | } |
517 | | |
518 | | int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, |
519 | | EC_POINT *point, |
520 | | const BIGNUM *x, |
521 | | const BIGNUM *y, BN_CTX *ctx) |
522 | 0 | { |
523 | 0 | if (x == NULL || y == NULL) { |
524 | | /* |
525 | | * unlike for projective coordinates, we do not tolerate this |
526 | | */ |
527 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, |
528 | 0 | ERR_R_PASSED_NULL_PARAMETER); |
529 | 0 | return 0; |
530 | 0 | } |
531 | | |
532 | 0 | return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, |
533 | 0 | BN_value_one(), ctx); |
534 | 0 | } |
535 | | |
536 | | int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, |
537 | | const EC_POINT *point, |
538 | | BIGNUM *x, BIGNUM *y, |
539 | | BN_CTX *ctx) |
540 | 0 | { |
541 | 0 | BN_CTX *new_ctx = NULL; |
542 | 0 | BIGNUM *Z, *Z_1, *Z_2, *Z_3; |
543 | 0 | const BIGNUM *Z_; |
544 | 0 | int ret = 0; |
545 | |
|
546 | 0 | if (EC_POINT_is_at_infinity(group, point)) { |
547 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, |
548 | 0 | EC_R_POINT_AT_INFINITY); |
549 | 0 | return 0; |
550 | 0 | } |
551 | | |
552 | 0 | if (ctx == NULL) { |
553 | 0 | ctx = new_ctx = BN_CTX_new(); |
554 | 0 | if (ctx == NULL) |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 0 | BN_CTX_start(ctx); |
559 | 0 | Z = BN_CTX_get(ctx); |
560 | 0 | Z_1 = BN_CTX_get(ctx); |
561 | 0 | Z_2 = BN_CTX_get(ctx); |
562 | 0 | Z_3 = BN_CTX_get(ctx); |
563 | 0 | if (Z_3 == NULL) |
564 | 0 | goto err; |
565 | | |
566 | | /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */ |
567 | | |
568 | 0 | if (group->meth->field_decode) { |
569 | 0 | if (!group->meth->field_decode(group, Z, &point->Z, ctx)) |
570 | 0 | goto err; |
571 | 0 | Z_ = Z; |
572 | 0 | } else { |
573 | 0 | Z_ = &point->Z; |
574 | 0 | } |
575 | | |
576 | 0 | if (BN_is_one(Z_)) { |
577 | 0 | if (group->meth->field_decode) { |
578 | 0 | if (x != NULL) { |
579 | 0 | if (!group->meth->field_decode(group, x, &point->X, ctx)) |
580 | 0 | goto err; |
581 | 0 | } |
582 | 0 | if (y != NULL) { |
583 | 0 | if (!group->meth->field_decode(group, y, &point->Y, ctx)) |
584 | 0 | goto err; |
585 | 0 | } |
586 | 0 | } else { |
587 | 0 | if (x != NULL) { |
588 | 0 | if (!BN_copy(x, &point->X)) |
589 | 0 | goto err; |
590 | 0 | } |
591 | 0 | if (y != NULL) { |
592 | 0 | if (!BN_copy(y, &point->Y)) |
593 | 0 | goto err; |
594 | 0 | } |
595 | 0 | } |
596 | 0 | } else { |
597 | 0 | if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx)) { |
598 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, |
599 | 0 | ERR_R_BN_LIB); |
600 | 0 | goto err; |
601 | 0 | } |
602 | | |
603 | 0 | if (group->meth->field_encode == 0) { |
604 | | /* field_sqr works on standard representation */ |
605 | 0 | if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) |
606 | 0 | goto err; |
607 | 0 | } else { |
608 | 0 | if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) |
609 | 0 | goto err; |
610 | 0 | } |
611 | | |
612 | 0 | if (x != NULL) { |
613 | | /* |
614 | | * in the Montgomery case, field_mul will cancel out Montgomery |
615 | | * factor in X: |
616 | | */ |
617 | 0 | if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx)) |
618 | 0 | goto err; |
619 | 0 | } |
620 | | |
621 | 0 | if (y != NULL) { |
622 | 0 | if (group->meth->field_encode == 0) { |
623 | | /* |
624 | | * field_mul works on standard representation |
625 | | */ |
626 | 0 | if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) |
627 | 0 | goto err; |
628 | 0 | } else { |
629 | 0 | if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) |
630 | 0 | goto err; |
631 | 0 | } |
632 | | |
633 | | /* |
634 | | * in the Montgomery case, field_mul will cancel out Montgomery |
635 | | * factor in Y: |
636 | | */ |
637 | 0 | if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) |
638 | 0 | goto err; |
639 | 0 | } |
640 | 0 | } |
641 | | |
642 | 0 | ret = 1; |
643 | |
|
644 | 0 | err: |
645 | 0 | BN_CTX_end(ctx); |
646 | 0 | if (new_ctx != NULL) |
647 | 0 | BN_CTX_free(new_ctx); |
648 | 0 | return ret; |
649 | 0 | } |
650 | | |
651 | | int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
652 | | const EC_POINT *b, BN_CTX *ctx) |
653 | 0 | { |
654 | 0 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
655 | 0 | const BIGNUM *, BN_CTX *); |
656 | 0 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
657 | 0 | const BIGNUM *p; |
658 | 0 | BN_CTX *new_ctx = NULL; |
659 | 0 | BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6; |
660 | 0 | int ret = 0; |
661 | |
|
662 | 0 | if (a == b) |
663 | 0 | return EC_POINT_dbl(group, r, a, ctx); |
664 | 0 | if (EC_POINT_is_at_infinity(group, a)) |
665 | 0 | return EC_POINT_copy(r, b); |
666 | 0 | if (EC_POINT_is_at_infinity(group, b)) |
667 | 0 | return EC_POINT_copy(r, a); |
668 | | |
669 | 0 | field_mul = group->meth->field_mul; |
670 | 0 | field_sqr = group->meth->field_sqr; |
671 | 0 | p = &group->field; |
672 | |
|
673 | 0 | if (ctx == NULL) { |
674 | 0 | ctx = new_ctx = BN_CTX_new(); |
675 | 0 | if (ctx == NULL) |
676 | 0 | return 0; |
677 | 0 | } |
678 | | |
679 | 0 | BN_CTX_start(ctx); |
680 | 0 | n0 = BN_CTX_get(ctx); |
681 | 0 | n1 = BN_CTX_get(ctx); |
682 | 0 | n2 = BN_CTX_get(ctx); |
683 | 0 | n3 = BN_CTX_get(ctx); |
684 | 0 | n4 = BN_CTX_get(ctx); |
685 | 0 | n5 = BN_CTX_get(ctx); |
686 | 0 | n6 = BN_CTX_get(ctx); |
687 | 0 | if (n6 == NULL) |
688 | 0 | goto end; |
689 | | |
690 | | /* |
691 | | * Note that in this function we must not read components of 'a' or 'b' |
692 | | * once we have written the corresponding components of 'r'. ('r' might |
693 | | * be one of 'a' or 'b'.) |
694 | | */ |
695 | | |
696 | | /* n1, n2 */ |
697 | 0 | if (b->Z_is_one) { |
698 | 0 | if (!BN_copy(n1, &a->X)) |
699 | 0 | goto end; |
700 | 0 | if (!BN_copy(n2, &a->Y)) |
701 | 0 | goto end; |
702 | | /* n1 = X_a */ |
703 | | /* n2 = Y_a */ |
704 | 0 | } else { |
705 | 0 | if (!field_sqr(group, n0, &b->Z, ctx)) |
706 | 0 | goto end; |
707 | 0 | if (!field_mul(group, n1, &a->X, n0, ctx)) |
708 | 0 | goto end; |
709 | | /* n1 = X_a * Z_b^2 */ |
710 | | |
711 | 0 | if (!field_mul(group, n0, n0, &b->Z, ctx)) |
712 | 0 | goto end; |
713 | 0 | if (!field_mul(group, n2, &a->Y, n0, ctx)) |
714 | 0 | goto end; |
715 | | /* n2 = Y_a * Z_b^3 */ |
716 | 0 | } |
717 | | |
718 | | /* n3, n4 */ |
719 | 0 | if (a->Z_is_one) { |
720 | 0 | if (!BN_copy(n3, &b->X)) |
721 | 0 | goto end; |
722 | 0 | if (!BN_copy(n4, &b->Y)) |
723 | 0 | goto end; |
724 | | /* n3 = X_b */ |
725 | | /* n4 = Y_b */ |
726 | 0 | } else { |
727 | 0 | if (!field_sqr(group, n0, &a->Z, ctx)) |
728 | 0 | goto end; |
729 | 0 | if (!field_mul(group, n3, &b->X, n0, ctx)) |
730 | 0 | goto end; |
731 | | /* n3 = X_b * Z_a^2 */ |
732 | | |
733 | 0 | if (!field_mul(group, n0, n0, &a->Z, ctx)) |
734 | 0 | goto end; |
735 | 0 | if (!field_mul(group, n4, &b->Y, n0, ctx)) |
736 | 0 | goto end; |
737 | | /* n4 = Y_b * Z_a^3 */ |
738 | 0 | } |
739 | | |
740 | | /* n5, n6 */ |
741 | 0 | if (!BN_mod_sub_quick(n5, n1, n3, p)) |
742 | 0 | goto end; |
743 | 0 | if (!BN_mod_sub_quick(n6, n2, n4, p)) |
744 | 0 | goto end; |
745 | | /* n5 = n1 - n3 */ |
746 | | /* n6 = n2 - n4 */ |
747 | | |
748 | 0 | if (BN_is_zero(n5)) { |
749 | 0 | if (BN_is_zero(n6)) { |
750 | | /* a is the same point as b */ |
751 | 0 | BN_CTX_end(ctx); |
752 | 0 | ret = EC_POINT_dbl(group, r, a, ctx); |
753 | 0 | ctx = NULL; |
754 | 0 | goto end; |
755 | 0 | } else { |
756 | | /* a is the inverse of b */ |
757 | 0 | BN_zero(&r->Z); |
758 | 0 | r->Z_is_one = 0; |
759 | 0 | ret = 1; |
760 | 0 | goto end; |
761 | 0 | } |
762 | 0 | } |
763 | | |
764 | | /* 'n7', 'n8' */ |
765 | 0 | if (!BN_mod_add_quick(n1, n1, n3, p)) |
766 | 0 | goto end; |
767 | 0 | if (!BN_mod_add_quick(n2, n2, n4, p)) |
768 | 0 | goto end; |
769 | | /* 'n7' = n1 + n3 */ |
770 | | /* 'n8' = n2 + n4 */ |
771 | | |
772 | | /* Z_r */ |
773 | 0 | if (a->Z_is_one && b->Z_is_one) { |
774 | 0 | if (!BN_copy(&r->Z, n5)) |
775 | 0 | goto end; |
776 | 0 | } else { |
777 | 0 | if (a->Z_is_one) { |
778 | 0 | if (!BN_copy(n0, &b->Z)) |
779 | 0 | goto end; |
780 | 0 | } else if (b->Z_is_one) { |
781 | 0 | if (!BN_copy(n0, &a->Z)) |
782 | 0 | goto end; |
783 | 0 | } else { |
784 | 0 | if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) |
785 | 0 | goto end; |
786 | 0 | } |
787 | 0 | if (!field_mul(group, &r->Z, n0, n5, ctx)) |
788 | 0 | goto end; |
789 | 0 | } |
790 | 0 | r->Z_is_one = 0; |
791 | | /* Z_r = Z_a * Z_b * n5 */ |
792 | | |
793 | | /* X_r */ |
794 | 0 | if (!field_sqr(group, n0, n6, ctx)) |
795 | 0 | goto end; |
796 | 0 | if (!field_sqr(group, n4, n5, ctx)) |
797 | 0 | goto end; |
798 | 0 | if (!field_mul(group, n3, n1, n4, ctx)) |
799 | 0 | goto end; |
800 | 0 | if (!BN_mod_sub_quick(&r->X, n0, n3, p)) |
801 | 0 | goto end; |
802 | | /* X_r = n6^2 - n5^2 * 'n7' */ |
803 | | |
804 | | /* 'n9' */ |
805 | 0 | if (!BN_mod_lshift1_quick(n0, &r->X, p)) |
806 | 0 | goto end; |
807 | 0 | if (!BN_mod_sub_quick(n0, n3, n0, p)) |
808 | 0 | goto end; |
809 | | /* n9 = n5^2 * 'n7' - 2 * X_r */ |
810 | | |
811 | | /* Y_r */ |
812 | 0 | if (!field_mul(group, n0, n0, n6, ctx)) |
813 | 0 | goto end; |
814 | 0 | if (!field_mul(group, n5, n4, n5, ctx)) |
815 | 0 | goto end; /* now n5 is n5^3 */ |
816 | 0 | if (!field_mul(group, n1, n2, n5, ctx)) |
817 | 0 | goto end; |
818 | 0 | if (!BN_mod_sub_quick(n0, n0, n1, p)) |
819 | 0 | goto end; |
820 | 0 | if (BN_is_odd(n0)) |
821 | 0 | if (!BN_add(n0, n0, p)) |
822 | 0 | goto end; |
823 | | /* now 0 <= n0 < 2*p, and n0 is even */ |
824 | 0 | if (!BN_rshift1(&r->Y, n0)) |
825 | 0 | goto end; |
826 | | /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */ |
827 | | |
828 | 0 | ret = 1; |
829 | |
|
830 | 0 | end: |
831 | 0 | if (ctx) /* otherwise we already called BN_CTX_end */ |
832 | 0 | BN_CTX_end(ctx); |
833 | 0 | if (new_ctx != NULL) |
834 | 0 | BN_CTX_free(new_ctx); |
835 | 0 | return ret; |
836 | 0 | } |
837 | | |
838 | | int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
839 | | BN_CTX *ctx) |
840 | 0 | { |
841 | 0 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
842 | 0 | const BIGNUM *, BN_CTX *); |
843 | 0 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
844 | 0 | const BIGNUM *p; |
845 | 0 | BN_CTX *new_ctx = NULL; |
846 | 0 | BIGNUM *n0, *n1, *n2, *n3; |
847 | 0 | int ret = 0; |
848 | |
|
849 | 0 | if (EC_POINT_is_at_infinity(group, a)) { |
850 | 0 | BN_zero(&r->Z); |
851 | 0 | r->Z_is_one = 0; |
852 | 0 | return 1; |
853 | 0 | } |
854 | | |
855 | 0 | field_mul = group->meth->field_mul; |
856 | 0 | field_sqr = group->meth->field_sqr; |
857 | 0 | p = &group->field; |
858 | |
|
859 | 0 | if (ctx == NULL) { |
860 | 0 | ctx = new_ctx = BN_CTX_new(); |
861 | 0 | if (ctx == NULL) |
862 | 0 | return 0; |
863 | 0 | } |
864 | | |
865 | 0 | BN_CTX_start(ctx); |
866 | 0 | n0 = BN_CTX_get(ctx); |
867 | 0 | n1 = BN_CTX_get(ctx); |
868 | 0 | n2 = BN_CTX_get(ctx); |
869 | 0 | n3 = BN_CTX_get(ctx); |
870 | 0 | if (n3 == NULL) |
871 | 0 | goto err; |
872 | | |
873 | | /* |
874 | | * Note that in this function we must not read components of 'a' once we |
875 | | * have written the corresponding components of 'r'. ('r' might the same |
876 | | * as 'a'.) |
877 | | */ |
878 | | |
879 | | /* n1 */ |
880 | 0 | if (a->Z_is_one) { |
881 | 0 | if (!field_sqr(group, n0, &a->X, ctx)) |
882 | 0 | goto err; |
883 | 0 | if (!BN_mod_lshift1_quick(n1, n0, p)) |
884 | 0 | goto err; |
885 | 0 | if (!BN_mod_add_quick(n0, n0, n1, p)) |
886 | 0 | goto err; |
887 | 0 | if (!BN_mod_add_quick(n1, n0, &group->a, p)) |
888 | 0 | goto err; |
889 | | /* n1 = 3 * X_a^2 + a_curve */ |
890 | 0 | } else if (group->a_is_minus3) { |
891 | 0 | if (!field_sqr(group, n1, &a->Z, ctx)) |
892 | 0 | goto err; |
893 | 0 | if (!BN_mod_add_quick(n0, &a->X, n1, p)) |
894 | 0 | goto err; |
895 | 0 | if (!BN_mod_sub_quick(n2, &a->X, n1, p)) |
896 | 0 | goto err; |
897 | 0 | if (!field_mul(group, n1, n0, n2, ctx)) |
898 | 0 | goto err; |
899 | 0 | if (!BN_mod_lshift1_quick(n0, n1, p)) |
900 | 0 | goto err; |
901 | 0 | if (!BN_mod_add_quick(n1, n0, n1, p)) |
902 | 0 | goto err; |
903 | | /*- |
904 | | * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2) |
905 | | * = 3 * X_a^2 - 3 * Z_a^4 |
906 | | */ |
907 | 0 | } else { |
908 | 0 | if (!field_sqr(group, n0, &a->X, ctx)) |
909 | 0 | goto err; |
910 | 0 | if (!BN_mod_lshift1_quick(n1, n0, p)) |
911 | 0 | goto err; |
912 | 0 | if (!BN_mod_add_quick(n0, n0, n1, p)) |
913 | 0 | goto err; |
914 | 0 | if (!field_sqr(group, n1, &a->Z, ctx)) |
915 | 0 | goto err; |
916 | 0 | if (!field_sqr(group, n1, n1, ctx)) |
917 | 0 | goto err; |
918 | 0 | if (!field_mul(group, n1, n1, &group->a, ctx)) |
919 | 0 | goto err; |
920 | 0 | if (!BN_mod_add_quick(n1, n1, n0, p)) |
921 | 0 | goto err; |
922 | | /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */ |
923 | 0 | } |
924 | | |
925 | | /* Z_r */ |
926 | 0 | if (a->Z_is_one) { |
927 | 0 | if (!BN_copy(n0, &a->Y)) |
928 | 0 | goto err; |
929 | 0 | } else { |
930 | 0 | if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) |
931 | 0 | goto err; |
932 | 0 | } |
933 | 0 | if (!BN_mod_lshift1_quick(&r->Z, n0, p)) |
934 | 0 | goto err; |
935 | 0 | r->Z_is_one = 0; |
936 | | /* Z_r = 2 * Y_a * Z_a */ |
937 | | |
938 | | /* n2 */ |
939 | 0 | if (!field_sqr(group, n3, &a->Y, ctx)) |
940 | 0 | goto err; |
941 | 0 | if (!field_mul(group, n2, &a->X, n3, ctx)) |
942 | 0 | goto err; |
943 | 0 | if (!BN_mod_lshift_quick(n2, n2, 2, p)) |
944 | 0 | goto err; |
945 | | /* n2 = 4 * X_a * Y_a^2 */ |
946 | | |
947 | | /* X_r */ |
948 | 0 | if (!BN_mod_lshift1_quick(n0, n2, p)) |
949 | 0 | goto err; |
950 | 0 | if (!field_sqr(group, &r->X, n1, ctx)) |
951 | 0 | goto err; |
952 | 0 | if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) |
953 | 0 | goto err; |
954 | | /* X_r = n1^2 - 2 * n2 */ |
955 | | |
956 | | /* n3 */ |
957 | 0 | if (!field_sqr(group, n0, n3, ctx)) |
958 | 0 | goto err; |
959 | 0 | if (!BN_mod_lshift_quick(n3, n0, 3, p)) |
960 | 0 | goto err; |
961 | | /* n3 = 8 * Y_a^4 */ |
962 | | |
963 | | /* Y_r */ |
964 | 0 | if (!BN_mod_sub_quick(n0, n2, &r->X, p)) |
965 | 0 | goto err; |
966 | 0 | if (!field_mul(group, n0, n1, n0, ctx)) |
967 | 0 | goto err; |
968 | 0 | if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) |
969 | 0 | goto err; |
970 | | /* Y_r = n1 * (n2 - X_r) - n3 */ |
971 | | |
972 | 0 | ret = 1; |
973 | |
|
974 | 0 | err: |
975 | 0 | BN_CTX_end(ctx); |
976 | 0 | if (new_ctx != NULL) |
977 | 0 | BN_CTX_free(new_ctx); |
978 | 0 | return ret; |
979 | 0 | } |
980 | | |
981 | | int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
982 | 0 | { |
983 | 0 | if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) |
984 | | /* point is its own inverse */ |
985 | 0 | return 1; |
986 | | |
987 | 0 | return BN_usub(&point->Y, &group->field, &point->Y); |
988 | 0 | } |
989 | | |
990 | | int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
991 | 0 | { |
992 | 0 | return BN_is_zero(&point->Z); |
993 | 0 | } |
994 | | |
995 | | int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
996 | | BN_CTX *ctx) |
997 | 0 | { |
998 | 0 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
999 | 0 | const BIGNUM *, BN_CTX *); |
1000 | 0 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
1001 | 0 | const BIGNUM *p; |
1002 | 0 | BN_CTX *new_ctx = NULL; |
1003 | 0 | BIGNUM *rh, *tmp, *Z4, *Z6; |
1004 | 0 | int ret = -1; |
1005 | |
|
1006 | 0 | if (EC_POINT_is_at_infinity(group, point)) |
1007 | 0 | return 1; |
1008 | | |
1009 | 0 | field_mul = group->meth->field_mul; |
1010 | 0 | field_sqr = group->meth->field_sqr; |
1011 | 0 | p = &group->field; |
1012 | |
|
1013 | 0 | if (ctx == NULL) { |
1014 | 0 | ctx = new_ctx = BN_CTX_new(); |
1015 | 0 | if (ctx == NULL) |
1016 | 0 | return -1; |
1017 | 0 | } |
1018 | | |
1019 | 0 | BN_CTX_start(ctx); |
1020 | 0 | rh = BN_CTX_get(ctx); |
1021 | 0 | tmp = BN_CTX_get(ctx); |
1022 | 0 | Z4 = BN_CTX_get(ctx); |
1023 | 0 | Z6 = BN_CTX_get(ctx); |
1024 | 0 | if (Z6 == NULL) |
1025 | 0 | goto err; |
1026 | | |
1027 | | /*- |
1028 | | * We have a curve defined by a Weierstrass equation |
1029 | | * y^2 = x^3 + a*x + b. |
1030 | | * The point to consider is given in Jacobian projective coordinates |
1031 | | * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3). |
1032 | | * Substituting this and multiplying by Z^6 transforms the above equation into |
1033 | | * Y^2 = X^3 + a*X*Z^4 + b*Z^6. |
1034 | | * To test this, we add up the right-hand side in 'rh'. |
1035 | | */ |
1036 | | |
1037 | | /* rh := X^2 */ |
1038 | 0 | if (!field_sqr(group, rh, &point->X, ctx)) |
1039 | 0 | goto err; |
1040 | | |
1041 | 0 | if (!point->Z_is_one) { |
1042 | 0 | if (!field_sqr(group, tmp, &point->Z, ctx)) |
1043 | 0 | goto err; |
1044 | 0 | if (!field_sqr(group, Z4, tmp, ctx)) |
1045 | 0 | goto err; |
1046 | 0 | if (!field_mul(group, Z6, Z4, tmp, ctx)) |
1047 | 0 | goto err; |
1048 | | |
1049 | | /* rh := (rh + a*Z^4)*X */ |
1050 | 0 | if (group->a_is_minus3) { |
1051 | 0 | if (!BN_mod_lshift1_quick(tmp, Z4, p)) |
1052 | 0 | goto err; |
1053 | 0 | if (!BN_mod_add_quick(tmp, tmp, Z4, p)) |
1054 | 0 | goto err; |
1055 | 0 | if (!BN_mod_sub_quick(rh, rh, tmp, p)) |
1056 | 0 | goto err; |
1057 | 0 | if (!field_mul(group, rh, rh, &point->X, ctx)) |
1058 | 0 | goto err; |
1059 | 0 | } else { |
1060 | 0 | if (!field_mul(group, tmp, Z4, &group->a, ctx)) |
1061 | 0 | goto err; |
1062 | 0 | if (!BN_mod_add_quick(rh, rh, tmp, p)) |
1063 | 0 | goto err; |
1064 | 0 | if (!field_mul(group, rh, rh, &point->X, ctx)) |
1065 | 0 | goto err; |
1066 | 0 | } |
1067 | | |
1068 | | /* rh := rh + b*Z^6 */ |
1069 | 0 | if (!field_mul(group, tmp, &group->b, Z6, ctx)) |
1070 | 0 | goto err; |
1071 | 0 | if (!BN_mod_add_quick(rh, rh, tmp, p)) |
1072 | 0 | goto err; |
1073 | 0 | } else { |
1074 | | /* point->Z_is_one */ |
1075 | | |
1076 | | /* rh := (rh + a)*X */ |
1077 | 0 | if (!BN_mod_add_quick(rh, rh, &group->a, p)) |
1078 | 0 | goto err; |
1079 | 0 | if (!field_mul(group, rh, rh, &point->X, ctx)) |
1080 | 0 | goto err; |
1081 | | /* rh := rh + b */ |
1082 | 0 | if (!BN_mod_add_quick(rh, rh, &group->b, p)) |
1083 | 0 | goto err; |
1084 | 0 | } |
1085 | | |
1086 | | /* 'lh' := Y^2 */ |
1087 | 0 | if (!field_sqr(group, tmp, &point->Y, ctx)) |
1088 | 0 | goto err; |
1089 | | |
1090 | 0 | ret = (0 == BN_ucmp(tmp, rh)); |
1091 | |
|
1092 | 0 | err: |
1093 | 0 | BN_CTX_end(ctx); |
1094 | 0 | if (new_ctx != NULL) |
1095 | 0 | BN_CTX_free(new_ctx); |
1096 | 0 | return ret; |
1097 | 0 | } |
1098 | | |
1099 | | int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, |
1100 | | const EC_POINT *b, BN_CTX *ctx) |
1101 | 0 | { |
1102 | | /*- |
1103 | | * return values: |
1104 | | * -1 error |
1105 | | * 0 equal (in affine coordinates) |
1106 | | * 1 not equal |
1107 | | */ |
1108 | |
|
1109 | 0 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
1110 | 0 | const BIGNUM *, BN_CTX *); |
1111 | 0 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
1112 | 0 | BN_CTX *new_ctx = NULL; |
1113 | 0 | BIGNUM *tmp1, *tmp2, *Za23, *Zb23; |
1114 | 0 | const BIGNUM *tmp1_, *tmp2_; |
1115 | 0 | int ret = -1; |
1116 | |
|
1117 | 0 | if (EC_POINT_is_at_infinity(group, a)) { |
1118 | 0 | return EC_POINT_is_at_infinity(group, b) ? 0 : 1; |
1119 | 0 | } |
1120 | | |
1121 | 0 | if (EC_POINT_is_at_infinity(group, b)) |
1122 | 0 | return 1; |
1123 | | |
1124 | 0 | if (a->Z_is_one && b->Z_is_one) { |
1125 | 0 | return ((BN_cmp(&a->X, &b->X) == 0) |
1126 | 0 | && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1; |
1127 | 0 | } |
1128 | | |
1129 | 0 | field_mul = group->meth->field_mul; |
1130 | 0 | field_sqr = group->meth->field_sqr; |
1131 | |
|
1132 | 0 | if (ctx == NULL) { |
1133 | 0 | ctx = new_ctx = BN_CTX_new(); |
1134 | 0 | if (ctx == NULL) |
1135 | 0 | return -1; |
1136 | 0 | } |
1137 | | |
1138 | 0 | BN_CTX_start(ctx); |
1139 | 0 | tmp1 = BN_CTX_get(ctx); |
1140 | 0 | tmp2 = BN_CTX_get(ctx); |
1141 | 0 | Za23 = BN_CTX_get(ctx); |
1142 | 0 | Zb23 = BN_CTX_get(ctx); |
1143 | 0 | if (Zb23 == NULL) |
1144 | 0 | goto end; |
1145 | | |
1146 | | /*- |
1147 | | * We have to decide whether |
1148 | | * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3), |
1149 | | * or equivalently, whether |
1150 | | * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3). |
1151 | | */ |
1152 | | |
1153 | 0 | if (!b->Z_is_one) { |
1154 | 0 | if (!field_sqr(group, Zb23, &b->Z, ctx)) |
1155 | 0 | goto end; |
1156 | 0 | if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) |
1157 | 0 | goto end; |
1158 | 0 | tmp1_ = tmp1; |
1159 | 0 | } else |
1160 | 0 | tmp1_ = &a->X; |
1161 | 0 | if (!a->Z_is_one) { |
1162 | 0 | if (!field_sqr(group, Za23, &a->Z, ctx)) |
1163 | 0 | goto end; |
1164 | 0 | if (!field_mul(group, tmp2, &b->X, Za23, ctx)) |
1165 | 0 | goto end; |
1166 | 0 | tmp2_ = tmp2; |
1167 | 0 | } else |
1168 | 0 | tmp2_ = &b->X; |
1169 | | |
1170 | | /* compare X_a*Z_b^2 with X_b*Z_a^2 */ |
1171 | 0 | if (BN_cmp(tmp1_, tmp2_) != 0) { |
1172 | 0 | ret = 1; /* points differ */ |
1173 | 0 | goto end; |
1174 | 0 | } |
1175 | | |
1176 | 0 | if (!b->Z_is_one) { |
1177 | 0 | if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) |
1178 | 0 | goto end; |
1179 | 0 | if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) |
1180 | 0 | goto end; |
1181 | | /* tmp1_ = tmp1 */ |
1182 | 0 | } else |
1183 | 0 | tmp1_ = &a->Y; |
1184 | 0 | if (!a->Z_is_one) { |
1185 | 0 | if (!field_mul(group, Za23, Za23, &a->Z, ctx)) |
1186 | 0 | goto end; |
1187 | 0 | if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) |
1188 | 0 | goto end; |
1189 | | /* tmp2_ = tmp2 */ |
1190 | 0 | } else |
1191 | 0 | tmp2_ = &b->Y; |
1192 | | |
1193 | | /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */ |
1194 | 0 | if (BN_cmp(tmp1_, tmp2_) != 0) { |
1195 | 0 | ret = 1; /* points differ */ |
1196 | 0 | goto end; |
1197 | 0 | } |
1198 | | |
1199 | | /* points are equal */ |
1200 | 0 | ret = 0; |
1201 | |
|
1202 | 0 | end: |
1203 | 0 | BN_CTX_end(ctx); |
1204 | 0 | if (new_ctx != NULL) |
1205 | 0 | BN_CTX_free(new_ctx); |
1206 | 0 | return ret; |
1207 | 0 | } |
1208 | | |
1209 | | int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, |
1210 | | BN_CTX *ctx) |
1211 | 0 | { |
1212 | 0 | BN_CTX *new_ctx = NULL; |
1213 | 0 | BIGNUM *x, *y; |
1214 | 0 | int ret = 0; |
1215 | |
|
1216 | 0 | if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) |
1217 | 0 | return 1; |
1218 | | |
1219 | 0 | if (ctx == NULL) { |
1220 | 0 | ctx = new_ctx = BN_CTX_new(); |
1221 | 0 | if (ctx == NULL) |
1222 | 0 | return 0; |
1223 | 0 | } |
1224 | | |
1225 | 0 | BN_CTX_start(ctx); |
1226 | 0 | x = BN_CTX_get(ctx); |
1227 | 0 | y = BN_CTX_get(ctx); |
1228 | 0 | if (y == NULL) |
1229 | 0 | goto err; |
1230 | | |
1231 | 0 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) |
1232 | 0 | goto err; |
1233 | 0 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) |
1234 | 0 | goto err; |
1235 | 0 | if (!point->Z_is_one) { |
1236 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR); |
1237 | 0 | goto err; |
1238 | 0 | } |
1239 | | |
1240 | 0 | ret = 1; |
1241 | |
|
1242 | 0 | err: |
1243 | 0 | BN_CTX_end(ctx); |
1244 | 0 | if (new_ctx != NULL) |
1245 | 0 | BN_CTX_free(new_ctx); |
1246 | 0 | return ret; |
1247 | 0 | } |
1248 | | |
1249 | | int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, |
1250 | | EC_POINT *points[], BN_CTX *ctx) |
1251 | 0 | { |
1252 | 0 | BN_CTX *new_ctx = NULL; |
1253 | 0 | BIGNUM *tmp, *tmp_Z; |
1254 | 0 | BIGNUM **prod_Z = NULL; |
1255 | 0 | size_t i; |
1256 | 0 | int ret = 0; |
1257 | |
|
1258 | 0 | if (num == 0) |
1259 | 0 | return 1; |
1260 | | |
1261 | 0 | if (ctx == NULL) { |
1262 | 0 | ctx = new_ctx = BN_CTX_new(); |
1263 | 0 | if (ctx == NULL) |
1264 | 0 | return 0; |
1265 | 0 | } |
1266 | | |
1267 | 0 | BN_CTX_start(ctx); |
1268 | 0 | tmp = BN_CTX_get(ctx); |
1269 | 0 | tmp_Z = BN_CTX_get(ctx); |
1270 | 0 | if (tmp == NULL || tmp_Z == NULL) |
1271 | 0 | goto err; |
1272 | | |
1273 | 0 | prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]); |
1274 | 0 | if (prod_Z == NULL) |
1275 | 0 | goto err; |
1276 | 0 | for (i = 0; i < num; i++) { |
1277 | 0 | prod_Z[i] = BN_new(); |
1278 | 0 | if (prod_Z[i] == NULL) |
1279 | 0 | goto err; |
1280 | 0 | } |
1281 | | |
1282 | | /* |
1283 | | * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z, |
1284 | | * skipping any zero-valued inputs (pretend that they're 1). |
1285 | | */ |
1286 | | |
1287 | 0 | if (!BN_is_zero(&points[0]->Z)) { |
1288 | 0 | if (!BN_copy(prod_Z[0], &points[0]->Z)) |
1289 | 0 | goto err; |
1290 | 0 | } else { |
1291 | 0 | if (group->meth->field_set_to_one != 0) { |
1292 | 0 | if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) |
1293 | 0 | goto err; |
1294 | 0 | } else { |
1295 | 0 | if (!BN_one(prod_Z[0])) |
1296 | 0 | goto err; |
1297 | 0 | } |
1298 | 0 | } |
1299 | | |
1300 | 0 | for (i = 1; i < num; i++) { |
1301 | 0 | if (!BN_is_zero(&points[i]->Z)) { |
1302 | 0 | if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1], |
1303 | 0 | &points[i]->Z, ctx)) |
1304 | 0 | goto err; |
1305 | 0 | } else { |
1306 | 0 | if (!BN_copy(prod_Z[i], prod_Z[i - 1])) |
1307 | 0 | goto err; |
1308 | 0 | } |
1309 | 0 | } |
1310 | | |
1311 | | /* |
1312 | | * Now use a single explicit inversion to replace every non-zero |
1313 | | * points[i]->Z by its inverse. |
1314 | | */ |
1315 | | |
1316 | 0 | if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx)) { |
1317 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB); |
1318 | 0 | goto err; |
1319 | 0 | } |
1320 | 0 | if (group->meth->field_encode != 0) { |
1321 | | /* |
1322 | | * In the Montgomery case, we just turned R*H (representing H) into |
1323 | | * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to |
1324 | | * multiply by the Montgomery factor twice. |
1325 | | */ |
1326 | 0 | if (!group->meth->field_encode(group, tmp, tmp, ctx)) |
1327 | 0 | goto err; |
1328 | 0 | if (!group->meth->field_encode(group, tmp, tmp, ctx)) |
1329 | 0 | goto err; |
1330 | 0 | } |
1331 | | |
1332 | 0 | for (i = num - 1; i > 0; --i) { |
1333 | | /* |
1334 | | * Loop invariant: tmp is the product of the inverses of points[0]->Z |
1335 | | * .. points[i]->Z (zero-valued inputs skipped). |
1336 | | */ |
1337 | 0 | if (!BN_is_zero(&points[i]->Z)) { |
1338 | | /* |
1339 | | * Set tmp_Z to the inverse of points[i]->Z (as product of Z |
1340 | | * inverses 0 .. i, Z values 0 .. i - 1). |
1341 | | */ |
1342 | 0 | if (!group-> |
1343 | 0 | meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx)) |
1344 | 0 | goto err; |
1345 | | /* |
1346 | | * Update tmp to satisfy the loop invariant for i - 1. |
1347 | | */ |
1348 | 0 | if (!group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx)) |
1349 | 0 | goto err; |
1350 | | /* Replace points[i]->Z by its inverse. */ |
1351 | 0 | if (!BN_copy(&points[i]->Z, tmp_Z)) |
1352 | 0 | goto err; |
1353 | 0 | } |
1354 | 0 | } |
1355 | | |
1356 | 0 | if (!BN_is_zero(&points[0]->Z)) { |
1357 | | /* Replace points[0]->Z by its inverse. */ |
1358 | 0 | if (!BN_copy(&points[0]->Z, tmp)) |
1359 | 0 | goto err; |
1360 | 0 | } |
1361 | | |
1362 | | /* Finally, fix up the X and Y coordinates for all points. */ |
1363 | | |
1364 | 0 | for (i = 0; i < num; i++) { |
1365 | 0 | EC_POINT *p = points[i]; |
1366 | |
|
1367 | 0 | if (!BN_is_zero(&p->Z)) { |
1368 | | /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */ |
1369 | |
|
1370 | 0 | if (!group->meth->field_sqr(group, tmp, &p->Z, ctx)) |
1371 | 0 | goto err; |
1372 | 0 | if (!group->meth->field_mul(group, &p->X, &p->X, tmp, ctx)) |
1373 | 0 | goto err; |
1374 | | |
1375 | 0 | if (!group->meth->field_mul(group, tmp, tmp, &p->Z, ctx)) |
1376 | 0 | goto err; |
1377 | 0 | if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) |
1378 | 0 | goto err; |
1379 | | |
1380 | 0 | if (group->meth->field_set_to_one != 0) { |
1381 | 0 | if (!group->meth->field_set_to_one(group, &p->Z, ctx)) |
1382 | 0 | goto err; |
1383 | 0 | } else { |
1384 | 0 | if (!BN_one(&p->Z)) |
1385 | 0 | goto err; |
1386 | 0 | } |
1387 | 0 | p->Z_is_one = 1; |
1388 | 0 | } |
1389 | 0 | } |
1390 | | |
1391 | 0 | ret = 1; |
1392 | |
|
1393 | 0 | err: |
1394 | 0 | BN_CTX_end(ctx); |
1395 | 0 | if (new_ctx != NULL) |
1396 | 0 | BN_CTX_free(new_ctx); |
1397 | 0 | if (prod_Z != NULL) { |
1398 | 0 | for (i = 0; i < num; i++) { |
1399 | 0 | if (prod_Z[i] == NULL) |
1400 | 0 | break; |
1401 | 0 | BN_clear_free(prod_Z[i]); |
1402 | 0 | } |
1403 | 0 | OPENSSL_free(prod_Z); |
1404 | 0 | } |
1405 | 0 | return ret; |
1406 | 0 | } |
1407 | | |
1408 | | int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
1409 | | const BIGNUM *b, BN_CTX *ctx) |
1410 | 0 | { |
1411 | 0 | return BN_mod_mul(r, a, b, &group->field, ctx); |
1412 | 0 | } |
1413 | | |
1414 | | int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
1415 | | BN_CTX *ctx) |
1416 | 0 | { |
1417 | 0 | return BN_mod_sqr(r, a, &group->field, ctx); |
1418 | 0 | } |