Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/bn/bn_mod.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-2016 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_lcl.h"
12
13
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
14
0
{
15
0
    /*
16
0
     * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
17
0
     * always holds)
18
0
     */
19
0
20
0
    if (!(BN_mod(r, m, d, ctx)))
21
0
        return 0;
22
0
    if (!r->neg)
23
0
        return 1;
24
0
    /* now   -|d| < r < 0,  so we have to set  r := r + |d| */
25
0
    return (d->neg ? BN_sub : BN_add) (r, r, d);
26
0
}
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
0
{
51
0
    size_t i, ai, bi, mtop = m->top;
52
0
    BN_ULONG storage[1024 / BN_BITS2];
53
0
    BN_ULONG carry, temp, mask, *rp, *tp = storage;
54
0
    const BN_ULONG *ap, *bp;
55
0
56
0
    if (bn_wexpand(r, mtop) == NULL)
57
0
        return 0;
58
0
59
0
    if (mtop > sizeof(storage) / sizeof(storage[0])
60
0
        && (tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG))) == NULL)
61
0
        return 0;
62
0
63
0
    ap = a->d != NULL ? a->d : tp;
64
0
    bp = b->d != NULL ? b->d : tp;
65
0
66
0
    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
67
0
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
68
0
        temp = ((ap[ai] & mask) + carry) & BN_MASK2;
69
0
        carry = (temp < carry);
70
0
71
0
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
72
0
        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
73
0
        carry += (tp[i] < temp);
74
0
75
0
        i++;
76
0
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
77
0
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
78
0
    }
79
0
    rp = r->d;
80
0
    carry -= bn_sub_words(rp, tp, m->d, mtop);
81
0
    for (i = 0; i < mtop; i++) {
82
0
        rp[i] = (carry & tp[i]) | (~carry & rp[i]);
83
0
        ((volatile BN_ULONG *)tp)[i] = 0;
84
0
    }
85
0
    r->top = mtop;
86
0
    r->flags |= BN_FLG_FIXED_TOP;
87
0
    r->neg = 0;
88
0
89
0
    if (tp != storage)
90
0
        OPENSSL_free(tp);
91
0
92
0
    return 1;
93
0
}
94
95
int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
96
                     const BIGNUM *m)
97
0
{
98
0
    int ret = bn_mod_add_fixed_top(r, a, b, m);
99
0
100
0
    if (ret)
101
0
        bn_correct_top(r);
102
0
103
0
    return ret;
104
0
}
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
0
{
131
0
    size_t i, ai, bi, mtop = m->top;
132
0
    BN_ULONG borrow, carry, ta, tb, mask, *rp;
133
0
    const BN_ULONG *ap, *bp;
134
0
135
0
    if (bn_wexpand(r, mtop) == NULL)
136
0
        return 0;
137
0
138
0
    rp = r->d;
139
0
    ap = a->d != NULL ? a->d : rp;
140
0
    bp = b->d != NULL ? b->d : rp;
141
0
142
0
    for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) {
143
0
        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
144
0
        ta = ap[ai] & mask;
145
0
146
0
        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
147
0
        tb = bp[bi] & mask;
148
0
        rp[i] = ta - tb - borrow;
149
0
        if (ta != tb)
150
0
            borrow = (ta < tb);
151
0
152
0
        i++;
153
0
        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
154
0
        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
155
0
    }
156
0
    ap = m->d;
157
0
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
158
0
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
159
0
        carry = (ta < carry);
160
0
        rp[i] = (rp[i] + ta) & BN_MASK2;
161
0
        carry += (rp[i] < ta);
162
0
    }
163
0
    borrow -= carry;
164
0
    for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
165
0
        ta = ((ap[i] & mask) + carry) & BN_MASK2;
166
0
        carry = (ta < carry);
167
0
        rp[i] = (rp[i] + ta) & BN_MASK2;
168
0
        carry += (rp[i] < ta);
169
0
    }
170
0
171
0
    r->top = mtop;
172
0
    r->flags |= BN_FLG_FIXED_TOP;
173
0
    r->neg = 0;
174
0
175
0
    return 1;
176
0
}
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
0
{
185
0
    if (!BN_sub(r, a, b))
186
0
        return 0;
187
0
    if (r->neg)
188
0
        return BN_add(r, r, m);
189
0
    return 1;
190
0
}
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
0
{
196
0
    BIGNUM *t;
197
0
    int ret = 0;
198
0
199
0
    bn_check_top(a);
200
0
    bn_check_top(b);
201
0
    bn_check_top(m);
202
0
203
0
    BN_CTX_start(ctx);
204
0
    if ((t = BN_CTX_get(ctx)) == NULL)
205
0
        goto err;
206
0
    if (a == b) {
207
0
        if (!BN_sqr(t, a, ctx))
208
0
            goto err;
209
0
    } else {
210
0
        if (!BN_mul(t, a, b, ctx))
211
0
            goto err;
212
0
    }
213
0
    if (!BN_nnmod(r, t, m, ctx))
214
0
        goto err;
215
0
    bn_check_top(r);
216
0
    ret = 1;
217
0
 err:
218
0
    BN_CTX_end(ctx);
219
0
    return ret;
220
0
}
221
222
int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
223
0
{
224
0
    if (!BN_sqr(r, a, ctx))
225
0
        return 0;
226
0
    /* r->neg == 0,  thus we don't need BN_nnmod */
227
0
    return BN_mod(r, r, m, ctx);
228
0
}
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
0
{
244
0
    if (!BN_lshift1(r, a))
245
0
        return 0;
246
0
    bn_check_top(r);
247
0
    if (BN_cmp(r, m) >= 0)
248
0
        return BN_sub(r, r, m);
249
0
    return 1;
250
0
}
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
0
258
0
    if (!BN_nnmod(r, a, m, ctx))
259
0
        return 0;
260
0
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
0
268
0
    ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
269
0
    bn_check_top(r);
270
0
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
0
{
281
0
    if (r != a) {
282
0
        if (BN_copy(r, a) == NULL)
283
0
            return 0;
284
0
    }
285
0
286
0
    while (n > 0) {
287
0
        int max_shift;
288
0
289
0
        /* 0 < r < m */
290
0
        max_shift = BN_num_bits(m) - BN_num_bits(r);
291
0
        /* max_shift >= 0 */
292
0
293
0
        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
0
298
0
        if (max_shift > n)
299
0
            max_shift = n;
300
0
301
0
        if (max_shift) {
302
0
            if (!BN_lshift(r, r, max_shift))
303
0
                return 0;
304
0
            n -= max_shift;
305
0
        } else {
306
0
            if (!BN_lshift1(r, r))
307
0
                return 0;
308
0
            --n;
309
0
        }
310
0
311
0
        /* BN_num_bits(r) <= BN_num_bits(m) */
312
0
313
0
        if (BN_cmp(r, m) >= 0) {
314
0
            if (!BN_sub(r, r, m))
315
0
                return 0;
316
0
        }
317
0
    }
318
0
    bn_check_top(r);
319
0
320
0
    return 1;
321
0
}