sha1.h 13 KB

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