EVP_KEYEXCH-ECDH.pod 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. =pod
  2. =head1 NAME
  3. EVP_KEYEXCH-ECDH - ECDH Key Exchange algorithm support
  4. =head1 DESCRIPTION
  5. Key exchange support for the B<ECDH> key type.
  6. =head2 ECDH Key Exchange parameters
  7. =over 4
  8. =item "ecdh-cofactor-mode" (B<OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE>) <integer>
  9. Sets or gets the ECDH mode of operation for the associated key exchange ctx.
  10. In the context of an Elliptic Curve Diffie-Hellman key exchange, this parameter
  11. can be used to select between the plain Diffie-Hellman (DH) or Cofactor
  12. Diffie-Hellman (CDH) variants of the key exchange algorithm.
  13. When setting, the value should be 1, 0 or -1, respectively forcing cofactor mode
  14. on, off, or resetting it to the default for the private key associated with the
  15. given key exchange ctx.
  16. When getting, the value should be either 1 or 0, respectively signaling if the
  17. cofactor mode is on or off.
  18. See also L<provider-keymgmt(7)> for the related
  19. B<OSSL_PKEY_PARAM_USE_COFACTOR_ECDH> parameter that can be set on a
  20. per-key basis.
  21. =item "kdf-type" (B<OSSL_EXCHANGE_PARAM_KDF_TYPE>) <UTF8 string>
  22. Sets or gets the Key Derivation Function type to apply within the associated key
  23. exchange ctx.
  24. =item "kdf-digest" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST>) <UTF8 string>
  25. Sets or gets the Digest algorithm to be used as part of the Key Derivation Function
  26. associated with the given key exchange ctx.
  27. =item "kdf-digest-props" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS>) <UTF8 string>
  28. Sets properties to be used upon look up of the implementation for the selected
  29. Digest algorithm for the Key Derivation Function associated with the given key
  30. exchange ctx.
  31. =item "kdf-outlen" (B<OSSL_EXCHANGE_PARAM_KDF_OUTLEN>) <unsigned integer>
  32. Sets or gets the desired size for the output of the chosen Key Derivation Function
  33. associated with the given key exchange ctx.
  34. The length of the "kdf-outlen" parameter should not exceed that of a B<size_t>.
  35. =item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string>
  36. Sets the User Key Material to be used as part of the selected Key Derivation
  37. Function associated with the given key exchange ctx.
  38. =item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string ptr>
  39. Gets a pointer to the User Key Material to be used as part of the selected
  40. Key Derivation Function associated with the given key exchange ctx. Providers
  41. usually do not need to support this gettable parameter as its sole purpose
  42. is to support functionality of the deprecated EVP_PKEY_CTX_get0_ecdh_kdf_ukm()
  43. function.
  44. =back
  45. =head1 EXAMPLES
  46. Keys for the host and peer must be generated as shown in
  47. L<EVP_PKEY-EC(7)/Examples> using the same curve name.
  48. The code to generate a shared secret for the normal case is identical to
  49. L<EVP_KEYEXCH-DH(7)/Examples>.
  50. To derive a shared secret on the host using the host's key and the peer's public
  51. key but also using X963KDF with a user key material:
  52. /* It is assumed that the host_key, peer_pub_key and ukm are set up */
  53. void derive_secret(EVP_PKEY *host_key, EVP_PKEY *peer_key,
  54. unsigned char *ukm, size_t ukm_len)
  55. {
  56. unsigned char secret[64];
  57. size_t out_len = sizeof(secret);
  58. size_t secret_len = out_len;
  59. unsigned int pad = 1;
  60. OSSL_PARAM params[6];
  61. EVP_PKET_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
  62. EVP_PKEY_derive_init(dctx);
  63. params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
  64. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
  65. "X963KDF", 0);
  66. params[2] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
  67. "SHA1", 0);
  68. params[3] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
  69. &out_len);
  70. params[4] = OSSL_PARAM_construct_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM,
  71. ukm, ukm_len);
  72. params[5] = OSSL_PARAM_construct_end();
  73. EVP_PKEY_CTX_set_params(dctx, params);
  74. EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
  75. EVP_PKEY_derive(dctx, secret, &secret_len);
  76. ...
  77. OPENSSL_clear_free(secret, secret_len);
  78. EVP_PKEY_CTX_free(dctx);
  79. }
  80. =head1 SEE ALSO
  81. L<EVP_PKEY-EC(7)>
  82. L<EVP_PKEY(3)>,
  83. L<provider-keyexch(7)>,
  84. L<provider-keymgmt(7)>,
  85. L<OSSL_PROVIDER-default(7)>,
  86. L<OSSL_PROVIDER-FIPS(7)>,
  87. =head1 COPYRIGHT
  88. Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  89. Licensed under the Apache License 2.0 (the "License"). You may not use
  90. this file except in compliance with the License. You can obtain a copy
  91. in the file LICENSE in the source distribution or at
  92. L<https://www.openssl.org/source/license.html>.
  93. =cut