A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.1 KiB

  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_CTX_set_hkdf_md, EVP_PKEY_CTX_set1_hkdf_salt,
  4. EVP_PKEY_CTX_set1_hkdf_key, EVP_PKEY_CTX_add1_hkdf_info -
  5. HMAC-based Extract-and-Expand key derivation algorithm
  6. =head1 SYNOPSIS
  7. #include <openssl/kdf.h>
  8. int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *pctx, const EVP_MD *md);
  9. int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *pctx, unsigned char *salt,
  10. int saltlen);
  11. int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *pctx, unsigned char *key,
  12. int keylen);
  13. int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *pctx, unsigned char *info,
  14. int infolen);
  15. =head1 DESCRIPTION
  16. The EVP_PKEY_HKDF algorithm implements the HKDF key derivation function.
  17. HKDF follows the "extract-then-expand" paradigm, where the KDF logically
  18. consists of two modules. The first stage takes the input keying material
  19. and "extracts" from it a fixed-length pseudorandom key K. The second stage
  20. "expands" the key K into several additional pseudorandom keys (the output
  21. of the KDF).
  22. EVP_PKEY_set_hkdf_md() sets the message digest associated with the HKDF.
  23. EVP_PKEY_CTX_set1_hkdf_salt() sets the salt to B<saltlen> bytes of the
  24. buffer B<salt>. Any existing value is replaced.
  25. EVP_PKEY_CTX_set_hkdf_key() sets the key to B<keylen> bytes of the buffer
  26. B<key>. Any existing value is replaced.
  27. EVP_PKEY_CTX_add1_hkdf_info() sets the info value to B<infolen> bytes of the
  28. buffer B<info>. If a value is already set, it is appended to the existing
  29. value.
  30. =head1 STRING CTRLS
  31. HKDF also supports string based control operations via
  32. L<EVP_PKEY_CTX_ctrl_str(3)>.
  33. The B<type> parameter "md" uses the supplied B<value> as the name of the digest
  34. algorithm to use.
  35. The B<type> parameters "salt", "key" and "info" use the supplied B<value>
  36. parameter as a B<seed>, B<key> or B<info> value.
  37. The names "hexsalt", "hexkey" and "hexinfo" are similar except they take a hex
  38. string which is converted to binary.
  39. =head1 NOTES
  40. All these functions are implemented as macros.
  41. A context for HKDF can be obtained by calling:
  42. EVP_PKEY_CTX *pctx = EVP_PKEY_new_id(EVP_PKEY_HKDF, NULL);
  43. The digest, key, salt and info values must be set before a key is derived or
  44. an error occurs.
  45. The total length of the info buffer cannot exceed 1024 bytes in length: this
  46. should be more than enough for any normal use of HKDF.
  47. The output length of the KDF is specified via the length parameter to the
  48. L<EVP_PKEY_derive(3)> function.
  49. Since the HKDF output length is variable, passing a B<NULL> buffer as a means
  50. to obtain the requisite length is not meaningful with HKDF.
  51. Instead, the caller must allocate a buffer of the desired length, and pass that
  52. buffer to L<EVP_PKEY_derive(3)> along with (a pointer initialized to) the
  53. desired length.
  54. Optimised versions of HKDF can be implemented in an ENGINE.
  55. =head1 RETURN VALUES
  56. All these functions return 1 for success and 0 or a negative value for failure.
  57. In particular a return value of -2 indicates the operation is not supported by
  58. the public key algorithm.
  59. =head1 EXAMPLE
  60. This example derives 10 bytes using SHA-256 with the secret key "secret",
  61. salt value "salt" and info value "label":
  62. EVP_PKEY_CTX *pctx;
  63. unsigned char out[10];
  64. size_t outlen = sizeof(out);
  65. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  66. if (EVP_PKEY_derive_init(pctx) <= 0)
  67. /* Error */
  68. if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0)
  69. /* Error */
  70. if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0)
  71. /* Error */
  72. if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0)
  73. /* Error */
  74. if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 6) <= 0)
  75. /* Error */
  76. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
  77. /* Error */
  78. =head1 CONFORMING TO
  79. RFC 5869
  80. =head1 SEE ALSO
  81. L<EVP_PKEY_CTX_new(3)>,
  82. L<EVP_PKEY_CTX_ctrl_str(3)>,
  83. L<EVP_PKEY_derive(3)>
  84. =head1 COPYRIGHT
  85. Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  86. Licensed under the OpenSSL license (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