2 * Copyright 2019 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
10 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "prov/implementations.h"
17 static OSSL_OP_keymgmt_importdomparams_fn dh_importdomparams;
18 static OSSL_OP_keymgmt_importkey_fn dh_importkey;
20 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
22 const OSSL_PARAM *param_p, *param_g;
23 BIGNUM *p = NULL, *g = NULL;
28 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
29 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
31 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
32 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
35 if (!DH_set0_pqg(dh, p, NULL, g))
46 static int params_to_key(DH *dh, const OSSL_PARAM params[])
48 const OSSL_PARAM *param_priv_key, *param_pub_key;
49 BIGNUM *priv_key = NULL, *pub_key = NULL;
54 if (!params_to_domparams(dh, params))
58 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
60 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
63 * DH documentation says that a public key must be present if a
64 * private key is present.
65 * We want to have at least a public key either way, so we end up
66 * requiring it unconditionally.
68 if (param_pub_key == NULL)
71 if ((param_priv_key != NULL
72 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
73 || !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
76 if (!DH_set0_key(dh, pub_key, priv_key))
87 static void *dh_importdomparams(void *provctx, const OSSL_PARAM params[])
91 if ((dh = DH_new()) == NULL
92 || !params_to_domparams(dh, params)) {
99 static void *dh_importkey(void *provctx, const OSSL_PARAM params[])
103 if ((dh = DH_new()) == NULL
104 || !params_to_key(dh, params)) {
111 const OSSL_DISPATCH dh_keymgmt_functions[] = {
113 * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
114 * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
116 { OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dh_importdomparams },
117 { OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DH_free },
118 { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dh_importkey },
119 { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DH_free },