dh: update to structure based atomics
[openssl.git] / crypto / dh / dh_lib.c
1 /*
2  * Copyright 1995-2021 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 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/bn.h>
18 #ifndef FIPS_MODULE
19 # include <openssl/engine.h>
20 #endif
21 #include <openssl/obj_mac.h>
22 #include <openssl/core_names.h>
23 #include "internal/cryptlib.h"
24 #include "internal/refcount.h"
25 #include "crypto/evp.h"
26 #include "crypto/dh.h"
27 #include "dh_local.h"
28
29 static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
30
31 #ifndef FIPS_MODULE
32 int DH_set_method(DH *dh, const DH_METHOD *meth)
33 {
34     /*
35      * NB: The caller is specifically setting a method, so it's not up to us
36      * to deal with which ENGINE it comes from.
37      */
38     const DH_METHOD *mtmp;
39     mtmp = dh->meth;
40     if (mtmp->finish)
41         mtmp->finish(dh);
42 #ifndef OPENSSL_NO_ENGINE
43     ENGINE_finish(dh->engine);
44     dh->engine = NULL;
45 #endif
46     dh->meth = meth;
47     if (meth->init)
48         meth->init(dh);
49     return 1;
50 }
51
52 const DH_METHOD *ossl_dh_get_method(const DH *dh)
53 {
54     return dh->meth;
55 }
56 # ifndef OPENSSL_NO_DEPRECATED_3_0
57 DH *DH_new(void)
58 {
59     return dh_new_intern(NULL, NULL);
60 }
61 # endif
62
63 DH *DH_new_method(ENGINE *engine)
64 {
65     return dh_new_intern(engine, NULL);
66 }
67 #endif /* !FIPS_MODULE */
68
69 DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
70 {
71     return dh_new_intern(NULL, libctx);
72 }
73
74 static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
75 {
76     DH *ret = OPENSSL_zalloc(sizeof(*ret));
77
78     if (ret == NULL)
79         return NULL;
80
81     ret->lock = CRYPTO_THREAD_lock_new();
82     if (ret->lock == NULL) {
83         ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
84         OPENSSL_free(ret);
85         return NULL;
86     }
87
88     if (!CRYPTO_NEW_REF(&ret->references, 1))
89         goto err;
90
91     ret->libctx = libctx;
92     ret->meth = DH_get_default_method();
93 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
94     ret->flags = ret->meth->flags;  /* early default init */
95     if (engine) {
96         if (!ENGINE_init(engine)) {
97             ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
98             goto err;
99         }
100         ret->engine = engine;
101     } else
102         ret->engine = ENGINE_get_default_DH();
103     if (ret->engine) {
104         ret->meth = ENGINE_get_DH(ret->engine);
105         if (ret->meth == NULL) {
106             ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
107             goto err;
108         }
109     }
110 #endif
111
112     ret->flags = ret->meth->flags;
113
114 #ifndef FIPS_MODULE
115     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
116         goto err;
117 #endif /* FIPS_MODULE */
118
119     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
120         ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
121         goto err;
122     }
123
124     return ret;
125
126  err:
127     DH_free(ret);
128     return NULL;
129 }
130
131 void DH_free(DH *r)
132 {
133     int i;
134
135     if (r == NULL)
136         return;
137
138     CRYPTO_DOWN_REF(&r->references, &i);
139     REF_PRINT_COUNT("DH", r);
140     if (i > 0)
141         return;
142     REF_ASSERT_ISNT(i < 0);
143
144     if (r->meth != NULL && r->meth->finish != NULL)
145         r->meth->finish(r);
146 #if !defined(FIPS_MODULE)
147 # if !defined(OPENSSL_NO_ENGINE)
148     ENGINE_finish(r->engine);
149 # endif
150     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
151 #endif
152
153     CRYPTO_THREAD_lock_free(r->lock);
154     CRYPTO_FREE_REF(&r->references);
155
156     ossl_ffc_params_cleanup(&r->params);
157     BN_clear_free(r->pub_key);
158     BN_clear_free(r->priv_key);
159     OPENSSL_free(r);
160 }
161
162 int DH_up_ref(DH *r)
163 {
164     int i;
165
166     if (CRYPTO_UP_REF(&r->references, &i) <= 0)
167         return 0;
168
169     REF_PRINT_COUNT("DH", r);
170     REF_ASSERT_ISNT(i < 2);
171     return ((i > 1) ? 1 : 0);
172 }
173
174 void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
175 {
176     d->libctx = libctx;
177 }
178
179 #ifndef FIPS_MODULE
180 int DH_set_ex_data(DH *d, int idx, void *arg)
181 {
182     return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
183 }
184
185 void *DH_get_ex_data(const DH *d, int idx)
186 {
187     return CRYPTO_get_ex_data(&d->ex_data, idx);
188 }
189 #endif
190
191 int DH_bits(const DH *dh)
192 {
193     if (dh->params.p != NULL)
194         return BN_num_bits(dh->params.p);
195     return -1;
196 }
197
198 int DH_size(const DH *dh)
199 {
200     if (dh->params.p != NULL)
201         return BN_num_bytes(dh->params.p);
202     return -1;
203 }
204
205 int DH_security_bits(const DH *dh)
206 {
207     int N;
208
209     if (dh->params.q != NULL)
210         N = BN_num_bits(dh->params.q);
211     else if (dh->length)
212         N = dh->length;
213     else
214         N = -1;
215     if (dh->params.p != NULL)
216         return BN_security_bits(BN_num_bits(dh->params.p), N);
217     return -1;
218 }
219
220 void DH_get0_pqg(const DH *dh,
221                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
222 {
223     ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
224 }
225
226 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
227 {
228     /*
229      * If the fields p and g in dh are NULL, the corresponding input
230      * parameters MUST be non-NULL.  q may remain NULL.
231      */
232     if ((dh->params.p == NULL && p == NULL)
233         || (dh->params.g == NULL && g == NULL))
234         return 0;
235
236     ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
237     ossl_dh_cache_named_group(dh);
238     dh->dirty_cnt++;
239     return 1;
240 }
241
242 long DH_get_length(const DH *dh)
243 {
244     return dh->length;
245 }
246
247 int DH_set_length(DH *dh, long length)
248 {
249     dh->length = length;
250     dh->dirty_cnt++;
251     return 1;
252 }
253
254 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
255 {
256     if (pub_key != NULL)
257         *pub_key = dh->pub_key;
258     if (priv_key != NULL)
259         *priv_key = dh->priv_key;
260 }
261
262 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
263 {
264     if (pub_key != NULL) {
265         BN_clear_free(dh->pub_key);
266         dh->pub_key = pub_key;
267     }
268     if (priv_key != NULL) {
269         BN_clear_free(dh->priv_key);
270         dh->priv_key = priv_key;
271     }
272
273     dh->dirty_cnt++;
274     return 1;
275 }
276
277 const BIGNUM *DH_get0_p(const DH *dh)
278 {
279     return dh->params.p;
280 }
281
282 const BIGNUM *DH_get0_q(const DH *dh)
283 {
284     return dh->params.q;
285 }
286
287 const BIGNUM *DH_get0_g(const DH *dh)
288 {
289     return dh->params.g;
290 }
291
292 const BIGNUM *DH_get0_priv_key(const DH *dh)
293 {
294     return dh->priv_key;
295 }
296
297 const BIGNUM *DH_get0_pub_key(const DH *dh)
298 {
299     return dh->pub_key;
300 }
301
302 void DH_clear_flags(DH *dh, int flags)
303 {
304     dh->flags &= ~flags;
305 }
306
307 int DH_test_flags(const DH *dh, int flags)
308 {
309     return dh->flags & flags;
310 }
311
312 void DH_set_flags(DH *dh, int flags)
313 {
314     dh->flags |= flags;
315 }
316
317 #ifndef FIPS_MODULE
318 ENGINE *DH_get0_engine(DH *dh)
319 {
320     return dh->engine;
321 }
322 #endif /*FIPS_MODULE */
323
324 FFC_PARAMS *ossl_dh_get0_params(DH *dh)
325 {
326     return &dh->params;
327 }
328 int ossl_dh_get0_nid(const DH *dh)
329 {
330     return dh->params.nid;
331 }