Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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.52M
{
84
1.52M
    static const BN_ULONG data_one = 1L;
85
1.52M
    static const BIGNUM const_one = {
86
1.52M
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
87
1.52M
    };
88
89
1.52M
    return &const_one;
90
1.52M
}
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
187M
{
103
187M
    BN_ULONG x, mask;
104
187M
    int bits = (l != 0);
105
106
187M
#if BN_BITS2 > 32
107
187M
    x = l >> 32;
108
187M
    mask = (0 - x) & BN_MASK2;
109
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
110
187M
    bits += 32 & mask;
111
187M
    l ^= (x ^ l) & mask;
112
187M
#endif
113
114
187M
    x = l >> 16;
115
187M
    mask = (0 - x) & BN_MASK2;
116
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
117
187M
    bits += 16 & mask;
118
187M
    l ^= (x ^ l) & mask;
119
120
187M
    x = l >> 8;
121
187M
    mask = (0 - x) & BN_MASK2;
122
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
123
187M
    bits += 8 & mask;
124
187M
    l ^= (x ^ l) & mask;
125
126
187M
    x = l >> 4;
127
187M
    mask = (0 - x) & BN_MASK2;
128
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
129
187M
    bits += 4 & mask;
130
187M
    l ^= (x ^ l) & mask;
131
132
187M
    x = l >> 2;
133
187M
    mask = (0 - x) & BN_MASK2;
134
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
135
187M
    bits += 2 & mask;
136
187M
    l ^= (x ^ l) & mask;
137
138
187M
    x = l >> 1;
139
187M
    mask = (0 - x) & BN_MASK2;
140
187M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
141
187M
    bits += 1 & mask;
142
143
187M
    return bits;
144
187M
}
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
4.57M
{
155
4.57M
    int j, ret;
156
4.57M
    unsigned int mask, past_i;
157
4.57M
    int i = a->top - 1;
158
4.57M
    bn_check_top(a);
159
160
45.3M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
40.8M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
40.8M
        ret += BN_BITS2 & (~mask & ~past_i);
164
40.8M
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
40.8M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
40.8M
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
4.57M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
4.57M
    return ret & mask;
176
4.57M
}
177
178
int BN_num_bits(const BIGNUM *a)
179
13.5M
{
180
13.5M
    int i = a->top - 1;
181
13.5M
    bn_check_top(a);
182
183
13.5M
    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.37M
        return bn_num_bits_consttime(a);
194
2.37M
    }
195
196
11.2M
    if (BN_is_zero(a))
197
235k
        return 0;
198
199
10.9M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
11.2M
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
58.2M
{
204
58.2M
    if (BN_get_flags(a, BN_FLG_SECURE))
205
1.15M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
57.1M
    else if (clear != 0)
207
35.0M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
22.0M
    else
209
22.0M
        OPENSSL_free(a->d);
210
58.2M
}
211
212
void BN_clear_free(BIGNUM *a)
213
29.7M
{
214
29.7M
    if (a == NULL)
215
4.51M
        return;
216
25.2M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
217
22.8M
        bn_free_d(a, 1);
218
25.2M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
219
2.21M
        OPENSSL_cleanse(a, sizeof(*a));
220
2.21M
        OPENSSL_free(a);
221
2.21M
    }
222
25.2M
}
223
224
void BN_free(BIGNUM *a)
225
39.1M
{
226
39.1M
    if (a == NULL)
227
17.0M
        return;
228
22.1M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
229
22.0M
        bn_free_d(a, 0);
230
22.1M
    if (a->flags & BN_FLG_MALLOCED)
231
22.0M
        OPENSSL_free(a);
232
22.1M
}
233
234
void bn_init(BIGNUM *a)
235
58.1M
{
236
58.1M
    static BIGNUM nilbn;
237
238
58.1M
    *a = nilbn;
239
58.1M
    bn_check_top(a);
240
58.1M
}
241
242
BIGNUM *BN_new(void)
243
24.2M
{
244
24.2M
    BIGNUM *ret;
245
246
24.2M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
247
0
        return NULL;
248
24.2M
    ret->flags = BN_FLG_MALLOCED;
249
24.2M
    bn_check_top(ret);
250
24.2M
    return ret;
251
24.2M
}
252
253
BIGNUM *BN_secure_new(void)
254
1.28M
{
255
1.28M
    BIGNUM *ret = BN_new();
256
257
1.28M
    if (ret != NULL)
258
1.28M
        ret->flags |= BN_FLG_SECURE;
259
1.28M
    return ret;
260
1.28M
}
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
24.6M
{
266
24.6M
    BN_ULONG *a = NULL;
267
268
24.6M
    if (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
24.6M
    if (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
24.6M
    if (BN_get_flags(b, BN_FLG_SECURE))
277
609k
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
278
23.9M
    else
279
23.9M
        a = OPENSSL_zalloc(words * sizeof(*a));
280
24.6M
    if (a == NULL)
281
0
        return NULL;
282
283
24.6M
    assert(b->top <= words);
284
24.6M
    if (b->top > 0)
285
2.76M
        memcpy(a, b->d, sizeof(*a) * b->top);
286
287
24.6M
    return a;
288
24.6M
}
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
24.6M
{
300
24.6M
    if (words > b->dmax) {
301
24.6M
        BN_ULONG *a = bn_expand_internal(b, words);
302
24.6M
        if (!a)
303
0
            return NULL;
304
24.6M
        if (b->d != NULL)
305
5.76M
            bn_free_d(b, 1);
306
24.6M
        b->d = a;
307
24.6M
        b->dmax = words;
308
24.6M
    }
309
310
24.6M
    return b;
311
24.6M
}
312
313
BIGNUM *BN_dup(const BIGNUM *a)
314
1.46M
{
315
1.46M
    BIGNUM *t;
316
317
1.46M
    if (a == NULL)
318
0
        return NULL;
319
1.46M
    bn_check_top(a);
320
321
1.46M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
322
1.46M
    if (t == NULL)
323
0
        return NULL;
324
1.46M
    if (!BN_copy(t, a)) {
325
0
        BN_free(t);
326
0
        return NULL;
327
0
    }
328
1.46M
    bn_check_top(t);
329
1.46M
    return t;
330
1.46M
}
331
332
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
333
58.6M
{
334
58.6M
    int bn_words;
335
336
58.6M
    bn_check_top(b);
337
338
58.6M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
339
340
58.6M
    if (a == b)
341
8
        return a;
342
58.6M
    if (bn_wexpand(a, bn_words) == NULL)
343
0
        return NULL;
344
345
58.6M
    if (b->top > 0)
346
58.1M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
347
348
58.6M
    a->neg = b->neg;
349
58.6M
    a->top = b->top;
350
58.6M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
351
58.6M
    bn_check_top(a);
352
58.6M
    return a;
353
58.6M
}
354
355
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA | BN_FLG_CONSTTIME | BN_FLG_SECURE | BN_FLG_FIXED_TOP))
356
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
357
358
void BN_swap(BIGNUM *a, BIGNUM *b)
359
0
{
360
0
    int flags_old_a, flags_old_b;
361
0
    BN_ULONG *tmp_d;
362
0
    int tmp_top, tmp_dmax, tmp_neg;
363
364
0
    bn_check_top(a);
365
0
    bn_check_top(b);
366
367
0
    flags_old_a = a->flags;
368
0
    flags_old_b = b->flags;
369
370
0
    tmp_d = a->d;
371
0
    tmp_top = a->top;
372
0
    tmp_dmax = a->dmax;
373
0
    tmp_neg = a->neg;
374
375
0
    a->d = b->d;
376
0
    a->top = b->top;
377
0
    a->dmax = b->dmax;
378
0
    a->neg = b->neg;
379
380
0
    b->d = tmp_d;
381
0
    b->top = tmp_top;
382
0
    b->dmax = tmp_dmax;
383
0
    b->neg = tmp_neg;
384
385
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
386
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
387
0
    bn_check_top(a);
388
0
    bn_check_top(b);
389
0
}
390
391
void BN_clear(BIGNUM *a)
392
376k
{
393
376k
    if (a == NULL)
394
0
        return;
395
376k
    bn_check_top(a);
396
376k
    if (a->d != NULL)
397
35.7k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
398
376k
    a->neg = 0;
399
376k
    a->top = 0;
400
376k
    a->flags &= ~BN_FLG_FIXED_TOP;
401
376k
}
402
403
BN_ULONG BN_get_word(const BIGNUM *a)
404
264
{
405
264
    if (a->top > 1)
406
9
        return BN_MASK2;
407
255
    else if (a->top == 1)
408
211
        return a->d[0];
409
    /* a->top == 0 */
410
44
    return 0;
411
264
}
412
413
int BN_set_word(BIGNUM *a, BN_ULONG w)
414
2.62M
{
415
2.62M
    bn_check_top(a);
416
2.62M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
417
0
        return 0;
418
2.62M
    a->neg = 0;
419
2.62M
    a->d[0] = w;
420
2.62M
    a->top = (w ? 1 : 0);
421
2.62M
    a->flags &= ~BN_FLG_FIXED_TOP;
422
2.62M
    bn_check_top(a);
423
2.62M
    return 1;
424
2.62M
}
425
426
typedef enum { BIG,
427
    LITTLE } endianness_t;
428
typedef enum { SIGNED,
429
    UNSIGNED } signedness_t;
430
431
static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret,
432
    endianness_t endianness, signedness_t signedness)
433
9.94M
{
434
9.94M
    int inc;
435
9.94M
    const unsigned char *s2;
436
9.94M
    int inc2;
437
9.94M
    int neg = 0, xor = 0, carry = 0;
438
9.94M
    unsigned int i;
439
9.94M
    unsigned int n;
440
9.94M
    BIGNUM *bn = NULL;
441
442
    /* Negative length is not acceptable */
443
9.94M
    if (len < 0)
444
0
        return NULL;
445
446
9.94M
    if (ret == NULL)
447
5.55M
        ret = bn = BN_new();
448
9.94M
    if (ret == NULL)
449
0
        return NULL;
450
9.94M
    bn_check_top(ret);
451
452
    /*
453
     * If the input has no bits, the number is considered zero.
454
     * This makes calls with s==NULL and len==0 safe.
455
     */
456
9.94M
    if (len == 0) {
457
338k
        BN_clear(ret);
458
338k
        return ret;
459
338k
    }
460
461
    /*
462
     * The loop that does the work iterates from least to most
463
     * significant BIGNUM chunk, so we adapt parameters to transfer
464
     * input bytes accordingly.
465
     */
466
9.60M
    if (endianness == LITTLE) {
467
774k
        s2 = s + len - 1;
468
774k
        inc2 = -1;
469
774k
        inc = 1;
470
8.82M
    } else {
471
8.82M
        s2 = s;
472
8.82M
        inc2 = 1;
473
8.82M
        inc = -1;
474
8.82M
        s += len - 1;
475
8.82M
    }
476
477
    /* Take note of the signedness of the input bytes*/
478
9.60M
    if (signedness == SIGNED) {
479
0
        neg = !!(*s2 & 0x80);
480
0
        xor = neg ? 0xff : 0x00;
481
0
        carry = neg;
482
0
    }
483
484
    /*
485
     * Skip leading sign extensions (the value of |xor|).
486
     * This is the only spot where |s2| and |inc2| are used.
487
     */
488
24.6M
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
489
15.0M
        continue;
490
491
    /*
492
     * If there was a set of 0xff, we backtrack one byte unless the next
493
     * one has a sign bit, as the last 0xff is then part of the actual
494
     * number, rather then a mere sign extension.
495
     */
496
9.60M
    if (xor == 0xff) {
497
0
        if (len == 0 || !(*s2 & 0x80))
498
0
            len++;
499
0
    }
500
    /* If it was all zeros, we're done */
501
9.60M
    if (len == 0) {
502
213k
        ret->top = 0;
503
213k
        return ret;
504
213k
    }
505
9.38M
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
506
9.38M
    if (bn_wexpand(ret, (int)n) == NULL) {
507
0
        BN_free(bn);
508
0
        return NULL;
509
0
    }
510
9.38M
    ret->top = n;
511
9.38M
    ret->neg = neg;
512
105M
    for (i = 0; n-- > 0; i++) {
513
95.8M
        BN_ULONG l = 0; /* Accumulator */
514
95.8M
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
515
516
833M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
517
737M
            BN_ULONG byte_xored = *s ^ xor;
518
737M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
519
520
737M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
521
737M
            l |= (byte << m);
522
737M
        }
523
95.8M
        ret->d[i] = l;
524
95.8M
    }
525
    /*
526
     * need to call this due to clear byte at top if avoiding having the top
527
     * bit set (-ve number)
528
     */
529
9.38M
    bn_correct_top(ret);
530
9.38M
    return ret;
531
9.38M
}
532
533
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
534
9.16M
{
535
9.16M
    return bin2bn(s, len, ret, BIG, UNSIGNED);
536
9.16M
}
537
538
BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
539
0
{
540
0
    return bin2bn(s, len, ret, BIG, SIGNED);
541
0
}
542
543
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen,
544
    endianness_t endianness, signedness_t signedness)
545
1.47M
{
546
1.47M
    int inc;
547
1.47M
    int n, n8;
548
1.47M
    int xor = 0, carry = 0, ext = 0;
549
1.47M
    size_t i, lasti, j, atop, mask;
550
1.47M
    BN_ULONG l;
551
552
    /*
553
     * In case |a| is fixed-top, BN_num_bits can return bogus length,
554
     * but it's assumed that fixed-top inputs ought to be "nominated"
555
     * even for padded output, so it works out...
556
     */
557
1.47M
    n8 = BN_num_bits(a);
558
1.47M
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
559
560
    /* Take note of the signedness of the bignum */
561
1.47M
    if (signedness == SIGNED) {
562
0
        xor = a->neg ? 0xff : 0x00;
563
0
        carry = a->neg;
564
565
        /*
566
         * if |n * 8 == n|, then the MSbit is set, otherwise unset.
567
         * We must compensate with one extra byte if that doesn't
568
         * correspond to the signedness of the bignum with regards
569
         * to 2's complement.
570
         */
571
0
        ext = (n * 8 == n8)
572
0
            ? !a->neg /* MSbit set on nonnegative bignum */
573
0
            : a->neg; /* MSbit unset on negative bignum */
574
0
    }
575
576
1.47M
    if (tolen == -1) {
577
441k
        tolen = n + ext;
578
1.03M
    } else if (tolen < n + ext) { /* uncommon/unlike case */
579
4.81k
        BIGNUM temp = *a;
580
581
4.81k
        bn_correct_top(&temp);
582
4.81k
        n8 = BN_num_bits(&temp);
583
4.81k
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
584
4.81k
        if (tolen < n + ext)
585
4.81k
            return -1;
586
4.81k
    }
587
588
    /* Swipe through whole available data and don't give away padded zero. */
589
1.47M
    atop = a->dmax * BN_BYTES;
590
1.47M
    if (atop == 0) {
591
64.1k
        if (tolen != 0)
592
5.26k
            memset(to, '\0', tolen);
593
64.1k
        return tolen;
594
64.1k
    }
595
596
    /*
597
     * The loop that does the work iterates from least significant
598
     * to most significant BIGNUM limb, so we adapt parameters to
599
     * transfer output bytes accordingly.
600
     */
601
1.40M
    if (endianness == LITTLE) {
602
741k
        inc = 1;
603
741k
    } else {
604
665k
        inc = -1;
605
665k
        to += tolen - 1; /* Move to the last byte, not beyond */
606
665k
    }
607
608
1.40M
    lasti = atop - 1;
609
1.40M
    atop = a->top * BN_BYTES;
610
146M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
611
145M
        unsigned char byte, byte_xored;
612
613
145M
        l = a->d[i / BN_BYTES];
614
145M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
615
145M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
616
145M
        byte_xored = byte ^ xor;
617
145M
        *to = (unsigned char)(byte_xored + carry);
618
145M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
619
145M
        to += inc;
620
145M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
621
145M
    }
622
623
1.40M
    return tolen;
624
1.47M
}
625
626
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
627
290k
{
628
290k
    if (tolen < 0)
629
0
        return -1;
630
290k
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
631
290k
}
632
633
int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen)
634
0
{
635
0
    if (tolen < 0)
636
0
        return -1;
637
0
    return bn2binpad(a, to, tolen, BIG, SIGNED);
638
0
}
639
640
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
641
441k
{
642
441k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
643
441k
}
644
645
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
646
774k
{
647
774k
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
648
774k
}
649
650
BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
651
0
{
652
0
    return bin2bn(s, len, ret, LITTLE, SIGNED);
653
0
}
654
655
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
656
743k
{
657
743k
    if (tolen < 0)
658
0
        return -1;
659
743k
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
660
743k
}
661
662
int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen)
663
0
{
664
0
    if (tolen < 0)
665
0
        return -1;
666
0
    return bn2binpad(a, to, tolen, LITTLE, SIGNED);
667
0
}
668
669
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
670
694k
{
671
694k
    DECLARE_IS_ENDIAN;
672
673
694k
    if (IS_LITTLE_ENDIAN)
674
694k
        return BN_lebin2bn(s, len, ret);
675
0
    return BN_bin2bn(s, len, ret);
676
694k
}
677
678
BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret)
679
0
{
680
0
    DECLARE_IS_ENDIAN;
681
682
0
    if (IS_LITTLE_ENDIAN)
683
0
        return BN_signed_lebin2bn(s, len, ret);
684
0
    return BN_signed_bin2bn(s, len, ret);
685
0
}
686
687
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
688
694k
{
689
694k
    DECLARE_IS_ENDIAN;
690
691
694k
    if (IS_LITTLE_ENDIAN)
692
694k
        return BN_bn2lebinpad(a, to, tolen);
693
0
    return BN_bn2binpad(a, to, tolen);
694
694k
}
695
696
int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen)
697
0
{
698
0
    DECLARE_IS_ENDIAN;
699
700
0
    if (IS_LITTLE_ENDIAN)
701
0
        return BN_signed_bn2lebin(a, to, tolen);
702
0
    return BN_signed_bn2bin(a, to, tolen);
703
0
}
704
705
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
706
91.9M
{
707
91.9M
    int i;
708
91.9M
    BN_ULONG t1, t2, *ap, *bp;
709
710
    /*
711
     * As it is a public API function, we should handle NULL parameters in
712
     * some way. The function can’t return an error, so let’s define that NULL
713
     * is less than any BIGNUM.
714
     */
715
91.9M
    if (!ossl_assert(a != NULL && b != NULL))
716
0
        return (b == NULL) - (a == NULL);
717
718
91.9M
    ap = a->d;
719
91.9M
    bp = b->d;
720
721
91.9M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
722
90.4M
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
723
1.62M
        int res = 0;
724
1.62M
        int min_top = a->top < b->top ? a->top : b->top;
725
726
8.95M
        for (i = 0; i < min_top; i++) {
727
7.33M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
728
7.33M
                -1, res);
729
7.33M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
730
7.33M
                1, res);
731
7.33M
        }
732
733
1.71M
        for (i = min_top; i < a->top; ++i)
734
94.5k
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
735
94.5k
                res, 1);
736
737
1.62M
        for (i = min_top; i < b->top; ++i)
738
3.69k
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
739
3.69k
                res, -1);
740
741
1.62M
        return res;
742
1.62M
    }
743
744
90.2M
    bn_check_top(a);
745
90.2M
    bn_check_top(b);
746
747
90.2M
    i = a->top - b->top;
748
90.2M
    if (i != 0)
749
10.9M
        return i;
750
751
84.5M
    for (i = a->top - 1; i >= 0; i--) {
752
82.8M
        t1 = ap[i];
753
82.8M
        t2 = bp[i];
754
82.8M
        if (t1 != t2)
755
77.6M
            return ((t1 > t2) ? 1 : -1);
756
82.8M
    }
757
1.69M
    return 0;
758
79.3M
}
759
760
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
761
11.6M
{
762
11.6M
    int i;
763
11.6M
    int gt, lt;
764
11.6M
    BN_ULONG t1, t2;
765
766
11.6M
    if ((a == NULL) || (b == NULL)) {
767
0
        if (a != NULL)
768
0
            return -1;
769
0
        else if (b != NULL)
770
0
            return 1;
771
0
        else
772
0
            return 0;
773
0
    }
774
775
11.6M
    bn_check_top(a);
776
11.6M
    bn_check_top(b);
777
778
11.6M
    if (a->neg != b->neg) {
779
995
        if (a->neg)
780
443
            return -1;
781
552
        else
782
552
            return 1;
783
995
    }
784
11.6M
    if (a->neg == 0) {
785
11.6M
        gt = 1;
786
11.6M
        lt = -1;
787
11.6M
    } else {
788
8.69k
        gt = -1;
789
8.69k
        lt = 1;
790
8.69k
    }
791
792
11.6M
    if (a->top > b->top)
793
728k
        return gt;
794
10.9M
    if (a->top < b->top)
795
1.42M
        return lt;
796
32.2M
    for (i = a->top - 1; i >= 0; i--) {
797
30.3M
        t1 = a->d[i];
798
30.3M
        t2 = b->d[i];
799
30.3M
        if (t1 > t2)
800
3.20M
            return gt;
801
27.1M
        if (t1 < t2)
802
4.36M
            return lt;
803
27.1M
    }
804
1.93M
    return 0;
805
9.50M
}
806
807
int BN_set_bit(BIGNUM *a, int n)
808
1.76M
{
809
1.76M
    int i, j, k;
810
811
1.76M
    if (n < 0)
812
0
        return 0;
813
814
1.76M
    i = n / BN_BITS2;
815
1.76M
    j = n % BN_BITS2;
816
1.76M
    if (a->top <= i) {
817
1.76M
        if (bn_wexpand(a, i + 1) == NULL)
818
0
            return 0;
819
14.9M
        for (k = a->top; k < i + 1; k++)
820
13.2M
            a->d[k] = 0;
821
1.76M
        a->top = i + 1;
822
1.76M
        a->flags &= ~BN_FLG_FIXED_TOP;
823
1.76M
    }
824
825
1.76M
    a->d[i] |= (((BN_ULONG)1) << j);
826
1.76M
    bn_check_top(a);
827
1.76M
    return 1;
828
1.76M
}
829
830
int BN_clear_bit(BIGNUM *a, int n)
831
218
{
832
218
    int i, j;
833
834
218
    bn_check_top(a);
835
218
    if (n < 0)
836
0
        return 0;
837
838
218
    i = n / BN_BITS2;
839
218
    j = n % BN_BITS2;
840
218
    if (a->top <= i)
841
0
        return 0;
842
843
218
    a->d[i] &= (~(((BN_ULONG)1) << j));
844
218
    bn_correct_top(a);
845
218
    return 1;
846
218
}
847
848
int BN_is_bit_set(const BIGNUM *a, int n)
849
86.9M
{
850
86.9M
    int i, j;
851
852
86.9M
    bn_check_top(a);
853
86.9M
    if (n < 0)
854
2.64k
        return 0;
855
86.9M
    i = n / BN_BITS2;
856
86.9M
    j = n % BN_BITS2;
857
86.9M
    if (a->top <= i)
858
3.82k
        return 0;
859
86.9M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
860
86.9M
}
861
862
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
863
6.79k
{
864
6.79k
    int b, w;
865
866
6.79k
    if (n < 0)
867
0
        return 0;
868
869
6.79k
    w = n / BN_BITS2;
870
6.79k
    b = n % BN_BITS2;
871
6.79k
    if (w >= a->top)
872
0
        return 0;
873
6.79k
    if (b == 0)
874
5.09k
        a->top = w;
875
1.70k
    else {
876
1.70k
        a->top = w + 1;
877
1.70k
        a->d[w] &= ~(BN_MASK2 << b);
878
1.70k
    }
879
6.79k
    a->flags |= BN_FLG_FIXED_TOP;
880
6.79k
    return 1;
881
6.79k
}
882
883
int BN_mask_bits(BIGNUM *a, int n)
884
0
{
885
0
    int ret;
886
887
0
    bn_check_top(a);
888
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
889
0
    if (ret)
890
0
        bn_correct_top(a);
891
0
    return ret;
892
0
}
893
894
void BN_set_negative(BIGNUM *a, int b)
895
1.95M
{
896
1.95M
    if (b && !BN_is_zero(a))
897
457k
        a->neg = 1;
898
1.49M
    else
899
1.49M
        a->neg = 0;
900
1.95M
}
901
902
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
903
55.2M
{
904
55.2M
    int i;
905
55.2M
    BN_ULONG aa, bb;
906
907
55.2M
    if (n == 0)
908
0
        return 0;
909
910
55.2M
    aa = a[n - 1];
911
55.2M
    bb = b[n - 1];
912
55.2M
    if (aa != bb)
913
49.2M
        return ((aa > bb) ? 1 : -1);
914
51.4M
    for (i = n - 2; i >= 0; i--) {
915
49.0M
        aa = a[i];
916
49.0M
        bb = b[i];
917
49.0M
        if (aa != bb)
918
3.63M
            return ((aa > bb) ? 1 : -1);
919
49.0M
    }
920
2.36M
    return 0;
921
5.99M
}
922
923
/*
924
 * Here follows a specialised variants of bn_cmp_words().  It has the
925
 * capability of performing the operation on arrays of different sizes. The
926
 * sizes of those arrays is expressed through cl, which is the common length
927
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
928
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
929
 * BN_ULONGs...
930
 */
931
932
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
933
92.3M
{
934
92.3M
    int n, i;
935
92.3M
    n = cl - 1;
936
937
92.3M
    if (dl < 0) {
938
2.31M
        for (i = dl; i < 0; i++) {
939
2.24M
            if (b[n - i] != 0)
940
1.06M
                return -1; /* a < b */
941
2.24M
        }
942
1.14M
    }
943
91.3M
    if (dl > 0) {
944
3.28M
        for (i = dl; i > 0; i--) {
945
3.10M
            if (a[n + i] != 0)
946
970k
                return 1; /* a > b */
947
3.10M
        }
948
1.14M
    }
949
90.3M
    return bn_cmp_words(a, b, cl);
950
91.3M
}
951
952
/*-
953
 * Constant-time conditional swap of a and b.
954
 * a and b are swapped if condition is not 0.
955
 * nwords is the number of words to swap.
956
 * Assumes that at least nwords are allocated in both a and b.
957
 * Assumes that no more than nwords are used by either a or b.
958
 */
959
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
960
86.0M
{
961
86.0M
    BN_ULONG t;
962
86.0M
    int i;
963
964
86.0M
    bn_wcheck_size(a, nwords);
965
86.0M
    bn_wcheck_size(b, nwords);
966
967
86.0M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
968
969
86.0M
    t = (a->top ^ b->top) & value_barrier_bn(condition);
970
86.0M
    a->top ^= t;
971
86.0M
    b->top ^= t;
972
973
86.0M
    t = (a->neg ^ b->neg) & value_barrier_bn(condition);
974
86.0M
    a->neg ^= t;
975
86.0M
    b->neg ^= t;
976
977
    /*-
978
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
979
     * is actually to treat it as it's read-only data, and some (if not most)
980
     * of it does reside in read-only segment. In other words observation of
981
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
982
     * condition. It would either cause SEGV or effectively cause data
983
     * corruption.
984
     *
985
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
986
     * preserved.
987
     *
988
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
989
     * allocated and hence how to free it.
990
     *
991
     * BN_FLG_CONSTTIME: sufficient to mask and swap
992
     *
993
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
994
     * the data, so the d array may be padded with additional 0 values (i.e.
995
     * top could be greater than the minimal value that it could be). We should
996
     * be swapping it
997
     */
998
999
86.0M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
1000
1001
86.0M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & value_barrier_bn(condition);
1002
86.0M
    a->flags ^= t;
1003
86.0M
    b->flags ^= t;
1004
1005
    /* conditionally swap the data */
1006
12.4G
    for (i = 0; i < nwords; i++) {
1007
12.4G
        t = (a->d[i] ^ b->d[i]) & value_barrier_bn(condition);
1008
12.4G
        a->d[i] ^= t;
1009
12.4G
        b->d[i] ^= t;
1010
12.4G
    }
1011
86.0M
}
1012
1013
#undef BN_CONSTTIME_SWAP_FLAGS
1014
1015
/* Bits of security, see SP800-57 */
1016
1017
int BN_security_bits(int L, int N)
1018
171k
{
1019
171k
    int secbits, bits;
1020
171k
    if (L >= 15360)
1021
714
        secbits = 256;
1022
170k
    else if (L >= 7680)
1023
387
        secbits = 192;
1024
170k
    else if (L >= 3072)
1025
2.11k
        secbits = 128;
1026
168k
    else if (L >= 2048)
1027
4.64k
        secbits = 112;
1028
163k
    else if (L >= 1024)
1029
99.8k
        secbits = 80;
1030
63.6k
    else
1031
63.6k
        return 0;
1032
107k
    if (N == -1)
1033
9.74k
        return secbits;
1034
97.9k
    bits = N / 2;
1035
97.9k
    if (bits < 80)
1036
7.21k
        return 0;
1037
90.7k
    return bits >= secbits ? secbits : bits;
1038
97.9k
}
1039
1040
void BN_zero_ex(BIGNUM *a)
1041
950M
{
1042
950M
    a->neg = 0;
1043
950M
    a->top = 0;
1044
950M
    a->flags &= ~BN_FLG_FIXED_TOP;
1045
950M
}
1046
1047
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1048
85.7M
{
1049
85.7M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1050
85.7M
}
1051
1052
int BN_is_zero(const BIGNUM *a)
1053
306M
{
1054
306M
    return a->top == 0;
1055
306M
}
1056
1057
int BN_is_one(const BIGNUM *a)
1058
84.4M
{
1059
84.4M
    return BN_abs_is_word(a, 1) && !a->neg;
1060
84.4M
}
1061
1062
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1063
280k
{
1064
280k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1065
280k
}
1066
1067
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1068
5.38k
{
1069
5.38k
    int res, i;
1070
5.38k
    const BN_ULONG *ap = a->d;
1071
1072
5.38k
    if (a->neg || a->top == 0)
1073
0
        return 0;
1074
1075
5.38k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
1076
1077
21.2k
    for (i = 1; i < a->top; i++)
1078
15.8k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
1079
15.8k
            res, 0);
1080
5.38k
    return res;
1081
5.38k
}
1082
1083
int BN_is_odd(const BIGNUM *a)
1084
50.8M
{
1085
50.8M
    return (a->top > 0) && (a->d[0] & 1);
1086
50.8M
}
1087
1088
int BN_is_negative(const BIGNUM *a)
1089
5.85M
{
1090
5.85M
    return (a->neg != 0);
1091
5.85M
}
1092
1093
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1094
    BN_CTX *ctx)
1095
1.92M
{
1096
1.92M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1097
1.92M
}
1098
1099
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1100
17.0M
{
1101
17.0M
    dest->d = b->d;
1102
17.0M
    dest->top = b->top;
1103
17.0M
    dest->dmax = b->dmax;
1104
17.0M
    dest->neg = b->neg;
1105
17.0M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1106
17.0M
        | (b->flags & ~BN_FLG_MALLOCED)
1107
17.0M
        | BN_FLG_STATIC_DATA | flags);
1108
17.0M
}
1109
1110
BN_GENCB *BN_GENCB_new(void)
1111
3.52k
{
1112
3.52k
    BN_GENCB *ret;
1113
1114
3.52k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1115
0
        return NULL;
1116
1117
3.52k
    return ret;
1118
3.52k
}
1119
1120
void BN_GENCB_free(BN_GENCB *cb)
1121
4.86k
{
1122
4.86k
    if (cb == NULL)
1123
1.33k
        return;
1124
3.52k
    OPENSSL_free(cb);
1125
3.52k
}
1126
1127
void BN_set_flags(BIGNUM *b, int n)
1128
1.56M
{
1129
1.56M
    b->flags |= n;
1130
1.56M
}
1131
1132
int BN_get_flags(const BIGNUM *b, int n)
1133
574M
{
1134
574M
    return b->flags & n;
1135
574M
}
1136
1137
/* Populate a BN_GENCB structure with an "old"-style callback */
1138
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback)(int, int, void *),
1139
    void *cb_arg)
1140
0
{
1141
0
    BN_GENCB *tmp_gencb = gencb;
1142
0
    tmp_gencb->ver = 1;
1143
0
    tmp_gencb->arg = cb_arg;
1144
0
    tmp_gencb->cb.cb_1 = callback;
1145
0
}
1146
1147
/* Populate a BN_GENCB structure with a "new"-style callback */
1148
void BN_GENCB_set(BN_GENCB *gencb, int (*callback)(int, int, BN_GENCB *),
1149
    void *cb_arg)
1150
3.52k
{
1151
3.52k
    BN_GENCB *tmp_gencb = gencb;
1152
3.52k
    tmp_gencb->ver = 2;
1153
3.52k
    tmp_gencb->arg = cb_arg;
1154
3.52k
    tmp_gencb->cb.cb_2 = callback;
1155
3.52k
}
1156
1157
void *BN_GENCB_get_arg(BN_GENCB *cb)
1158
0
{
1159
0
    return cb->arg;
1160
0
}
1161
1162
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1163
1.77G
{
1164
1.77G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1165
1.77G
}
1166
1167
void bn_correct_top_consttime(BIGNUM *a)
1168
13.2k
{
1169
13.2k
    int j, atop;
1170
13.2k
    BN_ULONG limb;
1171
13.2k
    unsigned int mask;
1172
1173
861k
    for (j = 0, atop = 0; j < a->dmax; j++) {
1174
848k
        limb = a->d[j];
1175
848k
        limb |= 0 - limb;
1176
848k
        limb >>= BN_BITS2 - 1;
1177
848k
        limb = 0 - limb;
1178
848k
        mask = (unsigned int)limb;
1179
848k
        mask &= constant_time_msb(j - a->top);
1180
848k
        atop = constant_time_select_int(mask, j + 1, atop);
1181
848k
    }
1182
1183
13.2k
    mask = constant_time_eq_int(atop, 0);
1184
13.2k
    a->top = atop;
1185
13.2k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1186
13.2k
    a->flags &= ~BN_FLG_FIXED_TOP;
1187
13.2k
}
1188
1189
void bn_correct_top(BIGNUM *a)
1190
459M
{
1191
459M
    BN_ULONG *ftl;
1192
459M
    int tmp_top = a->top;
1193
1194
459M
    if (tmp_top > 0) {
1195
1.33G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1196
1.33G
            ftl--;
1197
1.33G
            if (*ftl != 0)
1198
457M
                break;
1199
1.33G
        }
1200
459M
        a->top = tmp_top;
1201
459M
    }
1202
459M
    if (a->top == 0)
1203
2.10M
        a->neg = 0;
1204
459M
    a->flags &= ~BN_FLG_FIXED_TOP;
1205
459M
    bn_pollute(a);
1206
459M
}