Coverage Report

Created: 2026-07-16 06:59

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