1 | /*
|
---|
2 | * Copyright 1995-2016 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)
|
---|
99 | memset(&rp[r->top], 0, sizeof(*rp) * i);
|
---|
100 |
|
---|
101 | r->top = max;
|
---|
102 | n0 = mont->n0[0];
|
---|
103 |
|
---|
104 | for (carry = 0, i = 0; i < nl; i++, rp++) {
|
---|
105 | v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
|
---|
106 | v = (v + carry + rp[nl]) & BN_MASK2;
|
---|
107 | carry |= (v != rp[nl]);
|
---|
108 | carry &= (v <= rp[nl]);
|
---|
109 | rp[nl] = v;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (bn_wexpand(ret, nl) == NULL)
|
---|
113 | return (0);
|
---|
114 | ret->top = nl;
|
---|
115 | ret->neg = r->neg;
|
---|
116 |
|
---|
117 | rp = ret->d;
|
---|
118 | ap = &(r->d[nl]);
|
---|
119 |
|
---|
120 | # define BRANCH_FREE 1
|
---|
121 | # if BRANCH_FREE
|
---|
122 | {
|
---|
123 | BN_ULONG *nrp;
|
---|
124 | size_t m;
|
---|
125 |
|
---|
126 | v = bn_sub_words(rp, ap, np, nl) - carry;
|
---|
127 | /*
|
---|
128 | * if subtraction result is real, then trick unconditional memcpy
|
---|
129 | * below to perform in-place "refresh" instead of actual copy.
|
---|
130 | */
|
---|
131 | m = (0 - (size_t)v);
|
---|
132 | nrp =
|
---|
133 | (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
|
---|
134 |
|
---|
135 | for (i = 0, nl -= 4; i < nl; i += 4) {
|
---|
136 | BN_ULONG t1, t2, t3, t4;
|
---|
137 |
|
---|
138 | t1 = nrp[i + 0];
|
---|
139 | t2 = nrp[i + 1];
|
---|
140 | t3 = nrp[i + 2];
|
---|
141 | ap[i + 0] = 0;
|
---|
142 | t4 = nrp[i + 3];
|
---|
143 | ap[i + 1] = 0;
|
---|
144 | rp[i + 0] = t1;
|
---|
145 | ap[i + 2] = 0;
|
---|
146 | rp[i + 1] = t2;
|
---|
147 | ap[i + 3] = 0;
|
---|
148 | rp[i + 2] = t3;
|
---|
149 | rp[i + 3] = t4;
|
---|
150 | }
|
---|
151 | for (nl += 4; i < nl; i++)
|
---|
152 | rp[i] = nrp[i], ap[i] = 0;
|
---|
153 | }
|
---|
154 | # else
|
---|
155 | if (bn_sub_words(rp, ap, np, nl) - carry)
|
---|
156 | memcpy(rp, ap, nl * sizeof(BN_ULONG));
|
---|
157 | # endif
|
---|
158 | bn_correct_top(r);
|
---|
159 | bn_correct_top(ret);
|
---|
160 | bn_check_top(ret);
|
---|
161 |
|
---|
162 | return (1);
|
---|
163 | }
|
---|
164 | #endif /* MONT_WORD */
|
---|
165 |
|
---|
166 | int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
|
---|
167 | BN_CTX *ctx)
|
---|
168 | {
|
---|
169 | int retn = 0;
|
---|
170 | #ifdef MONT_WORD
|
---|
171 | BIGNUM *t;
|
---|
172 |
|
---|
173 | BN_CTX_start(ctx);
|
---|
174 | if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))
|
---|
175 | retn = BN_from_montgomery_word(ret, t, mont);
|
---|
176 | BN_CTX_end(ctx);
|
---|
177 | #else /* !MONT_WORD */
|
---|
178 | BIGNUM *t1, *t2;
|
---|
179 |
|
---|
180 | BN_CTX_start(ctx);
|
---|
181 | t1 = BN_CTX_get(ctx);
|
---|
182 | t2 = BN_CTX_get(ctx);
|
---|
183 | if (t1 == NULL || t2 == NULL)
|
---|
184 | goto err;
|
---|
185 |
|
---|
186 | if (!BN_copy(t1, a))
|
---|
187 | goto err;
|
---|
188 | BN_mask_bits(t1, mont->ri);
|
---|
189 |
|
---|
190 | if (!BN_mul(t2, t1, &mont->Ni, ctx))
|
---|
191 | goto err;
|
---|
192 | BN_mask_bits(t2, mont->ri);
|
---|
193 |
|
---|
194 | if (!BN_mul(t1, t2, &mont->N, ctx))
|
---|
195 | goto err;
|
---|
196 | if (!BN_add(t2, a, t1))
|
---|
197 | goto err;
|
---|
198 | if (!BN_rshift(ret, t2, mont->ri))
|
---|
199 | goto err;
|
---|
200 |
|
---|
201 | if (BN_ucmp(ret, &(mont->N)) >= 0) {
|
---|
202 | if (!BN_usub(ret, ret, &(mont->N)))
|
---|
203 | goto err;
|
---|
204 | }
|
---|
205 | retn = 1;
|
---|
206 | bn_check_top(ret);
|
---|
207 | err:
|
---|
208 | BN_CTX_end(ctx);
|
---|
209 | #endif /* MONT_WORD */
|
---|
210 | return (retn);
|
---|
211 | }
|
---|
212 |
|
---|
213 | BN_MONT_CTX *BN_MONT_CTX_new(void)
|
---|
214 | {
|
---|
215 | BN_MONT_CTX *ret;
|
---|
216 |
|
---|
217 | if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
|
---|
218 | return (NULL);
|
---|
219 |
|
---|
220 | BN_MONT_CTX_init(ret);
|
---|
221 | ret->flags = BN_FLG_MALLOCED;
|
---|
222 | return (ret);
|
---|
223 | }
|
---|
224 |
|
---|
225 | void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
|
---|
226 | {
|
---|
227 | ctx->ri = 0;
|
---|
228 | bn_init(&(ctx->RR));
|
---|
229 | bn_init(&(ctx->N));
|
---|
230 | bn_init(&(ctx->Ni));
|
---|
231 | ctx->n0[0] = ctx->n0[1] = 0;
|
---|
232 | ctx->flags = 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 | void BN_MONT_CTX_free(BN_MONT_CTX *mont)
|
---|
236 | {
|
---|
237 | if (mont == NULL)
|
---|
238 | return;
|
---|
239 |
|
---|
240 | BN_clear_free(&(mont->RR));
|
---|
241 | BN_clear_free(&(mont->N));
|
---|
242 | BN_clear_free(&(mont->Ni));
|
---|
243 | if (mont->flags & BN_FLG_MALLOCED)
|
---|
244 | OPENSSL_free(mont);
|
---|
245 | }
|
---|
246 |
|
---|
247 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
|
---|
248 | {
|
---|
249 | int ret = 0;
|
---|
250 | BIGNUM *Ri, *R;
|
---|
251 |
|
---|
252 | if (BN_is_zero(mod))
|
---|
253 | return 0;
|
---|
254 |
|
---|
255 | BN_CTX_start(ctx);
|
---|
256 | if ((Ri = BN_CTX_get(ctx)) == NULL)
|
---|
257 | goto err;
|
---|
258 | R = &(mont->RR); /* grab RR as a temp */
|
---|
259 | if (!BN_copy(&(mont->N), mod))
|
---|
260 | goto err; /* Set N */
|
---|
261 | mont->N.neg = 0;
|
---|
262 |
|
---|
263 | #ifdef MONT_WORD
|
---|
264 | {
|
---|
265 | BIGNUM tmod;
|
---|
266 | BN_ULONG buf[2];
|
---|
267 |
|
---|
268 | bn_init(&tmod);
|
---|
269 | tmod.d = buf;
|
---|
270 | tmod.dmax = 2;
|
---|
271 | tmod.neg = 0;
|
---|
272 |
|
---|
273 | mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
|
---|
274 |
|
---|
275 | # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
|
---|
276 | /*
|
---|
277 | * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
|
---|
278 | * and we could use the #else case (with a shorter R value) for the
|
---|
279 | * others. However, currently only the assembler files do know which
|
---|
280 | * is which.
|
---|
281 | */
|
---|
282 |
|
---|
283 | BN_zero(R);
|
---|
284 | if (!(BN_set_bit(R, 2 * BN_BITS2)))
|
---|
285 | goto err;
|
---|
286 |
|
---|
287 | tmod.top = 0;
|
---|
288 | if ((buf[0] = mod->d[0]))
|
---|
289 | tmod.top = 1;
|
---|
290 | if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
|
---|
291 | tmod.top = 2;
|
---|
292 |
|
---|
293 | if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
---|
294 | goto err;
|
---|
295 | if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
|
---|
296 | goto err; /* R*Ri */
|
---|
297 | if (!BN_is_zero(Ri)) {
|
---|
298 | if (!BN_sub_word(Ri, 1))
|
---|
299 | goto err;
|
---|
300 | } else { /* if N mod word size == 1 */
|
---|
301 |
|
---|
302 | if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
|
---|
303 | goto err;
|
---|
304 | /* Ri-- (mod double word size) */
|
---|
305 | Ri->neg = 0;
|
---|
306 | Ri->d[0] = BN_MASK2;
|
---|
307 | Ri->d[1] = BN_MASK2;
|
---|
308 | Ri->top = 2;
|
---|
309 | }
|
---|
310 | if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
|
---|
311 | goto err;
|
---|
312 | /*
|
---|
313 | * Ni = (R*Ri-1)/N, keep only couple of least significant words:
|
---|
314 | */
|
---|
315 | mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
|
---|
316 | mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
|
---|
317 | # else
|
---|
318 | BN_zero(R);
|
---|
319 | if (!(BN_set_bit(R, BN_BITS2)))
|
---|
320 | goto err; /* R */
|
---|
321 |
|
---|
322 | buf[0] = mod->d[0]; /* tmod = N mod word size */
|
---|
323 | buf[1] = 0;
|
---|
324 | tmod.top = buf[0] != 0 ? 1 : 0;
|
---|
325 | /* Ri = R^-1 mod N */
|
---|
326 | if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
|
---|
327 | goto err;
|
---|
328 | if (!BN_lshift(Ri, Ri, BN_BITS2))
|
---|
329 | goto err; /* R*Ri */
|
---|
330 | if (!BN_is_zero(Ri)) {
|
---|
331 | if (!BN_sub_word(Ri, 1))
|
---|
332 | goto err;
|
---|
333 | } else { /* if N mod word size == 1 */
|
---|
334 |
|
---|
335 | if (!BN_set_word(Ri, BN_MASK2))
|
---|
336 | goto err; /* Ri-- (mod word size) */
|
---|
337 | }
|
---|
338 | if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
|
---|
339 | goto err;
|
---|
340 | /*
|
---|
341 | * Ni = (R*Ri-1)/N, keep only least significant word:
|
---|
342 | */
|
---|
343 | mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
|
---|
344 | mont->n0[1] = 0;
|
---|
345 | # endif
|
---|
346 | }
|
---|
347 | #else /* !MONT_WORD */
|
---|
348 | { /* bignum version */
|
---|
349 | mont->ri = BN_num_bits(&mont->N);
|
---|
350 | BN_zero(R);
|
---|
351 | if (!BN_set_bit(R, mont->ri))
|
---|
352 | goto err; /* R = 2^ri */
|
---|
353 | /* Ri = R^-1 mod N */
|
---|
354 | if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
|
---|
355 | goto err;
|
---|
356 | if (!BN_lshift(Ri, Ri, mont->ri))
|
---|
357 | goto err; /* R*Ri */
|
---|
358 | if (!BN_sub_word(Ri, 1))
|
---|
359 | goto err;
|
---|
360 | /*
|
---|
361 | * Ni = (R*Ri-1) / N
|
---|
362 | */
|
---|
363 | if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
|
---|
364 | goto err;
|
---|
365 | }
|
---|
366 | #endif
|
---|
367 |
|
---|
368 | /* setup RR for conversions */
|
---|
369 | BN_zero(&(mont->RR));
|
---|
370 | if (!BN_set_bit(&(mont->RR), mont->ri * 2))
|
---|
371 | goto err;
|
---|
372 | if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
|
---|
373 | goto err;
|
---|
374 |
|
---|
375 | ret = 1;
|
---|
376 | err:
|
---|
377 | BN_CTX_end(ctx);
|
---|
378 | return ret;
|
---|
379 | }
|
---|
380 |
|
---|
381 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
|
---|
382 | {
|
---|
383 | if (to == from)
|
---|
384 | return (to);
|
---|
385 |
|
---|
386 | if (!BN_copy(&(to->RR), &(from->RR)))
|
---|
387 | return NULL;
|
---|
388 | if (!BN_copy(&(to->N), &(from->N)))
|
---|
389 | return NULL;
|
---|
390 | if (!BN_copy(&(to->Ni), &(from->Ni)))
|
---|
391 | return NULL;
|
---|
392 | to->ri = from->ri;
|
---|
393 | to->n0[0] = from->n0[0];
|
---|
394 | to->n0[1] = from->n0[1];
|
---|
395 | return (to);
|
---|
396 | }
|
---|
397 |
|
---|
398 | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
|
---|
399 | const BIGNUM *mod, BN_CTX *ctx)
|
---|
400 | {
|
---|
401 | BN_MONT_CTX *ret;
|
---|
402 |
|
---|
403 | CRYPTO_THREAD_read_lock(lock);
|
---|
404 | ret = *pmont;
|
---|
405 | CRYPTO_THREAD_unlock(lock);
|
---|
406 | if (ret)
|
---|
407 | return ret;
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * We don't want to serialise globally while doing our lazy-init math in
|
---|
411 | * BN_MONT_CTX_set. That punishes threads that are doing independent
|
---|
412 | * things. Instead, punish the case where more than one thread tries to
|
---|
413 | * lazy-init the same 'pmont', by having each do the lazy-init math work
|
---|
414 | * independently and only use the one from the thread that wins the race
|
---|
415 | * (the losers throw away the work they've done).
|
---|
416 | */
|
---|
417 | ret = BN_MONT_CTX_new();
|
---|
418 | if (ret == NULL)
|
---|
419 | return NULL;
|
---|
420 | if (!BN_MONT_CTX_set(ret, mod, ctx)) {
|
---|
421 | BN_MONT_CTX_free(ret);
|
---|
422 | return NULL;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /* The locked compare-and-set, after the local work is done. */
|
---|
426 | CRYPTO_THREAD_write_lock(lock);
|
---|
427 | if (*pmont) {
|
---|
428 | BN_MONT_CTX_free(ret);
|
---|
429 | ret = *pmont;
|
---|
430 | } else
|
---|
431 | *pmont = ret;
|
---|
432 | CRYPTO_THREAD_unlock(lock);
|
---|
433 | return ret;
|
---|
434 | }
|
---|