Coverage Report

Created: 2025-12-31 06:58

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