sha1.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * \file sha1.h
  3. *
  4. * \brief This file contains SHA-1 definitions and functions.
  5. *
  6. * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in
  7. * <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
  8. *
  9. * \warning SHA-1 is considered a weak message digest and its use constitutes
  10. * a security risk. We recommend considering stronger message
  11. * digests instead.
  12. */
  13. /*
  14. * Copyright The Mbed TLS Contributors
  15. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  16. */
  17. #ifndef MBEDTLS_SHA1_H
  18. #define MBEDTLS_SHA1_H
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. /* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
  27. /** SHA-1 hardware accelerator failed */
  28. #define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035
  29. /** SHA-1 input data was malformed. */
  30. #define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #if !defined(MBEDTLS_SHA1_ALT)
  35. // Regular implementation
  36. //
  37. /**
  38. * \brief The SHA-1 context structure.
  39. *
  40. * \warning SHA-1 is considered a weak message digest and its use
  41. * constitutes a security risk. We recommend considering
  42. * stronger message digests instead.
  43. *
  44. */
  45. typedef struct mbedtls_sha1_context {
  46. uint32_t total[2]; /*!< The number of Bytes processed. */
  47. uint32_t state[5]; /*!< The intermediate digest state. */
  48. unsigned char buffer[64]; /*!< The data block being processed. */
  49. }
  50. mbedtls_sha1_context;
  51. #else /* MBEDTLS_SHA1_ALT */
  52. #include "sha1_alt.h"
  53. #endif /* MBEDTLS_SHA1_ALT */
  54. /**
  55. * \brief This function initializes a SHA-1 context.
  56. *
  57. * \warning SHA-1 is considered a weak message digest and its use
  58. * constitutes a security risk. We recommend considering
  59. * stronger message digests instead.
  60. *
  61. * \param ctx The SHA-1 context to initialize.
  62. * This must not be \c NULL.
  63. *
  64. */
  65. void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
  66. /**
  67. * \brief This function clears a SHA-1 context.
  68. *
  69. * \warning SHA-1 is considered a weak message digest and its use
  70. * constitutes a security risk. We recommend considering
  71. * stronger message digests instead.
  72. *
  73. * \param ctx The SHA-1 context to clear. This may be \c NULL,
  74. * in which case this function does nothing. If it is
  75. * not \c NULL, it must point to an initialized
  76. * SHA-1 context.
  77. *
  78. */
  79. void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
  80. /**
  81. * \brief This function clones the state of a SHA-1 context.
  82. *
  83. * \warning SHA-1 is considered a weak message digest and its use
  84. * constitutes a security risk. We recommend considering
  85. * stronger message digests instead.
  86. *
  87. * \param dst The SHA-1 context to clone to. This must be initialized.
  88. * \param src The SHA-1 context to clone from. This must be initialized.
  89. *
  90. */
  91. void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
  92. const mbedtls_sha1_context *src);
  93. /**
  94. * \brief This function starts a SHA-1 checksum calculation.
  95. *
  96. * \warning SHA-1 is considered a weak message digest and its use
  97. * constitutes a security risk. We recommend considering
  98. * stronger message digests instead.
  99. *
  100. * \param ctx The SHA-1 context to initialize. This must be initialized.
  101. *
  102. * \return \c 0 on success.
  103. * \return A negative error code on failure.
  104. *
  105. */
  106. int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx);
  107. /**
  108. * \brief This function feeds an input buffer into an ongoing SHA-1
  109. * checksum calculation.
  110. *
  111. * \warning SHA-1 is considered a weak message digest and its use
  112. * constitutes a security risk. We recommend considering
  113. * stronger message digests instead.
  114. *
  115. * \param ctx The SHA-1 context. This must be initialized
  116. * and have a hash operation started.
  117. * \param input The buffer holding the input data.
  118. * This must be a readable buffer of length \p ilen Bytes.
  119. * \param ilen The length of the input data \p input in Bytes.
  120. *
  121. * \return \c 0 on success.
  122. * \return A negative error code on failure.
  123. */
  124. int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
  125. const unsigned char *input,
  126. size_t ilen);
  127. /**
  128. * \brief This function finishes the SHA-1 operation, and writes
  129. * the result to the output buffer.
  130. *
  131. * \warning SHA-1 is considered a weak message digest and its use
  132. * constitutes a security risk. We recommend considering
  133. * stronger message digests instead.
  134. *
  135. * \param ctx The SHA-1 context to use. This must be initialized and
  136. * have a hash operation started.
  137. * \param output The SHA-1 checksum result. This must be a writable
  138. * buffer of length \c 20 Bytes.
  139. *
  140. * \return \c 0 on success.
  141. * \return A negative error code on failure.
  142. */
  143. int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
  144. unsigned char output[20]);
  145. /**
  146. * \brief SHA-1 process data block (internal use only).
  147. *
  148. * \warning SHA-1 is considered a weak message digest and its use
  149. * constitutes a security risk. We recommend considering
  150. * stronger message digests instead.
  151. *
  152. * \param ctx The SHA-1 context to use. This must be initialized.
  153. * \param data The data block being processed. This must be a
  154. * readable buffer of length \c 64 Bytes.
  155. *
  156. * \return \c 0 on success.
  157. * \return A negative error code on failure.
  158. *
  159. */
  160. int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
  161. const unsigned char data[64]);
  162. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  163. #if defined(MBEDTLS_DEPRECATED_WARNING)
  164. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  165. #else
  166. #define MBEDTLS_DEPRECATED
  167. #endif
  168. /**
  169. * \brief This function starts a SHA-1 checksum calculation.
  170. *
  171. * \warning SHA-1 is considered a weak message digest and its use
  172. * constitutes a security risk. We recommend considering
  173. * stronger message digests instead.
  174. *
  175. * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
  176. *
  177. * \param ctx The SHA-1 context to initialize. This must be initialized.
  178. *
  179. */
  180. MBEDTLS_DEPRECATED void mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
  181. /**
  182. * \brief This function feeds an input buffer into an ongoing SHA-1
  183. * checksum calculation.
  184. *
  185. * \warning SHA-1 is considered a weak message digest and its use
  186. * constitutes a security risk. We recommend considering
  187. * stronger message digests instead.
  188. *
  189. * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
  190. *
  191. * \param ctx The SHA-1 context. This must be initialized and
  192. * have a hash operation started.
  193. * \param input The buffer holding the input data.
  194. * This must be a readable buffer of length \p ilen Bytes.
  195. * \param ilen The length of the input data \p input in Bytes.
  196. *
  197. */
  198. MBEDTLS_DEPRECATED void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
  199. const unsigned char *input,
  200. size_t ilen);
  201. /**
  202. * \brief This function finishes the SHA-1 operation, and writes
  203. * the result to the output buffer.
  204. *
  205. * \warning SHA-1 is considered a weak message digest and its use
  206. * constitutes a security risk. We recommend considering
  207. * stronger message digests instead.
  208. *
  209. * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
  210. *
  211. * \param ctx The SHA-1 context. This must be initialized and
  212. * have a hash operation started.
  213. * \param output The SHA-1 checksum result.
  214. * This must be a writable buffer of length \c 20 Bytes.
  215. */
  216. MBEDTLS_DEPRECATED void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
  217. unsigned char output[20]);
  218. /**
  219. * \brief SHA-1 process data block (internal use only).
  220. *
  221. * \warning SHA-1 is considered a weak message digest and its use
  222. * constitutes a security risk. We recommend considering
  223. * stronger message digests instead.
  224. *
  225. * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
  226. *
  227. * \param ctx The SHA-1 context. This must be initialized.
  228. * \param data The data block being processed.
  229. * This must be a readable buffer of length \c 64 bytes.
  230. *
  231. */
  232. MBEDTLS_DEPRECATED void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
  233. const unsigned char data[64]);
  234. #undef MBEDTLS_DEPRECATED
  235. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  236. /**
  237. * \brief This function calculates the SHA-1 checksum of a buffer.
  238. *
  239. * The function allocates the context, performs the
  240. * calculation, and frees the context.
  241. *
  242. * The SHA-1 result is calculated as
  243. * output = SHA-1(input buffer).
  244. *
  245. * \warning SHA-1 is considered a weak message digest and its use
  246. * constitutes a security risk. We recommend considering
  247. * stronger message digests instead.
  248. *
  249. * \param input The buffer holding the input data.
  250. * This must be a readable buffer of length \p ilen Bytes.
  251. * \param ilen The length of the input data \p input in Bytes.
  252. * \param output The SHA-1 checksum result.
  253. * This must be a writable buffer of length \c 20 Bytes.
  254. *
  255. * \return \c 0 on success.
  256. * \return A negative error code on failure.
  257. *
  258. */
  259. int mbedtls_sha1_ret(const unsigned char *input,
  260. size_t ilen,
  261. unsigned char output[20]);
  262. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  263. #if defined(MBEDTLS_DEPRECATED_WARNING)
  264. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  265. #else
  266. #define MBEDTLS_DEPRECATED
  267. #endif
  268. /**
  269. * \brief This function calculates the SHA-1 checksum of a buffer.
  270. *
  271. * The function allocates the context, performs the
  272. * calculation, and frees the context.
  273. *
  274. * The SHA-1 result is calculated as
  275. * output = SHA-1(input buffer).
  276. *
  277. * \warning SHA-1 is considered a weak message digest and its use
  278. * constitutes a security risk. We recommend considering
  279. * stronger message digests instead.
  280. *
  281. * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
  282. *
  283. * \param input The buffer holding the input data.
  284. * This must be a readable buffer of length \p ilen Bytes.
  285. * \param ilen The length of the input data \p input in Bytes.
  286. * \param output The SHA-1 checksum result. This must be a writable
  287. * buffer of size \c 20 Bytes.
  288. *
  289. */
  290. MBEDTLS_DEPRECATED void mbedtls_sha1(const unsigned char *input,
  291. size_t ilen,
  292. unsigned char output[20]);
  293. #undef MBEDTLS_DEPRECATED
  294. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  295. #if defined(MBEDTLS_SELF_TEST)
  296. /**
  297. * \brief The SHA-1 checkup routine.
  298. *
  299. * \warning SHA-1 is considered a weak message digest and its use
  300. * constitutes a security risk. We recommend considering
  301. * stronger message digests instead.
  302. *
  303. * \return \c 0 on success.
  304. * \return \c 1 on failure.
  305. *
  306. */
  307. int mbedtls_sha1_self_test(int verbose);
  308. #endif /* MBEDTLS_SELF_TEST */
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif /* mbedtls_sha1.h */