EVP_PKEY_CTX_set_hkdf_md.pod 5.5 KB

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