diff options
Diffstat (limited to 'src/lib/libcrypto/ec/ec2_smpl.c')
-rw-r--r-- | src/lib/libcrypto/ec/ec2_smpl.c | 723 |
1 files changed, 0 insertions, 723 deletions
diff --git a/src/lib/libcrypto/ec/ec2_smpl.c b/src/lib/libcrypto/ec/ec2_smpl.c deleted file mode 100644 index 850159cb25..0000000000 --- a/src/lib/libcrypto/ec/ec2_smpl.c +++ /dev/null | |||
@@ -1,723 +0,0 @@ | |||
1 | /* $OpenBSD: ec2_smpl.c,v 1.35 2023/04/11 18:58:20 jsing Exp $ */ | ||
2 | /* ==================================================================== | ||
3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
4 | * | ||
5 | * The Elliptic Curve Public-Key Crypto Library (ECC Code) included | ||
6 | * herein is developed by SUN MICROSYSTEMS, INC., and is contributed | ||
7 | * to the OpenSSL project. | ||
8 | * | ||
9 | * The ECC Code is licensed pursuant to the OpenSSL open source | ||
10 | * license provided below. | ||
11 | * | ||
12 | * The software is originally written by Sheueling Chang Shantz and | ||
13 | * Douglas Stebila of Sun Microsystems Laboratories. | ||
14 | * | ||
15 | */ | ||
16 | /* ==================================================================== | ||
17 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. | ||
18 | * | ||
19 | * Redistribution and use in source and binary forms, with or without | ||
20 | * modification, are permitted provided that the following conditions | ||
21 | * are met: | ||
22 | * | ||
23 | * 1. Redistributions of source code must retain the above copyright | ||
24 | * notice, this list of conditions and the following disclaimer. | ||
25 | * | ||
26 | * 2. Redistributions in binary form must reproduce the above copyright | ||
27 | * notice, this list of conditions and the following disclaimer in | ||
28 | * the documentation and/or other materials provided with the | ||
29 | * distribution. | ||
30 | * | ||
31 | * 3. All advertising materials mentioning features or use of this | ||
32 | * software must display the following acknowledgment: | ||
33 | * "This product includes software developed by the OpenSSL Project | ||
34 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
35 | * | ||
36 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
37 | * endorse or promote products derived from this software without | ||
38 | * prior written permission. For written permission, please contact | ||
39 | * openssl-core@openssl.org. | ||
40 | * | ||
41 | * 5. Products derived from this software may not be called "OpenSSL" | ||
42 | * nor may "OpenSSL" appear in their names without prior written | ||
43 | * permission of the OpenSSL Project. | ||
44 | * | ||
45 | * 6. Redistributions of any form whatsoever must retain the following | ||
46 | * acknowledgment: | ||
47 | * "This product includes software developed by the OpenSSL Project | ||
48 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
49 | * | ||
50 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
51 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
53 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
54 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
55 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
56 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
57 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
59 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
60 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
61 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
62 | * ==================================================================== | ||
63 | * | ||
64 | * This product includes cryptographic software written by Eric Young | ||
65 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
66 | * Hudson (tjh@cryptsoft.com). | ||
67 | * | ||
68 | */ | ||
69 | |||
70 | #include <openssl/opensslconf.h> | ||
71 | |||
72 | #include <openssl/err.h> | ||
73 | |||
74 | #include "ec_local.h" | ||
75 | |||
76 | #ifndef OPENSSL_NO_EC2M | ||
77 | |||
78 | /* | ||
79 | * Initialize a GF(2^m)-based EC_GROUP structure. | ||
80 | * Note that all other members are handled by EC_GROUP_new. | ||
81 | */ | ||
82 | static int | ||
83 | ec_GF2m_simple_group_init(EC_GROUP *group) | ||
84 | { | ||
85 | BN_init(&group->field); | ||
86 | BN_init(&group->a); | ||
87 | BN_init(&group->b); | ||
88 | return 1; | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * Clear and free a GF(2^m)-based EC_GROUP structure. | ||
93 | * Note that all other members are handled by EC_GROUP_free. | ||
94 | */ | ||
95 | static void | ||
96 | ec_GF2m_simple_group_finish(EC_GROUP *group) | ||
97 | { | ||
98 | BN_free(&group->field); | ||
99 | BN_free(&group->a); | ||
100 | BN_free(&group->b); | ||
101 | group->poly[0] = 0; | ||
102 | group->poly[1] = 0; | ||
103 | group->poly[2] = 0; | ||
104 | group->poly[3] = 0; | ||
105 | group->poly[4] = 0; | ||
106 | group->poly[5] = -1; | ||
107 | } | ||
108 | |||
109 | /* | ||
110 | * Copy a GF(2^m)-based EC_GROUP structure. | ||
111 | * Note that all other members are handled by EC_GROUP_copy. | ||
112 | */ | ||
113 | static int | ||
114 | ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) | ||
115 | { | ||
116 | int i; | ||
117 | |||
118 | if (!bn_copy(&dest->field, &src->field)) | ||
119 | return 0; | ||
120 | if (!bn_copy(&dest->a, &src->a)) | ||
121 | return 0; | ||
122 | if (!bn_copy(&dest->b, &src->b)) | ||
123 | return 0; | ||
124 | dest->poly[0] = src->poly[0]; | ||
125 | dest->poly[1] = src->poly[1]; | ||
126 | dest->poly[2] = src->poly[2]; | ||
127 | dest->poly[3] = src->poly[3]; | ||
128 | dest->poly[4] = src->poly[4]; | ||
129 | dest->poly[5] = src->poly[5]; | ||
130 | if (!bn_expand(&dest->a, dest->poly[0])) | ||
131 | return 0; | ||
132 | if (!bn_expand(&dest->b, dest->poly[0])) | ||
133 | return 0; | ||
134 | for (i = dest->a.top; i < dest->a.dmax; i++) | ||
135 | dest->a.d[i] = 0; | ||
136 | for (i = dest->b.top; i < dest->b.dmax; i++) | ||
137 | dest->b.d[i] = 0; | ||
138 | return 1; | ||
139 | } | ||
140 | |||
141 | /* Set the curve parameters of an EC_GROUP structure. */ | ||
142 | static int | ||
143 | ec_GF2m_simple_group_set_curve(EC_GROUP *group, | ||
144 | const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | ||
145 | { | ||
146 | int ret = 0, i; | ||
147 | |||
148 | /* group->field */ | ||
149 | if (!bn_copy(&group->field, p)) | ||
150 | goto err; | ||
151 | i = BN_GF2m_poly2arr(&group->field, group->poly, 6) - 1; | ||
152 | if ((i != 5) && (i != 3)) { | ||
153 | ECerror(EC_R_UNSUPPORTED_FIELD); | ||
154 | goto err; | ||
155 | } | ||
156 | /* group->a */ | ||
157 | if (!BN_GF2m_mod_arr(&group->a, a, group->poly)) | ||
158 | goto err; | ||
159 | if (!bn_expand(&group->a, group->poly[0])) | ||
160 | goto err; | ||
161 | for (i = group->a.top; i < group->a.dmax; i++) | ||
162 | group->a.d[i] = 0; | ||
163 | |||
164 | /* group->b */ | ||
165 | if (!BN_GF2m_mod_arr(&group->b, b, group->poly)) | ||
166 | goto err; | ||
167 | if (!bn_expand(&group->b, group->poly[0])) | ||
168 | goto err; | ||
169 | for (i = group->b.top; i < group->b.dmax; i++) | ||
170 | group->b.d[i] = 0; | ||
171 | |||
172 | ret = 1; | ||
173 | err: | ||
174 | return ret; | ||
175 | } | ||
176 | |||
177 | /* | ||
178 | * Get the curve parameters of an EC_GROUP structure. | ||
179 | * If p, a, or b are NULL then there values will not be set but the method will return with success. | ||
180 | */ | ||
181 | static int | ||
182 | ec_GF2m_simple_group_get_curve(const EC_GROUP *group, | ||
183 | BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) | ||
184 | { | ||
185 | int ret = 0; | ||
186 | |||
187 | if (p != NULL) { | ||
188 | if (!bn_copy(p, &group->field)) | ||
189 | return 0; | ||
190 | } | ||
191 | if (a != NULL) { | ||
192 | if (!bn_copy(a, &group->a)) | ||
193 | goto err; | ||
194 | } | ||
195 | if (b != NULL) { | ||
196 | if (!bn_copy(b, &group->b)) | ||
197 | goto err; | ||
198 | } | ||
199 | ret = 1; | ||
200 | |||
201 | err: | ||
202 | return ret; | ||
203 | } | ||
204 | |||
205 | /* Gets the degree of the field. For a curve over GF(2^m) this is the value m. */ | ||
206 | static int | ||
207 | ec_GF2m_simple_group_get_degree(const EC_GROUP *group) | ||
208 | { | ||
209 | return BN_num_bits(&group->field) - 1; | ||
210 | } | ||
211 | |||
212 | /* | ||
213 | * Checks the discriminant of the curve. | ||
214 | * y^2 + x*y = x^3 + a*x^2 + b is an elliptic curve <=> b != 0 (mod p) | ||
215 | */ | ||
216 | static int | ||
217 | ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) | ||
218 | { | ||
219 | BIGNUM *b; | ||
220 | int ret = 0; | ||
221 | |||
222 | BN_CTX_start(ctx); | ||
223 | |||
224 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
225 | goto err; | ||
226 | |||
227 | if (!BN_GF2m_mod_arr(b, &group->b, group->poly)) | ||
228 | goto err; | ||
229 | |||
230 | /* | ||
231 | * check the discriminant: y^2 + x*y = x^3 + a*x^2 + b is an elliptic | ||
232 | * curve <=> b != 0 (mod p) | ||
233 | */ | ||
234 | if (BN_is_zero(b)) | ||
235 | goto err; | ||
236 | |||
237 | ret = 1; | ||
238 | |||
239 | err: | ||
240 | BN_CTX_end(ctx); | ||
241 | |||
242 | return ret; | ||
243 | } | ||
244 | |||
245 | /* Initializes an EC_POINT. */ | ||
246 | static int | ||
247 | ec_GF2m_simple_point_init(EC_POINT *point) | ||
248 | { | ||
249 | BN_init(&point->X); | ||
250 | BN_init(&point->Y); | ||
251 | BN_init(&point->Z); | ||
252 | return 1; | ||
253 | } | ||
254 | |||
255 | /* Clears and frees an EC_POINT. */ | ||
256 | static void | ||
257 | ec_GF2m_simple_point_finish(EC_POINT *point) | ||
258 | { | ||
259 | BN_free(&point->X); | ||
260 | BN_free(&point->Y); | ||
261 | BN_free(&point->Z); | ||
262 | point->Z_is_one = 0; | ||
263 | } | ||
264 | |||
265 | /* Copy the contents of one EC_POINT into another. Assumes dest is initialized. */ | ||
266 | static int | ||
267 | ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src) | ||
268 | { | ||
269 | if (!bn_copy(&dest->X, &src->X)) | ||
270 | return 0; | ||
271 | if (!bn_copy(&dest->Y, &src->Y)) | ||
272 | return 0; | ||
273 | if (!bn_copy(&dest->Z, &src->Z)) | ||
274 | return 0; | ||
275 | dest->Z_is_one = src->Z_is_one; | ||
276 | |||
277 | return 1; | ||
278 | } | ||
279 | |||
280 | /* | ||
281 | * Set an EC_POINT to the point at infinity. | ||
282 | * A point at infinity is represented by having Z=0. | ||
283 | */ | ||
284 | static int | ||
285 | ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point) | ||
286 | { | ||
287 | point->Z_is_one = 0; | ||
288 | BN_zero(&point->Z); | ||
289 | return 1; | ||
290 | } | ||
291 | |||
292 | /* | ||
293 | * Set the coordinates of an EC_POINT using affine coordinates. | ||
294 | * Note that the simple implementation only uses affine coordinates. | ||
295 | */ | ||
296 | static int | ||
297 | ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point, | ||
298 | const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) | ||
299 | { | ||
300 | int ret = 0; | ||
301 | if (x == NULL || y == NULL) { | ||
302 | ECerror(ERR_R_PASSED_NULL_PARAMETER); | ||
303 | return 0; | ||
304 | } | ||
305 | if (!bn_copy(&point->X, x)) | ||
306 | goto err; | ||
307 | BN_set_negative(&point->X, 0); | ||
308 | if (!bn_copy(&point->Y, y)) | ||
309 | goto err; | ||
310 | BN_set_negative(&point->Y, 0); | ||
311 | if (!bn_copy(&point->Z, BN_value_one())) | ||
312 | goto err; | ||
313 | BN_set_negative(&point->Z, 0); | ||
314 | point->Z_is_one = 1; | ||
315 | ret = 1; | ||
316 | |||
317 | err: | ||
318 | return ret; | ||
319 | } | ||
320 | |||
321 | /* | ||
322 | * Gets the affine coordinates of an EC_POINT. | ||
323 | * Note that the simple implementation only uses affine coordinates. | ||
324 | */ | ||
325 | static int | ||
326 | ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group, | ||
327 | const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx) | ||
328 | { | ||
329 | int ret = 0; | ||
330 | |||
331 | if (EC_POINT_is_at_infinity(group, point) > 0) { | ||
332 | ECerror(EC_R_POINT_AT_INFINITY); | ||
333 | return 0; | ||
334 | } | ||
335 | if (BN_cmp(&point->Z, BN_value_one())) { | ||
336 | ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | ||
337 | return 0; | ||
338 | } | ||
339 | if (x != NULL) { | ||
340 | if (!bn_copy(x, &point->X)) | ||
341 | goto err; | ||
342 | BN_set_negative(x, 0); | ||
343 | } | ||
344 | if (y != NULL) { | ||
345 | if (!bn_copy(y, &point->Y)) | ||
346 | goto err; | ||
347 | BN_set_negative(y, 0); | ||
348 | } | ||
349 | ret = 1; | ||
350 | |||
351 | err: | ||
352 | return ret; | ||
353 | } | ||
354 | |||
355 | /* | ||
356 | * Computes a + b and stores the result in r. r could be a or b, a could be b. | ||
357 | * Uses algorithm A.10.2 of IEEE P1363. | ||
358 | */ | ||
359 | static int | ||
360 | ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | ||
361 | const EC_POINT *b, BN_CTX *ctx) | ||
362 | { | ||
363 | BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t; | ||
364 | int ret = 0; | ||
365 | |||
366 | if (EC_POINT_is_at_infinity(group, a) > 0) { | ||
367 | if (!EC_POINT_copy(r, b)) | ||
368 | return 0; | ||
369 | return 1; | ||
370 | } | ||
371 | if (EC_POINT_is_at_infinity(group, b) > 0) { | ||
372 | if (!EC_POINT_copy(r, a)) | ||
373 | return 0; | ||
374 | return 1; | ||
375 | } | ||
376 | |||
377 | BN_CTX_start(ctx); | ||
378 | |||
379 | if ((x0 = BN_CTX_get(ctx)) == NULL) | ||
380 | goto err; | ||
381 | if ((y0 = BN_CTX_get(ctx)) == NULL) | ||
382 | goto err; | ||
383 | if ((x1 = BN_CTX_get(ctx)) == NULL) | ||
384 | goto err; | ||
385 | if ((y1 = BN_CTX_get(ctx)) == NULL) | ||
386 | goto err; | ||
387 | if ((x2 = BN_CTX_get(ctx)) == NULL) | ||
388 | goto err; | ||
389 | if ((y2 = BN_CTX_get(ctx)) == NULL) | ||
390 | goto err; | ||
391 | if ((s = BN_CTX_get(ctx)) == NULL) | ||
392 | goto err; | ||
393 | if ((t = BN_CTX_get(ctx)) == NULL) | ||
394 | goto err; | ||
395 | |||
396 | if (a->Z_is_one) { | ||
397 | if (!bn_copy(x0, &a->X)) | ||
398 | goto err; | ||
399 | if (!bn_copy(y0, &a->Y)) | ||
400 | goto err; | ||
401 | } else { | ||
402 | if (!EC_POINT_get_affine_coordinates(group, a, x0, y0, ctx)) | ||
403 | goto err; | ||
404 | } | ||
405 | if (b->Z_is_one) { | ||
406 | if (!bn_copy(x1, &b->X)) | ||
407 | goto err; | ||
408 | if (!bn_copy(y1, &b->Y)) | ||
409 | goto err; | ||
410 | } else { | ||
411 | if (!EC_POINT_get_affine_coordinates(group, b, x1, y1, ctx)) | ||
412 | goto err; | ||
413 | } | ||
414 | |||
415 | if (BN_GF2m_cmp(x0, x1)) { | ||
416 | if (!BN_GF2m_add(t, x0, x1)) | ||
417 | goto err; | ||
418 | if (!BN_GF2m_add(s, y0, y1)) | ||
419 | goto err; | ||
420 | if (!group->meth->field_div(group, s, s, t, ctx)) | ||
421 | goto err; | ||
422 | if (!group->meth->field_sqr(group, x2, s, ctx)) | ||
423 | goto err; | ||
424 | if (!BN_GF2m_add(x2, x2, &group->a)) | ||
425 | goto err; | ||
426 | if (!BN_GF2m_add(x2, x2, s)) | ||
427 | goto err; | ||
428 | if (!BN_GF2m_add(x2, x2, t)) | ||
429 | goto err; | ||
430 | } else { | ||
431 | if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) { | ||
432 | if (!EC_POINT_set_to_infinity(group, r)) | ||
433 | goto err; | ||
434 | ret = 1; | ||
435 | goto err; | ||
436 | } | ||
437 | if (!group->meth->field_div(group, s, y1, x1, ctx)) | ||
438 | goto err; | ||
439 | if (!BN_GF2m_add(s, s, x1)) | ||
440 | goto err; | ||
441 | |||
442 | if (!group->meth->field_sqr(group, x2, s, ctx)) | ||
443 | goto err; | ||
444 | if (!BN_GF2m_add(x2, x2, s)) | ||
445 | goto err; | ||
446 | if (!BN_GF2m_add(x2, x2, &group->a)) | ||
447 | goto err; | ||
448 | } | ||
449 | |||
450 | if (!BN_GF2m_add(y2, x1, x2)) | ||
451 | goto err; | ||
452 | if (!group->meth->field_mul(group, y2, y2, s, ctx)) | ||
453 | goto err; | ||
454 | if (!BN_GF2m_add(y2, y2, x2)) | ||
455 | goto err; | ||
456 | if (!BN_GF2m_add(y2, y2, y1)) | ||
457 | goto err; | ||
458 | |||
459 | if (!EC_POINT_set_affine_coordinates(group, r, x2, y2, ctx)) | ||
460 | goto err; | ||
461 | |||
462 | ret = 1; | ||
463 | |||
464 | err: | ||
465 | BN_CTX_end(ctx); | ||
466 | |||
467 | return ret; | ||
468 | } | ||
469 | |||
470 | /* | ||
471 | * Computes 2 * a and stores the result in r. r could be a. | ||
472 | * Uses algorithm A.10.2 of IEEE P1363. | ||
473 | */ | ||
474 | static int | ||
475 | ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | ||
476 | BN_CTX *ctx) | ||
477 | { | ||
478 | return ec_GF2m_simple_add(group, r, a, a, ctx); | ||
479 | } | ||
480 | |||
481 | static int | ||
482 | ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) | ||
483 | { | ||
484 | if (EC_POINT_is_at_infinity(group, point) > 0 || BN_is_zero(&point->Y)) | ||
485 | /* point is its own inverse */ | ||
486 | return 1; | ||
487 | |||
488 | if (!EC_POINT_make_affine(group, point, ctx)) | ||
489 | return 0; | ||
490 | return BN_GF2m_add(&point->Y, &point->X, &point->Y); | ||
491 | } | ||
492 | |||
493 | /* Indicates whether the given point is the point at infinity. */ | ||
494 | static int | ||
495 | ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) | ||
496 | { | ||
497 | return BN_is_zero(&point->Z); | ||
498 | } | ||
499 | |||
500 | /* | ||
501 | * Determines whether the given EC_POINT is an actual point on the curve defined | ||
502 | * in the EC_GROUP. A point is valid if it satisfies the Weierstrass equation: | ||
503 | * y^2 + x*y = x^3 + a*x^2 + b. | ||
504 | */ | ||
505 | static int | ||
506 | ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx) | ||
507 | { | ||
508 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); | ||
509 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | ||
510 | BIGNUM *lh, *y2; | ||
511 | int ret = -1; | ||
512 | |||
513 | if (EC_POINT_is_at_infinity(group, point) > 0) | ||
514 | return 1; | ||
515 | |||
516 | field_mul = group->meth->field_mul; | ||
517 | field_sqr = group->meth->field_sqr; | ||
518 | |||
519 | /* only support affine coordinates */ | ||
520 | if (!point->Z_is_one) | ||
521 | return -1; | ||
522 | |||
523 | BN_CTX_start(ctx); | ||
524 | |||
525 | if ((y2 = BN_CTX_get(ctx)) == NULL) | ||
526 | goto err; | ||
527 | if ((lh = BN_CTX_get(ctx)) == NULL) | ||
528 | goto err; | ||
529 | |||
530 | /* | ||
531 | * We have a curve defined by a Weierstrass equation y^2 + x*y = x^3 | ||
532 | * + a*x^2 + b. <=> x^3 + a*x^2 + x*y + b + y^2 = 0 <=> ((x + a) * x | ||
533 | * + y ) * x + b + y^2 = 0 | ||
534 | */ | ||
535 | if (!BN_GF2m_add(lh, &point->X, &group->a)) | ||
536 | goto err; | ||
537 | if (!field_mul(group, lh, lh, &point->X, ctx)) | ||
538 | goto err; | ||
539 | if (!BN_GF2m_add(lh, lh, &point->Y)) | ||
540 | goto err; | ||
541 | if (!field_mul(group, lh, lh, &point->X, ctx)) | ||
542 | goto err; | ||
543 | if (!BN_GF2m_add(lh, lh, &group->b)) | ||
544 | goto err; | ||
545 | if (!field_sqr(group, y2, &point->Y, ctx)) | ||
546 | goto err; | ||
547 | if (!BN_GF2m_add(lh, lh, y2)) | ||
548 | goto err; | ||
549 | |||
550 | ret = BN_is_zero(lh); | ||
551 | |||
552 | err: | ||
553 | BN_CTX_end(ctx); | ||
554 | |||
555 | return ret; | ||
556 | } | ||
557 | |||
558 | /* | ||
559 | * Indicates whether two points are equal. | ||
560 | * Return values: | ||
561 | * -1 error | ||
562 | * 0 equal (in affine coordinates) | ||
563 | * 1 not equal | ||
564 | */ | ||
565 | static int | ||
566 | ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a, | ||
567 | const EC_POINT *b, BN_CTX *ctx) | ||
568 | { | ||
569 | BIGNUM *aX, *aY, *bX, *bY; | ||
570 | int ret = -1; | ||
571 | |||
572 | if (EC_POINT_is_at_infinity(group, a) > 0) | ||
573 | return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1; | ||
574 | |||
575 | if (EC_POINT_is_at_infinity(group, b) > 0) | ||
576 | return 1; | ||
577 | |||
578 | if (a->Z_is_one && b->Z_is_one) | ||
579 | return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1; | ||
580 | |||
581 | BN_CTX_start(ctx); | ||
582 | |||
583 | if ((aX = BN_CTX_get(ctx)) == NULL) | ||
584 | goto err; | ||
585 | if ((aY = BN_CTX_get(ctx)) == NULL) | ||
586 | goto err; | ||
587 | if ((bX = BN_CTX_get(ctx)) == NULL) | ||
588 | goto err; | ||
589 | if ((bY = BN_CTX_get(ctx)) == NULL) | ||
590 | goto err; | ||
591 | |||
592 | if (!EC_POINT_get_affine_coordinates(group, a, aX, aY, ctx)) | ||
593 | goto err; | ||
594 | if (!EC_POINT_get_affine_coordinates(group, b, bX, bY, ctx)) | ||
595 | goto err; | ||
596 | ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1; | ||
597 | |||
598 | err: | ||
599 | BN_CTX_end(ctx); | ||
600 | |||
601 | return ret; | ||
602 | } | ||
603 | |||
604 | /* Forces the given EC_POINT to internally use affine coordinates. */ | ||
605 | static int | ||
606 | ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) | ||
607 | { | ||
608 | BIGNUM *x, *y; | ||
609 | int ret = 0; | ||
610 | |||
611 | if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0) | ||
612 | return 1; | ||
613 | |||
614 | BN_CTX_start(ctx); | ||
615 | |||
616 | if ((x = BN_CTX_get(ctx)) == NULL) | ||
617 | goto err; | ||
618 | if ((y = BN_CTX_get(ctx)) == NULL) | ||
619 | goto err; | ||
620 | |||
621 | if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) | ||
622 | goto err; | ||
623 | if (!bn_copy(&point->X, x)) | ||
624 | goto err; | ||
625 | if (!bn_copy(&point->Y, y)) | ||
626 | goto err; | ||
627 | if (!BN_one(&point->Z)) | ||
628 | goto err; | ||
629 | |||
630 | ret = 1; | ||
631 | |||
632 | err: | ||
633 | BN_CTX_end(ctx); | ||
634 | |||
635 | return ret; | ||
636 | } | ||
637 | |||
638 | /* Forces each of the EC_POINTs in the given array to use affine coordinates. */ | ||
639 | static int | ||
640 | ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num, | ||
641 | EC_POINT *points[], BN_CTX *ctx) | ||
642 | { | ||
643 | size_t i; | ||
644 | |||
645 | for (i = 0; i < num; i++) { | ||
646 | if (!group->meth->make_affine(group, points[i], ctx)) | ||
647 | return 0; | ||
648 | } | ||
649 | |||
650 | return 1; | ||
651 | } | ||
652 | |||
653 | /* Wrapper to simple binary polynomial field multiplication implementation. */ | ||
654 | static int | ||
655 | ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | ||
656 | const BIGNUM *b, BN_CTX *ctx) | ||
657 | { | ||
658 | return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx); | ||
659 | } | ||
660 | |||
661 | /* Wrapper to simple binary polynomial field squaring implementation. */ | ||
662 | static int | ||
663 | ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | ||
664 | BN_CTX *ctx) | ||
665 | { | ||
666 | return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx); | ||
667 | } | ||
668 | |||
669 | /* Wrapper to simple binary polynomial field division implementation. */ | ||
670 | static int | ||
671 | ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | ||
672 | const BIGNUM *b, BN_CTX *ctx) | ||
673 | { | ||
674 | return BN_GF2m_mod_div(r, a, b, &group->field, ctx); | ||
675 | } | ||
676 | |||
677 | static const EC_METHOD ec_GF2m_simple_method = { | ||
678 | .field_type = NID_X9_62_characteristic_two_field, | ||
679 | .group_init = ec_GF2m_simple_group_init, | ||
680 | .group_finish = ec_GF2m_simple_group_finish, | ||
681 | .group_copy = ec_GF2m_simple_group_copy, | ||
682 | .group_set_curve = ec_GF2m_simple_group_set_curve, | ||
683 | .group_get_curve = ec_GF2m_simple_group_get_curve, | ||
684 | .group_get_degree = ec_GF2m_simple_group_get_degree, | ||
685 | .group_order_bits = ec_group_simple_order_bits, | ||
686 | .group_check_discriminant = ec_GF2m_simple_group_check_discriminant, | ||
687 | .point_init = ec_GF2m_simple_point_init, | ||
688 | .point_finish = ec_GF2m_simple_point_finish, | ||
689 | .point_copy = ec_GF2m_simple_point_copy, | ||
690 | .point_set_to_infinity = ec_GF2m_simple_point_set_to_infinity, | ||
691 | .point_set_affine_coordinates = | ||
692 | ec_GF2m_simple_point_set_affine_coordinates, | ||
693 | .point_get_affine_coordinates = | ||
694 | ec_GF2m_simple_point_get_affine_coordinates, | ||
695 | .point_set_compressed_coordinates = | ||
696 | ec_GF2m_simple_set_compressed_coordinates, | ||
697 | .point2oct = ec_GF2m_simple_point2oct, | ||
698 | .oct2point = ec_GF2m_simple_oct2point, | ||
699 | .add = ec_GF2m_simple_add, | ||
700 | .dbl = ec_GF2m_simple_dbl, | ||
701 | .invert = ec_GF2m_simple_invert, | ||
702 | .is_at_infinity = ec_GF2m_simple_is_at_infinity, | ||
703 | .is_on_curve = ec_GF2m_simple_is_on_curve, | ||
704 | .point_cmp = ec_GF2m_simple_cmp, | ||
705 | .make_affine = ec_GF2m_simple_make_affine, | ||
706 | .points_make_affine = ec_GF2m_simple_points_make_affine, | ||
707 | .mul_generator_ct = ec_GFp_simple_mul_generator_ct, | ||
708 | .mul_single_ct = ec_GFp_simple_mul_single_ct, | ||
709 | .mul_double_nonct = ec_GFp_simple_mul_double_nonct, | ||
710 | .precompute_mult = ec_GF2m_precompute_mult, | ||
711 | .have_precompute_mult = ec_GF2m_have_precompute_mult, | ||
712 | .field_mul = ec_GF2m_simple_field_mul, | ||
713 | .field_sqr = ec_GF2m_simple_field_sqr, | ||
714 | .field_div = ec_GF2m_simple_field_div, | ||
715 | .blind_coordinates = NULL, | ||
716 | }; | ||
717 | |||
718 | const EC_METHOD * | ||
719 | EC_GF2m_simple_method(void) | ||
720 | { | ||
721 | return &ec_GF2m_simple_method; | ||
722 | } | ||
723 | #endif | ||