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