1=pod
2
3=head1 NAME
4
5EVP_KEYEXCH-DH
6- DH Key Exchange algorithm support
7
8=head1 DESCRIPTION
9
10Key exchange support for the B<DH> key type.
11
12=head2 DH key exchange parameters
13
14=over 4
15
16=item "pad" (B<OSSL_EXCHANGE_PARAM_PAD>) <unsigned integer>
17
18See L<provider-keyexch(7)/Common Key Exchange parameters>.
19
20=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string>
21
22Sets the User Key Material to be used as part of the selected Key Derivation
23Function associated with the given key exchange ctx.
24
25=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string ptr>
26
27Gets a pointer to the User Key Material to be used as part of the selected
28Key Derivation Function associated with the given key exchange ctx. Providers
29usually do not need to support this gettable parameter as its sole purpose
30is to support functionality of the deprecated EVP_PKEY_CTX_get0_dh_kdf_ukm()
31function.
32
33=back
34
35=head1 EXAMPLES
36
37The examples assume a host and peer both generate keys using the same
38named group (or domain parameters). See L<EVP_PKEY-DH(7)/Examples>.
39Both the host and peer transfer their public key to each other.
40
41To convert the peer's generated key pair to a public key in DER format in order
42to transfer to the host:
43
44    EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */
45    unsigned char *peer_pub_der = NULL;
46    int peer_pub_der_len;
47
48    peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der);
49    ...
50    OPENSSL_free(peer_pub_der);
51
52To convert the received peer's public key from DER format on the host:
53
54    const unsigned char *pd = peer_pub_der;
55    EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len);
56    ...
57    EVP_PKEY_free(peer_pub_key);
58
59To derive a shared secret on the host using the host's key and the peer's public
60key:
61    /* It is assumed that the host_key and peer_pub_key are set up */
62    void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key)
63    {
64        unsigned int pad = 1;
65        OSSL_PARAM params[2];
66        unsigned char *secret = NULL;
67        size_t secret_len = 0;
68        EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
69
70        EVP_PKEY_derive_init(dctx);
71
72        /* Optionally set the padding */
73        params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
74        params[1] = OSSL_PARAM_construct_end();
75        EVP_PKEY_CTX_set_params(dctx, params);
76
77        EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
78
79        /* Get the size by passing NULL as the buffer */
80        EVP_PKEY_derive(dctx, NULL, &secret_len);
81        secret = OPENSSL_zalloc(secret_len);
82
83        EVP_PKEY_derive(dctx, secret, &secret_len);
84        ...
85        OPENSSL_clear_free(secret, secret_len);
86        EVP_PKEY_CTX_free(dctx);
87    }
88
89Very similar code can be used by the peer to derive the same shared secret
90using the host's public key and the peer's generated key pair.
91
92=head1 SEE ALSO
93
94L<EVP_PKEY-DH(7)>,
95L<EVP_PKEY-FFC(7)>,
96L<EVP_PKEY(3)>,
97L<provider-keyexch(7)>,
98L<provider-keymgmt(7)>,
99L<OSSL_PROVIDER-default(7)>,
100L<OSSL_PROVIDER-FIPS(7)>,
101
102=head1 COPYRIGHT
103
104Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
105
106Licensed under the Apache License 2.0 (the "License").  You may not use
107this file except in compliance with the License.  You can obtain a copy
108in the file LICENSE in the source distribution or at
109L<https://www.openssl.org/source/license.html>.
110
111=cut
112