sha256.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * \file sha256.h
  3. *
  4. * \brief The SHA-224 and SHA-256 cryptographic hash function.
  5. */
  6. /*
  7. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of Mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_SHA256_H
  25. #define MBEDTLS_SHA256_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
  34. #if !defined(MBEDTLS_SHA256_ALT)
  35. // Regular implementation
  36. //
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /**
  41. * \brief The SHA-256 context structure.
  42. *
  43. * The structure is used both for SHA-256 and for SHA-224
  44. * checksum calculations. The choice between these two is
  45. * made in the call to mbedtls_sha256_starts_ret().
  46. */
  47. typedef struct
  48. {
  49. uint32_t total[2]; /*!< The number of Bytes processed. */
  50. uint32_t state[8]; /*!< The intermediate digest state. */
  51. unsigned char buffer[64]; /*!< The data block being processed. */
  52. int is224; /*!< Determines which function to use.
  53. <ul><li>0: Use SHA-256.</li>
  54. <li>1: Use SHA-224.</li></ul> */
  55. }
  56. mbedtls_sha256_context;
  57. /**
  58. * \brief This function initializes a SHA-256 context.
  59. *
  60. * \param ctx The SHA-256 context to initialize.
  61. */
  62. void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
  63. /**
  64. * \brief This function clears a SHA-256 context.
  65. *
  66. * \param ctx The SHA-256 context to clear.
  67. */
  68. void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  69. /**
  70. * \brief This function clones the state of a SHA-256 context.
  71. *
  72. * \param dst The destination context.
  73. * \param src The context to clone.
  74. */
  75. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  76. const mbedtls_sha256_context *src );
  77. /**
  78. * \brief This function starts a SHA-224 or SHA-256 checksum
  79. * calculation.
  80. *
  81. * \param ctx The context to initialize.
  82. * \param is224 Determines which function to use.
  83. * <ul><li>0: Use SHA-256.</li>
  84. * <li>1: Use SHA-224.</li></ul>
  85. *
  86. * \return \c 0 on success.
  87. */
  88. int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
  89. /**
  90. * \brief This function feeds an input buffer into an ongoing
  91. * SHA-256 checksum calculation.
  92. *
  93. * \param ctx SHA-256 context
  94. * \param input buffer holding the data
  95. * \param ilen length of the input data
  96. *
  97. * \return \c 0 on success.
  98. */
  99. int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
  100. const unsigned char *input,
  101. size_t ilen );
  102. /**
  103. * \brief This function finishes the SHA-256 operation, and writes
  104. * the result to the output buffer.
  105. *
  106. * \param ctx The SHA-256 context.
  107. * \param output The SHA-224 or SHA-256 checksum result.
  108. *
  109. * \return \c 0 on success.
  110. */
  111. int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
  112. unsigned char output[32] );
  113. /**
  114. * \brief This function processes a single data block within
  115. * the ongoing SHA-256 computation. This function is for
  116. * internal use only.
  117. *
  118. * \param ctx The SHA-256 context.
  119. * \param data The buffer holding one block of data.
  120. *
  121. * \return \c 0 on success.
  122. */
  123. int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
  124. const unsigned char data[64] );
  125. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  126. #if defined(MBEDTLS_DEPRECATED_WARNING)
  127. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  128. #else
  129. #define MBEDTLS_DEPRECATED
  130. #endif
  131. /**
  132. * \brief This function starts a SHA-256 checksum calculation.
  133. *
  134. * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
  135. *
  136. * \param ctx The SHA-256 context to initialize.
  137. * \param is224 Determines which function to use.
  138. * <ul><li>0: Use SHA-256.</li>
  139. * <li>1: Use SHA-224.</li></ul>
  140. */
  141. MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
  142. int is224 );
  143. /**
  144. * \brief This function feeds an input buffer into an ongoing
  145. * SHA-256 checksum calculation.
  146. *
  147. * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
  148. *
  149. * \param ctx The SHA-256 context to initialize.
  150. * \param input The buffer holding the data.
  151. * \param ilen The length of the input data.
  152. */
  153. MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
  154. const unsigned char *input,
  155. size_t ilen );
  156. /**
  157. * \brief This function finishes the SHA-256 operation, and writes
  158. * the result to the output buffer.
  159. *
  160. * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
  161. *
  162. * \param ctx The SHA-256 context.
  163. * \param output The SHA-224or SHA-256 checksum result.
  164. */
  165. MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
  166. unsigned char output[32] );
  167. /**
  168. * \brief This function processes a single data block within
  169. * the ongoing SHA-256 computation. This function is for
  170. * internal use only.
  171. *
  172. * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
  173. *
  174. * \param ctx The SHA-256 context.
  175. * \param data The buffer holding one block of data.
  176. */
  177. MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
  178. const unsigned char data[64] );
  179. #undef MBEDTLS_DEPRECATED
  180. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #else /* MBEDTLS_SHA256_ALT */
  185. #include "sha256_alt.h"
  186. #endif /* MBEDTLS_SHA256_ALT */
  187. #ifdef __cplusplus
  188. extern "C" {
  189. #endif
  190. /**
  191. * \brief This function calculates the SHA-224 or SHA-256
  192. * checksum of a buffer.
  193. *
  194. * The function allocates the context, performs the
  195. * calculation, and frees the context.
  196. *
  197. * The SHA-256 result is calculated as
  198. * output = SHA-256(input buffer).
  199. *
  200. * \param input The buffer holding the input data.
  201. * \param ilen The length of the input data.
  202. * \param output The SHA-224 or SHA-256 checksum result.
  203. * \param is224 Determines which function to use.
  204. * <ul><li>0: Use SHA-256.</li>
  205. * <li>1: Use SHA-224.</li></ul>
  206. */
  207. int mbedtls_sha256_ret( const unsigned char *input,
  208. size_t ilen,
  209. unsigned char output[32],
  210. int is224 );
  211. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  212. #if defined(MBEDTLS_DEPRECATED_WARNING)
  213. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  214. #else
  215. #define MBEDTLS_DEPRECATED
  216. #endif
  217. /**
  218. * \brief This function calculates the SHA-224 or SHA-256 checksum
  219. * of a buffer.
  220. *
  221. * The function allocates the context, performs the
  222. * calculation, and frees the context.
  223. *
  224. * The SHA-256 result is calculated as
  225. * output = SHA-256(input buffer).
  226. *
  227. * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
  228. *
  229. * \param input The buffer holding the data.
  230. * \param ilen The length of the input data.
  231. * \param output The SHA-224 or SHA-256 checksum result.
  232. * \param is224 Determines which function to use.
  233. * <ul><li>0: Use SHA-256.</li>
  234. * <li>1: Use SHA-224.</li></ul>
  235. */
  236. MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
  237. size_t ilen,
  238. unsigned char output[32],
  239. int is224 );
  240. #undef MBEDTLS_DEPRECATED
  241. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  242. /**
  243. * \brief The SHA-224 and SHA-256 checkup routine.
  244. *
  245. * \return \c 0 on success, or \c 1 on failure.
  246. */
  247. int mbedtls_sha256_self_test( int verbose );
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #endif /* mbedtls_sha256.h */