]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_lib.c
e6fc3ef2c565210124d9216f71e14fbb4502265c
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <openssl/bn.h>
12 #include <openssl/engine.h>
13 #include <openssl/obj_mac.h>
14 #include "internal/cryptlib.h"
15 #include "internal/refcount.h"
16 #include "crypto/dh.h"
17 #include "dh_local.h"
18
19 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
20
21 #ifndef FIPS_MODE
22 int DH_set_method(DH *dh, const DH_METHOD *meth)
23 {
24 /*
25 * NB: The caller is specifically setting a method, so it's not up to us
26 * to deal with which ENGINE it comes from.
27 */
28 const DH_METHOD *mtmp;
29 mtmp = dh->meth;
30 if (mtmp->finish)
31 mtmp->finish(dh);
32 #ifndef OPENSSL_NO_ENGINE
33 ENGINE_finish(dh->engine);
34 dh->engine = NULL;
35 #endif
36 dh->meth = meth;
37 if (meth->init)
38 meth->init(dh);
39 return 1;
40 }
41
42 DH *DH_new(void)
43 {
44 return dh_new_intern(NULL, NULL);
45 }
46
47 DH *DH_new_method(ENGINE *engine)
48 {
49 return dh_new_intern(engine, NULL);
50 }
51 #endif /* !FIPS_MODE */
52
53 DH *dh_new_with_ctx(OPENSSL_CTX *libctx)
54 {
55 return dh_new_intern(NULL, libctx);
56 }
57
58 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
59 {
60 DH *ret = OPENSSL_zalloc(sizeof(*ret));
61
62 if (ret == NULL) {
63 DHerr(0, ERR_R_MALLOC_FAILURE);
64 return NULL;
65 }
66
67 ret->references = 1;
68 ret->lock = CRYPTO_THREAD_lock_new();
69 if (ret->lock == NULL) {
70 DHerr(0, ERR_R_MALLOC_FAILURE);
71 OPENSSL_free(ret);
72 return NULL;
73 }
74
75 ret->libctx = libctx;
76 ret->meth = DH_get_default_method();
77 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
78 ret->flags = ret->meth->flags; /* early default init */
79 if (engine) {
80 if (!ENGINE_init(engine)) {
81 DHerr(0, ERR_R_ENGINE_LIB);
82 goto err;
83 }
84 ret->engine = engine;
85 } else
86 ret->engine = ENGINE_get_default_DH();
87 if (ret->engine) {
88 ret->meth = ENGINE_get_DH(ret->engine);
89 if (ret->meth == NULL) {
90 DHerr(0, ERR_R_ENGINE_LIB);
91 goto err;
92 }
93 }
94 #endif
95
96 ret->flags = ret->meth->flags;
97
98 #ifndef FIPS_MODE
99 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
100 goto err;
101 #endif /* FIPS_MODE */
102
103 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
104 DHerr(0, ERR_R_INIT_FAIL);
105 goto err;
106 }
107
108 return ret;
109
110 err:
111 DH_free(ret);
112 return NULL;
113 }
114
115 void DH_free(DH *r)
116 {
117 int i;
118
119 if (r == NULL)
120 return;
121
122 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
123 REF_PRINT_COUNT("DH", r);
124 if (i > 0)
125 return;
126 REF_ASSERT_ISNT(i < 0);
127
128 if (r->meth != NULL && r->meth->finish != NULL)
129 r->meth->finish(r);
130 #if !defined(FIPS_MODE)
131 # if !defined(OPENSSL_NO_ENGINE)
132 ENGINE_finish(r->engine);
133 # endif
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
135 #endif
136
137 CRYPTO_THREAD_lock_free(r->lock);
138
139 ffc_params_cleanup(&r->params);
140 BN_clear_free(r->pub_key);
141 BN_clear_free(r->priv_key);
142 OPENSSL_free(r);
143 }
144
145 int DH_up_ref(DH *r)
146 {
147 int i;
148
149 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
150 return 0;
151
152 REF_PRINT_COUNT("DH", r);
153 REF_ASSERT_ISNT(i < 2);
154 return ((i > 1) ? 1 : 0);
155 }
156
157 #ifndef FIPS_MODE
158 int DH_set_ex_data(DH *d, int idx, void *arg)
159 {
160 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
161 }
162
163 void *DH_get_ex_data(DH *d, int idx)
164 {
165 return CRYPTO_get_ex_data(&d->ex_data, idx);
166 }
167 #endif
168
169 int DH_bits(const DH *dh)
170 {
171 return BN_num_bits(dh->params.p);
172 }
173
174 int DH_size(const DH *dh)
175 {
176 return BN_num_bytes(dh->params.p);
177 }
178
179 int DH_security_bits(const DH *dh)
180 {
181 int N;
182 if (dh->params.q != NULL)
183 N = BN_num_bits(dh->params.q);
184 else if (dh->length)
185 N = dh->length;
186 else
187 N = -1;
188 return BN_security_bits(BN_num_bits(dh->params.p), N);
189 }
190
191 void DH_get0_pqg(const DH *dh,
192 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
193 {
194 ffc_params_get0_pqg(&dh->params, p, q, g);
195 }
196
197 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
198 {
199 /* If the fields p and g in d are NULL, the corresponding input
200 * parameters MUST be non-NULL. q may remain NULL.
201 */
202 if ((dh->params.p == NULL && p == NULL)
203 || (dh->params.g == NULL && g == NULL))
204 return 0;
205
206 ffc_params_set0_pqg(&dh->params, p, q, g);
207 dh->params.nid = NID_undef;
208 DH_get_nid(dh); /* Check if this is a named group and cache it */
209
210 if (q != NULL)
211 dh->length = BN_num_bits(q);
212
213 dh->dirty_cnt++;
214 return 1;
215 }
216
217 long DH_get_length(const DH *dh)
218 {
219 return dh->length;
220 }
221
222 int DH_set_length(DH *dh, long length)
223 {
224 dh->length = length;
225 return 1;
226 }
227
228 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
229 {
230 if (pub_key != NULL)
231 *pub_key = dh->pub_key;
232 if (priv_key != NULL)
233 *priv_key = dh->priv_key;
234 }
235
236 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
237 {
238 if (pub_key != NULL) {
239 BN_clear_free(dh->pub_key);
240 dh->pub_key = pub_key;
241 }
242 if (priv_key != NULL) {
243 BN_clear_free(dh->priv_key);
244 dh->priv_key = priv_key;
245 }
246
247 dh->dirty_cnt++;
248 return 1;
249 }
250
251 const BIGNUM *DH_get0_p(const DH *dh)
252 {
253 return dh->params.p;
254 }
255
256 const BIGNUM *DH_get0_q(const DH *dh)
257 {
258 return dh->params.q;
259 }
260
261 const BIGNUM *DH_get0_g(const DH *dh)
262 {
263 return dh->params.g;
264 }
265
266 const BIGNUM *DH_get0_priv_key(const DH *dh)
267 {
268 return dh->priv_key;
269 }
270
271 const BIGNUM *DH_get0_pub_key(const DH *dh)
272 {
273 return dh->pub_key;
274 }
275
276 void DH_clear_flags(DH *dh, int flags)
277 {
278 dh->flags &= ~flags;
279 }
280
281 int DH_test_flags(const DH *dh, int flags)
282 {
283 return dh->flags & flags;
284 }
285
286 void DH_set_flags(DH *dh, int flags)
287 {
288 dh->flags |= flags;
289 }
290
291 #ifndef FIPS_MODE
292 ENGINE *DH_get0_engine(DH *dh)
293 {
294 return dh->engine;
295 }
296 #endif /*FIPS_MODE */
297
298 FFC_PARAMS *dh_get0_params(DH *dh)
299 {
300 return &dh->params;
301 }
302 int dh_get0_nid(const DH *dh)
303 {
304 return dh->params.nid;
305 }