1EVP_KEYEXCH-DH(7ossl) OpenSSL EVP_KEYEXCH-DH(7ossl)
2
3
4
6 EVP_KEYEXCH-DH - DH Key Exchange algorithm support
7
9 Key exchange support for the DH key type.
10
11 DH key exchange parameters
12 "pad" (OSSL_EXCHANGE_PARAM_PAD) <unsigned integer>
13 Sets the padding mode for the associated key exchange ctx. Setting
14 a value of 1 will turn padding on. Setting a value of 0 will turn
15 padding off. If padding is off then the derived shared secret may
16 be smaller than the largest possible secret size. If padding is on
17 then the derived shared secret will have its first bytes filled
18 with zeros where necessary to make the shared secret the same size
19 as the largest possible secret size. The padding mode parameter is
20 ignored (and padding implicitly enabled) when the KDF type is set
21 to "X942KDF-ASN1" (OSSL_KDF_NAME_X942KDF_ASN1).
22
23 "kdf-type" (OSSL_EXCHANGE_PARAM_KDF_TYPE) <UTF8 string>
24 See "Common Key Exchange parameters" in provider-keyexch(7).
25
26 "kdf-digest" (OSSL_EXCHANGE_PARAM_KDF_DIGEST) <UTF8 string>
27 See "Common Key Exchange parameters" in provider-keyexch(7).
28
29 "kdf-digest-props" (OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS) <UTF8 string>
30 See "Common Key Exchange parameters" in provider-keyexch(7).
31
32 "kdf-outlen" (OSSL_EXCHANGE_PARAM_KDF_OUTLEN) <unsigned integer>
33 See "Common Key Exchange parameters" in provider-keyexch(7).
34
35 "kdf-ukm" (OSSL_EXCHANGE_PARAM_KDF_UKM) <octet string>
36 See "Common Key Exchange parameters" in provider-keyexch(7).
37
38 "cekalg" (OSSL_KDF_PARAM_CEK_ALG) <octet string ptr>
39 See "KDF Parameters" in provider-kdf(7).
40
42 The examples assume a host and peer both generate keys using the same
43 named group (or domain parameters). See "Examples" in EVP_PKEY-DH(7).
44 Both the host and peer transfer their public key to each other.
45
46 To convert the peer's generated key pair to a public key in DER format
47 in order to transfer to the host:
48
49 EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */
50 unsigned char *peer_pub_der = NULL;
51 int peer_pub_der_len;
52
53 peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der);
54 ...
55 OPENSSL_free(peer_pub_der);
56
57 To convert the received peer's public key from DER format on the host:
58
59 const unsigned char *pd = peer_pub_der;
60 EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len);
61 ...
62 EVP_PKEY_free(peer_pub_key);
63
64 To derive a shared secret on the host using the host's key and the
65 peer's public key:
66
67 /* It is assumed that the host_key and peer_pub_key are set up */
68 void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key)
69 {
70 unsigned int pad = 1;
71 OSSL_PARAM params[2];
72 unsigned char *secret = NULL;
73 size_t secret_len = 0;
74 EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
75
76 EVP_PKEY_derive_init(dctx);
77
78 /* Optionally set the padding */
79 params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
80 params[1] = OSSL_PARAM_construct_end();
81 EVP_PKEY_CTX_set_params(dctx, params);
82
83 EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
84
85 /* Get the size by passing NULL as the buffer */
86 EVP_PKEY_derive(dctx, NULL, &secret_len);
87 secret = OPENSSL_zalloc(secret_len);
88
89 EVP_PKEY_derive(dctx, secret, &secret_len);
90 ...
91 OPENSSL_clear_free(secret, secret_len);
92 EVP_PKEY_CTX_free(dctx);
93 }
94
95 Very similar code can be used by the peer to derive the same shared
96 secret using the host's public key and the peer's generated key pair.
97
99 EVP_PKEY-DH(7), EVP_PKEY-FFC(7), EVP_PKEY(3), provider-keyexch(7),
100 provider-keymgmt(7), OSSL_PROVIDER-default(7), OSSL_PROVIDER-FIPS(7),
101
103 Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
104
105 Licensed under the Apache License 2.0 (the "License"). You may not use
106 this file except in compliance with the License. You can obtain a copy
107 in the file LICENSE in the source distribution or at
108 <https://www.openssl.org/source/license.html>.
109
110
111
1123.0.9 2023-07-27 EVP_KEYEXCH-DH(7ossl)