Coverage Report

Created: 2023-09-25 06:45

/src/openssl30/crypto/bn/bn_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 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
663k
{
84
663k
    static const BN_ULONG data_one = 1L;
85
663k
    static const BIGNUM const_one =
86
663k
        { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
87
88
663k
    return &const_one;
89
663k
}
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
104M
{
102
104M
    BN_ULONG x, mask;
103
104M
    int bits = (l != 0);
104
105
104M
#if BN_BITS2 > 32
106
104M
    x = l >> 32;
107
104M
    mask = (0 - x) & BN_MASK2;
108
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
109
104M
    bits += 32 & mask;
110
104M
    l ^= (x ^ l) & mask;
111
104M
#endif
112
113
104M
    x = l >> 16;
114
104M
    mask = (0 - x) & BN_MASK2;
115
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
116
104M
    bits += 16 & mask;
117
104M
    l ^= (x ^ l) & mask;
118
119
104M
    x = l >> 8;
120
104M
    mask = (0 - x) & BN_MASK2;
121
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
122
104M
    bits += 8 & mask;
123
104M
    l ^= (x ^ l) & mask;
124
125
104M
    x = l >> 4;
126
104M
    mask = (0 - x) & BN_MASK2;
127
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
128
104M
    bits += 4 & mask;
129
104M
    l ^= (x ^ l) & mask;
130
131
104M
    x = l >> 2;
132
104M
    mask = (0 - x) & BN_MASK2;
133
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
134
104M
    bits += 2 & mask;
135
104M
    l ^= (x ^ l) & mask;
136
137
104M
    x = l >> 1;
138
104M
    mask = (0 - x) & BN_MASK2;
139
104M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
140
104M
    bits += 1 & mask;
141
142
104M
    return bits;
143
104M
}
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
3.38M
{
155
3.38M
    int j, ret;
156
3.38M
    unsigned int mask, past_i;
157
3.38M
    int i = a->top - 1;
158
3.38M
    bn_check_top(a);
159
160
23.3M
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
19.9M
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
19.9M
        ret += BN_BITS2 & (~mask & ~past_i);
164
19.9M
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
19.9M
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
19.9M
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
3.38M
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
3.38M
    return ret & mask;
176
3.38M
}
177
178
int BN_num_bits(const BIGNUM *a)
179
21.1M
{
180
21.1M
    int i = a->top - 1;
181
21.1M
    bn_check_top(a);
182
183
21.1M
    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
3.38M
        return bn_num_bits_consttime(a);
194
3.38M
    }
195
196
17.7M
    if (BN_is_zero(a))
197
1.31M
        return 0;
198
199
16.4M
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
17.7M
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
32.9M
{
204
32.9M
    if (BN_get_flags(a, BN_FLG_SECURE))
205
676k
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
32.2M
    else if (clear != 0)
207
20.1M
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
12.0M
    else
209
12.0M
        OPENSSL_free(a->d);
210
32.9M
}
211
212
213
void BN_clear_free(BIGNUM *a)
214
15.1M
{
215
15.1M
    if (a == NULL)
216
2.80M
        return;
217
12.3M
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
218
11.3M
        bn_free_d(a, 1);
219
12.3M
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
220
1.14M
        OPENSSL_cleanse(a, sizeof(*a));
221
1.14M
        OPENSSL_free(a);
222
1.14M
    }
223
12.3M
}
224
225
void BN_free(BIGNUM *a)
226
18.1M
{
227
18.1M
    if (a == NULL)
228
6.00M
        return;
229
12.0M
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
230
12.0M
        bn_free_d(a, 0);
231
12.0M
    if (a->flags & BN_FLG_MALLOCED)
232
12.0M
        OPENSSL_free(a);
233
12.0M
}
234
235
void bn_init(BIGNUM *a)
236
24.8M
{
237
24.8M
    static BIGNUM nilbn;
238
239
24.8M
    *a = nilbn;
240
24.8M
    bn_check_top(a);
241
24.8M
}
242
243
BIGNUM *BN_new(void)
244
13.2M
{
245
13.2M
    BIGNUM *ret;
246
247
13.2M
    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
13.2M
    ret->flags = BN_FLG_MALLOCED;
252
13.2M
    bn_check_top(ret);
253
13.2M
    return ret;
254
13.2M
}
255
256
 BIGNUM *BN_secure_new(void)
257
707k
 {
258
707k
     BIGNUM *ret = BN_new();
259
707k
     if (ret != NULL)
260
707k
         ret->flags |= BN_FLG_SECURE;
261
707k
     return ret;
262
707k
 }
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
31.2M
{
268
31.2M
    BN_ULONG *a = NULL;
269
270
31.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
31.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
31.2M
    if (BN_get_flags(b, BN_FLG_SECURE))
279
676k
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
280
30.5M
    else
281
30.5M
        a = OPENSSL_zalloc(words * sizeof(*a));
282
31.2M
    if (a == NULL) {
283
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
284
0
        return NULL;
285
0
    }
286
287
31.2M
    assert(b->top <= words);
288
31.2M
    if (b->top > 0)
289
3.86M
        memcpy(a, b->d, sizeof(*a) * b->top);
290
291
31.2M
    return a;
292
31.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
31.2M
{
304
31.2M
    if (words > b->dmax) {
305
31.2M
        BN_ULONG *a = bn_expand_internal(b, words);
306
31.2M
        if (!a)
307
0
            return NULL;
308
31.2M
        if (b->d != NULL)
309
9.56M
            bn_free_d(b, 1);
310
31.2M
        b->d = a;
311
31.2M
        b->dmax = words;
312
31.2M
    }
313
314
31.2M
    return b;
315
31.2M
}
316
317
BIGNUM *BN_dup(const BIGNUM *a)
318
2.13M
{
319
2.13M
    BIGNUM *t;
320
321
2.13M
    if (a == NULL)
322
0
        return NULL;
323
2.13M
    bn_check_top(a);
324
325
2.13M
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
326
2.13M
    if (t == NULL)
327
0
        return NULL;
328
2.13M
    if (!BN_copy(t, a)) {
329
0
        BN_free(t);
330
0
        return NULL;
331
0
    }
332
2.13M
    bn_check_top(t);
333
2.13M
    return t;
334
2.13M
}
335
336
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
337
77.8M
{
338
77.8M
    int bn_words;
339
340
77.8M
    bn_check_top(b);
341
342
77.8M
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
343
344
77.8M
    if (a == b)
345
1
        return a;
346
77.8M
    if (bn_wexpand(a, bn_words) == NULL)
347
0
        return NULL;
348
349
77.8M
    if (b->top > 0)
350
76.7M
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
351
352
77.8M
    a->neg = b->neg;
353
77.8M
    a->top = b->top;
354
77.8M
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
355
77.8M
    bn_check_top(a);
356
77.8M
    return a;
357
77.8M
}
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
82.4k
{
400
82.4k
    if (a == NULL)
401
0
        return;
402
82.4k
    bn_check_top(a);
403
82.4k
    if (a->d != NULL)
404
8.93k
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
405
82.4k
    a->neg = 0;
406
82.4k
    a->top = 0;
407
82.4k
    a->flags &= ~BN_FLG_FIXED_TOP;
408
82.4k
}
409
410
BN_ULONG BN_get_word(const BIGNUM *a)
411
111
{
412
111
    if (a->top > 1)
413
3
        return BN_MASK2;
414
108
    else if (a->top == 1)
415
91
        return a->d[0];
416
    /* a->top == 0 */
417
17
    return 0;
418
111
}
419
420
int BN_set_word(BIGNUM *a, BN_ULONG w)
421
176M
{
422
176M
    bn_check_top(a);
423
176M
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
424
0
        return 0;
425
176M
    a->neg = 0;
426
176M
    a->d[0] = w;
427
176M
    a->top = (w ? 1 : 0);
428
176M
    a->flags &= ~BN_FLG_FIXED_TOP;
429
176M
    bn_check_top(a);
430
176M
    return 1;
431
176M
}
432
433
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
434
3.83M
{
435
3.83M
    unsigned int i, m;
436
3.83M
    unsigned int n;
437
3.83M
    BN_ULONG l;
438
3.83M
    BIGNUM *bn = NULL;
439
440
3.83M
    if (ret == NULL)
441
2.77M
        ret = bn = BN_new();
442
3.83M
    if (ret == NULL)
443
0
        return NULL;
444
3.83M
    bn_check_top(ret);
445
    /* Skip leading zero's. */
446
8.09M
    for ( ; len > 0 && *s == 0; s++, len--)
447
4.25M
        continue;
448
3.83M
    n = len;
449
3.83M
    if (n == 0) {
450
665k
        ret->top = 0;
451
665k
        return ret;
452
665k
    }
453
3.17M
    i = ((n - 1) / BN_BYTES) + 1;
454
3.17M
    m = ((n - 1) % (BN_BYTES));
455
3.17M
    if (bn_wexpand(ret, (int)i) == NULL) {
456
0
        BN_free(bn);
457
0
        return NULL;
458
0
    }
459
3.17M
    ret->top = i;
460
3.17M
    ret->neg = 0;
461
3.17M
    l = 0;
462
146M
    while (n--) {
463
143M
        l = (l << 8L) | *(s++);
464
143M
        if (m-- == 0) {
465
19.6M
            ret->d[--i] = l;
466
19.6M
            l = 0;
467
19.6M
            m = BN_BYTES - 1;
468
19.6M
        }
469
143M
    }
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.17M
    bn_correct_top(ret);
475
3.17M
    return ret;
476
3.17M
}
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
226k
{
484
226k
    int n;
485
226k
    size_t i, lasti, j, atop, mask;
486
226k
    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
226k
    n = BN_num_bytes(a);
494
226k
    if (tolen == -1) {
495
98.8k
        tolen = n;
496
127k
    } else if (tolen < n) {     /* uncommon/unlike case */
497
780
        BIGNUM temp = *a;
498
499
780
        bn_correct_top(&temp);
500
780
        n = BN_num_bytes(&temp);
501
780
        if (tolen < n)
502
780
            return -1;
503
780
    }
504
505
    /* Swipe through whole available data and don't give away padded zero. */
506
226k
    atop = a->dmax * BN_BYTES;
507
226k
    if (atop == 0) {
508
3.21k
        if (tolen != 0)
509
534
            memset(to, '\0', tolen);
510
3.21k
        return tolen;
511
3.21k
    }
512
513
222k
    lasti = atop - 1;
514
222k
    atop = a->top * BN_BYTES;
515
222k
    if (endianess == big)
516
119k
        to += tolen; /* start from the end of the buffer */
517
16.2M
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
518
16.0M
        unsigned char val;
519
16.0M
        l = a->d[i / BN_BYTES];
520
16.0M
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
521
16.0M
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
522
16.0M
        if (endianess == big)
523
5.34M
            *--to = val;
524
10.6M
        else
525
10.6M
            *to++ = val;
526
16.0M
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
527
16.0M
    }
528
529
222k
    return tolen;
530
226k
}
531
532
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
533
68.8k
{
534
68.8k
    if (tolen < 0)
535
0
        return -1;
536
68.8k
    return bn2binpad(a, to, tolen, big);
537
68.8k
}
538
539
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
540
310k
{
541
310k
    return bn2binpad(a, to, -1, big);
542
310k
}
543
544
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
545
106k
{
546
106k
    unsigned int i, m;
547
106k
    unsigned int n;
548
106k
    BN_ULONG l;
549
106k
    BIGNUM *bn = NULL;
550
551
106k
    if (ret == NULL)
552
92.5k
        ret = bn = BN_new();
553
106k
    if (ret == NULL)
554
0
        return NULL;
555
106k
    bn_check_top(ret);
556
106k
    s += len;
557
    /* Skip trailing zeroes. */
558
132k
    for ( ; len > 0 && s[-1] == 0; s--, len--)
559
26.0k
        continue;
560
106k
    n = len;
561
106k
    if (n == 0) {
562
462
        ret->top = 0;
563
462
        return ret;
564
462
    }
565
105k
    i = ((n - 1) / BN_BYTES) + 1;
566
105k
    m = ((n - 1) % (BN_BYTES));
567
105k
    if (bn_wexpand(ret, (int)i) == NULL) {
568
0
        BN_free(bn);
569
0
        return NULL;
570
0
    }
571
105k
    ret->top = i;
572
105k
    ret->neg = 0;
573
105k
    l = 0;
574
10.9M
    while (n--) {
575
10.8M
        s--;
576
10.8M
        l = (l << 8L) | *s;
577
10.8M
        if (m-- == 0) {
578
1.36M
            ret->d[--i] = l;
579
1.36M
            l = 0;
580
1.36M
            m = BN_BYTES - 1;
581
1.36M
        }
582
10.8M
    }
583
    /*
584
     * need to call this due to clear byte at top if avoiding having the top
585
     * bit set (-ve number)
586
     */
587
105k
    bn_correct_top(ret);
588
105k
    return ret;
589
105k
}
590
591
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
592
222k
{
593
222k
    if (tolen < 0)
594
0
        return -1;
595
222k
    return bn2binpad(a, to, tolen, little);
596
222k
}
597
598
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
599
211k
{
600
211k
    DECLARE_IS_ENDIAN;
601
602
211k
    if (IS_LITTLE_ENDIAN)
603
211k
        return BN_lebin2bn(s, len, ret);
604
0
    return BN_bin2bn(s, len, ret);
605
211k
}
606
607
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
608
211k
{
609
211k
    DECLARE_IS_ENDIAN;
610
611
211k
    if (IS_LITTLE_ENDIAN)
612
211k
        return BN_bn2lebinpad(a, to, tolen);
613
0
    return BN_bn2binpad(a, to, tolen);
614
211k
}
615
616
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
617
63.3M
{
618
63.3M
    int i;
619
63.3M
    BN_ULONG t1, t2, *ap, *bp;
620
621
63.3M
    bn_check_top(a);
622
63.3M
    bn_check_top(b);
623
624
63.3M
    i = a->top - b->top;
625
63.3M
    if (i != 0)
626
8.00M
        return i;
627
55.3M
    ap = a->d;
628
55.3M
    bp = b->d;
629
58.1M
    for (i = a->top - 1; i >= 0; i--) {
630
57.3M
        t1 = ap[i];
631
57.3M
        t2 = bp[i];
632
57.3M
        if (t1 != t2)
633
54.5M
            return ((t1 > t2) ? 1 : -1);
634
57.3M
    }
635
778k
    return 0;
636
55.3M
}
637
638
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
639
6.06M
{
640
6.06M
    int i;
641
6.06M
    int gt, lt;
642
6.06M
    BN_ULONG t1, t2;
643
644
6.06M
    if ((a == NULL) || (b == NULL)) {
645
0
        if (a != NULL)
646
0
            return -1;
647
0
        else if (b != NULL)
648
0
            return 1;
649
0
        else
650
0
            return 0;
651
0
    }
652
653
6.06M
    bn_check_top(a);
654
6.06M
    bn_check_top(b);
655
656
6.06M
    if (a->neg != b->neg) {
657
218
        if (a->neg)
658
4
            return -1;
659
214
        else
660
214
            return 1;
661
218
    }
662
6.06M
    if (a->neg == 0) {
663
6.06M
        gt = 1;
664
6.06M
        lt = -1;
665
6.06M
    } else {
666
4.87k
        gt = -1;
667
4.87k
        lt = 1;
668
4.87k
    }
669
670
6.06M
    if (a->top > b->top)
671
1.03M
        return gt;
672
5.03M
    if (a->top < b->top)
673
601k
        return lt;
674
14.7M
    for (i = a->top - 1; i >= 0; i--) {
675
13.9M
        t1 = a->d[i];
676
13.9M
        t2 = b->d[i];
677
13.9M
        if (t1 > t2)
678
1.59M
            return gt;
679
12.3M
        if (t1 < t2)
680
2.05M
            return lt;
681
12.3M
    }
682
774k
    return 0;
683
4.42M
}
684
685
int BN_set_bit(BIGNUM *a, int n)
686
1.06M
{
687
1.06M
    int i, j, k;
688
689
1.06M
    if (n < 0)
690
0
        return 0;
691
692
1.06M
    i = n / BN_BITS2;
693
1.06M
    j = n % BN_BITS2;
694
1.06M
    if (a->top <= i) {
695
1.06M
        if (bn_wexpand(a, i + 1) == NULL)
696
0
            return 0;
697
8.81M
        for (k = a->top; k < i + 1; k++)
698
7.74M
            a->d[k] = 0;
699
1.06M
        a->top = i + 1;
700
1.06M
        a->flags &= ~BN_FLG_FIXED_TOP;
701
1.06M
    }
702
703
1.06M
    a->d[i] |= (((BN_ULONG)1) << j);
704
1.06M
    bn_check_top(a);
705
1.06M
    return 1;
706
1.06M
}
707
708
int BN_clear_bit(BIGNUM *a, int n)
709
228
{
710
228
    int i, j;
711
712
228
    bn_check_top(a);
713
228
    if (n < 0)
714
0
        return 0;
715
716
228
    i = n / BN_BITS2;
717
228
    j = n % BN_BITS2;
718
228
    if (a->top <= i)
719
0
        return 0;
720
721
228
    a->d[i] &= (~(((BN_ULONG)1) << j));
722
228
    bn_correct_top(a);
723
228
    return 1;
724
228
}
725
726
int BN_is_bit_set(const BIGNUM *a, int n)
727
130M
{
728
130M
    int i, j;
729
730
130M
    bn_check_top(a);
731
130M
    if (n < 0)
732
2.39k
        return 0;
733
130M
    i = n / BN_BITS2;
734
130M
    j = n % BN_BITS2;
735
130M
    if (a->top <= i)
736
3.80k
        return 0;
737
130M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
738
130M
}
739
740
int BN_mask_bits(BIGNUM *a, int n)
741
0
{
742
0
    int b, w;
743
744
0
    bn_check_top(a);
745
0
    if (n < 0)
746
0
        return 0;
747
748
0
    w = n / BN_BITS2;
749
0
    b = n % BN_BITS2;
750
0
    if (w >= a->top)
751
0
        return 0;
752
0
    if (b == 0)
753
0
        a->top = w;
754
0
    else {
755
0
        a->top = w + 1;
756
0
        a->d[w] &= ~(BN_MASK2 << b);
757
0
    }
758
0
    bn_correct_top(a);
759
0
    return 1;
760
0
}
761
762
void BN_set_negative(BIGNUM *a, int b)
763
896k
{
764
896k
    if (b && !BN_is_zero(a))
765
280k
        a->neg = 1;
766
616k
    else
767
616k
        a->neg = 0;
768
896k
}
769
770
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
771
80.0M
{
772
80.0M
    int i;
773
80.0M
    BN_ULONG aa, bb;
774
775
80.0M
    if (n == 0)
776
0
        return 0;
777
778
80.0M
    aa = a[n - 1];
779
80.0M
    bb = b[n - 1];
780
80.0M
    if (aa != bb)
781
75.5M
        return ((aa > bb) ? 1 : -1);
782
46.7M
    for (i = n - 2; i >= 0; i--) {
783
44.5M
        aa = a[i];
784
44.5M
        bb = b[i];
785
44.5M
        if (aa != bb)
786
2.38M
            return ((aa > bb) ? 1 : -1);
787
44.5M
    }
788
2.20M
    return 0;
789
4.58M
}
790
791
/*
792
 * Here follows a specialised variants of bn_cmp_words().  It has the
793
 * capability of performing the operation on arrays of different sizes. The
794
 * sizes of those arrays is expressed through cl, which is the common length
795
 * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
796
 * two lengths, calculated as len(a)-len(b). All lengths are the number of
797
 * BN_ULONGs...
798
 */
799
800
int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
801
76.4M
{
802
76.4M
    int n, i;
803
76.4M
    n = cl - 1;
804
805
76.4M
    if (dl < 0) {
806
774k
        for (i = dl; i < 0; i++) {
807
746k
            if (b[n - i] != 0)
808
338k
                return -1;      /* a < b */
809
746k
        }
810
367k
    }
811
76.1M
    if (dl > 0) {
812
2.07M
        for (i = dl; i > 0; i--) {
813
2.00M
            if (a[n + i] != 0)
814
300k
                return 1;       /* a > b */
815
2.00M
        }
816
369k
    }
817
75.8M
    return bn_cmp_words(a, b, cl);
818
76.1M
}
819
820
/*-
821
 * Constant-time conditional swap of a and b.
822
 * a and b are swapped if condition is not 0.
823
 * nwords is the number of words to swap.
824
 * Assumes that at least nwords are allocated in both a and b.
825
 * Assumes that no more than nwords are used by either a or b.
826
 */
827
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
828
1.72M
{
829
1.72M
    BN_ULONG t;
830
1.72M
    int i;
831
832
1.72M
    if (a == b)
833
0
        return;
834
835
1.72M
    bn_wcheck_size(a, nwords);
836
1.72M
    bn_wcheck_size(b, nwords);
837
838
1.72M
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
839
840
1.72M
    t = (a->top ^ b->top) & condition;
841
1.72M
    a->top ^= t;
842
1.72M
    b->top ^= t;
843
844
1.72M
    t = (a->neg ^ b->neg) & condition;
845
1.72M
    a->neg ^= t;
846
1.72M
    b->neg ^= t;
847
848
    /*-
849
     * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
850
     * is actually to treat it as it's read-only data, and some (if not most)
851
     * of it does reside in read-only segment. In other words observation of
852
     * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
853
     * condition. It would either cause SEGV or effectively cause data
854
     * corruption.
855
     *
856
     * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
857
     * preserved.
858
     *
859
     * BN_FLG_SECURE: must be preserved, because it determines how x->d was
860
     * allocated and hence how to free it.
861
     *
862
     * BN_FLG_CONSTTIME: sufficient to mask and swap
863
     *
864
     * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
865
     * the data, so the d array may be padded with additional 0 values (i.e.
866
     * top could be greater than the minimal value that it could be). We should
867
     * be swapping it
868
     */
869
870
1.72M
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
871
872
1.72M
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
873
1.72M
    a->flags ^= t;
874
1.72M
    b->flags ^= t;
875
876
    /* conditionally swap the data */
877
10.1M
    for (i = 0; i < nwords; i++) {
878
8.46M
        t = (a->d[i] ^ b->d[i]) & condition;
879
8.46M
        a->d[i] ^= t;
880
8.46M
        b->d[i] ^= t;
881
8.46M
    }
882
1.72M
}
883
884
#undef BN_CONSTTIME_SWAP_FLAGS
885
886
/* Bits of security, see SP800-57 */
887
888
int BN_security_bits(int L, int N)
889
67.6k
{
890
67.6k
    int secbits, bits;
891
67.6k
    if (L >= 15360)
892
289
        secbits = 256;
893
67.3k
    else if (L >= 7680)
894
553
        secbits = 192;
895
66.8k
    else if (L >= 3072)
896
1.79k
        secbits = 128;
897
65.0k
    else if (L >= 2048)
898
10.0k
        secbits = 112;
899
54.9k
    else if (L >= 1024)
900
33.5k
        secbits = 80;
901
21.3k
    else
902
21.3k
        return 0;
903
46.2k
    if (N == -1)
904
13.7k
        return secbits;
905
32.5k
    bits = N / 2;
906
32.5k
    if (bits < 80)
907
2.81k
        return 0;
908
29.7k
    return bits >= secbits ? secbits : bits;
909
32.5k
}
910
911
void BN_zero_ex(BIGNUM *a)
912
415M
{
913
415M
    a->neg = 0;
914
415M
    a->top = 0;
915
415M
    a->flags &= ~BN_FLG_FIXED_TOP;
916
415M
}
917
918
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
919
49.5M
{
920
49.5M
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
921
49.5M
}
922
923
int BN_is_zero(const BIGNUM *a)
924
185M
{
925
185M
    return a->top == 0;
926
185M
}
927
928
int BN_is_one(const BIGNUM *a)
929
48.8M
{
930
48.8M
    return BN_abs_is_word(a, 1) && !a->neg;
931
48.8M
}
932
933
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
934
115k
{
935
115k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
936
115k
}
937
938
int BN_is_odd(const BIGNUM *a)
939
30.0M
{
940
30.0M
    return (a->top > 0) && (a->d[0] & 1);
941
30.0M
}
942
943
int BN_is_negative(const BIGNUM *a)
944
3.32M
{
945
3.32M
    return (a->neg != 0);
946
3.32M
}
947
948
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
949
                     BN_CTX *ctx)
950
861k
{
951
861k
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
952
861k
}
953
954
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
955
6.35M
{
956
6.35M
    dest->d = b->d;
957
6.35M
    dest->top = b->top;
958
6.35M
    dest->dmax = b->dmax;
959
6.35M
    dest->neg = b->neg;
960
6.35M
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
961
6.35M
                   | (b->flags & ~BN_FLG_MALLOCED)
962
6.35M
                   | BN_FLG_STATIC_DATA | flags);
963
6.35M
}
964
965
BN_GENCB *BN_GENCB_new(void)
966
1.76k
{
967
1.76k
    BN_GENCB *ret;
968
969
1.76k
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
970
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
971
0
        return NULL;
972
0
    }
973
974
1.76k
    return ret;
975
1.76k
}
976
977
void BN_GENCB_free(BN_GENCB *cb)
978
2.41k
{
979
2.41k
    if (cb == NULL)
980
649
        return;
981
1.76k
    OPENSSL_free(cb);
982
1.76k
}
983
984
void BN_set_flags(BIGNUM *b, int n)
985
689k
{
986
689k
    b->flags |= n;
987
689k
}
988
989
int BN_get_flags(const BIGNUM *b, int n)
990
214M
{
991
214M
    return b->flags & n;
992
214M
}
993
994
/* Populate a BN_GENCB structure with an "old"-style callback */
995
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
996
                      void *cb_arg)
997
0
{
998
0
    BN_GENCB *tmp_gencb = gencb;
999
0
    tmp_gencb->ver = 1;
1000
0
    tmp_gencb->arg = cb_arg;
1001
0
    tmp_gencb->cb.cb_1 = callback;
1002
0
}
1003
1004
/* Populate a BN_GENCB structure with a "new"-style callback */
1005
void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
1006
                  void *cb_arg)
1007
1.76k
{
1008
1.76k
    BN_GENCB *tmp_gencb = gencb;
1009
1.76k
    tmp_gencb->ver = 2;
1010
1.76k
    tmp_gencb->arg = cb_arg;
1011
1.76k
    tmp_gencb->cb.cb_2 = callback;
1012
1.76k
}
1013
1014
void *BN_GENCB_get_arg(BN_GENCB *cb)
1015
0
{
1016
0
    return cb->arg;
1017
0
}
1018
1019
BIGNUM *bn_wexpand(BIGNUM *a, int words)
1020
1.12G
{
1021
1.12G
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1022
1.12G
}
1023
1024
void bn_correct_top_consttime(BIGNUM *a)
1025
5.12k
{
1026
5.12k
    int j, atop;
1027
5.12k
    BN_ULONG limb;
1028
5.12k
    unsigned int mask;
1029
1030
332k
    for (j = 0, atop = 0; j < a->dmax; j++) {
1031
327k
        limb = a->d[j];
1032
327k
        limb |= 0 - limb;
1033
327k
        limb >>= BN_BITS2 - 1;
1034
327k
        limb = 0 - limb;
1035
327k
        mask = (unsigned int)limb;
1036
327k
        mask &= constant_time_msb(j - a->top);
1037
327k
        atop = constant_time_select_int(mask, j + 1, atop);
1038
327k
    }
1039
1040
5.12k
    mask = constant_time_eq_int(atop, 0);
1041
5.12k
    a->top = atop;
1042
5.12k
    a->neg = constant_time_select_int(mask, 0, a->neg);
1043
5.12k
    a->flags &= ~BN_FLG_FIXED_TOP;
1044
5.12k
}
1045
1046
void bn_correct_top(BIGNUM *a)
1047
723M
{
1048
723M
    BN_ULONG *ftl;
1049
723M
    int tmp_top = a->top;
1050
1051
723M
    if (tmp_top > 0) {
1052
2.15G
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1053
2.15G
            ftl--;
1054
2.15G
            if (*ftl != 0)
1055
721M
                break;
1056
2.15G
        }
1057
722M
        a->top = tmp_top;
1058
722M
    }
1059
723M
    if (a->top == 0)
1060
2.27M
        a->neg = 0;
1061
723M
    a->flags &= ~BN_FLG_FIXED_TOP;
1062
723M
    bn_pollute(a);
1063
723M
}