/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 | 174k | { |
15 | | /* |
16 | | * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d| |
17 | | * always holds) |
18 | | */ |
19 | | |
20 | 174k | if (!(BN_mod(r, m, d, ctx))) |
21 | 0 | return 0; |
22 | 174k | if (!r->neg) |
23 | 170k | return 1; |
24 | | /* now -|d| < r < 0, so we have to set r := r + |d| */ |
25 | 3.73k | return (d->neg ? BN_sub : BN_add) (r, r, d); |
26 | 174k | } |
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 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
63 | 0 | return 0; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 0 | ap = a->d != NULL ? a->d : tp; |
68 | 0 | bp = b->d != NULL ? b->d : tp; |
69 | |
|
70 | 0 | for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) { |
71 | 0 | mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1)); |
72 | 0 | temp = ((ap[ai] & mask) + carry) & BN_MASK2; |
73 | 0 | carry = (temp < carry); |
74 | |
|
75 | 0 | mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1)); |
76 | 0 | tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2; |
77 | 0 | carry += (tp[i] < temp); |
78 | |
|
79 | 0 | i++; |
80 | 0 | ai += (i - a->dmax) >> (8 * sizeof(i) - 1); |
81 | 0 | bi += (i - b->dmax) >> (8 * sizeof(i) - 1); |
82 | 0 | } |
83 | 0 | rp = r->d; |
84 | 0 | carry -= bn_sub_words(rp, tp, m->d, mtop); |
85 | 0 | for (i = 0; i < mtop; i++) { |
86 | 0 | rp[i] = (carry & tp[i]) | (~carry & rp[i]); |
87 | 0 | ((volatile BN_ULONG *)tp)[i] = 0; |
88 | 0 | } |
89 | 0 | r->top = mtop; |
90 | 0 | r->flags |= BN_FLG_FIXED_TOP; |
91 | 0 | r->neg = 0; |
92 | |
|
93 | 0 | if (tp != storage) |
94 | 0 | OPENSSL_free(tp); |
95 | |
|
96 | 0 | return 1; |
97 | 0 | } |
98 | | |
99 | | int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
100 | | const BIGNUM *m) |
101 | 0 | { |
102 | 0 | int ret = bn_mod_add_fixed_top(r, a, b, m); |
103 | |
|
104 | 0 | if (ret) |
105 | 0 | bn_correct_top(r); |
106 | |
|
107 | 0 | return ret; |
108 | 0 | } |
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 | 0 | { |
135 | 0 | size_t i, ai, bi, mtop = m->top; |
136 | 0 | BN_ULONG borrow, carry, ta, tb, mask, *rp; |
137 | 0 | const BN_ULONG *ap, *bp; |
138 | |
|
139 | 0 | if (bn_wexpand(r, mtop) == NULL) |
140 | 0 | return 0; |
141 | | |
142 | 0 | rp = r->d; |
143 | 0 | ap = a->d != NULL ? a->d : rp; |
144 | 0 | bp = b->d != NULL ? b->d : rp; |
145 | |
|
146 | 0 | for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) { |
147 | 0 | mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1)); |
148 | 0 | ta = ap[ai] & mask; |
149 | |
|
150 | 0 | mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1)); |
151 | 0 | tb = bp[bi] & mask; |
152 | 0 | rp[i] = ta - tb - borrow; |
153 | 0 | if (ta != tb) |
154 | 0 | borrow = (ta < tb); |
155 | |
|
156 | 0 | i++; |
157 | 0 | ai += (i - a->dmax) >> (8 * sizeof(i) - 1); |
158 | 0 | bi += (i - b->dmax) >> (8 * sizeof(i) - 1); |
159 | 0 | } |
160 | 0 | ap = m->d; |
161 | 0 | for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) { |
162 | 0 | ta = ((ap[i] & mask) + carry) & BN_MASK2; |
163 | 0 | carry = (ta < carry); |
164 | 0 | rp[i] = (rp[i] + ta) & BN_MASK2; |
165 | 0 | carry += (rp[i] < ta); |
166 | 0 | } |
167 | 0 | borrow -= carry; |
168 | 0 | for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) { |
169 | 0 | ta = ((ap[i] & mask) + carry) & BN_MASK2; |
170 | 0 | carry = (ta < carry); |
171 | 0 | rp[i] = (rp[i] + ta) & BN_MASK2; |
172 | 0 | carry += (rp[i] < ta); |
173 | 0 | } |
174 | |
|
175 | 0 | r->top = mtop; |
176 | 0 | r->flags |= BN_FLG_FIXED_TOP; |
177 | 0 | r->neg = 0; |
178 | |
|
179 | 0 | return 1; |
180 | 0 | } |
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 | 0 | { |
189 | 0 | if (!BN_sub(r, a, b)) |
190 | 0 | return 0; |
191 | 0 | if (r->neg) |
192 | 0 | return BN_add(r, r, m); |
193 | 0 | return 1; |
194 | 0 | } |
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 | 167k | { |
200 | 167k | BIGNUM *t; |
201 | 167k | int ret = 0; |
202 | | |
203 | 167k | bn_check_top(a); |
204 | 167k | bn_check_top(b); |
205 | 167k | bn_check_top(m); |
206 | | |
207 | 167k | BN_CTX_start(ctx); |
208 | 167k | if ((t = BN_CTX_get(ctx)) == NULL) |
209 | 0 | goto err; |
210 | 167k | if (a == b) { |
211 | 136k | if (!BN_sqr(t, a, ctx)) |
212 | 0 | goto err; |
213 | 136k | } else { |
214 | 30.7k | if (!BN_mul(t, a, b, ctx)) |
215 | 0 | goto err; |
216 | 30.7k | } |
217 | 167k | if (!BN_nnmod(r, t, m, ctx)) |
218 | 0 | goto err; |
219 | 167k | bn_check_top(r); |
220 | 167k | ret = 1; |
221 | 167k | err: |
222 | 167k | BN_CTX_end(ctx); |
223 | 167k | return ret; |
224 | 167k | } |
225 | | |
226 | | int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) |
227 | 0 | { |
228 | 0 | if (!BN_sqr(r, a, ctx)) |
229 | 0 | return 0; |
230 | | /* r->neg == 0, thus we don't need BN_nnmod */ |
231 | 0 | return BN_mod(r, r, m, ctx); |
232 | 0 | } |
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 | 0 | { |
248 | 0 | if (!BN_lshift1(r, a)) |
249 | 0 | return 0; |
250 | 0 | bn_check_top(r); |
251 | 0 | if (BN_cmp(r, m) >= 0) |
252 | 0 | return BN_sub(r, r, m); |
253 | 0 | return 1; |
254 | 0 | } |
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 | 0 | { |
285 | 0 | if (r != a) { |
286 | 0 | if (BN_copy(r, a) == NULL) |
287 | 0 | return 0; |
288 | 0 | } |
289 | | |
290 | 0 | while (n > 0) { |
291 | 0 | int max_shift; |
292 | | |
293 | | /* 0 < r < m */ |
294 | 0 | max_shift = BN_num_bits(m) - BN_num_bits(r); |
295 | | /* max_shift >= 0 */ |
296 | |
|
297 | 0 | if (max_shift < 0) { |
298 | 0 | ERR_raise(ERR_LIB_BN, BN_R_INPUT_NOT_REDUCED); |
299 | 0 | return 0; |
300 | 0 | } |
301 | | |
302 | 0 | if (max_shift > n) |
303 | 0 | max_shift = n; |
304 | |
|
305 | 0 | if (max_shift) { |
306 | 0 | if (!BN_lshift(r, r, max_shift)) |
307 | 0 | return 0; |
308 | 0 | n -= max_shift; |
309 | 0 | } else { |
310 | 0 | if (!BN_lshift1(r, r)) |
311 | 0 | return 0; |
312 | 0 | --n; |
313 | 0 | } |
314 | | |
315 | | /* BN_num_bits(r) <= BN_num_bits(m) */ |
316 | | |
317 | 0 | if (BN_cmp(r, m) >= 0) { |
318 | 0 | if (!BN_sub(r, r, m)) |
319 | 0 | return 0; |
320 | 0 | } |
321 | 0 | } |
322 | 0 | bn_check_top(r); |
323 | |
|
324 | 0 | return 1; |
325 | 0 | } |