Coverage Report

Created: 2025-08-28 07:07

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