summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh/dh_key.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dh/dh_key.c')
-rw-r--r--src/lib/libcrypto/dh/dh_key.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
index 0c7eeaf260..6915d79dcc 100644
--- a/src/lib/libcrypto/dh/dh_key.c
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -61,6 +61,7 @@
61#include <openssl/bn.h> 61#include <openssl/bn.h>
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#include <openssl/dh.h> 63#include <openssl/dh.h>
64#include <openssl/engine.h>
64 65
65static int generate_key(DH *dh); 66static int generate_key(DH *dh);
66static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh); 67static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh);
@@ -72,12 +73,12 @@ static int dh_finish(DH *dh);
72 73
73int DH_generate_key(DH *dh) 74int DH_generate_key(DH *dh)
74 { 75 {
75 return dh->meth->generate_key(dh); 76 return ENGINE_get_DH(dh->engine)->generate_key(dh);
76 } 77 }
77 78
78int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) 79int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)
79 { 80 {
80 return dh->meth->compute_key(key, pub_key, dh); 81 return ENGINE_get_DH(dh->engine)->compute_key(key, pub_key, dh);
81 } 82 }
82 83
83static DH_METHOD dh_ossl = { 84static DH_METHOD dh_ossl = {
@@ -137,8 +138,9 @@ static int generate_key(DH *dh)
137 } 138 }
138 mont=(BN_MONT_CTX *)dh->method_mont_p; 139 mont=(BN_MONT_CTX *)dh->method_mont_p;
139 140
140 if (!dh->meth->bn_mod_exp(dh, pub_key,dh->g,priv_key,dh->p,&ctx,mont)) 141 if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, pub_key, dh->g,
141 goto err; 142 priv_key,dh->p,&ctx,mont))
143 goto err;
142 144
143 dh->pub_key=pub_key; 145 dh->pub_key=pub_key;
144 dh->priv_key=priv_key; 146 dh->priv_key=priv_key;
@@ -177,7 +179,8 @@ static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)
177 } 179 }
178 180
179 mont=(BN_MONT_CTX *)dh->method_mont_p; 181 mont=(BN_MONT_CTX *)dh->method_mont_p;
180 if (!dh->meth->bn_mod_exp(dh, tmp,pub_key,dh->priv_key,dh->p,&ctx,mont)) 182 if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, tmp, pub_key,
183 dh->priv_key,dh->p,&ctx,mont))
181 { 184 {
182 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); 185 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
183 goto err; 186 goto err;
@@ -193,19 +196,26 @@ err:
193static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, 196static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
194 const BIGNUM *m, BN_CTX *ctx, 197 const BIGNUM *m, BN_CTX *ctx,
195 BN_MONT_CTX *m_ctx) 198 BN_MONT_CTX *m_ctx)
196{ 199 {
197 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); 200 if (a->top == 1)
198} 201 {
202 BN_ULONG A = a->d[0];
203 return BN_mod_exp_mont_word(r,A,p,m,ctx,m_ctx);
204 }
205 else
206 return BN_mod_exp_mont(r,a,p,m,ctx,m_ctx);
207 }
208
199 209
200static int dh_init(DH *dh) 210static int dh_init(DH *dh)
201{ 211 {
202 dh->flags |= DH_FLAG_CACHE_MONT_P; 212 dh->flags |= DH_FLAG_CACHE_MONT_P;
203 return(1); 213 return(1);
204} 214 }
205 215
206static int dh_finish(DH *dh) 216static int dh_finish(DH *dh)
207{ 217 {
208 if(dh->method_mont_p) 218 if(dh->method_mont_p)
209 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p); 219 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p);
210 return(1); 220 return(1);
211} 221 }