sha256.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
  37. #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
  38. #define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. #if !defined(MBEDTLS_SHA256_ALT)
  43. // Regular implementation
  44. //
  45. /**
  46. * \brief The SHA-256 context structure.
  47. *
  48. * The structure is used both for SHA-256 and for SHA-224
  49. * checksum calculations. The choice between these two is
  50. * made in the call to mbedtls_sha256_starts_ret().
  51. */
  52. typedef struct mbedtls_sha256_context
  53. {
  54. uint32_t total[2]; /*!< The number of Bytes processed. */
  55. uint32_t state[8]; /*!< The intermediate digest state. */
  56. unsigned char buffer[64]; /*!< The data block being processed. */
  57. int is224; /*!< Determines which function to use:
  58. 0: Use SHA-256, or 1: Use SHA-224. */
  59. }
  60. mbedtls_sha256_context;
  61. #else /* MBEDTLS_SHA256_ALT */
  62. #include "sha256_alt.h"
  63. #endif /* MBEDTLS_SHA256_ALT */
  64. /**
  65. * \brief This function initializes a SHA-256 context.
  66. *
  67. * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
  68. */
  69. void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
  70. /**
  71. * \brief This function clears a SHA-256 context.
  72. *
  73. * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
  74. * case this function returns immediately. If it is not \c NULL,
  75. * it must point to an initialized SHA-256 context.
  76. */
  77. void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  78. /**
  79. * \brief This function clones the state of a SHA-256 context.
  80. *
  81. * \param dst The destination context. This must be initialized.
  82. * \param src The context to clone. This must be initialized.
  83. */
  84. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  85. const mbedtls_sha256_context *src );
  86. /**
  87. * \brief This function starts a SHA-224 or SHA-256 checksum
  88. * calculation.
  89. *
  90. * \param ctx The context to use. This must be initialized.
  91. * \param is224 This determines which function to use. This must be
  92. * either \c 0 for SHA-256, or \c 1 for SHA-224.
  93. *
  94. * \return \c 0 on success.
  95. * \return A negative error code on failure.
  96. */
  97. int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
  98. /**
  99. * \brief This function feeds an input buffer into an ongoing
  100. * SHA-256 checksum calculation.
  101. *
  102. * \param ctx The SHA-256 context. This must be initialized
  103. * and have a hash operation started.
  104. * \param input The buffer holding the data. This must be a readable
  105. * buffer of length \p ilen Bytes.
  106. * \param ilen The length of the input data in Bytes.
  107. *
  108. * \return \c 0 on success.
  109. * \return A negative error code on failure.
  110. */
  111. int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
  112. const unsigned char *input,
  113. size_t ilen );
  114. /**
  115. * \brief This function finishes the SHA-256 operation, and writes
  116. * the result to the output buffer.
  117. *
  118. * \param ctx The SHA-256 context. This must be initialized
  119. * and have a hash operation started.
  120. * \param output The SHA-224 or SHA-256 checksum result.
  121. * This must be a writable buffer of length \c 32 Bytes.
  122. *
  123. * \return \c 0 on success.
  124. * \return A negative error code on failure.
  125. */
  126. int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
  127. unsigned char output[32] );
  128. /**
  129. * \brief This function processes a single data block within
  130. * the ongoing SHA-256 computation. This function is for
  131. * internal use only.
  132. *
  133. * \param ctx The SHA-256 context. This must be initialized.
  134. * \param data The buffer holding one block of data. This must
  135. * be a readable buffer of length \c 64 Bytes.
  136. *
  137. * \return \c 0 on success.
  138. * \return A negative error code on failure.
  139. */
  140. int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
  141. const unsigned char data[64] );
  142. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  143. #if defined(MBEDTLS_DEPRECATED_WARNING)
  144. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  145. #else
  146. #define MBEDTLS_DEPRECATED
  147. #endif
  148. /**
  149. * \brief This function starts a SHA-224 or SHA-256 checksum
  150. * calculation.
  151. *
  152. * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
  153. *
  154. * \param ctx The context to use. This must be initialized.
  155. * \param is224 Determines which function to use. This must be
  156. * either \c 0 for SHA-256, or \c 1 for SHA-224.
  157. */
  158. MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
  159. int is224 );
  160. /**
  161. * \brief This function feeds an input buffer into an ongoing
  162. * SHA-256 checksum calculation.
  163. *
  164. * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
  165. *
  166. * \param ctx The SHA-256 context to use. This must be
  167. * initialized and have a hash operation started.
  168. * \param input The buffer holding the data. This must be a readable
  169. * buffer of length \p ilen Bytes.
  170. * \param ilen The length of the input data in Bytes.
  171. */
  172. MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
  173. const unsigned char *input,
  174. size_t ilen );
  175. /**
  176. * \brief This function finishes the SHA-256 operation, and writes
  177. * the result to the output buffer.
  178. *
  179. * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
  180. *
  181. * \param ctx The SHA-256 context. This must be initialized and
  182. * have a hash operation started.
  183. * \param output The SHA-224 or SHA-256 checksum result. This must be
  184. * a writable buffer of length \c 32 Bytes.
  185. */
  186. MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
  187. unsigned char output[32] );
  188. /**
  189. * \brief This function processes a single data block within
  190. * the ongoing SHA-256 computation. This function is for
  191. * internal use only.
  192. *
  193. * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
  194. *
  195. * \param ctx The SHA-256 context. This must be initialized.
  196. * \param data The buffer holding one block of data. This must be
  197. * a readable buffer of size \c 64 Bytes.
  198. */
  199. MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
  200. const unsigned char data[64] );
  201. #undef MBEDTLS_DEPRECATED
  202. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  203. /**
  204. * \brief This function calculates the SHA-224 or SHA-256
  205. * checksum of a buffer.
  206. *
  207. * The function allocates the context, performs the
  208. * calculation, and frees the context.
  209. *
  210. * The SHA-256 result is calculated as
  211. * output = SHA-256(input buffer).
  212. *
  213. * \param input The buffer holding the data. This must be a readable
  214. * buffer of length \p ilen Bytes.
  215. * \param ilen The length of the input data in Bytes.
  216. * \param output The SHA-224 or SHA-256 checksum result. This must
  217. * be a writable buffer of length \c 32 Bytes.
  218. * \param is224 Determines which function to use. This must be
  219. * either \c 0 for SHA-256, or \c 1 for SHA-224.
  220. */
  221. int mbedtls_sha256_ret( const unsigned char *input,
  222. size_t ilen,
  223. unsigned char output[32],
  224. int is224 );
  225. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  226. #if defined(MBEDTLS_DEPRECATED_WARNING)
  227. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  228. #else
  229. #define MBEDTLS_DEPRECATED
  230. #endif
  231. /**
  232. * \brief This function calculates the SHA-224 or SHA-256 checksum
  233. * of a buffer.
  234. *
  235. * The function allocates the context, performs the
  236. * calculation, and frees the context.
  237. *
  238. * The SHA-256 result is calculated as
  239. * output = SHA-256(input buffer).
  240. *
  241. * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
  242. *
  243. * \param input The buffer holding the data. This must be a readable
  244. * buffer of length \p ilen Bytes.
  245. * \param ilen The length of the input data in Bytes.
  246. * \param output The SHA-224 or SHA-256 checksum result. This must be
  247. * a writable buffer of length \c 32 Bytes.
  248. * \param is224 Determines which function to use. This must be either
  249. * \c 0 for SHA-256, or \c 1 for SHA-224.
  250. */
  251. MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
  252. size_t ilen,
  253. unsigned char output[32],
  254. int is224 );
  255. #undef MBEDTLS_DEPRECATED
  256. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  257. #if defined(MBEDTLS_SELF_TEST)
  258. /**
  259. * \brief The SHA-224 and SHA-256 checkup routine.
  260. *
  261. * \return \c 0 on success.
  262. * \return \c 1 on failure.
  263. */
  264. int mbedtls_sha256_self_test( int verbose );
  265. #endif /* MBEDTLS_SELF_TEST */
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif /* mbedtls_sha256.h */