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