1 /* $OpenBSD: dh_lib.c,v 1.46 2024/11/29 15:59:57 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 #include <limits.h>
60 #include <stdio.h>
61
62 #include <openssl/opensslconf.h>
63
64 #include <openssl/bn.h>
65 #include <openssl/dh.h>
66 #include <openssl/err.h>
67
68 #include "dh_local.h"
69
70 static const DH_METHOD *default_DH_method = NULL;
71
72 void
DH_set_default_method(const DH_METHOD * meth)73 DH_set_default_method(const DH_METHOD *meth)
74 {
75 default_DH_method = meth;
76 }
77 LCRYPTO_ALIAS(DH_set_default_method);
78
79 const DH_METHOD *
DH_get_default_method(void)80 DH_get_default_method(void)
81 {
82 if (!default_DH_method)
83 default_DH_method = DH_OpenSSL();
84 return default_DH_method;
85 }
86 LCRYPTO_ALIAS(DH_get_default_method);
87
88 int
DH_set_method(DH * dh,const DH_METHOD * meth)89 DH_set_method(DH *dh, const DH_METHOD *meth)
90 {
91 /*
92 * NB: The caller is specifically setting a method, so it's not up to us
93 * to deal with which ENGINE it comes from.
94 */
95 const DH_METHOD *mtmp;
96
97 mtmp = dh->meth;
98 if (mtmp->finish)
99 mtmp->finish(dh);
100 dh->meth = meth;
101 if (meth->init)
102 meth->init(dh);
103 return 1;
104 }
105 LCRYPTO_ALIAS(DH_set_method);
106
107 DH *
DH_new(void)108 DH_new(void)
109 {
110 return DH_new_method(NULL);
111 }
112 LCRYPTO_ALIAS(DH_new);
113
114 DH *
DH_new_method(ENGINE * engine)115 DH_new_method(ENGINE *engine)
116 {
117 DH *dh;
118
119 if ((dh = calloc(1, sizeof(*dh))) == NULL) {
120 DHerror(ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123
124 dh->meth = DH_get_default_method();
125 dh->flags = dh->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
126 dh->references = 1;
127
128 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, dh, &dh->ex_data))
129 goto err;
130 if (dh->meth->init != NULL && !dh->meth->init(dh))
131 goto err;
132
133 return dh;
134
135 err:
136 DH_free(dh);
137
138 return NULL;
139 }
140 LCRYPTO_ALIAS(DH_new_method);
141
142 void
DH_free(DH * dh)143 DH_free(DH *dh)
144 {
145 if (dh == NULL)
146 return;
147
148 if (CRYPTO_add(&dh->references, -1, CRYPTO_LOCK_DH) > 0)
149 return;
150
151 if (dh->meth != NULL && dh->meth->finish != NULL)
152 dh->meth->finish(dh);
153
154 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, dh, &dh->ex_data);
155
156 BN_free(dh->p);
157 BN_free(dh->q);
158 BN_free(dh->g);
159 BN_free(dh->pub_key);
160 BN_free(dh->priv_key);
161 free(dh);
162 }
163 LCRYPTO_ALIAS(DH_free);
164
165 int
DH_up_ref(DH * dh)166 DH_up_ref(DH *dh)
167 {
168 return CRYPTO_add(&dh->references, 1, CRYPTO_LOCK_DH) > 1;
169 }
170 LCRYPTO_ALIAS(DH_up_ref);
171
172 int
DH_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)173 DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
174 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
175 {
176 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
177 dup_func, free_func);
178 }
179 LCRYPTO_ALIAS(DH_get_ex_new_index);
180
181 int
DH_set_ex_data(DH * dh,int idx,void * arg)182 DH_set_ex_data(DH *dh, int idx, void *arg)
183 {
184 return CRYPTO_set_ex_data(&dh->ex_data, idx, arg);
185 }
186 LCRYPTO_ALIAS(DH_set_ex_data);
187
188 void *
DH_get_ex_data(DH * dh,int idx)189 DH_get_ex_data(DH *dh, int idx)
190 {
191 return CRYPTO_get_ex_data(&dh->ex_data, idx);
192 }
193 LCRYPTO_ALIAS(DH_get_ex_data);
194
195 int
DH_size(const DH * dh)196 DH_size(const DH *dh)
197 {
198 return BN_num_bytes(dh->p);
199 }
200 LCRYPTO_ALIAS(DH_size);
201
202 int
DH_bits(const DH * dh)203 DH_bits(const DH *dh)
204 {
205 return BN_num_bits(dh->p);
206 }
207 LCRYPTO_ALIAS(DH_bits);
208
209 int
DH_security_bits(const DH * dh)210 DH_security_bits(const DH *dh)
211 {
212 int N = -1;
213
214 if (dh->q != NULL)
215 N = BN_num_bits(dh->q);
216 else if (dh->length > 0)
217 N = dh->length;
218
219 return BN_security_bits(BN_num_bits(dh->p), N);
220 }
221 LCRYPTO_ALIAS(DH_security_bits);
222
223 ENGINE *
DH_get0_engine(DH * dh)224 DH_get0_engine(DH *dh)
225 {
226 return NULL;
227 }
228 LCRYPTO_ALIAS(DH_get0_engine);
229
230 void
DH_get0_pqg(const DH * dh,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)231 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
232 {
233 if (p != NULL)
234 *p = dh->p;
235 if (q != NULL)
236 *q = dh->q;
237 if (g != NULL)
238 *g = dh->g;
239 }
240 LCRYPTO_ALIAS(DH_get0_pqg);
241
242 int
DH_set0_pqg(DH * dh,BIGNUM * p,BIGNUM * q,BIGNUM * g)243 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
244 {
245 if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
246 return 0;
247
248 if (p != NULL) {
249 BN_free(dh->p);
250 dh->p = p;
251 }
252 if (q != NULL) {
253 BN_free(dh->q);
254 dh->q = q;
255 dh->length = BN_num_bits(dh->q);
256 }
257 if (g != NULL) {
258 BN_free(dh->g);
259 dh->g = g;
260 }
261
262 return 1;
263 }
264 LCRYPTO_ALIAS(DH_set0_pqg);
265
266 void
DH_get0_key(const DH * dh,const BIGNUM ** pub_key,const BIGNUM ** priv_key)267 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
268 {
269 if (pub_key != NULL)
270 *pub_key = dh->pub_key;
271 if (priv_key != NULL)
272 *priv_key = dh->priv_key;
273 }
274 LCRYPTO_ALIAS(DH_get0_key);
275
276 int
DH_set0_key(DH * dh,BIGNUM * pub_key,BIGNUM * priv_key)277 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
278 {
279 if (pub_key != NULL) {
280 BN_free(dh->pub_key);
281 dh->pub_key = pub_key;
282 }
283 if (priv_key != NULL) {
284 BN_free(dh->priv_key);
285 dh->priv_key = priv_key;
286 }
287
288 return 1;
289 }
290 LCRYPTO_ALIAS(DH_set0_key);
291
292 const BIGNUM *
DH_get0_p(const DH * dh)293 DH_get0_p(const DH *dh)
294 {
295 return dh->p;
296 }
297 LCRYPTO_ALIAS(DH_get0_p);
298
299 const BIGNUM *
DH_get0_q(const DH * dh)300 DH_get0_q(const DH *dh)
301 {
302 return dh->q;
303 }
304 LCRYPTO_ALIAS(DH_get0_q);
305
306 const BIGNUM *
DH_get0_g(const DH * dh)307 DH_get0_g(const DH *dh)
308 {
309 return dh->g;
310 }
311 LCRYPTO_ALIAS(DH_get0_g);
312
313 const BIGNUM *
DH_get0_priv_key(const DH * dh)314 DH_get0_priv_key(const DH *dh)
315 {
316 return dh->priv_key;
317 }
318 LCRYPTO_ALIAS(DH_get0_priv_key);
319
320 const BIGNUM *
DH_get0_pub_key(const DH * dh)321 DH_get0_pub_key(const DH *dh)
322 {
323 return dh->pub_key;
324 }
325 LCRYPTO_ALIAS(DH_get0_pub_key);
326
327 void
DH_clear_flags(DH * dh,int flags)328 DH_clear_flags(DH *dh, int flags)
329 {
330 dh->flags &= ~flags;
331 }
332 LCRYPTO_ALIAS(DH_clear_flags);
333
334 int
DH_test_flags(const DH * dh,int flags)335 DH_test_flags(const DH *dh, int flags)
336 {
337 return dh->flags & flags;
338 }
339 LCRYPTO_ALIAS(DH_test_flags);
340
341 void
DH_set_flags(DH * dh,int flags)342 DH_set_flags(DH *dh, int flags)
343 {
344 dh->flags |= flags;
345 }
346 LCRYPTO_ALIAS(DH_set_flags);
347
348 long
DH_get_length(const DH * dh)349 DH_get_length(const DH *dh)
350 {
351 return dh->length;
352 }
353 LCRYPTO_ALIAS(DH_get_length);
354
355 int
DH_set_length(DH * dh,long length)356 DH_set_length(DH *dh, long length)
357 {
358 if (length < 0 || length > INT_MAX)
359 return 0;
360
361 dh->length = length;
362 return 1;
363 }
364 LCRYPTO_ALIAS(DH_set_length);
365