hkdf.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_HKDF_H
  15. #define OPENSSL_HEADER_HKDF_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. // HKDF.
  21. // HKDF computes HKDF (as specified by RFC 5869) of initial keying material
  22. // |secret| with |salt| and |info| using |digest|, and outputs |out_len| bytes
  23. // to |out_key|. It returns one on success and zero on error.
  24. //
  25. // HKDF is an Extract-and-Expand algorithm. It does not do any key stretching,
  26. // and as such, is not suited to be used alone to generate a key from a
  27. // password.
  28. OPENSSL_EXPORT int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
  29. const uint8_t *secret, size_t secret_len,
  30. const uint8_t *salt, size_t salt_len,
  31. const uint8_t *info, size_t info_len);
  32. // HKDF_extract computes a HKDF PRK (as specified by RFC 5869) from initial
  33. // keying material |secret| and salt |salt| using |digest|, and outputs
  34. // |out_len| bytes to |out_key|. The maximum output size is |EVP_MAX_MD_SIZE|.
  35. // It returns one on success and zero on error.
  36. //
  37. // WARNING: This function orders the inputs differently from RFC 5869
  38. // specification. Double-check which parameter is the secret/IKM and which is
  39. // the salt when using.
  40. OPENSSL_EXPORT int HKDF_extract(uint8_t *out_key, size_t *out_len,
  41. const EVP_MD *digest, const uint8_t *secret,
  42. size_t secret_len, const uint8_t *salt,
  43. size_t salt_len);
  44. // HKDF_expand computes a HKDF OKM (as specified by RFC 5869) of length
  45. // |out_len| from the PRK |prk| and info |info| using |digest|, and outputs
  46. // the result to |out_key|. It returns one on success and zero on error.
  47. OPENSSL_EXPORT int HKDF_expand(uint8_t *out_key, size_t out_len,
  48. const EVP_MD *digest, const uint8_t *prk,
  49. size_t prk_len, const uint8_t *info,
  50. size_t info_len);
  51. #if defined(__cplusplus)
  52. } // extern C
  53. #endif
  54. #define HKDF_R_OUTPUT_TOO_LARGE 100
  55. #endif // OPENSSL_HEADER_HKDF_H