Coverage Report

Created: 2026-07-23 06:28

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
1.98M
{
84
1.98M
    static const BN_ULONG data_one = 1L;
85
1.98M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
1.98M
    return &const_one;
88
1.98M
}
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
260M
{
101
260M
    BN_ULONG x, mask;
102
260M
    int bits = (l != 0);
103
104
260M
#if BN_BITS2 > 32
105
260M
    x = l >> 32;
106
260M
    mask = (0 - x) & BN_MASK2;
107
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
260M
    bits += 32 & mask;
109
260M
    l ^= (x ^ l) & mask;
110
260M
#endif
111
112
260M
    x = l >> 16;
113
260M
    mask = (0 - x) & BN_MASK2;
114
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
260M
    bits += 16 & mask;
116
260M
    l ^= (x ^ l) & mask;
117
118
260M
    x = l >> 8;
119
260M
    mask = (0 - x) & BN_MASK2;
120
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
260M
    bits += 8 & mask;
122
260M
    l ^= (x ^ l) & mask;
123
124
260M
    x = l >> 4;
125
260M
    mask = (0 - x) & BN_MASK2;
126
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
260M
    bits += 4 & mask;
128
260M
    l ^= (x ^ l) & mask;
129
130
260M
    x = l >> 2;
131
260M
    mask = (0 - x) & BN_MASK2;
132
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
260M
    bits += 2 & mask;
134
260M
    l ^= (x ^ l) & mask;
135
136
260M
    x = l >> 1;
137
260M
    mask = (0 - x) & BN_MASK2;
138
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
260M
    bits += 1 & mask;
140
141
260M
    return bits;
142
260M
}
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.42M
{
153
8.42M
    int j, ret;
154
8.42M
    unsigned int mask, past_i;
155
8.42M
    int i = a->top - 1;
156
8.42M
    bn_check_top(a);
157
158
67.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
58.5M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
58.5M
        ret += BN_BITS2 & (~mask & ~past_i);
162
58.5M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
58.5M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
58.5M
    }
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.42M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
8.42M
    return ret & mask;
174
8.42M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
30.7M
{
178
30.7M
    int i = a->top - 1;
179
30.7M
    bn_check_top(a);
180
181
30.7M
    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.37M
        return bn_num_bits_consttime(a);
192
6.37M
    }
193
194
24.4M
    if (BN_is_zero(a))
195
684k
        return 0;
196
197
23.7M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
24.4M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
78.8M
{
202
78.8M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.83M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
76.9M
    else if (clear != 0)
205
47.3M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
29.6M
    else
207
29.6M
        OPENSSL_free(a->d);
208
78.8M
}
209
210
void BN_clear_free(BIGNUM *a)
211
39.6M
{
212
39.6M
    if (a == NULL)
213
5.82M
        return;
214
33.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
30.6M
        bn_free_d(a, 1);
216
33.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
3.30M
        OPENSSL_cleanse(a, sizeof(*a));
218
3.30M
        OPENSSL_free(a);
219
3.30M
    }
220
33.7M
}
221
222
void BN_free(BIGNUM *a)
223
50.7M
{
224
50.7M
    if (a == NULL)
225
21.0M
        return;
226
29.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
29.6M
        bn_free_d(a, 0);
228
29.7M
    if (a->flags & BN_FLG_MALLOCED)
229
29.6M
        OPENSSL_free(a);
230
29.7M
}
231
232
void bn_init(BIGNUM *a)
233
75.1M
{
234
75.1M
    static BIGNUM nilbn;
235
236
75.1M
    *a = nilbn;
237
75.1M
    bn_check_top(a);
238
75.1M
}
239
240
BIGNUM *BN_new(void)
241
32.9M
{
242
32.9M
    BIGNUM *ret;
243
244
32.9M
    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
32.9M
    ret->flags = BN_FLG_MALLOCED;
249
32.9M
    bn_check_top(ret);
250
32.9M
    return ret;
251
32.9M
}
252
253
BIGNUM *BN_secure_new(void)
254
2.08M
{
255
2.08M
    BIGNUM *ret = BN_new();
256
2.08M
    if (ret != NULL)
257
2.08M
        ret->flags |= BN_FLG_SECURE;
258
2.08M
    return ret;
259
2.08M
}
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
51.4M
{
265
51.4M
    BN_ULONG *a = NULL;
266
267
51.4M
    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
51.4M
    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
51.4M
    if (BN_get_flags(b, BN_FLG_SECURE))
276
1.37M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
277
50.0M
    else
278
50.0M
        a = OPENSSL_zalloc(words * sizeof(*a));
279
51.4M
    if (a == NULL) {
280
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
281
0
        return NULL;
282
0
    }
283
284
51.4M
    assert(b->top <= words);
285
51.4M
    if (b->top > 0)
286
5.87M
        memcpy(a, b->d, sizeof(*a) * b->top);
287
288
51.4M
    return a;
289
51.4M
}
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
51.4M
{
301
51.4M
    if (words > b->dmax) {
302
51.4M
        BN_ULONG *a = bn_expand_internal(b, words);
303
51.4M
        if (!a)
304
0
            return NULL;
305
51.4M
        if (b->d != NULL)
306
12.6M
            bn_free_d(b, 1);
307
51.4M
        b->d = a;
308
51.4M
        b->dmax = words;
309
51.4M
    }
310
311
51.4M
    return b;
312
51.4M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
2.64M
{
316
2.64M
    BIGNUM *t;
317
318
2.64M
    if (a == NULL)
319
0
        return NULL;
320
2.64M
    bn_check_top(a);
321
322
2.64M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
2.64M
    if (t == NULL)
324
0
        return NULL;
325
2.64M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
2.64M
    bn_check_top(t);
330
2.64M
    return t;
331
2.64M
}
332
333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
334
116M
{
335
116M
    int bn_words;
336
337
116M
    bn_check_top(b);
338
339
116M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
340
341
116M
    if (a == b)
342
11
        return a;
343
116M
    if (bn_wexpand(a, bn_words) == NULL)
344
0
        return NULL;
345
346
116M
    if (b->top > 0)
347
115M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
348
349
116M
    a->neg = b->neg;
350
116M
    a->top = b->top;
351
116M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
352
116M
    bn_check_top(a);
353
116M
    return a;
354
116M
}
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
487k
{
394
487k
    if (a == NULL)
395
0
        return;
396
487k
    bn_check_top(a);
397
487k
    if (a->d != NULL)
398
44.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
487k
    a->neg = 0;
400
487k
    a->top = 0;
401
487k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
487k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
432
{
406
432
    if (a->top > 1)
407
12
        return BN_MASK2;
408
420
    else if (a->top == 1)
409
358
        return a->d[0];
410
    /* a->top == 0 */
411
62
    return 0;
412
432
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.40M
{
416
3.40M
    bn_check_top(a);
417
3.40M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.40M
    a->neg = 0;
420
3.40M
    a->d[0] = w;
421
3.40M
    a->top = (w ? 1 : 0);
422
3.40M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.40M
    bn_check_top(a);
424
3.40M
    return 1;
425
3.40M
}
426
427
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
428
1.86M
{
429
1.86M
    unsigned int i, m;
430
1.86M
    unsigned int n;
431
1.86M
    BN_ULONG l;
432
1.86M
    BIGNUM *bn = NULL;
433
434
1.86M
    if (ret == NULL)
435
1.22M
        ret = bn = BN_new();
436
1.86M
    if (ret == NULL)
437
0
        return NULL;
438
1.86M
    bn_check_top(ret);
439
    /* Skip leading zero's. */
440
4.27M
    for (; len > 0 && *s == 0; s++, len--)
441
2.41M
        continue;
442
1.86M
    n = len;
443
1.86M
    if (n == 0) {
444
285k
        ret->top = 0;
445
285k
        return ret;
446
285k
    }
447
1.57M
    i = ((n - 1) / BN_BYTES) + 1;
448
1.57M
    m = ((n - 1) % (BN_BYTES));
449
1.57M
    if (bn_wexpand(ret, (int)i) == NULL) {
450
0
        BN_free(bn);
451
0
        return NULL;
452
0
    }
453
1.57M
    ret->top = i;
454
1.57M
    ret->neg = 0;
455
1.57M
    l = 0;
456
92.9M
    while (n--) {
457
91.3M
        l = (l << 8L) | *(s++);
458
91.3M
        if (m-- == 0) {
459
12.2M
            ret->d[--i] = l;
460
12.2M
            l = 0;
461
12.2M
            m = BN_BYTES - 1;
462
12.2M
        }
463
91.3M
    }
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.57M
    bn_correct_top(ret);
469
1.57M
    return ret;
470
1.57M
}
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
283k
{
478
283k
    int n;
479
283k
    size_t i, lasti, j, atop, mask;
480
283k
    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
283k
    n = BN_num_bytes(a);
488
283k
    if (tolen == -1) {
489
152k
        tolen = n;
490
152k
    } else if (tolen < n) { /* uncommon/unlike case */
491
984
        BIGNUM temp = *a;
492
493
984
        bn_correct_top(&temp);
494
984
        n = BN_num_bytes(&temp);
495
984
        if (tolen < n)
496
984
            return -1;
497
984
    }
498
499
    /* Swipe through whole available data and don't give away padded zero. */
500
282k
    atop = a->dmax * BN_BYTES;
501
282k
    if (atop == 0) {
502
38.5k
        if (tolen != 0)
503
993
            memset(to, '\0', tolen);
504
38.5k
        return tolen;
505
38.5k
    }
506
507
244k
    lasti = atop - 1;
508
244k
    atop = a->top * BN_BYTES;
509
244k
    if (endianess == big)
510
152k
        to += tolen; /* start from the end of the buffer */
511
22.1M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
512
21.9M
        unsigned char val;
513
21.9M
        l = a->d[i / BN_BYTES];
514
21.9M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
515
21.9M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
516
21.9M
        if (endianess == big)
517
12.5M
            *--to = val;
518
9.40M
        else
519
9.40M
            *to++ = val;
520
21.9M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
521
21.9M
    }
522
523
244k
    return tolen;
524
282k
}
525
526
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
527
229k
{
528
229k
    if (tolen < 0)
529
0
        return -1;
530
229k
    return bn2binpad(a, to, tolen, big);
531
229k
}
532
533
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
534
748k
{
535
748k
    return bn2binpad(a, to, -1, big);
536
748k
}
537
538
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
92.4k
{
540
92.4k
    unsigned int i, m;
541
92.4k
    unsigned int n;
542
92.4k
    BN_ULONG l;
543
92.4k
    BIGNUM *bn = NULL;
544
545
92.4k
    if (ret == NULL)
546
80.6k
        ret = bn = BN_new();
547
92.4k
    if (ret == NULL)
548
0
        return NULL;
549
92.4k
    bn_check_top(ret);
550
92.4k
    s += len;
551
    /* Skip trailing zeroes. */
552
109k
    for (; len > 0 && s[-1] == 0; s--, len--)
553
16.9k
        continue;
554
92.4k
    n = len;
555
92.4k
    if (n == 0) {
556
305
        ret->top = 0;
557
305
        return ret;
558
305
    }
559
92.1k
    i = ((n - 1) / BN_BYTES) + 1;
560
92.1k
    m = ((n - 1) % (BN_BYTES));
561
92.1k
    if (bn_wexpand(ret, (int)i) == NULL) {
562
0
        BN_free(bn);
563
0
        return NULL;
564
0
    }
565
92.1k
    ret->top = i;
566
92.1k
    ret->neg = 0;
567
92.1k
    l = 0;
568
9.49M
    while (n--) {
569
9.40M
        s--;
570
9.40M
        l = (l << 8L) | *s;
571
9.40M
        if (m-- == 0) {
572
1.18M
            ret->d[--i] = l;
573
1.18M
            l = 0;
574
1.18M
            m = BN_BYTES - 1;
575
1.18M
        }
576
9.40M
    }
577
    /*
578
     * need to call this due to clear byte at top if avoiding having the top
579
     * bit set (-ve number)
580
     */
581
92.1k
    bn_correct_top(ret);
582
92.1k
    return ret;
583
92.1k
}
584
585
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
586
1.04M
{
587
1.04M
    if (tolen < 0)
588
0
        return -1;
589
1.04M
    return bn2binpad(a, to, tolen, little);
590
1.04M
}
591
592
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
593
985k
{
594
985k
    DECLARE_IS_ENDIAN;
595
596
985k
    if (IS_LITTLE_ENDIAN)
597
985k
        return BN_lebin2bn(s, len, ret);
598
0
    return BN_bin2bn(s, len, ret);
599
985k
}
600
601
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
602
985k
{
603
985k
    DECLARE_IS_ENDIAN;
604
605
985k
    if (IS_LITTLE_ENDIAN)
606
985k
        return BN_bn2lebinpad(a, to, tolen);
607
0
    return BN_bn2binpad(a, to, tolen);
608
985k
}
609
610
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
611
106M
{
612
106M
    int i;
613
106M
    BN_ULONG t1, t2, *ap, *bp;
614
615
    /*
616
     * As it is a public API function, we should handle NULL parameters in
617
     * some way. The function can’t return an error, so let’s define that NULL
618
     * is less than any BIGNUM.
619
     */
620
106M
    if (!ossl_assert(a != NULL && b != NULL))
621
0
        return (b == NULL) - (a == NULL);
622
623
106M
    ap = a->d;
624
106M
    bp = b->d;
625
626
106M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
627
104M
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
628
2.32M
        int res = 0;
629
2.32M
        int min_top = a->top < b->top ? a->top : b->top;
630
631
13.0M
        for (i = 0; i < min_top; i++) {
632
10.7M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
633
10.7M
                -1, res);
634
10.7M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
635
10.7M
                1, res);
636
10.7M
        }
637
638
2.42M
        for (i = min_top; i < a->top; ++i)
639
98.2k
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
640
98.2k
                res, 1);
641
642
2.32M
        for (i = min_top; i < b->top; ++i)
643
2.71k
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
644
2.71k
                res, -1);
645
646
2.32M
        return res;
647
2.32M
    }
648
649
104M
    bn_check_top(a);
650
104M
    bn_check_top(b);
651
652
104M
    i = a->top - b->top;
653
104M
    if (i != 0)
654
12.4M
        return i;
655
656
97.6M
    for (i = a->top - 1; i >= 0; i--) {
657
95.8M
        t1 = ap[i];
658
95.8M
        t2 = bp[i];
659
95.8M
        if (t1 != t2)
660
90.2M
            return ((t1 > t2) ? 1 : -1);
661
95.8M
    }
662
1.83M
    return 0;
663
92.1M
}
664
665
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
666
19.2M
{
667
19.2M
    int i;
668
19.2M
    int gt, lt;
669
19.2M
    BN_ULONG t1, t2;
670
671
19.2M
    if ((a == NULL) || (b == NULL)) {
672
0
        if (a != NULL)
673
0
            return -1;
674
0
        else if (b != NULL)
675
0
            return 1;
676
0
        else
677
0
            return 0;
678
0
    }
679
680
19.2M
    bn_check_top(a);
681
19.2M
    bn_check_top(b);
682
683
19.2M
    if (a->neg != b->neg) {
684
1.48k
        if (a->neg)
685
646
            return -1;
686
835
        else
687
835
            return 1;
688
1.48k
    }
689
19.2M
    if (a->neg == 0) {
690
19.2M
        gt = 1;
691
19.2M
        lt = -1;
692
19.2M
    } else {
693
9.90k
        gt = -1;
694
9.90k
        lt = 1;
695
9.90k
    }
696
697
19.2M
    if (a->top > b->top)
698
2.18M
        return gt;
699
17.0M
    if (a->top < b->top)
700
1.80M
        return lt;
701
43.1M
    for (i = a->top - 1; i >= 0; i--) {
702
40.7M
        t1 = a->d[i];
703
40.7M
        t2 = b->d[i];
704
40.7M
        if (t1 > t2)
705
5.33M
            return gt;
706
35.4M
        if (t1 < t2)
707
7.51M
            return lt;
708
35.4M
    }
709
2.39M
    return 0;
710
15.2M
}
711
712
int BN_set_bit(BIGNUM *a, int n)
713
2.54M
{
714
2.54M
    int i, j, k;
715
716
2.54M
    if (n < 0)
717
0
        return 0;
718
719
2.54M
    i = n / BN_BITS2;
720
2.54M
    j = n % BN_BITS2;
721
2.54M
    if (a->top <= i) {
722
2.54M
        if (bn_wexpand(a, i + 1) == NULL)
723
0
            return 0;
724
21.3M
        for (k = a->top; k < i + 1; k++)
725
18.7M
            a->d[k] = 0;
726
2.54M
        a->top = i + 1;
727
2.54M
        a->flags &= ~BN_FLG_FIXED_TOP;
728
2.54M
    }
729
730
2.54M
    a->d[i] |= (((BN_ULONG)1) << j);
731
2.54M
    bn_check_top(a);
732
2.54M
    return 1;
733
2.54M
}
734
735
int BN_clear_bit(BIGNUM *a, int n)
736
222
{
737
222
    int i, j;
738
739
222
    bn_check_top(a);
740
222
    if (n < 0)
741
0
        return 0;
742
743
222
    i = n / BN_BITS2;
744
222
    j = n % BN_BITS2;
745
222
    if (a->top <= i)
746
0
        return 0;
747
748
222
    a->d[i] &= (~(((BN_ULONG)1) << j));
749
222
    bn_correct_top(a);
750
222
    return 1;
751
222
}
752
753
int BN_is_bit_set(const BIGNUM *a, int n)
754
200M
{
755
200M
    int i, j;
756
757
200M
    bn_check_top(a);
758
200M
    if (n < 0)
759
3.70k
        return 0;
760
200M
    i = n / BN_BITS2;
761
200M
    j = n % BN_BITS2;
762
200M
    if (a->top <= i)
763
9.16k
        return 0;
764
200M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
765
200M
}
766
767
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
768
6.83k
{
769
6.83k
    int b, w;
770
771
6.83k
    if (n < 0)
772
0
        return 0;
773
774
6.83k
    w = n / BN_BITS2;
775
6.83k
    b = n % BN_BITS2;
776
6.83k
    if (w >= a->top)
777
0
        return 0;
778
6.83k
    if (b == 0)
779
6.31k
        a->top = w;
780
527
    else {
781
527
        a->top = w + 1;
782
527
        a->d[w] &= ~(BN_MASK2 << b);
783
527
    }
784
6.83k
    a->flags |= BN_FLG_FIXED_TOP;
785
6.83k
    return 1;
786
6.83k
}
787
788
int BN_mask_bits(BIGNUM *a, int n)
789
0
{
790
0
    int ret;
791
792
0
    bn_check_top(a);
793
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
794
0
    if (ret)
795
0
        bn_correct_top(a);
796
0
    return ret;
797
0
}
798
799
void BN_set_negative(BIGNUM *a, int b)
800
2.47M
{
801
2.47M
    if (b && !BN_is_zero(a))
802
557k
        a->neg = 1;
803
1.91M
    else
804
1.91M
        a->neg = 0;
805
2.47M
}
806
807
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
808
95.0M
{
809
95.0M
    int i;
810
95.0M
    BN_ULONG aa, bb;
811
812
95.0M
    if (n == 0)
813
0
        return 0;
814
815
95.0M
    aa = a[n - 1];
816
95.0M
    bb = b[n - 1];
817
95.0M
    if (aa != bb)
818
83.7M
        return ((aa > bb) ? 1 : -1);
819
97.9M
    for (i = n - 2; i >= 0; i--) {
820
93.3M
        aa = a[i];
821
93.3M
        bb = b[i];
822
93.3M
        if (aa != bb)
823
6.74M
            return ((aa > bb) ? 1 : -1);
824
93.3M
    }
825
4.59M
    return 0;
826
11.3M
}
827
828
/*
829
 * Here follows a specialised variants of bn_cmp_words().  It has the
830
 * capability of performing the operation on arrays of different sizes. The
831
 * sizes of those arrays is expressed through cl, which is the common length
832
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
833
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
834
 * BN_ULONGs...
835
 */
836
837
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
838
133M
{
839
133M
    int n, i;
840
133M
    n = cl - 1;
841
842
133M
    if (dl < 0) {
843
2.84M
        for (i = dl; i < 0; i++) {
844
2.75M
            if (b[n - i] != 0)
845
1.29M
                return -1; /* a < b */
846
2.75M
        }
847
1.38M
    }
848
132M
    if (dl > 0) {
849
4.24M
        for (i = dl; i > 0; i--) {
850
4.01M
            if (a[n + i] != 0)
851
1.16M
                return 1; /* a > b */
852
4.01M
        }
853
1.39M
    }
854
131M
    return bn_cmp_words(a, b, cl);
855
132M
}
856
857
/*-
858
 * Constant-time conditional swap of a and b.
859
 * a and b are swapped if condition is not 0.
860
 * nwords is the number of words to swap.
861
 * Assumes that at least nwords are allocated in both a and b.
862
 * Assumes that no more than nwords are used by either a or b.
863
 */
864
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
865
2.15M
{
866
2.15M
    BN_ULONG t;
867
2.15M
    int i;
868
869
2.15M
    if (a == b)
870
0
        return;
871
872
2.15M
    bn_wcheck_size(a, nwords);
873
2.15M
    bn_wcheck_size(b, nwords);
874
875
2.15M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
876
877
2.15M
    t = (a->top ^ b->top) & condition;
878
2.15M
    a->top ^= t;
879
2.15M
    b->top ^= t;
880
881
2.15M
    t = (a->neg ^ b->neg) & condition;
882
2.15M
    a->neg ^= t;
883
2.15M
    b->neg ^= t;
884
885
    /*-
886
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
887
     * is actually to treat it as it's read-only data, and some (if not most)
888
     * of it does reside in read-only segment. In other words observation of
889
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
890
     * condition. It would either cause SEGV or effectively cause data
891
     * corruption.
892
     *
893
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
894
     * preserved.
895
     *
896
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
897
     * allocated and hence how to free it.
898
     *
899
     * BN_FLG_CONSTTIME: sufficient to mask and swap
900
     *
901
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
902
     * the data, so the d array may be padded with additional 0 values (i.e.
903
     * top could be greater than the minimal value that it could be). We should
904
     * be swapping it
905
     */
906
907
2.15M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
908
909
2.15M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
910
2.15M
    a->flags ^= t;
911
2.15M
    b->flags ^= t;
912
913
    /* conditionally swap the data */
914
13.1M
    for (i = 0; i < nwords; i++) {
915
11.0M
        t = (a->d[i] ^ b->d[i]) & condition;
916
11.0M
        a->d[i] ^= t;
917
11.0M
        b->d[i] ^= t;
918
11.0M
    }
919
2.15M
}
920
921
#undef BN_CONSTTIME_SWAP_FLAGS
922
923
/* Bits of security, see SP800-57 */
924
925
int BN_security_bits(int L, int N)
926
209k
{
927
209k
    int secbits, bits;
928
209k
    if (L >= 15360)
929
973
        secbits = 256;
930
208k
    else if (L >= 7680)
931
1.19k
        secbits = 192;
932
207k
    else if (L >= 3072)
933
2.64k
        secbits = 128;
934
205k
    else if (L >= 2048)
935
4.42k
        secbits = 112;
936
200k
    else if (L >= 1024)
937
119k
        secbits = 80;
938
80.9k
    else
939
80.9k
        return 0;
940
129k
    if (N == -1)
941
11.4k
        return secbits;
942
117k
    bits = N / 2;
943
117k
    if (bits < 80)
944
9.36k
        return 0;
945
108k
    return bits >= secbits ? secbits : bits;
946
117k
}
947
948
void BN_zero_ex(BIGNUM *a)
949
1.23G
{
950
1.23G
    a->neg = 0;
951
1.23G
    a->top = 0;
952
1.23G
    a->flags &= ~BN_FLG_FIXED_TOP;
953
1.23G
}
954
955
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
956
116M
{
957
116M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
958
116M
}
959
960
int BN_is_zero(const BIGNUM *a)
961
427M
{
962
427M
    return a->top == 0;
963
427M
}
964
965
int BN_is_one(const BIGNUM *a)
966
115M
{
967
115M
    return BN_abs_is_word(a, 1) && !a->neg;
968
115M
}
969
970
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
971
314k
{
972
314k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
973
314k
}
974
975
int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w)
976
6.39k
{
977
6.39k
    int res, i;
978
6.39k
    const BN_ULONG *ap = a->d;
979
980
6.39k
    if (a->neg || a->top == 0)
981
0
        return 0;
982
983
6.39k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
984
985
25.4k
    for (i = 1; i < a->top; i++)
986
19.0k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
987
19.0k
            res, 0);
988
6.39k
    return res;
989
6.39k
}
990
991
int BN_is_odd(const BIGNUM *a)
992
72.8M
{
993
72.8M
    return (a->top > 0) && (a->d[0] & 1);
994
72.8M
}
995
996
int BN_is_negative(const BIGNUM *a)
997
8.37M
{
998
8.37M
    return (a->neg != 0);
999
8.37M
}
1000
1001
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1002
    BN_CTX *ctx)
1003
2.51M
{
1004
2.51M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1005
2.51M
}
1006
1007
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1008
21.4M
{
1009
21.4M
    dest->d = b->d;
1010
21.4M
    dest->top = b->top;
1011
21.4M
    dest->dmax = b->dmax;
1012
21.4M
    dest->neg = b->neg;
1013
21.4M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1014
21.4M
        | (b->flags & ~BN_FLG_MALLOCED)
1015
21.4M
        | BN_FLG_STATIC_DATA | flags);
1016
21.4M
}
1017
1018
BN_GENCB *BN_GENCB_new(void)
1019
4.63k
{
1020
4.63k
    BN_GENCB *ret;
1021
1022
4.63k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
1023
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
1024
0
        return NULL;
1025
0
    }
1026
1027
4.63k
    return ret;
1028
4.63k
}
1029
1030
void BN_GENCB_free(BN_GENCB *cb)
1031
6.46k
{
1032
6.46k
    if (cb == NULL)
1033
1.82k
        return;
1034
4.63k
    OPENSSL_free(cb);
1035
4.63k
}
1036
1037
void BN_set_flags(BIGNUM *b, int n)
1038
2.29M
{
1039
2.29M
    b->flags |= n;
1040
2.29M
}
1041
1042
int BN_get_flags(const BIGNUM *b, int n)
1043
772M
{
1044
772M
    return b->flags & n;
1045
772M
}
1046
1047
/* Populate a BN_GENCB structure with an "old"-style callback */
1048
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
1049
    void *cb_arg)
1050
0
{
1051
0
    BN_GENCB *tmp_gencb = gencb;
1052
0
    tmp_gencb->ver = 1;
1053
0
    tmp_gencb->arg = cb_arg;
1054
0
    tmp_gencb->cb.cb_1 = callback;
1055
0
}
1056
1057
/* Populate a BN_GENCB structure with a "new"-style callback */
1058
void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
1059
    void *cb_arg)
1060
4.63k
{
1061
4.63k
    BN_GENCB *tmp_gencb = gencb;
1062
4.63k
    tmp_gencb->ver = 2;
1063
4.63k
    tmp_gencb->arg = cb_arg;
1064
4.63k
    tmp_gencb->cb.cb_2 = callback;
1065
4.63k
}
1066
1067
void *BN_GENCB_get_arg(BN_GENCB *cb)
1068
0
{
1069
0
    return cb->arg;
1070
0
}
1071
1072
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1073
2.30G
{
1074
2.30G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1075
2.30G
}
1076
1077
void bn_correct_top_consttime(BIGNUM *a)
1078
16.6k
{
1079
16.6k
    int j, atop;
1080
16.6k
    BN_ULONG limb;
1081
16.6k
    unsigned int mask;
1082
1083
1.08M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1084
1.06M
        limb = a->d[j];
1085
1.06M
        limb |= 0 - limb;
1086
1.06M
        limb >>= BN_BITS2 - 1;
1087
1.06M
        limb = 0 - limb;
1088
1.06M
        mask = (unsigned int)limb;
1089
1.06M
        mask &= constant_time_msb(j - a->top);
1090
1.06M
        atop = constant_time_select_int(mask, j + 1, atop);
1091
1.06M
    }
1092
1093
16.6k
    mask = constant_time_eq_int(atop, 0);
1094
16.6k
    a->top = atop;
1095
16.6k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1096
16.6k
    a->flags &= ~BN_FLG_FIXED_TOP;
1097
16.6k
}
1098
1099
void bn_correct_top(BIGNUM *a)
1100
908M
{
1101
908M
    BN_ULONG *ftl;
1102
908M
    int tmp_top = a->top;
1103
1104
908M
    if (tmp_top > 0) {
1105
2.56G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1106
2.56G
            ftl--;
1107
2.56G
            if (*ftl != 0)
1108
905M
                break;
1109
2.56G
        }
1110
908M
        a->top = tmp_top;
1111
908M
    }
1112
908M
    if (a->top == 0)
1113
3.79M
        a->neg = 0;
1114
908M
    a->flags &= ~BN_FLG_FIXED_TOP;
1115
908M
    bn_pollute(a);
1116
908M
}