Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_lib.c
Line
Count
Source
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
2.08M
{
84
2.08M
    static const BN_ULONG data_one = 1L;
85
2.08M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
2.08M
    return &const_one;
88
2.08M
}
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
253M
{
101
253M
    BN_ULONG x, mask;
102
253M
    int bits = (l != 0);
103
104
253M
#if BN_BITS2 > 32
105
253M
    x = l >> 32;
106
253M
    mask = (0 - x) & BN_MASK2;
107
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
253M
    bits += 32 & mask;
109
253M
    l ^= (x ^ l) & mask;
110
253M
#endif
111
112
253M
    x = l >> 16;
113
253M
    mask = (0 - x) & BN_MASK2;
114
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
253M
    bits += 16 & mask;
116
253M
    l ^= (x ^ l) & mask;
117
118
253M
    x = l >> 8;
119
253M
    mask = (0 - x) & BN_MASK2;
120
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
253M
    bits += 8 & mask;
122
253M
    l ^= (x ^ l) & mask;
123
124
253M
    x = l >> 4;
125
253M
    mask = (0 - x) & BN_MASK2;
126
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
253M
    bits += 4 & mask;
128
253M
    l ^= (x ^ l) & mask;
129
130
253M
    x = l >> 2;
131
253M
    mask = (0 - x) & BN_MASK2;
132
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
253M
    bits += 2 & mask;
134
253M
    l ^= (x ^ l) & mask;
135
136
253M
    x = l >> 1;
137
253M
    mask = (0 - x) & BN_MASK2;
138
253M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
253M
    bits += 1 & mask;
140
141
253M
    return bits;
142
253M
}
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 int bn_num_bits_consttime(const BIGNUM *a)
152
8.10M
{
153
8.10M
    int j, ret;
154
8.10M
    unsigned int mask, past_i;
155
8.10M
    int i = a->top - 1;
156
8.10M
    bn_check_top(a);
157
158
68.3M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
60.2M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
60.2M
        ret += BN_BITS2 & (~mask & ~past_i);
162
60.2M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
60.2M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
60.2M
    }
166
167
    /*
168
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
169
     * final result.
170
     */
171
8.10M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
8.10M
    return ret & mask;
174
8.10M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
33.2M
{
178
33.2M
    int i = a->top - 1;
179
33.2M
    bn_check_top(a);
180
181
33.2M
    if (a->flags & BN_FLG_CONSTTIME) {
182
        /*
183
         * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
184
         * so that a->dmax is not leaking secret information.
185
         *
186
         * In other words, it's the caller's responsibility to ensure `a` has
187
         * been preallocated in advance to a public length if we hit this
188
         * branch.
189
         *
190
         */
191
6.08M
        return bn_num_bits_consttime(a);
192
6.08M
    }
193
194
27.1M
    if (BN_is_zero(a))
195
517k
        return 0;
196
197
26.6M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
27.1M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
86.2M
{
202
86.2M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.71M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
84.5M
    else if (clear != 0)
205
50.5M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
33.9M
    else
207
33.9M
        OPENSSL_free(a->d);
208
86.2M
}
209
210
void BN_clear_free(BIGNUM *a)
211
41.2M
{
212
41.2M
    if (a == NULL)
213
6.17M
        return;
214
35.1M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
31.7M
        bn_free_d(a, 1);
216
35.1M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
3.26M
        OPENSSL_cleanse(a, sizeof(*a));
218
3.26M
        OPENSSL_free(a);
219
3.26M
    }
220
35.1M
}
221
222
void BN_free(BIGNUM *a)
223
54.0M
{
224
54.0M
    if (a == NULL)
225
20.0M
        return;
226
34.0M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
33.9M
        bn_free_d(a, 0);
228
34.0M
    if (a->flags & BN_FLG_MALLOCED)
229
33.9M
        OPENSSL_free(a);
230
34.0M
}
231
232
void bn_init(BIGNUM *a)
233
78.5M
{
234
78.5M
    static BIGNUM nilbn;
235
236
78.5M
    *a = nilbn;
237
78.5M
    bn_check_top(a);
238
78.5M
}
239
240
BIGNUM *BN_new(void)
241
37.2M
{
242
37.2M
    BIGNUM *ret;
243
244
37.2M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
245
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
246
0
        return NULL;
247
0
    }
248
37.2M
    ret->flags = BN_FLG_MALLOCED;
249
37.2M
    bn_check_top(ret);
250
37.2M
    return ret;
251
37.2M
}
252
253
BIGNUM *BN_secure_new(void)
254
1.92M
{
255
1.92M
    BIGNUM *ret = BN_new();
256
1.92M
    if (ret != NULL)
257
1.92M
        ret->flags |= BN_FLG_SECURE;
258
1.92M
    return ret;
259
1.92M
}
260
261
/* This is used by bn_expand2() */
262
/* The caller MUST check that words > b->dmax before calling this */
263
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
264
57.7M
{
265
57.7M
    BN_ULONG *a = NULL;
266
267
57.7M
    if (words > (INT_MAX / (4 * BN_BITS2))) {
268
0
        ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG);
269
0
        return NULL;
270
0
    }
271
57.7M
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
272
0
        ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
273
0
        return NULL;
274
0
    }
275
57.7M
    if (BN_get_flags(b, BN_FLG_SECURE))
276
1.24M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
277
56.4M
    else
278
56.4M
        a = OPENSSL_zalloc(words * sizeof(*a));
279
57.7M
    if (a == NULL) {
280
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
281
0
        return NULL;
282
0
    }
283
284
57.7M
    assert(b->top <= words);
285
57.7M
    if (b->top > 0)
286
7.48M
        memcpy(a, b->d, sizeof(*a) * b->top);
287
288
57.7M
    return a;
289
57.7M
}
290
291
/*
292
 * This is an internal function that should not be used in applications. It
293
 * ensures that 'b' has enough room for a 'words' word number and initialises
294
 * any unused part of b->d with leading zeros. It is mostly used by the
295
 * various BIGNUM routines. If there is an error, NULL is returned. If not,
296
 * 'b' is returned.
297
 */
298
299
BIGNUM *bn_expand2(BIGNUM *b, int words)
300
57.7M
{
301
57.7M
    if (words > b->dmax) {
302
57.7M
        BN_ULONG *a = bn_expand_internal(b, words);
303
57.7M
        if (!a)
304
0
            return NULL;
305
57.7M
        if (b->d != NULL)
306
14.5M
            bn_free_d(b, 1);
307
57.7M
        b->d = a;
308
57.7M
        b->dmax = words;
309
57.7M
    }
310
311
57.7M
    return b;
312
57.7M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
4.05M
{
316
4.05M
    BIGNUM *t;
317
318
4.05M
    if (a == NULL)
319
0
        return NULL;
320
4.05M
    bn_check_top(a);
321
322
4.05M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
4.05M
    if (t == NULL)
324
0
        return NULL;
325
4.05M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
4.05M
    bn_check_top(t);
330
4.05M
    return t;
331
4.05M
}
332
333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
334
119M
{
335
119M
    int bn_words;
336
337
119M
    bn_check_top(b);
338
339
119M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
340
341
119M
    if (a == b)
342
9
        return a;
343
119M
    if (bn_wexpand(a, bn_words) == NULL)
344
0
        return NULL;
345
346
119M
    if (b->top > 0)
347
118M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
348
349
119M
    a->neg = b->neg;
350
119M
    a->top = b->top;
351
119M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
352
119M
    bn_check_top(a);
353
119M
    return a;
354
119M
}
355
356
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA | BN_FLG_CONSTTIME | BN_FLG_SECURE | BN_FLG_FIXED_TOP))
357
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
358
359
void BN_swap(BIGNUM *a, BIGNUM *b)
360
0
{
361
0
    int flags_old_a, flags_old_b;
362
0
    BN_ULONG *tmp_d;
363
0
    int tmp_top, tmp_dmax, tmp_neg;
364
365
0
    bn_check_top(a);
366
0
    bn_check_top(b);
367
368
0
    flags_old_a = a->flags;
369
0
    flags_old_b = b->flags;
370
371
0
    tmp_d = a->d;
372
0
    tmp_top = a->top;
373
0
    tmp_dmax = a->dmax;
374
0
    tmp_neg = a->neg;
375
376
0
    a->d = b->d;
377
0
    a->top = b->top;
378
0
    a->dmax = b->dmax;
379
0
    a->neg = b->neg;
380
381
0
    b->d = tmp_d;
382
0
    b->top = tmp_top;
383
0
    b->dmax = tmp_dmax;
384
0
    b->neg = tmp_neg;
385
386
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
387
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
388
0
    bn_check_top(a);
389
0
    bn_check_top(b);
390
0
}
391
392
void BN_clear(BIGNUM *a)
393
421k
{
394
421k
    if (a == NULL)
395
0
        return;
396
421k
    bn_check_top(a);
397
421k
    if (a->d != NULL)
398
44.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
421k
    a->neg = 0;
400
421k
    a->top = 0;
401
421k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
421k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
406
{
406
406
    if (a->top > 1)
407
12
        return BN_MASK2;
408
394
    else if (a->top == 1)
409
340
        return a->d[0];
410
    /* a->top == 0 */
411
54
    return 0;
412
406
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.43M
{
416
3.43M
    bn_check_top(a);
417
3.43M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.43M
    a->neg = 0;
420
3.43M
    a->d[0] = w;
421
3.43M
    a->top = (w ? 1 : 0);
422
3.43M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.43M
    bn_check_top(a);
424
3.43M
    return 1;
425
3.43M
}
426
427
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
428
1.65M
{
429
1.65M
    unsigned int i, m;
430
1.65M
    unsigned int n;
431
1.65M
    BN_ULONG l;
432
1.65M
    BIGNUM *bn = NULL;
433
434
1.65M
    if (ret == NULL)
435
1.01M
        ret = bn = BN_new();
436
1.65M
    if (ret == NULL)
437
0
        return NULL;
438
1.65M
    bn_check_top(ret);
439
    /* Skip leading zero's. */
440
3.88M
    for (; len > 0 && *s == 0; s++, len--)
441
2.22M
        continue;
442
1.65M
    n = len;
443
1.65M
    if (n == 0) {
444
233k
        ret->top = 0;
445
233k
        return ret;
446
233k
    }
447
1.42M
    i = ((n - 1) / BN_BYTES) + 1;
448
1.42M
    m = ((n - 1) % (BN_BYTES));
449
1.42M
    if (bn_wexpand(ret, (int)i) == NULL) {
450
0
        BN_free(bn);
451
0
        return NULL;
452
0
    }
453
1.42M
    ret->top = i;
454
1.42M
    ret->neg = 0;
455
1.42M
    l = 0;
456
96.4M
    while (n--) {
457
94.9M
        l = (l << 8L) | *(s++);
458
94.9M
        if (m-- == 0) {
459
12.5M
            ret->d[--i] = l;
460
12.5M
            l = 0;
461
12.5M
            m = BN_BYTES - 1;
462
12.5M
        }
463
94.9M
    }
464
    /*
465
     * need to call this due to clear byte at top if avoiding having the top
466
     * bit set (-ve number)
467
     */
468
1.42M
    bn_correct_top(ret);
469
1.42M
    return ret;
470
1.42M
}
471
472
typedef enum { big,
473
    little } endianess_t;
474
475
/* ignore negative */
476
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess)
477
290k
{
478
290k
    int n;
479
290k
    size_t i, lasti, j, atop, mask;
480
290k
    BN_ULONG l;
481
482
    /*
483
     * In case |a| is fixed-top, BN_num_bytes can return bogus length,
484
     * but it's assumed that fixed-top inputs ought to be "nominated"
485
     * even for padded output, so it works out...
486
     */
487
290k
    n = BN_num_bytes(a);
488
290k
    if (tolen == -1) {
489
156k
        tolen = n;
490
156k
    } else if (tolen < n) { /* uncommon/unlike case */
491
744
        BIGNUM temp = *a;
492
493
744
        bn_correct_top(&temp);
494
744
        n = BN_num_bytes(&temp);
495
744
        if (tolen < n)
496
744
            return -1;
497
744
    }
498
499
    /* Swipe through whole available data and don't give away padded zero. */
500
289k
    atop = a->dmax * BN_BYTES;
501
289k
    if (atop == 0) {
502
51.5k
        if (tolen != 0)
503
839
            memset(to, '\0', tolen);
504
51.5k
        return tolen;
505
51.5k
    }
506
507
237k
    lasti = atop - 1;
508
237k
    atop = a->top * BN_BYTES;
509
237k
    if (endianess == big)
510
139k
        to += tolen; /* start from the end of the buffer */
511
20.2M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
512
20.0M
        unsigned char val;
513
20.0M
        l = a->d[i / BN_BYTES];
514
20.0M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
515
20.0M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
516
20.0M
        if (endianess == big)
517
9.95M
            *--to = val;
518
10.0M
        else
519
10.0M
            *to++ = val;
520
20.0M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
521
20.0M
    }
522
523
237k
    return tolen;
524
289k
}
525
526
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
527
314k
{
528
314k
    if (tolen < 0)
529
0
        return -1;
530
314k
    return bn2binpad(a, to, tolen, big);
531
314k
}
532
533
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
534
744k
{
535
744k
    return bn2binpad(a, to, -1, big);
536
744k
}
537
538
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
98.6k
{
540
98.6k
    unsigned int i, m;
541
98.6k
    unsigned int n;
542
98.6k
    BN_ULONG l;
543
98.6k
    BIGNUM *bn = NULL;
544
545
98.6k
    if (ret == NULL)
546
86.4k
        ret = bn = BN_new();
547
98.6k
    if (ret == NULL)
548
0
        return NULL;
549
98.6k
    bn_check_top(ret);
550
98.6k
    s += len;
551
    /* Skip trailing zeroes. */
552
115k
    for (; len > 0 && s[-1] == 0; s--, len--)
553
17.2k
        continue;
554
98.6k
    n = len;
555
98.6k
    if (n == 0) {
556
282
        ret->top = 0;
557
282
        return ret;
558
282
    }
559
98.3k
    i = ((n - 1) / BN_BYTES) + 1;
560
98.3k
    m = ((n - 1) % (BN_BYTES));
561
98.3k
    if (bn_wexpand(ret, (int)i) == NULL) {
562
0
        BN_free(bn);
563
0
        return NULL;
564
0
    }
565
98.3k
    ret->top = i;
566
98.3k
    ret->neg = 0;
567
98.3k
    l = 0;
568
10.1M
    while (n--) {
569
10.0M
        s--;
570
10.0M
        l = (l << 8L) | *s;
571
10.0M
        if (m-- == 0) {
572
1.27M
            ret->d[--i] = l;
573
1.27M
            l = 0;
574
1.27M
            m = BN_BYTES - 1;
575
1.27M
        }
576
10.0M
    }
577
    /*
578
     * need to call this due to clear byte at top if avoiding having the top
579
     * bit set (-ve number)
580
     */
581
98.3k
    bn_correct_top(ret);
582
98.3k
    return ret;
583
98.3k
}
584
585
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
586
1.16M
{
587
1.16M
    if (tolen < 0)
588
0
        return -1;
589
1.16M
    return bn2binpad(a, to, tolen, little);
590
1.16M
}
591
592
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
593
1.10M
{
594
1.10M
    DECLARE_IS_ENDIAN;
595
596
1.10M
    if (IS_LITTLE_ENDIAN)
597
1.10M
        return BN_lebin2bn(s, len, ret);
598
0
    return BN_bin2bn(s, len, ret);
599
1.10M
}
600
601
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
602
1.10M
{
603
1.10M
    DECLARE_IS_ENDIAN;
604
605
1.10M
    if (IS_LITTLE_ENDIAN)
606
1.10M
        return BN_bn2lebinpad(a, to, tolen);
607
0
    return BN_bn2binpad(a, to, tolen);
608
1.10M
}
609
610
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
611
139M
{
612
139M
    int i;
613
139M
    BN_ULONG t1, t2, *ap, *bp;
614
615
139M
    ap = a->d;
616
139M
    bp = b->d;
617
618
139M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
619
2.67M
        && a->top == b->top) {
620
2.66M
        int res = 0;
621
622
13.6M
        for (i = 0; i < b->top; i++) {
623
11.0M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
624
11.0M
                -1, res);
625
11.0M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
626
11.0M
                1, res);
627
11.0M
        }
628
2.66M
        return res;
629
2.66M
    }
630
631
136M
    bn_check_top(a);
632
136M
    bn_check_top(b);
633
634
136M
    i = a->top - b->top;
635
136M
    if (i != 0)
636
15.4M
        return i;
637
638
128M
    for (i = a->top - 1; i >= 0; i--) {
639
126M
        t1 = ap[i];
640
126M
        t2 = bp[i];
641
126M
        if (t1 != t2)
642
119M
            return ((t1 > t2) ? 1 : -1);
643
126M
    }
644
2.32M
    return 0;
645
121M
}
646
647
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
648
19.1M
{
649
19.1M
    int i;
650
19.1M
    int gt, lt;
651
19.1M
    BN_ULONG t1, t2;
652
653
19.1M
    if ((a == NULL) || (b == NULL)) {
654
0
        if (a != NULL)
655
0
            return -1;
656
0
        else if (b != NULL)
657
0
            return 1;
658
0
        else
659
0
            return 0;
660
0
    }
661
662
19.1M
    bn_check_top(a);
663
19.1M
    bn_check_top(b);
664
665
19.1M
    if (a->neg != b->neg) {
666
1.23k
        if (a->neg)
667
505
            return -1;
668
726
        else
669
726
            return 1;
670
1.23k
    }
671
19.1M
    if (a->neg == 0) {
672
19.1M
        gt = 1;
673
19.1M
        lt = -1;
674
19.1M
    } else {
675
10.2k
        gt = -1;
676
10.2k
        lt = 1;
677
10.2k
    }
678
679
19.1M
    if (a->top > b->top)
680
2.25M
        return gt;
681
16.8M
    if (a->top < b->top)
682
1.76M
        return lt;
683
43.0M
    for (i = a->top - 1; i >= 0; i--) {
684
40.6M
        t1 = a->d[i];
685
40.6M
        t2 = b->d[i];
686
40.6M
        if (t1 > t2)
687
5.06M
            return gt;
688
35.5M
        if (t1 < t2)
689
7.63M
            return lt;
690
35.5M
    }
691
2.42M
    return 0;
692
15.1M
}
693
694
int BN_set_bit(BIGNUM *a, int n)
695
2.59M
{
696
2.59M
    int i, j, k;
697
698
2.59M
    if (n < 0)
699
0
        return 0;
700
701
2.59M
    i = n / BN_BITS2;
702
2.59M
    j = n % BN_BITS2;
703
2.59M
    if (a->top <= i) {
704
2.59M
        if (bn_wexpand(a, i + 1) == NULL)
705
0
            return 0;
706
21.6M
        for (k = a->top; k < i + 1; k++)
707
19.0M
            a->d[k] = 0;
708
2.59M
        a->top = i + 1;
709
2.59M
        a->flags &= ~BN_FLG_FIXED_TOP;
710
2.59M
    }
711
712
2.59M
    a->d[i] |= (((BN_ULONG)1) << j);
713
2.59M
    bn_check_top(a);
714
2.59M
    return 1;
715
2.59M
}
716
717
int BN_clear_bit(BIGNUM *a, int n)
718
283
{
719
283
    int i, j;
720
721
283
    bn_check_top(a);
722
283
    if (n < 0)
723
0
        return 0;
724
725
283
    i = n / BN_BITS2;
726
283
    j = n % BN_BITS2;
727
283
    if (a->top <= i)
728
0
        return 0;
729
730
283
    a->d[i] &= (~(((BN_ULONG)1) << j));
731
283
    bn_correct_top(a);
732
283
    return 1;
733
283
}
734
735
int BN_is_bit_set(const BIGNUM *a, int n)
736
210M
{
737
210M
    int i, j;
738
739
210M
    bn_check_top(a);
740
210M
    if (n < 0)
741
4.71k
        return 0;
742
210M
    i = n / BN_BITS2;
743
210M
    j = n % BN_BITS2;
744
210M
    if (a->top <= i)
745
9.31k
        return 0;
746
210M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
747
210M
}
748
749
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
750
7.33k
{
751
7.33k
    int b, w;
752
753
7.33k
    if (n < 0)
754
0
        return 0;
755
756
7.33k
    w = n / BN_BITS2;
757
7.33k
    b = n % BN_BITS2;
758
7.33k
    if (w >= a->top)
759
0
        return 0;
760
7.33k
    if (b == 0)
761
6.69k
        a->top = w;
762
639
    else {
763
639
        a->top = w + 1;
764
639
        a->d[w] &= ~(BN_MASK2 << b);
765
639
    }
766
7.33k
    a->flags |= BN_FLG_FIXED_TOP;
767
7.33k
    return 1;
768
7.33k
}
769
770
int BN_mask_bits(BIGNUM *a, int n)
771
0
{
772
0
    int ret;
773
774
0
    bn_check_top(a);
775
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
776
0
    if (ret)
777
0
        bn_correct_top(a);
778
0
    return ret;
779
0
}
780
781
void BN_set_negative(BIGNUM *a, int b)
782
2.71M
{
783
2.71M
    if (b && !BN_is_zero(a))
784
669k
        a->neg = 1;
785
2.04M
    else
786
2.04M
        a->neg = 0;
787
2.71M
}
788
789
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
790
85.3M
{
791
85.3M
    int i;
792
85.3M
    BN_ULONG aa, bb;
793
794
85.3M
    if (n == 0)
795
0
        return 0;
796
797
85.3M
    aa = a[n - 1];
798
85.3M
    bb = b[n - 1];
799
85.3M
    if (aa != bb)
800
76.0M
        return ((aa > bb) ? 1 : -1);
801
78.3M
    for (i = n - 2; i >= 0; i--) {
802
74.6M
        aa = a[i];
803
74.6M
        bb = b[i];
804
74.6M
        if (aa != bb)
805
5.58M
            return ((aa > bb) ? 1 : -1);
806
74.6M
    }
807
3.72M
    return 0;
808
9.31M
}
809
810
/*
811
 * Here follows a specialised variants of bn_cmp_words().  It has the
812
 * capability of performing the operation on arrays of different sizes. The
813
 * sizes of those arrays is expressed through cl, which is the common length
814
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
815
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
816
 * BN_ULONGs...
817
 */
818
819
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
820
124M
{
821
124M
    int n, i;
822
124M
    n = cl - 1;
823
824
124M
    if (dl < 0) {
825
2.89M
        for (i = dl; i < 0; i++) {
826
2.79M
            if (b[n - i] != 0)
827
1.39M
                return -1; /* a < b */
828
2.79M
        }
829
1.49M
    }
830
122M
    if (dl > 0) {
831
4.60M
        for (i = dl; i > 0; i--) {
832
4.35M
            if (a[n + i] != 0)
833
1.24M
                return 1; /* a > b */
834
4.35M
        }
835
1.49M
    }
836
121M
    return bn_cmp_words(a, b, cl);
837
122M
}
838
839
/*-
840
 * Constant-time conditional swap of a and b.
841
 * a and b are swapped if condition is not 0.
842
 * nwords is the number of words to swap.
843
 * Assumes that at least nwords are allocated in both a and b.
844
 * Assumes that no more than nwords are used by either a or b.
845
 */
846
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
847
1.96M
{
848
1.96M
    BN_ULONG t;
849
1.96M
    int i;
850
851
1.96M
    if (a == b)
852
0
        return;
853
854
1.96M
    bn_wcheck_size(a, nwords);
855
1.96M
    bn_wcheck_size(b, nwords);
856
857
1.96M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
858
859
1.96M
    t = (a->top ^ b->top) & condition;
860
1.96M
    a->top ^= t;
861
1.96M
    b->top ^= t;
862
863
1.96M
    t = (a->neg ^ b->neg) & condition;
864
1.96M
    a->neg ^= t;
865
1.96M
    b->neg ^= t;
866
867
    /*-
868
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
869
     * is actually to treat it as it's read-only data, and some (if not most)
870
     * of it does reside in read-only segment. In other words observation of
871
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
872
     * condition. It would either cause SEGV or effectively cause data
873
     * corruption.
874
     *
875
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
876
     * preserved.
877
     *
878
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
879
     * allocated and hence how to free it.
880
     *
881
     * BN_FLG_CONSTTIME: sufficient to mask and swap
882
     *
883
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
884
     * the data, so the d array may be padded with additional 0 values (i.e.
885
     * top could be greater than the minimal value that it could be). We should
886
     * be swapping it
887
     */
888
889
1.96M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
890
891
1.96M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
892
1.96M
    a->flags ^= t;
893
1.96M
    b->flags ^= t;
894
895
    /* conditionally swap the data */
896
12.1M
    for (i = 0; i < nwords; i++) {
897
10.2M
        t = (a->d[i] ^ b->d[i]) & condition;
898
10.2M
        a->d[i] ^= t;
899
10.2M
        b->d[i] ^= t;
900
10.2M
    }
901
1.96M
}
902
903
#undef BN_CONSTTIME_SWAP_FLAGS
904
905
/* Bits of security, see SP800-57 */
906
907
int BN_security_bits(int L, int N)
908
221k
{
909
221k
    int secbits, bits;
910
221k
    if (L >= 15360)
911
955
        secbits = 256;
912
220k
    else if (L >= 7680)
913
1.14k
        secbits = 192;
914
219k
    else if (L >= 3072)
915
2.82k
        secbits = 128;
916
216k
    else if (L >= 2048)
917
5.07k
        secbits = 112;
918
211k
    else if (L >= 1024)
919
126k
        secbits = 80;
920
84.4k
    else
921
84.4k
        return 0;
922
136k
    if (N == -1)
923
12.9k
        return secbits;
924
123k
    bits = N / 2;
925
123k
    if (bits < 80)
926
9.92k
        return 0;
927
114k
    return bits >= secbits ? secbits : bits;
928
123k
}
929
930
void BN_zero_ex(BIGNUM *a)
931
1.16G
{
932
1.16G
    a->neg = 0;
933
1.16G
    a->top = 0;
934
1.16G
    a->flags &= ~BN_FLG_FIXED_TOP;
935
1.16G
}
936
937
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
938
102M
{
939
102M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
940
102M
}
941
942
int BN_is_zero(const BIGNUM *a)
943
425M
{
944
425M
    return a->top == 0;
945
425M
}
946
947
int BN_is_one(const BIGNUM *a)
948
100M
{
949
100M
    return BN_abs_is_word(a, 1) && !a->neg;
950
100M
}
951
952
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
953
362k
{
954
362k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
955
362k
}
956
957
int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w)
958
6.75k
{
959
6.75k
    int res, i;
960
6.75k
    const BN_ULONG *ap = a->d;
961
962
6.75k
    if (a->neg || a->top == 0)
963
0
        return 0;
964
965
6.75k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
966
967
26.9k
    for (i = 1; i < a->top; i++)
968
20.1k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
969
20.1k
            res, 0);
970
6.75k
    return res;
971
6.75k
}
972
973
int BN_is_odd(const BIGNUM *a)
974
75.3M
{
975
75.3M
    return (a->top > 0) && (a->d[0] & 1);
976
75.3M
}
977
978
int BN_is_negative(const BIGNUM *a)
979
10.1M
{
980
10.1M
    return (a->neg != 0);
981
10.1M
}
982
983
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
984
    BN_CTX *ctx)
985
2.74M
{
986
2.74M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
987
2.74M
}
988
989
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
990
21.9M
{
991
21.9M
    dest->d = b->d;
992
21.9M
    dest->top = b->top;
993
21.9M
    dest->dmax = b->dmax;
994
21.9M
    dest->neg = b->neg;
995
21.9M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
996
21.9M
        | (b->flags & ~BN_FLG_MALLOCED)
997
21.9M
        | BN_FLG_STATIC_DATA | flags);
998
21.9M
}
999
1000
BN_GENCB *BN_GENCB_new(void)
1001
5.17k
{
1002
5.17k
    BN_GENCB *ret;
1003
1004
5.17k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
1005
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
1006
0
        return NULL;
1007
0
    }
1008
1009
5.17k
    return ret;
1010
5.17k
}
1011
1012
void BN_GENCB_free(BN_GENCB *cb)
1013
7.16k
{
1014
7.16k
    if (cb == NULL)
1015
1.99k
        return;
1016
5.17k
    OPENSSL_free(cb);
1017
5.17k
}
1018
1019
void BN_set_flags(BIGNUM *b, int n)
1020
2.25M
{
1021
2.25M
    b->flags |= n;
1022
2.25M
}
1023
1024
int BN_get_flags(const BIGNUM *b, int n)
1025
687M
{
1026
687M
    return b->flags & n;
1027
687M
}
1028
1029
/* Populate a BN_GENCB structure with an "old"-style callback */
1030
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
1031
    void *cb_arg)
1032
0
{
1033
0
    BN_GENCB *tmp_gencb = gencb;
1034
0
    tmp_gencb->ver = 1;
1035
0
    tmp_gencb->arg = cb_arg;
1036
0
    tmp_gencb->cb.cb_1 = callback;
1037
0
}
1038
1039
/* Populate a BN_GENCB structure with a "new"-style callback */
1040
void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
1041
    void *cb_arg)
1042
5.17k
{
1043
5.17k
    BN_GENCB *tmp_gencb = gencb;
1044
5.17k
    tmp_gencb->ver = 2;
1045
5.17k
    tmp_gencb->arg = cb_arg;
1046
5.17k
    tmp_gencb->cb.cb_2 = callback;
1047
5.17k
}
1048
1049
void *BN_GENCB_get_arg(BN_GENCB *cb)
1050
0
{
1051
0
    return cb->arg;
1052
0
}
1053
1054
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1055
2.28G
{
1056
2.28G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1057
2.28G
}
1058
1059
void bn_correct_top_consttime(BIGNUM *a)
1060
17.0k
{
1061
17.0k
    int j, atop;
1062
17.0k
    BN_ULONG limb;
1063
17.0k
    unsigned int mask;
1064
1065
1.10M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1066
1.08M
        limb = a->d[j];
1067
1.08M
        limb |= 0 - limb;
1068
1.08M
        limb >>= BN_BITS2 - 1;
1069
1.08M
        limb = 0 - limb;
1070
1.08M
        mask = (unsigned int)limb;
1071
1.08M
        mask &= constant_time_msb(j - a->top);
1072
1.08M
        atop = constant_time_select_int(mask, j + 1, atop);
1073
1.08M
    }
1074
1075
17.0k
    mask = constant_time_eq_int(atop, 0);
1076
17.0k
    a->top = atop;
1077
17.0k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1078
17.0k
    a->flags &= ~BN_FLG_FIXED_TOP;
1079
17.0k
}
1080
1081
void bn_correct_top(BIGNUM *a)
1082
888M
{
1083
888M
    BN_ULONG *ftl;
1084
888M
    int tmp_top = a->top;
1085
1086
888M
    if (tmp_top > 0) {
1087
2.47G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1088
2.47G
            ftl--;
1089
2.47G
            if (*ftl != 0)
1090
885M
                break;
1091
2.47G
        }
1092
888M
        a->top = tmp_top;
1093
888M
    }
1094
888M
    if (a->top == 0)
1095
3.51M
        a->neg = 0;
1096
888M
    a->flags &= ~BN_FLG_FIXED_TOP;
1097
888M
    bn_pollute(a);
1098
888M
}