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.c37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
index e7db440342..74de589204 100644
--- a/src/lib/libcrypto/dh/dh_key.c
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -62,6 +62,8 @@
62#include <openssl/rand.h> 62#include <openssl/rand.h>
63#include <openssl/dh.h> 63#include <openssl/dh.h>
64 64
65#ifndef OPENSSL_FIPS
66
65static int generate_key(DH *dh); 67static int generate_key(DH *dh);
66static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); 68static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
67static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, 69static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
@@ -89,7 +91,6 @@ dh_bn_mod_exp,
89dh_init, 91dh_init,
90dh_finish, 92dh_finish,
910, 930,
92NULL,
93NULL 94NULL
94}; 95};
95 96
@@ -130,7 +131,8 @@ static int generate_key(DH *dh)
130 131
131 if (dh->flags & DH_FLAG_CACHE_MONT_P) 132 if (dh->flags & DH_FLAG_CACHE_MONT_P)
132 { 133 {
133 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, 134 mont = BN_MONT_CTX_set_locked(
135 (BN_MONT_CTX **)&dh->method_mont_p,
134 CRYPTO_LOCK_DH, dh->p, ctx); 136 CRYPTO_LOCK_DH, dh->p, ctx);
135 if (!mont) 137 if (!mont)
136 goto err; 138 goto err;
@@ -150,7 +152,7 @@ static int generate_key(DH *dh)
150 { 152 {
151 BN_init(&local_prk); 153 BN_init(&local_prk);
152 prk = &local_prk; 154 prk = &local_prk;
153 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); 155 BN_with_flags(prk, priv_key, BN_FLG_EXP_CONSTTIME);
154 } 156 }
155 else 157 else
156 prk = priv_key; 158 prk = priv_key;
@@ -163,7 +165,7 @@ static int generate_key(DH *dh)
163 ok=1; 165 ok=1;
164err: 166err:
165 if (ok != 1) 167 if (ok != 1)
166 DHerr(DH_F_GENERATE_KEY,ERR_R_BN_LIB); 168 DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);
167 169
168 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key); 170 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
169 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key); 171 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
@@ -173,16 +175,16 @@ err:
173 175
174static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) 176static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
175 { 177 {
176 BN_CTX *ctx=NULL; 178 BN_CTX *ctx;
177 BN_MONT_CTX *mont=NULL; 179 BN_MONT_CTX *mont=NULL;
178 BIGNUM *tmp; 180 BIGNUM *tmp;
179 int ret= -1; 181 int ret= -1;
180 int check_result; 182 int check_result;
181 183
182 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) 184 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS)
183 { 185 {
184 DHerr(DH_F_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE); 186 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE);
185 goto err; 187 return -1;
186 } 188 }
187 189
188 ctx = BN_CTX_new(); 190 ctx = BN_CTX_new();
@@ -192,32 +194,31 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
192 194
193 if (dh->priv_key == NULL) 195 if (dh->priv_key == NULL)
194 { 196 {
195 DHerr(DH_F_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); 197 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
196 goto err; 198 goto err;
197 } 199 }
198 200
199 if (dh->flags & DH_FLAG_CACHE_MONT_P) 201 if (dh->flags & DH_FLAG_CACHE_MONT_P)
200 { 202 {
201 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, 203 mont = BN_MONT_CTX_set_locked(
204 (BN_MONT_CTX **)&dh->method_mont_p,
202 CRYPTO_LOCK_DH, dh->p, ctx); 205 CRYPTO_LOCK_DH, dh->p, ctx);
203 if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) 206 if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0)
204 { 207 {
205 /* XXX */ 208 /* XXX */
206 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); 209 BN_set_flags(dh->priv_key, BN_FLG_EXP_CONSTTIME);
207 } 210 }
208 if (!mont) 211 if (!mont)
209 goto err; 212 goto err;
210 } 213 }
211 214 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result)
212 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result)
213 { 215 {
214 DHerr(DH_F_COMPUTE_KEY,DH_R_INVALID_PUBKEY); 216 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_INVALID_PUBKEY);
215 goto err; 217 goto err;
216 } 218 }
217
218 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) 219 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont))
219 { 220 {
220 DHerr(DH_F_COMPUTE_KEY,ERR_R_BN_LIB); 221 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
221 goto err; 222 goto err;
222 } 223 }
223 224
@@ -258,6 +259,8 @@ static int dh_init(DH *dh)
258static int dh_finish(DH *dh) 259static int dh_finish(DH *dh)
259 { 260 {
260 if(dh->method_mont_p) 261 if(dh->method_mont_p)
261 BN_MONT_CTX_free(dh->method_mont_p); 262 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p);
262 return(1); 263 return(1);
263 } 264 }
265
266#endif