sha256.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2015 - 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. /** @file
  41. *
  42. * @defgroup sha256 SHA-256 hash library
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief This module calculates SHA-256 (SHA-2, FIPS-180) hashes.
  47. *
  48. * @details To use this module, first call @ref sha256_init on a @ref sha256_context_t instance. Then call @ref
  49. * sha256_update with the data to be hashed. This step can optionally be done with multiple
  50. * calls to @ref sha256_update, each with a section of the data (in the correct order).
  51. * After all data has been passed to @ref sha256_update, call @ref sha256_final to finalize
  52. * and extract the hash value.
  53. *
  54. * This code is adapted from code by Brad Conte, retrieved from
  55. * https://github.com/B-Con/crypto-algorithms.
  56. *
  57. */
  58. #ifndef SHA256_H
  59. #define SHA256_H
  60. #include <stdint.h>
  61. #include "sdk_errors.h"
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**@brief Current state of a hash operation.
  66. */
  67. typedef struct {
  68. uint8_t data[64];
  69. uint32_t datalen;
  70. uint64_t bitlen;
  71. uint32_t state[8];
  72. } sha256_context_t;
  73. /**@brief Function for initializing a @ref sha256_context_t instance.
  74. *
  75. * @param[out] ctx Context instance to be initialized.
  76. *
  77. * @retval NRF_SUCCESS If the instance was successfully initialized.
  78. * @retval NRF_ERROR_NULL If the parameter was NULL.
  79. */
  80. ret_code_t sha256_init(sha256_context_t *ctx);
  81. /**@brief Function for calculating the hash of an array of uint8_t data.
  82. *
  83. * @details This function can be called multiple times in sequence. This is equivalent to calling
  84. * the function once on a concatenation of the data from the different calls.
  85. *
  86. * @param[in,out] ctx Hash instance.
  87. * @param[in] data Data to be hashed.
  88. * @param[in] len Length of the data to be hashed.
  89. *
  90. * @retval NRF_SUCCESS If the data was successfully hashed.
  91. * @retval NRF_ERROR_NULL If the ctx parameter was NULL or the data parameter was NULL, while the len parameter was not zero.
  92. */
  93. ret_code_t sha256_update(sha256_context_t *ctx, const uint8_t * data, const size_t len);
  94. /**@brief Function for extracting the hash value from a hash instance.
  95. *
  96. * @details This function should be called after all data to be hashed has been passed to the hash
  97. * instance (by one or more calls to @ref sha256_update).
  98. *
  99. * Do not call @ref sha256_update again after @ref sha256_final has been called.
  100. *
  101. * @param[in,out] ctx Hash instance.
  102. * @param[out] hash Array to hold the extracted hash value (assumed to be 32 bytes long).
  103. * @param[in] le Store the hash in little-endian.
  104. *
  105. * @retval NRF_SUCCESS If the has value was successfully extracted.
  106. * @retval NRF_ERROR_NULL If a parameter was NULL.
  107. */
  108. ret_code_t sha256_final(sha256_context_t *ctx, uint8_t * hash, uint8_t le);
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. #endif // SHA256_H
  113. /** @} */