Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
547k
{
15
547k
#ifndef BN_LLONG
16
547k
    BN_ULONG ret = 0;
17
#else
18
    BN_ULLONG ret = 0;
19
#endif
20
547k
    int i;
21
22
547k
    if (w == 0)
23
0
        return (BN_ULONG)-1;
24
25
547k
#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
547k
    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
547k
#endif
41
42
547k
    bn_check_top(a);
43
547k
    w &= BN_MASK2;
44
18.2M
    for (i = a->top - 1; i >= 0; i--) {
45
17.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
17.6M
        ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
52
17.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
17.6M
    }
57
547k
    return (BN_ULONG)ret;
58
547k
}
59
60
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
61
3.02M
{
62
3.02M
    BN_ULONG ret = 0;
63
3.02M
    int i, j;
64
65
3.02M
    bn_check_top(a);
66
3.02M
    w &= BN_MASK2;
67
68
3.02M
    if (!w)
69
        /* actually this an error (division by zero) */
70
0
        return (BN_ULONG)-1;
71
3.02M
    if (a->top == 0)
72
0
        return 0;
73
74
    /* normalize input (so bn_div_words doesn't complain) */
75
3.02M
    j = BN_BITS2 - BN_num_bits_word(w);
76
3.02M
    w <<= j;
77
3.02M
    if (!BN_lshift(a, a, j))
78
0
        return (BN_ULONG)-1;
79
80
7.58M
    for (i = a->top - 1; i >= 0; i--) {
81
4.55M
        BN_ULONG l, d;
82
83
4.55M
        l = a->d[i];
84
4.55M
        d = bn_div_words(ret, l, w);
85
4.55M
        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
86
4.55M
        a->d[i] = d;
87
4.55M
    }
88
3.02M
    if ((a->top > 0) && (a->d[a->top - 1] == 0))
89
3.00M
        a->top--;
90
3.02M
    ret >>= j;
91
3.02M
    if (!a->top)
92
2.65M
        a->neg = 0; /* don't allow negative zero */
93
3.02M
    bn_check_top(a);
94
3.02M
    return ret;
95
3.02M
}
96
97
int BN_add_word(BIGNUM *a, BN_ULONG w)
98
3.55M
{
99
3.55M
    BN_ULONG l;
100
3.55M
    int i;
101
102
3.55M
    bn_check_top(a);
103
3.55M
    w &= BN_MASK2;
104
105
    /* degenerate case: w is zero */
106
3.55M
    if (!w)
107
514k
        return 1;
108
    /* degenerate case: a is zero */
109
3.03M
    if (BN_is_zero(a))
110
62.0k
        return BN_set_word(a, w);
111
    /* handle 'a' when negative */
112
2.97M
    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
6.26M
    for (i = 0; w != 0 && i < a->top; i++) {
120
3.29M
        a->d[i] = l = (a->d[i] + w) & BN_MASK2;
121
3.29M
        w = (w > l) ? 1 : 0;
122
3.29M
    }
123
2.97M
    if (w && i == a->top) {
124
2.65k
        if (bn_wexpand(a, a->top + 1) == NULL)
125
0
            return 0;
126
2.65k
        a->top++;
127
2.65k
        a->d[i] = w;
128
2.65k
    }
129
2.97M
    bn_check_top(a);
130
2.97M
    return 1;
131
2.97M
}
132
133
int BN_sub_word(BIGNUM *a, BN_ULONG w)
134
1.34M
{
135
1.34M
    int i;
136
137
1.34M
    bn_check_top(a);
138
1.34M
    w &= BN_MASK2;
139
140
    /* degenerate case: w is zero */
141
1.34M
    if (!w)
142
0
        return 1;
143
    /* degenerate case: a is zero */
144
1.34M
    if (BN_is_zero(a)) {
145
870
        i = BN_set_word(a, w);
146
870
        if (i != 0)
147
870
            BN_set_negative(a, 1);
148
870
        return i;
149
870
    }
150
    /* handle 'a' when negative */
151
1.34M
    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.34M
    if ((a->top == 1) && (a->d[0] < w)) {
159
19
        a->d[0] = w - a->d[0];
160
19
        a->neg = 1;
161
19
        return 1;
162
19
    }
163
1.34M
    i = 0;
164
2.61M
    for (;;) {
165
2.61M
        if (a->d[i] >= w) {
166
1.34M
            a->d[i] -= w;
167
1.34M
            break;
168
1.34M
        } else {
169
1.26M
            a->d[i] = (a->d[i] - w) & BN_MASK2;
170
1.26M
            i++;
171
1.26M
            w = 1;
172
1.26M
        }
173
2.61M
    }
174
1.34M
    if ((a->d[i] == 0) && (i == (a->top - 1)))
175
206k
        a->top--;
176
1.34M
    bn_check_top(a);
177
1.34M
    return 1;
178
1.34M
}
179
180
int BN_mul_word(BIGNUM *a, BN_ULONG w)
181
6.39M
{
182
6.39M
    BN_ULONG ll;
183
184
6.39M
    bn_check_top(a);
185
6.39M
    w &= BN_MASK2;
186
6.39M
    if (a->top) {
187
6.39M
        if (w == 0)
188
0
            BN_zero(a);
189
6.39M
        else {
190
6.39M
            ll = bn_mul_words(a->d, a->d, a->top, w);
191
6.39M
            if (ll) {
192
6.18M
                if (bn_wexpand(a, a->top + 1) == NULL)
193
0
                    return 0;
194
6.18M
                a->d[a->top++] = ll;
195
6.18M
            }
196
6.39M
        }
197
6.39M
    }
198
6.39M
    bn_check_top(a);
199
6.39M
    return 1;
200
6.39M
}