Coverage Report

Created: 2026-07-23 06:28

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.98M
{
84
1.98M
    static const BN_ULONG data_one = 1L;
85
1.98M
    static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
86
87
1.98M
    return &const_one;
88
1.98M
}
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
260M
{
101
260M
    BN_ULONG x, mask;
102
260M
    int bits = (l != 0);
103
104
260M
#if BN_BITS2 > 32
105
260M
    x = l >> 32;
106
260M
    mask = (0 - x) & BN_MASK2;
107
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
108
260M
    bits += 32 & mask;
109
260M
    l ^= (x ^ l) & mask;
110
260M
#endif
111
112
260M
    x = l >> 16;
113
260M
    mask = (0 - x) & BN_MASK2;
114
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
260M
    bits += 16 & mask;
116
260M
    l ^= (x ^ l) & mask;
117
118
260M
    x = l >> 8;
119
260M
    mask = (0 - x) & BN_MASK2;
120
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
260M
    bits += 8 & mask;
122
260M
    l ^= (x ^ l) & mask;
123
124
260M
    x = l >> 4;
125
260M
    mask = (0 - x) & BN_MASK2;
126
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
260M
    bits += 4 & mask;
128
260M
    l ^= (x ^ l) & mask;
129
130
260M
    x = l >> 2;
131
260M
    mask = (0 - x) & BN_MASK2;
132
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
260M
    bits += 2 & mask;
134
260M
    l ^= (x ^ l) & mask;
135
136
260M
    x = l >> 1;
137
260M
    mask = (0 - x) & BN_MASK2;
138
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
139
260M
    bits += 1 & mask;
140
141
260M
    return bits;
142
260M
}
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.42M
{
153
8.42M
    int j, ret;
154
8.42M
    unsigned int mask, past_i;
155
8.42M
    int i = a->top - 1;
156
8.42M
    bn_check_top(a);
157
158
67.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
159
58.5M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
160
161
58.5M
        ret += BN_BITS2 & (~mask & ~past_i);
162
58.5M
        ret += BN_num_bits_word(a->d[j]) & mask;
163
164
58.5M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
165
58.5M
    }
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.42M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
172
173
8.42M
    return ret & mask;
174
8.42M
}
175
176
int BN_num_bits(const BIGNUM *a)
177
30.7M
{
178
30.7M
    int i = a->top - 1;
179
30.7M
    bn_check_top(a);
180
181
30.7M
    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.37M
        return bn_num_bits_consttime(a);
192
6.37M
    }
193
194
24.4M
    if (BN_is_zero(a))
195
684k
        return 0;
196
197
23.7M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
198
24.4M
}
199
200
static void bn_free_d(BIGNUM *a, int clear)
201
78.8M
{
202
78.8M
    if (BN_get_flags(a, BN_FLG_SECURE))
203
1.83M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
204
76.9M
    else if (clear != 0)
205
47.3M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
29.6M
    else
207
29.6M
        OPENSSL_free(a->d);
208
78.8M
}
209
210
void BN_clear_free(BIGNUM *a)
211
39.6M
{
212
39.6M
    if (a == NULL)
213
5.82M
        return;
214
33.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
215
30.6M
        bn_free_d(a, 1);
216
33.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
217
3.30M
        OPENSSL_cleanse(a, sizeof(*a));
218
3.30M
        OPENSSL_free(a);
219
3.30M
    }
220
33.7M
}
221
222
void BN_free(BIGNUM *a)
223
50.7M
{
224
50.7M
    if (a == NULL)
225
21.0M
        return;
226
29.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
227
29.6M
        bn_free_d(a, 0);
228
29.7M
    if (a->flags & BN_FLG_MALLOCED)
229
29.6M
        OPENSSL_free(a);
230
29.7M
}
231
232
void bn_init(BIGNUM *a)
233
75.1M
{
234
75.1M
    static BIGNUM nilbn;
235
236
75.1M
    *a = nilbn;
237
75.1M
    bn_check_top(a);
238
75.1M
}
239
240
BIGNUM *BN_new(void)
241
32.9M
{
242
32.9M
    BIGNUM *ret;
243
244
32.9M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
245
0
        return NULL;
246
32.9M
    ret->flags = BN_FLG_MALLOCED;
247
32.9M
    bn_check_top(ret);
248
32.9M
    return ret;
249
32.9M
}
250
251
BIGNUM *BN_secure_new(void)
252
2.08M
{
253
2.08M
    BIGNUM *ret = BN_new();
254
2.08M
    if (ret != NULL)
255
2.08M
        ret->flags |= BN_FLG_SECURE;
256
2.08M
    return ret;
257
2.08M
}
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
51.4M
{
263
51.4M
    BN_ULONG *a = NULL;
264
265
51.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
51.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
51.4M
    if (BN_get_flags(b, BN_FLG_SECURE))
274
1.37M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
275
50.0M
    else
276
50.0M
        a = OPENSSL_zalloc(words * sizeof(*a));
277
51.4M
    if (a == NULL)
278
0
        return NULL;
279
280
51.4M
    assert(b->top <= words);
281
51.4M
    if (b->top > 0)
282
5.87M
        memcpy(a, b->d, sizeof(*a) * b->top);
283
284
51.4M
    return a;
285
51.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
51.4M
{
297
51.4M
    if (words > b->dmax) {
298
51.4M
        BN_ULONG *a = bn_expand_internal(b, words);
299
51.4M
        if (!a)
300
0
            return NULL;
301
51.4M
        if (b->d != NULL)
302
12.6M
            bn_free_d(b, 1);
303
51.4M
        b->d = a;
304
51.4M
        b->dmax = words;
305
51.4M
    }
306
307
51.4M
    return b;
308
51.4M
}
309
310
BIGNUM *BN_dup(const BIGNUM *a)
311
2.64M
{
312
2.64M
    BIGNUM *t;
313
314
2.64M
    if (a == NULL)
315
0
        return NULL;
316
2.64M
    bn_check_top(a);
317
318
2.64M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
319
2.64M
    if (t == NULL)
320
0
        return NULL;
321
2.64M
    if (!BN_copy(t, a)) {
322
0
        BN_free(t);
323
0
        return NULL;
324
0
    }
325
2.64M
    bn_check_top(t);
326
2.64M
    return t;
327
2.64M
}
328
329
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
330
116M
{
331
116M
    int bn_words;
332
333
116M
    bn_check_top(b);
334
335
116M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
336
337
116M
    if (a == b)
338
11
        return a;
339
116M
    if (bn_wexpand(a, bn_words) == NULL)
340
0
        return NULL;
341
342
116M
    if (b->top > 0)
343
115M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
344
345
116M
    a->neg = b->neg;
346
116M
    a->top = b->top;
347
116M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
348
116M
    bn_check_top(a);
349
116M
    return a;
350
116M
}
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
487k
{
390
487k
    if (a == NULL)
391
0
        return;
392
487k
    bn_check_top(a);
393
487k
    if (a->d != NULL)
394
44.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
395
487k
    a->neg = 0;
396
487k
    a->top = 0;
397
487k
    a->flags &= ~BN_FLG_FIXED_TOP;
398
487k
}
399
400
BN_ULONG BN_get_word(const BIGNUM *a)
401
432
{
402
432
    if (a->top > 1)
403
12
        return BN_MASK2;
404
420
    else if (a->top == 1)
405
358
        return a->d[0];
406
    /* a->top == 0 */
407
62
    return 0;
408
432
}
409
410
int BN_set_word(BIGNUM *a, BN_ULONG w)
411
3.40M
{
412
3.40M
    bn_check_top(a);
413
3.40M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
414
0
        return 0;
415
3.40M
    a->neg = 0;
416
3.40M
    a->d[0] = w;
417
3.40M
    a->top = (w ? 1 : 0);
418
3.40M
    a->flags &= ~BN_FLG_FIXED_TOP;
419
3.40M
    bn_check_top(a);
420
3.40M
    return 1;
421
3.40M
}
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.5M
{
431
11.5M
    int inc;
432
11.5M
    const unsigned char *s2;
433
11.5M
    int inc2;
434
11.5M
    int neg = 0, xor = 0, carry = 0;
435
11.5M
    unsigned int i;
436
11.5M
    unsigned int n;
437
11.5M
    BIGNUM *bn = NULL;
438
439
    /* Negative length is not acceptable */
440
11.5M
    if (len < 0)
441
0
        return NULL;
442
443
11.5M
    if (ret == NULL)
444
6.55M
        ret = bn = BN_new();
445
11.5M
    if (ret == NULL)
446
0
        return NULL;
447
11.5M
    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.5M
    if (len == 0) {
454
440k
        BN_clear(ret);
455
440k
        return ret;
456
440k
    }
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.1M
    if (endianness == LITTLE) {
464
990k
        s2 = s + len - 1;
465
990k
        inc2 = -1;
466
990k
        inc = 1;
467
10.1M
    } else {
468
10.1M
        s2 = s;
469
10.1M
        inc2 = 1;
470
10.1M
        inc = -1;
471
10.1M
        s += len - 1;
472
10.1M
    }
473
474
    /* Take note of the signedness of the input bytes*/
475
11.1M
    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
25.1M
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
486
13.9M
        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.1M
    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.1M
    if (len == 0) {
499
225k
        ret->top = 0;
500
225k
        return ret;
501
225k
    }
502
10.9M
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
503
10.9M
    if (bn_wexpand(ret, (int)n) == NULL) {
504
0
        BN_free(bn);
505
0
        return NULL;
506
0
    }
507
10.9M
    ret->top = n;
508
10.9M
    ret->neg = neg;
509
119M
    for (i = 0; n-- > 0; i++) {
510
108M
        BN_ULONG l = 0; /* Accumulator */
511
108M
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
512
513
942M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
514
833M
            BN_ULONG byte_xored = *s ^ xor;
515
833M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
516
517
833M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
518
833M
            l |= (byte << m);
519
833M
        }
520
108M
        ret->d[i] = l;
521
108M
    }
522
    /*
523
     * need to call this due to clear byte at top if avoiding having the top
524
     * bit set (-ve number)
525
     */
526
10.9M
    bn_correct_top(ret);
527
10.9M
    return ret;
528
10.9M
}
529
530
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
531
10.5M
{
532
10.5M
    return bin2bn(s, len, ret, BIG, UNSIGNED);
533
10.5M
}
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.74M
{
543
1.74M
    int inc;
544
1.74M
    int n, n8;
545
1.74M
    int xor = 0, carry = 0, ext = 0;
546
1.74M
    size_t i, lasti, j, atop, mask;
547
1.74M
    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.74M
    n8 = BN_num_bits(a);
555
1.74M
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
556
557
    /* Take note of the signedness of the bignum */
558
1.74M
    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.74M
    if (tolen == -1) {
574
595k
        tolen = n + ext;
575
1.14M
    } else if (tolen < n + ext) { /* uncommon/unlike case */
576
4.70k
        BIGNUM temp = *a;
577
578
4.70k
        bn_correct_top(&temp);
579
4.70k
        n8 = BN_num_bits(&temp);
580
4.70k
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
581
4.70k
        if (tolen < n + ext)
582
4.70k
            return -1;
583
4.70k
    }
584
585
    /* Swipe through whole available data and don't give away padded zero. */
586
1.73M
    atop = a->dmax * BN_BYTES;
587
1.73M
    if (atop == 0) {
588
78.7k
        if (tolen != 0)
589
5.13k
            memset(to, '\0', tolen);
590
78.7k
        return tolen;
591
78.7k
    }
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.65M
    if (endianness == LITTLE) {
599
951k
        inc = 1;
600
951k
    } else {
601
705k
        inc = -1;
602
705k
        to += tolen - 1; /* Move to the last byte, not beyond */
603
705k
    }
604
605
1.65M
    lasti = atop - 1;
606
1.65M
    atop = a->top * BN_BYTES;
607
162M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
608
160M
        unsigned char byte, byte_xored;
609
610
160M
        l = a->d[i / BN_BYTES];
611
160M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
612
160M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
613
160M
        byte_xored = byte ^ xor;
614
160M
        *to = (unsigned char)(byte_xored + carry);
615
160M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
616
160M
        to += inc;
617
160M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
618
160M
    }
619
620
1.65M
    return tolen;
621
1.73M
}
622
623
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
624
229k
{
625
229k
    if (tolen < 0)
626
0
        return -1;
627
229k
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
628
229k
}
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
748k
{
639
748k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
640
748k
}
641
642
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
643
990k
{
644
990k
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
645
990k
}
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.04M
{
654
1.04M
    if (tolen < 0)
655
0
        return -1;
656
1.04M
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
657
1.04M
}
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
985k
{
668
985k
    DECLARE_IS_ENDIAN;
669
670
985k
    if (IS_LITTLE_ENDIAN)
671
985k
        return BN_lebin2bn(s, len, ret);
672
0
    return BN_bin2bn(s, len, ret);
673
985k
}
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
985k
{
686
985k
    DECLARE_IS_ENDIAN;
687
688
985k
    if (IS_LITTLE_ENDIAN)
689
985k
        return BN_bn2lebinpad(a, to, tolen);
690
0
    return BN_bn2binpad(a, to, tolen);
691
985k
}
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
30.4M
{
704
30.4M
    int i;
705
30.4M
    BN_ULONG t1, t2, *ap, *bp;
706
707
30.4M
    ap = a->d;
708
30.4M
    bp = b->d;
709
710
30.4M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
711
404k
        && a->top == b->top) {
712
402k
        int res = 0;
713
714
1.97M
        for (i = 0; i < b->top; i++) {
715
1.57M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
716
1.57M
                -1, res);
717
1.57M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
718
1.57M
                1, res);
719
1.57M
        }
720
402k
        return res;
721
402k
    }
722
723
30.0M
    bn_check_top(a);
724
30.0M
    bn_check_top(b);
725
726
30.0M
    i = a->top - b->top;
727
30.0M
    if (i != 0)
728
3.33M
        return i;
729
730
28.0M
    for (i = a->top - 1; i >= 0; i--) {
731
27.5M
        t1 = ap[i];
732
27.5M
        t2 = bp[i];
733
27.5M
        if (t1 != t2)
734
26.2M
            return ((t1 > t2) ? 1 : -1);
735
27.5M
    }
736
485k
    return 0;
737
26.7M
}
738
739
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
740
19.2M
{
741
19.2M
    int i;
742
19.2M
    int gt, lt;
743
19.2M
    BN_ULONG t1, t2;
744
745
19.2M
    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
19.2M
    bn_check_top(a);
755
19.2M
    bn_check_top(b);
756
757
19.2M
    if (a->neg != b->neg) {
758
1.48k
        if (a->neg)
759
646
            return -1;
760
835
        else
761
835
            return 1;
762
1.48k
    }
763
19.2M
    if (a->neg == 0) {
764
19.2M
        gt = 1;
765
19.2M
        lt = -1;
766
19.2M
    } else {
767
9.90k
        gt = -1;
768
9.90k
        lt = 1;
769
9.90k
    }
770
771
19.2M
    if (a->top > b->top)
772
2.18M
        return gt;
773
17.0M
    if (a->top < b->top)
774
1.80M
        return lt;
775
43.1M
    for (i = a->top - 1; i >= 0; i--) {
776
40.7M
        t1 = a->d[i];
777
40.7M
        t2 = b->d[i];
778
40.7M
        if (t1 > t2)
779
5.33M
            return gt;
780
35.4M
        if (t1 < t2)
781
7.51M
            return lt;
782
35.4M
    }
783
2.39M
    return 0;
784
15.2M
}
785
786
int BN_set_bit(BIGNUM *a, int n)
787
2.54M
{
788
2.54M
    int i, j, k;
789
790
2.54M
    if (n < 0)
791
0
        return 0;
792
793
2.54M
    i = n / BN_BITS2;
794
2.54M
    j = n % BN_BITS2;
795
2.54M
    if (a->top <= i) {
796
2.54M
        if (bn_wexpand(a, i + 1) == NULL)
797
0
            return 0;
798
21.3M
        for (k = a->top; k < i + 1; k++)
799
18.7M
            a->d[k] = 0;
800
2.54M
        a->top = i + 1;
801
2.54M
        a->flags &= ~BN_FLG_FIXED_TOP;
802
2.54M
    }
803
804
2.54M
    a->d[i] |= (((BN_ULONG)1) << j);
805
2.54M
    bn_check_top(a);
806
2.54M
    return 1;
807
2.54M
}
808
809
int BN_clear_bit(BIGNUM *a, int n)
810
222
{
811
222
    int i, j;
812
813
222
    bn_check_top(a);
814
222
    if (n < 0)
815
0
        return 0;
816
817
222
    i = n / BN_BITS2;
818
222
    j = n % BN_BITS2;
819
222
    if (a->top <= i)
820
0
        return 0;
821
822
222
    a->d[i] &= (~(((BN_ULONG)1) << j));
823
222
    bn_correct_top(a);
824
222
    return 1;
825
222
}
826
827
int BN_is_bit_set(const BIGNUM *a, int n)
828
200M
{
829
200M
    int i, j;
830
831
200M
    bn_check_top(a);
832
200M
    if (n < 0)
833
3.70k
        return 0;
834
200M
    i = n / BN_BITS2;
835
200M
    j = n % BN_BITS2;
836
200M
    if (a->top <= i)
837
9.16k
        return 0;
838
200M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
839
200M
}
840
841
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
842
6.83k
{
843
6.83k
    int b, w;
844
845
6.83k
    if (n < 0)
846
0
        return 0;
847
848
6.83k
    w = n / BN_BITS2;
849
6.83k
    b = n % BN_BITS2;
850
6.83k
    if (w >= a->top)
851
0
        return 0;
852
6.83k
    if (b == 0)
853
6.31k
        a->top = w;
854
527
    else {
855
527
        a->top = w + 1;
856
527
        a->d[w] &= ~(BN_MASK2 << b);
857
527
    }
858
6.83k
    a->flags |= BN_FLG_FIXED_TOP;
859
6.83k
    return 1;
860
6.83k
}
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.47M
{
875
2.47M
    if (b && !BN_is_zero(a))
876
557k
        a->neg = 1;
877
1.91M
    else
878
1.91M
        a->neg = 0;
879
2.47M
}
880
881
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
882
95.0M
{
883
95.0M
    int i;
884
95.0M
    BN_ULONG aa, bb;
885
886
95.0M
    if (n == 0)
887
0
        return 0;
888
889
95.0M
    aa = a[n - 1];
890
95.0M
    bb = b[n - 1];
891
95.0M
    if (aa != bb)
892
83.7M
        return ((aa > bb) ? 1 : -1);
893
97.9M
    for (i = n - 2; i >= 0; i--) {
894
93.3M
        aa = a[i];
895
93.3M
        bb = b[i];
896
93.3M
        if (aa != bb)
897
6.74M
            return ((aa > bb) ? 1 : -1);
898
93.3M
    }
899
4.59M
    return 0;
900
11.3M
}
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
133M
{
913
133M
    int n, i;
914
133M
    n = cl - 1;
915
916
133M
    if (dl < 0) {
917
2.84M
        for (i = dl; i < 0; i++) {
918
2.75M
            if (b[n - i] != 0)
919
1.29M
                return -1; /* a < b */
920
2.75M
        }
921
1.38M
    }
922
132M
    if (dl > 0) {
923
4.24M
        for (i = dl; i > 0; i--) {
924
4.01M
            if (a[n + i] != 0)
925
1.16M
                return 1; /* a > b */
926
4.01M
        }
927
1.39M
    }
928
131M
    return bn_cmp_words(a, b, cl);
929
132M
}
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
114M
{
940
114M
    BN_ULONG t;
941
114M
    int i;
942
943
114M
    bn_wcheck_size(a, nwords);
944
114M
    bn_wcheck_size(b, nwords);
945
946
114M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
947
948
114M
    t = (a->top ^ b->top) & condition;
949
114M
    a->top ^= t;
950
114M
    b->top ^= t;
951
952
114M
    t = (a->neg ^ b->neg) & condition;
953
114M
    a->neg ^= t;
954
114M
    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
114M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
979
980
114M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
981
114M
    a->flags ^= t;
982
114M
    b->flags ^= t;
983
984
    /* conditionally swap the data */
985
18.2G
    for (i = 0; i < nwords; i++) {
986
18.1G
        t = (a->d[i] ^ b->d[i]) & condition;
987
18.1G
        a->d[i] ^= t;
988
18.1G
        b->d[i] ^= t;
989
18.1G
    }
990
114M
}
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
209k
{
998
209k
    int secbits, bits;
999
209k
    if (L >= 15360)
1000
973
        secbits = 256;
1001
208k
    else if (L >= 7680)
1002
1.19k
        secbits = 192;
1003
207k
    else if (L >= 3072)
1004
2.64k
        secbits = 128;
1005
205k
    else if (L >= 2048)
1006
4.42k
        secbits = 112;
1007
200k
    else if (L >= 1024)
1008
119k
        secbits = 80;
1009
80.9k
    else
1010
80.9k
        return 0;
1011
129k
    if (N == -1)
1012
11.4k
        return secbits;
1013
117k
    bits = N / 2;
1014
117k
    if (bits < 80)
1015
9.36k
        return 0;
1016
108k
    return bits >= secbits ? secbits : bits;
1017
117k
}
1018
1019
void BN_zero_ex(BIGNUM *a)
1020
1.23G
{
1021
1.23G
    a->neg = 0;
1022
1.23G
    a->top = 0;
1023
1.23G
    a->flags &= ~BN_FLG_FIXED_TOP;
1024
1.23G
}
1025
1026
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1027
116M
{
1028
116M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1029
116M
}
1030
1031
int BN_is_zero(const BIGNUM *a)
1032
427M
{
1033
427M
    return a->top == 0;
1034
427M
}
1035
1036
int BN_is_one(const BIGNUM *a)
1037
115M
{
1038
115M
    return BN_abs_is_word(a, 1) && !a->neg;
1039
115M
}
1040
1041
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1042
314k
{
1043
314k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1044
314k
}
1045
1046
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1047
6.39k
{
1048
6.39k
    int res, i;
1049
6.39k
    const BN_ULONG *ap = a->d;
1050
1051
6.39k
    if (a->neg || a->top == 0)
1052
0
        return 0;
1053
1054
6.39k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
1055
1056
25.4k
    for (i = 1; i < a->top; i++)
1057
19.0k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
1058
19.0k
            res, 0);
1059
6.39k
    return res;
1060
6.39k
}
1061
1062
int BN_is_odd(const BIGNUM *a)
1063
72.8M
{
1064
72.8M
    return (a->top > 0) && (a->d[0] & 1);
1065
72.8M
}
1066
1067
int BN_is_negative(const BIGNUM *a)
1068
8.37M
{
1069
8.37M
    return (a->neg != 0);
1070
8.37M
}
1071
1072
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1073
    BN_CTX *ctx)
1074
2.51M
{
1075
2.51M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1076
2.51M
}
1077
1078
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1079
21.4M
{
1080
21.4M
    dest->d = b->d;
1081
21.4M
    dest->top = b->top;
1082
21.4M
    dest->dmax = b->dmax;
1083
21.4M
    dest->neg = b->neg;
1084
21.4M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1085
21.4M
        | (b->flags & ~BN_FLG_MALLOCED)
1086
21.4M
        | BN_FLG_STATIC_DATA | flags);
1087
21.4M
}
1088
1089
BN_GENCB *BN_GENCB_new(void)
1090
4.63k
{
1091
4.63k
    BN_GENCB *ret;
1092
1093
4.63k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1094
0
        return NULL;
1095
1096
4.63k
    return ret;
1097
4.63k
}
1098
1099
void BN_GENCB_free(BN_GENCB *cb)
1100
6.46k
{
1101
6.46k
    if (cb == NULL)
1102
1.82k
        return;
1103
4.63k
    OPENSSL_free(cb);
1104
4.63k
}
1105
1106
void BN_set_flags(BIGNUM *b, int n)
1107
2.29M
{
1108
2.29M
    b->flags |= n;
1109
2.29M
}
1110
1111
int BN_get_flags(const BIGNUM *b, int n)
1112
772M
{
1113
772M
    return b->flags & n;
1114
772M
}
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.63k
{
1130
4.63k
    BN_GENCB *tmp_gencb = gencb;
1131
4.63k
    tmp_gencb->ver = 2;
1132
4.63k
    tmp_gencb->arg = cb_arg;
1133
4.63k
    tmp_gencb->cb.cb_2 = callback;
1134
4.63k
}
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.30G
{
1143
2.30G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1144
2.30G
}
1145
1146
void bn_correct_top_consttime(BIGNUM *a)
1147
16.6k
{
1148
16.6k
    int j, atop;
1149
16.6k
    BN_ULONG limb;
1150
16.6k
    unsigned int mask;
1151
1152
1.08M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1153
1.06M
        limb = a->d[j];
1154
1.06M
        limb |= 0 - limb;
1155
1.06M
        limb >>= BN_BITS2 - 1;
1156
1.06M
        limb = 0 - limb;
1157
1.06M
        mask = (unsigned int)limb;
1158
1.06M
        mask &= constant_time_msb(j - a->top);
1159
1.06M
        atop = constant_time_select_int(mask, j + 1, atop);
1160
1.06M
    }
1161
1162
16.6k
    mask = constant_time_eq_int(atop, 0);
1163
16.6k
    a->top = atop;
1164
16.6k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1165
16.6k
    a->flags &= ~BN_FLG_FIXED_TOP;
1166
16.6k
}
1167
1168
void bn_correct_top(BIGNUM *a)
1169
908M
{
1170
908M
    BN_ULONG *ftl;
1171
908M
    int tmp_top = a->top;
1172
1173
908M
    if (tmp_top > 0) {
1174
2.56G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1175
2.56G
            ftl--;
1176
2.56G
            if (*ftl != 0)
1177
905M
                break;
1178
2.56G
        }
1179
908M
        a->top = tmp_top;
1180
908M
    }
1181
908M
    if (a->top == 0)
1182
3.79M
        a->neg = 0;
1183
908M
    a->flags &= ~BN_FLG_FIXED_TOP;
1184
908M
    bn_pollute(a);
1185
908M
}