sha512.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * \file sha512.h
  3. * \brief This file contains SHA-384 and SHA-512 definitions and functions.
  4. *
  5. * The Secure Hash Algorithms 384 and 512 (SHA-384 and SHA-512) cryptographic
  6. * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
  7. */
  8. /*
  9. * Copyright The Mbed TLS Contributors
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #ifndef MBEDTLS_SHA512_H
  25. #define MBEDTLS_SHA512_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "mbedtls/config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. /* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */
  34. #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
  35. #define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075 /**< SHA-512 input data was malformed. */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #if !defined(MBEDTLS_SHA512_ALT)
  40. // Regular implementation
  41. //
  42. /**
  43. * \brief The SHA-512 context structure.
  44. *
  45. * The structure is used both for SHA-384 and for SHA-512
  46. * checksum calculations. The choice between these two is
  47. * made in the call to mbedtls_sha512_starts_ret().
  48. */
  49. typedef struct mbedtls_sha512_context
  50. {
  51. uint64_t total[2]; /*!< The number of Bytes processed. */
  52. uint64_t state[8]; /*!< The intermediate digest state. */
  53. unsigned char buffer[128]; /*!< The data block being processed. */
  54. #if !defined(MBEDTLS_SHA512_NO_SHA384)
  55. int is384; /*!< Determines which function to use:
  56. 0: Use SHA-512, or 1: Use SHA-384. */
  57. #endif
  58. }
  59. mbedtls_sha512_context;
  60. #else /* MBEDTLS_SHA512_ALT */
  61. #include "sha512_alt.h"
  62. #endif /* MBEDTLS_SHA512_ALT */
  63. /**
  64. * \brief This function initializes a SHA-512 context.
  65. *
  66. * \param ctx The SHA-512 context to initialize. This must
  67. * not be \c NULL.
  68. */
  69. void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
  70. /**
  71. * \brief This function clears a SHA-512 context.
  72. *
  73. * \param ctx The SHA-512 context to clear. This may be \c NULL,
  74. * in which case this function does nothing. If it
  75. * is not \c NULL, it must point to an initialized
  76. * SHA-512 context.
  77. */
  78. void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
  79. /**
  80. * \brief This function clones the state of a SHA-512 context.
  81. *
  82. * \param dst The destination context. This must be initialized.
  83. * \param src The context to clone. This must be initialized.
  84. */
  85. void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
  86. const mbedtls_sha512_context *src );
  87. /**
  88. * \brief This function starts a SHA-384 or SHA-512 checksum
  89. * calculation.
  90. *
  91. * \param ctx The SHA-512 context to use. This must be initialized.
  92. * \param is384 Determines which function to use. This must be
  93. * either \c 0 for SHA-512, or \c 1 for SHA-384.
  94. *
  95. * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
  96. * be \c 0, or the function will return
  97. * #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
  98. *
  99. * \return \c 0 on success.
  100. * \return A negative error code on failure.
  101. */
  102. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
  103. /**
  104. * \brief This function feeds an input buffer into an ongoing
  105. * SHA-512 checksum calculation.
  106. *
  107. * \param ctx The SHA-512 context. This must be initialized
  108. * and have a hash operation started.
  109. * \param input The buffer holding the input data. This must
  110. * be a readable buffer of length \p ilen Bytes.
  111. * \param ilen The length of the input data in Bytes.
  112. *
  113. * \return \c 0 on success.
  114. * \return A negative error code on failure.
  115. */
  116. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  117. const unsigned char *input,
  118. size_t ilen );
  119. /**
  120. * \brief This function finishes the SHA-512 operation, and writes
  121. * the result to the output buffer.
  122. *
  123. * \param ctx The SHA-512 context. This must be initialized
  124. * and have a hash operation started.
  125. * \param output The SHA-384 or SHA-512 checksum result.
  126. * This must be a writable buffer of length \c 64 Bytes.
  127. *
  128. * \return \c 0 on success.
  129. * \return A negative error code on failure.
  130. */
  131. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  132. unsigned char output[64] );
  133. /**
  134. * \brief This function processes a single data block within
  135. * the ongoing SHA-512 computation.
  136. * This function is for internal use only.
  137. *
  138. * \param ctx The SHA-512 context. This must be initialized.
  139. * \param data The buffer holding one block of data. This
  140. * must be a readable buffer of length \c 128 Bytes.
  141. *
  142. * \return \c 0 on success.
  143. * \return A negative error code on failure.
  144. */
  145. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  146. const unsigned char data[128] );
  147. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  148. #if defined(MBEDTLS_DEPRECATED_WARNING)
  149. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  150. #else
  151. #define MBEDTLS_DEPRECATED
  152. #endif
  153. /**
  154. * \brief This function starts a SHA-384 or SHA-512 checksum
  155. * calculation.
  156. *
  157. * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
  158. *
  159. * \param ctx The SHA-512 context to use. This must be initialized.
  160. * \param is384 Determines which function to use. This must be either
  161. * \c 0 for SHA-512 or \c 1 for SHA-384.
  162. *
  163. * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
  164. * be \c 0, or the function will fail to work.
  165. */
  166. MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
  167. int is384 );
  168. /**
  169. * \brief This function feeds an input buffer into an ongoing
  170. * SHA-512 checksum calculation.
  171. *
  172. * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
  173. *
  174. * \param ctx The SHA-512 context. This must be initialized
  175. * and have a hash operation started.
  176. * \param input The buffer holding the data. This must be a readable
  177. * buffer of length \p ilen Bytes.
  178. * \param ilen The length of the input data in Bytes.
  179. */
  180. MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
  181. const unsigned char *input,
  182. size_t ilen );
  183. /**
  184. * \brief This function finishes the SHA-512 operation, and writes
  185. * the result to the output buffer.
  186. *
  187. * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
  188. *
  189. * \param ctx The SHA-512 context. This must be initialized
  190. * and have a hash operation started.
  191. * \param output The SHA-384 or SHA-512 checksum result. This must
  192. * be a writable buffer of size \c 64 Bytes.
  193. */
  194. MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
  195. unsigned char output[64] );
  196. /**
  197. * \brief This function processes a single data block within
  198. * the ongoing SHA-512 computation. This function is for
  199. * internal use only.
  200. *
  201. * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
  202. *
  203. * \param ctx The SHA-512 context. This must be initialized.
  204. * \param data The buffer holding one block of data. This must be
  205. * a readable buffer of length \c 128 Bytes.
  206. */
  207. MBEDTLS_DEPRECATED void mbedtls_sha512_process(
  208. mbedtls_sha512_context *ctx,
  209. const unsigned char data[128] );
  210. #undef MBEDTLS_DEPRECATED
  211. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  212. /**
  213. * \brief This function calculates the SHA-512 or SHA-384
  214. * checksum of a buffer.
  215. *
  216. * The function allocates the context, performs the
  217. * calculation, and frees the context.
  218. *
  219. * The SHA-512 result is calculated as
  220. * output = SHA-512(input buffer).
  221. *
  222. * \param input The buffer holding the input data. This must be
  223. * a readable buffer of length \p ilen Bytes.
  224. * \param ilen The length of the input data in Bytes.
  225. * \param output The SHA-384 or SHA-512 checksum result.
  226. * This must be a writable buffer of length \c 64 Bytes.
  227. * \param is384 Determines which function to use. This must be either
  228. * \c 0 for SHA-512, or \c 1 for SHA-384.
  229. *
  230. * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
  231. * be \c 0, or the function will return
  232. * #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
  233. *
  234. * \return \c 0 on success.
  235. * \return A negative error code on failure.
  236. */
  237. int mbedtls_sha512_ret( const unsigned char *input,
  238. size_t ilen,
  239. unsigned char output[64],
  240. int is384 );
  241. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  242. #if defined(MBEDTLS_DEPRECATED_WARNING)
  243. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  244. #else
  245. #define MBEDTLS_DEPRECATED
  246. #endif
  247. /**
  248. * \brief This function calculates the SHA-512 or SHA-384
  249. * checksum of a buffer.
  250. *
  251. * The function allocates the context, performs the
  252. * calculation, and frees the context.
  253. *
  254. * The SHA-512 result is calculated as
  255. * output = SHA-512(input buffer).
  256. *
  257. * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
  258. *
  259. * \param input The buffer holding the data. This must be a
  260. * readable buffer of length \p ilen Bytes.
  261. * \param ilen The length of the input data in Bytes.
  262. * \param output The SHA-384 or SHA-512 checksum result. This must
  263. * be a writable buffer of length \c 64 Bytes.
  264. * \param is384 Determines which function to use. This must be either
  265. * \c 0 for SHA-512, or \c 1 for SHA-384.
  266. *
  267. * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
  268. * be \c 0, or the function will fail to work.
  269. */
  270. MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
  271. size_t ilen,
  272. unsigned char output[64],
  273. int is384 );
  274. #undef MBEDTLS_DEPRECATED
  275. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  276. #if defined(MBEDTLS_SELF_TEST)
  277. /**
  278. * \brief The SHA-384 or SHA-512 checkup routine.
  279. *
  280. * \return \c 0 on success.
  281. * \return \c 1 on failure.
  282. */
  283. int mbedtls_sha512_self_test( int verbose );
  284. #endif /* MBEDTLS_SELF_TEST */
  285. #ifdef __cplusplus
  286. }
  287. #endif
  288. #endif /* mbedtls_sha512.h */