Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/bn/bn_word.c
Line
Count
Source
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
497k
{
15
497k
#ifndef BN_LLONG
16
497k
    BN_ULONG ret = 0;
17
#else
18
    BN_ULLONG ret = 0;
19
#endif
20
497k
    int i;
21
22
497k
    if (w == 0)
23
0
        return (BN_ULONG)-1;
24
25
497k
#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
497k
    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
497k
#endif
41
42
497k
    bn_check_top(a);
43
497k
    w &= BN_MASK2;
44
13.1M
    for (i = a->top - 1; i >= 0; i--) {
45
12.6M
#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
12.6M
        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
52
12.6M
        ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
53
#else
54
        ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
55
#endif
56
12.6M
    }
57
497k
    return (BN_ULONG)ret;
58
497k
}
59
60
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
61
3.87M
{
62
3.87M
    BN_ULONG ret = 0;
63
3.87M
    int i, j;
64
65
3.87M
    bn_check_top(a);
66
3.87M
    w &= BN_MASK2;
67
68
3.87M
    if (!w)
69
        /* actually this an error (division by zero) */
70
0
        return (BN_ULONG)-1;
71
3.87M
    if (a->top == 0)
72
0
        return 0;
73
74
    /* normalize input (so bn_div_words doesn't complain) */
75
3.87M
    j = BN_BITS2 - BN_num_bits_word(w);
76
3.87M
    w <<= j;
77
3.87M
    if (!BN_lshift(a, a, j))
78
0
        return (BN_ULONG)-1;
79
80
19.8M
    for (i = a->top - 1; i >= 0; i--) {
81
15.9M
        BN_ULONG l, d;
82
83
15.9M
        l = a->d[i];
84
15.9M
        d = bn_div_words(ret, l, w);
85
15.9M
        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
86
15.9M
        a->d[i] = d;
87
15.9M
    }
88
3.87M
    if ((a->top > 0) && (a->d[a->top - 1] == 0))
89
3.85M
        a->top--;
90
3.87M
    ret >>= j;
91
3.87M
    if (!a->top)
92
3.22M
        a->neg = 0; /* don't allow negative zero */
93
3.87M
    bn_check_top(a);
94
3.87M
    return ret;
95
3.87M
}
96
97
int BN_add_word(BIGNUM *a, BN_ULONG w)
98
4.28M
{
99
4.28M
    BN_ULONG l;
100
4.28M
    int i;
101
102
4.28M
    bn_check_top(a);
103
4.28M
    w &= BN_MASK2;
104
105
    /* degenerate case: w is zero */
106
4.28M
    if (!w)
107
755k
        return 1;
108
    /* degenerate case: a is zero */
109
3.52M
    if (BN_is_zero(a))
110
84.5k
        return BN_set_word(a, w);
111
    /* handle 'a' when negative */
112
3.44M
    if (a->neg) {
113
0
        a->neg = 0;
114
0
        i = BN_sub_word(a, w);
115
0
        if (!BN_is_zero(a))
116
0
            a->neg = !(a->neg);
117
0
        return i;
118
0
    }
119
7.14M
    for (i = 0; w != 0 && i < a->top; i++) {
120
3.70M
        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
121
3.70M
        w = (w > l) ? 1 : 0;
122
3.70M
    }
123
3.44M
    if (w && i == a->top) {
124
3.39k
        if (bn_wexpand(a, a->top + 1) == NULL)
125
0
            return 0;
126
3.39k
        a->top++;
127
3.39k
        a->d[i] = w;
128
3.39k
    }
129
3.44M
    bn_check_top(a);
130
3.44M
    return 1;
131
3.44M
}
132
133
int BN_sub_word(BIGNUM *a, BN_ULONG w)
134
1.06M
{
135
1.06M
    int i;
136
137
1.06M
    bn_check_top(a);
138
1.06M
    w &= BN_MASK2;
139
140
    /* degenerate case: w is zero */
141
1.06M
    if (!w)
142
0
        return 1;
143
    /* degenerate case: a is zero */
144
1.06M
    if (BN_is_zero(a)) {
145
692
        i = BN_set_word(a, w);
146
692
        if (i != 0)
147
692
            BN_set_negative(a, 1);
148
692
        return i;
149
692
    }
150
    /* handle 'a' when negative */
151
1.05M
    if (a->neg) {
152
0
        a->neg = 0;
153
0
        i = BN_add_word(a, w);
154
0
        a->neg = 1;
155
0
        return i;
156
0
    }
157
158
1.05M
    if ((a->top == 1) && (a->d[0] < w)) {
159
23
        a->d[0] = w - a->d[0];
160
23
        a->neg = 1;
161
23
        return 1;
162
23
    }
163
1.05M
    i = 0;
164
2.02M
    for (;;) {
165
2.02M
        if (a->d[i] >= w) {
166
1.05M
            a->d[i] -= w;
167
1.05M
            break;
168
1.05M
        } else {
169
966k
            a->d[i] = (a->d[i] - w) & BN_MASK2;
170
966k
            i++;
171
966k
            w = 1;
172
966k
        }
173
2.02M
    }
174
1.05M
    if ((a->d[i] == 0) && (i == (a->top - 1)))
175
115k
        a->top--;
176
1.05M
    bn_check_top(a);
177
1.05M
    return 1;
178
1.05M
}
179
180
int BN_mul_word(BIGNUM *a, BN_ULONG w)
181
4.59M
{
182
4.59M
    BN_ULONG ll;
183
184
4.59M
    bn_check_top(a);
185
4.59M
    w &= BN_MASK2;
186
4.59M
    if (a->top) {
187
4.56M
        if (w == 0)
188
0
            BN_zero(a);
189
4.56M
        else {
190
4.56M
            ll = bn_mul_words(a->d, a->d, a->top, w);
191
4.56M
            if (ll) {
192
3.95M
                if (bn_wexpand(a, a->top + 1) == NULL)
193
0
                    return 0;
194
3.95M
                a->d[a->top++] = ll;
195
3.95M
            }
196
4.56M
        }
197
4.56M
    }
198
4.59M
    bn_check_top(a);
199
4.59M
    return 1;
200
4.59M
}