Coverage Report

Created: 2024-07-27 06:39

/src/openssl31/crypto/bn/bn_lib.c
Line
Count
Source (jump to first uncovered line)
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.07M
{
84
1.07M
    static const BN_ULONG data_one = 1L;
85
1.07M
    static const BIGNUM const_one =
86
1.07M
        { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
87
88
1.07M
    return &const_one;
89
1.07M
}
90
91
/*
92
 * Old Visual Studio ARM compiler miscompiles BN_num_bits_word()
93
 * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html
94
 */
95
#if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \
96
    && _MSC_VER>=1400 && _MSC_VER<1501
97
# define MS_BROKEN_BN_num_bits_word
98
# pragma optimize("", off)
99
#endif
100
int BN_num_bits_word(BN_ULONG l)
101
147M
{
102
147M
    BN_ULONG x, mask;
103
147M
    int bits = (l != 0);
104
105
147M
#if BN_BITS2 > 32
106
147M
    x = l >> 32;
107
147M
    mask = (0 - x) & BN_MASK2;
108
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
109
147M
    bits += 32 & mask;
110
147M
    l ^= (x ^ l) & mask;
111
147M
#endif
112
113
147M
    x = l >> 16;
114
147M
    mask = (0 - x) & BN_MASK2;
115
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
116
147M
    bits += 16 & mask;
117
147M
    l ^= (x ^ l) & mask;
118
119
147M
    x = l >> 8;
120
147M
    mask = (0 - x) & BN_MASK2;
121
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
122
147M
    bits += 8 & mask;
123
147M
    l ^= (x ^ l) & mask;
124
125
147M
    x = l >> 4;
126
147M
    mask = (0 - x) & BN_MASK2;
127
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
128
147M
    bits += 4 & mask;
129
147M
    l ^= (x ^ l) & mask;
130
131
147M
    x = l >> 2;
132
147M
    mask = (0 - x) & BN_MASK2;
133
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
134
147M
    bits += 2 & mask;
135
147M
    l ^= (x ^ l) & mask;
136
137
147M
    x = l >> 1;
138
147M
    mask = (0 - x) & BN_MASK2;
139
147M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
140
147M
    bits += 1 & mask;
141
142
147M
    return bits;
143
147M
}
144
#ifdef MS_BROKEN_BN_num_bits_word
145
# pragma optimize("", on)
146
#endif
147
148
/*
149
 * This function still leaks `a->dmax`: it's caller's responsibility to
150
 * expand the input `a` in advance to a public length.
151
 */
152
static ossl_inline
153
int bn_num_bits_consttime(const BIGNUM *a)
154
9.34M
{
155
9.34M
    int j, ret;
156
9.34M
    unsigned int mask, past_i;
157
9.34M
    int i = a->top - 1;
158
9.34M
    bn_check_top(a);
159
160
47.0M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
37.7M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
37.7M
        ret += BN_BITS2 & (~mask & ~past_i);
164
37.7M
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
37.7M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
37.7M
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
9.34M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
9.34M
    return ret & mask;
176
9.34M
}
177
178
int BN_num_bits(const BIGNUM *a)
179
35.2M
{
180
35.2M
    int i = a->top - 1;
181
35.2M
    bn_check_top(a);
182
183
35.2M
    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
9.34M
        return bn_num_bits_consttime(a);
194
9.34M
    }
195
196
25.9M
    if (BN_is_zero(a))
197
2.29M
        return 0;
198
199
23.6M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
25.9M
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
50.1M
{
204
50.1M
    if (BN_get_flags(a, BN_FLG_SECURE))
205
1.47M
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
48.6M
    else if (clear != 0)
207
27.4M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
21.1M
    else
209
21.1M
        OPENSSL_free(a->d);
210
50.1M
}
211
212
213
void BN_clear_free(BIGNUM *a)
214
22.6M
{
215
22.6M
    if (a == NULL)
216
3.87M
        return;
217
18.7M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
218
16.9M
        bn_free_d(a, 1);
219
18.7M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
220
2.44M
        OPENSSL_cleanse(a, sizeof(*a));
221
2.44M
        OPENSSL_free(a);
222
2.44M
    }
223
18.7M
}
224
225
void BN_free(BIGNUM *a)
226
32.6M
{
227
32.6M
    if (a == NULL)
228
11.4M
        return;
229
21.1M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
230
21.1M
        bn_free_d(a, 0);
231
21.1M
    if (a->flags & BN_FLG_MALLOCED)
232
21.1M
        OPENSSL_free(a);
233
21.1M
}
234
235
void bn_init(BIGNUM *a)
236
38.9M
{
237
38.9M
    static BIGNUM nilbn;
238
239
38.9M
    *a = nilbn;
240
38.9M
    bn_check_top(a);
241
38.9M
}
242
243
BIGNUM *BN_new(void)
244
23.6M
{
245
23.6M
    BIGNUM *ret;
246
247
23.6M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
248
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
249
0
        return NULL;
250
0
    }
251
23.6M
    ret->flags = BN_FLG_MALLOCED;
252
23.6M
    bn_check_top(ret);
253
23.6M
    return ret;
254
23.6M
}
255
256
 BIGNUM *BN_secure_new(void)
257
1.71M
 {
258
1.71M
     BIGNUM *ret = BN_new();
259
1.71M
     if (ret != NULL)
260
1.71M
         ret->flags |= BN_FLG_SECURE;
261
1.71M
     return ret;
262
1.71M
 }
263
264
/* This is used by bn_expand2() */
265
/* The caller MUST check that words > b->dmax before calling this */
266
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
267
47.2M
{
268
47.2M
    BN_ULONG *a = NULL;
269
270
47.2M
    if (words > (INT_MAX / (4 * BN_BITS2))) {
271
0
        ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG);
272
0
        return NULL;
273
0
    }
274
47.2M
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
275
0
        ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
276
0
        return NULL;
277
0
    }
278
47.2M
    if (BN_get_flags(b, BN_FLG_SECURE))
279
1.47M
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
280
45.7M
    else
281
45.7M
        a = OPENSSL_zalloc(words * sizeof(*a));
282
47.2M
    if (a == NULL) {
283
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
284
0
        return NULL;
285
0
    }
286
287
47.2M
    assert(b->top <= words);
288
47.2M
    if (b->top > 0)
289
6.39M
        memcpy(a, b->d, sizeof(*a) * b->top);
290
291
47.2M
    return a;
292
47.2M
}
293
294
/*
295
 * This is an internal function that should not be used in applications. It
296
 * ensures that 'b' has enough room for a 'words' word number and initialises
297
 * any unused part of b->d with leading zeros. It is mostly used by the
298
 * various BIGNUM routines. If there is an error, NULL is returned. If not,
299
 * 'b' is returned.
300
 */
301
302
BIGNUM *bn_expand2(BIGNUM *b, int words)
303
47.2M
{
304
47.2M
    if (words > b->dmax) {
305
47.2M
        BN_ULONG *a = bn_expand_internal(b, words);
306
47.2M
        if (!a)
307
0
            return NULL;
308
47.2M
        if (b->d != NULL)
309
12.0M
            bn_free_d(b, 1);
310
47.2M
        b->d = a;
311
47.2M
        b->dmax = words;
312
47.2M
    }
313
314
47.2M
    return b;
315
47.2M
}
316
317
BIGNUM *BN_dup(const BIGNUM *a)
318
4.30M
{
319
4.30M
    BIGNUM *t;
320
321
4.30M
    if (a == NULL)
322
0
        return NULL;
323
4.30M
    bn_check_top(a);
324
325
4.30M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
326
4.30M
    if (t == NULL)
327
0
        return NULL;
328
4.30M
    if (!BN_copy(t, a)) {
329
0
        BN_free(t);
330
0
        return NULL;
331
0
    }
332
4.30M
    bn_check_top(t);
333
4.30M
    return t;
334
4.30M
}
335
336
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
337
101M
{
338
101M
    int bn_words;
339
340
101M
    bn_check_top(b);
341
342
101M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
343
344
101M
    if (a == b)
345
2
        return a;
346
101M
    if (bn_wexpand(a, bn_words) == NULL)
347
0
        return NULL;
348
349
101M
    if (b->top > 0)
350
99.6M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
351
352
101M
    a->neg = b->neg;
353
101M
    a->top = b->top;
354
101M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
355
101M
    bn_check_top(a);
356
101M
    return a;
357
101M
}
358
359
0
#define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
360
0
                                    | BN_FLG_CONSTTIME   \
361
0
                                    | BN_FLG_SECURE      \
362
0
                                    | BN_FLG_FIXED_TOP))
363
0
#define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
364
365
void BN_swap(BIGNUM *a, BIGNUM *b)
366
0
{
367
0
    int flags_old_a, flags_old_b;
368
0
    BN_ULONG *tmp_d;
369
0
    int tmp_top, tmp_dmax, tmp_neg;
370
371
0
    bn_check_top(a);
372
0
    bn_check_top(b);
373
374
0
    flags_old_a = a->flags;
375
0
    flags_old_b = b->flags;
376
377
0
    tmp_d = a->d;
378
0
    tmp_top = a->top;
379
0
    tmp_dmax = a->dmax;
380
0
    tmp_neg = a->neg;
381
382
0
    a->d = b->d;
383
0
    a->top = b->top;
384
0
    a->dmax = b->dmax;
385
0
    a->neg = b->neg;
386
387
0
    b->d = tmp_d;
388
0
    b->top = tmp_top;
389
0
    b->dmax = tmp_dmax;
390
0
    b->neg = tmp_neg;
391
392
0
    a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
393
0
    b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
394
0
    bn_check_top(a);
395
0
    bn_check_top(b);
396
0
}
397
398
void BN_clear(BIGNUM *a)
399
352k
{
400
352k
    if (a == NULL)
401
0
        return;
402
352k
    bn_check_top(a);
403
352k
    if (a->d != NULL)
404
21.4k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
405
352k
    a->neg = 0;
406
352k
    a->top = 0;
407
352k
    a->flags &= ~BN_FLG_FIXED_TOP;
408
352k
}
409
410
BN_ULONG BN_get_word(const BIGNUM *a)
411
281
{
412
281
    if (a->top > 1)
413
6
        return BN_MASK2;
414
275
    else if (a->top == 1)
415
240
        return a->d[0];
416
    /* a->top == 0 */
417
35
    return 0;
418
281
}
419
420
int BN_set_word(BIGNUM *a, BN_ULONG w)
421
1.81M
{
422
1.81M
    bn_check_top(a);
423
1.81M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
424
0
        return 0;
425
1.81M
    a->neg = 0;
426
1.81M
    a->d[0] = w;
427
1.81M
    a->top = (w ? 1 : 0);
428
1.81M
    a->flags &= ~BN_FLG_FIXED_TOP;
429
1.81M
    bn_check_top(a);
430
1.81M
    return 1;
431
1.81M
}
432
433
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
434
4.30M
{
435
4.30M
    unsigned int i, m;
436
4.30M
    unsigned int n;
437
4.30M
    BN_ULONG l;
438
4.30M
    BIGNUM *bn = NULL;
439
440
4.30M
    if (ret == NULL)
441
3.13M
        ret = bn = BN_new();
442
4.30M
    if (ret == NULL)
443
0
        return NULL;
444
4.30M
    bn_check_top(ret);
445
    /* Skip leading zero's. */
446
9.10M
    for ( ; len > 0 && *s == 0; s++, len--)
447
4.80M
        continue;
448
4.30M
    n = len;
449
4.30M
    if (n == 0) {
450
577k
        ret->top = 0;
451
577k
        return ret;
452
577k
    }
453
3.72M
    i = ((n - 1) / BN_BYTES) + 1;
454
3.72M
    m = ((n - 1) % (BN_BYTES));
455
3.72M
    if (bn_wexpand(ret, (int)i) == NULL) {
456
0
        BN_free(bn);
457
0
        return NULL;
458
0
    }
459
3.72M
    ret->top = i;
460
3.72M
    ret->neg = 0;
461
3.72M
    l = 0;
462
152M
    while (n--) {
463
149M
        l = (l << 8L) | *(s++);
464
149M
        if (m-- == 0) {
465
20.6M
            ret->d[--i] = l;
466
20.6M
            l = 0;
467
20.6M
            m = BN_BYTES - 1;
468
20.6M
        }
469
149M
    }
470
    /*
471
     * need to call this due to clear byte at top if avoiding having the top
472
     * bit set (-ve number)
473
     */
474
3.72M
    bn_correct_top(ret);
475
3.72M
    return ret;
476
3.72M
}
477
478
typedef enum {big, little} endianess_t;
479
480
/* ignore negative */
481
static
482
int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess)
483
536k
{
484
536k
    int n;
485
536k
    size_t i, lasti, j, atop, mask;
486
536k
    BN_ULONG l;
487
488
    /*
489
     * In case |a| is fixed-top, BN_num_bytes can return bogus length,
490
     * but it's assumed that fixed-top inputs ought to be "nominated"
491
     * even for padded output, so it works out...
492
     */
493
536k
    n = BN_num_bytes(a);
494
536k
    if (tolen == -1) {
495
263k
        tolen = n;
496
272k
    } else if (tolen < n) {     /* uncommon/unlike case */
497
1.04k
        BIGNUM temp = *a;
498
499
1.04k
        bn_correct_top(&temp);
500
1.04k
        n = BN_num_bytes(&temp);
501
1.04k
        if (tolen < n)
502
1.04k
            return -1;
503
1.04k
    }
504
505
    /* Swipe through whole available data and don't give away padded zero. */
506
535k
    atop = a->dmax * BN_BYTES;
507
535k
    if (atop == 0) {
508
12.5k
        if (tolen != 0)
509
1.59k
            memset(to, '\0', tolen);
510
12.5k
        return tolen;
511
12.5k
    }
512
513
523k
    lasti = atop - 1;
514
523k
    atop = a->top * BN_BYTES;
515
523k
    if (endianess == big)
516
309k
        to += tolen; /* start from the end of the buffer */
517
37.2M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
518
36.7M
        unsigned char val;
519
36.7M
        l = a->d[i / BN_BYTES];
520
36.7M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
521
36.7M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
522
36.7M
        if (endianess == big)
523
14.7M
            *--to = val;
524
21.9M
        else
525
21.9M
            *to++ = val;
526
36.7M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
527
36.7M
    }
528
529
523k
    return tolen;
530
535k
}
531
532
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
533
124k
{
534
124k
    if (tolen < 0)
535
0
        return -1;
536
124k
    return bn2binpad(a, to, tolen, big);
537
124k
}
538
539
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
540
789k
{
541
789k
    return bn2binpad(a, to, -1, big);
542
789k
}
543
544
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
545
214k
{
546
214k
    unsigned int i, m;
547
214k
    unsigned int n;
548
214k
    BN_ULONG l;
549
214k
    BIGNUM *bn = NULL;
550
551
214k
    if (ret == NULL)
552
189k
        ret = bn = BN_new();
553
214k
    if (ret == NULL)
554
0
        return NULL;
555
214k
    bn_check_top(ret);
556
214k
    s += len;
557
    /* Skip trailing zeroes. */
558
243k
    for ( ; len > 0 && s[-1] == 0; s--, len--)
559
28.7k
        continue;
560
214k
    n = len;
561
214k
    if (n == 0) {
562
964
        ret->top = 0;
563
964
        return ret;
564
964
    }
565
213k
    i = ((n - 1) / BN_BYTES) + 1;
566
213k
    m = ((n - 1) % (BN_BYTES));
567
213k
    if (bn_wexpand(ret, (int)i) == NULL) {
568
0
        BN_free(bn);
569
0
        return NULL;
570
0
    }
571
213k
    ret->top = i;
572
213k
    ret->neg = 0;
573
213k
    l = 0;
574
22.1M
    while (n--) {
575
21.9M
        s--;
576
21.9M
        l = (l << 8L) | *s;
577
21.9M
        if (m-- == 0) {
578
2.77M
            ret->d[--i] = l;
579
2.77M
            l = 0;
580
2.77M
            m = BN_BYTES - 1;
581
2.77M
        }
582
21.9M
    }
583
    /*
584
     * need to call this due to clear byte at top if avoiding having the top
585
     * bit set (-ve number)
586
     */
587
213k
    bn_correct_top(ret);
588
213k
    return ret;
589
213k
}
590
591
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
592
583k
{
593
583k
    if (tolen < 0)
594
0
        return -1;
595
583k
    return bn2binpad(a, to, tolen, little);
596
583k
}
597
598
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
599
558k
{
600
558k
    DECLARE_IS_ENDIAN;
601
602
558k
    if (IS_LITTLE_ENDIAN)
603
558k
        return BN_lebin2bn(s, len, ret);
604
0
    return BN_bin2bn(s, len, ret);
605
558k
}
606
607
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
608
558k
{
609
558k
    DECLARE_IS_ENDIAN;
610
611
558k
    if (IS_LITTLE_ENDIAN)
612
558k
        return BN_bn2lebinpad(a, to, tolen);
613
0
    return BN_bn2binpad(a, to, tolen);
614
558k
}
615
616
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
617
77.7M
{
618
77.7M
    int i;
619
77.7M
    BN_ULONG t1, t2, *ap, *bp;
620
621
77.7M
    ap = a->d;
622
77.7M
    bp = b->d;
623
624
77.7M
    if (BN_get_flags(a, BN_FLG_CONSTTIME)
625
77.7M
            && a->top == b->top) {
626
1.40M
        int res = 0;
627
628
8.15M
        for (i = 0; i < b->top; i++) {
629
6.75M
            res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
630
6.75M
                                           -1, res);
631
6.75M
            res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
632
6.75M
                                           1, res);
633
6.75M
        }
634
1.40M
        return res;
635
1.40M
    }
636
637
76.3M
    bn_check_top(a);
638
76.3M
    bn_check_top(b);
639
640
76.3M
    i = a->top - b->top;
641
76.3M
    if (i != 0)
642
9.39M
        return i;
643
644
70.9M
    for (i = a->top - 1; i >= 0; i--) {
645
69.7M
        t1 = ap[i];
646
69.7M
        t2 = bp[i];
647
69.7M
        if (t1 != t2)
648
65.8M
            return ((t1 > t2) ? 1 : -1);
649
69.7M
    }
650
1.17M
    return 0;
651
66.9M
}
652
653
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
654
11.6M
{
655
11.6M
    int i;
656
11.6M
    int gt, lt;
657
11.6M
    BN_ULONG t1, t2;
658
659
11.6M
    if ((a == NULL) || (b == NULL)) {
660
0
        if (a != NULL)
661
0
            return -1;
662
0
        else if (b != NULL)
663
0
            return 1;
664
0
        else
665
0
            return 0;
666
0
    }
667
668
11.6M
    bn_check_top(a);
669
11.6M
    bn_check_top(b);
670
671
11.6M
    if (a->neg != b->neg) {
672
838
        if (a->neg)
673
343
            return -1;
674
495
        else
675
495
            return 1;
676
838
    }
677
11.6M
    if (a->neg == 0) {
678
11.6M
        gt = 1;
679
11.6M
        lt = -1;
680
11.6M
    } else {
681
6.34k
        gt = -1;
682
6.34k
        lt = 1;
683
6.34k
    }
684
685
11.6M
    if (a->top > b->top)
686
3.23M
        return gt;
687
8.45M
    if (a->top < b->top)
688
1.02M
        return lt;
689
21.9M
    for (i = a->top - 1; i >= 0; i--) {
690
20.8M
        t1 = a->d[i];
691
20.8M
        t2 = b->d[i];
692
20.8M
        if (t1 > t2)
693
2.09M
            return gt;
694
18.7M
        if (t1 < t2)
695
4.15M
            return lt;
696
18.7M
    }
697
1.17M
    return 0;
698
7.42M
}
699
700
int BN_set_bit(BIGNUM *a, int n)
701
1.49M
{
702
1.49M
    int i, j, k;
703
704
1.49M
    if (n < 0)
705
0
        return 0;
706
707
1.49M
    i = n / BN_BITS2;
708
1.49M
    j = n % BN_BITS2;
709
1.49M
    if (a->top <= i) {
710
1.49M
        if (bn_wexpand(a, i + 1) == NULL)
711
0
            return 0;
712
12.3M
        for (k = a->top; k < i + 1; k++)
713
10.8M
            a->d[k] = 0;
714
1.49M
        a->top = i + 1;
715
1.49M
        a->flags &= ~BN_FLG_FIXED_TOP;
716
1.49M
    }
717
718
1.49M
    a->d[i] |= (((BN_ULONG)1) << j);
719
1.49M
    bn_check_top(a);
720
1.49M
    return 1;
721
1.49M
}
722
723
int BN_clear_bit(BIGNUM *a, int n)
724
331
{
725
331
    int i, j;
726
727
331
    bn_check_top(a);
728
331
    if (n < 0)
729
0
        return 0;
730
731
331
    i = n / BN_BITS2;
732
331
    j = n % BN_BITS2;
733
331
    if (a->top <= i)
734
0
        return 0;
735
736
331
    a->d[i] &= (~(((BN_ULONG)1) << j));
737
331
    bn_correct_top(a);
738
331
    return 1;
739
331
}
740
741
int BN_is_bit_set(const BIGNUM *a, int n)
742
174M
{
743
174M
    int i, j;
744
745
174M
    bn_check_top(a);
746
174M
    if (n < 0)
747
3.10k
        return 0;
748
174M
    i = n / BN_BITS2;
749
174M
    j = n % BN_BITS2;
750
174M
    if (a->top <= i)
751
10.4k
        return 0;
752
174M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
753
174M
}
754
755
int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
756
2.80k
{
757
2.80k
    int b, w;
758
759
2.80k
    if (n < 0)
760
0
        return 0;
761
762
2.80k
    w = n / BN_BITS2;
763
2.80k
    b = n % BN_BITS2;
764
2.80k
    if (w >= a->top)
765
0
        return 0;
766
2.80k
    if (b == 0)
767
2.80k
        a->top = w;
768
0
    else {
769
0
        a->top = w + 1;
770
0
        a->d[w] &= ~(BN_MASK2 << b);
771
0
    }
772
2.80k
    a->flags |= BN_FLG_FIXED_TOP;
773
2.80k
    return 1;
774
2.80k
}
775
776
int BN_mask_bits(BIGNUM *a, int n)
777
0
{
778
0
    int ret;
779
780
0
    bn_check_top(a);
781
0
    ret = ossl_bn_mask_bits_fixed_top(a, n);
782
0
    if (ret)
783
0
        bn_correct_top(a);
784
0
    return ret;
785
0
}
786
787
void BN_set_negative(BIGNUM *a, int b)
788
1.49M
{
789
1.49M
    if (b && !BN_is_zero(a))
790
487k
        a->neg = 1;
791
1.00M
    else
792
1.00M
        a->neg = 0;
793
1.49M
}
794
795
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
796
56.2M
{
797
56.2M
    int i;
798
56.2M
    BN_ULONG aa, bb;
799
800
56.2M
    if (n == 0)
801
0
        return 0;
802
803
56.2M
    aa = a[n - 1];
804
56.2M
    bb = b[n - 1];
805
56.2M
    if (aa != bb)
806
52.0M
        return ((aa > bb) ? 1 : -1);
807
37.1M
    for (i = n - 2; i >= 0; i--) {
808
35.4M
        aa = a[i];
809
35.4M
        bb = b[i];
810
35.4M
        if (aa != bb)
811
2.45M
            return ((aa > bb) ? 1 : -1);
812
35.4M
    }
813
1.68M
    return 0;
814
4.13M
}
815
816
/*
817
 * Here follows a specialised variants of bn_cmp_words().  It has the
818
 * capability of performing the operation on arrays of different sizes. The
819
 * sizes of those arrays is expressed through cl, which is the common length
820
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
821
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
822
 * BN_ULONGs...
823
 */
824
825
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
826
52.4M
{
827
52.4M
    int n, i;
828
52.4M
    n = cl - 1;
829
830
52.4M
    if (dl < 0) {
831
1.07M
        for (i = dl; i < 0; i++) {
832
1.04M
            if (b[n - i] != 0)
833
397k
                return -1;      /* a < b */
834
1.04M
        }
835
434k
    }
836
52.0M
    if (dl > 0) {
837
1.36M
        for (i = dl; i > 0; i--) {
838
1.29M
            if (a[n + i] != 0)
839
374k
                return 1;       /* a > b */
840
1.29M
        }
841
441k
    }
842
51.6M
    return bn_cmp_words(a, b, cl);
843
52.0M
}
844
845
/*-
846
 * Constant-time conditional swap of a and b.
847
 * a and b are swapped if condition is not 0.
848
 * nwords is the number of words to swap.
849
 * Assumes that at least nwords are allocated in both a and b.
850
 * Assumes that no more than nwords are used by either a or b.
851
 */
852
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
853
49.1M
{
854
49.1M
    BN_ULONG t;
855
49.1M
    int i;
856
857
49.1M
    bn_wcheck_size(a, nwords);
858
49.1M
    bn_wcheck_size(b, nwords);
859
860
49.1M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
861
862
49.1M
    t = (a->top ^ b->top) & condition;
863
49.1M
    a->top ^= t;
864
49.1M
    b->top ^= t;
865
866
49.1M
    t = (a->neg ^ b->neg) & condition;
867
49.1M
    a->neg ^= t;
868
49.1M
    b->neg ^= t;
869
870
    /*-
871
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
872
     * is actually to treat it as it's read-only data, and some (if not most)
873
     * of it does reside in read-only segment. In other words observation of
874
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
875
     * condition. It would either cause SEGV or effectively cause data
876
     * corruption.
877
     *
878
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
879
     * preserved.
880
     *
881
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
882
     * allocated and hence how to free it.
883
     *
884
     * BN_FLG_CONSTTIME: sufficient to mask and swap
885
     *
886
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
887
     * the data, so the d array may be padded with additional 0 values (i.e.
888
     * top could be greater than the minimal value that it could be). We should
889
     * be swapping it
890
     */
891
892
49.1M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
893
894
49.1M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
895
49.1M
    a->flags ^= t;
896
49.1M
    b->flags ^= t;
897
898
    /* conditionally swap the data */
899
12.4G
    for (i = 0; i < nwords; i++) {
900
12.4G
        t = (a->d[i] ^ b->d[i]) & condition;
901
12.4G
        a->d[i] ^= t;
902
12.4G
        b->d[i] ^= t;
903
12.4G
    }
904
49.1M
}
905
906
#undef BN_CONSTTIME_SWAP_FLAGS
907
908
/* Bits of security, see SP800-57 */
909
910
int BN_security_bits(int L, int N)
911
105k
{
912
105k
    int secbits, bits;
913
105k
    if (L >= 15360)
914
386
        secbits = 256;
915
105k
    else if (L >= 7680)
916
1.24k
        secbits = 192;
917
104k
    else if (L >= 3072)
918
2.00k
        secbits = 128;
919
102k
    else if (L >= 2048)
920
2.36k
        secbits = 112;
921
99.6k
    else if (L >= 1024)
922
62.5k
        secbits = 80;
923
37.1k
    else
924
37.1k
        return 0;
925
68.5k
    if (N == -1)
926
7.72k
        return secbits;
927
60.7k
    bits = N / 2;
928
60.7k
    if (bits < 80)
929
4.55k
        return 0;
930
56.2k
    return bits >= secbits ? secbits : bits;
931
60.7k
}
932
933
void BN_zero_ex(BIGNUM *a)
934
681M
{
935
681M
    a->neg = 0;
936
681M
    a->top = 0;
937
681M
    a->flags &= ~BN_FLG_FIXED_TOP;
938
681M
}
939
940
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
941
56.6M
{
942
56.6M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
943
56.6M
}
944
945
int BN_is_zero(const BIGNUM *a)
946
240M
{
947
240M
    return a->top == 0;
948
240M
}
949
950
int BN_is_one(const BIGNUM *a)
951
55.6M
{
952
55.6M
    return BN_abs_is_word(a, 1) && !a->neg;
953
55.6M
}
954
955
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
956
231k
{
957
231k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
958
231k
}
959
960
int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w)
961
2.80k
{
962
2.80k
    int res, i;
963
2.80k
    const BN_ULONG *ap = a->d;
964
965
2.80k
    if (a->neg || a->top == 0)
966
0
        return 0;
967
968
2.80k
    res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
969
970
11.2k
    for (i = 1; i < a->top; i++)
971
8.42k
        res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
972
8.42k
                                       res, 0);
973
2.80k
    return res;
974
2.80k
}
975
976
int BN_is_odd(const BIGNUM *a)
977
42.7M
{
978
42.7M
    return (a->top > 0) && (a->d[0] & 1);
979
42.7M
}
980
981
int BN_is_negative(const BIGNUM *a)
982
6.45M
{
983
6.45M
    return (a->neg != 0);
984
6.45M
}
985
986
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
987
                     BN_CTX *ctx)
988
1.44M
{
989
1.44M
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
990
1.44M
}
991
992
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
993
10.3M
{
994
10.3M
    dest->d = b->d;
995
10.3M
    dest->top = b->top;
996
10.3M
    dest->dmax = b->dmax;
997
10.3M
    dest->neg = b->neg;
998
10.3M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
999
10.3M
                   | (b->flags & ~BN_FLG_MALLOCED)
1000
10.3M
                   | BN_FLG_STATIC_DATA | flags);
1001
10.3M
}
1002
1003
BN_GENCB *BN_GENCB_new(void)
1004
3.95k
{
1005
3.95k
    BN_GENCB *ret;
1006
1007
3.95k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
1008
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
1009
0
        return NULL;
1010
0
    }
1011
1012
3.95k
    return ret;
1013
3.95k
}
1014
1015
void BN_GENCB_free(BN_GENCB *cb)
1016
5.67k
{
1017
5.67k
    if (cb == NULL)
1018
1.71k
        return;
1019
3.95k
    OPENSSL_free(cb);
1020
3.95k
}
1021
1022
void BN_set_flags(BIGNUM *b, int n)
1023
1.56M
{
1024
1.56M
    b->flags |= n;
1025
1.56M
}
1026
1027
int BN_get_flags(const BIGNUM *b, int n)
1028
389M
{
1029
389M
    return b->flags & n;
1030
389M
}
1031
1032
/* Populate a BN_GENCB structure with an "old"-style callback */
1033
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
1034
                      void *cb_arg)
1035
0
{
1036
0
    BN_GENCB *tmp_gencb = gencb;
1037
0
    tmp_gencb->ver = 1;
1038
0
    tmp_gencb->arg = cb_arg;
1039
0
    tmp_gencb->cb.cb_1 = callback;
1040
0
}
1041
1042
/* Populate a BN_GENCB structure with a "new"-style callback */
1043
void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
1044
                  void *cb_arg)
1045
3.95k
{
1046
3.95k
    BN_GENCB *tmp_gencb = gencb;
1047
3.95k
    tmp_gencb->ver = 2;
1048
3.95k
    tmp_gencb->arg = cb_arg;
1049
3.95k
    tmp_gencb->cb.cb_2 = callback;
1050
3.95k
}
1051
1052
void *BN_GENCB_get_arg(BN_GENCB *cb)
1053
0
{
1054
0
    return cb->arg;
1055
0
}
1056
1057
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1058
1.35G
{
1059
1.35G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1060
1.35G
}
1061
1062
void bn_correct_top_consttime(BIGNUM *a)
1063
8.36k
{
1064
8.36k
    int j, atop;
1065
8.36k
    BN_ULONG limb;
1066
8.36k
    unsigned int mask;
1067
1068
543k
    for (j = 0, atop = 0; j < a->dmax; j++) {
1069
535k
        limb = a->d[j];
1070
535k
        limb |= 0 - limb;
1071
535k
        limb >>= BN_BITS2 - 1;
1072
535k
        limb = 0 - limb;
1073
535k
        mask = (unsigned int)limb;
1074
535k
        mask &= constant_time_msb(j - a->top);
1075
535k
        atop = constant_time_select_int(mask, j + 1, atop);
1076
535k
    }
1077
1078
8.36k
    mask = constant_time_eq_int(atop, 0);
1079
8.36k
    a->top = atop;
1080
8.36k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1081
8.36k
    a->flags &= ~BN_FLG_FIXED_TOP;
1082
8.36k
}
1083
1084
void bn_correct_top(BIGNUM *a)
1085
846M
{
1086
846M
    BN_ULONG *ftl;
1087
846M
    int tmp_top = a->top;
1088
1089
846M
    if (tmp_top > 0) {
1090
2.46G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1091
2.45G
            ftl--;
1092
2.45G
            if (*ftl != 0)
1093
843M
                break;
1094
2.45G
        }
1095
845M
        a->top = tmp_top;
1096
845M
    }
1097
846M
    if (a->top == 0)
1098
3.27M
        a->neg = 0;
1099
846M
    a->flags &= ~BN_FLG_FIXED_TOP;
1100
846M
    bn_pollute(a);
1101
846M
}