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