sha512.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
  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
  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. int is384; /*!< Determines which function to use:
  55. 0: Use SHA-512, or 1: Use SHA-384. */
  56. }
  57. mbedtls_sha512_context;
  58. #else /* MBEDTLS_SHA512_ALT */
  59. #include "sha512_alt.h"
  60. #endif /* MBEDTLS_SHA512_ALT */
  61. /**
  62. * \brief This function initializes a SHA-512 context.
  63. *
  64. * \param ctx The SHA-512 context to initialize.
  65. */
  66. void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
  67. /**
  68. * \brief This function clears a SHA-512 context.
  69. *
  70. * \param ctx The SHA-512 context to clear.
  71. */
  72. void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
  73. /**
  74. * \brief This function clones the state of a SHA-512 context.
  75. *
  76. * \param dst The destination context.
  77. * \param src The context to clone.
  78. */
  79. void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
  80. const mbedtls_sha512_context *src );
  81. /**
  82. * \brief This function starts a SHA-384 or SHA-512 checksum
  83. * calculation.
  84. *
  85. * \param ctx The SHA-512 context to initialize.
  86. * \param is384 Determines which function to use:
  87. * 0: Use SHA-512, or 1: Use SHA-384.
  88. *
  89. * \return \c 0 on success.
  90. */
  91. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
  92. /**
  93. * \brief This function feeds an input buffer into an ongoing
  94. * SHA-512 checksum calculation.
  95. *
  96. * \param ctx The SHA-512 context.
  97. * \param input The buffer holding the input data.
  98. * \param ilen The length of the input data.
  99. *
  100. * \return \c 0 on success.
  101. */
  102. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  103. const unsigned char *input,
  104. size_t ilen );
  105. /**
  106. * \brief This function finishes the SHA-512 operation, and writes
  107. * the result to the output buffer. This function is for
  108. * internal use only.
  109. *
  110. * \param ctx The SHA-512 context.
  111. * \param output The SHA-384 or SHA-512 checksum result.
  112. *
  113. * \return \c 0 on success.
  114. */
  115. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  116. unsigned char output[64] );
  117. /**
  118. * \brief This function processes a single data block within
  119. * the ongoing SHA-512 computation.
  120. *
  121. * \param ctx The SHA-512 context.
  122. * \param data The buffer holding one block of data.
  123. *
  124. * \return \c 0 on success.
  125. */
  126. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  127. const unsigned char data[128] );
  128. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  129. #if defined(MBEDTLS_DEPRECATED_WARNING)
  130. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  131. #else
  132. #define MBEDTLS_DEPRECATED
  133. #endif
  134. /**
  135. * \brief This function starts a SHA-384 or SHA-512 checksum
  136. * calculation.
  137. *
  138. * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
  139. *
  140. * \param ctx The SHA-512 context to initialize.
  141. * \param is384 Determines which function to use:
  142. * 0: Use SHA-512, or 1: Use SHA-384.
  143. */
  144. MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
  145. int is384 );
  146. /**
  147. * \brief This function feeds an input buffer into an ongoing
  148. * SHA-512 checksum calculation.
  149. *
  150. * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
  151. *
  152. * \param ctx The SHA-512 context.
  153. * \param input The buffer holding the data.
  154. * \param ilen The length of the input data.
  155. */
  156. MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
  157. const unsigned char *input,
  158. size_t ilen );
  159. /**
  160. * \brief This function finishes the SHA-512 operation, and writes
  161. * the result to the output buffer.
  162. *
  163. * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
  164. *
  165. * \param ctx The SHA-512 context.
  166. * \param output The SHA-384 or SHA-512 checksum result.
  167. */
  168. MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
  169. unsigned char output[64] );
  170. /**
  171. * \brief This function processes a single data block within
  172. * the ongoing SHA-512 computation. This function is for
  173. * internal use only.
  174. *
  175. * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
  176. *
  177. * \param ctx The SHA-512 context.
  178. * \param data The buffer holding one block of data.
  179. */
  180. MBEDTLS_DEPRECATED void mbedtls_sha512_process(
  181. mbedtls_sha512_context *ctx,
  182. const unsigned char data[128] );
  183. #undef MBEDTLS_DEPRECATED
  184. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  185. /**
  186. * \brief This function calculates the SHA-512 or SHA-384
  187. * checksum of a buffer.
  188. *
  189. * The function allocates the context, performs the
  190. * calculation, and frees the context.
  191. *
  192. * The SHA-512 result is calculated as
  193. * output = SHA-512(input buffer).
  194. *
  195. * \param input The buffer holding the input data.
  196. * \param ilen The length of the input data.
  197. * \param output The SHA-384 or SHA-512 checksum result.
  198. * \param is384 Determines which function to use:
  199. * 0: Use SHA-512, or 1: Use SHA-384.
  200. *
  201. * \return \c 0 on success.
  202. */
  203. int mbedtls_sha512_ret( const unsigned char *input,
  204. size_t ilen,
  205. unsigned char output[64],
  206. int is384 );
  207. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  208. #if defined(MBEDTLS_DEPRECATED_WARNING)
  209. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  210. #else
  211. #define MBEDTLS_DEPRECATED
  212. #endif
  213. /**
  214. * \brief This function calculates the SHA-512 or SHA-384
  215. * checksum of a buffer.
  216. *
  217. * The function allocates the context, performs the
  218. * calculation, and frees the context.
  219. *
  220. * The SHA-512 result is calculated as
  221. * output = SHA-512(input buffer).
  222. *
  223. * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
  224. *
  225. * \param input The buffer holding the data.
  226. * \param ilen The length of the input data.
  227. * \param output The SHA-384 or SHA-512 checksum result.
  228. * \param is384 Determines which function to use:
  229. * 0: Use SHA-512, or 1: Use SHA-384.
  230. */
  231. MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
  232. size_t ilen,
  233. unsigned char output[64],
  234. int is384 );
  235. #undef MBEDTLS_DEPRECATED
  236. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  237. /**
  238. * \brief The SHA-384 or SHA-512 checkup routine.
  239. *
  240. * \return \c 0 on success.
  241. * \return \c 1 on failure.
  242. */
  243. int mbedtls_sha512_self_test( int verbose );
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* mbedtls_sha512.h */