]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_lib.c
x86_64: Add endbranch at function entries for Intel CET
[thirdparty/openssl.git] / crypto / dh / dh_lib.c
CommitLineData
aa6bb135 1/*
83cf7abf 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
ec577822 11#include <openssl/bn.h>
3c27208f 12#include <openssl/engine.h>
5a778ce5
DG
13#include <openssl/obj_mac.h>
14#include "internal/cryptlib.h"
15#include "internal/refcount.h"
dc8de3e6 16#include "crypto/dh.h"
5a778ce5 17#include "dh_local.h"
d02b48c6 18
62f49b90 19#ifndef FIPS_MODE
cb78486d 20int DH_set_method(DH *dh, const DH_METHOD *meth)
0f113f3e
MC
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);
0b13e9f0 30#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
31 ENGINE_finish(dh->engine);
32 dh->engine = NULL;
0b13e9f0 33#endif
0f113f3e
MC
34 dh->meth = meth;
35 if (meth->init)
36 meth->init(dh);
37 return 1;
38}
62f49b90 39#endif /* !FIPS_MODE */
13066cee 40
6b691a5c 41DH *DH_new(void)
0f113f3e
MC
42{
43 return DH_new_method(NULL);
44}
13066cee 45
5270e702 46DH *DH_new_method(ENGINE *engine)
0f113f3e 47{
64b25758 48 DH *ret = OPENSSL_zalloc(sizeof(*ret));
13066cee 49
0f113f3e
MC
50 if (ret == NULL) {
51 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
d188a536 52 return NULL;
0f113f3e 53 }
0c9de428 54
2bbf0baa
F
55 ret->references = 1;
56 ret->lock = CRYPTO_THREAD_lock_new();
57 if (ret->lock == NULL) {
b2b361f6 58 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
2bbf0baa
F
59 OPENSSL_free(ret);
60 return NULL;
61 }
62
0f113f3e 63 ret->meth = DH_get_default_method();
62f49b90 64#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
2bbf0baa 65 ret->flags = ret->meth->flags; /* early default init */
0f113f3e
MC
66 if (engine) {
67 if (!ENGINE_init(engine)) {
68 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 69 goto err;
0f113f3e
MC
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);
7c96dbcd 76 if (ret->meth == NULL) {
0f113f3e 77 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 78 goto err;
0f113f3e
MC
79 }
80 }
0b13e9f0 81#endif
cb78486d 82
0f113f3e 83 ret->flags = ret->meth->flags;
d188a536 84
a3327784 85#ifndef FIPS_MODE
2bbf0baa
F
86 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
87 goto err;
62f49b90 88#endif /* FIPS_MODE */
d188a536
AG
89
90 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
2bbf0baa 91 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
544648a8 92 goto err;
0f113f3e 93 }
d188a536
AG
94
95 return ret;
544648a8
NT
96
97 err:
98 DH_free(ret);
99 return NULL;
0f113f3e 100}
d02b48c6 101
6b691a5c 102void DH_free(DH *r)
0f113f3e
MC
103{
104 int i;
d6407083 105
0f113f3e
MC
106 if (r == NULL)
107 return;
d188a536 108
2f545ae4 109 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 110 REF_PRINT_COUNT("DH", r);
0f113f3e
MC
111 if (i > 0)
112 return;
f3f1cf84 113 REF_ASSERT_ISNT(i < 0);
13066cee 114
0c5d725e 115 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 116 r->meth->finish(r);
62f49b90
SL
117#if !defined(FIPS_MODE)
118# if !defined(OPENSSL_NO_ENGINE)
7c96dbcd 119 ENGINE_finish(r->engine);
62f49b90 120# endif
0f113f3e 121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
a3327784 122#endif
d50f1bdf 123
d188a536
AG
124 CRYPTO_THREAD_lock_free(r->lock);
125
dc8de3e6 126 ffc_params_cleanup(&r->params);
23a1d5e9
RS
127 BN_clear_free(r->pub_key);
128 BN_clear_free(r->priv_key);
0f113f3e
MC
129 OPENSSL_free(r);
130}
d02b48c6 131
dc2a33d6 132int DH_up_ref(DH *r)
0f113f3e 133{
d188a536
AG
134 int i;
135
2f545ae4 136 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 137 return 0;
f3f1cf84
RS
138
139 REF_PRINT_COUNT("DH", r);
140 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
141 return ((i > 1) ? 1 : 0);
142}
5cbc2e8b 143
a3327784 144#ifndef FIPS_MODE
dd9d233e 145int DH_set_ex_data(DH *d, int idx, void *arg)
0f113f3e 146{
26a7d938 147 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
0f113f3e 148}
13066cee 149
dd9d233e 150void *DH_get_ex_data(DH *d, int idx)
0f113f3e 151{
26a7d938 152 return CRYPTO_get_ex_data(&d->ex_data, idx);
0f113f3e 153}
a3327784 154#endif
13066cee 155
26c79d56
KR
156int DH_bits(const DH *dh)
157{
dc8de3e6 158 return BN_num_bits(dh->params.p);
26c79d56
KR
159}
160
f971ccb2 161int DH_size(const DH *dh)
0f113f3e 162{
dc8de3e6 163 return BN_num_bytes(dh->params.p);
0f113f3e 164}
2514fa79
DSH
165
166int DH_security_bits(const DH *dh)
0f113f3e
MC
167{
168 int N;
dc8de3e6
SL
169 if (dh->params.q != NULL)
170 N = BN_num_bits(dh->params.q);
0f113f3e
MC
171 else if (dh->length)
172 N = dh->length;
173 else
174 N = -1;
dc8de3e6 175 return BN_security_bits(BN_num_bits(dh->params.p), N);
0f113f3e 176}
0aeddcfa 177
fd809cfd
RL
178void DH_get0_pqg(const DH *dh,
179 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
0aeddcfa 180{
dc8de3e6 181 ffc_params_get0_pqg(&dh->params, p, q, g);
0aeddcfa
MC
182}
183
184int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
185{
1da12e34
RL
186 /* If the fields p and g in d are NULL, the corresponding input
187 * parameters MUST be non-NULL. q may remain NULL.
1da12e34 188 */
dc8de3e6
SL
189 if ((dh->params.p == NULL && p == NULL)
190 || (dh->params.g == NULL && g == NULL))
0aeddcfa 191 return 0;
1da12e34 192
dc8de3e6 193 ffc_params_set0_pqg(&dh->params, p, q, g);
ca2bf555
SL
194 dh->params.nid = NID_undef;
195 DH_get_nid(dh); /* Check if this is a named group and cache it */
0aeddcfa 196
dc8de3e6 197 if (q != NULL)
0aeddcfa 198 dh->length = BN_num_bits(q);
0aeddcfa 199
8b84b075 200 dh->dirty_cnt++;
0aeddcfa
MC
201 return 1;
202}
203
204long DH_get_length(const DH *dh)
205{
206 return dh->length;
207}
208
209int DH_set_length(DH *dh, long length)
210{
211 dh->length = length;
212 return 1;
213}
214
fd809cfd 215void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
0aeddcfa
MC
216{
217 if (pub_key != NULL)
218 *pub_key = dh->pub_key;
219 if (priv_key != NULL)
220 *priv_key = dh->priv_key;
221}
222
223int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
224{
1da12e34 225 if (pub_key != NULL) {
fa01370f 226 BN_clear_free(dh->pub_key);
1da12e34
RL
227 dh->pub_key = pub_key;
228 }
229 if (priv_key != NULL) {
fa01370f 230 BN_clear_free(dh->priv_key);
1da12e34
RL
231 dh->priv_key = priv_key;
232 }
0aeddcfa 233
8b84b075 234 dh->dirty_cnt++;
0aeddcfa
MC
235 return 1;
236}
237
6db7fadf
DMSP
238const BIGNUM *DH_get0_p(const DH *dh)
239{
dc8de3e6 240 return dh->params.p;
6db7fadf
DMSP
241}
242
243const BIGNUM *DH_get0_q(const DH *dh)
244{
dc8de3e6 245 return dh->params.q;
6db7fadf
DMSP
246}
247
248const BIGNUM *DH_get0_g(const DH *dh)
249{
dc8de3e6 250 return dh->params.g;
6db7fadf
DMSP
251}
252
253const BIGNUM *DH_get0_priv_key(const DH *dh)
254{
255 return dh->priv_key;
256}
257
258const BIGNUM *DH_get0_pub_key(const DH *dh)
259{
260 return dh->pub_key;
261}
262
0aeddcfa
MC
263void DH_clear_flags(DH *dh, int flags)
264{
265 dh->flags &= ~flags;
266}
267
268int DH_test_flags(const DH *dh, int flags)
269{
270 return dh->flags & flags;
271}
272
273void DH_set_flags(DH *dh, int flags)
274{
275 dh->flags |= flags;
276}
277
62f49b90 278#ifndef FIPS_MODE
0aeddcfa
MC
279ENGINE *DH_get0_engine(DH *dh)
280{
281 return dh->engine;
282}
62f49b90 283#endif /*FIPS_MODE */
dc8de3e6
SL
284
285FFC_PARAMS *dh_get0_params(DH *dh)
286{
287 return &dh->params;
288}
ca2bf555
SL
289int dh_get0_nid(const DH *dh)
290{
291 return dh->params.nid;
292}