bn_mont.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * All rights reserved.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * This package is an SSL implementation written
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * by Eric Young (eay@cryptsoft.com).
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * The implementation was written so as to conform with Netscapes SSL.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * This library is free for commercial and non-commercial use as long as
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * the following conditions are aheared to. The following conditions
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * apply to all code found in this distribution, be it the RC4, RSA,
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * lhash, DES, etc., code; not just the SSL code. The SSL documentation
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * included with this distribution is covered by the same copyright terms
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * except that the holder is Tim Hudson (tjh@cryptsoft.com).
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * Copyright remains Eric Young's, and as such any Copyright notices in
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * the code are not to be removed.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * If this package is used in a product, Eric Young should be given attribution
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * as the author of the parts of the library used.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * This can be in the form of a textual message at program startup or
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * in documentation (online or textual) provided with the package.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * Redistribution and use in source and binary forms, with or without
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * modification, are permitted provided that the following conditions
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * 1. Redistributions of source code must retain the copyright
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * notice, this list of conditions and the following disclaimer.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * 2. Redistributions in binary form must reproduce the above copyright
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * notice, this list of conditions and the following disclaimer in the
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * documentation and/or other materials provided with the distribution.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * 3. All advertising materials mentioning features or use of this software
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * must display the following acknowledgement:
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * "This product includes cryptographic software written by
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * Eric Young (eay@cryptsoft.com)"
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * The word 'cryptographic' can be left out if the rouines from the library
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * being used are not cryptographic related :-).
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * 4. If you include any Windows specific code (or a derivative thereof) from
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * the apps directory (application code) you must include an acknowledgement:
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * SUCH DAMAGE.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * The licence and distribution terms for any publically available version or
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * derivative of this code cannot be changed. i.e. this code cannot simply be
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * copied and put under another distribution licence
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * [including the GNU Public Licence.]
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * Details about Montgomery multiplication algorithms can be found at
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * http://security.ece.orst.edu/publications.html, e.g.
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * http://security.ece.orst.edu/koc/papers/j37acmon.pdf and
c7158ae983f5a04c4a998f468ecefba6d23ba721tariq * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf
#include <stdio.h>
#include "cryptlib.h"
#include "bn_lcl.h"
int ret=0;
bn_check_top(r);
err:
return(ret);
int retn=0;
#ifdef MONT_WORD
BIGNUM *n,*r;
n= &(mont->N);
ap=a->d;
np=n->d;
rp=r->d;
#ifdef BN_COUNT
for (i=0; i<nl; i++)
#ifdef __TANDEM
long long t1;
long long t2;
long long t3;
nrp++;
rp++;
bn_correct_top(r);
x=ri;
ap= &(r->d[x]);
if (r->top < x)
al=0;
for (; i<al; i++)
err:
return(retn);
return(NULL);
return(ret);
int ret = 0;
#ifdef MONT_WORD
BN_zero(R);
goto err;
BN_zero(R);
goto err;
err:
return ret;
return(to);
if (*pmont)
return *pmont;
if (!*pmont)
return *pmont;