Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <assert.h>
11
#include <limits.h>
12
#include "internal/cryptlib.h"
13
#include "internal/endian.h"
14
#include "bn_local.h"
15
#include <openssl/opensslconf.h>
16
#include "internal/constant_time.h"
17
18
/* This stuff appears to be completely unused, so is deprecated */
19
#ifndef OPENSSL_NO_DEPRECATED_0_9_8
20
/*-
21
 * For a 32 bit machine
22
 * 2 -   4 ==  128
23
 * 3 -   8 ==  256
24
 * 4 -  16 ==  512
25
 * 5 -  32 == 1024
26
 * 6 -  64 == 2048
27
 * 7 - 128 == 4096
28
 * 8 - 256 == 8192
29
 */
30
static int bn_limit_bits = 0;
31
static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
32
static int bn_limit_bits_low = 0;
33
static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
34
static int bn_limit_bits_high = 0;
35
static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
36
static int bn_limit_bits_mont = 0;
37
static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
38
39
void BN_set_params(int mult, int high, int low, int mont)
40
0
{
41
0
    if (mult >= 0) {
42
0
        if (mult > (int)(sizeof(int) * 8) - 1)
43
0
            mult = sizeof(int) * 8 - 1;
44
0
        bn_limit_bits = mult;
45
0
        bn_limit_num = 1 << mult;
46
0
    }
47
0
    if (high >= 0) {
48
0
        if (high > (int)(sizeof(int) * 8) - 1)
49
0
            high = sizeof(int) * 8 - 1;
50
0
        bn_limit_bits_high = high;
51
0
        bn_limit_num_high = 1 << high;
52
0
    }
53
0
    if (low >= 0) {
54
0
        if (low > (int)(sizeof(int) * 8) - 1)
55
0
            low = sizeof(int) * 8 - 1;
56
0
        bn_limit_bits_low = low;
57
0
        bn_limit_num_low = 1 << low;
58
0
    }
59
0
    if (mont >= 0) {
60
0
        if (mont > (int)(sizeof(int) * 8) - 1)
61
0
            mont = sizeof(int) * 8 - 1;
62
0
        bn_limit_bits_mont = mont;
63
0
        bn_limit_num_mont = 1 << mont;
64
0
    }
65
0
}
66
67
int BN_get_params(int which)
68
0
{
69
0
    if (which == 0)
70
0
        return bn_limit_bits;
71
0
    else if (which == 1)
72
0
        return bn_limit_bits_high;
73
0
    else if (which == 2)
74
0
        return bn_limit_bits_low;
75
0
    else if (which == 3)
76
0
        return bn_limit_bits_mont;
77
0
    else
78
0
        return 0;
79
0
}
80
#endif
81
82
const BIGNUM *BN_value_one(void)
83
2.03M
{
84
2.03M
    static const BN_ULONG data_one = 1L;
85
2.03M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
2.03M
    return &const_one;
88
2.03M
}
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
247M
{
101
247M
    BN_ULONG x, mask;
102
247M
    int bits = (l != 0);
103
104
247M
#if BN_BITS2 > 32
105
247M
    x = l >> 32;
106
247M
    mask = (0 - x) & BN_MASK2;
107
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
247M
    bits += 32 & mask;
109
247M
    l ^= (x ^ l) & mask;
110
247M
#endif
111
112
247M
    x = l >> 16;
113
247M
    mask = (0 - x) & BN_MASK2;
114
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
247M
    bits += 16 & mask;
116
247M
    l ^= (x ^ l) & mask;
117
118
247M
    x = l >> 8;
119
247M
    mask = (0 - x) & BN_MASK2;
120
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
247M
    bits += 8 & mask;
122
247M
    l ^= (x ^ l) & mask;
123
124
247M
    x = l >> 4;
125
247M
    mask = (0 - x) & BN_MASK2;
126
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
247M
    bits += 4 & mask;
128
247M
    l ^= (x ^ l) & mask;
129
130
247M
    x = l >> 2;
131
247M
    mask = (0 - x) & BN_MASK2;
132
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
247M
    bits += 2 & mask;
134
247M
    l ^= (x ^ l) & mask;
135
136
247M
    x = l >> 1;
137
247M
    mask = (0 - x) & BN_MASK2;
138
247M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
247M
    bits += 1 & mask;
140
141
247M
    return bits;
142
247M
}
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.04M
{
153
8.04M
    int j, ret;
154
8.04M
    unsigned int mask, past_i;
155
8.04M
    int i = a->top - 1;
156
8.04M
    bn_check_top(a);
157
158
67.4M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
59.4M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
59.4M
        ret += BN_BITS2 & (~mask & ~past_i);
162
59.4M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
59.4M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
59.4M
    }
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.04M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
8.04M
    return ret & mask;
174
8.04M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
34.0M
{
178
34.0M
    int i = a->top - 1;
179
34.0M
    bn_check_top(a);
180
181
34.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.04M
        return bn_num_bits_consttime(a);
192
6.04M
    }
193
194
28.0M
    if (BN_is_zero(a))
195
413k
        return 0;
196
197
27.5M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
28.0M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
82.9M
{
202
82.9M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.68M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
81.3M
    else if (clear != 0)
205
48.8M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
32.4M
    else
207
32.4M
        OPENSSL_free(a->d);
208
82.9M
}
209
210
void BN_clear_free(BIGNUM *a)
211
40.3M
{
212
40.3M
    if (a == NULL)
213
6.17M
        return;
214
34.2M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
30.9M
        bn_free_d(a, 1);
216
34.2M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
3.21M
        OPENSSL_cleanse(a, sizeof(*a));
218
3.21M
        OPENSSL_free(a);
219
3.21M
    }
220
34.2M
}
221
222
void BN_free(BIGNUM *a)
223
52.3M
{
224
52.3M
    if (a == NULL)
225
19.9M
        return;
226
32.4M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
32.4M
        bn_free_d(a, 0);
228
32.4M
    if (a->flags & BN_FLG_MALLOCED)
229
32.4M
        OPENSSL_free(a);
230
32.4M
}
231
232
void bn_init(BIGNUM *a)
233
77.0M
{
234
77.0M
    static BIGNUM nilbn;
235
236
77.0M
    *a = nilbn;
237
77.0M
    bn_check_top(a);
238
77.0M
}
239
240
BIGNUM *BN_new(void)
241
35.6M
{
242
35.6M
    BIGNUM *ret;
243
244
35.6M
    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
35.6M
    ret->flags = BN_FLG_MALLOCED;
249
35.6M
    bn_check_top(ret);
250
35.6M
    return ret;
251
35.6M
}
252
253
BIGNUM *BN_secure_new(void)
254
1.87M
{
255
1.87M
    BIGNUM *ret = BN_new();
256
1.87M
    if (ret != NULL)
257
1.87M
        ret->flags |= BN_FLG_SECURE;
258
1.87M
    return ret;
259
1.87M
}
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
55.4M
{
265
55.4M
    BN_ULONG *a = NULL;
266
267
55.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
55.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
55.4M
    if (BN_get_flags(b, BN_FLG_SECURE))
276
1.21M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
277
54.2M
    else
278
54.2M
        a = OPENSSL_zalloc(words * sizeof(*a));
279
55.4M
    if (a == NULL) {
280
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
281
0
        return NULL;
282
0
    }
283
284
55.4M
    assert(b->top <= words);
285
55.4M
    if (b->top > 0)
286
7.00M
        memcpy(a, b->d, sizeof(*a) * b->top);
287
288
55.4M
    return a;
289
55.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
55.4M
{
301
55.4M
    if (words > b->dmax) {
302
55.4M
        BN_ULONG *a = bn_expand_internal(b, words);
303
55.4M
        if (!a)
304
0
            return NULL;
305
55.4M
        if (b->d != NULL)
306
13.9M
            bn_free_d(b, 1);
307
55.4M
        b->d = a;
308
55.4M
        b->dmax = words;
309
55.4M
    }
310
311
55.4M
    return b;
312
55.4M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
3.61M
{
316
3.61M
    BIGNUM *t;
317
318
3.61M
    if (a == NULL)
319
0
        return NULL;
320
3.61M
    bn_check_top(a);
321
322
3.61M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
3.61M
    if (t == NULL)
324
0
        return NULL;
325
3.61M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
3.61M
    bn_check_top(t);
330
3.61M
    return t;
331
3.61M
}
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
9
        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
401k
{
394
401k
    if (a == NULL)
395
0
        return;
396
401k
    bn_check_top(a);
397
401k
    if (a->d != NULL)
398
44.1k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
401k
    a->neg = 0;
400
401k
    a->top = 0;
401
401k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
401k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
400
{
406
400
    if (a->top > 1)
407
13
        return BN_MASK2;
408
387
    else if (a->top == 1)
409
333
        return a->d[0];
410
    /* a->top == 0 */
411
54
    return 0;
412
400
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.36M
{
416
3.36M
    bn_check_top(a);
417
3.36M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.36M
    a->neg = 0;
420
3.36M
    a->d[0] = w;
421
3.36M
    a->top = (w ? 1 : 0);
422
3.36M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.36M
    bn_check_top(a);
424
3.36M
    return 1;
425
3.36M
}
426
427
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
428
1.53M
{
429
1.53M
    unsigned int i, m;
430
1.53M
    unsigned int n;
431
1.53M
    BN_ULONG l;
432
1.53M
    BIGNUM *bn = NULL;
433
434
1.53M
    if (ret == NULL)
435
870k
        ret = bn = BN_new();
436
1.53M
    if (ret == NULL)
437
0
        return NULL;
438
1.53M
    bn_check_top(ret);
439
    /* Skip leading zero's. */
440
3.79M
    for (; len > 0 && *s == 0; s++, len--)
441
2.26M
        continue;
442
1.53M
    n = len;
443
1.53M
    if (n == 0) {
444
193k
        ret->top = 0;
445
193k
        return ret;
446
193k
    }
447
1.34M
    i = ((n - 1) / BN_BYTES) + 1;
448
1.34M
    m = ((n - 1) % (BN_BYTES));
449
1.34M
    if (bn_wexpand(ret, (int)i) == NULL) {
450
0
        BN_free(bn);
451
0
        return NULL;
452
0
    }
453
1.34M
    ret->top = i;
454
1.34M
    ret->neg = 0;
455
1.34M
    l = 0;
456
105M
    while (n--) {
457
103M
        l = (l << 8L) | *(s++);
458
103M
        if (m-- == 0) {
459
13.5M
            ret->d[--i] = l;
460
13.5M
            l = 0;
461
13.5M
            m = BN_BYTES - 1;
462
13.5M
        }
463
103M
    }
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.34M
    bn_correct_top(ret);
469
1.34M
    return ret;
470
1.34M
}
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
298k
{
478
298k
    int n;
479
298k
    size_t i, lasti, j, atop, mask;
480
298k
    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
298k
    n = BN_num_bytes(a);
488
298k
    if (tolen == -1) {
489
162k
        tolen = n;
490
162k
    } else if (tolen < n) { /* uncommon/unlike case */
491
782
        BIGNUM temp = *a;
492
493
782
        bn_correct_top(&temp);
494
782
        n = BN_num_bytes(&temp);
495
782
        if (tolen < n)
496
782
            return -1;
497
782
    }
498
499
    /* Swipe through whole available data and don't give away padded zero. */
500
297k
    atop = a->dmax * BN_BYTES;
501
297k
    if (atop == 0) {
502
51.9k
        if (tolen != 0)
503
908
            memset(to, '\0', tolen);
504
51.9k
        return tolen;
505
51.9k
    }
506
507
245k
    lasti = atop - 1;
508
245k
    atop = a->top * BN_BYTES;
509
245k
    if (endianess == big)
510
148k
        to += tolen; /* start from the end of the buffer */
511
20.9M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
512
20.7M
        unsigned char val;
513
20.7M
        l = a->d[i / BN_BYTES];
514
20.7M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
515
20.7M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
516
20.7M
        if (endianess == big)
517
10.7M
            *--to = val;
518
9.95M
        else
519
9.95M
            *to++ = val;
520
20.7M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
521
20.7M
    }
522
523
245k
    return tolen;
524
297k
}
525
526
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
527
315k
{
528
315k
    if (tolen < 0)
529
0
        return -1;
530
315k
    return bn2binpad(a, to, tolen, big);
531
315k
}
532
533
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
534
739k
{
535
739k
    return bn2binpad(a, to, -1, big);
536
739k
}
537
538
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
97.7k
{
540
97.7k
    unsigned int i, m;
541
97.7k
    unsigned int n;
542
97.7k
    BN_ULONG l;
543
97.7k
    BIGNUM *bn = NULL;
544
545
97.7k
    if (ret == NULL)
546
85.6k
        ret = bn = BN_new();
547
97.7k
    if (ret == NULL)
548
0
        return NULL;
549
97.7k
    bn_check_top(ret);
550
97.7k
    s += len;
551
    /* Skip trailing zeroes. */
552
115k
    for (; len > 0 && s[-1] == 0; s--, len--)
553
17.7k
        continue;
554
97.7k
    n = len;
555
97.7k
    if (n == 0) {
556
278
        ret->top = 0;
557
278
        return ret;
558
278
    }
559
97.5k
    i = ((n - 1) / BN_BYTES) + 1;
560
97.5k
    m = ((n - 1) % (BN_BYTES));
561
97.5k
    if (bn_wexpand(ret, (int)i) == NULL) {
562
0
        BN_free(bn);
563
0
        return NULL;
564
0
    }
565
97.5k
    ret->top = i;
566
97.5k
    ret->neg = 0;
567
97.5k
    l = 0;
568
10.0M
    while (n--) {
569
9.95M
        s--;
570
9.95M
        l = (l << 8L) | *s;
571
9.95M
        if (m-- == 0) {
572
1.25M
            ret->d[--i] = l;
573
1.25M
            l = 0;
574
1.25M
            m = BN_BYTES - 1;
575
1.25M
        }
576
9.95M
    }
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.5k
    bn_correct_top(ret);
582
97.5k
    return ret;
583
97.5k
}
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.09M
{
594
1.09M
    DECLARE_IS_ENDIAN;
595
596
1.09M
    if (IS_LITTLE_ENDIAN)
597
1.09M
        return BN_lebin2bn(s, len, ret);
598
0
    return BN_bin2bn(s, len, ret);
599
1.09M
}
600
601
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
602
1.09M
{
603
1.09M
    DECLARE_IS_ENDIAN;
604
605
1.09M
    if (IS_LITTLE_ENDIAN)
606
1.09M
        return BN_bn2lebinpad(a, to, tolen);
607
0
    return BN_bn2binpad(a, to, tolen);
608
1.09M
}
609
610
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
611
143M
{
612
143M
    int i;
613
143M
    BN_ULONG t1, t2, *ap, *bp;
614
615
143M
    ap = a->d;
616
143M
    bp = b->d;
617
618
143M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
619
3.16M
        && a->top == b->top) {
620
3.15M
        int res = 0;
621
622
16.0M
        for (i = 0; i < b->top; i++) {
623
12.9M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
624
12.9M
                -1, res);
625
12.9M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
626
12.9M
                1, res);
627
12.9M
        }
628
3.15M
        return res;
629
3.15M
    }
630
631
140M
    bn_check_top(a);
632
140M
    bn_check_top(b);
633
634
140M
    i = a->top - b->top;
635
140M
    if (i != 0)
636
15.8M
        return i;
637
638
131M
    for (i = a->top - 1; i >= 0; i--) {
639
128M
        t1 = ap[i];
640
128M
        t2 = bp[i];
641
128M
        if (t1 != t2)
642
122M
            return ((t1 > t2) ? 1 : -1);
643
128M
    }
644
2.25M
    return 0;
645
124M
}
646
647
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
648
20.2M
{
649
20.2M
    int i;
650
20.2M
    int gt, lt;
651
20.2M
    BN_ULONG t1, t2;
652
653
20.2M
    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
20.2M
    bn_check_top(a);
663
20.2M
    bn_check_top(b);
664
665
20.2M
    if (a->neg != b->neg) {
666
1.21k
        if (a->neg)
667
502
            return -1;
668
710
        else
669
710
            return 1;
670
1.21k
    }
671
20.2M
    if (a->neg == 0) {
672
20.2M
        gt = 1;
673
20.2M
        lt = -1;
674
20.2M
    } else {
675
10.2k
        gt = -1;
676
10.2k
        lt = 1;
677
10.2k
    }
678
679
20.2M
    if (a->top > b->top)
680
2.20M
        return gt;
681
18.0M
    if (a->top < b->top)
682
1.76M
        return lt;
683
44.1M
    for (i = a->top - 1; i >= 0; i--) {
684
41.7M
        t1 = a->d[i];
685
41.7M
        t2 = b->d[i];
686
41.7M
        if (t1 > t2)
687
5.42M
            return gt;
688
36.3M
        if (t1 < t2)
689
8.53M
            return lt;
690
36.3M
    }
691
2.37M
    return 0;
692
16.3M
}
693
694
int BN_set_bit(BIGNUM *a, int n)
695
2.52M
{
696
2.52M
    int i, j, k;
697
698
2.52M
    if (n < 0)
699
0
        return 0;
700
701
2.52M
    i = n / BN_BITS2;
702
2.52M
    j = n % BN_BITS2;
703
2.52M
    if (a->top <= i) {
704
2.52M
        if (bn_wexpand(a, i + 1) == NULL)
705
0
            return 0;
706
21.1M
        for (k = a->top; k < i + 1; k++)
707
18.6M
            a->d[k] = 0;
708
2.52M
        a->top = i + 1;
709
2.52M
        a->flags &= ~BN_FLG_FIXED_TOP;
710
2.52M
    }
711
712
2.52M
    a->d[i] |= (((BN_ULONG)1) << j);
713
2.52M
    bn_check_top(a);
714
2.52M
    return 1;
715
2.52M
}
716
717
int BN_clear_bit(BIGNUM *a, int n)
718
260
{
719
260
    int i, j;
720
721
260
    bn_check_top(a);
722
260
    if (n < 0)
723
0
        return 0;
724
725
260
    i = n / BN_BITS2;
726
260
    j = n % BN_BITS2;
727
260
    if (a->top <= i)
728
0
        return 0;
729
730
260
    a->d[i] &= (~(((BN_ULONG)1) << j));
731
260
    bn_correct_top(a);
732
260
    return 1;
733
260
}
734
735
int BN_is_bit_set(const BIGNUM *a, int n)
736
206M
{
737
206M
    int i, j;
738
739
206M
    bn_check_top(a);
740
206M
    if (n < 0)
741
5.17k
        return 0;
742
206M
    i = n / BN_BITS2;
743
206M
    j = n % BN_BITS2;
744
206M
    if (a->top <= i)
745
8.58k
        return 0;
746
206M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
747
206M
}
748
749
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
750
6.95k
{
751
6.95k
    int b, w;
752
753
6.95k
    if (n < 0)
754
0
        return 0;
755
756
6.95k
    w = n / BN_BITS2;
757
6.95k
    b = n % BN_BITS2;
758
6.95k
    if (w >= a->top)
759
0
        return 0;
760
6.95k
    if (b == 0)
761
6.59k
        a->top = w;
762
352
    else {
763
352
        a->top = w + 1;
764
352
        a->d[w] &= ~(BN_MASK2 << b);
765
352
    }
766
6.95k
    a->flags |= BN_FLG_FIXED_TOP;
767
6.95k
    return 1;
768
6.95k
}
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.52M
{
783
2.52M
    if (b && !BN_is_zero(a))
784
537k
        a->neg = 1;
785
1.98M
    else
786
1.98M
        a->neg = 0;
787
2.52M
}
788
789
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
790
84.8M
{
791
84.8M
    int i;
792
84.8M
    BN_ULONG aa, bb;
793
794
84.8M
    if (n == 0)
795
0
        return 0;
796
797
84.8M
    aa = a[n - 1];
798
84.8M
    bb = b[n - 1];
799
84.8M
    if (aa != bb)
800
76.0M
        return ((aa > bb) ? 1 : -1);
801
75.8M
    for (i = n - 2; i >= 0; i--) {
802
72.2M
        aa = a[i];
803
72.2M
        bb = b[i];
804
72.2M
        if (aa != bb)
805
5.22M
            return ((aa > bb) ? 1 : -1);
806
72.2M
    }
807
3.58M
    return 0;
808
8.80M
}
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
123M
{
821
123M
    int n, i;
822
123M
    n = cl - 1;
823
824
123M
    if (dl < 0) {
825
3.04M
        for (i = dl; i < 0; i++) {
826
2.94M
            if (b[n - i] != 0)
827
1.46M
                return -1; /* a < b */
828
2.94M
        }
829
1.56M
    }
830
122M
    if (dl > 0) {
831
4.76M
        for (i = dl; i > 0; i--) {
832
4.50M
            if (a[n + i] != 0)
833
1.31M
                return 1; /* a > b */
834
4.50M
        }
835
1.56M
    }
836
120M
    return bn_cmp_words(a, b, cl);
837
122M
}
838
839
/*-
840
 * Constant-time conditional swap of a and b.
841
 * a and b are swapped if condition is not 0.
842
 * nwords is the number of words to swap.
843
 * Assumes that at least nwords are allocated in both a and b.
844
 * Assumes that no more than nwords are used by either a or b.
845
 */
846
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
847
2.48M
{
848
2.48M
    BN_ULONG t;
849
2.48M
    int i;
850
851
2.48M
    if (a == b)
852
0
        return;
853
854
2.48M
    bn_wcheck_size(a, nwords);
855
2.48M
    bn_wcheck_size(b, nwords);
856
857
2.48M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
858
859
2.48M
    t = (a->top ^ b->top) & condition;
860
2.48M
    a->top ^= t;
861
2.48M
    b->top ^= t;
862
863
2.48M
    t = (a->neg ^ b->neg) & condition;
864
2.48M
    a->neg ^= t;
865
2.48M
    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.48M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
890
891
2.48M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
892
2.48M
    a->flags ^= t;
893
2.48M
    b->flags ^= t;
894
895
    /* conditionally swap the data */
896
14.7M
    for (i = 0; i < nwords; i++) {
897
12.2M
        t = (a->d[i] ^ b->d[i]) & condition;
898
12.2M
        a->d[i] ^= t;
899
12.2M
        b->d[i] ^= t;
900
12.2M
    }
901
2.48M
}
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
220k
{
909
220k
    int secbits, bits;
910
220k
    if (L >= 15360)
911
950
        secbits = 256;
912
219k
    else if (L >= 7680)
913
1.13k
        secbits = 192;
914
217k
    else if (L >= 3072)
915
2.87k
        secbits = 128;
916
215k
    else if (L >= 2048)
917
5.20k
        secbits = 112;
918
209k
    else if (L >= 1024)
919
126k
        secbits = 80;
920
83.8k
    else
921
83.8k
        return 0;
922
136k
    if (N == -1)
923
13.2k
        return secbits;
924
122k
    bits = N / 2;
925
122k
    if (bits < 80)
926
9.99k
        return 0;
927
112k
    return bits >= secbits ? secbits : bits;
928
122k
}
929
930
void BN_zero_ex(BIGNUM *a)
931
1.15G
{
932
1.15G
    a->neg = 0;
933
1.15G
    a->top = 0;
934
1.15G
    a->flags &= ~BN_FLG_FIXED_TOP;
935
1.15G
}
936
937
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
938
98.4M
{
939
98.4M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
940
98.4M
}
941
942
int BN_is_zero(const BIGNUM *a)
943
416M
{
944
416M
    return a->top == 0;
945
416M
}
946
947
int BN_is_one(const BIGNUM *a)
948
96.6M
{
949
96.6M
    return BN_abs_is_word(a, 1) && !a->neg;
950
96.6M
}
951
952
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
953
373k
{
954
373k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
955
373k
}
956
957
int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w)
958
6.63k
{
959
6.63k
    int res, i;
960
6.63k
    const BN_ULONG *ap = a->d;
961
962
6.63k
    if (a->neg || a->top == 0)
963
0
        return 0;
964
965
6.63k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
966
967
26.4k
    for (i = 1; i < a->top; i++)
968
19.8k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
969
19.8k
            res, 0);
970
6.63k
    return res;
971
6.63k
}
972
973
int BN_is_odd(const BIGNUM *a)
974
73.2M
{
975
73.2M
    return (a->top > 0) && (a->d[0] & 1);
976
73.2M
}
977
978
int BN_is_negative(const BIGNUM *a)
979
9.59M
{
980
9.59M
    return (a->neg != 0);
981
9.59M
}
982
983
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
984
    BN_CTX *ctx)
985
2.69M
{
986
2.69M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
987
2.69M
}
988
989
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
990
21.8M
{
991
21.8M
    dest->d = b->d;
992
21.8M
    dest->top = b->top;
993
21.8M
    dest->dmax = b->dmax;
994
21.8M
    dest->neg = b->neg;
995
21.8M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
996
21.8M
        | (b->flags & ~BN_FLG_MALLOCED)
997
21.8M
        | BN_FLG_STATIC_DATA | flags);
998
21.8M
}
999
1000
BN_GENCB *BN_GENCB_new(void)
1001
5.21k
{
1002
5.21k
    BN_GENCB *ret;
1003
1004
5.21k
    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.21k
    return ret;
1010
5.21k
}
1011
1012
void BN_GENCB_free(BN_GENCB *cb)
1013
7.14k
{
1014
7.14k
    if (cb == NULL)
1015
1.92k
        return;
1016
5.21k
    OPENSSL_free(cb);
1017
5.21k
}
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
672M
{
1026
672M
    return b->flags & n;
1027
672M
}
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.21k
{
1043
5.21k
    BN_GENCB *tmp_gencb = gencb;
1044
5.21k
    tmp_gencb->ver = 2;
1045
5.21k
    tmp_gencb->arg = cb_arg;
1046
5.21k
    tmp_gencb->cb.cb_2 = callback;
1047
5.21k
}
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.29G
{
1056
2.29G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1057
2.29G
}
1058
1059
void bn_correct_top_consttime(BIGNUM *a)
1060
16.9k
{
1061
16.9k
    int j, atop;
1062
16.9k
    BN_ULONG limb;
1063
16.9k
    unsigned int mask;
1064
1065
1.10M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1066
1.08M
        limb = a->d[j];
1067
1.08M
        limb |= 0 - limb;
1068
1.08M
        limb >>= BN_BITS2 - 1;
1069
1.08M
        limb = 0 - limb;
1070
1.08M
        mask = (unsigned int)limb;
1071
1.08M
        mask &= constant_time_msb(j - a->top);
1072
1.08M
        atop = constant_time_select_int(mask, j + 1, atop);
1073
1.08M
    }
1074
1075
16.9k
    mask = constant_time_eq_int(atop, 0);
1076
16.9k
    a->top = atop;
1077
16.9k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1078
16.9k
    a->flags &= ~BN_FLG_FIXED_TOP;
1079
16.9k
}
1080
1081
void bn_correct_top(BIGNUM *a)
1082
957M
{
1083
957M
    BN_ULONG *ftl;
1084
957M
    int tmp_top = a->top;
1085
1086
957M
    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
953M
                break;
1091
2.68G
        }
1092
956M
        a->top = tmp_top;
1093
956M
    }
1094
957M
    if (a->top == 0)
1095
3.51M
        a->neg = 0;
1096
957M
    a->flags &= ~BN_FLG_FIXED_TOP;
1097
957M
    bn_pollute(a);
1098
957M
}