/src/openssl111/crypto/ec/ecp_smpl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the OpenSSL license (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <openssl/err.h> |
12 | | #include <openssl/symhacks.h> |
13 | | |
14 | | #include "ec_local.h" |
15 | | |
16 | | const EC_METHOD *EC_GFp_simple_method(void) |
17 | 0 | { |
18 | 0 | static const EC_METHOD ret = { |
19 | 0 | EC_FLAGS_DEFAULT_OCT, |
20 | 0 | NID_X9_62_prime_field, |
21 | 0 | ec_GFp_simple_group_init, |
22 | 0 | ec_GFp_simple_group_finish, |
23 | 0 | ec_GFp_simple_group_clear_finish, |
24 | 0 | ec_GFp_simple_group_copy, |
25 | 0 | ec_GFp_simple_group_set_curve, |
26 | 0 | ec_GFp_simple_group_get_curve, |
27 | 0 | ec_GFp_simple_group_get_degree, |
28 | 0 | ec_group_simple_order_bits, |
29 | 0 | ec_GFp_simple_group_check_discriminant, |
30 | 0 | ec_GFp_simple_point_init, |
31 | 0 | ec_GFp_simple_point_finish, |
32 | 0 | ec_GFp_simple_point_clear_finish, |
33 | 0 | ec_GFp_simple_point_copy, |
34 | 0 | ec_GFp_simple_point_set_to_infinity, |
35 | 0 | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
36 | 0 | ec_GFp_simple_get_Jprojective_coordinates_GFp, |
37 | 0 | ec_GFp_simple_point_set_affine_coordinates, |
38 | 0 | ec_GFp_simple_point_get_affine_coordinates, |
39 | 0 | 0, 0, 0, |
40 | 0 | ec_GFp_simple_add, |
41 | 0 | ec_GFp_simple_dbl, |
42 | 0 | ec_GFp_simple_invert, |
43 | 0 | ec_GFp_simple_is_at_infinity, |
44 | 0 | ec_GFp_simple_is_on_curve, |
45 | 0 | ec_GFp_simple_cmp, |
46 | 0 | ec_GFp_simple_make_affine, |
47 | 0 | ec_GFp_simple_points_make_affine, |
48 | 0 | 0 /* mul */ , |
49 | 0 | 0 /* precompute_mult */ , |
50 | 0 | 0 /* have_precompute_mult */ , |
51 | 0 | ec_GFp_simple_field_mul, |
52 | 0 | ec_GFp_simple_field_sqr, |
53 | 0 | 0 /* field_div */ , |
54 | 0 | ec_GFp_simple_field_inv, |
55 | 0 | 0 /* field_encode */ , |
56 | 0 | 0 /* field_decode */ , |
57 | 0 | 0, /* field_set_to_one */ |
58 | 0 | ec_key_simple_priv2oct, |
59 | 0 | ec_key_simple_oct2priv, |
60 | 0 | 0, /* set private */ |
61 | 0 | ec_key_simple_generate_key, |
62 | 0 | ec_key_simple_check_key, |
63 | 0 | ec_key_simple_generate_public_key, |
64 | 0 | 0, /* keycopy */ |
65 | 0 | 0, /* keyfinish */ |
66 | 0 | ecdh_simple_compute_key, |
67 | 0 | 0, /* field_inverse_mod_ord */ |
68 | 0 | ec_GFp_simple_blind_coordinates, |
69 | 0 | ec_GFp_simple_ladder_pre, |
70 | 0 | ec_GFp_simple_ladder_step, |
71 | 0 | ec_GFp_simple_ladder_post |
72 | 0 | }; |
73 | |
|
74 | 0 | return &ret; |
75 | 0 | } |
76 | | |
77 | | /* |
78 | | * Most method functions in this file are designed to work with |
79 | | * non-trivial representations of field elements if necessary |
80 | | * (see ecp_mont.c): while standard modular addition and subtraction |
81 | | * are used, the field_mul and field_sqr methods will be used for |
82 | | * multiplication, and field_encode and field_decode (if defined) |
83 | | * will be used for converting between representations. |
84 | | * |
85 | | * Functions ec_GFp_simple_points_make_affine() and |
86 | | * ec_GFp_simple_point_get_affine_coordinates() specifically assume |
87 | | * that if a non-trivial representation is used, it is a Montgomery |
88 | | * representation (i.e. 'encoding' means multiplying by some factor R). |
89 | | */ |
90 | | |
91 | | int ec_GFp_simple_group_init(EC_GROUP *group) |
92 | 21.4k | { |
93 | 21.4k | group->field = BN_new(); |
94 | 21.4k | group->a = BN_new(); |
95 | 21.4k | group->b = BN_new(); |
96 | 21.4k | if (group->field == NULL || group->a == NULL || group->b == NULL) { |
97 | 0 | BN_free(group->field); |
98 | 0 | BN_free(group->a); |
99 | 0 | BN_free(group->b); |
100 | 0 | return 0; |
101 | 0 | } |
102 | 21.4k | group->a_is_minus3 = 0; |
103 | 21.4k | return 1; |
104 | 21.4k | } |
105 | | |
106 | | void ec_GFp_simple_group_finish(EC_GROUP *group) |
107 | 20.7k | { |
108 | 20.7k | BN_free(group->field); |
109 | 20.7k | BN_free(group->a); |
110 | 20.7k | BN_free(group->b); |
111 | 20.7k | } |
112 | | |
113 | | void ec_GFp_simple_group_clear_finish(EC_GROUP *group) |
114 | 630 | { |
115 | 630 | BN_clear_free(group->field); |
116 | 630 | BN_clear_free(group->a); |
117 | 630 | BN_clear_free(group->b); |
118 | 630 | } |
119 | | |
120 | | int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
121 | 9.88k | { |
122 | 9.88k | if (!BN_copy(dest->field, src->field)) |
123 | 0 | return 0; |
124 | 9.88k | if (!BN_copy(dest->a, src->a)) |
125 | 0 | return 0; |
126 | 9.88k | if (!BN_copy(dest->b, src->b)) |
127 | 0 | return 0; |
128 | | |
129 | 9.88k | dest->a_is_minus3 = src->a_is_minus3; |
130 | | |
131 | 9.88k | return 1; |
132 | 9.88k | } |
133 | | |
134 | | int ec_GFp_simple_group_set_curve(EC_GROUP *group, |
135 | | const BIGNUM *p, const BIGNUM *a, |
136 | | const BIGNUM *b, BN_CTX *ctx) |
137 | 10.9k | { |
138 | 10.9k | int ret = 0; |
139 | 10.9k | BN_CTX *new_ctx = NULL; |
140 | 10.9k | BIGNUM *tmp_a; |
141 | | |
142 | | /* p must be a prime > 3 */ |
143 | 10.9k | if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) { |
144 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD); |
145 | 0 | return 0; |
146 | 0 | } |
147 | | |
148 | 10.9k | if (ctx == NULL) { |
149 | 0 | ctx = new_ctx = BN_CTX_new(); |
150 | 0 | if (ctx == NULL) |
151 | 0 | return 0; |
152 | 0 | } |
153 | | |
154 | 10.9k | BN_CTX_start(ctx); |
155 | 10.9k | tmp_a = BN_CTX_get(ctx); |
156 | 10.9k | if (tmp_a == NULL) |
157 | 0 | goto err; |
158 | | |
159 | | /* group->field */ |
160 | 10.9k | if (!BN_copy(group->field, p)) |
161 | 0 | goto err; |
162 | 10.9k | BN_set_negative(group->field, 0); |
163 | | |
164 | | /* group->a */ |
165 | 10.9k | if (!BN_nnmod(tmp_a, a, p, ctx)) |
166 | 0 | goto err; |
167 | 10.9k | if (group->meth->field_encode) { |
168 | 7.47k | if (!group->meth->field_encode(group, group->a, tmp_a, ctx)) |
169 | 0 | goto err; |
170 | 7.47k | } else if (!BN_copy(group->a, tmp_a)) |
171 | 0 | goto err; |
172 | | |
173 | | /* group->b */ |
174 | 10.9k | if (!BN_nnmod(group->b, b, p, ctx)) |
175 | 0 | goto err; |
176 | 10.9k | if (group->meth->field_encode) |
177 | 7.47k | if (!group->meth->field_encode(group, group->b, group->b, ctx)) |
178 | 0 | goto err; |
179 | | |
180 | | /* group->a_is_minus3 */ |
181 | 10.9k | if (!BN_add_word(tmp_a, 3)) |
182 | 0 | goto err; |
183 | 10.9k | group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field)); |
184 | | |
185 | 10.9k | ret = 1; |
186 | | |
187 | 10.9k | err: |
188 | 10.9k | BN_CTX_end(ctx); |
189 | 10.9k | BN_CTX_free(new_ctx); |
190 | 10.9k | return ret; |
191 | 10.9k | } |
192 | | |
193 | | int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, |
194 | | BIGNUM *b, BN_CTX *ctx) |
195 | 2.29k | { |
196 | 2.29k | int ret = 0; |
197 | 2.29k | BN_CTX *new_ctx = NULL; |
198 | | |
199 | 2.29k | if (p != NULL) { |
200 | 2.19k | if (!BN_copy(p, group->field)) |
201 | 0 | return 0; |
202 | 2.19k | } |
203 | | |
204 | 2.29k | if (a != NULL || b != NULL) { |
205 | 2.19k | if (group->meth->field_decode) { |
206 | 2.19k | if (ctx == NULL) { |
207 | 103 | ctx = new_ctx = BN_CTX_new(); |
208 | 103 | if (ctx == NULL) |
209 | 0 | return 0; |
210 | 103 | } |
211 | 2.19k | if (a != NULL) { |
212 | 2.19k | if (!group->meth->field_decode(group, a, group->a, ctx)) |
213 | 0 | goto err; |
214 | 2.19k | } |
215 | 2.19k | if (b != NULL) { |
216 | 2.19k | if (!group->meth->field_decode(group, b, group->b, ctx)) |
217 | 0 | goto err; |
218 | 2.19k | } |
219 | 2.19k | } else { |
220 | 0 | if (a != NULL) { |
221 | 0 | if (!BN_copy(a, group->a)) |
222 | 0 | goto err; |
223 | 0 | } |
224 | 0 | if (b != NULL) { |
225 | 0 | if (!BN_copy(b, group->b)) |
226 | 0 | goto err; |
227 | 0 | } |
228 | 0 | } |
229 | 2.19k | } |
230 | | |
231 | 2.29k | ret = 1; |
232 | | |
233 | 2.29k | err: |
234 | 2.29k | BN_CTX_free(new_ctx); |
235 | 2.29k | return ret; |
236 | 2.29k | } |
237 | | |
238 | | int ec_GFp_simple_group_get_degree(const EC_GROUP *group) |
239 | 103 | { |
240 | 103 | return BN_num_bits(group->field); |
241 | 103 | } |
242 | | |
243 | | int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
244 | 0 | { |
245 | 0 | int ret = 0; |
246 | 0 | BIGNUM *a, *b, *order, *tmp_1, *tmp_2; |
247 | 0 | const BIGNUM *p = group->field; |
248 | 0 | BN_CTX *new_ctx = NULL; |
249 | |
|
250 | 0 | if (ctx == NULL) { |
251 | 0 | ctx = new_ctx = BN_CTX_new(); |
252 | 0 | if (ctx == NULL) { |
253 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, |
254 | 0 | ERR_R_MALLOC_FAILURE); |
255 | 0 | goto err; |
256 | 0 | } |
257 | 0 | } |
258 | 0 | BN_CTX_start(ctx); |
259 | 0 | a = BN_CTX_get(ctx); |
260 | 0 | b = BN_CTX_get(ctx); |
261 | 0 | tmp_1 = BN_CTX_get(ctx); |
262 | 0 | tmp_2 = BN_CTX_get(ctx); |
263 | 0 | order = BN_CTX_get(ctx); |
264 | 0 | if (order == NULL) |
265 | 0 | goto err; |
266 | | |
267 | 0 | if (group->meth->field_decode) { |
268 | 0 | if (!group->meth->field_decode(group, a, group->a, ctx)) |
269 | 0 | goto err; |
270 | 0 | if (!group->meth->field_decode(group, b, group->b, ctx)) |
271 | 0 | goto err; |
272 | 0 | } else { |
273 | 0 | if (!BN_copy(a, group->a)) |
274 | 0 | goto err; |
275 | 0 | if (!BN_copy(b, group->b)) |
276 | 0 | goto err; |
277 | 0 | } |
278 | | |
279 | | /*- |
280 | | * check the discriminant: |
281 | | * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p) |
282 | | * 0 =< a, b < p |
283 | | */ |
284 | 0 | if (BN_is_zero(a)) { |
285 | 0 | if (BN_is_zero(b)) |
286 | 0 | goto err; |
287 | 0 | } else if (!BN_is_zero(b)) { |
288 | 0 | if (!BN_mod_sqr(tmp_1, a, p, ctx)) |
289 | 0 | goto err; |
290 | 0 | if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) |
291 | 0 | goto err; |
292 | 0 | if (!BN_lshift(tmp_1, tmp_2, 2)) |
293 | 0 | goto err; |
294 | | /* tmp_1 = 4*a^3 */ |
295 | | |
296 | 0 | if (!BN_mod_sqr(tmp_2, b, p, ctx)) |
297 | 0 | goto err; |
298 | 0 | if (!BN_mul_word(tmp_2, 27)) |
299 | 0 | goto err; |
300 | | /* tmp_2 = 27*b^2 */ |
301 | | |
302 | 0 | if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) |
303 | 0 | goto err; |
304 | 0 | if (BN_is_zero(a)) |
305 | 0 | goto err; |
306 | 0 | } |
307 | 0 | ret = 1; |
308 | |
|
309 | 0 | err: |
310 | 0 | BN_CTX_end(ctx); |
311 | 0 | BN_CTX_free(new_ctx); |
312 | 0 | return ret; |
313 | 0 | } |
314 | | |
315 | | int ec_GFp_simple_point_init(EC_POINT *point) |
316 | 41.6k | { |
317 | 41.6k | point->X = BN_new(); |
318 | 41.6k | point->Y = BN_new(); |
319 | 41.6k | point->Z = BN_new(); |
320 | 41.6k | point->Z_is_one = 0; |
321 | | |
322 | 41.6k | if (point->X == NULL || point->Y == NULL || point->Z == NULL) { |
323 | 0 | BN_free(point->X); |
324 | 0 | BN_free(point->Y); |
325 | 0 | BN_free(point->Z); |
326 | 0 | return 0; |
327 | 0 | } |
328 | 41.6k | return 1; |
329 | 41.6k | } |
330 | | |
331 | | void ec_GFp_simple_point_finish(EC_POINT *point) |
332 | 41.1k | { |
333 | 41.1k | BN_free(point->X); |
334 | 41.1k | BN_free(point->Y); |
335 | 41.1k | BN_free(point->Z); |
336 | 41.1k | } |
337 | | |
338 | | void ec_GFp_simple_point_clear_finish(EC_POINT *point) |
339 | 467 | { |
340 | 467 | BN_clear_free(point->X); |
341 | 467 | BN_clear_free(point->Y); |
342 | 467 | BN_clear_free(point->Z); |
343 | 467 | point->Z_is_one = 0; |
344 | 467 | } |
345 | | |
346 | | int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) |
347 | 22.4k | { |
348 | 22.4k | if (!BN_copy(dest->X, src->X)) |
349 | 0 | return 0; |
350 | 22.4k | if (!BN_copy(dest->Y, src->Y)) |
351 | 0 | return 0; |
352 | 22.4k | if (!BN_copy(dest->Z, src->Z)) |
353 | 0 | return 0; |
354 | 22.4k | dest->Z_is_one = src->Z_is_one; |
355 | 22.4k | dest->curve_name = src->curve_name; |
356 | | |
357 | 22.4k | return 1; |
358 | 22.4k | } |
359 | | |
360 | | int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, |
361 | | EC_POINT *point) |
362 | 311 | { |
363 | 311 | point->Z_is_one = 0; |
364 | 311 | BN_zero(point->Z); |
365 | 311 | return 1; |
366 | 311 | } |
367 | | |
368 | | int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, |
369 | | EC_POINT *point, |
370 | | const BIGNUM *x, |
371 | | const BIGNUM *y, |
372 | | const BIGNUM *z, |
373 | | BN_CTX *ctx) |
374 | 16.2k | { |
375 | 16.2k | BN_CTX *new_ctx = NULL; |
376 | 16.2k | int ret = 0; |
377 | | |
378 | 16.2k | if (ctx == NULL) { |
379 | 0 | ctx = new_ctx = BN_CTX_new(); |
380 | 0 | if (ctx == NULL) |
381 | 0 | return 0; |
382 | 0 | } |
383 | | |
384 | 16.2k | if (x != NULL) { |
385 | 16.2k | if (!BN_nnmod(point->X, x, group->field, ctx)) |
386 | 0 | goto err; |
387 | 16.2k | if (group->meth->field_encode) { |
388 | 10.6k | if (!group->meth->field_encode(group, point->X, point->X, ctx)) |
389 | 0 | goto err; |
390 | 10.6k | } |
391 | 16.2k | } |
392 | | |
393 | 16.2k | if (y != NULL) { |
394 | 16.2k | if (!BN_nnmod(point->Y, y, group->field, ctx)) |
395 | 0 | goto err; |
396 | 16.2k | if (group->meth->field_encode) { |
397 | 10.6k | if (!group->meth->field_encode(group, point->Y, point->Y, ctx)) |
398 | 0 | goto err; |
399 | 10.6k | } |
400 | 16.2k | } |
401 | | |
402 | 16.2k | if (z != NULL) { |
403 | 16.2k | int Z_is_one; |
404 | | |
405 | 16.2k | if (!BN_nnmod(point->Z, z, group->field, ctx)) |
406 | 0 | goto err; |
407 | 16.2k | Z_is_one = BN_is_one(point->Z); |
408 | 16.2k | if (group->meth->field_encode) { |
409 | 10.6k | if (Z_is_one && (group->meth->field_set_to_one != 0)) { |
410 | 10.6k | if (!group->meth->field_set_to_one(group, point->Z, ctx)) |
411 | 0 | goto err; |
412 | 10.6k | } else { |
413 | 0 | if (!group-> |
414 | 0 | meth->field_encode(group, point->Z, point->Z, ctx)) |
415 | 0 | goto err; |
416 | 0 | } |
417 | 10.6k | } |
418 | 16.2k | point->Z_is_one = Z_is_one; |
419 | 16.2k | } |
420 | | |
421 | 16.2k | ret = 1; |
422 | | |
423 | 16.2k | err: |
424 | 16.2k | BN_CTX_free(new_ctx); |
425 | 16.2k | return ret; |
426 | 16.2k | } |
427 | | |
428 | | int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, |
429 | | const EC_POINT *point, |
430 | | BIGNUM *x, BIGNUM *y, |
431 | | BIGNUM *z, BN_CTX *ctx) |
432 | 0 | { |
433 | 0 | BN_CTX *new_ctx = NULL; |
434 | 0 | int ret = 0; |
435 | |
|
436 | 0 | if (group->meth->field_decode != 0) { |
437 | 0 | if (ctx == NULL) { |
438 | 0 | ctx = new_ctx = BN_CTX_new(); |
439 | 0 | if (ctx == NULL) |
440 | 0 | return 0; |
441 | 0 | } |
442 | | |
443 | 0 | if (x != NULL) { |
444 | 0 | if (!group->meth->field_decode(group, x, point->X, ctx)) |
445 | 0 | goto err; |
446 | 0 | } |
447 | 0 | if (y != NULL) { |
448 | 0 | if (!group->meth->field_decode(group, y, point->Y, ctx)) |
449 | 0 | goto err; |
450 | 0 | } |
451 | 0 | if (z != NULL) { |
452 | 0 | if (!group->meth->field_decode(group, z, point->Z, ctx)) |
453 | 0 | goto err; |
454 | 0 | } |
455 | 0 | } else { |
456 | 0 | if (x != NULL) { |
457 | 0 | if (!BN_copy(x, point->X)) |
458 | 0 | goto err; |
459 | 0 | } |
460 | 0 | if (y != NULL) { |
461 | 0 | if (!BN_copy(y, point->Y)) |
462 | 0 | goto err; |
463 | 0 | } |
464 | 0 | if (z != NULL) { |
465 | 0 | if (!BN_copy(z, point->Z)) |
466 | 0 | goto err; |
467 | 0 | } |
468 | 0 | } |
469 | | |
470 | 0 | ret = 1; |
471 | |
|
472 | 0 | err: |
473 | 0 | BN_CTX_free(new_ctx); |
474 | 0 | return ret; |
475 | 0 | } |
476 | | |
477 | | int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, |
478 | | EC_POINT *point, |
479 | | const BIGNUM *x, |
480 | | const BIGNUM *y, BN_CTX *ctx) |
481 | 16.1k | { |
482 | 16.1k | if (x == NULL || y == NULL) { |
483 | | /* |
484 | | * unlike for projective coordinates, we do not tolerate this |
485 | | */ |
486 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, |
487 | 0 | ERR_R_PASSED_NULL_PARAMETER); |
488 | 0 | return 0; |
489 | 0 | } |
490 | | |
491 | 16.1k | return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, |
492 | 16.1k | BN_value_one(), ctx); |
493 | 16.1k | } |
494 | | |
495 | | int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, |
496 | | const EC_POINT *point, |
497 | | BIGNUM *x, BIGNUM *y, |
498 | | BN_CTX *ctx) |
499 | 2.46k | { |
500 | 2.46k | BN_CTX *new_ctx = NULL; |
501 | 2.46k | BIGNUM *Z, *Z_1, *Z_2, *Z_3; |
502 | 2.46k | const BIGNUM *Z_; |
503 | 2.46k | int ret = 0; |
504 | | |
505 | 2.46k | if (EC_POINT_is_at_infinity(group, point)) { |
506 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, |
507 | 0 | EC_R_POINT_AT_INFINITY); |
508 | 0 | return 0; |
509 | 0 | } |
510 | | |
511 | 2.46k | if (ctx == NULL) { |
512 | 0 | ctx = new_ctx = BN_CTX_new(); |
513 | 0 | if (ctx == NULL) |
514 | 0 | return 0; |
515 | 0 | } |
516 | | |
517 | 2.46k | BN_CTX_start(ctx); |
518 | 2.46k | Z = BN_CTX_get(ctx); |
519 | 2.46k | Z_1 = BN_CTX_get(ctx); |
520 | 2.46k | Z_2 = BN_CTX_get(ctx); |
521 | 2.46k | Z_3 = BN_CTX_get(ctx); |
522 | 2.46k | if (Z_3 == NULL) |
523 | 0 | goto err; |
524 | | |
525 | | /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */ |
526 | | |
527 | 2.46k | if (group->meth->field_decode) { |
528 | 2.46k | if (!group->meth->field_decode(group, Z, point->Z, ctx)) |
529 | 0 | goto err; |
530 | 2.46k | Z_ = Z; |
531 | 2.46k | } else { |
532 | 0 | Z_ = point->Z; |
533 | 0 | } |
534 | | |
535 | 2.46k | if (BN_is_one(Z_)) { |
536 | 2.39k | if (group->meth->field_decode) { |
537 | 2.39k | if (x != NULL) { |
538 | 2.39k | if (!group->meth->field_decode(group, x, point->X, ctx)) |
539 | 0 | goto err; |
540 | 2.39k | } |
541 | 2.39k | if (y != NULL) { |
542 | 2.39k | if (!group->meth->field_decode(group, y, point->Y, ctx)) |
543 | 0 | goto err; |
544 | 2.39k | } |
545 | 2.39k | } else { |
546 | 0 | if (x != NULL) { |
547 | 0 | if (!BN_copy(x, point->X)) |
548 | 0 | goto err; |
549 | 0 | } |
550 | 0 | if (y != NULL) { |
551 | 0 | if (!BN_copy(y, point->Y)) |
552 | 0 | goto err; |
553 | 0 | } |
554 | 0 | } |
555 | 2.39k | } else { |
556 | 68 | if (!group->meth->field_inv(group, Z_1, Z_, ctx)) { |
557 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, |
558 | 0 | ERR_R_BN_LIB); |
559 | 0 | goto err; |
560 | 0 | } |
561 | | |
562 | 68 | if (group->meth->field_encode == 0) { |
563 | | /* field_sqr works on standard representation */ |
564 | 0 | if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) |
565 | 0 | goto err; |
566 | 68 | } else { |
567 | 68 | if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx)) |
568 | 0 | goto err; |
569 | 68 | } |
570 | | |
571 | 68 | if (x != NULL) { |
572 | | /* |
573 | | * in the Montgomery case, field_mul will cancel out Montgomery |
574 | | * factor in X: |
575 | | */ |
576 | 68 | if (!group->meth->field_mul(group, x, point->X, Z_2, ctx)) |
577 | 0 | goto err; |
578 | 68 | } |
579 | | |
580 | 68 | if (y != NULL) { |
581 | 68 | if (group->meth->field_encode == 0) { |
582 | | /* |
583 | | * field_mul works on standard representation |
584 | | */ |
585 | 0 | if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) |
586 | 0 | goto err; |
587 | 68 | } else { |
588 | 68 | if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx)) |
589 | 0 | goto err; |
590 | 68 | } |
591 | | |
592 | | /* |
593 | | * in the Montgomery case, field_mul will cancel out Montgomery |
594 | | * factor in Y: |
595 | | */ |
596 | 68 | if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx)) |
597 | 0 | goto err; |
598 | 68 | } |
599 | 68 | } |
600 | | |
601 | 2.46k | ret = 1; |
602 | | |
603 | 2.46k | err: |
604 | 2.46k | BN_CTX_end(ctx); |
605 | 2.46k | BN_CTX_free(new_ctx); |
606 | 2.46k | return ret; |
607 | 2.46k | } |
608 | | |
609 | | int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
610 | | const EC_POINT *b, BN_CTX *ctx) |
611 | 1.60k | { |
612 | 1.60k | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
613 | 1.60k | const BIGNUM *, BN_CTX *); |
614 | 1.60k | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
615 | 1.60k | const BIGNUM *p; |
616 | 1.60k | BN_CTX *new_ctx = NULL; |
617 | 1.60k | BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6; |
618 | 1.60k | int ret = 0; |
619 | | |
620 | 1.60k | if (a == b) |
621 | 0 | return EC_POINT_dbl(group, r, a, ctx); |
622 | 1.60k | if (EC_POINT_is_at_infinity(group, a)) |
623 | 0 | return EC_POINT_copy(r, b); |
624 | 1.60k | if (EC_POINT_is_at_infinity(group, b)) |
625 | 0 | return EC_POINT_copy(r, a); |
626 | | |
627 | 1.60k | field_mul = group->meth->field_mul; |
628 | 1.60k | field_sqr = group->meth->field_sqr; |
629 | 1.60k | p = group->field; |
630 | | |
631 | 1.60k | if (ctx == NULL) { |
632 | 0 | ctx = new_ctx = BN_CTX_new(); |
633 | 0 | if (ctx == NULL) |
634 | 0 | return 0; |
635 | 0 | } |
636 | | |
637 | 1.60k | BN_CTX_start(ctx); |
638 | 1.60k | n0 = BN_CTX_get(ctx); |
639 | 1.60k | n1 = BN_CTX_get(ctx); |
640 | 1.60k | n2 = BN_CTX_get(ctx); |
641 | 1.60k | n3 = BN_CTX_get(ctx); |
642 | 1.60k | n4 = BN_CTX_get(ctx); |
643 | 1.60k | n5 = BN_CTX_get(ctx); |
644 | 1.60k | n6 = BN_CTX_get(ctx); |
645 | 1.60k | if (n6 == NULL) |
646 | 0 | goto end; |
647 | | |
648 | | /* |
649 | | * Note that in this function we must not read components of 'a' or 'b' |
650 | | * once we have written the corresponding components of 'r'. ('r' might |
651 | | * be one of 'a' or 'b'.) |
652 | | */ |
653 | | |
654 | | /* n1, n2 */ |
655 | 1.60k | if (b->Z_is_one) { |
656 | 1.43k | if (!BN_copy(n1, a->X)) |
657 | 0 | goto end; |
658 | 1.43k | if (!BN_copy(n2, a->Y)) |
659 | 0 | goto end; |
660 | | /* n1 = X_a */ |
661 | | /* n2 = Y_a */ |
662 | 1.43k | } else { |
663 | 171 | if (!field_sqr(group, n0, b->Z, ctx)) |
664 | 0 | goto end; |
665 | 171 | if (!field_mul(group, n1, a->X, n0, ctx)) |
666 | 0 | goto end; |
667 | | /* n1 = X_a * Z_b^2 */ |
668 | | |
669 | 171 | if (!field_mul(group, n0, n0, b->Z, ctx)) |
670 | 0 | goto end; |
671 | 171 | if (!field_mul(group, n2, a->Y, n0, ctx)) |
672 | 0 | goto end; |
673 | | /* n2 = Y_a * Z_b^3 */ |
674 | 171 | } |
675 | | |
676 | | /* n3, n4 */ |
677 | 1.60k | if (a->Z_is_one) { |
678 | 37 | if (!BN_copy(n3, b->X)) |
679 | 0 | goto end; |
680 | 37 | if (!BN_copy(n4, b->Y)) |
681 | 0 | goto end; |
682 | | /* n3 = X_b */ |
683 | | /* n4 = Y_b */ |
684 | 1.56k | } else { |
685 | 1.56k | if (!field_sqr(group, n0, a->Z, ctx)) |
686 | 0 | goto end; |
687 | 1.56k | if (!field_mul(group, n3, b->X, n0, ctx)) |
688 | 0 | goto end; |
689 | | /* n3 = X_b * Z_a^2 */ |
690 | | |
691 | 1.56k | if (!field_mul(group, n0, n0, a->Z, ctx)) |
692 | 0 | goto end; |
693 | 1.56k | if (!field_mul(group, n4, b->Y, n0, ctx)) |
694 | 0 | goto end; |
695 | | /* n4 = Y_b * Z_a^3 */ |
696 | 1.56k | } |
697 | | |
698 | | /* n5, n6 */ |
699 | 1.60k | if (!BN_mod_sub_quick(n5, n1, n3, p)) |
700 | 0 | goto end; |
701 | 1.60k | if (!BN_mod_sub_quick(n6, n2, n4, p)) |
702 | 0 | goto end; |
703 | | /* n5 = n1 - n3 */ |
704 | | /* n6 = n2 - n4 */ |
705 | | |
706 | 1.60k | if (BN_is_zero(n5)) { |
707 | 0 | if (BN_is_zero(n6)) { |
708 | | /* a is the same point as b */ |
709 | 0 | BN_CTX_end(ctx); |
710 | 0 | ret = EC_POINT_dbl(group, r, a, ctx); |
711 | 0 | ctx = NULL; |
712 | 0 | goto end; |
713 | 0 | } else { |
714 | | /* a is the inverse of b */ |
715 | 0 | BN_zero(r->Z); |
716 | 0 | r->Z_is_one = 0; |
717 | 0 | ret = 1; |
718 | 0 | goto end; |
719 | 0 | } |
720 | 0 | } |
721 | | |
722 | | /* 'n7', 'n8' */ |
723 | 1.60k | if (!BN_mod_add_quick(n1, n1, n3, p)) |
724 | 0 | goto end; |
725 | 1.60k | if (!BN_mod_add_quick(n2, n2, n4, p)) |
726 | 0 | goto end; |
727 | | /* 'n7' = n1 + n3 */ |
728 | | /* 'n8' = n2 + n4 */ |
729 | | |
730 | | /* Z_r */ |
731 | 1.60k | if (a->Z_is_one && b->Z_is_one) { |
732 | 0 | if (!BN_copy(r->Z, n5)) |
733 | 0 | goto end; |
734 | 1.60k | } else { |
735 | 1.60k | if (a->Z_is_one) { |
736 | 37 | if (!BN_copy(n0, b->Z)) |
737 | 0 | goto end; |
738 | 1.56k | } else if (b->Z_is_one) { |
739 | 1.43k | if (!BN_copy(n0, a->Z)) |
740 | 0 | goto end; |
741 | 1.43k | } else { |
742 | 134 | if (!field_mul(group, n0, a->Z, b->Z, ctx)) |
743 | 0 | goto end; |
744 | 134 | } |
745 | 1.60k | if (!field_mul(group, r->Z, n0, n5, ctx)) |
746 | 0 | goto end; |
747 | 1.60k | } |
748 | 1.60k | r->Z_is_one = 0; |
749 | | /* Z_r = Z_a * Z_b * n5 */ |
750 | | |
751 | | /* X_r */ |
752 | 1.60k | if (!field_sqr(group, n0, n6, ctx)) |
753 | 0 | goto end; |
754 | 1.60k | if (!field_sqr(group, n4, n5, ctx)) |
755 | 0 | goto end; |
756 | 1.60k | if (!field_mul(group, n3, n1, n4, ctx)) |
757 | 0 | goto end; |
758 | 1.60k | if (!BN_mod_sub_quick(r->X, n0, n3, p)) |
759 | 0 | goto end; |
760 | | /* X_r = n6^2 - n5^2 * 'n7' */ |
761 | | |
762 | | /* 'n9' */ |
763 | 1.60k | if (!BN_mod_lshift1_quick(n0, r->X, p)) |
764 | 0 | goto end; |
765 | 1.60k | if (!BN_mod_sub_quick(n0, n3, n0, p)) |
766 | 0 | goto end; |
767 | | /* n9 = n5^2 * 'n7' - 2 * X_r */ |
768 | | |
769 | | /* Y_r */ |
770 | 1.60k | if (!field_mul(group, n0, n0, n6, ctx)) |
771 | 0 | goto end; |
772 | 1.60k | if (!field_mul(group, n5, n4, n5, ctx)) |
773 | 0 | goto end; /* now n5 is n5^3 */ |
774 | 1.60k | if (!field_mul(group, n1, n2, n5, ctx)) |
775 | 0 | goto end; |
776 | 1.60k | if (!BN_mod_sub_quick(n0, n0, n1, p)) |
777 | 0 | goto end; |
778 | 1.60k | if (BN_is_odd(n0)) |
779 | 775 | if (!BN_add(n0, n0, p)) |
780 | 0 | goto end; |
781 | | /* now 0 <= n0 < 2*p, and n0 is even */ |
782 | 1.60k | if (!BN_rshift1(r->Y, n0)) |
783 | 0 | goto end; |
784 | | /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */ |
785 | | |
786 | 1.60k | ret = 1; |
787 | | |
788 | 1.60k | end: |
789 | 1.60k | BN_CTX_end(ctx); |
790 | 1.60k | BN_CTX_free(new_ctx); |
791 | 1.60k | return ret; |
792 | 1.60k | } |
793 | | |
794 | | int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, |
795 | | BN_CTX *ctx) |
796 | 9.73k | { |
797 | 9.73k | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
798 | 9.73k | const BIGNUM *, BN_CTX *); |
799 | 9.73k | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
800 | 9.73k | const BIGNUM *p; |
801 | 9.73k | BN_CTX *new_ctx = NULL; |
802 | 9.73k | BIGNUM *n0, *n1, *n2, *n3; |
803 | 9.73k | int ret = 0; |
804 | | |
805 | 9.73k | if (EC_POINT_is_at_infinity(group, a)) { |
806 | 0 | BN_zero(r->Z); |
807 | 0 | r->Z_is_one = 0; |
808 | 0 | return 1; |
809 | 0 | } |
810 | | |
811 | 9.73k | field_mul = group->meth->field_mul; |
812 | 9.73k | field_sqr = group->meth->field_sqr; |
813 | 9.73k | p = group->field; |
814 | | |
815 | 9.73k | if (ctx == NULL) { |
816 | 0 | ctx = new_ctx = BN_CTX_new(); |
817 | 0 | if (ctx == NULL) |
818 | 0 | return 0; |
819 | 0 | } |
820 | | |
821 | 9.73k | BN_CTX_start(ctx); |
822 | 9.73k | n0 = BN_CTX_get(ctx); |
823 | 9.73k | n1 = BN_CTX_get(ctx); |
824 | 9.73k | n2 = BN_CTX_get(ctx); |
825 | 9.73k | n3 = BN_CTX_get(ctx); |
826 | 9.73k | if (n3 == NULL) |
827 | 0 | goto err; |
828 | | |
829 | | /* |
830 | | * Note that in this function we must not read components of 'a' once we |
831 | | * have written the corresponding components of 'r'. ('r' might the same |
832 | | * as 'a'.) |
833 | | */ |
834 | | |
835 | | /* n1 */ |
836 | 9.73k | if (a->Z_is_one) { |
837 | 37 | if (!field_sqr(group, n0, a->X, ctx)) |
838 | 0 | goto err; |
839 | 37 | if (!BN_mod_lshift1_quick(n1, n0, p)) |
840 | 0 | goto err; |
841 | 37 | if (!BN_mod_add_quick(n0, n0, n1, p)) |
842 | 0 | goto err; |
843 | 37 | if (!BN_mod_add_quick(n1, n0, group->a, p)) |
844 | 0 | goto err; |
845 | | /* n1 = 3 * X_a^2 + a_curve */ |
846 | 9.69k | } else if (group->a_is_minus3) { |
847 | 81 | if (!field_sqr(group, n1, a->Z, ctx)) |
848 | 0 | goto err; |
849 | 81 | if (!BN_mod_add_quick(n0, a->X, n1, p)) |
850 | 0 | goto err; |
851 | 81 | if (!BN_mod_sub_quick(n2, a->X, n1, p)) |
852 | 0 | goto err; |
853 | 81 | if (!field_mul(group, n1, n0, n2, ctx)) |
854 | 0 | goto err; |
855 | 81 | if (!BN_mod_lshift1_quick(n0, n1, p)) |
856 | 0 | goto err; |
857 | 81 | if (!BN_mod_add_quick(n1, n0, n1, p)) |
858 | 0 | goto err; |
859 | | /*- |
860 | | * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2) |
861 | | * = 3 * X_a^2 - 3 * Z_a^4 |
862 | | */ |
863 | 9.61k | } else { |
864 | 9.61k | if (!field_sqr(group, n0, a->X, ctx)) |
865 | 0 | goto err; |
866 | 9.61k | if (!BN_mod_lshift1_quick(n1, n0, p)) |
867 | 0 | goto err; |
868 | 9.61k | if (!BN_mod_add_quick(n0, n0, n1, p)) |
869 | 0 | goto err; |
870 | 9.61k | if (!field_sqr(group, n1, a->Z, ctx)) |
871 | 0 | goto err; |
872 | 9.61k | if (!field_sqr(group, n1, n1, ctx)) |
873 | 0 | goto err; |
874 | 9.61k | if (!field_mul(group, n1, n1, group->a, ctx)) |
875 | 0 | goto err; |
876 | 9.61k | if (!BN_mod_add_quick(n1, n1, n0, p)) |
877 | 0 | goto err; |
878 | | /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */ |
879 | 9.61k | } |
880 | | |
881 | | /* Z_r */ |
882 | 9.73k | if (a->Z_is_one) { |
883 | 37 | if (!BN_copy(n0, a->Y)) |
884 | 0 | goto err; |
885 | 9.69k | } else { |
886 | 9.69k | if (!field_mul(group, n0, a->Y, a->Z, ctx)) |
887 | 0 | goto err; |
888 | 9.69k | } |
889 | 9.73k | if (!BN_mod_lshift1_quick(r->Z, n0, p)) |
890 | 0 | goto err; |
891 | 9.73k | r->Z_is_one = 0; |
892 | | /* Z_r = 2 * Y_a * Z_a */ |
893 | | |
894 | | /* n2 */ |
895 | 9.73k | if (!field_sqr(group, n3, a->Y, ctx)) |
896 | 0 | goto err; |
897 | 9.73k | if (!field_mul(group, n2, a->X, n3, ctx)) |
898 | 0 | goto err; |
899 | 9.73k | if (!BN_mod_lshift_quick(n2, n2, 2, p)) |
900 | 0 | goto err; |
901 | | /* n2 = 4 * X_a * Y_a^2 */ |
902 | | |
903 | | /* X_r */ |
904 | 9.73k | if (!BN_mod_lshift1_quick(n0, n2, p)) |
905 | 0 | goto err; |
906 | 9.73k | if (!field_sqr(group, r->X, n1, ctx)) |
907 | 0 | goto err; |
908 | 9.73k | if (!BN_mod_sub_quick(r->X, r->X, n0, p)) |
909 | 0 | goto err; |
910 | | /* X_r = n1^2 - 2 * n2 */ |
911 | | |
912 | | /* n3 */ |
913 | 9.73k | if (!field_sqr(group, n0, n3, ctx)) |
914 | 0 | goto err; |
915 | 9.73k | if (!BN_mod_lshift_quick(n3, n0, 3, p)) |
916 | 0 | goto err; |
917 | | /* n3 = 8 * Y_a^4 */ |
918 | | |
919 | | /* Y_r */ |
920 | 9.73k | if (!BN_mod_sub_quick(n0, n2, r->X, p)) |
921 | 0 | goto err; |
922 | 9.73k | if (!field_mul(group, n0, n1, n0, ctx)) |
923 | 0 | goto err; |
924 | 9.73k | if (!BN_mod_sub_quick(r->Y, n0, n3, p)) |
925 | 0 | goto err; |
926 | | /* Y_r = n1 * (n2 - X_r) - n3 */ |
927 | | |
928 | 9.73k | ret = 1; |
929 | | |
930 | 9.73k | err: |
931 | 9.73k | BN_CTX_end(ctx); |
932 | 9.73k | BN_CTX_free(new_ctx); |
933 | 9.73k | return ret; |
934 | 9.73k | } |
935 | | |
936 | | int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) |
937 | 486 | { |
938 | 486 | if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y)) |
939 | | /* point is its own inverse */ |
940 | 0 | return 1; |
941 | | |
942 | 486 | return BN_usub(point->Y, group->field, point->Y); |
943 | 486 | } |
944 | | |
945 | | int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) |
946 | 35.7k | { |
947 | 35.7k | return BN_is_zero(point->Z); |
948 | 35.7k | } |
949 | | |
950 | | int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
951 | | BN_CTX *ctx) |
952 | 16.1k | { |
953 | 16.1k | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
954 | 16.1k | const BIGNUM *, BN_CTX *); |
955 | 16.1k | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
956 | 16.1k | const BIGNUM *p; |
957 | 16.1k | BN_CTX *new_ctx = NULL; |
958 | 16.1k | BIGNUM *rh, *tmp, *Z4, *Z6; |
959 | 16.1k | int ret = -1; |
960 | | |
961 | 16.1k | if (EC_POINT_is_at_infinity(group, point)) |
962 | 0 | return 1; |
963 | | |
964 | 16.1k | field_mul = group->meth->field_mul; |
965 | 16.1k | field_sqr = group->meth->field_sqr; |
966 | 16.1k | p = group->field; |
967 | | |
968 | 16.1k | if (ctx == NULL) { |
969 | 0 | ctx = new_ctx = BN_CTX_new(); |
970 | 0 | if (ctx == NULL) |
971 | 0 | return -1; |
972 | 0 | } |
973 | | |
974 | 16.1k | BN_CTX_start(ctx); |
975 | 16.1k | rh = BN_CTX_get(ctx); |
976 | 16.1k | tmp = BN_CTX_get(ctx); |
977 | 16.1k | Z4 = BN_CTX_get(ctx); |
978 | 16.1k | Z6 = BN_CTX_get(ctx); |
979 | 16.1k | if (Z6 == NULL) |
980 | 0 | goto err; |
981 | | |
982 | | /*- |
983 | | * We have a curve defined by a Weierstrass equation |
984 | | * y^2 = x^3 + a*x + b. |
985 | | * The point to consider is given in Jacobian projective coordinates |
986 | | * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3). |
987 | | * Substituting this and multiplying by Z^6 transforms the above equation into |
988 | | * Y^2 = X^3 + a*X*Z^4 + b*Z^6. |
989 | | * To test this, we add up the right-hand side in 'rh'. |
990 | | */ |
991 | | |
992 | | /* rh := X^2 */ |
993 | 16.1k | if (!field_sqr(group, rh, point->X, ctx)) |
994 | 0 | goto err; |
995 | | |
996 | 16.1k | if (!point->Z_is_one) { |
997 | 0 | if (!field_sqr(group, tmp, point->Z, ctx)) |
998 | 0 | goto err; |
999 | 0 | if (!field_sqr(group, Z4, tmp, ctx)) |
1000 | 0 | goto err; |
1001 | 0 | if (!field_mul(group, Z6, Z4, tmp, ctx)) |
1002 | 0 | goto err; |
1003 | | |
1004 | | /* rh := (rh + a*Z^4)*X */ |
1005 | 0 | if (group->a_is_minus3) { |
1006 | 0 | if (!BN_mod_lshift1_quick(tmp, Z4, p)) |
1007 | 0 | goto err; |
1008 | 0 | if (!BN_mod_add_quick(tmp, tmp, Z4, p)) |
1009 | 0 | goto err; |
1010 | 0 | if (!BN_mod_sub_quick(rh, rh, tmp, p)) |
1011 | 0 | goto err; |
1012 | 0 | if (!field_mul(group, rh, rh, point->X, ctx)) |
1013 | 0 | goto err; |
1014 | 0 | } else { |
1015 | 0 | if (!field_mul(group, tmp, Z4, group->a, ctx)) |
1016 | 0 | goto err; |
1017 | 0 | if (!BN_mod_add_quick(rh, rh, tmp, p)) |
1018 | 0 | goto err; |
1019 | 0 | if (!field_mul(group, rh, rh, point->X, ctx)) |
1020 | 0 | goto err; |
1021 | 0 | } |
1022 | | |
1023 | | /* rh := rh + b*Z^6 */ |
1024 | 0 | if (!field_mul(group, tmp, group->b, Z6, ctx)) |
1025 | 0 | goto err; |
1026 | 0 | if (!BN_mod_add_quick(rh, rh, tmp, p)) |
1027 | 0 | goto err; |
1028 | 16.1k | } else { |
1029 | | /* point->Z_is_one */ |
1030 | | |
1031 | | /* rh := (rh + a)*X */ |
1032 | 16.1k | if (!BN_mod_add_quick(rh, rh, group->a, p)) |
1033 | 0 | goto err; |
1034 | 16.1k | if (!field_mul(group, rh, rh, point->X, ctx)) |
1035 | 0 | goto err; |
1036 | | /* rh := rh + b */ |
1037 | 16.1k | if (!BN_mod_add_quick(rh, rh, group->b, p)) |
1038 | 0 | goto err; |
1039 | 16.1k | } |
1040 | | |
1041 | | /* 'lh' := Y^2 */ |
1042 | 16.1k | if (!field_sqr(group, tmp, point->Y, ctx)) |
1043 | 0 | goto err; |
1044 | | |
1045 | 16.1k | ret = (0 == BN_ucmp(tmp, rh)); |
1046 | | |
1047 | 16.1k | err: |
1048 | 16.1k | BN_CTX_end(ctx); |
1049 | 16.1k | BN_CTX_free(new_ctx); |
1050 | 16.1k | return ret; |
1051 | 16.1k | } |
1052 | | |
1053 | | int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, |
1054 | | const EC_POINT *b, BN_CTX *ctx) |
1055 | 28 | { |
1056 | | /*- |
1057 | | * return values: |
1058 | | * -1 error |
1059 | | * 0 equal (in affine coordinates) |
1060 | | * 1 not equal |
1061 | | */ |
1062 | | |
1063 | 28 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, |
1064 | 28 | const BIGNUM *, BN_CTX *); |
1065 | 28 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
1066 | 28 | BN_CTX *new_ctx = NULL; |
1067 | 28 | BIGNUM *tmp1, *tmp2, *Za23, *Zb23; |
1068 | 28 | const BIGNUM *tmp1_, *tmp2_; |
1069 | 28 | int ret = -1; |
1070 | | |
1071 | 28 | if (EC_POINT_is_at_infinity(group, a)) { |
1072 | 0 | return EC_POINT_is_at_infinity(group, b) ? 0 : 1; |
1073 | 0 | } |
1074 | | |
1075 | 28 | if (EC_POINT_is_at_infinity(group, b)) |
1076 | 0 | return 1; |
1077 | | |
1078 | 28 | if (a->Z_is_one && b->Z_is_one) { |
1079 | 28 | return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1; |
1080 | 28 | } |
1081 | | |
1082 | 0 | field_mul = group->meth->field_mul; |
1083 | 0 | field_sqr = group->meth->field_sqr; |
1084 | |
|
1085 | 0 | if (ctx == NULL) { |
1086 | 0 | ctx = new_ctx = BN_CTX_new(); |
1087 | 0 | if (ctx == NULL) |
1088 | 0 | return -1; |
1089 | 0 | } |
1090 | | |
1091 | 0 | BN_CTX_start(ctx); |
1092 | 0 | tmp1 = BN_CTX_get(ctx); |
1093 | 0 | tmp2 = BN_CTX_get(ctx); |
1094 | 0 | Za23 = BN_CTX_get(ctx); |
1095 | 0 | Zb23 = BN_CTX_get(ctx); |
1096 | 0 | if (Zb23 == NULL) |
1097 | 0 | goto end; |
1098 | | |
1099 | | /*- |
1100 | | * We have to decide whether |
1101 | | * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3), |
1102 | | * or equivalently, whether |
1103 | | * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3). |
1104 | | */ |
1105 | | |
1106 | 0 | if (!b->Z_is_one) { |
1107 | 0 | if (!field_sqr(group, Zb23, b->Z, ctx)) |
1108 | 0 | goto end; |
1109 | 0 | if (!field_mul(group, tmp1, a->X, Zb23, ctx)) |
1110 | 0 | goto end; |
1111 | 0 | tmp1_ = tmp1; |
1112 | 0 | } else |
1113 | 0 | tmp1_ = a->X; |
1114 | 0 | if (!a->Z_is_one) { |
1115 | 0 | if (!field_sqr(group, Za23, a->Z, ctx)) |
1116 | 0 | goto end; |
1117 | 0 | if (!field_mul(group, tmp2, b->X, Za23, ctx)) |
1118 | 0 | goto end; |
1119 | 0 | tmp2_ = tmp2; |
1120 | 0 | } else |
1121 | 0 | tmp2_ = b->X; |
1122 | | |
1123 | | /* compare X_a*Z_b^2 with X_b*Z_a^2 */ |
1124 | 0 | if (BN_cmp(tmp1_, tmp2_) != 0) { |
1125 | 0 | ret = 1; /* points differ */ |
1126 | 0 | goto end; |
1127 | 0 | } |
1128 | | |
1129 | 0 | if (!b->Z_is_one) { |
1130 | 0 | if (!field_mul(group, Zb23, Zb23, b->Z, ctx)) |
1131 | 0 | goto end; |
1132 | 0 | if (!field_mul(group, tmp1, a->Y, Zb23, ctx)) |
1133 | 0 | goto end; |
1134 | | /* tmp1_ = tmp1 */ |
1135 | 0 | } else |
1136 | 0 | tmp1_ = a->Y; |
1137 | 0 | if (!a->Z_is_one) { |
1138 | 0 | if (!field_mul(group, Za23, Za23, a->Z, ctx)) |
1139 | 0 | goto end; |
1140 | 0 | if (!field_mul(group, tmp2, b->Y, Za23, ctx)) |
1141 | 0 | goto end; |
1142 | | /* tmp2_ = tmp2 */ |
1143 | 0 | } else |
1144 | 0 | tmp2_ = b->Y; |
1145 | | |
1146 | | /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */ |
1147 | 0 | if (BN_cmp(tmp1_, tmp2_) != 0) { |
1148 | 0 | ret = 1; /* points differ */ |
1149 | 0 | goto end; |
1150 | 0 | } |
1151 | | |
1152 | | /* points are equal */ |
1153 | 0 | ret = 0; |
1154 | |
|
1155 | 0 | end: |
1156 | 0 | BN_CTX_end(ctx); |
1157 | 0 | BN_CTX_free(new_ctx); |
1158 | 0 | return ret; |
1159 | 0 | } |
1160 | | |
1161 | | int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, |
1162 | | BN_CTX *ctx) |
1163 | 0 | { |
1164 | 0 | BN_CTX *new_ctx = NULL; |
1165 | 0 | BIGNUM *x, *y; |
1166 | 0 | int ret = 0; |
1167 | |
|
1168 | 0 | if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) |
1169 | 0 | return 1; |
1170 | | |
1171 | 0 | if (ctx == NULL) { |
1172 | 0 | ctx = new_ctx = BN_CTX_new(); |
1173 | 0 | if (ctx == NULL) |
1174 | 0 | return 0; |
1175 | 0 | } |
1176 | | |
1177 | 0 | BN_CTX_start(ctx); |
1178 | 0 | x = BN_CTX_get(ctx); |
1179 | 0 | y = BN_CTX_get(ctx); |
1180 | 0 | if (y == NULL) |
1181 | 0 | goto err; |
1182 | | |
1183 | 0 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
1184 | 0 | goto err; |
1185 | 0 | if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
1186 | 0 | goto err; |
1187 | 0 | if (!point->Z_is_one) { |
1188 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR); |
1189 | 0 | goto err; |
1190 | 0 | } |
1191 | | |
1192 | 0 | ret = 1; |
1193 | |
|
1194 | 0 | err: |
1195 | 0 | BN_CTX_end(ctx); |
1196 | 0 | BN_CTX_free(new_ctx); |
1197 | 0 | return ret; |
1198 | 0 | } |
1199 | | |
1200 | | int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, |
1201 | | EC_POINT *points[], BN_CTX *ctx) |
1202 | 69 | { |
1203 | 69 | BN_CTX *new_ctx = NULL; |
1204 | 69 | BIGNUM *tmp, *tmp_Z; |
1205 | 69 | BIGNUM **prod_Z = NULL; |
1206 | 69 | size_t i; |
1207 | 69 | int ret = 0; |
1208 | | |
1209 | 69 | if (num == 0) |
1210 | 0 | return 1; |
1211 | | |
1212 | 69 | if (ctx == NULL) { |
1213 | 0 | ctx = new_ctx = BN_CTX_new(); |
1214 | 0 | if (ctx == NULL) |
1215 | 0 | return 0; |
1216 | 0 | } |
1217 | | |
1218 | 69 | BN_CTX_start(ctx); |
1219 | 69 | tmp = BN_CTX_get(ctx); |
1220 | 69 | tmp_Z = BN_CTX_get(ctx); |
1221 | 69 | if (tmp_Z == NULL) |
1222 | 0 | goto err; |
1223 | | |
1224 | 69 | prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0])); |
1225 | 69 | if (prod_Z == NULL) |
1226 | 0 | goto err; |
1227 | 309 | for (i = 0; i < num; i++) { |
1228 | 240 | prod_Z[i] = BN_new(); |
1229 | 240 | if (prod_Z[i] == NULL) |
1230 | 0 | goto err; |
1231 | 240 | } |
1232 | | |
1233 | | /* |
1234 | | * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z, |
1235 | | * skipping any zero-valued inputs (pretend that they're 1). |
1236 | | */ |
1237 | | |
1238 | 69 | if (!BN_is_zero(points[0]->Z)) { |
1239 | 69 | if (!BN_copy(prod_Z[0], points[0]->Z)) |
1240 | 0 | goto err; |
1241 | 69 | } else { |
1242 | 0 | if (group->meth->field_set_to_one != 0) { |
1243 | 0 | if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) |
1244 | 0 | goto err; |
1245 | 0 | } else { |
1246 | 0 | if (!BN_one(prod_Z[0])) |
1247 | 0 | goto err; |
1248 | 0 | } |
1249 | 0 | } |
1250 | | |
1251 | 240 | for (i = 1; i < num; i++) { |
1252 | 171 | if (!BN_is_zero(points[i]->Z)) { |
1253 | 171 | if (!group-> |
1254 | 171 | meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z, |
1255 | 171 | ctx)) |
1256 | 0 | goto err; |
1257 | 171 | } else { |
1258 | 0 | if (!BN_copy(prod_Z[i], prod_Z[i - 1])) |
1259 | 0 | goto err; |
1260 | 0 | } |
1261 | 171 | } |
1262 | | |
1263 | | /* |
1264 | | * Now use a single explicit inversion to replace every non-zero |
1265 | | * points[i]->Z by its inverse. |
1266 | | */ |
1267 | | |
1268 | 69 | if (!group->meth->field_inv(group, tmp, prod_Z[num - 1], ctx)) { |
1269 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB); |
1270 | 0 | goto err; |
1271 | 0 | } |
1272 | 69 | if (group->meth->field_encode != 0) { |
1273 | | /* |
1274 | | * In the Montgomery case, we just turned R*H (representing H) into |
1275 | | * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to |
1276 | | * multiply by the Montgomery factor twice. |
1277 | | */ |
1278 | 69 | if (!group->meth->field_encode(group, tmp, tmp, ctx)) |
1279 | 0 | goto err; |
1280 | 69 | if (!group->meth->field_encode(group, tmp, tmp, ctx)) |
1281 | 0 | goto err; |
1282 | 69 | } |
1283 | | |
1284 | 240 | for (i = num - 1; i > 0; --i) { |
1285 | | /* |
1286 | | * Loop invariant: tmp is the product of the inverses of points[0]->Z |
1287 | | * .. points[i]->Z (zero-valued inputs skipped). |
1288 | | */ |
1289 | 171 | if (!BN_is_zero(points[i]->Z)) { |
1290 | | /* |
1291 | | * Set tmp_Z to the inverse of points[i]->Z (as product of Z |
1292 | | * inverses 0 .. i, Z values 0 .. i - 1). |
1293 | | */ |
1294 | 171 | if (!group-> |
1295 | 171 | meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx)) |
1296 | 0 | goto err; |
1297 | | /* |
1298 | | * Update tmp to satisfy the loop invariant for i - 1. |
1299 | | */ |
1300 | 171 | if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx)) |
1301 | 0 | goto err; |
1302 | | /* Replace points[i]->Z by its inverse. */ |
1303 | 171 | if (!BN_copy(points[i]->Z, tmp_Z)) |
1304 | 0 | goto err; |
1305 | 171 | } |
1306 | 171 | } |
1307 | | |
1308 | 69 | if (!BN_is_zero(points[0]->Z)) { |
1309 | | /* Replace points[0]->Z by its inverse. */ |
1310 | 69 | if (!BN_copy(points[0]->Z, tmp)) |
1311 | 0 | goto err; |
1312 | 69 | } |
1313 | | |
1314 | | /* Finally, fix up the X and Y coordinates for all points. */ |
1315 | | |
1316 | 309 | for (i = 0; i < num; i++) { |
1317 | 240 | EC_POINT *p = points[i]; |
1318 | | |
1319 | 240 | if (!BN_is_zero(p->Z)) { |
1320 | | /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */ |
1321 | | |
1322 | 240 | if (!group->meth->field_sqr(group, tmp, p->Z, ctx)) |
1323 | 0 | goto err; |
1324 | 240 | if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx)) |
1325 | 0 | goto err; |
1326 | | |
1327 | 240 | if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx)) |
1328 | 0 | goto err; |
1329 | 240 | if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx)) |
1330 | 0 | goto err; |
1331 | | |
1332 | 240 | if (group->meth->field_set_to_one != 0) { |
1333 | 240 | if (!group->meth->field_set_to_one(group, p->Z, ctx)) |
1334 | 0 | goto err; |
1335 | 240 | } else { |
1336 | 0 | if (!BN_one(p->Z)) |
1337 | 0 | goto err; |
1338 | 0 | } |
1339 | 240 | p->Z_is_one = 1; |
1340 | 240 | } |
1341 | 240 | } |
1342 | | |
1343 | 69 | ret = 1; |
1344 | | |
1345 | 69 | err: |
1346 | 69 | BN_CTX_end(ctx); |
1347 | 69 | BN_CTX_free(new_ctx); |
1348 | 69 | if (prod_Z != NULL) { |
1349 | 309 | for (i = 0; i < num; i++) { |
1350 | 240 | if (prod_Z[i] == NULL) |
1351 | 0 | break; |
1352 | 240 | BN_clear_free(prod_Z[i]); |
1353 | 240 | } |
1354 | 69 | OPENSSL_free(prod_Z); |
1355 | 69 | } |
1356 | 69 | return ret; |
1357 | 69 | } |
1358 | | |
1359 | | int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
1360 | | const BIGNUM *b, BN_CTX *ctx) |
1361 | 0 | { |
1362 | 0 | return BN_mod_mul(r, a, b, group->field, ctx); |
1363 | 0 | } |
1364 | | |
1365 | | int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
1366 | | BN_CTX *ctx) |
1367 | 0 | { |
1368 | 0 | return BN_mod_sqr(r, a, group->field, ctx); |
1369 | 0 | } |
1370 | | |
1371 | | /*- |
1372 | | * Computes the multiplicative inverse of a in GF(p), storing the result in r. |
1373 | | * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error. |
1374 | | * Since we don't have a Mont structure here, SCA hardening is with blinding. |
1375 | | * NB: "a" must be in _decoded_ form. (i.e. field_decode must precede.) |
1376 | | */ |
1377 | | int ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
1378 | | BN_CTX *ctx) |
1379 | 0 | { |
1380 | 0 | BIGNUM *e = NULL; |
1381 | 0 | BN_CTX *new_ctx = NULL; |
1382 | 0 | int ret = 0; |
1383 | |
|
1384 | 0 | if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) |
1385 | 0 | return 0; |
1386 | | |
1387 | 0 | BN_CTX_start(ctx); |
1388 | 0 | if ((e = BN_CTX_get(ctx)) == NULL) |
1389 | 0 | goto err; |
1390 | | |
1391 | 0 | do { |
1392 | 0 | if (!BN_priv_rand_range(e, group->field)) |
1393 | 0 | goto err; |
1394 | 0 | } while (BN_is_zero(e)); |
1395 | | |
1396 | | /* r := a * e */ |
1397 | 0 | if (!group->meth->field_mul(group, r, a, e, ctx)) |
1398 | 0 | goto err; |
1399 | | /* r := 1/(a * e) */ |
1400 | 0 | if (!BN_mod_inverse(r, r, group->field, ctx)) { |
1401 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT); |
1402 | 0 | goto err; |
1403 | 0 | } |
1404 | | /* r := e/(a * e) = 1/a */ |
1405 | 0 | if (!group->meth->field_mul(group, r, r, e, ctx)) |
1406 | 0 | goto err; |
1407 | | |
1408 | 0 | ret = 1; |
1409 | |
|
1410 | 0 | err: |
1411 | 0 | BN_CTX_end(ctx); |
1412 | 0 | BN_CTX_free(new_ctx); |
1413 | 0 | return ret; |
1414 | 0 | } |
1415 | | |
1416 | | /*- |
1417 | | * Apply randomization of EC point projective coordinates: |
1418 | | * |
1419 | | * (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z) |
1420 | | * lambda = [1,group->field) |
1421 | | * |
1422 | | */ |
1423 | | int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p, |
1424 | | BN_CTX *ctx) |
1425 | 68 | { |
1426 | 68 | int ret = 0; |
1427 | 68 | BIGNUM *lambda = NULL; |
1428 | 68 | BIGNUM *temp = NULL; |
1429 | | |
1430 | 68 | BN_CTX_start(ctx); |
1431 | 68 | lambda = BN_CTX_get(ctx); |
1432 | 68 | temp = BN_CTX_get(ctx); |
1433 | 68 | if (temp == NULL) { |
1434 | 0 | ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE); |
1435 | 0 | goto end; |
1436 | 0 | } |
1437 | | |
1438 | | /*- |
1439 | | * Make sure lambda is not zero. |
1440 | | * If the RNG fails, we cannot blind but nevertheless want |
1441 | | * code to continue smoothly and not clobber the error stack. |
1442 | | */ |
1443 | 68 | do { |
1444 | 68 | ERR_set_mark(); |
1445 | 68 | ret = BN_priv_rand_range(lambda, group->field); |
1446 | 68 | ERR_pop_to_mark(); |
1447 | 68 | if (ret == 0) { |
1448 | 0 | ret = 1; |
1449 | 0 | goto end; |
1450 | 0 | } |
1451 | 68 | } while (BN_is_zero(lambda)); |
1452 | | |
1453 | | /* if field_encode defined convert between representations */ |
1454 | 68 | if ((group->meth->field_encode != NULL |
1455 | 68 | && !group->meth->field_encode(group, lambda, lambda, ctx)) |
1456 | 68 | || !group->meth->field_mul(group, p->Z, p->Z, lambda, ctx) |
1457 | 68 | || !group->meth->field_sqr(group, temp, lambda, ctx) |
1458 | 68 | || !group->meth->field_mul(group, p->X, p->X, temp, ctx) |
1459 | 68 | || !group->meth->field_mul(group, temp, temp, lambda, ctx) |
1460 | 68 | || !group->meth->field_mul(group, p->Y, p->Y, temp, ctx)) |
1461 | 0 | goto end; |
1462 | | |
1463 | 68 | p->Z_is_one = 0; |
1464 | 68 | ret = 1; |
1465 | | |
1466 | 68 | end: |
1467 | 68 | BN_CTX_end(ctx); |
1468 | 68 | return ret; |
1469 | 68 | } |
1470 | | |
1471 | | /*- |
1472 | | * Input: |
1473 | | * - p: affine coordinates |
1474 | | * |
1475 | | * Output: |
1476 | | * - s := p, r := 2p: blinded projective (homogeneous) coordinates |
1477 | | * |
1478 | | * For doubling we use Formula 3 from Izu-Takagi "A fast parallel elliptic curve |
1479 | | * multiplication resistant against side channel attacks" appendix, described at |
1480 | | * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#doubling-dbl-2002-it-2 |
1481 | | * simplified for Z1=1. |
1482 | | * |
1483 | | * Blinding uses the equivalence relation (\lambda X, \lambda Y, \lambda Z) |
1484 | | * for any non-zero \lambda that holds for projective (homogeneous) coords. |
1485 | | */ |
1486 | | int ec_GFp_simple_ladder_pre(const EC_GROUP *group, |
1487 | | EC_POINT *r, EC_POINT *s, |
1488 | | EC_POINT *p, BN_CTX *ctx) |
1489 | 227 | { |
1490 | 227 | BIGNUM *t1, *t2, *t3, *t4, *t5 = NULL; |
1491 | | |
1492 | 227 | t1 = s->Z; |
1493 | 227 | t2 = r->Z; |
1494 | 227 | t3 = s->X; |
1495 | 227 | t4 = r->X; |
1496 | 227 | t5 = s->Y; |
1497 | | |
1498 | 227 | if (!p->Z_is_one /* r := 2p */ |
1499 | 227 | || !group->meth->field_sqr(group, t3, p->X, ctx) |
1500 | 227 | || !BN_mod_sub_quick(t4, t3, group->a, group->field) |
1501 | 227 | || !group->meth->field_sqr(group, t4, t4, ctx) |
1502 | 227 | || !group->meth->field_mul(group, t5, p->X, group->b, ctx) |
1503 | 227 | || !BN_mod_lshift_quick(t5, t5, 3, group->field) |
1504 | | /* r->X coord output */ |
1505 | 227 | || !BN_mod_sub_quick(r->X, t4, t5, group->field) |
1506 | 227 | || !BN_mod_add_quick(t1, t3, group->a, group->field) |
1507 | 227 | || !group->meth->field_mul(group, t2, p->X, t1, ctx) |
1508 | 227 | || !BN_mod_add_quick(t2, group->b, t2, group->field) |
1509 | | /* r->Z coord output */ |
1510 | 227 | || !BN_mod_lshift_quick(r->Z, t2, 2, group->field)) |
1511 | 0 | return 0; |
1512 | | |
1513 | | /* make sure lambda (r->Y here for storage) is not zero */ |
1514 | 227 | do { |
1515 | 227 | if (!BN_priv_rand_range(r->Y, group->field)) |
1516 | 0 | return 0; |
1517 | 227 | } while (BN_is_zero(r->Y)); |
1518 | | |
1519 | | /* make sure lambda (s->Z here for storage) is not zero */ |
1520 | 227 | do { |
1521 | 227 | if (!BN_priv_rand_range(s->Z, group->field)) |
1522 | 0 | return 0; |
1523 | 227 | } while (BN_is_zero(s->Z)); |
1524 | | |
1525 | | /* if field_encode defined convert between representations */ |
1526 | 227 | if (group->meth->field_encode != NULL |
1527 | 227 | && (!group->meth->field_encode(group, r->Y, r->Y, ctx) |
1528 | 227 | || !group->meth->field_encode(group, s->Z, s->Z, ctx))) |
1529 | 0 | return 0; |
1530 | | |
1531 | | /* blind r and s independently */ |
1532 | 227 | if (!group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx) |
1533 | 227 | || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx) |
1534 | 227 | || !group->meth->field_mul(group, s->X, p->X, s->Z, ctx)) /* s := p */ |
1535 | 0 | return 0; |
1536 | | |
1537 | 227 | r->Z_is_one = 0; |
1538 | 227 | s->Z_is_one = 0; |
1539 | | |
1540 | 227 | return 1; |
1541 | 227 | } |
1542 | | |
1543 | | /*- |
1544 | | * Input: |
1545 | | * - s, r: projective (homogeneous) coordinates |
1546 | | * - p: affine coordinates |
1547 | | * |
1548 | | * Output: |
1549 | | * - s := r + s, r := 2r: projective (homogeneous) coordinates |
1550 | | * |
1551 | | * Differential addition-and-doubling using Eq. (9) and (10) from Izu-Takagi |
1552 | | * "A fast parallel elliptic curve multiplication resistant against side channel |
1553 | | * attacks", as described at |
1554 | | * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-mladd-2002-it-4 |
1555 | | */ |
1556 | | int ec_GFp_simple_ladder_step(const EC_GROUP *group, |
1557 | | EC_POINT *r, EC_POINT *s, |
1558 | | EC_POINT *p, BN_CTX *ctx) |
1559 | 53.4k | { |
1560 | 53.4k | int ret = 0; |
1561 | 53.4k | BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL; |
1562 | | |
1563 | 53.4k | BN_CTX_start(ctx); |
1564 | 53.4k | t0 = BN_CTX_get(ctx); |
1565 | 53.4k | t1 = BN_CTX_get(ctx); |
1566 | 53.4k | t2 = BN_CTX_get(ctx); |
1567 | 53.4k | t3 = BN_CTX_get(ctx); |
1568 | 53.4k | t4 = BN_CTX_get(ctx); |
1569 | 53.4k | t5 = BN_CTX_get(ctx); |
1570 | 53.4k | t6 = BN_CTX_get(ctx); |
1571 | | |
1572 | 53.4k | if (t6 == NULL |
1573 | 53.4k | || !group->meth->field_mul(group, t6, r->X, s->X, ctx) |
1574 | 53.4k | || !group->meth->field_mul(group, t0, r->Z, s->Z, ctx) |
1575 | 53.4k | || !group->meth->field_mul(group, t4, r->X, s->Z, ctx) |
1576 | 53.4k | || !group->meth->field_mul(group, t3, r->Z, s->X, ctx) |
1577 | 53.4k | || !group->meth->field_mul(group, t5, group->a, t0, ctx) |
1578 | 53.4k | || !BN_mod_add_quick(t5, t6, t5, group->field) |
1579 | 53.4k | || !BN_mod_add_quick(t6, t3, t4, group->field) |
1580 | 53.4k | || !group->meth->field_mul(group, t5, t6, t5, ctx) |
1581 | 53.4k | || !group->meth->field_sqr(group, t0, t0, ctx) |
1582 | 53.4k | || !BN_mod_lshift_quick(t2, group->b, 2, group->field) |
1583 | 53.4k | || !group->meth->field_mul(group, t0, t2, t0, ctx) |
1584 | 53.4k | || !BN_mod_lshift1_quick(t5, t5, group->field) |
1585 | 53.4k | || !BN_mod_sub_quick(t3, t4, t3, group->field) |
1586 | | /* s->Z coord output */ |
1587 | 53.4k | || !group->meth->field_sqr(group, s->Z, t3, ctx) |
1588 | 53.4k | || !group->meth->field_mul(group, t4, s->Z, p->X, ctx) |
1589 | 53.4k | || !BN_mod_add_quick(t0, t0, t5, group->field) |
1590 | | /* s->X coord output */ |
1591 | 53.4k | || !BN_mod_sub_quick(s->X, t0, t4, group->field) |
1592 | 53.4k | || !group->meth->field_sqr(group, t4, r->X, ctx) |
1593 | 53.4k | || !group->meth->field_sqr(group, t5, r->Z, ctx) |
1594 | 53.4k | || !group->meth->field_mul(group, t6, t5, group->a, ctx) |
1595 | 53.4k | || !BN_mod_add_quick(t1, r->X, r->Z, group->field) |
1596 | 53.4k | || !group->meth->field_sqr(group, t1, t1, ctx) |
1597 | 53.4k | || !BN_mod_sub_quick(t1, t1, t4, group->field) |
1598 | 53.4k | || !BN_mod_sub_quick(t1, t1, t5, group->field) |
1599 | 53.4k | || !BN_mod_sub_quick(t3, t4, t6, group->field) |
1600 | 53.4k | || !group->meth->field_sqr(group, t3, t3, ctx) |
1601 | 53.4k | || !group->meth->field_mul(group, t0, t5, t1, ctx) |
1602 | 53.4k | || !group->meth->field_mul(group, t0, t2, t0, ctx) |
1603 | | /* r->X coord output */ |
1604 | 53.4k | || !BN_mod_sub_quick(r->X, t3, t0, group->field) |
1605 | 53.4k | || !BN_mod_add_quick(t3, t4, t6, group->field) |
1606 | 53.4k | || !group->meth->field_sqr(group, t4, t5, ctx) |
1607 | 53.4k | || !group->meth->field_mul(group, t4, t4, t2, ctx) |
1608 | 53.4k | || !group->meth->field_mul(group, t1, t1, t3, ctx) |
1609 | 53.4k | || !BN_mod_lshift1_quick(t1, t1, group->field) |
1610 | | /* r->Z coord output */ |
1611 | 53.4k | || !BN_mod_add_quick(r->Z, t4, t1, group->field)) |
1612 | 0 | goto err; |
1613 | | |
1614 | 53.4k | ret = 1; |
1615 | | |
1616 | 53.4k | err: |
1617 | 53.4k | BN_CTX_end(ctx); |
1618 | 53.4k | return ret; |
1619 | 53.4k | } |
1620 | | |
1621 | | /*- |
1622 | | * Input: |
1623 | | * - s, r: projective (homogeneous) coordinates |
1624 | | * - p: affine coordinates |
1625 | | * |
1626 | | * Output: |
1627 | | * - r := (x,y): affine coordinates |
1628 | | * |
1629 | | * Recovers the y-coordinate of r using Eq. (8) from Brier-Joye, "Weierstrass |
1630 | | * Elliptic Curves and Side-Channel Attacks", modified to work in mixed |
1631 | | * projective coords, i.e. p is affine and (r,s) in projective (homogeneous) |
1632 | | * coords, and return r in affine coordinates. |
1633 | | * |
1634 | | * X4 = two*Y1*X2*Z3*Z2; |
1635 | | * Y4 = two*b*Z3*SQR(Z2) + Z3*(a*Z2+X1*X2)*(X1*Z2+X2) - X3*SQR(X1*Z2-X2); |
1636 | | * Z4 = two*Y1*Z3*SQR(Z2); |
1637 | | * |
1638 | | * Z4 != 0 because: |
1639 | | * - Z2==0 implies r is at infinity (handled by the BN_is_zero(r->Z) branch); |
1640 | | * - Z3==0 implies s is at infinity (handled by the BN_is_zero(s->Z) branch); |
1641 | | * - Y1==0 implies p has order 2, so either r or s are infinity and handled by |
1642 | | * one of the BN_is_zero(...) branches. |
1643 | | */ |
1644 | | int ec_GFp_simple_ladder_post(const EC_GROUP *group, |
1645 | | EC_POINT *r, EC_POINT *s, |
1646 | | EC_POINT *p, BN_CTX *ctx) |
1647 | 227 | { |
1648 | 227 | int ret = 0; |
1649 | 227 | BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL; |
1650 | | |
1651 | 227 | if (BN_is_zero(r->Z)) |
1652 | 16 | return EC_POINT_set_to_infinity(group, r); |
1653 | | |
1654 | 211 | if (BN_is_zero(s->Z)) { |
1655 | 0 | if (!EC_POINT_copy(r, p) |
1656 | 0 | || !EC_POINT_invert(group, r, ctx)) |
1657 | 0 | return 0; |
1658 | 0 | return 1; |
1659 | 0 | } |
1660 | | |
1661 | 211 | BN_CTX_start(ctx); |
1662 | 211 | t0 = BN_CTX_get(ctx); |
1663 | 211 | t1 = BN_CTX_get(ctx); |
1664 | 211 | t2 = BN_CTX_get(ctx); |
1665 | 211 | t3 = BN_CTX_get(ctx); |
1666 | 211 | t4 = BN_CTX_get(ctx); |
1667 | 211 | t5 = BN_CTX_get(ctx); |
1668 | 211 | t6 = BN_CTX_get(ctx); |
1669 | | |
1670 | 211 | if (t6 == NULL |
1671 | 211 | || !BN_mod_lshift1_quick(t4, p->Y, group->field) |
1672 | 211 | || !group->meth->field_mul(group, t6, r->X, t4, ctx) |
1673 | 211 | || !group->meth->field_mul(group, t6, s->Z, t6, ctx) |
1674 | 211 | || !group->meth->field_mul(group, t5, r->Z, t6, ctx) |
1675 | 211 | || !BN_mod_lshift1_quick(t1, group->b, group->field) |
1676 | 211 | || !group->meth->field_mul(group, t1, s->Z, t1, ctx) |
1677 | 211 | || !group->meth->field_sqr(group, t3, r->Z, ctx) |
1678 | 211 | || !group->meth->field_mul(group, t2, t3, t1, ctx) |
1679 | 211 | || !group->meth->field_mul(group, t6, r->Z, group->a, ctx) |
1680 | 211 | || !group->meth->field_mul(group, t1, p->X, r->X, ctx) |
1681 | 211 | || !BN_mod_add_quick(t1, t1, t6, group->field) |
1682 | 211 | || !group->meth->field_mul(group, t1, s->Z, t1, ctx) |
1683 | 211 | || !group->meth->field_mul(group, t0, p->X, r->Z, ctx) |
1684 | 211 | || !BN_mod_add_quick(t6, r->X, t0, group->field) |
1685 | 211 | || !group->meth->field_mul(group, t6, t6, t1, ctx) |
1686 | 211 | || !BN_mod_add_quick(t6, t6, t2, group->field) |
1687 | 211 | || !BN_mod_sub_quick(t0, t0, r->X, group->field) |
1688 | 211 | || !group->meth->field_sqr(group, t0, t0, ctx) |
1689 | 211 | || !group->meth->field_mul(group, t0, t0, s->X, ctx) |
1690 | 211 | || !BN_mod_sub_quick(t0, t6, t0, group->field) |
1691 | 211 | || !group->meth->field_mul(group, t1, s->Z, t4, ctx) |
1692 | 211 | || !group->meth->field_mul(group, t1, t3, t1, ctx) |
1693 | 211 | || (group->meth->field_decode != NULL |
1694 | 211 | && !group->meth->field_decode(group, t1, t1, ctx)) |
1695 | 211 | || !group->meth->field_inv(group, t1, t1, ctx) |
1696 | 211 | || (group->meth->field_encode != NULL |
1697 | 211 | && !group->meth->field_encode(group, t1, t1, ctx)) |
1698 | 211 | || !group->meth->field_mul(group, r->X, t5, t1, ctx) |
1699 | 211 | || !group->meth->field_mul(group, r->Y, t0, t1, ctx)) |
1700 | 0 | goto err; |
1701 | | |
1702 | 211 | if (group->meth->field_set_to_one != NULL) { |
1703 | 211 | if (!group->meth->field_set_to_one(group, r->Z, ctx)) |
1704 | 0 | goto err; |
1705 | 211 | } else { |
1706 | 0 | if (!BN_one(r->Z)) |
1707 | 0 | goto err; |
1708 | 0 | } |
1709 | | |
1710 | 211 | r->Z_is_one = 1; |
1711 | 211 | ret = 1; |
1712 | | |
1713 | 211 | err: |
1714 | 211 | BN_CTX_end(ctx); |
1715 | 211 | return ret; |
1716 | 211 | } |