Coverage Report

Created: 2026-08-18 07:24

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_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
0
{
84
0
    static const BN_ULONG data_one = 1L;
85
0
    static const BIGNUM const_one = {
86
0
        (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
87
0
    };
88
89
0
    return &const_one;
90
0
}
91
92
#ifndef __e2k__
93
int BN_num_bits_word(BN_ULONG l)
94
120k
{
95
120k
    BN_ULONG x, mask;
96
120k
    int bits = (l != 0);
97
98
120k
#if BN_BITS2 > 32
99
120k
    x = l >> 32;
100
120k
    mask = (0 - x) & BN_MASK2;
101
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
102
120k
    bits += 32 & mask;
103
120k
    l ^= (x ^ l) & mask;
104
120k
#endif
105
106
120k
    x = l >> 16;
107
120k
    mask = (0 - x) & BN_MASK2;
108
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
109
120k
    bits += 16 & mask;
110
120k
    l ^= (x ^ l) & mask;
111
112
120k
    x = l >> 8;
113
120k
    mask = (0 - x) & BN_MASK2;
114
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
115
120k
    bits += 8 & mask;
116
120k
    l ^= (x ^ l) & mask;
117
118
120k
    x = l >> 4;
119
120k
    mask = (0 - x) & BN_MASK2;
120
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
121
120k
    bits += 4 & mask;
122
120k
    l ^= (x ^ l) & mask;
123
124
120k
    x = l >> 2;
125
120k
    mask = (0 - x) & BN_MASK2;
126
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
127
120k
    bits += 2 & mask;
128
120k
    l ^= (x ^ l) & mask;
129
130
120k
    x = l >> 1;
131
120k
    mask = (0 - x) & BN_MASK2;
132
120k
    mask = (0 - (mask >> (BN_BITS2 - 1)));
133
120k
    bits += 1 & mask;
134
135
120k
    return bits;
136
120k
}
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
0
{
158
0
    int j, ret;
159
0
    unsigned int mask, past_i;
160
0
    int i = a->top - 1;
161
0
    bn_check_top(a);
162
163
0
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
164
0
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
165
166
0
        ret += BN_BITS2 & (~mask & ~past_i);
167
0
        ret += BN_num_bits_word(a->d[j]) & mask;
168
169
0
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
170
0
    }
171
172
    /*
173
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
174
     * final result.
175
     */
176
0
    mask = ~(constant_time_eq_int(i, ((int)-1)));
177
178
0
    return ret & mask;
179
0
}
180
181
int BN_num_bits(const BIGNUM *a)
182
123k
{
183
123k
    int i = a->top - 1;
184
123k
    bn_check_top(a);
185
186
123k
    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
0
        return bn_num_bits_consttime(a);
197
0
    }
198
199
123k
    if (ossl_unlikely(BN_is_zero(a)))
200
2.40k
        return 0;
201
202
120k
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
203
123k
}
204
205
static void bn_free_d(BIGNUM *a, int clear)
206
89.0k
{
207
89.0k
    if (BN_get_flags(a, BN_FLG_SECURE))
208
0
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
209
89.0k
    else if (clear != 0)
210
0
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
211
89.0k
    else
212
89.0k
        OPENSSL_free(a->d);
213
89.0k
}
214
215
void BN_clear_free(BIGNUM *a)
216
317k
{
217
317k
    if (a == NULL)
218
317k
        return;
219
0
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
220
0
        bn_free_d(a, 1);
221
0
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
222
0
        OPENSSL_cleanse(a, sizeof(*a));
223
0
        OPENSSL_free(a);
224
0
    }
225
0
}
226
227
void BN_free(BIGNUM *a)
228
109k
{
229
109k
    if (a == NULL)
230
20.0k
        return;
231
89.0k
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
232
89.0k
        bn_free_d(a, 0);
233
89.0k
    if (a->flags & BN_FLG_MALLOCED)
234
89.0k
        OPENSSL_free(a);
235
89.0k
}
236
237
void bn_init(BIGNUM *a)
238
0
{
239
0
    static BIGNUM nilbn;
240
241
0
    *a = nilbn;
242
0
    bn_check_top(a);
243
0
}
244
245
BIGNUM *BN_new(void)
246
89.1k
{
247
89.1k
    BIGNUM *ret;
248
249
89.1k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
250
0
        return NULL;
251
89.1k
    ret->flags = BN_FLG_MALLOCED;
252
89.1k
    bn_check_top(ret);
253
89.1k
    return ret;
254
89.1k
}
255
256
BIGNUM *BN_secure_new(void)
257
0
{
258
0
    BIGNUM *ret = BN_new();
259
260
0
    if (ret != NULL)
261
0
        ret->flags |= BN_FLG_SECURE;
262
0
    return ret;
263
0
}
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
51.4k
{
269
51.4k
    BN_ULONG *a = NULL;
270
271
51.4k
    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
51.4k
    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
51.4k
    if (BN_get_flags(b, BN_FLG_SECURE))
280
0
        a = OPENSSL_secure_calloc(words, sizeof(*a));
281
51.4k
    else
282
51.4k
        a = OPENSSL_calloc(words, sizeof(*a));
283
51.4k
    if (ossl_unlikely(a == NULL))
284
0
        return NULL;
285
286
51.4k
    assert(b->top <= words);
287
51.4k
    if (b->top > 0)
288
0
        memcpy(a, b->d, sizeof(*a) * b->top);
289
290
51.4k
    return a;
291
51.4k
}
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
51.4k
{
303
51.4k
    if (ossl_likely(words > b->dmax)) {
304
51.4k
        BN_ULONG *a = bn_expand_internal(b, words);
305
306
51.4k
        if (ossl_unlikely(!a))
307
0
            return NULL;
308
51.4k
        if (b->d != NULL)
309
0
            bn_free_d(b, 1);
310
51.4k
        b->d = a;
311
51.4k
        b->dmax = words;
312
51.4k
    }
313
314
51.4k
    return b;
315
51.4k
}
316
317
BIGNUM *BN_dup(const BIGNUM *a)
318
0
{
319
0
    BIGNUM *t;
320
321
0
    if (a == NULL)
322
0
        return NULL;
323
0
    bn_check_top(a);
324
325
0
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
326
0
    if (t == NULL)
327
0
        return NULL;
328
0
    if (BN_copy(t, a) == NULL) {
329
0
        BN_free(t);
330
0
        return NULL;
331
0
    }
332
0
    bn_check_top(t);
333
0
    return t;
334
0
}
335
336
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
337
0
{
338
0
    int bn_words;
339
340
0
    bn_check_top(b);
341
342
0
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
343
344
0
    if (ossl_unlikely(a == b))
345
0
        return a;
346
0
    if (ossl_unlikely(bn_wexpand(a, bn_words) == NULL))
347
0
        return NULL;
348
349
0
    if (ossl_likely(b->top > 0))
350
0
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
351
352
0
    a->neg = b->neg;
353
0
    a->top = b->top;
354
0
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
355
0
    bn_check_top(a);
356
0
    return a;
357
0
}
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
0
{
397
0
    if (a == NULL)
398
0
        return;
399
0
    bn_check_top(a);
400
0
    if (a->d != NULL)
401
0
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
402
0
    a->neg = 0;
403
0
    a->top = 0;
404
0
    a->flags &= ~BN_FLG_FIXED_TOP;
405
0
}
406
407
BN_ULONG BN_get_word(const BIGNUM *a)
408
0
{
409
0
    if (a->top > 1)
410
0
        return BN_MASK2;
411
0
    else if (a->top == 1)
412
0
        return a->d[0];
413
    /* a->top == 0 */
414
0
    return 0;
415
0
}
416
417
int BN_set_word(BIGNUM *a, BN_ULONG w)
418
16
{
419
16
    bn_check_top(a);
420
16
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
421
0
        return 0;
422
16
    a->neg = 0;
423
16
    a->d[0] = w;
424
16
    a->top = (w ? 1 : 0);
425
16
    a->flags &= ~BN_FLG_FIXED_TOP;
426
16
    bn_check_top(a);
427
16
    return 1;
428
16
}
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
89.0k
{
438
89.0k
    int inc;
439
89.0k
    const unsigned char *s2;
440
89.0k
    int inc2;
441
89.0k
    int neg = 0, xor = 0, carry = 0;
442
89.0k
    unsigned int i;
443
89.0k
    unsigned int n;
444
89.0k
    BIGNUM *bn = NULL;
445
446
    /* Negative length is not acceptable */
447
89.0k
    if (len < 0)
448
0
        return NULL;
449
450
89.0k
    if (ret == NULL)
451
0
        ret = bn = BN_new();
452
89.0k
    if (ret == NULL)
453
0
        return NULL;
454
89.0k
    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
89.0k
    if (len == 0) {
461
0
        BN_clear(ret);
462
0
        return ret;
463
0
    }
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
89.0k
    if (endianness == LITTLE) {
471
0
        s2 = s + len - 1;
472
0
        inc2 = -1;
473
0
        inc = 1;
474
89.0k
    } else {
475
89.0k
        s2 = s;
476
89.0k
        inc2 = 1;
477
89.0k
        inc = -1;
478
89.0k
        s += len - 1;
479
89.0k
    }
480
481
    /* Take note of the signedness of the input bytes*/
482
89.0k
    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
168k
    for (; len > 0 && *s2 == xor; s2 += inc2, len--)
493
79.3k
        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
89.0k
    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
89.0k
    if (len == 0) {
506
37.6k
        ret->top = 0;
507
37.6k
        return ret;
508
37.6k
    }
509
51.4k
    n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
510
51.4k
    if (bn_wexpand(ret, (int)n) == NULL) {
511
0
        BN_free(bn);
512
0
        return NULL;
513
0
    }
514
51.4k
    ret->top = n;
515
51.4k
    ret->neg = neg;
516
996k
    for (i = 0; n-- > 0; i++) {
517
945k
        BN_ULONG l = 0; /* Accumulator */
518
945k
        unsigned int m = 0; /* Offset in a bignum chunk, in bits */
519
520
8.27M
        for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
521
7.33M
            BN_ULONG byte_xored = *s ^ xor;
522
7.33M
            BN_ULONG byte = (byte_xored + carry) & 0xff;
523
524
7.33M
            carry = byte_xored > byte; /* Implicit 1 or 0 */
525
7.33M
            l |= (byte << m);
526
7.33M
        }
527
945k
        ret->d[i] = l;
528
945k
    }
529
    /*
530
     * need to call this due to clear byte at top if avoiding having the top
531
     * bit set (-ve number)
532
     */
533
51.4k
    bn_correct_top(ret);
534
51.4k
    return ret;
535
51.4k
}
536
537
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
538
89.0k
{
539
89.0k
    return bin2bn(s, len, ret, BIG, UNSIGNED);
540
89.0k
}
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
11.4k
{
550
11.4k
    int inc;
551
11.4k
    int n, n8;
552
11.4k
    int xor = 0, carry = 0, ext = 0;
553
11.4k
    size_t i, lasti, j, atop, mask;
554
11.4k
    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
11.4k
    n8 = BN_num_bits(a);
562
11.4k
    n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
563
564
    /* Take note of the signedness of the bignum */
565
11.4k
    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
11.4k
    if (tolen == -1) {
581
11.4k
        tolen = n + ext;
582
11.4k
    } else if (tolen < n + ext) { /* uncommon/unlike case */
583
0
        BIGNUM temp = *a;
584
585
0
        bn_correct_top(&temp);
586
0
        n8 = BN_num_bits(&temp);
587
0
        n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
588
0
        if (tolen < n + ext)
589
0
            return -1;
590
0
    }
591
592
    /* Swipe through whole available data and don't give away padded zero. */
593
11.4k
    atop = a->dmax * BN_BYTES;
594
11.4k
    if (atop == 0) {
595
184
        if (tolen != 0)
596
0
            memset(to, '\0', tolen);
597
184
        return tolen;
598
184
    }
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
11.2k
    if (endianness == LITTLE) {
606
0
        inc = 1;
607
11.2k
    } else {
608
11.2k
        inc = -1;
609
11.2k
        to += tolen - 1; /* Move to the last byte, not beyond */
610
11.2k
    }
611
612
11.2k
    lasti = atop - 1;
613
11.2k
    atop = a->top * BN_BYTES;
614
1.09M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
615
1.08M
        unsigned char byte, byte_xored;
616
617
1.08M
        l = a->d[i / BN_BYTES];
618
1.08M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
619
1.08M
        byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
620
1.08M
        byte_xored = byte ^ xor;
621
1.08M
        *to = (unsigned char)(byte_xored + carry);
622
1.08M
        carry = byte_xored > *to; /* Implicit 1 or 0 */
623
1.08M
        to += inc;
624
1.08M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
625
1.08M
    }
626
627
11.2k
    return tolen;
628
11.4k
}
629
630
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
631
0
{
632
0
    if (tolen < 0)
633
0
        return -1;
634
0
    return bn2binpad(a, to, tolen, BIG, UNSIGNED);
635
0
}
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
11.4k
{
646
11.4k
    return bn2binpad(a, to, -1, BIG, UNSIGNED);
647
11.4k
}
648
649
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
650
0
{
651
0
    return bin2bn(s, len, ret, LITTLE, UNSIGNED);
652
0
}
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
0
{
661
0
    if (tolen < 0)
662
0
        return -1;
663
0
    return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
664
0
}
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
0
{
675
0
    DECLARE_IS_ENDIAN;
676
677
0
    if (IS_LITTLE_ENDIAN)
678
0
        return BN_lebin2bn(s, len, ret);
679
0
    return BN_bin2bn(s, len, ret);
680
0
}
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
0
{
693
0
    DECLARE_IS_ENDIAN;
694
695
0
    if (IS_LITTLE_ENDIAN)
696
0
        return BN_bn2lebinpad(a, to, tolen);
697
0
    return BN_bn2binpad(a, to, tolen);
698
0
}
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
0
{
711
0
    int i;
712
0
    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
0
    if (!ossl_assert(a != NULL && b != NULL))
720
0
        return (b == NULL) - (a == NULL);
721
722
0
    ap = a->d;
723
0
    bp = b->d;
724
725
0
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
726
0
        || BN_get_flags(b, BN_FLG_CONSTTIME)) {
727
0
        int res = 0;
728
0
        int min_top = a->top < b->top ? a->top : b->top;
729
730
0
        for (i = 0; i < min_top; i++) {
731
0
            res = constant_time_select_int((int)constant_time_lt_bn(ap[i], bp[i]),
732
0
                -1, res);
733
0
            res = constant_time_select_int((int)constant_time_lt_bn(bp[i], ap[i]),
734
0
                1, res);
735
0
        }
736
737
0
        for (i = min_top; i < a->top; ++i)
738
0
            res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
739
0
                res, 1);
740
741
0
        for (i = min_top; i < b->top; ++i)
742
0
            res = constant_time_select_int((int)constant_time_is_zero_bn(bp[i]),
743
0
                res, -1);
744
745
0
        return res;
746
0
    }
747
748
0
    bn_check_top(a);
749
0
    bn_check_top(b);
750
751
0
    i = a->top - b->top;
752
0
    if (i != 0)
753
0
        return i;
754
755
0
    for (i = a->top - 1; i >= 0; i--) {
756
0
        t1 = ap[i];
757
0
        t2 = bp[i];
758
0
        if (t1 != t2)
759
0
            return ((t1 > t2) ? 1 : -1);
760
0
    }
761
0
    return 0;
762
0
}
763
764
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
765
0
{
766
0
    int i;
767
0
    int gt, lt;
768
0
    BN_ULONG t1, t2;
769
770
0
    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
0
    bn_check_top(a);
780
0
    bn_check_top(b);
781
782
0
    if (a->neg != b->neg) {
783
0
        if (a->neg)
784
0
            return -1;
785
0
        else
786
0
            return 1;
787
0
    }
788
0
    if (a->neg == 0) {
789
0
        gt = 1;
790
0
        lt = -1;
791
0
    } else {
792
0
        gt = -1;
793
0
        lt = 1;
794
0
    }
795
796
0
    if (a->top > b->top)
797
0
        return gt;
798
0
    if (a->top < b->top)
799
0
        return lt;
800
0
    for (i = a->top - 1; i >= 0; i--) {
801
0
        t1 = a->d[i];
802
0
        t2 = b->d[i];
803
0
        if (t1 > t2)
804
0
            return gt;
805
0
        if (t1 < t2)
806
0
            return lt;
807
0
    }
808
0
    return 0;
809
0
}
810
811
int BN_set_bit(BIGNUM *a, int n)
812
0
{
813
0
    int i, j, k;
814
815
0
    if (n < 0)
816
0
        return 0;
817
818
0
    i = n / BN_BITS2;
819
0
    j = n % BN_BITS2;
820
0
    if (a->top <= i) {
821
0
        if (bn_wexpand(a, i + 1) == NULL)
822
0
            return 0;
823
0
        for (k = a->top; k < i + 1; k++)
824
0
            a->d[k] = 0;
825
0
        a->top = i + 1;
826
0
        a->flags &= ~BN_FLG_FIXED_TOP;
827
0
    }
828
829
0
    a->d[i] |= (((BN_ULONG)1) << j);
830
0
    bn_check_top(a);
831
0
    return 1;
832
0
}
833
834
int BN_clear_bit(BIGNUM *a, int n)
835
0
{
836
0
    int i, j;
837
838
0
    bn_check_top(a);
839
0
    if (n < 0)
840
0
        return 0;
841
842
0
    i = n / BN_BITS2;
843
0
    j = n % BN_BITS2;
844
0
    if (a->top <= i)
845
0
        return 0;
846
847
0
    a->d[i] &= (~(((BN_ULONG)1) << j));
848
0
    bn_correct_top(a);
849
0
    return 1;
850
0
}
851
852
int BN_is_bit_set(const BIGNUM *a, int n)
853
0
{
854
0
    int i, j;
855
856
0
    bn_check_top(a);
857
0
    if (ossl_unlikely(n < 0))
858
0
        return 0;
859
0
    i = n / BN_BITS2;
860
0
    j = n % BN_BITS2;
861
0
    if (ossl_unlikely(a->top <= i))
862
0
        return 0;
863
0
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
864
0
}
865
866
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
867
0
{
868
0
    int b, w;
869
870
0
    if (n < 0)
871
0
        return 0;
872
873
0
    w = n / BN_BITS2;
874
0
    b = n % BN_BITS2;
875
0
    if (w >= a->top)
876
0
        return 0;
877
0
    if (b == 0)
878
0
        a->top = w;
879
0
    else {
880
0
        a->top = w + 1;
881
0
        a->d[w] &= ~(BN_MASK2 << b);
882
0
    }
883
0
    a->flags |= BN_FLG_FIXED_TOP;
884
0
    return 1;
885
0
}
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
0
{
900
0
    if (b && !BN_is_zero(a))
901
0
        a->neg = 1;
902
0
    else
903
0
        a->neg = 0;
904
0
}
905
906
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
907
0
{
908
0
    int i;
909
0
    BN_ULONG aa, bb;
910
911
0
    if (ossl_unlikely(n == 0))
912
0
        return 0;
913
914
0
    aa = a[n - 1];
915
0
    bb = b[n - 1];
916
0
    if (ossl_likely(aa != bb))
917
0
        return ((aa > bb) ? 1 : -1);
918
0
    for (i = n - 2; i >= 0; i--) {
919
0
        aa = a[i];
920
0
        bb = b[i];
921
0
        if (aa != bb)
922
0
            return ((aa > bb) ? 1 : -1);
923
0
    }
924
0
    return 0;
925
0
}
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
0
{
938
0
    int n, i;
939
0
    n = cl - 1;
940
941
0
    if (dl < 0) {
942
0
        for (i = dl; i < 0; i++) {
943
0
            if (b[n - i] != 0)
944
0
                return -1; /* a < b */
945
0
        }
946
0
    }
947
0
    if (dl > 0) {
948
0
        for (i = dl; i > 0; i--) {
949
0
            if (a[n + i] != 0)
950
0
                return 1; /* a > b */
951
0
        }
952
0
    }
953
0
    return bn_cmp_words(a, b, cl);
954
0
}
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
0
{
965
0
    BN_ULONG t;
966
0
    int i;
967
968
0
    bn_wcheck_size(a, nwords);
969
0
    bn_wcheck_size(b, nwords);
970
971
0
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
972
973
0
    t = (a->top ^ b->top) & value_barrier_bn(condition);
974
0
    a->top ^= t;
975
0
    b->top ^= t;
976
977
0
    t = (a->neg ^ b->neg) & value_barrier_bn(condition);
978
0
    a->neg ^= t;
979
0
    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
0
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
1007
1008
0
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & value_barrier_bn(condition);
1009
0
    a->flags ^= t;
1010
0
    b->flags ^= t;
1011
1012
    /* conditionally swap the data */
1013
0
    for (i = 0; i < nwords; i++) {
1014
0
        t = (a->d[i] ^ b->d[i]) & value_barrier_bn(condition);
1015
0
        a->d[i] ^= t;
1016
0
        b->d[i] ^= t;
1017
0
    }
1018
0
}
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
0
{
1026
0
    int secbits, bits;
1027
0
    if (L >= 15360)
1028
0
        secbits = 256;
1029
0
    else if (L >= 7680)
1030
0
        secbits = 192;
1031
0
    else if (L >= 3072)
1032
0
        secbits = 128;
1033
0
    else if (L >= 2048)
1034
0
        secbits = 112;
1035
0
    else if (L >= 1024)
1036
0
        secbits = 80;
1037
0
    else
1038
0
        return 0;
1039
0
    if (N == -1)
1040
0
        return secbits;
1041
0
    bits = N / 2;
1042
0
    if (bits < 80)
1043
0
        return 0;
1044
0
    return bits >= secbits ? secbits : bits;
1045
0
}
1046
1047
void BN_zero_ex(BIGNUM *a)
1048
32
{
1049
32
    a->neg = 0;
1050
32
    a->top = 0;
1051
32
    a->flags &= ~BN_FLG_FIXED_TOP;
1052
32
}
1053
1054
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1055
773
{
1056
773
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
1057
773
}
1058
1059
int BN_is_zero(const BIGNUM *a)
1060
123k
{
1061
123k
    return a->top == 0;
1062
123k
}
1063
1064
int BN_is_one(const BIGNUM *a)
1065
0
{
1066
0
    return BN_abs_is_word(a, 1) && !a->neg;
1067
0
}
1068
1069
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
1070
773
{
1071
773
    return BN_abs_is_word(a, w) && (!w || !a->neg);
1072
773
}
1073
1074
int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
1075
0
{
1076
0
    int res, i;
1077
0
    const BN_ULONG *ap = a->d;
1078
1079
0
    if (a->neg || a->top == 0)
1080
0
        return 0;
1081
1082
0
    res = constant_time_select_int((int)constant_time_eq_bn(ap[0], w), 1, 0);
1083
1084
0
    for (i = 1; i < a->top; i++)
1085
0
        res = constant_time_select_int((int)constant_time_is_zero_bn(ap[i]),
1086
0
            res, 0);
1087
0
    return res;
1088
0
}
1089
1090
int BN_is_odd(const BIGNUM *a)
1091
0
{
1092
0
    return (a->top > 0) && (a->d[0] & 1);
1093
0
}
1094
1095
int BN_is_negative(const BIGNUM *a)
1096
0
{
1097
0
    return (a->neg != 0);
1098
0
}
1099
1100
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
1101
    BN_CTX *ctx)
1102
0
{
1103
0
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
1104
0
}
1105
1106
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
1107
0
{
1108
0
    dest->d = b->d;
1109
0
    dest->top = b->top;
1110
0
    dest->dmax = b->dmax;
1111
0
    dest->neg = b->neg;
1112
0
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
1113
0
        | (b->flags & ~BN_FLG_MALLOCED)
1114
0
        | BN_FLG_STATIC_DATA | flags);
1115
0
}
1116
1117
BN_GENCB *BN_GENCB_new(void)
1118
0
{
1119
0
    BN_GENCB *ret;
1120
1121
0
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
1122
0
        return NULL;
1123
1124
0
    return ret;
1125
0
}
1126
1127
void BN_GENCB_free(BN_GENCB *cb)
1128
0
{
1129
0
    if (cb == NULL)
1130
0
        return;
1131
0
    OPENSSL_free(cb);
1132
0
}
1133
1134
void BN_set_flags(BIGNUM *b, int n)
1135
0
{
1136
0
    b->flags |= n;
1137
0
}
1138
1139
int BN_get_flags(const BIGNUM *b, int n)
1140
281k
{
1141
281k
    return b->flags & n;
1142
281k
}
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
0
{
1158
0
    BN_GENCB *tmp_gencb = gencb;
1159
0
    tmp_gencb->ver = 2;
1160
0
    tmp_gencb->arg = cb_arg;
1161
0
    tmp_gencb->cb.cb_2 = callback;
1162
0
}
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
51.4k
{
1171
51.4k
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1172
51.4k
}
1173
1174
void bn_correct_top_consttime(BIGNUM *a)
1175
0
{
1176
0
    int j, atop;
1177
0
    BN_ULONG limb;
1178
0
    unsigned int mask;
1179
1180
0
    for (j = 0, atop = 0; j < a->dmax; j++) {
1181
0
        limb = a->d[j];
1182
0
        limb |= 0 - limb;
1183
0
        limb >>= BN_BITS2 - 1;
1184
0
        limb = 0 - limb;
1185
0
        mask = (unsigned int)limb;
1186
0
        mask &= constant_time_msb(j - a->top);
1187
0
        atop = constant_time_select_int(mask, j + 1, atop);
1188
0
    }
1189
1190
0
    mask = constant_time_eq_int(atop, 0);
1191
0
    a->top = atop;
1192
0
    a->neg = constant_time_select_int(mask, 0, a->neg);
1193
0
    a->flags &= ~BN_FLG_FIXED_TOP;
1194
0
}
1195
1196
void bn_correct_top(BIGNUM *a)
1197
51.4k
{
1198
51.4k
    BN_ULONG *ftl;
1199
51.4k
    int tmp_top = a->top;
1200
1201
51.4k
    if (ossl_likely(tmp_top > 0)) {
1202
51.4k
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1203
51.4k
            ftl--;
1204
51.4k
            if (*ftl != 0)
1205
51.4k
                break;
1206
51.4k
        }
1207
51.4k
        a->top = tmp_top;
1208
51.4k
    }
1209
51.4k
    if (a->top == 0)
1210
0
        a->neg = 0;
1211
51.4k
    a->flags &= ~BN_FLG_FIXED_TOP;
1212
51.4k
    bn_pollute(a);
1213
51.4k
}