nrf_crypto_hkdf.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_CRYPTO_HKDF_H__
  41. #define NRF_CRYPTO_HKDF_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_hkdf HMAC based Key Derivation Function (HKDF) related functions
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Provides functions to generate HMAC based Key Derivation Function (HKDF).
  49. *
  50. * @details Provides functions to generate HMAC based Key Derivation Function (HKDF) using
  51. * one of the supported hash algorithms. This layer is independent of backend crypto library.
  52. * The HKDF module does not have a backend configuration, as it uses the nrf_crypto_hmac API,
  53. * including the backend configured for HMAC in @ref sdk_config.
  54. */
  55. #include <stdint.h>
  56. #include "sdk_common.h"
  57. #include "nrf_crypto_hmac.h"
  58. #include "nrf_crypto_hkdf.h"
  59. #include "nrf_crypto_types.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**
  64. * @brief Enumeration of HKDF modes.
  65. */
  66. typedef enum
  67. {
  68. NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND, //!< HKDF Extract and expand mode (normal).
  69. NRF_CRYPTO_HKDF_EXPAND_ONLY //!< HKDF Expand only mode.
  70. } nrf_crypto_hkdf_mode_t;
  71. /**
  72. * @brief Integrated HKDF calculation function
  73. *
  74. * @details This HKDF calculation function uses the nrf_crypto HMAC frontend directly.
  75. * The backend is selected by configuring the HMAC backend in @ref sdk_config.
  76. *
  77. * @param[in,out] p_context Pointer to context structure. Context memory will be
  78. * allocated internally if the context pointer is NULL.
  79. * @param[in] p_info Pointer to static info structure. This defines the algorithm.
  80. * This should be either @ref g_nrf_crypto_hmac_sha256_info or
  81. * @ref g_nrf_crypto_hmac_sha512_info.
  82. * @param[out] p_output_key Pointer to buffer to hold the output key material.
  83. * @param[in,out] p_output_key_size Pointer to the length of the wanted output key material as input
  84. * and actual length of the output material as output. Can be any
  85. * number between 1 and the hash digest size multiplied by 255
  86. * (65280 for SHA-256 or 130560 for SHA-512). The p_output_key
  87. * buffer must be large enough to hold this value.
  88. * @param[in] p_input_key Pointer to buffer holding the input key material.
  89. * @param[in] input_key_size Length of the input key material.
  90. * @param[in] p_salt Pointer to buffer of nonsecret random salt data. Set to NULL in
  91. * order to use the default salt defined by RFC 5869 (all zero
  92. * array of hash digest size) or if salt is not used (expand only).
  93. * @param[in] salt_size Length of the salt. Must be > 0 unless default salt is used, or
  94. * in case mode is set to @ref NRF_CRYPTO_HKDF_EXPAND_ONLY.
  95. * @param[in] p_ainfo Pointer to optional application specific information.
  96. * (set to NULL and set ainfo_size to 0 if unused).
  97. * @param[in] ainfo_size Length of the additional information.
  98. * @param[in] mode Set to @ref NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND for normal mode.
  99. * Alternatively, set to @ref NRF_CRYPTO_HKDF_EXPAND_ONLY to skip
  100. * the extraction step.
  101. *
  102. * @retval NRF_SUCCESS Output key material hash was successfully calculated.
  103. * @retval NRF_ERROR_CRYPTO_INPUT_NULL If p_input_key was NULL.
  104. * @retval NRF_ERROR_CRYPTO_INPUT_LENGTH If input_key_size or salt_size was invalid.
  105. * @retval NRF_ERROR_CRYPTO_OUTPUT_NULL If p_output_key_sizen was NULL.
  106. * @retval NRF_ERROR_CRYPTO_OUTPUT_LENGTH If *p_output_key_size is 0.
  107. * @retval NRF_ERROR_CRYPTO_ALLOC_FAILED Unable to allocate memory for the context.
  108. * @retval NRF_ERROR_CRYPTO_INTERNAL An error occurred in the crypto backend.
  109. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  110. * nrf_crypto backend was busy. Please rerun the
  111. * cryptographic routine at a later time. CC310 only.
  112. */
  113. ret_code_t nrf_crypto_hkdf_calculate(nrf_crypto_hmac_context_t * const p_context,
  114. nrf_crypto_hmac_info_t const * p_info,
  115. uint8_t * const p_output_key,
  116. size_t * const p_output_key_size,
  117. uint8_t const * const p_input_key,
  118. size_t input_key_size,
  119. uint8_t const * p_salt,
  120. size_t salt_size,
  121. uint8_t const * const p_ainfo,
  122. size_t ainfo_size,
  123. nrf_crypto_hkdf_mode_t mode);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. /**@} */
  128. #endif // #ifndef NRF_CRYPTO_HKDF_H__