sha512.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  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. * This file is part of Mbed TLS (https://tls.mbed.org)
  25. */
  26. #ifndef MBEDTLS_SHA512_H
  27. #define MBEDTLS_SHA512_H
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #include <stddef.h>
  34. #include <stdint.h>
  35. /* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */
  36. #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
  37. #define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075 /**< SHA-512 input data was malformed. */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #if !defined(MBEDTLS_SHA512_ALT)
  42. // Regular implementation
  43. //
  44. /**
  45. * \brief The SHA-512 context structure.
  46. *
  47. * The structure is used both for SHA-384 and for SHA-512
  48. * checksum calculations. The choice between these two is
  49. * made in the call to mbedtls_sha512_starts_ret().
  50. */
  51. typedef struct mbedtls_sha512_context
  52. {
  53. uint64_t total[2]; /*!< The number of Bytes processed. */
  54. uint64_t state[8]; /*!< The intermediate digest state. */
  55. unsigned char buffer[128]; /*!< The data block being processed. */
  56. int is384; /*!< Determines which function to use:
  57. 0: Use SHA-512, or 1: Use SHA-384. */
  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 for SHA-512, or \c 1 for SHA-384.
  94. *
  95. * \return \c 0 on success.
  96. * \return A negative error code on failure.
  97. */
  98. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
  99. /**
  100. * \brief This function feeds an input buffer into an ongoing
  101. * SHA-512 checksum calculation.
  102. *
  103. * \param ctx The SHA-512 context. This must be initialized
  104. * and have a hash operation started.
  105. * \param input The buffer holding the input data. This must
  106. * be a readable buffer of length \p ilen Bytes.
  107. * \param ilen The length of the input data in Bytes.
  108. *
  109. * \return \c 0 on success.
  110. * \return A negative error code on failure.
  111. */
  112. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  113. const unsigned char *input,
  114. size_t ilen );
  115. /**
  116. * \brief This function finishes the SHA-512 operation, and writes
  117. * the result to the output buffer. This function is for
  118. * internal use only.
  119. *
  120. * \param ctx The SHA-512 context. This must be initialized
  121. * and have a hash operation started.
  122. * \param output The SHA-384 or SHA-512 checksum result.
  123. * This must be a writable buffer of length \c 64 Bytes.
  124. *
  125. * \return \c 0 on success.
  126. * \return A negative error code on failure.
  127. */
  128. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  129. unsigned char output[64] );
  130. /**
  131. * \brief This function processes a single data block within
  132. * the ongoing SHA-512 computation.
  133. *
  134. * \param ctx The SHA-512 context. This must be initialized.
  135. * \param data The buffer holding one block of data. This
  136. * must be a readable buffer of length \c 128 Bytes.
  137. *
  138. * \return \c 0 on success.
  139. * \return A negative error code on failure.
  140. */
  141. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  142. const unsigned char data[128] );
  143. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  144. #if defined(MBEDTLS_DEPRECATED_WARNING)
  145. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  146. #else
  147. #define MBEDTLS_DEPRECATED
  148. #endif
  149. /**
  150. * \brief This function starts a SHA-384 or SHA-512 checksum
  151. * calculation.
  152. *
  153. * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
  154. *
  155. * \param ctx The SHA-512 context to use. This must be initialized.
  156. * \param is384 Determines which function to use. This must be either
  157. * \c 0 for SHA-512 or \c 1 for SHA-384.
  158. */
  159. MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
  160. int is384 );
  161. /**
  162. * \brief This function feeds an input buffer into an ongoing
  163. * SHA-512 checksum calculation.
  164. *
  165. * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
  166. *
  167. * \param ctx The SHA-512 context. This must be initialized
  168. * and have a hash operation started.
  169. * \param input The buffer holding the data. This must be a readable
  170. * buffer of length \p ilen Bytes.
  171. * \param ilen The length of the input data in Bytes.
  172. */
  173. MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
  174. const unsigned char *input,
  175. size_t ilen );
  176. /**
  177. * \brief This function finishes the SHA-512 operation, and writes
  178. * the result to the output buffer.
  179. *
  180. * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
  181. *
  182. * \param ctx The SHA-512 context. This must be initialized
  183. * and have a hash operation started.
  184. * \param output The SHA-384 or SHA-512 checksum result. This must
  185. * be a writable buffer of size \c 64 Bytes.
  186. */
  187. MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
  188. unsigned char output[64] );
  189. /**
  190. * \brief This function processes a single data block within
  191. * the ongoing SHA-512 computation. This function is for
  192. * internal use only.
  193. *
  194. * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
  195. *
  196. * \param ctx The SHA-512 context. This must be initialized.
  197. * \param data The buffer holding one block of data. This must be
  198. * a readable buffer of length \c 128 Bytes.
  199. */
  200. MBEDTLS_DEPRECATED void mbedtls_sha512_process(
  201. mbedtls_sha512_context *ctx,
  202. const unsigned char data[128] );
  203. #undef MBEDTLS_DEPRECATED
  204. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  205. /**
  206. * \brief This function calculates the SHA-512 or SHA-384
  207. * checksum of a buffer.
  208. *
  209. * The function allocates the context, performs the
  210. * calculation, and frees the context.
  211. *
  212. * The SHA-512 result is calculated as
  213. * output = SHA-512(input buffer).
  214. *
  215. * \param input The buffer holding the input data. This must be
  216. * a readable buffer of length \p ilen Bytes.
  217. * \param ilen The length of the input data in Bytes.
  218. * \param output The SHA-384 or SHA-512 checksum result.
  219. * This must be a writable buffer of length \c 64 Bytes.
  220. * \param is384 Determines which function to use. This must be either
  221. * \c 0 for SHA-512, or \c 1 for SHA-384.
  222. *
  223. * \return \c 0 on success.
  224. * \return A negative error code on failure.
  225. */
  226. int mbedtls_sha512_ret( const unsigned char *input,
  227. size_t ilen,
  228. unsigned char output[64],
  229. int is384 );
  230. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  231. #if defined(MBEDTLS_DEPRECATED_WARNING)
  232. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  233. #else
  234. #define MBEDTLS_DEPRECATED
  235. #endif
  236. /**
  237. * \brief This function calculates the SHA-512 or SHA-384
  238. * checksum of a buffer.
  239. *
  240. * The function allocates the context, performs the
  241. * calculation, and frees the context.
  242. *
  243. * The SHA-512 result is calculated as
  244. * output = SHA-512(input buffer).
  245. *
  246. * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
  247. *
  248. * \param input The buffer holding the data. This must be a
  249. * readable buffer of length \p ilen Bytes.
  250. * \param ilen The length of the input data in Bytes.
  251. * \param output The SHA-384 or SHA-512 checksum result. This must
  252. * be a writable buffer of length \c 64 Bytes.
  253. * \param is384 Determines which function to use. This must be either
  254. * \c 0 for SHA-512, or \c 1 for SHA-384.
  255. */
  256. MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
  257. size_t ilen,
  258. unsigned char output[64],
  259. int is384 );
  260. #undef MBEDTLS_DEPRECATED
  261. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  262. #if defined(MBEDTLS_SELF_TEST)
  263. /**
  264. * \brief The SHA-384 or SHA-512 checkup routine.
  265. *
  266. * \return \c 0 on success.
  267. * \return \c 1 on failure.
  268. */
  269. int mbedtls_sha512_self_test( int verbose );
  270. #endif /* MBEDTLS_SELF_TEST */
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif /* mbedtls_sha512.h */