os/ossrv/ssl/libcrypto/src/crypto/bn/bn_sqr.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bn/bn_sqr.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include "cryptlib.h"
sl@0
    61
#include "bn_lcl.h"
sl@0
    62
sl@0
    63
/* r must not be a */
sl@0
    64
/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
sl@0
    65
EXPORT_C int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
sl@0
    66
	{
sl@0
    67
	int max,al;
sl@0
    68
	int ret = 0;
sl@0
    69
	BIGNUM *tmp,*rr;
sl@0
    70
sl@0
    71
#ifdef BN_COUNT
sl@0
    72
	fprintf(stderr,"BN_sqr %d * %d\n",a->top,a->top);
sl@0
    73
#endif
sl@0
    74
	bn_check_top(a);
sl@0
    75
sl@0
    76
	al=a->top;
sl@0
    77
	if (al <= 0)
sl@0
    78
		{
sl@0
    79
		r->top=0;
sl@0
    80
		return 1;
sl@0
    81
		}
sl@0
    82
sl@0
    83
	BN_CTX_start(ctx);
sl@0
    84
	rr=(a != r) ? r : BN_CTX_get(ctx);
sl@0
    85
	tmp=BN_CTX_get(ctx);
sl@0
    86
	if (!rr || !tmp) goto err;
sl@0
    87
sl@0
    88
	max = 2 * al; /* Non-zero (from above) */
sl@0
    89
	if (bn_wexpand(rr,max) == NULL) goto err;
sl@0
    90
sl@0
    91
	if (al == 4)
sl@0
    92
		{
sl@0
    93
#ifndef BN_SQR_COMBA
sl@0
    94
		BN_ULONG t[8];
sl@0
    95
		bn_sqr_normal(rr->d,a->d,4,t);
sl@0
    96
#else
sl@0
    97
		bn_sqr_comba4(rr->d,a->d);
sl@0
    98
#endif
sl@0
    99
		}
sl@0
   100
	else if (al == 8)
sl@0
   101
		{
sl@0
   102
#ifndef BN_SQR_COMBA
sl@0
   103
		BN_ULONG t[16];
sl@0
   104
		bn_sqr_normal(rr->d,a->d,8,t);
sl@0
   105
#else
sl@0
   106
		bn_sqr_comba8(rr->d,a->d);
sl@0
   107
#endif
sl@0
   108
		}
sl@0
   109
	else 
sl@0
   110
		{
sl@0
   111
#if defined(BN_RECURSION)
sl@0
   112
		if (al < BN_SQR_RECURSIVE_SIZE_NORMAL)
sl@0
   113
			{
sl@0
   114
			BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
sl@0
   115
			bn_sqr_normal(rr->d,a->d,al,t);
sl@0
   116
			}
sl@0
   117
		else
sl@0
   118
			{
sl@0
   119
			int j,k;
sl@0
   120
sl@0
   121
			j=BN_num_bits_word((BN_ULONG)al);
sl@0
   122
			j=1<<(j-1);
sl@0
   123
			k=j+j;
sl@0
   124
			if (al == j)
sl@0
   125
				{
sl@0
   126
				if (bn_wexpand(tmp,k*2) == NULL) goto err;
sl@0
   127
				bn_sqr_recursive(rr->d,a->d,al,tmp->d);
sl@0
   128
				}
sl@0
   129
			else
sl@0
   130
				{
sl@0
   131
				if (bn_wexpand(tmp,max) == NULL) goto err;
sl@0
   132
				bn_sqr_normal(rr->d,a->d,al,tmp->d);
sl@0
   133
				}
sl@0
   134
			}
sl@0
   135
#else
sl@0
   136
		if (bn_wexpand(tmp,max) == NULL) goto err;
sl@0
   137
		bn_sqr_normal(rr->d,a->d,al,tmp->d);
sl@0
   138
#endif
sl@0
   139
		}
sl@0
   140
sl@0
   141
	rr->neg=0;
sl@0
   142
	/* If the most-significant half of the top word of 'a' is zero, then
sl@0
   143
	 * the square of 'a' will max-1 words. */
sl@0
   144
	if(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
sl@0
   145
		rr->top = max - 1;
sl@0
   146
	else
sl@0
   147
		rr->top = max;
sl@0
   148
	if (rr != r) BN_copy(r,rr);
sl@0
   149
	ret = 1;
sl@0
   150
 err:
sl@0
   151
	bn_check_top(rr);
sl@0
   152
	bn_check_top(tmp);
sl@0
   153
	BN_CTX_end(ctx);
sl@0
   154
	return(ret);
sl@0
   155
	}
sl@0
   156
sl@0
   157
/* tmp must have 2*n words */
sl@0
   158
EXPORT_C void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
sl@0
   159
	{
sl@0
   160
	int i,j,max;
sl@0
   161
	const BN_ULONG *ap;
sl@0
   162
	BN_ULONG *rp;
sl@0
   163
sl@0
   164
	max=n*2;
sl@0
   165
	ap=a;
sl@0
   166
	rp=r;
sl@0
   167
	rp[0]=rp[max-1]=0;
sl@0
   168
	rp++;
sl@0
   169
	j=n;
sl@0
   170
sl@0
   171
	if (--j > 0)
sl@0
   172
		{
sl@0
   173
		ap++;
sl@0
   174
		rp[j]=bn_mul_words(rp,ap,j,ap[-1]);
sl@0
   175
		rp+=2;
sl@0
   176
		}
sl@0
   177
sl@0
   178
	for (i=n-2; i>0; i--)
sl@0
   179
		{
sl@0
   180
		j--;
sl@0
   181
		ap++;
sl@0
   182
		rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]);
sl@0
   183
		rp+=2;
sl@0
   184
		}
sl@0
   185
sl@0
   186
	bn_add_words(r,r,r,max);
sl@0
   187
sl@0
   188
	/* There will not be a carry */
sl@0
   189
sl@0
   190
	bn_sqr_words(tmp,a,n);
sl@0
   191
sl@0
   192
	bn_add_words(r,r,tmp,max);
sl@0
   193
	}
sl@0
   194
sl@0
   195
#ifdef BN_RECURSION
sl@0
   196
/* r is 2*n words in size,
sl@0
   197
 * a and b are both n words in size.    (There's not actually a 'b' here ...)
sl@0
   198
 * n must be a power of 2.
sl@0
   199
 * We multiply and return the result.
sl@0
   200
 * t must be 2*n words in size
sl@0
   201
 * We calculate
sl@0
   202
 * a[0]*b[0]
sl@0
   203
 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
sl@0
   204
 * a[1]*b[1]
sl@0
   205
 */
sl@0
   206
EXPORT_C void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t)
sl@0
   207
	{
sl@0
   208
	int n=n2/2;
sl@0
   209
	int zero,c1;
sl@0
   210
	BN_ULONG ln,lo,*p;
sl@0
   211
sl@0
   212
#ifdef BN_COUNT
sl@0
   213
	fprintf(stderr," bn_sqr_recursive %d * %d\n",n2,n2);
sl@0
   214
#endif
sl@0
   215
	if (n2 == 4)
sl@0
   216
		{
sl@0
   217
#ifndef BN_SQR_COMBA
sl@0
   218
		bn_sqr_normal(r,a,4,t);
sl@0
   219
#else
sl@0
   220
		bn_sqr_comba4(r,a);
sl@0
   221
#endif
sl@0
   222
		return;
sl@0
   223
		}
sl@0
   224
	else if (n2 == 8)
sl@0
   225
		{
sl@0
   226
#ifndef BN_SQR_COMBA
sl@0
   227
		bn_sqr_normal(r,a,8,t);
sl@0
   228
#else
sl@0
   229
		bn_sqr_comba8(r,a);
sl@0
   230
#endif
sl@0
   231
		return;
sl@0
   232
		}
sl@0
   233
	if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL)
sl@0
   234
		{
sl@0
   235
		bn_sqr_normal(r,a,n2,t);
sl@0
   236
		return;
sl@0
   237
		}
sl@0
   238
	/* r=(a[0]-a[1])*(a[1]-a[0]) */
sl@0
   239
	c1=bn_cmp_words(a,&(a[n]),n);
sl@0
   240
	zero=0;
sl@0
   241
	if (c1 > 0)
sl@0
   242
		bn_sub_words(t,a,&(a[n]),n);
sl@0
   243
	else if (c1 < 0)
sl@0
   244
		bn_sub_words(t,&(a[n]),a,n);
sl@0
   245
	else
sl@0
   246
		zero=1;
sl@0
   247
sl@0
   248
	/* The result will always be negative unless it is zero */
sl@0
   249
	p= &(t[n2*2]);
sl@0
   250
sl@0
   251
	if (!zero)
sl@0
   252
		bn_sqr_recursive(&(t[n2]),t,n,p);
sl@0
   253
	else
sl@0
   254
		memset(&(t[n2]),0,n2*sizeof(BN_ULONG));
sl@0
   255
	bn_sqr_recursive(r,a,n,p);
sl@0
   256
	bn_sqr_recursive(&(r[n2]),&(a[n]),n,p);
sl@0
   257
sl@0
   258
	/* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
sl@0
   259
	 * r[10] holds (a[0]*b[0])
sl@0
   260
	 * r[32] holds (b[1]*b[1])
sl@0
   261
	 */
sl@0
   262
sl@0
   263
	c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
sl@0
   264
sl@0
   265
	/* t[32] is negative */
sl@0
   266
	c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
sl@0
   267
sl@0
   268
	/* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
sl@0
   269
	 * r[10] holds (a[0]*a[0])
sl@0
   270
	 * r[32] holds (a[1]*a[1])
sl@0
   271
	 * c1 holds the carry bits
sl@0
   272
	 */
sl@0
   273
	c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
sl@0
   274
	if (c1)
sl@0
   275
		{
sl@0
   276
		p= &(r[n+n2]);
sl@0
   277
		lo= *p;
sl@0
   278
		ln=(lo+c1)&BN_MASK2;
sl@0
   279
		*p=ln;
sl@0
   280
sl@0
   281
		/* The overflow will stop before we over write
sl@0
   282
		 * words we should not overwrite */
sl@0
   283
		if (ln < (BN_ULONG)c1)
sl@0
   284
			{
sl@0
   285
			do	{
sl@0
   286
				p++;
sl@0
   287
				lo= *p;
sl@0
   288
				ln=(lo+1)&BN_MASK2;
sl@0
   289
				*p=ln;
sl@0
   290
				} while (ln == 0);
sl@0
   291
			}
sl@0
   292
		}
sl@0
   293
	}
sl@0
   294
#endif