sha1.h 12 KB

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