Coverage Report

Created: 2025-06-13 06:36

/src/openssl/crypto/bn/bn_gf2m.c
Line
Count
Source (jump to first uncovered line)
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
#include <assert.h>
12
#include <limits.h>
13
#include <stdio.h>
14
#include "internal/cryptlib.h"
15
#include "bn_local.h"
16
17
#ifndef OPENSSL_NO_EC2M
18
# include <openssl/ec.h>
19
20
/*
21
 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
22
 * fail.
23
 */
24
0
# define MAX_ITERATIONS 50
25
26
0
# define SQR_nibble(w)   ((((w) & 8) << 3) \
27
0
                       |  (((w) & 4) << 2) \
28
0
                       |  (((w) & 2) << 1) \
29
0
                       |   ((w) & 1))
30
31
32
/* Platform-specific macros to accelerate squaring. */
33
# if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
34
#  define SQR1(w) \
35
0
    SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \
36
0
    SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \
37
0
    SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \
38
0
    SQR_nibble((w) >> 36) <<  8 | SQR_nibble((w) >> 32)
39
#  define SQR0(w) \
40
0
    SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \
41
0
    SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \
42
0
    SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >>  8) << 16 | \
43
0
    SQR_nibble((w) >>  4) <<  8 | SQR_nibble((w)      )
44
# endif
45
# ifdef THIRTY_TWO_BIT
46
#  define SQR1(w) \
47
    SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | \
48
    SQR_nibble((w) >> 20) <<  8 | SQR_nibble((w) >> 16)
49
#  define SQR0(w) \
50
    SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >>  8) << 16 | \
51
    SQR_nibble((w) >>  4) <<  8 | SQR_nibble((w)      )
52
# endif
53
54
# if !defined(OPENSSL_BN_ASM_GF2m)
55
/*
56
 * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
57
 * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
58
 * the variables have the right amount of space allocated.
59
 */
60
#  ifdef THIRTY_TWO_BIT
61
static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
62
                            const BN_ULONG b)
63
{
64
    register BN_ULONG h, l, s;
65
    BN_ULONG tab[8], top2b = a >> 30;
66
    register BN_ULONG a1, a2, a4;
67
68
    a1 = a & (0x3FFFFFFF);
69
    a2 = a1 << 1;
70
    a4 = a2 << 1;
71
72
    tab[0] = 0;
73
    tab[1] = a1;
74
    tab[2] = a2;
75
    tab[3] = a1 ^ a2;
76
    tab[4] = a4;
77
    tab[5] = a1 ^ a4;
78
    tab[6] = a2 ^ a4;
79
    tab[7] = a1 ^ a2 ^ a4;
80
81
    s = tab[b & 0x7];
82
    l = s;
83
    s = tab[b >> 3 & 0x7];
84
    l ^= s << 3;
85
    h = s >> 29;
86
    s = tab[b >> 6 & 0x7];
87
    l ^= s << 6;
88
    h ^= s >> 26;
89
    s = tab[b >> 9 & 0x7];
90
    l ^= s << 9;
91
    h ^= s >> 23;
92
    s = tab[b >> 12 & 0x7];
93
    l ^= s << 12;
94
    h ^= s >> 20;
95
    s = tab[b >> 15 & 0x7];
96
    l ^= s << 15;
97
    h ^= s >> 17;
98
    s = tab[b >> 18 & 0x7];
99
    l ^= s << 18;
100
    h ^= s >> 14;
101
    s = tab[b >> 21 & 0x7];
102
    l ^= s << 21;
103
    h ^= s >> 11;
104
    s = tab[b >> 24 & 0x7];
105
    l ^= s << 24;
106
    h ^= s >> 8;
107
    s = tab[b >> 27 & 0x7];
108
    l ^= s << 27;
109
    h ^= s >> 5;
110
    s = tab[b >> 30];
111
    l ^= s << 30;
112
    h ^= s >> 2;
113
114
    /* compensate for the top two bits of a */
115
116
    if (top2b & 01) {
117
        l ^= b << 30;
118
        h ^= b >> 2;
119
    }
120
    if (top2b & 02) {
121
        l ^= b << 31;
122
        h ^= b >> 1;
123
    }
124
125
    *r1 = h;
126
    *r0 = l;
127
}
128
#  endif
129
#  if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
130
static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
131
                            const BN_ULONG b)
132
0
{
133
0
    register BN_ULONG h, l, s;
134
0
    BN_ULONG tab[16], top3b = a >> 61;
135
0
    register BN_ULONG a1, a2, a4, a8;
136
137
0
    a1 = a & (0x1FFFFFFFFFFFFFFFULL);
138
0
    a2 = a1 << 1;
139
0
    a4 = a2 << 1;
140
0
    a8 = a4 << 1;
141
142
0
    tab[0] = 0;
143
0
    tab[1] = a1;
144
0
    tab[2] = a2;
145
0
    tab[3] = a1 ^ a2;
146
0
    tab[4] = a4;
147
0
    tab[5] = a1 ^ a4;
148
0
    tab[6] = a2 ^ a4;
149
0
    tab[7] = a1 ^ a2 ^ a4;
150
0
    tab[8] = a8;
151
0
    tab[9] = a1 ^ a8;
152
0
    tab[10] = a2 ^ a8;
153
0
    tab[11] = a1 ^ a2 ^ a8;
154
0
    tab[12] = a4 ^ a8;
155
0
    tab[13] = a1 ^ a4 ^ a8;
156
0
    tab[14] = a2 ^ a4 ^ a8;
157
0
    tab[15] = a1 ^ a2 ^ a4 ^ a8;
158
159
0
    s = tab[b & 0xF];
160
0
    l = s;
161
0
    s = tab[b >> 4 & 0xF];
162
0
    l ^= s << 4;
163
0
    h = s >> 60;
164
0
    s = tab[b >> 8 & 0xF];
165
0
    l ^= s << 8;
166
0
    h ^= s >> 56;
167
0
    s = tab[b >> 12 & 0xF];
168
0
    l ^= s << 12;
169
0
    h ^= s >> 52;
170
0
    s = tab[b >> 16 & 0xF];
171
0
    l ^= s << 16;
172
0
    h ^= s >> 48;
173
0
    s = tab[b >> 20 & 0xF];
174
0
    l ^= s << 20;
175
0
    h ^= s >> 44;
176
0
    s = tab[b >> 24 & 0xF];
177
0
    l ^= s << 24;
178
0
    h ^= s >> 40;
179
0
    s = tab[b >> 28 & 0xF];
180
0
    l ^= s << 28;
181
0
    h ^= s >> 36;
182
0
    s = tab[b >> 32 & 0xF];
183
0
    l ^= s << 32;
184
0
    h ^= s >> 32;
185
0
    s = tab[b >> 36 & 0xF];
186
0
    l ^= s << 36;
187
0
    h ^= s >> 28;
188
0
    s = tab[b >> 40 & 0xF];
189
0
    l ^= s << 40;
190
0
    h ^= s >> 24;
191
0
    s = tab[b >> 44 & 0xF];
192
0
    l ^= s << 44;
193
0
    h ^= s >> 20;
194
0
    s = tab[b >> 48 & 0xF];
195
0
    l ^= s << 48;
196
0
    h ^= s >> 16;
197
0
    s = tab[b >> 52 & 0xF];
198
0
    l ^= s << 52;
199
0
    h ^= s >> 12;
200
0
    s = tab[b >> 56 & 0xF];
201
0
    l ^= s << 56;
202
0
    h ^= s >> 8;
203
0
    s = tab[b >> 60];
204
0
    l ^= s << 60;
205
0
    h ^= s >> 4;
206
207
    /* compensate for the top three bits of a */
208
209
0
    if (top3b & 01) {
210
0
        l ^= b << 61;
211
0
        h ^= b >> 3;
212
0
    }
213
0
    if (top3b & 02) {
214
0
        l ^= b << 62;
215
0
        h ^= b >> 2;
216
0
    }
217
0
    if (top3b & 04) {
218
0
        l ^= b << 63;
219
0
        h ^= b >> 1;
220
0
    }
221
222
0
    *r1 = h;
223
0
    *r0 = l;
224
0
}
225
#  endif
226
227
/*
228
 * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
229
 * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
230
 * ensure that the variables have the right amount of space allocated.
231
 */
232
static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
233
                            const BN_ULONG b1, const BN_ULONG b0)
234
0
{
235
0
    BN_ULONG m1, m0;
236
    /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
237
0
    bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
238
0
    bn_GF2m_mul_1x1(r + 1, r, a0, b0);
239
0
    bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
240
    /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
241
0
    r[2] ^= m1 ^ r[1] ^ r[3];   /* h0 ^= m1 ^ l1 ^ h1; */
242
0
    r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
243
0
}
244
# else
245
void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
246
                     BN_ULONG b0);
247
# endif
248
249
/*
250
 * Add polynomials a and b and store result in r; r could be a or b, a and b
251
 * could be equal; r is the bitwise XOR of a and b.
252
 */
253
int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
254
0
{
255
0
    int i;
256
0
    const BIGNUM *at, *bt;
257
258
0
    bn_check_top(a);
259
0
    bn_check_top(b);
260
261
0
    if (a->top < b->top) {
262
0
        at = b;
263
0
        bt = a;
264
0
    } else {
265
0
        at = a;
266
0
        bt = b;
267
0
    }
268
269
0
    if (bn_wexpand(r, at->top) == NULL)
270
0
        return 0;
271
272
0
    for (i = 0; i < bt->top; i++) {
273
0
        r->d[i] = at->d[i] ^ bt->d[i];
274
0
    }
275
0
    for (; i < at->top; i++) {
276
0
        r->d[i] = at->d[i];
277
0
    }
278
279
0
    r->top = at->top;
280
0
    bn_correct_top(r);
281
282
0
    return 1;
283
0
}
284
285
/*-
286
 * Some functions allow for representation of the irreducible polynomials
287
 * as an int[], say p.  The irreducible f(t) is then of the form:
288
 *     t^p[0] + t^p[1] + ... + t^p[k]
289
 * where m = p[0] > p[1] > ... > p[k] = 0.
290
 */
291
292
/* Performs modular reduction of a and store result in r.  r could be a. */
293
int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
294
0
{
295
0
    int j, k;
296
0
    int n, dN, d0, d1;
297
0
    BN_ULONG zz, *z;
298
299
0
    bn_check_top(a);
300
301
0
    if (p[0] == 0) {
302
        /* reduction mod 1 => return 0 */
303
0
        BN_zero(r);
304
0
        return 1;
305
0
    }
306
307
    /*
308
     * Since the algorithm does reduction in the r value, if a != r, copy the
309
     * contents of a into r so we can do reduction in r.
310
     */
311
0
    if (a != r) {
312
0
        if (!bn_wexpand(r, a->top))
313
0
            return 0;
314
0
        for (j = 0; j < a->top; j++) {
315
0
            r->d[j] = a->d[j];
316
0
        }
317
0
        r->top = a->top;
318
0
    }
319
0
    z = r->d;
320
321
    /* start reduction */
322
0
    dN = p[0] / BN_BITS2;
323
0
    for (j = r->top - 1; j > dN;) {
324
0
        zz = z[j];
325
0
        if (z[j] == 0) {
326
0
            j--;
327
0
            continue;
328
0
        }
329
0
        z[j] = 0;
330
331
0
        for (k = 1; p[k] != 0; k++) {
332
            /* reducing component t^p[k] */
333
0
            n = p[0] - p[k];
334
0
            d0 = n % BN_BITS2;
335
0
            d1 = BN_BITS2 - d0;
336
0
            n /= BN_BITS2;
337
0
            z[j - n] ^= (zz >> d0);
338
0
            if (d0)
339
0
                z[j - n - 1] ^= (zz << d1);
340
0
        }
341
342
        /* reducing component t^0 */
343
0
        n = dN;
344
0
        d0 = p[0] % BN_BITS2;
345
0
        d1 = BN_BITS2 - d0;
346
0
        z[j - n] ^= (zz >> d0);
347
0
        if (d0)
348
0
            z[j - n - 1] ^= (zz << d1);
349
0
    }
350
351
    /* final round of reduction */
352
0
    while (j == dN) {
353
354
0
        d0 = p[0] % BN_BITS2;
355
0
        zz = z[dN] >> d0;
356
0
        if (zz == 0)
357
0
            break;
358
0
        d1 = BN_BITS2 - d0;
359
360
        /* clear up the top d1 bits */
361
0
        if (d0)
362
0
            z[dN] = (z[dN] << d1) >> d1;
363
0
        else
364
0
            z[dN] = 0;
365
0
        z[0] ^= zz;             /* reduction t^0 component */
366
367
0
        for (k = 1; p[k] != 0; k++) {
368
0
            BN_ULONG tmp_ulong;
369
370
            /* reducing component t^p[k] */
371
0
            n = p[k] / BN_BITS2;
372
0
            d0 = p[k] % BN_BITS2;
373
0
            d1 = BN_BITS2 - d0;
374
0
            z[n] ^= (zz << d0);
375
0
            if (d0 && (tmp_ulong = zz >> d1))
376
0
                z[n + 1] ^= tmp_ulong;
377
0
        }
378
379
0
    }
380
381
0
    bn_correct_top(r);
382
0
    return 1;
383
0
}
384
385
/*
386
 * Performs modular reduction of a by p and store result in r.  r could be a.
387
 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
388
 * function is only provided for convenience; for best performance, use the
389
 * BN_GF2m_mod_arr function.
390
 */
391
int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
392
0
{
393
0
    int ret = 0;
394
0
    int arr[6];
395
0
    bn_check_top(a);
396
0
    bn_check_top(p);
397
0
    ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
398
0
    if (!ret || ret > (int)OSSL_NELEM(arr)) {
399
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
400
0
        return 0;
401
0
    }
402
0
    ret = BN_GF2m_mod_arr(r, a, arr);
403
0
    bn_check_top(r);
404
0
    return ret;
405
0
}
406
407
/*
408
 * Compute the product of two polynomials a and b, reduce modulo p, and store
409
 * the result in r.  r could be a or b; a could be b.
410
 */
411
int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
412
                        const int p[], BN_CTX *ctx)
413
0
{
414
0
    int zlen, i, j, k, ret = 0;
415
0
    BIGNUM *s;
416
0
    BN_ULONG x1, x0, y1, y0, zz[4];
417
418
0
    bn_check_top(a);
419
0
    bn_check_top(b);
420
421
0
    if (a == b) {
422
0
        return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
423
0
    }
424
425
0
    BN_CTX_start(ctx);
426
0
    if ((s = BN_CTX_get(ctx)) == NULL)
427
0
        goto err;
428
429
0
    zlen = a->top + b->top + 4;
430
0
    if (!bn_wexpand(s, zlen))
431
0
        goto err;
432
0
    s->top = zlen;
433
434
0
    for (i = 0; i < zlen; i++)
435
0
        s->d[i] = 0;
436
437
0
    for (j = 0; j < b->top; j += 2) {
438
0
        y0 = b->d[j];
439
0
        y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
440
0
        for (i = 0; i < a->top; i += 2) {
441
0
            x0 = a->d[i];
442
0
            x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
443
0
            bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
444
0
            for (k = 0; k < 4; k++)
445
0
                s->d[i + j + k] ^= zz[k];
446
0
        }
447
0
    }
448
449
0
    bn_correct_top(s);
450
0
    if (BN_GF2m_mod_arr(r, s, p))
451
0
        ret = 1;
452
0
    bn_check_top(r);
453
454
0
 err:
455
0
    BN_CTX_end(ctx);
456
0
    return ret;
457
0
}
458
459
/*
460
 * Compute the product of two polynomials a and b, reduce modulo p, and store
461
 * the result in r.  r could be a or b; a could equal b. This function calls
462
 * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
463
 * only provided for convenience; for best performance, use the
464
 * BN_GF2m_mod_mul_arr function.
465
 */
466
int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
467
                    const BIGNUM *p, BN_CTX *ctx)
468
0
{
469
0
    int ret = 0;
470
0
    const int max = BN_num_bits(p) + 1;
471
0
    int *arr;
472
473
0
    bn_check_top(a);
474
0
    bn_check_top(b);
475
0
    bn_check_top(p);
476
477
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
478
0
    if (arr == NULL)
479
0
        return 0;
480
0
    ret = BN_GF2m_poly2arr(p, arr, max);
481
0
    if (!ret || ret > max) {
482
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
483
0
        goto err;
484
0
    }
485
0
    ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
486
0
    bn_check_top(r);
487
0
 err:
488
0
    OPENSSL_free(arr);
489
0
    return ret;
490
0
}
491
492
/* Square a, reduce the result mod p, and store it in a.  r could be a. */
493
int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
494
                        BN_CTX *ctx)
495
0
{
496
0
    int i, ret = 0;
497
0
    BIGNUM *s;
498
499
0
    bn_check_top(a);
500
0
    BN_CTX_start(ctx);
501
0
    if ((s = BN_CTX_get(ctx)) == NULL)
502
0
        goto err;
503
0
    if (!bn_wexpand(s, 2 * a->top))
504
0
        goto err;
505
506
0
    for (i = a->top - 1; i >= 0; i--) {
507
0
        s->d[2 * i + 1] = SQR1(a->d[i]);
508
0
        s->d[2 * i] = SQR0(a->d[i]);
509
0
    }
510
511
0
    s->top = 2 * a->top;
512
0
    bn_correct_top(s);
513
0
    if (!BN_GF2m_mod_arr(r, s, p))
514
0
        goto err;
515
0
    bn_check_top(r);
516
0
    ret = 1;
517
0
 err:
518
0
    BN_CTX_end(ctx);
519
0
    return ret;
520
0
}
521
522
/*
523
 * Square a, reduce the result mod p, and store it in a.  r could be a. This
524
 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
525
 * wrapper function is only provided for convenience; for best performance,
526
 * use the BN_GF2m_mod_sqr_arr function.
527
 */
528
int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
529
0
{
530
0
    int ret = 0;
531
0
    const int max = BN_num_bits(p) + 1;
532
0
    int *arr;
533
534
0
    bn_check_top(a);
535
0
    bn_check_top(p);
536
537
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
538
0
    if (arr == NULL)
539
0
        return 0;
540
0
    ret = BN_GF2m_poly2arr(p, arr, max);
541
0
    if (!ret || ret > max) {
542
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
543
0
        goto err;
544
0
    }
545
0
    ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
546
0
    bn_check_top(r);
547
0
 err:
548
0
    OPENSSL_free(arr);
549
0
    return ret;
550
0
}
551
552
/*
553
 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
554
 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
555
 * Hernandez, J.L., and Menezes, A.  "Software Implementation of Elliptic
556
 * Curve Cryptography Over Binary Fields".
557
 */
558
static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
559
                                   const BIGNUM *p, BN_CTX *ctx)
560
0
{
561
0
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
562
0
    int ret = 0;
563
564
0
    bn_check_top(a);
565
0
    bn_check_top(p);
566
567
0
    BN_CTX_start(ctx);
568
569
0
    b = BN_CTX_get(ctx);
570
0
    c = BN_CTX_get(ctx);
571
0
    u = BN_CTX_get(ctx);
572
0
    v = BN_CTX_get(ctx);
573
0
    if (v == NULL)
574
0
        goto err;
575
576
0
    if (!BN_GF2m_mod(u, a, p))
577
0
        goto err;
578
0
    if (BN_is_zero(u))
579
0
        goto err;
580
581
0
    if (!BN_copy(v, p))
582
0
        goto err;
583
# if 0
584
    if (!BN_one(b))
585
        goto err;
586
587
    while (1) {
588
        while (!BN_is_odd(u)) {
589
            if (BN_is_zero(u))
590
                goto err;
591
            if (!BN_rshift1(u, u))
592
                goto err;
593
            if (BN_is_odd(b)) {
594
                if (!BN_GF2m_add(b, b, p))
595
                    goto err;
596
            }
597
            if (!BN_rshift1(b, b))
598
                goto err;
599
        }
600
601
        if (BN_abs_is_word(u, 1))
602
            break;
603
604
        if (BN_num_bits(u) < BN_num_bits(v)) {
605
            tmp = u;
606
            u = v;
607
            v = tmp;
608
            tmp = b;
609
            b = c;
610
            c = tmp;
611
        }
612
613
        if (!BN_GF2m_add(u, u, v))
614
            goto err;
615
        if (!BN_GF2m_add(b, b, c))
616
            goto err;
617
    }
618
# else
619
0
    {
620
0
        int i;
621
0
        int ubits = BN_num_bits(u);
622
0
        int vbits = BN_num_bits(v); /* v is copy of p */
623
0
        int top = p->top;
624
0
        BN_ULONG *udp, *bdp, *vdp, *cdp;
625
626
0
        if (!bn_wexpand(u, top))
627
0
            goto err;
628
0
        udp = u->d;
629
0
        for (i = u->top; i < top; i++)
630
0
            udp[i] = 0;
631
0
        u->top = top;
632
0
        if (!bn_wexpand(b, top))
633
0
          goto err;
634
0
        bdp = b->d;
635
0
        bdp[0] = 1;
636
0
        for (i = 1; i < top; i++)
637
0
            bdp[i] = 0;
638
0
        b->top = top;
639
0
        if (!bn_wexpand(c, top))
640
0
          goto err;
641
0
        cdp = c->d;
642
0
        for (i = 0; i < top; i++)
643
0
            cdp[i] = 0;
644
0
        c->top = top;
645
0
        vdp = v->d;             /* It pays off to "cache" *->d pointers,
646
                                 * because it allows optimizer to be more
647
                                 * aggressive. But we don't have to "cache"
648
                                 * p->d, because *p is declared 'const'... */
649
0
        while (1) {
650
0
            while (ubits && !(udp[0] & 1)) {
651
0
                BN_ULONG u0, u1, b0, b1, mask;
652
653
0
                u0 = udp[0];
654
0
                b0 = bdp[0];
655
0
                mask = (BN_ULONG)0 - (b0 & 1);
656
0
                b0 ^= p->d[0] & mask;
657
0
                for (i = 0; i < top - 1; i++) {
658
0
                    u1 = udp[i + 1];
659
0
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
660
0
                    u0 = u1;
661
0
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
662
0
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
663
0
                    b0 = b1;
664
0
                }
665
0
                udp[i] = u0 >> 1;
666
0
                bdp[i] = b0 >> 1;
667
0
                ubits--;
668
0
            }
669
670
0
            if (ubits <= BN_BITS2) {
671
0
                if (udp[0] == 0) /* poly was reducible */
672
0
                    goto err;
673
0
                if (udp[0] == 1)
674
0
                    break;
675
0
            }
676
677
0
            if (ubits < vbits) {
678
0
                i = ubits;
679
0
                ubits = vbits;
680
0
                vbits = i;
681
0
                tmp = u;
682
0
                u = v;
683
0
                v = tmp;
684
0
                tmp = b;
685
0
                b = c;
686
0
                c = tmp;
687
0
                udp = vdp;
688
0
                vdp = v->d;
689
0
                bdp = cdp;
690
0
                cdp = c->d;
691
0
            }
692
0
            for (i = 0; i < top; i++) {
693
0
                udp[i] ^= vdp[i];
694
0
                bdp[i] ^= cdp[i];
695
0
            }
696
0
            if (ubits == vbits) {
697
0
                BN_ULONG ul;
698
0
                int utop = (ubits - 1) / BN_BITS2;
699
700
0
                while ((ul = udp[utop]) == 0 && utop)
701
0
                    utop--;
702
0
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
703
0
            }
704
0
        }
705
0
        bn_correct_top(b);
706
0
    }
707
0
# endif
708
709
0
    if (!BN_copy(r, b))
710
0
        goto err;
711
0
    bn_check_top(r);
712
0
    ret = 1;
713
714
0
 err:
715
# ifdef BN_DEBUG
716
    /* BN_CTX_end would complain about the expanded form */
717
    bn_correct_top(c);
718
    bn_correct_top(u);
719
    bn_correct_top(v);
720
# endif
721
0
    BN_CTX_end(ctx);
722
0
    return ret;
723
0
}
724
725
/*-
726
 * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
727
 * This is not constant time.
728
 * But it does eliminate first order deduction on the input.
729
 */
730
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
731
0
{
732
0
    BIGNUM *b = NULL;
733
0
    int ret = 0;
734
0
    int numbits;
735
736
0
    BN_CTX_start(ctx);
737
0
    if ((b = BN_CTX_get(ctx)) == NULL)
738
0
        goto err;
739
740
    /* Fail on a non-sensical input p value */
741
0
    numbits = BN_num_bits(p);
742
0
    if (numbits <= 1)
743
0
        goto err;
744
745
    /* generate blinding value */
746
0
    do {
747
0
        if (!BN_priv_rand_ex(b, numbits - 1,
748
0
                             BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
749
0
            goto err;
750
0
    } while (BN_is_zero(b));
751
752
    /* r := a * b */
753
0
    if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
754
0
        goto err;
755
756
    /* r := 1/(a * b) */
757
0
    if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
758
0
        goto err;
759
760
    /* r := b/(a * b) = 1/a */
761
0
    if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
762
0
        goto err;
763
764
0
    ret = 1;
765
766
0
 err:
767
0
    BN_CTX_end(ctx);
768
0
    return ret;
769
0
}
770
771
/*
772
 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
773
 * This function calls down to the BN_GF2m_mod_inv implementation; this
774
 * wrapper function is only provided for convenience; for best performance,
775
 * use the BN_GF2m_mod_inv function.
776
 */
777
int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
778
                        BN_CTX *ctx)
779
0
{
780
0
    BIGNUM *field;
781
0
    int ret = 0;
782
783
0
    bn_check_top(xx);
784
0
    BN_CTX_start(ctx);
785
0
    if ((field = BN_CTX_get(ctx)) == NULL)
786
0
        goto err;
787
0
    if (!BN_GF2m_arr2poly(p, field))
788
0
        goto err;
789
790
0
    ret = BN_GF2m_mod_inv(r, xx, field, ctx);
791
0
    bn_check_top(r);
792
793
0
 err:
794
0
    BN_CTX_end(ctx);
795
0
    return ret;
796
0
}
797
798
/*
799
 * Divide y by x, reduce modulo p, and store the result in r. r could be x
800
 * or y, x could equal y.
801
 */
802
int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
803
                    const BIGNUM *p, BN_CTX *ctx)
804
0
{
805
0
    BIGNUM *xinv = NULL;
806
0
    int ret = 0;
807
808
0
    bn_check_top(y);
809
0
    bn_check_top(x);
810
0
    bn_check_top(p);
811
812
0
    BN_CTX_start(ctx);
813
0
    xinv = BN_CTX_get(ctx);
814
0
    if (xinv == NULL)
815
0
        goto err;
816
817
0
    if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
818
0
        goto err;
819
0
    if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
820
0
        goto err;
821
0
    bn_check_top(r);
822
0
    ret = 1;
823
824
0
 err:
825
0
    BN_CTX_end(ctx);
826
0
    return ret;
827
0
}
828
829
/*
830
 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
831
 * * or yy, xx could equal yy. This function calls down to the
832
 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
833
 * convenience; for best performance, use the BN_GF2m_mod_div function.
834
 */
835
int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
836
                        const int p[], BN_CTX *ctx)
837
0
{
838
0
    BIGNUM *field;
839
0
    int ret = 0;
840
841
0
    bn_check_top(yy);
842
0
    bn_check_top(xx);
843
844
0
    BN_CTX_start(ctx);
845
0
    if ((field = BN_CTX_get(ctx)) == NULL)
846
0
        goto err;
847
0
    if (!BN_GF2m_arr2poly(p, field))
848
0
        goto err;
849
850
0
    ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
851
0
    bn_check_top(r);
852
853
0
 err:
854
0
    BN_CTX_end(ctx);
855
0
    return ret;
856
0
}
857
858
/*
859
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
860
 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
861
 * P1363.
862
 */
863
int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
864
                        const int p[], BN_CTX *ctx)
865
0
{
866
0
    int ret = 0, i, n;
867
0
    BIGNUM *u;
868
869
0
    bn_check_top(a);
870
0
    bn_check_top(b);
871
872
0
    if (BN_is_zero(b))
873
0
        return BN_one(r);
874
875
0
    if (BN_abs_is_word(b, 1))
876
0
        return (BN_copy(r, a) != NULL);
877
878
0
    BN_CTX_start(ctx);
879
0
    if ((u = BN_CTX_get(ctx)) == NULL)
880
0
        goto err;
881
882
0
    if (!BN_GF2m_mod_arr(u, a, p))
883
0
        goto err;
884
885
0
    n = BN_num_bits(b) - 1;
886
0
    for (i = n - 1; i >= 0; i--) {
887
0
        if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
888
0
            goto err;
889
0
        if (BN_is_bit_set(b, i)) {
890
0
            if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
891
0
                goto err;
892
0
        }
893
0
    }
894
0
    if (!BN_copy(r, u))
895
0
        goto err;
896
0
    bn_check_top(r);
897
0
    ret = 1;
898
0
 err:
899
0
    BN_CTX_end(ctx);
900
0
    return ret;
901
0
}
902
903
/*
904
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
905
 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
906
 * implementation; this wrapper function is only provided for convenience;
907
 * for best performance, use the BN_GF2m_mod_exp_arr function.
908
 */
909
int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
910
                    const BIGNUM *p, BN_CTX *ctx)
911
0
{
912
0
    int ret = 0;
913
0
    const int max = BN_num_bits(p) + 1;
914
0
    int *arr;
915
916
0
    bn_check_top(a);
917
0
    bn_check_top(b);
918
0
    bn_check_top(p);
919
920
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
921
0
    if (arr == NULL)
922
0
        return 0;
923
0
    ret = BN_GF2m_poly2arr(p, arr, max);
924
0
    if (!ret || ret > max) {
925
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
926
0
        goto err;
927
0
    }
928
0
    ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
929
0
    bn_check_top(r);
930
0
 err:
931
0
    OPENSSL_free(arr);
932
0
    return ret;
933
0
}
934
935
/*
936
 * Compute the square root of a, reduce modulo p, and store the result in r.
937
 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
938
 */
939
int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
940
                         BN_CTX *ctx)
941
0
{
942
0
    int ret = 0;
943
0
    BIGNUM *u;
944
945
0
    bn_check_top(a);
946
947
0
    if (p[0] == 0) {
948
        /* reduction mod 1 => return 0 */
949
0
        BN_zero(r);
950
0
        return 1;
951
0
    }
952
953
0
    BN_CTX_start(ctx);
954
0
    if ((u = BN_CTX_get(ctx)) == NULL)
955
0
        goto err;
956
957
0
    if (!BN_set_bit(u, p[0] - 1))
958
0
        goto err;
959
0
    ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
960
0
    bn_check_top(r);
961
962
0
 err:
963
0
    BN_CTX_end(ctx);
964
0
    return ret;
965
0
}
966
967
/*
968
 * Compute the square root of a, reduce modulo p, and store the result in r.
969
 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
970
 * implementation; this wrapper function is only provided for convenience;
971
 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
972
 */
973
int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
974
0
{
975
0
    int ret = 0;
976
0
    const int max = BN_num_bits(p) + 1;
977
0
    int *arr;
978
979
0
    bn_check_top(a);
980
0
    bn_check_top(p);
981
982
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
983
0
    if (arr == NULL)
984
0
        return 0;
985
0
    ret = BN_GF2m_poly2arr(p, arr, max);
986
0
    if (!ret || ret > max) {
987
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
988
0
        goto err;
989
0
    }
990
0
    ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
991
0
    bn_check_top(r);
992
0
 err:
993
0
    OPENSSL_free(arr);
994
0
    return ret;
995
0
}
996
997
/*
998
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
999
 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
1000
 */
1001
int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1002
                               BN_CTX *ctx)
1003
0
{
1004
0
    int ret = 0, count = 0, j;
1005
0
    BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1006
1007
0
    bn_check_top(a_);
1008
1009
0
    if (p[0] == 0) {
1010
        /* reduction mod 1 => return 0 */
1011
0
        BN_zero(r);
1012
0
        return 1;
1013
0
    }
1014
1015
0
    BN_CTX_start(ctx);
1016
0
    a = BN_CTX_get(ctx);
1017
0
    z = BN_CTX_get(ctx);
1018
0
    w = BN_CTX_get(ctx);
1019
0
    if (w == NULL)
1020
0
        goto err;
1021
1022
0
    if (!BN_GF2m_mod_arr(a, a_, p))
1023
0
        goto err;
1024
1025
0
    if (BN_is_zero(a)) {
1026
0
        BN_zero(r);
1027
0
        ret = 1;
1028
0
        goto err;
1029
0
    }
1030
1031
0
    if (p[0] & 0x1) {           /* m is odd */
1032
        /* compute half-trace of a */
1033
0
        if (!BN_copy(z, a))
1034
0
            goto err;
1035
0
        for (j = 1; j <= (p[0] - 1) / 2; j++) {
1036
0
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1037
0
                goto err;
1038
0
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1039
0
                goto err;
1040
0
            if (!BN_GF2m_add(z, z, a))
1041
0
                goto err;
1042
0
        }
1043
1044
0
    } else {                    /* m is even */
1045
1046
0
        rho = BN_CTX_get(ctx);
1047
0
        w2 = BN_CTX_get(ctx);
1048
0
        tmp = BN_CTX_get(ctx);
1049
0
        if (tmp == NULL)
1050
0
            goto err;
1051
0
        do {
1052
0
            if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1053
0
                                 0, ctx))
1054
0
                goto err;
1055
0
            if (!BN_GF2m_mod_arr(rho, rho, p))
1056
0
                goto err;
1057
0
            BN_zero(z);
1058
0
            if (!BN_copy(w, rho))
1059
0
                goto err;
1060
0
            for (j = 1; j <= p[0] - 1; j++) {
1061
0
                if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1062
0
                    goto err;
1063
0
                if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1064
0
                    goto err;
1065
0
                if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1066
0
                    goto err;
1067
0
                if (!BN_GF2m_add(z, z, tmp))
1068
0
                    goto err;
1069
0
                if (!BN_GF2m_add(w, w2, rho))
1070
0
                    goto err;
1071
0
            }
1072
0
            count++;
1073
0
        } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1074
0
        if (BN_is_zero(w)) {
1075
0
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1076
0
            goto err;
1077
0
        }
1078
0
    }
1079
1080
0
    if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1081
0
        goto err;
1082
0
    if (!BN_GF2m_add(w, z, w))
1083
0
        goto err;
1084
0
    if (BN_GF2m_cmp(w, a)) {
1085
0
        ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1086
0
        goto err;
1087
0
    }
1088
1089
0
    if (!BN_copy(r, z))
1090
0
        goto err;
1091
0
    bn_check_top(r);
1092
1093
0
    ret = 1;
1094
1095
0
 err:
1096
0
    BN_CTX_end(ctx);
1097
0
    return ret;
1098
0
}
1099
1100
/*
1101
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
1102
 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1103
 * implementation; this wrapper function is only provided for convenience;
1104
 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1105
 */
1106
int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1107
                           BN_CTX *ctx)
1108
0
{
1109
0
    int ret = 0;
1110
0
    const int max = BN_num_bits(p) + 1;
1111
0
    int *arr;
1112
1113
0
    bn_check_top(a);
1114
0
    bn_check_top(p);
1115
1116
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
1117
0
    if (arr == NULL)
1118
0
        goto err;
1119
0
    ret = BN_GF2m_poly2arr(p, arr, max);
1120
0
    if (!ret || ret > max) {
1121
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
1122
0
        goto err;
1123
0
    }
1124
0
    ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1125
0
    bn_check_top(r);
1126
0
 err:
1127
0
    OPENSSL_free(arr);
1128
0
    return ret;
1129
0
}
1130
1131
/*
1132
 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1133
 * x^i) into an array of integers corresponding to the bits with non-zero
1134
 * coefficient.  The array is intended to be suitable for use with
1135
 * `BN_GF2m_mod_arr()`, and so the constant term of the polynomial must not be
1136
 * zero.  This translates to a requirement that the input BIGNUM `a` is odd.
1137
 *
1138
 * Given sufficient room, the array is terminated with -1.  Up to max elements
1139
 * of the array will be filled.
1140
 *
1141
 * The return value is total number of array elements that would be filled if
1142
 * array was large enough, including the terminating `-1`.  It is `0` when `a`
1143
 * is not odd or the constant term is zero contrary to requirement.
1144
 *
1145
 * The return value is also `0` when the leading exponent exceeds
1146
 * `OPENSSL_ECC_MAX_FIELD_BITS`, this guards against CPU exhaustion attacks,
1147
 */
1148
int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1149
0
{
1150
0
    int i, j, k = 0;
1151
0
    BN_ULONG mask;
1152
1153
0
    if (!BN_is_odd(a))
1154
0
        return 0;
1155
1156
0
    for (i = a->top - 1; i >= 0; i--) {
1157
0
        if (!a->d[i])
1158
            /* skip word if a->d[i] == 0 */
1159
0
            continue;
1160
0
        mask = BN_TBIT;
1161
0
        for (j = BN_BITS2 - 1; j >= 0; j--) {
1162
0
            if (a->d[i] & mask) {
1163
0
                if (k < max)
1164
0
                    p[k] = BN_BITS2 * i + j;
1165
0
                k++;
1166
0
            }
1167
0
            mask >>= 1;
1168
0
        }
1169
0
    }
1170
1171
0
    if (k > 0 && p[0] > OPENSSL_ECC_MAX_FIELD_BITS)
1172
0
        return 0;
1173
1174
0
    if (k < max)
1175
0
        p[k] = -1;
1176
1177
0
    return k + 1;
1178
0
}
1179
1180
/*
1181
 * Convert the coefficient array representation of a polynomial to a
1182
 * bit-string.  The array must be terminated by -1.
1183
 */
1184
int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1185
0
{
1186
0
    int i;
1187
1188
0
    bn_check_top(a);
1189
0
    BN_zero(a);
1190
0
    for (i = 0; p[i] != -1; i++) {
1191
0
        if (BN_set_bit(a, p[i]) == 0)
1192
0
            return 0;
1193
0
    }
1194
0
    bn_check_top(a);
1195
1196
0
    return 1;
1197
0
}
1198
1199
#endif