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