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