EVP_KEYEXCH-DH.pod 4.3 KB

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