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