2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/bn.h>
18 #include <openssl/engine.h>
19 #include <openssl/obj_mac.h>
20 #include <openssl/core_names.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "crypto/evp.h"
24 #include "crypto/dh.h"
27 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
30 int DH_set_method(DH *dh, const DH_METHOD *meth)
33 * NB: The caller is specifically setting a method, so it's not up to us
34 * to deal with which ENGINE it comes from.
36 const DH_METHOD *mtmp;
40 #ifndef OPENSSL_NO_ENGINE
41 ENGINE_finish(dh->engine);
50 const DH_METHOD *dh_get_method(const DH *dh)
57 return dh_new_intern(NULL, NULL);
60 DH *DH_new_method(ENGINE *engine)
62 return dh_new_intern(engine, NULL);
64 #endif /* !FIPS_MODULE */
66 DH *dh_new_with_libctx(OPENSSL_CTX *libctx)
68 return dh_new_intern(NULL, libctx);
71 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
73 DH *ret = OPENSSL_zalloc(sizeof(*ret));
76 DHerr(0, ERR_R_MALLOC_FAILURE);
81 ret->lock = CRYPTO_THREAD_lock_new();
82 if (ret->lock == NULL) {
83 DHerr(0, ERR_R_MALLOC_FAILURE);
89 ret->meth = DH_get_default_method();
90 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
91 ret->flags = ret->meth->flags; /* early default init */
93 if (!ENGINE_init(engine)) {
94 DHerr(0, ERR_R_ENGINE_LIB);
99 ret->engine = ENGINE_get_default_DH();
101 ret->meth = ENGINE_get_DH(ret->engine);
102 if (ret->meth == NULL) {
103 DHerr(0, ERR_R_ENGINE_LIB);
109 ret->flags = ret->meth->flags;
112 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
114 #endif /* FIPS_MODULE */
116 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
117 DHerr(0, ERR_R_INIT_FAIL);
135 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
136 REF_PRINT_COUNT("DH", r);
139 REF_ASSERT_ISNT(i < 0);
141 if (r->meth != NULL && r->meth->finish != NULL)
143 #if !defined(FIPS_MODULE)
144 # if !defined(OPENSSL_NO_ENGINE)
145 ENGINE_finish(r->engine);
147 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
150 CRYPTO_THREAD_lock_free(r->lock);
152 ffc_params_cleanup(&r->params);
153 BN_clear_free(r->pub_key);
154 BN_clear_free(r->priv_key);
162 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
165 REF_PRINT_COUNT("DH", r);
166 REF_ASSERT_ISNT(i < 2);
167 return ((i > 1) ? 1 : 0);
171 int DH_set_ex_data(DH *d, int idx, void *arg)
173 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
176 void *DH_get_ex_data(const DH *d, int idx)
178 return CRYPTO_get_ex_data(&d->ex_data, idx);
182 int DH_bits(const DH *dh)
184 return BN_num_bits(dh->params.p);
187 int DH_size(const DH *dh)
189 return BN_num_bytes(dh->params.p);
192 int DH_security_bits(const DH *dh)
195 if (dh->params.q != NULL)
196 N = BN_num_bits(dh->params.q);
201 return BN_security_bits(BN_num_bits(dh->params.p), N);
204 void DH_get0_pqg(const DH *dh,
205 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
207 ffc_params_get0_pqg(&dh->params, p, q, g);
210 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
213 * If the fields p and g in d are NULL, the corresponding input
214 * parameters MUST be non-NULL. q may remain NULL.
216 if ((dh->params.p == NULL && p == NULL)
217 || (dh->params.g == NULL && g == NULL))
220 ffc_params_set0_pqg(&dh->params, p, q, g);
221 dh_cache_named_group(dh);
223 dh->length = BN_num_bits(q);
225 * Check if this is a named group. If it finds a named group then the
226 * 'q' and 'length' value are either already set or are set by the
229 if (DH_get_nid(dh) == NID_undef) {
230 /* If its not a named group then set the 'length' if q is not NULL */
232 dh->length = BN_num_bits(q);
238 long DH_get_length(const DH *dh)
243 int DH_set_length(DH *dh, long length)
249 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
252 *pub_key = dh->pub_key;
253 if (priv_key != NULL)
254 *priv_key = dh->priv_key;
257 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
259 if (pub_key != NULL) {
260 BN_clear_free(dh->pub_key);
261 dh->pub_key = pub_key;
263 if (priv_key != NULL) {
264 BN_clear_free(dh->priv_key);
265 dh->priv_key = priv_key;
266 dh->length = BN_num_bits(priv_key);
273 const BIGNUM *DH_get0_p(const DH *dh)
278 const BIGNUM *DH_get0_q(const DH *dh)
283 const BIGNUM *DH_get0_g(const DH *dh)
288 const BIGNUM *DH_get0_priv_key(const DH *dh)
293 const BIGNUM *DH_get0_pub_key(const DH *dh)
298 void DH_clear_flags(DH *dh, int flags)
303 int DH_test_flags(const DH *dh, int flags)
305 return dh->flags & flags;
308 void DH_set_flags(DH *dh, int flags)
314 ENGINE *DH_get0_engine(DH *dh)
318 #endif /*FIPS_MODULE */
320 FFC_PARAMS *dh_get0_params(DH *dh)
324 int dh_get0_nid(const DH *dh)
326 return dh->params.nid;
329 int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
336 ffc = dh_get0_params(dh);
340 ret = ffc_params_fromdata(ffc, params);
342 dh_cache_named_group(dh);
348 static int dh_paramgen_check(EVP_PKEY_CTX *ctx)
350 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
351 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
352 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
355 /* If key type not DH return error */
356 if (ctx->pmeth != NULL
357 && ctx->pmeth->pkey_id != EVP_PKEY_DH
358 && ctx->pmeth->pkey_id != EVP_PKEY_DHX)
363 int EVP_PKEY_CTX_set_dh_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
366 OSSL_PARAM params[2], *p = params;
368 if ((ret = dh_paramgen_check(ctx)) <= 0)
371 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
372 *p++ = OSSL_PARAM_construct_end();
374 return EVP_PKEY_CTX_set_params(ctx, params);
377 int EVP_PKEY_CTX_set_dh_paramgen_seed(EVP_PKEY_CTX *ctx,
378 const unsigned char *seed,
382 OSSL_PARAM params[2], *p = params;
384 if ((ret = dh_paramgen_check(ctx)) <= 0)
387 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
388 (void *)seed, seedlen);
389 *p++ = OSSL_PARAM_construct_end();
391 return EVP_PKEY_CTX_set_params(ctx, params);
394 int EVP_PKEY_CTX_set_dh_paramgen_type(EVP_PKEY_CTX *ctx, int typ)
397 OSSL_PARAM params[2], *p = params;
400 if ((ret = dh_paramgen_check(ctx)) <= 0)
403 #if !defined(FIPS_MODULE)
404 /* TODO(3.0): Remove this eventually when no more legacy */
405 if (ctx->op.keymgmt.genctx == NULL)
406 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
407 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL);
410 name = dh_gen_type_id2name(typ);
413 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
415 *p++ = OSSL_PARAM_construct_end();
417 return EVP_PKEY_CTX_set_params(ctx, params);
420 int EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits)
423 OSSL_PARAM params[2], *p = params;
426 if ((ret = dh_paramgen_check(ctx)) <= 0)
429 #if !defined(FIPS_MODULE)
430 /* TODO(3.0): Remove this eventually when no more legacy */
431 if (ctx->op.keymgmt.genctx == NULL)
432 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
433 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, pbits,
436 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
437 *p++ = OSSL_PARAM_construct_end();
438 return EVP_PKEY_CTX_set_params(ctx, params);
441 int EVP_PKEY_CTX_set_dh_paramgen_subprime_len(EVP_PKEY_CTX *ctx, int qbits)
444 OSSL_PARAM params[2], *p = params;
445 size_t bits2 = qbits;
447 if ((ret = dh_paramgen_check(ctx)) <= 0)
450 #if !defined(FIPS_MODULE)
451 /* TODO(3.0): Remove this eventually when no more legacy */
452 if (ctx->op.keymgmt.genctx == NULL)
453 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
454 EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, qbits,
457 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
458 *p++ = OSSL_PARAM_construct_end();
460 return EVP_PKEY_CTX_set_params(ctx, params);
463 int EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen)
466 OSSL_PARAM params[2], *p = params;
468 if ((ret = dh_paramgen_check(ctx)) <= 0)
471 #if !defined(FIPS_MODULE)
472 /* TODO(3.0): Remove this eventually when no more legacy */
473 if (ctx->op.keymgmt.genctx == NULL)
474 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
475 EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL);
478 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GENERATOR, &gen);
479 *p++ = OSSL_PARAM_construct_end();
481 return EVP_PKEY_CTX_set_params(ctx, params);
484 int EVP_PKEY_CTX_set_dh_rfc5114(EVP_PKEY_CTX *ctx, int gen)
487 OSSL_PARAM params[2], *p = params;
490 if ((ret = dh_paramgen_check(ctx)) <= 0)
493 #if !defined(FIPS_MODULE)
494 /* TODO(3.0): Remove this eventually when no more legacy */
495 if (ctx->op.keymgmt.genctx == NULL)
496 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN,
497 EVP_PKEY_CTRL_DH_RFC5114, gen, NULL);
499 name = ffc_named_group_from_uid(gen);
503 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_GROUP,
505 *p++ = OSSL_PARAM_construct_end();
506 return EVP_PKEY_CTX_set_params(ctx, params);
509 int EVP_PKEY_CTX_set_dhx_rfc5114(EVP_PKEY_CTX *ctx, int gen)
511 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen);
514 int EVP_PKEY_CTX_set_dh_nid(EVP_PKEY_CTX *ctx, int nid)
517 OSSL_PARAM params[2], *p = params;
520 if ((ret = dh_paramgen_check(ctx)) <= 0)
523 #if !defined(FIPS_MODULE)
524 /* TODO(3.0): Remove this eventually when no more legacy */
525 if (ctx->op.keymgmt.genctx == NULL)
526 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH,
527 EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
528 EVP_PKEY_CTRL_DH_NID, nid, NULL);
530 name = ffc_named_group_from_uid(nid);
534 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_GROUP,
536 *p++ = OSSL_PARAM_construct_end();
537 return EVP_PKEY_CTX_set_params(ctx, params);