Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/bn/bn_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <assert.h>
11
#include <limits.h>
12
#include "internal/cryptlib.h"
13
#include "bn_local.h"
14
#include <openssl/opensslconf.h>
15
#include "internal/constant_time.h"
16
17
/* This stuff appears to be completely unused, so is deprecated */
18
#if OPENSSL_API_COMPAT < 0x00908000L
19
/*-
20
 * For a 32 bit machine
21
 * 2 -   4 ==  128
22
 * 3 -   8 ==  256
23
 * 4 -  16 ==  512
24
 * 5 -  32 == 1024
25
 * 6 -  64 == 2048
26
 * 7 - 128 == 4096
27
 * 8 - 256 == 8192
28
 */
29
static int bn_limit_bits = 0;
30
static int bn_limit_num = 8;    /* (1<<bn_limit_bits) */
31
static int bn_limit_bits_low = 0;
32
static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
33
static int bn_limit_bits_high = 0;
34
static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
35
static int bn_limit_bits_mont = 0;
36
static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
37
38
void BN_set_params(int mult, int high, int low, int mont)
39
0
{
40
0
    if (mult >= 0) {
41
0
        if (mult > (int)(sizeof(int) * 8) - 1)
42
0
            mult = sizeof(int) * 8 - 1;
43
0
        bn_limit_bits = mult;
44
0
        bn_limit_num = 1 << mult;
45
0
    }
46
0
    if (high >= 0) {
47
0
        if (high > (int)(sizeof(int) * 8) - 1)
48
0
            high = sizeof(int) * 8 - 1;
49
0
        bn_limit_bits_high = high;
50
0
        bn_limit_num_high = 1 << high;
51
0
    }
52
0
    if (low >= 0) {
53
0
        if (low > (int)(sizeof(int) * 8) - 1)
54
0
            low = sizeof(int) * 8 - 1;
55
0
        bn_limit_bits_low = low;
56
0
        bn_limit_num_low = 1 << low;
57
0
    }
58
0
    if (mont >= 0) {
59
0
        if (mont > (int)(sizeof(int) * 8) - 1)
60
0
            mont = sizeof(int) * 8 - 1;
61
0
        bn_limit_bits_mont = mont;
62
0
        bn_limit_num_mont = 1 << mont;
63
0
    }
64
0
}
65
66
int BN_get_params(int which)
67
0
{
68
0
    if (which == 0)
69
0
        return bn_limit_bits;
70
0
    else if (which == 1)
71
0
        return bn_limit_bits_high;
72
0
    else if (which == 2)
73
0
        return bn_limit_bits_low;
74
0
    else if (which == 3)
75
0
        return bn_limit_bits_mont;
76
0
    else
77
0
        return 0;
78
0
}
79
#endif
80
81
const BIGNUM *BN_value_one(void)
82
6.90k
{
83
6.90k
    static const BN_ULONG data_one = 1L;
84
6.90k
    static const BIGNUM const_one =
85
6.90k
        { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
6.90k
    return &const_one;
88
6.90k
}
89
90
/*
91
 * Old Visual Studio ARM compiler miscompiles BN_num_bits_word()
92
 * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html
93
 */
94
#if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \
95
    && _MSC_VER>=1400 && _MSC_VER<1501
96
# define MS_BROKEN_BN_num_bits_word
97
# pragma optimize("", off)
98
#endif
99
int BN_num_bits_word(BN_ULONG l)
100
529k
{
101
529k
    BN_ULONG x, mask;
102
529k
    int bits = (l != 0);
103
104
529k
#if BN_BITS2 > 32
105
529k
    x = l >> 32;
106
529k
    mask = (0 - x) & BN_MASK2;
107
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
529k
    bits += 32 & mask;
109
529k
    l ^= (x ^ l) & mask;
110
529k
#endif
111
112
529k
    x = l >> 16;
113
529k
    mask = (0 - x) & BN_MASK2;
114
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
529k
    bits += 16 & mask;
116
529k
    l ^= (x ^ l) & mask;
117
118
529k
    x = l >> 8;
119
529k
    mask = (0 - x) & BN_MASK2;
120
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
529k
    bits += 8 & mask;
122
529k
    l ^= (x ^ l) & mask;
123
124
529k
    x = l >> 4;
125
529k
    mask = (0 - x) & BN_MASK2;
126
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
529k
    bits += 4 & mask;
128
529k
    l ^= (x ^ l) & mask;
129
130
529k
    x = l >> 2;
131
529k
    mask = (0 - x) & BN_MASK2;
132
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
529k
    bits += 2 & mask;
134
529k
    l ^= (x ^ l) & mask;
135
136
529k
    x = l >> 1;
137
529k
    mask = (0 - x) & BN_MASK2;
138
529k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
529k
    bits += 1 & mask;
140
141
529k
    return bits;
142
529k
}
143
#ifdef MS_BROKEN_BN_num_bits_word
144
# pragma optimize("", on)
145
#endif
146
147
/*
148
 * This function still leaks `a->dmax`: it's caller's responsibility to
149
 * expand the input `a` in advance to a public length.
150
 */
151
static ossl_inline
152
int bn_num_bits_consttime(const BIGNUM *a)
153
10.1k
{
154
10.1k
    int j, ret;
155
10.1k
    unsigned int mask, past_i;
156
10.1k
    int i = a->top - 1;
157
10.1k
    bn_check_top(a);
158
159
46.1k
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
160
36.0k
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
161
162
36.0k
        ret += BN_BITS2 & (~mask & ~past_i);
163
36.0k
        ret += BN_num_bits_word(a->d[j]) & mask;
164
165
36.0k
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
166
36.0k
    }
167
168
    /*
169
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
170
     * final result.
171
     */
172
10.1k
    mask = ~(constant_time_eq_int(i, ((int)-1)));
173
174
10.1k
    return ret & mask;
175
10.1k
}
176
177
int BN_num_bits(const BIGNUM *a)
178
370k
{
179
370k
    int i = a->top - 1;
180
370k
    bn_check_top(a);
181
182
370k
    if (a->flags & BN_FLG_CONSTTIME) {
183
        /*
184
         * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
185
         * so that a->dmax is not leaking secret information.
186
         *
187
         * In other words, it's the caller's responsibility to ensure `a` has
188
         * been preallocated in advance to a public length if we hit this
189
         * branch.
190
         *
191
         */
192
10.1k
        return bn_num_bits_consttime(a);
193
10.1k
    }
194
195
360k
    if (BN_is_zero(a))
196
58
        return 0;
197
198
360k
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
199
360k
}
200
201
static void bn_free_d(BIGNUM *a, int clear)
202
354k
{
203
354k
    if (BN_get_flags(a, BN_FLG_SECURE))
204
2.13k
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
205
352k
    else if (clear != 0)
206
284k
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
207
67.5k
    else
208
67.5k
        OPENSSL_free(a->d);
209
354k
}
210
211
212
void BN_clear_free(BIGNUM *a)
213
179k
{
214
179k
    if (a == NULL)
215
30.3k
        return;
216
149k
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
217
139k
        bn_free_d(a, 1);
218
149k
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
219
18.0k
        OPENSSL_cleanse(a, sizeof(*a));
220
18.0k
        OPENSSL_free(a);
221
18.0k
    }
222
149k
}
223
224
void BN_free(BIGNUM *a)
225
199k
{
226
199k
    if (a == NULL)
227
132k
        return;
228
67.5k
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
229
67.5k
        bn_free_d(a, 0);
230
67.5k
    if (a->flags & BN_FLG_MALLOCED)
231
67.5k
        OPENSSL_free(a);
232
67.5k
}
233
234
void bn_init(BIGNUM *a)
235
276k
{
236
276k
    static BIGNUM nilbn;
237
238
276k
    *a = nilbn;
239
276k
    bn_check_top(a);
240
276k
}
241
242
BIGNUM *BN_new(void)
243
85.5k
{
244
85.5k
    BIGNUM *ret;
245
246
85.5k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
247
0
        BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
248
0
        return NULL;
249
0
    }
250
85.5k
    ret->flags = BN_FLG_MALLOCED;
251
85.5k
    bn_check_top(ret);
252
85.5k
    return ret;
253
85.5k
}
254
255
 BIGNUM *BN_secure_new(void)
256
2.14k
 {
257
2.14k
     BIGNUM *ret = BN_new();
258
2.14k
     if (ret != NULL)
259
2.14k
         ret->flags |= BN_FLG_SECURE;
260
2.14k
     return ret;
261
2.14k
 }
262
263
/* This is used by bn_expand2() */
264
/* The caller MUST check that words > b->dmax before calling this */
265
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
266
351k
{
267
351k
    BN_ULONG *a = NULL;
268
269
351k
    if (words > (INT_MAX / (4 * BN_BITS2))) {
270
0
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
271
0
        return NULL;
272
0
    }
273
351k
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
274
0
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
275
0
        return NULL;
276
0
    }
277
351k
    if (BN_get_flags(b, BN_FLG_SECURE))
278
2.13k
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
279
349k
    else
280
349k
        a = OPENSSL_zalloc(words * sizeof(*a));
281
351k
    if (a == NULL) {
282
0
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
283
0
        return NULL;
284
0
    }
285
286
351k
    assert(b->top <= words);
287
351k
    if (b->top > 0)
288
27.7k
        memcpy(a, b->d, sizeof(*a) * b->top);
289
290
351k
    return a;
291
351k
}
292
293
/*
294
 * This is an internal function that should not be used in applications. It
295
 * ensures that 'b' has enough room for a 'words' word number and initialises
296
 * any unused part of b->d with leading zeros. It is mostly used by the
297
 * various BIGNUM routines. If there is an error, NULL is returned. If not,
298
 * 'b' is returned.
299
 */
300
301
BIGNUM *bn_expand2(BIGNUM *b, int words)
302
351k
{
303
351k
    if (words > b->dmax) {
304
351k
        BN_ULONG *a = bn_expand_internal(b, words);
305
351k
        if (!a)
306
0
            return NULL;
307
351k
        if (b->d != NULL)
308
147k
            bn_free_d(b, 1);
309
351k
        b->d = a;
310
351k
        b->dmax = words;
311
351k
    }
312
313
351k
    return b;
314
351k
}
315
316
BIGNUM *BN_dup(const BIGNUM *a)
317
3.22k
{
318
3.22k
    BIGNUM *t;
319
320
3.22k
    if (a == NULL)
321
0
        return NULL;
322
3.22k
    bn_check_top(a);
323
324
3.22k
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
325
3.22k
    if (t == NULL)
326
0
        return NULL;
327
3.22k
    if (!BN_copy(t, a)) {
328
0
        BN_free(t);
329
0
        return NULL;
330
0
    }
331
3.22k
    bn_check_top(t);
332
3.22k
    return t;
333
3.22k
}
334
335
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
336
222k
{
337
222k
    int bn_words;
338
339
222k
    bn_check_top(b);
340
341
222k
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
342
343
222k
    if (a == b)
344
0
        return a;
345
222k
    if (bn_wexpand(a, bn_words) == NULL)
346
0
        return NULL;
347
348
222k
    if (b->top > 0)
349
218k
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
350
351
222k
    a->neg = b->neg;
352
222k
    a->top = b->top;
353
222k
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
354
222k
    bn_check_top(a);
355
222k
    return a;
356
222k
}
357
358
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
359
0
                                    | BN_FLG_CONSTTIME   \
360
0
                                    | BN_FLG_SECURE      \
361
0
                                    | BN_FLG_FIXED_TOP))
362
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
363
364
void BN_swap(BIGNUM *a, BIGNUM *b)
365
0
{
366
0
    int flags_old_a, flags_old_b;
367
0
    BN_ULONG *tmp_d;
368
0
    int tmp_top, tmp_dmax, tmp_neg;
369
370
0
    bn_check_top(a);
371
0
    bn_check_top(b);
372
373
0
    flags_old_a = a->flags;
374
0
    flags_old_b = b->flags;
375
376
0
    tmp_d = a->d;
377
0
    tmp_top = a->top;
378
0
    tmp_dmax = a->dmax;
379
0
    tmp_neg = a->neg;
380
381
0
    a->d = b->d;
382
0
    a->top = b->top;
383
0
    a->dmax = b->dmax;
384
0
    a->neg = b->neg;
385
386
0
    b->d = tmp_d;
387
0
    b->top = tmp_top;
388
0
    b->dmax = tmp_dmax;
389
0
    b->neg = tmp_neg;
390
391
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
392
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
393
0
    bn_check_top(a);
394
0
    bn_check_top(b);
395
0
}
396
397
void BN_clear(BIGNUM *a)
398
0
{
399
0
    if (a == NULL)
400
0
        return;
401
0
    bn_check_top(a);
402
0
    if (a->d != NULL)
403
0
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
404
0
    a->neg = 0;
405
0
    a->top = 0;
406
0
    a->flags &= ~BN_FLG_FIXED_TOP;
407
0
}
408
409
BN_ULONG BN_get_word(const BIGNUM *a)
410
0
{
411
0
    if (a->top > 1)
412
0
        return BN_MASK2;
413
0
    else if (a->top == 1)
414
0
        return a->d[0];
415
    /* a->top == 0 */
416
0
    return 0;
417
0
}
418
419
int BN_set_word(BIGNUM *a, BN_ULONG w)
420
3.30M
{
421
3.30M
    bn_check_top(a);
422
3.30M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
423
0
        return 0;
424
3.30M
    a->neg = 0;
425
3.30M
    a->d[0] = w;
426
3.30M
    a->top = (w ? 1 : 0);
427
3.30M
    a->flags &= ~BN_FLG_FIXED_TOP;
428
3.30M
    bn_check_top(a);
429
3.30M
    return 1;
430
3.30M
}
431
432
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
433
32.5k
{
434
32.5k
    unsigned int i, m;
435
32.5k
    unsigned int n;
436
32.5k
    BN_ULONG l;
437
32.5k
    BIGNUM *bn = NULL;
438
439
32.5k
    if (ret == NULL)
440
18.1k
        ret = bn = BN_new();
441
32.5k
    if (ret == NULL)
442
0
        return NULL;
443
32.5k
    bn_check_top(ret);
444
    /* Skip leading zero's. */
445
176k
    for ( ; len > 0 && *s == 0; s++, len--)
446
143k
        continue;
447
32.5k
    n = len;
448
32.5k
    if (n == 0) {
449
600
        ret->top = 0;
450
600
        return ret;
451
600
    }
452
31.9k
    i = ((n - 1) / BN_BYTES) + 1;
453
31.9k
    m = ((n - 1) % (BN_BYTES));
454
31.9k
    if (bn_wexpand(ret, (int)i) == NULL) {
455
0
        BN_free(bn);
456
0
        return NULL;
457
0
    }
458
31.9k
    ret->top = i;
459
31.9k
    ret->neg = 0;
460
31.9k
    l = 0;
461
2.25M
    while (n--) {
462
2.22M
        l = (l << 8L) | *(s++);
463
2.22M
        if (m-- == 0) {
464
288k
            ret->d[--i] = l;
465
288k
            l = 0;
466
288k
            m = BN_BYTES - 1;
467
288k
        }
468
2.22M
    }
469
    /*
470
     * need to call this due to clear byte at top if avoiding having the top
471
     * bit set (-ve number)
472
     */
473
31.9k
    bn_correct_top(ret);
474
31.9k
    return ret;
475
31.9k
}
476
477
typedef enum {big, little} endianess_t;
478
479
/* ignore negative */
480
static
481
int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess)
482
4.31k
{
483
4.31k
    int n;
484
4.31k
    size_t i, lasti, j, atop, mask;
485
4.31k
    BN_ULONG l;
486
487
    /*
488
     * In case |a| is fixed-top, BN_num_bytes can return bogus length,
489
     * but it's assumed that fixed-top inputs ought to be "nominated"
490
     * even for padded output, so it works out...
491
     */
492
4.31k
    n = BN_num_bytes(a);
493
4.31k
    if (tolen == -1) {
494
2.34k
        tolen = n;
495
2.34k
    } else if (tolen < n) {     /* uncommon/unlike case */
496
0
        BIGNUM temp = *a;
497
498
0
        bn_correct_top(&temp);
499
0
        n = BN_num_bytes(&temp);
500
0
        if (tolen < n)
501
0
            return -1;
502
0
    }
503
504
    /* Swipe through whole available data and don't give away padded zero. */
505
4.31k
    atop = a->dmax * BN_BYTES;
506
4.31k
    if (atop == 0) {
507
9
        OPENSSL_cleanse(to, tolen);
508
9
        return tolen;
509
9
    }
510
511
4.30k
    lasti = atop - 1;
512
4.30k
    atop = a->top * BN_BYTES;
513
4.30k
    if (endianess == big)
514
4.26k
        to += tolen; /* start from the end of the buffer */
515
566k
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
516
562k
        unsigned char val;
517
562k
        l = a->d[i / BN_BYTES];
518
562k
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
519
562k
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
520
562k
        if (endianess == big)
521
559k
            *--to = val;
522
3.03k
        else
523
3.03k
            *to++ = val;
524
562k
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
525
562k
    }
526
527
4.30k
    return tolen;
528
4.31k
}
529
530
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
531
1.92k
{
532
1.92k
    if (tolen < 0)
533
0
        return -1;
534
1.92k
    return bn2binpad(a, to, tolen, big);
535
1.92k
}
536
537
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
538
2.34k
{
539
2.34k
    return bn2binpad(a, to, -1, big);
540
2.34k
}
541
542
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
543
62
{
544
62
    unsigned int i, m;
545
62
    unsigned int n;
546
62
    BN_ULONG l;
547
62
    BIGNUM *bn = NULL;
548
549
62
    if (ret == NULL)
550
0
        ret = bn = BN_new();
551
62
    if (ret == NULL)
552
0
        return NULL;
553
62
    bn_check_top(ret);
554
62
    s += len;
555
    /* Skip trailing zeroes. */
556
1.00k
    for ( ; len > 0 && s[-1] == 0; s--, len--)
557
943
        continue;
558
62
    n = len;
559
62
    if (n == 0) {
560
6
        ret->top = 0;
561
6
        return ret;
562
6
    }
563
56
    i = ((n - 1) / BN_BYTES) + 1;
564
56
    m = ((n - 1) % (BN_BYTES));
565
56
    if (bn_wexpand(ret, (int)i) == NULL) {
566
0
        BN_free(bn);
567
0
        return NULL;
568
0
    }
569
56
    ret->top = i;
570
56
    ret->neg = 0;
571
56
    l = 0;
572
3.20k
    while (n--) {
573
3.14k
        s--;
574
3.14k
        l = (l << 8L) | *s;
575
3.14k
        if (m-- == 0) {
576
438
            ret->d[--i] = l;
577
438
            l = 0;
578
438
            m = BN_BYTES - 1;
579
438
        }
580
3.14k
    }
581
    /*
582
     * need to call this due to clear byte at top if avoiding having the top
583
     * bit set (-ve number)
584
     */
585
56
    bn_correct_top(ret);
586
56
    return ret;
587
56
}
588
589
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
590
50
{
591
50
    if (tolen < 0)
592
0
        return -1;
593
50
    return bn2binpad(a, to, tolen, little);
594
50
}
595
596
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
597
817k
{
598
817k
    int i;
599
817k
    BN_ULONG t1, t2, *ap, *bp;
600
601
817k
    bn_check_top(a);
602
817k
    bn_check_top(b);
603
604
817k
    i = a->top - b->top;
605
817k
    if (i != 0)
606
172k
        return i;
607
645k
    ap = a->d;
608
645k
    bp = b->d;
609
661k
    for (i = a->top - 1; i >= 0; i--) {
610
654k
        t1 = ap[i];
611
654k
        t2 = bp[i];
612
654k
        if (t1 != t2)
613
637k
            return ((t1 > t2) ? 1 : -1);
614
654k
    }
615
7.67k
    return 0;
616
645k
}
617
618
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
619
288k
{
620
288k
    int i;
621
288k
    int gt, lt;
622
288k
    BN_ULONG t1, t2;
623
624
288k
    if ((a == NULL) || (b == NULL)) {
625
0
        if (a != NULL)
626
0
            return -1;
627
0
        else if (b != NULL)
628
0
            return 1;
629
0
        else
630
0
            return 0;
631
0
    }
632
633
288k
    bn_check_top(a);
634
288k
    bn_check_top(b);
635
636
288k
    if (a->neg != b->neg) {
637
65
        if (a->neg)
638
0
            return -1;
639
65
        else
640
65
            return 1;
641
65
    }
642
288k
    if (a->neg == 0) {
643
288k
        gt = 1;
644
288k
        lt = -1;
645
288k
    } else {
646
0
        gt = -1;
647
0
        lt = 1;
648
0
    }
649
650
288k
    if (a->top > b->top)
651
157k
        return gt;
652
130k
    if (a->top < b->top)
653
2.62k
        return lt;
654
169k
    for (i = a->top - 1; i >= 0; i--) {
655
164k
        t1 = a->d[i];
656
164k
        t2 = b->d[i];
657
164k
        if (t1 > t2)
658
84
            return gt;
659
164k
        if (t1 < t2)
660
123k
            return lt;
661
164k
    }
662
4.63k
    return 0;
663
127k
}
664
665
int BN_set_bit(BIGNUM *a, int n)
666
11.5k
{
667
11.5k
    int i, j, k;
668
669
11.5k
    if (n < 0)
670
0
        return 0;
671
672
11.5k
    i = n / BN_BITS2;
673
11.5k
    j = n % BN_BITS2;
674
11.5k
    if (a->top <= i) {
675
11.5k
        if (bn_wexpand(a, i + 1) == NULL)
676
0
            return 0;
677
160k
        for (k = a->top; k < i + 1; k++)
678
149k
            a->d[k] = 0;
679
11.5k
        a->top = i + 1;
680
11.5k
        a->flags &= ~BN_FLG_FIXED_TOP;
681
11.5k
    }
682
683
11.5k
    a->d[i] |= (((BN_ULONG)1) << j);
684
11.5k
    bn_check_top(a);
685
11.5k
    return 1;
686
11.5k
}
687
688
int BN_clear_bit(BIGNUM *a, int n)
689
157
{
690
157
    int i, j;
691
692
157
    bn_check_top(a);
693
157
    if (n < 0)
694
0
        return 0;
695
696
157
    i = n / BN_BITS2;
697
157
    j = n % BN_BITS2;
698
157
    if (a->top <= i)
699
0
        return 0;
700
701
157
    a->d[i] &= (~(((BN_ULONG)1) << j));
702
157
    bn_correct_top(a);
703
157
    return 1;
704
157
}
705
706
int BN_is_bit_set(const BIGNUM *a, int n)
707
1.33M
{
708
1.33M
    int i, j;
709
710
1.33M
    bn_check_top(a);
711
1.33M
    if (n < 0)
712
747
        return 0;
713
1.33M
    i = n / BN_BITS2;
714
1.33M
    j = n % BN_BITS2;
715
1.33M
    if (a->top <= i)
716
1.03k
        return 0;
717
1.33M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
718
1.33M
}
719
720
int BN_mask_bits(BIGNUM *a, int n)
721
0
{
722
0
    int b, w;
723
724
0
    bn_check_top(a);
725
0
    if (n < 0)
726
0
        return 0;
727
728
0
    w = n / BN_BITS2;
729
0
    b = n % BN_BITS2;
730
0
    if (w >= a->top)
731
0
        return 0;
732
0
    if (b == 0)
733
0
        a->top = w;
734
0
    else {
735
0
        a->top = w + 1;
736
0
        a->d[w] &= ~(BN_MASK2 << b);
737
0
    }
738
0
    bn_correct_top(a);
739
0
    return 1;
740
0
}
741
742
void BN_set_negative(BIGNUM *a, int b)
743
4.23k
{
744
4.23k
    if (b && !BN_is_zero(a))
745
149
        a->neg = 1;
746
4.08k
    else
747
4.08k
        a->neg = 0;
748
4.23k
}
749
750
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
751
96.1k
{
752
96.1k
    int i;
753
96.1k
    BN_ULONG aa, bb;
754
755
96.1k
    if (n == 0)
756
0
        return 0;
757
758
96.1k
    aa = a[n - 1];
759
96.1k
    bb = b[n - 1];
760
96.1k
    if (aa != bb)
761
87.8k
        return ((aa > bb) ? 1 : -1);
762
31.2k
    for (i = n - 2; i >= 0; i--) {
763
29.6k
        aa = a[i];
764
29.6k
        bb = b[i];
765
29.6k
        if (aa != bb)
766
6.74k
            return ((aa > bb) ? 1 : -1);
767
29.6k
    }
768
1.62k
    return 0;
769
8.36k
}
770
771
/*
772
 * Here follows a specialised variants of bn_cmp_words().  It has the
773
 * capability of performing the operation on arrays of different sizes. The
774
 * sizes of those arrays is expressed through cl, which is the common length
775
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
776
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
777
 * BN_ULONGs...
778
 */
779
780
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
781
92.2k
{
782
92.2k
    int n, i;
783
92.2k
    n = cl - 1;
784
785
92.2k
    if (dl < 0) {
786
15.2k
        for (i = dl; i < 0; i++) {
787
13.8k
            if (b[n - i] != 0)
788
10.0k
                return -1;      /* a < b */
789
13.8k
        }
790
11.4k
    }
791
82.2k
    if (dl > 0) {
792
11.7k
        for (i = dl; i > 0; i--) {
793
11.2k
            if (a[n + i] != 0)
794
9.25k
                return 1;       /* a > b */
795
11.2k
        }
796
9.77k
    }
797
72.9k
    return bn_cmp_words(a, b, cl);
798
82.2k
}
799
800
/*-
801
 * Constant-time conditional swap of a and b.
802
 * a and b are swapped if condition is not 0.
803
 * nwords is the number of words to swap.
804
 * Assumes that at least nwords are allocated in both a and b.
805
 * Assumes that no more than nwords are used by either a or b.
806
 */
807
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
808
10.4k
{
809
10.4k
    BN_ULONG t;
810
10.4k
    int i;
811
812
10.4k
    if (a == b)
813
0
        return;
814
815
10.4k
    bn_wcheck_size(a, nwords);
816
10.4k
    bn_wcheck_size(b, nwords);
817
818
10.4k
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
819
820
10.4k
    t = (a->top ^ b->top) & condition;
821
10.4k
    a->top ^= t;
822
10.4k
    b->top ^= t;
823
824
10.4k
    t = (a->neg ^ b->neg) & condition;
825
10.4k
    a->neg ^= t;
826
10.4k
    b->neg ^= t;
827
828
    /*-
829
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
830
     * is actually to treat it as it's read-only data, and some (if not most)
831
     * of it does reside in read-only segment. In other words observation of
832
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
833
     * condition. It would either cause SEGV or effectively cause data
834
     * corruption.
835
     *
836
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
837
     * preserved.
838
     *
839
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
840
     * allocated and hence how to free it.
841
     *
842
     * BN_FLG_CONSTTIME: sufficient to mask and swap
843
     *
844
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
845
     * the data, so the d array may be padded with additional 0 values (i.e.
846
     * top could be greater than the minimal value that it could be). We should
847
     * be swapping it
848
     */
849
850
10.4k
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
851
852
10.4k
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
853
10.4k
    a->flags ^= t;
854
10.4k
    b->flags ^= t;
855
856
    /* conditionally swap the data */
857
72.8k
    for (i = 0; i < nwords; i++) {
858
62.4k
        t = (a->d[i] ^ b->d[i]) & condition;
859
62.4k
        a->d[i] ^= t;
860
62.4k
        b->d[i] ^= t;
861
62.4k
    }
862
10.4k
}
863
864
#undef BN_CONSTTIME_SWAP_FLAGS
865
866
/* Bits of security, see SP800-57 */
867
868
int BN_security_bits(int L, int N)
869
1.58k
{
870
1.58k
    int secbits, bits;
871
1.58k
    if (L >= 15360)
872
4
        secbits = 256;
873
1.57k
    else if (L >= 7680)
874
2
        secbits = 192;
875
1.57k
    else if (L >= 3072)
876
21
        secbits = 128;
877
1.55k
    else if (L >= 2048)
878
197
        secbits = 112;
879
1.35k
    else if (L >= 1024)
880
1.18k
        secbits = 80;
881
169
    else
882
169
        return 0;
883
1.41k
    if (N == -1)
884
1.41k
        return secbits;
885
0
    bits = N / 2;
886
0
    if (bits < 80)
887
0
        return 0;
888
0
    return bits >= secbits ? secbits : bits;
889
0
}
890
891
void BN_zero_ex(BIGNUM *a)
892
0
{
893
0
    a->neg = 0;
894
0
    a->top = 0;
895
0
    a->flags &= ~BN_FLG_FIXED_TOP;
896
0
}
897
898
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
899
26.2k
{
900
26.2k
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
901
26.2k
}
902
903
int BN_is_zero(const BIGNUM *a)
904
1.24M
{
905
1.24M
    return a->top == 0;
906
1.24M
}
907
908
int BN_is_one(const BIGNUM *a)
909
17.5k
{
910
17.5k
    return BN_abs_is_word(a, 1) && !a->neg;
911
17.5k
}
912
913
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
914
2.32k
{
915
2.32k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
916
2.32k
}
917
918
int BN_is_odd(const BIGNUM *a)
919
347k
{
920
347k
    return (a->top > 0) && (a->d[0] & 1);
921
347k
}
922
923
int BN_is_negative(const BIGNUM *a)
924
10.9k
{
925
10.9k
    return (a->neg != 0);
926
10.9k
}
927
928
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
929
                     BN_CTX *ctx)
930
8.14k
{
931
8.14k
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
932
8.14k
}
933
934
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
935
37.6k
{
936
37.6k
    dest->d = b->d;
937
37.6k
    dest->top = b->top;
938
37.6k
    dest->dmax = b->dmax;
939
37.6k
    dest->neg = b->neg;
940
37.6k
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
941
37.6k
                   | (b->flags & ~BN_FLG_MALLOCED)
942
37.6k
                   | BN_FLG_STATIC_DATA | flags);
943
37.6k
}
944
945
BN_GENCB *BN_GENCB_new(void)
946
0
{
947
0
    BN_GENCB *ret;
948
949
0
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
950
0
        BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
951
0
        return NULL;
952
0
    }
953
954
0
    return ret;
955
0
}
956
957
void BN_GENCB_free(BN_GENCB *cb)
958
0
{
959
0
    if (cb == NULL)
960
0
        return;
961
0
    OPENSSL_free(cb);
962
0
}
963
964
void BN_set_flags(BIGNUM *b, int n)
965
2.24k
{
966
2.24k
    b->flags |= n;
967
2.24k
}
968
969
int BN_get_flags(const BIGNUM *b, int n)
970
1.67M
{
971
1.67M
    return b->flags & n;
972
1.67M
}
973
974
/* Populate a BN_GENCB structure with an "old"-style callback */
975
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
976
                      void *cb_arg)
977
0
{
978
0
    BN_GENCB *tmp_gencb = gencb;
979
0
    tmp_gencb->ver = 1;
980
0
    tmp_gencb->arg = cb_arg;
981
0
    tmp_gencb->cb.cb_1 = callback;
982
0
}
983
984
/* Populate a BN_GENCB structure with a "new"-style callback */
985
void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
986
                  void *cb_arg)
987
0
{
988
0
    BN_GENCB *tmp_gencb = gencb;
989
0
    tmp_gencb->ver = 2;
990
0
    tmp_gencb->arg = cb_arg;
991
0
    tmp_gencb->cb.cb_2 = callback;
992
0
}
993
994
void *BN_GENCB_get_arg(BN_GENCB *cb)
995
0
{
996
0
    return cb->arg;
997
0
}
998
999
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1000
10.1M
{
1001
10.1M
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1002
10.1M
}
1003
1004
void bn_correct_top_consttime(BIGNUM *a)
1005
0
{
1006
0
    int j, atop;
1007
0
    BN_ULONG limb;
1008
0
    unsigned int mask;
1009
1010
0
    for (j = 0, atop = 0; j < a->dmax; j++) {
1011
0
        limb = a->d[j];
1012
0
        limb |= 0 - limb;
1013
0
        limb >>= BN_BITS2 - 1;
1014
0
        limb = 0 - limb;
1015
0
        mask = (unsigned int)limb;
1016
0
        mask &= constant_time_msb(j - a->top);
1017
0
        atop = constant_time_select_int(mask, j + 1, atop);
1018
0
    }
1019
1020
0
    mask = constant_time_eq_int(atop, 0);
1021
0
    a->top = atop;
1022
0
    a->neg = constant_time_select_int(mask, 0, a->neg);
1023
0
    a->flags &= ~BN_FLG_FIXED_TOP;
1024
0
}
1025
1026
void bn_correct_top(BIGNUM *a)
1027
8.17M
{
1028
8.17M
    BN_ULONG *ftl;
1029
8.17M
    int tmp_top = a->top;
1030
1031
8.17M
    if (tmp_top > 0) {
1032
27.3M
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1033
27.3M
            ftl--;
1034
27.3M
            if (*ftl != 0)
1035
8.16M
                break;
1036
27.3M
        }
1037
8.17M
        a->top = tmp_top;
1038
8.17M
    }
1039
8.17M
    if (a->top == 0)
1040
10.0k
        a->neg = 0;
1041
8.17M
    a->flags &= ~BN_FLG_FIXED_TOP;
1042
8.17M
    bn_pollute(a);
1043
8.17M
}