xref: /openbsd-src/lib/libcrypto/dh/dh_lib.c (revision d6d0b2f33c509ffa37804da3ebac9d6b3c0c3818)
1 /* $OpenBSD: dh_lib.c,v 1.39 2023/07/08 15:29:03 beck 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 *ret;
126 
127 	ret = malloc(sizeof(DH));
128 	if (ret == NULL) {
129 		DHerror(ERR_R_MALLOC_FAILURE);
130 		return NULL;
131 	}
132 
133 	ret->meth = DH_get_default_method();
134 #ifndef OPENSSL_NO_ENGINE
135 	if (engine) {
136 		if (!ENGINE_init(engine)) {
137 			DHerror(ERR_R_ENGINE_LIB);
138 			free(ret);
139 			return NULL;
140 		}
141 		ret->engine = engine;
142 	} else
143 		ret->engine = ENGINE_get_default_DH();
144 	if(ret->engine) {
145 		ret->meth = ENGINE_get_DH(ret->engine);
146 		if (ret->meth == NULL) {
147 			DHerror(ERR_R_ENGINE_LIB);
148 			ENGINE_finish(ret->engine);
149 			free(ret);
150 			return NULL;
151 		}
152 	}
153 #endif
154 
155 	ret->pad = 0;
156 	ret->version = 0;
157 	ret->p = NULL;
158 	ret->g = NULL;
159 	ret->length = 0;
160 	ret->pub_key = NULL;
161 	ret->priv_key = NULL;
162 	ret->q = NULL;
163 	ret->j = NULL;
164 	ret->seed = NULL;
165 	ret->seedlen = 0;
166 	ret->counter = NULL;
167 	ret->method_mont_p=NULL;
168 	ret->references = 1;
169 	ret->flags = ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
170 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
171 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
172 #ifndef OPENSSL_NO_ENGINE
173 		ENGINE_finish(ret->engine);
174 #endif
175 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
176 		free(ret);
177 		ret = NULL;
178 	}
179 	return ret;
180 }
181 LCRYPTO_ALIAS(DH_new_method);
182 
183 void
184 DH_free(DH *r)
185 {
186 	int i;
187 
188 	if (r == NULL)
189 		return;
190 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
191 	if (i > 0)
192 		return;
193 
194 	if (r->meth->finish)
195 		r->meth->finish(r);
196 #ifndef OPENSSL_NO_ENGINE
197 	ENGINE_finish(r->engine);
198 #endif
199 
200 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
201 
202 	BN_free(r->p);
203 	BN_free(r->g);
204 	BN_free(r->q);
205 	BN_free(r->j);
206 	free(r->seed);
207 	BN_free(r->counter);
208 	BN_free(r->pub_key);
209 	BN_free(r->priv_key);
210 	free(r);
211 }
212 LCRYPTO_ALIAS(DH_free);
213 
214 int
215 DH_up_ref(DH *r)
216 {
217 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
218 
219 	return i > 1 ? 1 : 0;
220 }
221 LCRYPTO_ALIAS(DH_up_ref);
222 
223 int
224 DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
225     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
226 {
227 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
228 	    dup_func, free_func);
229 }
230 LCRYPTO_ALIAS(DH_get_ex_new_index);
231 
232 int
233 DH_set_ex_data(DH *d, int idx, void *arg)
234 {
235 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
236 }
237 LCRYPTO_ALIAS(DH_set_ex_data);
238 
239 void *
240 DH_get_ex_data(DH *d, int idx)
241 {
242 	return CRYPTO_get_ex_data(&d->ex_data, idx);
243 }
244 LCRYPTO_ALIAS(DH_get_ex_data);
245 
246 int
247 DH_size(const DH *dh)
248 {
249 	return BN_num_bytes(dh->p);
250 }
251 LCRYPTO_ALIAS(DH_size);
252 
253 int
254 DH_bits(const DH *dh)
255 {
256 	return BN_num_bits(dh->p);
257 }
258 LCRYPTO_ALIAS(DH_bits);
259 
260 int
261 DH_security_bits(const DH *dh)
262 {
263 	int N = -1;
264 
265 	if (dh->q != NULL)
266 		N = BN_num_bits(dh->q);
267 	else if (dh->length > 0)
268 		N = dh->length;
269 
270 	return BN_security_bits(BN_num_bits(dh->p), N);
271 }
272 LCRYPTO_ALIAS(DH_security_bits);
273 
274 ENGINE *
275 DH_get0_engine(DH *dh)
276 {
277 	return dh->engine;
278 }
279 LCRYPTO_ALIAS(DH_get0_engine);
280 
281 void
282 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
283 {
284 	if (p != NULL)
285 		*p = dh->p;
286 	if (q != NULL)
287 		*q = dh->q;
288 	if (g != NULL)
289 		*g = dh->g;
290 }
291 LCRYPTO_ALIAS(DH_get0_pqg);
292 
293 int
294 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
295 {
296 	if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
297 		return 0;
298 
299 	if (p != NULL) {
300 		BN_free(dh->p);
301 		dh->p = p;
302 	}
303 	if (q != NULL) {
304 		BN_free(dh->q);
305 		dh->q = q;
306 		dh->length = BN_num_bits(dh->q);
307 	}
308 	if (g != NULL) {
309 		BN_free(dh->g);
310 		dh->g = g;
311 	}
312 
313 	return 1;
314 }
315 LCRYPTO_ALIAS(DH_set0_pqg);
316 
317 void
318 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
319 {
320 	if (pub_key != NULL)
321 		*pub_key = dh->pub_key;
322 	if (priv_key != NULL)
323 		*priv_key = dh->priv_key;
324 }
325 LCRYPTO_ALIAS(DH_get0_key);
326 
327 int
328 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
329 {
330 	if (pub_key != NULL) {
331 		BN_free(dh->pub_key);
332 		dh->pub_key = pub_key;
333 	}
334 	if (priv_key != NULL) {
335 		BN_free(dh->priv_key);
336 		dh->priv_key = priv_key;
337 	}
338 
339 	return 1;
340 }
341 LCRYPTO_ALIAS(DH_set0_key);
342 
343 const BIGNUM *
344 DH_get0_p(const DH *dh)
345 {
346 	return dh->p;
347 }
348 LCRYPTO_ALIAS(DH_get0_p);
349 
350 const BIGNUM *
351 DH_get0_q(const DH *dh)
352 {
353 	return dh->q;
354 }
355 LCRYPTO_ALIAS(DH_get0_q);
356 
357 const BIGNUM *
358 DH_get0_g(const DH *dh)
359 {
360 	return dh->g;
361 }
362 LCRYPTO_ALIAS(DH_get0_g);
363 
364 const BIGNUM *
365 DH_get0_priv_key(const DH *dh)
366 {
367 	return dh->priv_key;
368 }
369 LCRYPTO_ALIAS(DH_get0_priv_key);
370 
371 const BIGNUM *
372 DH_get0_pub_key(const DH *dh)
373 {
374 	return dh->pub_key;
375 }
376 LCRYPTO_ALIAS(DH_get0_pub_key);
377 
378 void
379 DH_clear_flags(DH *dh, int flags)
380 {
381 	dh->flags &= ~flags;
382 }
383 LCRYPTO_ALIAS(DH_clear_flags);
384 
385 int
386 DH_test_flags(const DH *dh, int flags)
387 {
388 	return dh->flags & flags;
389 }
390 LCRYPTO_ALIAS(DH_test_flags);
391 
392 void
393 DH_set_flags(DH *dh, int flags)
394 {
395 	dh->flags |= flags;
396 }
397 LCRYPTO_ALIAS(DH_set_flags);
398 
399 long
400 DH_get_length(const DH *dh)
401 {
402 	return dh->length;
403 }
404 LCRYPTO_ALIAS(DH_get_length);
405 
406 int
407 DH_set_length(DH *dh, long length)
408 {
409 	if (length < 0 || length > INT_MAX)
410 		return 0;
411 
412 	dh->length = length;
413 	return 1;
414 }
415 LCRYPTO_ALIAS(DH_set_length);
416