1 /* $OpenBSD: bn_gcd.c,v 1.15 2017/01/29 17:49:22 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
112 #include <openssl/err.h>
116 static BIGNUM
*euclid(BIGNUM
*a
, BIGNUM
*b
);
117 static BIGNUM
*BN_gcd_no_branch(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
,
121 BN_gcd(BIGNUM
*r
, const BIGNUM
*in_a
, const BIGNUM
*in_b
, BN_CTX
*ctx
)
130 if ((a
= BN_CTX_get(ctx
)) == NULL
)
132 if ((b
= BN_CTX_get(ctx
)) == NULL
)
135 if (BN_copy(a
, in_a
) == NULL
)
137 if (BN_copy(b
, in_b
) == NULL
)
142 if (BN_cmp(a
, b
) < 0) {
151 if (BN_copy(r
, t
) == NULL
)
162 BN_gcd_ct(BIGNUM
*r
, const BIGNUM
*in_a
, const BIGNUM
*in_b
, BN_CTX
*ctx
)
164 if (BN_gcd_no_branch(r
, in_a
, in_b
, ctx
) == NULL
)
170 BN_gcd_nonct(BIGNUM
*r
, const BIGNUM
*in_a
, const BIGNUM
*in_b
, BN_CTX
*ctx
)
172 return BN_gcd(r
, in_a
, in_b
, ctx
);
177 euclid(BIGNUM
*a
, BIGNUM
*b
)
186 while (!BN_is_zero(b
)) {
191 if (!BN_sub(a
, a
, b
))
193 if (!BN_rshift1(a
, a
))
195 if (BN_cmp(a
, b
) < 0) {
201 else /* a odd - b even */
203 if (!BN_rshift1(b
, b
))
205 if (BN_cmp(a
, b
) < 0) {
215 if (!BN_rshift1(a
, a
))
217 if (BN_cmp(a
, b
) < 0) {
223 else /* a even - b even */
225 if (!BN_rshift1(a
, a
))
227 if (!BN_rshift1(b
, b
))
236 if (!BN_lshift(a
, a
, shifts
))
247 /* solves ax == 1 (mod n) */
248 static BIGNUM
*BN_mod_inverse_no_branch(BIGNUM
*in
, const BIGNUM
*a
,
249 const BIGNUM
*n
, BN_CTX
*ctx
);
252 BN_mod_inverse_internal(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
,
255 BIGNUM
*A
, *B
, *X
, *Y
, *M
, *D
, *T
, *R
= NULL
;
260 return BN_mod_inverse_no_branch(in
, a
, n
, ctx
);
266 if ((A
= BN_CTX_get(ctx
)) == NULL
)
268 if ((B
= BN_CTX_get(ctx
)) == NULL
)
270 if ((X
= BN_CTX_get(ctx
)) == NULL
)
272 if ((D
= BN_CTX_get(ctx
)) == NULL
)
274 if ((M
= BN_CTX_get(ctx
)) == NULL
)
276 if ((Y
= BN_CTX_get(ctx
)) == NULL
)
278 if ((T
= BN_CTX_get(ctx
)) == NULL
)
290 if (BN_copy(B
, a
) == NULL
)
292 if (BN_copy(A
, n
) == NULL
)
295 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0)) {
296 if (!BN_nnmod(B
, B
, A
, ctx
))
300 /* From B = a mod |n|, A = |n| it follows that
303 * -sign*X*a == B (mod |n|),
304 * sign*Y*a == A (mod |n|).
307 if (BN_is_odd(n
) && (BN_num_bits(n
) <= (BN_BITS
<= 32 ? 450 : 2048))) {
308 /* Binary inversion algorithm; requires odd modulus.
309 * This is faster than the general algorithm if the modulus
310 * is sufficiently small (about 400 .. 500 bits on 32-bit
311 * sytems, but much more on 64-bit systems) */
314 while (!BN_is_zero(B
)) {
318 * (1) -sign*X*a == B (mod |n|),
319 * (2) sign*Y*a == A (mod |n|)
322 /* Now divide B by the maximum possible power of two in the integers,
323 * and divide X by the same value mod |n|.
324 * When we're done, (1) still holds. */
326 while (!BN_is_bit_set(B
, shift
)) /* note that 0 < B */
331 if (!BN_uadd(X
, X
, n
))
334 /* now X is even, so we can easily divide it by two */
335 if (!BN_rshift1(X
, X
))
339 if (!BN_rshift(B
, B
, shift
))
344 /* Same for A and Y. Afterwards, (2) still holds. */
346 while (!BN_is_bit_set(A
, shift
)) /* note that 0 < A */
351 if (!BN_uadd(Y
, Y
, n
))
355 if (!BN_rshift1(Y
, Y
))
359 if (!BN_rshift(A
, A
, shift
))
364 /* We still have (1) and (2).
365 * Both A and B are odd.
366 * The following computations ensure that
370 * (1) -sign*X*a == B (mod |n|),
371 * (2) sign*Y*a == A (mod |n|),
373 * and that either A or B is even in the next iteration.
375 if (BN_ucmp(B
, A
) >= 0) {
376 /* -sign*(X + Y)*a == B - A (mod |n|) */
377 if (!BN_uadd(X
, X
, Y
))
379 /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
380 * actually makes the algorithm slower */
381 if (!BN_usub(B
, B
, A
))
384 /* sign*(X + Y)*a == A - B (mod |n|) */
385 if (!BN_uadd(Y
, Y
, X
))
387 /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
388 if (!BN_usub(A
, A
, B
))
393 /* general inversion algorithm */
395 while (!BN_is_zero(B
)) {
400 * (*) -sign*X*a == B (mod |n|),
401 * sign*Y*a == A (mod |n|)
404 /* (D, M) := (A/B, A%B) ... */
405 if (BN_num_bits(A
) == BN_num_bits(B
)) {
408 if (!BN_sub(M
, A
, B
))
410 } else if (BN_num_bits(A
) == BN_num_bits(B
) + 1) {
411 /* A/B is 1, 2, or 3 */
412 if (!BN_lshift1(T
, B
))
414 if (BN_ucmp(A
, T
) < 0) {
415 /* A < 2*B, so D=1 */
418 if (!BN_sub(M
, A
, B
))
421 /* A >= 2*B, so D=2 or D=3 */
422 if (!BN_sub(M
, A
, T
))
424 if (!BN_add(D
,T
,B
)) goto err
; /* use D (:= 3*B) as temp */
425 if (BN_ucmp(A
, D
) < 0) {
426 /* A < 3*B, so D=2 */
427 if (!BN_set_word(D
, 2))
429 /* M (= A - 2*B) already has the correct value */
431 /* only D=3 remains */
432 if (!BN_set_word(D
, 3))
434 /* currently M = A - 2*B, but we need M = A - 3*B */
435 if (!BN_sub(M
, M
, B
))
440 if (!BN_div_nonct(D
, M
, A
, B
, ctx
))
447 * (**) sign*Y*a == D*B + M (mod |n|).
449 tmp
= A
; /* keep the BIGNUM object, the value does not matter */
451 /* (A, B) := (B, A mod B) ... */
454 /* ... so we have 0 <= B < A again */
456 /* Since the former M is now B and the former B is now A,
457 * (**) translates into
458 * sign*Y*a == D*A + B (mod |n|),
460 * sign*Y*a - D*A == B (mod |n|).
461 * Similarly, (*) translates into
462 * -sign*X*a == A (mod |n|).
465 * sign*Y*a + D*sign*X*a == B (mod |n|),
467 * sign*(Y + D*X)*a == B (mod |n|).
469 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
470 * -sign*X*a == B (mod |n|),
471 * sign*Y*a == A (mod |n|).
472 * Note that X and Y stay non-negative all the time.
475 /* most of the time D is very small, so we can optimize tmp := D*X+Y */
477 if (!BN_add(tmp
, X
, Y
))
480 if (BN_is_word(D
, 2)) {
481 if (!BN_lshift1(tmp
, X
))
483 } else if (BN_is_word(D
, 4)) {
484 if (!BN_lshift(tmp
, X
, 2))
486 } else if (D
->top
== 1) {
487 if (!BN_copy(tmp
, X
))
489 if (!BN_mul_word(tmp
, D
->d
[0]))
492 if (!BN_mul(tmp
, D
,X
, ctx
))
495 if (!BN_add(tmp
, tmp
, Y
))
499 M
= Y
; /* keep the BIGNUM object, the value does not matter */
507 * The while loop (Euclid's algorithm) ends when
510 * sign*Y*a == A (mod |n|),
511 * where Y is non-negative.
515 if (!BN_sub(Y
, n
, Y
))
518 /* Now Y*a == A (mod |n|). */
521 /* Y*a == 1 (mod |n|) */
522 if (!Y
->neg
&& BN_ucmp(Y
, n
) < 0) {
526 if (!BN_nnmod(R
, Y
,n
, ctx
))
530 BNerror(BN_R_NO_INVERSE
);
536 if ((ret
== NULL
) && (in
== NULL
))
544 BN_mod_inverse(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
546 int ct
= ((BN_get_flags(a
, BN_FLG_CONSTTIME
) != 0) ||
547 (BN_get_flags(n
, BN_FLG_CONSTTIME
) != 0));
548 return BN_mod_inverse_internal(in
, a
, n
, ctx
, ct
);
552 BN_mod_inverse_nonct(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
554 return BN_mod_inverse_internal(in
, a
, n
, ctx
, 0);
558 BN_mod_inverse_ct(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
560 return BN_mod_inverse_internal(in
, a
, n
, ctx
, 1);
563 /* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
564 * It does not contain branches that may leak sensitive information.
567 BN_mod_inverse_no_branch(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
,
570 BIGNUM
*A
, *B
, *X
, *Y
, *M
, *D
, *T
, *R
= NULL
;
571 BIGNUM local_A
, local_B
;
580 if ((A
= BN_CTX_get(ctx
)) == NULL
)
582 if ((B
= BN_CTX_get(ctx
)) == NULL
)
584 if ((X
= BN_CTX_get(ctx
)) == NULL
)
586 if ((D
= BN_CTX_get(ctx
)) == NULL
)
588 if ((M
= BN_CTX_get(ctx
)) == NULL
)
590 if ((Y
= BN_CTX_get(ctx
)) == NULL
)
592 if ((T
= BN_CTX_get(ctx
)) == NULL
)
604 if (BN_copy(B
, a
) == NULL
)
606 if (BN_copy(A
, n
) == NULL
)
610 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0)) {
611 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
612 * BN_div_no_branch will be called eventually.
615 BN_with_flags(pB
, B
, BN_FLG_CONSTTIME
);
616 if (!BN_nnmod(B
, pB
, A
, ctx
))
620 /* From B = a mod |n|, A = |n| it follows that
623 * -sign*X*a == B (mod |n|),
624 * sign*Y*a == A (mod |n|).
627 while (!BN_is_zero(B
)) {
632 * (*) -sign*X*a == B (mod |n|),
633 * sign*Y*a == A (mod |n|)
636 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
637 * BN_div_no_branch will be called eventually.
640 BN_with_flags(pA
, A
, BN_FLG_CONSTTIME
);
642 /* (D, M) := (A/B, A%B) ... */
643 if (!BN_div_ct(D
, M
, pA
, B
, ctx
))
649 * (**) sign*Y*a == D*B + M (mod |n|).
651 tmp
= A
; /* keep the BIGNUM object, the value does not matter */
653 /* (A, B) := (B, A mod B) ... */
656 /* ... so we have 0 <= B < A again */
658 /* Since the former M is now B and the former B is now A,
659 * (**) translates into
660 * sign*Y*a == D*A + B (mod |n|),
662 * sign*Y*a - D*A == B (mod |n|).
663 * Similarly, (*) translates into
664 * -sign*X*a == A (mod |n|).
667 * sign*Y*a + D*sign*X*a == B (mod |n|),
669 * sign*(Y + D*X)*a == B (mod |n|).
671 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
672 * -sign*X*a == B (mod |n|),
673 * sign*Y*a == A (mod |n|).
674 * Note that X and Y stay non-negative all the time.
677 if (!BN_mul(tmp
, D
, X
, ctx
))
679 if (!BN_add(tmp
, tmp
, Y
))
682 M
= Y
; /* keep the BIGNUM object, the value does not matter */
689 * The while loop (Euclid's algorithm) ends when
692 * sign*Y*a == A (mod |n|),
693 * where Y is non-negative.
697 if (!BN_sub(Y
, n
, Y
))
700 /* Now Y*a == A (mod |n|). */
703 /* Y*a == 1 (mod |n|) */
704 if (!Y
->neg
&& BN_ucmp(Y
, n
) < 0) {
708 if (!BN_nnmod(R
, Y
, n
, ctx
))
712 BNerror(BN_R_NO_INVERSE
);
718 if ((ret
== NULL
) && (in
== NULL
))
726 * BN_gcd_no_branch is a special version of BN_mod_inverse_no_branch.
727 * that returns the GCD.
730 BN_gcd_no_branch(BIGNUM
*in
, const BIGNUM
*a
, const BIGNUM
*n
,
733 BIGNUM
*A
, *B
, *X
, *Y
, *M
, *D
, *T
, *R
= NULL
;
734 BIGNUM local_A
, local_B
;
747 if ((A
= BN_CTX_get(ctx
)) == NULL
)
749 if ((B
= BN_CTX_get(ctx
)) == NULL
)
751 if ((X
= BN_CTX_get(ctx
)) == NULL
)
753 if ((D
= BN_CTX_get(ctx
)) == NULL
)
755 if ((M
= BN_CTX_get(ctx
)) == NULL
)
757 if ((Y
= BN_CTX_get(ctx
)) == NULL
)
759 if ((T
= BN_CTX_get(ctx
)) == NULL
)
764 if (BN_copy(B
, a
) == NULL
)
766 if (BN_copy(A
, n
) == NULL
)
770 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0)) {
771 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
772 * BN_div_no_branch will be called eventually.
775 BN_with_flags(pB
, B
, BN_FLG_CONSTTIME
);
776 if (!BN_nnmod(B
, pB
, A
, ctx
))
780 /* From B = a mod |n|, A = |n| it follows that
783 * -sign*X*a == B (mod |n|),
784 * sign*Y*a == A (mod |n|).
787 while (!BN_is_zero(B
)) {
792 * (*) -sign*X*a == B (mod |n|),
793 * sign*Y*a == A (mod |n|)
796 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
797 * BN_div_no_branch will be called eventually.
800 BN_with_flags(pA
, A
, BN_FLG_CONSTTIME
);
802 /* (D, M) := (A/B, A%B) ... */
803 if (!BN_div_ct(D
, M
, pA
, B
, ctx
))
809 * (**) sign*Y*a == D*B + M (mod |n|).
811 tmp
= A
; /* keep the BIGNUM object, the value does not matter */
813 /* (A, B) := (B, A mod B) ... */
816 /* ... so we have 0 <= B < A again */
818 /* Since the former M is now B and the former B is now A,
819 * (**) translates into
820 * sign*Y*a == D*A + B (mod |n|),
822 * sign*Y*a - D*A == B (mod |n|).
823 * Similarly, (*) translates into
824 * -sign*X*a == A (mod |n|).
827 * sign*Y*a + D*sign*X*a == B (mod |n|),
829 * sign*(Y + D*X)*a == B (mod |n|).
831 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
832 * -sign*X*a == B (mod |n|),
833 * sign*Y*a == A (mod |n|).
834 * Note that X and Y stay non-negative all the time.
837 if (!BN_mul(tmp
, D
, X
, ctx
))
839 if (!BN_add(tmp
, tmp
, Y
))
842 M
= Y
; /* keep the BIGNUM object, the value does not matter */
849 * The while loop (Euclid's algorithm) ends when
857 if ((ret
== NULL
) && (in
== NULL
))