Coverage Report

Created: 2023-09-25 06:41

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