]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
48e5119a | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
675f605d | 3 | * |
4f22f405 RS |
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 | |
675f605d | 8 | */ |
d02b48c6 | 9 | |
1b3b0a54 | 10 | /* |
b99b1107 BM |
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 | |
1b3b0a54 RE |
15 | */ |
16 | ||
b39fc560 | 17 | #include "internal/cryptlib.h" |
d02b48c6 RE |
18 | #include "bn_lcl.h" |
19 | ||
0f113f3e | 20 | #define MONT_WORD /* use the faster word-based algorithm */ |
6535eb17 | 21 | |
9b4eab50 | 22 | #ifdef MONT_WORD |
71883868 | 23 | static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont); |
9b4eab50 AP |
24 | #endif |
25 | ||
020fc820 | 26 | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
0f113f3e | 27 | BN_MONT_CTX *mont, BN_CTX *ctx) |
71883868 AP |
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) | |
0f113f3e MC |
39 | { |
40 | BIGNUM *tmp; | |
41 | int ret = 0; | |
0f113f3e MC |
42 | int num = mont->N.top; |
43 | ||
3c97e412 | 44 | #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD) |
0f113f3e MC |
45 | if (num > 1 && a->top == num && b->top == num) { |
46 | if (bn_wexpand(r, num) == NULL) | |
26a7d938 | 47 | return 0; |
0f113f3e MC |
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; | |
71883868 | 51 | r->flags |= BN_FLG_FIXED_TOP; |
208fb891 | 52 | return 1; |
0f113f3e MC |
53 | } |
54 | } | |
e7382805 | 55 | #endif |
dfeab068 | 56 | |
3c97e412 AP |
57 | if ((a->top + b->top) > 2 * num) |
58 | return 0; | |
59 | ||
0f113f3e MC |
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 */ | |
9b4eab50 | 74 | #ifdef MONT_WORD |
71883868 | 75 | if (!bn_from_montgomery_word(r, tmp, mont)) |
0f113f3e | 76 | goto err; |
9b4eab50 | 77 | #else |
0f113f3e MC |
78 | if (!BN_from_montgomery(r, tmp, mont, ctx)) |
79 | goto err; | |
9b4eab50 | 80 | #endif |
0f113f3e MC |
81 | ret = 1; |
82 | err: | |
83 | BN_CTX_end(ctx); | |
26a7d938 | 84 | return ret; |
0f113f3e | 85 | } |
d02b48c6 | 86 | |
6535eb17 | 87 | #ifdef MONT_WORD |
71883868 | 88 | static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont) |
0f113f3e MC |
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; | |
208fb891 | 98 | return 1; |
0f113f3e MC |
99 | } |
100 | ||
101 | max = (2 * nl); /* carry is stored separately */ | |
102 | if (bn_wexpand(r, max) == NULL) | |
26a7d938 | 103 | return 0; |
0f113f3e MC |
104 | |
105 | r->neg ^= n->neg; | |
106 | np = n->d; | |
107 | rp = r->d; | |
108 | ||
109 | /* clear the top words of T */ | |
9f040d6d RS |
110 | i = max - r->top; |
111 | if (i) | |
112 | memset(&rp[r->top], 0, sizeof(*rp) * i); | |
0f113f3e MC |
113 | |
114 | r->top = max; | |
71883868 | 115 | r->flags |= BN_FLG_FIXED_TOP; |
0f113f3e MC |
116 | n0 = mont->n0[0]; |
117 | ||
f345b1f3 DB |
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 | */ | |
0f113f3e MC |
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) | |
26a7d938 | 132 | return 0; |
0f113f3e | 133 | ret->top = nl; |
71883868 | 134 | ret->flags |= BN_FLG_FIXED_TOP; |
0f113f3e MC |
135 | ret->neg = r->neg; |
136 | ||
137 | rp = ret->d; | |
f345b1f3 DB |
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 | */ | |
0f113f3e MC |
143 | ap = &(r->d[nl]); |
144 | ||
6c90182a | 145 | carry -= bn_sub_words(rp, ap, np, nl); |
f345b1f3 | 146 | /* |
6c90182a AP |
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. | |
f345b1f3 | 150 | */ |
f345b1f3 | 151 | for (i = 0; i < nl; i++) { |
6c90182a | 152 | rp[i] = (carry & ap[i]) | (~carry & rp[i]); |
f345b1f3 | 153 | ap[i] = 0; |
0f113f3e | 154 | } |
0f113f3e | 155 | |
208fb891 | 156 | return 1; |
0f113f3e MC |
157 | } |
158 | #endif /* MONT_WORD */ | |
9b4eab50 AP |
159 | |
160 | int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, | |
0f113f3e MC |
161 | BN_CTX *ctx) |
162 | { | |
163 | int retn = 0; | |
9b4eab50 | 164 | #ifdef MONT_WORD |
0f113f3e MC |
165 | BIGNUM *t; |
166 | ||
167 | BN_CTX_start(ctx); | |
71883868 AP |
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 | } | |
0f113f3e MC |
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); | |
edea42c6 | 180 | if (t2 == NULL) |
0f113f3e MC |
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); | |
e93f9a32 | 204 | err: |
0f113f3e MC |
205 | BN_CTX_end(ctx); |
206 | #endif /* MONT_WORD */ | |
26a7d938 | 207 | return retn; |
0f113f3e | 208 | } |
d02b48c6 | 209 | |
71883868 AP |
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 | ||
6b691a5c | 216 | BN_MONT_CTX *BN_MONT_CTX_new(void) |
0f113f3e MC |
217 | { |
218 | BN_MONT_CTX *ret; | |
d02b48c6 | 219 | |
f06080cb F |
220 | if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) { |
221 | BNerr(BN_F_BN_MONT_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
26a7d938 | 222 | return NULL; |
f06080cb | 223 | } |
dfeab068 | 224 | |
0f113f3e MC |
225 | BN_MONT_CTX_init(ret); |
226 | ret->flags = BN_FLG_MALLOCED; | |
26a7d938 | 227 | return ret; |
0f113f3e | 228 | } |
d02b48c6 | 229 | |
6b691a5c | 230 | void BN_MONT_CTX_init(BN_MONT_CTX *ctx) |
0f113f3e MC |
231 | { |
232 | ctx->ri = 0; | |
e6e9170d RS |
233 | bn_init(&ctx->RR); |
234 | bn_init(&ctx->N); | |
235 | bn_init(&ctx->Ni); | |
0f113f3e MC |
236 | ctx->n0[0] = ctx->n0[1] = 0; |
237 | ctx->flags = 0; | |
238 | } | |
dfeab068 | 239 | |
6b691a5c | 240 | void BN_MONT_CTX_free(BN_MONT_CTX *mont) |
0f113f3e | 241 | { |
e6e9170d RS |
242 | if (mont == NULL) |
243 | return; | |
244 | BN_clear_free(&mont->RR); | |
245 | BN_clear_free(&mont->N); | |
246 | BN_clear_free(&mont->Ni); | |
0f113f3e MC |
247 | if (mont->flags & BN_FLG_MALLOCED) |
248 | OPENSSL_free(mont); | |
249 | } | |
d02b48c6 | 250 | |
84c15db5 | 251 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) |
0f113f3e | 252 | { |
71883868 | 253 | int i, ret = 0; |
0f113f3e MC |
254 | BIGNUM *Ri, *R; |
255 | ||
6a009812 MC |
256 | if (BN_is_zero(mod)) |
257 | return 0; | |
258 | ||
0f113f3e MC |
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 */ | |
7d461736 MC |
265 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) |
266 | BN_set_flags(&(mont->N), BN_FLG_CONSTTIME); | |
0f113f3e | 267 | mont->N.neg = 0; |
dfeab068 | 268 | |
6535eb17 | 269 | #ifdef MONT_WORD |
0f113f3e MC |
270 | { |
271 | BIGNUM tmod; | |
272 | BN_ULONG buf[2]; | |
273 | ||
d59c7c81 | 274 | bn_init(&tmod); |
0f113f3e MC |
275 | tmod.d = buf; |
276 | tmod.dmax = 2; | |
277 | tmod.neg = 0; | |
278 | ||
3de81a59 SW |
279 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) |
280 | BN_set_flags(&tmod, BN_FLG_CONSTTIME); | |
281 | ||
0f113f3e MC |
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 | ||
b1860d6c MC |
302 | if (BN_is_one(&tmod)) |
303 | BN_zero(Ri); | |
304 | else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) | |
0f113f3e MC |
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 */ | |
b1860d6c MC |
337 | if (BN_is_one(&tmod)) |
338 | BN_zero(Ri); | |
339 | else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) | |
0f113f3e MC |
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 | } | |
d02b48c6 RE |
379 | #endif |
380 | ||
0f113f3e MC |
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; | |
d02b48c6 | 387 | |
71883868 AP |
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 | ||
0f113f3e MC |
393 | ret = 1; |
394 | err: | |
395 | BN_CTX_end(ctx); | |
396 | return ret; | |
397 | } | |
d02b48c6 | 398 | |
6b691a5c | 399 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from) |
0f113f3e MC |
400 | { |
401 | if (to == from) | |
26a7d938 | 402 | return to; |
0f113f3e MC |
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]; | |
26a7d938 | 413 | return to; |
0f113f3e | 414 | } |
dfeab068 | 415 | |
d188a536 | 416 | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock, |
0f113f3e MC |
417 | const BIGNUM *mod, BN_CTX *ctx) |
418 | { | |
419 | BN_MONT_CTX *ret; | |
420 | ||
d188a536 | 421 | CRYPTO_THREAD_read_lock(lock); |
0f113f3e | 422 | ret = *pmont; |
d188a536 | 423 | CRYPTO_THREAD_unlock(lock); |
0f113f3e MC |
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(); | |
90945fa3 | 436 | if (ret == NULL) |
0f113f3e MC |
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. */ | |
d188a536 | 444 | CRYPTO_THREAD_write_lock(lock); |
0f113f3e MC |
445 | if (*pmont) { |
446 | BN_MONT_CTX_free(ret); | |
447 | ret = *pmont; | |
448 | } else | |
449 | *pmont = ret; | |
d188a536 | 450 | CRYPTO_THREAD_unlock(lock); |
0f113f3e MC |
451 | return ret; |
452 | } |