Coverage Report

Created: 2025-12-10 06:24

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