EVP_KDF-HKDF.pod 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. =pod
  2. =head1 NAME
  3. EVP_KDF-HKDF - The HKDF EVP_KDF implementation
  4. =head1 DESCRIPTION
  5. Support for computing the B<HKDF> KDF through the B<EVP_KDF> API.
  6. The EVP_KDF-HKDF algorithm implements the HKDF key derivation function.
  7. HKDF follows the "extract-then-expand" paradigm, where the KDF logically
  8. consists of two modules. The first stage takes the input keying material
  9. and "extracts" from it a fixed-length pseudorandom key K. The second stage
  10. "expands" the key K into several additional pseudorandom keys (the output
  11. of the KDF).
  12. =head2 Identity
  13. "HKDF" is the name for this implementation; it
  14. can be used with the EVP_KDF_fetch() function.
  15. =head2 Supported parameters
  16. The supported parameters are:
  17. =over 4
  18. =item "properties" (B<OSSL_KDF_PARAM_PROPERTIES>) <UTF8 string>
  19. =item "digest" (B<OSSL_KDF_PARAM_DIGEST>) <UTF8 string>
  20. =item "key" (B<OSSL_KDF_PARAM_KEY>) <octet string>
  21. =item "salt" (B<OSSL_KDF_PARAM_SALT>) <octet string>
  22. These parameters work as described in L<EVP_KDF(3)/PARAMETERS>.
  23. =item "info" (B<OSSL_KDF_PARAM_INFO>) <octet string>
  24. This parameter sets the info value.
  25. The length of the context info buffer cannot exceed 1024 bytes;
  26. this should be more than enough for any normal use of HKDF.
  27. =item "mode" (B<OSSL_KDF_PARAM_MODE>) <UTF8 string> or <integer>
  28. This parameter sets the mode for the HKDF operation.
  29. There are three modes that are currently defined:
  30. =over 4
  31. =item "EXTRACT_AND_EXPAND" or B<EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND>
  32. This is the default mode. Calling L<EVP_KDF_derive(3)> on an EVP_KDF_CTX set
  33. up for HKDF will perform an extract followed by an expand operation in one go.
  34. The derived key returned will be the result after the expand operation. The
  35. intermediate fixed-length pseudorandom key K is not returned.
  36. In this mode the digest, key, salt and info values must be set before a key is
  37. derived otherwise an error will occur.
  38. =item "EXTRACT_ONLY" or B<EVP_KDF_HKDF_MODE_EXTRACT_ONLY>
  39. In this mode calling L<EVP_KDF_derive(3)> will just perform the extract
  40. operation. The value returned will be the intermediate fixed-length pseudorandom
  41. key K. The I<keylen> parameter must match the size of K, which can be looked
  42. up by calling EVP_KDF_CTX_get_kdf_size() after setting the mode and digest.
  43. The digest, key and salt values must be set before a key is derived otherwise
  44. an error will occur.
  45. =item "EXPAND_ONLY" or B<EVP_KDF_HKDF_MODE_EXPAND_ONLY>
  46. In this mode calling L<EVP_KDF_derive(3)> will just perform the expand
  47. operation. The input key should be set to the intermediate fixed-length
  48. pseudorandom key K returned from a previous extract operation.
  49. The digest, key and info values must be set before a key is derived otherwise
  50. an error will occur.
  51. =back
  52. =back
  53. =head1 NOTES
  54. A context for HKDF can be obtained by calling:
  55. EVP_KDF *kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
  56. EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
  57. The output length of an HKDF expand operation is specified via the I<keylen>
  58. parameter to the L<EVP_KDF_derive(3)> function. When using
  59. EVP_KDF_HKDF_MODE_EXTRACT_ONLY the I<keylen> parameter must equal the size of
  60. the intermediate fixed-length pseudorandom key otherwise an error will occur.
  61. For that mode, the fixed output size can be looked up by calling EVP_KDF_CTX_get_kdf_size()
  62. after setting the mode and digest on the B<EVP_KDF_CTX>.
  63. =head1 EXAMPLES
  64. This example derives 10 bytes using SHA-256 with the secret key "secret",
  65. salt value "salt" and info value "label":
  66. EVP_KDF *kdf;
  67. EVP_KDF_CTX *kctx;
  68. unsigned char out[10];
  69. OSSL_PARAM params[5], *p = params;
  70. kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
  71. kctx = EVP_KDF_CTX_new(kdf);
  72. EVP_KDF_free(kdf);
  73. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  74. SN_sha256, strlen(SN_sha256));
  75. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  76. "secret", (size_t)6);
  77. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
  78. "label", (size_t)5);
  79. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  80. "salt", (size_t)4);
  81. *p = OSSL_PARAM_construct_end();
  82. if (EVP_KDF_derive(kctx, out, sizeof(out), params) <= 0) {
  83. error("EVP_KDF_derive");
  84. }
  85. EVP_KDF_CTX_free(kctx);
  86. =head1 CONFORMING TO
  87. RFC 5869
  88. =head1 SEE ALSO
  89. L<EVP_KDF(3)>,
  90. L<EVP_KDF_CTX_new(3)>,
  91. L<EVP_KDF_CTX_free(3)>,
  92. L<EVP_KDF_CTX_get_kdf_size(3)>,
  93. L<EVP_KDF_CTX_set_params(3)>,
  94. L<EVP_KDF_derive(3)>,
  95. L<EVP_KDF(3)/PARAMETERS>,
  96. L<EVP_KDF-TLS13_KDF(7)>
  97. =head1 COPYRIGHT
  98. Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  99. Licensed under the Apache License 2.0 (the "License"). You may not use
  100. this file except in compliance with the License. You can obtain a copy
  101. in the file LICENSE in the source distribution or at
  102. L<https://www.openssl.org/source/license.html>.
  103. =cut