Coverage Report

Created: 2025-08-11 07:04

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