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