1 /* crypto/bn/bn_gcd.c */
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 "cryptlib.h"
115 static BIGNUM
*euclid(BIGNUM
*a
, BIGNUM
*b
);
117 int BN_gcd(BIGNUM
*r
, const BIGNUM
*in_a
, const BIGNUM
*in_b
, BN_CTX
*ctx
)
128 if (a
== NULL
|| b
== NULL
) goto err
;
130 if (BN_copy(a
,in_a
) == NULL
) goto err
;
131 if (BN_copy(b
,in_b
) == NULL
) goto err
;
135 if (BN_cmp(a
,b
) < 0) { t
=a
; a
=b
; b
=t
; }
137 if (t
== NULL
) goto err
;
139 if (BN_copy(r
,t
) == NULL
) goto err
;
147 static BIGNUM
*euclid(BIGNUM
*a
, BIGNUM
*b
)
156 while (!BN_is_zero(b
))
164 if (!BN_sub(a
,a
,b
)) goto err
;
165 if (!BN_rshift1(a
,a
)) goto err
;
169 else /* a odd - b even */
171 if (!BN_rshift1(b
,b
)) goto err
;
180 if (!BN_rshift1(a
,a
)) goto err
;
184 else /* a even - b even */
186 if (!BN_rshift1(a
,a
)) goto err
;
187 if (!BN_rshift1(b
,b
)) goto err
;
196 if (!BN_lshift(a
,a
,shifts
)) goto err
;
205 /* solves ax == 1 (mod n) */
206 static BIGNUM
*BN_mod_inverse_no_branch(BIGNUM
*in
,
207 const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
);
209 BIGNUM
*BN_mod_inverse(BIGNUM
*in
,
210 const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
212 BIGNUM
*A
,*B
,*X
,*Y
,*M
,*D
,*T
,*R
=NULL
;
216 if ((BN_get_flags(a
, BN_FLG_CONSTTIME
) != 0) || (BN_get_flags(n
, BN_FLG_CONSTTIME
) != 0))
218 return BN_mod_inverse_no_branch(in
, a
, n
, ctx
);
232 if (T
== NULL
) goto err
;
238 if (R
== NULL
) goto err
;
242 if (BN_copy(B
,a
) == NULL
) goto err
;
243 if (BN_copy(A
,n
) == NULL
) goto err
;
245 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0))
247 if (!BN_nnmod(B
, B
, A
, ctx
)) goto err
;
251 * From B = a mod |n|, A = |n| it follows that
254 * -sign*X*a == B (mod |n|),
255 * sign*Y*a == A (mod |n|).
258 if (BN_is_odd(n
) && (BN_num_bits(n
) <= (BN_BITS
<= 32 ? 450 : 2048)))
260 /* Binary inversion algorithm; requires odd modulus.
261 * This is faster than the general algorithm if the modulus
262 * is sufficiently small (about 400 .. 500 bits on 32-bit
263 * sytems, but much more on 64-bit systems) */
266 while (!BN_is_zero(B
))
271 * (1) -sign*X*a == B (mod |n|),
272 * (2) sign*Y*a == A (mod |n|)
275 /* Now divide B by the maximum possible power of two in the integers,
276 * and divide X by the same value mod |n|.
277 * When we're done, (1) still holds. */
279 while (!BN_is_bit_set(B
, shift
)) /* note that 0 < B */
285 if (!BN_uadd(X
, X
, n
)) goto err
;
287 /* now X is even, so we can easily divide it by two */
288 if (!BN_rshift1(X
, X
)) goto err
;
292 if (!BN_rshift(B
, B
, shift
)) goto err
;
296 /* Same for A and Y. Afterwards, (2) still holds. */
298 while (!BN_is_bit_set(A
, shift
)) /* note that 0 < A */
304 if (!BN_uadd(Y
, Y
, n
)) goto err
;
307 if (!BN_rshift1(Y
, Y
)) goto err
;
311 if (!BN_rshift(A
, A
, shift
)) goto err
;
316 * We still have (1) and (2).
317 * Both A and B are odd.
318 * The following computations ensure that
322 * (1) -sign*X*a == B (mod |n|),
323 * (2) sign*Y*a == A (mod |n|),
325 * and that either A or B is even in the next iteration.
327 if (BN_ucmp(B
, A
) >= 0)
329 /* -sign*(X + Y)*a == B - A (mod |n|) */
330 if (!BN_uadd(X
, X
, Y
)) goto err
;
331 /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
332 * actually makes the algorithm slower */
333 if (!BN_usub(B
, B
, A
)) goto err
;
337 /* sign*(X + Y)*a == A - B (mod |n|) */
338 if (!BN_uadd(Y
, Y
, X
)) goto err
;
339 /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
340 if (!BN_usub(A
, A
, B
)) goto err
;
346 /* general inversion algorithm */
348 while (!BN_is_zero(B
))
354 * (*) -sign*X*a == B (mod |n|),
355 * sign*Y*a == A (mod |n|)
358 /* (D, M) := (A/B, A%B) ... */
359 if (BN_num_bits(A
) == BN_num_bits(B
))
361 if (!BN_one(D
)) goto err
;
362 if (!BN_sub(M
,A
,B
)) goto err
;
364 else if (BN_num_bits(A
) == BN_num_bits(B
) + 1)
366 /* A/B is 1, 2, or 3 */
367 if (!BN_lshift1(T
,B
)) goto err
;
368 if (BN_ucmp(A
,T
) < 0)
370 /* A < 2*B, so D=1 */
371 if (!BN_one(D
)) goto err
;
372 if (!BN_sub(M
,A
,B
)) goto err
;
376 /* A >= 2*B, so D=2 or D=3 */
377 if (!BN_sub(M
,A
,T
)) goto err
;
378 if (!BN_add(D
,T
,B
)) goto err
; /* use D (:= 3*B) as temp */
379 if (BN_ucmp(A
,D
) < 0)
381 /* A < 3*B, so D=2 */
382 if (!BN_set_word(D
,2)) goto err
;
383 /* M (= A - 2*B) already has the correct value */
387 /* only D=3 remains */
388 if (!BN_set_word(D
,3)) goto err
;
389 /* currently M = A - 2*B, but we need M = A - 3*B */
390 if (!BN_sub(M
,M
,B
)) goto err
;
396 if (!BN_div(D
,M
,A
,B
,ctx
)) goto err
;
403 * (**) sign*Y*a == D*B + M (mod |n|).
406 tmp
=A
; /* keep the BIGNUM object, the value does not matter */
408 /* (A, B) := (B, A mod B) ... */
411 /* ... so we have 0 <= B < A again */
414 * Since the former M is now B and the former B is now A,
415 * (**) translates into
416 * sign*Y*a == D*A + B (mod |n|),
418 * sign*Y*a - D*A == B (mod |n|).
419 * Similarly, (*) translates into
420 * -sign*X*a == A (mod |n|).
423 * sign*Y*a + D*sign*X*a == B (mod |n|),
425 * sign*(Y + D*X)*a == B (mod |n|).
427 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
428 * -sign*X*a == B (mod |n|),
429 * sign*Y*a == A (mod |n|).
430 * Note that X and Y stay non-negative all the time.
433 /* most of the time D is very small, so we can optimize tmp := D*X+Y */
436 if (!BN_add(tmp
,X
,Y
)) goto err
;
442 if (!BN_lshift1(tmp
,X
)) goto err
;
444 else if (BN_is_word(D
,4))
446 if (!BN_lshift(tmp
,X
,2)) goto err
;
448 else if (D
->top
== 1)
450 if (!BN_copy(tmp
,X
)) goto err
;
451 if (!BN_mul_word(tmp
,D
->d
[0])) goto err
;
455 if (!BN_mul(tmp
,D
,X
,ctx
)) goto err
;
457 if (!BN_add(tmp
,tmp
,Y
)) goto err
;
460 M
=Y
; /* keep the BIGNUM object, the value does not matter */
468 * The while loop (Euclid's algorithm) ends when
471 * sign*Y*a == A (mod |n|),
472 * where Y is non-negative.
477 if (!BN_sub(Y
,n
,Y
)) goto err
;
479 /* Now Y*a == A (mod |n|). */
484 /* Y*a == 1 (mod |n|) */
485 if (!Y
->neg
&& BN_ucmp(Y
,n
) < 0)
487 if (!BN_copy(R
,Y
)) goto err
;
491 if (!BN_nnmod(R
,Y
,n
,ctx
)) goto err
;
496 BNerr(BN_F_BN_MOD_INVERSE
,BN_R_NO_INVERSE
);
501 if ((ret
== NULL
) && (in
== NULL
)) BN_free(R
);
508 /* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
509 * It does not contain branches that may leak sensitive information.
511 static BIGNUM
*BN_mod_inverse_no_branch(BIGNUM
*in
,
512 const BIGNUM
*a
, const BIGNUM
*n
, BN_CTX
*ctx
)
514 BIGNUM
*A
,*B
,*X
,*Y
,*M
,*D
,*T
,*R
=NULL
;
515 BIGNUM local_A
, local_B
;
531 if (T
== NULL
) goto err
;
537 if (R
== NULL
) goto err
;
541 if (BN_copy(B
,a
) == NULL
) goto err
;
542 if (BN_copy(A
,n
) == NULL
) goto err
;
545 if (B
->neg
|| (BN_ucmp(B
, A
) >= 0))
547 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
548 * BN_div_no_branch will be called eventually.
551 BN_with_flags(pB
, B
, BN_FLG_CONSTTIME
);
552 if (!BN_nnmod(B
, pB
, A
, ctx
)) goto err
;
556 * From B = a mod |n|, A = |n| it follows that
559 * -sign*X*a == B (mod |n|),
560 * sign*Y*a == A (mod |n|).
563 while (!BN_is_zero(B
))
569 * (*) -sign*X*a == B (mod |n|),
570 * sign*Y*a == A (mod |n|)
573 /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
574 * BN_div_no_branch will be called eventually.
577 BN_with_flags(pA
, A
, BN_FLG_CONSTTIME
);
579 /* (D, M) := (A/B, A%B) ... */
580 if (!BN_div(D
,M
,pA
,B
,ctx
)) goto err
;
586 * (**) sign*Y*a == D*B + M (mod |n|).
589 tmp
=A
; /* keep the BIGNUM object, the value does not matter */
591 /* (A, B) := (B, A mod B) ... */
594 /* ... so we have 0 <= B < A again */
597 * Since the former M is now B and the former B is now A,
598 * (**) translates into
599 * sign*Y*a == D*A + B (mod |n|),
601 * sign*Y*a - D*A == B (mod |n|).
602 * Similarly, (*) translates into
603 * -sign*X*a == A (mod |n|).
606 * sign*Y*a + D*sign*X*a == B (mod |n|),
608 * sign*(Y + D*X)*a == B (mod |n|).
610 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
611 * -sign*X*a == B (mod |n|),
612 * sign*Y*a == A (mod |n|).
613 * Note that X and Y stay non-negative all the time.
616 if (!BN_mul(tmp
,D
,X
,ctx
)) goto err
;
617 if (!BN_add(tmp
,tmp
,Y
)) goto err
;
619 M
=Y
; /* keep the BIGNUM object, the value does not matter */
626 * The while loop (Euclid's algorithm) ends when
629 * sign*Y*a == A (mod |n|),
630 * where Y is non-negative.
635 if (!BN_sub(Y
,n
,Y
)) goto err
;
637 /* Now Y*a == A (mod |n|). */
641 /* Y*a == 1 (mod |n|) */
642 if (!Y
->neg
&& BN_ucmp(Y
,n
) < 0)
644 if (!BN_copy(R
,Y
)) goto err
;
648 if (!BN_nnmod(R
,Y
,n
,ctx
)) goto err
;
653 BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH
,BN_R_NO_INVERSE
);
658 if ((ret
== NULL
) && (in
== NULL
)) BN_free(R
);