Coverage Report

Created: 2026-02-14 07:20

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