]>
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 AP |
22 | #ifdef MONT_WORD |
23 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont); | |
24 | #endif | |
25 | ||
020fc820 | 26 | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
0f113f3e MC |
27 | BN_MONT_CTX *mont, BN_CTX *ctx) |
28 | { | |
29 | BIGNUM *tmp; | |
30 | int ret = 0; | |
9b4eab50 | 31 | #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD) |
0f113f3e MC |
32 | int num = mont->N.top; |
33 | ||
34 | if (num > 1 && a->top == num && b->top == num) { | |
35 | if (bn_wexpand(r, num) == NULL) | |
26a7d938 | 36 | return 0; |
0f113f3e MC |
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); | |
208fb891 | 41 | return 1; |
0f113f3e MC |
42 | } |
43 | } | |
e7382805 | 44 | #endif |
dfeab068 | 45 | |
0f113f3e MC |
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 */ | |
9b4eab50 | 60 | #ifdef MONT_WORD |
0f113f3e MC |
61 | if (!BN_from_montgomery_word(r, tmp, mont)) |
62 | goto err; | |
9b4eab50 | 63 | #else |
0f113f3e MC |
64 | if (!BN_from_montgomery(r, tmp, mont, ctx)) |
65 | goto err; | |
9b4eab50 | 66 | #endif |
0f113f3e MC |
67 | bn_check_top(r); |
68 | ret = 1; | |
69 | err: | |
70 | BN_CTX_end(ctx); | |
26a7d938 | 71 | return ret; |
0f113f3e | 72 | } |
d02b48c6 | 73 | |
6535eb17 | 74 | #ifdef MONT_WORD |
9b4eab50 | 75 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont) |
0f113f3e MC |
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; | |
208fb891 | 85 | return 1; |
0f113f3e MC |
86 | } |
87 | ||
88 | max = (2 * nl); /* carry is stored separately */ | |
89 | if (bn_wexpand(r, max) == NULL) | |
26a7d938 | 90 | return 0; |
0f113f3e MC |
91 | |
92 | r->neg ^= n->neg; | |
93 | np = n->d; | |
94 | rp = r->d; | |
95 | ||
96 | /* clear the top words of T */ | |
9f040d6d | 97 | i = max - r->top; |
f91e026e BE |
98 | if (i < 0) |
99 | return 0; | |
9f040d6d RS |
100 | if (i) |
101 | memset(&rp[r->top], 0, sizeof(*rp) * i); | |
0f113f3e MC |
102 | |
103 | r->top = max; | |
104 | n0 = mont->n0[0]; | |
105 | ||
f345b1f3 DB |
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 | */ | |
0f113f3e MC |
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) | |
26a7d938 | 120 | return 0; |
0f113f3e MC |
121 | ret->top = nl; |
122 | ret->neg = r->neg; | |
123 | ||
124 | rp = ret->d; | |
f345b1f3 DB |
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 | */ | |
0f113f3e MC |
130 | ap = &(r->d[nl]); |
131 | ||
f345b1f3 DB |
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; | |
0f113f3e | 142 | } |
0f113f3e MC |
143 | bn_correct_top(r); |
144 | bn_correct_top(ret); | |
145 | bn_check_top(ret); | |
146 | ||
208fb891 | 147 | return 1; |
0f113f3e MC |
148 | } |
149 | #endif /* MONT_WORD */ | |
9b4eab50 AP |
150 | |
151 | int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, | |
0f113f3e MC |
152 | BN_CTX *ctx) |
153 | { | |
154 | int retn = 0; | |
9b4eab50 | 155 | #ifdef MONT_WORD |
0f113f3e MC |
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); | |
edea42c6 | 168 | if (t2 == NULL) |
0f113f3e MC |
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); | |
e93f9a32 | 192 | err: |
0f113f3e MC |
193 | BN_CTX_end(ctx); |
194 | #endif /* MONT_WORD */ | |
26a7d938 | 195 | return retn; |
0f113f3e | 196 | } |
d02b48c6 | 197 | |
6b691a5c | 198 | BN_MONT_CTX *BN_MONT_CTX_new(void) |
0f113f3e MC |
199 | { |
200 | BN_MONT_CTX *ret; | |
d02b48c6 | 201 | |
f06080cb F |
202 | if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) { |
203 | BNerr(BN_F_BN_MONT_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
26a7d938 | 204 | return NULL; |
f06080cb | 205 | } |
dfeab068 | 206 | |
0f113f3e MC |
207 | BN_MONT_CTX_init(ret); |
208 | ret->flags = BN_FLG_MALLOCED; | |
26a7d938 | 209 | return ret; |
0f113f3e | 210 | } |
d02b48c6 | 211 | |
6b691a5c | 212 | void BN_MONT_CTX_init(BN_MONT_CTX *ctx) |
0f113f3e MC |
213 | { |
214 | ctx->ri = 0; | |
e6e9170d RS |
215 | bn_init(&ctx->RR); |
216 | bn_init(&ctx->N); | |
217 | bn_init(&ctx->Ni); | |
0f113f3e MC |
218 | ctx->n0[0] = ctx->n0[1] = 0; |
219 | ctx->flags = 0; | |
220 | } | |
dfeab068 | 221 | |
6b691a5c | 222 | void BN_MONT_CTX_free(BN_MONT_CTX *mont) |
0f113f3e | 223 | { |
e6e9170d RS |
224 | if (mont == NULL) |
225 | return; | |
226 | BN_clear_free(&mont->RR); | |
227 | BN_clear_free(&mont->N); | |
228 | BN_clear_free(&mont->Ni); | |
0f113f3e MC |
229 | if (mont->flags & BN_FLG_MALLOCED) |
230 | OPENSSL_free(mont); | |
231 | } | |
d02b48c6 | 232 | |
84c15db5 | 233 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) |
0f113f3e MC |
234 | { |
235 | int ret = 0; | |
236 | BIGNUM *Ri, *R; | |
237 | ||
6a009812 MC |
238 | if (BN_is_zero(mod)) |
239 | return 0; | |
240 | ||
0f113f3e MC |
241 | BN_CTX_start(ctx); |
242 | if ((Ri = BN_CTX_get(ctx)) == NULL) | |
243 | goto err; | |
244 | R = &(mont->RR); /* grab RR as a temp */ | |
245 | if (!BN_copy(&(mont->N), mod)) | |
246 | goto err; /* Set N */ | |
7d461736 MC |
247 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) |
248 | BN_set_flags(&(mont->N), BN_FLG_CONSTTIME); | |
0f113f3e | 249 | mont->N.neg = 0; |
dfeab068 | 250 | |
6535eb17 | 251 | #ifdef MONT_WORD |
0f113f3e MC |
252 | { |
253 | BIGNUM tmod; | |
254 | BN_ULONG buf[2]; | |
255 | ||
d59c7c81 | 256 | bn_init(&tmod); |
0f113f3e MC |
257 | tmod.d = buf; |
258 | tmod.dmax = 2; | |
259 | tmod.neg = 0; | |
260 | ||
3de81a59 SW |
261 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) |
262 | BN_set_flags(&tmod, BN_FLG_CONSTTIME); | |
263 | ||
0f113f3e MC |
264 | mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2; |
265 | ||
266 | # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32) | |
267 | /* | |
268 | * Only certain BN_BITS2<=32 platforms actually make use of n0[1], | |
269 | * and we could use the #else case (with a shorter R value) for the | |
270 | * others. However, currently only the assembler files do know which | |
271 | * is which. | |
272 | */ | |
273 | ||
274 | BN_zero(R); | |
275 | if (!(BN_set_bit(R, 2 * BN_BITS2))) | |
276 | goto err; | |
277 | ||
278 | tmod.top = 0; | |
279 | if ((buf[0] = mod->d[0])) | |
280 | tmod.top = 1; | |
281 | if ((buf[1] = mod->top > 1 ? mod->d[1] : 0)) | |
282 | tmod.top = 2; | |
283 | ||
b1860d6c MC |
284 | if (BN_is_one(&tmod)) |
285 | BN_zero(Ri); | |
286 | else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) | |
0f113f3e MC |
287 | goto err; |
288 | if (!BN_lshift(Ri, Ri, 2 * BN_BITS2)) | |
289 | goto err; /* R*Ri */ | |
290 | if (!BN_is_zero(Ri)) { | |
291 | if (!BN_sub_word(Ri, 1)) | |
292 | goto err; | |
293 | } else { /* if N mod word size == 1 */ | |
294 | ||
295 | if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL) | |
296 | goto err; | |
297 | /* Ri-- (mod double word size) */ | |
298 | Ri->neg = 0; | |
299 | Ri->d[0] = BN_MASK2; | |
300 | Ri->d[1] = BN_MASK2; | |
301 | Ri->top = 2; | |
302 | } | |
303 | if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) | |
304 | goto err; | |
305 | /* | |
306 | * Ni = (R*Ri-1)/N, keep only couple of least significant words: | |
307 | */ | |
308 | mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0; | |
309 | mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0; | |
310 | # else | |
311 | BN_zero(R); | |
312 | if (!(BN_set_bit(R, BN_BITS2))) | |
313 | goto err; /* R */ | |
314 | ||
315 | buf[0] = mod->d[0]; /* tmod = N mod word size */ | |
316 | buf[1] = 0; | |
317 | tmod.top = buf[0] != 0 ? 1 : 0; | |
318 | /* Ri = R^-1 mod N */ | |
b1860d6c MC |
319 | if (BN_is_one(&tmod)) |
320 | BN_zero(Ri); | |
321 | else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) | |
0f113f3e MC |
322 | goto err; |
323 | if (!BN_lshift(Ri, Ri, BN_BITS2)) | |
324 | goto err; /* R*Ri */ | |
325 | if (!BN_is_zero(Ri)) { | |
326 | if (!BN_sub_word(Ri, 1)) | |
327 | goto err; | |
328 | } else { /* if N mod word size == 1 */ | |
329 | ||
330 | if (!BN_set_word(Ri, BN_MASK2)) | |
331 | goto err; /* Ri-- (mod word size) */ | |
332 | } | |
333 | if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) | |
334 | goto err; | |
335 | /* | |
336 | * Ni = (R*Ri-1)/N, keep only least significant word: | |
337 | */ | |
338 | mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0; | |
339 | mont->n0[1] = 0; | |
340 | # endif | |
341 | } | |
342 | #else /* !MONT_WORD */ | |
343 | { /* bignum version */ | |
344 | mont->ri = BN_num_bits(&mont->N); | |
345 | BN_zero(R); | |
346 | if (!BN_set_bit(R, mont->ri)) | |
347 | goto err; /* R = 2^ri */ | |
348 | /* Ri = R^-1 mod N */ | |
349 | if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL) | |
350 | goto err; | |
351 | if (!BN_lshift(Ri, Ri, mont->ri)) | |
352 | goto err; /* R*Ri */ | |
353 | if (!BN_sub_word(Ri, 1)) | |
354 | goto err; | |
355 | /* | |
356 | * Ni = (R*Ri-1) / N | |
357 | */ | |
358 | if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx)) | |
359 | goto err; | |
360 | } | |
d02b48c6 RE |
361 | #endif |
362 | ||
0f113f3e MC |
363 | /* setup RR for conversions */ |
364 | BN_zero(&(mont->RR)); | |
365 | if (!BN_set_bit(&(mont->RR), mont->ri * 2)) | |
366 | goto err; | |
367 | if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx)) | |
368 | goto err; | |
d02b48c6 | 369 | |
0f113f3e MC |
370 | ret = 1; |
371 | err: | |
372 | BN_CTX_end(ctx); | |
373 | return ret; | |
374 | } | |
d02b48c6 | 375 | |
6b691a5c | 376 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from) |
0f113f3e MC |
377 | { |
378 | if (to == from) | |
26a7d938 | 379 | return to; |
0f113f3e MC |
380 | |
381 | if (!BN_copy(&(to->RR), &(from->RR))) | |
382 | return NULL; | |
383 | if (!BN_copy(&(to->N), &(from->N))) | |
384 | return NULL; | |
385 | if (!BN_copy(&(to->Ni), &(from->Ni))) | |
386 | return NULL; | |
387 | to->ri = from->ri; | |
388 | to->n0[0] = from->n0[0]; | |
389 | to->n0[1] = from->n0[1]; | |
26a7d938 | 390 | return to; |
0f113f3e | 391 | } |
dfeab068 | 392 | |
d188a536 | 393 | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock, |
0f113f3e MC |
394 | const BIGNUM *mod, BN_CTX *ctx) |
395 | { | |
396 | BN_MONT_CTX *ret; | |
397 | ||
d188a536 | 398 | CRYPTO_THREAD_read_lock(lock); |
0f113f3e | 399 | ret = *pmont; |
d188a536 | 400 | CRYPTO_THREAD_unlock(lock); |
0f113f3e MC |
401 | if (ret) |
402 | return ret; | |
403 | ||
404 | /* | |
405 | * We don't want to serialise globally while doing our lazy-init math in | |
406 | * BN_MONT_CTX_set. That punishes threads that are doing independent | |
407 | * things. Instead, punish the case where more than one thread tries to | |
408 | * lazy-init the same 'pmont', by having each do the lazy-init math work | |
409 | * independently and only use the one from the thread that wins the race | |
410 | * (the losers throw away the work they've done). | |
411 | */ | |
412 | ret = BN_MONT_CTX_new(); | |
90945fa3 | 413 | if (ret == NULL) |
0f113f3e MC |
414 | return NULL; |
415 | if (!BN_MONT_CTX_set(ret, mod, ctx)) { | |
416 | BN_MONT_CTX_free(ret); | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | /* The locked compare-and-set, after the local work is done. */ | |
d188a536 | 421 | CRYPTO_THREAD_write_lock(lock); |
0f113f3e MC |
422 | if (*pmont) { |
423 | BN_MONT_CTX_free(ret); | |
424 | ret = *pmont; | |
425 | } else | |
426 | *pmont = ret; | |
d188a536 | 427 | CRYPTO_THREAD_unlock(lock); |
0f113f3e MC |
428 | return ret; |
429 | } |