nrf_crypto_hash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Copyright (c) 2018, 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_HASH_H__
  41. #define NRF_CRYPTO_HASH_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_hash Cryptographic hash related functions
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Provides cryptographic hash related functionality through nrf_crypto.
  49. */
  50. #include <stdint.h>
  51. #include "nrf_crypto_types.h"
  52. #include "nrf_crypto_hash_shared.h"
  53. #include "nrf_crypto_hash_backend.h"
  54. #include "app_util.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**@brief External variable declaration to info structure for SHA-256
  59. *
  60. * @note The variable is defined in the nrf_crypto backend that is
  61. * enabled in the @ref sdk_config.
  62. *
  63. */
  64. extern const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info;
  65. /**@brief External variable declaration to info structure for SHA-512
  66. *
  67. * @note The variable is defined in the nrf_crypto backend that is
  68. * enabled in the @ref sdk_config.
  69. *
  70. */
  71. extern const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha512_info;
  72. /**
  73. * @brief Context type for Hash.
  74. *
  75. * @note The size of this type is scaled for the largest Hash backend context that is
  76. * enabled in @ref sdk_config.
  77. */
  78. typedef nrf_crypto_backend_hash_context_t nrf_crypto_hash_context_t;
  79. /** @brief Type definition for an array holding a SHA-256 hash digest. */
  80. typedef uint8_t nrf_crypto_hash_sha256_digest_t[NRF_CRYPTO_HASH_SIZE_SHA256];
  81. /** @brief Type definition for an array holding a SHA-512 hash digest. */
  82. typedef uint8_t nrf_crypto_hash_sha512_digest_t[NRF_CRYPTO_HASH_SIZE_SHA512];
  83. /**@brief Function for initializing the context structure required to compute a hash digest from
  84. * arbitrary input data.
  85. *
  86. * @note The context structure is assumed to be an opaque type defined by the
  87. * nrf_crypto backend.
  88. *
  89. * @note The return codes @ref NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE and
  90. * NRF_ERROR_CRYPTO_INTERNAL only happens in cc310 backend.
  91. *
  92. * @param[in,out] p_context Pointer to structure holding context information for
  93. * the hash calculation.
  94. * @param[in] p_info Pointer to structure holding info about the hash algorithm
  95. * used to do the computed hash.
  96. *
  97. * @retval NRF_SUCCESS The hash initialization was successful.
  98. * @retval NRF_ERROR_CRYPTO_NOT_INITIALIZED @ref nrf_crypto_init was not called prior to
  99. * this crypto function.
  100. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL A NULL pointer was provided for the context
  101. * structure.
  102. * @retval NRF_ERROR_CRYPTO_INPUT_NULL The pointer to the info structure was NULL.
  103. * @retval NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE The function was called with a hash mode that
  104. * is unavailable.
  105. * @retval NRF_ERROR_CRYPTO_INTERNAL An internal error occurred when initializing
  106. * the constext in the nrf_crypto backend.
  107. */
  108. ret_code_t nrf_crypto_hash_init(nrf_crypto_hash_context_t * const p_context,
  109. nrf_crypto_hash_info_t const * p_info);
  110. /**@brief Function for updating the hash calculation with partial arbitrary data.
  111. *
  112. * @details This function should be called one or more times until all arbituary input data
  113. * required for the hash calcuation is provided.
  114. *
  115. * @note @ref nrf_crypto_hash_init must be called prior to this function to configure the
  116. * context structure used as input parameter to this function.
  117. *
  118. * @note @ref nrf_crypto_hash_finalize must be called after all arbitruary input data
  119. * has been provided to get the calculated hash digest.
  120. *
  121. * @note The context object is assumed to be an opaque type defined by the
  122. * nrf_crypto backend.
  123. *
  124. * @note The return values @ref NRF_ERROR_CRYPTO_BUSY, @ref NRF_ERROR_CRYPTO_INPUT_LOCATION
  125. * and @ref NRF_ERROR_CRYPTO_INPUT_LOCATION can only occur in CC310 backend.
  126. *
  127. * @param[in,out] p_context Pointer to structure holding context information for
  128. * the hash calculation.
  129. * @param[in] p_data Pointer to data to be hashed.
  130. * @param[in] data_size Length of the data to be hashed.
  131. *
  132. * @retval NRF_SUCCESS The hash digest was computed successfully.
  133. * @retval NRF_ERROR_CRYPTO_NOT_INITIALIZED @ref nrf_crypto_init was not called prior to
  134. * this crypto function.
  135. * @retval NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED The context was not initialized prior to
  136. * this call or it was corrupted. Please call
  137. * @ref nrf_crypto_hash_init to initialize it.
  138. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL A NULL pointer was provided for the context
  139. * structure.
  140. * @retval NRF_ERROR_CRYPTO_INPUT_NULL p_data was NULL.
  141. * @retval NRF_ERROR_CRYPTO_INPUT_LOCATION Input data not in RAM.
  142. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  143. * nrf_crypto backend was busy. Please rerun the
  144. * cryptographic routine at a later time.
  145. * @retval NRF_ERROR_CRYPTO_INTERNAL An internal error occurred in the nrf_crypto
  146. * backend.
  147. */
  148. ret_code_t nrf_crypto_hash_update(nrf_crypto_hash_context_t * const p_context,
  149. uint8_t const * p_data,
  150. size_t data_size);
  151. /**@brief Function for finalizing computation of a hash digest from arbitrary data.
  152. *
  153. * @details This function is called to get the calculated
  154. *
  155. * @note @ref nrf_crypto_hash_init must be called prior to this function to configure the
  156. * context structure used as input parameter to this function.
  157. *
  158. * @note The input data for the calculated hash digest must be provided by calling
  159. * @ref nrf_crypto_hash_update one or more times.
  160. *
  161. * @note The context object is assumed to be an opaque type defined by the
  162. * nrf_crypto backend.
  163. *
  164. * @note The return values @ref NRF_ERROR_CRYPTO_BUSY and @ref NRF_ERROR_CRYPTO_INPUT_LOCATION
  165. * can only occur in CC310 backend.
  166. *
  167. *
  168. * @param[in] p_context Pointer to structure holding context information for
  169. * the hash calculation.
  170. * @param[out] p_digest Pointer to buffer holding the calculated hash digest.
  171. * @param[in,out] p_digest_size Pointer to a variable holding the length of the calculated hash.
  172. * Set this to the length of buffer that p_digest is pointing to.
  173. *
  174. * @retval NRF_SUCCESS The hash digest was computed successfully.
  175. * @retval NRF_ERROR_CRYPTO_NOT_INITIALIZED @ref nrf_crypto_init was not called prior to
  176. * this crypto function.
  177. * @retval NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED The context was not initialized prior to
  178. * this call or it was corrupted. Please call
  179. * @ref nrf_crypto_hash_init to initialize it.
  180. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL A NULL pointer was provided for the context
  181. * structure.
  182. * @retval NRF_ERROR_CRYPTO_OUTPUT_NULL p_digest or p_digest_size was NULL.
  183. * @retval NRF_ERROR_CRYPTO_OUTPUT_LENGTH The length of p_digest was too small for
  184. * the hash digest result.
  185. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  186. * nrf_crypto backend was busy. Please rerun the
  187. * cryptographic routine at a later time.
  188. * @retval NRF_ERROR_CRYPTO_INTERNAL An internal error occurred in the nrf_crypto
  189. * backend.
  190. */
  191. ret_code_t nrf_crypto_hash_finalize(nrf_crypto_hash_context_t * const p_context,
  192. uint8_t * p_digest,
  193. size_t * const p_digest_size);
  194. /**@brief Function for computing a hash from arbitrary data in a single integrated step.
  195. *
  196. * @details This function calculates the hash digest from arbitruary data in a single integrated step.
  197. * This means calling init, update and finalize in one step.
  198. *
  199. * @note The context object is assumed to be an opaque type defined by the
  200. * nrf_crypto backend.
  201. *
  202. * @note The return values @ref NRF_ERROR_CRYPTO_BUSY, @ref NRF_ERROR_CRYPTO_INPUT_LOCATION
  203. * and @ref NRF_ERROR_CRYPTO_INPUT_LOCATION can only occur in CC310 backend.
  204. *
  205. * @param[in,out] p_context Pointer to structure holding context information for
  206. * the hash calculation. If this
  207. * is set to NULL, it will be allocated by the user configurable
  208. * allocate/free function @ref NRF_CRYPTO_ALLOC and
  209. * @ref NRF_CRYPTO_FREE.
  210. * @param[in] p_info Pointer to structure holding info about hash algorithm
  211. * for the computed hash.
  212. * @param[in] p_data Pointer to data to be hashed.
  213. * @param[in] data_size Length of the data to be hashed.
  214. * @param[out] p_digest Pointer to buffer holding the calculated hash digest.
  215. * @param[in,out] p_digest_size Pointer to a variable holding the length of the calculated hash.
  216. * Set this to the length of buffer that p_digest is pointing to.
  217. *
  218. * @retval NRF_SUCCESS The hash initialization was successful.
  219. * @retval NRF_ERROR_CRYPTO_NOT_INITIALIZED @ref nrf_crypto_init was not called prior to
  220. * this crypto function.
  221. * @retval NRF_ERROR_CRYPTO_CONTEXT_NULL A NULL pointer was provided for the context
  222. * structure.
  223. * @retval NRF_ERROR_CRYPTO_INPUT_NULL p_info or p_data was NULL.
  224. * @retval NRF_ERROR_CRYPTO_INPUT_LOCATION Input data not in RAM.
  225. * @retval NRF_ERROR_CRYPTO_OUTPUT_NULL p_digest or p_digest_size was NULL.
  226. * @retval NRF_ERROR_CRYPTO_OUTPUT_LENGTH The length of p_digest was too small for
  227. * the hash digest result.
  228. * @retval NRF_ERROR_CRYPTO_BUSY The function could not be called because the
  229. * nrf_crypto backend was busy. Please rerun the
  230. * cryptographic routine at a later time.
  231. * @retval NRF_ERROR_CRYPTO_ALLOC_FAILED Unable to allocate memory for the context.
  232. * @retval NRF_ERROR_CRYPTO_INTERNAL An internal error occurred in the nrf_crypto
  233. * backend.
  234. */
  235. ret_code_t nrf_crypto_hash_calculate(nrf_crypto_hash_context_t * const p_context,
  236. nrf_crypto_hash_info_t const * p_info,
  237. uint8_t const * p_data,
  238. size_t data_size,
  239. uint8_t * p_digest,
  240. size_t * const p_digest_size);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. /**@} */
  245. #endif // NRF_CRYPTO_HASH_H__