Coverage Report

Created: 2025-08-11 07:04

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