Coverage Report

Created: 2024-01-12 07:13

/src/openssl/crypto/bn/bn_word.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2016 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 "internal/cryptlib.h"
11
#include "bn_local.h"
12
13
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w)
14
0
{
15
0
#ifndef BN_LLONG
16
0
    BN_ULONG ret = 0;
17
#else
18
    BN_ULLONG ret = 0;
19
#endif
20
0
    int i;
21
22
0
    if (w == 0)
23
0
        return (BN_ULONG)-1;
24
25
0
#ifndef BN_LLONG
26
    /*
27
     * If |w| is too long and we don't have BN_ULLONG then we need to fall
28
     * back to using BN_div_word
29
     */
30
0
    if (w > ((BN_ULONG)1 << BN_BITS4)) {
31
0
        BIGNUM *tmp = BN_dup(a);
32
0
        if (tmp == NULL)
33
0
            return (BN_ULONG)-1;
34
35
0
        ret = BN_div_word(tmp, w);
36
0
        BN_free(tmp);
37
38
0
        return ret;
39
0
    }
40
0
#endif
41
42
0
    bn_check_top(a);
43
0
    w &= BN_MASK2;
44
0
    for (i = a->top - 1; i >= 0; i--) {
45
0
#ifndef BN_LLONG
46
        /*
47
         * We can assume here that | w <= ((BN_ULONG)1 << BN_BITS4) | and so
48
         * | ret < ((BN_ULONG)1 << BN_BITS4) | and therefore the shifts here are
49
         * safe and will not overflow
50
         */
51
0
        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
52
0
        ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
53
#else
54
        ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) %
55
                           (BN_ULLONG) w);
56
#endif
57
0
    }
58
0
    return (BN_ULONG)ret;
59
0
}
60
61
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
62
20.1k
{
63
20.1k
    BN_ULONG ret = 0;
64
20.1k
    int i, j;
65
66
20.1k
    bn_check_top(a);
67
20.1k
    w &= BN_MASK2;
68
69
20.1k
    if (!w)
70
        /* actually this an error (division by zero) */
71
0
        return (BN_ULONG)-1;
72
20.1k
    if (a->top == 0)
73
0
        return 0;
74
75
    /* normalize input (so bn_div_words doesn't complain) */
76
20.1k
    j = BN_BITS2 - BN_num_bits_word(w);
77
20.1k
    w <<= j;
78
20.1k
    if (!BN_lshift(a, a, j))
79
0
        return (BN_ULONG)-1;
80
81
65.4k
    for (i = a->top - 1; i >= 0; i--) {
82
45.3k
        BN_ULONG l, d;
83
84
45.3k
        l = a->d[i];
85
45.3k
        d = bn_div_words(ret, l, w);
86
45.3k
        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
87
45.3k
        a->d[i] = d;
88
45.3k
    }
89
20.1k
    if ((a->top > 0) && (a->d[a->top - 1] == 0))
90
19.4k
        a->top--;
91
20.1k
    ret >>= j;
92
20.1k
    if (!a->top)
93
9.47k
        a->neg = 0; /* don't allow negative zero */
94
20.1k
    bn_check_top(a);
95
20.1k
    return ret;
96
20.1k
}
97
98
int BN_add_word(BIGNUM *a, BN_ULONG w)
99
63.9k
{
100
63.9k
    BN_ULONG l;
101
63.9k
    int i;
102
103
63.9k
    bn_check_top(a);
104
63.9k
    w &= BN_MASK2;
105
106
    /* degenerate case: w is zero */
107
63.9k
    if (!w)
108
41.3k
        return 1;
109
    /* degenerate case: a is zero */
110
22.6k
    if (BN_is_zero(a))
111
331
        return BN_set_word(a, w);
112
    /* handle 'a' when negative */
113
22.2k
    if (a->neg) {
114
0
        a->neg = 0;
115
0
        i = BN_sub_word(a, w);
116
0
        if (!BN_is_zero(a))
117
0
            a->neg = !(a->neg);
118
0
        return i;
119
0
    }
120
45.4k
    for (i = 0; w != 0 && i < a->top; i++) {
121
23.1k
        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
122
23.1k
        w = (w > l) ? 1 : 0;
123
23.1k
    }
124
22.2k
    if (w && i == a->top) {
125
2
        if (bn_wexpand(a, a->top + 1) == NULL)
126
0
            return 0;
127
2
        a->top++;
128
2
        a->d[i] = w;
129
2
    }
130
22.2k
    bn_check_top(a);
131
22.2k
    return 1;
132
22.2k
}
133
134
int BN_sub_word(BIGNUM *a, BN_ULONG w)
135
7.40k
{
136
7.40k
    int i;
137
138
7.40k
    bn_check_top(a);
139
7.40k
    w &= BN_MASK2;
140
141
    /* degenerate case: w is zero */
142
7.40k
    if (!w)
143
0
        return 1;
144
    /* degenerate case: a is zero */
145
7.40k
    if (BN_is_zero(a)) {
146
0
        i = BN_set_word(a, w);
147
0
        if (i != 0)
148
0
            BN_set_negative(a, 1);
149
0
        return i;
150
0
    }
151
    /* handle 'a' when negative */
152
7.40k
    if (a->neg) {
153
0
        a->neg = 0;
154
0
        i = BN_add_word(a, w);
155
0
        a->neg = 1;
156
0
        return i;
157
0
    }
158
159
7.40k
    if ((a->top == 1) && (a->d[0] < w)) {
160
0
        a->d[0] = w - a->d[0];
161
0
        a->neg = 1;
162
0
        return 1;
163
0
    }
164
7.40k
    i = 0;
165
14.4k
    for (;;) {
166
14.4k
        if (a->d[i] >= w) {
167
7.40k
            a->d[i] -= w;
168
7.40k
            break;
169
7.40k
        } else {
170
7.02k
            a->d[i] = (a->d[i] - w) & BN_MASK2;
171
7.02k
            i++;
172
7.02k
            w = 1;
173
7.02k
        }
174
14.4k
    }
175
7.40k
    if ((a->d[i] == 0) && (i == (a->top - 1)))
176
688
        a->top--;
177
7.40k
    bn_check_top(a);
178
7.40k
    return 1;
179
7.40k
}
180
181
int BN_mul_word(BIGNUM *a, BN_ULONG w)
182
35.9k
{
183
35.9k
    BN_ULONG ll;
184
185
35.9k
    bn_check_top(a);
186
35.9k
    w &= BN_MASK2;
187
35.9k
    if (a->top) {
188
35.9k
        if (w == 0)
189
0
            BN_zero(a);
190
35.9k
        else {
191
35.9k
            ll = bn_mul_words(a->d, a->d, a->top, w);
192
35.9k
            if (ll) {
193
32.4k
                if (bn_wexpand(a, a->top + 1) == NULL)
194
0
                    return 0;
195
32.4k
                a->d[a->top++] = ll;
196
32.4k
            }
197
35.9k
        }
198
35.9k
    }
199
35.9k
    bn_check_top(a);
200
35.9k
    return 1;
201
35.9k
}