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/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "prov/implementations.h"
16 #include "prov/provider_ctx.h"
17 #include "crypto/dh.h"
19 static OSSL_OP_keyexch_newctx_fn dh_newctx;
20 static OSSL_OP_keyexch_init_fn dh_init;
21 static OSSL_OP_keyexch_set_peer_fn dh_set_peer;
22 static OSSL_OP_keyexch_derive_fn dh_derive;
23 static OSSL_OP_keyexch_freectx_fn dh_freectx;
24 static OSSL_OP_keyexch_dupctx_fn dh_dupctx;
25 static OSSL_OP_keyexch_set_ctx_params_fn dh_set_ctx_params;
26 static OSSL_OP_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
29 * What's passed as an actual key is defined by the KEYMGMT interface.
30 * We happen to know that our KEYMGMT simply passes DH structures, so
31 * we use that here too.
41 static void *dh_newctx(void *provctx)
43 PROV_DH_CTX *pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX));
47 pdhctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
51 static int dh_init(void *vpdhctx, void *vdh)
53 PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
55 if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
62 static int dh_set_peer(void *vpdhctx, void *vdh)
64 PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
66 if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
68 DH_free(pdhctx->dhpeer);
73 static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
76 PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
79 const BIGNUM *pub_key = NULL;
81 /* TODO(3.0): Add errors to stack */
82 if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
85 dhsize = (size_t)DH_size(pdhctx->dh);
93 DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
95 ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh);
97 ret = DH_compute_key(secret, pub_key, pdhctx->dh);
105 static void dh_freectx(void *vpdhctx)
107 PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
110 DH_free(pdhctx->dhpeer);
112 OPENSSL_free(pdhctx);
115 static void *dh_dupctx(void *vpdhctx)
117 PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
120 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
125 if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
126 OPENSSL_free(dstctx);
130 if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
132 OPENSSL_free(dstctx);
139 static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[])
141 PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
145 if (pdhctx == NULL || params == NULL)
148 p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD);
149 if (p == NULL || !OSSL_PARAM_get_uint(p, &pad))
151 pdhctx->pad = pad ? 1 : 0;
155 static const OSSL_PARAM known_settable_ctx_params[] = {
156 OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL),
160 static const OSSL_PARAM *dh_settable_ctx_params(void)
162 return known_settable_ctx_params;
165 const OSSL_DISPATCH dh_keyexch_functions[] = {
166 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
167 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
168 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
169 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
170 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
171 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
172 { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params },
173 { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
174 (void (*)(void))dh_settable_ctx_params },