Coverage Report

Created: 2023-09-25 06:45

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