14ec2eca9Schristos /*
2*5b10f583Schristos  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*5b10f583Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
54ec2eca9Schristos  * this file except in compliance with the License.  You can obtain a copy
64ec2eca9Schristos  * in the file LICENSE in the source distribution or at
74ec2eca9Schristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10*5b10f583Schristos /*
11*5b10f583Schristos  * DH low level APIs are deprecated for public use, but still ok for
12*5b10f583Schristos  * internal use.
13*5b10f583Schristos  */
14*5b10f583Schristos #include "internal/deprecated.h"
15*5b10f583Schristos 
16a89c9211Schristos #include <stdio.h>
17*5b10f583Schristos #include <openssl/bn.h>
18*5b10f583Schristos #ifndef FIPS_MODULE
19*5b10f583Schristos # include <openssl/engine.h>
20*5b10f583Schristos #endif
21*5b10f583Schristos #include <openssl/obj_mac.h>
22*5b10f583Schristos #include <openssl/core_names.h>
234ec2eca9Schristos #include "internal/cryptlib.h"
24e7ccb6d1Schristos #include "internal/refcount.h"
25*5b10f583Schristos #include "crypto/evp.h"
26*5b10f583Schristos #include "crypto/dh.h"
27cb006352Schristos #include "dh_local.h"
28a89c9211Schristos 
29*5b10f583Schristos static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
30*5b10f583Schristos 
31*5b10f583Schristos #ifndef FIPS_MODULE
DH_set_method(DH * dh,const DH_METHOD * meth)32a89c9211Schristos int DH_set_method(DH *dh, const DH_METHOD *meth)
33a89c9211Schristos {
34ac65d4acSspz     /*
35ac65d4acSspz      * NB: The caller is specifically setting a method, so it's not up to us
36ac65d4acSspz      * to deal with which ENGINE it comes from.
37ac65d4acSspz      */
38a89c9211Schristos     const DH_METHOD *mtmp;
39a89c9211Schristos     mtmp = dh->meth;
40ac65d4acSspz     if (mtmp->finish)
41ac65d4acSspz         mtmp->finish(dh);
42a89c9211Schristos #ifndef OPENSSL_NO_ENGINE
43a89c9211Schristos     ENGINE_finish(dh->engine);
44a89c9211Schristos     dh->engine = NULL;
45a89c9211Schristos #endif
46a89c9211Schristos     dh->meth = meth;
47ac65d4acSspz     if (meth->init)
48ac65d4acSspz         meth->init(dh);
49a89c9211Schristos     return 1;
50a89c9211Schristos }
51a89c9211Schristos 
ossl_dh_get_method(const DH * dh)52*5b10f583Schristos const DH_METHOD *ossl_dh_get_method(const DH *dh)
53*5b10f583Schristos {
54*5b10f583Schristos     return dh->meth;
55*5b10f583Schristos }
56*5b10f583Schristos # ifndef OPENSSL_NO_DEPRECATED_3_0
DH_new(void)57a89c9211Schristos DH *DH_new(void)
58a89c9211Schristos {
59*5b10f583Schristos     return dh_new_intern(NULL, NULL);
60a89c9211Schristos }
61*5b10f583Schristos # endif
62a89c9211Schristos 
DH_new_method(ENGINE * engine)63a89c9211Schristos DH *DH_new_method(ENGINE *engine)
64a89c9211Schristos {
65*5b10f583Schristos     return dh_new_intern(engine, NULL);
66*5b10f583Schristos }
67*5b10f583Schristos #endif /* !FIPS_MODULE */
68*5b10f583Schristos 
ossl_dh_new_ex(OSSL_LIB_CTX * libctx)69*5b10f583Schristos DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
70*5b10f583Schristos {
71*5b10f583Schristos     return dh_new_intern(NULL, libctx);
72*5b10f583Schristos }
73*5b10f583Schristos 
dh_new_intern(ENGINE * engine,OSSL_LIB_CTX * libctx)74*5b10f583Schristos static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
75*5b10f583Schristos {
764ec2eca9Schristos     DH *ret = OPENSSL_zalloc(sizeof(*ret));
77a89c9211Schristos 
78ac65d4acSspz     if (ret == NULL) {
79*5b10f583Schristos         ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
804ec2eca9Schristos         return NULL;
814ec2eca9Schristos     }
824ec2eca9Schristos 
834ec2eca9Schristos     ret->references = 1;
844ec2eca9Schristos     ret->lock = CRYPTO_THREAD_lock_new();
854ec2eca9Schristos     if (ret->lock == NULL) {
86*5b10f583Schristos         ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
874ec2eca9Schristos         OPENSSL_free(ret);
884ec2eca9Schristos         return NULL;
89a89c9211Schristos     }
90a89c9211Schristos 
91*5b10f583Schristos     ret->libctx = libctx;
92a89c9211Schristos     ret->meth = DH_get_default_method();
93*5b10f583Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
944ec2eca9Schristos     ret->flags = ret->meth->flags;  /* early default init */
95ac65d4acSspz     if (engine) {
96ac65d4acSspz         if (!ENGINE_init(engine)) {
97*5b10f583Schristos             ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
984ec2eca9Schristos             goto err;
99a89c9211Schristos         }
100a89c9211Schristos         ret->engine = engine;
101ac65d4acSspz     } else
102a89c9211Schristos         ret->engine = ENGINE_get_default_DH();
103ac65d4acSspz     if (ret->engine) {
104a89c9211Schristos         ret->meth = ENGINE_get_DH(ret->engine);
1054ec2eca9Schristos         if (ret->meth == NULL) {
106*5b10f583Schristos             ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
1074ec2eca9Schristos             goto err;
108a89c9211Schristos         }
109a89c9211Schristos     }
110a89c9211Schristos #endif
111a89c9211Schristos 
1124ec2eca9Schristos     ret->flags = ret->meth->flags;
1134ec2eca9Schristos 
114*5b10f583Schristos #ifndef FIPS_MODULE
1154ec2eca9Schristos     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
1164ec2eca9Schristos         goto err;
117*5b10f583Schristos #endif /* FIPS_MODULE */
1184ec2eca9Schristos 
119ac65d4acSspz     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
120*5b10f583Schristos         ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
121e7ccb6d1Schristos         goto err;
122a89c9211Schristos     }
1234ec2eca9Schristos 
1244ec2eca9Schristos     return ret;
125e7ccb6d1Schristos 
126e7ccb6d1Schristos  err:
127e7ccb6d1Schristos     DH_free(ret);
128e7ccb6d1Schristos     return NULL;
129a89c9211Schristos }
130a89c9211Schristos 
DH_free(DH * r)131a89c9211Schristos void DH_free(DH *r)
132a89c9211Schristos {
133a89c9211Schristos     int i;
1344ec2eca9Schristos 
135ac65d4acSspz     if (r == NULL)
136ac65d4acSspz         return;
1374ec2eca9Schristos 
138e7ccb6d1Schristos     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
1394ec2eca9Schristos     REF_PRINT_COUNT("DH", r);
140ac65d4acSspz     if (i > 0)
141ac65d4acSspz         return;
1424ec2eca9Schristos     REF_ASSERT_ISNT(i < 0);
143a89c9211Schristos 
144e7ccb6d1Schristos     if (r->meth != NULL && r->meth->finish != NULL)
145a89c9211Schristos         r->meth->finish(r);
146*5b10f583Schristos #if !defined(FIPS_MODULE)
147*5b10f583Schristos # if !defined(OPENSSL_NO_ENGINE)
148a89c9211Schristos     ENGINE_finish(r->engine);
149a89c9211Schristos # endif
150a89c9211Schristos     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
151*5b10f583Schristos #endif
152a89c9211Schristos 
1534ec2eca9Schristos     CRYPTO_THREAD_lock_free(r->lock);
1544ec2eca9Schristos 
155*5b10f583Schristos     ossl_ffc_params_cleanup(&r->params);
156ac65d4acSspz     BN_clear_free(r->pub_key);
157ac65d4acSspz     BN_clear_free(r->priv_key);
158a89c9211Schristos     OPENSSL_free(r);
159a89c9211Schristos }
160a89c9211Schristos 
DH_up_ref(DH * r)161a89c9211Schristos int DH_up_ref(DH *r)
162a89c9211Schristos {
1634ec2eca9Schristos     int i;
164a89c9211Schristos 
165e7ccb6d1Schristos     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
1664ec2eca9Schristos         return 0;
1674ec2eca9Schristos 
1684ec2eca9Schristos     REF_PRINT_COUNT("DH", r);
1694ec2eca9Schristos     REF_ASSERT_ISNT(i < 2);
1704ec2eca9Schristos     return ((i > 1) ? 1 : 0);
171a89c9211Schristos }
172a89c9211Schristos 
ossl_dh_set0_libctx(DH * d,OSSL_LIB_CTX * libctx)173*5b10f583Schristos void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
174*5b10f583Schristos {
175*5b10f583Schristos     d->libctx = libctx;
176*5b10f583Schristos }
177*5b10f583Schristos 
178*5b10f583Schristos #ifndef FIPS_MODULE
DH_set_ex_data(DH * d,int idx,void * arg)179a89c9211Schristos int DH_set_ex_data(DH *d, int idx, void *arg)
180a89c9211Schristos {
181e7ccb6d1Schristos     return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
182a89c9211Schristos }
183a89c9211Schristos 
DH_get_ex_data(const DH * d,int idx)184*5b10f583Schristos void *DH_get_ex_data(const DH *d, int idx)
185a89c9211Schristos {
186e7ccb6d1Schristos     return CRYPTO_get_ex_data(&d->ex_data, idx);
187a89c9211Schristos }
188*5b10f583Schristos #endif
189a89c9211Schristos 
DH_bits(const DH * dh)1904ec2eca9Schristos int DH_bits(const DH *dh)
1914ec2eca9Schristos {
192*5b10f583Schristos     if (dh->params.p != NULL)
193*5b10f583Schristos         return BN_num_bits(dh->params.p);
194*5b10f583Schristos     return -1;
1954ec2eca9Schristos }
1964ec2eca9Schristos 
DH_size(const DH * dh)197a89c9211Schristos int DH_size(const DH *dh)
198a89c9211Schristos {
199*5b10f583Schristos     if (dh->params.p != NULL)
200*5b10f583Schristos         return BN_num_bytes(dh->params.p);
201*5b10f583Schristos     return -1;
202a89c9211Schristos }
2034ec2eca9Schristos 
DH_security_bits(const DH * dh)2044ec2eca9Schristos int DH_security_bits(const DH *dh)
2054ec2eca9Schristos {
2064ec2eca9Schristos     int N;
207*5b10f583Schristos 
208*5b10f583Schristos     if (dh->params.q != NULL)
209*5b10f583Schristos         N = BN_num_bits(dh->params.q);
2104ec2eca9Schristos     else if (dh->length)
2114ec2eca9Schristos         N = dh->length;
2124ec2eca9Schristos     else
2134ec2eca9Schristos         N = -1;
214*5b10f583Schristos     if (dh->params.p != NULL)
215*5b10f583Schristos         return BN_security_bits(BN_num_bits(dh->params.p), N);
216*5b10f583Schristos     return -1;
2174ec2eca9Schristos }
2184ec2eca9Schristos 
DH_get0_pqg(const DH * dh,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)2194ec2eca9Schristos void DH_get0_pqg(const DH *dh,
2204ec2eca9Schristos                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
2214ec2eca9Schristos {
222*5b10f583Schristos     ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
2234ec2eca9Schristos }
2244ec2eca9Schristos 
DH_set0_pqg(DH * dh,BIGNUM * p,BIGNUM * q,BIGNUM * g)2254ec2eca9Schristos int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
2264ec2eca9Schristos {
227*5b10f583Schristos     /*
228*5b10f583Schristos      * If the fields p and g in dh are NULL, the corresponding input
2294ec2eca9Schristos      * parameters MUST be non-NULL.  q may remain NULL.
2304ec2eca9Schristos      */
231*5b10f583Schristos     if ((dh->params.p == NULL && p == NULL)
232*5b10f583Schristos         || (dh->params.g == NULL && g == NULL))
2334ec2eca9Schristos         return 0;
2344ec2eca9Schristos 
235*5b10f583Schristos     ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
236*5b10f583Schristos     ossl_dh_cache_named_group(dh);
237*5b10f583Schristos     dh->dirty_cnt++;
2384ec2eca9Schristos     return 1;
2394ec2eca9Schristos }
2404ec2eca9Schristos 
DH_get_length(const DH * dh)2414ec2eca9Schristos long DH_get_length(const DH *dh)
2424ec2eca9Schristos {
2434ec2eca9Schristos     return dh->length;
2444ec2eca9Schristos }
2454ec2eca9Schristos 
DH_set_length(DH * dh,long length)2464ec2eca9Schristos int DH_set_length(DH *dh, long length)
2474ec2eca9Schristos {
2484ec2eca9Schristos     dh->length = length;
249*5b10f583Schristos     dh->dirty_cnt++;
2504ec2eca9Schristos     return 1;
2514ec2eca9Schristos }
2524ec2eca9Schristos 
DH_get0_key(const DH * dh,const BIGNUM ** pub_key,const BIGNUM ** priv_key)2534ec2eca9Schristos void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
2544ec2eca9Schristos {
2554ec2eca9Schristos     if (pub_key != NULL)
2564ec2eca9Schristos         *pub_key = dh->pub_key;
2574ec2eca9Schristos     if (priv_key != NULL)
2584ec2eca9Schristos         *priv_key = dh->priv_key;
2594ec2eca9Schristos }
2604ec2eca9Schristos 
DH_set0_key(DH * dh,BIGNUM * pub_key,BIGNUM * priv_key)2614ec2eca9Schristos int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
2624ec2eca9Schristos {
2634ec2eca9Schristos     if (pub_key != NULL) {
264f23d01f8Schristos         BN_clear_free(dh->pub_key);
2654ec2eca9Schristos         dh->pub_key = pub_key;
2664ec2eca9Schristos     }
2674ec2eca9Schristos     if (priv_key != NULL) {
268f23d01f8Schristos         BN_clear_free(dh->priv_key);
2694ec2eca9Schristos         dh->priv_key = priv_key;
2704ec2eca9Schristos     }
2714ec2eca9Schristos 
272*5b10f583Schristos     dh->dirty_cnt++;
2734ec2eca9Schristos     return 1;
2744ec2eca9Schristos }
2754ec2eca9Schristos 
DH_get0_p(const DH * dh)276e7ccb6d1Schristos const BIGNUM *DH_get0_p(const DH *dh)
277e7ccb6d1Schristos {
278*5b10f583Schristos     return dh->params.p;
279e7ccb6d1Schristos }
280e7ccb6d1Schristos 
DH_get0_q(const DH * dh)281e7ccb6d1Schristos const BIGNUM *DH_get0_q(const DH *dh)
282e7ccb6d1Schristos {
283*5b10f583Schristos     return dh->params.q;
284e7ccb6d1Schristos }
285e7ccb6d1Schristos 
DH_get0_g(const DH * dh)286e7ccb6d1Schristos const BIGNUM *DH_get0_g(const DH *dh)
287e7ccb6d1Schristos {
288*5b10f583Schristos     return dh->params.g;
289e7ccb6d1Schristos }
290e7ccb6d1Schristos 
DH_get0_priv_key(const DH * dh)291e7ccb6d1Schristos const BIGNUM *DH_get0_priv_key(const DH *dh)
292e7ccb6d1Schristos {
293e7ccb6d1Schristos     return dh->priv_key;
294e7ccb6d1Schristos }
295e7ccb6d1Schristos 
DH_get0_pub_key(const DH * dh)296e7ccb6d1Schristos const BIGNUM *DH_get0_pub_key(const DH *dh)
297e7ccb6d1Schristos {
298e7ccb6d1Schristos     return dh->pub_key;
299e7ccb6d1Schristos }
300e7ccb6d1Schristos 
DH_clear_flags(DH * dh,int flags)3014ec2eca9Schristos void DH_clear_flags(DH *dh, int flags)
3024ec2eca9Schristos {
3034ec2eca9Schristos     dh->flags &= ~flags;
3044ec2eca9Schristos }
3054ec2eca9Schristos 
DH_test_flags(const DH * dh,int flags)3064ec2eca9Schristos int DH_test_flags(const DH *dh, int flags)
3074ec2eca9Schristos {
3084ec2eca9Schristos     return dh->flags & flags;
3094ec2eca9Schristos }
3104ec2eca9Schristos 
DH_set_flags(DH * dh,int flags)3114ec2eca9Schristos void DH_set_flags(DH *dh, int flags)
3124ec2eca9Schristos {
3134ec2eca9Schristos     dh->flags |= flags;
3144ec2eca9Schristos }
3154ec2eca9Schristos 
316*5b10f583Schristos #ifndef FIPS_MODULE
DH_get0_engine(DH * dh)3174ec2eca9Schristos ENGINE *DH_get0_engine(DH *dh)
3184ec2eca9Schristos {
3194ec2eca9Schristos     return dh->engine;
3204ec2eca9Schristos }
321*5b10f583Schristos #endif /*FIPS_MODULE */
322*5b10f583Schristos 
ossl_dh_get0_params(DH * dh)323*5b10f583Schristos FFC_PARAMS *ossl_dh_get0_params(DH *dh)
324*5b10f583Schristos {
325*5b10f583Schristos     return &dh->params;
326*5b10f583Schristos }
ossl_dh_get0_nid(const DH * dh)327*5b10f583Schristos int ossl_dh_get0_nid(const DH *dh)
328*5b10f583Schristos {
329*5b10f583Schristos     return dh->params.nid;
330*5b10f583Schristos }
331