summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh/dh_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dh/dh_lib.c')
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c364
1 files changed, 0 insertions, 364 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
deleted file mode 100644
index 803aca6421..0000000000
--- a/src/lib/libcrypto/dh/dh_lib.c
+++ /dev/null
@@ -1,364 +0,0 @@
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
70static const DH_METHOD *default_DH_method = NULL;
71
72void
73DH_set_default_method(const DH_METHOD *meth)
74{
75 default_DH_method = meth;
76}
77LCRYPTO_ALIAS(DH_set_default_method);
78
79const DH_METHOD *
80DH_get_default_method(void)
81{
82 if (!default_DH_method)
83 default_DH_method = DH_OpenSSL();
84 return default_DH_method;
85}
86LCRYPTO_ALIAS(DH_get_default_method);
87
88int
89DH_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}
105LCRYPTO_ALIAS(DH_set_method);
106
107DH *
108DH_new(void)
109{
110 return DH_new_method(NULL);
111}
112LCRYPTO_ALIAS(DH_new);
113
114DH *
115DH_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}
140LCRYPTO_ALIAS(DH_new_method);
141
142void
143DH_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}
163LCRYPTO_ALIAS(DH_free);
164
165int
166DH_up_ref(DH *dh)
167{
168 return CRYPTO_add(&dh->references, 1, CRYPTO_LOCK_DH) > 1;
169}
170LCRYPTO_ALIAS(DH_up_ref);
171
172int
173DH_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}
179LCRYPTO_ALIAS(DH_get_ex_new_index);
180
181int
182DH_set_ex_data(DH *dh, int idx, void *arg)
183{
184 return CRYPTO_set_ex_data(&dh->ex_data, idx, arg);
185}
186LCRYPTO_ALIAS(DH_set_ex_data);
187
188void *
189DH_get_ex_data(DH *dh, int idx)
190{
191 return CRYPTO_get_ex_data(&dh->ex_data, idx);
192}
193LCRYPTO_ALIAS(DH_get_ex_data);
194
195int
196DH_size(const DH *dh)
197{
198 return BN_num_bytes(dh->p);
199}
200LCRYPTO_ALIAS(DH_size);
201
202int
203DH_bits(const DH *dh)
204{
205 return BN_num_bits(dh->p);
206}
207LCRYPTO_ALIAS(DH_bits);
208
209int
210DH_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}
221LCRYPTO_ALIAS(DH_security_bits);
222
223ENGINE *
224DH_get0_engine(DH *dh)
225{
226 return NULL;
227}
228LCRYPTO_ALIAS(DH_get0_engine);
229
230void
231DH_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}
240LCRYPTO_ALIAS(DH_get0_pqg);
241
242int
243DH_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}
264LCRYPTO_ALIAS(DH_set0_pqg);
265
266void
267DH_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}
274LCRYPTO_ALIAS(DH_get0_key);
275
276int
277DH_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}
290LCRYPTO_ALIAS(DH_set0_key);
291
292const BIGNUM *
293DH_get0_p(const DH *dh)
294{
295 return dh->p;
296}
297LCRYPTO_ALIAS(DH_get0_p);
298
299const BIGNUM *
300DH_get0_q(const DH *dh)
301{
302 return dh->q;
303}
304LCRYPTO_ALIAS(DH_get0_q);
305
306const BIGNUM *
307DH_get0_g(const DH *dh)
308{
309 return dh->g;
310}
311LCRYPTO_ALIAS(DH_get0_g);
312
313const BIGNUM *
314DH_get0_priv_key(const DH *dh)
315{
316 return dh->priv_key;
317}
318LCRYPTO_ALIAS(DH_get0_priv_key);
319
320const BIGNUM *
321DH_get0_pub_key(const DH *dh)
322{
323 return dh->pub_key;
324}
325LCRYPTO_ALIAS(DH_get0_pub_key);
326
327void
328DH_clear_flags(DH *dh, int flags)
329{
330 dh->flags &= ~flags;
331}
332LCRYPTO_ALIAS(DH_clear_flags);
333
334int
335DH_test_flags(const DH *dh, int flags)
336{
337 return dh->flags & flags;
338}
339LCRYPTO_ALIAS(DH_test_flags);
340
341void
342DH_set_flags(DH *dh, int flags)
343{
344 dh->flags |= flags;
345}
346LCRYPTO_ALIAS(DH_set_flags);
347
348long
349DH_get_length(const DH *dh)
350{
351 return dh->length;
352}
353LCRYPTO_ALIAS(DH_get_length);
354
355int
356DH_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}
364LCRYPTO_ALIAS(DH_set_length);