sl@0: /* crypto/bn/bn_div.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: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: sl@0: /* The old slow way */ sl@0: #if 0 sl@0: EXPORT_C int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, sl@0: BN_CTX *ctx) sl@0: { sl@0: int i,nm,nd; sl@0: int ret = 0; sl@0: BIGNUM *D; sl@0: sl@0: bn_check_top(m); sl@0: bn_check_top(d); sl@0: if (BN_is_zero(d)) sl@0: { sl@0: BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO); sl@0: return(0); sl@0: } sl@0: sl@0: if (BN_ucmp(m,d) < 0) sl@0: { sl@0: if (rem != NULL) sl@0: { if (BN_copy(rem,m) == NULL) return(0); } sl@0: if (dv != NULL) BN_zero(dv); sl@0: return(1); sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: D = BN_CTX_get(ctx); sl@0: if (dv == NULL) dv = BN_CTX_get(ctx); sl@0: if (rem == NULL) rem = BN_CTX_get(ctx); sl@0: if (D == NULL || dv == NULL || rem == NULL) sl@0: goto end; sl@0: sl@0: nd=BN_num_bits(d); sl@0: nm=BN_num_bits(m); sl@0: if (BN_copy(D,d) == NULL) goto end; sl@0: if (BN_copy(rem,m) == NULL) goto end; sl@0: sl@0: /* The next 2 are needed so we can do a dv->d[0]|=1 later sl@0: * since BN_lshift1 will only work once there is a value :-) */ sl@0: BN_zero(dv); sl@0: bn_wexpand(dv,1); sl@0: dv->top=1; sl@0: sl@0: if (!BN_lshift(D,D,nm-nd)) goto end; sl@0: for (i=nm-nd; i>=0; i--) sl@0: { sl@0: if (!BN_lshift1(dv,dv)) goto end; sl@0: if (BN_ucmp(rem,D) >= 0) sl@0: { sl@0: dv->d[0]|=1; sl@0: if (!BN_usub(rem,rem,D)) goto end; sl@0: } sl@0: /* CAN IMPROVE (and have now :=) */ sl@0: if (!BN_rshift1(D,D)) goto end; sl@0: } sl@0: rem->neg=BN_is_zero(rem)?0:m->neg; sl@0: dv->neg=m->neg^d->neg; sl@0: ret = 1; sl@0: end: sl@0: BN_CTX_end(ctx); sl@0: return(ret); sl@0: } sl@0: sl@0: #else sl@0: sl@0: #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \ sl@0: && !defined(PEDANTIC) && !defined(BN_DIV3W) sl@0: # if defined(__GNUC__) && __GNUC__>=2 sl@0: # if defined(__i386) || defined (__i386__) sl@0: /* sl@0: * There were two reasons for implementing this template: sl@0: * - GNU C generates a call to a function (__udivdi3 to be exact) sl@0: * in reply to ((((BN_ULLONG)n0)< sl@0: */ sl@0: # define bn_div_words(n0,n1,d0) \ sl@0: ({ asm volatile ( \ sl@0: "divl %4" \ sl@0: : "=a"(q), "=d"(rem) \ sl@0: : "a"(n1), "d"(n0), "g"(d0) \ sl@0: : "cc"); \ sl@0: q; \ sl@0: }) sl@0: # define REMAINDER_IS_ALREADY_CALCULATED sl@0: # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) sl@0: /* sl@0: * Same story here, but it's 128-bit by 64-bit division. Wow! sl@0: * sl@0: */ sl@0: # define bn_div_words(n0,n1,d0) \ sl@0: ({ asm volatile ( \ sl@0: "divq %4" \ sl@0: : "=a"(q), "=d"(rem) \ sl@0: : "a"(n1), "d"(n0), "g"(d0) \ sl@0: : "cc"); \ sl@0: q; \ sl@0: }) sl@0: # define REMAINDER_IS_ALREADY_CALCULATED sl@0: # endif /* __ */ sl@0: # endif /* __GNUC__ */ sl@0: #endif /* OPENSSL_NO_ASM */ sl@0: sl@0: sl@0: /* BN_div[_no_branch] computes dv := num / divisor, rounding towards sl@0: * zero, and sets up rm such that dv*divisor + rm = num holds. sl@0: * Thus: sl@0: * dv->neg == num->neg ^ divisor->neg (unless the result is zero) sl@0: * rm->neg == num->neg (unless the remainder is zero) sl@0: * If 'dv' or 'rm' is NULL, the respective value is not returned. sl@0: */ sl@0: static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, sl@0: const BIGNUM *divisor, BN_CTX *ctx); sl@0: EXPORT_C int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, sl@0: BN_CTX *ctx) sl@0: { sl@0: int norm_shift,i,loop; sl@0: BIGNUM *tmp,wnum,*snum,*sdiv,*res; sl@0: BN_ULONG *resp,*wnump; sl@0: BN_ULONG d0,d1; sl@0: int num_n,div_n; sl@0: if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) sl@0: { sl@0: return BN_div_no_branch(dv, rm, num, divisor, ctx); sl@0: } sl@0: sl@0: bn_check_top(dv); sl@0: bn_check_top(rm); sl@0: bn_check_top(num); sl@0: bn_check_top(divisor); sl@0: sl@0: if (BN_is_zero(divisor)) sl@0: { sl@0: BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO); sl@0: return(0); sl@0: } sl@0: sl@0: if (BN_ucmp(num,divisor) < 0) sl@0: { sl@0: if (rm != NULL) sl@0: { if (BN_copy(rm,num) == NULL) return(0); } sl@0: if (dv != NULL) BN_zero(dv); sl@0: return(1); sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: tmp=BN_CTX_get(ctx); sl@0: snum=BN_CTX_get(ctx); sl@0: sdiv=BN_CTX_get(ctx); sl@0: if (dv == NULL) sl@0: res=BN_CTX_get(ctx); sl@0: else res=dv; sl@0: if (sdiv == NULL || res == NULL) goto err; sl@0: sl@0: /* First we normalise the numbers */ sl@0: norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); sl@0: if (!(BN_lshift(sdiv,divisor,norm_shift))) goto err; sl@0: sdiv->neg=0; sl@0: norm_shift+=BN_BITS2; sl@0: if (!(BN_lshift(snum,num,norm_shift))) goto err; sl@0: snum->neg=0; sl@0: div_n=sdiv->top; sl@0: num_n=snum->top; sl@0: loop=num_n-div_n; sl@0: /* Lets setup a 'window' into snum sl@0: * This is the part that corresponds to the current sl@0: * 'area' being divided */ sl@0: wnum.neg = 0; sl@0: wnum.d = &(snum->d[loop]); sl@0: wnum.top = div_n; sl@0: /* only needed when BN_ucmp messes up the values between top and max */ sl@0: wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */ sl@0: sl@0: /* Get the top 2 words of sdiv */ sl@0: /* div_n=sdiv->top; */ sl@0: d0=sdiv->d[div_n-1]; sl@0: d1=(div_n == 1)?0:sdiv->d[div_n-2]; sl@0: sl@0: /* pointer to the 'top' of snum */ sl@0: wnump= &(snum->d[num_n-1]); sl@0: sl@0: /* Setup to 'res' */ sl@0: res->neg= (num->neg^divisor->neg); sl@0: if (!bn_wexpand(res,(loop+1))) goto err; sl@0: res->top=loop; sl@0: resp= &(res->d[loop-1]); sl@0: sl@0: /* space for temp */ sl@0: if (!bn_wexpand(tmp,(div_n+1))) goto err; sl@0: sl@0: if (BN_ucmp(&wnum,sdiv) >= 0) sl@0: { sl@0: /* If BN_DEBUG_RAND is defined BN_ucmp changes (via sl@0: * bn_pollute) the const bignum arguments => sl@0: * clean the values between top and max again */ sl@0: bn_clear_top2max(&wnum); sl@0: bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n); sl@0: *resp=1; sl@0: } sl@0: else sl@0: res->top--; sl@0: /* if res->top == 0 then clear the neg value otherwise decrease sl@0: * the resp pointer */ sl@0: if (res->top == 0) sl@0: res->neg = 0; sl@0: else sl@0: resp--; sl@0: sl@0: for (i=0; i 0x%08X\n", sl@0: n0, n1, d0, q); sl@0: #endif sl@0: #endif sl@0: sl@0: #ifndef REMAINDER_IS_ALREADY_CALCULATED sl@0: /* sl@0: * rem doesn't have to be BN_ULLONG. The least we sl@0: * know it's less that d0, isn't it? sl@0: */ sl@0: rem=(n1-q*d0)&BN_MASK2; sl@0: #endif sl@0: t2=(BN_ULLONG)d1*q; sl@0: sl@0: for (;;) sl@0: { sl@0: if (t2 <= ((((BN_ULLONG)rem)< 0x%08X\n", sl@0: n0, n1, d0, q); sl@0: #endif sl@0: #ifndef REMAINDER_IS_ALREADY_CALCULATED sl@0: rem=(n1-q*d0)&BN_MASK2; sl@0: #endif sl@0: sl@0: #if defined(BN_UMULT_LOHI) sl@0: BN_UMULT_LOHI(t2l,t2h,d1,q); sl@0: #elif defined(BN_UMULT_HIGH) sl@0: t2l = d1 * q; sl@0: t2h = BN_UMULT_HIGH(d1,q); sl@0: #else sl@0: t2l=LBITS(d1); t2h=HBITS(d1); sl@0: ql =LBITS(q); qh =HBITS(q); sl@0: mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ sl@0: #endif sl@0: sl@0: for (;;) sl@0: { sl@0: if ((t2h < rem) || sl@0: ((t2h == rem) && (t2l <= wnump[-2]))) sl@0: break; sl@0: q--; sl@0: rem += d0; sl@0: if (rem < d0) break; /* don't let rem overflow */ sl@0: if (t2l < d1) t2h--; t2l -= d1; sl@0: } sl@0: #endif /* !BN_LLONG */ sl@0: } sl@0: #endif /* !BN_DIV3W */ sl@0: sl@0: l0=bn_mul_words(tmp->d,sdiv->d,div_n,q); sl@0: tmp->d[div_n]=l0; sl@0: wnum.d--; sl@0: /* ingore top values of the bignums just sub the two sl@0: * BN_ULONG arrays with bn_sub_words */ sl@0: if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1)) sl@0: { sl@0: /* Note: As we have considered only the leading sl@0: * two BN_ULONGs in the calculation of q, sdiv * q sl@0: * might be greater than wnum (but then (q-1) * sdiv sl@0: * is less or equal than wnum) sl@0: */ sl@0: q--; sl@0: if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) sl@0: /* we can't have an overflow here (assuming sl@0: * that q != 0, but if q == 0 then tmp is sl@0: * zero anyway) */ sl@0: (*wnump)++; sl@0: } sl@0: /* store part of the result */ sl@0: *resp = q; sl@0: } sl@0: bn_correct_top(snum); sl@0: if (rm != NULL) sl@0: { sl@0: /* Keep a copy of the neg flag in num because if rm==num sl@0: * BN_rshift() will overwrite it. sl@0: */ sl@0: int neg = num->neg; sl@0: BN_rshift(rm,snum,norm_shift); sl@0: if (!BN_is_zero(rm)) sl@0: rm->neg = neg; sl@0: bn_check_top(rm); sl@0: } sl@0: BN_CTX_end(ctx); sl@0: return(1); sl@0: err: sl@0: bn_check_top(rm); sl@0: BN_CTX_end(ctx); sl@0: return(0); sl@0: } sl@0: sl@0: /* BN_div_no_branch is a special version of BN_div. It does not contain sl@0: * branches that may leak sensitive information. sl@0: */ sl@0: static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, sl@0: const BIGNUM *divisor, BN_CTX *ctx) sl@0: { sl@0: int norm_shift,i,loop; sl@0: BIGNUM *tmp,wnum,*snum,*sdiv,*res; sl@0: BN_ULONG *resp,*wnump; sl@0: BN_ULONG d0,d1; sl@0: int num_n,div_n; sl@0: sl@0: bn_check_top(dv); sl@0: bn_check_top(rm); sl@0: bn_check_top(num); sl@0: bn_check_top(divisor); sl@0: sl@0: if (BN_is_zero(divisor)) sl@0: { sl@0: BNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO); sl@0: return(0); sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: tmp=BN_CTX_get(ctx); sl@0: snum=BN_CTX_get(ctx); sl@0: sdiv=BN_CTX_get(ctx); sl@0: if (dv == NULL) sl@0: res=BN_CTX_get(ctx); sl@0: else res=dv; sl@0: if (sdiv == NULL || res == NULL) goto err; sl@0: sl@0: /* First we normalise the numbers */ sl@0: norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); sl@0: if (!(BN_lshift(sdiv,divisor,norm_shift))) goto err; sl@0: sdiv->neg=0; sl@0: norm_shift+=BN_BITS2; sl@0: if (!(BN_lshift(snum,num,norm_shift))) goto err; sl@0: snum->neg=0; sl@0: sl@0: /* Since we don't know whether snum is larger than sdiv, sl@0: * we pad snum with enough zeroes without changing its sl@0: * value. sl@0: */ sl@0: if (snum->top <= sdiv->top+1) sl@0: { sl@0: if (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err; sl@0: for (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0; sl@0: snum->top = sdiv->top + 2; sl@0: } sl@0: else sl@0: { sl@0: if (bn_wexpand(snum, snum->top + 1) == NULL) goto err; sl@0: snum->d[snum->top] = 0; sl@0: snum->top ++; sl@0: } sl@0: sl@0: div_n=sdiv->top; sl@0: num_n=snum->top; sl@0: loop=num_n-div_n; sl@0: /* Lets setup a 'window' into snum sl@0: * This is the part that corresponds to the current sl@0: * 'area' being divided */ sl@0: wnum.neg = 0; sl@0: wnum.d = &(snum->d[loop]); sl@0: wnum.top = div_n; sl@0: /* only needed when BN_ucmp messes up the values between top and max */ sl@0: wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */ sl@0: sl@0: /* Get the top 2 words of sdiv */ sl@0: /* div_n=sdiv->top; */ sl@0: d0=sdiv->d[div_n-1]; sl@0: d1=(div_n == 1)?0:sdiv->d[div_n-2]; sl@0: sl@0: /* pointer to the 'top' of snum */ sl@0: wnump= &(snum->d[num_n-1]); sl@0: sl@0: /* Setup to 'res' */ sl@0: res->neg= (num->neg^divisor->neg); sl@0: if (!bn_wexpand(res,(loop+1))) goto err; sl@0: res->top=loop-1; sl@0: resp= &(res->d[loop-1]); sl@0: sl@0: /* space for temp */ sl@0: if (!bn_wexpand(tmp,(div_n+1))) goto err; sl@0: sl@0: /* if res->top == 0 then clear the neg value otherwise decrease sl@0: * the resp pointer */ sl@0: if (res->top == 0) sl@0: res->neg = 0; sl@0: else sl@0: resp--; sl@0: sl@0: for (i=0; i 0x%08X\n", sl@0: n0, n1, d0, q); sl@0: #endif sl@0: #endif sl@0: sl@0: #ifndef REMAINDER_IS_ALREADY_CALCULATED sl@0: /* sl@0: * rem doesn't have to be BN_ULLONG. The least we sl@0: * know it's less that d0, isn't it? sl@0: */ sl@0: rem=(n1-q*d0)&BN_MASK2; sl@0: #endif sl@0: t2=(BN_ULLONG)d1*q; sl@0: sl@0: for (;;) sl@0: { sl@0: if (t2 <= ((((BN_ULLONG)rem)< 0x%08X\n", sl@0: n0, n1, d0, q); sl@0: #endif sl@0: #ifndef REMAINDER_IS_ALREADY_CALCULATED sl@0: rem=(n1-q*d0)&BN_MASK2; sl@0: #endif sl@0: sl@0: #if defined(BN_UMULT_LOHI) sl@0: BN_UMULT_LOHI(t2l,t2h,d1,q); sl@0: #elif defined(BN_UMULT_HIGH) sl@0: t2l = d1 * q; sl@0: t2h = BN_UMULT_HIGH(d1,q); sl@0: #else sl@0: t2l=LBITS(d1); t2h=HBITS(d1); sl@0: ql =LBITS(q); qh =HBITS(q); sl@0: mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ sl@0: #endif sl@0: sl@0: for (;;) sl@0: { sl@0: if ((t2h < rem) || sl@0: ((t2h == rem) && (t2l <= wnump[-2]))) sl@0: break; sl@0: q--; sl@0: rem += d0; sl@0: if (rem < d0) break; /* don't let rem overflow */ sl@0: if (t2l < d1) t2h--; t2l -= d1; sl@0: } sl@0: #endif /* !BN_LLONG */ sl@0: } sl@0: #endif /* !BN_DIV3W */ sl@0: sl@0: l0=bn_mul_words(tmp->d,sdiv->d,div_n,q); sl@0: tmp->d[div_n]=l0; sl@0: wnum.d--; sl@0: /* ingore top values of the bignums just sub the two sl@0: * BN_ULONG arrays with bn_sub_words */ sl@0: if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1)) sl@0: { sl@0: /* Note: As we have considered only the leading sl@0: * two BN_ULONGs in the calculation of q, sdiv * q sl@0: * might be greater than wnum (but then (q-1) * sdiv sl@0: * is less or equal than wnum) sl@0: */ sl@0: q--; sl@0: if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) sl@0: /* we can't have an overflow here (assuming sl@0: * that q != 0, but if q == 0 then tmp is sl@0: * zero anyway) */ sl@0: (*wnump)++; sl@0: } sl@0: /* store part of the result */ sl@0: *resp = q; sl@0: } sl@0: bn_correct_top(snum); sl@0: if (rm != NULL) sl@0: { sl@0: /* Keep a copy of the neg flag in num because if rm==num sl@0: * BN_rshift() will overwrite it. sl@0: */ sl@0: int neg = num->neg; sl@0: BN_rshift(rm,snum,norm_shift); sl@0: if (!BN_is_zero(rm)) sl@0: rm->neg = neg; sl@0: bn_check_top(rm); sl@0: } sl@0: bn_correct_top(res); sl@0: BN_CTX_end(ctx); sl@0: return(1); sl@0: err: sl@0: bn_check_top(rm); sl@0: BN_CTX_end(ctx); sl@0: return(0); sl@0: } sl@0: sl@0: #endif