sha1.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include "mbedtls/private_access.h"
  20. #include "mbedtls/build_info.h"
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. /** SHA-1 input data was malformed. */
  24. #define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #if !defined(MBEDTLS_SHA1_ALT)
  29. // Regular implementation
  30. //
  31. /**
  32. * \brief The SHA-1 context structure.
  33. *
  34. * \warning SHA-1 is considered a weak message digest and its use
  35. * constitutes a security risk. We recommend considering
  36. * stronger message digests instead.
  37. *
  38. */
  39. typedef struct mbedtls_sha1_context {
  40. uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
  41. uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
  42. unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
  43. }
  44. mbedtls_sha1_context;
  45. #else /* MBEDTLS_SHA1_ALT */
  46. #include "sha1_alt.h"
  47. #endif /* MBEDTLS_SHA1_ALT */
  48. /**
  49. * \brief This function initializes a SHA-1 context.
  50. *
  51. * \warning SHA-1 is considered a weak message digest and its use
  52. * constitutes a security risk. We recommend considering
  53. * stronger message digests instead.
  54. *
  55. * \param ctx The SHA-1 context to initialize.
  56. * This must not be \c NULL.
  57. *
  58. */
  59. void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
  60. /**
  61. * \brief This function clears a SHA-1 context.
  62. *
  63. * \warning SHA-1 is considered a weak message digest and its use
  64. * constitutes a security risk. We recommend considering
  65. * stronger message digests instead.
  66. *
  67. * \param ctx The SHA-1 context to clear. This may be \c NULL,
  68. * in which case this function does nothing. If it is
  69. * not \c NULL, it must point to an initialized
  70. * SHA-1 context.
  71. *
  72. */
  73. void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
  74. /**
  75. * \brief This function clones the state of a SHA-1 context.
  76. *
  77. * \warning SHA-1 is considered a weak message digest and its use
  78. * constitutes a security risk. We recommend considering
  79. * stronger message digests instead.
  80. *
  81. * \param dst The SHA-1 context to clone to. This must be initialized.
  82. * \param src The SHA-1 context to clone from. This must be initialized.
  83. *
  84. */
  85. void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
  86. const mbedtls_sha1_context *src);
  87. /**
  88. * \brief This function starts a SHA-1 checksum calculation.
  89. *
  90. * \warning SHA-1 is considered a weak message digest and its use
  91. * constitutes a security risk. We recommend considering
  92. * stronger message digests instead.
  93. *
  94. * \param ctx The SHA-1 context to initialize. This must be initialized.
  95. *
  96. * \return \c 0 on success.
  97. * \return A negative error code on failure.
  98. *
  99. */
  100. int mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
  101. /**
  102. * \brief This function feeds an input buffer into an ongoing SHA-1
  103. * checksum calculation.
  104. *
  105. * \warning SHA-1 is considered a weak message digest and its use
  106. * constitutes a security risk. We recommend considering
  107. * stronger message digests instead.
  108. *
  109. * \param ctx The SHA-1 context. This must be initialized
  110. * and have a hash operation started.
  111. * \param input The buffer holding the input data.
  112. * This must be a readable buffer of length \p ilen Bytes.
  113. * \param ilen The length of the input data \p input in Bytes.
  114. *
  115. * \return \c 0 on success.
  116. * \return A negative error code on failure.
  117. */
  118. int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
  119. const unsigned char *input,
  120. size_t ilen);
  121. /**
  122. * \brief This function finishes the SHA-1 operation, and writes
  123. * the result to the output buffer.
  124. *
  125. * \warning SHA-1 is considered a weak message digest and its use
  126. * constitutes a security risk. We recommend considering
  127. * stronger message digests instead.
  128. *
  129. * \param ctx The SHA-1 context to use. This must be initialized and
  130. * have a hash operation started.
  131. * \param output The SHA-1 checksum result. This must be a writable
  132. * buffer of length \c 20 Bytes.
  133. *
  134. * \return \c 0 on success.
  135. * \return A negative error code on failure.
  136. */
  137. int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
  138. unsigned char output[20]);
  139. /**
  140. * \brief SHA-1 process data block (internal use only).
  141. *
  142. * \warning SHA-1 is considered a weak message digest and its use
  143. * constitutes a security risk. We recommend considering
  144. * stronger message digests instead.
  145. *
  146. * \param ctx The SHA-1 context to use. This must be initialized.
  147. * \param data The data block being processed. This must be a
  148. * readable buffer of length \c 64 Bytes.
  149. *
  150. * \return \c 0 on success.
  151. * \return A negative error code on failure.
  152. *
  153. */
  154. int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
  155. const unsigned char data[64]);
  156. /**
  157. * \brief This function calculates the SHA-1 checksum of a buffer.
  158. *
  159. * The function allocates the context, performs the
  160. * calculation, and frees the context.
  161. *
  162. * The SHA-1 result is calculated as
  163. * output = SHA-1(input buffer).
  164. *
  165. * \warning SHA-1 is considered a weak message digest and its use
  166. * constitutes a security risk. We recommend considering
  167. * stronger message digests instead.
  168. *
  169. * \param input The buffer holding the input data.
  170. * This must be a readable buffer of length \p ilen Bytes.
  171. * \param ilen The length of the input data \p input in Bytes.
  172. * \param output The SHA-1 checksum result.
  173. * This must be a writable buffer of length \c 20 Bytes.
  174. *
  175. * \return \c 0 on success.
  176. * \return A negative error code on failure.
  177. *
  178. */
  179. int mbedtls_sha1(const unsigned char *input,
  180. size_t ilen,
  181. unsigned char output[20]);
  182. #if defined(MBEDTLS_SELF_TEST)
  183. /**
  184. * \brief The SHA-1 checkup routine.
  185. *
  186. * \warning SHA-1 is considered a weak message digest and its use
  187. * constitutes a security risk. We recommend considering
  188. * stronger message digests instead.
  189. *
  190. * \return \c 0 on success.
  191. * \return \c 1 on failure.
  192. *
  193. */
  194. int mbedtls_sha1_self_test(int verbose);
  195. #endif /* MBEDTLS_SELF_TEST */
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #endif /* mbedtls_sha1.h */