10Sstevel@tonic-gate /* crypto/bn/bn_gcd.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate * are met:
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate * the documentation and/or other materials provided with the
710Sstevel@tonic-gate * distribution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate * software must display the following acknowledgment:
750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate * endorse or promote products derived from this software without
800Sstevel@tonic-gate * prior written permission. For written permission, please contact
810Sstevel@tonic-gate * openssl-core@openssl.org.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate * permission of the OpenSSL Project.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate * acknowledgment:
890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate * ====================================================================
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate #include "cryptlib.h"
1130Sstevel@tonic-gate #include "bn_lcl.h"
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
1160Sstevel@tonic-gate
BN_gcd(BIGNUM * r,const BIGNUM * in_a,const BIGNUM * in_b,BN_CTX * ctx)1170Sstevel@tonic-gate int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate BIGNUM *a,*b,*t;
1200Sstevel@tonic-gate int ret=0;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate bn_check_top(in_a);
1230Sstevel@tonic-gate bn_check_top(in_b);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate BN_CTX_start(ctx);
1260Sstevel@tonic-gate a = BN_CTX_get(ctx);
1270Sstevel@tonic-gate b = BN_CTX_get(ctx);
1280Sstevel@tonic-gate if (a == NULL || b == NULL) goto err;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (BN_copy(a,in_a) == NULL) goto err;
1310Sstevel@tonic-gate if (BN_copy(b,in_b) == NULL) goto err;
1320Sstevel@tonic-gate a->neg = 0;
1330Sstevel@tonic-gate b->neg = 0;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; }
1360Sstevel@tonic-gate t=euclid(a,b);
1370Sstevel@tonic-gate if (t == NULL) goto err;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (BN_copy(r,t) == NULL) goto err;
1400Sstevel@tonic-gate ret=1;
1410Sstevel@tonic-gate err:
1420Sstevel@tonic-gate BN_CTX_end(ctx);
143*2139Sjp161948 bn_check_top(r);
1440Sstevel@tonic-gate return(ret);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
euclid(BIGNUM * a,BIGNUM * b)1470Sstevel@tonic-gate static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate BIGNUM *t;
1500Sstevel@tonic-gate int shifts=0;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate bn_check_top(a);
1530Sstevel@tonic-gate bn_check_top(b);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* 0 <= b <= a */
1560Sstevel@tonic-gate while (!BN_is_zero(b))
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate /* 0 < b <= a */
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (BN_is_odd(a))
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate if (BN_is_odd(b))
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate if (!BN_sub(a,a,b)) goto err;
1650Sstevel@tonic-gate if (!BN_rshift1(a,a)) goto err;
1660Sstevel@tonic-gate if (BN_cmp(a,b) < 0)
1670Sstevel@tonic-gate { t=a; a=b; b=t; }
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate else /* a odd - b even */
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate if (!BN_rshift1(b,b)) goto err;
1720Sstevel@tonic-gate if (BN_cmp(a,b) < 0)
1730Sstevel@tonic-gate { t=a; a=b; b=t; }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate else /* a is even */
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate if (BN_is_odd(b))
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate if (!BN_rshift1(a,a)) goto err;
1810Sstevel@tonic-gate if (BN_cmp(a,b) < 0)
1820Sstevel@tonic-gate { t=a; a=b; b=t; }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate else /* a even - b even */
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate if (!BN_rshift1(a,a)) goto err;
1870Sstevel@tonic-gate if (!BN_rshift1(b,b)) goto err;
1880Sstevel@tonic-gate shifts++;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate /* 0 <= b <= a */
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (shifts)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate if (!BN_lshift(a,a,shifts)) goto err;
1970Sstevel@tonic-gate }
198*2139Sjp161948 bn_check_top(a);
1990Sstevel@tonic-gate return(a);
2000Sstevel@tonic-gate err:
2010Sstevel@tonic-gate return(NULL);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /* solves ax == 1 (mod n) */
BN_mod_inverse(BIGNUM * in,const BIGNUM * a,const BIGNUM * n,BN_CTX * ctx)2060Sstevel@tonic-gate BIGNUM *BN_mod_inverse(BIGNUM *in,
2070Sstevel@tonic-gate const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
2100Sstevel@tonic-gate BIGNUM *ret=NULL;
2110Sstevel@tonic-gate int sign;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate bn_check_top(a);
2140Sstevel@tonic-gate bn_check_top(n);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate BN_CTX_start(ctx);
2170Sstevel@tonic-gate A = BN_CTX_get(ctx);
2180Sstevel@tonic-gate B = BN_CTX_get(ctx);
2190Sstevel@tonic-gate X = BN_CTX_get(ctx);
2200Sstevel@tonic-gate D = BN_CTX_get(ctx);
2210Sstevel@tonic-gate M = BN_CTX_get(ctx);
2220Sstevel@tonic-gate Y = BN_CTX_get(ctx);
2230Sstevel@tonic-gate T = BN_CTX_get(ctx);
2240Sstevel@tonic-gate if (T == NULL) goto err;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if (in == NULL)
2270Sstevel@tonic-gate R=BN_new();
2280Sstevel@tonic-gate else
2290Sstevel@tonic-gate R=in;
2300Sstevel@tonic-gate if (R == NULL) goto err;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate BN_one(X);
2330Sstevel@tonic-gate BN_zero(Y);
2340Sstevel@tonic-gate if (BN_copy(B,a) == NULL) goto err;
2350Sstevel@tonic-gate if (BN_copy(A,n) == NULL) goto err;
2360Sstevel@tonic-gate A->neg = 0;
2370Sstevel@tonic-gate if (B->neg || (BN_ucmp(B, A) >= 0))
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate if (!BN_nnmod(B, B, A, ctx)) goto err;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate sign = -1;
2420Sstevel@tonic-gate /* From B = a mod |n|, A = |n| it follows that
2430Sstevel@tonic-gate *
2440Sstevel@tonic-gate * 0 <= B < A,
2450Sstevel@tonic-gate * -sign*X*a == B (mod |n|),
2460Sstevel@tonic-gate * sign*Y*a == A (mod |n|).
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate /* Binary inversion algorithm; requires odd modulus.
2520Sstevel@tonic-gate * This is faster than the general algorithm if the modulus
2530Sstevel@tonic-gate * is sufficiently small (about 400 .. 500 bits on 32-bit
2540Sstevel@tonic-gate * sytems, but much more on 64-bit systems) */
2550Sstevel@tonic-gate int shift;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate while (!BN_is_zero(B))
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * 0 < B < |n|,
2610Sstevel@tonic-gate * 0 < A <= |n|,
2620Sstevel@tonic-gate * (1) -sign*X*a == B (mod |n|),
2630Sstevel@tonic-gate * (2) sign*Y*a == A (mod |n|)
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /* Now divide B by the maximum possible power of two in the integers,
2670Sstevel@tonic-gate * and divide X by the same value mod |n|.
2680Sstevel@tonic-gate * When we're done, (1) still holds. */
2690Sstevel@tonic-gate shift = 0;
2700Sstevel@tonic-gate while (!BN_is_bit_set(B, shift)) /* note that 0 < B */
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate shift++;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (BN_is_odd(X))
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate if (!BN_uadd(X, X, n)) goto err;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate /* now X is even, so we can easily divide it by two */
2790Sstevel@tonic-gate if (!BN_rshift1(X, X)) goto err;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate if (shift > 0)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate if (!BN_rshift(B, B, shift)) goto err;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /* Same for A and Y. Afterwards, (2) still holds. */
2880Sstevel@tonic-gate shift = 0;
2890Sstevel@tonic-gate while (!BN_is_bit_set(A, shift)) /* note that 0 < A */
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate shift++;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate if (BN_is_odd(Y))
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate if (!BN_uadd(Y, Y, n)) goto err;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate /* now Y is even */
2980Sstevel@tonic-gate if (!BN_rshift1(Y, Y)) goto err;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate if (shift > 0)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate if (!BN_rshift(A, A, shift)) goto err;
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate /* We still have (1) and (2).
3070Sstevel@tonic-gate * Both A and B are odd.
3080Sstevel@tonic-gate * The following computations ensure that
3090Sstevel@tonic-gate *
3100Sstevel@tonic-gate * 0 <= B < |n|,
3110Sstevel@tonic-gate * 0 < A < |n|,
3120Sstevel@tonic-gate * (1) -sign*X*a == B (mod |n|),
3130Sstevel@tonic-gate * (2) sign*Y*a == A (mod |n|),
3140Sstevel@tonic-gate *
3150Sstevel@tonic-gate * and that either A or B is even in the next iteration.
3160Sstevel@tonic-gate */
3170Sstevel@tonic-gate if (BN_ucmp(B, A) >= 0)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate /* -sign*(X + Y)*a == B - A (mod |n|) */
3200Sstevel@tonic-gate if (!BN_uadd(X, X, Y)) goto err;
3210Sstevel@tonic-gate /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
3220Sstevel@tonic-gate * actually makes the algorithm slower */
3230Sstevel@tonic-gate if (!BN_usub(B, B, A)) goto err;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate else
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate /* sign*(X + Y)*a == A - B (mod |n|) */
3280Sstevel@tonic-gate if (!BN_uadd(Y, Y, X)) goto err;
3290Sstevel@tonic-gate /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
3300Sstevel@tonic-gate if (!BN_usub(A, A, B)) goto err;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate else
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate /* general inversion algorithm */
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate while (!BN_is_zero(B))
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate BIGNUM *tmp;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * 0 < B < A,
3440Sstevel@tonic-gate * (*) -sign*X*a == B (mod |n|),
3450Sstevel@tonic-gate * sign*Y*a == A (mod |n|)
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /* (D, M) := (A/B, A%B) ... */
3490Sstevel@tonic-gate if (BN_num_bits(A) == BN_num_bits(B))
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate if (!BN_one(D)) goto err;
3520Sstevel@tonic-gate if (!BN_sub(M,A,B)) goto err;
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate else if (BN_num_bits(A) == BN_num_bits(B) + 1)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate /* A/B is 1, 2, or 3 */
3570Sstevel@tonic-gate if (!BN_lshift1(T,B)) goto err;
3580Sstevel@tonic-gate if (BN_ucmp(A,T) < 0)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate /* A < 2*B, so D=1 */
3610Sstevel@tonic-gate if (!BN_one(D)) goto err;
3620Sstevel@tonic-gate if (!BN_sub(M,A,B)) goto err;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate else
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate /* A >= 2*B, so D=2 or D=3 */
3670Sstevel@tonic-gate if (!BN_sub(M,A,T)) goto err;
3680Sstevel@tonic-gate if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */
3690Sstevel@tonic-gate if (BN_ucmp(A,D) < 0)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate /* A < 3*B, so D=2 */
3720Sstevel@tonic-gate if (!BN_set_word(D,2)) goto err;
3730Sstevel@tonic-gate /* M (= A - 2*B) already has the correct value */
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate else
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate /* only D=3 remains */
3780Sstevel@tonic-gate if (!BN_set_word(D,3)) goto err;
3790Sstevel@tonic-gate /* currently M = A - 2*B, but we need M = A - 3*B */
3800Sstevel@tonic-gate if (!BN_sub(M,M,B)) goto err;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate else
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate if (!BN_div(D,M,A,B,ctx)) goto err;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /* Now
3900Sstevel@tonic-gate * A = D*B + M;
3910Sstevel@tonic-gate * thus we have
3920Sstevel@tonic-gate * (**) sign*Y*a == D*B + M (mod |n|).
3930Sstevel@tonic-gate */
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate tmp=A; /* keep the BIGNUM object, the value does not matter */
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate /* (A, B) := (B, A mod B) ... */
3980Sstevel@tonic-gate A=B;
3990Sstevel@tonic-gate B=M;
4000Sstevel@tonic-gate /* ... so we have 0 <= B < A again */
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /* Since the former M is now B and the former B is now A,
4030Sstevel@tonic-gate * (**) translates into
4040Sstevel@tonic-gate * sign*Y*a == D*A + B (mod |n|),
4050Sstevel@tonic-gate * i.e.
4060Sstevel@tonic-gate * sign*Y*a - D*A == B (mod |n|).
4070Sstevel@tonic-gate * Similarly, (*) translates into
4080Sstevel@tonic-gate * -sign*X*a == A (mod |n|).
4090Sstevel@tonic-gate *
4100Sstevel@tonic-gate * Thus,
4110Sstevel@tonic-gate * sign*Y*a + D*sign*X*a == B (mod |n|),
4120Sstevel@tonic-gate * i.e.
4130Sstevel@tonic-gate * sign*(Y + D*X)*a == B (mod |n|).
4140Sstevel@tonic-gate *
4150Sstevel@tonic-gate * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
4160Sstevel@tonic-gate * -sign*X*a == B (mod |n|),
4170Sstevel@tonic-gate * sign*Y*a == A (mod |n|).
4180Sstevel@tonic-gate * Note that X and Y stay non-negative all the time.
4190Sstevel@tonic-gate */
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate /* most of the time D is very small, so we can optimize tmp := D*X+Y */
4220Sstevel@tonic-gate if (BN_is_one(D))
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate if (!BN_add(tmp,X,Y)) goto err;
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate else
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate if (BN_is_word(D,2))
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate if (!BN_lshift1(tmp,X)) goto err;
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate else if (BN_is_word(D,4))
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate if (!BN_lshift(tmp,X,2)) goto err;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate else if (D->top == 1)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate if (!BN_copy(tmp,X)) goto err;
4390Sstevel@tonic-gate if (!BN_mul_word(tmp,D->d[0])) goto err;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate else
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate if (!BN_mul(tmp,D,X,ctx)) goto err;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate if (!BN_add(tmp,tmp,Y)) goto err;
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate M=Y; /* keep the BIGNUM object, the value does not matter */
4490Sstevel@tonic-gate Y=X;
4500Sstevel@tonic-gate X=tmp;
4510Sstevel@tonic-gate sign = -sign;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * The while loop (Euclid's algorithm) ends when
4570Sstevel@tonic-gate * A == gcd(a,n);
4580Sstevel@tonic-gate * we have
4590Sstevel@tonic-gate * sign*Y*a == A (mod |n|),
4600Sstevel@tonic-gate * where Y is non-negative.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (sign < 0)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate if (!BN_sub(Y,n,Y)) goto err;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate /* Now Y*a == A (mod |n|). */
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (BN_is_one(A))
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate /* Y*a == 1 (mod |n|) */
4730Sstevel@tonic-gate if (!Y->neg && BN_ucmp(Y,n) < 0)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate if (!BN_copy(R,Y)) goto err;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate else
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate if (!BN_nnmod(R,Y,n,ctx)) goto err;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate else
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
4850Sstevel@tonic-gate goto err;
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate ret=R;
4880Sstevel@tonic-gate err:
4890Sstevel@tonic-gate if ((ret == NULL) && (in == NULL)) BN_free(R);
4900Sstevel@tonic-gate BN_CTX_end(ctx);
491*2139Sjp161948 bn_check_top(ret);
4920Sstevel@tonic-gate return(ret);
4930Sstevel@tonic-gate }
494