xref: /openbsd-src/lib/libcrypto/dh/dh_lib.c (revision e498bf2d5f803457d9476b97c3c125b2c81f4b2e)
1 /* $OpenBSD: dh_lib.c,v 1.40 2023/08/12 06:14:36 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 #ifndef OPENSSL_NO_ENGINE
69 #include <openssl/engine.h>
70 #endif
71 
72 #include "dh_local.h"
73 
74 static const DH_METHOD *default_DH_method = NULL;
75 
76 void
77 DH_set_default_method(const DH_METHOD *meth)
78 {
79 	default_DH_method = meth;
80 }
81 LCRYPTO_ALIAS(DH_set_default_method);
82 
83 const DH_METHOD *
84 DH_get_default_method(void)
85 {
86 	if (!default_DH_method)
87 		default_DH_method = DH_OpenSSL();
88 	return default_DH_method;
89 }
90 LCRYPTO_ALIAS(DH_get_default_method);
91 
92 int
93 DH_set_method(DH *dh, const DH_METHOD *meth)
94 {
95 	/*
96 	 * NB: The caller is specifically setting a method, so it's not up to us
97 	 * to deal with which ENGINE it comes from.
98 	 */
99         const DH_METHOD *mtmp;
100 
101         mtmp = dh->meth;
102         if (mtmp->finish)
103 		mtmp->finish(dh);
104 #ifndef OPENSSL_NO_ENGINE
105 	ENGINE_finish(dh->engine);
106 	dh->engine = NULL;
107 #endif
108         dh->meth = meth;
109         if (meth->init)
110 		meth->init(dh);
111         return 1;
112 }
113 LCRYPTO_ALIAS(DH_set_method);
114 
115 DH *
116 DH_new(void)
117 {
118 	return DH_new_method(NULL);
119 }
120 LCRYPTO_ALIAS(DH_new);
121 
122 DH *
123 DH_new_method(ENGINE *engine)
124 {
125 	DH *dh;
126 
127 	if ((dh = calloc(1, sizeof(*dh))) == NULL) {
128 		DHerror(ERR_R_MALLOC_FAILURE);
129 		goto err;
130 	}
131 
132 	dh->meth = DH_get_default_method();
133 	dh->flags = dh->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
134 	dh->references = 1;
135 
136 #ifndef OPENSSL_NO_ENGINE
137 	if (engine != NULL) {
138 		if (!ENGINE_init(engine)) {
139 			DHerror(ERR_R_ENGINE_LIB);
140 			goto err;
141 		}
142 		dh->engine = engine;
143 	} else
144 		dh->engine = ENGINE_get_default_DH();
145 	if (dh->engine != NULL) {
146 		if ((dh->meth = ENGINE_get_DH(dh->engine)) == NULL) {
147 			DHerror(ERR_R_ENGINE_LIB);
148 			goto err;
149 		}
150 		dh->flags = dh->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
151 	}
152 #endif
153 
154 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, dh, &dh->ex_data))
155 		goto err;
156 
157 	if (dh->meth->init != NULL && !dh->meth->init(dh))
158 		goto err;
159 
160 	return dh;
161 
162  err:
163 	DH_free(dh);
164 
165 	return NULL;
166 }
167 LCRYPTO_ALIAS(DH_new_method);
168 
169 void
170 DH_free(DH *r)
171 {
172 	int i;
173 
174 	if (r == NULL)
175 		return;
176 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
177 	if (i > 0)
178 		return;
179 
180 	if (r->meth != NULL && r->meth->finish != NULL)
181 		r->meth->finish(r);
182 #ifndef OPENSSL_NO_ENGINE
183 	ENGINE_finish(r->engine);
184 #endif
185 
186 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
187 
188 	BN_free(r->p);
189 	BN_free(r->g);
190 	BN_free(r->q);
191 	BN_free(r->j);
192 	free(r->seed);
193 	BN_free(r->counter);
194 	BN_free(r->pub_key);
195 	BN_free(r->priv_key);
196 	free(r);
197 }
198 LCRYPTO_ALIAS(DH_free);
199 
200 int
201 DH_up_ref(DH *r)
202 {
203 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
204 
205 	return i > 1 ? 1 : 0;
206 }
207 LCRYPTO_ALIAS(DH_up_ref);
208 
209 int
210 DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
211     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
212 {
213 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
214 	    dup_func, free_func);
215 }
216 LCRYPTO_ALIAS(DH_get_ex_new_index);
217 
218 int
219 DH_set_ex_data(DH *d, int idx, void *arg)
220 {
221 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
222 }
223 LCRYPTO_ALIAS(DH_set_ex_data);
224 
225 void *
226 DH_get_ex_data(DH *d, int idx)
227 {
228 	return CRYPTO_get_ex_data(&d->ex_data, idx);
229 }
230 LCRYPTO_ALIAS(DH_get_ex_data);
231 
232 int
233 DH_size(const DH *dh)
234 {
235 	return BN_num_bytes(dh->p);
236 }
237 LCRYPTO_ALIAS(DH_size);
238 
239 int
240 DH_bits(const DH *dh)
241 {
242 	return BN_num_bits(dh->p);
243 }
244 LCRYPTO_ALIAS(DH_bits);
245 
246 int
247 DH_security_bits(const DH *dh)
248 {
249 	int N = -1;
250 
251 	if (dh->q != NULL)
252 		N = BN_num_bits(dh->q);
253 	else if (dh->length > 0)
254 		N = dh->length;
255 
256 	return BN_security_bits(BN_num_bits(dh->p), N);
257 }
258 LCRYPTO_ALIAS(DH_security_bits);
259 
260 ENGINE *
261 DH_get0_engine(DH *dh)
262 {
263 	return dh->engine;
264 }
265 LCRYPTO_ALIAS(DH_get0_engine);
266 
267 void
268 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
269 {
270 	if (p != NULL)
271 		*p = dh->p;
272 	if (q != NULL)
273 		*q = dh->q;
274 	if (g != NULL)
275 		*g = dh->g;
276 }
277 LCRYPTO_ALIAS(DH_get0_pqg);
278 
279 int
280 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
281 {
282 	if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
283 		return 0;
284 
285 	if (p != NULL) {
286 		BN_free(dh->p);
287 		dh->p = p;
288 	}
289 	if (q != NULL) {
290 		BN_free(dh->q);
291 		dh->q = q;
292 		dh->length = BN_num_bits(dh->q);
293 	}
294 	if (g != NULL) {
295 		BN_free(dh->g);
296 		dh->g = g;
297 	}
298 
299 	return 1;
300 }
301 LCRYPTO_ALIAS(DH_set0_pqg);
302 
303 void
304 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
305 {
306 	if (pub_key != NULL)
307 		*pub_key = dh->pub_key;
308 	if (priv_key != NULL)
309 		*priv_key = dh->priv_key;
310 }
311 LCRYPTO_ALIAS(DH_get0_key);
312 
313 int
314 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
315 {
316 	if (pub_key != NULL) {
317 		BN_free(dh->pub_key);
318 		dh->pub_key = pub_key;
319 	}
320 	if (priv_key != NULL) {
321 		BN_free(dh->priv_key);
322 		dh->priv_key = priv_key;
323 	}
324 
325 	return 1;
326 }
327 LCRYPTO_ALIAS(DH_set0_key);
328 
329 const BIGNUM *
330 DH_get0_p(const DH *dh)
331 {
332 	return dh->p;
333 }
334 LCRYPTO_ALIAS(DH_get0_p);
335 
336 const BIGNUM *
337 DH_get0_q(const DH *dh)
338 {
339 	return dh->q;
340 }
341 LCRYPTO_ALIAS(DH_get0_q);
342 
343 const BIGNUM *
344 DH_get0_g(const DH *dh)
345 {
346 	return dh->g;
347 }
348 LCRYPTO_ALIAS(DH_get0_g);
349 
350 const BIGNUM *
351 DH_get0_priv_key(const DH *dh)
352 {
353 	return dh->priv_key;
354 }
355 LCRYPTO_ALIAS(DH_get0_priv_key);
356 
357 const BIGNUM *
358 DH_get0_pub_key(const DH *dh)
359 {
360 	return dh->pub_key;
361 }
362 LCRYPTO_ALIAS(DH_get0_pub_key);
363 
364 void
365 DH_clear_flags(DH *dh, int flags)
366 {
367 	dh->flags &= ~flags;
368 }
369 LCRYPTO_ALIAS(DH_clear_flags);
370 
371 int
372 DH_test_flags(const DH *dh, int flags)
373 {
374 	return dh->flags & flags;
375 }
376 LCRYPTO_ALIAS(DH_test_flags);
377 
378 void
379 DH_set_flags(DH *dh, int flags)
380 {
381 	dh->flags |= flags;
382 }
383 LCRYPTO_ALIAS(DH_set_flags);
384 
385 long
386 DH_get_length(const DH *dh)
387 {
388 	return dh->length;
389 }
390 LCRYPTO_ALIAS(DH_get_length);
391 
392 int
393 DH_set_length(DH *dh, long length)
394 {
395 	if (length < 0 || length > INT_MAX)
396 		return 0;
397 
398 	dh->length = length;
399 	return 1;
400 }
401 LCRYPTO_ALIAS(DH_set_length);
402