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