blob: ab011ca3f3289787d57e56a805d59ca7146e6de9 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55/* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68#include <openssl/ec.h>
69
70#include <string.h>
71
72#include <openssl/bn.h>
73#include <openssl/err.h>
74#include <openssl/mem.h>
75
76#include "internal.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070077#include "../../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080078
79
Robert Sloan8f860b12017-08-28 07:37:06 -070080// Most method functions in this file are designed to work with non-trivial
81// representations of field elements if necessary (see ecp_mont.c): while
82// standard modular addition and subtraction are used, the field_mul and
83// field_sqr methods will be used for multiplication, and field_encode and
84// field_decode (if defined) will be used for converting between
85// representations.
86//
87// Functions here specifically assume that if a non-trivial representation is
88// used, it is a Montgomery representation (i.e. 'encoding' means multiplying
89// by some factor R).
Adam Langleyd9e397b2015-01-22 14:27:53 -080090
91int ec_GFp_simple_group_init(EC_GROUP *group) {
92 BN_init(&group->field);
93 BN_init(&group->a);
94 BN_init(&group->b);
David Benjamin4969cc92016-04-22 15:02:23 -040095 BN_init(&group->one);
Adam Langleyd9e397b2015-01-22 14:27:53 -080096 group->a_is_minus3 = 0;
97 return 1;
98}
99
100void ec_GFp_simple_group_finish(EC_GROUP *group) {
101 BN_free(&group->field);
102 BN_free(&group->a);
103 BN_free(&group->b);
David Benjamin4969cc92016-04-22 15:02:23 -0400104 BN_free(&group->one);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800105}
106
Adam Langleyd9e397b2015-01-22 14:27:53 -0800107int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
108 const BIGNUM *a, const BIGNUM *b,
109 BN_CTX *ctx) {
110 int ret = 0;
111 BN_CTX *new_ctx = NULL;
112 BIGNUM *tmp_a;
113
Robert Sloan8f860b12017-08-28 07:37:06 -0700114 // p must be a prime > 3
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115 if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000116 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800117 return 0;
118 }
119
120 if (ctx == NULL) {
121 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700122 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700124 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125 }
126
127 BN_CTX_start(ctx);
128 tmp_a = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700129 if (tmp_a == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700131 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800132
Robert Sloan8f860b12017-08-28 07:37:06 -0700133 // group->field
Adam Langleye9ada862015-05-11 17:20:37 -0700134 if (!BN_copy(&group->field, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700136 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800137 BN_set_negative(&group->field, 0);
138
Robert Sloan8f860b12017-08-28 07:37:06 -0700139 // group->a
Adam Langleye9ada862015-05-11 17:20:37 -0700140 if (!BN_nnmod(tmp_a, a, p, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800141 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700142 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143 if (group->meth->field_encode) {
Adam Langleye9ada862015-05-11 17:20:37 -0700144 if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800145 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700146 }
147 } else if (!BN_copy(&group->a, tmp_a)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800148 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700149 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150
Robert Sloan8f860b12017-08-28 07:37:06 -0700151 // group->b
Adam Langleye9ada862015-05-11 17:20:37 -0700152 if (!BN_nnmod(&group->b, b, p, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700154 }
155 if (group->meth->field_encode &&
156 !group->meth->field_encode(group, &group->b, &group->b, ctx)) {
157 goto err;
158 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159
Robert Sloan8f860b12017-08-28 07:37:06 -0700160 // group->a_is_minus3
Adam Langleye9ada862015-05-11 17:20:37 -0700161 if (!BN_add_word(tmp_a, 3)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700163 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164 group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
165
David Benjamin4969cc92016-04-22 15:02:23 -0400166 if (group->meth->field_encode != NULL) {
167 if (!group->meth->field_encode(group, &group->one, BN_value_one(), ctx)) {
168 goto err;
169 }
170 } else if (!BN_copy(&group->one, BN_value_one())) {
171 goto err;
172 }
173
Adam Langleyd9e397b2015-01-22 14:27:53 -0800174 ret = 1;
175
176err:
177 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700178 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 return ret;
180}
181
182int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
183 BIGNUM *b, BN_CTX *ctx) {
184 int ret = 0;
185 BN_CTX *new_ctx = NULL;
186
Adam Langleye9ada862015-05-11 17:20:37 -0700187 if (p != NULL && !BN_copy(p, &group->field)) {
188 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800189 }
190
191 if (a != NULL || b != NULL) {
192 if (group->meth->field_decode) {
193 if (ctx == NULL) {
194 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700195 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700197 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198 }
Adam Langleye9ada862015-05-11 17:20:37 -0700199 if (a != NULL && !group->meth->field_decode(group, a, &group->a, ctx)) {
200 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201 }
Adam Langleye9ada862015-05-11 17:20:37 -0700202 if (b != NULL && !group->meth->field_decode(group, b, &group->b, ctx)) {
203 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204 }
205 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700206 if (a != NULL && !BN_copy(a, &group->a)) {
207 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208 }
Adam Langleye9ada862015-05-11 17:20:37 -0700209 if (b != NULL && !BN_copy(b, &group->b)) {
210 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 }
212 }
213 }
214
215 ret = 1;
216
217err:
Adam Langleye9ada862015-05-11 17:20:37 -0700218 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 return ret;
220}
221
Kenny Roote99801b2015-11-06 15:31:15 -0800222unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223 return BN_num_bits(&group->field);
224}
225
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226int ec_GFp_simple_point_init(EC_POINT *point) {
227 BN_init(&point->X);
228 BN_init(&point->Y);
229 BN_init(&point->Z);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800230
231 return 1;
232}
233
234void ec_GFp_simple_point_finish(EC_POINT *point) {
235 BN_free(&point->X);
236 BN_free(&point->Y);
237 BN_free(&point->Z);
238}
239
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
Adam Langleye9ada862015-05-11 17:20:37 -0700241 if (!BN_copy(&dest->X, &src->X) ||
242 !BN_copy(&dest->Y, &src->Y) ||
243 !BN_copy(&dest->Z, &src->Z)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700245 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246
247 return 1;
248}
249
250int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
251 EC_POINT *point) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252 BN_zero(&point->Z);
253 return 1;
254}
255
David Benjamin4969cc92016-04-22 15:02:23 -0400256static int set_Jprojective_coordinate_GFp(const EC_GROUP *group, BIGNUM *out,
257 const BIGNUM *in, BN_CTX *ctx) {
258 if (in == NULL) {
259 return 1;
260 }
261 if (BN_is_negative(in) ||
262 BN_cmp(in, &group->field) >= 0) {
263 OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
264 return 0;
265 }
266 if (group->meth->field_encode) {
267 return group->meth->field_encode(group, out, in, ctx);
268 }
269 return BN_copy(out, in) != NULL;
270}
271
Robert Sloan0db7f542018-01-16 15:48:33 -0800272int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
273 EC_POINT *point, const BIGNUM *x,
274 const BIGNUM *y, BN_CTX *ctx) {
275 if (x == NULL || y == NULL) {
276 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
277 return 0;
278 }
279
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280 BN_CTX *new_ctx = NULL;
281 int ret = 0;
282
283 if (ctx == NULL) {
284 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700285 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800286 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700287 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800288 }
289
David Benjamin4969cc92016-04-22 15:02:23 -0400290 if (!set_Jprojective_coordinate_GFp(group, &point->X, x, ctx) ||
291 !set_Jprojective_coordinate_GFp(group, &point->Y, y, ctx) ||
Robert Sloan0db7f542018-01-16 15:48:33 -0800292 !BN_copy(&point->Z, &group->one)) {
David Benjamin4969cc92016-04-22 15:02:23 -0400293 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800294 }
295
296 ret = 1;
297
298err:
Adam Langleye9ada862015-05-11 17:20:37 -0700299 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800300 return ret;
301}
302
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
304 const EC_POINT *b, BN_CTX *ctx) {
305 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
306 BN_CTX *);
307 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
308 const BIGNUM *p;
309 BN_CTX *new_ctx = NULL;
310 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
311 int ret = 0;
312
Adam Langleye9ada862015-05-11 17:20:37 -0700313 if (a == b) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800314 return EC_POINT_dbl(group, r, a, ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700315 }
316 if (EC_POINT_is_at_infinity(group, a)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317 return EC_POINT_copy(r, b);
Adam Langleye9ada862015-05-11 17:20:37 -0700318 }
319 if (EC_POINT_is_at_infinity(group, b)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800320 return EC_POINT_copy(r, a);
Adam Langleye9ada862015-05-11 17:20:37 -0700321 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800322
323 field_mul = group->meth->field_mul;
324 field_sqr = group->meth->field_sqr;
325 p = &group->field;
326
327 if (ctx == NULL) {
328 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700329 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700331 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 }
333
334 BN_CTX_start(ctx);
335 n0 = BN_CTX_get(ctx);
336 n1 = BN_CTX_get(ctx);
337 n2 = BN_CTX_get(ctx);
338 n3 = BN_CTX_get(ctx);
339 n4 = BN_CTX_get(ctx);
340 n5 = BN_CTX_get(ctx);
341 n6 = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700342 if (n6 == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700344 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345
Robert Sloan8f860b12017-08-28 07:37:06 -0700346 // Note that in this function we must not read components of 'a' or 'b'
347 // once we have written the corresponding components of 'r'.
348 // ('r' might be one of 'a' or 'b'.)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800349
Robert Sloan8f860b12017-08-28 07:37:06 -0700350 // n1, n2
David Benjamin4969cc92016-04-22 15:02:23 -0400351 int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
352
353 if (b_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700354 if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700356 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700357 // n1 = X_a
358 // n2 = Y_a
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700360 if (!field_sqr(group, n0, &b->Z, ctx) ||
361 !field_mul(group, n1, &a->X, n0, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700363 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700364 // n1 = X_a * Z_b^2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800365
Adam Langleye9ada862015-05-11 17:20:37 -0700366 if (!field_mul(group, n0, n0, &b->Z, ctx) ||
367 !field_mul(group, n2, &a->Y, n0, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800368 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700369 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700370 // n2 = Y_a * Z_b^3
Adam Langleyd9e397b2015-01-22 14:27:53 -0800371 }
372
Robert Sloan8f860b12017-08-28 07:37:06 -0700373 // n3, n4
David Benjamin4969cc92016-04-22 15:02:23 -0400374 int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
375 if (a_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700376 if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800377 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700378 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700379 // n3 = X_b
380 // n4 = Y_b
Adam Langleyd9e397b2015-01-22 14:27:53 -0800381 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700382 if (!field_sqr(group, n0, &a->Z, ctx) ||
383 !field_mul(group, n3, &b->X, n0, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800384 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700385 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700386 // n3 = X_b * Z_a^2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800387
Adam Langleye9ada862015-05-11 17:20:37 -0700388 if (!field_mul(group, n0, n0, &a->Z, ctx) ||
389 !field_mul(group, n4, &b->Y, n0, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700391 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700392 // n4 = Y_b * Z_a^3
Adam Langleyd9e397b2015-01-22 14:27:53 -0800393 }
394
Robert Sloan8f860b12017-08-28 07:37:06 -0700395 // n5, n6
Adam Langleye9ada862015-05-11 17:20:37 -0700396 if (!BN_mod_sub_quick(n5, n1, n3, p) ||
397 !BN_mod_sub_quick(n6, n2, n4, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700399 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700400 // n5 = n1 - n3
401 // n6 = n2 - n4
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402
403 if (BN_is_zero(n5)) {
404 if (BN_is_zero(n6)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700405 // a is the same point as b
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406 BN_CTX_end(ctx);
407 ret = EC_POINT_dbl(group, r, a, ctx);
408 ctx = NULL;
409 goto end;
410 } else {
Robert Sloan8f860b12017-08-28 07:37:06 -0700411 // a is the inverse of b
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 BN_zero(&r->Z);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800413 ret = 1;
414 goto end;
415 }
416 }
417
Robert Sloan8f860b12017-08-28 07:37:06 -0700418 // 'n7', 'n8'
Adam Langleye9ada862015-05-11 17:20:37 -0700419 if (!BN_mod_add_quick(n1, n1, n3, p) ||
420 !BN_mod_add_quick(n2, n2, n4, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800421 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700422 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700423 // 'n7' = n1 + n3
424 // 'n8' = n2 + n4
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425
Robert Sloan8f860b12017-08-28 07:37:06 -0700426 // Z_r
David Benjamin4969cc92016-04-22 15:02:23 -0400427 if (a_Z_is_one && b_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700428 if (!BN_copy(&r->Z, n5)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700430 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800431 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400432 if (a_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700433 if (!BN_copy(n0, &b->Z)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800434 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700435 }
David Benjamin4969cc92016-04-22 15:02:23 -0400436 } else if (b_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700437 if (!BN_copy(n0, &a->Z)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800438 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700439 }
440 } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700442 }
443 if (!field_mul(group, &r->Z, n0, n5, ctx)) {
444 goto end;
445 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800446 }
David Benjamin4969cc92016-04-22 15:02:23 -0400447
Robert Sloan8f860b12017-08-28 07:37:06 -0700448 // Z_r = Z_a * Z_b * n5
Adam Langleyd9e397b2015-01-22 14:27:53 -0800449
Robert Sloan8f860b12017-08-28 07:37:06 -0700450 // X_r
Adam Langleye9ada862015-05-11 17:20:37 -0700451 if (!field_sqr(group, n0, n6, ctx) ||
452 !field_sqr(group, n4, n5, ctx) ||
453 !field_mul(group, n3, n1, n4, ctx) ||
454 !BN_mod_sub_quick(&r->X, n0, n3, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800455 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700456 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700457 // X_r = n6^2 - n5^2 * 'n7'
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458
Robert Sloan8f860b12017-08-28 07:37:06 -0700459 // 'n9'
Adam Langleye9ada862015-05-11 17:20:37 -0700460 if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
461 !BN_mod_sub_quick(n0, n3, n0, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700463 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700464 // n9 = n5^2 * 'n7' - 2 * X_r
Adam Langleyd9e397b2015-01-22 14:27:53 -0800465
Robert Sloan8f860b12017-08-28 07:37:06 -0700466 // Y_r
Adam Langleye9ada862015-05-11 17:20:37 -0700467 if (!field_mul(group, n0, n0, n6, ctx) ||
468 !field_mul(group, n5, n4, n5, ctx)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700469 goto end; // now n5 is n5^3
Adam Langleye9ada862015-05-11 17:20:37 -0700470 }
471 if (!field_mul(group, n1, n2, n5, ctx) ||
472 !BN_mod_sub_quick(n0, n0, n1, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800473 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700474 }
475 if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800476 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700477 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700478 // now 0 <= n0 < 2*p, and n0 is even
Adam Langleye9ada862015-05-11 17:20:37 -0700479 if (!BN_rshift1(&r->Y, n0)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800480 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700481 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700482 // Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800483
484 ret = 1;
485
486end:
Adam Langleye9ada862015-05-11 17:20:37 -0700487 if (ctx) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700488 // otherwise we already called BN_CTX_end
Adam Langleyd9e397b2015-01-22 14:27:53 -0800489 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700490 }
491 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800492 return ret;
493}
494
495int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
496 BN_CTX *ctx) {
497 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
498 BN_CTX *);
499 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
500 const BIGNUM *p;
501 BN_CTX *new_ctx = NULL;
502 BIGNUM *n0, *n1, *n2, *n3;
503 int ret = 0;
504
505 if (EC_POINT_is_at_infinity(group, a)) {
506 BN_zero(&r->Z);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800507 return 1;
508 }
509
510 field_mul = group->meth->field_mul;
511 field_sqr = group->meth->field_sqr;
512 p = &group->field;
513
514 if (ctx == NULL) {
515 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700516 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700518 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800519 }
520
521 BN_CTX_start(ctx);
522 n0 = BN_CTX_get(ctx);
523 n1 = BN_CTX_get(ctx);
524 n2 = BN_CTX_get(ctx);
525 n3 = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700526 if (n3 == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800527 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700528 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800529
Robert Sloan8f860b12017-08-28 07:37:06 -0700530 // Note that in this function we must not read components of 'a'
531 // once we have written the corresponding components of 'r'.
532 // ('r' might the same as 'a'.)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800533
Robert Sloan8f860b12017-08-28 07:37:06 -0700534 // n1
David Benjamin4969cc92016-04-22 15:02:23 -0400535 if (BN_cmp(&a->Z, &group->one) == 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700536 if (!field_sqr(group, n0, &a->X, ctx) ||
537 !BN_mod_lshift1_quick(n1, n0, p) ||
538 !BN_mod_add_quick(n0, n0, n1, p) ||
539 !BN_mod_add_quick(n1, n0, &group->a, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800540 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700541 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700542 // n1 = 3 * X_a^2 + a_curve
Adam Langleyd9e397b2015-01-22 14:27:53 -0800543 } else if (group->a_is_minus3) {
Adam Langleye9ada862015-05-11 17:20:37 -0700544 if (!field_sqr(group, n1, &a->Z, ctx) ||
545 !BN_mod_add_quick(n0, &a->X, n1, p) ||
546 !BN_mod_sub_quick(n2, &a->X, n1, p) ||
547 !field_mul(group, n1, n0, n2, ctx) ||
548 !BN_mod_lshift1_quick(n0, n1, p) ||
549 !BN_mod_add_quick(n1, n0, n1, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800550 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700551 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700552 // n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
553 // = 3 * X_a^2 - 3 * Z_a^4
Adam Langleyd9e397b2015-01-22 14:27:53 -0800554 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700555 if (!field_sqr(group, n0, &a->X, ctx) ||
556 !BN_mod_lshift1_quick(n1, n0, p) ||
557 !BN_mod_add_quick(n0, n0, n1, p) ||
558 !field_sqr(group, n1, &a->Z, ctx) ||
559 !field_sqr(group, n1, n1, ctx) ||
560 !field_mul(group, n1, n1, &group->a, ctx) ||
561 !BN_mod_add_quick(n1, n1, n0, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800562 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700563 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700564 // n1 = 3 * X_a^2 + a_curve * Z_a^4
Adam Langleyd9e397b2015-01-22 14:27:53 -0800565 }
566
Robert Sloan8f860b12017-08-28 07:37:06 -0700567 // Z_r
David Benjamin4969cc92016-04-22 15:02:23 -0400568 if (BN_cmp(&a->Z, &group->one) == 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700569 if (!BN_copy(n0, &a->Y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800570 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700571 }
572 } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800573 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700574 }
575 if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
576 goto err;
577 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700578 // Z_r = 2 * Y_a * Z_a
Adam Langleyd9e397b2015-01-22 14:27:53 -0800579
Robert Sloan8f860b12017-08-28 07:37:06 -0700580 // n2
Adam Langleye9ada862015-05-11 17:20:37 -0700581 if (!field_sqr(group, n3, &a->Y, ctx) ||
582 !field_mul(group, n2, &a->X, n3, ctx) ||
583 !BN_mod_lshift_quick(n2, n2, 2, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800584 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700585 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700586 // n2 = 4 * X_a * Y_a^2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800587
Robert Sloan8f860b12017-08-28 07:37:06 -0700588 // X_r
Adam Langleye9ada862015-05-11 17:20:37 -0700589 if (!BN_mod_lshift1_quick(n0, n2, p) ||
590 !field_sqr(group, &r->X, n1, ctx) ||
591 !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800592 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700593 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700594 // X_r = n1^2 - 2 * n2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800595
Robert Sloan8f860b12017-08-28 07:37:06 -0700596 // n3
Adam Langleye9ada862015-05-11 17:20:37 -0700597 if (!field_sqr(group, n0, n3, ctx) ||
598 !BN_mod_lshift_quick(n3, n0, 3, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800599 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700600 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700601 // n3 = 8 * Y_a^4
Adam Langleyd9e397b2015-01-22 14:27:53 -0800602
Robert Sloan8f860b12017-08-28 07:37:06 -0700603 // Y_r
Adam Langleye9ada862015-05-11 17:20:37 -0700604 if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
605 !field_mul(group, n0, n1, n0, ctx) ||
606 !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800607 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700608 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700609 // Y_r = n1 * (n2 - X_r) - n3
Adam Langleyd9e397b2015-01-22 14:27:53 -0800610
611 ret = 1;
612
613err:
614 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700615 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800616 return ret;
617}
618
619int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
Adam Langleye9ada862015-05-11 17:20:37 -0700620 if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700621 // point is its own inverse
Adam Langleyd9e397b2015-01-22 14:27:53 -0800622 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700623 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800624
625 return BN_usub(&point->Y, &group->field, &point->Y);
626}
627
628int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
David Benjamin4969cc92016-04-22 15:02:23 -0400629 return BN_is_zero(&point->Z);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800630}
631
632int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
633 BN_CTX *ctx) {
634 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
635 BN_CTX *);
636 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
637 const BIGNUM *p;
638 BN_CTX *new_ctx = NULL;
639 BIGNUM *rh, *tmp, *Z4, *Z6;
David Benjamin4969cc92016-04-22 15:02:23 -0400640 int ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800641
Adam Langleye9ada862015-05-11 17:20:37 -0700642 if (EC_POINT_is_at_infinity(group, point)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800643 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700644 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800645
646 field_mul = group->meth->field_mul;
647 field_sqr = group->meth->field_sqr;
648 p = &group->field;
649
650 if (ctx == NULL) {
651 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700652 if (ctx == NULL) {
David Benjamin4969cc92016-04-22 15:02:23 -0400653 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700654 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800655 }
656
657 BN_CTX_start(ctx);
658 rh = BN_CTX_get(ctx);
659 tmp = BN_CTX_get(ctx);
660 Z4 = BN_CTX_get(ctx);
661 Z6 = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700662 if (Z6 == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800663 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700664 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800665
Robert Sloan8f860b12017-08-28 07:37:06 -0700666 // We have a curve defined by a Weierstrass equation
667 // y^2 = x^3 + a*x + b.
668 // The point to consider is given in Jacobian projective coordinates
669 // where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
670 // Substituting this and multiplying by Z^6 transforms the above equation
671 // into
672 // Y^2 = X^3 + a*X*Z^4 + b*Z^6.
673 // To test this, we add up the right-hand side in 'rh'.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674
Robert Sloan8f860b12017-08-28 07:37:06 -0700675 // rh := X^2
Adam Langleye9ada862015-05-11 17:20:37 -0700676 if (!field_sqr(group, rh, &point->X, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800677 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700678 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800679
David Benjamin4969cc92016-04-22 15:02:23 -0400680 if (BN_cmp(&point->Z, &group->one) != 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700681 if (!field_sqr(group, tmp, &point->Z, ctx) ||
682 !field_sqr(group, Z4, tmp, ctx) ||
683 !field_mul(group, Z6, Z4, tmp, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800684 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700685 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800686
Robert Sloan8f860b12017-08-28 07:37:06 -0700687 // rh := (rh + a*Z^4)*X
Adam Langleyd9e397b2015-01-22 14:27:53 -0800688 if (group->a_is_minus3) {
Adam Langleye9ada862015-05-11 17:20:37 -0700689 if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
690 !BN_mod_add_quick(tmp, tmp, Z4, p) ||
691 !BN_mod_sub_quick(rh, rh, tmp, p) ||
692 !field_mul(group, rh, rh, &point->X, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800693 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700694 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800695 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700696 if (!field_mul(group, tmp, Z4, &group->a, ctx) ||
697 !BN_mod_add_quick(rh, rh, tmp, p) ||
698 !field_mul(group, rh, rh, &point->X, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800699 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700700 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800701 }
702
Robert Sloan8f860b12017-08-28 07:37:06 -0700703 // rh := rh + b*Z^6
Adam Langleye9ada862015-05-11 17:20:37 -0700704 if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
705 !BN_mod_add_quick(rh, rh, tmp, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800706 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700707 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800708 } else {
Robert Sloan8f860b12017-08-28 07:37:06 -0700709 // rh := (rh + a)*X
Adam Langleye9ada862015-05-11 17:20:37 -0700710 if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
711 !field_mul(group, rh, rh, &point->X, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800712 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700713 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700714 // rh := rh + b
Adam Langleye9ada862015-05-11 17:20:37 -0700715 if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800716 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700717 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800718 }
719
Robert Sloan8f860b12017-08-28 07:37:06 -0700720 // 'lh' := Y^2
Adam Langleye9ada862015-05-11 17:20:37 -0700721 if (!field_sqr(group, tmp, &point->Y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800722 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700723 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800724
725 ret = (0 == BN_ucmp(tmp, rh));
726
727err:
728 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700729 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800730 return ret;
731}
732
733int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
734 const EC_POINT *b, BN_CTX *ctx) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700735 // return values:
736 // -1 error
737 // 0 equal (in affine coordinates)
738 // 1 not equal
Adam Langleyd9e397b2015-01-22 14:27:53 -0800739
740 int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
741 BN_CTX *);
742 int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
743 BN_CTX *new_ctx = NULL;
744 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
745 const BIGNUM *tmp1_, *tmp2_;
746 int ret = -1;
747
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700748 if (ec_GFp_simple_is_at_infinity(group, a)) {
749 return ec_GFp_simple_is_at_infinity(group, b) ? 0 : 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800750 }
751
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700752 if (ec_GFp_simple_is_at_infinity(group, b)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800753 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700754 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800755
David Benjamin4969cc92016-04-22 15:02:23 -0400756 int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
757 int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
758
759 if (a_Z_is_one && b_Z_is_one) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800760 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
761 }
762
763 field_mul = group->meth->field_mul;
764 field_sqr = group->meth->field_sqr;
765
766 if (ctx == NULL) {
767 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700768 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800769 return -1;
Adam Langleye9ada862015-05-11 17:20:37 -0700770 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800771 }
772
773 BN_CTX_start(ctx);
774 tmp1 = BN_CTX_get(ctx);
775 tmp2 = BN_CTX_get(ctx);
776 Za23 = BN_CTX_get(ctx);
777 Zb23 = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700778 if (Zb23 == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800779 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700780 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800781
Robert Sloan8f860b12017-08-28 07:37:06 -0700782 // We have to decide whether
783 // (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
784 // or equivalently, whether
785 // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800786
David Benjamin4969cc92016-04-22 15:02:23 -0400787 if (!b_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700788 if (!field_sqr(group, Zb23, &b->Z, ctx) ||
789 !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800790 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700791 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800792 tmp1_ = tmp1;
Adam Langleye9ada862015-05-11 17:20:37 -0700793 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800794 tmp1_ = &a->X;
Adam Langleye9ada862015-05-11 17:20:37 -0700795 }
David Benjamin4969cc92016-04-22 15:02:23 -0400796 if (!a_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700797 if (!field_sqr(group, Za23, &a->Z, ctx) ||
798 !field_mul(group, tmp2, &b->X, Za23, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800799 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700800 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800801 tmp2_ = tmp2;
Adam Langleye9ada862015-05-11 17:20:37 -0700802 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800803 tmp2_ = &b->X;
Adam Langleye9ada862015-05-11 17:20:37 -0700804 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800805
Robert Sloan8f860b12017-08-28 07:37:06 -0700806 // compare X_a*Z_b^2 with X_b*Z_a^2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800807 if (BN_cmp(tmp1_, tmp2_) != 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700808 ret = 1; // points differ
Adam Langleyd9e397b2015-01-22 14:27:53 -0800809 goto end;
810 }
811
812
David Benjamin4969cc92016-04-22 15:02:23 -0400813 if (!b_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700814 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
815 !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800816 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700817 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700818 // tmp1_ = tmp1
Adam Langleye9ada862015-05-11 17:20:37 -0700819 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800820 tmp1_ = &a->Y;
Adam Langleye9ada862015-05-11 17:20:37 -0700821 }
David Benjamin4969cc92016-04-22 15:02:23 -0400822 if (!a_Z_is_one) {
Adam Langleye9ada862015-05-11 17:20:37 -0700823 if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
824 !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800825 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700826 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700827 // tmp2_ = tmp2
Adam Langleye9ada862015-05-11 17:20:37 -0700828 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800829 tmp2_ = &b->Y;
Adam Langleye9ada862015-05-11 17:20:37 -0700830 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800831
Robert Sloan8f860b12017-08-28 07:37:06 -0700832 // compare Y_a*Z_b^3 with Y_b*Z_a^3
Adam Langleyd9e397b2015-01-22 14:27:53 -0800833 if (BN_cmp(tmp1_, tmp2_) != 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700834 ret = 1; // points differ
Adam Langleyd9e397b2015-01-22 14:27:53 -0800835 goto end;
836 }
837
Robert Sloan8f860b12017-08-28 07:37:06 -0700838 // points are equal
Adam Langleyd9e397b2015-01-22 14:27:53 -0800839 ret = 0;
840
841end:
842 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700843 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800844 return ret;
845}
846
847int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
848 BN_CTX *ctx) {
849 BN_CTX *new_ctx = NULL;
850 BIGNUM *x, *y;
851 int ret = 0;
852
David Benjamin4969cc92016-04-22 15:02:23 -0400853 if (BN_cmp(&point->Z, &group->one) == 0 ||
854 EC_POINT_is_at_infinity(group, point)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800855 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700856 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800857
858 if (ctx == NULL) {
859 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700860 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800861 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700862 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800863 }
864
865 BN_CTX_start(ctx);
866 x = BN_CTX_get(ctx);
867 y = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700868 if (y == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800869 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700870 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800871
Adam Langleye9ada862015-05-11 17:20:37 -0700872 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) ||
873 !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800874 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700875 }
David Benjamin4969cc92016-04-22 15:02:23 -0400876 if (BN_cmp(&point->Z, &group->one) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000877 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800878 goto err;
879 }
880
881 ret = 1;
882
883err:
884 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700885 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800886 return ret;
887}
888
889int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
890 EC_POINT *points[], BN_CTX *ctx) {
891 BN_CTX *new_ctx = NULL;
892 BIGNUM *tmp, *tmp_Z;
893 BIGNUM **prod_Z = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800894 int ret = 0;
895
896 if (num == 0) {
897 return 1;
898 }
899
900 if (ctx == NULL) {
901 ctx = new_ctx = BN_CTX_new();
902 if (ctx == NULL) {
903 return 0;
904 }
905 }
906
907 BN_CTX_start(ctx);
908 tmp = BN_CTX_get(ctx);
909 tmp_Z = BN_CTX_get(ctx);
910 if (tmp == NULL || tmp_Z == NULL) {
911 goto err;
912 }
913
914 prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
915 if (prod_Z == NULL) {
916 goto err;
917 }
Robert Sloan69939df2017-01-09 10:53:07 -0800918 OPENSSL_memset(prod_Z, 0, num * sizeof(prod_Z[0]));
David Benjamin7c0d06c2016-08-11 13:26:41 -0400919 for (size_t i = 0; i < num; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800920 prod_Z[i] = BN_new();
921 if (prod_Z[i] == NULL) {
922 goto err;
923 }
924 }
925
Robert Sloan8f860b12017-08-28 07:37:06 -0700926 // Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
927 // skipping any zero-valued inputs (pretend that they're 1).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800928
929 if (!BN_is_zero(&points[0]->Z)) {
930 if (!BN_copy(prod_Z[0], &points[0]->Z)) {
931 goto err;
932 }
933 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400934 if (BN_copy(prod_Z[0], &group->one) == NULL) {
935 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800936 }
937 }
938
David Benjamin7c0d06c2016-08-11 13:26:41 -0400939 for (size_t i = 1; i < num; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800940 if (!BN_is_zero(&points[i]->Z)) {
941 if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
942 &points[i]->Z, ctx)) {
943 goto err;
944 }
945 } else {
946 if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
947 goto err;
948 }
949 }
950 }
951
Robert Sloan8f860b12017-08-28 07:37:06 -0700952 // Now use a single explicit inversion to replace every non-zero points[i]->Z
953 // by its inverse. We use |BN_mod_inverse_odd| instead of doing a constant-
954 // time inversion using Fermat's Little Theorem because this function is
955 // usually only used for converting multiples of a public key point to
956 // affine, and a public key point isn't secret. If we were to use Fermat's
957 // Little Theorem then the cost of the inversion would usually be so high
958 // that converting the multiples to affine would be counterproductive.
David Benjaminc895d6b2016-08-11 13:26:41 -0400959 int no_inverse;
960 if (!BN_mod_inverse_odd(tmp, &no_inverse, prod_Z[num - 1], &group->field,
961 ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000962 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800963 goto err;
964 }
965
966 if (group->meth->field_encode != NULL) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700967 // In the Montgomery case, we just turned R*H (representing H)
968 // into 1/(R*H), but we need R*(1/H) (representing 1/H);
969 // i.e. we need to multiply by the Montgomery factor twice.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800970 if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
971 !group->meth->field_encode(group, tmp, tmp, ctx)) {
972 goto err;
973 }
974 }
975
David Benjamin7c0d06c2016-08-11 13:26:41 -0400976 for (size_t i = num - 1; i > 0; --i) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700977 // Loop invariant: tmp is the product of the inverses of
978 // points[0]->Z .. points[i]->Z (zero-valued inputs skipped).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800979 if (BN_is_zero(&points[i]->Z)) {
980 continue;
981 }
982
Robert Sloan8f860b12017-08-28 07:37:06 -0700983 // Set tmp_Z to the inverse of points[i]->Z (as product
984 // of Z inverses 0 .. i, Z values 0 .. i - 1).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800985 if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700986 // Update tmp to satisfy the loop invariant for i - 1.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800987 !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700988 // Replace points[i]->Z by its inverse.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800989 !BN_copy(&points[i]->Z, tmp_Z)) {
990 goto err;
991 }
992 }
993
Robert Sloan8f860b12017-08-28 07:37:06 -0700994 // Replace points[0]->Z by its inverse.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800995 if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
996 goto err;
997 }
998
Robert Sloan8f860b12017-08-28 07:37:06 -0700999 // Finally, fix up the X and Y coordinates for all points.
David Benjamin7c0d06c2016-08-11 13:26:41 -04001000 for (size_t i = 0; i < num; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001001 EC_POINT *p = points[i];
1002
1003 if (!BN_is_zero(&p->Z)) {
Robert Sloan8f860b12017-08-28 07:37:06 -07001004 // turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1).
Adam Langleyd9e397b2015-01-22 14:27:53 -08001005 if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
1006 !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
1007 !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
1008 !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
1009 goto err;
1010 }
1011
David Benjamin4969cc92016-04-22 15:02:23 -04001012 if (BN_copy(&p->Z, &group->one) == NULL) {
1013 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001014 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001015 }
1016 }
1017
1018 ret = 1;
1019
1020err:
1021 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -07001022 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001023 if (prod_Z != NULL) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04001024 for (size_t i = 0; i < num; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001025 if (prod_Z[i] == NULL) {
1026 break;
1027 }
1028 BN_clear_free(prod_Z[i]);
1029 }
1030 OPENSSL_free(prod_Z);
1031 }
1032
1033 return ret;
1034}
1035
1036int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1037 const BIGNUM *b, BN_CTX *ctx) {
1038 return BN_mod_mul(r, a, b, &group->field, ctx);
1039}
1040
1041int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1042 BN_CTX *ctx) {
1043 return BN_mod_sqr(r, a, &group->field, ctx);
1044}