Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/bn/bn_mod.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-2023 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
68.9M
{
15
    /*
16
     * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
17
     * always holds)
18
     */
19
20
68.9M
    if (r == d) {
21
0
        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
22
0
        return 0;
23
0
    }
24
25
68.9M
    if (!(BN_mod(r, m, d, ctx)))
26
14
        return 0;
27
68.9M
    if (!r->neg)
28
68.3M
        return 1;
29
    /* now   -|d| < r < 0,  so we have to set  r := r + |d| */
30
542k
    return (d->neg ? BN_sub : BN_add) (r, r, d);
31
68.9M
}
32
33
int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
34
               BN_CTX *ctx)
35
962
{
36
962
    if (!BN_add(r, a, b))
37
0
        return 0;
38
962
    return BN_nnmod(r, r, m, ctx);
39
962
}
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
12.8M
{
56
12.8M
    size_t i, ai, bi, mtop = m->top;
57
12.8M
    BN_ULONG storage[1024 / BN_BITS2];
58
12.8M
    BN_ULONG carry, temp, mask, *rp, *tp = storage;
59
12.8M
    const BN_ULONG *ap, *bp;
60
61
12.8M
    if (bn_wexpand(r, mtop) == NULL)
62
0
        return 0;
63
64
12.8M
    if (mtop > sizeof(storage) / sizeof(storage[0])) {
65
8.87k
        tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG));
66
8.87k
        if (tp == NULL)
67
0
            return 0;
68
8.87k
    }
69
70
12.8M
    ap = a->d != NULL ? a->d : tp;
71
12.8M
    bp = b->d != NULL ? b->d : tp;
72
73
70.2M
    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
74
57.4M
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
75
57.4M
        temp = ((ap[ai] & mask) + carry) & BN_MASK2;
76
57.4M
        carry = (temp < carry);
77
78
57.4M
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
79
57.4M
        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
80
57.4M
        carry += (tp[i] < temp);
81
82
57.4M
        i++;
83
57.4M
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
84
57.4M
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
85
57.4M
    }
86
12.8M
    rp = r->d;
87
12.8M
    carry -= bn_sub_words(rp, tp, m->d, mtop);
88
70.2M
    for (i = 0; i < mtop; i++) {
89
57.4M
        rp[i] = (carry & tp[i]) | (~carry & rp[i]);
90
57.4M
        ((volatile BN_ULONG *)tp)[i] = 0;
91
57.4M
    }
92
12.8M
    r->top = mtop;
93
12.8M
    r->flags |= BN_FLG_FIXED_TOP;
94
12.8M
    r->neg = 0;
95
96
12.8M
    if (tp != storage)
97
8.87k
        OPENSSL_free(tp);
98
99
12.8M
    return 1;
100
12.8M
}
101
102
int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
103
                     const BIGNUM *m)
104
12.8M
{
105
12.8M
    int ret = bn_mod_add_fixed_top(r, a, b, m);
106
107
12.8M
    if (ret)
108
12.8M
        bn_correct_top(r);
109
110
12.8M
    return ret;
111
12.8M
}
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
8.87k
{
138
8.87k
    size_t i, ai, bi, mtop = m->top;
139
8.87k
    BN_ULONG borrow, carry, ta, tb, mask, *rp;
140
8.87k
    const BN_ULONG *ap, *bp;
141
142
8.87k
    if (bn_wexpand(r, mtop) == NULL)
143
0
        return 0;
144
145
8.87k
    rp = r->d;
146
8.87k
    ap = a->d != NULL ? a->d : rp;
147
8.87k
    bp = b->d != NULL ? b->d : rp;
148
149
150k
    for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) {
150
142k
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
151
142k
        ta = ap[ai] & mask;
152
153
142k
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
154
142k
        tb = bp[bi] & mask;
155
142k
        rp[i] = ta - tb - borrow;
156
142k
        if (ta != tb)
157
129k
            borrow = (ta < tb);
158
159
142k
        i++;
160
142k
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
161
142k
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
162
142k
    }
163
8.87k
    ap = m->d;
164
150k
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
165
142k
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
166
142k
        carry = (ta < carry);
167
142k
        rp[i] = (rp[i] + ta) & BN_MASK2;
168
142k
        carry += (rp[i] < ta);
169
142k
    }
170
8.87k
    borrow -= carry;
171
150k
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
172
142k
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
173
142k
        carry = (ta < carry);
174
142k
        rp[i] = (rp[i] + ta) & BN_MASK2;
175
142k
        carry += (rp[i] < ta);
176
142k
    }
177
178
8.87k
    r->top = mtop;
179
8.87k
    r->flags |= BN_FLG_FIXED_TOP;
180
8.87k
    r->neg = 0;
181
182
8.87k
    return 1;
183
8.87k
}
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
12.9M
{
192
12.9M
    if (r == m) {
193
0
        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
194
0
        return 0;
195
0
    }
196
197
12.9M
    if (!BN_sub(r, a, b))
198
0
        return 0;
199
12.9M
    if (r->neg)
200
6.30M
        return BN_add(r, r, m);
201
6.63M
    return 1;
202
12.9M
}
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
63.3M
{
208
63.3M
    BIGNUM *t;
209
63.3M
    int ret = 0;
210
211
63.3M
    bn_check_top(a);
212
63.3M
    bn_check_top(b);
213
63.3M
    bn_check_top(m);
214
215
63.3M
    BN_CTX_start(ctx);
216
63.3M
    if ((t = BN_CTX_get(ctx)) == NULL)
217
0
        goto err;
218
63.3M
    if (a == b) {
219
60.6M
        if (!BN_sqr(t, a, ctx))
220
0
            goto err;
221
60.6M
    } else {
222
2.75M
        if (!BN_mul(t, a, b, ctx))
223
0
            goto err;
224
2.75M
    }
225
63.3M
    if (!BN_nnmod(r, t, m, ctx))
226
14
        goto err;
227
63.3M
    bn_check_top(r);
228
63.3M
    ret = 1;
229
63.3M
 err:
230
63.3M
    BN_CTX_end(ctx);
231
63.3M
    return ret;
232
63.3M
}
233
234
int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
235
2.71M
{
236
2.71M
    if (!BN_sqr(r, a, ctx))
237
0
        return 0;
238
    /* r->neg == 0,  thus we don't need BN_nnmod */
239
2.71M
    return BN_mod(r, r, m, ctx);
240
2.71M
}
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
5.38M
{
256
5.38M
    if (!BN_lshift1(r, a))
257
0
        return 0;
258
5.38M
    bn_check_top(r);
259
5.38M
    if (BN_cmp(r, m) >= 0)
260
2.64M
        return BN_sub(r, r, m);
261
2.73M
    return 1;
262
5.38M
}
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
2.90M
{
293
2.90M
    if (r != a) {
294
2.30M
        if (BN_copy(r, a) == NULL)
295
0
            return 0;
296
2.30M
    }
297
298
8.06M
    while (n > 0) {
299
5.16M
        int max_shift;
300
301
        /* 0 < r < m */
302
5.16M
        max_shift = BN_num_bits(m) - BN_num_bits(r);
303
        /* max_shift >= 0 */
304
305
5.16M
        if (max_shift < 0) {
306
0
            ERR_raise(ERR_LIB_BN, BN_R_INPUT_NOT_REDUCED);
307
0
            return 0;
308
0
        }
309
310
5.16M
        if (max_shift > n)
311
1.27M
            max_shift = n;
312
313
5.16M
        if (max_shift) {
314
2.63M
            if (!BN_lshift(r, r, max_shift))
315
0
                return 0;
316
2.63M
            n -= max_shift;
317
2.63M
        } else {
318
2.52M
            if (!BN_lshift1(r, r))
319
0
                return 0;
320
2.52M
            --n;
321
2.52M
        }
322
323
        /* BN_num_bits(r) <= BN_num_bits(m) */
324
325
5.16M
        if (BN_cmp(r, m) >= 0) {
326
3.03M
            if (!BN_sub(r, r, m))
327
0
                return 0;
328
3.03M
        }
329
5.16M
    }
330
2.90M
    bn_check_top(r);
331
332
2.90M
    return 1;
333
2.90M
}