sha512.h 9.0 KB

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