CORE: Add OPENSSL_CTX_set0_default(), to set a default library context
[openssl.git] / doc / man7 / EVP_KEYEXCH-DH.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_KEYEXCH-DH
6 - DH Key Exchange algorithm support
7
8 =head1 DESCRIPTION
9
10 Key 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
18 See L<provider-keyexch(7)/Common Key Exchange parameters>.
19
20 =back
21
22 =head1 EXAMPLES
23
24 The examples assume a host and peer both generate keys using the same
25 named group (or domain parameters). See L<EVP_PKEY-DH(7)/Examples>.
26 Both the host and peer transfer their public key to each other.
27
28 To convert the peer's generated key pair to a public key in DER format in order
29 to transfer to the host:
30
31     EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */
32     unsigned char *peer_pub_der = NULL;
33     int peer_pub_der_len;
34
35     peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der);
36     ...
37     OPENSSL_free(peer_pub_der);
38
39 To convert the received peer's public key from DER format on the host:
40
41     const unsigned char *pd = peer_pub_der;
42     EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len);
43     ...
44     EVP_PKEY_free(peer_pub_key);
45
46 To derive a shared secret on the host using the host's key and the peer's public
47 key:
48     /* It is assumed that the host_key and peer_pub_key are set up */
49     void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key)
50     {
51         unsigned int pad = 1;
52         OSSL_PARAM params[2];
53         unsigned char *secret = NULL;
54         size_t secret_len = 0;
55         EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
56
57         EVP_PKEY_derive_init(dctx);
58
59         /* Optionally set the padding */
60         params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
61         params[1] = OSSL_PARAM_construct_end();
62         EVP_PKEY_CTX_set_params(dctx, params);
63
64         EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
65
66         /* Get the size by passing NULL as the buffer */
67         EVP_PKEY_derive(dctx, NULL, &secret_len);
68         secret = OPENSSL_zalloc(secret_len);
69
70         EVP_PKEY_derive(dctx, secret, &secret_len);
71         ...
72         OPENSSL_clear_free(secret, secret_len);
73         EVP_PKEY_CTX_free(dctx);
74     }
75
76 Very similar code can be used by the peer to derive the same shared secret
77 using the host's public key and the peer's generated key pair.
78
79 =head1 SEE ALSO
80
81 L<EVP_PKEY-DH(7)>,
82 L<EVP_PKEY-FFC(7)>,
83 L<EVP_PKEY(3)>,
84 L<provider-keyexch(7)>,
85 L<provider-keymgmt(7)>,
86 L<OSSL_PROVIDER-default(7)>,
87 L<OSSL_PROVIDER-FIPS(7)>,
88
89 =head1 COPYRIGHT
90
91 Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
92
93 Licensed under the Apache License 2.0 (the "License").  You may not use
94 this file except in compliance with the License.  You can obtain a copy
95 in the file LICENSE in the source distribution or at
96 L<https://www.openssl.org/source/license.html>.
97
98 =cut