Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
532k
{
30
532k
    group->field = BN_new();
31
532k
    group->a = BN_new();
32
532k
    group->b = BN_new();
33
34
532k
    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
532k
    return 1;
41
532k
}
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
532k
{
49
532k
    BN_free(group->field);
50
532k
    BN_free(group->a);
51
532k
    BN_free(group->b);
52
532k
}
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
263k
{
77
263k
    if (!BN_copy(dest->field, src->field))
78
0
        return 0;
79
263k
    if (!BN_copy(dest->a, src->a))
80
0
        return 0;
81
263k
    if (!BN_copy(dest->b, src->b))
82
0
        return 0;
83
263k
    dest->poly[0] = src->poly[0];
84
263k
    dest->poly[1] = src->poly[1];
85
263k
    dest->poly[2] = src->poly[2];
86
263k
    dest->poly[3] = src->poly[3];
87
263k
    dest->poly[4] = src->poly[4];
88
263k
    dest->poly[5] = src->poly[5];
89
263k
    if (bn_wexpand(dest->a, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
90
0
        return 0;
91
263k
    if (bn_wexpand(dest->b, (int)(dest->poly[0] + BN_BITS2 - 1) / BN_BITS2) == NULL)
92
0
        return 0;
93
263k
    bn_set_all_zero(dest->a);
94
263k
    bn_set_all_zero(dest->b);
95
263k
    return 1;
96
263k
}
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
269k
{
103
269k
    int ret = 0, i;
104
105
    /* group->field */
106
269k
    if (!BN_copy(group->field, p))
107
0
        goto err;
108
269k
    i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
109
269k
    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
269k
    if (!BN_GF2m_mod_arr(group->a, a, group->poly))
116
0
        goto err;
117
269k
    if (bn_wexpand(group->a, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
118
269k
        == NULL)
119
0
        goto err;
120
269k
    bn_set_all_zero(group->a);
121
122
    /* group->b */
123
269k
    if (!BN_GF2m_mod_arr(group->b, b, group->poly))
124
0
        goto err;
125
269k
    if (bn_wexpand(group->b, (int)(group->poly[0] + BN_BITS2 - 1) / BN_BITS2)
126
269k
        == NULL)
127
0
        goto err;
128
269k
    bn_set_all_zero(group->b);
129
130
269k
    ret = 1;
131
269k
err:
132
269k
    return ret;
133
269k
}
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
3.10k
{
142
3.10k
    int ret = 0;
143
144
3.10k
    if (p != NULL) {
145
3.10k
        if (!BN_copy(p, group->field))
146
0
            return 0;
147
3.10k
    }
148
149
3.10k
    if (a != NULL) {
150
3.10k
        if (!BN_copy(a, group->a))
151
0
            goto err;
152
3.10k
    }
153
154
3.10k
    if (b != NULL) {
155
3.10k
        if (!BN_copy(b, group->b))
156
0
            goto err;
157
3.10k
    }
158
159
3.10k
    ret = 1;
160
161
3.10k
err:
162
3.10k
    return ret;
163
3.10k
}
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
355k
{
171
355k
    return BN_num_bits(group->field) - 1;
172
355k
}
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
1.55k
{
181
1.55k
    int ret = 0;
182
1.55k
    BIGNUM *b;
183
1.55k
#ifndef FIPS_MODULE
184
1.55k
    BN_CTX *new_ctx = NULL;
185
186
1.55k
    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
1.55k
#endif
194
1.55k
    BN_CTX_start(ctx);
195
1.55k
    b = BN_CTX_get(ctx);
196
1.55k
    if (b == NULL)
197
0
        goto err;
198
199
1.55k
    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
1.55k
    if (BN_is_zero(b))
207
0
        goto err;
208
209
1.55k
    ret = 1;
210
211
1.55k
err:
212
1.55k
    BN_CTX_end(ctx);
213
1.55k
#ifndef FIPS_MODULE
214
1.55k
    BN_CTX_free(new_ctx);
215
1.55k
#endif
216
1.55k
    return ret;
217
1.55k
}
218
219
/* Initializes an EC_POINT. */
220
int ossl_ec_GF2m_simple_point_init(EC_POINT *point)
221
1.09M
{
222
1.09M
    point->X = BN_new();
223
1.09M
    point->Y = BN_new();
224
1.09M
    point->Z = BN_new();
225
226
1.09M
    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
1.09M
    return 1;
233
1.09M
}
234
235
/* Frees an EC_POINT. */
236
void ossl_ec_GF2m_simple_point_finish(EC_POINT *point)
237
1.08M
{
238
1.08M
    BN_free(point->X);
239
1.08M
    BN_free(point->Y);
240
1.08M
    BN_free(point->Z);
241
1.08M
}
242
243
/* Clears and frees an EC_POINT. */
244
void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *point)
245
10.9k
{
246
10.9k
    BN_clear_free(point->X);
247
10.9k
    BN_clear_free(point->Y);
248
10.9k
    BN_clear_free(point->Z);
249
10.9k
    point->Z_is_one = 0;
250
10.9k
}
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
545k
{
258
545k
    if (!BN_copy(dest->X, src->X))
259
0
        return 0;
260
545k
    if (!BN_copy(dest->Y, src->Y))
261
0
        return 0;
262
545k
    if (!BN_copy(dest->Z, src->Z))
263
0
        return 0;
264
545k
    dest->Z_is_one = src->Z_is_one;
265
545k
    dest->curve_name = src->curve_name;
266
267
545k
    return 1;
268
545k
}
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
33.6k
{
277
33.6k
    point->Z_is_one = 0;
278
33.6k
    BN_zero(point->Z);
279
33.6k
    return 1;
280
33.6k
}
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
381k
{
292
381k
    int ret = 0;
293
381k
    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
381k
    if (!BN_copy(point->X, x))
299
0
        goto err;
300
381k
    BN_set_negative(point->X, 0);
301
381k
    if (!BN_copy(point->Y, y))
302
0
        goto err;
303
381k
    BN_set_negative(point->Y, 0);
304
381k
    if (!BN_copy(point->Z, BN_value_one()))
305
0
        goto err;
306
381k
    BN_set_negative(point->Z, 0);
307
381k
    point->Z_is_one = 1;
308
381k
    ret = 1;
309
310
381k
err:
311
381k
    return ret;
312
381k
}
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
7.25k
{
323
7.25k
    int ret = 0;
324
325
7.25k
    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
7.25k
    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
7.25k
    if (x != NULL) {
335
7.25k
        if (!BN_copy(x, point->X))
336
0
            goto err;
337
7.25k
        BN_set_negative(x, 0);
338
7.25k
    }
339
7.25k
    if (y != NULL) {
340
7.02k
        if (!BN_copy(y, point->Y))
341
0
            goto err;
342
7.02k
        BN_set_negative(y, 0);
343
7.02k
    }
344
7.25k
    ret = 1;
345
346
7.25k
err:
347
7.25k
    return ret;
348
7.25k
}
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
228
{
357
228
    BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
358
228
    int ret = 0;
359
228
#ifndef FIPS_MODULE
360
228
    BN_CTX *new_ctx = NULL;
361
228
#endif
362
363
228
    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
228
    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
228
#ifndef FIPS_MODULE
376
228
    if (ctx == NULL) {
377
0
        ctx = new_ctx = BN_CTX_new();
378
0
        if (ctx == NULL)
379
0
            return 0;
380
0
    }
381
228
#endif
382
383
228
    BN_CTX_start(ctx);
384
228
    x0 = BN_CTX_get(ctx);
385
228
    y0 = BN_CTX_get(ctx);
386
228
    x1 = BN_CTX_get(ctx);
387
228
    y1 = BN_CTX_get(ctx);
388
228
    x2 = BN_CTX_get(ctx);
389
228
    y2 = BN_CTX_get(ctx);
390
228
    s = BN_CTX_get(ctx);
391
228
    t = BN_CTX_get(ctx);
392
228
    if (t == NULL)
393
0
        goto err;
394
395
228
    if (a->Z_is_one) {
396
228
        if (!BN_copy(x0, a->X))
397
0
            goto err;
398
228
        if (!BN_copy(y0, a->Y))
399
0
            goto err;
400
228
    } else {
401
0
        if (!EC_POINT_get_affine_coordinates(group, a, x0, y0, ctx))
402
0
            goto err;
403
0
    }
404
228
    if (b->Z_is_one) {
405
228
        if (!BN_copy(x1, b->X))
406
0
            goto err;
407
228
        if (!BN_copy(y1, b->Y))
408
0
            goto err;
409
228
    } else {
410
0
        if (!EC_POINT_get_affine_coordinates(group, b, x1, y1, ctx))
411
0
            goto err;
412
0
    }
413
414
228
    if (BN_GF2m_cmp(x0, x1)) {
415
228
        if (!BN_GF2m_add(t, x0, x1))
416
0
            goto err;
417
228
        if (!BN_GF2m_add(s, y0, y1))
418
0
            goto err;
419
228
        if (!group->meth->field_div(group, s, s, t, ctx))
420
0
            goto err;
421
228
        if (!group->meth->field_sqr(group, x2, s, ctx))
422
0
            goto err;
423
228
        if (!BN_GF2m_add(x2, x2, group->a))
424
0
            goto err;
425
228
        if (!BN_GF2m_add(x2, x2, s))
426
0
            goto err;
427
228
        if (!BN_GF2m_add(x2, x2, t))
428
0
            goto err;
429
228
    } 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
228
    if (!BN_GF2m_add(y2, x1, x2))
450
0
        goto err;
451
228
    if (!group->meth->field_mul(group, y2, y2, s, ctx))
452
0
        goto err;
453
228
    if (!BN_GF2m_add(y2, y2, x2))
454
0
        goto err;
455
228
    if (!BN_GF2m_add(y2, y2, y1))
456
0
        goto err;
457
458
228
    if (!EC_POINT_set_affine_coordinates(group, r, x2, y2, ctx))
459
0
        goto err;
460
461
228
    ret = 1;
462
463
228
err:
464
228
    BN_CTX_end(ctx);
465
228
#ifndef FIPS_MODULE
466
228
    BN_CTX_free(new_ctx);
467
228
#endif
468
228
    return ret;
469
228
}
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
70
{
484
70
    if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
485
        /* point is its own inverse */
486
10
        return 1;
487
488
60
    if (group->meth->make_affine == NULL
489
60
        || !group->meth->make_affine(group, point, ctx))
490
0
        return 0;
491
60
    return BN_GF2m_add(point->Y, point->X, point->Y);
492
60
}
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
427k
{
498
427k
    return BN_is_zero(point->Z);
499
427k
}
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
385k
{
509
385k
    int ret = -1;
510
385k
    BIGNUM *lh, *y2;
511
385k
    int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *,
512
385k
        const BIGNUM *, BN_CTX *);
513
385k
    int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
514
385k
#ifndef FIPS_MODULE
515
385k
    BN_CTX *new_ctx = NULL;
516
385k
#endif
517
518
385k
    if (EC_POINT_is_at_infinity(group, point))
519
0
        return 1;
520
521
385k
    field_mul = group->meth->field_mul;
522
385k
    field_sqr = group->meth->field_sqr;
523
524
    /* only support affine coordinates */
525
385k
    if (!point->Z_is_one)
526
0
        return -1;
527
528
385k
#ifndef FIPS_MODULE
529
385k
    if (ctx == NULL) {
530
0
        ctx = new_ctx = BN_CTX_new();
531
0
        if (ctx == NULL)
532
0
            return -1;
533
0
    }
534
385k
#endif
535
536
385k
    BN_CTX_start(ctx);
537
385k
    y2 = BN_CTX_get(ctx);
538
385k
    lh = BN_CTX_get(ctx);
539
385k
    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
385k
    if (!BN_GF2m_add(lh, point->X, group->a))
549
0
        goto err;
550
385k
    if (!field_mul(group, lh, lh, point->X, ctx))
551
0
        goto err;
552
385k
    if (!BN_GF2m_add(lh, lh, point->Y))
553
0
        goto err;
554
385k
    if (!field_mul(group, lh, lh, point->X, ctx))
555
0
        goto err;
556
385k
    if (!BN_GF2m_add(lh, lh, group->b))
557
0
        goto err;
558
385k
    if (!field_sqr(group, y2, point->Y, ctx))
559
0
        goto err;
560
385k
    if (!BN_GF2m_add(lh, lh, y2))
561
0
        goto err;
562
385k
    ret = BN_is_zero(lh);
563
564
385k
err:
565
385k
    BN_CTX_end(ctx);
566
385k
#ifndef FIPS_MODULE
567
385k
    BN_CTX_free(new_ctx);
568
385k
#endif
569
385k
    return ret;
570
385k
}
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
3.57k
{
582
3.57k
    BIGNUM *aX, *aY, *bX, *bY;
583
3.57k
    int ret = -1;
584
3.57k
#ifndef FIPS_MODULE
585
3.57k
    BN_CTX *new_ctx = NULL;
586
3.57k
#endif
587
588
3.57k
    if (EC_POINT_is_at_infinity(group, a)) {
589
109
        return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
590
109
    }
591
592
3.46k
    if (EC_POINT_is_at_infinity(group, b))
593
0
        return 1;
594
595
3.46k
    if (a->Z_is_one && b->Z_is_one) {
596
3.46k
        return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1;
597
3.46k
    }
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
60
{
633
60
    BIGNUM *x, *y;
634
60
    int ret = 0;
635
60
#ifndef FIPS_MODULE
636
60
    BN_CTX *new_ctx = NULL;
637
60
#endif
638
639
60
    if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
640
60
        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
16.9M
{
696
16.9M
    return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
697
16.9M
}
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
13.8M
{
703
13.8M
    return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
704
13.8M
}
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
151k
{
710
151k
    return BN_GF2m_mod_div(r, a, b, group->field, ctx);
711
151k
}
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
10.9k
{
723
    /* if p is not affine, something is wrong */
724
10.9k
    if (p->Z_is_one == 0)
725
0
        return 0;
726
727
    /* s blinding: make sure lambda (s->Z here) is not zero */
728
10.9k
    do {
729
10.9k
        if (!BN_priv_rand_ex(s->Z, BN_num_bits(group->field) - 1,
730
10.9k
                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
10.9k
    } while (BN_is_zero(s->Z));
735
736
    /* if field_encode defined convert between representations */
737
10.9k
    if ((group->meth->field_encode != NULL
738
0
            && !group->meth->field_encode(group, s->Z, s->Z, ctx))
739
10.9k
        || !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
10.9k
    do {
744
10.9k
        if (!BN_priv_rand_ex(r->Y, BN_num_bits(group->field) - 1,
745
10.9k
                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
10.9k
    } while (BN_is_zero(r->Y));
750
751
10.9k
    if ((group->meth->field_encode != NULL
752
0
            && !group->meth->field_encode(group, r->Y, r->Y, ctx))
753
10.9k
        || !group->meth->field_sqr(group, r->Z, p->X, ctx)
754
10.9k
        || !group->meth->field_sqr(group, r->X, r->Z, ctx)
755
10.9k
        || !BN_GF2m_add(r->X, r->X, group->b)
756
10.9k
        || !group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx)
757
10.9k
        || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx))
758
0
        return 0;
759
760
10.9k
    s->Z_is_one = 0;
761
10.9k
    r->Z_is_one = 0;
762
763
10.9k
    return 1;
764
10.9k
}
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
2.66M
{
775
2.66M
    if (!group->meth->field_mul(group, r->Y, r->Z, s->X, ctx)
776
2.66M
        || !group->meth->field_mul(group, s->X, r->X, s->Z, ctx)
777
2.66M
        || !group->meth->field_sqr(group, s->Y, r->Z, ctx)
778
2.66M
        || !group->meth->field_sqr(group, r->Z, r->X, ctx)
779
2.66M
        || !BN_GF2m_add(s->Z, r->Y, s->X)
780
2.66M
        || !group->meth->field_sqr(group, s->Z, s->Z, ctx)
781
2.66M
        || !group->meth->field_mul(group, s->X, r->Y, s->X, ctx)
782
2.66M
        || !group->meth->field_mul(group, r->Y, s->Z, p->X, ctx)
783
2.66M
        || !BN_GF2m_add(s->X, s->X, r->Y)
784
2.66M
        || !group->meth->field_sqr(group, r->Y, r->Z, ctx)
785
2.66M
        || !group->meth->field_mul(group, r->Z, r->Z, s->Y, ctx)
786
2.66M
        || !group->meth->field_sqr(group, s->Y, s->Y, ctx)
787
2.66M
        || !group->meth->field_mul(group, s->Y, s->Y, group->b, ctx)
788
2.66M
        || !BN_GF2m_add(r->X, r->Y, s->Y))
789
0
        return 0;
790
791
2.66M
    return 1;
792
2.66M
}
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
10.9k
{
804
10.9k
    int ret = 0;
805
10.9k
    BIGNUM *t0, *t1, *t2 = NULL;
806
807
10.9k
    if (BN_is_zero(r->Z))
808
4.15k
        return EC_POINT_set_to_infinity(group, r);
809
810
6.76k
    if (BN_is_zero(s->Z)) {
811
70
        if (!EC_POINT_copy(r, p)
812
70
            || !EC_POINT_invert(group, r, ctx)) {
813
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
814
0
            return 0;
815
0
        }
816
70
        return 1;
817
70
    }
818
819
6.69k
    BN_CTX_start(ctx);
820
6.69k
    t0 = BN_CTX_get(ctx);
821
6.69k
    t1 = BN_CTX_get(ctx);
822
6.69k
    t2 = BN_CTX_get(ctx);
823
6.69k
    if (t2 == NULL) {
824
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
825
0
        goto err;
826
0
    }
827
828
6.69k
    if (!group->meth->field_mul(group, t0, r->Z, s->Z, ctx)
829
6.69k
        || !group->meth->field_mul(group, t1, p->X, r->Z, ctx)
830
6.69k
        || !BN_GF2m_add(t1, r->X, t1)
831
6.69k
        || !group->meth->field_mul(group, t2, p->X, s->Z, ctx)
832
6.69k
        || !group->meth->field_mul(group, r->Z, r->X, t2, ctx)
833
6.69k
        || !BN_GF2m_add(t2, t2, s->X)
834
6.69k
        || !group->meth->field_mul(group, t1, t1, t2, ctx)
835
6.69k
        || !group->meth->field_sqr(group, t2, p->X, ctx)
836
6.69k
        || !BN_GF2m_add(t2, p->Y, t2)
837
6.69k
        || !group->meth->field_mul(group, t2, t2, t0, ctx)
838
6.69k
        || !BN_GF2m_add(t1, t2, t1)
839
6.69k
        || !group->meth->field_mul(group, t2, p->X, t0, ctx)
840
6.69k
        || !group->meth->field_inv(group, t2, t2, ctx)
841
6.69k
        || !group->meth->field_mul(group, t1, t1, t2, ctx)
842
6.69k
        || !group->meth->field_mul(group, r->X, r->Z, t2, ctx)
843
6.69k
        || !BN_GF2m_add(t2, p->X, r->X)
844
6.69k
        || !group->meth->field_mul(group, t2, t2, t1, ctx)
845
6.69k
        || !BN_GF2m_add(r->Y, p->Y, t2)
846
6.69k
        || !BN_one(r->Z))
847
0
        goto err;
848
849
6.69k
    r->Z_is_one = 1;
850
851
    /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
852
6.69k
    BN_set_negative(r->X, 0);
853
6.69k
    BN_set_negative(r->Y, 0);
854
855
6.69k
    ret = 1;
856
857
6.69k
err:
858
6.69k
    BN_CTX_end(ctx);
859
6.69k
    return ret;
860
6.69k
}
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
10.6k
{
868
10.6k
    int ret = 0;
869
10.6k
    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
10.6k
    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
10.6k
    if (scalar != NULL && num == 0)
889
        /* Fixed point multiplication */
890
7.89k
        return ossl_ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
891
892
2.80k
    if (scalar == NULL && num == 1)
893
        /* Variable point multiplication */
894
2.57k
        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
228
    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
228
    if (!ossl_ec_scalar_mul_ladder(group, t, scalar, NULL, ctx)
907
228
        || !ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx)
908
228
        || !EC_POINT_add(group, r, t, r, ctx))
909
0
        goto err;
910
911
228
    ret = 1;
912
913
228
err:
914
228
    EC_POINT_free(t);
915
228
    return ret;
916
228
}
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
6.69k
{
926
6.69k
    int ret;
927
928
6.69k
    if (!(ret = BN_GF2m_mod_inv(r, a, group->field, ctx)))
929
6.69k
        ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT);
930
6.69k
    return ret;
931
6.69k
}
932
933
const EC_METHOD *EC_GF2m_simple_method(void)
934
269k
{
935
269k
    static const EC_METHOD ret = {
936
269k
        EC_FLAGS_DEFAULT_OCT,
937
269k
        NID_X9_62_characteristic_two_field,
938
269k
        ossl_ec_GF2m_simple_group_init,
939
269k
        ossl_ec_GF2m_simple_group_finish,
940
269k
        ossl_ec_GF2m_simple_group_clear_finish,
941
269k
        ossl_ec_GF2m_simple_group_copy,
942
269k
        ossl_ec_GF2m_simple_group_set_curve,
943
269k
        ossl_ec_GF2m_simple_group_get_curve,
944
269k
        ossl_ec_GF2m_simple_group_get_degree,
945
269k
        ossl_ec_group_simple_order_bits,
946
269k
        ossl_ec_GF2m_simple_group_check_discriminant,
947
269k
        ossl_ec_GF2m_simple_point_init,
948
269k
        ossl_ec_GF2m_simple_point_finish,
949
269k
        ossl_ec_GF2m_simple_point_clear_finish,
950
269k
        ossl_ec_GF2m_simple_point_copy,
951
269k
        ossl_ec_GF2m_simple_point_set_to_infinity,
952
269k
        ossl_ec_GF2m_simple_point_set_affine_coordinates,
953
269k
        ossl_ec_GF2m_simple_point_get_affine_coordinates,
954
269k
        0, /* point_set_compressed_coordinates */
955
269k
        0, /* point2oct */
956
269k
        0, /* oct2point */
957
269k
        ossl_ec_GF2m_simple_add,
958
269k
        ossl_ec_GF2m_simple_dbl,
959
269k
        ossl_ec_GF2m_simple_invert,
960
269k
        ossl_ec_GF2m_simple_is_at_infinity,
961
269k
        ossl_ec_GF2m_simple_is_on_curve,
962
269k
        ossl_ec_GF2m_simple_cmp,
963
269k
        ossl_ec_GF2m_simple_make_affine,
964
269k
        ossl_ec_GF2m_simple_points_make_affine,
965
269k
        ec_GF2m_simple_points_mul,
966
269k
        0, /* precompute_mult */
967
269k
        0, /* have_precompute_mult */
968
269k
        ossl_ec_GF2m_simple_field_mul,
969
269k
        ossl_ec_GF2m_simple_field_sqr,
970
269k
        ossl_ec_GF2m_simple_field_div,
971
269k
        ec_GF2m_simple_field_inv,
972
269k
        0, /* field_encode */
973
269k
        0, /* field_decode */
974
269k
        0, /* field_set_to_one */
975
269k
        ossl_ec_key_simple_priv2oct,
976
269k
        ossl_ec_key_simple_oct2priv,
977
269k
        0, /* set private */
978
269k
        ossl_ec_key_simple_generate_key,
979
269k
        ossl_ec_key_simple_check_key,
980
269k
        ossl_ec_key_simple_generate_public_key,
981
269k
        0, /* keycopy */
982
269k
        0, /* keyfinish */
983
269k
        ossl_ecdh_simple_compute_key,
984
269k
        ossl_ecdsa_simple_sign_setup,
985
269k
        ossl_ecdsa_simple_sign_sig,
986
269k
        ossl_ecdsa_simple_verify_sig,
987
269k
        0, /* field_inverse_mod_ord */
988
269k
        0, /* blind_coordinates */
989
269k
        ec_GF2m_simple_ladder_pre,
990
269k
        ec_GF2m_simple_ladder_step,
991
269k
        ec_GF2m_simple_ladder_post
992
269k
    };
993
994
269k
    return &ret;
995
269k
}
996
997
#endif