EVP_KEYEXCH-DH.pod 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. =back
  12. =head1 EXAMPLES
  13. The examples assume a host and peer both generate keys using the same
  14. named group (or domain parameters). See L<EVP_PKEY-DH(7)/Examples>.
  15. Both the host and peer transfer their public key to each other.
  16. To convert the peer's generated key pair to a public key in DER format in order
  17. to transfer to the host:
  18. EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */
  19. unsigned char *peer_pub_der = NULL;
  20. int peer_pub_der_len;
  21. peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der);
  22. ...
  23. OPENSSL_free(peer_pub_der);
  24. To convert the received peer's public key from DER format on the host:
  25. const unsigned char *pd = peer_pub_der;
  26. EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len);
  27. ...
  28. EVP_PKEY_free(peer_pub_key);
  29. To derive a shared secret on the host using the host's key and the peer's public
  30. key:
  31. /* It is assumed that the host_key and peer_pub_key are set up */
  32. void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key)
  33. {
  34. unsigned int pad = 1;
  35. OSSL_PARAM params[2];
  36. unsigned char *secret = NULL;
  37. size_t secret_len = 0;
  38. EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
  39. EVP_PKEY_derive_init(dctx);
  40. /* Optionally set the padding */
  41. params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
  42. params[1] = OSSL_PARAM_construct_end();
  43. EVP_PKEY_CTX_set_params(dctx, params);
  44. EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
  45. /* Get the size by passing NULL as the buffer */
  46. EVP_PKEY_derive(dctx, NULL, &secret_len);
  47. secret = OPENSSL_zalloc(secret_len);
  48. EVP_PKEY_derive(dctx, secret, &secret_len);
  49. ...
  50. OPENSSL_clear_free(secret, secret_len);
  51. EVP_PKEY_CTX_free(dctx);
  52. }
  53. Very similar code can be used by the peer to derive the same shared secret
  54. using the host's public key and the peer's generated key pair.
  55. =head1 SEE ALSO
  56. L<EVP_PKEY-DH(7)>,
  57. L<EVP_PKEY-FFC(7)>,
  58. L<EVP_PKEY(3)>,
  59. L<provider-keyexch(7)>,
  60. L<provider-keymgmt(7)>,
  61. L<OSSL_PROVIDER-default(7)>,
  62. L<OSSL_PROVIDER-FIPS(7)>,
  63. =head1 COPYRIGHT
  64. Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  65. Licensed under the Apache License 2.0 (the "License"). You may not use
  66. this file except in compliance with the License. You can obtain a copy
  67. in the file LICENSE in the source distribution or at
  68. L<https://www.openssl.org/source/license.html>.
  69. =cut