Coverage Report

Created: 2023-06-08 06:40

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