/src/openssl/crypto/bn/bn_mont.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-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  |  | /*  | 
11  |  |  * Details about Montgomery multiplication algorithms can be found at  | 
12  |  |  * http://security.ece.orst.edu/publications.html, e.g.  | 
13  |  |  * http://security.ece.orst.edu/koc/papers/j37acmon.pdf and  | 
14  |  |  * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf  | 
15  |  |  */  | 
16  |  |  | 
17  |  | #include "internal/cryptlib.h"  | 
18  |  | #include "bn_local.h"  | 
19  |  |  | 
20  |  | #define MONT_WORD               /* use the faster word-based algorithm */  | 
21  |  |  | 
22  |  | #ifdef MONT_WORD  | 
23  |  | static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont);  | 
24  |  | #endif  | 
25  |  |  | 
26  |  | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,  | 
27  |  |                           BN_MONT_CTX *mont, BN_CTX *ctx)  | 
28  | 0  | { | 
29  | 0  |     int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);  | 
30  |  | 
  | 
31  | 0  |     bn_correct_top(r);  | 
32  | 0  |     bn_check_top(r);  | 
33  |  | 
  | 
34  | 0  |     return ret;  | 
35  | 0  | }  | 
36  |  |  | 
37  |  | int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,  | 
38  |  |                           BN_MONT_CTX *mont, BN_CTX *ctx)  | 
39  | 0  | { | 
40  | 0  |     BIGNUM *tmp;  | 
41  | 0  |     int ret = 0;  | 
42  | 0  |     int num = mont->N.top;  | 
43  |  | 
  | 
44  | 0  | #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)  | 
45  | 0  |     if (num > 1 && num <= BN_SOFT_LIMIT && a->top == num && b->top == num) { | 
46  | 0  |         if (bn_wexpand(r, num) == NULL)  | 
47  | 0  |             return 0;  | 
48  | 0  |         if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) { | 
49  | 0  |             r->neg = a->neg ^ b->neg;  | 
50  | 0  |             r->top = num;  | 
51  | 0  |             r->flags |= BN_FLG_FIXED_TOP;  | 
52  | 0  |             return 1;  | 
53  | 0  |         }  | 
54  | 0  |     }  | 
55  | 0  | #endif  | 
56  |  |  | 
57  | 0  |     if ((a->top + b->top) > 2 * num)  | 
58  | 0  |         return 0;  | 
59  |  |  | 
60  | 0  |     BN_CTX_start(ctx);  | 
61  | 0  |     tmp = BN_CTX_get(ctx);  | 
62  | 0  |     if (tmp == NULL)  | 
63  | 0  |         goto err;  | 
64  |  |  | 
65  | 0  |     bn_check_top(tmp);  | 
66  | 0  |     if (a == b) { | 
67  | 0  |         if (!bn_sqr_fixed_top(tmp, a, ctx))  | 
68  | 0  |             goto err;  | 
69  | 0  |     } else { | 
70  | 0  |         if (!bn_mul_fixed_top(tmp, a, b, ctx))  | 
71  | 0  |             goto err;  | 
72  | 0  |     }  | 
73  |  |     /* reduce from aRR to aR */  | 
74  | 0  | #ifdef MONT_WORD  | 
75  | 0  |     if (!bn_from_montgomery_word(r, tmp, mont))  | 
76  | 0  |         goto err;  | 
77  |  | #else  | 
78  |  |     if (!BN_from_montgomery(r, tmp, mont, ctx))  | 
79  |  |         goto err;  | 
80  |  | #endif  | 
81  | 0  |     ret = 1;  | 
82  | 0  |  err:  | 
83  | 0  |     BN_CTX_end(ctx);  | 
84  | 0  |     return ret;  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | #ifdef MONT_WORD  | 
88  |  | static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)  | 
89  | 0  | { | 
90  | 0  |     BIGNUM *n;  | 
91  | 0  |     BN_ULONG *ap, *np, *rp, n0, v, carry;  | 
92  | 0  |     int nl, max, i;  | 
93  | 0  |     unsigned int rtop;  | 
94  |  | 
  | 
95  | 0  |     n = &(mont->N);  | 
96  | 0  |     nl = n->top;  | 
97  | 0  |     if (nl == 0) { | 
98  | 0  |         ret->top = 0;  | 
99  | 0  |         return 1;  | 
100  | 0  |     }  | 
101  |  |  | 
102  | 0  |     max = (2 * nl);             /* carry is stored separately */  | 
103  | 0  |     if (bn_wexpand(r, max) == NULL)  | 
104  | 0  |         return 0;  | 
105  |  |  | 
106  | 0  |     r->neg ^= n->neg;  | 
107  | 0  |     np = n->d;  | 
108  | 0  |     rp = r->d;  | 
109  |  |  | 
110  |  |     /* clear the top words of T */  | 
111  | 0  |     for (rtop = r->top, i = 0; i < max; i++) { | 
112  | 0  |         v = (BN_ULONG)0 - ((i - rtop) >> (8 * sizeof(rtop) - 1));  | 
113  | 0  |         rp[i] &= v;  | 
114  | 0  |     }  | 
115  |  | 
  | 
116  | 0  |     r->top = max;  | 
117  | 0  |     r->flags |= BN_FLG_FIXED_TOP;  | 
118  | 0  |     n0 = mont->n0[0];  | 
119  |  |  | 
120  |  |     /*  | 
121  |  |      * Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On  | 
122  |  |      * input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|  | 
123  |  |      * includes |carry| which is stored separately.  | 
124  |  |      */  | 
125  | 0  |     for (carry = 0, i = 0; i < nl; i++, rp++) { | 
126  | 0  |         v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);  | 
127  | 0  |         v = (v + carry + rp[nl]) & BN_MASK2;  | 
128  | 0  |         carry |= (v != rp[nl]);  | 
129  | 0  |         carry &= (v <= rp[nl]);  | 
130  | 0  |         rp[nl] = v;  | 
131  | 0  |     }  | 
132  |  | 
  | 
133  | 0  |     if (bn_wexpand(ret, nl) == NULL)  | 
134  | 0  |         return 0;  | 
135  | 0  |     ret->top = nl;  | 
136  | 0  |     ret->flags |= BN_FLG_FIXED_TOP;  | 
137  | 0  |     ret->neg = r->neg;  | 
138  |  | 
  | 
139  | 0  |     rp = ret->d;  | 
140  |  |  | 
141  |  |     /*  | 
142  |  |      * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|  | 
143  |  |      * includes |carry| which is stored separately.  | 
144  |  |      */  | 
145  | 0  |     ap = &(r->d[nl]);  | 
146  |  | 
  | 
147  | 0  |     carry -= bn_sub_words(rp, ap, np, nl);  | 
148  |  |     /*  | 
149  |  |      * |carry| is -1 if |ap| - |np| underflowed or zero if it did not. Note  | 
150  |  |      * |carry| cannot be 1. That would imply the subtraction did not fit in  | 
151  |  |      * |nl| words, and we know at most one subtraction is needed.  | 
152  |  |      */  | 
153  | 0  |     for (i = 0; i < nl; i++) { | 
154  | 0  |         rp[i] = (carry & ap[i]) | (~carry & rp[i]);  | 
155  | 0  |         ap[i] = 0;  | 
156  | 0  |     }  | 
157  |  | 
  | 
158  | 0  |     return 1;  | 
159  | 0  | }  | 
160  |  | #endif                          /* MONT_WORD */  | 
161  |  |  | 
162  |  | int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,  | 
163  |  |                        BN_CTX *ctx)  | 
164  | 0  | { | 
165  | 0  |     int retn;  | 
166  |  | 
  | 
167  | 0  |     retn = bn_from_mont_fixed_top(ret, a, mont, ctx);  | 
168  | 0  |     bn_correct_top(ret);  | 
169  | 0  |     bn_check_top(ret);  | 
170  |  | 
  | 
171  | 0  |     return retn;  | 
172  | 0  | }  | 
173  |  |  | 
174  |  | int bn_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,  | 
175  |  |                            BN_CTX *ctx)  | 
176  | 0  | { | 
177  | 0  |     int retn = 0;  | 
178  | 0  | #ifdef MONT_WORD  | 
179  | 0  |     BIGNUM *t;  | 
180  |  | 
  | 
181  | 0  |     BN_CTX_start(ctx);  | 
182  | 0  |     if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) { | 
183  | 0  |         retn = bn_from_montgomery_word(ret, t, mont);  | 
184  | 0  |     }  | 
185  | 0  |     BN_CTX_end(ctx);  | 
186  |  | #else                           /* !MONT_WORD */  | 
187  |  |     BIGNUM *t1, *t2;  | 
188  |  |  | 
189  |  |     BN_CTX_start(ctx);  | 
190  |  |     t1 = BN_CTX_get(ctx);  | 
191  |  |     t2 = BN_CTX_get(ctx);  | 
192  |  |     if (t2 == NULL)  | 
193  |  |         goto err;  | 
194  |  |  | 
195  |  |     if (!BN_copy(t1, a))  | 
196  |  |         goto err;  | 
197  |  |     BN_mask_bits(t1, mont->ri);  | 
198  |  |  | 
199  |  |     if (!BN_mul(t2, t1, &mont->Ni, ctx))  | 
200  |  |         goto err;  | 
201  |  |     BN_mask_bits(t2, mont->ri);  | 
202  |  |  | 
203  |  |     if (!BN_mul(t1, t2, &mont->N, ctx))  | 
204  |  |         goto err;  | 
205  |  |     if (!BN_add(t2, a, t1))  | 
206  |  |         goto err;  | 
207  |  |     if (!BN_rshift(ret, t2, mont->ri))  | 
208  |  |         goto err;  | 
209  |  |  | 
210  |  |     if (BN_ucmp(ret, &(mont->N)) >= 0) { | 
211  |  |         if (!BN_usub(ret, ret, &(mont->N)))  | 
212  |  |             goto err;  | 
213  |  |     }  | 
214  |  |     retn = 1;  | 
215  |  |     bn_check_top(ret);  | 
216  |  |  err:  | 
217  |  |     BN_CTX_end(ctx);  | 
218  |  | #endif                          /* MONT_WORD */  | 
219  | 0  |     return retn;  | 
220  | 0  | }  | 
221  |  |  | 
222  |  | int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,  | 
223  |  |                          BN_CTX *ctx)  | 
224  | 0  | { | 
225  | 0  |     return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);  | 
226  | 0  | }  | 
227  |  |  | 
228  |  | BN_MONT_CTX *BN_MONT_CTX_new(void)  | 
229  | 0  | { | 
230  | 0  |     BN_MONT_CTX *ret;  | 
231  |  | 
  | 
232  | 0  |     if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)  | 
233  | 0  |         return NULL;  | 
234  |  |  | 
235  | 0  |     BN_MONT_CTX_init(ret);  | 
236  | 0  |     ret->flags = BN_FLG_MALLOCED;  | 
237  | 0  |     return ret;  | 
238  | 0  | }  | 
239  |  |  | 
240  |  | void BN_MONT_CTX_init(BN_MONT_CTX *ctx)  | 
241  | 0  | { | 
242  | 0  |     ctx->ri = 0;  | 
243  | 0  |     bn_init(&ctx->RR);  | 
244  | 0  |     bn_init(&ctx->N);  | 
245  | 0  |     bn_init(&ctx->Ni);  | 
246  | 0  |     ctx->n0[0] = ctx->n0[1] = 0;  | 
247  | 0  |     ctx->flags = 0;  | 
248  | 0  | }  | 
249  |  |  | 
250  |  | void BN_MONT_CTX_free(BN_MONT_CTX *mont)  | 
251  | 176k  | { | 
252  | 176k  |     if (mont == NULL)  | 
253  | 176k  |         return;  | 
254  | 0  |     BN_clear_free(&mont->RR);  | 
255  | 0  |     BN_clear_free(&mont->N);  | 
256  | 0  |     BN_clear_free(&mont->Ni);  | 
257  | 0  |     if (mont->flags & BN_FLG_MALLOCED)  | 
258  | 0  |         OPENSSL_free(mont);  | 
259  | 0  | }  | 
260  |  |  | 
261  |  | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)  | 
262  | 0  | { | 
263  | 0  |     int i, ret = 0;  | 
264  | 0  |     BIGNUM *Ri, *R;  | 
265  |  | 
  | 
266  | 0  |     if (BN_is_zero(mod))  | 
267  | 0  |         return 0;  | 
268  |  |  | 
269  | 0  |     BN_CTX_start(ctx);  | 
270  | 0  |     if ((Ri = BN_CTX_get(ctx)) == NULL)  | 
271  | 0  |         goto err;  | 
272  | 0  |     R = &(mont->RR);            /* grab RR as a temp */  | 
273  | 0  |     if (!BN_copy(&(mont->N), mod))  | 
274  | 0  |         goto err;               /* Set N */  | 
275  | 0  |     if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)  | 
276  | 0  |         BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);  | 
277  | 0  |     mont->N.neg = 0;  | 
278  |  | 
  | 
279  | 0  | #ifdef MONT_WORD  | 
280  | 0  |     { | 
281  | 0  |         BIGNUM tmod;  | 
282  | 0  |         BN_ULONG buf[2];  | 
283  |  | 
  | 
284  | 0  |         bn_init(&tmod);  | 
285  | 0  |         tmod.d = buf;  | 
286  | 0  |         tmod.dmax = 2;  | 
287  | 0  |         tmod.neg = 0;  | 
288  |  | 
  | 
289  | 0  |         if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)  | 
290  | 0  |             BN_set_flags(&tmod, BN_FLG_CONSTTIME);  | 
291  |  | 
  | 
292  | 0  |         mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;  | 
293  |  | 
  | 
294  |  | # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)  | 
295  |  |         /*  | 
296  |  |          * Only certain BN_BITS2<=32 platforms actually make use of n0[1],  | 
297  |  |          * and we could use the #else case (with a shorter R value) for the  | 
298  |  |          * others.  However, currently only the assembler files do know which  | 
299  |  |          * is which.  | 
300  |  |          */  | 
301  |  |  | 
302  |  |         BN_zero(R);  | 
303  |  |         if (!(BN_set_bit(R, 2 * BN_BITS2)))  | 
304  |  |             goto err;  | 
305  |  |  | 
306  |  |         tmod.top = 0;  | 
307  |  |         if ((buf[0] = mod->d[0]))  | 
308  |  |             tmod.top = 1;  | 
309  |  |         if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))  | 
310  |  |             tmod.top = 2;  | 
311  |  |  | 
312  |  |         if (BN_is_one(&tmod))  | 
313  |  |             BN_zero(Ri);  | 
314  |  |         else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)  | 
315  |  |             goto err;  | 
316  |  |         if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))  | 
317  |  |             goto err;           /* R*Ri */  | 
318  |  |         if (!BN_is_zero(Ri)) { | 
319  |  |             if (!BN_sub_word(Ri, 1))  | 
320  |  |                 goto err;  | 
321  |  |         } else {                /* if N mod word size == 1 */ | 
322  |  |  | 
323  |  |             if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)  | 
324  |  |                 goto err;  | 
325  |  |             /* Ri-- (mod double word size) */  | 
326  |  |             Ri->neg = 0;  | 
327  |  |             Ri->d[0] = BN_MASK2;  | 
328  |  |             Ri->d[1] = BN_MASK2;  | 
329  |  |             Ri->top = 2;  | 
330  |  |         }  | 
331  |  |         if (!BN_div(Ri, NULL, Ri, &tmod, ctx))  | 
332  |  |             goto err;  | 
333  |  |         /*  | 
334  |  |          * Ni = (R*Ri-1)/N, keep only couple of least significant words:  | 
335  |  |          */  | 
336  |  |         mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;  | 
337  |  |         mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;  | 
338  |  | # else  | 
339  | 0  |         BN_zero(R);  | 
340  | 0  |         if (!(BN_set_bit(R, BN_BITS2)))  | 
341  | 0  |             goto err;           /* R */  | 
342  |  |  | 
343  | 0  |         buf[0] = mod->d[0];     /* tmod = N mod word size */  | 
344  | 0  |         buf[1] = 0;  | 
345  | 0  |         tmod.top = buf[0] != 0 ? 1 : 0;  | 
346  |  |         /* Ri = R^-1 mod N */  | 
347  | 0  |         if (BN_is_one(&tmod))  | 
348  | 0  |             BN_zero(Ri);  | 
349  | 0  |         else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)  | 
350  | 0  |             goto err;  | 
351  | 0  |         if (!BN_lshift(Ri, Ri, BN_BITS2))  | 
352  | 0  |             goto err;           /* R*Ri */  | 
353  | 0  |         if (!BN_is_zero(Ri)) { | 
354  | 0  |             if (!BN_sub_word(Ri, 1))  | 
355  | 0  |                 goto err;  | 
356  | 0  |         } else {                /* if N mod word size == 1 */ | 
357  |  | 
  | 
358  | 0  |             if (!BN_set_word(Ri, BN_MASK2))  | 
359  | 0  |                 goto err;       /* Ri-- (mod word size) */  | 
360  | 0  |         }  | 
361  | 0  |         if (!BN_div(Ri, NULL, Ri, &tmod, ctx))  | 
362  | 0  |             goto err;  | 
363  |  |         /*  | 
364  |  |          * Ni = (R*Ri-1)/N, keep only least significant word:  | 
365  |  |          */  | 
366  | 0  |         mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;  | 
367  | 0  |         mont->n0[1] = 0;  | 
368  | 0  | # endif  | 
369  | 0  |     }  | 
370  |  | #else                           /* !MONT_WORD */  | 
371  |  |     {                           /* bignum version */ | 
372  |  |         mont->ri = BN_num_bits(&mont->N);  | 
373  |  |         BN_zero(R);  | 
374  |  |         if (!BN_set_bit(R, mont->ri))  | 
375  |  |             goto err;           /* R = 2^ri */  | 
376  |  |         /* Ri = R^-1 mod N */  | 
377  |  |         if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)  | 
378  |  |             goto err;  | 
379  |  |         if (!BN_lshift(Ri, Ri, mont->ri))  | 
380  |  |             goto err;           /* R*Ri */  | 
381  |  |         if (!BN_sub_word(Ri, 1))  | 
382  |  |             goto err;  | 
383  |  |         /*  | 
384  |  |          * Ni = (R*Ri-1) / N  | 
385  |  |          */  | 
386  |  |         if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))  | 
387  |  |             goto err;  | 
388  |  |     }  | 
389  |  | #endif  | 
390  |  |  | 
391  |  |     /* setup RR for conversions */  | 
392  | 0  |     BN_zero(&(mont->RR));  | 
393  | 0  |     if (!BN_set_bit(&(mont->RR), mont->ri * 2))  | 
394  | 0  |         goto err;  | 
395  | 0  |     if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))  | 
396  | 0  |         goto err;  | 
397  |  |  | 
398  | 0  |     for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)  | 
399  | 0  |         mont->RR.d[i] = 0;  | 
400  | 0  |     mont->RR.top = ret;  | 
401  | 0  |     mont->RR.flags |= BN_FLG_FIXED_TOP;  | 
402  |  | 
  | 
403  | 0  |     ret = 1;  | 
404  | 0  |  err:  | 
405  | 0  |     BN_CTX_end(ctx);  | 
406  | 0  |     return ret;  | 
407  | 0  | }  | 
408  |  |  | 
409  |  | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)  | 
410  | 0  | { | 
411  | 0  |     if (to == from)  | 
412  | 0  |         return to;  | 
413  |  |  | 
414  | 0  |     if (!BN_copy(&(to->RR), &(from->RR)))  | 
415  | 0  |         return NULL;  | 
416  | 0  |     if (!BN_copy(&(to->N), &(from->N)))  | 
417  | 0  |         return NULL;  | 
418  | 0  |     if (!BN_copy(&(to->Ni), &(from->Ni)))  | 
419  | 0  |         return NULL;  | 
420  | 0  |     to->ri = from->ri;  | 
421  | 0  |     to->n0[0] = from->n0[0];  | 
422  | 0  |     to->n0[1] = from->n0[1];  | 
423  | 0  |     return to;  | 
424  | 0  | }  | 
425  |  |  | 
426  |  | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,  | 
427  |  |                                     const BIGNUM *mod, BN_CTX *ctx)  | 
428  | 0  | { | 
429  | 0  |     BN_MONT_CTX *ret;  | 
430  |  | 
  | 
431  | 0  |     if (!CRYPTO_THREAD_read_lock(lock))  | 
432  | 0  |         return NULL;  | 
433  | 0  |     ret = *pmont;  | 
434  | 0  |     CRYPTO_THREAD_unlock(lock);  | 
435  | 0  |     if (ret)  | 
436  | 0  |         return ret;  | 
437  |  |  | 
438  |  |     /*  | 
439  |  |      * We don't want to serialize globally while doing our lazy-init math in  | 
440  |  |      * BN_MONT_CTX_set. That punishes threads that are doing independent  | 
441  |  |      * things. Instead, punish the case where more than one thread tries to  | 
442  |  |      * lazy-init the same 'pmont', by having each do the lazy-init math work  | 
443  |  |      * independently and only use the one from the thread that wins the race  | 
444  |  |      * (the losers throw away the work they've done).  | 
445  |  |      */  | 
446  | 0  |     ret = BN_MONT_CTX_new();  | 
447  | 0  |     if (ret == NULL)  | 
448  | 0  |         return NULL;  | 
449  | 0  |     if (!BN_MONT_CTX_set(ret, mod, ctx)) { | 
450  | 0  |         BN_MONT_CTX_free(ret);  | 
451  | 0  |         return NULL;  | 
452  | 0  |     }  | 
453  |  |  | 
454  |  |     /* The locked compare-and-set, after the local work is done. */  | 
455  | 0  |     if (!CRYPTO_THREAD_write_lock(lock)) { | 
456  | 0  |         BN_MONT_CTX_free(ret);  | 
457  | 0  |         return NULL;  | 
458  | 0  |     }  | 
459  |  |  | 
460  | 0  |     if (*pmont) { | 
461  | 0  |         BN_MONT_CTX_free(ret);  | 
462  | 0  |         ret = *pmont;  | 
463  | 0  |     } else  | 
464  | 0  |         *pmont = ret;  | 
465  | 0  |     CRYPTO_THREAD_unlock(lock);  | 
466  | 0  |     return ret;  | 
467  | 0  | }  | 
468  |  |  | 
469  |  | int ossl_bn_mont_ctx_set(BN_MONT_CTX *ctx, const BIGNUM *modulus, int ri, const unsigned char *rr,  | 
470  |  |                          int rrlen, uint32_t nlo, uint32_t nhi)  | 
471  | 0  | { | 
472  | 0  |     if (BN_copy(&ctx->N, modulus) == NULL)  | 
473  | 0  |         return 0;  | 
474  | 0  |     if (BN_bin2bn(rr, rrlen, &ctx->RR) == NULL)  | 
475  | 0  |         return 0;  | 
476  | 0  |     ctx->ri = ri;  | 
477  |  | #if (BN_BITS2 <= 32) && defined(OPENSSL_BN_ASM_MONT)  | 
478  |  |     ctx->n0[0] = nlo;  | 
479  |  |     ctx->n0[1] = nhi;  | 
480  |  | #elif BN_BITS2 <= 32  | 
481  |  |     ctx->n0[0] = nlo;  | 
482  |  |     ctx->n0[1] = 0;  | 
483  |  | #else  | 
484  | 0  |     ctx->n0[0] = ((BN_ULONG)nhi << 32)| nlo;  | 
485  | 0  |     ctx->n0[1] = 0;  | 
486  | 0  | #endif  | 
487  |  | 
  | 
488  | 0  |     return 1;  | 
489  | 0  | }  | 
490  |  |  | 
491  |  | int ossl_bn_mont_ctx_eq(const BN_MONT_CTX *m1, const BN_MONT_CTX *m2)  | 
492  | 0  | { | 
493  | 0  |     if (m1->ri != m2->ri)  | 
494  | 0  |         return 0;  | 
495  | 0  |     if (BN_cmp(&m1->RR, &m2->RR) != 0)  | 
496  | 0  |         return 0;  | 
497  | 0  |     if (m1->flags != m2->flags)  | 
498  | 0  |         return 0;  | 
499  | 0  | #ifdef MONT_WORD  | 
500  | 0  |     if (m1->n0[0] != m2->n0[0])  | 
501  | 0  |         return 0;  | 
502  | 0  |     if (m1->n0[1] != m2->n0[1])  | 
503  | 0  |         return 0;  | 
504  |  | #else  | 
505  |  |     if (BN_cmp(&m1->Ni, &m2->Ni) != 0)  | 
506  |  |         return 0;  | 
507  |  | #endif  | 
508  | 0  |     return 1;  | 
509  | 0  | }  |