nrf_crypto_hmac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_HMAC_H__
  41. #define NRF_CRYPTO_HMAC_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_hmac Hash-based message authentication code (HMAC) related functions
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Provides functions to generate Hash-based message authentication code (HMAC).
  49. *
  50. * @details Provides functions to generate Hash-based message authentication code (HMAC) using
  51. * one of the supported hash algorithms. This layer is independent of backend crypto library.
  52. */
  53. #include <stdint.h>
  54. #include "sdk_common.h"
  55. #include "nrf_crypto_types.h"
  56. #include "nrf_crypto_hmac_backend.h"
  57. #include "nrf_crypto_hmac_shared.h"
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * @brief Information structures used to select the specific algorithm (SHA-256)
  63. *
  64. * @details The information structure is used in a generic way but is populated by the backend,
  65. * and contains backend specific data. */
  66. extern const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha256_info;
  67. /**
  68. * @brief Information structures used to select the specific algorithm (SHA-512)
  69. *
  70. * @details The information structure is used in a generic way but is populated by the backend,
  71. * and contains backend specific data.
  72. */
  73. extern const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha512_info;
  74. /**
  75. * @brief Context type for HMAC.
  76. *
  77. * @note The size of this type is scaled for the largest HMAC backend context that is
  78. * enabled in @ref sdk_config.
  79. */
  80. typedef nrf_crypto_backend_hmac_context_t nrf_crypto_hmac_context_t;
  81. /**
  82. * @brief Initialize context object for HMAC
  83. *
  84. * @details Use to initialize a context once it has been allocated.
  85. *
  86. * @note Must be called before @ref nrf_crypto_hmac_update. Can also be called after
  87. * @ref nrf_crypto_hmac_finalize order to start a new HMAC calculation re-using an
  88. * existing context object.
  89. *
  90. * @param[in,out] p_context Pointer to context structure.
  91. * @param[in] p_info Pointer to static info structure. This defines the algorithm.
  92. * This should be either @ref g_nrf_crypto_hmac_sha256_info or
  93. * @ref g_nrf_crypto_hmac_sha512_info.
  94. * @param[in] p_key HMAC key.
  95. * @param[in] key_size Length of the HMAC key in bytes.
  96. *
  97. * @retval NRF_SUCCESS Data successfully consumed.
  98. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL If p_context has not been initialized.
  99. * @retval NRF_ERROR_CRYPTO_INPUT_NULL If p_info or p_key was NULL.
  100. * @retval NRF_ERROR_CRYPTO_INPUT_LENGTH If key_size was invalid.
  101. * @retval NRF_ERROR_CRYPTO_INPUT_LOCATION Input data not in RAM (CC310 only).
  102. * @retval NRF_ERROR_CRYPTO_INTERNAL An error occurred in the crypto backend.
  103. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  104. * nrf_crypto backend was busy. Please rerun
  105. * the cryptographic routine at a later time.
  106. * CC310 only.
  107. */
  108. ret_code_t nrf_crypto_hmac_init(nrf_crypto_hmac_context_t * const p_context,
  109. nrf_crypto_hmac_info_t const * p_info,
  110. uint8_t const * p_key,
  111. size_t key_size);
  112. /**
  113. * @brief Feed data to HMAC algorithm.
  114. *
  115. * @note Must be called after @ref nrf_crypto_hmac_init and before @ref nrf_crypto_hmac_finalize.
  116. * Can be called repeatedly to consume data as it arrives.
  117. *
  118. * @param[in,out] p_context Context pointer.
  119. * @param[in] p_data Pointer to input data buffer.
  120. * @param[in] data_size Length of input data.
  121. *
  122. * @retval NRF_SUCCESS Data successfully consumed.
  123. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL If p_context has not been initialized.
  124. * @retval NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED If p_data was NULL.
  125. * @retval NRF_ERROR_CRYPTO_INPUT_NULL If p_data was NULL.
  126. * @retval NRF_ERROR_CRYPTO_INPUT_LENGTH If size was invalid.
  127. * @retval NRF_ERROR_CRYPTO_INPUT_LOCATION Input data not in RAM (CC310 only).
  128. * @retval NRF_ERROR_CRYPTO_INTERNAL An error occurred in the crypto backend.
  129. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  130. * nrf_crypto backend was busy. Please rerun
  131. * the cryptographic routine at a later time.
  132. * CC310 only.
  133. */
  134. ret_code_t nrf_crypto_hmac_update(nrf_crypto_hmac_context_t * const p_context,
  135. uint8_t const * p_data,
  136. size_t data_size);
  137. /**
  138. * @brief Calculate HMAC
  139. *
  140. * @note @ref nrf_crypto_hmac_update must be called at least once before calling this.
  141. *
  142. * @param[in,out] p_context Context pointer.
  143. * @param[out] p_digest Pointer to HMAC digest (result) buffer. Must be large enough to
  144. * hold the digest (32 byte for SHA-256 and 64 byte for SHA-512).
  145. * @param[in,out] p_digest_size Length of buffer as input. Length of digest as output.
  146. *
  147. * @retval NRF_SUCCESS HMAC hash was successfully calculated.
  148. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL If p_context was NULL.
  149. * @retval NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED If p_context has not been initialized.
  150. * @retval NRF_ERROR_CRYPTO_OUTPUT_NULL If p_digest was NULL.
  151. * @retval NRF_ERROR_CRYPTO_OUTPUT_LENGTH If p_size is not enough to hold the digest.
  152. * @retval NRF_ERROR_CRYPTO_INTERNAL An error occurred in the crypto backend.
  153. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  154. * nrf_crypto backend was busy. Please rerun
  155. * the cryptographic routine at a later time.
  156. * CC310 only.
  157. */
  158. ret_code_t nrf_crypto_hmac_finalize(nrf_crypto_hmac_context_t * const p_context,
  159. uint8_t * p_digest,
  160. size_t * const p_digest_size);
  161. /**
  162. * @brief Integrated HMAC wrapper function
  163. *
  164. * @note This is an integrated wrapper functions that can be used instead of calling other HMAC
  165. * functions individually.
  166. *
  167. * @param[in,out] p_context Optional pointer to context structure.
  168. * Context memory will be allocated internally if the pointer is NULL.
  169. * @param[in] p_info Pointer to static info structure. This defines the algorithm.
  170. * This should be either @ref g_nrf_crypto_hmac_sha256_info or
  171. * @ref g_nrf_crypto_hmac_sha512_info.
  172. * @param[out] p_digest Pointer to HMAC digest.
  173. * Buffer must be large enough to hold the digest.
  174. * @param[in,out] p_digest_size Length of digest (result) buffer as input.
  175. * Length of digest as output.
  176. * @param[in] p_key Pointer to HMAC key.
  177. * @param[in] key_size Lenth of the HMAC key in bytes.
  178. * @param[in] p_data Pointer to input data.
  179. * @param[in] data_size Length of input data.
  180. *
  181. * @retval NRF_SUCCESS HMAC hash was successfully calculated.
  182. * @retval NRF_ERROR_CRYPTO_INPUT_NULL If p_key or p_data was NULL.
  183. * @retval NRF_ERROR_CRYPTO_INPUT_LOCATION Input data not in RAM (CC310 only).
  184. * @retval NRF_ERROR_CRYPTO_INPUT_LENGTH If key_size or data_size was invalid.
  185. * @retval NRF_ERROR_CRYPTO_OUTPUT_NULL If data_size was NULL.
  186. * @retval NRF_ERROR_CRYPTO_OUTPUT_LENGTH If data_size is not enough to hold the digest.
  187. * @retval NRF_ERROR_CRYPTO_ALLOC_FAILED Unable to allocate memory for the context.
  188. * @retval NRF_ERROR_CRYPTO_INTERNAL An error occurred in the crypto backend.
  189. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  190. * nrf_crypto backend was busy. Please rerun the
  191. * cryptographic routine at a later time. CC310 only.
  192. */
  193. ret_code_t nrf_crypto_hmac_calculate(nrf_crypto_hmac_context_t * const p_context,
  194. nrf_crypto_hmac_info_t const * p_info,
  195. uint8_t * p_digest,
  196. size_t * const p_digest_size,
  197. uint8_t const * p_key,
  198. size_t key_size,
  199. uint8_t const * p_data,
  200. size_t data_size);
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. /**@} */
  205. #endif // #ifndef NRF_CRYPTO_HMAC_H__