bn_exp.c revision 6b7257f756eb0530cdf54df9a7fab8d51a5001c3
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * All rights reserved.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * This package is an SSL implementation written
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * by Eric Young (eay@cryptsoft.com).
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * The implementation was written so as to conform with Netscapes SSL.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * This library is free for commercial and non-commercial use as long as
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * the following conditions are aheared to. The following conditions
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * apply to all code found in this distribution, be it the RC4, RSA,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * lhash, DES, etc., code; not just the SSL code. The SSL documentation
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * included with this distribution is covered by the same copyright terms
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * except that the holder is Tim Hudson (tjh@cryptsoft.com).
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * Copyright remains Eric Young's, and as such any Copyright notices in
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * the code are not to be removed.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * If this package is used in a product, Eric Young should be given attribution
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * as the author of the parts of the library used.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * This can be in the form of a textual message at program startup or
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * in documentation (online or textual) provided with the package.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * Redistribution and use in source and binary forms, with or without
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * modification, are permitted provided that the following conditions
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley * 1. Redistributions of source code must retain the copyright
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * notice, this list of conditions and the following disclaimer.
f73e404eb9e1d314f85128cae48cdee036b405ecAndreas Gustafsson * 2. Redistributions in binary form must reproduce the above copyright
f73e404eb9e1d314f85128cae48cdee036b405ecAndreas Gustafsson * notice, this list of conditions and the following disclaimer in the
7df0472d8a76c83d2137bd9549414a30787d58cfBrian Wellington * documentation and/or other materials provided with the distribution.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * 3. All advertising materials mentioning features or use of this software
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * must display the following acknowledgement:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * "This product includes cryptographic software written by
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * Eric Young (eay@cryptsoft.com)"
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * The word 'cryptographic' can be left out if the rouines from the library
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * being used are not cryptographic related :-).
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * 4. If you include any Windows specific code (or a derivative thereof) from
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * the apps directory (application code) you must include an acknowledgement:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
941f41d6139c868e357892869c94c4693b943373Bob Halley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * SUCH DAMAGE.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * The licence and distribution terms for any publically available version or
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * derivative of this code cannot be changed. i.e. this code cannot simply be
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * copied and put under another distribution licence
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * [including the GNU Public Licence.]
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/* slow but works */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyint BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/* this one works - simple but works */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyint BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/* this one works - simple but works */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyint BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley if ((r == a) || (r == p))
return(ret);
#ifdef ATALLA
unsigned int *ret_buf);
unsigned char *output,
unsigned char *input,
unsigned int modulus_len);
static int asi_accelerator_present;
static int tried_atalla;
void atalla_initialize_accelerator_handle(void)
void *dl_handle;
int status;
static int tested;
if(tested)
/* printf("atasi.so library is not present on the system\n");
if (status != 0)
unsigned char *abin;
unsigned char *pbin;
unsigned char *mbin;
unsigned char *rbin;
if(!ret)
int ret;
bn_check_top(a);
bn_check_top(p);
bn_check_top(m);
#ifdef ATALLA
if(BN_mod_exp_atalla(r,a,p,m))
#ifdef MONT_MUL_MOD
if (BN_is_odd(m))
#ifdef RECP_MUL_MOD
#ifdef ATALLA
tried_atalla=0;
return(ret);
if (bits == 0)
BN_one(r);
goto err;
ts=i;
if (!start)
goto err;
if (wstart == 0) break;
wstart--;
j=wstart;
wend=0;
if (wstart-i < 0) break;
wend=i;
if (!start)
goto err;
goto err;
wvalue=0;
start=0;
if (wstart < 0) break;
err:
for (i=0; i<ts; i++)
return(ret);
BIGNUM *d,*r;
bn_check_top(a);
bn_check_top(p);
bn_check_top(m);
#ifdef ATALLA
if (bits == 0)
if (BN_ucmp(a,m) >= 0)
aa=a;
goto err;
ts=i;
if (!start)
goto err;
if (wstart == 0) break;
wstart--;
j=wstart;
wend=0;
if (wstart-i < 0) break;
wend=i;
if (!start)
goto err;
goto err;
wvalue=0;
start=0;
if (wstart < 0) break;
err:
for (i=0; i<ts; i++)
return(ret);
BIGNUM *d;
if (bits == 0)
BN_one(r);
goto err;
ts=i;
if (!start)
goto err;
if (wstart == 0) break;
wstart--;
j=wstart;
wend=0;
if (wstart-i < 0) break;
wend=i;
if (!start)
goto err;
goto err;
wvalue=0;
start=0;
if (wstart < 0) break;
err:
for (i=0; i<ts; i++)
return(ret);