Coverage Report

Created: 2023-06-08 06:41

/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
19
/*
20
 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
21
 * fail.
22
 */
23
550
# define MAX_ITERATIONS 50
24
25
43.9M
# define SQR_nibble(w)   ((((w) & 8) << 3) \
26
43.9M
                       |  (((w) & 4) << 2) \
27
43.9M
                       |  (((w) & 2) << 1) \
28
43.9M
                       |   ((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
2.74M
    SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \
35
2.74M
    SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \
36
2.74M
    SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \
37
2.74M
    SQR_nibble((w) >> 36) <<  8 | SQR_nibble((w) >> 32)
38
#  define SQR0(w) \
39
2.74M
    SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \
40
2.74M
    SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \
41
2.74M
    SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >>  8) << 16 | \
42
2.74M
    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
438k
{
254
438k
    int i;
255
438k
    const BIGNUM *at, *bt;
256
257
438k
    bn_check_top(a);
258
438k
    bn_check_top(b);
259
260
438k
    if (a->top < b->top) {
261
1.19k
        at = b;
262
1.19k
        bt = a;
263
436k
    } else {
264
436k
        at = a;
265
436k
        bt = b;
266
436k
    }
267
268
438k
    if (bn_wexpand(r, at->top) == NULL)
269
0
        return 0;
270
271
3.04M
    for (i = 0; i < bt->top; i++) {
272
2.61M
        r->d[i] = at->d[i] ^ bt->d[i];
273
2.61M
    }
274
444k
    for (; i < at->top; i++) {
275
5.99k
        r->d[i] = at->d[i];
276
5.99k
    }
277
278
438k
    r->top = at->top;
279
438k
    bn_correct_top(r);
280
281
438k
    return 1;
282
438k
}
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
671k
{
294
671k
    int j, k;
295
671k
    int n, dN, d0, d1;
296
671k
    BN_ULONG zz, *z;
297
298
671k
    bn_check_top(a);
299
300
671k
    if (p[0] == 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
671k
    if (a != r) {
311
670k
        if (!bn_wexpand(r, a->top))
312
0
            return 0;
313
8.63M
        for (j = 0; j < a->top; j++) {
314
7.96M
            r->d[j] = a->d[j];
315
7.96M
        }
316
670k
        r->top = a->top;
317
670k
    }
318
671k
    z = r->d;
319
320
    /* start reduction */
321
671k
    dN = p[0] / BN_BITS2;
322
8.60M
    for (j = r->top - 1; j > dN;) {
323
7.93M
        zz = z[j];
324
7.93M
        if (z[j] == 0) {
325
3.96M
            j--;
326
3.96M
            continue;
327
3.96M
        }
328
3.96M
        z[j] = 0;
329
330
15.3M
        for (k = 1; p[k] != 0; k++) {
331
            /* reducing component t^p[k] */
332
11.3M
            n = p[0] - p[k];
333
11.3M
            d0 = n % BN_BITS2;
334
11.3M
            d1 = BN_BITS2 - d0;
335
11.3M
            n /= BN_BITS2;
336
11.3M
            z[j - n] ^= (zz >> d0);
337
11.3M
            if (d0)
338
11.3M
                z[j - n - 1] ^= (zz << d1);
339
11.3M
        }
340
341
        /* reducing component t^0 */
342
3.96M
        n = dN;
343
3.96M
        d0 = p[0] % BN_BITS2;
344
3.96M
        d1 = BN_BITS2 - d0;
345
3.96M
        z[j - n] ^= (zz >> d0);
346
3.96M
        if (d0)
347
3.96M
            z[j - n - 1] ^= (zz << d1);
348
3.96M
    }
349
350
    /* final round of reduction */
351
1.33M
    while (j == dN) {
352
353
1.33M
        d0 = p[0] % BN_BITS2;
354
1.33M
        zz = z[dN] >> d0;
355
1.33M
        if (zz == 0)
356
668k
            break;
357
666k
        d1 = BN_BITS2 - d0;
358
359
        /* clear up the top d1 bits */
360
666k
        if (d0)
361
666k
            z[dN] = (z[dN] << d1) >> d1;
362
0
        else
363
0
            z[dN] = 0;
364
666k
        z[0] ^= zz;             /* reduction t^0 component */
365
366
2.57M
        for (k = 1; p[k] != 0; k++) {
367
1.91M
            BN_ULONG tmp_ulong;
368
369
            /* reducing component t^p[k] */
370
1.91M
            n = p[k] / BN_BITS2;
371
1.91M
            d0 = p[k] % BN_BITS2;
372
1.91M
            d1 = BN_BITS2 - d0;
373
1.91M
            z[n] ^= (zz << d0);
374
1.91M
            if (d0 && (tmp_ulong = zz >> d1))
375
47.9k
                z[n + 1] ^= tmp_ulong;
376
1.91M
        }
377
378
666k
    }
379
380
671k
    bn_correct_top(r);
381
671k
    return 1;
382
671k
}
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
192
{
392
192
    int ret = 0;
393
192
    int arr[6];
394
192
    bn_check_top(a);
395
192
    bn_check_top(p);
396
192
    ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
397
192
    if (!ret || ret > (int)OSSL_NELEM(arr)) {
398
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
399
0
        return 0;
400
0
    }
401
192
    ret = BN_GF2m_mod_arr(r, a, arr);
402
192
    bn_check_top(r);
403
192
    return ret;
404
192
}
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
208k
{
413
208k
    int zlen, i, j, k, ret = 0;
414
208k
    BIGNUM *s;
415
208k
    BN_ULONG x1, x0, y1, y0, zz[4];
416
417
208k
    bn_check_top(a);
418
208k
    bn_check_top(b);
419
420
208k
    if (a == b) {
421
0
        return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
422
0
    }
423
424
208k
    BN_CTX_start(ctx);
425
208k
    if ((s = BN_CTX_get(ctx)) == NULL)
426
0
        goto err;
427
428
208k
    zlen = a->top + b->top + 4;
429
208k
    if (!bn_wexpand(s, zlen))
430
0
        goto err;
431
208k
    s->top = zlen;
432
433
3.52M
    for (i = 0; i < zlen; i++)
434
3.31M
        s->d[i] = 0;
435
436
831k
    for (j = 0; j < b->top; j += 2) {
437
623k
        y0 = b->d[j];
438
623k
        y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
439
2.49M
        for (i = 0; i < a->top; i += 2) {
440
1.86M
            x0 = a->d[i];
441
1.86M
            x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
442
1.86M
            bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
443
9.34M
            for (k = 0; k < 4; k++)
444
7.47M
                s->d[i + j + k] ^= zz[k];
445
1.86M
        }
446
623k
    }
447
448
208k
    bn_correct_top(s);
449
208k
    if (BN_GF2m_mod_arr(r, s, p))
450
208k
        ret = 1;
451
208k
    bn_check_top(r);
452
453
208k
 err:
454
208k
    BN_CTX_end(ctx);
455
208k
    return ret;
456
208k
}
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
576
{
468
576
    int ret = 0;
469
576
    const int max = BN_num_bits(p) + 1;
470
576
    int *arr;
471
472
576
    bn_check_top(a);
473
576
    bn_check_top(b);
474
576
    bn_check_top(p);
475
476
576
    arr = OPENSSL_malloc(sizeof(*arr) * max);
477
576
    if (arr == NULL)
478
0
        return 0;
479
576
    ret = BN_GF2m_poly2arr(p, arr, max);
480
576
    if (!ret || ret > max) {
481
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
482
0
        goto err;
483
0
    }
484
576
    ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
485
576
    bn_check_top(r);
486
576
 err:
487
576
    OPENSSL_free(arr);
488
576
    return ret;
489
576
}
490
491
/* Square a, reduce the result mod p, and store it in a.  r could be a. */
492
int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
493
                        BN_CTX *ctx)
494
461k
{
495
461k
    int i, ret = 0;
496
461k
    BIGNUM *s;
497
498
461k
    bn_check_top(a);
499
461k
    BN_CTX_start(ctx);
500
461k
    if ((s = BN_CTX_get(ctx)) == NULL)
501
0
        goto err;
502
461k
    if (!bn_wexpand(s, 2 * a->top))
503
0
        goto err;
504
505
3.20M
    for (i = a->top - 1; i >= 0; i--) {
506
2.74M
        s->d[2 * i + 1] = SQR1(a->d[i]);
507
2.74M
        s->d[2 * i] = SQR0(a->d[i]);
508
2.74M
    }
509
510
461k
    s->top = 2 * a->top;
511
461k
    bn_correct_top(s);
512
461k
    if (!BN_GF2m_mod_arr(r, s, p))
513
0
        goto err;
514
461k
    bn_check_top(r);
515
461k
    ret = 1;
516
461k
 err:
517
461k
    BN_CTX_end(ctx);
518
461k
    return ret;
519
461k
}
520
521
/*
522
 * Square a, reduce the result mod p, and store it in a.  r could be a. This
523
 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
524
 * wrapper function is only provided for convenience; for best performance,
525
 * use the BN_GF2m_mod_sqr_arr function.
526
 */
527
int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
528
0
{
529
0
    int ret = 0;
530
0
    const int max = BN_num_bits(p) + 1;
531
0
    int *arr;
532
533
0
    bn_check_top(a);
534
0
    bn_check_top(p);
535
536
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
537
0
    if (arr == NULL)
538
0
        return 0;
539
0
    ret = BN_GF2m_poly2arr(p, arr, max);
540
0
    if (!ret || ret > max) {
541
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
542
0
        goto err;
543
0
    }
544
0
    ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
545
0
    bn_check_top(r);
546
0
 err:
547
0
    OPENSSL_free(arr);
548
0
    return ret;
549
0
}
550
551
/*
552
 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
553
 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
554
 * Hernandez, J.L., and Menezes, A.  "Software Implementation of Elliptic
555
 * Curve Cryptography Over Binary Fields".
556
 */
557
static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
558
                                   const BIGNUM *p, BN_CTX *ctx)
559
192
{
560
192
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
561
192
    int ret = 0;
562
563
192
    bn_check_top(a);
564
192
    bn_check_top(p);
565
566
192
    BN_CTX_start(ctx);
567
568
192
    b = BN_CTX_get(ctx);
569
192
    c = BN_CTX_get(ctx);
570
192
    u = BN_CTX_get(ctx);
571
192
    v = BN_CTX_get(ctx);
572
192
    if (v == NULL)
573
0
        goto err;
574
575
192
    if (!BN_GF2m_mod(u, a, p))
576
0
        goto err;
577
192
    if (BN_is_zero(u))
578
0
        goto err;
579
580
192
    if (!BN_copy(v, p))
581
0
        goto err;
582
# if 0
583
    if (!BN_one(b))
584
        goto err;
585
586
    while (1) {
587
        while (!BN_is_odd(u)) {
588
            if (BN_is_zero(u))
589
                goto err;
590
            if (!BN_rshift1(u, u))
591
                goto err;
592
            if (BN_is_odd(b)) {
593
                if (!BN_GF2m_add(b, b, p))
594
                    goto err;
595
            }
596
            if (!BN_rshift1(b, b))
597
                goto err;
598
        }
599
600
        if (BN_abs_is_word(u, 1))
601
            break;
602
603
        if (BN_num_bits(u) < BN_num_bits(v)) {
604
            tmp = u;
605
            u = v;
606
            v = tmp;
607
            tmp = b;
608
            b = c;
609
            c = tmp;
610
        }
611
612
        if (!BN_GF2m_add(u, u, v))
613
            goto err;
614
        if (!BN_GF2m_add(b, b, c))
615
            goto err;
616
    }
617
# else
618
192
    {
619
192
        int i;
620
192
        int ubits = BN_num_bits(u);
621
192
        int vbits = BN_num_bits(v); /* v is copy of p */
622
192
        int top = p->top;
623
192
        BN_ULONG *udp, *bdp, *vdp, *cdp;
624
625
192
        if (!bn_wexpand(u, top))
626
0
            goto err;
627
192
        udp = u->d;
628
200
        for (i = u->top; i < top; i++)
629
8
            udp[i] = 0;
630
192
        u->top = top;
631
192
        if (!bn_wexpand(b, top))
632
0
          goto err;
633
192
        bdp = b->d;
634
192
        bdp[0] = 1;
635
1.02k
        for (i = 1; i < top; i++)
636
836
            bdp[i] = 0;
637
192
        b->top = top;
638
192
        if (!bn_wexpand(c, top))
639
0
          goto err;
640
192
        cdp = c->d;
641
1.22k
        for (i = 0; i < top; i++)
642
1.02k
            cdp[i] = 0;
643
192
        c->top = top;
644
192
        vdp = v->d;             /* It pays off to "cache" *->d pointers,
645
                                 * because it allows optimizer to be more
646
                                 * aggressive. But we don't have to "cache"
647
                                 * p->d, because *p is declared 'const'... */
648
50.1k
        while (1) {
649
150k
            while (ubits && !(udp[0] & 1)) {
650
100k
                BN_ULONG u0, u1, b0, b1, mask;
651
652
100k
                u0 = udp[0];
653
100k
                b0 = bdp[0];
654
100k
                mask = (BN_ULONG)0 - (b0 & 1);
655
100k
                b0 ^= p->d[0] & mask;
656
584k
                for (i = 0; i < top - 1; i++) {
657
484k
                    u1 = udp[i + 1];
658
484k
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
659
484k
                    u0 = u1;
660
484k
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
661
484k
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
662
484k
                    b0 = b1;
663
484k
                }
664
100k
                udp[i] = u0 >> 1;
665
100k
                bdp[i] = b0 >> 1;
666
100k
                ubits--;
667
100k
            }
668
669
50.1k
            if (ubits <= BN_BITS2) {
670
9.99k
                if (udp[0] == 0) /* poly was reducible */
671
0
                    goto err;
672
9.99k
                if (udp[0] == 1)
673
192
                    break;
674
9.99k
            }
675
676
49.9k
            if (ubits < vbits) {
677
19.7k
                i = ubits;
678
19.7k
                ubits = vbits;
679
19.7k
                vbits = i;
680
19.7k
                tmp = u;
681
19.7k
                u = v;
682
19.7k
                v = tmp;
683
19.7k
                tmp = b;
684
19.7k
                b = c;
685
19.7k
                c = tmp;
686
19.7k
                udp = vdp;
687
19.7k
                vdp = v->d;
688
19.7k
                bdp = cdp;
689
19.7k
                cdp = c->d;
690
19.7k
            }
691
342k
            for (i = 0; i < top; i++) {
692
292k
                udp[i] ^= vdp[i];
693
292k
                bdp[i] ^= cdp[i];
694
292k
            }
695
49.9k
            if (ubits == vbits) {
696
9.94k
                BN_ULONG ul;
697
9.94k
                int utop = (ubits - 1) / BN_BITS2;
698
699
10.2k
                while ((ul = udp[utop]) == 0 && utop)
700
278
                    utop--;
701
9.94k
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
702
9.94k
            }
703
49.9k
        }
704
192
        bn_correct_top(b);
705
192
    }
706
0
# endif
707
708
192
    if (!BN_copy(r, b))
709
0
        goto err;
710
192
    bn_check_top(r);
711
192
    ret = 1;
712
713
192
 err:
714
# ifdef BN_DEBUG
715
    /* BN_CTX_end would complain about the expanded form */
716
    bn_correct_top(c);
717
    bn_correct_top(u);
718
    bn_correct_top(v);
719
# endif
720
192
    BN_CTX_end(ctx);
721
192
    return ret;
722
192
}
723
724
/*-
725
 * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
726
 * This is not constant time.
727
 * But it does eliminate first order deduction on the input.
728
 */
729
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
730
192
{
731
192
    BIGNUM *b = NULL;
732
192
    int ret = 0;
733
734
192
    BN_CTX_start(ctx);
735
192
    if ((b = BN_CTX_get(ctx)) == NULL)
736
0
        goto err;
737
738
    /* generate blinding value */
739
192
    do {
740
192
        if (!BN_priv_rand_ex(b, BN_num_bits(p) - 1,
741
192
                             BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
742
0
            goto err;
743
192
    } while (BN_is_zero(b));
744
745
    /* r := a * b */
746
192
    if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
747
0
        goto err;
748
749
    /* r := 1/(a * b) */
750
192
    if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
751
0
        goto err;
752
753
    /* r := b/(a * b) = 1/a */
754
192
    if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
755
0
        goto err;
756
757
192
    ret = 1;
758
759
192
 err:
760
192
    BN_CTX_end(ctx);
761
192
    return ret;
762
192
}
763
764
/*
765
 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
766
 * This function calls down to the BN_GF2m_mod_inv implementation; this
767
 * wrapper function is only provided for convenience; for best performance,
768
 * use the BN_GF2m_mod_inv function.
769
 */
770
int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
771
                        BN_CTX *ctx)
772
0
{
773
0
    BIGNUM *field;
774
0
    int ret = 0;
775
776
0
    bn_check_top(xx);
777
0
    BN_CTX_start(ctx);
778
0
    if ((field = BN_CTX_get(ctx)) == NULL)
779
0
        goto err;
780
0
    if (!BN_GF2m_arr2poly(p, field))
781
0
        goto err;
782
783
0
    ret = BN_GF2m_mod_inv(r, xx, field, ctx);
784
0
    bn_check_top(r);
785
786
0
 err:
787
0
    BN_CTX_end(ctx);
788
0
    return ret;
789
0
}
790
791
/*
792
 * Divide y by x, reduce modulo p, and store the result in r. r could be x
793
 * or y, x could equal y.
794
 */
795
int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
796
                    const BIGNUM *p, BN_CTX *ctx)
797
192
{
798
192
    BIGNUM *xinv = NULL;
799
192
    int ret = 0;
800
801
192
    bn_check_top(y);
802
192
    bn_check_top(x);
803
192
    bn_check_top(p);
804
805
192
    BN_CTX_start(ctx);
806
192
    xinv = BN_CTX_get(ctx);
807
192
    if (xinv == NULL)
808
0
        goto err;
809
810
192
    if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
811
0
        goto err;
812
192
    if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
813
0
        goto err;
814
192
    bn_check_top(r);
815
192
    ret = 1;
816
817
192
 err:
818
192
    BN_CTX_end(ctx);
819
192
    return ret;
820
192
}
821
822
/*
823
 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
824
 * * or yy, xx could equal yy. This function calls down to the
825
 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
826
 * convenience; for best performance, use the BN_GF2m_mod_div function.
827
 */
828
int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
829
                        const int p[], BN_CTX *ctx)
830
0
{
831
0
    BIGNUM *field;
832
0
    int ret = 0;
833
834
0
    bn_check_top(yy);
835
0
    bn_check_top(xx);
836
837
0
    BN_CTX_start(ctx);
838
0
    if ((field = BN_CTX_get(ctx)) == NULL)
839
0
        goto err;
840
0
    if (!BN_GF2m_arr2poly(p, field))
841
0
        goto err;
842
843
0
    ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
844
0
    bn_check_top(r);
845
846
0
 err:
847
0
    BN_CTX_end(ctx);
848
0
    return ret;
849
0
}
850
851
/*
852
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
853
 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
854
 * P1363.
855
 */
856
int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
857
                        const int p[], BN_CTX *ctx)
858
9
{
859
9
    int ret = 0, i, n;
860
9
    BIGNUM *u;
861
862
9
    bn_check_top(a);
863
9
    bn_check_top(b);
864
865
9
    if (BN_is_zero(b))
866
0
        return BN_one(r);
867
868
9
    if (BN_abs_is_word(b, 1))
869
0
        return (BN_copy(r, a) != NULL);
870
871
9
    BN_CTX_start(ctx);
872
9
    if ((u = BN_CTX_get(ctx)) == NULL)
873
0
        goto err;
874
875
9
    if (!BN_GF2m_mod_arr(u, a, p))
876
0
        goto err;
877
878
9
    n = BN_num_bits(b) - 1;
879
2.24k
    for (i = n - 1; i >= 0; i--) {
880
2.23k
        if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
881
0
            goto err;
882
2.23k
        if (BN_is_bit_set(b, i)) {
883
0
            if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
884
0
                goto err;
885
0
        }
886
2.23k
    }
887
9
    if (!BN_copy(r, u))
888
0
        goto err;
889
9
    bn_check_top(r);
890
9
    ret = 1;
891
9
 err:
892
9
    BN_CTX_end(ctx);
893
9
    return ret;
894
9
}
895
896
/*
897
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
898
 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
899
 * implementation; this wrapper function is only provided for convenience;
900
 * for best performance, use the BN_GF2m_mod_exp_arr function.
901
 */
902
int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
903
                    const BIGNUM *p, BN_CTX *ctx)
904
0
{
905
0
    int ret = 0;
906
0
    const int max = BN_num_bits(p) + 1;
907
0
    int *arr;
908
909
0
    bn_check_top(a);
910
0
    bn_check_top(b);
911
0
    bn_check_top(p);
912
913
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
914
0
    if (arr == NULL)
915
0
        return 0;
916
0
    ret = BN_GF2m_poly2arr(p, arr, max);
917
0
    if (!ret || ret > max) {
918
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
919
0
        goto err;
920
0
    }
921
0
    ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
922
0
    bn_check_top(r);
923
0
 err:
924
0
    OPENSSL_free(arr);
925
0
    return ret;
926
0
}
927
928
/*
929
 * Compute the square root of a, reduce modulo p, and store the result in r.
930
 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
931
 */
932
int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
933
                         BN_CTX *ctx)
934
9
{
935
9
    int ret = 0;
936
9
    BIGNUM *u;
937
938
9
    bn_check_top(a);
939
940
9
    if (p[0] == 0) {
941
        /* reduction mod 1 => return 0 */
942
0
        BN_zero(r);
943
0
        return 1;
944
0
    }
945
946
9
    BN_CTX_start(ctx);
947
9
    if ((u = BN_CTX_get(ctx)) == NULL)
948
0
        goto err;
949
950
9
    if (!BN_set_bit(u, p[0] - 1))
951
0
        goto err;
952
9
    ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
953
9
    bn_check_top(r);
954
955
9
 err:
956
9
    BN_CTX_end(ctx);
957
9
    return ret;
958
9
}
959
960
/*
961
 * Compute the square root of a, reduce modulo p, and store the result in r.
962
 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
963
 * implementation; this wrapper function is only provided for convenience;
964
 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
965
 */
966
int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
967
0
{
968
0
    int ret = 0;
969
0
    const int max = BN_num_bits(p) + 1;
970
0
    int *arr;
971
972
0
    bn_check_top(a);
973
0
    bn_check_top(p);
974
975
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
976
0
    if (arr == NULL)
977
0
        return 0;
978
0
    ret = BN_GF2m_poly2arr(p, arr, max);
979
0
    if (!ret || ret > max) {
980
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
981
0
        goto err;
982
0
    }
983
0
    ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
984
0
    bn_check_top(r);
985
0
 err:
986
0
    OPENSSL_free(arr);
987
0
    return ret;
988
0
}
989
990
/*
991
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
992
 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
993
 */
994
int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
995
                               BN_CTX *ctx)
996
172
{
997
172
    int ret = 0, count = 0, j;
998
172
    BIGNUM *a, *z, *rho, *w, *w2, *tmp;
999
1000
172
    bn_check_top(a_);
1001
1002
172
    if (p[0] == 0) {
1003
        /* reduction mod 1 => return 0 */
1004
0
        BN_zero(r);
1005
0
        return 1;
1006
0
    }
1007
1008
172
    BN_CTX_start(ctx);
1009
172
    a = BN_CTX_get(ctx);
1010
172
    z = BN_CTX_get(ctx);
1011
172
    w = BN_CTX_get(ctx);
1012
172
    if (w == NULL)
1013
0
        goto err;
1014
1015
172
    if (!BN_GF2m_mod_arr(a, a_, p))
1016
0
        goto err;
1017
1018
172
    if (BN_is_zero(a)) {
1019
4
        BN_zero(r);
1020
4
        ret = 1;
1021
4
        goto err;
1022
4
    }
1023
1024
168
    if (p[0] & 0x1) {           /* m is odd */
1025
        /* compute half-trace of a */
1026
139
        if (!BN_copy(z, a))
1027
0
            goto err;
1028
22.6k
        for (j = 1; j <= (p[0] - 1) / 2; j++) {
1029
22.5k
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1030
0
                goto err;
1031
22.5k
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1032
0
                goto err;
1033
22.5k
            if (!BN_GF2m_add(z, z, a))
1034
0
                goto err;
1035
22.5k
        }
1036
1037
139
    } else {                    /* m is even */
1038
1039
29
        rho = BN_CTX_get(ctx);
1040
29
        w2 = BN_CTX_get(ctx);
1041
29
        tmp = BN_CTX_get(ctx);
1042
29
        if (tmp == NULL)
1043
0
            goto err;
1044
568
        do {
1045
568
            if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1046
568
                                 0, ctx))
1047
0
                goto err;
1048
568
            if (!BN_GF2m_mod_arr(rho, rho, p))
1049
0
                goto err;
1050
568
            BN_zero(z);
1051
568
            if (!BN_copy(w, rho))
1052
0
                goto err;
1053
207k
            for (j = 1; j <= p[0] - 1; j++) {
1054
206k
                if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1055
0
                    goto err;
1056
206k
                if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1057
0
                    goto err;
1058
206k
                if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1059
0
                    goto err;
1060
206k
                if (!BN_GF2m_add(z, z, tmp))
1061
0
                    goto err;
1062
206k
                if (!BN_GF2m_add(w, w2, rho))
1063
0
                    goto err;
1064
206k
            }
1065
568
            count++;
1066
568
        } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1067
29
        if (BN_is_zero(w)) {
1068
11
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1069
11
            goto err;
1070
11
        }
1071
29
    }
1072
1073
157
    if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1074
0
        goto err;
1075
157
    if (!BN_GF2m_add(w, z, w))
1076
0
        goto err;
1077
157
    if (BN_GF2m_cmp(w, a)) {
1078
72
        ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1079
72
        goto err;
1080
72
    }
1081
1082
85
    if (!BN_copy(r, z))
1083
0
        goto err;
1084
85
    bn_check_top(r);
1085
1086
85
    ret = 1;
1087
1088
172
 err:
1089
172
    BN_CTX_end(ctx);
1090
172
    return ret;
1091
85
}
1092
1093
/*
1094
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
1095
 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1096
 * implementation; this wrapper function is only provided for convenience;
1097
 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1098
 */
1099
int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1100
                           BN_CTX *ctx)
1101
0
{
1102
0
    int ret = 0;
1103
0
    const int max = BN_num_bits(p) + 1;
1104
0
    int *arr;
1105
1106
0
    bn_check_top(a);
1107
0
    bn_check_top(p);
1108
1109
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
1110
0
    if (arr == NULL)
1111
0
        goto err;
1112
0
    ret = BN_GF2m_poly2arr(p, arr, max);
1113
0
    if (!ret || ret > max) {
1114
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
1115
0
        goto err;
1116
0
    }
1117
0
    ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1118
0
    bn_check_top(r);
1119
0
 err:
1120
0
    OPENSSL_free(arr);
1121
0
    return ret;
1122
0
}
1123
1124
/*
1125
 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1126
 * x^i) into an array of integers corresponding to the bits with non-zero
1127
 * coefficient.  Array is terminated with -1. Up to max elements of the array
1128
 * will be filled.  Return value is total number of array elements that would
1129
 * be filled if array was large enough.
1130
 */
1131
int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1132
1.17k
{
1133
1.17k
    int i, j, k = 0;
1134
1.17k
    BN_ULONG mask;
1135
1136
1.17k
    if (BN_is_zero(a))
1137
0
        return 0;
1138
1139
7.24k
    for (i = a->top - 1; i >= 0; i--) {
1140
6.06k
        if (!a->d[i])
1141
            /* skip word if a->d[i] == 0 */
1142
3.02k
            continue;
1143
3.04k
        mask = BN_TBIT;
1144
197k
        for (j = BN_BITS2 - 1; j >= 0; j--) {
1145
194k
            if (a->d[i] & mask) {
1146
4.36k
                if (k < max)
1147
4.36k
                    p[k] = BN_BITS2 * i + j;
1148
4.36k
                k++;
1149
4.36k
            }
1150
194k
            mask >>= 1;
1151
194k
        }
1152
3.04k
    }
1153
1154
1.17k
    if (k < max) {
1155
1.17k
        p[k] = -1;
1156
1.17k
        k++;
1157
1.17k
    }
1158
1159
1.17k
    return k;
1160
1.17k
}
1161
1162
/*
1163
 * Convert the coefficient array representation of a polynomial to a
1164
 * bit-string.  The array must be terminated by -1.
1165
 */
1166
int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1167
0
{
1168
0
    int i;
1169
1170
0
    bn_check_top(a);
1171
0
    BN_zero(a);
1172
0
    for (i = 0; p[i] != -1; i++) {
1173
0
        if (BN_set_bit(a, p[i]) == 0)
1174
0
            return 0;
1175
0
    }
1176
0
    bn_check_top(a);
1177
1178
0
    return 1;
1179
0
}
1180
1181
#endif