aboutsummaryrefslogtreecommitdiff
path: root/crypto/dh
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/dh')
-rw-r--r--crypto/dh/dh.h5
-rw-r--r--crypto/dh/dh_err.c4
-rw-r--r--crypto/dh/dh_gen.c6
-rw-r--r--crypto/dh/dh_key.c38
-rw-r--r--crypto/dh/dh_lib.c6
-rw-r--r--crypto/dh/dhtest.c1
6 files changed, 43 insertions, 17 deletions
diff --git a/crypto/dh/dh.h b/crypto/dh/dh.h
index 4cc1df2..8d60ae5 100644
--- a/crypto/dh/dh.h
+++ b/crypto/dh/dh.h
@@ -67,6 +67,8 @@ extern "C" {
#define BIGNUM char
#endif
+#define DH_FLAG_CACHE_MONT_P 0x01
+
typedef struct dh_st
{
/* This first argument is used to pick up errors when
@@ -78,6 +80,9 @@ typedef struct dh_st
int length; /* optional */
BIGNUM *pub_key; /* y */
BIGNUM *priv_key; /* x */
+
+ int flags;
+ char *method_mont_p;
} DH;
#define DH_GENERATOR_2 2
diff --git a/crypto/dh/dh_err.c b/crypto/dh/dh_err.c
index 9d5c06a..83ad45f 100644
--- a/crypto/dh/dh_err.c
+++ b/crypto/dh/dh_err.c
@@ -84,8 +84,8 @@ void ERR_load_DH_strings()
{
static int init=1;
- if (init);
- {;
+ if (init)
+ {
init=0;
#ifndef NO_ERR
ERR_load_strings(ERR_LIB_DH,DH_str_functs);
diff --git a/crypto/dh/dh_gen.c b/crypto/dh/dh_gen.c
index 04c7046..466f2bf 100644
--- a/crypto/dh/dh_gen.c
+++ b/crypto/dh/dh_gen.c
@@ -97,8 +97,8 @@ char *cb_arg;
ret=DH_new();
ctx=BN_CTX_new();
if (ctx == NULL) goto err;
- t1=ctx->bn[0];
- t2=ctx->bn[1];
+ t1= &(ctx->bn[0]);
+ t2= &(ctx->bn[1]);
ctx->tos=2;
if (generator == DH_GENERATOR_2)
@@ -126,7 +126,7 @@ char *cb_arg;
else
g=generator;
- p=BN_generate_prime(prime_len,1,t1,t2,callback,cb_arg);
+ p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg);
if (p == NULL) goto err;
if (callback != NULL) callback(3,0,cb_arg);
ret->p=p;
diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
index 7576772..fa2c960 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);
}
diff --git a/crypto/dh/dh_lib.c b/crypto/dh/dh_lib.c
index a300b38..1b4bb4e 100644
--- a/crypto/dh/dh_lib.c
+++ b/crypto/dh/dh_lib.c
@@ -61,7 +61,7 @@
#include "bn.h"
#include "dh.h"
-char *DH_version="Diffie-Hellman part of SSLeay 0.9.0b 29-Jun-1998";
+char *DH_version="Diffie-Hellman part of SSLeay 0.9.1a 06-Jul-1998";
DH *DH_new()
{
@@ -80,6 +80,8 @@ DH *DH_new()
ret->length=0;
ret->pub_key=NULL;
ret->priv_key=NULL;
+ ret->flags=DH_FLAG_CACHE_MONT_P;
+ ret->method_mont_p=NULL;
return(ret);
}
@@ -90,6 +92,8 @@ DH *r;
if (r->g != NULL) BN_clear_free(r->g);
if (r->pub_key != NULL) BN_clear_free(r->pub_key);
if (r->priv_key != NULL) BN_clear_free(r->priv_key);
+ if (r->method_mont_p != NULL)
+ BN_MONT_CTX_free((BN_MONT_CTX *)r->method_mont_p);
Free(r);
}
diff --git a/crypto/dh/dhtest.c b/crypto/dh/dhtest.c
index 488f10f..e8e1695 100644
--- a/crypto/dh/dhtest.c
+++ b/crypto/dh/dhtest.c
@@ -182,6 +182,7 @@ char *arg;
if (p == 2) c='*';
if (p == 3) c='\n';
BIO_write((BIO *)arg,&c,1);
+ BIO_flush((BIO *)arg);
#ifdef LINT
p=n;
#endif