Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/bn/bn_mod.c
Line
Count
Source
1
/*
2
 * Copyright 1998-2021 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
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
14
104M
{
15
    /*
16
     * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
17
     * always holds)
18
     */
19
20
104M
    if (r == d) {
21
0
        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
22
0
        return 0;
23
0
    }
24
25
104M
    if (!(BN_mod(r, m, d, ctx)))
26
34
        return 0;
27
104M
    if (!r->neg)
28
104M
        return 1;
29
    /* now   -|d| < r < 0,  so we have to set  r := r + |d| */
30
812k
    return (d->neg ? BN_sub : BN_add)(r, r, d);
31
104M
}
32
33
int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
34
    BN_CTX *ctx)
35
2.89k
{
36
2.89k
    if (!BN_add(r, a, b))
37
0
        return 0;
38
2.89k
    return BN_nnmod(r, r, m, ctx);
39
2.89k
}
40
41
/*
42
 * BN_mod_add variant that may be used if both a and b are non-negative and
43
 * less than m. The original algorithm was
44
 *
45
 *    if (!BN_uadd(r, a, b))
46
 *       return 0;
47
 *    if (BN_ucmp(r, m) >= 0)
48
 *       return BN_usub(r, r, m);
49
 *
50
 * which is replaced with addition, subtracting modulus, and conditional
51
 * move depending on whether or not subtraction borrowed.
52
 */
53
int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
54
    const BIGNUM *m)
55
18.7M
{
56
18.7M
    size_t i, ai, bi, mtop = m->top;
57
18.7M
    BN_ULONG storage[1024 / BN_BITS2];
58
18.7M
    BN_ULONG carry, temp, mask, *rp, *tp = storage;
59
18.7M
    const BN_ULONG *ap, *bp;
60
61
18.7M
    if (bn_wexpand(r, mtop) == NULL)
62
0
        return 0;
63
64
18.7M
    if (mtop > sizeof(storage) / sizeof(storage[0])) {
65
16.3k
        tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG));
66
16.3k
        if (tp == NULL)
67
0
            return 0;
68
16.3k
    }
69
70
18.7M
    ap = a->d != NULL ? a->d : tp;
71
18.7M
    bp = b->d != NULL ? b->d : tp;
72
73
94.6M
    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
74
75.9M
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
75
75.9M
        temp = ((ap[ai] & mask) + carry) & BN_MASK2;
76
75.9M
        carry = (temp < carry);
77
78
75.9M
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
79
75.9M
        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
80
75.9M
        carry += (tp[i] < temp);
81
82
75.9M
        i++;
83
75.9M
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
84
75.9M
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
85
75.9M
    }
86
18.7M
    rp = r->d;
87
18.7M
    carry -= bn_sub_words(rp, tp, m->d, mtop);
88
94.6M
    for (i = 0; i < mtop; i++) {
89
75.9M
        rp[i] = (carry & tp[i]) | (~carry & rp[i]);
90
75.9M
        ((volatile BN_ULONG *)tp)[i] = 0;
91
75.9M
    }
92
18.7M
    r->top = mtop;
93
18.7M
    r->flags |= BN_FLG_FIXED_TOP;
94
18.7M
    r->neg = 0;
95
96
18.7M
    if (tp != storage)
97
16.3k
        OPENSSL_free(tp);
98
99
18.7M
    return 1;
100
18.7M
}
101
102
int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
103
    const BIGNUM *m)
104
18.6M
{
105
18.6M
    int ret = bn_mod_add_fixed_top(r, a, b, m);
106
107
18.6M
    if (ret)
108
18.6M
        bn_correct_top(r);
109
110
18.6M
    return ret;
111
18.6M
}
112
113
int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
114
    BN_CTX *ctx)
115
0
{
116
0
    if (!BN_sub(r, a, b))
117
0
        return 0;
118
0
    return BN_nnmod(r, r, m, ctx);
119
0
}
120
121
/*
122
 * BN_mod_sub variant that may be used if both a and b are non-negative,
123
 * a is less than m, while b is of same bit width as m. It's implemented
124
 * as subtraction followed by two conditional additions.
125
 *
126
 * 0 <= a < m
127
 * 0 <= b < 2^w < 2*m
128
 *
129
 * after subtraction
130
 *
131
 * -2*m < r = a - b < m
132
 *
133
 * Thus it takes up to two conditional additions to make |r| positive.
134
 */
135
int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
136
    const BIGNUM *m)
137
16.3k
{
138
16.3k
    size_t i, ai, bi, mtop = m->top;
139
16.3k
    BN_ULONG borrow, carry, ta, tb, mask, *rp;
140
16.3k
    const BN_ULONG *ap, *bp;
141
142
16.3k
    if (bn_wexpand(r, mtop) == NULL)
143
0
        return 0;
144
145
16.3k
    rp = r->d;
146
16.3k
    ap = a->d != NULL ? a->d : rp;
147
16.3k
    bp = b->d != NULL ? b->d : rp;
148
149
277k
    for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) {
150
261k
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
151
261k
        ta = ap[ai] & mask;
152
153
261k
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
154
261k
        tb = bp[bi] & mask;
155
261k
        rp[i] = ta - tb - borrow;
156
261k
        if (ta != tb)
157
246k
            borrow = (ta < tb);
158
159
261k
        i++;
160
261k
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
161
261k
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
162
261k
    }
163
16.3k
    ap = m->d;
164
277k
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
165
261k
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
166
261k
        carry = (ta < carry);
167
261k
        rp[i] = (rp[i] + ta) & BN_MASK2;
168
261k
        carry += (rp[i] < ta);
169
261k
    }
170
16.3k
    borrow -= carry;
171
277k
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
172
261k
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
173
261k
        carry = (ta < carry);
174
261k
        rp[i] = (rp[i] + ta) & BN_MASK2;
175
261k
        carry += (rp[i] < ta);
176
261k
    }
177
178
16.3k
    r->top = mtop;
179
16.3k
    r->flags |= BN_FLG_FIXED_TOP;
180
16.3k
    r->neg = 0;
181
182
16.3k
    return 1;
183
16.3k
}
184
185
/*
186
 * BN_mod_sub variant that may be used if both a and b are non-negative and
187
 * less than m
188
 */
189
int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
190
    const BIGNUM *m)
191
18.1M
{
192
18.1M
    if (r == m) {
193
0
        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
194
0
        return 0;
195
0
    }
196
197
18.1M
    if (!BN_sub(r, a, b))
198
0
        return 0;
199
18.1M
    if (r->neg)
200
8.75M
        return BN_add(r, r, m);
201
9.39M
    return 1;
202
18.1M
}
203
204
/* slow but works */
205
int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
206
    BN_CTX *ctx)
207
95.9M
{
208
95.9M
    BIGNUM *t;
209
95.9M
    int ret = 0;
210
211
95.9M
    bn_check_top(a);
212
95.9M
    bn_check_top(b);
213
95.9M
    bn_check_top(m);
214
215
95.9M
    BN_CTX_start(ctx);
216
95.9M
    if ((t = BN_CTX_get(ctx)) == NULL)
217
0
        goto err;
218
95.9M
    if (a == b) {
219
91.8M
        if (!BN_sqr(t, a, ctx))
220
0
            goto err;
221
91.8M
    } else {
222
4.11M
        if (!BN_mul(t, a, b, ctx))
223
0
            goto err;
224
4.11M
    }
225
95.9M
    if (!BN_nnmod(r, t, m, ctx))
226
34
        goto err;
227
95.9M
    bn_check_top(r);
228
95.9M
    ret = 1;
229
95.9M
err:
230
95.9M
    BN_CTX_end(ctx);
231
95.9M
    return ret;
232
95.9M
}
233
234
int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
235
3.96M
{
236
3.96M
    if (!BN_sqr(r, a, ctx))
237
0
        return 0;
238
    /* r->neg == 0,  thus we don't need BN_nnmod */
239
3.96M
    return BN_mod(r, r, m, ctx);
240
3.96M
}
241
242
int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
243
0
{
244
0
    if (!BN_lshift1(r, a))
245
0
        return 0;
246
0
    bn_check_top(r);
247
0
    return BN_nnmod(r, r, m, ctx);
248
0
}
249
250
/*
251
 * BN_mod_lshift1 variant that may be used if a is non-negative and less than
252
 * m
253
 */
254
int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
255
7.51M
{
256
7.51M
    if (!BN_lshift1(r, a))
257
0
        return 0;
258
7.51M
    bn_check_top(r);
259
7.51M
    if (BN_cmp(r, m) >= 0)
260
3.62M
        return BN_sub(r, r, m);
261
3.89M
    return 1;
262
7.51M
}
263
264
int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
265
    BN_CTX *ctx)
266
0
{
267
0
    BIGNUM *abs_m = NULL;
268
0
    int ret;
269
270
0
    if (!BN_nnmod(r, a, m, ctx))
271
0
        return 0;
272
273
0
    if (m->neg) {
274
0
        abs_m = BN_dup(m);
275
0
        if (abs_m == NULL)
276
0
            return 0;
277
0
        abs_m->neg = 0;
278
0
    }
279
280
0
    ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
281
0
    bn_check_top(r);
282
283
0
    BN_free(abs_m);
284
0
    return ret;
285
0
}
286
287
/*
288
 * BN_mod_lshift variant that may be used if a is non-negative and less than
289
 * m
290
 */
291
int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
292
4.02M
{
293
4.02M
    if (r != a) {
294
3.24M
        if (BN_copy(r, a) == NULL)
295
0
            return 0;
296
3.24M
    }
297
298
10.5M
    while (n > 0) {
299
6.54M
        int max_shift;
300
301
        /* 0 < r < m */
302
6.54M
        max_shift = BN_num_bits(m) - BN_num_bits(r);
303
        /* max_shift >= 0 */
304
305
6.54M
        if (max_shift < 0) {
306
0
            ERR_raise(ERR_LIB_BN, BN_R_INPUT_NOT_REDUCED);
307
0
            return 0;
308
0
        }
309
310
6.54M
        if (max_shift > n)
311
1.97M
            max_shift = n;
312
313
6.54M
        if (max_shift) {
314
3.88M
            if (!BN_lshift(r, r, max_shift))
315
0
                return 0;
316
3.88M
            n -= max_shift;
317
3.88M
        } else {
318
2.65M
            if (!BN_lshift1(r, r))
319
0
                return 0;
320
2.65M
            --n;
321
2.65M
        }
322
323
        /* BN_num_bits(r) <= BN_num_bits(m) */
324
325
6.54M
        if (BN_cmp(r, m) >= 0) {
326
3.30M
            if (!BN_sub(r, r, m))
327
0
                return 0;
328
3.30M
        }
329
6.54M
    }
330
4.02M
    bn_check_top(r);
331
332
4.02M
    return 1;
333
4.02M
}