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 36See L<provider-keyexch(7)/Common Key Exchange parameters>. 37 38=item "kdf-digest" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST>) <UTF8 string> 39 40See L<provider-keyexch(7)/Common Key Exchange parameters>. 41 42=item "kdf-digest-props" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS>) <UTF8 string> 43 44See L<provider-keyexch(7)/Common Key Exchange parameters>. 45 46=item "kdf-outlen" (B<OSSL_EXCHANGE_PARAM_KDF_OUTLEN>) <unsigned integer> 47 48See L<provider-keyexch(7)/Common Key Exchange parameters>. 49 50=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string> 51 52See L<provider-keyexch(7)/Common Key Exchange parameters>. 53 54=item "cekalg" (B<OSSL_KDF_PARAM_CEK_ALG>) <octet string ptr> 55 56See L<provider-kdf(7)/KDF Parameters>. 57 58=back 59 60=head1 EXAMPLES 61 62The examples assume a host and peer both generate keys using the same 63named group (or domain parameters). See L<EVP_PKEY-DH(7)/Examples>. 64Both the host and peer transfer their public key to each other. 65 66To convert the peer's generated key pair to a public key in DER format in order 67to transfer to the host: 68 69 EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */ 70 unsigned char *peer_pub_der = NULL; 71 int peer_pub_der_len; 72 73 peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der); 74 ... 75 OPENSSL_free(peer_pub_der); 76 77To convert the received peer's public key from DER format on the host: 78 79 const unsigned char *pd = peer_pub_der; 80 EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len); 81 ... 82 EVP_PKEY_free(peer_pub_key); 83 84To derive a shared secret on the host using the host's key and the peer's public 85key: 86 87 /* It is assumed that the host_key and peer_pub_key are set up */ 88 void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key) 89 { 90 unsigned int pad = 1; 91 OSSL_PARAM params[2]; 92 unsigned char *secret = NULL; 93 size_t secret_len = 0; 94 EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL); 95 96 EVP_PKEY_derive_init(dctx); 97 98 /* Optionally set the padding */ 99 params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad); 100 params[1] = OSSL_PARAM_construct_end(); 101 EVP_PKEY_CTX_set_params(dctx, params); 102 103 EVP_PKEY_derive_set_peer(dctx, peer_pub_key); 104 105 /* Get the size by passing NULL as the buffer */ 106 EVP_PKEY_derive(dctx, NULL, &secret_len); 107 secret = OPENSSL_zalloc(secret_len); 108 109 EVP_PKEY_derive(dctx, secret, &secret_len); 110 ... 111 OPENSSL_clear_free(secret, secret_len); 112 EVP_PKEY_CTX_free(dctx); 113 } 114 115Very similar code can be used by the peer to derive the same shared secret 116using the host's public key and the peer's generated key pair. 117 118=head1 SEE ALSO 119 120L<EVP_PKEY-DH(7)>, 121L<EVP_PKEY-FFC(7)>, 122L<EVP_PKEY(3)>, 123L<provider-keyexch(7)>, 124L<provider-keymgmt(7)>, 125L<OSSL_PROVIDER-default(7)>, 126L<OSSL_PROVIDER-FIPS(7)>, 127 128=head1 COPYRIGHT 129 130Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved. 131 132Licensed under the Apache License 2.0 (the "License"). You may not use 133this file except in compliance with the License. You can obtain a copy 134in the file LICENSE in the source distribution or at 135L<https://www.openssl.org/source/license.html>. 136 137=cut 138