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