Coverage Report

Created: 2026-10-16 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
{
76
0
    static const BN_ULONG data_one = 1L;
77
0
    static const BIGNUM const_one = {
78
0
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
79
0
    };
80
81
0
    return &const_one;
82
0
}
83
84
#ifndef __e2k__
85
int BN_num_bits_word(BN_ULONG l)
86
119k
{
87
119k
    BN_ULONG x, mask;
88
119k
    int bits = (l != 0);
89
90
119k
#if BN_BITS2 > 32
91
119k
    x = l >> 32;
92
119k
    mask = (0 - x) & BN_MASK2;
93
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
94
119k
    bits += 32 & mask;
95
119k
    l ^= (x ^ l) & mask;
96
119k
#endif
97
98
119k
    x = l >> 16;
99
119k
    mask = (0 - x) & BN_MASK2;
100
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
101
119k
    bits += 16 & mask;
102
119k
    l ^= (x ^ l) & mask;
103
104
119k
    x = l >> 8;
105
119k
    mask = (0 - x) & BN_MASK2;
106
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
107
119k
    bits += 8 & mask;
108
119k
    l ^= (x ^ l) & mask;
109
110
119k
    x = l >> 4;
111
119k
    mask = (0 - x) & BN_MASK2;
112
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
113
119k
    bits += 4 & mask;
114
119k
    l ^= (x ^ l) & mask;
115
116
119k
    x = l >> 2;
117
119k
    mask = (0 - x) & BN_MASK2;
118
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
119
119k
    bits += 2 & mask;
120
119k
    l ^= (x ^ l) & mask;
121
122
119k
    x = l >> 1;
123
119k
    mask = (0 - x) & BN_MASK2;
124
119k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
125
119k
    bits += 1 & mask;
126
127
119k
    return bits;
128
119k
}
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
0
{
150
0
    int j, ret;
151
0
    unsigned int mask, past_i;
152
0
    int i = a->top - 1;
153
0
    bn_check_top(a);
154
155
0
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
156
0
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
157
158
0
        ret += BN_BITS2 & (~mask & ~past_i);
159
0
        ret += BN_num_bits_word(a->d[j]) & mask;
160
161
0
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
162
0
    }
163
164
    /*
165
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
166
     * final result.
167
     */
168
0
    mask = ~(constant_time_eq_int(i, ((int)-1)));
169
170
0
    return ret & mask;
171
0
}
172
173
int BN_num_bits(const BIGNUM *a)
174
121k
{
175
121k
    int i = a->top - 1;
176
121k
    bn_check_top(a);
177
178
121k
    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
0
        return bn_num_bits_consttime(a);
189
0
    }
190
191
121k
    if (ossl_unlikely(BN_is_zero(a)))
192
2.55k
        return 0;
193
194
119k
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
195
121k
}
196
197
static void bn_free_d(BIGNUM *a, int clear)
198
90.5k
{
199
90.5k
    if (BN_get_flags(a, BN_FLG_SECURE))
200
0
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
201
90.5k
    else if (clear != 0)
202
0
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
203
90.5k
    else
204
90.5k
        OPENSSL_free(a->d);
205
90.5k
}
206
207
void BN_clear_free(BIGNUM *a)
208
322k
{
209
322k
    if (a == NULL)
210
322k
        return;
211
0
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
212
0
        bn_free_d(a, 1);
213
0
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
214
0
        OPENSSL_cleanse(a, sizeof(*a));
215
0
        OPENSSL_free(a);
216
0
    }
217
0
}
218
219
void BN_free(BIGNUM *a)
220
110k
{
221
110k
    if (a == NULL)
222
20.1k
        return;
223
90.5k
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
224
90.5k
        bn_free_d(a, 0);
225
90.5k
    if (a->flags & BN_FLG_MALLOCED)
226
90.5k
        OPENSSL_free(a);
227
90.5k
}
228
229
void bn_init(BIGNUM *a)
230
0
{
231
0
    static BIGNUM nilbn;
232
233
0
    *a = nilbn;
234
0
    bn_check_top(a);
235
0
}
236
237
BIGNUM *BN_new(void)
238
90.5k
{
239
90.5k
    BIGNUM *ret;
240
241
90.5k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
242
0
        return NULL;
243
90.5k
    ret->flags = BN_FLG_MALLOCED;
244
90.5k
    bn_check_top(ret);
245
90.5k
    return ret;
246
90.5k
}
247
248
BIGNUM *BN_secure_new(void)
249
0
{
250
0
    BIGNUM *ret = BN_new();
251
252
0
    if (ret != NULL)
253
0
        ret->flags |= BN_FLG_SECURE;
254
0
    return ret;
255
0
}
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
52.5k
{
261
52.5k
    BN_ULONG *a = NULL;
262
263
52.5k
    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
52.5k
    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
52.5k
    if (BN_get_flags(b, BN_FLG_SECURE))
272
0
        a = OPENSSL_secure_calloc(words, sizeof(*a));
273
52.5k
    else
274
52.5k
        a = OPENSSL_calloc(words, sizeof(*a));
275
52.5k
    if (ossl_unlikely(a == NULL))
276
0
        return NULL;
277
278
52.5k
    assert(b->top <= words);
279
52.5k
    if (b->top > 0)
280
0
        memcpy(a, b->d, sizeof(*a) * b->top);
281
282
52.5k
    return a;
283
52.5k
}
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
52.5k
{
295
52.5k
    if (ossl_likely(words > b->dmax)) {
296
52.5k
        BN_ULONG *a = bn_expand_internal(b, words);
297
298
52.5k
        if (ossl_unlikely(!a))
299
0
            return NULL;
300
52.5k
        if (b->d != NULL)
301
0
            bn_free_d(b, 1);
302
52.5k
        b->d = a;
303
52.5k
        b->dmax = words;
304
52.5k
    }
305
306
52.5k
    return b;
307
52.5k
}
308
309
BIGNUM *BN_dup(const BIGNUM *a)
310
0
{
311
0
    BIGNUM *t;
312
313
0
    if (a == NULL)
314
0
        return NULL;
315
0
    bn_check_top(a);
316
317
0
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
318
0
    if (t == NULL)
319
0
        return NULL;
320
0
    if (BN_copy(t, a) == NULL) {
321
0
        BN_free(t);
322
0
        return NULL;
323
0
    }
324
0
    bn_check_top(t);
325
0
    return t;
326
0
}
327
328
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
329
0
{
330
0
    int bn_words;
331
332
0
    bn_check_top(b);
333
334
0
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
335
336
0
    if (ossl_unlikely(a == b))
337
0
        return a;
338
0
    if (ossl_unlikely(bn_wexpand(a, bn_words) == NULL))
339
0
        return NULL;
340
341
0
    if (ossl_likely(b->top > 0))
342
0
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
343
344
0
    a->neg = b->neg;
345
0
    a->top = b->top;
346
0
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
347
0
    bn_check_top(a);
348
0
    return a;
349
0
}
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
0
{
389
0
    if (a == NULL)
390
0
        return;
391
0
    bn_check_top(a);
392
0
    if (a->d != NULL)
393
0
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
394
0
    a->neg = 0;
395
0
    a->top = 0;
396
0
    a->flags &= ~BN_FLG_FIXED_TOP;
397
0
}
398
399
BN_ULONG BN_get_word(const BIGNUM *a)
400
0
{
401
0
    if (a->top > 1)
402
0
        return BN_MASK2;
403
0
    else if (a->top == 1)
404
0
        return a->d[0];
405
    /* a->top == 0 */
406
0
    return 0;
407
0
}
408
409
int BN_set_word(BIGNUM *a, BN_ULONG w)
410
16
{
411
16
    bn_check_top(a);
412
16
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
413
0
        return 0;
414
16
    a->neg = 0;
415
16
    a->d[0] = w;
416
16
    a->top = (w ? 1 : 0);
417
16
    a->flags &= ~BN_FLG_FIXED_TOP;
418
16
    bn_check_top(a);
419
16
    return 1;
420
16
}
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
90.5k
{
430
90.5k
    int inc;
431
90.5k
    const unsigned char *s2;
432
90.5k
    int inc2;
433
90.5k
    int neg = 0, xor = 0, carry = 0;
434
90.5k
    unsigned int i;
435
90.5k
    unsigned int n;
436
90.5k
    BIGNUM *bn = NULL;
437
438
    /* Negative length is not acceptable */
439
90.5k
    if (len < 0)
440
0
        return NULL;
441
442
90.5k
    if (ret == NULL)
443
0
        ret = bn = BN_new();
444
90.5k
    if (ret == NULL)
445
0
        return NULL;
446
90.5k
    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
90.5k
    if (len == 0) {
453
0
        BN_clear(ret);
454
0
        return ret;
455
0
    }
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
90.5k
    if (endianness == LITTLE) {
463
0
        s2 = s + len - 1;
464
0
        inc2 = -1;
465
0
        inc = 1;
466
90.5k
    } else {
467
90.5k
        s2 = s;
468
90.5k
        inc2 = 1;
469
90.5k
        inc = -1;
470
90.5k
        s += len - 1;
471
90.5k
    }
472
473
    /* Take note of the signedness of the input bytes*/
474
90.5k
    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
170k
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
485
79.5k
        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
90.5k
    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
90.5k
    if (len == 0) {
498
37.9k
        ret->top = 0;
499
37.9k
        return ret;
500
37.9k
    }
501
52.5k
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
502
52.5k
    if (bn_wexpand(ret, (int)n) == NULL) {
503
0
        BN_free(bn);
504
0
        return NULL;
505
0
    }
506
52.5k
    ret->top = n;
507
52.5k
    ret->neg = neg;
508
1.03M
    for (i = 0; n-- > 0; i++) {
509
985k
        BN_ULONG l = 0; /* Accumulator */
510
985k
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
511
512
8.63M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
513
7.64M
            BN_ULONG byte_xored = *s ^ xor;
514
7.64M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
515
516
7.64M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
517
7.64M
            l |= (byte << m);
518
7.64M
        }
519
985k
        ret->d[i] = l;
520
985k
    }
521
    /*
522
     * need to call this due to clear byte at top if avoiding having the top
523
     * bit set (-ve number)
524
     */
525
52.5k
    bn_correct_top(ret);
526
52.5k
    return ret;
527
52.5k
}
528
529
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
530
90.5k
{
531
90.5k
    return bin2bn(s, len, ret, BIG, UNSIGNED);
532
90.5k
}
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
11.3k
{
542
11.3k
    int inc;
543
11.3k
    int n, n8;
544
11.3k
    int xor = 0, carry = 0, ext = 0;
545
11.3k
    size_t i, lasti, j, atop, mask;
546
11.3k
    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
11.3k
    n8 = BN_num_bits(a);
554
11.3k
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
555
556
    /* Take note of the signedness of the bignum */
557
11.3k
    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
11.3k
    if (tolen == -1) {
573
11.3k
        tolen = n + ext;
574
11.3k
    } else if (tolen < n + ext) { /* uncommon/unlike case */
575
0
        BIGNUM temp = *a;
576
577
0
        bn_correct_top(&temp);
578
0
        n8 = BN_num_bits(&temp);
579
0
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
580
0
        if (tolen < n + ext)
581
0
            return -1;
582
0
    }
583
584
    /* Swipe through whole available data and don't give away padded zero. */
585
11.3k
    atop = a->dmax * BN_BYTES;
586
11.3k
    if (atop == 0) {
587
238
        if (tolen != 0)
588
0
            memset(to, '\0', tolen);
589
238
        return tolen;
590
238
    }
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
11.0k
    if (endianness == LITTLE) {
598
0
        inc = 1;
599
11.0k
    } else {
600
11.0k
        inc = -1;
601
11.0k
        to += tolen - 1; /* Move to the last byte, not beyond */
602
11.0k
    }
603
604
11.0k
    lasti = atop - 1;
605
11.0k
    atop = a->top * BN_BYTES;
606
1.05M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
607
1.04M
        unsigned char byte, byte_xored;
608
609
1.04M
        l = a->d[i / BN_BYTES];
610
1.04M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
611
1.04M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
612
1.04M
        byte_xored = byte ^ xor;
613
1.04M
        *to = (unsigned char)(byte_xored + carry);
614
1.04M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
615
1.04M
        to += inc;
616
1.04M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
617
1.04M
    }
618
619
11.0k
    return tolen;
620
11.3k
}
621
622
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
623
0
{
624
0
    if (tolen < 0)
625
0
        return -1;
626
0
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
627
0
}
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
11.3k
{
638
11.3k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
639
11.3k
}
640
641
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
642
0
{
643
0
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
644
0
}
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
0
{
653
0
    if (tolen < 0)
654
0
        return -1;
655
0
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
656
0
}
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
0
{
667
0
    DECLARE_IS_ENDIAN;
668
669
0
    if (IS_LITTLE_ENDIAN)
670
0
        return BN_lebin2bn(s, len, ret);
671
0
    return BN_bin2bn(s, len, ret);
672
0
}
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
0
{
685
0
    DECLARE_IS_ENDIAN;
686
687
0
    if (IS_LITTLE_ENDIAN)
688
0
        return BN_bn2lebinpad(a, to, tolen);
689
0
    return BN_bn2binpad(a, to, tolen);
690
0
}
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
0
{
703
0
    int i;
704
0
    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
0
    if (!ossl_assert(a != NULL && b != NULL))
712
0
        return (b == NULL) - (a == NULL);
713
714
0
    ap = a->d;
715
0
    bp = b->d;
716
717
0
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
718
0
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
719
0
        int res = 0;
720
0
        int min_top = a->top < b->top ? a->top : b->top;
721
722
0
        for (i = 0; i < min_top; i++) {
723
0
            res = constant_time_select_int((int)constant_time_lt_bn(ap[i], bp[i]),
724
0
                -1, res);
725
0
            res = constant_time_select_int((int)constant_time_lt_bn(bp[i], ap[i]),
726
0
                1, res);
727
0
        }
728
729
0
        for (i = min_top; i < a->top; ++i)
730
0
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
731
0
                res, 1);
732
733
0
        for (i = min_top; i < b->top; ++i)
734
0
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
735
0
                res, -1);
736
737
0
        return res;
738
0
    }
739
740
0
    bn_check_top(a);
741
0
    bn_check_top(b);
742
743
0
    i = a->top - b->top;
744
0
    if (i != 0)
745
0
        return i;
746
747
0
    for (i = a->top - 1; i >= 0; i--) {
748
0
        t1 = ap[i];
749
0
        t2 = bp[i];
750
0
        if (t1 != t2)
751
0
            return ((t1 > t2) ? 1 : -1);
752
0
    }
753
0
    return 0;
754
0
}
755
756
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
757
0
{
758
0
    int i;
759
0
    int gt, lt;
760
0
    BN_ULONG t1, t2;
761
762
0
    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
0
    bn_check_top(a);
772
0
    bn_check_top(b);
773
774
0
    if (a->neg != b->neg) {
775
0
        if (a->neg)
776
0
            return -1;
777
0
        else
778
0
            return 1;
779
0
    }
780
0
    if (a->neg == 0) {
781
0
        gt = 1;
782
0
        lt = -1;
783
0
    } else {
784
0
        gt = -1;
785
0
        lt = 1;
786
0
    }
787
788
0
    if (a->top > b->top)
789
0
        return gt;
790
0
    if (a->top < b->top)
791
0
        return lt;
792
0
    for (i = a->top - 1; i >= 0; i--) {
793
0
        t1 = a->d[i];
794
0
        t2 = b->d[i];
795
0
        if (t1 > t2)
796
0
            return gt;
797
0
        if (t1 < t2)
798
0
            return lt;
799
0
    }
800
0
    return 0;
801
0
}
802
803
int BN_set_bit(BIGNUM *a, int n)
804
0
{
805
0
    int i, j, k;
806
807
0
    if (n < 0)
808
0
        return 0;
809
810
0
    i = n / BN_BITS2;
811
0
    j = n % BN_BITS2;
812
0
    if (a->top <= i) {
813
0
        if (bn_wexpand(a, i + 1) == NULL)
814
0
            return 0;
815
0
        for (k = a->top; k < i + 1; k++)
816
0
            a->d[k] = 0;
817
0
        a->top = i + 1;
818
0
        a->flags &= ~BN_FLG_FIXED_TOP;
819
0
    }
820
821
0
    a->d[i] |= (((BN_ULONG)1) << j);
822
0
    bn_check_top(a);
823
0
    return 1;
824
0
}
825
826
int BN_clear_bit(BIGNUM *a, int n)
827
0
{
828
0
    int i, j;
829
830
0
    bn_check_top(a);
831
0
    if (n < 0)
832
0
        return 0;
833
834
0
    i = n / BN_BITS2;
835
0
    j = n % BN_BITS2;
836
0
    if (a->top <= i)
837
0
        return 0;
838
839
0
    a->d[i] &= (~(((BN_ULONG)1) << j));
840
0
    bn_correct_top(a);
841
0
    return 1;
842
0
}
843
844
int BN_is_bit_set(const BIGNUM *a, int n)
845
0
{
846
0
    int i, j;
847
848
0
    bn_check_top(a);
849
0
    if (ossl_unlikely(n < 0))
850
0
        return 0;
851
0
    i = n / BN_BITS2;
852
0
    j = n % BN_BITS2;
853
0
    if (ossl_unlikely(a->top <= i))
854
0
        return 0;
855
0
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
856
0
}
857
858
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
859
0
{
860
0
    int b, w;
861
862
0
    if (n < 0)
863
0
        return 0;
864
865
0
    w = n / BN_BITS2;
866
0
    b = n % BN_BITS2;
867
0
    if (w >= a->top)
868
0
        return 0;
869
0
    if (b == 0)
870
0
        a->top = w;
871
0
    else {
872
0
        a->top = w + 1;
873
0
        a->d[w] &= ~(BN_MASK2 << b);
874
0
    }
875
0
    a->flags |= BN_FLG_FIXED_TOP;
876
0
    return 1;
877
0
}
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
0
{
892
0
    if (b && !BN_is_zero(a))
893
0
        a->neg = 1;
894
0
    else
895
0
        a->neg = 0;
896
0
}
897
898
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
899
0
{
900
0
    int i;
901
0
    BN_ULONG aa, bb;
902
903
0
    if (ossl_unlikely(n == 0))
904
0
        return 0;
905
906
0
    aa = a[n - 1];
907
0
    bb = b[n - 1];
908
0
    if (ossl_likely(aa != bb))
909
0
        return ((aa > bb) ? 1 : -1);
910
0
    for (i = n - 2; i >= 0; i--) {
911
0
        aa = a[i];
912
0
        bb = b[i];
913
0
        if (aa != bb)
914
0
            return ((aa > bb) ? 1 : -1);
915
0
    }
916
0
    return 0;
917
0
}
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
0
{
930
0
    int n, i;
931
0
    n = cl - 1;
932
933
0
    if (dl < 0) {
934
0
        for (i = dl; i < 0; i++) {
935
0
            if (b[n - i] != 0)
936
0
                return -1; /* a < b */
937
0
        }
938
0
    }
939
0
    if (dl > 0) {
940
0
        for (i = dl; i > 0; i--) {
941
0
            if (a[n + i] != 0)
942
0
                return 1; /* a > b */
943
0
        }
944
0
    }
945
0
    return bn_cmp_words(a, b, cl);
946
0
}
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
0
{
957
0
    BN_ULONG t;
958
0
    int i;
959
960
0
    bn_wcheck_size(a, nwords);
961
0
    bn_wcheck_size(b, nwords);
962
963
0
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
964
965
0
    t = (a->top ^ b->top) & value_barrier_bn(condition);
966
0
    a->top ^= t;
967
0
    b->top ^= t;
968
969
0
    t = (a->neg ^ b->neg) & value_barrier_bn(condition);
970
0
    a->neg ^= t;
971
0
    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
0
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
999
1000
0
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & value_barrier_bn(condition);
1001
0
    a->flags ^= t;
1002
0
    b->flags ^= t;
1003
1004
    /* conditionally swap the data */
1005
0
    for (i = 0; i < nwords; i++) {
1006
0
        t = (a->d[i] ^ b->d[i]) & value_barrier_bn(condition);
1007
0
        a->d[i] ^= t;
1008
0
        b->d[i] ^= t;
1009
0
    }
1010
0
}
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
0
{
1018
0
    int secbits, bits;
1019
0
    if (L >= 15360)
1020
0
        secbits = 256;
1021
0
    else if (L >= 7680)
1022
0
        secbits = 192;
1023
0
    else if (L >= 3072)
1024
0
        secbits = 128;
1025
0
    else if (L >= 2048)
1026
0
        secbits = 112;
1027
0
    else if (L >= 1024)
1028
0
        secbits = 80;
1029
0
    else
1030
0
        return 0;
1031
0
    if (N == -1)
1032
0
        return secbits;
1033
0
    bits = N / 2;
1034
0
    if (bits < 80)
1035
0
        return 0;
1036
0
    return bits >= secbits ? secbits : bits;
1037
0
}
1038
1039
void BN_zero_ex(BIGNUM *a)
1040
32
{
1041
32
    a->neg = 0;
1042
32
    a->top = 0;
1043
32
    a->flags &= ~BN_FLG_FIXED_TOP;
1044
32
}
1045
1046
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1047
750
{
1048
750
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1049
750
}
1050
1051
int BN_is_zero(const BIGNUM *a)
1052
121k
{
1053
121k
    return a->top == 0;
1054
121k
}
1055
1056
int BN_is_one(const BIGNUM *a)
1057
0
{
1058
0
    return BN_abs_is_word(a, 1) && !a->neg;
1059
0
}
1060
1061
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1062
750
{
1063
750
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1064
750
}
1065
1066
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1067
0
{
1068
0
    int res, i;
1069
0
    const BN_ULONG *ap = a->d;
1070
1071
0
    if (a->neg || a->top == 0)
1072
0
        return 0;
1073
1074
0
    res = constant_time_select_int((int)constant_time_eq_bn(ap[0], w), 1, 0);
1075
1076
0
    for (i = 1; i < a->top; i++)
1077
0
        res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
1078
0
            res, 0);
1079
0
    return res;
1080
0
}
1081
1082
int BN_is_odd(const BIGNUM *a)
1083
0
{
1084
0
    return (a->top > 0) && (a->d[0] & 1);
1085
0
}
1086
1087
int BN_is_negative(const BIGNUM *a)
1088
0
{
1089
0
    return (a->neg != 0);
1090
0
}
1091
1092
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1093
    BN_CTX *ctx)
1094
0
{
1095
0
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1096
0
}
1097
1098
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1099
0
{
1100
0
    dest->d = b->d;
1101
0
    dest->top = b->top;
1102
0
    dest->dmax = b->dmax;
1103
0
    dest->neg = b->neg;
1104
0
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1105
0
        | (b->flags & ~BN_FLG_MALLOCED)
1106
0
        | BN_FLG_STATIC_DATA | flags);
1107
0
}
1108
1109
BN_GENCB *BN_GENCB_new(void)
1110
0
{
1111
0
    BN_GENCB *ret;
1112
1113
0
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1114
0
        return NULL;
1115
1116
0
    return ret;
1117
0
}
1118
1119
void BN_GENCB_free(BN_GENCB *cb)
1120
0
{
1121
0
    if (cb == NULL)
1122
0
        return;
1123
0
    OPENSSL_free(cb);
1124
0
}
1125
1126
void BN_set_flags(BIGNUM *b, int n)
1127
0
{
1128
0
    b->flags |= n;
1129
0
}
1130
1131
int BN_get_flags(const BIGNUM *b, int n)
1132
286k
{
1133
286k
    return b->flags & n;
1134
286k
}
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
0
{
1150
0
    BN_GENCB *tmp_gencb = gencb;
1151
0
    tmp_gencb->ver = 2;
1152
0
    tmp_gencb->arg = cb_arg;
1153
0
    tmp_gencb->cb.cb_2 = callback;
1154
0
}
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
52.5k
{
1163
52.5k
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1164
52.5k
}
1165
1166
void bn_correct_top_consttime(BIGNUM *a)
1167
0
{
1168
0
    int j, atop;
1169
0
    BN_ULONG limb;
1170
0
    unsigned int mask;
1171
1172
0
    for (j = 0, atop = 0; j < a->dmax; j++) {
1173
0
        limb = a->d[j];
1174
0
        limb |= 0 - limb;
1175
0
        limb >>= BN_BITS2 - 1;
1176
0
        limb = 0 - limb;
1177
0
        mask = (unsigned int)limb;
1178
0
        mask &= constant_time_msb(j - a->top);
1179
0
        atop = constant_time_select_int(mask, j + 1, atop);
1180
0
    }
1181
1182
0
    mask = constant_time_eq_int(atop, 0);
1183
0
    a->top = atop;
1184
0
    a->neg = constant_time_select_int(mask, 0, a->neg);
1185
0
    a->flags &= ~BN_FLG_FIXED_TOP;
1186
0
}
1187
1188
void bn_correct_top(BIGNUM *a)
1189
52.5k
{
1190
52.5k
    BN_ULONG *ftl;
1191
52.5k
    int tmp_top = a->top;
1192
1193
52.5k
    if (ossl_likely(tmp_top > 0)) {
1194
52.5k
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1195
52.5k
            ftl--;
1196
52.5k
            if (*ftl != 0)
1197
52.5k
                break;
1198
52.5k
        }
1199
52.5k
        a->top = tmp_top;
1200
52.5k
    }
1201
52.5k
    if (a->top == 0)
1202
0
        a->neg = 0;
1203
52.5k
    a->flags &= ~BN_FLG_FIXED_TOP;
1204
52.5k
    bn_pollute(a);
1205
52.5k
}