2 * Copyright 1995-2018 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
10 #include "internal/cryptlib.h"
13 static BIGNUM
*euclid(BIGNUM
*a
, BIGNUM
*b
);
15 int BN_gcd(BIGNUM
*r
, const BIGNUM
*in_a
, const BIGNUM
*in_b
, BN_CTX
*ctx
)
29 if (BN_copy(a
, in_a
) == NULL
)
31 if (BN_copy(b
, in_b
) == NULL
)
36 if (BN_cmp(a
, b
) < 0) {
45 if (BN_copy(r
, t
) == NULL
)
54 static BIGNUM
*euclid(BIGNUM
*a
, BIGNUM
*b
)
63 while (!BN_is_zero(b
)) {
70 if (!BN_rshift1(a
, a
))
72 if (BN_cmp(a
, b
) < 0) {
77 } else { /* a odd - b even */
79 if (!BN_rshift1(b
, b
))
81 if (BN_cmp(a
, b
) < 0) {
87 } else { /* a is even */
90 if (!BN_rshift1(a
, a
))
92 if (BN_cmp(a
, b
) < 0) {
97 } else { /* a even - b even */
99 if (!BN_rshift1(a
, a
))
101 if (!BN_rshift1(b
, b
))
110 if (!BN_lshift(a
, a
, shifts
))
119 /* solves ax == 1 (mod n) */
120 static BIGNUM
*BN_mod_inverse_no_branch(BIGNUM
*in
,
121 const BIGNUM
*a
, const BIGNUM
*n
,
124 BIGNUM
*BN_mod_inverse(BIGNUM
*in
,
125 const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
129 rv
= int_bn_mod_inverse(in
, a
, n
, ctx
, &noinv
);
131 BNerr(BN_F_BN_MOD_INVERSE
, BN_R_NO_INVERSE
);
135 BIGNUM
*int_bn_mod_inverse(BIGNUM
*in
,
136 const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
,
139 BIGNUM
*A
, *B
, *X
, *Y
, *M
, *D
, *T
, *R
= NULL
;
143 /* This is invalid input so we don't worry about constant time here */
144 if (BN_abs_is_word(n
, 1) || BN_is_zero(n
)) {
153 if ((BN_get_flags(a
, BN_FLG_CONSTTIME
) != 0)
154 || (BN_get_flags(n
, BN_FLG_CONSTTIME
) != 0)) {
155 return BN_mod_inverse_no_branch(in
, a
, n
, ctx
);
181 if (BN_copy(B
, a
) == NULL
)
183 if (BN_copy(A
, n
) == NULL
)
186 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0)) {
187 if (!BN_nnmod(B
, B
, A
, ctx
))
192 * From B = a mod |n|, A = |n| it follows that
195 * -sign*X*a == B (mod |n|),
196 * sign*Y*a == A (mod |n|).
199 if (BN_is_odd(n
) && (BN_num_bits(n
) <= 2048)) {
201 * Binary inversion algorithm; requires odd modulus. This is faster
202 * than the general algorithm if the modulus is sufficiently small
203 * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit
208 while (!BN_is_zero(B
)) {
212 * (1) -sign*X*a == B (mod |n|),
213 * (2) sign*Y*a == A (mod |n|)
217 * Now divide B by the maximum possible power of two in the
218 * integers, and divide X by the same value mod |n|. When we're
219 * done, (1) still holds.
222 while (!BN_is_bit_set(B
, shift
)) { /* note that 0 < B */
226 if (!BN_uadd(X
, X
, n
))
230 * now X is even, so we can easily divide it by two
232 if (!BN_rshift1(X
, X
))
236 if (!BN_rshift(B
, B
, shift
))
241 * Same for A and Y. Afterwards, (2) still holds.
244 while (!BN_is_bit_set(A
, shift
)) { /* note that 0 < A */
248 if (!BN_uadd(Y
, Y
, n
))
252 if (!BN_rshift1(Y
, Y
))
256 if (!BN_rshift(A
, A
, shift
))
261 * We still have (1) and (2).
262 * Both A and B are odd.
263 * The following computations ensure that
267 * (1) -sign*X*a == B (mod |n|),
268 * (2) sign*Y*a == A (mod |n|),
270 * and that either A or B is even in the next iteration.
272 if (BN_ucmp(B
, A
) >= 0) {
273 /* -sign*(X + Y)*a == B - A (mod |n|) */
274 if (!BN_uadd(X
, X
, Y
))
277 * NB: we could use BN_mod_add_quick(X, X, Y, n), but that
278 * actually makes the algorithm slower
280 if (!BN_usub(B
, B
, A
))
283 /* sign*(X + Y)*a == A - B (mod |n|) */
284 if (!BN_uadd(Y
, Y
, X
))
287 * as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
289 if (!BN_usub(A
, A
, B
))
294 /* general inversion algorithm */
296 while (!BN_is_zero(B
)) {
301 * (*) -sign*X*a == B (mod |n|),
302 * sign*Y*a == A (mod |n|)
305 /* (D, M) := (A/B, A%B) ... */
306 if (BN_num_bits(A
) == BN_num_bits(B
)) {
309 if (!BN_sub(M
, A
, B
))
311 } else if (BN_num_bits(A
) == BN_num_bits(B
) + 1) {
312 /* A/B is 1, 2, or 3 */
313 if (!BN_lshift1(T
, B
))
315 if (BN_ucmp(A
, T
) < 0) {
316 /* A < 2*B, so D=1 */
319 if (!BN_sub(M
, A
, B
))
322 /* A >= 2*B, so D=2 or D=3 */
323 if (!BN_sub(M
, A
, T
))
325 if (!BN_add(D
, T
, B
))
326 goto err
; /* use D (:= 3*B) as temp */
327 if (BN_ucmp(A
, D
) < 0) {
328 /* A < 3*B, so D=2 */
329 if (!BN_set_word(D
, 2))
332 * M (= A - 2*B) already has the correct value
335 /* only D=3 remains */
336 if (!BN_set_word(D
, 3))
339 * currently M = A - 2*B, but we need M = A - 3*B
341 if (!BN_sub(M
, M
, B
))
346 if (!BN_div(D
, M
, A
, B
, ctx
))
354 * (**) sign*Y*a == D*B + M (mod |n|).
357 tmp
= A
; /* keep the BIGNUM object, the value does not matter */
359 /* (A, B) := (B, A mod B) ... */
362 /* ... so we have 0 <= B < A again */
365 * Since the former M is now B and the former B is now A,
366 * (**) translates into
367 * sign*Y*a == D*A + B (mod |n|),
369 * sign*Y*a - D*A == B (mod |n|).
370 * Similarly, (*) translates into
371 * -sign*X*a == A (mod |n|).
374 * sign*Y*a + D*sign*X*a == B (mod |n|),
376 * sign*(Y + D*X)*a == B (mod |n|).
378 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
379 * -sign*X*a == B (mod |n|),
380 * sign*Y*a == A (mod |n|).
381 * Note that X and Y stay non-negative all the time.
385 * most of the time D is very small, so we can optimize tmp := D*X+Y
388 if (!BN_add(tmp
, X
, Y
))
391 if (BN_is_word(D
, 2)) {
392 if (!BN_lshift1(tmp
, X
))
394 } else if (BN_is_word(D
, 4)) {
395 if (!BN_lshift(tmp
, X
, 2))
397 } else if (D
->top
== 1) {
398 if (!BN_copy(tmp
, X
))
400 if (!BN_mul_word(tmp
, D
->d
[0]))
403 if (!BN_mul(tmp
, D
, X
, ctx
))
406 if (!BN_add(tmp
, tmp
, Y
))
410 M
= Y
; /* keep the BIGNUM object, the value does not matter */
418 * The while loop (Euclid's algorithm) ends when
421 * sign*Y*a == A (mod |n|),
422 * where Y is non-negative.
426 if (!BN_sub(Y
, n
, Y
))
429 /* Now Y*a == A (mod |n|). */
432 /* Y*a == 1 (mod |n|) */
433 if (!Y
->neg
&& BN_ucmp(Y
, n
) < 0) {
437 if (!BN_nnmod(R
, Y
, n
, ctx
))
447 if ((ret
== NULL
) && (in
== NULL
))
455 * BN_mod_inverse_no_branch is a special version of BN_mod_inverse. It does
456 * not contain branches that may leak sensitive information.
458 static BIGNUM
*BN_mod_inverse_no_branch(BIGNUM
*in
,
459 const BIGNUM
*a
, const BIGNUM
*n
,
462 BIGNUM
*A
, *B
, *X
, *Y
, *M
, *D
, *T
, *R
= NULL
;
489 if (BN_copy(B
, a
) == NULL
)
491 if (BN_copy(A
, n
) == NULL
)
495 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0)) {
497 * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
498 * BN_div_no_branch will be called eventually.
503 BN_with_flags(&local_B
, B
, BN_FLG_CONSTTIME
);
504 if (!BN_nnmod(B
, &local_B
, A
, ctx
))
506 /* Ensure local_B goes out of scope before any further use of B */
511 * From B = a mod |n|, A = |n| it follows that
514 * -sign*X*a == B (mod |n|),
515 * sign*Y*a == A (mod |n|).
518 while (!BN_is_zero(B
)) {
523 * (*) -sign*X*a == B (mod |n|),
524 * sign*Y*a == A (mod |n|)
528 * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
529 * BN_div_no_branch will be called eventually.
534 BN_with_flags(&local_A
, A
, BN_FLG_CONSTTIME
);
536 /* (D, M) := (A/B, A%B) ... */
537 if (!BN_div(D
, M
, &local_A
, B
, ctx
))
539 /* Ensure local_A goes out of scope before any further use of A */
546 * (**) sign*Y*a == D*B + M (mod |n|).
549 tmp
= A
; /* keep the BIGNUM object, the value does not
552 /* (A, B) := (B, A mod B) ... */
555 /* ... so we have 0 <= B < A again */
558 * Since the former M is now B and the former B is now A,
559 * (**) translates into
560 * sign*Y*a == D*A + B (mod |n|),
562 * sign*Y*a - D*A == B (mod |n|).
563 * Similarly, (*) translates into
564 * -sign*X*a == A (mod |n|).
567 * sign*Y*a + D*sign*X*a == B (mod |n|),
569 * sign*(Y + D*X)*a == B (mod |n|).
571 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
572 * -sign*X*a == B (mod |n|),
573 * sign*Y*a == A (mod |n|).
574 * Note that X and Y stay non-negative all the time.
577 if (!BN_mul(tmp
, D
, X
, ctx
))
579 if (!BN_add(tmp
, tmp
, Y
))
582 M
= Y
; /* keep the BIGNUM object, the value does not
590 * The while loop (Euclid's algorithm) ends when
593 * sign*Y*a == A (mod |n|),
594 * where Y is non-negative.
598 if (!BN_sub(Y
, n
, Y
))
601 /* Now Y*a == A (mod |n|). */
604 /* Y*a == 1 (mod |n|) */
605 if (!Y
->neg
&& BN_ucmp(Y
, n
) < 0) {
609 if (!BN_nnmod(R
, Y
, n
, ctx
))
613 BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH
, BN_R_NO_INVERSE
);
618 if ((ret
== NULL
) && (in
== NULL
))