]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_mont.c
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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
17 #include "internal/cryptlib.h"
20 #define MONT_WORD /* use the faster word-based algorithm */
23 static int BN_from_montgomery_word(BIGNUM
*ret
, BIGNUM
*r
, BN_MONT_CTX
*mont
);
26 int BN_mod_mul_montgomery(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
27 BN_MONT_CTX
*mont
, BN_CTX
*ctx
)
31 int num
= mont
->N
.top
;
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
)
37 if (bn_mul_mont(r
->d
, a
->d
, b
->d
, mont
->N
.d
, mont
->n0
, num
)) {
38 r
->neg
= a
->neg
^ b
->neg
;
46 if ((a
->top
+ b
->top
) > 2 * num
)
50 tmp
= BN_CTX_get(ctx
);
56 if (!BN_sqr(tmp
, a
, ctx
))
59 if (!BN_mul(tmp
, a
, b
, ctx
))
62 /* reduce from aRR to aR */
64 if (!BN_from_montgomery_word(r
, tmp
, mont
))
67 if (!BN_from_montgomery(r
, tmp
, mont
, ctx
))
78 static int BN_from_montgomery_word(BIGNUM
*ret
, BIGNUM
*r
, BN_MONT_CTX
*mont
)
81 BN_ULONG
*ap
, *np
, *rp
, n0
, v
, carry
;
91 max
= (2 * nl
); /* carry is stored separately */
92 if (bn_wexpand(r
, max
) == NULL
)
99 /* clear the top words of T */
102 memset(&rp
[r
->top
], 0, sizeof(*rp
) * i
);
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.
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
]);
120 if (bn_wexpand(ret
, nl
) == NULL
)
128 * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|
129 * includes |carry| which is stored separately.
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.
138 v
= bn_sub_words(rp
, ap
, np
, nl
) - carry
;
140 for (i
= 0; i
< nl
; i
++) {
141 rp
[i
] = (v
& ap
[i
]) | (~v
& rp
[i
]);
150 #endif /* MONT_WORD */
152 int BN_from_montgomery(BIGNUM
*ret
, const BIGNUM
*a
, BN_MONT_CTX
*mont
,
160 if ((t
= BN_CTX_get(ctx
)) && BN_copy(t
, a
))
161 retn
= BN_from_montgomery_word(ret
, t
, mont
);
163 #else /* !MONT_WORD */
167 t1
= BN_CTX_get(ctx
);
168 t2
= BN_CTX_get(ctx
);
174 BN_mask_bits(t1
, mont
->ri
);
176 if (!BN_mul(t2
, t1
, &mont
->Ni
, ctx
))
178 BN_mask_bits(t2
, mont
->ri
);
180 if (!BN_mul(t1
, t2
, &mont
->N
, ctx
))
182 if (!BN_add(t2
, a
, t1
))
184 if (!BN_rshift(ret
, t2
, mont
->ri
))
187 if (BN_ucmp(ret
, &(mont
->N
)) >= 0) {
188 if (!BN_usub(ret
, ret
, &(mont
->N
)))
195 #endif /* MONT_WORD */
199 BN_MONT_CTX
*BN_MONT_CTX_new(void)
203 if ((ret
= OPENSSL_malloc(sizeof(*ret
))) == NULL
) {
204 BNerr(BN_F_BN_MONT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
208 BN_MONT_CTX_init(ret
);
209 ret
->flags
= BN_FLG_MALLOCED
;
213 void BN_MONT_CTX_init(BN_MONT_CTX
*ctx
)
219 ctx
->n0
[0] = ctx
->n0
[1] = 0;
223 void BN_MONT_CTX_free(BN_MONT_CTX
*mont
)
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
)
234 int BN_MONT_CTX_set(BN_MONT_CTX
*mont
, const BIGNUM
*mod
, BN_CTX
*ctx
)
243 if ((Ri
= BN_CTX_get(ctx
)) == NULL
)
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
);
262 if (BN_get_flags(mod
, BN_FLG_CONSTTIME
) != 0)
263 BN_set_flags(&tmod
, BN_FLG_CONSTTIME
);
265 mont
->ri
= (BN_num_bits(mod
) + (BN_BITS2
- 1)) / BN_BITS2
* BN_BITS2
;
267 # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
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
276 if (!(BN_set_bit(R
, 2 * BN_BITS2
)))
280 if ((buf
[0] = mod
->d
[0]))
282 if ((buf
[1] = mod
->top
> 1 ? mod
->d
[1] : 0))
285 if (BN_is_one(&tmod
))
287 else if ((BN_mod_inverse(Ri
, R
, &tmod
, ctx
)) == NULL
)
289 if (!BN_lshift(Ri
, Ri
, 2 * BN_BITS2
))
291 if (!BN_is_zero(Ri
)) {
292 if (!BN_sub_word(Ri
, 1))
294 } else { /* if N mod word size == 1 */
296 if (bn_expand(Ri
, (int)sizeof(BN_ULONG
) * 2) == NULL
)
298 /* Ri-- (mod double word size) */
304 if (!BN_div(Ri
, NULL
, Ri
, &tmod
, ctx
))
307 * Ni = (R*Ri-1)/N, keep only couple of least significant words:
309 mont
->n0
[0] = (Ri
->top
> 0) ? Ri
->d
[0] : 0;
310 mont
->n0
[1] = (Ri
->top
> 1) ? Ri
->d
[1] : 0;
313 if (!(BN_set_bit(R
, BN_BITS2
)))
316 buf
[0] = mod
->d
[0]; /* tmod = N mod word size */
318 tmod
.top
= buf
[0] != 0 ? 1 : 0;
319 /* Ri = R^-1 mod N */
320 if (BN_is_one(&tmod
))
322 else if ((BN_mod_inverse(Ri
, R
, &tmod
, ctx
)) == NULL
)
324 if (!BN_lshift(Ri
, Ri
, BN_BITS2
))
326 if (!BN_is_zero(Ri
)) {
327 if (!BN_sub_word(Ri
, 1))
329 } else { /* if N mod word size == 1 */
331 if (!BN_set_word(Ri
, BN_MASK2
))
332 goto err
; /* Ri-- (mod word size) */
334 if (!BN_div(Ri
, NULL
, Ri
, &tmod
, ctx
))
337 * Ni = (R*Ri-1)/N, keep only least significant word:
339 mont
->n0
[0] = (Ri
->top
> 0) ? Ri
->d
[0] : 0;
343 #else /* !MONT_WORD */
344 { /* bignum version */
345 mont
->ri
= BN_num_bits(&mont
->N
);
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
)
352 if (!BN_lshift(Ri
, Ri
, mont
->ri
))
354 if (!BN_sub_word(Ri
, 1))
359 if (!BN_div(&(mont
->Ni
), NULL
, Ri
, &mont
->N
, ctx
))
364 /* setup RR for conversions */
365 BN_zero(&(mont
->RR
));
366 if (!BN_set_bit(&(mont
->RR
), mont
->ri
* 2))
368 if (!BN_mod(&(mont
->RR
), &(mont
->RR
), &(mont
->N
), ctx
))
377 BN_MONT_CTX
*BN_MONT_CTX_copy(BN_MONT_CTX
*to
, BN_MONT_CTX
*from
)
382 if (!BN_copy(&(to
->RR
), &(from
->RR
)))
384 if (!BN_copy(&(to
->N
), &(from
->N
)))
386 if (!BN_copy(&(to
->Ni
), &(from
->Ni
)))
389 to
->n0
[0] = from
->n0
[0];
390 to
->n0
[1] = from
->n0
[1];
394 BN_MONT_CTX
*BN_MONT_CTX_set_locked(BN_MONT_CTX
**pmont
, CRYPTO_RWLOCK
*lock
,
395 const BIGNUM
*mod
, BN_CTX
*ctx
)
399 CRYPTO_THREAD_read_lock(lock
);
401 CRYPTO_THREAD_unlock(lock
);
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).
413 ret
= BN_MONT_CTX_new();
416 if (!BN_MONT_CTX_set(ret
, mod
, ctx
)) {
417 BN_MONT_CTX_free(ret
);
421 /* The locked compare-and-set, after the local work is done. */
422 CRYPTO_THREAD_write_lock(lock
);
424 BN_MONT_CTX_free(ret
);
428 CRYPTO_THREAD_unlock(lock
);