Coverage Report

Created: 2025-06-13 06:58

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