diff options
Diffstat (limited to 'src/lib/libcrypto/dh')
-rw-r--r-- | src/lib/libcrypto/dh/dh.h | 55 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_check.c | 10 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_err.c | 18 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_gen.c | 50 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_key.c | 37 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 2 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dhtest.c | 28 |
7 files changed, 108 insertions, 92 deletions
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h index 582b34329f..0afabc7dd3 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,11 +68,14 @@ | |||
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 | ||
73 | #define OPENSSL_DH_MAX_MODULUS_BITS 10000 | 76 | #ifndef OPENSSL_DH_MAX_MODULUS_BITS |
77 | # define OPENSSL_DH_MAX_MODULUS_BITS 10000 | ||
78 | #endif | ||
74 | 79 | ||
75 | #define DH_FLAG_CACHE_MONT_P 0x01 | 80 | #define DH_FLAG_CACHE_MONT_P 0x01 |
76 | #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 |
@@ -85,9 +90,12 @@ | |||
85 | extern "C" { | 90 | extern "C" { |
86 | #endif | 91 | #endif |
87 | 92 | ||
88 | typedef struct dh_st DH; | 93 | /* Already defined in ossl_typ.h */ |
94 | /* typedef struct dh_st DH; */ | ||
95 | /* typedef struct dh_method DH_METHOD; */ | ||
89 | 96 | ||
90 | typedef struct dh_method { | 97 | struct dh_method |
98 | { | ||
91 | const char *name; | 99 | const char *name; |
92 | /* Methods here */ | 100 | /* Methods here */ |
93 | int (*generate_key)(DH *dh); | 101 | int (*generate_key)(DH *dh); |
@@ -100,7 +108,9 @@ typedef struct dh_method { | |||
100 | int (*finish)(DH *dh); | 108 | int (*finish)(DH *dh); |
101 | int flags; | 109 | int flags; |
102 | char *app_data; | 110 | char *app_data; |
103 | } 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 | }; | ||
104 | 114 | ||
105 | struct dh_st | 115 | struct dh_st |
106 | { | 116 | { |
@@ -115,7 +125,7 @@ struct dh_st | |||
115 | BIGNUM *priv_key; /* x */ | 125 | BIGNUM *priv_key; /* x */ |
116 | 126 | ||
117 | int flags; | 127 | int flags; |
118 | char *method_mont_p; | 128 | BN_MONT_CTX *method_mont_p; |
119 | /* Place holders if we want to do X9.42 DH */ | 129 | /* Place holders if we want to do X9.42 DH */ |
120 | BIGNUM *q; | 130 | BIGNUM *q; |
121 | BIGNUM *j; | 131 | BIGNUM *j; |
@@ -147,21 +157,13 @@ struct dh_st | |||
147 | this for backward compatibility: */ | 157 | this for backward compatibility: */ |
148 | #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 |
149 | 159 | ||
150 | #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) |
151 | (char *(*)())d2i_DHparams,(char *)(x)) | ||
152 | #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, \ |
153 | (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x)) | 162 | (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x)) |
154 | #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), \ |
155 | (unsigned char *)(x)) | 164 | (unsigned char *)(x)) |
156 | #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) |
157 | (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x)) | 166 | #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x) |
158 | #ifdef __cplusplus | ||
159 | #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio((int (*)())i2d_DHparams,(bp), \ | ||
160 | (unsigned char *)(x)) | ||
161 | #else | ||
162 | #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \ | ||
163 | (unsigned char *)(x)) | ||
164 | #endif | ||
165 | 167 | ||
166 | const DH_METHOD *DH_OpenSSL(void); | 168 | const DH_METHOD *DH_OpenSSL(void); |
167 | 169 | ||
@@ -178,8 +180,16 @@ int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | |||
178 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | 180 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); |
179 | int DH_set_ex_data(DH *d, int idx, void *arg); | 181 | int DH_set_ex_data(DH *d, int idx, void *arg); |
180 | void *DH_get_ex_data(DH *d, int idx); | 182 | void *DH_get_ex_data(DH *d, int idx); |
183 | |||
184 | /* Deprecated version */ | ||
185 | #ifndef OPENSSL_NO_DEPRECATED | ||
181 | DH * DH_generate_parameters(int prime_len,int generator, | 186 | DH * DH_generate_parameters(int prime_len,int generator, |
182 | 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 */ | ||
191 | int DH_generate_parameters_ex(DH *dh, int prime_len,int generator, BN_GENCB *cb); | ||
192 | |||
183 | int DH_check(const DH *dh,int *codes); | 193 | int DH_check(const DH *dh,int *codes); |
184 | int DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes); | 194 | int DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes); |
185 | int DH_generate_key(DH *dh); | 195 | int DH_generate_key(DH *dh); |
@@ -204,15 +214,18 @@ void ERR_load_DH_strings(void); | |||
204 | /* Error codes for the DH functions. */ | 214 | /* Error codes for the DH functions. */ |
205 | 215 | ||
206 | /* Function codes. */ | 216 | /* Function codes. */ |
217 | #define DH_F_COMPUTE_KEY 102 | ||
207 | #define DH_F_DHPARAMS_PRINT 100 | 218 | #define DH_F_DHPARAMS_PRINT 100 |
208 | #define DH_F_DHPARAMS_PRINT_FP 101 | 219 | #define DH_F_DHPARAMS_PRINT_FP 101 |
209 | #define DH_F_DH_COMPUTE_KEY 102 | 220 | #define DH_F_DH_BUILTIN_GENPARAMS 106 |
210 | #define DH_F_DH_GENERATE_KEY 103 | ||
211 | #define DH_F_DH_GENERATE_PARAMETERS 104 | ||
212 | #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 | ||
213 | 224 | ||
214 | /* Reason codes. */ | 225 | /* Reason codes. */ |
215 | #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 | ||
216 | #define DH_R_NO_PRIVATE_VALUE 100 | 229 | #define DH_R_NO_PRIVATE_VALUE 100 |
217 | #define DH_R_INVALID_PUBKEY 102 | 230 | #define DH_R_INVALID_PUBKEY 102 |
218 | #define DH_R_MODULUS_TOO_LARGE 103 | 231 | #define DH_R_MODULUS_TOO_LARGE 103 |
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c index 17debff62d..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 | |||
75 | int DH_check(const DH *dh, int *ret) | 73 | int 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; |
@@ -142,5 +140,3 @@ err: | |||
142 | if (q != NULL) BN_free(q); | 140 | if (q != NULL) BN_free(q); |
143 | return(ok); | 141 | return(ok); |
144 | } | 142 | } |
145 | |||
146 | #endif | ||
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c index 611067ef4a..b2361c7389 100644 --- a/src/lib/libcrypto/dh/dh_err.c +++ b/src/lib/libcrypto/dh/dh_err.c | |||
@@ -70,18 +70,21 @@ | |||
70 | 70 | ||
71 | static ERR_STRING_DATA DH_str_functs[]= | 71 | static 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 | ||
82 | static ERR_STRING_DATA DH_str_reasons[]= | 83 | static 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 | {ERR_REASON(DH_R_INVALID_PUBKEY) ,"invalid public key"}, | 89 | {ERR_REASON(DH_R_INVALID_PUBKEY) ,"invalid public key"}, |
87 | {ERR_REASON(DH_R_MODULUS_TOO_LARGE) ,"modulus too large"}, | 90 | {ERR_REASON(DH_R_MODULUS_TOO_LARGE) ,"modulus too large"}, |
@@ -92,15 +95,12 @@ static ERR_STRING_DATA DH_str_reasons[]= | |||
92 | 95 | ||
93 | void ERR_load_DH_strings(void) | 96 | void ERR_load_DH_strings(void) |
94 | { | 97 | { |
95 | static int init=1; | 98 | #ifndef OPENSSL_NO_ERR |
96 | 99 | ||
97 | if (init) | 100 | if (ERR_func_error_string(DH_str_functs[0].error) == NULL) |
98 | { | 101 | { |
99 | init=0; | ||
100 | #ifndef OPENSSL_NO_ERR | ||
101 | ERR_load_strings(0,DH_str_functs); | 102 | ERR_load_strings(0,DH_str_functs); |
102 | ERR_load_strings(0,DH_str_reasons); | 103 | ERR_load_strings(0,DH_str_reasons); |
103 | #endif | ||
104 | |||
105 | } | 104 | } |
105 | #endif | ||
106 | } | 106 | } |
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 | ||
69 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb); | ||
70 | |||
71 | int 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 | 103 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb) | |
90 | #ifndef OPENSSL_FIPS | ||
91 | |||
92 | DH *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; |
155 | err: | 162 | err: |
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 74de589204..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 | |||
67 | static int generate_key(DH *dh); | 65 | static int generate_key(DH *dh); |
68 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | 66 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); |
69 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | 67 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, |
@@ -91,6 +89,7 @@ dh_bn_mod_exp, | |||
91 | dh_init, | 89 | dh_init, |
92 | dh_finish, | 90 | dh_finish, |
93 | 0, | 91 | 0, |
92 | NULL, | ||
94 | NULL | 93 | NULL |
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; |
166 | err: | 164 | err: |
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,16 +173,16 @@ err: | |||
175 | 173 | ||
176 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | 174 | static 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; |
182 | int check_result; | 180 | int check_result; |
183 | 181 | ||
184 | if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) | 182 | if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) |
185 | { | 183 | { |
186 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE); | 184 | DHerr(DH_F_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE); |
187 | return -1; | 185 | goto err; |
188 | } | 186 | } |
189 | 187 | ||
190 | ctx = BN_CTX_new(); | 188 | ctx = BN_CTX_new(); |
@@ -194,31 +192,32 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
194 | 192 | ||
195 | if (dh->priv_key == NULL) | 193 | if (dh->priv_key == NULL) |
196 | { | 194 | { |
197 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); | 195 | DHerr(DH_F_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); |
198 | goto err; | 196 | goto err; |
199 | } | 197 | } |
200 | 198 | ||
201 | if (dh->flags & DH_FLAG_CACHE_MONT_P) | 199 | if (dh->flags & DH_FLAG_CACHE_MONT_P) |
202 | { | 200 | { |
203 | mont = BN_MONT_CTX_set_locked( | 201 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, |
204 | (BN_MONT_CTX **)&dh->method_mont_p, | ||
205 | CRYPTO_LOCK_DH, dh->p, ctx); | 202 | CRYPTO_LOCK_DH, dh->p, ctx); |
206 | if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) | 203 | if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) |
207 | { | 204 | { |
208 | /* XXX */ | 205 | /* XXX */ |
209 | BN_set_flags(dh->priv_key, BN_FLG_EXP_CONSTTIME); | 206 | BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); |
210 | } | 207 | } |
211 | if (!mont) | 208 | if (!mont) |
212 | goto err; | 209 | goto err; |
213 | } | 210 | } |
214 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) | 211 | |
212 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) | ||
215 | { | 213 | { |
216 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_INVALID_PUBKEY); | 214 | DHerr(DH_F_COMPUTE_KEY,DH_R_INVALID_PUBKEY); |
217 | goto err; | 215 | goto err; |
218 | } | 216 | } |
217 | |||
219 | 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)) |
220 | { | 219 | { |
221 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); | 220 | DHerr(DH_F_COMPUTE_KEY,ERR_R_BN_LIB); |
222 | goto err; | 221 | goto err; |
223 | } | 222 | } |
224 | 223 | ||
@@ -259,8 +258,6 @@ static int dh_init(DH *dh) | |||
259 | static int dh_finish(DH *dh) | 258 | static int dh_finish(DH *dh) |
260 | { | 259 | { |
261 | if(dh->method_mont_p) | 260 | if(dh->method_mont_p) |
262 | BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p); | 261 | BN_MONT_CTX_free(dh->method_mont_p); |
263 | return(1); | 262 | return(1); |
264 | } | 263 | } |
265 | |||
266 | #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 | ||
67 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; | 67 | const char DH_version[]="Diffie-Hellman" OPENSSL_VERSION_PTEXT; |
68 | 68 | ||
69 | static const DH_METHOD *default_DH_method = NULL; | 69 | static const DH_METHOD *default_DH_method = NULL; |
70 | 70 | ||
diff --git a/src/lib/libcrypto/dh/dhtest.c b/src/lib/libcrypto/dh/dhtest.c index b76dede771..882f5c310a 100644 --- a/src/lib/libcrypto/dh/dhtest.c +++ b/src/lib/libcrypto/dh/dhtest.c | |||
@@ -56,6 +56,12 @@ | |||
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | 58 | ||
59 | /* Until the key-gen callbacks are modified to use newer prototypes, we allow | ||
60 | * deprecated functions for openssl-internal code */ | ||
61 | #ifdef OPENSSL_NO_DEPRECATED | ||
62 | #undef OPENSSL_NO_DEPRECATED | ||
63 | #endif | ||
64 | |||
59 | #include <stdio.h> | 65 | #include <stdio.h> |
60 | #include <stdlib.h> | 66 | #include <stdlib.h> |
61 | #include <string.h> | 67 | #include <string.h> |
@@ -83,12 +89,13 @@ int main(int argc, char *argv[]) | |||
83 | #define MS_CALLBACK | 89 | #define MS_CALLBACK |
84 | #endif | 90 | #endif |
85 | 91 | ||
86 | static void MS_CALLBACK cb(int p, int n, void *arg); | 92 | static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg); |
87 | 93 | ||
88 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; | 94 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; |
89 | 95 | ||
90 | int main(int argc, char *argv[]) | 96 | int main(int argc, char *argv[]) |
91 | { | 97 | { |
98 | BN_GENCB _cb; | ||
92 | DH *a; | 99 | DH *a; |
93 | DH *b=NULL; | 100 | DH *b=NULL; |
94 | char buf[12]; | 101 | char buf[12]; |
@@ -110,8 +117,10 @@ int main(int argc, char *argv[]) | |||
110 | if (out == NULL) EXIT(1); | 117 | if (out == NULL) EXIT(1); |
111 | BIO_set_fp(out,stdout,BIO_NOCLOSE); | 118 | BIO_set_fp(out,stdout,BIO_NOCLOSE); |
112 | 119 | ||
113 | a=DH_generate_parameters(64,DH_GENERATOR_5,cb,out); | 120 | BN_GENCB_set(&_cb, &cb, out); |
114 | if (a == NULL) goto err; | 121 | if(((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 64, |
122 | DH_GENERATOR_5, &_cb)) | ||
123 | goto err; | ||
115 | 124 | ||
116 | if (!DH_check(a, &i)) goto err; | 125 | if (!DH_check(a, &i)) goto err; |
117 | if (i & DH_CHECK_P_NOT_PRIME) | 126 | if (i & DH_CHECK_P_NOT_PRIME) |
@@ -192,14 +201,14 @@ err: | |||
192 | if(b != NULL) DH_free(b); | 201 | if(b != NULL) DH_free(b); |
193 | if(a != NULL) DH_free(a); | 202 | if(a != NULL) DH_free(a); |
194 | BIO_free(out); | 203 | BIO_free(out); |
195 | CRYPTO_cleanup_all_ex_data(); | 204 | #ifdef OPENSSL_SYS_NETWARE |
196 | ERR_remove_state(0); | 205 | if (ret) printf("ERROR: %d\n", ret); |
197 | CRYPTO_mem_leaks_fp(stderr); | 206 | #endif |
198 | EXIT(ret); | 207 | EXIT(ret); |
199 | return(ret); | 208 | return(ret); |
200 | } | 209 | } |
201 | 210 | ||
202 | static void MS_CALLBACK cb(int p, int n, void *arg) | 211 | static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg) |
203 | { | 212 | { |
204 | char c='*'; | 213 | char c='*'; |
205 | 214 | ||
@@ -207,10 +216,11 @@ static void MS_CALLBACK cb(int p, int n, void *arg) | |||
207 | if (p == 1) c='+'; | 216 | if (p == 1) c='+'; |
208 | if (p == 2) c='*'; | 217 | if (p == 2) c='*'; |
209 | if (p == 3) c='\n'; | 218 | if (p == 3) c='\n'; |
210 | BIO_write((BIO *)arg,&c,1); | 219 | BIO_write(arg->arg,&c,1); |
211 | (void)BIO_flush((BIO *)arg); | 220 | (void)BIO_flush(arg->arg); |
212 | #ifdef LINT | 221 | #ifdef LINT |
213 | p=n; | 222 | p=n; |
214 | #endif | 223 | #endif |
224 | return 1; | ||
215 | } | 225 | } |
216 | #endif | 226 | #endif |