sha256.h 8.9 KB

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