155714Skris/* crypto/bn/bn_sqr.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include "bn_lcl.h"
6255714Skris
6355714Skris/* r must not be a */
6455714Skris/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
65109998Smarkmint BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
6655714Skris	{
6755714Skris	int max,al;
6859191Skris	int ret = 0;
6955714Skris	BIGNUM *tmp,*rr;
7055714Skris
7155714Skris#ifdef BN_COUNT
72109998Smarkm	fprintf(stderr,"BN_sqr %d * %d\n",a->top,a->top);
7355714Skris#endif
7455714Skris	bn_check_top(a);
7555714Skris
7655714Skris	al=a->top;
7755714Skris	if (al <= 0)
7855714Skris		{
7955714Skris		r->top=0;
80279264Sdelphij		r->neg = 0;
81160814Ssimon		return 1;
8255714Skris		}
8355714Skris
8459191Skris	BN_CTX_start(ctx);
8559191Skris	rr=(a != r) ? r : BN_CTX_get(ctx);
8659191Skris	tmp=BN_CTX_get(ctx);
87160814Ssimon	if (!rr || !tmp) goto err;
8859191Skris
89160814Ssimon	max = 2 * al; /* Non-zero (from above) */
90160814Ssimon	if (bn_wexpand(rr,max) == NULL) goto err;
9155714Skris
9255714Skris	if (al == 4)
9355714Skris		{
9455714Skris#ifndef BN_SQR_COMBA
9555714Skris		BN_ULONG t[8];
9655714Skris		bn_sqr_normal(rr->d,a->d,4,t);
9755714Skris#else
9855714Skris		bn_sqr_comba4(rr->d,a->d);
9955714Skris#endif
10055714Skris		}
10155714Skris	else if (al == 8)
10255714Skris		{
10355714Skris#ifndef BN_SQR_COMBA
10455714Skris		BN_ULONG t[16];
10555714Skris		bn_sqr_normal(rr->d,a->d,8,t);
10655714Skris#else
10755714Skris		bn_sqr_comba8(rr->d,a->d);
10855714Skris#endif
10955714Skris		}
11055714Skris	else
11155714Skris		{
11255714Skris#if defined(BN_RECURSION)
11355714Skris		if (al < BN_SQR_RECURSIVE_SIZE_NORMAL)
11455714Skris			{
11555714Skris			BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
11655714Skris			bn_sqr_normal(rr->d,a->d,al,t);
11755714Skris			}
11855714Skris		else
11955714Skris			{
12055714Skris			int j,k;
12155714Skris
12255714Skris			j=BN_num_bits_word((BN_ULONG)al);
12355714Skris			j=1<<(j-1);
12455714Skris			k=j+j;
12555714Skris			if (al == j)
12655714Skris				{
12759191Skris				if (bn_wexpand(tmp,k*2) == NULL) goto err;
12855714Skris				bn_sqr_recursive(rr->d,a->d,al,tmp->d);
12955714Skris				}
13055714Skris			else
13155714Skris				{
13259191Skris				if (bn_wexpand(tmp,max) == NULL) goto err;
13355714Skris				bn_sqr_normal(rr->d,a->d,al,tmp->d);
13455714Skris				}
13555714Skris			}
13655714Skris#else
13759191Skris		if (bn_wexpand(tmp,max) == NULL) goto err;
13855714Skris		bn_sqr_normal(rr->d,a->d,al,tmp->d);
13955714Skris#endif
14055714Skris		}
14155714Skris
142109998Smarkm	rr->neg=0;
143160814Ssimon	/* If the most-significant half of the top word of 'a' is zero, then
144160814Ssimon	 * the square of 'a' will max-1 words. */
145160814Ssimon	if(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
146160814Ssimon		rr->top = max - 1;
147160814Ssimon	else
148160814Ssimon		rr->top = max;
14955714Skris	if (rr != r) BN_copy(r,rr);
15059191Skris	ret = 1;
15159191Skris err:
152160814Ssimon	bn_check_top(rr);
153160814Ssimon	bn_check_top(tmp);
15459191Skris	BN_CTX_end(ctx);
15559191Skris	return(ret);
15655714Skris	}
15755714Skris
15855714Skris/* tmp must have 2*n words */
159109998Smarkmvoid bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
16055714Skris	{
16155714Skris	int i,j,max;
162109998Smarkm	const BN_ULONG *ap;
163109998Smarkm	BN_ULONG *rp;
16455714Skris
16555714Skris	max=n*2;
16655714Skris	ap=a;
16755714Skris	rp=r;
16855714Skris	rp[0]=rp[max-1]=0;
16955714Skris	rp++;
17055714Skris	j=n;
17155714Skris
17255714Skris	if (--j > 0)
17355714Skris		{
17455714Skris		ap++;
17555714Skris		rp[j]=bn_mul_words(rp,ap,j,ap[-1]);
17655714Skris		rp+=2;
17755714Skris		}
17855714Skris
17955714Skris	for (i=n-2; i>0; i--)
18055714Skris		{
18155714Skris		j--;
18255714Skris		ap++;
18355714Skris		rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]);
18455714Skris		rp+=2;
18555714Skris		}
18655714Skris
18755714Skris	bn_add_words(r,r,r,max);
18855714Skris
18955714Skris	/* There will not be a carry */
19055714Skris
19155714Skris	bn_sqr_words(tmp,a,n);
19255714Skris
19355714Skris	bn_add_words(r,r,tmp,max);
19455714Skris	}
19555714Skris
19655714Skris#ifdef BN_RECURSION
19755714Skris/* r is 2*n words in size,
19868651Skris * a and b are both n words in size.    (There's not actually a 'b' here ...)
19955714Skris * n must be a power of 2.
20055714Skris * We multiply and return the result.
20155714Skris * t must be 2*n words in size
20259191Skris * We calculate
20355714Skris * a[0]*b[0]
20455714Skris * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
20555714Skris * a[1]*b[1]
20655714Skris */
207109998Smarkmvoid bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
20855714Skris	{
20955714Skris	int n=n2/2;
21055714Skris	int zero,c1;
21155714Skris	BN_ULONG ln,lo,*p;
21255714Skris
21355714Skris#ifdef BN_COUNT
214109998Smarkm	fprintf(stderr," bn_sqr_recursive %d * %d\n",n2,n2);
21555714Skris#endif
21655714Skris	if (n2 == 4)
21755714Skris		{
21855714Skris#ifndef BN_SQR_COMBA
21955714Skris		bn_sqr_normal(r,a,4,t);
22055714Skris#else
22155714Skris		bn_sqr_comba4(r,a);
22255714Skris#endif
22355714Skris		return;
22455714Skris		}
22555714Skris	else if (n2 == 8)
22655714Skris		{
22755714Skris#ifndef BN_SQR_COMBA
22855714Skris		bn_sqr_normal(r,a,8,t);
22955714Skris#else
23055714Skris		bn_sqr_comba8(r,a);
23155714Skris#endif
23255714Skris		return;
23355714Skris		}
23455714Skris	if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL)
23555714Skris		{
23655714Skris		bn_sqr_normal(r,a,n2,t);
23755714Skris		return;
23855714Skris		}
23955714Skris	/* r=(a[0]-a[1])*(a[1]-a[0]) */
24055714Skris	c1=bn_cmp_words(a,&(a[n]),n);
24155714Skris	zero=0;
24255714Skris	if (c1 > 0)
24355714Skris		bn_sub_words(t,a,&(a[n]),n);
24455714Skris	else if (c1 < 0)
24555714Skris		bn_sub_words(t,&(a[n]),a,n);
24655714Skris	else
24755714Skris		zero=1;
24855714Skris
24955714Skris	/* The result will always be negative unless it is zero */
25055714Skris	p= &(t[n2*2]);
25155714Skris
25255714Skris	if (!zero)
25355714Skris		bn_sqr_recursive(&(t[n2]),t,n,p);
25455714Skris	else
25589837Skris		memset(&(t[n2]),0,n2*sizeof(BN_ULONG));
25655714Skris	bn_sqr_recursive(r,a,n,p);
25755714Skris	bn_sqr_recursive(&(r[n2]),&(a[n]),n,p);
25855714Skris
25955714Skris	/* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
26055714Skris	 * r[10] holds (a[0]*b[0])
26155714Skris	 * r[32] holds (b[1]*b[1])
26255714Skris	 */
26355714Skris
26455714Skris	c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
26555714Skris
26655714Skris	/* t[32] is negative */
26755714Skris	c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
26855714Skris
26955714Skris	/* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
27055714Skris	 * r[10] holds (a[0]*a[0])
27155714Skris	 * r[32] holds (a[1]*a[1])
27255714Skris	 * c1 holds the carry bits
27355714Skris	 */
27455714Skris	c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
27555714Skris	if (c1)
27655714Skris		{
27755714Skris		p= &(r[n+n2]);
27855714Skris		lo= *p;
27955714Skris		ln=(lo+c1)&BN_MASK2;
28055714Skris		*p=ln;
28155714Skris
28255714Skris		/* The overflow will stop before we over write
28355714Skris		 * words we should not overwrite */
28455714Skris		if (ln < (BN_ULONG)c1)
28555714Skris			{
28655714Skris			do	{
28755714Skris				p++;
28855714Skris				lo= *p;
28955714Skris				ln=(lo+1)&BN_MASK2;
29055714Skris				*p=ln;
29155714Skris				} while (ln == 0);
29255714Skris			}
29355714Skris		}
29455714Skris	}
29555714Skris#endif
296