xref: /openbsd-src/lib/libcrypto/dh/dh_lib.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /* $OpenBSD: dh_lib.c,v 1.43 2023/11/29 21:35: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
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 *
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
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 *
108 DH_new(void)
109 {
110 	return DH_new_method(NULL);
111 }
112 LCRYPTO_ALIAS(DH_new);
113 
114 DH *
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
143 DH_free(DH *r)
144 {
145 	int i;
146 
147 	if (r == NULL)
148 		return;
149 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
150 	if (i > 0)
151 		return;
152 
153 	if (r->meth != NULL && r->meth->finish != NULL)
154 		r->meth->finish(r);
155 
156 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
157 
158 	BN_free(r->p);
159 	BN_free(r->g);
160 	BN_free(r->q);
161 	BN_free(r->j);
162 	free(r->seed);
163 	BN_free(r->counter);
164 	BN_free(r->pub_key);
165 	BN_free(r->priv_key);
166 	free(r);
167 }
168 LCRYPTO_ALIAS(DH_free);
169 
170 int
171 DH_up_ref(DH *r)
172 {
173 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
174 
175 	return i > 1 ? 1 : 0;
176 }
177 LCRYPTO_ALIAS(DH_up_ref);
178 
179 int
180 DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
181     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
182 {
183 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
184 	    dup_func, free_func);
185 }
186 LCRYPTO_ALIAS(DH_get_ex_new_index);
187 
188 int
189 DH_set_ex_data(DH *d, int idx, void *arg)
190 {
191 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
192 }
193 LCRYPTO_ALIAS(DH_set_ex_data);
194 
195 void *
196 DH_get_ex_data(DH *d, int idx)
197 {
198 	return CRYPTO_get_ex_data(&d->ex_data, idx);
199 }
200 LCRYPTO_ALIAS(DH_get_ex_data);
201 
202 int
203 DH_size(const DH *dh)
204 {
205 	return BN_num_bytes(dh->p);
206 }
207 LCRYPTO_ALIAS(DH_size);
208 
209 int
210 DH_bits(const DH *dh)
211 {
212 	return BN_num_bits(dh->p);
213 }
214 LCRYPTO_ALIAS(DH_bits);
215 
216 int
217 DH_security_bits(const DH *dh)
218 {
219 	int N = -1;
220 
221 	if (dh->q != NULL)
222 		N = BN_num_bits(dh->q);
223 	else if (dh->length > 0)
224 		N = dh->length;
225 
226 	return BN_security_bits(BN_num_bits(dh->p), N);
227 }
228 LCRYPTO_ALIAS(DH_security_bits);
229 
230 ENGINE *
231 DH_get0_engine(DH *dh)
232 {
233 	return NULL;
234 }
235 LCRYPTO_ALIAS(DH_get0_engine);
236 
237 void
238 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
239 {
240 	if (p != NULL)
241 		*p = dh->p;
242 	if (q != NULL)
243 		*q = dh->q;
244 	if (g != NULL)
245 		*g = dh->g;
246 }
247 LCRYPTO_ALIAS(DH_get0_pqg);
248 
249 int
250 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
251 {
252 	if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
253 		return 0;
254 
255 	if (p != NULL) {
256 		BN_free(dh->p);
257 		dh->p = p;
258 	}
259 	if (q != NULL) {
260 		BN_free(dh->q);
261 		dh->q = q;
262 		dh->length = BN_num_bits(dh->q);
263 	}
264 	if (g != NULL) {
265 		BN_free(dh->g);
266 		dh->g = g;
267 	}
268 
269 	return 1;
270 }
271 LCRYPTO_ALIAS(DH_set0_pqg);
272 
273 void
274 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
275 {
276 	if (pub_key != NULL)
277 		*pub_key = dh->pub_key;
278 	if (priv_key != NULL)
279 		*priv_key = dh->priv_key;
280 }
281 LCRYPTO_ALIAS(DH_get0_key);
282 
283 int
284 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
285 {
286 	if (pub_key != NULL) {
287 		BN_free(dh->pub_key);
288 		dh->pub_key = pub_key;
289 	}
290 	if (priv_key != NULL) {
291 		BN_free(dh->priv_key);
292 		dh->priv_key = priv_key;
293 	}
294 
295 	return 1;
296 }
297 LCRYPTO_ALIAS(DH_set0_key);
298 
299 const BIGNUM *
300 DH_get0_p(const DH *dh)
301 {
302 	return dh->p;
303 }
304 LCRYPTO_ALIAS(DH_get0_p);
305 
306 const BIGNUM *
307 DH_get0_q(const DH *dh)
308 {
309 	return dh->q;
310 }
311 LCRYPTO_ALIAS(DH_get0_q);
312 
313 const BIGNUM *
314 DH_get0_g(const DH *dh)
315 {
316 	return dh->g;
317 }
318 LCRYPTO_ALIAS(DH_get0_g);
319 
320 const BIGNUM *
321 DH_get0_priv_key(const DH *dh)
322 {
323 	return dh->priv_key;
324 }
325 LCRYPTO_ALIAS(DH_get0_priv_key);
326 
327 const BIGNUM *
328 DH_get0_pub_key(const DH *dh)
329 {
330 	return dh->pub_key;
331 }
332 LCRYPTO_ALIAS(DH_get0_pub_key);
333 
334 void
335 DH_clear_flags(DH *dh, int flags)
336 {
337 	dh->flags &= ~flags;
338 }
339 LCRYPTO_ALIAS(DH_clear_flags);
340 
341 int
342 DH_test_flags(const DH *dh, int flags)
343 {
344 	return dh->flags & flags;
345 }
346 LCRYPTO_ALIAS(DH_test_flags);
347 
348 void
349 DH_set_flags(DH *dh, int flags)
350 {
351 	dh->flags |= flags;
352 }
353 LCRYPTO_ALIAS(DH_set_flags);
354 
355 long
356 DH_get_length(const DH *dh)
357 {
358 	return dh->length;
359 }
360 LCRYPTO_ALIAS(DH_get_length);
361 
362 int
363 DH_set_length(DH *dh, long length)
364 {
365 	if (length < 0 || length > INT_MAX)
366 		return 0;
367 
368 	dh->length = length;
369 	return 1;
370 }
371 LCRYPTO_ALIAS(DH_set_length);
372