Coverage Report

Created: 2026-07-23 06:28

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-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 = {
86
1.98M
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
87
1.98M
    };
88
89
1.98M
    return &const_one;
90
1.98M
}
91
92
/*
93
 * Old Visual Studio ARM compiler miscompiles BN_num_bits_word()
94
 * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html
95
 */
96
#if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \
97
    && _MSC_VER >= 1400 && _MSC_VER < 1501
98
#define MS_BROKEN_BN_num_bits_word
99
#pragma optimize("", off)
100
#endif
101
int BN_num_bits_word(BN_ULONG l)
102
260M
{
103
260M
    BN_ULONG x, mask;
104
260M
    int bits = (l != 0);
105
106
260M
#if BN_BITS2 > 32
107
260M
    x = l >> 32;
108
260M
    mask = (0 - x) & BN_MASK2;
109
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
110
260M
    bits += 32 & mask;
111
260M
    l ^= (x ^ l) & mask;
112
260M
#endif
113
114
260M
    x = l >> 16;
115
260M
    mask = (0 - x) & BN_MASK2;
116
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
117
260M
    bits += 16 & mask;
118
260M
    l ^= (x ^ l) & mask;
119
120
260M
    x = l >> 8;
121
260M
    mask = (0 - x) & BN_MASK2;
122
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
123
260M
    bits += 8 & mask;
124
260M
    l ^= (x ^ l) & mask;
125
126
260M
    x = l >> 4;
127
260M
    mask = (0 - x) & BN_MASK2;
128
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
129
260M
    bits += 4 & mask;
130
260M
    l ^= (x ^ l) & mask;
131
132
260M
    x = l >> 2;
133
260M
    mask = (0 - x) & BN_MASK2;
134
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
135
260M
    bits += 2 & mask;
136
260M
    l ^= (x ^ l) & mask;
137
138
260M
    x = l >> 1;
139
260M
    mask = (0 - x) & BN_MASK2;
140
260M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
141
260M
    bits += 1 & mask;
142
143
260M
    return bits;
144
260M
}
145
#ifdef MS_BROKEN_BN_num_bits_word
146
#pragma optimize("", on)
147
#endif
148
149
/*
150
 * This function still leaks `a->dmax`: it's caller's responsibility to
151
 * expand the input `a` in advance to a public length.
152
 */
153
static ossl_inline int bn_num_bits_consttime(const BIGNUM *a)
154
8.42M
{
155
8.42M
    int j, ret;
156
8.42M
    unsigned int mask, past_i;
157
8.42M
    int i = a->top - 1;
158
8.42M
    bn_check_top(a);
159
160
67.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
58.5M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
58.5M
        ret += BN_BITS2 & (~mask & ~past_i);
164
58.5M
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
58.5M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
58.5M
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
8.42M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
8.42M
    return ret & mask;
176
8.42M
}
177
178
int BN_num_bits(const BIGNUM *a)
179
30.7M
{
180
30.7M
    int i = a->top - 1;
181
30.7M
    bn_check_top(a);
182
183
30.7M
    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.37M
        return bn_num_bits_consttime(a);
194
6.37M
    }
195
196
24.4M
    if (BN_is_zero(a))
197
684k
        return 0;
198
199
23.7M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
24.4M
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
78.8M
{
204
78.8M
    if (BN_get_flags(a, BN_FLG_SECURE))
205
1.83M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
76.9M
    else if (clear != 0)
207
47.3M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
29.6M
    else
209
29.6M
        OPENSSL_free(a->d);
210
78.8M
}
211
212
void BN_clear_free(BIGNUM *a)
213
39.6M
{
214
39.6M
    if (a == NULL)
215
5.82M
        return;
216
33.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
217
30.6M
        bn_free_d(a, 1);
218
33.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
219
3.30M
        OPENSSL_cleanse(a, sizeof(*a));
220
3.30M
        OPENSSL_free(a);
221
3.30M
    }
222
33.7M
}
223
224
void BN_free(BIGNUM *a)
225
50.7M
{
226
50.7M
    if (a == NULL)
227
21.0M
        return;
228
29.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
229
29.6M
        bn_free_d(a, 0);
230
29.7M
    if (a->flags & BN_FLG_MALLOCED)
231
29.6M
        OPENSSL_free(a);
232
29.7M
}
233
234
void bn_init(BIGNUM *a)
235
75.1M
{
236
75.1M
    static BIGNUM nilbn;
237
238
75.1M
    *a = nilbn;
239
75.1M
    bn_check_top(a);
240
75.1M
}
241
242
BIGNUM *BN_new(void)
243
32.9M
{
244
32.9M
    BIGNUM *ret;
245
246
32.9M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
247
0
        return NULL;
248
32.9M
    ret->flags = BN_FLG_MALLOCED;
249
32.9M
    bn_check_top(ret);
250
32.9M
    return ret;
251
32.9M
}
252
253
BIGNUM *BN_secure_new(void)
254
2.08M
{
255
2.08M
    BIGNUM *ret = BN_new();
256
257
2.08M
    if (ret != NULL)
258
2.08M
        ret->flags |= BN_FLG_SECURE;
259
2.08M
    return ret;
260
2.08M
}
261
262
/* This is used by bn_expand2() */
263
/* The caller MUST check that words > b->dmax before calling this */
264
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
265
51.4M
{
266
51.4M
    BN_ULONG *a = NULL;
267
268
51.4M
    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
51.4M
    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
51.4M
    if (BN_get_flags(b, BN_FLG_SECURE))
277
1.37M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
278
50.0M
    else
279
50.0M
        a = OPENSSL_zalloc(words * sizeof(*a));
280
51.4M
    if (a == NULL)
281
0
        return NULL;
282
283
51.4M
    assert(b->top <= words);
284
51.4M
    if (b->top > 0)
285
5.87M
        memcpy(a, b->d, sizeof(*a) * b->top);
286
287
51.4M
    return a;
288
51.4M
}
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
51.4M
{
300
51.4M
    if (words > b->dmax) {
301
51.4M
        BN_ULONG *a = bn_expand_internal(b, words);
302
51.4M
        if (!a)
303
0
            return NULL;
304
51.4M
        if (b->d != NULL)
305
12.6M
            bn_free_d(b, 1);
306
51.4M
        b->d = a;
307
51.4M
        b->dmax = words;
308
51.4M
    }
309
310
51.4M
    return b;
311
51.4M
}
312
313
BIGNUM *BN_dup(const BIGNUM *a)
314
2.64M
{
315
2.64M
    BIGNUM *t;
316
317
2.64M
    if (a == NULL)
318
0
        return NULL;
319
2.64M
    bn_check_top(a);
320
321
2.64M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
322
2.64M
    if (t == NULL)
323
0
        return NULL;
324
2.64M
    if (!BN_copy(t, a)) {
325
0
        BN_free(t);
326
0
        return NULL;
327
0
    }
328
2.64M
    bn_check_top(t);
329
2.64M
    return t;
330
2.64M
}
331
332
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
333
116M
{
334
116M
    int bn_words;
335
336
116M
    bn_check_top(b);
337
338
116M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
339
340
116M
    if (a == b)
341
11
        return a;
342
116M
    if (bn_wexpand(a, bn_words) == NULL)
343
0
        return NULL;
344
345
116M
    if (b->top > 0)
346
115M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
347
348
116M
    a->neg = b->neg;
349
116M
    a->top = b->top;
350
116M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
351
116M
    bn_check_top(a);
352
116M
    return a;
353
116M
}
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
487k
{
393
487k
    if (a == NULL)
394
0
        return;
395
487k
    bn_check_top(a);
396
487k
    if (a->d != NULL)
397
44.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
398
487k
    a->neg = 0;
399
487k
    a->top = 0;
400
487k
    a->flags &= ~BN_FLG_FIXED_TOP;
401
487k
}
402
403
BN_ULONG BN_get_word(const BIGNUM *a)
404
432
{
405
432
    if (a->top > 1)
406
12
        return BN_MASK2;
407
420
    else if (a->top == 1)
408
358
        return a->d[0];
409
    /* a->top == 0 */
410
62
    return 0;
411
432
}
412
413
int BN_set_word(BIGNUM *a, BN_ULONG w)
414
3.40M
{
415
3.40M
    bn_check_top(a);
416
3.40M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
417
0
        return 0;
418
3.40M
    a->neg = 0;
419
3.40M
    a->d[0] = w;
420
3.40M
    a->top = (w ? 1 : 0);
421
3.40M
    a->flags &= ~BN_FLG_FIXED_TOP;
422
3.40M
    bn_check_top(a);
423
3.40M
    return 1;
424
3.40M
}
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
11.5M
{
434
11.5M
    int inc;
435
11.5M
    const unsigned char *s2;
436
11.5M
    int inc2;
437
11.5M
    int neg = 0, xor = 0, carry = 0;
438
11.5M
    unsigned int i;
439
11.5M
    unsigned int n;
440
11.5M
    BIGNUM *bn = NULL;
441
442
    /* Negative length is not acceptable */
443
11.5M
    if (len < 0)
444
0
        return NULL;
445
446
11.5M
    if (ret == NULL)
447
6.55M
        ret = bn = BN_new();
448
11.5M
    if (ret == NULL)
449
0
        return NULL;
450
11.5M
    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
11.5M
    if (len == 0) {
457
440k
        BN_clear(ret);
458
440k
        return ret;
459
440k
    }
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
11.1M
    if (endianness == LITTLE) {
467
990k
        s2 = s + len - 1;
468
990k
        inc2 = -1;
469
990k
        inc = 1;
470
10.1M
    } else {
471
10.1M
        s2 = s;
472
10.1M
        inc2 = 1;
473
10.1M
        inc = -1;
474
10.1M
        s += len - 1;
475
10.1M
    }
476
477
    /* Take note of the signedness of the input bytes*/
478
11.1M
    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
25.1M
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
489
13.9M
        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
11.1M
    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
11.1M
    if (len == 0) {
502
225k
        ret->top = 0;
503
225k
        return ret;
504
225k
    }
505
10.9M
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
506
10.9M
    if (bn_wexpand(ret, (int)n) == NULL) {
507
0
        BN_free(bn);
508
0
        return NULL;
509
0
    }
510
10.9M
    ret->top = n;
511
10.9M
    ret->neg = neg;
512
119M
    for (i = 0; n-- > 0; i++) {
513
108M
        BN_ULONG l = 0; /* Accumulator */
514
108M
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
515
516
942M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
517
833M
            BN_ULONG byte_xored = *s ^ xor;
518
833M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
519
520
833M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
521
833M
            l |= (byte << m);
522
833M
        }
523
108M
        ret->d[i] = l;
524
108M
    }
525
    /*
526
     * need to call this due to clear byte at top if avoiding having the top
527
     * bit set (-ve number)
528
     */
529
10.9M
    bn_correct_top(ret);
530
10.9M
    return ret;
531
10.9M
}
532
533
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
534
10.5M
{
535
10.5M
    return bin2bn(s, len, ret, BIG, UNSIGNED);
536
10.5M
}
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.74M
{
546
1.74M
    int inc;
547
1.74M
    int n, n8;
548
1.74M
    int xor = 0, carry = 0, ext = 0;
549
1.74M
    size_t i, lasti, j, atop, mask;
550
1.74M
    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.74M
    n8 = BN_num_bits(a);
558
1.74M
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
559
560
    /* Take note of the signedness of the bignum */
561
1.74M
    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.74M
    if (tolen == -1) {
577
595k
        tolen = n + ext;
578
1.14M
    } else if (tolen < n + ext) { /* uncommon/unlike case */
579
4.70k
        BIGNUM temp = *a;
580
581
4.70k
        bn_correct_top(&temp);
582
4.70k
        n8 = BN_num_bits(&temp);
583
4.70k
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
584
4.70k
        if (tolen < n + ext)
585
4.70k
            return -1;
586
4.70k
    }
587
588
    /* Swipe through whole available data and don't give away padded zero. */
589
1.73M
    atop = a->dmax * BN_BYTES;
590
1.73M
    if (atop == 0) {
591
78.7k
        if (tolen != 0)
592
5.13k
            memset(to, '\0', tolen);
593
78.7k
        return tolen;
594
78.7k
    }
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.65M
    if (endianness == LITTLE) {
602
951k
        inc = 1;
603
951k
    } else {
604
705k
        inc = -1;
605
705k
        to += tolen - 1; /* Move to the last byte, not beyond */
606
705k
    }
607
608
1.65M
    lasti = atop - 1;
609
1.65M
    atop = a->top * BN_BYTES;
610
162M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
611
160M
        unsigned char byte, byte_xored;
612
613
160M
        l = a->d[i / BN_BYTES];
614
160M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
615
160M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
616
160M
        byte_xored = byte ^ xor;
617
160M
        *to = (unsigned char)(byte_xored + carry);
618
160M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
619
160M
        to += inc;
620
160M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
621
160M
    }
622
623
1.65M
    return tolen;
624
1.73M
}
625
626
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
627
229k
{
628
229k
    if (tolen < 0)
629
0
        return -1;
630
229k
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
631
229k
}
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
748k
{
642
748k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
643
748k
}
644
645
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
646
990k
{
647
990k
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
648
990k
}
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
1.04M
{
657
1.04M
    if (tolen < 0)
658
0
        return -1;
659
1.04M
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
660
1.04M
}
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
985k
{
671
985k
    DECLARE_IS_ENDIAN;
672
673
985k
    if (IS_LITTLE_ENDIAN)
674
985k
        return BN_lebin2bn(s, len, ret);
675
0
    return BN_bin2bn(s, len, ret);
676
985k
}
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
985k
{
689
985k
    DECLARE_IS_ENDIAN;
690
691
985k
    if (IS_LITTLE_ENDIAN)
692
985k
        return BN_bn2lebinpad(a, to, tolen);
693
0
    return BN_bn2binpad(a, to, tolen);
694
985k
}
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
106M
{
707
106M
    int i;
708
106M
    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
106M
    if (!ossl_assert(a != NULL && b != NULL))
716
0
        return (b == NULL) - (a == NULL);
717
718
106M
    ap = a->d;
719
106M
    bp = b->d;
720
721
106M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
722
104M
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
723
2.32M
        int res = 0;
724
2.32M
        int min_top = a->top < b->top ? a->top : b->top;
725
726
13.0M
        for (i = 0; i < min_top; i++) {
727
10.7M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
728
10.7M
                -1, res);
729
10.7M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
730
10.7M
                1, res);
731
10.7M
        }
732
733
2.42M
        for (i = min_top; i < a->top; ++i)
734
98.2k
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
735
98.2k
                res, 1);
736
737
2.32M
        for (i = min_top; i < b->top; ++i)
738
2.71k
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
739
2.71k
                res, -1);
740
741
2.32M
        return res;
742
2.32M
    }
743
744
104M
    bn_check_top(a);
745
104M
    bn_check_top(b);
746
747
104M
    i = a->top - b->top;
748
104M
    if (i != 0)
749
12.4M
        return i;
750
751
97.6M
    for (i = a->top - 1; i >= 0; i--) {
752
95.8M
        t1 = ap[i];
753
95.8M
        t2 = bp[i];
754
95.8M
        if (t1 != t2)
755
90.2M
            return ((t1 > t2) ? 1 : -1);
756
95.8M
    }
757
1.83M
    return 0;
758
92.1M
}
759
760
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
761
19.2M
{
762
19.2M
    int i;
763
19.2M
    int gt, lt;
764
19.2M
    BN_ULONG t1, t2;
765
766
19.2M
    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
19.2M
    bn_check_top(a);
776
19.2M
    bn_check_top(b);
777
778
19.2M
    if (a->neg != b->neg) {
779
1.48k
        if (a->neg)
780
646
            return -1;
781
835
        else
782
835
            return 1;
783
1.48k
    }
784
19.2M
    if (a->neg == 0) {
785
19.2M
        gt = 1;
786
19.2M
        lt = -1;
787
19.2M
    } else {
788
9.90k
        gt = -1;
789
9.90k
        lt = 1;
790
9.90k
    }
791
792
19.2M
    if (a->top > b->top)
793
2.18M
        return gt;
794
17.0M
    if (a->top < b->top)
795
1.80M
        return lt;
796
43.1M
    for (i = a->top - 1; i >= 0; i--) {
797
40.7M
        t1 = a->d[i];
798
40.7M
        t2 = b->d[i];
799
40.7M
        if (t1 > t2)
800
5.33M
            return gt;
801
35.4M
        if (t1 < t2)
802
7.51M
            return lt;
803
35.4M
    }
804
2.39M
    return 0;
805
15.2M
}
806
807
int BN_set_bit(BIGNUM *a, int n)
808
2.54M
{
809
2.54M
    int i, j, k;
810
811
2.54M
    if (n < 0)
812
0
        return 0;
813
814
2.54M
    i = n / BN_BITS2;
815
2.54M
    j = n % BN_BITS2;
816
2.54M
    if (a->top <= i) {
817
2.54M
        if (bn_wexpand(a, i + 1) == NULL)
818
0
            return 0;
819
21.3M
        for (k = a->top; k < i + 1; k++)
820
18.7M
            a->d[k] = 0;
821
2.54M
        a->top = i + 1;
822
2.54M
        a->flags &= ~BN_FLG_FIXED_TOP;
823
2.54M
    }
824
825
2.54M
    a->d[i] |= (((BN_ULONG)1) << j);
826
2.54M
    bn_check_top(a);
827
2.54M
    return 1;
828
2.54M
}
829
830
int BN_clear_bit(BIGNUM *a, int n)
831
222
{
832
222
    int i, j;
833
834
222
    bn_check_top(a);
835
222
    if (n < 0)
836
0
        return 0;
837
838
222
    i = n / BN_BITS2;
839
222
    j = n % BN_BITS2;
840
222
    if (a->top <= i)
841
0
        return 0;
842
843
222
    a->d[i] &= (~(((BN_ULONG)1) << j));
844
222
    bn_correct_top(a);
845
222
    return 1;
846
222
}
847
848
int BN_is_bit_set(const BIGNUM *a, int n)
849
200M
{
850
200M
    int i, j;
851
852
200M
    bn_check_top(a);
853
200M
    if (n < 0)
854
3.70k
        return 0;
855
200M
    i = n / BN_BITS2;
856
200M
    j = n % BN_BITS2;
857
200M
    if (a->top <= i)
858
9.16k
        return 0;
859
200M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
860
200M
}
861
862
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
863
6.83k
{
864
6.83k
    int b, w;
865
866
6.83k
    if (n < 0)
867
0
        return 0;
868
869
6.83k
    w = n / BN_BITS2;
870
6.83k
    b = n % BN_BITS2;
871
6.83k
    if (w >= a->top)
872
0
        return 0;
873
6.83k
    if (b == 0)
874
6.31k
        a->top = w;
875
527
    else {
876
527
        a->top = w + 1;
877
527
        a->d[w] &= ~(BN_MASK2 << b);
878
527
    }
879
6.83k
    a->flags |= BN_FLG_FIXED_TOP;
880
6.83k
    return 1;
881
6.83k
}
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
2.47M
{
896
2.47M
    if (b && !BN_is_zero(a))
897
557k
        a->neg = 1;
898
1.91M
    else
899
1.91M
        a->neg = 0;
900
2.47M
}
901
902
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
903
95.0M
{
904
95.0M
    int i;
905
95.0M
    BN_ULONG aa, bb;
906
907
95.0M
    if (n == 0)
908
0
        return 0;
909
910
95.0M
    aa = a[n - 1];
911
95.0M
    bb = b[n - 1];
912
95.0M
    if (aa != bb)
913
83.7M
        return ((aa > bb) ? 1 : -1);
914
97.9M
    for (i = n - 2; i >= 0; i--) {
915
93.3M
        aa = a[i];
916
93.3M
        bb = b[i];
917
93.3M
        if (aa != bb)
918
6.74M
            return ((aa > bb) ? 1 : -1);
919
93.3M
    }
920
4.59M
    return 0;
921
11.3M
}
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
133M
{
934
133M
    int n, i;
935
133M
    n = cl - 1;
936
937
133M
    if (dl < 0) {
938
2.84M
        for (i = dl; i < 0; i++) {
939
2.75M
            if (b[n - i] != 0)
940
1.29M
                return -1; /* a < b */
941
2.75M
        }
942
1.38M
    }
943
132M
    if (dl > 0) {
944
4.24M
        for (i = dl; i > 0; i--) {
945
4.01M
            if (a[n + i] != 0)
946
1.16M
                return 1; /* a > b */
947
4.01M
        }
948
1.39M
    }
949
131M
    return bn_cmp_words(a, b, cl);
950
132M
}
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
114M
{
961
114M
    BN_ULONG t;
962
114M
    int i;
963
964
114M
    bn_wcheck_size(a, nwords);
965
114M
    bn_wcheck_size(b, nwords);
966
967
114M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
968
969
114M
    t = (a->top ^ b->top) & condition;
970
114M
    a->top ^= t;
971
114M
    b->top ^= t;
972
973
114M
    t = (a->neg ^ b->neg) & condition;
974
114M
    a->neg ^= t;
975
114M
    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
114M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
1000
1001
114M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
1002
114M
    a->flags ^= t;
1003
114M
    b->flags ^= t;
1004
1005
    /* conditionally swap the data */
1006
18.2G
    for (i = 0; i < nwords; i++) {
1007
18.1G
        t = (a->d[i] ^ b->d[i]) & condition;
1008
18.1G
        a->d[i] ^= t;
1009
18.1G
        b->d[i] ^= t;
1010
18.1G
    }
1011
114M
}
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
209k
{
1019
209k
    int secbits, bits;
1020
209k
    if (L >= 15360)
1021
973
        secbits = 256;
1022
208k
    else if (L >= 7680)
1023
1.19k
        secbits = 192;
1024
207k
    else if (L >= 3072)
1025
2.64k
        secbits = 128;
1026
205k
    else if (L >= 2048)
1027
4.42k
        secbits = 112;
1028
200k
    else if (L >= 1024)
1029
119k
        secbits = 80;
1030
80.9k
    else
1031
80.9k
        return 0;
1032
129k
    if (N == -1)
1033
11.4k
        return secbits;
1034
117k
    bits = N / 2;
1035
117k
    if (bits < 80)
1036
9.36k
        return 0;
1037
108k
    return bits >= secbits ? secbits : bits;
1038
117k
}
1039
1040
void BN_zero_ex(BIGNUM *a)
1041
1.23G
{
1042
1.23G
    a->neg = 0;
1043
1.23G
    a->top = 0;
1044
1.23G
    a->flags &= ~BN_FLG_FIXED_TOP;
1045
1.23G
}
1046
1047
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1048
116M
{
1049
116M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1050
116M
}
1051
1052
int BN_is_zero(const BIGNUM *a)
1053
427M
{
1054
427M
    return a->top == 0;
1055
427M
}
1056
1057
int BN_is_one(const BIGNUM *a)
1058
115M
{
1059
115M
    return BN_abs_is_word(a, 1) && !a->neg;
1060
115M
}
1061
1062
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1063
314k
{
1064
314k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1065
314k
}
1066
1067
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1068
6.39k
{
1069
6.39k
    int res, i;
1070
6.39k
    const BN_ULONG *ap = a->d;
1071
1072
6.39k
    if (a->neg || a->top == 0)
1073
0
        return 0;
1074
1075
6.39k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
1076
1077
25.4k
    for (i = 1; i < a->top; i++)
1078
19.0k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
1079
19.0k
            res, 0);
1080
6.39k
    return res;
1081
6.39k
}
1082
1083
int BN_is_odd(const BIGNUM *a)
1084
72.8M
{
1085
72.8M
    return (a->top > 0) && (a->d[0] & 1);
1086
72.8M
}
1087
1088
int BN_is_negative(const BIGNUM *a)
1089
8.37M
{
1090
8.37M
    return (a->neg != 0);
1091
8.37M
}
1092
1093
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1094
    BN_CTX *ctx)
1095
2.51M
{
1096
2.51M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1097
2.51M
}
1098
1099
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1100
21.4M
{
1101
21.4M
    dest->d = b->d;
1102
21.4M
    dest->top = b->top;
1103
21.4M
    dest->dmax = b->dmax;
1104
21.4M
    dest->neg = b->neg;
1105
21.4M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1106
21.4M
        | (b->flags & ~BN_FLG_MALLOCED)
1107
21.4M
        | BN_FLG_STATIC_DATA | flags);
1108
21.4M
}
1109
1110
BN_GENCB *BN_GENCB_new(void)
1111
4.63k
{
1112
4.63k
    BN_GENCB *ret;
1113
1114
4.63k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1115
0
        return NULL;
1116
1117
4.63k
    return ret;
1118
4.63k
}
1119
1120
void BN_GENCB_free(BN_GENCB *cb)
1121
6.46k
{
1122
6.46k
    if (cb == NULL)
1123
1.82k
        return;
1124
4.63k
    OPENSSL_free(cb);
1125
4.63k
}
1126
1127
void BN_set_flags(BIGNUM *b, int n)
1128
2.29M
{
1129
2.29M
    b->flags |= n;
1130
2.29M
}
1131
1132
int BN_get_flags(const BIGNUM *b, int n)
1133
772M
{
1134
772M
    return b->flags & n;
1135
772M
}
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
4.63k
{
1151
4.63k
    BN_GENCB *tmp_gencb = gencb;
1152
4.63k
    tmp_gencb->ver = 2;
1153
4.63k
    tmp_gencb->arg = cb_arg;
1154
4.63k
    tmp_gencb->cb.cb_2 = callback;
1155
4.63k
}
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
2.30G
{
1164
2.30G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1165
2.30G
}
1166
1167
void bn_correct_top_consttime(BIGNUM *a)
1168
16.6k
{
1169
16.6k
    int j, atop;
1170
16.6k
    BN_ULONG limb;
1171
16.6k
    unsigned int mask;
1172
1173
1.08M
    for (j = 0, atop = 0; j < a->dmax; j++) {
1174
1.06M
        limb = a->d[j];
1175
1.06M
        limb |= 0 - limb;
1176
1.06M
        limb >>= BN_BITS2 - 1;
1177
1.06M
        limb = 0 - limb;
1178
1.06M
        mask = (unsigned int)limb;
1179
1.06M
        mask &= constant_time_msb(j - a->top);
1180
1.06M
        atop = constant_time_select_int(mask, j + 1, atop);
1181
1.06M
    }
1182
1183
16.6k
    mask = constant_time_eq_int(atop, 0);
1184
16.6k
    a->top = atop;
1185
16.6k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1186
16.6k
    a->flags &= ~BN_FLG_FIXED_TOP;
1187
16.6k
}
1188
1189
void bn_correct_top(BIGNUM *a)
1190
908M
{
1191
908M
    BN_ULONG *ftl;
1192
908M
    int tmp_top = a->top;
1193
1194
908M
    if (tmp_top > 0) {
1195
2.56G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1196
2.56G
            ftl--;
1197
2.56G
            if (*ftl != 0)
1198
905M
                break;
1199
2.56G
        }
1200
908M
        a->top = tmp_top;
1201
908M
    }
1202
908M
    if (a->top == 0)
1203
3.79M
        a->neg = 0;
1204
908M
    a->flags &= ~BN_FLG_FIXED_TOP;
1205
908M
    bn_pollute(a);
1206
908M
}