sl@0: /* crypto/bn/bn_gcd.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: static BIGNUM *euclid(BIGNUM *a, BIGNUM *b); sl@0: sl@0: EXPORT_C int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *a,*b,*t; sl@0: int ret=0; sl@0: sl@0: bn_check_top(in_a); sl@0: bn_check_top(in_b); sl@0: sl@0: BN_CTX_start(ctx); sl@0: a = BN_CTX_get(ctx); sl@0: b = BN_CTX_get(ctx); sl@0: if (a == NULL || b == NULL) goto err; sl@0: sl@0: if (BN_copy(a,in_a) == NULL) goto err; sl@0: if (BN_copy(b,in_b) == NULL) goto err; sl@0: a->neg = 0; sl@0: b->neg = 0; sl@0: sl@0: if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; } sl@0: t=euclid(a,b); sl@0: if (t == NULL) goto err; sl@0: sl@0: if (BN_copy(r,t) == NULL) goto err; sl@0: ret=1; sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: bn_check_top(r); sl@0: return(ret); sl@0: } sl@0: sl@0: static BIGNUM *euclid(BIGNUM *a, BIGNUM *b) sl@0: { sl@0: BIGNUM *t; sl@0: int shifts=0; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: /* 0 <= b <= a */ sl@0: while (!BN_is_zero(b)) sl@0: { sl@0: /* 0 < b <= a */ sl@0: sl@0: if (BN_is_odd(a)) sl@0: { sl@0: if (BN_is_odd(b)) sl@0: { sl@0: if (!BN_sub(a,a,b)) goto err; sl@0: if (!BN_rshift1(a,a)) goto err; sl@0: if (BN_cmp(a,b) < 0) sl@0: { t=a; a=b; b=t; } sl@0: } sl@0: else /* a odd - b even */ sl@0: { sl@0: if (!BN_rshift1(b,b)) goto err; sl@0: if (BN_cmp(a,b) < 0) sl@0: { t=a; a=b; b=t; } sl@0: } sl@0: } sl@0: else /* a is even */ sl@0: { sl@0: if (BN_is_odd(b)) sl@0: { sl@0: if (!BN_rshift1(a,a)) goto err; sl@0: if (BN_cmp(a,b) < 0) sl@0: { t=a; a=b; b=t; } sl@0: } sl@0: else /* a even - b even */ sl@0: { sl@0: if (!BN_rshift1(a,a)) goto err; sl@0: if (!BN_rshift1(b,b)) goto err; sl@0: shifts++; sl@0: } sl@0: } sl@0: /* 0 <= b <= a */ sl@0: } sl@0: sl@0: if (shifts) sl@0: { sl@0: if (!BN_lshift(a,a,shifts)) goto err; sl@0: } sl@0: bn_check_top(a); sl@0: return(a); sl@0: err: sl@0: return(NULL); sl@0: } sl@0: sl@0: sl@0: /* solves ax == 1 (mod n) */ sl@0: static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, sl@0: const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); sl@0: EXPORT_C BIGNUM *BN_mod_inverse(BIGNUM *in, sl@0: const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL; sl@0: BIGNUM *ret=NULL; sl@0: int sign; sl@0: if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) sl@0: { sl@0: return BN_mod_inverse_no_branch(in, a, n, ctx); sl@0: } sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(n); sl@0: sl@0: BN_CTX_start(ctx); sl@0: A = BN_CTX_get(ctx); sl@0: B = BN_CTX_get(ctx); sl@0: X = BN_CTX_get(ctx); sl@0: D = BN_CTX_get(ctx); sl@0: M = BN_CTX_get(ctx); sl@0: Y = BN_CTX_get(ctx); sl@0: T = BN_CTX_get(ctx); sl@0: if (T == NULL) goto err; sl@0: sl@0: if (in == NULL) sl@0: R=BN_new(); sl@0: else sl@0: R=in; sl@0: if (R == NULL) goto err; sl@0: sl@0: BN_one(X); sl@0: BN_zero(Y); sl@0: if (BN_copy(B,a) == NULL) goto err; sl@0: if (BN_copy(A,n) == NULL) goto err; sl@0: A->neg = 0; sl@0: if (B->neg || (BN_ucmp(B, A) >= 0)) sl@0: { sl@0: if (!BN_nnmod(B, B, A, ctx)) goto err; sl@0: } sl@0: sign = -1; sl@0: /* From B = a mod |n|, A = |n| it follows that sl@0: * sl@0: * 0 <= B < A, sl@0: * -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|). sl@0: */ sl@0: sl@0: if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) sl@0: { sl@0: /* Binary inversion algorithm; requires odd modulus. sl@0: * This is faster than the general algorithm if the modulus sl@0: * is sufficiently small (about 400 .. 500 bits on 32-bit sl@0: * sytems, but much more on 64-bit systems) */ sl@0: int shift; sl@0: sl@0: while (!BN_is_zero(B)) sl@0: { sl@0: /* sl@0: * 0 < B < |n|, sl@0: * 0 < A <= |n|, sl@0: * (1) -sign*X*a == B (mod |n|), sl@0: * (2) sign*Y*a == A (mod |n|) sl@0: */ sl@0: sl@0: /* Now divide B by the maximum possible power of two in the integers, sl@0: * and divide X by the same value mod |n|. sl@0: * When we're done, (1) still holds. */ sl@0: shift = 0; sl@0: while (!BN_is_bit_set(B, shift)) /* note that 0 < B */ sl@0: { sl@0: shift++; sl@0: sl@0: if (BN_is_odd(X)) sl@0: { sl@0: if (!BN_uadd(X, X, n)) goto err; sl@0: } sl@0: /* now X is even, so we can easily divide it by two */ sl@0: if (!BN_rshift1(X, X)) goto err; sl@0: } sl@0: if (shift > 0) sl@0: { sl@0: if (!BN_rshift(B, B, shift)) goto err; sl@0: } sl@0: sl@0: sl@0: /* Same for A and Y. Afterwards, (2) still holds. */ sl@0: shift = 0; sl@0: while (!BN_is_bit_set(A, shift)) /* note that 0 < A */ sl@0: { sl@0: shift++; sl@0: sl@0: if (BN_is_odd(Y)) sl@0: { sl@0: if (!BN_uadd(Y, Y, n)) goto err; sl@0: } sl@0: /* now Y is even */ sl@0: if (!BN_rshift1(Y, Y)) goto err; sl@0: } sl@0: if (shift > 0) sl@0: { sl@0: if (!BN_rshift(A, A, shift)) goto err; sl@0: } sl@0: sl@0: sl@0: /* We still have (1) and (2). sl@0: * Both A and B are odd. sl@0: * The following computations ensure that sl@0: * sl@0: * 0 <= B < |n|, sl@0: * 0 < A < |n|, sl@0: * (1) -sign*X*a == B (mod |n|), sl@0: * (2) sign*Y*a == A (mod |n|), sl@0: * sl@0: * and that either A or B is even in the next iteration. sl@0: */ sl@0: if (BN_ucmp(B, A) >= 0) sl@0: { sl@0: /* -sign*(X + Y)*a == B - A (mod |n|) */ sl@0: if (!BN_uadd(X, X, Y)) goto err; sl@0: /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that sl@0: * actually makes the algorithm slower */ sl@0: if (!BN_usub(B, B, A)) goto err; sl@0: } sl@0: else sl@0: { sl@0: /* sign*(X + Y)*a == A - B (mod |n|) */ sl@0: if (!BN_uadd(Y, Y, X)) goto err; sl@0: /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */ sl@0: if (!BN_usub(A, A, B)) goto err; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: /* general inversion algorithm */ sl@0: sl@0: while (!BN_is_zero(B)) sl@0: { sl@0: BIGNUM *tmp; sl@0: sl@0: /* sl@0: * 0 < B < A, sl@0: * (*) -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|) sl@0: */ sl@0: sl@0: /* (D, M) := (A/B, A%B) ... */ sl@0: if (BN_num_bits(A) == BN_num_bits(B)) sl@0: { sl@0: if (!BN_one(D)) goto err; sl@0: if (!BN_sub(M,A,B)) goto err; sl@0: } sl@0: else if (BN_num_bits(A) == BN_num_bits(B) + 1) sl@0: { sl@0: /* A/B is 1, 2, or 3 */ sl@0: if (!BN_lshift1(T,B)) goto err; sl@0: if (BN_ucmp(A,T) < 0) sl@0: { sl@0: /* A < 2*B, so D=1 */ sl@0: if (!BN_one(D)) goto err; sl@0: if (!BN_sub(M,A,B)) goto err; sl@0: } sl@0: else sl@0: { sl@0: /* A >= 2*B, so D=2 or D=3 */ sl@0: if (!BN_sub(M,A,T)) goto err; sl@0: if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */ sl@0: if (BN_ucmp(A,D) < 0) sl@0: { sl@0: /* A < 3*B, so D=2 */ sl@0: if (!BN_set_word(D,2)) goto err; sl@0: /* M (= A - 2*B) already has the correct value */ sl@0: } sl@0: else sl@0: { sl@0: /* only D=3 remains */ sl@0: if (!BN_set_word(D,3)) goto err; sl@0: /* currently M = A - 2*B, but we need M = A - 3*B */ sl@0: if (!BN_sub(M,M,B)) goto err; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (!BN_div(D,M,A,B,ctx)) goto err; sl@0: } sl@0: sl@0: /* Now sl@0: * A = D*B + M; sl@0: * thus we have sl@0: * (**) sign*Y*a == D*B + M (mod |n|). sl@0: */ sl@0: sl@0: tmp=A; /* keep the BIGNUM object, the value does not matter */ sl@0: sl@0: /* (A, B) := (B, A mod B) ... */ sl@0: A=B; sl@0: B=M; sl@0: /* ... so we have 0 <= B < A again */ sl@0: sl@0: /* Since the former M is now B and the former B is now A, sl@0: * (**) translates into sl@0: * sign*Y*a == D*A + B (mod |n|), sl@0: * i.e. sl@0: * sign*Y*a - D*A == B (mod |n|). sl@0: * Similarly, (*) translates into sl@0: * -sign*X*a == A (mod |n|). sl@0: * sl@0: * Thus, sl@0: * sign*Y*a + D*sign*X*a == B (mod |n|), sl@0: * i.e. sl@0: * sign*(Y + D*X)*a == B (mod |n|). sl@0: * sl@0: * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at sl@0: * -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|). sl@0: * Note that X and Y stay non-negative all the time. sl@0: */ sl@0: sl@0: /* most of the time D is very small, so we can optimize tmp := D*X+Y */ sl@0: if (BN_is_one(D)) sl@0: { sl@0: if (!BN_add(tmp,X,Y)) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (BN_is_word(D,2)) sl@0: { sl@0: if (!BN_lshift1(tmp,X)) goto err; sl@0: } sl@0: else if (BN_is_word(D,4)) sl@0: { sl@0: if (!BN_lshift(tmp,X,2)) goto err; sl@0: } sl@0: else if (D->top == 1) sl@0: { sl@0: if (!BN_copy(tmp,X)) goto err; sl@0: if (!BN_mul_word(tmp,D->d[0])) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_mul(tmp,D,X,ctx)) goto err; sl@0: } sl@0: if (!BN_add(tmp,tmp,Y)) goto err; sl@0: } sl@0: sl@0: M=Y; /* keep the BIGNUM object, the value does not matter */ sl@0: Y=X; sl@0: X=tmp; sl@0: sign = -sign; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * The while loop (Euclid's algorithm) ends when sl@0: * A == gcd(a,n); sl@0: * we have sl@0: * sign*Y*a == A (mod |n|), sl@0: * where Y is non-negative. sl@0: */ sl@0: sl@0: if (sign < 0) sl@0: { sl@0: if (!BN_sub(Y,n,Y)) goto err; sl@0: } sl@0: /* Now Y*a == A (mod |n|). */ sl@0: sl@0: sl@0: if (BN_is_one(A)) sl@0: { sl@0: /* Y*a == 1 (mod |n|) */ sl@0: if (!Y->neg && BN_ucmp(Y,n) < 0) sl@0: { sl@0: if (!BN_copy(R,Y)) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_nnmod(R,Y,n,ctx)) goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE); sl@0: goto err; sl@0: } sl@0: ret=R; sl@0: err: sl@0: if ((ret == NULL) && (in == NULL)) BN_free(R); sl@0: BN_CTX_end(ctx); sl@0: bn_check_top(ret); sl@0: return(ret); sl@0: } sl@0: sl@0: sl@0: /* BN_mod_inverse_no_branch is a special version of BN_mod_inverse. sl@0: * It does not contain branches that may leak sensitive information. sl@0: */ sl@0: static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, sl@0: const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL; sl@0: BIGNUM local_A, local_B; sl@0: BIGNUM *pA, *pB; sl@0: BIGNUM *ret=NULL; sl@0: int sign; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(n); sl@0: sl@0: BN_CTX_start(ctx); sl@0: A = BN_CTX_get(ctx); sl@0: B = BN_CTX_get(ctx); sl@0: X = BN_CTX_get(ctx); sl@0: D = BN_CTX_get(ctx); sl@0: M = BN_CTX_get(ctx); sl@0: Y = BN_CTX_get(ctx); sl@0: T = BN_CTX_get(ctx); sl@0: if (T == NULL) goto err; sl@0: sl@0: if (in == NULL) sl@0: R=BN_new(); sl@0: else sl@0: R=in; sl@0: if (R == NULL) goto err; sl@0: sl@0: BN_one(X); sl@0: BN_zero(Y); sl@0: if (BN_copy(B,a) == NULL) goto err; sl@0: if (BN_copy(A,n) == NULL) goto err; sl@0: A->neg = 0; sl@0: sl@0: if (B->neg || (BN_ucmp(B, A) >= 0)) sl@0: { sl@0: /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, sl@0: * BN_div_no_branch will be called eventually. sl@0: */ sl@0: pB = &local_B; sl@0: BN_with_flags(pB, B, BN_FLG_CONSTTIME); sl@0: if (!BN_nnmod(B, pB, A, ctx)) goto err; sl@0: } sl@0: sign = -1; sl@0: /* From B = a mod |n|, A = |n| it follows that sl@0: * sl@0: * 0 <= B < A, sl@0: * -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|). sl@0: */ sl@0: sl@0: while (!BN_is_zero(B)) sl@0: { sl@0: BIGNUM *tmp; sl@0: sl@0: /* sl@0: * 0 < B < A, sl@0: * (*) -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|) sl@0: */ sl@0: sl@0: /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, sl@0: * BN_div_no_branch will be called eventually. sl@0: */ sl@0: pA = &local_A; sl@0: BN_with_flags(pA, A, BN_FLG_CONSTTIME); sl@0: sl@0: /* (D, M) := (A/B, A%B) ... */ sl@0: if (!BN_div(D,M,pA,B,ctx)) goto err; sl@0: sl@0: /* Now sl@0: * A = D*B + M; sl@0: * thus we have sl@0: * (**) sign*Y*a == D*B + M (mod |n|). sl@0: */ sl@0: sl@0: tmp=A; /* keep the BIGNUM object, the value does not matter */ sl@0: sl@0: /* (A, B) := (B, A mod B) ... */ sl@0: A=B; sl@0: B=M; sl@0: /* ... so we have 0 <= B < A again */ sl@0: sl@0: /* Since the former M is now B and the former B is now A, sl@0: * (**) translates into sl@0: * sign*Y*a == D*A + B (mod |n|), sl@0: * i.e. sl@0: * sign*Y*a - D*A == B (mod |n|). sl@0: * Similarly, (*) translates into sl@0: * -sign*X*a == A (mod |n|). sl@0: * sl@0: * Thus, sl@0: * sign*Y*a + D*sign*X*a == B (mod |n|), sl@0: * i.e. sl@0: * sign*(Y + D*X)*a == B (mod |n|). sl@0: * sl@0: * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at sl@0: * -sign*X*a == B (mod |n|), sl@0: * sign*Y*a == A (mod |n|). sl@0: * Note that X and Y stay non-negative all the time. sl@0: */ sl@0: sl@0: if (!BN_mul(tmp,D,X,ctx)) goto err; sl@0: if (!BN_add(tmp,tmp,Y)) goto err; sl@0: sl@0: M=Y; /* keep the BIGNUM object, the value does not matter */ sl@0: Y=X; sl@0: X=tmp; sl@0: sign = -sign; sl@0: } sl@0: sl@0: /* sl@0: * The while loop (Euclid's algorithm) ends when sl@0: * A == gcd(a,n); sl@0: * we have sl@0: * sign*Y*a == A (mod |n|), sl@0: * where Y is non-negative. sl@0: */ sl@0: sl@0: if (sign < 0) sl@0: { sl@0: if (!BN_sub(Y,n,Y)) goto err; sl@0: } sl@0: /* Now Y*a == A (mod |n|). */ sl@0: sl@0: if (BN_is_one(A)) sl@0: { sl@0: /* Y*a == 1 (mod |n|) */ sl@0: if (!Y->neg && BN_ucmp(Y,n) < 0) sl@0: { sl@0: if (!BN_copy(R,Y)) goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_nnmod(R,Y,n,ctx)) goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE); sl@0: goto err; sl@0: } sl@0: ret=R; sl@0: err: sl@0: if ((ret == NULL) && (in == NULL)) BN_free(R); sl@0: BN_CTX_end(ctx); sl@0: bn_check_top(ret); sl@0: return(ret); sl@0: }