summaryrefslogtreecommitdiff
path: root/crypto/dh/dh_key.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/dh/dh_key.c')
-rw-r--r--crypto/dh/dh_key.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
index 7576772bcd..fa2c96082b 100644
--- a/crypto/dh/dh_key.c
+++ b/crypto/dh/dh_key.c
@@ -67,11 +67,11 @@ DH *dh;
{
int ok=0;
unsigned int i;
- BN_CTX *ctx=NULL;
+ BN_CTX ctx;
+ BN_MONT_CTX *mont;
BIGNUM *pub_key=NULL,*priv_key=NULL;
- ctx=BN_CTX_new();
- if (ctx == NULL) goto err;
+ BN_CTX_init(&ctx);
if (dh->priv_key == NULL)
{
@@ -96,7 +96,15 @@ DH *dh;
else
pub_key=dh->pub_key;
- if (!BN_mod_exp(pub_key,dh->g,priv_key,dh->p,ctx)) goto err;
+ if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
+ {
+ if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
+ if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
+ dh->p,&ctx)) goto err;
+ }
+ mont=(BN_MONT_CTX *)dh->method_mont_p;
+
+ if (!BN_mod_exp_mont(pub_key,dh->g,priv_key,dh->p,&ctx,mont)) goto err;
dh->pub_key=pub_key;
dh->priv_key=priv_key;
@@ -107,7 +115,7 @@ err:
if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
- if (ctx != NULL) BN_CTX_free(ctx);
+ BN_CTX_free(&ctx);
return(ok);
}
@@ -116,20 +124,28 @@ unsigned char *key;
BIGNUM *pub_key;
DH *dh;
{
- BN_CTX *ctx;
+ BN_CTX ctx;
+ BN_MONT_CTX *mont;
BIGNUM *tmp;
int ret= -1;
- ctx=BN_CTX_new();
- if (ctx == NULL) goto err;
- tmp=ctx->bn[ctx->tos++];
+ BN_CTX_init(&ctx);
+ tmp= &(ctx.bn[ctx.tos++]);
if (dh->priv_key == NULL)
{
DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
goto err;
}
- if (!BN_mod_exp(tmp,pub_key,dh->priv_key,dh->p,ctx))
+ if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
+ {
+ if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
+ if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
+ dh->p,&ctx)) goto err;
+ }
+
+ mont=(BN_MONT_CTX *)dh->method_mont_p;
+ if (!BN_mod_exp_mont(tmp,pub_key,dh->priv_key,dh->p,&ctx,mont))
{
DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
goto err;
@@ -137,6 +153,6 @@ DH *dh;
ret=BN_bn2bin(tmp,key);
err:
- if (ctx != NULL) BN_CTX_free(ctx);
+ BN_CTX_free(&ctx);
return(ret);
}