Coverage Report

Created: 2026-02-14 07:20

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.94M
{
84
1.94M
    static const BN_ULONG data_one = 1L;
85
1.94M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
1.94M
    return &const_one;
88
1.94M
}
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
243M
{
101
243M
    BN_ULONG x, mask;
102
243M
    int bits = (l != 0);
103
104
243M
#if BN_BITS2 > 32
105
243M
    x = l >> 32;
106
243M
    mask = (0 - x) & BN_MASK2;
107
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
243M
    bits += 32 & mask;
109
243M
    l ^= (x ^ l) & mask;
110
243M
#endif
111
112
243M
    x = l >> 16;
113
243M
    mask = (0 - x) & BN_MASK2;
114
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
243M
    bits += 16 & mask;
116
243M
    l ^= (x ^ l) & mask;
117
118
243M
    x = l >> 8;
119
243M
    mask = (0 - x) & BN_MASK2;
120
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
243M
    bits += 8 & mask;
122
243M
    l ^= (x ^ l) & mask;
123
124
243M
    x = l >> 4;
125
243M
    mask = (0 - x) & BN_MASK2;
126
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
243M
    bits += 4 & mask;
128
243M
    l ^= (x ^ l) & mask;
129
130
243M
    x = l >> 2;
131
243M
    mask = (0 - x) & BN_MASK2;
132
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
243M
    bits += 2 & mask;
134
243M
    l ^= (x ^ l) & mask;
135
136
243M
    x = l >> 1;
137
243M
    mask = (0 - x) & BN_MASK2;
138
243M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
243M
    bits += 1 & mask;
140
141
243M
    return bits;
142
243M
}
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
7.91M
{
153
7.91M
    int j, ret;
154
7.91M
    unsigned int mask, past_i;
155
7.91M
    int i = a->top - 1;
156
7.91M
    bn_check_top(a);
157
158
68.9M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
61.0M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
61.0M
        ret += BN_BITS2 & (~mask & ~past_i);
162
61.0M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
61.0M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
61.0M
    }
166
167
    /*
168
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
169
     * final result.
170
     */
171
7.91M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
7.91M
    return ret & mask;
174
7.91M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
31.0M
{
178
31.0M
    int i = a->top - 1;
179
31.0M
    bn_check_top(a);
180
181
31.0M
    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.00M
        return bn_num_bits_consttime(a);
192
6.00M
    }
193
194
25.0M
    if (BN_is_zero(a))
195
347k
        return 0;
196
197
24.7M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
25.0M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
77.1M
{
202
77.1M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.69M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
75.4M
    else if (clear != 0)
205
45.8M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
29.6M
    else
207
29.6M
        OPENSSL_free(a->d);
208
77.1M
}
209
210
void BN_clear_free(BIGNUM *a)
211
38.9M
{
212
38.9M
    if (a == NULL)
213
6.17M
        return;
214
32.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
29.6M
        bn_free_d(a, 1);
216
32.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
3.20M
        OPENSSL_cleanse(a, sizeof(*a));
218
3.20M
        OPENSSL_free(a);
219
3.20M
    }
220
32.7M
}
221
222
void BN_free(BIGNUM *a)
223
49.0M
{
224
49.0M
    if (a == NULL)
225
19.3M
        return;
226
29.6M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
29.6M
        bn_free_d(a, 0);
228
29.6M
    if (a->flags & BN_FLG_MALLOCED)
229
29.6M
        OPENSSL_free(a);
230
29.6M
}
231
232
void bn_init(BIGNUM *a)
233
73.6M
{
234
73.6M
    static BIGNUM nilbn;
235
236
73.6M
    *a = nilbn;
237
73.6M
    bn_check_top(a);
238
73.6M
}
239
240
BIGNUM *BN_new(void)
241
32.8M
{
242
32.8M
    BIGNUM *ret;
243
244
32.8M
    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.8M
    ret->flags = BN_FLG_MALLOCED;
249
32.8M
    bn_check_top(ret);
250
32.8M
    return ret;
251
32.8M
}
252
253
BIGNUM *BN_secure_new(void)
254
1.88M
{
255
1.88M
    BIGNUM *ret = BN_new();
256
1.88M
    if (ret != NULL)
257
1.88M
        ret->flags |= BN_FLG_SECURE;
258
1.88M
    return ret;
259
1.88M
}
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
52.4M
{
265
52.4M
    BN_ULONG *a = NULL;
266
267
52.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
52.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
52.4M
    if (BN_get_flags(b, BN_FLG_SECURE))
276
1.23M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
277
51.2M
    else
278
51.2M
        a = OPENSSL_zalloc(words * sizeof(*a));
279
52.4M
    if (a == NULL) {
280
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
281
0
        return NULL;
282
0
    }
283
284
52.4M
    assert(b->top <= words);
285
52.4M
    if (b->top > 0)
286
6.10M
        memcpy(a, b->d, sizeof(*a) * b->top);
287
288
52.4M
    return a;
289
52.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
52.4M
{
301
52.4M
    if (words > b->dmax) {
302
52.4M
        BN_ULONG *a = bn_expand_internal(b, words);
303
52.4M
        if (!a)
304
0
            return NULL;
305
52.4M
        if (b->d != NULL)
306
12.9M
            bn_free_d(b, 1);
307
52.4M
        b->d = a;
308
52.4M
        b->dmax = words;
309
52.4M
    }
310
311
52.4M
    return b;
312
52.4M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
2.92M
{
316
2.92M
    BIGNUM *t;
317
318
2.92M
    if (a == NULL)
319
0
        return NULL;
320
2.92M
    bn_check_top(a);
321
322
2.92M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
2.92M
    if (t == NULL)
324
0
        return NULL;
325
2.92M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
2.92M
    bn_check_top(t);
330
2.92M
    return t;
331
2.92M
}
332
333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
334
117M
{
335
117M
    int bn_words;
336
337
117M
    bn_check_top(b);
338
339
117M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
340
341
117M
    if (a == b)
342
8
        return a;
343
117M
    if (bn_wexpand(a, bn_words) == NULL)
344
0
        return NULL;
345
346
117M
    if (b->top > 0)
347
116M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
348
349
117M
    a->neg = b->neg;
350
117M
    a->top = b->top;
351
117M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
352
117M
    bn_check_top(a);
353
117M
    return a;
354
117M
}
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
385k
{
394
385k
    if (a == NULL)
395
0
        return;
396
385k
    bn_check_top(a);
397
385k
    if (a->d != NULL)
398
43.5k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
385k
    a->neg = 0;
400
385k
    a->top = 0;
401
385k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
385k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
446
{
406
446
    if (a->top > 1)
407
15
        return BN_MASK2;
408
431
    else if (a->top == 1)
409
376
        return a->d[0];
410
    /* a->top == 0 */
411
55
    return 0;
412
446
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.23M
{
416
3.23M
    bn_check_top(a);
417
3.23M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.23M
    a->neg = 0;
420
3.23M
    a->d[0] = w;
421
3.23M
    a->top = (w ? 1 : 0);
422
3.23M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.23M
    bn_check_top(a);
424
3.23M
    return 1;
425
3.23M
}
426
427
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
428
1.41M
{
429
1.41M
    unsigned int i, m;
430
1.41M
    unsigned int n;
431
1.41M
    BN_ULONG l;
432
1.41M
    BIGNUM *bn = NULL;
433
434
1.41M
    if (ret == NULL)
435
770k
        ret = bn = BN_new();
436
1.41M
    if (ret == NULL)
437
0
        return NULL;
438
1.41M
    bn_check_top(ret);
439
    /* Skip leading zero's. */
440
3.67M
    for (; len > 0 && *s == 0; s++, len--)
441
2.25M
        continue;
442
1.41M
    n = len;
443
1.41M
    if (n == 0) {
444
161k
        ret->top = 0;
445
161k
        return ret;
446
161k
    }
447
1.25M
    i = ((n - 1) / BN_BYTES) + 1;
448
1.25M
    m = ((n - 1) % (BN_BYTES));
449
1.25M
    if (bn_wexpand(ret, (int)i) == NULL) {
450
0
        BN_free(bn);
451
0
        return NULL;
452
0
    }
453
1.25M
    ret->top = i;
454
1.25M
    ret->neg = 0;
455
1.25M
    l = 0;
456
106M
    while (n--) {
457
105M
        l = (l << 8L) | *(s++);
458
105M
        if (m-- == 0) {
459
13.6M
            ret->d[--i] = l;
460
13.6M
            l = 0;
461
13.6M
            m = BN_BYTES - 1;
462
13.6M
        }
463
105M
    }
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.25M
    bn_correct_top(ret);
469
1.25M
    return ret;
470
1.25M
}
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
300k
{
478
300k
    int n;
479
300k
    size_t i, lasti, j, atop, mask;
480
300k
    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
300k
    n = BN_num_bytes(a);
488
300k
    if (tolen == -1) {
489
163k
        tolen = n;
490
163k
    } else if (tolen < n) { /* uncommon/unlike case */
491
1.00k
        BIGNUM temp = *a;
492
493
1.00k
        bn_correct_top(&temp);
494
1.00k
        n = BN_num_bytes(&temp);
495
1.00k
        if (tolen < n)
496
1.00k
            return -1;
497
1.00k
    }
498
499
    /* Swipe through whole available data and don't give away padded zero. */
500
299k
    atop = a->dmax * BN_BYTES;
501
299k
    if (atop == 0) {
502
47.0k
        if (tolen != 0)
503
949
            memset(to, '\0', tolen);
504
47.0k
        return tolen;
505
47.0k
    }
506
507
252k
    lasti = atop - 1;
508
252k
    atop = a->top * BN_BYTES;
509
252k
    if (endianess == big)
510
154k
        to += tolen; /* start from the end of the buffer */
511
22.1M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
512
21.8M
        unsigned char val;
513
21.8M
        l = a->d[i / BN_BYTES];
514
21.8M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
515
21.8M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
516
21.8M
        if (endianess == big)
517
11.9M
            *--to = val;
518
9.97M
        else
519
9.97M
            *to++ = val;
520
21.8M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
521
21.8M
    }
522
523
252k
    return tolen;
524
299k
}
525
526
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
527
298k
{
528
298k
    if (tolen < 0)
529
0
        return -1;
530
298k
    return bn2binpad(a, to, tolen, big);
531
298k
}
532
533
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
534
728k
{
535
728k
    return bn2binpad(a, to, -1, big);
536
728k
}
537
538
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
98.2k
{
540
98.2k
    unsigned int i, m;
541
98.2k
    unsigned int n;
542
98.2k
    BN_ULONG l;
543
98.2k
    BIGNUM *bn = NULL;
544
545
98.2k
    if (ret == NULL)
546
85.7k
        ret = bn = BN_new();
547
98.2k
    if (ret == NULL)
548
0
        return NULL;
549
98.2k
    bn_check_top(ret);
550
98.2k
    s += len;
551
    /* Skip trailing zeroes. */
552
118k
    for (; len > 0 && s[-1] == 0; s--, len--)
553
19.8k
        continue;
554
98.2k
    n = len;
555
98.2k
    if (n == 0) {
556
299
        ret->top = 0;
557
299
        return ret;
558
299
    }
559
97.9k
    i = ((n - 1) / BN_BYTES) + 1;
560
97.9k
    m = ((n - 1) % (BN_BYTES));
561
97.9k
    if (bn_wexpand(ret, (int)i) == NULL) {
562
0
        BN_free(bn);
563
0
        return NULL;
564
0
    }
565
97.9k
    ret->top = i;
566
97.9k
    ret->neg = 0;
567
97.9k
    l = 0;
568
10.0M
    while (n--) {
569
9.98M
        s--;
570
9.98M
        l = (l << 8L) | *s;
571
9.98M
        if (m-- == 0) {
572
1.26M
            ret->d[--i] = l;
573
1.26M
            l = 0;
574
1.26M
            m = BN_BYTES - 1;
575
1.26M
        }
576
9.98M
    }
577
    /*
578
     * need to call this due to clear byte at top if avoiding having the top
579
     * bit set (-ve number)
580
     */
581
97.9k
    bn_correct_top(ret);
582
97.9k
    return ret;
583
97.9k
}
584
585
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
586
1.15M
{
587
1.15M
    if (tolen < 0)
588
0
        return -1;
589
1.15M
    return bn2binpad(a, to, tolen, little);
590
1.15M
}
591
592
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
593
1.08M
{
594
1.08M
    DECLARE_IS_ENDIAN;
595
596
1.08M
    if (IS_LITTLE_ENDIAN)
597
1.08M
        return BN_lebin2bn(s, len, ret);
598
0
    return BN_bin2bn(s, len, ret);
599
1.08M
}
600
601
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
602
1.08M
{
603
1.08M
    DECLARE_IS_ENDIAN;
604
605
1.08M
    if (IS_LITTLE_ENDIAN)
606
1.08M
        return BN_bn2lebinpad(a, to, tolen);
607
0
    return BN_bn2binpad(a, to, tolen);
608
1.08M
}
609
610
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
611
131M
{
612
131M
    int i;
613
131M
    BN_ULONG t1, t2, *ap, *bp;
614
615
131M
    ap = a->d;
616
131M
    bp = b->d;
617
618
131M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
619
2.49M
        && a->top == b->top) {
620
2.48M
        int res = 0;
621
622
12.6M
        for (i = 0; i < b->top; i++) {
623
10.1M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
624
10.1M
                -1, res);
625
10.1M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
626
10.1M
                1, res);
627
10.1M
        }
628
2.48M
        return res;
629
2.48M
    }
630
631
129M
    bn_check_top(a);
632
129M
    bn_check_top(b);
633
634
129M
    i = a->top - b->top;
635
129M
    if (i != 0)
636
15.2M
        return i;
637
638
120M
    for (i = a->top - 1; i >= 0; i--) {
639
118M
        t1 = ap[i];
640
118M
        t2 = bp[i];
641
118M
        if (t1 != t2)
642
112M
            return ((t1 > t2) ? 1 : -1);
643
118M
    }
644
2.15M
    return 0;
645
114M
}
646
647
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
648
18.1M
{
649
18.1M
    int i;
650
18.1M
    int gt, lt;
651
18.1M
    BN_ULONG t1, t2;
652
653
18.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
18.1M
    bn_check_top(a);
663
18.1M
    bn_check_top(b);
664
665
18.1M
    if (a->neg != b->neg) {
666
1.19k
        if (a->neg)
667
449
            return -1;
668
747
        else
669
747
            return 1;
670
1.19k
    }
671
18.1M
    if (a->neg == 0) {
672
18.1M
        gt = 1;
673
18.1M
        lt = -1;
674
18.1M
    } else {
675
10.3k
        gt = -1;
676
10.3k
        lt = 1;
677
10.3k
    }
678
679
18.1M
    if (a->top > b->top)
680
2.11M
        return gt;
681
16.0M
    if (a->top < b->top)
682
1.66M
        return lt;
683
41.5M
    for (i = a->top - 1; i >= 0; i--) {
684
39.2M
        t1 = a->d[i];
685
39.2M
        t2 = b->d[i];
686
39.2M
        if (t1 > t2)
687
4.96M
            return gt;
688
34.3M
        if (t1 < t2)
689
7.11M
            return lt;
690
34.3M
    }
691
2.31M
    return 0;
692
14.3M
}
693
694
int BN_set_bit(BIGNUM *a, int n)
695
2.42M
{
696
2.42M
    int i, j, k;
697
698
2.42M
    if (n < 0)
699
0
        return 0;
700
701
2.42M
    i = n / BN_BITS2;
702
2.42M
    j = n % BN_BITS2;
703
2.42M
    if (a->top <= i) {
704
2.42M
        if (bn_wexpand(a, i + 1) == NULL)
705
0
            return 0;
706
20.3M
        for (k = a->top; k < i + 1; k++)
707
17.9M
            a->d[k] = 0;
708
2.42M
        a->top = i + 1;
709
2.42M
        a->flags &= ~BN_FLG_FIXED_TOP;
710
2.42M
    }
711
712
2.42M
    a->d[i] |= (((BN_ULONG)1) << j);
713
2.42M
    bn_check_top(a);
714
2.42M
    return 1;
715
2.42M
}
716
717
int BN_clear_bit(BIGNUM *a, int n)
718
195
{
719
195
    int i, j;
720
721
195
    bn_check_top(a);
722
195
    if (n < 0)
723
0
        return 0;
724
725
195
    i = n / BN_BITS2;
726
195
    j = n % BN_BITS2;
727
195
    if (a->top <= i)
728
0
        return 0;
729
730
195
    a->d[i] &= (~(((BN_ULONG)1) << j));
731
195
    bn_correct_top(a);
732
195
    return 1;
733
195
}
734
735
int BN_is_bit_set(const BIGNUM *a, int n)
736
201M
{
737
201M
    int i, j;
738
739
201M
    bn_check_top(a);
740
201M
    if (n < 0)
741
4.46k
        return 0;
742
201M
    i = n / BN_BITS2;
743
201M
    j = n % BN_BITS2;
744
201M
    if (a->top <= i)
745
8.55k
        return 0;
746
201M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
747
201M
}
748
749
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
750
7.03k
{
751
7.03k
    int b, w;
752
753
7.03k
    if (n < 0)
754
0
        return 0;
755
756
7.03k
    w = n / BN_BITS2;
757
7.03k
    b = n % BN_BITS2;
758
7.03k
    if (w >= a->top)
759
0
        return 0;
760
7.03k
    if (b == 0)
761
6.70k
        a->top = w;
762
339
    else {
763
339
        a->top = w + 1;
764
339
        a->d[w] &= ~(BN_MASK2 << b);
765
339
    }
766
7.03k
    a->flags |= BN_FLG_FIXED_TOP;
767
7.03k
    return 1;
768
7.03k
}
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.39M
{
783
2.39M
    if (b && !BN_is_zero(a))
784
544k
        a->neg = 1;
785
1.85M
    else
786
1.85M
        a->neg = 0;
787
2.39M
}
788
789
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
790
78.9M
{
791
78.9M
    int i;
792
78.9M
    BN_ULONG aa, bb;
793
794
78.9M
    if (n == 0)
795
0
        return 0;
796
797
78.9M
    aa = a[n - 1];
798
78.9M
    bb = b[n - 1];
799
78.9M
    if (aa != bb)
800
70.6M
        return ((aa > bb) ? 1 : -1);
801
70.8M
    for (i = n - 2; i >= 0; i--) {
802
67.5M
        aa = a[i];
803
67.5M
        bb = b[i];
804
67.5M
        if (aa != bb)
805
5.00M
            return ((aa > bb) ? 1 : -1);
806
67.5M
    }
807
3.36M
    return 0;
808
8.36M
}
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
121M
{
821
121M
    int n, i;
822
121M
    n = cl - 1;
823
824
121M
    if (dl < 0) {
825
3.38M
        for (i = dl; i < 0; i++) {
826
3.27M
            if (b[n - i] != 0)
827
1.72M
                return -1; /* a < b */
828
3.27M
        }
829
1.82M
    }
830
119M
    if (dl > 0) {
831
4.92M
        for (i = dl; i > 0; i--) {
832
4.65M
            if (a[n + i] != 0)
833
1.56M
                return 1; /* a > b */
834
4.65M
        }
835
1.83M
    }
836
117M
    return bn_cmp_words(a, b, cl);
837
119M
}
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
2.17M
{
848
2.17M
    BN_ULONG t;
849
2.17M
    int i;
850
851
2.17M
    if (a == b)
852
0
        return;
853
854
2.17M
    bn_wcheck_size(a, nwords);
855
2.17M
    bn_wcheck_size(b, nwords);
856
857
2.17M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
858
859
2.17M
    t = (a->top ^ b->top) & condition;
860
2.17M
    a->top ^= t;
861
2.17M
    b->top ^= t;
862
863
2.17M
    t = (a->neg ^ b->neg) & condition;
864
2.17M
    a->neg ^= t;
865
2.17M
    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
2.17M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
890
891
2.17M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
892
2.17M
    a->flags ^= t;
893
2.17M
    b->flags ^= t;
894
895
    /* conditionally swap the data */
896
13.0M
    for (i = 0; i < nwords; i++) {
897
10.8M
        t = (a->d[i] ^ b->d[i]) & condition;
898
10.8M
        a->d[i] ^= t;
899
10.8M
        b->d[i] ^= t;
900
10.8M
    }
901
2.17M
}
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
207k
{
909
207k
    int secbits, bits;
910
207k
    if (L >= 15360)
911
935
        secbits = 256;
912
206k
    else if (L >= 7680)
913
1.13k
        secbits = 192;
914
204k
    else if (L >= 3072)
915
2.87k
        secbits = 128;
916
202k
    else if (L >= 2048)
917
4.85k
        secbits = 112;
918
197k
    else if (L >= 1024)
919
122k
        secbits = 80;
920
74.3k
    else
921
74.3k
        return 0;
922
132k
    if (N == -1)
923
12.5k
        return secbits;
924
120k
    bits = N / 2;
925
120k
    if (bits < 80)
926
9.97k
        return 0;
927
110k
    return bits >= secbits ? secbits : bits;
928
120k
}
929
930
void BN_zero_ex(BIGNUM *a)
931
1.07G
{
932
1.07G
    a->neg = 0;
933
1.07G
    a->top = 0;
934
1.07G
    a->flags &= ~BN_FLG_FIXED_TOP;
935
1.07G
}
936
937
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
938
99.5M
{
939
99.5M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
940
99.5M
}
941
942
int BN_is_zero(const BIGNUM *a)
943
398M
{
944
398M
    return a->top == 0;
945
398M
}
946
947
int BN_is_one(const BIGNUM *a)
948
97.8M
{
949
97.8M
    return BN_abs_is_word(a, 1) && !a->neg;
950
97.8M
}
951
952
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
953
358k
{
954
358k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
955
358k
}
956
957
int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w)
958
6.71k
{
959
6.71k
    int res, i;
960
6.71k
    const BN_ULONG *ap = a->d;
961
962
6.71k
    if (a->neg || a->top == 0)
963
0
        return 0;
964
965
6.71k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
966
967
26.8k
    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.71k
    return res;
971
6.71k
}
972
973
int BN_is_odd(const BIGNUM *a)
974
69.6M
{
975
69.6M
    return (a->top > 0) && (a->d[0] & 1);
976
69.6M
}
977
978
int BN_is_negative(const BIGNUM *a)
979
8.58M
{
980
8.58M
    return (a->neg != 0);
981
8.58M
}
982
983
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
984
    BN_CTX *ctx)
985
2.59M
{
986
2.59M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
987
2.59M
}
988
989
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
990
20.9M
{
991
20.9M
    dest->d = b->d;
992
20.9M
    dest->top = b->top;
993
20.9M
    dest->dmax = b->dmax;
994
20.9M
    dest->neg = b->neg;
995
20.9M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
996
20.9M
        | (b->flags & ~BN_FLG_MALLOCED)
997
20.9M
        | BN_FLG_STATIC_DATA | flags);
998
20.9M
}
999
1000
BN_GENCB *BN_GENCB_new(void)
1001
4.95k
{
1002
4.95k
    BN_GENCB *ret;
1003
1004
4.95k
    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
4.95k
    return ret;
1010
4.95k
}
1011
1012
void BN_GENCB_free(BN_GENCB *cb)
1013
6.85k
{
1014
6.85k
    if (cb == NULL)
1015
1.90k
        return;
1016
4.95k
    OPENSSL_free(cb);
1017
4.95k
}
1018
1019
void BN_set_flags(BIGNUM *b, int n)
1020
2.20M
{
1021
2.20M
    b->flags |= n;
1022
2.20M
}
1023
1024
int BN_get_flags(const BIGNUM *b, int n)
1025
634M
{
1026
634M
    return b->flags & n;
1027
634M
}
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
4.95k
{
1043
4.95k
    BN_GENCB *tmp_gencb = gencb;
1044
4.95k
    tmp_gencb->ver = 2;
1045
4.95k
    tmp_gencb->arg = cb_arg;
1046
4.95k
    tmp_gencb->cb.cb_2 = callback;
1047
4.95k
}
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.04G
{
1056
2.04G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1057
2.04G
}
1058
1059
void bn_correct_top_consttime(BIGNUM *a)
1060
16.3k
{
1061
16.3k
    int j, atop;
1062
16.3k
    BN_ULONG limb;
1063
16.3k
    unsigned int mask;
1064
1065
1.06M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1066
1.04M
        limb = a->d[j];
1067
1.04M
        limb |= 0 - limb;
1068
1.04M
        limb >>= BN_BITS2 - 1;
1069
1.04M
        limb = 0 - limb;
1070
1.04M
        mask = (unsigned int)limb;
1071
1.04M
        mask &= constant_time_msb(j - a->top);
1072
1.04M
        atop = constant_time_select_int(mask, j + 1, atop);
1073
1.04M
    }
1074
1075
16.3k
    mask = constant_time_eq_int(atop, 0);
1076
16.3k
    a->top = atop;
1077
16.3k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1078
16.3k
    a->flags &= ~BN_FLG_FIXED_TOP;
1079
16.3k
}
1080
1081
void bn_correct_top(BIGNUM *a)
1082
781M
{
1083
781M
    BN_ULONG *ftl;
1084
781M
    int tmp_top = a->top;
1085
1086
781M
    if (tmp_top > 0) {
1087
2.07G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1088
2.07G
            ftl--;
1089
2.07G
            if (*ftl != 0)
1090
778M
                break;
1091
2.07G
        }
1092
780M
        a->top = tmp_top;
1093
780M
    }
1094
781M
    if (a->top == 0)
1095
3.31M
        a->neg = 0;
1096
781M
    a->flags &= ~BN_FLG_FIXED_TOP;
1097
781M
    bn_pollute(a);
1098
781M
}