Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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_bits_low = 0;
32
static int bn_limit_bits_high = 0;
33
static int bn_limit_bits_mont = 0;
34
35
void BN_set_params(int mult, int high, int low, int mont)
36
0
{
37
0
    if (mult >= 0) {
38
0
        if (mult > (int)(sizeof(int) * 8) - 1)
39
0
            mult = sizeof(int) * 8 - 1;
40
0
        bn_limit_bits = mult;
41
0
    }
42
0
    if (high >= 0) {
43
0
        if (high > (int)(sizeof(int) * 8) - 1)
44
0
            high = sizeof(int) * 8 - 1;
45
0
        bn_limit_bits_high = high;
46
0
    }
47
0
    if (low >= 0) {
48
0
        if (low > (int)(sizeof(int) * 8) - 1)
49
0
            low = sizeof(int) * 8 - 1;
50
0
        bn_limit_bits_low = low;
51
0
    }
52
0
    if (mont >= 0) {
53
0
        if (mont > (int)(sizeof(int) * 8) - 1)
54
0
            mont = sizeof(int) * 8 - 1;
55
0
        bn_limit_bits_mont = mont;
56
0
    }
57
0
}
58
59
int BN_get_params(int which)
60
0
{
61
0
    if (which == 0)
62
0
        return bn_limit_bits;
63
0
    else if (which == 1)
64
0
        return bn_limit_bits_high;
65
0
    else if (which == 2)
66
0
        return bn_limit_bits_low;
67
0
    else if (which == 3)
68
0
        return bn_limit_bits_mont;
69
0
    else
70
0
        return 0;
71
0
}
72
#endif
73
74
const BIGNUM *BN_value_one(void)
75
1.81M
{
76
1.81M
    static const BN_ULONG data_one = 1L;
77
1.81M
    static const BIGNUM const_one = {
78
1.81M
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
79
1.81M
    };
80
81
1.81M
    return &const_one;
82
1.81M
}
83
84
#ifndef __e2k__
85
int BN_num_bits_word(BN_ULONG l)
86
237M
{
87
237M
    BN_ULONG x, mask;
88
237M
    int bits = (l != 0);
89
90
237M
#if BN_BITS2 > 32
91
237M
    x = l >> 32;
92
237M
    mask = (0 - x) & BN_MASK2;
93
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
94
237M
    bits += 32 & mask;
95
237M
    l ^= (x ^ l) & mask;
96
237M
#endif
97
98
237M
    x = l >> 16;
99
237M
    mask = (0 - x) & BN_MASK2;
100
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
101
237M
    bits += 16 & mask;
102
237M
    l ^= (x ^ l) & mask;
103
104
237M
    x = l >> 8;
105
237M
    mask = (0 - x) & BN_MASK2;
106
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
107
237M
    bits += 8 & mask;
108
237M
    l ^= (x ^ l) & mask;
109
110
237M
    x = l >> 4;
111
237M
    mask = (0 - x) & BN_MASK2;
112
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
113
237M
    bits += 4 & mask;
114
237M
    l ^= (x ^ l) & mask;
115
116
237M
    x = l >> 2;
117
237M
    mask = (0 - x) & BN_MASK2;
118
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
119
237M
    bits += 2 & mask;
120
237M
    l ^= (x ^ l) & mask;
121
122
237M
    x = l >> 1;
123
237M
    mask = (0 - x) & BN_MASK2;
124
237M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
125
237M
    bits += 1 & mask;
126
127
237M
    return bits;
128
237M
}
129
#else /* __e2k__ */
130
#include <x86gprintrin.h>
131
int BN_num_bits_word(BN_ULONG l)
132
{
133
    /* clz(0) is well-defined on e2k, hence no if (l == 0) return 0;
134
     * is required here.
135
     */
136
#if BN_BITS2 > 32
137
    return 64 - __builtin_clzll(l);
138
#else
139
    return 32 - __builtin_clz(l);
140
#endif
141
}
142
#endif /* __e2k__ */
143
144
/*
145
 * This function still leaks `a->dmax`: it's caller's responsibility to
146
 * expand the input `a` in advance to a public length.
147
 */
148
static ossl_inline int bn_num_bits_consttime(const BIGNUM *a)
149
4.61M
{
150
4.61M
    int j, ret;
151
4.61M
    unsigned int mask, past_i;
152
4.61M
    int i = a->top - 1;
153
4.61M
    bn_check_top(a);
154
155
57.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
156
52.4M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
157
158
52.4M
        ret += BN_BITS2 & (~mask & ~past_i);
159
52.4M
        ret += BN_num_bits_word(a->d[j]) & mask;
160
161
52.4M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
162
52.4M
    }
163
164
    /*
165
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
166
     * final result.
167
     */
168
4.61M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
169
170
4.61M
    return ret & mask;
171
4.61M
}
172
173
int BN_num_bits(const BIGNUM *a)
174
21.4M
{
175
21.4M
    int i = a->top - 1;
176
21.4M
    bn_check_top(a);
177
178
21.4M
    if (a->flags & BN_FLG_CONSTTIME) {
179
        /*
180
         * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
181
         * so that a->dmax is not leaking secret information.
182
         *
183
         * In other words, it's the caller's responsibility to ensure `a` has
184
         * been preallocated in advance to a public length if we hit this
185
         * branch.
186
         *
187
         */
188
2.02M
        return bn_num_bits_consttime(a);
189
2.02M
    }
190
191
19.4M
    if (ossl_unlikely(BN_is_zero(a)))
192
212k
        return 0;
193
194
19.2M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
195
19.4M
}
196
197
static void bn_free_d(BIGNUM *a, int clear)
198
74.5M
{
199
74.5M
    if (BN_get_flags(a, BN_FLG_SECURE))
200
1.26M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
201
73.2M
    else if (clear != 0)
202
43.5M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
203
29.7M
    else
204
29.7M
        OPENSSL_free(a->d);
205
74.5M
}
206
207
void BN_clear_free(BIGNUM *a)
208
35.6M
{
209
35.6M
    if (a == NULL)
210
5.76M
        return;
211
29.8M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
212
27.0M
        bn_free_d(a, 1);
213
29.8M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
214
2.47M
        OPENSSL_cleanse(a, sizeof(*a));
215
2.47M
        OPENSSL_free(a);
216
2.47M
    }
217
29.8M
}
218
219
void BN_free(BIGNUM *a)
220
51.7M
{
221
51.7M
    if (a == NULL)
222
21.9M
        return;
223
29.7M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
224
29.7M
        bn_free_d(a, 0);
225
29.7M
    if (a->flags & BN_FLG_MALLOCED)
226
29.7M
        OPENSSL_free(a);
227
29.7M
}
228
229
void bn_init(BIGNUM *a)
230
70.1M
{
231
70.1M
    static BIGNUM nilbn;
232
233
70.1M
    *a = nilbn;
234
70.1M
    bn_check_top(a);
235
70.1M
}
236
237
BIGNUM *BN_new(void)
238
32.2M
{
239
32.2M
    BIGNUM *ret;
240
241
32.2M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
242
0
        return NULL;
243
32.2M
    ret->flags = BN_FLG_MALLOCED;
244
32.2M
    bn_check_top(ret);
245
32.2M
    return ret;
246
32.2M
}
247
248
BIGNUM *BN_secure_new(void)
249
1.34M
{
250
1.34M
    BIGNUM *ret = BN_new();
251
252
1.34M
    if (ret != NULL)
253
1.34M
        ret->flags |= BN_FLG_SECURE;
254
1.34M
    return ret;
255
1.34M
}
256
257
/* This is used by bn_expand2() */
258
/* The caller MUST check that words > b->dmax before calling this */
259
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
260
46.9M
{
261
46.9M
    BN_ULONG *a = NULL;
262
263
46.9M
    if (ossl_unlikely(words > (INT_MAX / (4 * BN_BITS2)))) {
264
0
        ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG);
265
0
        return NULL;
266
0
    }
267
46.9M
    if (ossl_unlikely(BN_get_flags(b, BN_FLG_STATIC_DATA))) {
268
0
        ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
269
0
        return NULL;
270
0
    }
271
46.9M
    if (BN_get_flags(b, BN_FLG_SECURE))
272
604k
        a = OPENSSL_secure_calloc(words, sizeof(*a));
273
46.3M
    else
274
46.3M
        a = OPENSSL_calloc(words, sizeof(*a));
275
46.9M
    if (ossl_unlikely(a == NULL))
276
0
        return NULL;
277
278
46.9M
    assert(b->top <= words);
279
46.9M
    if (b->top > 0)
280
5.76M
        memcpy(a, b->d, sizeof(*a) * b->top);
281
282
46.9M
    return a;
283
46.9M
}
284
285
/*
286
 * This is an internal function that should not be used in applications. It
287
 * ensures that 'b' has enough room for a 'words' word number and initialises
288
 * any unused part of b->d with leading zeros. It is mostly used by the
289
 * various BIGNUM routines. If there is an error, NULL is returned. If not,
290
 * 'b' is returned.
291
 */
292
293
BIGNUM *bn_expand2(BIGNUM *b, int words)
294
46.9M
{
295
46.9M
    if (ossl_likely(words > b->dmax)) {
296
46.9M
        BN_ULONG *a = bn_expand_internal(b, words);
297
298
46.9M
        if (ossl_unlikely(!a))
299
0
            return NULL;
300
46.9M
        if (b->d != NULL)
301
11.6M
            bn_free_d(b, 1);
302
46.9M
        b->d = a;
303
46.9M
        b->dmax = words;
304
46.9M
    }
305
306
46.9M
    return b;
307
46.9M
}
308
309
BIGNUM *BN_dup(const BIGNUM *a)
310
1.31M
{
311
1.31M
    BIGNUM *t;
312
313
1.31M
    if (a == NULL)
314
0
        return NULL;
315
1.31M
    bn_check_top(a);
316
317
1.31M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
318
1.31M
    if (t == NULL)
319
0
        return NULL;
320
1.31M
    if (BN_copy(t, a) == NULL) {
321
0
        BN_free(t);
322
0
        return NULL;
323
0
    }
324
1.31M
    bn_check_top(t);
325
1.31M
    return t;
326
1.31M
}
327
328
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
329
113M
{
330
113M
    int bn_words;
331
332
113M
    bn_check_top(b);
333
334
113M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
335
336
113M
    if (ossl_unlikely(a == b))
337
9
        return a;
338
113M
    if (ossl_unlikely(bn_wexpand(a, bn_words) == NULL))
339
0
        return NULL;
340
341
113M
    if (ossl_likely(b->top > 0))
342
112M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
343
344
113M
    a->neg = b->neg;
345
113M
    a->top = b->top;
346
113M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
347
113M
    bn_check_top(a);
348
113M
    return a;
349
113M
}
350
351
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA | BN_FLG_CONSTTIME | BN_FLG_SECURE | BN_FLG_FIXED_TOP))
352
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
353
354
void BN_swap(BIGNUM *a, BIGNUM *b)
355
0
{
356
0
    int flags_old_a, flags_old_b;
357
0
    BN_ULONG *tmp_d;
358
0
    int tmp_top, tmp_dmax, tmp_neg;
359
360
0
    bn_check_top(a);
361
0
    bn_check_top(b);
362
363
0
    flags_old_a = a->flags;
364
0
    flags_old_b = b->flags;
365
366
0
    tmp_d = a->d;
367
0
    tmp_top = a->top;
368
0
    tmp_dmax = a->dmax;
369
0
    tmp_neg = a->neg;
370
371
0
    a->d = b->d;
372
0
    a->top = b->top;
373
0
    a->dmax = b->dmax;
374
0
    a->neg = b->neg;
375
376
0
    b->d = tmp_d;
377
0
    b->top = tmp_top;
378
0
    b->dmax = tmp_dmax;
379
0
    b->neg = tmp_neg;
380
381
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
382
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
383
0
    bn_check_top(a);
384
0
    bn_check_top(b);
385
0
}
386
387
void BN_clear(BIGNUM *a)
388
365k
{
389
365k
    if (a == NULL)
390
0
        return;
391
365k
    bn_check_top(a);
392
365k
    if (a->d != NULL)
393
49.6k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
394
365k
    a->neg = 0;
395
365k
    a->top = 0;
396
365k
    a->flags &= ~BN_FLG_FIXED_TOP;
397
365k
}
398
399
BN_ULONG BN_get_word(const BIGNUM *a)
400
314
{
401
314
    if (a->top > 1)
402
11
        return BN_MASK2;
403
303
    else if (a->top == 1)
404
257
        return a->d[0];
405
    /* a->top == 0 */
406
46
    return 0;
407
314
}
408
409
int BN_set_word(BIGNUM *a, BN_ULONG w)
410
3.17M
{
411
3.17M
    bn_check_top(a);
412
3.17M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
413
0
        return 0;
414
3.17M
    a->neg = 0;
415
3.17M
    a->d[0] = w;
416
3.17M
    a->top = (w ? 1 : 0);
417
3.17M
    a->flags &= ~BN_FLG_FIXED_TOP;
418
3.17M
    bn_check_top(a);
419
3.17M
    return 1;
420
3.17M
}
421
422
typedef enum { BIG,
423
    LITTLE } endianness_t;
424
typedef enum { SIGNED,
425
    UNSIGNED } signedness_t;
426
427
static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret,
428
    endianness_t endianness, signedness_t signedness)
429
13.6M
{
430
13.6M
    int inc;
431
13.6M
    const unsigned char *s2;
432
13.6M
    int inc2;
433
13.6M
    int neg = 0, xor = 0, carry = 0;
434
13.6M
    unsigned int i;
435
13.6M
    unsigned int n;
436
13.6M
    BIGNUM *bn = NULL;
437
438
    /* Negative length is not acceptable */
439
13.6M
    if (len < 0)
440
0
        return NULL;
441
442
13.6M
    if (ret == NULL)
443
8.34M
        ret = bn = BN_new();
444
13.6M
    if (ret == NULL)
445
0
        return NULL;
446
13.6M
    bn_check_top(ret);
447
448
    /*
449
     * If the input has no bits, the number is considered zero.
450
     * This makes calls with s==NULL and len==0 safe.
451
     */
452
13.6M
    if (len == 0) {
453
313k
        BN_clear(ret);
454
313k
        return ret;
455
313k
    }
456
457
    /*
458
     * The loop that does the work iterates from least to most
459
     * significant BIGNUM chunk, so we adapt parameters to transfer
460
     * input bytes accordingly.
461
     */
462
13.3M
    if (endianness == LITTLE) {
463
835k
        s2 = s + len - 1;
464
835k
        inc2 = -1;
465
835k
        inc = 1;
466
12.5M
    } else {
467
12.5M
        s2 = s;
468
12.5M
        inc2 = 1;
469
12.5M
        inc = -1;
470
12.5M
        s += len - 1;
471
12.5M
    }
472
473
    /* Take note of the signedness of the input bytes*/
474
13.3M
    if (signedness == SIGNED) {
475
0
        neg = !!(*s2 & 0x80);
476
0
        xor = neg ? 0xff : 0x00;
477
0
        carry = neg;
478
0
    }
479
480
    /*
481
     * Skip leading sign extensions (the value of |xor|).
482
     * This is the only spot where |s2| and |inc2| are used.
483
     */
484
38.3M
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
485
25.0M
        continue;
486
487
    /*
488
     * If there was a set of 0xff, we backtrack one byte unless the next
489
     * one has a sign bit, as the last 0xff is then part of the actual
490
     * number, rather then a mere sign extension.
491
     */
492
13.3M
    if (xor == 0xff) {
493
0
        if (len == 0 || !(*s2 & 0x80))
494
0
            len++;
495
0
    }
496
    /* If it was all zeros, we're done */
497
13.3M
    if (len == 0) {
498
313k
        ret->top = 0;
499
313k
        return ret;
500
313k
    }
501
13.0M
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
502
13.0M
    if (bn_wexpand(ret, (int)n) == NULL) {
503
0
        BN_free(bn);
504
0
        return NULL;
505
0
    }
506
13.0M
    ret->top = n;
507
13.0M
    ret->neg = neg;
508
131M
    for (i = 0; n-- > 0; i++) {
509
118M
        BN_ULONG l = 0; /* Accumulator */
510
118M
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
511
512
1.02G
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
513
902M
            BN_ULONG byte_xored = *s ^ xor;
514
902M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
515
516
902M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
517
902M
            l |= (byte << m);
518
902M
        }
519
118M
        ret->d[i] = l;
520
118M
    }
521
    /*
522
     * need to call this due to clear byte at top if avoiding having the top
523
     * bit set (-ve number)
524
     */
525
13.0M
    bn_correct_top(ret);
526
13.0M
    return ret;
527
13.0M
}
528
529
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
530
12.8M
{
531
12.8M
    return bin2bn(s, len, ret, BIG, UNSIGNED);
532
12.8M
}
533
534
BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
535
0
{
536
0
    return bin2bn(s, len, ret, BIG, SIGNED);
537
0
}
538
539
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen,
540
    endianness_t endianness, signedness_t signedness)
541
1.67M
{
542
1.67M
    int inc;
543
1.67M
    int n, n8;
544
1.67M
    int xor = 0, carry = 0, ext = 0;
545
1.67M
    size_t i, lasti, j, atop, mask;
546
1.67M
    BN_ULONG l;
547
548
    /*
549
     * In case |a| is fixed-top, BN_num_bits can return bogus length,
550
     * but it's assumed that fixed-top inputs ought to be "nominated"
551
     * even for padded output, so it works out...
552
     */
553
1.67M
    n8 = BN_num_bits(a);
554
1.67M
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
555
556
    /* Take note of the signedness of the bignum */
557
1.67M
    if (signedness == SIGNED) {
558
0
        xor = a->neg ? 0xff : 0x00;
559
0
        carry = a->neg;
560
561
        /*
562
         * if |n * 8 == n|, then the MSbit is set, otherwise unset.
563
         * We must compensate with one extra byte if that doesn't
564
         * correspond to the signedness of the bignum with regards
565
         * to 2's complement.
566
         */
567
0
        ext = (n * 8 == n8)
568
0
            ? !a->neg /* MSbit set on nonnegative bignum */
569
0
            : a->neg; /* MSbit unset on negative bignum */
570
0
    }
571
572
1.67M
    if (tolen == -1) {
573
478k
        tolen = n + ext;
574
1.19M
    } else if (tolen < n + ext) { /* uncommon/unlike case */
575
6.81k
        BIGNUM temp = *a;
576
577
6.81k
        bn_correct_top(&temp);
578
6.81k
        n8 = BN_num_bits(&temp);
579
6.81k
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
580
6.81k
        if (tolen < n + ext)
581
6.81k
            return -1;
582
6.81k
    }
583
584
    /* Swipe through whole available data and don't give away padded zero. */
585
1.66M
    atop = a->dmax * BN_BYTES;
586
1.66M
    if (atop == 0) {
587
59.1k
        if (tolen != 0)
588
7.56k
            memset(to, '\0', tolen);
589
59.1k
        return tolen;
590
59.1k
    }
591
592
    /*
593
     * The loop that does the work iterates from least significant
594
     * to most significant BIGNUM limb, so we adapt parameters to
595
     * transfer output bytes accordingly.
596
     */
597
1.60M
    if (endianness == LITTLE) {
598
778k
        inc = 1;
599
830k
    } else {
600
830k
        inc = -1;
601
830k
        to += tolen - 1; /* Move to the last byte, not beyond */
602
830k
    }
603
604
1.60M
    lasti = atop - 1;
605
1.60M
    atop = a->top * BN_BYTES;
606
191M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
607
190M
        unsigned char byte, byte_xored;
608
609
190M
        l = a->d[i / BN_BYTES];
610
190M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
611
190M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
612
190M
        byte_xored = byte ^ xor;
613
190M
        *to = (unsigned char)(byte_xored + carry);
614
190M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
615
190M
        to += inc;
616
190M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
617
190M
    }
618
619
1.60M
    return tolen;
620
1.66M
}
621
622
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
623
414k
{
624
414k
    if (tolen < 0)
625
0
        return -1;
626
414k
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
627
414k
}
628
629
int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen)
630
0
{
631
0
    if (tolen < 0)
632
0
        return -1;
633
0
    return bn2binpad(a, to, tolen, BIG, SIGNED);
634
0
}
635
636
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
637
478k
{
638
478k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
639
478k
}
640
641
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
642
835k
{
643
835k
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
644
835k
}
645
646
BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
647
0
{
648
0
    return bin2bn(s, len, ret, LITTLE, SIGNED);
649
0
}
650
651
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
652
782k
{
653
782k
    if (tolen < 0)
654
0
        return -1;
655
782k
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
656
782k
}
657
658
int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen)
659
0
{
660
0
    if (tolen < 0)
661
0
        return -1;
662
0
    return bn2binpad(a, to, tolen, LITTLE, SIGNED);
663
0
}
664
665
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
666
726k
{
667
726k
    DECLARE_IS_ENDIAN;
668
669
726k
    if (IS_LITTLE_ENDIAN)
670
726k
        return BN_lebin2bn(s, len, ret);
671
0
    return BN_bin2bn(s, len, ret);
672
726k
}
673
674
BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret)
675
0
{
676
0
    DECLARE_IS_ENDIAN;
677
678
0
    if (IS_LITTLE_ENDIAN)
679
0
        return BN_signed_lebin2bn(s, len, ret);
680
0
    return BN_signed_bin2bn(s, len, ret);
681
0
}
682
683
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
684
726k
{
685
726k
    DECLARE_IS_ENDIAN;
686
687
726k
    if (IS_LITTLE_ENDIAN)
688
726k
        return BN_bn2lebinpad(a, to, tolen);
689
0
    return BN_bn2binpad(a, to, tolen);
690
726k
}
691
692
int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen)
693
0
{
694
0
    DECLARE_IS_ENDIAN;
695
696
0
    if (IS_LITTLE_ENDIAN)
697
0
        return BN_signed_bn2lebin(a, to, tolen);
698
0
    return BN_signed_bn2bin(a, to, tolen);
699
0
}
700
701
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
702
114M
{
703
114M
    int i;
704
114M
    BN_ULONG t1, t2, *ap, *bp;
705
706
    /*
707
     * As it is a public API function, we should handle NULL parameters in
708
     * some way. The function can’t return an error, so let’s define that NULL
709
     * is less than any BIGNUM.
710
     */
711
114M
    if (!ossl_assert(a != NULL && b != NULL))
712
0
        return (b == NULL) - (a == NULL);
713
714
114M
    ap = a->d;
715
114M
    bp = b->d;
716
717
114M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
718
113M
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
719
1.66M
        int res = 0;
720
1.66M
        int min_top = a->top < b->top ? a->top : b->top;
721
722
9.35M
        for (i = 0; i < min_top; i++) {
723
7.69M
            res = constant_time_select_int((int)constant_time_lt_bn(ap[i], bp[i]),
724
7.69M
                -1, res);
725
7.69M
            res = constant_time_select_int((int)constant_time_lt_bn(bp[i], ap[i]),
726
7.69M
                1, res);
727
7.69M
        }
728
729
1.79M
        for (i = min_top; i < a->top; ++i)
730
126k
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
731
126k
                res, 1);
732
733
1.66M
        for (i = min_top; i < b->top; ++i)
734
5.59k
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
735
5.59k
                res, -1);
736
737
1.66M
        return res;
738
1.66M
    }
739
740
113M
    bn_check_top(a);
741
113M
    bn_check_top(b);
742
743
113M
    i = a->top - b->top;
744
113M
    if (i != 0)
745
16.1M
        return i;
746
747
103M
    for (i = a->top - 1; i >= 0; i--) {
748
101M
        t1 = ap[i];
749
101M
        t2 = bp[i];
750
101M
        if (t1 != t2)
751
95.1M
            return ((t1 > t2) ? 1 : -1);
752
101M
    }
753
1.98M
    return 0;
754
97.0M
}
755
756
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
757
13.8M
{
758
13.8M
    int i;
759
13.8M
    int gt, lt;
760
13.8M
    BN_ULONG t1, t2;
761
762
13.8M
    if ((a == NULL) || (b == NULL)) {
763
0
        if (a != NULL)
764
0
            return -1;
765
0
        else if (b != NULL)
766
0
            return 1;
767
0
        else
768
0
            return 0;
769
0
    }
770
771
13.8M
    bn_check_top(a);
772
13.8M
    bn_check_top(b);
773
774
13.8M
    if (a->neg != b->neg) {
775
1.18k
        if (a->neg)
776
549
            return -1;
777
632
        else
778
632
            return 1;
779
1.18k
    }
780
13.8M
    if (a->neg == 0) {
781
13.8M
        gt = 1;
782
13.8M
        lt = -1;
783
13.8M
    } else {
784
10.7k
        gt = -1;
785
10.7k
        lt = 1;
786
10.7k
    }
787
788
13.8M
    if (a->top > b->top)
789
1.19M
        return gt;
790
12.6M
    if (a->top < b->top)
791
1.69M
        return lt;
792
39.1M
    for (i = a->top - 1; i >= 0; i--) {
793
36.7M
        t1 = a->d[i];
794
36.7M
        t2 = b->d[i];
795
36.7M
        if (t1 > t2)
796
3.47M
            return gt;
797
33.2M
        if (t1 < t2)
798
5.09M
            return lt;
799
33.2M
    }
800
2.38M
    return 0;
801
10.9M
}
802
803
int BN_set_bit(BIGNUM *a, int n)
804
2.11M
{
805
2.11M
    int i, j, k;
806
807
2.11M
    if (n < 0)
808
0
        return 0;
809
810
2.11M
    i = n / BN_BITS2;
811
2.11M
    j = n % BN_BITS2;
812
2.11M
    if (a->top <= i) {
813
2.11M
        if (bn_wexpand(a, i + 1) == NULL)
814
0
            return 0;
815
18.9M
        for (k = a->top; k < i + 1; k++)
816
16.8M
            a->d[k] = 0;
817
2.11M
        a->top = i + 1;
818
2.11M
        a->flags &= ~BN_FLG_FIXED_TOP;
819
2.11M
    }
820
821
2.11M
    a->d[i] |= (((BN_ULONG)1) << j);
822
2.11M
    bn_check_top(a);
823
2.11M
    return 1;
824
2.11M
}
825
826
int BN_clear_bit(BIGNUM *a, int n)
827
238
{
828
238
    int i, j;
829
830
238
    bn_check_top(a);
831
238
    if (n < 0)
832
0
        return 0;
833
834
238
    i = n / BN_BITS2;
835
238
    j = n % BN_BITS2;
836
238
    if (a->top <= i)
837
0
        return 0;
838
839
238
    a->d[i] &= (~(((BN_ULONG)1) << j));
840
238
    bn_correct_top(a);
841
238
    return 1;
842
238
}
843
844
int BN_is_bit_set(const BIGNUM *a, int n)
845
163M
{
846
163M
    int i, j;
847
848
163M
    bn_check_top(a);
849
163M
    if (ossl_unlikely(n < 0))
850
7.75k
        return 0;
851
163M
    i = n / BN_BITS2;
852
163M
    j = n % BN_BITS2;
853
163M
    if (ossl_unlikely(a->top <= i))
854
8.52k
        return 0;
855
163M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
856
163M
}
857
858
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
859
9.27k
{
860
9.27k
    int b, w;
861
862
9.27k
    if (n < 0)
863
0
        return 0;
864
865
9.27k
    w = n / BN_BITS2;
866
9.27k
    b = n % BN_BITS2;
867
9.27k
    if (w >= a->top)
868
0
        return 0;
869
9.27k
    if (b == 0)
870
6.17k
        a->top = w;
871
3.10k
    else {
872
3.10k
        a->top = w + 1;
873
3.10k
        a->d[w] &= ~(BN_MASK2 << b);
874
3.10k
    }
875
9.27k
    a->flags |= BN_FLG_FIXED_TOP;
876
9.27k
    return 1;
877
9.27k
}
878
879
int BN_mask_bits(BIGNUM *a, int n)
880
0
{
881
0
    int ret;
882
883
0
    bn_check_top(a);
884
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
885
0
    if (ret)
886
0
        bn_correct_top(a);
887
0
    return ret;
888
0
}
889
890
void BN_set_negative(BIGNUM *a, int b)
891
2.08M
{
892
2.08M
    if (b && !BN_is_zero(a))
893
391k
        a->neg = 1;
894
1.68M
    else
895
1.68M
        a->neg = 0;
896
2.08M
}
897
898
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
899
79.6M
{
900
79.6M
    int i;
901
79.6M
    BN_ULONG aa, bb;
902
903
79.6M
    if (ossl_unlikely(n == 0))
904
0
        return 0;
905
906
79.6M
    aa = a[n - 1];
907
79.6M
    bb = b[n - 1];
908
79.6M
    if (ossl_likely(aa != bb))
909
70.7M
        return ((aa > bb) ? 1 : -1);
910
81.9M
    for (i = n - 2; i >= 0; i--) {
911
77.8M
        aa = a[i];
912
77.8M
        bb = b[i];
913
77.8M
        if (aa != bb)
914
4.76M
            return ((aa > bb) ? 1 : -1);
915
77.8M
    }
916
4.05M
    return 0;
917
8.82M
}
918
919
/*
920
 * Here follows a specialised variants of bn_cmp_words().  It has the
921
 * capability of performing the operation on arrays of different sizes. The
922
 * sizes of those arrays is expressed through cl, which is the common length
923
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
924
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
925
 * BN_ULONGs...
926
 */
927
928
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
929
134M
{
930
134M
    int n, i;
931
134M
    n = cl - 1;
932
933
134M
    if (dl < 0) {
934
4.30M
        for (i = dl; i < 0; i++) {
935
4.18M
            if (b[n - i] != 0)
936
2.08M
                return -1; /* a < b */
937
4.18M
        }
938
2.20M
    }
939
132M
    if (dl > 0) {
940
5.34M
        for (i = dl; i > 0; i--) {
941
5.01M
            if (a[n + i] != 0)
942
1.87M
                return 1; /* a > b */
943
5.01M
        }
944
2.20M
    }
945
130M
    return bn_cmp_words(a, b, cl);
946
132M
}
947
948
/*-
949
 * Constant-time conditional swap of a and b.
950
 * a and b are swapped if condition is not 0.
951
 * nwords is the number of words to swap.
952
 * Assumes that at least nwords are allocated in both a and b.
953
 * Assumes that no more than nwords are used by either a or b.
954
 */
955
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
956
128M
{
957
128M
    BN_ULONG t;
958
128M
    int i;
959
960
128M
    bn_wcheck_size(a, nwords);
961
128M
    bn_wcheck_size(b, nwords);
962
963
128M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
964
965
128M
    t = (a->top ^ b->top) & value_barrier_bn(condition);
966
128M
    a->top ^= t;
967
128M
    b->top ^= t;
968
969
128M
    t = (a->neg ^ b->neg) & value_barrier_bn(condition);
970
128M
    a->neg ^= t;
971
128M
    b->neg ^= t;
972
973
    /*-
974
     * BN_FLG_STATIC_DATA: indicates that d points to a buffer that this
975
     * BIGNUM does not own, so it must never be reallocated or freed through
976
     * the BIGNUM. The flag by itself does not forbid writing to the words,
977
     * but much of the data marked this way is compiled-in and does reside in
978
     * a read-only segment. Since BN_consttime_swap writes to d, observing
979
     * BN_FLG_STATIC_DATA here should be treated as a fatal condition: it
980
     * would either cause SEGV or effectively cause data corruption. The flag
981
     * is therefore never swapped, as it describes the storage of each d
982
     * buffer, which is not exchanged.
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
128M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
999
1000
128M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & value_barrier_bn(condition);
1001
128M
    a->flags ^= t;
1002
128M
    b->flags ^= t;
1003
1004
    /* conditionally swap the data */
1005
17.6G
    for (i = 0; i < nwords; i++) {
1006
17.4G
        t = (a->d[i] ^ b->d[i]) & value_barrier_bn(condition);
1007
17.4G
        a->d[i] ^= t;
1008
17.4G
        b->d[i] ^= t;
1009
17.4G
    }
1010
128M
}
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
222k
{
1018
222k
    int secbits, bits;
1019
222k
    if (L >= 15360)
1020
954
        secbits = 256;
1021
221k
    else if (L >= 7680)
1022
570
        secbits = 192;
1023
221k
    else if (L >= 3072)
1024
3.45k
        secbits = 128;
1025
217k
    else if (L >= 2048)
1026
7.85k
        secbits = 112;
1027
209k
    else if (L >= 1024)
1028
125k
        secbits = 80;
1029
84.3k
    else
1030
84.3k
        return 0;
1031
138k
    if (N == -1)
1032
12.2k
        return secbits;
1033
125k
    bits = N / 2;
1034
125k
    if (bits < 80)
1035
10.4k
        return 0;
1036
115k
    return bits >= secbits ? secbits : bits;
1037
125k
}
1038
1039
void BN_zero_ex(BIGNUM *a)
1040
1.32G
{
1041
1.32G
    a->neg = 0;
1042
1.32G
    a->top = 0;
1043
1.32G
    a->flags &= ~BN_FLG_FIXED_TOP;
1044
1.32G
}
1045
1046
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1047
103M
{
1048
103M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1049
103M
}
1050
1051
int BN_is_zero(const BIGNUM *a)
1052
392M
{
1053
392M
    return a->top == 0;
1054
392M
}
1055
1056
int BN_is_one(const BIGNUM *a)
1057
102M
{
1058
102M
    return BN_abs_is_word(a, 1) && !a->neg;
1059
102M
}
1060
1061
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1062
384k
{
1063
384k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1064
384k
}
1065
1066
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1067
7.09k
{
1068
7.09k
    int res, i;
1069
7.09k
    const BN_ULONG *ap = a->d;
1070
1071
7.09k
    if (a->neg || a->top == 0)
1072
0
        return 0;
1073
1074
7.09k
    res = constant_time_select_int((int)constant_time_eq_bn(ap[0], w), 1, 0);
1075
1076
27.4k
    for (i = 1; i < a->top; i++)
1077
20.3k
        res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
1078
20.3k
            res, 0);
1079
7.09k
    return res;
1080
7.09k
}
1081
1082
int BN_is_odd(const BIGNUM *a)
1083
60.3M
{
1084
60.3M
    return (a->top > 0) && (a->d[0] & 1);
1085
60.3M
}
1086
1087
int BN_is_negative(const BIGNUM *a)
1088
8.65M
{
1089
8.65M
    return (a->neg != 0);
1090
8.65M
}
1091
1092
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1093
    BN_CTX *ctx)
1094
2.28M
{
1095
2.28M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1096
2.28M
}
1097
1098
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1099
21.5M
{
1100
21.5M
    dest->d = b->d;
1101
21.5M
    dest->top = b->top;
1102
21.5M
    dest->dmax = b->dmax;
1103
21.5M
    dest->neg = b->neg;
1104
21.5M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1105
21.5M
        | (b->flags & ~BN_FLG_MALLOCED)
1106
21.5M
        | BN_FLG_STATIC_DATA | flags);
1107
21.5M
}
1108
1109
BN_GENCB *BN_GENCB_new(void)
1110
4.48k
{
1111
4.48k
    BN_GENCB *ret;
1112
1113
4.48k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1114
0
        return NULL;
1115
1116
4.48k
    return ret;
1117
4.48k
}
1118
1119
void BN_GENCB_free(BN_GENCB *cb)
1120
7.34k
{
1121
7.34k
    if (cb == NULL)
1122
2.86k
        return;
1123
4.48k
    OPENSSL_free(cb);
1124
4.48k
}
1125
1126
void BN_set_flags(BIGNUM *b, int n)
1127
1.71M
{
1128
1.71M
    b->flags |= n;
1129
1.71M
}
1130
1131
int BN_get_flags(const BIGNUM *b, int n)
1132
719M
{
1133
719M
    return b->flags & n;
1134
719M
}
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.48k
{
1150
4.48k
    BN_GENCB *tmp_gencb = gencb;
1151
4.48k
    tmp_gencb->ver = 2;
1152
4.48k
    tmp_gencb->arg = cb_arg;
1153
4.48k
    tmp_gencb->cb.cb_2 = callback;
1154
4.48k
}
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.61G
{
1163
2.61G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1164
2.61G
}
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
1.24G
{
1190
1.24G
    BN_ULONG *ftl;
1191
1.24G
    int tmp_top = a->top;
1192
1193
1.24G
    if (ossl_likely(tmp_top > 0)) {
1194
3.91G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1195
3.91G
            ftl--;
1196
3.91G
            if (*ftl != 0)
1197
1.23G
                break;
1198
3.91G
        }
1199
1.24G
        a->top = tmp_top;
1200
1.24G
    }
1201
1.24G
    if (a->top == 0)
1202
3.10M
        a->neg = 0;
1203
1.24G
    a->flags &= ~BN_FLG_FIXED_TOP;
1204
1.24G
    bn_pollute(a);
1205
1.24G
}