Coverage Report

Created: 2026-07-12 07:21

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