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