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