sha256.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. *
  14. * @defgroup sha256 SHA-256 hash library
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief This module calculates SHA-256 (SHA-2, FIPS-180) hashes.
  19. *
  20. * @details To use this module, first call @ref sha256_init on a @ref sha256_context_t instance. Then call @ref
  21. * sha256_update with the data to be hashed. This step can optionally be done with multiple
  22. * calls to @ref sha256_update, each with a section of the data (in the correct order).
  23. * After all data has been passed to @ref sha256_update, call @ref sha256_final to finalize
  24. * and extract the hash value.
  25. *
  26. * This code is adapted from code by Brad Conte, retrieved from
  27. * https://github.com/B-Con/crypto-algorithms.
  28. *
  29. */
  30. #ifndef SHA256_H
  31. #define SHA256_H
  32. #include <stdint.h>
  33. #include "sdk_errors.h"
  34. /**@brief Current state of a hash operation.
  35. */
  36. typedef struct {
  37. uint8_t data[64];
  38. uint32_t datalen;
  39. uint64_t bitlen;
  40. uint32_t state[8];
  41. } sha256_context_t;
  42. /**@brief Function for initializing a @ref sha256_context_t instance.
  43. *
  44. * @param[out] ctx Context instance to be initialized.
  45. *
  46. * @retval NRF_SUCCESS If the instance was successfully initialized.
  47. * @retval NRF_ERROR_NULL If the parameter was NULL.
  48. */
  49. ret_code_t sha256_init(sha256_context_t *ctx);
  50. /**@brief Function for calculating the hash of an array of uint8_t data.
  51. *
  52. * @details This function can be called multiple times in sequence. This is equivalent to calling
  53. * the function once on a concatenation of the data from the different calls.
  54. *
  55. * @param[in,out] ctx Hash instance.
  56. * @param[in] data Data to be hashed.
  57. * @param[in] len Length of the data to be hashed.
  58. *
  59. * @retval NRF_SUCCESS If the data was successfully hashed.
  60. * @retval NRF_ERROR_NULL If the ctx parameter was NULL or the data parameter was NULL, while the len parameter was not zero.
  61. */
  62. ret_code_t sha256_update(sha256_context_t *ctx, const uint8_t * data, const size_t len);
  63. /**@brief Function for extracting the hash value from a hash instance.
  64. *
  65. * @details This function should be called after all data to be hashed has been passed to the hash
  66. * instance (by one or more calls to @ref sha256_update).
  67. *
  68. * Do not call @ref sha256_update again after @ref sha256_final has been called.
  69. *
  70. * @param[in,out] ctx Hash instance.
  71. * @param[out] hash Array to hold the extracted hash value (assumed to be 32 bytes long).
  72. *
  73. * @retval NRF_SUCCESS If the has value was successfully extracted.
  74. * @retval NRF_ERROR_NULL If a parameter was NULL.
  75. */
  76. ret_code_t sha256_final(sha256_context_t *ctx, uint8_t * hash);
  77. #endif // SHA256_H
  78. /** @} */