os/ossrv/ssl/libcrypto/src/crypto/bn/bn_exp.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_exp.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
 * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
sl@0
    60
 *
sl@0
    61
 * Redistribution and use in source and binary forms, with or without
sl@0
    62
 * modification, are permitted provided that the following conditions
sl@0
    63
 * are met:
sl@0
    64
 *
sl@0
    65
 * 1. Redistributions of source code must retain the above copyright
sl@0
    66
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    67
 *
sl@0
    68
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    69
 *    notice, this list of conditions and the following disclaimer in
sl@0
    70
 *    the documentation and/or other materials provided with the
sl@0
    71
 *    distribution.
sl@0
    72
 *
sl@0
    73
 * 3. All advertising materials mentioning features or use of this
sl@0
    74
 *    software must display the following acknowledgment:
sl@0
    75
 *    "This product includes software developed by the OpenSSL Project
sl@0
    76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    77
 *
sl@0
    78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    79
 *    endorse or promote products derived from this software without
sl@0
    80
 *    prior written permission. For written permission, please contact
sl@0
    81
 *    openssl-core@openssl.org.
sl@0
    82
 *
sl@0
    83
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    84
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    85
 *    permission of the OpenSSL Project.
sl@0
    86
 *
sl@0
    87
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    88
 *    acknowledgment:
sl@0
    89
 *    "This product includes software developed by the OpenSSL Project
sl@0
    90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    91
 *
sl@0
    92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
   101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
   102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
   103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
   104
 * ====================================================================
sl@0
   105
 *
sl@0
   106
 * This product includes cryptographic software written by Eric Young
sl@0
   107
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
   108
 * Hudson (tjh@cryptsoft.com).
sl@0
   109
 *
sl@0
   110
 */
sl@0
   111
sl@0
   112
sl@0
   113
#include "cryptlib.h"
sl@0
   114
#include "bn_lcl.h"
sl@0
   115
sl@0
   116
/* maximum precomputation table size for *variable* sliding windows */
sl@0
   117
#define TABLE_SIZE	32
sl@0
   118
sl@0
   119
/* this one works - simple but works */
sl@0
   120
EXPORT_C int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
sl@0
   121
	{
sl@0
   122
	int i,bits,ret=0;
sl@0
   123
	BIGNUM *v,*rr;
sl@0
   124
sl@0
   125
	if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)
sl@0
   126
		{
sl@0
   127
		/* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
sl@0
   128
		BNerr(BN_F_BN_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   129
		return -1;
sl@0
   130
		}
sl@0
   131
sl@0
   132
	BN_CTX_start(ctx);
sl@0
   133
	if ((r == a) || (r == p))
sl@0
   134
		rr = BN_CTX_get(ctx);
sl@0
   135
	else
sl@0
   136
		rr = r;
sl@0
   137
	if ((v = BN_CTX_get(ctx)) == NULL) goto err;
sl@0
   138
sl@0
   139
	if (BN_copy(v,a) == NULL) goto err;
sl@0
   140
	bits=BN_num_bits(p);
sl@0
   141
sl@0
   142
	if (BN_is_odd(p))
sl@0
   143
		{ if (BN_copy(rr,a) == NULL) goto err; }
sl@0
   144
	else	{ if (!BN_one(rr)) goto err; }
sl@0
   145
sl@0
   146
	for (i=1; i<bits; i++)
sl@0
   147
		{
sl@0
   148
		if (!BN_sqr(v,v,ctx)) goto err;
sl@0
   149
		if (BN_is_bit_set(p,i))
sl@0
   150
			{
sl@0
   151
			if (!BN_mul(rr,rr,v,ctx)) goto err;
sl@0
   152
			}
sl@0
   153
		}
sl@0
   154
	ret=1;
sl@0
   155
err:
sl@0
   156
	if (r != rr) BN_copy(r,rr);
sl@0
   157
	BN_CTX_end(ctx);
sl@0
   158
	bn_check_top(r);
sl@0
   159
	return(ret);
sl@0
   160
	}
sl@0
   161
sl@0
   162
sl@0
   163
EXPORT_C int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
sl@0
   164
	       BN_CTX *ctx)
sl@0
   165
	{
sl@0
   166
	int ret;
sl@0
   167
sl@0
   168
	bn_check_top(a);
sl@0
   169
	bn_check_top(p);
sl@0
   170
	bn_check_top(m);
sl@0
   171
sl@0
   172
	/* For even modulus  m = 2^k*m_odd,  it might make sense to compute
sl@0
   173
	 * a^p mod m_odd  and  a^p mod 2^k  separately (with Montgomery
sl@0
   174
	 * exponentiation for the odd part), using appropriate exponent
sl@0
   175
	 * reductions, and combine the results using the CRT.
sl@0
   176
	 *
sl@0
   177
	 * For now, we use Montgomery only if the modulus is odd; otherwise,
sl@0
   178
	 * exponentiation using the reciprocal-based quick remaindering
sl@0
   179
	 * algorithm is used.
sl@0
   180
	 *
sl@0
   181
	 * (Timing obtained with expspeed.c [computations  a^p mod m
sl@0
   182
	 * where  a, p, m  are of the same length: 256, 512, 1024, 2048,
sl@0
   183
	 * 4096, 8192 bits], compared to the running time of the
sl@0
   184
	 * standard algorithm:
sl@0
   185
	 *
sl@0
   186
	 *   BN_mod_exp_mont   33 .. 40 %  [AMD K6-2, Linux, debug configuration]
sl@0
   187
         *                     55 .. 77 %  [UltraSparc processor, but
sl@0
   188
	 *                                  debug-solaris-sparcv8-gcc conf.]
sl@0
   189
	 * 
sl@0
   190
	 *   BN_mod_exp_recp   50 .. 70 %  [AMD K6-2, Linux, debug configuration]
sl@0
   191
	 *                     62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
sl@0
   192
	 *
sl@0
   193
	 * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont
sl@0
   194
	 * at 2048 and more bits, but at 512 and 1024 bits, it was
sl@0
   195
	 * slower even than the standard algorithm!
sl@0
   196
	 *
sl@0
   197
	 * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations]
sl@0
   198
	 * should be obtained when the new Montgomery reduction code
sl@0
   199
	 * has been integrated into OpenSSL.)
sl@0
   200
	 */
sl@0
   201
sl@0
   202
#define MONT_MUL_MOD
sl@0
   203
#define MONT_EXP_WORD
sl@0
   204
#define RECP_MUL_MOD
sl@0
   205
sl@0
   206
#ifdef MONT_MUL_MOD
sl@0
   207
	/* I have finally been able to take out this pre-condition of
sl@0
   208
	 * the top bit being set.  It was caused by an error in BN_div
sl@0
   209
	 * with negatives.  There was also another problem when for a^b%m
sl@0
   210
	 * a >= m.  eay 07-May-97 */
sl@0
   211
/*	if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
sl@0
   212
sl@0
   213
	if (BN_is_odd(m))
sl@0
   214
		{
sl@0
   215
#  ifdef MONT_EXP_WORD
sl@0
   216
		if (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0))
sl@0
   217
			{
sl@0
   218
			BN_ULONG A = a->d[0];
sl@0
   219
			ret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);
sl@0
   220
			}
sl@0
   221
		else
sl@0
   222
#  endif
sl@0
   223
			ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);
sl@0
   224
		}
sl@0
   225
	else
sl@0
   226
#endif
sl@0
   227
#ifdef RECP_MUL_MOD
sl@0
   228
		{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }
sl@0
   229
#else
sl@0
   230
		{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }
sl@0
   231
#endif
sl@0
   232
sl@0
   233
	bn_check_top(r);
sl@0
   234
	return(ret);
sl@0
   235
	}
sl@0
   236
sl@0
   237
sl@0
   238
EXPORT_C int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
sl@0
   239
		    const BIGNUM *m, BN_CTX *ctx)
sl@0
   240
	{
sl@0
   241
	int i,j,bits,ret=0,wstart,wend,window,wvalue;
sl@0
   242
	int start=1;
sl@0
   243
	BIGNUM *aa;
sl@0
   244
	/* Table of variables obtained from 'ctx' */
sl@0
   245
	BIGNUM *val[TABLE_SIZE];
sl@0
   246
	BN_RECP_CTX recp;
sl@0
   247
sl@0
   248
	if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)
sl@0
   249
		{
sl@0
   250
		/* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
sl@0
   251
		BNerr(BN_F_BN_MOD_EXP_RECP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   252
		return -1;
sl@0
   253
		}
sl@0
   254
sl@0
   255
	bits=BN_num_bits(p);
sl@0
   256
sl@0
   257
	if (bits == 0)
sl@0
   258
		{
sl@0
   259
		ret = BN_one(r);
sl@0
   260
		return ret;
sl@0
   261
		}
sl@0
   262
sl@0
   263
	BN_CTX_start(ctx);
sl@0
   264
	aa = BN_CTX_get(ctx);
sl@0
   265
	val[0] = BN_CTX_get(ctx);
sl@0
   266
	if(!aa || !val[0]) goto err;
sl@0
   267
sl@0
   268
	BN_RECP_CTX_init(&recp);
sl@0
   269
	if (m->neg)
sl@0
   270
		{
sl@0
   271
		/* ignore sign of 'm' */
sl@0
   272
		if (!BN_copy(aa, m)) goto err;
sl@0
   273
		aa->neg = 0;
sl@0
   274
		if (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;
sl@0
   275
		}
sl@0
   276
	else
sl@0
   277
		{
sl@0
   278
		if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
sl@0
   279
		}
sl@0
   280
sl@0
   281
	if (!BN_nnmod(val[0],a,m,ctx)) goto err;		/* 1 */
sl@0
   282
	if (BN_is_zero(val[0]))
sl@0
   283
		{
sl@0
   284
		BN_zero(r);
sl@0
   285
		ret = 1;
sl@0
   286
		goto err;
sl@0
   287
		}
sl@0
   288
sl@0
   289
	window = BN_window_bits_for_exponent_size(bits);
sl@0
   290
	if (window > 1)
sl@0
   291
		{
sl@0
   292
		if (!BN_mod_mul_reciprocal(aa,val[0],val[0],&recp,ctx))
sl@0
   293
			goto err;				/* 2 */
sl@0
   294
		j=1<<(window-1);
sl@0
   295
		for (i=1; i<j; i++)
sl@0
   296
			{
sl@0
   297
			if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
sl@0
   298
					!BN_mod_mul_reciprocal(val[i],val[i-1],
sl@0
   299
						aa,&recp,ctx))
sl@0
   300
				goto err;
sl@0
   301
			}
sl@0
   302
		}
sl@0
   303
		
sl@0
   304
	start=1;	/* This is used to avoid multiplication etc
sl@0
   305
			 * when there is only the value '1' in the
sl@0
   306
			 * buffer. */
sl@0
   307
	wvalue=0;	/* The 'value' of the window */
sl@0
   308
	wstart=bits-1;	/* The top bit of the window */
sl@0
   309
	wend=0;		/* The bottom bit of the window */
sl@0
   310
sl@0
   311
	if (!BN_one(r)) goto err;
sl@0
   312
sl@0
   313
	for (;;)
sl@0
   314
		{
sl@0
   315
		if (BN_is_bit_set(p,wstart) == 0)
sl@0
   316
			{
sl@0
   317
			if (!start)
sl@0
   318
				if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
sl@0
   319
				goto err;
sl@0
   320
			if (wstart == 0) break;
sl@0
   321
			wstart--;
sl@0
   322
			continue;
sl@0
   323
			}
sl@0
   324
		/* We now have wstart on a 'set' bit, we now need to work out
sl@0
   325
		 * how bit a window to do.  To do this we need to scan
sl@0
   326
		 * forward until the last set bit before the end of the
sl@0
   327
		 * window */
sl@0
   328
		j=wstart;
sl@0
   329
		wvalue=1;
sl@0
   330
		wend=0;
sl@0
   331
		for (i=1; i<window; i++)
sl@0
   332
			{
sl@0
   333
			if (wstart-i < 0) break;
sl@0
   334
			if (BN_is_bit_set(p,wstart-i))
sl@0
   335
				{
sl@0
   336
				wvalue<<=(i-wend);
sl@0
   337
				wvalue|=1;
sl@0
   338
				wend=i;
sl@0
   339
				}
sl@0
   340
			}
sl@0
   341
sl@0
   342
		/* wend is the size of the current window */
sl@0
   343
		j=wend+1;
sl@0
   344
		/* add the 'bytes above' */
sl@0
   345
		if (!start)
sl@0
   346
			for (i=0; i<j; i++)
sl@0
   347
				{
sl@0
   348
				if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
sl@0
   349
					goto err;
sl@0
   350
				}
sl@0
   351
		
sl@0
   352
		/* wvalue will be an odd number < 2^window */
sl@0
   353
		if (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],&recp,ctx))
sl@0
   354
			goto err;
sl@0
   355
sl@0
   356
		/* move the 'window' down further */
sl@0
   357
		wstart-=wend+1;
sl@0
   358
		wvalue=0;
sl@0
   359
		start=0;
sl@0
   360
		if (wstart < 0) break;
sl@0
   361
		}
sl@0
   362
	ret=1;
sl@0
   363
err:
sl@0
   364
	BN_CTX_end(ctx);
sl@0
   365
	BN_RECP_CTX_free(&recp);
sl@0
   366
	bn_check_top(r);
sl@0
   367
	return(ret);
sl@0
   368
	}
sl@0
   369
sl@0
   370
sl@0
   371
EXPORT_C int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
sl@0
   372
		    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
sl@0
   373
	{
sl@0
   374
	int i,j,bits,ret=0,wstart,wend,window,wvalue;
sl@0
   375
	int start=1;
sl@0
   376
	BIGNUM *d,*r;
sl@0
   377
	const BIGNUM *aa;
sl@0
   378
	/* Table of variables obtained from 'ctx' */
sl@0
   379
	BIGNUM *val[TABLE_SIZE];
sl@0
   380
	BN_MONT_CTX *mont=NULL;
sl@0
   381
sl@0
   382
	if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)
sl@0
   383
		{
sl@0
   384
		return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
sl@0
   385
		}
sl@0
   386
sl@0
   387
	bn_check_top(a);
sl@0
   388
	bn_check_top(p);
sl@0
   389
	bn_check_top(m);
sl@0
   390
sl@0
   391
	if (!BN_is_odd(m))
sl@0
   392
		{
sl@0
   393
		BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
sl@0
   394
		return(0);
sl@0
   395
		}
sl@0
   396
	bits=BN_num_bits(p);
sl@0
   397
	if (bits == 0)
sl@0
   398
		{
sl@0
   399
		ret = BN_one(rr);
sl@0
   400
		return ret;
sl@0
   401
		}
sl@0
   402
sl@0
   403
	BN_CTX_start(ctx);
sl@0
   404
	d = BN_CTX_get(ctx);
sl@0
   405
	r = BN_CTX_get(ctx);
sl@0
   406
	val[0] = BN_CTX_get(ctx);
sl@0
   407
	if (!d || !r || !val[0]) goto err;
sl@0
   408
sl@0
   409
	/* If this is not done, things will break in the montgomery
sl@0
   410
	 * part */
sl@0
   411
sl@0
   412
	if (in_mont != NULL)
sl@0
   413
		mont=in_mont;
sl@0
   414
	else
sl@0
   415
		{
sl@0
   416
		if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
sl@0
   417
		if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
sl@0
   418
		}
sl@0
   419
sl@0
   420
	if (a->neg || BN_ucmp(a,m) >= 0)
sl@0
   421
		{
sl@0
   422
		if (!BN_nnmod(val[0],a,m,ctx))
sl@0
   423
			goto err;
sl@0
   424
		aa= val[0];
sl@0
   425
		}
sl@0
   426
	else
sl@0
   427
		aa=a;
sl@0
   428
	if (BN_is_zero(aa))
sl@0
   429
		{
sl@0
   430
		BN_zero(rr);
sl@0
   431
		ret = 1;
sl@0
   432
		goto err;
sl@0
   433
		}
sl@0
   434
	if (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err; /* 1 */
sl@0
   435
sl@0
   436
	window = BN_window_bits_for_exponent_size(bits);
sl@0
   437
	if (window > 1)
sl@0
   438
		{
sl@0
   439
		if (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err; /* 2 */
sl@0
   440
		j=1<<(window-1);
sl@0
   441
		for (i=1; i<j; i++)
sl@0
   442
			{
sl@0
   443
			if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
sl@0
   444
					!BN_mod_mul_montgomery(val[i],val[i-1],
sl@0
   445
						d,mont,ctx))
sl@0
   446
				goto err;
sl@0
   447
			}
sl@0
   448
		}
sl@0
   449
sl@0
   450
	start=1;	/* This is used to avoid multiplication etc
sl@0
   451
			 * when there is only the value '1' in the
sl@0
   452
			 * buffer. */
sl@0
   453
	wvalue=0;	/* The 'value' of the window */
sl@0
   454
	wstart=bits-1;	/* The top bit of the window */
sl@0
   455
	wend=0;		/* The bottom bit of the window */
sl@0
   456
sl@0
   457
	if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
sl@0
   458
	for (;;)
sl@0
   459
		{
sl@0
   460
		if (BN_is_bit_set(p,wstart) == 0)
sl@0
   461
			{
sl@0
   462
			if (!start)
sl@0
   463
				{
sl@0
   464
				if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
sl@0
   465
				goto err;
sl@0
   466
				}
sl@0
   467
			if (wstart == 0) break;
sl@0
   468
			wstart--;
sl@0
   469
			continue;
sl@0
   470
			}
sl@0
   471
		/* We now have wstart on a 'set' bit, we now need to work out
sl@0
   472
		 * how bit a window to do.  To do this we need to scan
sl@0
   473
		 * forward until the last set bit before the end of the
sl@0
   474
		 * window */
sl@0
   475
		j=wstart;
sl@0
   476
		wvalue=1;
sl@0
   477
		wend=0;
sl@0
   478
		for (i=1; i<window; i++)
sl@0
   479
			{
sl@0
   480
			if (wstart-i < 0) break;
sl@0
   481
			if (BN_is_bit_set(p,wstart-i))
sl@0
   482
				{
sl@0
   483
				wvalue<<=(i-wend);
sl@0
   484
				wvalue|=1;
sl@0
   485
				wend=i;
sl@0
   486
				}
sl@0
   487
			}
sl@0
   488
sl@0
   489
		/* wend is the size of the current window */
sl@0
   490
		j=wend+1;
sl@0
   491
		/* add the 'bytes above' */
sl@0
   492
		if (!start)
sl@0
   493
			for (i=0; i<j; i++)
sl@0
   494
				{
sl@0
   495
				if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
sl@0
   496
					goto err;
sl@0
   497
				}
sl@0
   498
		
sl@0
   499
		/* wvalue will be an odd number < 2^window */
sl@0
   500
		if (!BN_mod_mul_montgomery(r,r,val[wvalue>>1],mont,ctx))
sl@0
   501
			goto err;
sl@0
   502
sl@0
   503
		/* move the 'window' down further */
sl@0
   504
		wstart-=wend+1;
sl@0
   505
		wvalue=0;
sl@0
   506
		start=0;
sl@0
   507
		if (wstart < 0) break;
sl@0
   508
		}
sl@0
   509
	if (!BN_from_montgomery(rr,r,mont,ctx)) goto err;
sl@0
   510
	ret=1;
sl@0
   511
err:
sl@0
   512
	if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
sl@0
   513
	BN_CTX_end(ctx);
sl@0
   514
	bn_check_top(rr);
sl@0
   515
	return(ret);
sl@0
   516
	}
sl@0
   517
sl@0
   518
sl@0
   519
/* BN_mod_exp_mont_consttime() stores the precomputed powers in a specific layout
sl@0
   520
 * so that accessing any of these table values shows the same access pattern as far
sl@0
   521
 * as cache lines are concerned.  The following functions are used to transfer a BIGNUM
sl@0
   522
 * from/to that table. */
sl@0
   523
sl@0
   524
static int MOD_EXP_CTIME_COPY_TO_PREBUF(BIGNUM *b, int top, unsigned char *buf, int idx, int width)
sl@0
   525
	{
sl@0
   526
	size_t i, j;
sl@0
   527
sl@0
   528
	if (bn_wexpand(b, top) == NULL)
sl@0
   529
		return 0;
sl@0
   530
	while (b->top < top)
sl@0
   531
		{
sl@0
   532
		b->d[b->top++] = 0;
sl@0
   533
		}
sl@0
   534
	
sl@0
   535
	for (i = 0, j=idx; i < top * sizeof b->d[0]; i++, j+=width)
sl@0
   536
		{
sl@0
   537
		buf[j] = ((unsigned char*)b->d)[i];
sl@0
   538
		}
sl@0
   539
sl@0
   540
	bn_correct_top(b);
sl@0
   541
	return 1;
sl@0
   542
	}
sl@0
   543
sl@0
   544
static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, unsigned char *buf, int idx, int width)
sl@0
   545
	{
sl@0
   546
	size_t i, j;
sl@0
   547
sl@0
   548
	if (bn_wexpand(b, top) == NULL)
sl@0
   549
		return 0;
sl@0
   550
sl@0
   551
	for (i=0, j=idx; i < top * sizeof b->d[0]; i++, j+=width)
sl@0
   552
		{
sl@0
   553
		((unsigned char*)b->d)[i] = buf[j];
sl@0
   554
		}
sl@0
   555
sl@0
   556
	b->top = top;
sl@0
   557
	bn_correct_top(b);
sl@0
   558
	return 1;
sl@0
   559
	}	
sl@0
   560
sl@0
   561
/* Given a pointer value, compute the next address that is a cache line multiple. */
sl@0
   562
#define MOD_EXP_CTIME_ALIGN(x_) \
sl@0
   563
	((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((BN_ULONG)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
sl@0
   564
sl@0
   565
/* This variant of BN_mod_exp_mont() uses fixed windows and the special
sl@0
   566
 * precomputation memory layout to limit data-dependency to a minimum
sl@0
   567
 * to protect secret exponents (cf. the hyper-threading timing attacks
sl@0
   568
 * pointed out by Colin Percival,
sl@0
   569
 * http://www.daemonology.net/hyperthreading-considered-harmful/)
sl@0
   570
 */
sl@0
   571
EXPORT_C int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
sl@0
   572
		    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
sl@0
   573
	{
sl@0
   574
	int i,bits,ret=0,idx,window,wvalue;
sl@0
   575
	int top;
sl@0
   576
 	BIGNUM *r;
sl@0
   577
	const BIGNUM *aa;
sl@0
   578
	BN_MONT_CTX *mont=NULL;
sl@0
   579
sl@0
   580
	int numPowers;
sl@0
   581
	unsigned char *powerbufFree=NULL;
sl@0
   582
	int powerbufLen = 0;
sl@0
   583
	unsigned char *powerbuf=NULL;
sl@0
   584
	BIGNUM *computeTemp=NULL, *am=NULL;
sl@0
   585
sl@0
   586
	bn_check_top(a);
sl@0
   587
	bn_check_top(p);
sl@0
   588
	bn_check_top(m);
sl@0
   589
sl@0
   590
	top = m->top;
sl@0
   591
sl@0
   592
	if (!(m->d[0] & 1))
sl@0
   593
		{
sl@0
   594
		BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);
sl@0
   595
		return(0);
sl@0
   596
		}
sl@0
   597
	bits=BN_num_bits(p);
sl@0
   598
	if (bits == 0)
sl@0
   599
		{
sl@0
   600
		ret = BN_one(rr);
sl@0
   601
		return ret;
sl@0
   602
		}
sl@0
   603
sl@0
   604
 	/* Initialize BIGNUM context and allocate intermediate result */
sl@0
   605
	BN_CTX_start(ctx);
sl@0
   606
	r = BN_CTX_get(ctx);
sl@0
   607
	if (r == NULL) goto err;
sl@0
   608
sl@0
   609
	/* Allocate a montgomery context if it was not supplied by the caller.
sl@0
   610
	 * If this is not done, things will break in the montgomery part.
sl@0
   611
 	 */
sl@0
   612
	if (in_mont != NULL)
sl@0
   613
		mont=in_mont;
sl@0
   614
	else
sl@0
   615
		{
sl@0
   616
		if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
sl@0
   617
		if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
sl@0
   618
		}
sl@0
   619
sl@0
   620
	/* Get the window size to use with size of p. */
sl@0
   621
	window = BN_window_bits_for_ctime_exponent_size(bits);
sl@0
   622
sl@0
   623
	/* Allocate a buffer large enough to hold all of the pre-computed
sl@0
   624
	 * powers of a.
sl@0
   625
	 */
sl@0
   626
	numPowers = 1 << window;
sl@0
   627
	powerbufLen = sizeof(m->d[0])*top*numPowers;
sl@0
   628
	if ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)
sl@0
   629
		goto err;
sl@0
   630
		
sl@0
   631
	powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
sl@0
   632
	memset(powerbuf, 0, powerbufLen);
sl@0
   633
sl@0
   634
 	/* Initialize the intermediate result. Do this early to save double conversion,
sl@0
   635
	 * once each for a^0 and intermediate result.
sl@0
   636
	 */
sl@0
   637
 	if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
sl@0
   638
	if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;
sl@0
   639
sl@0
   640
	/* Initialize computeTemp as a^1 with montgomery precalcs */
sl@0
   641
	computeTemp = BN_CTX_get(ctx);
sl@0
   642
	am = BN_CTX_get(ctx);
sl@0
   643
	if (computeTemp==NULL || am==NULL) goto err;
sl@0
   644
sl@0
   645
	if (a->neg || BN_ucmp(a,m) >= 0)
sl@0
   646
		{
sl@0
   647
		if (!BN_mod(am,a,m,ctx))
sl@0
   648
			goto err;
sl@0
   649
		aa= am;
sl@0
   650
		}
sl@0
   651
	else
sl@0
   652
		aa=a;
sl@0
   653
	if (!BN_to_montgomery(am,aa,mont,ctx)) goto err;
sl@0
   654
	if (!BN_copy(computeTemp, am)) goto err;
sl@0
   655
	if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;
sl@0
   656
sl@0
   657
	/* If the window size is greater than 1, then calculate
sl@0
   658
	 * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1)
sl@0
   659
	 * (even powers could instead be computed as (a^(i/2))^2
sl@0
   660
	 * to use the slight performance advantage of sqr over mul).
sl@0
   661
	 */
sl@0
   662
	if (window > 1)
sl@0
   663
		{
sl@0
   664
		for (i=2; i<numPowers; i++)
sl@0
   665
			{
sl@0
   666
			/* Calculate a^i = a^(i-1) * a */
sl@0
   667
			if (!BN_mod_mul_montgomery(computeTemp,am,computeTemp,mont,ctx))
sl@0
   668
				goto err;
sl@0
   669
			if (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers)) goto err;
sl@0
   670
			}
sl@0
   671
		}
sl@0
   672
sl@0
   673
 	/* Adjust the number of bits up to a multiple of the window size.
sl@0
   674
 	 * If the exponent length is not a multiple of the window size, then
sl@0
   675
 	 * this pads the most significant bits with zeros to normalize the
sl@0
   676
 	 * scanning loop to there's no special cases.
sl@0
   677
 	 *
sl@0
   678
 	 * * NOTE: Making the window size a power of two less than the native
sl@0
   679
	 * * word size ensures that the padded bits won't go past the last
sl@0
   680
 	 * * word in the internal BIGNUM structure. Going past the end will
sl@0
   681
 	 * * still produce the correct result, but causes a different branch
sl@0
   682
 	 * * to be taken in the BN_is_bit_set function.
sl@0
   683
 	 */
sl@0
   684
 	bits = ((bits+window-1)/window)*window;
sl@0
   685
 	idx=bits-1;	/* The top bit of the window */
sl@0
   686
sl@0
   687
 	/* Scan the exponent one window at a time starting from the most
sl@0
   688
 	 * significant bits.
sl@0
   689
 	 */
sl@0
   690
 	while (idx >= 0)
sl@0
   691
  		{
sl@0
   692
 		wvalue=0; /* The 'value' of the window */
sl@0
   693
 		
sl@0
   694
 		/* Scan the window, squaring the result as we go */
sl@0
   695
 		for (i=0; i<window; i++,idx--)
sl@0
   696
 			{
sl@0
   697
			if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))	goto err;
sl@0
   698
			wvalue = (wvalue<<1)+BN_is_bit_set(p,idx);
sl@0
   699
  			}
sl@0
   700
 		
sl@0
   701
		/* Fetch the appropriate pre-computed value from the pre-buf */
sl@0
   702
		if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers)) goto err;
sl@0
   703
sl@0
   704
 		/* Multiply the result into the intermediate result */
sl@0
   705
 		if (!BN_mod_mul_montgomery(r,r,computeTemp,mont,ctx)) goto err;
sl@0
   706
  		}
sl@0
   707
sl@0
   708
 	/* Convert the final result from montgomery to standard format */
sl@0
   709
	if (!BN_from_montgomery(rr,r,mont,ctx)) goto err;
sl@0
   710
	ret=1;
sl@0
   711
err:
sl@0
   712
	if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
sl@0
   713
	if (powerbuf!=NULL)
sl@0
   714
		{
sl@0
   715
		OPENSSL_cleanse(powerbuf,powerbufLen);
sl@0
   716
		OPENSSL_free(powerbufFree);
sl@0
   717
		}
sl@0
   718
 	if (am!=NULL) BN_clear(am);
sl@0
   719
 	if (computeTemp!=NULL) BN_clear(computeTemp);
sl@0
   720
	BN_CTX_end(ctx);
sl@0
   721
	return(ret);
sl@0
   722
	}
sl@0
   723
sl@0
   724
EXPORT_C int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
sl@0
   725
                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
sl@0
   726
	{
sl@0
   727
	BN_MONT_CTX *mont = NULL;
sl@0
   728
	int b, bits, ret=0;
sl@0
   729
	int r_is_one;
sl@0
   730
	BN_ULONG w, next_w;
sl@0
   731
	BIGNUM *d, *r, *t;
sl@0
   732
	BIGNUM *swap_tmp;
sl@0
   733
#define BN_MOD_MUL_WORD(r, w, m) \
sl@0
   734
		(BN_mul_word(r, (w)) && \
sl@0
   735
		(/* BN_ucmp(r, (m)) < 0 ? 1 :*/  \
sl@0
   736
			(BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
sl@0
   737
		/* BN_MOD_MUL_WORD is only used with 'w' large,
sl@0
   738
		 * so the BN_ucmp test is probably more overhead
sl@0
   739
		 * than always using BN_mod (which uses BN_copy if
sl@0
   740
		 * a similar test returns true). */
sl@0
   741
		/* We can use BN_mod and do not need BN_nnmod because our
sl@0
   742
		 * accumulator is never negative (the result of BN_mod does
sl@0
   743
		 * not depend on the sign of the modulus).
sl@0
   744
		 */
sl@0
   745
#define BN_TO_MONTGOMERY_WORD(r, w, mont) \
sl@0
   746
		(BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
sl@0
   747
sl@0
   748
	if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)
sl@0
   749
		{
sl@0
   750
		/* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
sl@0
   751
		BNerr(BN_F_BN_MOD_EXP_MONT_WORD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   752
		return -1;
sl@0
   753
		}
sl@0
   754
sl@0
   755
	bn_check_top(p);
sl@0
   756
	bn_check_top(m);
sl@0
   757
sl@0
   758
	if (!BN_is_odd(m))
sl@0
   759
		{
sl@0
   760
		BNerr(BN_F_BN_MOD_EXP_MONT_WORD,BN_R_CALLED_WITH_EVEN_MODULUS);
sl@0
   761
		return(0);
sl@0
   762
		}
sl@0
   763
	if (m->top == 1)
sl@0
   764
		a %= m->d[0]; /* make sure that 'a' is reduced */
sl@0
   765
sl@0
   766
	bits = BN_num_bits(p);
sl@0
   767
	if (bits == 0)
sl@0
   768
		{
sl@0
   769
		ret = BN_one(rr);
sl@0
   770
		return ret;
sl@0
   771
		}
sl@0
   772
	if (a == 0)
sl@0
   773
		{
sl@0
   774
		BN_zero(rr);
sl@0
   775
		ret = 1;
sl@0
   776
		return ret;
sl@0
   777
		}
sl@0
   778
sl@0
   779
	BN_CTX_start(ctx);
sl@0
   780
	d = BN_CTX_get(ctx);
sl@0
   781
	r = BN_CTX_get(ctx);
sl@0
   782
	t = BN_CTX_get(ctx);
sl@0
   783
	if (d == NULL || r == NULL || t == NULL) goto err;
sl@0
   784
sl@0
   785
	if (in_mont != NULL)
sl@0
   786
		mont=in_mont;
sl@0
   787
	else
sl@0
   788
		{
sl@0
   789
		if ((mont = BN_MONT_CTX_new()) == NULL) goto err;
sl@0
   790
		if (!BN_MONT_CTX_set(mont, m, ctx)) goto err;
sl@0
   791
		}
sl@0
   792
sl@0
   793
	r_is_one = 1; /* except for Montgomery factor */
sl@0
   794
sl@0
   795
	/* bits-1 >= 0 */
sl@0
   796
sl@0
   797
	/* The result is accumulated in the product r*w. */
sl@0
   798
	w = a; /* bit 'bits-1' of 'p' is always set */
sl@0
   799
	for (b = bits-2; b >= 0; b--)
sl@0
   800
		{
sl@0
   801
		/* First, square r*w. */
sl@0
   802
		next_w = w*w;
sl@0
   803
		if ((next_w/w) != w) /* overflow */
sl@0
   804
			{
sl@0
   805
			if (r_is_one)
sl@0
   806
				{
sl@0
   807
				if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
sl@0
   808
				r_is_one = 0;
sl@0
   809
				}
sl@0
   810
			else
sl@0
   811
				{
sl@0
   812
				if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
sl@0
   813
				}
sl@0
   814
			next_w = 1;
sl@0
   815
			}
sl@0
   816
		w = next_w;
sl@0
   817
		if (!r_is_one)
sl@0
   818
			{
sl@0
   819
			if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) goto err;
sl@0
   820
			}
sl@0
   821
sl@0
   822
		/* Second, multiply r*w by 'a' if exponent bit is set. */
sl@0
   823
		if (BN_is_bit_set(p, b))
sl@0
   824
			{
sl@0
   825
			next_w = w*a;
sl@0
   826
			if ((next_w/a) != w) /* overflow */
sl@0
   827
				{
sl@0
   828
				if (r_is_one)
sl@0
   829
					{
sl@0
   830
					if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
sl@0
   831
					r_is_one = 0;
sl@0
   832
					}
sl@0
   833
				else
sl@0
   834
					{
sl@0
   835
					if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
sl@0
   836
					}
sl@0
   837
				next_w = a;
sl@0
   838
				}
sl@0
   839
			w = next_w;
sl@0
   840
			}
sl@0
   841
		}
sl@0
   842
sl@0
   843
	/* Finally, set r:=r*w. */
sl@0
   844
	if (w != 1)
sl@0
   845
		{
sl@0
   846
		if (r_is_one)
sl@0
   847
			{
sl@0
   848
			if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
sl@0
   849
			r_is_one = 0;
sl@0
   850
			}
sl@0
   851
		else
sl@0
   852
			{
sl@0
   853
			if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
sl@0
   854
			}
sl@0
   855
		}
sl@0
   856
sl@0
   857
	if (r_is_one) /* can happen only if a == 1*/
sl@0
   858
		{
sl@0
   859
		if (!BN_one(rr)) goto err;
sl@0
   860
		}
sl@0
   861
	else
sl@0
   862
		{
sl@0
   863
		if (!BN_from_montgomery(rr, r, mont, ctx)) goto err;
sl@0
   864
		}
sl@0
   865
	ret = 1;
sl@0
   866
err:
sl@0
   867
	if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
sl@0
   868
	BN_CTX_end(ctx);
sl@0
   869
	bn_check_top(rr);
sl@0
   870
	return(ret);
sl@0
   871
	}
sl@0
   872
sl@0
   873
sl@0
   874
/* The old fallback, simple version :-) */
sl@0
   875
EXPORT_C int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
sl@0
   876
		const BIGNUM *m, BN_CTX *ctx)
sl@0
   877
	{
sl@0
   878
	int i,j,bits,ret=0,wstart,wend,window,wvalue;
sl@0
   879
	int start=1;
sl@0
   880
	BIGNUM *d;
sl@0
   881
	/* Table of variables obtained from 'ctx' */
sl@0
   882
	BIGNUM *val[TABLE_SIZE];
sl@0
   883
sl@0
   884
	if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)
sl@0
   885
		{
sl@0
   886
		/* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
sl@0
   887
		BNerr(BN_F_BN_MOD_EXP_SIMPLE,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   888
		return -1;
sl@0
   889
		}
sl@0
   890
sl@0
   891
	bits=BN_num_bits(p);
sl@0
   892
sl@0
   893
	if (bits == 0)
sl@0
   894
		{
sl@0
   895
		ret = BN_one(r);
sl@0
   896
		return ret;
sl@0
   897
		}
sl@0
   898
sl@0
   899
	BN_CTX_start(ctx);
sl@0
   900
	d = BN_CTX_get(ctx);
sl@0
   901
	val[0] = BN_CTX_get(ctx);
sl@0
   902
	if(!d || !val[0]) goto err;
sl@0
   903
sl@0
   904
	if (!BN_nnmod(val[0],a,m,ctx)) goto err;		/* 1 */
sl@0
   905
	if (BN_is_zero(val[0]))
sl@0
   906
		{
sl@0
   907
		BN_zero(r);
sl@0
   908
		ret = 1;
sl@0
   909
		goto err;
sl@0
   910
		}
sl@0
   911
sl@0
   912
	window = BN_window_bits_for_exponent_size(bits);
sl@0
   913
	if (window > 1)
sl@0
   914
		{
sl@0
   915
		if (!BN_mod_mul(d,val[0],val[0],m,ctx))
sl@0
   916
			goto err;				/* 2 */
sl@0
   917
		j=1<<(window-1);
sl@0
   918
		for (i=1; i<j; i++)
sl@0
   919
			{
sl@0
   920
			if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
sl@0
   921
					!BN_mod_mul(val[i],val[i-1],d,m,ctx))
sl@0
   922
				goto err;
sl@0
   923
			}
sl@0
   924
		}
sl@0
   925
sl@0
   926
	start=1;	/* This is used to avoid multiplication etc
sl@0
   927
			 * when there is only the value '1' in the
sl@0
   928
			 * buffer. */
sl@0
   929
	wvalue=0;	/* The 'value' of the window */
sl@0
   930
	wstart=bits-1;	/* The top bit of the window */
sl@0
   931
	wend=0;		/* The bottom bit of the window */
sl@0
   932
sl@0
   933
	if (!BN_one(r)) goto err;
sl@0
   934
sl@0
   935
	for (;;)
sl@0
   936
		{
sl@0
   937
		if (BN_is_bit_set(p,wstart) == 0)
sl@0
   938
			{
sl@0
   939
			if (!start)
sl@0
   940
				if (!BN_mod_mul(r,r,r,m,ctx))
sl@0
   941
				goto err;
sl@0
   942
			if (wstart == 0) break;
sl@0
   943
			wstart--;
sl@0
   944
			continue;
sl@0
   945
			}
sl@0
   946
		/* We now have wstart on a 'set' bit, we now need to work out
sl@0
   947
		 * how bit a window to do.  To do this we need to scan
sl@0
   948
		 * forward until the last set bit before the end of the
sl@0
   949
		 * window */
sl@0
   950
		j=wstart;
sl@0
   951
		wvalue=1;
sl@0
   952
		wend=0;
sl@0
   953
		for (i=1; i<window; i++)
sl@0
   954
			{
sl@0
   955
			if (wstart-i < 0) break;
sl@0
   956
			if (BN_is_bit_set(p,wstart-i))
sl@0
   957
				{
sl@0
   958
				wvalue<<=(i-wend);
sl@0
   959
				wvalue|=1;
sl@0
   960
				wend=i;
sl@0
   961
				}
sl@0
   962
			}
sl@0
   963
sl@0
   964
		/* wend is the size of the current window */
sl@0
   965
		j=wend+1;
sl@0
   966
		/* add the 'bytes above' */
sl@0
   967
		if (!start)
sl@0
   968
			for (i=0; i<j; i++)
sl@0
   969
				{
sl@0
   970
				if (!BN_mod_mul(r,r,r,m,ctx))
sl@0
   971
					goto err;
sl@0
   972
				}
sl@0
   973
		
sl@0
   974
		/* wvalue will be an odd number < 2^window */
sl@0
   975
		if (!BN_mod_mul(r,r,val[wvalue>>1],m,ctx))
sl@0
   976
			goto err;
sl@0
   977
sl@0
   978
		/* move the 'window' down further */
sl@0
   979
		wstart-=wend+1;
sl@0
   980
		wvalue=0;
sl@0
   981
		start=0;
sl@0
   982
		if (wstart < 0) break;
sl@0
   983
		}
sl@0
   984
	ret=1;
sl@0
   985
err:
sl@0
   986
	BN_CTX_end(ctx);
sl@0
   987
	bn_check_top(r);
sl@0
   988
	return(ret);
sl@0
   989
	}
sl@0
   990