Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/bn/bn_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <assert.h>
11
#include <limits.h>
12
#include "internal/cryptlib.h"
13
#include "internal/endian.h"
14
#include "bn_local.h"
15
#include <openssl/opensslconf.h>
16
#include "internal/constant_time.h"
17
18
/* This stuff appears to be completely unused, so is deprecated */
19
#ifndef OPENSSL_NO_DEPRECATED_0_9_8
20
/*-
21
 * For a 32 bit machine
22
 * 2 -   4 ==  128
23
 * 3 -   8 ==  256
24
 * 4 -  16 ==  512
25
 * 5 -  32 == 1024
26
 * 6 -  64 == 2048
27
 * 7 - 128 == 4096
28
 * 8 - 256 == 8192
29
 */
30
static int bn_limit_bits = 0;
31
static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
32
static int bn_limit_bits_low = 0;
33
static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
34
static int bn_limit_bits_high = 0;
35
static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
36
static int bn_limit_bits_mont = 0;
37
static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
38
39
void BN_set_params(int mult, int high, int low, int mont)
40
0
{
41
0
    if (mult >= 0) {
42
0
        if (mult > (int)(sizeof(int) * 8) - 1)
43
0
            mult = sizeof(int) * 8 - 1;
44
0
        bn_limit_bits = mult;
45
0
        bn_limit_num = 1 << mult;
46
0
    }
47
0
    if (high >= 0) {
48
0
        if (high > (int)(sizeof(int) * 8) - 1)
49
0
            high = sizeof(int) * 8 - 1;
50
0
        bn_limit_bits_high = high;
51
0
        bn_limit_num_high = 1 << high;
52
0
    }
53
0
    if (low >= 0) {
54
0
        if (low > (int)(sizeof(int) * 8) - 1)
55
0
            low = sizeof(int) * 8 - 1;
56
0
        bn_limit_bits_low = low;
57
0
        bn_limit_num_low = 1 << low;
58
0
    }
59
0
    if (mont >= 0) {
60
0
        if (mont > (int)(sizeof(int) * 8) - 1)
61
0
            mont = sizeof(int) * 8 - 1;
62
0
        bn_limit_bits_mont = mont;
63
0
        bn_limit_num_mont = 1 << mont;
64
0
    }
65
0
}
66
67
int BN_get_params(int which)
68
0
{
69
0
    if (which == 0)
70
0
        return bn_limit_bits;
71
0
    else if (which == 1)
72
0
        return bn_limit_bits_high;
73
0
    else if (which == 2)
74
0
        return bn_limit_bits_low;
75
0
    else if (which == 3)
76
0
        return bn_limit_bits_mont;
77
0
    else
78
0
        return 0;
79
0
}
80
#endif
81
82
const BIGNUM *BN_value_one(void)
83
1.98M
{
84
1.98M
    static const BN_ULONG data_one = 1L;
85
1.98M
    static const BIGNUM const_one = {
86
1.98M
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
87
1.98M
    };
88
89
1.98M
    return &const_one;
90
1.98M
}
91
92
/*
93
 * Old Visual Studio ARM compiler miscompiles BN_num_bits_word()
94
 * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html
95
 */
96
#if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \
97
    && _MSC_VER >= 1400 && _MSC_VER < 1501
98
#define MS_BROKEN_BN_num_bits_word
99
#pragma optimize("", off)
100
#endif
101
int BN_num_bits_word(BN_ULONG l)
102
260M
{
103
260M
    BN_ULONG x, mask;
104
260M
    int bits = (l != 0);
105
106
260M
#if BN_BITS2 > 32
107
260M
    x = l >> 32;
108
260M
    mask = (0 - x) & BN_MASK2;
109
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
110
260M
    bits += 32 & mask;
111
260M
    l ^= (x ^ l) & mask;
112
260M
#endif
113
114
260M
    x = l >> 16;
115
260M
    mask = (0 - x) & BN_MASK2;
116
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
117
260M
    bits += 16 & mask;
118
260M
    l ^= (x ^ l) & mask;
119
120
260M
    x = l >> 8;
121
260M
    mask = (0 - x) & BN_MASK2;
122
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
123
260M
    bits += 8 & mask;
124
260M
    l ^= (x ^ l) & mask;
125
126
260M
    x = l >> 4;
127
260M
    mask = (0 - x) & BN_MASK2;
128
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
129
260M
    bits += 4 & mask;
130
260M
    l ^= (x ^ l) & mask;
131
132
260M
    x = l >> 2;
133
260M
    mask = (0 - x) & BN_MASK2;
134
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
135
260M
    bits += 2 & mask;
136
260M
    l ^= (x ^ l) & mask;
137
138
260M
    x = l >> 1;
139
260M
    mask = (0 - x) & BN_MASK2;
140
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
141
260M
    bits += 1 & mask;
142
143
260M
    return bits;
144
260M
}
145
#ifdef MS_BROKEN_BN_num_bits_word
146
#pragma optimize("", on)
147
#endif
148
149
/*
150
 * This function still leaks `a->dmax`: it's caller's responsibility to
151
 * expand the input `a` in advance to a public length.
152
 */
153
static ossl_inline int bn_num_bits_consttime(const BIGNUM *a)
154
8.42M
{
155
8.42M
    int j, ret;
156
8.42M
    unsigned int mask, past_i;
157
8.42M
    int i = a->top - 1;
158
8.42M
    bn_check_top(a);
159
160
67.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
58.5M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
58.5M
        ret += BN_BITS2 & (~mask & ~past_i);
164
58.5M
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
58.5M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
58.5M
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
8.42M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
8.42M
    return ret & mask;
176
8.42M
}
177
178
int BN_num_bits(const BIGNUM *a)
179
12.1M
{
180
12.1M
    int i = a->top - 1;
181
12.1M
    bn_check_top(a);
182
183
12.1M
    if (a->flags & BN_FLG_CONSTTIME) {
184
        /*
185
         * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
186
         * so that a->dmax is not leaking secret information.
187
         *
188
         * In other words, it's the caller's responsibility to ensure `a` has
189
         * been preallocated in advance to a public length if we hit this
190
         * branch.
191
         *
192
         */
193
2.04M
        return bn_num_bits_consttime(a);
194
2.04M
    }
195
196
10.0M
    if (ossl_unlikely(BN_is_zero(a)))
197
79.3k
        return 0;
198
199
9.98M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
10.0M
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
78.8M
{
204
78.8M
    if (BN_get_flags(a, BN_FLG_SECURE))
205
1.83M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
76.9M
    else if (clear != 0)
207
47.3M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
29.6M
    else
209
29.6M
        OPENSSL_free(a->d);
210
78.8M
}
211
212
void BN_clear_free(BIGNUM *a)
213
39.6M
{
214
39.6M
    if (a == NULL)
215
5.82M
        return;
216
33.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
217
30.6M
        bn_free_d(a, 1);
218
33.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
219
3.30M
        OPENSSL_cleanse(a, sizeof(*a));
220
3.30M
        OPENSSL_free(a);
221
3.30M
    }
222
33.7M
}
223
224
void BN_free(BIGNUM *a)
225
50.7M
{
226
50.7M
    if (a == NULL)
227
21.0M
        return;
228
29.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
229
29.6M
        bn_free_d(a, 0);
230
29.7M
    if (a->flags & BN_FLG_MALLOCED)
231
29.6M
        OPENSSL_free(a);
232
29.7M
}
233
234
void bn_init(BIGNUM *a)
235
75.1M
{
236
75.1M
    static BIGNUM nilbn;
237
238
75.1M
    *a = nilbn;
239
75.1M
    bn_check_top(a);
240
75.1M
}
241
242
BIGNUM *BN_new(void)
243
32.9M
{
244
32.9M
    BIGNUM *ret;
245
246
32.9M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
247
0
        return NULL;
248
32.9M
    ret->flags = BN_FLG_MALLOCED;
249
32.9M
    bn_check_top(ret);
250
32.9M
    return ret;
251
32.9M
}
252
253
BIGNUM *BN_secure_new(void)
254
2.08M
{
255
2.08M
    BIGNUM *ret = BN_new();
256
257
2.08M
    if (ret != NULL)
258
2.08M
        ret->flags |= BN_FLG_SECURE;
259
2.08M
    return ret;
260
2.08M
}
261
262
/* This is used by bn_expand2() */
263
/* The caller MUST check that words > b->dmax before calling this */
264
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
265
25.3M
{
266
25.3M
    BN_ULONG *a = NULL;
267
268
25.3M
    if (ossl_unlikely(words > (INT_MAX / (4 * BN_BITS2)))) {
269
0
        ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG);
270
0
        return NULL;
271
0
    }
272
25.3M
    if (ossl_unlikely(BN_get_flags(b, BN_FLG_STATIC_DATA))) {
273
0
        ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
274
0
        return NULL;
275
0
    }
276
25.3M
    if (BN_get_flags(b, BN_FLG_SECURE))
277
459k
        a = OPENSSL_secure_calloc(words, sizeof(*a));
278
24.8M
    else
279
24.8M
        a = OPENSSL_calloc(words, sizeof(*a));
280
25.3M
    if (ossl_unlikely(a == NULL))
281
0
        return NULL;
282
283
25.3M
    assert(b->top <= words);
284
25.3M
    if (b->top > 0)
285
2.32M
        memcpy(a, b->d, sizeof(*a) * b->top);
286
287
25.3M
    return a;
288
25.3M
}
289
290
/*
291
 * This is an internal function that should not be used in applications. It
292
 * ensures that 'b' has enough room for a 'words' word number and initialises
293
 * any unused part of b->d with leading zeros. It is mostly used by the
294
 * various BIGNUM routines. If there is an error, NULL is returned. If not,
295
 * 'b' is returned.
296
 */
297
298
BIGNUM *bn_expand2(BIGNUM *b, int words)
299
25.3M
{
300
25.3M
    if (ossl_likely(words > b->dmax)) {
301
25.3M
        BN_ULONG *a = bn_expand_internal(b, words);
302
303
25.3M
        if (ossl_unlikely(!a))
304
0
            return NULL;
305
25.3M
        if (b->d != NULL)
306
5.93M
            bn_free_d(b, 1);
307
25.3M
        b->d = a;
308
25.3M
        b->dmax = words;
309
25.3M
    }
310
311
25.3M
    return b;
312
25.3M
}
313
314
BIGNUM *BN_dup(const BIGNUM *a)
315
2.64M
{
316
2.64M
    BIGNUM *t;
317
318
2.64M
    if (a == NULL)
319
0
        return NULL;
320
2.64M
    bn_check_top(a);
321
322
2.64M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
323
2.64M
    if (t == NULL)
324
0
        return NULL;
325
2.64M
    if (!BN_copy(t, a)) {
326
0
        BN_free(t);
327
0
        return NULL;
328
0
    }
329
2.64M
    bn_check_top(t);
330
2.64M
    return t;
331
2.64M
}
332
333
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
334
76.7M
{
335
76.7M
    int bn_words;
336
337
76.7M
    bn_check_top(b);
338
339
76.7M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
340
341
76.7M
    if (ossl_unlikely(a == b))
342
12
        return a;
343
76.7M
    if (ossl_unlikely(bn_wexpand(a, bn_words) == NULL))
344
0
        return NULL;
345
346
76.7M
    if (ossl_likely(b->top > 0))
347
76.2M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
348
349
76.7M
    a->neg = b->neg;
350
76.7M
    a->top = b->top;
351
76.7M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
352
76.7M
    bn_check_top(a);
353
76.7M
    return a;
354
76.7M
}
355
356
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA | BN_FLG_CONSTTIME | BN_FLG_SECURE | BN_FLG_FIXED_TOP))
357
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
358
359
void BN_swap(BIGNUM *a, BIGNUM *b)
360
0
{
361
0
    int flags_old_a, flags_old_b;
362
0
    BN_ULONG *tmp_d;
363
0
    int tmp_top, tmp_dmax, tmp_neg;
364
365
0
    bn_check_top(a);
366
0
    bn_check_top(b);
367
368
0
    flags_old_a = a->flags;
369
0
    flags_old_b = b->flags;
370
371
0
    tmp_d = a->d;
372
0
    tmp_top = a->top;
373
0
    tmp_dmax = a->dmax;
374
0
    tmp_neg = a->neg;
375
376
0
    a->d = b->d;
377
0
    a->top = b->top;
378
0
    a->dmax = b->dmax;
379
0
    a->neg = b->neg;
380
381
0
    b->d = tmp_d;
382
0
    b->top = tmp_top;
383
0
    b->dmax = tmp_dmax;
384
0
    b->neg = tmp_neg;
385
386
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
387
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
388
0
    bn_check_top(a);
389
0
    bn_check_top(b);
390
0
}
391
392
void BN_clear(BIGNUM *a)
393
487k
{
394
487k
    if (a == NULL)
395
0
        return;
396
487k
    bn_check_top(a);
397
487k
    if (a->d != NULL)
398
44.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
399
487k
    a->neg = 0;
400
487k
    a->top = 0;
401
487k
    a->flags &= ~BN_FLG_FIXED_TOP;
402
487k
}
403
404
BN_ULONG BN_get_word(const BIGNUM *a)
405
432
{
406
432
    if (a->top > 1)
407
12
        return BN_MASK2;
408
420
    else if (a->top == 1)
409
358
        return a->d[0];
410
    /* a->top == 0 */
411
62
    return 0;
412
432
}
413
414
int BN_set_word(BIGNUM *a, BN_ULONG w)
415
3.40M
{
416
3.40M
    bn_check_top(a);
417
3.40M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
418
0
        return 0;
419
3.40M
    a->neg = 0;
420
3.40M
    a->d[0] = w;
421
3.40M
    a->top = (w ? 1 : 0);
422
3.40M
    a->flags &= ~BN_FLG_FIXED_TOP;
423
3.40M
    bn_check_top(a);
424
3.40M
    return 1;
425
3.40M
}
426
427
typedef enum { BIG,
428
    LITTLE } endianness_t;
429
typedef enum { SIGNED,
430
    UNSIGNED } signedness_t;
431
432
static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret,
433
    endianness_t endianness, signedness_t signedness)
434
11.5M
{
435
11.5M
    int inc;
436
11.5M
    const unsigned char *s2;
437
11.5M
    int inc2;
438
11.5M
    int neg = 0, xor = 0, carry = 0;
439
11.5M
    unsigned int i;
440
11.5M
    unsigned int n;
441
11.5M
    BIGNUM *bn = NULL;
442
443
    /* Negative length is not acceptable */
444
11.5M
    if (len < 0)
445
0
        return NULL;
446
447
11.5M
    if (ret == NULL)
448
6.55M
        ret = bn = BN_new();
449
11.5M
    if (ret == NULL)
450
0
        return NULL;
451
11.5M
    bn_check_top(ret);
452
453
    /*
454
     * If the input has no bits, the number is considered zero.
455
     * This makes calls with s==NULL and len==0 safe.
456
     */
457
11.5M
    if (len == 0) {
458
440k
        BN_clear(ret);
459
440k
        return ret;
460
440k
    }
461
462
    /*
463
     * The loop that does the work iterates from least to most
464
     * significant BIGNUM chunk, so we adapt parameters to transfer
465
     * input bytes accordingly.
466
     */
467
11.1M
    if (endianness == LITTLE) {
468
990k
        s2 = s + len - 1;
469
990k
        inc2 = -1;
470
990k
        inc = 1;
471
10.1M
    } else {
472
10.1M
        s2 = s;
473
10.1M
        inc2 = 1;
474
10.1M
        inc = -1;
475
10.1M
        s += len - 1;
476
10.1M
    }
477
478
    /* Take note of the signedness of the input bytes*/
479
11.1M
    if (signedness == SIGNED) {
480
0
        neg = !!(*s2 & 0x80);
481
0
        xor = neg ? 0xff : 0x00;
482
0
        carry = neg;
483
0
    }
484
485
    /*
486
     * Skip leading sign extensions (the value of |xor|).
487
     * This is the only spot where |s2| and |inc2| are used.
488
     */
489
25.1M
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
490
13.9M
        continue;
491
492
    /*
493
     * If there was a set of 0xff, we backtrack one byte unless the next
494
     * one has a sign bit, as the last 0xff is then part of the actual
495
     * number, rather then a mere sign extension.
496
     */
497
11.1M
    if (xor == 0xff) {
498
0
        if (len == 0 || !(*s2 & 0x80))
499
0
            len++;
500
0
    }
501
    /* If it was all zeros, we're done */
502
11.1M
    if (len == 0) {
503
225k
        ret->top = 0;
504
225k
        return ret;
505
225k
    }
506
10.9M
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
507
10.9M
    if (bn_wexpand(ret, (int)n) == NULL) {
508
0
        BN_free(bn);
509
0
        return NULL;
510
0
    }
511
10.9M
    ret->top = n;
512
10.9M
    ret->neg = neg;
513
119M
    for (i = 0; n-- > 0; i++) {
514
108M
        BN_ULONG l = 0; /* Accumulator */
515
108M
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
516
517
942M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
518
833M
            BN_ULONG byte_xored = *s ^ xor;
519
833M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
520
521
833M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
522
833M
            l |= (byte << m);
523
833M
        }
524
108M
        ret->d[i] = l;
525
108M
    }
526
    /*
527
     * need to call this due to clear byte at top if avoiding having the top
528
     * bit set (-ve number)
529
     */
530
10.9M
    bn_correct_top(ret);
531
10.9M
    return ret;
532
10.9M
}
533
534
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
535
10.5M
{
536
10.5M
    return bin2bn(s, len, ret, BIG, UNSIGNED);
537
10.5M
}
538
539
BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
540
0
{
541
0
    return bin2bn(s, len, ret, BIG, SIGNED);
542
0
}
543
544
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen,
545
    endianness_t endianness, signedness_t signedness)
546
1.74M
{
547
1.74M
    int inc;
548
1.74M
    int n, n8;
549
1.74M
    int xor = 0, carry = 0, ext = 0;
550
1.74M
    size_t i, lasti, j, atop, mask;
551
1.74M
    BN_ULONG l;
552
553
    /*
554
     * In case |a| is fixed-top, BN_num_bits can return bogus length,
555
     * but it's assumed that fixed-top inputs ought to be "nominated"
556
     * even for padded output, so it works out...
557
     */
558
1.74M
    n8 = BN_num_bits(a);
559
1.74M
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
560
561
    /* Take note of the signedness of the bignum */
562
1.74M
    if (signedness == SIGNED) {
563
0
        xor = a->neg ? 0xff : 0x00;
564
0
        carry = a->neg;
565
566
        /*
567
         * if |n * 8 == n|, then the MSbit is set, otherwise unset.
568
         * We must compensate with one extra byte if that doesn't
569
         * correspond to the signedness of the bignum with regards
570
         * to 2's complement.
571
         */
572
0
        ext = (n * 8 == n8)
573
0
            ? !a->neg /* MSbit set on nonnegative bignum */
574
0
            : a->neg; /* MSbit unset on negative bignum */
575
0
    }
576
577
1.74M
    if (tolen == -1) {
578
595k
        tolen = n + ext;
579
1.14M
    } else if (tolen < n + ext) { /* uncommon/unlike case */
580
4.70k
        BIGNUM temp = *a;
581
582
4.70k
        bn_correct_top(&temp);
583
4.70k
        n8 = BN_num_bits(&temp);
584
4.70k
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
585
4.70k
        if (tolen < n + ext)
586
4.70k
            return -1;
587
4.70k
    }
588
589
    /* Swipe through whole available data and don't give away padded zero. */
590
1.73M
    atop = a->dmax * BN_BYTES;
591
1.73M
    if (atop == 0) {
592
78.7k
        if (tolen != 0)
593
5.13k
            memset(to, '\0', tolen);
594
78.7k
        return tolen;
595
78.7k
    }
596
597
    /*
598
     * The loop that does the work iterates from least significant
599
     * to most significant BIGNUM limb, so we adapt parameters to
600
     * transfer output bytes accordingly.
601
     */
602
1.65M
    if (endianness == LITTLE) {
603
951k
        inc = 1;
604
951k
    } else {
605
705k
        inc = -1;
606
705k
        to += tolen - 1; /* Move to the last byte, not beyond */
607
705k
    }
608
609
1.65M
    lasti = atop - 1;
610
1.65M
    atop = a->top * BN_BYTES;
611
162M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
612
160M
        unsigned char byte, byte_xored;
613
614
160M
        l = a->d[i / BN_BYTES];
615
160M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
616
160M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
617
160M
        byte_xored = byte ^ xor;
618
160M
        *to = (unsigned char)(byte_xored + carry);
619
160M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
620
160M
        to += inc;
621
160M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
622
160M
    }
623
624
1.65M
    return tolen;
625
1.73M
}
626
627
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
628
229k
{
629
229k
    if (tolen < 0)
630
0
        return -1;
631
229k
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
632
229k
}
633
634
int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen)
635
0
{
636
0
    if (tolen < 0)
637
0
        return -1;
638
0
    return bn2binpad(a, to, tolen, BIG, SIGNED);
639
0
}
640
641
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
642
748k
{
643
748k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
644
748k
}
645
646
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
647
990k
{
648
990k
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
649
990k
}
650
651
BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
652
0
{
653
0
    return bin2bn(s, len, ret, LITTLE, SIGNED);
654
0
}
655
656
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
657
1.04M
{
658
1.04M
    if (tolen < 0)
659
0
        return -1;
660
1.04M
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
661
1.04M
}
662
663
int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen)
664
0
{
665
0
    if (tolen < 0)
666
0
        return -1;
667
0
    return bn2binpad(a, to, tolen, LITTLE, SIGNED);
668
0
}
669
670
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
671
985k
{
672
985k
    DECLARE_IS_ENDIAN;
673
674
985k
    if (IS_LITTLE_ENDIAN)
675
985k
        return BN_lebin2bn(s, len, ret);
676
0
    return BN_bin2bn(s, len, ret);
677
985k
}
678
679
BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret)
680
0
{
681
0
    DECLARE_IS_ENDIAN;
682
683
0
    if (IS_LITTLE_ENDIAN)
684
0
        return BN_signed_lebin2bn(s, len, ret);
685
0
    return BN_signed_bin2bn(s, len, ret);
686
0
}
687
688
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
689
985k
{
690
985k
    DECLARE_IS_ENDIAN;
691
692
985k
    if (IS_LITTLE_ENDIAN)
693
985k
        return BN_bn2lebinpad(a, to, tolen);
694
0
    return BN_bn2binpad(a, to, tolen);
695
985k
}
696
697
int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen)
698
0
{
699
0
    DECLARE_IS_ENDIAN;
700
701
0
    if (IS_LITTLE_ENDIAN)
702
0
        return BN_signed_bn2lebin(a, to, tolen);
703
0
    return BN_signed_bn2bin(a, to, tolen);
704
0
}
705
706
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
707
106M
{
708
106M
    int i;
709
106M
    BN_ULONG t1, t2, *ap, *bp;
710
711
    /*
712
     * As it is a public API function, we should handle NULL parameters in
713
     * some way. The function can’t return an error, so let’s define that NULL
714
     * is less than any BIGNUM.
715
     */
716
106M
    if (!ossl_assert(a != NULL && b != NULL))
717
0
        return (b == NULL) - (a == NULL);
718
719
106M
    ap = a->d;
720
106M
    bp = b->d;
721
722
106M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
723
104M
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
724
2.32M
        int res = 0;
725
2.32M
        int min_top = a->top < b->top ? a->top : b->top;
726
727
13.0M
        for (i = 0; i < min_top; i++) {
728
10.7M
            res = constant_time_select_int((int)constant_time_lt_bn(ap[i], bp[i]),
729
10.7M
                -1, res);
730
10.7M
            res = constant_time_select_int((int)constant_time_lt_bn(bp[i], ap[i]),
731
10.7M
                1, res);
732
10.7M
        }
733
734
2.42M
        for (i = min_top; i < a->top; ++i)
735
98.2k
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
736
98.2k
                res, 1);
737
738
2.32M
        for (i = min_top; i < b->top; ++i)
739
2.71k
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
740
2.71k
                res, -1);
741
742
2.32M
        return res;
743
2.32M
    }
744
745
104M
    bn_check_top(a);
746
104M
    bn_check_top(b);
747
748
104M
    i = a->top - b->top;
749
104M
    if (i != 0)
750
12.4M
        return i;
751
752
97.6M
    for (i = a->top - 1; i >= 0; i--) {
753
95.8M
        t1 = ap[i];
754
95.8M
        t2 = bp[i];
755
95.8M
        if (t1 != t2)
756
90.2M
            return ((t1 > t2) ? 1 : -1);
757
95.8M
    }
758
1.83M
    return 0;
759
92.1M
}
760
761
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
762
19.2M
{
763
19.2M
    int i;
764
19.2M
    int gt, lt;
765
19.2M
    BN_ULONG t1, t2;
766
767
19.2M
    if ((a == NULL) || (b == NULL)) {
768
0
        if (a != NULL)
769
0
            return -1;
770
0
        else if (b != NULL)
771
0
            return 1;
772
0
        else
773
0
            return 0;
774
0
    }
775
776
19.2M
    bn_check_top(a);
777
19.2M
    bn_check_top(b);
778
779
19.2M
    if (a->neg != b->neg) {
780
1.48k
        if (a->neg)
781
646
            return -1;
782
835
        else
783
835
            return 1;
784
1.48k
    }
785
19.2M
    if (a->neg == 0) {
786
19.2M
        gt = 1;
787
19.2M
        lt = -1;
788
19.2M
    } else {
789
9.90k
        gt = -1;
790
9.90k
        lt = 1;
791
9.90k
    }
792
793
19.2M
    if (a->top > b->top)
794
2.18M
        return gt;
795
17.0M
    if (a->top < b->top)
796
1.80M
        return lt;
797
43.1M
    for (i = a->top - 1; i >= 0; i--) {
798
40.7M
        t1 = a->d[i];
799
40.7M
        t2 = b->d[i];
800
40.7M
        if (t1 > t2)
801
5.33M
            return gt;
802
35.4M
        if (t1 < t2)
803
7.51M
            return lt;
804
35.4M
    }
805
2.39M
    return 0;
806
15.2M
}
807
808
int BN_set_bit(BIGNUM *a, int n)
809
2.54M
{
810
2.54M
    int i, j, k;
811
812
2.54M
    if (n < 0)
813
0
        return 0;
814
815
2.54M
    i = n / BN_BITS2;
816
2.54M
    j = n % BN_BITS2;
817
2.54M
    if (a->top <= i) {
818
2.54M
        if (bn_wexpand(a, i + 1) == NULL)
819
0
            return 0;
820
21.3M
        for (k = a->top; k < i + 1; k++)
821
18.7M
            a->d[k] = 0;
822
2.54M
        a->top = i + 1;
823
2.54M
        a->flags &= ~BN_FLG_FIXED_TOP;
824
2.54M
    }
825
826
2.54M
    a->d[i] |= (((BN_ULONG)1) << j);
827
2.54M
    bn_check_top(a);
828
2.54M
    return 1;
829
2.54M
}
830
831
int BN_clear_bit(BIGNUM *a, int n)
832
222
{
833
222
    int i, j;
834
835
222
    bn_check_top(a);
836
222
    if (n < 0)
837
0
        return 0;
838
839
222
    i = n / BN_BITS2;
840
222
    j = n % BN_BITS2;
841
222
    if (a->top <= i)
842
0
        return 0;
843
844
222
    a->d[i] &= (~(((BN_ULONG)1) << j));
845
222
    bn_correct_top(a);
846
222
    return 1;
847
222
}
848
849
int BN_is_bit_set(const BIGNUM *a, int n)
850
98.7M
{
851
98.7M
    int i, j;
852
853
98.7M
    bn_check_top(a);
854
98.7M
    if (ossl_unlikely(n < 0))
855
1.44k
        return 0;
856
98.7M
    i = n / BN_BITS2;
857
98.7M
    j = n % BN_BITS2;
858
98.7M
    if (ossl_unlikely(a->top <= i))
859
3.73k
        return 0;
860
98.7M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
861
98.7M
}
862
863
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
864
6.83k
{
865
6.83k
    int b, w;
866
867
6.83k
    if (n < 0)
868
0
        return 0;
869
870
6.83k
    w = n / BN_BITS2;
871
6.83k
    b = n % BN_BITS2;
872
6.83k
    if (w >= a->top)
873
0
        return 0;
874
6.83k
    if (b == 0)
875
6.31k
        a->top = w;
876
527
    else {
877
527
        a->top = w + 1;
878
527
        a->d[w] &= ~(BN_MASK2 << b);
879
527
    }
880
6.83k
    a->flags |= BN_FLG_FIXED_TOP;
881
6.83k
    return 1;
882
6.83k
}
883
884
int BN_mask_bits(BIGNUM *a, int n)
885
0
{
886
0
    int ret;
887
888
0
    bn_check_top(a);
889
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
890
0
    if (ret)
891
0
        bn_correct_top(a);
892
0
    return ret;
893
0
}
894
895
void BN_set_negative(BIGNUM *a, int b)
896
2.47M
{
897
2.47M
    if (b && !BN_is_zero(a))
898
557k
        a->neg = 1;
899
1.91M
    else
900
1.91M
        a->neg = 0;
901
2.47M
}
902
903
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
904
44.3M
{
905
44.3M
    int i;
906
44.3M
    BN_ULONG aa, bb;
907
908
44.3M
    if (ossl_unlikely(n == 0))
909
0
        return 0;
910
911
44.3M
    aa = a[n - 1];
912
44.3M
    bb = b[n - 1];
913
44.3M
    if (ossl_likely(aa != bb))
914
39.9M
        return ((aa > bb) ? 1 : -1);
915
37.6M
    for (i = n - 2; i >= 0; i--) {
916
35.8M
        aa = a[i];
917
35.8M
        bb = b[i];
918
35.8M
        if (aa != bb)
919
2.59M
            return ((aa > bb) ? 1 : -1);
920
35.8M
    }
921
1.81M
    return 0;
922
4.41M
}
923
924
/*
925
 * Here follows a specialised variants of bn_cmp_words().  It has the
926
 * capability of performing the operation on arrays of different sizes. The
927
 * sizes of those arrays is expressed through cl, which is the common length
928
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
929
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
930
 * BN_ULONGs...
931
 */
932
933
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
934
133M
{
935
133M
    int n, i;
936
133M
    n = cl - 1;
937
938
133M
    if (dl < 0) {
939
2.84M
        for (i = dl; i < 0; i++) {
940
2.75M
            if (b[n - i] != 0)
941
1.29M
                return -1; /* a < b */
942
2.75M
        }
943
1.38M
    }
944
132M
    if (dl > 0) {
945
4.24M
        for (i = dl; i > 0; i--) {
946
4.01M
            if (a[n + i] != 0)
947
1.16M
                return 1; /* a > b */
948
4.01M
        }
949
1.39M
    }
950
131M
    return bn_cmp_words(a, b, cl);
951
132M
}
952
953
/*-
954
 * Constant-time conditional swap of a and b.
955
 * a and b are swapped if condition is not 0.
956
 * nwords is the number of words to swap.
957
 * Assumes that at least nwords are allocated in both a and b.
958
 * Assumes that no more than nwords are used by either a or b.
959
 */
960
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
961
114M
{
962
114M
    BN_ULONG t;
963
114M
    int i;
964
965
114M
    bn_wcheck_size(a, nwords);
966
114M
    bn_wcheck_size(b, nwords);
967
968
114M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
969
970
114M
    t = (a->top ^ b->top) & condition;
971
114M
    a->top ^= t;
972
114M
    b->top ^= t;
973
974
114M
    t = (a->neg ^ b->neg) & condition;
975
114M
    a->neg ^= t;
976
114M
    b->neg ^= t;
977
978
    /*-
979
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
980
     * is actually to treat it as it's read-only data, and some (if not most)
981
     * of it does reside in read-only segment. In other words observation of
982
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
983
     * condition. It would either cause SEGV or effectively cause data
984
     * corruption.
985
     *
986
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
987
     * preserved.
988
     *
989
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
990
     * allocated and hence how to free it.
991
     *
992
     * BN_FLG_CONSTTIME: sufficient to mask and swap
993
     *
994
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
995
     * the data, so the d array may be padded with additional 0 values (i.e.
996
     * top could be greater than the minimal value that it could be). We should
997
     * be swapping it
998
     */
999
1000
114M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
1001
1002
114M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
1003
114M
    a->flags ^= t;
1004
114M
    b->flags ^= t;
1005
1006
    /* conditionally swap the data */
1007
18.2G
    for (i = 0; i < nwords; i++) {
1008
18.1G
        t = (a->d[i] ^ b->d[i]) & condition;
1009
18.1G
        a->d[i] ^= t;
1010
18.1G
        b->d[i] ^= t;
1011
18.1G
    }
1012
114M
}
1013
1014
#undef BN_CONSTTIME_SWAP_FLAGS
1015
1016
/* Bits of security, see SP800-57 */
1017
1018
int BN_security_bits(int L, int N)
1019
209k
{
1020
209k
    int secbits, bits;
1021
209k
    if (L >= 15360)
1022
973
        secbits = 256;
1023
208k
    else if (L >= 7680)
1024
1.19k
        secbits = 192;
1025
207k
    else if (L >= 3072)
1026
2.64k
        secbits = 128;
1027
205k
    else if (L >= 2048)
1028
4.42k
        secbits = 112;
1029
200k
    else if (L >= 1024)
1030
119k
        secbits = 80;
1031
80.9k
    else
1032
80.9k
        return 0;
1033
129k
    if (N == -1)
1034
11.4k
        return secbits;
1035
117k
    bits = N / 2;
1036
117k
    if (bits < 80)
1037
9.36k
        return 0;
1038
108k
    return bits >= secbits ? secbits : bits;
1039
117k
}
1040
1041
void BN_zero_ex(BIGNUM *a)
1042
1.23G
{
1043
1.23G
    a->neg = 0;
1044
1.23G
    a->top = 0;
1045
1.23G
    a->flags &= ~BN_FLG_FIXED_TOP;
1046
1.23G
}
1047
1048
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1049
116M
{
1050
116M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1051
116M
}
1052
1053
int BN_is_zero(const BIGNUM *a)
1054
427M
{
1055
427M
    return a->top == 0;
1056
427M
}
1057
1058
int BN_is_one(const BIGNUM *a)
1059
115M
{
1060
115M
    return BN_abs_is_word(a, 1) && !a->neg;
1061
115M
}
1062
1063
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1064
314k
{
1065
314k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1066
314k
}
1067
1068
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1069
6.39k
{
1070
6.39k
    int res, i;
1071
6.39k
    const BN_ULONG *ap = a->d;
1072
1073
6.39k
    if (a->neg || a->top == 0)
1074
0
        return 0;
1075
1076
6.39k
    res = constant_time_select_int((int)constant_time_eq_bn(ap[0], w), 1, 0);
1077
1078
25.4k
    for (i = 1; i < a->top; i++)
1079
19.0k
        res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
1080
19.0k
            res, 0);
1081
6.39k
    return res;
1082
6.39k
}
1083
1084
int BN_is_odd(const BIGNUM *a)
1085
72.8M
{
1086
72.8M
    return (a->top > 0) && (a->d[0] & 1);
1087
72.8M
}
1088
1089
int BN_is_negative(const BIGNUM *a)
1090
8.37M
{
1091
8.37M
    return (a->neg != 0);
1092
8.37M
}
1093
1094
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1095
    BN_CTX *ctx)
1096
2.51M
{
1097
2.51M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1098
2.51M
}
1099
1100
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1101
21.4M
{
1102
21.4M
    dest->d = b->d;
1103
21.4M
    dest->top = b->top;
1104
21.4M
    dest->dmax = b->dmax;
1105
21.4M
    dest->neg = b->neg;
1106
21.4M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1107
21.4M
        | (b->flags & ~BN_FLG_MALLOCED)
1108
21.4M
        | BN_FLG_STATIC_DATA | flags);
1109
21.4M
}
1110
1111
BN_GENCB *BN_GENCB_new(void)
1112
4.63k
{
1113
4.63k
    BN_GENCB *ret;
1114
1115
4.63k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1116
0
        return NULL;
1117
1118
4.63k
    return ret;
1119
4.63k
}
1120
1121
void BN_GENCB_free(BN_GENCB *cb)
1122
6.46k
{
1123
6.46k
    if (cb == NULL)
1124
1.82k
        return;
1125
4.63k
    OPENSSL_free(cb);
1126
4.63k
}
1127
1128
void BN_set_flags(BIGNUM *b, int n)
1129
2.29M
{
1130
2.29M
    b->flags |= n;
1131
2.29M
}
1132
1133
int BN_get_flags(const BIGNUM *b, int n)
1134
772M
{
1135
772M
    return b->flags & n;
1136
772M
}
1137
1138
/* Populate a BN_GENCB structure with an "old"-style callback */
1139
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
1140
    void *cb_arg)
1141
0
{
1142
0
    BN_GENCB *tmp_gencb = gencb;
1143
0
    tmp_gencb->ver = 1;
1144
0
    tmp_gencb->arg = cb_arg;
1145
0
    tmp_gencb->cb.cb_1 = callback;
1146
0
}
1147
1148
/* Populate a BN_GENCB structure with a "new"-style callback */
1149
void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
1150
    void *cb_arg)
1151
4.63k
{
1152
4.63k
    BN_GENCB *tmp_gencb = gencb;
1153
4.63k
    tmp_gencb->ver = 2;
1154
4.63k
    tmp_gencb->arg = cb_arg;
1155
4.63k
    tmp_gencb->cb.cb_2 = callback;
1156
4.63k
}
1157
1158
void *BN_GENCB_get_arg(BN_GENCB *cb)
1159
0
{
1160
0
    return cb->arg;
1161
0
}
1162
1163
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1164
2.30G
{
1165
2.30G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1166
2.30G
}
1167
1168
void bn_correct_top_consttime(BIGNUM *a)
1169
16.6k
{
1170
16.6k
    int j, atop;
1171
16.6k
    BN_ULONG limb;
1172
16.6k
    unsigned int mask;
1173
1174
1.08M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1175
1.06M
        limb = a->d[j];
1176
1.06M
        limb |= 0 - limb;
1177
1.06M
        limb >>= BN_BITS2 - 1;
1178
1.06M
        limb = 0 - limb;
1179
1.06M
        mask = (unsigned int)limb;
1180
1.06M
        mask &= constant_time_msb(j - a->top);
1181
1.06M
        atop = constant_time_select_int(mask, j + 1, atop);
1182
1.06M
    }
1183
1184
16.6k
    mask = constant_time_eq_int(atop, 0);
1185
16.6k
    a->top = atop;
1186
16.6k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1187
16.6k
    a->flags &= ~BN_FLG_FIXED_TOP;
1188
16.6k
}
1189
1190
void bn_correct_top(BIGNUM *a)
1191
424M
{
1192
424M
    BN_ULONG *ftl;
1193
424M
    int tmp_top = a->top;
1194
1195
424M
    if (ossl_likely(tmp_top > 0)) {
1196
1.08G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1197
1.08G
            ftl--;
1198
1.08G
            if (*ftl != 0)
1199
423M
                break;
1200
1.08G
        }
1201
424M
        a->top = tmp_top;
1202
424M
    }
1203
424M
    if (a->top == 0)
1204
1.50M
        a->neg = 0;
1205
424M
    a->flags &= ~BN_FLG_FIXED_TOP;
1206
424M
    bn_pollute(a);
1207
424M
}