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