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