Coverage Report

Created: 2026-09-12 06:55

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