/src/openssl/crypto/ec/ec2_smpl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (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 | | /* |
12 | | * ECDSA low-level APIs are deprecated for public use, but still ok for |
13 | | * internal use. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <openssl/err.h> |
18 | | |
19 | | #include "crypto/bn.h" |
20 | | #include "ec_local.h" |
21 | | |
22 | | #ifndef OPENSSL_NO_EC2M |
23 | | |
24 | | /* |
25 | | * Initialize a GF(2^m)-based EC_GROUP structure. Note that all other members |
26 | | * are handled by EC_GROUP_new. |
27 | | */ |
28 | | int ossl_ec_GF2m_simple_group_init(EC_GROUP *group) |
29 | 0 | { |
30 | 0 | group->field = BN_new(); |
31 | 0 | group->a = BN_new(); |
32 | 0 | group->b = BN_new(); |
33 | |
|
34 | 0 | if (group->field == NULL || group->a == NULL || group->b == NULL) { |
35 | 0 | BN_free(group->field); |
36 | 0 | BN_free(group->a); |
37 | 0 | BN_free(group->b); |
38 | 0 | return 0; |
39 | 0 | } |
40 | 0 | return 1; |
41 | 0 | } |
42 | | |
43 | | /* |
44 | | * Free a GF(2^m)-based EC_GROUP structure. Note that all other members are |
45 | | * handled by EC_GROUP_free. |
46 | | */ |
47 | | void ossl_ec_GF2m_simple_group_finish(EC_GROUP *group) |
48 | 0 | { |
49 | 0 | BN_free(group->field); |
50 | 0 | BN_free(group->a); |
51 | 0 | BN_free(group->b); |
52 | 0 | } |
53 | | |
54 | | /* |
55 | | * Clear and free a GF(2^m)-based EC_GROUP structure. Note that all other |
56 | | * members are handled by EC_GROUP_clear_free. |
57 | | */ |
58 | | void ossl_ec_GF2m_simple_group_clear_finish(EC_GROUP *group) |
59 | 0 | { |
60 | 0 | BN_clear_free(group->field); |
61 | 0 | BN_clear_free(group->a); |
62 | 0 | BN_clear_free(group->b); |
63 | 0 | group->poly[0] = 0; |
64 | 0 | group->poly[1] = 0; |
65 | 0 | group->poly[2] = 0; |
66 | 0 | group->poly[3] = 0; |
67 | 0 | group->poly[4] = 0; |
68 | 0 | group->poly[5] = -1; |
69 | 0 | } |
70 | | |
71 | | /* |
72 | | * Copy a GF(2^m)-based EC_GROUP structure. Note that all other members are |
73 | | * handled by EC_GROUP_copy. |
74 | | */ |
75 | | int ossl_ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) |
76 | 0 | { |
77 | 0 | if (!BN_copy(dest->field, src->field)) |
78 | 0 | return 0; |
79 | 0 | if (!BN_copy(dest->a, src->a)) |
80 | 0 | return 0; |
81 | 0 | if (!BN_copy(dest->b, src->b)) |
82 | 0 | return 0; |
83 | 0 | dest->poly[0] = src->poly[0]; |
84 | 0 | dest->poly[1] = src->poly[1]; |
85 | 0 | dest->poly[2] = src->poly[2]; |
86 | 0 | dest->poly[3] = src->poly[3]; |
87 | 0 | dest->poly[4] = src->poly[4]; |
88 | 0 | dest->poly[5] = src->poly[5]; |
89 | 0 | if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL) |
90 | 0 | return 0; |
91 | 0 | if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL) |
92 | 0 | return 0; |
93 | 0 | bn_set_all_zero(dest->a); |
94 | 0 | bn_set_all_zero(dest->b); |
95 | 0 | return 1; |
96 | 0 | } |
97 | | |
98 | | /* Set the curve parameters of an EC_GROUP structure. */ |
99 | | int ossl_ec_GF2m_simple_group_set_curve(EC_GROUP *group, |
100 | | const BIGNUM *p, const BIGNUM *a, |
101 | | const BIGNUM *b, BN_CTX *ctx) |
102 | 0 | { |
103 | 0 | int ret = 0, i; |
104 | | |
105 | | /* group->field */ |
106 | 0 | if (!BN_copy(group->field, p)) |
107 | 0 | goto err; |
108 | 0 | i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1; |
109 | 0 | if ((i != 5) && (i != 3)) { |
110 | 0 | ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); |
111 | 0 | goto err; |
112 | 0 | } |
113 | | |
114 | | /* group->a */ |
115 | 0 | if (!BN_GF2m_mod_arr(group->a, a, group->poly)) |
116 | 0 | goto err; |
117 | 0 | if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2) |
118 | 0 | == NULL) |
119 | 0 | goto err; |
120 | 0 | bn_set_all_zero(group->a); |
121 | | |
122 | | /* group->b */ |
123 | 0 | if (!BN_GF2m_mod_arr(group->b, b, group->poly)) |
124 | 0 | goto err; |
125 | 0 | if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2) |
126 | 0 | == NULL) |
127 | 0 | goto err; |
128 | 0 | bn_set_all_zero(group->b); |
129 | |
|
130 | 0 | ret = 1; |
131 | 0 | err: |
132 | 0 | return ret; |
133 | 0 | } |
134 | | |
135 | | /* |
136 | | * Get the curve parameters of an EC_GROUP structure. If p, a, or b are NULL |
137 | | * then there values will not be set but the method will return with success. |
138 | | */ |
139 | | int ossl_ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, |
140 | | BIGNUM *a, BIGNUM *b, BN_CTX *ctx) |
141 | 0 | { |
142 | 0 | int ret = 0; |
143 | |
|
144 | 0 | if (p != NULL) { |
145 | 0 | if (!BN_copy(p, group->field)) |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | 0 | if (a != NULL) { |
150 | 0 | if (!BN_copy(a, group->a)) |
151 | 0 | goto err; |
152 | 0 | } |
153 | | |
154 | 0 | if (b != NULL) { |
155 | 0 | if (!BN_copy(b, group->b)) |
156 | 0 | goto err; |
157 | 0 | } |
158 | | |
159 | 0 | ret = 1; |
160 | |
|
161 | 0 | err: |
162 | 0 | return ret; |
163 | 0 | } |
164 | | |
165 | | /* |
166 | | * Gets the degree of the field. For a curve over GF(2^m) this is the value |
167 | | * m. |
168 | | */ |
169 | | int ossl_ec_GF2m_simple_group_get_degree(const EC_GROUP *group) |
170 | 0 | { |
171 | 0 | return BN_num_bits(group->field) - 1; |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Checks the discriminant of the curve. y^2 + x*y = x^3 + a*x^2 + b is an |
176 | | * elliptic curve <=> b != 0 (mod p) |
177 | | */ |
178 | | int ossl_ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, |
179 | | BN_CTX *ctx) |
180 | 0 | { |
181 | 0 | int ret = 0; |
182 | 0 | BIGNUM *b; |
183 | 0 | #ifndef FIPS_MODULE |
184 | 0 | BN_CTX *new_ctx = NULL; |
185 | |
|
186 | 0 | if (ctx == NULL) { |
187 | 0 | ctx = new_ctx = BN_CTX_new(); |
188 | 0 | if (ctx == NULL) { |
189 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
190 | 0 | goto err; |
191 | 0 | } |
192 | 0 | } |
193 | 0 | #endif |
194 | 0 | BN_CTX_start(ctx); |
195 | 0 | b = BN_CTX_get(ctx); |
196 | 0 | if (b == NULL) |
197 | 0 | goto err; |
198 | | |
199 | 0 | if (!BN_GF2m_mod_arr(b, group->b, group->poly)) |
200 | 0 | goto err; |
201 | | |
202 | | /* |
203 | | * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic |
204 | | * curve <=> b != 0 (mod p) |
205 | | */ |
206 | 0 | if (BN_is_zero(b)) |
207 | 0 | goto err; |
208 | | |
209 | 0 | ret = 1; |
210 | |
|
211 | 0 | err: |
212 | 0 | BN_CTX_end(ctx); |
213 | 0 | #ifndef FIPS_MODULE |
214 | 0 | BN_CTX_free(new_ctx); |
215 | 0 | #endif |
216 | 0 | return ret; |
217 | 0 | } |
218 | | |
219 | | /* Initializes an EC_POINT. */ |
220 | | int ossl_ec_GF2m_simple_point_init(EC_POINT *point) |
221 | 0 | { |
222 | 0 | point->X = BN_new(); |
223 | 0 | point->Y = BN_new(); |
224 | 0 | point->Z = BN_new(); |
225 | |
|
226 | 0 | if (point->X == NULL || point->Y == NULL || point->Z == NULL) { |
227 | 0 | BN_free(point->X); |
228 | 0 | BN_free(point->Y); |
229 | 0 | BN_free(point->Z); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | return 1; |
233 | 0 | } |
234 | | |
235 | | /* Frees an EC_POINT. */ |
236 | | void ossl_ec_GF2m_simple_point_finish(EC_POINT *point) |
237 | 0 | { |
238 | 0 | BN_free(point->X); |
239 | 0 | BN_free(point->Y); |
240 | 0 | BN_free(point->Z); |
241 | 0 | } |
242 | | |
243 | | /* Clears and frees an EC_POINT. */ |
244 | | void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *point) |
245 | 0 | { |
246 | 0 | BN_clear_free(point->X); |
247 | 0 | BN_clear_free(point->Y); |
248 | 0 | BN_clear_free(point->Z); |
249 | 0 | point->Z_is_one = 0; |
250 | 0 | } |
251 | | |
252 | | /* |
253 | | * Copy the contents of one EC_POINT into another. Assumes dest is |
254 | | * initialized. |
255 | | */ |
256 | | int ossl_ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src) |
257 | 0 | { |
258 | 0 | if (!BN_copy(dest->X, src->X)) |
259 | 0 | return 0; |
260 | 0 | if (!BN_copy(dest->Y, src->Y)) |
261 | 0 | return 0; |
262 | 0 | if (!BN_copy(dest->Z, src->Z)) |
263 | 0 | return 0; |
264 | 0 | dest->Z_is_one = src->Z_is_one; |
265 | 0 | dest->curve_name = src->curve_name; |
266 | |
|
267 | 0 | return 1; |
268 | 0 | } |
269 | | |
270 | | /* |
271 | | * Set an EC_POINT to the point at infinity. A point at infinity is |
272 | | * represented by having Z=0. |
273 | | */ |
274 | | int ossl_ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group, |
275 | | EC_POINT *point) |
276 | 0 | { |
277 | 0 | point->Z_is_one = 0; |
278 | 0 | BN_zero(point->Z); |
279 | 0 | return 1; |
280 | 0 | } |
281 | | |
282 | | /* |
283 | | * Set the coordinates of an EC_POINT using affine coordinates. Note that |
284 | | * the simple implementation only uses affine coordinates. |
285 | | */ |
286 | | int ossl_ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group, |
287 | | EC_POINT *point, |
288 | | const BIGNUM *x, |
289 | | const BIGNUM *y, |
290 | | BN_CTX *ctx) |
291 | 0 | { |
292 | 0 | int ret = 0; |
293 | 0 | if (x == NULL || y == NULL) { |
294 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | 0 | if (!BN_copy(point->X, x)) |
299 | 0 | goto err; |
300 | 0 | BN_set_negative(point->X, 0); |
301 | 0 | if (!BN_copy(point->Y, y)) |
302 | 0 | goto err; |
303 | 0 | BN_set_negative(point->Y, 0); |
304 | 0 | if (!BN_copy(point->Z, BN_value_one())) |
305 | 0 | goto err; |
306 | 0 | BN_set_negative(point->Z, 0); |
307 | 0 | point->Z_is_one = 1; |
308 | 0 | ret = 1; |
309 | |
|
310 | 0 | err: |
311 | 0 | return ret; |
312 | 0 | } |
313 | | |
314 | | /* |
315 | | * Gets the affine coordinates of an EC_POINT. Note that the simple |
316 | | * implementation only uses affine coordinates. |
317 | | */ |
318 | | int ossl_ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group, |
319 | | const EC_POINT *point, |
320 | | BIGNUM *x, BIGNUM *y, |
321 | | BN_CTX *ctx) |
322 | 0 | { |
323 | 0 | int ret = 0; |
324 | |
|
325 | 0 | if (EC_POINT_is_at_infinity(group, point)) { |
326 | 0 | ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY); |
327 | 0 | return 0; |
328 | 0 | } |
329 | | |
330 | 0 | if (BN_cmp(point->Z, BN_value_one())) { |
331 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
332 | 0 | return 0; |
333 | 0 | } |
334 | 0 | if (x != NULL) { |
335 | 0 | if (!BN_copy(x, point->X)) |
336 | 0 | goto err; |
337 | 0 | BN_set_negative(x, 0); |
338 | 0 | } |
339 | 0 | if (y != NULL) { |
340 | 0 | if (!BN_copy(y, point->Y)) |
341 | 0 | goto err; |
342 | 0 | BN_set_negative(y, 0); |
343 | 0 | } |
344 | 0 | ret = 1; |
345 | |
|
346 | 0 | err: |
347 | 0 | return ret; |
348 | 0 | } |
349 | | |
350 | | /* |
351 | | * Computes a + b and stores the result in r. r could be a or b, a could be |
352 | | * b. Uses algorithm A.10.2 of IEEE P1363. |
353 | | */ |
354 | | int ossl_ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, |
355 | | const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx) |
356 | 0 | { |
357 | 0 | BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t; |
358 | 0 | int ret = 0; |
359 | 0 | #ifndef FIPS_MODULE |
360 | 0 | BN_CTX *new_ctx = NULL; |
361 | 0 | #endif |
362 | |
|
363 | 0 | if (EC_POINT_is_at_infinity(group, a)) { |
364 | 0 | if (!EC_POINT_copy(r, b)) |
365 | 0 | return 0; |
366 | 0 | return 1; |
367 | 0 | } |
368 | | |
369 | 0 | if (EC_POINT_is_at_infinity(group, b)) { |
370 | 0 | if (!EC_POINT_copy(r, a)) |
371 | 0 | return 0; |
372 | 0 | return 1; |
373 | 0 | } |
374 | | |
375 | 0 | #ifndef FIPS_MODULE |
376 | 0 | if (ctx == NULL) { |
377 | 0 | ctx = new_ctx = BN_CTX_new(); |
378 | 0 | if (ctx == NULL) |
379 | 0 | return 0; |
380 | 0 | } |
381 | 0 | #endif |
382 | | |
383 | 0 | BN_CTX_start(ctx); |
384 | 0 | x0 = BN_CTX_get(ctx); |
385 | 0 | y0 = BN_CTX_get(ctx); |
386 | 0 | x1 = BN_CTX_get(ctx); |
387 | 0 | y1 = BN_CTX_get(ctx); |
388 | 0 | x2 = BN_CTX_get(ctx); |
389 | 0 | y2 = BN_CTX_get(ctx); |
390 | 0 | s = BN_CTX_get(ctx); |
391 | 0 | t = BN_CTX_get(ctx); |
392 | 0 | if (t == NULL) |
393 | 0 | goto err; |
394 | | |
395 | 0 | if (a->Z_is_one) { |
396 | 0 | if (!BN_copy(x0, a->X)) |
397 | 0 | goto err; |
398 | 0 | if (!BN_copy(y0, a->Y)) |
399 | 0 | goto err; |
400 | 0 | } else { |
401 | 0 | if (!EC_POINT_get_affine_coordinates(group, a, x0, y0, ctx)) |
402 | 0 | goto err; |
403 | 0 | } |
404 | 0 | if (b->Z_is_one) { |
405 | 0 | if (!BN_copy(x1, b->X)) |
406 | 0 | goto err; |
407 | 0 | if (!BN_copy(y1, b->Y)) |
408 | 0 | goto err; |
409 | 0 | } else { |
410 | 0 | if (!EC_POINT_get_affine_coordinates(group, b, x1, y1, ctx)) |
411 | 0 | goto err; |
412 | 0 | } |
413 | | |
414 | 0 | if (BN_GF2m_cmp(x0, x1)) { |
415 | 0 | if (!BN_GF2m_add(t, x0, x1)) |
416 | 0 | goto err; |
417 | 0 | if (!BN_GF2m_add(s, y0, y1)) |
418 | 0 | goto err; |
419 | 0 | if (!group->meth->field_div(group, s, s, t, ctx)) |
420 | 0 | goto err; |
421 | 0 | if (!group->meth->field_sqr(group, x2, s, ctx)) |
422 | 0 | goto err; |
423 | 0 | if (!BN_GF2m_add(x2, x2, group->a)) |
424 | 0 | goto err; |
425 | 0 | if (!BN_GF2m_add(x2, x2, s)) |
426 | 0 | goto err; |
427 | 0 | if (!BN_GF2m_add(x2, x2, t)) |
428 | 0 | goto err; |
429 | 0 | } else { |
430 | 0 | if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) { |
431 | 0 | if (!EC_POINT_set_to_infinity(group, r)) |
432 | 0 | goto err; |
433 | 0 | ret = 1; |
434 | 0 | goto err; |
435 | 0 | } |
436 | 0 | if (!group->meth->field_div(group, s, y1, x1, ctx)) |
437 | 0 | goto err; |
438 | 0 | if (!BN_GF2m_add(s, s, x1)) |
439 | 0 | goto err; |
440 | | |
441 | 0 | if (!group->meth->field_sqr(group, x2, s, ctx)) |
442 | 0 | goto err; |
443 | 0 | if (!BN_GF2m_add(x2, x2, s)) |
444 | 0 | goto err; |
445 | 0 | if (!BN_GF2m_add(x2, x2, group->a)) |
446 | 0 | goto err; |
447 | 0 | } |
448 | | |
449 | 0 | if (!BN_GF2m_add(y2, x1, x2)) |
450 | 0 | goto err; |
451 | 0 | if (!group->meth->field_mul(group, y2, y2, s, ctx)) |
452 | 0 | goto err; |
453 | 0 | if (!BN_GF2m_add(y2, y2, x2)) |
454 | 0 | goto err; |
455 | 0 | if (!BN_GF2m_add(y2, y2, y1)) |
456 | 0 | goto err; |
457 | | |
458 | 0 | if (!EC_POINT_set_affine_coordinates(group, r, x2, y2, ctx)) |
459 | 0 | goto err; |
460 | | |
461 | 0 | ret = 1; |
462 | |
|
463 | 0 | err: |
464 | 0 | BN_CTX_end(ctx); |
465 | 0 | #ifndef FIPS_MODULE |
466 | 0 | BN_CTX_free(new_ctx); |
467 | 0 | #endif |
468 | 0 | return ret; |
469 | 0 | } |
470 | | |
471 | | /* |
472 | | * Computes 2 * a and stores the result in r. r could be a. Uses algorithm |
473 | | * A.10.2 of IEEE P1363. |
474 | | */ |
475 | | int ossl_ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, |
476 | | const EC_POINT *a, BN_CTX *ctx) |
477 | 0 | { |
478 | 0 | return ossl_ec_GF2m_simple_add(group, r, a, a, ctx); |
479 | 0 | } |
480 | | |
481 | | int ossl_ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, |
482 | | BN_CTX *ctx) |
483 | 0 | { |
484 | 0 | if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y)) |
485 | | /* point is its own inverse */ |
486 | 0 | return 1; |
487 | | |
488 | 0 | if (group->meth->make_affine == NULL |
489 | 0 | || !group->meth->make_affine(group, point, ctx)) |
490 | 0 | return 0; |
491 | 0 | return BN_GF2m_add(point->Y, point->X, point->Y); |
492 | 0 | } |
493 | | |
494 | | /* Indicates whether the given point is the point at infinity. */ |
495 | | int ossl_ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, |
496 | | const EC_POINT *point) |
497 | 0 | { |
498 | 0 | return BN_is_zero(point->Z); |
499 | 0 | } |
500 | | |
501 | | /*- |
502 | | * Determines whether the given EC_POINT is an actual point on the curve defined |
503 | | * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation: |
504 | | * y^2 + x*y = x^3 + a*x^2 + b. |
505 | | */ |
506 | | int ossl_ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, |
507 | | BN_CTX *ctx) |
508 | 0 | { |
509 | 0 | int ret = -1; |
510 | 0 | BIGNUM *lh, *y2; |
511 | 0 | int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, |
512 | 0 | const BIGNUM *, BN_CTX *); |
513 | 0 | int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); |
514 | 0 | #ifndef FIPS_MODULE |
515 | 0 | BN_CTX *new_ctx = NULL; |
516 | 0 | #endif |
517 | |
|
518 | 0 | if (EC_POINT_is_at_infinity(group, point)) |
519 | 0 | return 1; |
520 | | |
521 | 0 | field_mul = group->meth->field_mul; |
522 | 0 | field_sqr = group->meth->field_sqr; |
523 | | |
524 | | /* only support affine coordinates */ |
525 | 0 | if (!point->Z_is_one) |
526 | 0 | return -1; |
527 | | |
528 | 0 | #ifndef FIPS_MODULE |
529 | 0 | if (ctx == NULL) { |
530 | 0 | ctx = new_ctx = BN_CTX_new(); |
531 | 0 | if (ctx == NULL) |
532 | 0 | return -1; |
533 | 0 | } |
534 | 0 | #endif |
535 | | |
536 | 0 | BN_CTX_start(ctx); |
537 | 0 | y2 = BN_CTX_get(ctx); |
538 | 0 | lh = BN_CTX_get(ctx); |
539 | 0 | if (lh == NULL) |
540 | 0 | goto err; |
541 | | |
542 | | /*- |
543 | | * We have a curve defined by a Weierstrass equation |
544 | | * y^2 + x*y = x^3 + a*x^2 + b. |
545 | | * <=> x^3 + a*x^2 + x*y + b + y^2 = 0 |
546 | | * <=> ((x + a) * x + y) * x + b + y^2 = 0 |
547 | | */ |
548 | 0 | if (!BN_GF2m_add(lh, point->X, group->a)) |
549 | 0 | goto err; |
550 | 0 | if (!field_mul(group, lh, lh, point->X, ctx)) |
551 | 0 | goto err; |
552 | 0 | if (!BN_GF2m_add(lh, lh, point->Y)) |
553 | 0 | goto err; |
554 | 0 | if (!field_mul(group, lh, lh, point->X, ctx)) |
555 | 0 | goto err; |
556 | 0 | if (!BN_GF2m_add(lh, lh, group->b)) |
557 | 0 | goto err; |
558 | 0 | if (!field_sqr(group, y2, point->Y, ctx)) |
559 | 0 | goto err; |
560 | 0 | if (!BN_GF2m_add(lh, lh, y2)) |
561 | 0 | goto err; |
562 | 0 | ret = BN_is_zero(lh); |
563 | |
|
564 | 0 | err: |
565 | 0 | BN_CTX_end(ctx); |
566 | 0 | #ifndef FIPS_MODULE |
567 | 0 | BN_CTX_free(new_ctx); |
568 | 0 | #endif |
569 | 0 | return ret; |
570 | 0 | } |
571 | | |
572 | | /*- |
573 | | * Indicates whether two points are equal. |
574 | | * Return values: |
575 | | * -1 error |
576 | | * 0 equal (in affine coordinates) |
577 | | * 1 not equal |
578 | | */ |
579 | | int ossl_ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a, |
580 | | const EC_POINT *b, BN_CTX *ctx) |
581 | 0 | { |
582 | 0 | BIGNUM *aX, *aY, *bX, *bY; |
583 | 0 | int ret = -1; |
584 | 0 | #ifndef FIPS_MODULE |
585 | 0 | BN_CTX *new_ctx = NULL; |
586 | 0 | #endif |
587 | |
|
588 | 0 | if (EC_POINT_is_at_infinity(group, a)) { |
589 | 0 | return EC_POINT_is_at_infinity(group, b) ? 0 : 1; |
590 | 0 | } |
591 | | |
592 | 0 | if (EC_POINT_is_at_infinity(group, b)) |
593 | 0 | return 1; |
594 | | |
595 | 0 | if (a->Z_is_one && b->Z_is_one) { |
596 | 0 | return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1; |
597 | 0 | } |
598 | | |
599 | 0 | #ifndef FIPS_MODULE |
600 | 0 | if (ctx == NULL) { |
601 | 0 | ctx = new_ctx = BN_CTX_new(); |
602 | 0 | if (ctx == NULL) |
603 | 0 | return -1; |
604 | 0 | } |
605 | 0 | #endif |
606 | | |
607 | 0 | BN_CTX_start(ctx); |
608 | 0 | aX = BN_CTX_get(ctx); |
609 | 0 | aY = BN_CTX_get(ctx); |
610 | 0 | bX = BN_CTX_get(ctx); |
611 | 0 | bY = BN_CTX_get(ctx); |
612 | 0 | if (bY == NULL) |
613 | 0 | goto err; |
614 | | |
615 | 0 | if (!EC_POINT_get_affine_coordinates(group, a, aX, aY, ctx)) |
616 | 0 | goto err; |
617 | 0 | if (!EC_POINT_get_affine_coordinates(group, b, bX, bY, ctx)) |
618 | 0 | goto err; |
619 | 0 | ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1; |
620 | |
|
621 | 0 | err: |
622 | 0 | BN_CTX_end(ctx); |
623 | 0 | #ifndef FIPS_MODULE |
624 | 0 | BN_CTX_free(new_ctx); |
625 | 0 | #endif |
626 | 0 | return ret; |
627 | 0 | } |
628 | | |
629 | | /* Forces the given EC_POINT to internally use affine coordinates. */ |
630 | | int ossl_ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, |
631 | | BN_CTX *ctx) |
632 | 0 | { |
633 | 0 | BIGNUM *x, *y; |
634 | 0 | int ret = 0; |
635 | 0 | #ifndef FIPS_MODULE |
636 | 0 | BN_CTX *new_ctx = NULL; |
637 | 0 | #endif |
638 | |
|
639 | 0 | if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) |
640 | 0 | return 1; |
641 | | |
642 | 0 | #ifndef FIPS_MODULE |
643 | 0 | if (ctx == NULL) { |
644 | 0 | ctx = new_ctx = BN_CTX_new(); |
645 | 0 | if (ctx == NULL) |
646 | 0 | return 0; |
647 | 0 | } |
648 | 0 | #endif |
649 | | |
650 | 0 | BN_CTX_start(ctx); |
651 | 0 | x = BN_CTX_get(ctx); |
652 | 0 | y = BN_CTX_get(ctx); |
653 | 0 | if (y == NULL) |
654 | 0 | goto err; |
655 | | |
656 | 0 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
657 | 0 | goto err; |
658 | 0 | if (!BN_copy(point->X, x)) |
659 | 0 | goto err; |
660 | 0 | if (!BN_copy(point->Y, y)) |
661 | 0 | goto err; |
662 | 0 | if (!BN_one(point->Z)) |
663 | 0 | goto err; |
664 | 0 | point->Z_is_one = 1; |
665 | |
|
666 | 0 | ret = 1; |
667 | |
|
668 | 0 | err: |
669 | 0 | BN_CTX_end(ctx); |
670 | 0 | #ifndef FIPS_MODULE |
671 | 0 | BN_CTX_free(new_ctx); |
672 | 0 | #endif |
673 | 0 | return ret; |
674 | 0 | } |
675 | | |
676 | | /* |
677 | | * Forces each of the EC_POINTs in the given array to use affine coordinates. |
678 | | */ |
679 | | int ossl_ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num, |
680 | | EC_POINT *points[], BN_CTX *ctx) |
681 | 0 | { |
682 | 0 | size_t i; |
683 | |
|
684 | 0 | for (i = 0; i < num; i++) { |
685 | 0 | if (!group->meth->make_affine(group, points[i], ctx)) |
686 | 0 | return 0; |
687 | 0 | } |
688 | | |
689 | 0 | return 1; |
690 | 0 | } |
691 | | |
692 | | /* Wrapper to simple binary polynomial field multiplication implementation. */ |
693 | | int ossl_ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, |
694 | | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
695 | 0 | { |
696 | 0 | return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx); |
697 | 0 | } |
698 | | |
699 | | /* Wrapper to simple binary polynomial field squaring implementation. */ |
700 | | int ossl_ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, |
701 | | const BIGNUM *a, BN_CTX *ctx) |
702 | 0 | { |
703 | 0 | return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx); |
704 | 0 | } |
705 | | |
706 | | /* Wrapper to simple binary polynomial field division implementation. */ |
707 | | int ossl_ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, |
708 | | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
709 | 0 | { |
710 | 0 | return BN_GF2m_mod_div(r, a, b, group->field, ctx); |
711 | 0 | } |
712 | | |
713 | | /*- |
714 | | * Lopez-Dahab ladder, pre step. |
715 | | * See e.g. "Guide to ECC" Alg 3.40. |
716 | | * Modified to blind s and r independently. |
717 | | * s:= p, r := 2p |
718 | | */ |
719 | | static int ec_GF2m_simple_ladder_pre(const EC_GROUP *group, |
720 | | EC_POINT *r, EC_POINT *s, |
721 | | EC_POINT *p, BN_CTX *ctx) |
722 | 0 | { |
723 | | /* if p is not affine, something is wrong */ |
724 | 0 | if (p->Z_is_one == 0) |
725 | 0 | return 0; |
726 | | |
727 | | /* s blinding: make sure lambda (s->Z here) is not zero */ |
728 | 0 | do { |
729 | 0 | if (!BN_priv_rand_ex(s->Z, BN_num_bits(group->field) - 1, |
730 | 0 | BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) { |
731 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
732 | 0 | return 0; |
733 | 0 | } |
734 | 0 | } while (BN_is_zero(s->Z)); |
735 | | |
736 | | /* if field_encode defined convert between representations */ |
737 | 0 | if ((group->meth->field_encode != NULL |
738 | 0 | && !group->meth->field_encode(group, s->Z, s->Z, ctx)) |
739 | 0 | || !group->meth->field_mul(group, s->X, p->X, s->Z, ctx)) |
740 | 0 | return 0; |
741 | | |
742 | | /* r blinding: make sure lambda (r->Y here for storage) is not zero */ |
743 | 0 | do { |
744 | 0 | if (!BN_priv_rand_ex(r->Y, BN_num_bits(group->field) - 1, |
745 | 0 | BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) { |
746 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
747 | 0 | return 0; |
748 | 0 | } |
749 | 0 | } while (BN_is_zero(r->Y)); |
750 | | |
751 | 0 | if ((group->meth->field_encode != NULL |
752 | 0 | && !group->meth->field_encode(group, r->Y, r->Y, ctx)) |
753 | 0 | || !group->meth->field_sqr(group, r->Z, p->X, ctx) |
754 | 0 | || !group->meth->field_sqr(group, r->X, r->Z, ctx) |
755 | 0 | || !BN_GF2m_add(r->X, r->X, group->b) |
756 | 0 | || !group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx) |
757 | 0 | || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx)) |
758 | 0 | return 0; |
759 | | |
760 | 0 | s->Z_is_one = 0; |
761 | 0 | r->Z_is_one = 0; |
762 | |
|
763 | 0 | return 1; |
764 | 0 | } |
765 | | |
766 | | /*- |
767 | | * Ladder step: differential addition-and-doubling, mixed Lopez-Dahab coords. |
768 | | * http://www.hyperelliptic.org/EFD/g12o/auto-code/shortw/xz/ladder/mladd-2003-s.op3 |
769 | | * s := r + s, r := 2r |
770 | | */ |
771 | | static int ec_GF2m_simple_ladder_step(const EC_GROUP *group, |
772 | | EC_POINT *r, EC_POINT *s, |
773 | | EC_POINT *p, BN_CTX *ctx) |
774 | 0 | { |
775 | 0 | if (!group->meth->field_mul(group, r->Y, r->Z, s->X, ctx) |
776 | 0 | || !group->meth->field_mul(group, s->X, r->X, s->Z, ctx) |
777 | 0 | || !group->meth->field_sqr(group, s->Y, r->Z, ctx) |
778 | 0 | || !group->meth->field_sqr(group, r->Z, r->X, ctx) |
779 | 0 | || !BN_GF2m_add(s->Z, r->Y, s->X) |
780 | 0 | || !group->meth->field_sqr(group, s->Z, s->Z, ctx) |
781 | 0 | || !group->meth->field_mul(group, s->X, r->Y, s->X, ctx) |
782 | 0 | || !group->meth->field_mul(group, r->Y, s->Z, p->X, ctx) |
783 | 0 | || !BN_GF2m_add(s->X, s->X, r->Y) |
784 | 0 | || !group->meth->field_sqr(group, r->Y, r->Z, ctx) |
785 | 0 | || !group->meth->field_mul(group, r->Z, r->Z, s->Y, ctx) |
786 | 0 | || !group->meth->field_sqr(group, s->Y, s->Y, ctx) |
787 | 0 | || !group->meth->field_mul(group, s->Y, s->Y, group->b, ctx) |
788 | 0 | || !BN_GF2m_add(r->X, r->Y, s->Y)) |
789 | 0 | return 0; |
790 | | |
791 | 0 | return 1; |
792 | 0 | } |
793 | | |
794 | | /*- |
795 | | * Recover affine (x,y) result from Lopez-Dahab r and s, affine p. |
796 | | * See e.g. "Fast Multiplication on Elliptic Curves over GF(2**m) |
797 | | * without Precomputation" (Lopez and Dahab, CHES 1999), |
798 | | * Appendix Alg Mxy. |
799 | | */ |
800 | | static int ec_GF2m_simple_ladder_post(const EC_GROUP *group, |
801 | | EC_POINT *r, EC_POINT *s, |
802 | | EC_POINT *p, BN_CTX *ctx) |
803 | 0 | { |
804 | 0 | int ret = 0; |
805 | 0 | BIGNUM *t0, *t1, *t2 = NULL; |
806 | |
|
807 | 0 | if (BN_is_zero(r->Z)) |
808 | 0 | return EC_POINT_set_to_infinity(group, r); |
809 | | |
810 | 0 | if (BN_is_zero(s->Z)) { |
811 | 0 | if (!EC_POINT_copy(r, p) |
812 | 0 | || !EC_POINT_invert(group, r, ctx)) { |
813 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
814 | 0 | return 0; |
815 | 0 | } |
816 | 0 | return 1; |
817 | 0 | } |
818 | | |
819 | 0 | BN_CTX_start(ctx); |
820 | 0 | t0 = BN_CTX_get(ctx); |
821 | 0 | t1 = BN_CTX_get(ctx); |
822 | 0 | t2 = BN_CTX_get(ctx); |
823 | 0 | if (t2 == NULL) { |
824 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
825 | 0 | goto err; |
826 | 0 | } |
827 | | |
828 | 0 | if (!group->meth->field_mul(group, t0, r->Z, s->Z, ctx) |
829 | 0 | || !group->meth->field_mul(group, t1, p->X, r->Z, ctx) |
830 | 0 | || !BN_GF2m_add(t1, r->X, t1) |
831 | 0 | || !group->meth->field_mul(group, t2, p->X, s->Z, ctx) |
832 | 0 | || !group->meth->field_mul(group, r->Z, r->X, t2, ctx) |
833 | 0 | || !BN_GF2m_add(t2, t2, s->X) |
834 | 0 | || !group->meth->field_mul(group, t1, t1, t2, ctx) |
835 | 0 | || !group->meth->field_sqr(group, t2, p->X, ctx) |
836 | 0 | || !BN_GF2m_add(t2, p->Y, t2) |
837 | 0 | || !group->meth->field_mul(group, t2, t2, t0, ctx) |
838 | 0 | || !BN_GF2m_add(t1, t2, t1) |
839 | 0 | || !group->meth->field_mul(group, t2, p->X, t0, ctx) |
840 | 0 | || !group->meth->field_inv(group, t2, t2, ctx) |
841 | 0 | || !group->meth->field_mul(group, t1, t1, t2, ctx) |
842 | 0 | || !group->meth->field_mul(group, r->X, r->Z, t2, ctx) |
843 | 0 | || !BN_GF2m_add(t2, p->X, r->X) |
844 | 0 | || !group->meth->field_mul(group, t2, t2, t1, ctx) |
845 | 0 | || !BN_GF2m_add(r->Y, p->Y, t2) |
846 | 0 | || !BN_one(r->Z)) |
847 | 0 | goto err; |
848 | | |
849 | 0 | r->Z_is_one = 1; |
850 | | |
851 | | /* GF(2^m) field elements should always have BIGNUM::neg = 0 */ |
852 | 0 | BN_set_negative(r->X, 0); |
853 | 0 | BN_set_negative(r->Y, 0); |
854 | |
|
855 | 0 | ret = 1; |
856 | |
|
857 | 0 | err: |
858 | 0 | BN_CTX_end(ctx); |
859 | 0 | return ret; |
860 | 0 | } |
861 | | |
862 | | static int ec_GF2m_simple_points_mul(const EC_GROUP *group, EC_POINT *r, |
863 | | const BIGNUM *scalar, size_t num, |
864 | | const EC_POINT *points[], |
865 | | const BIGNUM *scalars[], |
866 | | BN_CTX *ctx) |
867 | 0 | { |
868 | 0 | int ret = 0; |
869 | 0 | EC_POINT *t = NULL; |
870 | | |
871 | | /*- |
872 | | * We limit use of the ladder only to the following cases: |
873 | | * - r := scalar * G |
874 | | * Fixed point mul: scalar != NULL && num == 0; |
875 | | * - r := scalars[0] * points[0] |
876 | | * Variable point mul: scalar == NULL && num == 1; |
877 | | * - r := scalar * G + scalars[0] * points[0] |
878 | | * used, e.g., in ECDSA verification: scalar != NULL && num == 1 |
879 | | * |
880 | | * In any other case (num > 1) we use the default wNAF implementation. |
881 | | * |
882 | | * We also let the default implementation handle degenerate cases like group |
883 | | * order or cofactor set to 0. |
884 | | */ |
885 | 0 | if (num > 1 || BN_is_zero(group->order) || BN_is_zero(group->cofactor)) |
886 | 0 | return ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); |
887 | | |
888 | 0 | if (scalar != NULL && num == 0) |
889 | | /* Fixed point multiplication */ |
890 | 0 | return ossl_ec_scalar_mul_ladder(group, r, scalar, NULL, ctx); |
891 | | |
892 | 0 | if (scalar == NULL && num == 1) |
893 | | /* Variable point multiplication */ |
894 | 0 | return ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx); |
895 | | |
896 | | /*- |
897 | | * Double point multiplication: |
898 | | * r := scalar * G + scalars[0] * points[0] |
899 | | */ |
900 | | |
901 | 0 | if ((t = EC_POINT_new(group)) == NULL) { |
902 | 0 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
903 | 0 | return 0; |
904 | 0 | } |
905 | | |
906 | 0 | if (!ossl_ec_scalar_mul_ladder(group, t, scalar, NULL, ctx) |
907 | 0 | || !ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx) |
908 | 0 | || !EC_POINT_add(group, r, t, r, ctx)) |
909 | 0 | goto err; |
910 | | |
911 | 0 | ret = 1; |
912 | |
|
913 | 0 | err: |
914 | 0 | EC_POINT_free(t); |
915 | 0 | return ret; |
916 | 0 | } |
917 | | |
918 | | /*- |
919 | | * Computes the multiplicative inverse of a in GF(2^m), storing the result in r. |
920 | | * If a is zero (or equivalent), you'll get an EC_R_CANNOT_INVERT error. |
921 | | * SCA hardening is with blinding: BN_GF2m_mod_inv does that. |
922 | | */ |
923 | | static int ec_GF2m_simple_field_inv(const EC_GROUP *group, BIGNUM *r, |
924 | | const BIGNUM *a, BN_CTX *ctx) |
925 | 0 | { |
926 | 0 | int ret; |
927 | |
|
928 | 0 | if (!(ret = BN_GF2m_mod_inv(r, a, group->field, ctx))) |
929 | 0 | ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT); |
930 | 0 | return ret; |
931 | 0 | } |
932 | | |
933 | | const EC_METHOD *EC_GF2m_simple_method(void) |
934 | 0 | { |
935 | 0 | static const EC_METHOD ret = { |
936 | 0 | EC_FLAGS_DEFAULT_OCT, |
937 | 0 | NID_X9_62_characteristic_two_field, |
938 | 0 | ossl_ec_GF2m_simple_group_init, |
939 | 0 | ossl_ec_GF2m_simple_group_finish, |
940 | 0 | ossl_ec_GF2m_simple_group_clear_finish, |
941 | 0 | ossl_ec_GF2m_simple_group_copy, |
942 | 0 | ossl_ec_GF2m_simple_group_set_curve, |
943 | 0 | ossl_ec_GF2m_simple_group_get_curve, |
944 | 0 | ossl_ec_GF2m_simple_group_get_degree, |
945 | 0 | ossl_ec_group_simple_order_bits, |
946 | 0 | ossl_ec_GF2m_simple_group_check_discriminant, |
947 | 0 | ossl_ec_GF2m_simple_point_init, |
948 | 0 | ossl_ec_GF2m_simple_point_finish, |
949 | 0 | ossl_ec_GF2m_simple_point_clear_finish, |
950 | 0 | ossl_ec_GF2m_simple_point_copy, |
951 | 0 | ossl_ec_GF2m_simple_point_set_to_infinity, |
952 | 0 | ossl_ec_GF2m_simple_point_set_affine_coordinates, |
953 | 0 | ossl_ec_GF2m_simple_point_get_affine_coordinates, |
954 | 0 | 0, /* point_set_compressed_coordinates */ |
955 | 0 | 0, /* point2oct */ |
956 | 0 | 0, /* oct2point */ |
957 | 0 | ossl_ec_GF2m_simple_add, |
958 | 0 | ossl_ec_GF2m_simple_dbl, |
959 | 0 | ossl_ec_GF2m_simple_invert, |
960 | 0 | ossl_ec_GF2m_simple_is_at_infinity, |
961 | 0 | ossl_ec_GF2m_simple_is_on_curve, |
962 | 0 | ossl_ec_GF2m_simple_cmp, |
963 | 0 | ossl_ec_GF2m_simple_make_affine, |
964 | 0 | ossl_ec_GF2m_simple_points_make_affine, |
965 | 0 | ec_GF2m_simple_points_mul, |
966 | 0 | 0, /* precompute_mult */ |
967 | 0 | 0, /* have_precompute_mult */ |
968 | 0 | ossl_ec_GF2m_simple_field_mul, |
969 | 0 | ossl_ec_GF2m_simple_field_sqr, |
970 | 0 | ossl_ec_GF2m_simple_field_div, |
971 | 0 | ec_GF2m_simple_field_inv, |
972 | 0 | 0, /* field_encode */ |
973 | 0 | 0, /* field_decode */ |
974 | 0 | 0, /* field_set_to_one */ |
975 | 0 | ossl_ec_key_simple_priv2oct, |
976 | 0 | ossl_ec_key_simple_oct2priv, |
977 | 0 | 0, /* set private */ |
978 | 0 | ossl_ec_key_simple_generate_key, |
979 | 0 | ossl_ec_key_simple_check_key, |
980 | 0 | ossl_ec_key_simple_generate_public_key, |
981 | 0 | 0, /* keycopy */ |
982 | 0 | 0, /* keyfinish */ |
983 | 0 | ossl_ecdh_simple_compute_key, |
984 | 0 | ossl_ecdsa_simple_sign_setup, |
985 | 0 | ossl_ecdsa_simple_sign_sig, |
986 | 0 | ossl_ecdsa_simple_verify_sig, |
987 | 0 | 0, /* field_inverse_mod_ord */ |
988 | 0 | 0, /* blind_coordinates */ |
989 | 0 | ec_GF2m_simple_ladder_pre, |
990 | 0 | ec_GF2m_simple_ladder_step, |
991 | 0 | ec_GF2m_simple_ladder_post |
992 | 0 | }; |
993 | |
|
994 | 0 | return &ret; |
995 | 0 | } |
996 | | |
997 | | #endif |