174664626SKris Kennaway /* crypto/bn/bn_div.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 5874664626SKris Kennaway 5974664626SKris Kennaway #include <stdio.h> 6074664626SKris Kennaway #include <openssl/bn.h> 6174664626SKris Kennaway #include "cryptlib.h" 6274664626SKris Kennaway #include "bn_lcl.h" 6374664626SKris Kennaway 6474664626SKris Kennaway /* The old slow way */ 6574664626SKris Kennaway #if 0 66f579bf8eSKris Kennaway int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, 67f579bf8eSKris Kennaway BN_CTX *ctx) 6874664626SKris Kennaway { 6974664626SKris Kennaway int i,nm,nd; 70f579bf8eSKris Kennaway int ret = 0; 7174664626SKris Kennaway BIGNUM *D; 7274664626SKris Kennaway 7374664626SKris Kennaway bn_check_top(m); 7474664626SKris Kennaway bn_check_top(d); 7574664626SKris Kennaway if (BN_is_zero(d)) 7674664626SKris Kennaway { 7774664626SKris Kennaway BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO); 7874664626SKris Kennaway return(0); 7974664626SKris Kennaway } 8074664626SKris Kennaway 8174664626SKris Kennaway if (BN_ucmp(m,d) < 0) 8274664626SKris Kennaway { 8374664626SKris Kennaway if (rem != NULL) 8474664626SKris Kennaway { if (BN_copy(rem,m) == NULL) return(0); } 8574664626SKris Kennaway if (dv != NULL) BN_zero(dv); 8674664626SKris Kennaway return(1); 8774664626SKris Kennaway } 8874664626SKris Kennaway 89f579bf8eSKris Kennaway BN_CTX_start(ctx); 90f579bf8eSKris Kennaway D = BN_CTX_get(ctx); 91f579bf8eSKris Kennaway if (dv == NULL) dv = BN_CTX_get(ctx); 92f579bf8eSKris Kennaway if (rem == NULL) rem = BN_CTX_get(ctx); 93f579bf8eSKris Kennaway if (D == NULL || dv == NULL || rem == NULL) 94f579bf8eSKris Kennaway goto end; 9574664626SKris Kennaway 9674664626SKris Kennaway nd=BN_num_bits(d); 9774664626SKris Kennaway nm=BN_num_bits(m); 98f579bf8eSKris Kennaway if (BN_copy(D,d) == NULL) goto end; 99f579bf8eSKris Kennaway if (BN_copy(rem,m) == NULL) goto end; 10074664626SKris Kennaway 10174664626SKris Kennaway /* The next 2 are needed so we can do a dv->d[0]|=1 later 10274664626SKris Kennaway * since BN_lshift1 will only work once there is a value :-) */ 10374664626SKris Kennaway BN_zero(dv); 10474664626SKris Kennaway bn_wexpand(dv,1); 10574664626SKris Kennaway dv->top=1; 10674664626SKris Kennaway 107f579bf8eSKris Kennaway if (!BN_lshift(D,D,nm-nd)) goto end; 10874664626SKris Kennaway for (i=nm-nd; i>=0; i--) 10974664626SKris Kennaway { 110f579bf8eSKris Kennaway if (!BN_lshift1(dv,dv)) goto end; 11174664626SKris Kennaway if (BN_ucmp(rem,D) >= 0) 11274664626SKris Kennaway { 11374664626SKris Kennaway dv->d[0]|=1; 114f579bf8eSKris Kennaway if (!BN_usub(rem,rem,D)) goto end; 11574664626SKris Kennaway } 11674664626SKris Kennaway /* CAN IMPROVE (and have now :=) */ 117f579bf8eSKris Kennaway if (!BN_rshift1(D,D)) goto end; 11874664626SKris Kennaway } 11974664626SKris Kennaway rem->neg=BN_is_zero(rem)?0:m->neg; 12074664626SKris Kennaway dv->neg=m->neg^d->neg; 121f579bf8eSKris Kennaway ret = 1; 122f579bf8eSKris Kennaway end: 123f579bf8eSKris Kennaway BN_CTX_end(ctx); 124f579bf8eSKris Kennaway return(ret); 12574664626SKris Kennaway } 12674664626SKris Kennaway 12774664626SKris Kennaway #else 12874664626SKris Kennaway 129f579bf8eSKris Kennaway #if !defined(NO_ASM) && !defined(NO_INLINE_ASM) && !defined(PEDANTIC) && !defined(BN_DIV3W) 130f579bf8eSKris Kennaway # if defined(__GNUC__) && __GNUC__>=2 131a21b1b38SKris Kennaway # if defined(__i386) || defined (__i386__) 132f579bf8eSKris Kennaway /* 133f579bf8eSKris Kennaway * There were two reasons for implementing this template: 134f579bf8eSKris Kennaway * - GNU C generates a call to a function (__udivdi3 to be exact) 135f579bf8eSKris Kennaway * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to 136f579bf8eSKris Kennaway * understand why...); 137f579bf8eSKris Kennaway * - divl doesn't only calculate quotient, but also leaves 138f579bf8eSKris Kennaway * remainder in %edx which we can definitely use here:-) 139f579bf8eSKris Kennaway * 140f579bf8eSKris Kennaway * <appro@fy.chalmers.se> 141f579bf8eSKris Kennaway */ 142f579bf8eSKris Kennaway # define bn_div_words(n0,n1,d0) \ 143f579bf8eSKris Kennaway ({ asm volatile ( \ 144f579bf8eSKris Kennaway "divl %4" \ 145f579bf8eSKris Kennaway : "=a"(q), "=d"(rem) \ 146f579bf8eSKris Kennaway : "a"(n1), "d"(n0), "g"(d0) \ 147f579bf8eSKris Kennaway : "cc"); \ 148f579bf8eSKris Kennaway q; \ 149f579bf8eSKris Kennaway }) 150f579bf8eSKris Kennaway # define REMAINDER_IS_ALREADY_CALCULATED 151f579bf8eSKris Kennaway # endif /* __<cpu> */ 152f579bf8eSKris Kennaway # endif /* __GNUC__ */ 153f579bf8eSKris Kennaway #endif /* NO_ASM */ 154f579bf8eSKris Kennaway 15574664626SKris Kennaway int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, 15674664626SKris Kennaway BN_CTX *ctx) 15774664626SKris Kennaway { 15874664626SKris Kennaway int norm_shift,i,j,loop; 15974664626SKris Kennaway BIGNUM *tmp,wnum,*snum,*sdiv,*res; 16074664626SKris Kennaway BN_ULONG *resp,*wnump; 16174664626SKris Kennaway BN_ULONG d0,d1; 16274664626SKris Kennaway int num_n,div_n; 16374664626SKris Kennaway 16474664626SKris Kennaway bn_check_top(num); 16574664626SKris Kennaway bn_check_top(divisor); 16674664626SKris Kennaway 16774664626SKris Kennaway if (BN_is_zero(divisor)) 16874664626SKris Kennaway { 16974664626SKris Kennaway BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO); 17074664626SKris Kennaway return(0); 17174664626SKris Kennaway } 17274664626SKris Kennaway 17374664626SKris Kennaway if (BN_ucmp(num,divisor) < 0) 17474664626SKris Kennaway { 17574664626SKris Kennaway if (rm != NULL) 17674664626SKris Kennaway { if (BN_copy(rm,num) == NULL) return(0); } 17774664626SKris Kennaway if (dv != NULL) BN_zero(dv); 17874664626SKris Kennaway return(1); 17974664626SKris Kennaway } 18074664626SKris Kennaway 181f579bf8eSKris Kennaway BN_CTX_start(ctx); 182f579bf8eSKris Kennaway tmp=BN_CTX_get(ctx); 183f579bf8eSKris Kennaway snum=BN_CTX_get(ctx); 184f579bf8eSKris Kennaway sdiv=BN_CTX_get(ctx); 18574664626SKris Kennaway if (dv == NULL) 186f579bf8eSKris Kennaway res=BN_CTX_get(ctx); 18774664626SKris Kennaway else res=dv; 188de7cdddaSKris Kennaway if (sdiv==NULL || res == NULL) goto err; 189de7cdddaSKris Kennaway tmp->neg=0; 19074664626SKris Kennaway 19174664626SKris Kennaway /* First we normalise the numbers */ 19274664626SKris Kennaway norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); 19374664626SKris Kennaway BN_lshift(sdiv,divisor,norm_shift); 19474664626SKris Kennaway sdiv->neg=0; 19574664626SKris Kennaway norm_shift+=BN_BITS2; 19674664626SKris Kennaway BN_lshift(snum,num,norm_shift); 19774664626SKris Kennaway snum->neg=0; 19874664626SKris Kennaway div_n=sdiv->top; 19974664626SKris Kennaway num_n=snum->top; 20074664626SKris Kennaway loop=num_n-div_n; 20174664626SKris Kennaway 20274664626SKris Kennaway /* Lets setup a 'window' into snum 20374664626SKris Kennaway * This is the part that corresponds to the current 20474664626SKris Kennaway * 'area' being divided */ 20574664626SKris Kennaway BN_init(&wnum); 20674664626SKris Kennaway wnum.d= &(snum->d[loop]); 20774664626SKris Kennaway wnum.top= div_n; 208ddd58736SKris Kennaway wnum.dmax= snum->dmax+1; /* a bit of a lie */ 20974664626SKris Kennaway 21074664626SKris Kennaway /* Get the top 2 words of sdiv */ 21174664626SKris Kennaway /* i=sdiv->top; */ 21274664626SKris Kennaway d0=sdiv->d[div_n-1]; 21374664626SKris Kennaway d1=(div_n == 1)?0:sdiv->d[div_n-2]; 21474664626SKris Kennaway 21574664626SKris Kennaway /* pointer to the 'top' of snum */ 21674664626SKris Kennaway wnump= &(snum->d[num_n-1]); 21774664626SKris Kennaway 21874664626SKris Kennaway /* Setup to 'res' */ 21974664626SKris Kennaway res->neg= (num->neg^divisor->neg); 22074664626SKris Kennaway if (!bn_wexpand(res,(loop+1))) goto err; 22174664626SKris Kennaway res->top=loop; 22274664626SKris Kennaway resp= &(res->d[loop-1]); 22374664626SKris Kennaway 22474664626SKris Kennaway /* space for temp */ 22574664626SKris Kennaway if (!bn_wexpand(tmp,(div_n+1))) goto err; 22674664626SKris Kennaway 22774664626SKris Kennaway if (BN_ucmp(&wnum,sdiv) >= 0) 22874664626SKris Kennaway { 22974664626SKris Kennaway if (!BN_usub(&wnum,&wnum,sdiv)) goto err; 23074664626SKris Kennaway *resp=1; 23174664626SKris Kennaway res->d[res->top-1]=1; 23274664626SKris Kennaway } 23374664626SKris Kennaway else 23474664626SKris Kennaway res->top--; 23574664626SKris Kennaway resp--; 23674664626SKris Kennaway 23774664626SKris Kennaway for (i=0; i<loop-1; i++) 23874664626SKris Kennaway { 23974664626SKris Kennaway BN_ULONG q,l0; 240de7cdddaSKris Kennaway #if defined(BN_DIV3W) && !defined(NO_ASM) 2415740a5e3SKris Kennaway BN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG); 242f579bf8eSKris Kennaway q=bn_div_3_words(wnump,d1,d0); 24374664626SKris Kennaway #else 24474664626SKris Kennaway BN_ULONG n0,n1,rem=0; 24574664626SKris Kennaway 24674664626SKris Kennaway n0=wnump[0]; 24774664626SKris Kennaway n1=wnump[-1]; 24874664626SKris Kennaway if (n0 == d0) 24974664626SKris Kennaway q=BN_MASK2; 250f579bf8eSKris Kennaway else /* n0 < d0 */ 25174664626SKris Kennaway { 25274664626SKris Kennaway #ifdef BN_LLONG 25374664626SKris Kennaway BN_ULLONG t2; 25474664626SKris Kennaway 255f579bf8eSKris Kennaway #if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words) 256f579bf8eSKris Kennaway q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0); 257f579bf8eSKris Kennaway #else 258f579bf8eSKris Kennaway q=bn_div_words(n0,n1,d0); 259f579bf8eSKris Kennaway #endif 260f579bf8eSKris Kennaway 261f579bf8eSKris Kennaway #ifndef REMAINDER_IS_ALREADY_CALCULATED 26274664626SKris Kennaway /* 26374664626SKris Kennaway * rem doesn't have to be BN_ULLONG. The least we 26474664626SKris Kennaway * know it's less that d0, isn't it? 26574664626SKris Kennaway */ 26674664626SKris Kennaway rem=(n1-q*d0)&BN_MASK2; 26774664626SKris Kennaway #endif 26874664626SKris Kennaway t2=(BN_ULLONG)d1*q; 26974664626SKris Kennaway 27074664626SKris Kennaway for (;;) 27174664626SKris Kennaway { 27274664626SKris Kennaway if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2])) 27374664626SKris Kennaway break; 27474664626SKris Kennaway q--; 27574664626SKris Kennaway rem += d0; 27674664626SKris Kennaway if (rem < d0) break; /* don't let rem overflow */ 27774664626SKris Kennaway t2 -= d1; 27874664626SKris Kennaway } 279f579bf8eSKris Kennaway #else /* !BN_LLONG */ 28074664626SKris Kennaway BN_ULONG t2l,t2h,ql,qh; 28174664626SKris Kennaway 282f579bf8eSKris Kennaway q=bn_div_words(n0,n1,d0); 283f579bf8eSKris Kennaway #ifndef REMAINDER_IS_ALREADY_CALCULATED 28474664626SKris Kennaway rem=(n1-q*d0)&BN_MASK2; 28574664626SKris Kennaway #endif 286f579bf8eSKris Kennaway 287f579bf8eSKris Kennaway #ifdef BN_UMULT_HIGH 288f579bf8eSKris Kennaway t2l = d1 * q; 289f579bf8eSKris Kennaway t2h = BN_UMULT_HIGH(d1,q); 290f579bf8eSKris Kennaway #else 29174664626SKris Kennaway t2l=LBITS(d1); t2h=HBITS(d1); 29274664626SKris Kennaway ql =LBITS(q); qh =HBITS(q); 29374664626SKris Kennaway mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ 294f579bf8eSKris Kennaway #endif 29574664626SKris Kennaway 29674664626SKris Kennaway for (;;) 29774664626SKris Kennaway { 29874664626SKris Kennaway if ((t2h < rem) || 29974664626SKris Kennaway ((t2h == rem) && (t2l <= wnump[-2]))) 30074664626SKris Kennaway break; 30174664626SKris Kennaway q--; 30274664626SKris Kennaway rem += d0; 30374664626SKris Kennaway if (rem < d0) break; /* don't let rem overflow */ 30474664626SKris Kennaway if (t2l < d1) t2h--; t2l -= d1; 30574664626SKris Kennaway } 306f579bf8eSKris Kennaway #endif /* !BN_LLONG */ 30774664626SKris Kennaway } 30874664626SKris Kennaway #endif /* !BN_DIV3W */ 309f579bf8eSKris Kennaway 31074664626SKris Kennaway l0=bn_mul_words(tmp->d,sdiv->d,div_n,q); 311f579bf8eSKris Kennaway wnum.d--; wnum.top++; 31274664626SKris Kennaway tmp->d[div_n]=l0; 31374664626SKris Kennaway for (j=div_n+1; j>0; j--) 31474664626SKris Kennaway if (tmp->d[j-1]) break; 31574664626SKris Kennaway tmp->top=j; 31674664626SKris Kennaway 31774664626SKris Kennaway j=wnum.top; 31874664626SKris Kennaway BN_sub(&wnum,&wnum,tmp); 31974664626SKris Kennaway 32074664626SKris Kennaway snum->top=snum->top+wnum.top-j; 32174664626SKris Kennaway 32274664626SKris Kennaway if (wnum.neg) 32374664626SKris Kennaway { 32474664626SKris Kennaway q--; 32574664626SKris Kennaway j=wnum.top; 32674664626SKris Kennaway BN_add(&wnum,&wnum,sdiv); 32774664626SKris Kennaway snum->top+=wnum.top-j; 32874664626SKris Kennaway } 32974664626SKris Kennaway *(resp--)=q; 33074664626SKris Kennaway wnump--; 33174664626SKris Kennaway } 33274664626SKris Kennaway if (rm != NULL) 33374664626SKris Kennaway { 33474664626SKris Kennaway BN_rshift(rm,snum,norm_shift); 33574664626SKris Kennaway rm->neg=num->neg; 33674664626SKris Kennaway } 337f579bf8eSKris Kennaway BN_CTX_end(ctx); 33874664626SKris Kennaway return(1); 33974664626SKris Kennaway err: 340f579bf8eSKris Kennaway BN_CTX_end(ctx); 34174664626SKris Kennaway return(0); 34274664626SKris Kennaway } 34374664626SKris Kennaway 34474664626SKris Kennaway #endif 34574664626SKris Kennaway 34674664626SKris Kennaway /* rem != m */ 34774664626SKris Kennaway int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) 34874664626SKris Kennaway { 34974664626SKris Kennaway #if 0 /* The old slow way */ 35074664626SKris Kennaway int i,nm,nd; 35174664626SKris Kennaway BIGNUM *dv; 35274664626SKris Kennaway 35374664626SKris Kennaway if (BN_ucmp(m,d) < 0) 35474664626SKris Kennaway return((BN_copy(rem,m) == NULL)?0:1); 35574664626SKris Kennaway 356f579bf8eSKris Kennaway BN_CTX_start(ctx); 357f579bf8eSKris Kennaway dv=BN_CTX_get(ctx); 35874664626SKris Kennaway 359f579bf8eSKris Kennaway if (!BN_copy(rem,m)) goto err; 36074664626SKris Kennaway 36174664626SKris Kennaway nm=BN_num_bits(rem); 36274664626SKris Kennaway nd=BN_num_bits(d); 363f579bf8eSKris Kennaway if (!BN_lshift(dv,d,nm-nd)) goto err; 36474664626SKris Kennaway for (i=nm-nd; i>=0; i--) 36574664626SKris Kennaway { 36674664626SKris Kennaway if (BN_cmp(rem,dv) >= 0) 36774664626SKris Kennaway { 368f579bf8eSKris Kennaway if (!BN_sub(rem,rem,dv)) goto err; 36974664626SKris Kennaway } 370f579bf8eSKris Kennaway if (!BN_rshift1(dv,dv)) goto err; 37174664626SKris Kennaway } 372f579bf8eSKris Kennaway BN_CTX_end(ctx); 37374664626SKris Kennaway return(1); 374f579bf8eSKris Kennaway err: 375f579bf8eSKris Kennaway BN_CTX_end(ctx); 376f579bf8eSKris Kennaway return(0); 37774664626SKris Kennaway #else 37874664626SKris Kennaway return(BN_div(NULL,rem,m,d,ctx)); 37974664626SKris Kennaway #endif 38074664626SKris Kennaway } 38174664626SKris Kennaway 382