Coverage Report

Created: 2023-06-08 06:41

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