EVP_KEYEXCH-DH.pod 4.0 KB

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