summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh
diff options
context:
space:
mode:
authordjm <>2008-09-06 12:15:56 +0000
committerdjm <>2008-09-06 12:15:56 +0000
commit5a3c0a05c7f2c5d3c584b7c8d6aec836dd724c80 (patch)
treeaba68249883aa9d2361d92eef69a81d0c4961732 /src/lib/libcrypto/dh
parentf6198d4d0ab97685dc56be2d48715ed39fcc74b9 (diff)
downloadopenbsd-5a3c0a05c7f2c5d3c584b7c8d6aec836dd724c80.tar.gz
openbsd-5a3c0a05c7f2c5d3c584b7c8d6aec836dd724c80.tar.bz2
openbsd-5a3c0a05c7f2c5d3c584b7c8d6aec836dd724c80.zip
import of OpenSSL 0.9.8h
Diffstat (limited to 'src/lib/libcrypto/dh')
-rw-r--r--src/lib/libcrypto/dh/dh.h60
-rw-r--r--src/lib/libcrypto/dh/dh_check.c30
-rw-r--r--src/lib/libcrypto/dh/dh_depr.c83
-rw-r--r--src/lib/libcrypto/dh/dh_err.c18
-rw-r--r--src/lib/libcrypto/dh/dh_gen.c50
-rw-r--r--src/lib/libcrypto/dh/dh_key.c38
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c2
7 files changed, 205 insertions, 76 deletions
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h
index 92c7481e10..ccdf35ae1c 100644
--- a/src/lib/libcrypto/dh/dh.h
+++ b/src/lib/libcrypto/dh/dh.h
@@ -59,6 +59,8 @@
59#ifndef HEADER_DH_H 59#ifndef HEADER_DH_H
60#define HEADER_DH_H 60#define HEADER_DH_H
61 61
62#include <openssl/e_os2.h>
63
62#ifdef OPENSSL_NO_DH 64#ifdef OPENSSL_NO_DH
63#error DH is disabled. 65#error DH is disabled.
64#endif 66#endif
@@ -66,10 +68,15 @@
66#ifndef OPENSSL_NO_BIO 68#ifndef OPENSSL_NO_BIO
67#include <openssl/bio.h> 69#include <openssl/bio.h>
68#endif 70#endif
69#include <openssl/bn.h>
70#include <openssl/crypto.h>
71#include <openssl/ossl_typ.h> 71#include <openssl/ossl_typ.h>
72#ifndef OPENSSL_NO_DEPRECATED
73#include <openssl/bn.h>
74#endif
72 75
76#ifndef OPENSSL_DH_MAX_MODULUS_BITS
77# define OPENSSL_DH_MAX_MODULUS_BITS 10000
78#endif
79
73#define DH_FLAG_CACHE_MONT_P 0x01 80#define DH_FLAG_CACHE_MONT_P 0x01
74#define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH 81#define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH
75 * implementation now uses constant time 82 * implementation now uses constant time
@@ -83,9 +90,12 @@
83extern "C" { 90extern "C" {
84#endif 91#endif
85 92
86typedef struct dh_st DH; 93/* Already defined in ossl_typ.h */
94/* typedef struct dh_st DH; */
95/* typedef struct dh_method DH_METHOD; */
87 96
88typedef struct dh_method { 97struct dh_method
98 {
89 const char *name; 99 const char *name;
90 /* Methods here */ 100 /* Methods here */
91 int (*generate_key)(DH *dh); 101 int (*generate_key)(DH *dh);
@@ -98,7 +108,9 @@ typedef struct dh_method {
98 int (*finish)(DH *dh); 108 int (*finish)(DH *dh);
99 int flags; 109 int flags;
100 char *app_data; 110 char *app_data;
101} DH_METHOD; 111 /* If this is non-NULL, it will be used to generate parameters */
112 int (*generate_params)(DH *dh, int prime_len, int generator, BN_GENCB *cb);
113 };
102 114
103struct dh_st 115struct dh_st
104 { 116 {
@@ -113,7 +125,7 @@ struct dh_st
113 BIGNUM *priv_key; /* x */ 125 BIGNUM *priv_key; /* x */
114 126
115 int flags; 127 int flags;
116 char *method_mont_p; 128 BN_MONT_CTX *method_mont_p;
117 /* Place holders if we want to do X9.42 DH */ 129 /* Place holders if we want to do X9.42 DH */
118 BIGNUM *q; 130 BIGNUM *q;
119 BIGNUM *j; 131 BIGNUM *j;
@@ -137,25 +149,21 @@ struct dh_st
137#define DH_UNABLE_TO_CHECK_GENERATOR 0x04 149#define DH_UNABLE_TO_CHECK_GENERATOR 0x04
138#define DH_NOT_SUITABLE_GENERATOR 0x08 150#define DH_NOT_SUITABLE_GENERATOR 0x08
139 151
152/* DH_check_pub_key error codes */
153#define DH_CHECK_PUBKEY_TOO_SMALL 0x01
154#define DH_CHECK_PUBKEY_TOO_LARGE 0x02
155
140/* primes p where (p-1)/2 is prime too are called "safe"; we define 156/* primes p where (p-1)/2 is prime too are called "safe"; we define
141 this for backward compatibility: */ 157 this for backward compatibility: */
142#define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME 158#define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME
143 159
144#define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \ 160#define DHparams_dup(x) ASN1_dup_of_const(DH,i2d_DHparams,d2i_DHparams,x)
145 (char *(*)())d2i_DHparams,(char *)(x))
146#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ 161#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
147 (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x)) 162 (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
148#define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \ 163#define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
149 (unsigned char *)(x)) 164 (unsigned char *)(x))
150#define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \ 165#define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
151 (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x)) 166#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
152#ifdef __cplusplus
153#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio((int (*)())i2d_DHparams,(bp), \
154 (unsigned char *)(x))
155#else
156#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \
157 (unsigned char *)(x))
158#endif
159 167
160const DH_METHOD *DH_OpenSSL(void); 168const DH_METHOD *DH_OpenSSL(void);
161 169
@@ -172,9 +180,18 @@ int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
172 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); 180 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
173int DH_set_ex_data(DH *d, int idx, void *arg); 181int DH_set_ex_data(DH *d, int idx, void *arg);
174void *DH_get_ex_data(DH *d, int idx); 182void *DH_get_ex_data(DH *d, int idx);
183
184/* Deprecated version */
185#ifndef OPENSSL_NO_DEPRECATED
175DH * DH_generate_parameters(int prime_len,int generator, 186DH * DH_generate_parameters(int prime_len,int generator,
176 void (*callback)(int,int,void *),void *cb_arg); 187 void (*callback)(int,int,void *),void *cb_arg);
188#endif /* !defined(OPENSSL_NO_DEPRECATED) */
189
190/* New version */
191int DH_generate_parameters_ex(DH *dh, int prime_len,int generator, BN_GENCB *cb);
192
177int DH_check(const DH *dh,int *codes); 193int DH_check(const DH *dh,int *codes);
194int DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes);
178int DH_generate_key(DH *dh); 195int DH_generate_key(DH *dh);
179int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); 196int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
180DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); 197DH * d2i_DHparams(DH **a,const unsigned char **pp, long length);
@@ -197,15 +214,18 @@ void ERR_load_DH_strings(void);
197/* Error codes for the DH functions. */ 214/* Error codes for the DH functions. */
198 215
199/* Function codes. */ 216/* Function codes. */
217#define DH_F_COMPUTE_KEY 102
200#define DH_F_DHPARAMS_PRINT 100 218#define DH_F_DHPARAMS_PRINT 100
201#define DH_F_DHPARAMS_PRINT_FP 101 219#define DH_F_DHPARAMS_PRINT_FP 101
202#define DH_F_DH_COMPUTE_KEY 102 220#define DH_F_DH_BUILTIN_GENPARAMS 106
203#define DH_F_DH_GENERATE_KEY 103
204#define DH_F_DH_GENERATE_PARAMETERS 104
205#define DH_F_DH_NEW_METHOD 105 221#define DH_F_DH_NEW_METHOD 105
222#define DH_F_GENERATE_KEY 103
223#define DH_F_GENERATE_PARAMETERS 104
206 224
207/* Reason codes. */ 225/* Reason codes. */
208#define DH_R_BAD_GENERATOR 101 226#define DH_R_BAD_GENERATOR 101
227#define DH_R_INVALID_PUBKEY 102
228#define DH_R_MODULUS_TOO_LARGE 103
209#define DH_R_NO_PRIVATE_VALUE 100 229#define DH_R_NO_PRIVATE_VALUE 100
210 230
211#ifdef __cplusplus 231#ifdef __cplusplus
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
index a7e9920efb..b846913004 100644
--- a/src/lib/libcrypto/dh/dh_check.c
+++ b/src/lib/libcrypto/dh/dh_check.c
@@ -62,7 +62,7 @@
62#include <openssl/dh.h> 62#include <openssl/dh.h>
63 63
64/* Check that p is a safe prime and 64/* Check that p is a safe prime and
65 * if g is 2, 3 or 5, check that is is a suitable generator 65 * if g is 2, 3 or 5, check that it is a suitable generator
66 * where 66 * where
67 * for 2, p mod 24 == 11 67 * for 2, p mod 24 == 11
68 * for 3, p mod 12 == 5 68 * for 3, p mod 12 == 5
@@ -70,8 +70,6 @@
70 * should hold. 70 * should hold.
71 */ 71 */
72 72
73#ifndef OPENSSL_FIPS
74
75int DH_check(const DH *dh, int *ret) 73int DH_check(const DH *dh, int *ret)
76 { 74 {
77 int ok=0; 75 int ok=0;
@@ -106,12 +104,12 @@ int DH_check(const DH *dh, int *ret)
106 else 104 else
107 *ret|=DH_UNABLE_TO_CHECK_GENERATOR; 105 *ret|=DH_UNABLE_TO_CHECK_GENERATOR;
108 106
109 if (!BN_is_prime(dh->p,BN_prime_checks,NULL,ctx,NULL)) 107 if (!BN_is_prime_ex(dh->p,BN_prime_checks,ctx,NULL))
110 *ret|=DH_CHECK_P_NOT_PRIME; 108 *ret|=DH_CHECK_P_NOT_PRIME;
111 else 109 else
112 { 110 {
113 if (!BN_rshift1(q,dh->p)) goto err; 111 if (!BN_rshift1(q,dh->p)) goto err;
114 if (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL)) 112 if (!BN_is_prime_ex(q,BN_prime_checks,ctx,NULL))
115 *ret|=DH_CHECK_P_NOT_SAFE_PRIME; 113 *ret|=DH_CHECK_P_NOT_SAFE_PRIME;
116 } 114 }
117 ok=1; 115 ok=1;
@@ -121,4 +119,24 @@ err:
121 return(ok); 119 return(ok);
122 } 120 }
123 121
124#endif 122int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
123 {
124 int ok=0;
125 BIGNUM *q=NULL;
126
127 *ret=0;
128 q=BN_new();
129 if (q == NULL) goto err;
130 BN_set_word(q,1);
131 if (BN_cmp(pub_key,q) <= 0)
132 *ret|=DH_CHECK_PUBKEY_TOO_SMALL;
133 BN_copy(q,dh->p);
134 BN_sub_word(q,1);
135 if (BN_cmp(pub_key,q) >= 0)
136 *ret|=DH_CHECK_PUBKEY_TOO_LARGE;
137
138 ok = 1;
139err:
140 if (q != NULL) BN_free(q);
141 return(ok);
142 }
diff --git a/src/lib/libcrypto/dh/dh_depr.c b/src/lib/libcrypto/dh/dh_depr.c
new file mode 100644
index 0000000000..acc05f252c
--- /dev/null
+++ b/src/lib/libcrypto/dh/dh_depr.c
@@ -0,0 +1,83 @@
1/* crypto/dh/dh_depr.c */
2/* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56
57/* This file contains deprecated functions as wrappers to the new ones */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/dh.h>
63
64static void *dummy=&dummy;
65
66#ifndef OPENSSL_NO_DEPRECATED
67DH *DH_generate_parameters(int prime_len, int generator,
68 void (*callback)(int,int,void *), void *cb_arg)
69 {
70 BN_GENCB cb;
71 DH *ret=NULL;
72
73 if((ret=DH_new()) == NULL)
74 return NULL;
75
76 BN_GENCB_set_old(&cb, callback, cb_arg);
77
78 if(DH_generate_parameters_ex(ret, prime_len, generator, &cb))
79 return ret;
80 DH_free(ret);
81 return NULL;
82 }
83#endif
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c
index 83ccb41221..a2d8196ecb 100644
--- a/src/lib/libcrypto/dh/dh_err.c
+++ b/src/lib/libcrypto/dh/dh_err.c
@@ -70,18 +70,21 @@
70 70
71static ERR_STRING_DATA DH_str_functs[]= 71static ERR_STRING_DATA DH_str_functs[]=
72 { 72 {
73{ERR_FUNC(DH_F_COMPUTE_KEY), "COMPUTE_KEY"},
73{ERR_FUNC(DH_F_DHPARAMS_PRINT), "DHparams_print"}, 74{ERR_FUNC(DH_F_DHPARAMS_PRINT), "DHparams_print"},
74{ERR_FUNC(DH_F_DHPARAMS_PRINT_FP), "DHparams_print_fp"}, 75{ERR_FUNC(DH_F_DHPARAMS_PRINT_FP), "DHparams_print_fp"},
75{ERR_FUNC(DH_F_DH_COMPUTE_KEY), "DH_compute_key"}, 76{ERR_FUNC(DH_F_DH_BUILTIN_GENPARAMS), "DH_BUILTIN_GENPARAMS"},
76{ERR_FUNC(DH_F_DH_GENERATE_KEY), "DH_generate_key"},
77{ERR_FUNC(DH_F_DH_GENERATE_PARAMETERS), "DH_generate_parameters"},
78{ERR_FUNC(DH_F_DH_NEW_METHOD), "DH_new_method"}, 77{ERR_FUNC(DH_F_DH_NEW_METHOD), "DH_new_method"},
78{ERR_FUNC(DH_F_GENERATE_KEY), "GENERATE_KEY"},
79{ERR_FUNC(DH_F_GENERATE_PARAMETERS), "GENERATE_PARAMETERS"},
79{0,NULL} 80{0,NULL}
80 }; 81 };
81 82
82static ERR_STRING_DATA DH_str_reasons[]= 83static ERR_STRING_DATA DH_str_reasons[]=
83 { 84 {
84{ERR_REASON(DH_R_BAD_GENERATOR) ,"bad generator"}, 85{ERR_REASON(DH_R_BAD_GENERATOR) ,"bad generator"},
86{ERR_REASON(DH_R_INVALID_PUBKEY) ,"invalid public key"},
87{ERR_REASON(DH_R_MODULUS_TOO_LARGE) ,"modulus too large"},
85{ERR_REASON(DH_R_NO_PRIVATE_VALUE) ,"no private value"}, 88{ERR_REASON(DH_R_NO_PRIVATE_VALUE) ,"no private value"},
86{0,NULL} 89{0,NULL}
87 }; 90 };
@@ -90,15 +93,12 @@ static ERR_STRING_DATA DH_str_reasons[]=
90 93
91void ERR_load_DH_strings(void) 94void ERR_load_DH_strings(void)
92 { 95 {
93 static int init=1; 96#ifndef OPENSSL_NO_ERR
94 97
95 if (init) 98 if (ERR_func_error_string(DH_str_functs[0].error) == NULL)
96 { 99 {
97 init=0;
98#ifndef OPENSSL_NO_ERR
99 ERR_load_strings(0,DH_str_functs); 100 ERR_load_strings(0,DH_str_functs);
100 ERR_load_strings(0,DH_str_reasons); 101 ERR_load_strings(0,DH_str_reasons);
101#endif
102
103 } 102 }
103#endif
104 } 104 }
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c
index 23777f5a16..cfd5b11868 100644
--- a/src/lib/libcrypto/dh/dh_gen.c
+++ b/src/lib/libcrypto/dh/dh_gen.c
@@ -56,11 +56,25 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59/* NB: These functions have been upgraded - the previous prototypes are in
60 * dh_depr.c as wrappers to these ones.
61 * - Geoff
62 */
63
59#include <stdio.h> 64#include <stdio.h>
60#include "cryptlib.h" 65#include "cryptlib.h"
61#include <openssl/bn.h> 66#include <openssl/bn.h>
62#include <openssl/dh.h> 67#include <openssl/dh.h>
63 68
69static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb);
70
71int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, BN_GENCB *cb)
72 {
73 if(ret->meth->generate_params)
74 return ret->meth->generate_params(ret, prime_len, generator, cb);
75 return dh_builtin_genparams(ret, prime_len, generator, cb);
76 }
77
64/* We generate DH parameters as follows 78/* We generate DH parameters as follows
65 * find a prime q which is prime_len/2 bits long. 79 * find a prime q which is prime_len/2 bits long.
66 * p=(2*q)+1 or (p-1)/2 = q 80 * p=(2*q)+1 or (p-1)/2 = q
@@ -86,29 +100,26 @@
86 * It's just as OK (and in some sense better) to use a generator of the 100 * It's just as OK (and in some sense better) to use a generator of the
87 * order-q subgroup. 101 * order-q subgroup.
88 */ 102 */
89 103static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb)
90#ifndef OPENSSL_FIPS
91
92DH *DH_generate_parameters(int prime_len, int generator,
93 void (*callback)(int,int,void *), void *cb_arg)
94 { 104 {
95 BIGNUM *p=NULL,*t1,*t2; 105 BIGNUM *t1,*t2;
96 DH *ret=NULL;
97 int g,ok= -1; 106 int g,ok= -1;
98 BN_CTX *ctx=NULL; 107 BN_CTX *ctx=NULL;
99 108
100 ret=DH_new();
101 if (ret == NULL) goto err;
102 ctx=BN_CTX_new(); 109 ctx=BN_CTX_new();
103 if (ctx == NULL) goto err; 110 if (ctx == NULL) goto err;
104 BN_CTX_start(ctx); 111 BN_CTX_start(ctx);
105 t1 = BN_CTX_get(ctx); 112 t1 = BN_CTX_get(ctx);
106 t2 = BN_CTX_get(ctx); 113 t2 = BN_CTX_get(ctx);
107 if (t1 == NULL || t2 == NULL) goto err; 114 if (t1 == NULL || t2 == NULL) goto err;
115
116 /* Make sure 'ret' has the necessary elements */
117 if(!ret->p && ((ret->p = BN_new()) == NULL)) goto err;
118 if(!ret->g && ((ret->g = BN_new()) == NULL)) goto err;
108 119
109 if (generator <= 1) 120 if (generator <= 1)
110 { 121 {
111 DHerr(DH_F_DH_GENERATE_PARAMETERS, DH_R_BAD_GENERATOR); 122 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
112 goto err; 123 goto err;
113 } 124 }
114 if (generator == DH_GENERATOR_2) 125 if (generator == DH_GENERATOR_2)
@@ -144,18 +155,14 @@ DH *DH_generate_parameters(int prime_len, int generator,
144 g=generator; 155 g=generator;
145 } 156 }
146 157
147 p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg); 158 if(!BN_generate_prime_ex(ret->p,prime_len,1,t1,t2,cb)) goto err;
148 if (p == NULL) goto err; 159 if(!BN_GENCB_call(cb, 3, 0)) goto err;
149 if (callback != NULL) callback(3,0,cb_arg);
150 ret->p=p;
151 ret->g=BN_new();
152 if (ret->g == NULL) goto err;
153 if (!BN_set_word(ret->g,g)) goto err; 160 if (!BN_set_word(ret->g,g)) goto err;
154 ok=1; 161 ok=1;
155err: 162err:
156 if (ok == -1) 163 if (ok == -1)
157 { 164 {
158 DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB); 165 DHerr(DH_F_DH_BUILTIN_GENPARAMS,ERR_R_BN_LIB);
159 ok=0; 166 ok=0;
160 } 167 }
161 168
@@ -164,12 +171,5 @@ err:
164 BN_CTX_end(ctx); 171 BN_CTX_end(ctx);
165 BN_CTX_free(ctx); 172 BN_CTX_free(ctx);
166 } 173 }
167 if (!ok && (ret != NULL)) 174 return ok;
168 {
169 DH_free(ret);
170 ret=NULL;
171 }
172 return(ret);
173 } 175 }
174
175#endif
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
index 3a39f7c8ca..e7db440342 100644
--- a/src/lib/libcrypto/dh/dh_key.c
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -62,8 +62,6 @@
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
67static int generate_key(DH *dh); 65static int generate_key(DH *dh);
68static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); 66static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
69static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, 67static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
@@ -91,6 +89,7 @@ dh_bn_mod_exp,
91dh_init, 89dh_init,
92dh_finish, 90dh_finish,
930, 910,
92NULL,
94NULL 93NULL
95}; 94};
96 95
@@ -131,8 +130,7 @@ static int generate_key(DH *dh)
131 130
132 if (dh->flags & DH_FLAG_CACHE_MONT_P) 131 if (dh->flags & DH_FLAG_CACHE_MONT_P)
133 { 132 {
134 mont = BN_MONT_CTX_set_locked( 133 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
135 (BN_MONT_CTX **)&dh->method_mont_p,
136 CRYPTO_LOCK_DH, dh->p, ctx); 134 CRYPTO_LOCK_DH, dh->p, ctx);
137 if (!mont) 135 if (!mont)
138 goto err; 136 goto err;
@@ -152,7 +150,7 @@ static int generate_key(DH *dh)
152 { 150 {
153 BN_init(&local_prk); 151 BN_init(&local_prk);
154 prk = &local_prk; 152 prk = &local_prk;
155 BN_with_flags(prk, priv_key, BN_FLG_EXP_CONSTTIME); 153 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
156 } 154 }
157 else 155 else
158 prk = priv_key; 156 prk = priv_key;
@@ -165,7 +163,7 @@ static int generate_key(DH *dh)
165 ok=1; 163 ok=1;
166err: 164err:
167 if (ok != 1) 165 if (ok != 1)
168 DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB); 166 DHerr(DH_F_GENERATE_KEY,ERR_R_BN_LIB);
169 167
170 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key); 168 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
171 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key); 169 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
@@ -175,10 +173,17 @@ err:
175 173
176static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) 174static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
177 { 175 {
178 BN_CTX *ctx; 176 BN_CTX *ctx=NULL;
179 BN_MONT_CTX *mont=NULL; 177 BN_MONT_CTX *mont=NULL;
180 BIGNUM *tmp; 178 BIGNUM *tmp;
181 int ret= -1; 179 int ret= -1;
180 int check_result;
181
182 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS)
183 {
184 DHerr(DH_F_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE);
185 goto err;
186 }
182 187
183 ctx = BN_CTX_new(); 188 ctx = BN_CTX_new();
184 if (ctx == NULL) goto err; 189 if (ctx == NULL) goto err;
@@ -187,27 +192,32 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
187 192
188 if (dh->priv_key == NULL) 193 if (dh->priv_key == NULL)
189 { 194 {
190 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); 195 DHerr(DH_F_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
191 goto err; 196 goto err;
192 } 197 }
193 198
194 if (dh->flags & DH_FLAG_CACHE_MONT_P) 199 if (dh->flags & DH_FLAG_CACHE_MONT_P)
195 { 200 {
196 mont = BN_MONT_CTX_set_locked( 201 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
197 (BN_MONT_CTX **)&dh->method_mont_p,
198 CRYPTO_LOCK_DH, dh->p, ctx); 202 CRYPTO_LOCK_DH, dh->p, ctx);
199 if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) 203 if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0)
200 { 204 {
201 /* XXX */ 205 /* XXX */
202 BN_set_flags(dh->priv_key, BN_FLG_EXP_CONSTTIME); 206 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
203 } 207 }
204 if (!mont) 208 if (!mont)
205 goto err; 209 goto err;
206 } 210 }
207 211
212 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result)
213 {
214 DHerr(DH_F_COMPUTE_KEY,DH_R_INVALID_PUBKEY);
215 goto err;
216 }
217
208 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) 218 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont))
209 { 219 {
210 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); 220 DHerr(DH_F_COMPUTE_KEY,ERR_R_BN_LIB);
211 goto err; 221 goto err;
212 } 222 }
213 223
@@ -248,8 +258,6 @@ static int dh_init(DH *dh)
248static int dh_finish(DH *dh) 258static int dh_finish(DH *dh)
249 { 259 {
250 if(dh->method_mont_p) 260 if(dh->method_mont_p)
251 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p); 261 BN_MONT_CTX_free(dh->method_mont_p);
252 return(1); 262 return(1);
253 } 263 }
254
255#endif
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
index 09965ee2ea..7aef080e7a 100644
--- a/src/lib/libcrypto/dh/dh_lib.c
+++ b/src/lib/libcrypto/dh/dh_lib.c
@@ -64,7 +64,7 @@
64#include <openssl/engine.h> 64#include <openssl/engine.h>
65#endif 65#endif
66 66
67const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; 67const char DH_version[]="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
68 68
69static const DH_METHOD *default_DH_method = NULL; 69static const DH_METHOD *default_DH_method = NULL;
70 70