Coverage Report

Created: 2023-09-25 06:41

/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
4.50k
{
84
4.50k
    static const BN_ULONG data_one = 1L;
85
4.50k
    static const BIGNUM const_one =
86
4.50k
        { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
87
88
4.50k
    return &const_one;
89
4.50k
}
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
1.00M
{
102
1.00M
    BN_ULONG x, mask;
103
1.00M
    int bits = (l != 0);
104
105
1.00M
#if BN_BITS2 > 32
106
1.00M
    x = l >> 32;
107
1.00M
    mask = (0 - x) & BN_MASK2;
108
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
109
1.00M
    bits += 32 & mask;
110
1.00M
    l ^= (x ^ l) & mask;
111
1.00M
#endif
112
113
1.00M
    x = l >> 16;
114
1.00M
    mask = (0 - x) & BN_MASK2;
115
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
116
1.00M
    bits += 16 & mask;
117
1.00M
    l ^= (x ^ l) & mask;
118
119
1.00M
    x = l >> 8;
120
1.00M
    mask = (0 - x) & BN_MASK2;
121
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
122
1.00M
    bits += 8 & mask;
123
1.00M
    l ^= (x ^ l) & mask;
124
125
1.00M
    x = l >> 4;
126
1.00M
    mask = (0 - x) & BN_MASK2;
127
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
128
1.00M
    bits += 4 & mask;
129
1.00M
    l ^= (x ^ l) & mask;
130
131
1.00M
    x = l >> 2;
132
1.00M
    mask = (0 - x) & BN_MASK2;
133
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
134
1.00M
    bits += 2 & mask;
135
1.00M
    l ^= (x ^ l) & mask;
136
137
1.00M
    x = l >> 1;
138
1.00M
    mask = (0 - x) & BN_MASK2;
139
1.00M
    mask = (0 - (mask >> (BN_BITS2 - 1)));
140
1.00M
    bits += 1 & mask;
141
142
1.00M
    return bits;
143
1.00M
}
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
28
{
155
28
    int j, ret;
156
28
    unsigned int mask, past_i;
157
28
    int i = a->top - 1;
158
28
    bn_check_top(a);
159
160
217
    for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
161
189
        mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
162
163
189
        ret += BN_BITS2 & (~mask & ~past_i);
164
189
        ret += BN_num_bits_word(a->d[j]) & mask;
165
166
189
        past_i |= mask; /* past_i will become 0xff..ff after i==j */
167
189
    }
168
169
    /*
170
     * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
171
     * final result.
172
     */
173
28
    mask = ~(constant_time_eq_int(i, ((int)-1)));
174
175
28
    return ret & mask;
176
28
}
177
178
int BN_num_bits(const BIGNUM *a)
179
423k
{
180
423k
    int i = a->top - 1;
181
423k
    bn_check_top(a);
182
183
423k
    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
28
        return bn_num_bits_consttime(a);
194
28
    }
195
196
423k
    if (BN_is_zero(a))
197
1.52k
        return 0;
198
199
422k
    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
200
423k
}
201
202
static void bn_free_d(BIGNUM *a, int clear)
203
187k
{
204
187k
    if (BN_get_flags(a, BN_FLG_SECURE))
205
1.11k
        OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
206
186k
    else if (clear != 0)
207
141k
        OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
208
44.4k
    else
209
44.4k
        OPENSSL_free(a->d);
210
187k
}
211
212
213
void BN_clear_free(BIGNUM *a)
214
112k
{
215
112k
    if (a == NULL)
216
18.3k
        return;
217
94.1k
    if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
218
87.1k
        bn_free_d(a, 1);
219
94.1k
    if (BN_get_flags(a, BN_FLG_MALLOCED)) {
220
9.92k
        OPENSSL_cleanse(a, sizeof(*a));
221
9.92k
        OPENSSL_free(a);
222
9.92k
    }
223
94.1k
}
224
225
void BN_free(BIGNUM *a)
226
349k
{
227
349k
    if (a == NULL)
228
303k
        return;
229
46.0k
    if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
230
44.4k
        bn_free_d(a, 0);
231
46.0k
    if (a->flags & BN_FLG_MALLOCED)
232
44.4k
        OPENSSL_free(a);
233
46.0k
}
234
235
void bn_init(BIGNUM *a)
236
178k
{
237
178k
    static BIGNUM nilbn;
238
239
178k
    *a = nilbn;
240
178k
    bn_check_top(a);
241
178k
}
242
243
BIGNUM *BN_new(void)
244
54.4k
{
245
54.4k
    BIGNUM *ret;
246
247
54.4k
    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
54.4k
    ret->flags = BN_FLG_MALLOCED;
252
54.4k
    bn_check_top(ret);
253
54.4k
    return ret;
254
54.4k
}
255
256
 BIGNUM *BN_secure_new(void)
257
764
 {
258
764
     BIGNUM *ret = BN_new();
259
764
     if (ret != NULL)
260
764
         ret->flags |= BN_FLG_SECURE;
261
764
     return ret;
262
764
 }
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
185k
{
268
185k
    BN_ULONG *a = NULL;
269
270
185k
    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
185k
    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
185k
    if (BN_get_flags(b, BN_FLG_SECURE))
279
1.11k
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
280
184k
    else
281
184k
        a = OPENSSL_zalloc(words * sizeof(*a));
282
185k
    if (a == NULL) {
283
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
284
0
        return NULL;
285
0
    }
286
287
185k
    assert(b->top <= words);
288
185k
    if (b->top > 0)
289
20.1k
        memcpy(a, b->d, sizeof(*a) * b->top);
290
291
185k
    return a;
292
185k
}
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
185k
{
304
185k
    if (words > b->dmax) {
305
185k
        BN_ULONG *a = bn_expand_internal(b, words);
306
185k
        if (!a)
307
0
            return NULL;
308
185k
        if (b->d != NULL)
309
55.6k
            bn_free_d(b, 1);
310
185k
        b->d = a;
311
185k
        b->dmax = words;
312
185k
    }
313
314
185k
    return b;
315
185k
}
316
317
BIGNUM *BN_dup(const BIGNUM *a)
318
1.82k
{
319
1.82k
    BIGNUM *t;
320
321
1.82k
    if (a == NULL)
322
0
        return NULL;
323
1.82k
    bn_check_top(a);
324
325
1.82k
    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
326
1.82k
    if (t == NULL)
327
0
        return NULL;
328
1.82k
    if (!BN_copy(t, a)) {
329
0
        BN_free(t);
330
0
        return NULL;
331
0
    }
332
1.82k
    bn_check_top(t);
333
1.82k
    return t;
334
1.82k
}
335
336
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
337
655k
{
338
655k
    int bn_words;
339
340
655k
    bn_check_top(b);
341
342
655k
    bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
343
344
655k
    if (a == b)
345
0
        return a;
346
655k
    if (bn_wexpand(a, bn_words) == NULL)
347
0
        return NULL;
348
349
655k
    if (b->top > 0)
350
653k
        memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
351
352
655k
    a->neg = b->neg;
353
655k
    a->top = b->top;
354
655k
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
355
655k
    bn_check_top(a);
356
655k
    return a;
357
655k
}
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
589
{
400
589
    if (a == NULL)
401
0
        return;
402
589
    bn_check_top(a);
403
589
    if (a->d != NULL)
404
589
        OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
405
589
    a->neg = 0;
406
589
    a->top = 0;
407
589
    a->flags &= ~BN_FLG_FIXED_TOP;
408
589
}
409
410
BN_ULONG BN_get_word(const BIGNUM *a)
411
0
{
412
0
    if (a->top > 1)
413
0
        return BN_MASK2;
414
0
    else if (a->top == 1)
415
0
        return a->d[0];
416
    /* a->top == 0 */
417
0
    return 0;
418
0
}
419
420
int BN_set_word(BIGNUM *a, BN_ULONG w)
421
20.6k
{
422
20.6k
    bn_check_top(a);
423
20.6k
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
424
0
        return 0;
425
20.6k
    a->neg = 0;
426
20.6k
    a->d[0] = w;
427
20.6k
    a->top = (w ? 1 : 0);
428
20.6k
    a->flags &= ~BN_FLG_FIXED_TOP;
429
20.6k
    bn_check_top(a);
430
20.6k
    return 1;
431
20.6k
}
432
433
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
434
21.5k
{
435
21.5k
    unsigned int i, m;
436
21.5k
    unsigned int n;
437
21.5k
    BN_ULONG l;
438
21.5k
    BIGNUM *bn = NULL;
439
440
21.5k
    if (ret == NULL)
441
10.6k
        ret = bn = BN_new();
442
21.5k
    if (ret == NULL)
443
0
        return NULL;
444
21.5k
    bn_check_top(ret);
445
    /* Skip leading zero's. */
446
75.7k
    for ( ; len > 0 && *s == 0; s++, len--)
447
54.2k
        continue;
448
21.5k
    n = len;
449
21.5k
    if (n == 0) {
450
599
        ret->top = 0;
451
599
        return ret;
452
599
    }
453
20.9k
    i = ((n - 1) / BN_BYTES) + 1;
454
20.9k
    m = ((n - 1) % (BN_BYTES));
455
20.9k
    if (bn_wexpand(ret, (int)i) == NULL) {
456
0
        BN_free(bn);
457
0
        return NULL;
458
0
    }
459
20.9k
    ret->top = i;
460
20.9k
    ret->neg = 0;
461
20.9k
    l = 0;
462
1.54M
    while (n--) {
463
1.52M
        l = (l << 8L) | *(s++);
464
1.52M
        if (m-- == 0) {
465
197k
            ret->d[--i] = l;
466
197k
            l = 0;
467
197k
            m = BN_BYTES - 1;
468
197k
        }
469
1.52M
    }
470
    /*
471
     * need to call this due to clear byte at top if avoiding having the top
472
     * bit set (-ve number)
473
     */
474
20.9k
    bn_correct_top(ret);
475
20.9k
    return ret;
476
20.9k
}
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
6.94k
{
484
6.94k
    int n;
485
6.94k
    size_t i, lasti, j, atop, mask;
486
6.94k
    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
6.94k
    n = BN_num_bytes(a);
494
6.94k
    if (tolen == -1) {
495
117
        tolen = n;
496
6.83k
    } else if (tolen < n) {     /* uncommon/unlike case */
497
0
        BIGNUM temp = *a;
498
499
0
        bn_correct_top(&temp);
500
0
        n = BN_num_bytes(&temp);
501
0
        if (tolen < n)
502
0
            return -1;
503
0
    }
504
505
    /* Swipe through whole available data and don't give away padded zero. */
506
6.94k
    atop = a->dmax * BN_BYTES;
507
6.94k
    if (atop == 0) {
508
415
        if (tolen != 0)
509
415
            memset(to, '\0', tolen);
510
415
        return tolen;
511
415
    }
512
513
6.53k
    lasti = atop - 1;
514
6.53k
    atop = a->top * BN_BYTES;
515
6.53k
    if (endianess == big)
516
3.13k
        to += tolen; /* start from the end of the buffer */
517
952k
    for (i = 0, j = 0; j < (size_t)tolen; j++) {
518
946k
        unsigned char val;
519
946k
        l = a->d[i / BN_BYTES];
520
946k
        mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
521
946k
        val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
522
946k
        if (endianess == big)
523
504k
            *--to = val;
524
441k
        else
525
441k
            *to++ = val;
526
946k
        i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
527
946k
    }
528
529
6.53k
    return tolen;
530
6.94k
}
531
532
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
533
3.02k
{
534
3.02k
    if (tolen < 0)
535
0
        return -1;
536
3.02k
    return bn2binpad(a, to, tolen, big);
537
3.02k
}
538
539
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
540
117
{
541
117
    return bn2binpad(a, to, -1, big);
542
117
}
543
544
BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
545
3.85k
{
546
3.85k
    unsigned int i, m;
547
3.85k
    unsigned int n;
548
3.85k
    BN_ULONG l;
549
3.85k
    BIGNUM *bn = NULL;
550
551
3.85k
    if (ret == NULL)
552
3.74k
        ret = bn = BN_new();
553
3.85k
    if (ret == NULL)
554
0
        return NULL;
555
3.85k
    bn_check_top(ret);
556
3.85k
    s += len;
557
    /* Skip trailing zeroes. */
558
5.22k
    for ( ; len > 0 && s[-1] == 0; s--, len--)
559
1.37k
        continue;
560
3.85k
    n = len;
561
3.85k
    if (n == 0) {
562
411
        ret->top = 0;
563
411
        return ret;
564
411
    }
565
3.44k
    i = ((n - 1) / BN_BYTES) + 1;
566
3.44k
    m = ((n - 1) % (BN_BYTES));
567
3.44k
    if (bn_wexpand(ret, (int)i) == NULL) {
568
0
        BN_free(bn);
569
0
        return NULL;
570
0
    }
571
3.44k
    ret->top = i;
572
3.44k
    ret->neg = 0;
573
3.44k
    l = 0;
574
446k
    while (n--) {
575
443k
        s--;
576
443k
        l = (l << 8L) | *s;
577
443k
        if (m-- == 0) {
578
56.5k
            ret->d[--i] = l;
579
56.5k
            l = 0;
580
56.5k
            m = BN_BYTES - 1;
581
56.5k
        }
582
443k
    }
583
    /*
584
     * need to call this due to clear byte at top if avoiding having the top
585
     * bit set (-ve number)
586
     */
587
3.44k
    bn_correct_top(ret);
588
3.44k
    return ret;
589
3.44k
}
590
591
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
592
3.80k
{
593
3.80k
    if (tolen < 0)
594
0
        return -1;
595
3.80k
    return bn2binpad(a, to, tolen, little);
596
3.80k
}
597
598
BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
599
3.74k
{
600
3.74k
    DECLARE_IS_ENDIAN;
601
602
3.74k
    if (IS_LITTLE_ENDIAN)
603
3.74k
        return BN_lebin2bn(s, len, ret);
604
0
    return BN_bin2bn(s, len, ret);
605
3.74k
}
606
607
int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
608
3.74k
{
609
3.74k
    DECLARE_IS_ENDIAN;
610
611
3.74k
    if (IS_LITTLE_ENDIAN)
612
3.74k
        return BN_bn2lebinpad(a, to, tolen);
613
0
    return BN_bn2binpad(a, to, tolen);
614
3.74k
}
615
616
int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
617
717k
{
618
717k
    int i;
619
717k
    BN_ULONG t1, t2, *ap, *bp;
620
621
717k
    bn_check_top(a);
622
717k
    bn_check_top(b);
623
624
717k
    i = a->top - b->top;
625
717k
    if (i != 0)
626
152k
        return i;
627
565k
    ap = a->d;
628
565k
    bp = b->d;
629
577k
    for (i = a->top - 1; i >= 0; i--) {
630
571k
        t1 = ap[i];
631
571k
        t2 = bp[i];
632
571k
        if (t1 != t2)
633
560k
            return ((t1 > t2) ? 1 : -1);
634
571k
    }
635
5.77k
    return 0;
636
565k
}
637
638
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
639
276k
{
640
276k
    int i;
641
276k
    int gt, lt;
642
276k
    BN_ULONG t1, t2;
643
644
276k
    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
276k
    bn_check_top(a);
654
276k
    bn_check_top(b);
655
656
276k
    if (a->neg != b->neg) {
657
41
        if (a->neg)
658
0
            return -1;
659
41
        else
660
41
            return 1;
661
41
    }
662
276k
    if (a->neg == 0) {
663
276k
        gt = 1;
664
276k
        lt = -1;
665
276k
    } else {
666
0
        gt = -1;
667
0
        lt = 1;
668
0
    }
669
670
276k
    if (a->top > b->top)
671
142k
        return gt;
672
133k
    if (a->top < b->top)
673
16.4k
        return lt;
674
151k
    for (i = a->top - 1; i >= 0; i--) {
675
149k
        t1 = a->d[i];
676
149k
        t2 = b->d[i];
677
149k
        if (t1 > t2)
678
1.45k
            return gt;
679
147k
        if (t1 < t2)
680
112k
            return lt;
681
147k
    }
682
2.50k
    return 0;
683
116k
}
684
685
int BN_set_bit(BIGNUM *a, int n)
686
8.18k
{
687
8.18k
    int i, j, k;
688
689
8.18k
    if (n < 0)
690
0
        return 0;
691
692
8.18k
    i = n / BN_BITS2;
693
8.18k
    j = n % BN_BITS2;
694
8.18k
    if (a->top <= i) {
695
8.18k
        if (bn_wexpand(a, i + 1) == NULL)
696
0
            return 0;
697
188k
        for (k = a->top; k < i + 1; k++)
698
180k
            a->d[k] = 0;
699
8.18k
        a->top = i + 1;
700
8.18k
        a->flags &= ~BN_FLG_FIXED_TOP;
701
8.18k
    }
702
703
8.18k
    a->d[i] |= (((BN_ULONG)1) << j);
704
8.18k
    bn_check_top(a);
705
8.18k
    return 1;
706
8.18k
}
707
708
int BN_clear_bit(BIGNUM *a, int n)
709
69
{
710
69
    int i, j;
711
712
69
    bn_check_top(a);
713
69
    if (n < 0)
714
0
        return 0;
715
716
69
    i = n / BN_BITS2;
717
69
    j = n % BN_BITS2;
718
69
    if (a->top <= i)
719
0
        return 0;
720
721
69
    a->d[i] &= (~(((BN_ULONG)1) << j));
722
69
    bn_correct_top(a);
723
69
    return 1;
724
69
}
725
726
int BN_is_bit_set(const BIGNUM *a, int n)
727
2.40M
{
728
2.40M
    int i, j;
729
730
2.40M
    bn_check_top(a);
731
2.40M
    if (n < 0)
732
814
        return 0;
733
2.40M
    i = n / BN_BITS2;
734
2.40M
    j = n % BN_BITS2;
735
2.40M
    if (a->top <= i)
736
1.12k
        return 0;
737
2.40M
    return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
738
2.40M
}
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
1.91k
{
764
1.91k
    if (b && !BN_is_zero(a))
765
132
        a->neg = 1;
766
1.78k
    else
767
1.78k
        a->neg = 0;
768
1.91k
}
769
770
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
771
116k
{
772
116k
    int i;
773
116k
    BN_ULONG aa, bb;
774
775
116k
    if (n == 0)
776
0
        return 0;
777
778
116k
    aa = a[n - 1];
779
116k
    bb = b[n - 1];
780
116k
    if (aa != bb)
781
108k
        return ((aa > bb) ? 1 : -1);
782
28.3k
    for (i = n - 2; i >= 0; i--) {
783
27.0k
        aa = a[i];
784
27.0k
        bb = b[i];
785
27.0k
        if (aa != bb)
786
6.79k
            return ((aa > bb) ? 1 : -1);
787
27.0k
    }
788
1.30k
    return 0;
789
8.10k
}
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
104k
{
802
104k
    int n, i;
803
104k
    n = cl - 1;
804
805
104k
    if (dl < 0) {
806
14.4k
        for (i = dl; i < 0; i++) {
807
13.2k
            if (b[n - i] != 0)
808
10.4k
                return -1;      /* a < b */
809
13.2k
        }
810
11.5k
    }
811
93.9k
    if (dl > 0) {
812
12.0k
        for (i = dl; i > 0; i--) {
813
11.6k
            if (a[n + i] != 0)
814
9.75k
                return 1;       /* a > b */
815
11.6k
        }
816
10.1k
    }
817
84.1k
    return bn_cmp_words(a, b, cl);
818
93.9k
}
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
8.09k
{
829
8.09k
    BN_ULONG t;
830
8.09k
    int i;
831
832
8.09k
    if (a == b)
833
0
        return;
834
835
8.09k
    bn_wcheck_size(a, nwords);
836
8.09k
    bn_wcheck_size(b, nwords);
837
838
8.09k
    condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
839
840
8.09k
    t = (a->top ^ b->top) & condition;
841
8.09k
    a->top ^= t;
842
8.09k
    b->top ^= t;
843
844
8.09k
    t = (a->neg ^ b->neg) & condition;
845
8.09k
    a->neg ^= t;
846
8.09k
    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
8.09k
#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
871
872
8.09k
    t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
873
8.09k
    a->flags ^= t;
874
8.09k
    b->flags ^= t;
875
876
    /* conditionally swap the data */
877
56.6k
    for (i = 0; i < nwords; i++) {
878
48.5k
        t = (a->d[i] ^ b->d[i]) & condition;
879
48.5k
        a->d[i] ^= t;
880
48.5k
        b->d[i] ^= t;
881
48.5k
    }
882
8.09k
}
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
2.72k
{
890
2.72k
    int secbits, bits;
891
2.72k
    if (L >= 15360)
892
0
        secbits = 256;
893
2.72k
    else if (L >= 7680)
894
191
        secbits = 192;
895
2.52k
    else if (L >= 3072)
896
328
        secbits = 128;
897
2.20k
    else if (L >= 2048)
898
610
        secbits = 112;
899
1.59k
    else if (L >= 1024)
900
1.22k
        secbits = 80;
901
365
    else
902
365
        return 0;
903
2.35k
    if (N == -1)
904
1.42k
        return secbits;
905
929
    bits = N / 2;
906
929
    if (bits < 80)
907
5
        return 0;
908
924
    return bits >= secbits ? secbits : bits;
909
929
}
910
911
void BN_zero_ex(BIGNUM *a)
912
4.10M
{
913
4.10M
    a->neg = 0;
914
4.10M
    a->top = 0;
915
4.10M
    a->flags &= ~BN_FLG_FIXED_TOP;
916
4.10M
}
917
918
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
919
62.2k
{
920
62.2k
    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
921
62.2k
}
922
923
int BN_is_zero(const BIGNUM *a)
924
1.63M
{
925
1.63M
    return a->top == 0;
926
1.63M
}
927
928
int BN_is_one(const BIGNUM *a)
929
34.4k
{
930
34.4k
    return BN_abs_is_word(a, 1) && !a->neg;
931
34.4k
}
932
933
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
934
23.0k
{
935
23.0k
    return BN_abs_is_word(a, w) && (!w || !a->neg);
936
23.0k
}
937
938
int BN_is_odd(const BIGNUM *a)
939
306k
{
940
306k
    return (a->top > 0) && (a->d[0] & 1);
941
306k
}
942
943
int BN_is_negative(const BIGNUM *a)
944
14.4k
{
945
14.4k
    return (a->neg != 0);
946
14.4k
}
947
948
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
949
                     BN_CTX *ctx)
950
6.07k
{
951
6.07k
    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
952
6.07k
}
953
954
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
955
711
{
956
711
    dest->d = b->d;
957
711
    dest->top = b->top;
958
711
    dest->dmax = b->dmax;
959
711
    dest->neg = b->neg;
960
711
    dest->flags = ((dest->flags & BN_FLG_MALLOCED)
961
711
                   | (b->flags & ~BN_FLG_MALLOCED)
962
711
                   | BN_FLG_STATIC_DATA | flags);
963
711
}
964
965
BN_GENCB *BN_GENCB_new(void)
966
694
{
967
694
    BN_GENCB *ret;
968
969
694
    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
694
    return ret;
975
694
}
976
977
void BN_GENCB_free(BN_GENCB *cb)
978
1.00k
{
979
1.00k
    if (cb == NULL)
980
308
        return;
981
694
    OPENSSL_free(cb);
982
694
}
983
984
void BN_set_flags(BIGNUM *b, int n)
985
1.07k
{
986
1.07k
    b->flags |= n;
987
1.07k
}
988
989
int BN_get_flags(const BIGNUM *b, int n)
990
1.47M
{
991
1.47M
    return b->flags & n;
992
1.47M
}
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
694
{
1008
694
    BN_GENCB *tmp_gencb = gencb;
1009
694
    tmp_gencb->ver = 2;
1010
694
    tmp_gencb->arg = cb_arg;
1011
694
    tmp_gencb->cb.cb_2 = callback;
1012
694
}
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
11.2M
{
1021
11.2M
    return (words <= a->dmax) ? a : bn_expand2(a, words);
1022
11.2M
}
1023
1024
void bn_correct_top_consttime(BIGNUM *a)
1025
0
{
1026
0
    int j, atop;
1027
0
    BN_ULONG limb;
1028
0
    unsigned int mask;
1029
1030
0
    for (j = 0, atop = 0; j < a->dmax; j++) {
1031
0
        limb = a->d[j];
1032
0
        limb |= 0 - limb;
1033
0
        limb >>= BN_BITS2 - 1;
1034
0
        limb = 0 - limb;
1035
0
        mask = (unsigned int)limb;
1036
0
        mask &= constant_time_msb(j - a->top);
1037
0
        atop = constant_time_select_int(mask, j + 1, atop);
1038
0
    }
1039
1040
0
    mask = constant_time_eq_int(atop, 0);
1041
0
    a->top = atop;
1042
0
    a->neg = constant_time_select_int(mask, 0, a->neg);
1043
0
    a->flags &= ~BN_FLG_FIXED_TOP;
1044
0
}
1045
1046
void bn_correct_top(BIGNUM *a)
1047
6.85M
{
1048
6.85M
    BN_ULONG *ftl;
1049
6.85M
    int tmp_top = a->top;
1050
1051
6.85M
    if (tmp_top > 0) {
1052
18.5M
        for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
1053
18.5M
            ftl--;
1054
18.5M
            if (*ftl != 0)
1055
6.84M
                break;
1056
18.5M
        }
1057
6.85M
        a->top = tmp_top;
1058
6.85M
    }
1059
6.85M
    if (a->top == 0)
1060
6.47k
        a->neg = 0;
1061
6.85M
    a->flags &= ~BN_FLG_FIXED_TOP;
1062
6.85M
    bn_pollute(a);
1063
6.85M
}