Coverage Report

Created: 2025-12-31 06:58

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