2
0

EVP_KEYEXCH-DH.pod 3.3 KB

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