Coverage Report

Created: 2025-12-31 06:58

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.90M
{
84
1.90M
    static const BN_ULONG data_one = 1L;
85
1.90M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
1.90M
    return &const_one;
88
1.90M
}
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
242M
{
101
242M
    BN_ULONG x, mask;
102
242M
    int bits = (l != 0);
103
104
242M
#if BN_BITS2 > 32
105
242M
    x = l >> 32;
106
242M
    mask = (0 - x) & BN_MASK2;
107
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
242M
    bits += 32 & mask;
109
242M
    l ^= (x ^ l) & mask;
110
242M
#endif
111
112
242M
    x = l >> 16;
113
242M
    mask = (0 - x) & BN_MASK2;
114
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
242M
    bits += 16 & mask;
116
242M
    l ^= (x ^ l) & mask;
117
118
242M
    x = l >> 8;
119
242M
    mask = (0 - x) & BN_MASK2;
120
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
242M
    bits += 8 & mask;
122
242M
    l ^= (x ^ l) & mask;
123
124
242M
    x = l >> 4;
125
242M
    mask = (0 - x) & BN_MASK2;
126
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
242M
    bits += 4 & mask;
128
242M
    l ^= (x ^ l) & mask;
129
130
242M
    x = l >> 2;
131
242M
    mask = (0 - x) & BN_MASK2;
132
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
242M
    bits += 2 & mask;
134
242M
    l ^= (x ^ l) & mask;
135
136
242M
    x = l >> 1;
137
242M
    mask = (0 - x) & BN_MASK2;
138
242M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
242M
    bits += 1 & mask;
140
141
242M
    return bits;
142
242M
}
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
6.29M
{
153
6.29M
    int j, ret;
154
6.29M
    unsigned int mask, past_i;
155
6.29M
    int i = a->top - 1;
156
6.29M
    bn_check_top(a);
157
158
68.5M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
62.2M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
62.2M
        ret += BN_BITS2 & (~mask & ~past_i);
162
62.2M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
62.2M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
62.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
6.29M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
6.29M
    return ret & mask;
174
6.29M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
31.3M
{
178
31.3M
    int i = a->top - 1;
179
31.3M
    bn_check_top(a);
180
181
31.3M
    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
5.16M
        return bn_num_bits_consttime(a);
192
5.16M
    }
193
194
26.1M
    if (BN_is_zero(a))
195
452k
        return 0;
196
197
25.7M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
26.1M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
77.2M
{
202
77.2M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.50M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
75.7M
    else if (clear != 0)
205
45.0M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
30.7M
    else
207
30.7M
        OPENSSL_free(a->d);
208
77.2M
}
209
210
void BN_clear_free(BIGNUM *a)
211
37.4M
{
212
37.4M
    if (a == NULL)
213
6.12M
        return;
214
31.3M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
28.4M
        bn_free_d(a, 1);
216
31.3M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
2.97M
        OPENSSL_cleanse(a, sizeof(*a));
218
2.97M
        OPENSSL_free(a);
219
2.97M
    }
220
31.3M
}
221
222
void BN_free(BIGNUM *a)
223
49.5M
{
224
49.5M
    if (a == NULL)
225
18.7M
        return;
226
30.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
30.7M
        bn_free_d(a, 0);
228
30.7M
    if (a->flags & BN_FLG_MALLOCED)
229
30.7M
        OPENSSL_free(a);
230
30.7M
}
231
232
void bn_init(BIGNUM *a)
233
72.9M
{
234
72.9M
    static BIGNUM nilbn;
235
236
72.9M
    *a = nilbn;
237
72.9M
    bn_check_top(a);
238
72.9M
}
239
240
BIGNUM *BN_new(void)
241
33.7M
{
242
33.7M
    BIGNUM *ret;
243
244
33.7M
    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
33.7M
    ret->flags = BN_FLG_MALLOCED;
249
33.7M
    bn_check_top(ret);
250
33.7M
    return ret;
251
33.7M
}
252
253
BIGNUM *BN_secure_new(void)
254
1.57M
{
255
1.57M
    BIGNUM *ret = BN_new();
256
1.57M
    if (ret != NULL)
257
1.57M
        ret->flags |= BN_FLG_SECURE;
258
1.57M
    return ret;
259
1.57M
}
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
54.7M
{
265
54.7M
    BN_ULONG *a = NULL;
266
267
54.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
54.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
54.7M
    if (BN_get_flags(b, BN_FLG_SECURE))
276
1.17M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
277
53.5M
    else
278
53.5M
        a = OPENSSL_zalloc(words * sizeof(*a));
279
54.7M
    if (a == NULL) {
280
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
281
0
        return NULL;
282
0
    }
283
284
54.7M
    assert(b->top <= words);
285
54.7M
    if (b->top > 0)
286
6.91M
        memcpy(a, b->d, sizeof(*a) * b->top);
287
288
54.7M
    return a;
289
54.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
54.7M
{
301
54.7M
    if (words > b->dmax) {
302
54.7M
        BN_ULONG *a = bn_expand_internal(b, words);
303
54.7M
        if (!a)
304
0
            return NULL;
305
54.7M
        if (b->d != NULL)
306
13.6M
            bn_free_d(b, 1);
307
54.7M
        b->d = a;
308
54.7M
        b->dmax = words;
309
54.7M
    }
310
311
54.7M
    return b;
312
54.7M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
3.83M
{
316
3.83M
    BIGNUM *t;
317
318
3.83M
    if (a == NULL)
319
0
        return NULL;
320
3.83M
    bn_check_top(a);
321
322
3.83M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
3.83M
    if (t == NULL)
324
0
        return NULL;
325
3.83M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
3.83M
    bn_check_top(t);
330
3.83M
    return t;
331
3.83M
}
332
333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
334
111M
{
335
111M
    int bn_words;
336
337
111M
    bn_check_top(b);
338
339
111M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
340
341
111M
    if (a == b)
342
7
        return a;
343
111M
    if (bn_wexpand(a, bn_words) == NULL)
344
0
        return NULL;
345
346
111M
    if (b->top > 0)
347
110M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
348
349
111M
    a->neg = b->neg;
350
111M
    a->top = b->top;
351
111M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
352
111M
    bn_check_top(a);
353
111M
    return a;
354
111M
}
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
249k
{
394
249k
    if (a == NULL)
395
0
        return;
396
249k
    bn_check_top(a);
397
249k
    if (a->d != NULL)
398
41.9k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
249k
    a->neg = 0;
400
249k
    a->top = 0;
401
249k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
249k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
437
{
406
437
    if (a->top > 1)
407
14
        return BN_MASK2;
408
423
    else if (a->top == 1)
409
372
        return a->d[0];
410
    /* a->top == 0 */
411
51
    return 0;
412
437
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.13M
{
416
3.13M
    bn_check_top(a);
417
3.13M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.13M
    a->neg = 0;
420
3.13M
    a->d[0] = w;
421
3.13M
    a->top = (w ? 1 : 0);
422
3.13M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.13M
    bn_check_top(a);
424
3.13M
    return 1;
425
3.13M
}
426
427
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
428
1.71M
{
429
1.71M
    unsigned int i, m;
430
1.71M
    unsigned int n;
431
1.71M
    BN_ULONG l;
432
1.71M
    BIGNUM *bn = NULL;
433
434
1.71M
    if (ret == NULL)
435
1.09M
        ret = bn = BN_new();
436
1.71M
    if (ret == NULL)
437
0
        return NULL;
438
1.71M
    bn_check_top(ret);
439
    /* Skip leading zero's. */
440
4.17M
    for (; len > 0 && *s == 0; s++, len--)
441
2.46M
        continue;
442
1.71M
    n = len;
443
1.71M
    if (n == 0) {
444
208k
        ret->top = 0;
445
208k
        return ret;
446
208k
    }
447
1.50M
    i = ((n - 1) / BN_BYTES) + 1;
448
1.50M
    m = ((n - 1) % (BN_BYTES));
449
1.50M
    if (bn_wexpand(ret, (int)i) == NULL) {
450
0
        BN_free(bn);
451
0
        return NULL;
452
0
    }
453
1.50M
    ret->top = i;
454
1.50M
    ret->neg = 0;
455
1.50M
    l = 0;
456
101M
    while (n--) {
457
99.7M
        l = (l << 8L) | *(s++);
458
99.7M
        if (m-- == 0) {
459
13.1M
            ret->d[--i] = l;
460
13.1M
            l = 0;
461
13.1M
            m = BN_BYTES - 1;
462
13.1M
        }
463
99.7M
    }
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.50M
    bn_correct_top(ret);
469
1.50M
    return ret;
470
1.50M
}
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
241k
{
478
241k
    int n;
479
241k
    size_t i, lasti, j, atop, mask;
480
241k
    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
241k
    n = BN_num_bytes(a);
488
241k
    if (tolen == -1) {
489
98.3k
        tolen = n;
490
143k
    } else if (tolen < n) { /* uncommon/unlike case */
491
922
        BIGNUM temp = *a;
492
493
922
        bn_correct_top(&temp);
494
922
        n = BN_num_bytes(&temp);
495
922
        if (tolen < n)
496
922
            return -1;
497
922
    }
498
499
    /* Swipe through whole available data and don't give away padded zero. */
500
240k
    atop = a->dmax * BN_BYTES;
501
240k
    if (atop == 0) {
502
11.1k
        if (tolen != 0)
503
816
            memset(to, '\0', tolen);
504
11.1k
        return tolen;
505
11.1k
    }
506
507
229k
    lasti = atop - 1;
508
229k
    atop = a->top * BN_BYTES;
509
229k
    if (endianess == big)
510
127k
        to += tolen; /* start from the end of the buffer */
511
19.9M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
512
19.7M
        unsigned char val;
513
19.7M
        l = a->d[i / BN_BYTES];
514
19.7M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
515
19.7M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
516
19.7M
        if (endianess == big)
517
9.33M
            *--to = val;
518
10.4M
        else
519
10.4M
            *to++ = val;
520
19.7M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
521
19.7M
    }
522
523
229k
    return tolen;
524
240k
}
525
526
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
527
178k
{
528
178k
    if (tolen < 0)
529
0
        return -1;
530
178k
    return bn2binpad(a, to, tolen, big);
531
178k
}
532
533
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
534
609k
{
535
609k
    return bn2binpad(a, to, -1, big);
536
609k
}
537
538
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
102k
{
540
102k
    unsigned int i, m;
541
102k
    unsigned int n;
542
102k
    BN_ULONG l;
543
102k
    BIGNUM *bn = NULL;
544
545
102k
    if (ret == NULL)
546
89.5k
        ret = bn = BN_new();
547
102k
    if (ret == NULL)
548
0
        return NULL;
549
102k
    bn_check_top(ret);
550
102k
    s += len;
551
    /* Skip trailing zeroes. */
552
121k
    for (; len > 0 && s[-1] == 0; s--, len--)
553
18.7k
        continue;
554
102k
    n = len;
555
102k
    if (n == 0) {
556
255
        ret->top = 0;
557
255
        return ret;
558
255
    }
559
102k
    i = ((n - 1) / BN_BYTES) + 1;
560
102k
    m = ((n - 1) % (BN_BYTES));
561
102k
    if (bn_wexpand(ret, (int)i) == NULL) {
562
0
        BN_free(bn);
563
0
        return NULL;
564
0
    }
565
102k
    ret->top = i;
566
102k
    ret->neg = 0;
567
102k
    l = 0;
568
10.5M
    while (n--) {
569
10.4M
        s--;
570
10.4M
        l = (l << 8L) | *s;
571
10.4M
        if (m-- == 0) {
572
1.32M
            ret->d[--i] = l;
573
1.32M
            l = 0;
574
1.32M
            m = BN_BYTES - 1;
575
1.32M
        }
576
10.4M
    }
577
    /*
578
     * need to call this due to clear byte at top if avoiding having the top
579
     * bit set (-ve number)
580
     */
581
102k
    bn_correct_top(ret);
582
102k
    return ret;
583
102k
}
584
585
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
586
1.29M
{
587
1.29M
    if (tolen < 0)
588
0
        return -1;
589
1.29M
    return bn2binpad(a, to, tolen, little);
590
1.29M
}
591
592
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
593
1.23M
{
594
1.23M
    DECLARE_IS_ENDIAN;
595
596
1.23M
    if (IS_LITTLE_ENDIAN)
597
1.23M
        return BN_lebin2bn(s, len, ret);
598
0
    return BN_bin2bn(s, len, ret);
599
1.23M
}
600
601
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
602
1.23M
{
603
1.23M
    DECLARE_IS_ENDIAN;
604
605
1.23M
    if (IS_LITTLE_ENDIAN)
606
1.23M
        return BN_bn2lebinpad(a, to, tolen);
607
0
    return BN_bn2binpad(a, to, tolen);
608
1.23M
}
609
610
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
611
127M
{
612
127M
    int i;
613
127M
    BN_ULONG t1, t2, *ap, *bp;
614
615
127M
    ap = a->d;
616
127M
    bp = b->d;
617
618
127M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
619
2.27M
        && a->top == b->top) {
620
2.26M
        int res = 0;
621
622
11.6M
        for (i = 0; i < b->top; i++) {
623
9.35M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
624
9.35M
                -1, res);
625
9.35M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
626
9.35M
                1, res);
627
9.35M
        }
628
2.26M
        return res;
629
2.26M
    }
630
631
125M
    bn_check_top(a);
632
125M
    bn_check_top(b);
633
634
125M
    i = a->top - b->top;
635
125M
    if (i != 0)
636
15.3M
        return i;
637
638
116M
    for (i = a->top - 1; i >= 0; i--) {
639
114M
        t1 = ap[i];
640
114M
        t2 = bp[i];
641
114M
        if (t1 != t2)
642
107M
            return ((t1 > t2) ? 1 : -1);
643
114M
    }
644
2.08M
    return 0;
645
109M
}
646
647
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
648
17.5M
{
649
17.5M
    int i;
650
17.5M
    int gt, lt;
651
17.5M
    BN_ULONG t1, t2;
652
653
17.5M
    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
17.5M
    bn_check_top(a);
663
17.5M
    bn_check_top(b);
664
665
17.5M
    if (a->neg != b->neg) {
666
1.00k
        if (a->neg)
667
331
            return -1;
668
675
        else
669
675
            return 1;
670
1.00k
    }
671
17.5M
    if (a->neg == 0) {
672
17.5M
        gt = 1;
673
17.5M
        lt = -1;
674
17.5M
    } else {
675
10.4k
        gt = -1;
676
10.4k
        lt = 1;
677
10.4k
    }
678
679
17.5M
    if (a->top > b->top)
680
2.26M
        return gt;
681
15.2M
    if (a->top < b->top)
682
1.41M
        return lt;
683
41.7M
    for (i = a->top - 1; i >= 0; i--) {
684
39.4M
        t1 = a->d[i];
685
39.4M
        t2 = b->d[i];
686
39.4M
        if (t1 > t2)
687
4.89M
            return gt;
688
34.5M
        if (t1 < t2)
689
6.63M
            return lt;
690
34.5M
    }
691
2.35M
    return 0;
692
13.8M
}
693
694
int BN_set_bit(BIGNUM *a, int n)
695
2.31M
{
696
2.31M
    int i, j, k;
697
698
2.31M
    if (n < 0)
699
0
        return 0;
700
701
2.31M
    i = n / BN_BITS2;
702
2.31M
    j = n % BN_BITS2;
703
2.31M
    if (a->top <= i) {
704
2.31M
        if (bn_wexpand(a, i + 1) == NULL)
705
0
            return 0;
706
20.1M
        for (k = a->top; k < i + 1; k++)
707
17.8M
            a->d[k] = 0;
708
2.31M
        a->top = i + 1;
709
2.31M
        a->flags &= ~BN_FLG_FIXED_TOP;
710
2.31M
    }
711
712
2.31M
    a->d[i] |= (((BN_ULONG)1) << j);
713
2.31M
    bn_check_top(a);
714
2.31M
    return 1;
715
2.31M
}
716
717
int BN_clear_bit(BIGNUM *a, int n)
718
176
{
719
176
    int i, j;
720
721
176
    bn_check_top(a);
722
176
    if (n < 0)
723
0
        return 0;
724
725
176
    i = n / BN_BITS2;
726
176
    j = n % BN_BITS2;
727
176
    if (a->top <= i)
728
0
        return 0;
729
730
176
    a->d[i] &= (~(((BN_ULONG)1) << j));
731
176
    bn_correct_top(a);
732
176
    return 1;
733
176
}
734
735
int BN_is_bit_set(const BIGNUM *a, int n)
736
200M
{
737
200M
    int i, j;
738
739
200M
    bn_check_top(a);
740
200M
    if (n < 0)
741
5.06k
        return 0;
742
200M
    i = n / BN_BITS2;
743
200M
    j = n % BN_BITS2;
744
200M
    if (a->top <= i)
745
9.01k
        return 0;
746
200M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
747
200M
}
748
749
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
750
6.75k
{
751
6.75k
    int b, w;
752
753
6.75k
    if (n < 0)
754
0
        return 0;
755
756
6.75k
    w = n / BN_BITS2;
757
6.75k
    b = n % BN_BITS2;
758
6.75k
    if (w >= a->top)
759
0
        return 0;
760
6.75k
    if (b == 0)
761
6.75k
        a->top = w;
762
0
    else {
763
0
        a->top = w + 1;
764
0
        a->d[w] &= ~(BN_MASK2 << b);
765
0
    }
766
6.75k
    a->flags |= BN_FLG_FIXED_TOP;
767
6.75k
    return 1;
768
6.75k
}
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.50M
{
783
2.50M
    if (b && !BN_is_zero(a))
784
801k
        a->neg = 1;
785
1.70M
    else
786
1.70M
        a->neg = 0;
787
2.50M
}
788
789
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
790
68.7M
{
791
68.7M
    int i;
792
68.7M
    BN_ULONG aa, bb;
793
794
68.7M
    if (n == 0)
795
0
        return 0;
796
797
68.7M
    aa = a[n - 1];
798
68.7M
    bb = b[n - 1];
799
68.7M
    if (aa != bb)
800
61.9M
        return ((aa > bb) ? 1 : -1);
801
58.1M
    for (i = n - 2; i >= 0; i--) {
802
55.3M
        aa = a[i];
803
55.3M
        bb = b[i];
804
55.3M
        if (aa != bb)
805
4.04M
            return ((aa > bb) ? 1 : -1);
806
55.3M
    }
807
2.74M
    return 0;
808
6.78M
}
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
119M
{
821
119M
    int n, i;
822
119M
    n = cl - 1;
823
824
119M
    if (dl < 0) {
825
3.79M
        for (i = dl; i < 0; i++) {
826
3.70M
            if (b[n - i] != 0)
827
2.14M
                return -1; /* a < b */
828
3.70M
        }
829
2.23M
    }
830
117M
    if (dl > 0) {
831
5.06M
        for (i = dl; i > 0; i--) {
832
4.78M
            if (a[n + i] != 0)
833
1.96M
                return 1; /* a > b */
834
4.78M
        }
835
2.24M
    }
836
115M
    return bn_cmp_words(a, b, cl);
837
117M
}
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.10M
{
848
2.10M
    BN_ULONG t;
849
2.10M
    int i;
850
851
2.10M
    if (a == b)
852
0
        return;
853
854
2.10M
    bn_wcheck_size(a, nwords);
855
2.10M
    bn_wcheck_size(b, nwords);
856
857
2.10M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
858
859
2.10M
    t = (a->top ^ b->top) & condition;
860
2.10M
    a->top ^= t;
861
2.10M
    b->top ^= t;
862
863
2.10M
    t = (a->neg ^ b->neg) & condition;
864
2.10M
    a->neg ^= t;
865
2.10M
    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.10M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
890
891
2.10M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
892
2.10M
    a->flags ^= t;
893
2.10M
    b->flags ^= t;
894
895
    /* conditionally swap the data */
896
13.0M
    for (i = 0; i < nwords; i++) {
897
10.9M
        t = (a->d[i] ^ b->d[i]) & condition;
898
10.9M
        a->d[i] ^= t;
899
10.9M
        b->d[i] ^= t;
900
10.9M
    }
901
2.10M
}
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
194k
{
909
194k
    int secbits, bits;
910
194k
    if (L >= 15360)
911
911
        secbits = 256;
912
193k
    else if (L >= 7680)
913
1.15k
        secbits = 192;
914
192k
    else if (L >= 3072)
915
3.69k
        secbits = 128;
916
188k
    else if (L >= 2048)
917
4.96k
        secbits = 112;
918
183k
    else if (L >= 1024)
919
125k
        secbits = 80;
920
58.3k
    else
921
58.3k
        return 0;
922
136k
    if (N == -1)
923
13.1k
        return secbits;
924
122k
    bits = N / 2;
925
122k
    if (bits < 80)
926
9.41k
        return 0;
927
113k
    return bits >= secbits ? secbits : bits;
928
122k
}
929
930
void BN_zero_ex(BIGNUM *a)
931
1.10G
{
932
1.10G
    a->neg = 0;
933
1.10G
    a->top = 0;
934
1.10G
    a->flags &= ~BN_FLG_FIXED_TOP;
935
1.10G
}
936
937
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
938
94.3M
{
939
94.3M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
940
94.3M
}
941
942
int BN_is_zero(const BIGNUM *a)
943
393M
{
944
393M
    return a->top == 0;
945
393M
}
946
947
int BN_is_one(const BIGNUM *a)
948
92.7M
{
949
92.7M
    return BN_abs_is_word(a, 1) && !a->neg;
950
92.7M
}
951
952
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
953
351k
{
954
351k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
955
351k
}
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
27.0k
    for (i = 1; i < a->top; i++)
968
20.2k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
969
20.2k
            res, 0);
970
6.75k
    return res;
971
6.75k
}
972
973
int BN_is_odd(const BIGNUM *a)
974
66.1M
{
975
66.1M
    return (a->top > 0) && (a->d[0] & 1);
976
66.1M
}
977
978
int BN_is_negative(const BIGNUM *a)
979
9.49M
{
980
9.49M
    return (a->neg != 0);
981
9.49M
}
982
983
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
984
    BN_CTX *ctx)
985
2.62M
{
986
2.62M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
987
2.62M
}
988
989
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
990
22.0M
{
991
22.0M
    dest->d = b->d;
992
22.0M
    dest->top = b->top;
993
22.0M
    dest->dmax = b->dmax;
994
22.0M
    dest->neg = b->neg;
995
22.0M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
996
22.0M
        | (b->flags & ~BN_FLG_MALLOCED)
997
22.0M
        | BN_FLG_STATIC_DATA | flags);
998
22.0M
}
999
1000
BN_GENCB *BN_GENCB_new(void)
1001
5.13k
{
1002
5.13k
    BN_GENCB *ret;
1003
1004
5.13k
    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.13k
    return ret;
1010
5.13k
}
1011
1012
void BN_GENCB_free(BN_GENCB *cb)
1013
6.95k
{
1014
6.95k
    if (cb == NULL)
1015
1.81k
        return;
1016
5.13k
    OPENSSL_free(cb);
1017
5.13k
}
1018
1019
void BN_set_flags(BIGNUM *b, int n)
1020
2.04M
{
1021
2.04M
    b->flags |= n;
1022
2.04M
}
1023
1024
int BN_get_flags(const BIGNUM *b, int n)
1025
624M
{
1026
624M
    return b->flags & n;
1027
624M
}
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.13k
{
1043
5.13k
    BN_GENCB *tmp_gencb = gencb;
1044
5.13k
    tmp_gencb->ver = 2;
1045
5.13k
    tmp_gencb->arg = cb_arg;
1046
5.13k
    tmp_gencb->cb.cb_2 = callback;
1047
5.13k
}
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.15G
{
1056
2.15G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1057
2.15G
}
1058
1059
void bn_correct_top_consttime(BIGNUM *a)
1060
17.3k
{
1061
17.3k
    int j, atop;
1062
17.3k
    BN_ULONG limb;
1063
17.3k
    unsigned int mask;
1064
1065
1.12M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1066
1.10M
        limb = a->d[j];
1067
1.10M
        limb |= 0 - limb;
1068
1.10M
        limb >>= BN_BITS2 - 1;
1069
1.10M
        limb = 0 - limb;
1070
1.10M
        mask = (unsigned int)limb;
1071
1.10M
        mask &= constant_time_msb(j - a->top);
1072
1.10M
        atop = constant_time_select_int(mask, j + 1, atop);
1073
1.10M
    }
1074
1075
17.3k
    mask = constant_time_eq_int(atop, 0);
1076
17.3k
    a->top = atop;
1077
17.3k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1078
17.3k
    a->flags &= ~BN_FLG_FIXED_TOP;
1079
17.3k
}
1080
1081
void bn_correct_top(BIGNUM *a)
1082
932M
{
1083
932M
    BN_ULONG *ftl;
1084
932M
    int tmp_top = a->top;
1085
1086
932M
    if (tmp_top > 0) {
1087
2.68G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1088
2.68G
            ftl--;
1089
2.68G
            if (*ftl != 0)
1090
928M
                break;
1091
2.68G
        }
1092
931M
        a->top = tmp_top;
1093
931M
    }
1094
932M
    if (a->top == 0)
1095
3.33M
        a->neg = 0;
1096
932M
    a->flags &= ~BN_FLG_FIXED_TOP;
1097
932M
    bn_pollute(a);
1098
932M
}