]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_mont.c
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
)
29 int ret
= bn_mul_mont_fixed_top(r
, a
, b
, mont
, ctx
);
37 int bn_mul_mont_fixed_top(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
38 BN_MONT_CTX
*mont
, BN_CTX
*ctx
)
42 int num
= mont
->N
.top
;
44 #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)
45 if (num
> 1 && a
->top
== num
&& b
->top
== num
) {
46 if (bn_wexpand(r
, num
) == NULL
)
48 if (bn_mul_mont(r
->d
, a
->d
, b
->d
, mont
->N
.d
, mont
->n0
, num
)) {
49 r
->neg
= a
->neg
^ b
->neg
;
51 r
->flags
|= BN_FLG_FIXED_TOP
;
57 if ((a
->top
+ b
->top
) > 2 * num
)
61 tmp
= BN_CTX_get(ctx
);
67 if (!bn_sqr_fixed_top(tmp
, a
, ctx
))
70 if (!bn_mul_fixed_top(tmp
, a
, b
, ctx
))
73 /* reduce from aRR to aR */
75 if (!bn_from_montgomery_word(r
, tmp
, mont
))
78 if (!BN_from_montgomery(r
, tmp
, mont
, ctx
))
88 static int bn_from_montgomery_word(BIGNUM
*ret
, BIGNUM
*r
, BN_MONT_CTX
*mont
)
91 BN_ULONG
*ap
, *np
, *rp
, n0
, v
, carry
;
102 max
= (2 * nl
); /* carry is stored separately */
103 if (bn_wexpand(r
, max
) == NULL
)
110 /* clear the top words of T */
111 for (rtop
= r
->top
, i
= 0; i
< max
; i
++) {
112 v
= (BN_ULONG
)0 - ((i
- rtop
) >> (8 * sizeof(rtop
) - 1));
117 r
->flags
|= BN_FLG_FIXED_TOP
;
121 * Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
122 * input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
123 * includes |carry| which is stored separately.
125 for (carry
= 0, i
= 0; i
< nl
; i
++, rp
++) {
126 v
= bn_mul_add_words(rp
, np
, nl
, (rp
[0] * n0
) & BN_MASK2
);
127 v
= (v
+ carry
+ rp
[nl
]) & BN_MASK2
;
128 carry
|= (v
!= rp
[nl
]);
129 carry
&= (v
<= rp
[nl
]);
133 if (bn_wexpand(ret
, nl
) == NULL
)
136 ret
->flags
|= BN_FLG_FIXED_TOP
;
142 * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|
143 * includes |carry| which is stored separately.
147 carry
-= bn_sub_words(rp
, ap
, np
, nl
);
149 * |carry| is -1 if |ap| - |np| underflowed or zero if it did not. Note
150 * |carry| cannot be 1. That would imply the subtraction did not fit in
151 * |nl| words, and we know at most one subtraction is needed.
153 for (i
= 0; i
< nl
; i
++) {
154 rp
[i
] = (carry
& ap
[i
]) | (~carry
& rp
[i
]);
160 #endif /* MONT_WORD */
162 int BN_from_montgomery(BIGNUM
*ret
, const BIGNUM
*a
, BN_MONT_CTX
*mont
,
167 retn
= bn_from_mont_fixed_top(ret
, a
, mont
, ctx
);
174 int bn_from_mont_fixed_top(BIGNUM
*ret
, const BIGNUM
*a
, BN_MONT_CTX
*mont
,
182 if ((t
= BN_CTX_get(ctx
)) && BN_copy(t
, a
)) {
183 retn
= bn_from_montgomery_word(ret
, t
, mont
);
186 #else /* !MONT_WORD */
190 t1
= BN_CTX_get(ctx
);
191 t2
= BN_CTX_get(ctx
);
197 BN_mask_bits(t1
, mont
->ri
);
199 if (!BN_mul(t2
, t1
, &mont
->Ni
, ctx
))
201 BN_mask_bits(t2
, mont
->ri
);
203 if (!BN_mul(t1
, t2
, &mont
->N
, ctx
))
205 if (!BN_add(t2
, a
, t1
))
207 if (!BN_rshift(ret
, t2
, mont
->ri
))
210 if (BN_ucmp(ret
, &(mont
->N
)) >= 0) {
211 if (!BN_usub(ret
, ret
, &(mont
->N
)))
218 #endif /* MONT_WORD */
222 int bn_to_mont_fixed_top(BIGNUM
*r
, const BIGNUM
*a
, BN_MONT_CTX
*mont
,
225 return bn_mul_mont_fixed_top(r
, a
, &(mont
->RR
), mont
, ctx
);
228 BN_MONT_CTX
*BN_MONT_CTX_new(void)
232 if ((ret
= OPENSSL_malloc(sizeof(*ret
))) == NULL
) {
233 ERR_raise(ERR_LIB_BN
, ERR_R_MALLOC_FAILURE
);
237 BN_MONT_CTX_init(ret
);
238 ret
->flags
= BN_FLG_MALLOCED
;
242 void BN_MONT_CTX_init(BN_MONT_CTX
*ctx
)
248 ctx
->n0
[0] = ctx
->n0
[1] = 0;
252 void BN_MONT_CTX_free(BN_MONT_CTX
*mont
)
256 BN_clear_free(&mont
->RR
);
257 BN_clear_free(&mont
->N
);
258 BN_clear_free(&mont
->Ni
);
259 if (mont
->flags
& BN_FLG_MALLOCED
)
263 int BN_MONT_CTX_set(BN_MONT_CTX
*mont
, const BIGNUM
*mod
, BN_CTX
*ctx
)
272 if ((Ri
= BN_CTX_get(ctx
)) == NULL
)
274 R
= &(mont
->RR
); /* grab RR as a temp */
275 if (!BN_copy(&(mont
->N
), mod
))
276 goto err
; /* Set N */
277 if (BN_get_flags(mod
, BN_FLG_CONSTTIME
) != 0)
278 BN_set_flags(&(mont
->N
), BN_FLG_CONSTTIME
);
291 if (BN_get_flags(mod
, BN_FLG_CONSTTIME
) != 0)
292 BN_set_flags(&tmod
, BN_FLG_CONSTTIME
);
294 mont
->ri
= (BN_num_bits(mod
) + (BN_BITS2
- 1)) / BN_BITS2
* BN_BITS2
;
296 # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
298 * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
299 * and we could use the #else case (with a shorter R value) for the
300 * others. However, currently only the assembler files do know which
305 if (!(BN_set_bit(R
, 2 * BN_BITS2
)))
309 if ((buf
[0] = mod
->d
[0]))
311 if ((buf
[1] = mod
->top
> 1 ? mod
->d
[1] : 0))
314 if (BN_is_one(&tmod
))
316 else if ((BN_mod_inverse(Ri
, R
, &tmod
, ctx
)) == NULL
)
318 if (!BN_lshift(Ri
, Ri
, 2 * BN_BITS2
))
320 if (!BN_is_zero(Ri
)) {
321 if (!BN_sub_word(Ri
, 1))
323 } else { /* if N mod word size == 1 */
325 if (bn_expand(Ri
, (int)sizeof(BN_ULONG
) * 2) == NULL
)
327 /* Ri-- (mod double word size) */
333 if (!BN_div(Ri
, NULL
, Ri
, &tmod
, ctx
))
336 * Ni = (R*Ri-1)/N, keep only couple of least significant words:
338 mont
->n0
[0] = (Ri
->top
> 0) ? Ri
->d
[0] : 0;
339 mont
->n0
[1] = (Ri
->top
> 1) ? Ri
->d
[1] : 0;
342 if (!(BN_set_bit(R
, BN_BITS2
)))
345 buf
[0] = mod
->d
[0]; /* tmod = N mod word size */
347 tmod
.top
= buf
[0] != 0 ? 1 : 0;
348 /* Ri = R^-1 mod N */
349 if (BN_is_one(&tmod
))
351 else if ((BN_mod_inverse(Ri
, R
, &tmod
, ctx
)) == NULL
)
353 if (!BN_lshift(Ri
, Ri
, BN_BITS2
))
355 if (!BN_is_zero(Ri
)) {
356 if (!BN_sub_word(Ri
, 1))
358 } else { /* if N mod word size == 1 */
360 if (!BN_set_word(Ri
, BN_MASK2
))
361 goto err
; /* Ri-- (mod word size) */
363 if (!BN_div(Ri
, NULL
, Ri
, &tmod
, ctx
))
366 * Ni = (R*Ri-1)/N, keep only least significant word:
368 mont
->n0
[0] = (Ri
->top
> 0) ? Ri
->d
[0] : 0;
372 #else /* !MONT_WORD */
373 { /* bignum version */
374 mont
->ri
= BN_num_bits(&mont
->N
);
376 if (!BN_set_bit(R
, mont
->ri
))
377 goto err
; /* R = 2^ri */
378 /* Ri = R^-1 mod N */
379 if ((BN_mod_inverse(Ri
, R
, &mont
->N
, ctx
)) == NULL
)
381 if (!BN_lshift(Ri
, Ri
, mont
->ri
))
383 if (!BN_sub_word(Ri
, 1))
388 if (!BN_div(&(mont
->Ni
), NULL
, Ri
, &mont
->N
, ctx
))
393 /* setup RR for conversions */
394 BN_zero(&(mont
->RR
));
395 if (!BN_set_bit(&(mont
->RR
), mont
->ri
* 2))
397 if (!BN_mod(&(mont
->RR
), &(mont
->RR
), &(mont
->N
), ctx
))
400 for (i
= mont
->RR
.top
, ret
= mont
->N
.top
; i
< ret
; i
++)
403 mont
->RR
.flags
|= BN_FLG_FIXED_TOP
;
411 BN_MONT_CTX
*BN_MONT_CTX_copy(BN_MONT_CTX
*to
, BN_MONT_CTX
*from
)
416 if (!BN_copy(&(to
->RR
), &(from
->RR
)))
418 if (!BN_copy(&(to
->N
), &(from
->N
)))
420 if (!BN_copy(&(to
->Ni
), &(from
->Ni
)))
423 to
->n0
[0] = from
->n0
[0];
424 to
->n0
[1] = from
->n0
[1];
428 BN_MONT_CTX
*BN_MONT_CTX_set_locked(BN_MONT_CTX
**pmont
, CRYPTO_RWLOCK
*lock
,
429 const BIGNUM
*mod
, BN_CTX
*ctx
)
433 if (!CRYPTO_THREAD_read_lock(lock
))
436 CRYPTO_THREAD_unlock(lock
);
441 * We don't want to serialize globally while doing our lazy-init math in
442 * BN_MONT_CTX_set. That punishes threads that are doing independent
443 * things. Instead, punish the case where more than one thread tries to
444 * lazy-init the same 'pmont', by having each do the lazy-init math work
445 * independently and only use the one from the thread that wins the race
446 * (the losers throw away the work they've done).
448 ret
= BN_MONT_CTX_new();
451 if (!BN_MONT_CTX_set(ret
, mod
, ctx
)) {
452 BN_MONT_CTX_free(ret
);
456 /* The locked compare-and-set, after the local work is done. */
457 if (!CRYPTO_THREAD_write_lock(lock
)) {
458 BN_MONT_CTX_free(ret
);
463 BN_MONT_CTX_free(ret
);
467 CRYPTO_THREAD_unlock(lock
);