sl@0: /* crypto/bn/bn_sqr.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 "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: /* r must not be a */ sl@0: /* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */ sl@0: EXPORT_C int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) sl@0: { sl@0: int max,al; sl@0: int ret = 0; sl@0: BIGNUM *tmp,*rr; sl@0: sl@0: #ifdef BN_COUNT sl@0: fprintf(stderr,"BN_sqr %d * %d\n",a->top,a->top); sl@0: #endif sl@0: bn_check_top(a); sl@0: sl@0: al=a->top; sl@0: if (al <= 0) sl@0: { sl@0: r->top=0; sl@0: return 1; sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: rr=(a != r) ? r : BN_CTX_get(ctx); sl@0: tmp=BN_CTX_get(ctx); sl@0: if (!rr || !tmp) goto err; sl@0: sl@0: max = 2 * al; /* Non-zero (from above) */ sl@0: if (bn_wexpand(rr,max) == NULL) goto err; sl@0: sl@0: if (al == 4) sl@0: { sl@0: #ifndef BN_SQR_COMBA sl@0: BN_ULONG t[8]; sl@0: bn_sqr_normal(rr->d,a->d,4,t); sl@0: #else sl@0: bn_sqr_comba4(rr->d,a->d); sl@0: #endif sl@0: } sl@0: else if (al == 8) sl@0: { sl@0: #ifndef BN_SQR_COMBA sl@0: BN_ULONG t[16]; sl@0: bn_sqr_normal(rr->d,a->d,8,t); sl@0: #else sl@0: bn_sqr_comba8(rr->d,a->d); sl@0: #endif sl@0: } sl@0: else sl@0: { sl@0: #if defined(BN_RECURSION) sl@0: if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) sl@0: { sl@0: BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2]; sl@0: bn_sqr_normal(rr->d,a->d,al,t); sl@0: } sl@0: else sl@0: { sl@0: int j,k; sl@0: sl@0: j=BN_num_bits_word((BN_ULONG)al); sl@0: j=1<<(j-1); sl@0: k=j+j; sl@0: if (al == j) sl@0: { sl@0: if (bn_wexpand(tmp,k*2) == NULL) goto err; sl@0: bn_sqr_recursive(rr->d,a->d,al,tmp->d); sl@0: } sl@0: else sl@0: { sl@0: if (bn_wexpand(tmp,max) == NULL) goto err; sl@0: bn_sqr_normal(rr->d,a->d,al,tmp->d); sl@0: } sl@0: } sl@0: #else sl@0: if (bn_wexpand(tmp,max) == NULL) goto err; sl@0: bn_sqr_normal(rr->d,a->d,al,tmp->d); sl@0: #endif sl@0: } sl@0: sl@0: rr->neg=0; sl@0: /* If the most-significant half of the top word of 'a' is zero, then sl@0: * the square of 'a' will max-1 words. */ sl@0: if(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) sl@0: rr->top = max - 1; sl@0: else sl@0: rr->top = max; sl@0: if (rr != r) BN_copy(r,rr); sl@0: ret = 1; sl@0: err: sl@0: bn_check_top(rr); sl@0: bn_check_top(tmp); sl@0: BN_CTX_end(ctx); sl@0: return(ret); sl@0: } sl@0: sl@0: /* tmp must have 2*n words */ sl@0: EXPORT_C void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) sl@0: { sl@0: int i,j,max; sl@0: const BN_ULONG *ap; sl@0: BN_ULONG *rp; sl@0: sl@0: max=n*2; sl@0: ap=a; sl@0: rp=r; sl@0: rp[0]=rp[max-1]=0; sl@0: rp++; sl@0: j=n; sl@0: sl@0: if (--j > 0) sl@0: { sl@0: ap++; sl@0: rp[j]=bn_mul_words(rp,ap,j,ap[-1]); sl@0: rp+=2; sl@0: } sl@0: sl@0: for (i=n-2; i>0; i--) sl@0: { sl@0: j--; sl@0: ap++; sl@0: rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]); sl@0: rp+=2; sl@0: } sl@0: sl@0: bn_add_words(r,r,r,max); sl@0: sl@0: /* There will not be a carry */ sl@0: sl@0: bn_sqr_words(tmp,a,n); sl@0: sl@0: bn_add_words(r,r,tmp,max); sl@0: } sl@0: sl@0: #ifdef BN_RECURSION sl@0: /* r is 2*n words in size, sl@0: * a and b are both n words in size. (There's not actually a 'b' here ...) sl@0: * n must be a power of 2. sl@0: * We multiply and return the result. sl@0: * t must be 2*n words in size sl@0: * We calculate sl@0: * a[0]*b[0] sl@0: * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0]) sl@0: * a[1]*b[1] sl@0: */ sl@0: EXPORT_C void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) sl@0: { sl@0: int n=n2/2; sl@0: int zero,c1; sl@0: BN_ULONG ln,lo,*p; sl@0: sl@0: #ifdef BN_COUNT sl@0: fprintf(stderr," bn_sqr_recursive %d * %d\n",n2,n2); sl@0: #endif sl@0: if (n2 == 4) sl@0: { sl@0: #ifndef BN_SQR_COMBA sl@0: bn_sqr_normal(r,a,4,t); sl@0: #else sl@0: bn_sqr_comba4(r,a); sl@0: #endif sl@0: return; sl@0: } sl@0: else if (n2 == 8) sl@0: { sl@0: #ifndef BN_SQR_COMBA sl@0: bn_sqr_normal(r,a,8,t); sl@0: #else sl@0: bn_sqr_comba8(r,a); sl@0: #endif sl@0: return; sl@0: } sl@0: if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) sl@0: { sl@0: bn_sqr_normal(r,a,n2,t); sl@0: return; sl@0: } sl@0: /* r=(a[0]-a[1])*(a[1]-a[0]) */ sl@0: c1=bn_cmp_words(a,&(a[n]),n); sl@0: zero=0; sl@0: if (c1 > 0) sl@0: bn_sub_words(t,a,&(a[n]),n); sl@0: else if (c1 < 0) sl@0: bn_sub_words(t,&(a[n]),a,n); sl@0: else sl@0: zero=1; sl@0: sl@0: /* The result will always be negative unless it is zero */ sl@0: p= &(t[n2*2]); sl@0: sl@0: if (!zero) sl@0: bn_sqr_recursive(&(t[n2]),t,n,p); sl@0: else sl@0: memset(&(t[n2]),0,n2*sizeof(BN_ULONG)); sl@0: bn_sqr_recursive(r,a,n,p); sl@0: bn_sqr_recursive(&(r[n2]),&(a[n]),n,p); sl@0: sl@0: /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero sl@0: * r[10] holds (a[0]*b[0]) sl@0: * r[32] holds (b[1]*b[1]) sl@0: */ sl@0: sl@0: c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); sl@0: sl@0: /* t[32] is negative */ sl@0: c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); sl@0: sl@0: /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1]) sl@0: * r[10] holds (a[0]*a[0]) sl@0: * r[32] holds (a[1]*a[1]) sl@0: * c1 holds the carry bits sl@0: */ sl@0: c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); sl@0: if (c1) sl@0: { sl@0: p= &(r[n+n2]); sl@0: lo= *p; sl@0: ln=(lo+c1)&BN_MASK2; sl@0: *p=ln; sl@0: sl@0: /* The overflow will stop before we over write sl@0: * words we should not overwrite */ sl@0: if (ln < (BN_ULONG)c1) sl@0: { sl@0: do { sl@0: p++; sl@0: lo= *p; sl@0: ln=(lo+1)&BN_MASK2; sl@0: *p=ln; sl@0: } while (ln == 0); sl@0: } sl@0: } sl@0: } sl@0: #endif