sha512.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  35. !defined(inline) && !defined(__cplusplus)
  36. #define inline __inline
  37. #endif
  38. #if !defined(MBEDTLS_SHA512_ALT)
  39. // Regular implementation
  40. //
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  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
  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. * <ul><li>0: Use SHA-512.</li>
  58. * <li>1: Use SHA-384.</li></ul> */
  59. }
  60. mbedtls_sha512_context;
  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. * <ul><li>0: Use SHA-512.</li>
  88. * <li>1: Use SHA-384.</li></ul>
  89. *
  90. * \return \c 0 on success.
  91. */
  92. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
  93. /**
  94. * \brief This function feeds an input buffer into an ongoing
  95. * SHA-512 checksum calculation.
  96. *
  97. * \param ctx The SHA-512 context.
  98. * \param input The buffer holding the input data.
  99. * \param ilen The length of the input data.
  100. *
  101. * \return \c 0 on success.
  102. */
  103. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  104. const unsigned char *input,
  105. size_t ilen );
  106. /**
  107. * \brief This function finishes the SHA-512 operation, and writes
  108. * the result to the output buffer. This function is for
  109. * internal use only.
  110. *
  111. * \param ctx The SHA-512 context.
  112. * \param output The SHA-384 or SHA-512 checksum result.
  113. *
  114. * \return \c 0 on success.
  115. */
  116. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  117. unsigned char output[64] );
  118. /**
  119. * \brief This function processes a single data block within
  120. * the ongoing SHA-512 computation.
  121. *
  122. * \param ctx The SHA-512 context.
  123. * \param data The buffer holding one block of data.
  124. *
  125. * \return \c 0 on success.
  126. */
  127. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  128. const unsigned char data[128] );
  129. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  130. #if defined(MBEDTLS_DEPRECATED_WARNING)
  131. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  132. #else
  133. #define MBEDTLS_DEPRECATED
  134. #endif
  135. /**
  136. * \brief This function starts a SHA-384 or SHA-512 checksum
  137. * calculation.
  138. *
  139. * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
  140. *
  141. * \param ctx The SHA-512 context to initialize.
  142. * \param is384 Determines which function to use.
  143. * <ul><li>0: Use SHA-512.</li>
  144. * <li>1: Use SHA-384.</li></ul>
  145. */
  146. MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
  147. mbedtls_sha512_context *ctx,
  148. int is384 )
  149. {
  150. mbedtls_sha512_starts_ret( ctx, is384 );
  151. }
  152. /**
  153. * \brief This function feeds an input buffer into an ongoing
  154. * SHA-512 checksum calculation.
  155. *
  156. * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
  157. *
  158. * \param ctx The SHA-512 context.
  159. * \param input The buffer holding the data.
  160. * \param ilen The length of the input data.
  161. */
  162. MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
  163. mbedtls_sha512_context *ctx,
  164. const unsigned char *input,
  165. size_t ilen )
  166. {
  167. mbedtls_sha512_update_ret( ctx, input, ilen );
  168. }
  169. /**
  170. * \brief This function finishes the SHA-512 operation, and writes
  171. * the result to the output buffer.
  172. *
  173. * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
  174. *
  175. * \param ctx The SHA-512 context.
  176. * \param output The SHA-384 or SHA-512 checksum result.
  177. */
  178. MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
  179. mbedtls_sha512_context *ctx,
  180. unsigned char output[64] )
  181. {
  182. mbedtls_sha512_finish_ret( ctx, output );
  183. }
  184. /**
  185. * \brief This function processes a single data block within
  186. * the ongoing SHA-512 computation. This function is for
  187. * internal use only.
  188. *
  189. * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
  190. *
  191. * \param ctx The SHA-512 context.
  192. * \param data The buffer holding one block of data.
  193. */
  194. MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
  195. mbedtls_sha512_context *ctx,
  196. const unsigned char data[128] )
  197. {
  198. mbedtls_internal_sha512_process( ctx, data );
  199. }
  200. #undef MBEDTLS_DEPRECATED
  201. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #else /* MBEDTLS_SHA512_ALT */
  206. #include "sha512_alt.h"
  207. #endif /* MBEDTLS_SHA512_ALT */
  208. #ifdef __cplusplus
  209. extern "C" {
  210. #endif
  211. /**
  212. * \brief This function calculates the SHA-512 or SHA-384
  213. * checksum of a buffer.
  214. *
  215. * The function allocates the context, performs the
  216. * calculation, and frees the context.
  217. *
  218. * The SHA-512 result is calculated as
  219. * output = SHA-512(input buffer).
  220. *
  221. * \param input The buffer holding the input data.
  222. * \param ilen The length of the input data.
  223. * \param output The SHA-384 or SHA-512 checksum result.
  224. * \param is384 Determines which function to use.
  225. * <ul><li>0: Use SHA-512.</li>
  226. * <li>1: Use SHA-384.</li></ul>
  227. *
  228. * \return \c 0 on success.
  229. */
  230. int mbedtls_sha512_ret( const unsigned char *input,
  231. size_t ilen,
  232. unsigned char output[64],
  233. int is384 );
  234. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  235. #if defined(MBEDTLS_DEPRECATED_WARNING)
  236. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  237. #else
  238. #define MBEDTLS_DEPRECATED
  239. #endif
  240. /**
  241. * \brief This function calculates the SHA-512 or SHA-384
  242. * checksum of a buffer.
  243. *
  244. * The function allocates the context, performs the
  245. * calculation, and frees the context.
  246. *
  247. * The SHA-512 result is calculated as
  248. * output = SHA-512(input buffer).
  249. *
  250. * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
  251. *
  252. * \param input The buffer holding the data.
  253. * \param ilen The length of the input data.
  254. * \param output The SHA-384 or SHA-512 checksum result.
  255. * \param is384 Determines which function to use.
  256. * <ul><li>0: Use SHA-512.</li>
  257. * <li>1: Use SHA-384.</li></ul>
  258. */
  259. MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
  260. const unsigned char *input,
  261. size_t ilen,
  262. unsigned char output[64],
  263. int is384 )
  264. {
  265. mbedtls_sha512_ret( input, ilen, output, is384 );
  266. }
  267. #undef MBEDTLS_DEPRECATED
  268. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  269. /**
  270. * \brief The SHA-384 or SHA-512 checkup routine.
  271. *
  272. * \return \c 0 on success, or \c 1 on failure.
  273. */
  274. int mbedtls_sha512_self_test( int verbose );
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #endif /* mbedtls_sha512.h */