summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh/dh_lib.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c209
1 files changed, 113 insertions, 96 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
index 793a8afdce..094a78041d 100644
--- a/src/lib/libcrypto/dh/dh_lib.c
+++ b/src/lib/libcrypto/dh/dh_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dh_lib.c,v 1.16 2014/07/09 11:10:50 bcook Exp $ */ 1/* $OpenBSD: dh_lib.c,v 1.17 2014/07/09 13:26:47 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -66,117 +66,121 @@
66 66
67static const DH_METHOD *default_DH_method = NULL; 67static const DH_METHOD *default_DH_method = NULL;
68 68
69void DH_set_default_method(const DH_METHOD *meth) 69void
70 { 70DH_set_default_method(const DH_METHOD *meth)
71{
71 default_DH_method = meth; 72 default_DH_method = meth;
72 } 73}
73 74
74const DH_METHOD *DH_get_default_method(void) 75const DH_METHOD *
75 { 76DH_get_default_method(void)
76 if(!default_DH_method) 77{
77 { 78 if (!default_DH_method)
78 default_DH_method = DH_OpenSSL(); 79 default_DH_method = DH_OpenSSL();
79 }
80 return default_DH_method; 80 return default_DH_method;
81 } 81}
82 82
83int DH_set_method(DH *dh, const DH_METHOD *meth) 83int
84 { 84DH_set_method(DH *dh, const DH_METHOD *meth)
85 /* NB: The caller is specifically setting a method, so it's not up to us 85{
86 * to deal with which ENGINE it comes from. */ 86 /*
87 * NB: The caller is specifically setting a method, so it's not up to us
88 * to deal with which ENGINE it comes from.
89 */
87 const DH_METHOD *mtmp; 90 const DH_METHOD *mtmp;
91
88 mtmp = dh->meth; 92 mtmp = dh->meth;
89 if (mtmp->finish) mtmp->finish(dh); 93 if (mtmp->finish)
94 mtmp->finish(dh);
90#ifndef OPENSSL_NO_ENGINE 95#ifndef OPENSSL_NO_ENGINE
91 if (dh->engine) 96 if (dh->engine) {
92 {
93 ENGINE_finish(dh->engine); 97 ENGINE_finish(dh->engine);
94 dh->engine = NULL; 98 dh->engine = NULL;
95 } 99 }
96#endif 100#endif
97 dh->meth = meth; 101 dh->meth = meth;
98 if (meth->init) meth->init(dh); 102 if (meth->init)
103 meth->init(dh);
99 return 1; 104 return 1;
100 } 105}
101 106
102DH *DH_new(void) 107DH *
103 { 108DH_new(void)
109{
104 return DH_new_method(NULL); 110 return DH_new_method(NULL);
105 } 111}
106 112
107DH *DH_new_method(ENGINE *engine) 113DH *
108 { 114DH_new_method(ENGINE *engine)
115{
109 DH *ret; 116 DH *ret;
110 117
111 ret = malloc(sizeof(DH)); 118 ret = malloc(sizeof(DH));
112 if (ret == NULL) 119 if (ret == NULL) {
113 { 120 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
114 DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); 121 return NULL;
115 return(NULL); 122 }
116 }
117 123
118 ret->meth = DH_get_default_method(); 124 ret->meth = DH_get_default_method();
119#ifndef OPENSSL_NO_ENGINE 125#ifndef OPENSSL_NO_ENGINE
120 if (engine) 126 if (engine) {
121 { 127 if (!ENGINE_init(engine)) {
122 if (!ENGINE_init(engine))
123 {
124 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); 128 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
125 free(ret); 129 free(ret);
126 return NULL; 130 return NULL;
127 }
128 ret->engine = engine;
129 } 131 }
130 else 132 ret->engine = engine;
133 } else
131 ret->engine = ENGINE_get_default_DH(); 134 ret->engine = ENGINE_get_default_DH();
132 if(ret->engine) 135 if(ret->engine) {
133 {
134 ret->meth = ENGINE_get_DH(ret->engine); 136 ret->meth = ENGINE_get_DH(ret->engine);
135 if(!ret->meth) 137 if (!ret->meth) {
136 { 138 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
137 DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
138 ENGINE_finish(ret->engine); 139 ENGINE_finish(ret->engine);
139 free(ret); 140 free(ret);
140 return NULL; 141 return NULL;
141 }
142 } 142 }
143 }
143#endif 144#endif
144 145
145 ret->pad=0; 146 ret->pad = 0;
146 ret->version=0; 147 ret->version = 0;
147 ret->p=NULL; 148 ret->p = NULL;
148 ret->g=NULL; 149 ret->g = NULL;
149 ret->length=0; 150 ret->length = 0;
150 ret->pub_key=NULL; 151 ret->pub_key = NULL;
151 ret->priv_key=NULL; 152 ret->priv_key = NULL;
152 ret->q=NULL; 153 ret->q = NULL;
153 ret->j=NULL; 154 ret->j = NULL;
154 ret->seed = NULL; 155 ret->seed = NULL;
155 ret->seedlen = 0; 156 ret->seedlen = 0;
156 ret->counter = NULL; 157 ret->counter = NULL;
157 ret->method_mont_p=NULL; 158 ret->method_mont_p=NULL;
158 ret->references = 1; 159 ret->references = 1;
159 ret->flags=ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW; 160 ret->flags = ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
160 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); 161 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
161 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 162 if (ret->meth->init != NULL && !ret->meth->init(ret)) {
162 {
163#ifndef OPENSSL_NO_ENGINE 163#ifndef OPENSSL_NO_ENGINE
164 if (ret->engine) 164 if (ret->engine)
165 ENGINE_finish(ret->engine); 165 ENGINE_finish(ret->engine);
166#endif 166#endif
167 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); 167 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
168 free(ret); 168 free(ret);
169 ret=NULL; 169 ret = NULL;
170 }
171 return(ret);
172 } 170 }
171 return ret;
172}
173 173
174void DH_free(DH *r) 174void
175 { 175DH_free(DH *r)
176{
176 int i; 177 int i;
177 if(r == NULL) return; 178
179 if (r == NULL)
180 return;
178 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); 181 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
179 if (i > 0) return; 182 if (i > 0)
183 return;
180 184
181 if (r->meth->finish) 185 if (r->meth->finish)
182 r->meth->finish(r); 186 r->meth->finish(r);
@@ -187,41 +191,54 @@ void DH_free(DH *r)
187 191
188 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); 192 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
189 193
190 if (r->p != NULL) BN_clear_free(r->p); 194 if (r->p != NULL)
191 if (r->g != NULL) BN_clear_free(r->g); 195 BN_clear_free(r->p);
192 if (r->q != NULL) BN_clear_free(r->q); 196 if (r->g != NULL)
193 if (r->j != NULL) BN_clear_free(r->j); 197 BN_clear_free(r->g);
198 if (r->q != NULL)
199 BN_clear_free(r->q);
200 if (r->j != NULL)
201 BN_clear_free(r->j);
194 free(r->seed); 202 free(r->seed);
195 if (r->counter != NULL) BN_clear_free(r->counter); 203 if (r->counter != NULL)
196 if (r->pub_key != NULL) BN_clear_free(r->pub_key); 204 BN_clear_free(r->counter);
197 if (r->priv_key != NULL) BN_clear_free(r->priv_key); 205 if (r->pub_key != NULL)
206 BN_clear_free(r->pub_key);
207 if (r->priv_key != NULL)
208 BN_clear_free(r->priv_key);
198 free(r); 209 free(r);
199 } 210}
200 211
201int DH_up_ref(DH *r) 212int
202 { 213DH_up_ref(DH *r)
214{
203 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); 215 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
204 return ((i > 1) ? 1 : 0);
205 }
206 216
207int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 217 return i > 1 ? 1 : 0;
208 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 218}
209 { 219
210 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, 220int
211 new_func, dup_func, free_func); 221DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
212 } 222 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
213 223{
214int DH_set_ex_data(DH *d, int idx, void *arg) 224 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
215 { 225 dup_func, free_func);
216 return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); 226}
217 } 227
218 228int
219void *DH_get_ex_data(DH *d, int idx) 229DH_set_ex_data(DH *d, int idx, void *arg)
220 { 230{
221 return(CRYPTO_get_ex_data(&d->ex_data,idx)); 231 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
222 } 232}
223 233
224int DH_size(const DH *dh) 234void *
225 { 235DH_get_ex_data(DH *d, int idx)
226 return(BN_num_bytes(dh->p)); 236{
227 } 237 return CRYPTO_get_ex_data(&d->ex_data, idx);
238}
239
240int
241DH_size(const DH *dh)
242{
243 return BN_num_bytes(dh->p);
244}