Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/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
241k
{
15
241k
#ifndef BN_LLONG
16
241k
    BN_ULONG ret = 0;
17
#else
18
    BN_ULLONG ret = 0;
19
#endif
20
241k
    int i;
21
22
241k
    if (w == 0)
23
0
        return (BN_ULONG)-1;
24
25
241k
#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
241k
    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
241k
#endif
41
42
241k
    bn_check_top(a);
43
241k
    w &= BN_MASK2;
44
6.29M
    for (i = a->top - 1; i >= 0; i--) {
45
6.05M
#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
6.05M
        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
52
6.05M
        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
6.05M
    }
58
241k
    return (BN_ULONG)ret;
59
241k
}
60
61
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
62
981k
{
63
981k
    BN_ULONG ret = 0;
64
981k
    int i, j;
65
66
981k
    bn_check_top(a);
67
981k
    w &= BN_MASK2;
68
69
981k
    if (!w)
70
        /* actually this an error (division by zero) */
71
0
        return (BN_ULONG)-1;
72
981k
    if (a->top == 0)
73
0
        return 0;
74
75
    /* normalize input (so bn_div_words doesn't complain) */
76
981k
    j = BN_BITS2 - BN_num_bits_word(w);
77
981k
    w <<= j;
78
981k
    if (!BN_lshift(a, a, j))
79
0
        return (BN_ULONG)-1;
80
81
2.95M
    for (i = a->top - 1; i >= 0; i--) {
82
1.97M
        BN_ULONG l, d;
83
84
1.97M
        l = a->d[i];
85
1.97M
        d = bn_div_words(ret, l, w);
86
1.97M
        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
87
1.97M
        a->d[i] = d;
88
1.97M
    }
89
981k
    if ((a->top > 0) && (a->d[a->top - 1] == 0))
90
962k
        a->top--;
91
981k
    ret >>= j;
92
981k
    if (!a->top)
93
745k
        a->neg = 0; /* don't allow negative zero */
94
981k
    bn_check_top(a);
95
981k
    return ret;
96
981k
}
97
98
int BN_add_word(BIGNUM *a, BN_ULONG w)
99
1.98M
{
100
1.98M
    BN_ULONG l;
101
1.98M
    int i;
102
103
1.98M
    bn_check_top(a);
104
1.98M
    w &= BN_MASK2;
105
106
    /* degenerate case: w is zero */
107
1.98M
    if (!w)
108
355k
        return 1;
109
    /* degenerate case: a is zero */
110
1.62M
    if (BN_is_zero(a))
111
33.7k
        return BN_set_word(a, w);
112
    /* handle 'a' when negative */
113
1.59M
    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
3.31M
    for (i = 0; w != 0 && i < a->top; i++) {
121
1.72M
        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
122
1.72M
        w = (w > l) ? 1 : 0;
123
1.72M
    }
124
1.59M
    if (w && i == a->top) {
125
1.11k
        if (bn_wexpand(a, a->top + 1) == NULL)
126
0
            return 0;
127
1.11k
        a->top++;
128
1.11k
        a->d[i] = w;
129
1.11k
    }
130
1.59M
    bn_check_top(a);
131
1.59M
    return 1;
132
1.59M
}
133
134
int BN_sub_word(BIGNUM *a, BN_ULONG w)
135
818k
{
136
818k
    int i;
137
138
818k
    bn_check_top(a);
139
818k
    w &= BN_MASK2;
140
141
    /* degenerate case: w is zero */
142
818k
    if (!w)
143
0
        return 1;
144
    /* degenerate case: a is zero */
145
818k
    if (BN_is_zero(a)) {
146
631
        i = BN_set_word(a, w);
147
631
        if (i != 0)
148
631
            BN_set_negative(a, 1);
149
631
        return i;
150
631
    }
151
    /* handle 'a' when negative */
152
817k
    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
817k
    if ((a->top == 1) && (a->d[0] < w)) {
160
4
        a->d[0] = w - a->d[0];
161
4
        a->neg = 1;
162
4
        return 1;
163
4
    }
164
817k
    i = 0;
165
1.58M
    for (;;) {
166
1.58M
        if (a->d[i] >= w) {
167
817k
            a->d[i] -= w;
168
817k
            break;
169
817k
        } else {
170
771k
            a->d[i] = (a->d[i] - w) & BN_MASK2;
171
771k
            i++;
172
771k
            w = 1;
173
771k
        }
174
1.58M
    }
175
817k
    if ((a->d[i] == 0) && (i == (a->top - 1)))
176
159k
        a->top--;
177
817k
    bn_check_top(a);
178
817k
    return 1;
179
817k
}
180
181
int BN_mul_word(BIGNUM *a, BN_ULONG w)
182
4.12M
{
183
4.12M
    BN_ULONG ll;
184
185
4.12M
    bn_check_top(a);
186
4.12M
    w &= BN_MASK2;
187
4.12M
    if (a->top) {
188
4.11M
        if (w == 0)
189
0
            BN_zero(a);
190
4.11M
        else {
191
4.11M
            ll = bn_mul_words(a->d, a->d, a->top, w);
192
4.11M
            if (ll) {
193
4.02M
                if (bn_wexpand(a, a->top + 1) == NULL)
194
0
                    return 0;
195
4.02M
                a->d[a->top++] = ll;
196
4.02M
            }
197
4.11M
        }
198
4.11M
    }
199
4.12M
    bn_check_top(a);
200
4.12M
    return 1;
201
4.12M
}