sha256.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  35. !defined(inline) && !defined(__cplusplus)
  36. #define inline __inline
  37. #endif
  38. #if !defined(MBEDTLS_SHA256_ALT)
  39. // Regular implementation
  40. //
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * \brief The SHA-256 context structure.
  46. *
  47. * The structure is used both for SHA-256 and for SHA-224
  48. * checksum calculations. The choice between these two is
  49. * made in the call to mbedtls_sha256_starts_ret().
  50. */
  51. typedef struct
  52. {
  53. uint32_t total[2]; /*!< The number of Bytes processed. */
  54. uint32_t state[8]; /*!< The intermediate digest state. */
  55. unsigned char buffer[64]; /*!< The data block being processed. */
  56. int is224; /*!< Determines which function to use.
  57. <ul><li>0: Use SHA-256.</li>
  58. <li>1: Use SHA-224.</li></ul> */
  59. }
  60. mbedtls_sha256_context;
  61. /**
  62. * \brief This function initializes a SHA-256 context.
  63. *
  64. * \param ctx The SHA-256 context to initialize.
  65. */
  66. void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
  67. /**
  68. * \brief This function clears a SHA-256 context.
  69. *
  70. * \param ctx The SHA-256 context to clear.
  71. */
  72. void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  73. /**
  74. * \brief This function clones the state of a SHA-256 context.
  75. *
  76. * \param dst The destination context.
  77. * \param src The context to clone.
  78. */
  79. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  80. const mbedtls_sha256_context *src );
  81. /**
  82. * \brief This function starts a SHA-224 or SHA-256 checksum
  83. * calculation.
  84. *
  85. * \param ctx The context to initialize.
  86. * \param is224 Determines which function to use.
  87. * <ul><li>0: Use SHA-256.</li>
  88. * <li>1: Use SHA-224.</li></ul>
  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 SHA-256 context
  98. * \param input buffer holding the data
  99. * \param ilen 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-256 checksum calculation.
  137. *
  138. * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
  139. *
  140. * \param ctx The SHA-256 context to initialize.
  141. * \param is224 Determines which function to use.
  142. * <ul><li>0: Use SHA-256.</li>
  143. * <li>1: Use SHA-224.</li></ul>
  144. */
  145. MBEDTLS_DEPRECATED static inline void mbedtls_sha256_starts(
  146. mbedtls_sha256_context *ctx,
  147. int is224 )
  148. {
  149. mbedtls_sha256_starts_ret( ctx, is224 );
  150. }
  151. /**
  152. * \brief This function feeds an input buffer into an ongoing
  153. * SHA-256 checksum calculation.
  154. *
  155. * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
  156. *
  157. * \param ctx The SHA-256 context to initialize.
  158. * \param input The buffer holding the data.
  159. * \param ilen The length of the input data.
  160. */
  161. MBEDTLS_DEPRECATED static inline void mbedtls_sha256_update(
  162. mbedtls_sha256_context *ctx,
  163. const unsigned char *input,
  164. size_t ilen )
  165. {
  166. mbedtls_sha256_update_ret( ctx, input, ilen );
  167. }
  168. /**
  169. * \brief This function finishes the SHA-256 operation, and writes
  170. * the result to the output buffer.
  171. *
  172. * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
  173. *
  174. * \param ctx The SHA-256 context.
  175. * \param output The SHA-224or SHA-256 checksum result.
  176. */
  177. MBEDTLS_DEPRECATED static inline void mbedtls_sha256_finish(
  178. mbedtls_sha256_context *ctx,
  179. unsigned char output[32] )
  180. {
  181. mbedtls_sha256_finish_ret( ctx, output );
  182. }
  183. /**
  184. * \brief This function processes a single data block within
  185. * the ongoing SHA-256 computation. This function is for
  186. * internal use only.
  187. *
  188. * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
  189. *
  190. * \param ctx The SHA-256 context.
  191. * \param data The buffer holding one block of data.
  192. */
  193. MBEDTLS_DEPRECATED static inline void mbedtls_sha256_process(
  194. mbedtls_sha256_context *ctx,
  195. const unsigned char data[64] )
  196. {
  197. mbedtls_internal_sha256_process( ctx, data );
  198. }
  199. #undef MBEDTLS_DEPRECATED
  200. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. #else /* MBEDTLS_SHA256_ALT */
  205. #include "sha256_alt.h"
  206. #endif /* MBEDTLS_SHA256_ALT */
  207. #ifdef __cplusplus
  208. extern "C" {
  209. #endif
  210. /**
  211. * \brief This function calculates the SHA-224 or SHA-256
  212. * checksum of a buffer.
  213. *
  214. * The function allocates the context, performs the
  215. * calculation, and frees the context.
  216. *
  217. * The SHA-256 result is calculated as
  218. * output = SHA-256(input buffer).
  219. *
  220. * \param input The buffer holding the input data.
  221. * \param ilen The length of the input data.
  222. * \param output The SHA-224 or SHA-256 checksum result.
  223. * \param is224 Determines which function to use.
  224. * <ul><li>0: Use SHA-256.</li>
  225. * <li>1: Use SHA-224.</li></ul>
  226. */
  227. int mbedtls_sha256_ret( const unsigned char *input,
  228. size_t ilen,
  229. unsigned char output[32],
  230. int is224 );
  231. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  232. #if defined(MBEDTLS_DEPRECATED_WARNING)
  233. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  234. #else
  235. #define MBEDTLS_DEPRECATED
  236. #endif
  237. /**
  238. * \brief This function calculates the SHA-224 or SHA-256 checksum
  239. * of a buffer.
  240. *
  241. * The function allocates the context, performs the
  242. * calculation, and frees the context.
  243. *
  244. * The SHA-256 result is calculated as
  245. * output = SHA-256(input buffer).
  246. *
  247. * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
  248. *
  249. * \param input The buffer holding the data.
  250. * \param ilen The length of the input data.
  251. * \param output The SHA-224 or SHA-256 checksum result.
  252. * \param is224 Determines which function to use.
  253. * <ul><li>0: Use SHA-256.</li>
  254. * <li>1: Use SHA-224.</li></ul>
  255. */
  256. MBEDTLS_DEPRECATED static inline void mbedtls_sha256(
  257. const unsigned char *input,
  258. size_t ilen,
  259. unsigned char output[32],
  260. int is224 )
  261. {
  262. mbedtls_sha256_ret( input, ilen, output, is224 );
  263. }
  264. #undef MBEDTLS_DEPRECATED
  265. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  266. /**
  267. * \brief The SHA-224 and SHA-256 checkup routine.
  268. *
  269. * \return \c 0 on success, or \c 1 on failure.
  270. */
  271. int mbedtls_sha256_self_test( int verbose );
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif /* mbedtls_sha256.h */