sha1.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
  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
  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. *
  74. */
  75. void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
  76. /**
  77. * \brief This function clears a SHA-1 context.
  78. *
  79. * \warning SHA-1 is considered a weak message digest and its use
  80. * constitutes a security risk. We recommend considering
  81. * stronger message digests instead.
  82. *
  83. * \param ctx The SHA-1 context to clear.
  84. *
  85. */
  86. void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
  87. /**
  88. * \brief This function clones the state of a SHA-1 context.
  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 dst The SHA-1 context to clone to.
  95. * \param src The SHA-1 context to clone from.
  96. *
  97. */
  98. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  99. const mbedtls_sha1_context *src );
  100. /**
  101. * \brief This function starts a SHA-1 checksum calculation.
  102. *
  103. * \warning SHA-1 is considered a weak message digest and its use
  104. * constitutes a security risk. We recommend considering
  105. * stronger message digests instead.
  106. *
  107. * \param ctx The SHA-1 context to initialize.
  108. *
  109. * \return \c 0 on success.
  110. *
  111. */
  112. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
  113. /**
  114. * \brief This function feeds an input buffer into an ongoing SHA-1
  115. * checksum calculation.
  116. *
  117. * \warning SHA-1 is considered a weak message digest and its use
  118. * constitutes a security risk. We recommend considering
  119. * stronger message digests instead.
  120. *
  121. * \param ctx The SHA-1 context.
  122. * \param input The buffer holding the input data.
  123. * \param ilen The length of the input data.
  124. *
  125. * \return \c 0 on success.
  126. */
  127. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  128. const unsigned char *input,
  129. size_t ilen );
  130. /**
  131. * \brief This function finishes the SHA-1 operation, and writes
  132. * the result to the output buffer.
  133. *
  134. * \warning SHA-1 is considered a weak message digest and its use
  135. * constitutes a security risk. We recommend considering
  136. * stronger message digests instead.
  137. *
  138. * \param ctx The SHA-1 context.
  139. * \param output The SHA-1 checksum result.
  140. *
  141. * \return \c 0 on success.
  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.
  153. * \param data The data block being processed.
  154. *
  155. * \return \c 0 on success.
  156. *
  157. */
  158. int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
  159. const unsigned char data[64] );
  160. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  161. #if defined(MBEDTLS_DEPRECATED_WARNING)
  162. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  163. #else
  164. #define MBEDTLS_DEPRECATED
  165. #endif
  166. /**
  167. * \brief This function starts a SHA-1 checksum calculation.
  168. *
  169. * \warning SHA-1 is considered a weak message digest and its use
  170. * constitutes a security risk. We recommend considering
  171. * stronger message digests instead.
  172. *
  173. * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
  174. *
  175. * \param ctx The SHA-1 context to initialize.
  176. *
  177. */
  178. MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
  179. /**
  180. * \brief This function feeds an input buffer into an ongoing SHA-1
  181. * checksum calculation.
  182. *
  183. * \warning SHA-1 is considered a weak message digest and its use
  184. * constitutes a security risk. We recommend considering
  185. * stronger message digests instead.
  186. *
  187. * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
  188. *
  189. * \param ctx The SHA-1 context.
  190. * \param input The buffer holding the input data.
  191. * \param ilen The length of the input data.
  192. *
  193. */
  194. MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
  195. const unsigned char *input,
  196. size_t ilen );
  197. /**
  198. * \brief This function finishes the SHA-1 operation, and writes
  199. * the result to the output buffer.
  200. *
  201. * \warning SHA-1 is considered a weak message digest and its use
  202. * constitutes a security risk. We recommend considering
  203. * stronger message digests instead.
  204. *
  205. * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
  206. *
  207. * \param ctx The SHA-1 context.
  208. * \param output The SHA-1 checksum result.
  209. *
  210. */
  211. MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
  212. unsigned char output[20] );
  213. /**
  214. * \brief SHA-1 process data block (internal use only).
  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_internal_sha1_process() in 2.7.0.
  221. *
  222. * \param ctx The SHA-1 context.
  223. * \param data The data block being processed.
  224. *
  225. */
  226. MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
  227. const unsigned char data[64] );
  228. #undef MBEDTLS_DEPRECATED
  229. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  230. /**
  231. * \brief This function calculates the SHA-1 checksum of a buffer.
  232. *
  233. * The function allocates the context, performs the
  234. * calculation, and frees the context.
  235. *
  236. * The SHA-1 result is calculated as
  237. * output = SHA-1(input buffer).
  238. *
  239. * \warning SHA-1 is considered a weak message digest and its use
  240. * constitutes a security risk. We recommend considering
  241. * stronger message digests instead.
  242. *
  243. * \param input The buffer holding the input data.
  244. * \param ilen The length of the input data.
  245. * \param output The SHA-1 checksum result.
  246. *
  247. * \return \c 0 on success.
  248. *
  249. */
  250. int mbedtls_sha1_ret( const unsigned char *input,
  251. size_t ilen,
  252. unsigned char output[20] );
  253. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  254. #if defined(MBEDTLS_DEPRECATED_WARNING)
  255. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  256. #else
  257. #define MBEDTLS_DEPRECATED
  258. #endif
  259. /**
  260. * \brief This function calculates the SHA-1 checksum of a buffer.
  261. *
  262. * The function allocates the context, performs the
  263. * calculation, and frees the context.
  264. *
  265. * The SHA-1 result is calculated as
  266. * output = SHA-1(input buffer).
  267. *
  268. * \warning SHA-1 is considered a weak message digest and its use
  269. * constitutes a security risk. We recommend considering
  270. * stronger message digests instead.
  271. *
  272. * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
  273. *
  274. * \param input The buffer holding the input data.
  275. * \param ilen The length of the input data.
  276. * \param output The SHA-1 checksum result.
  277. *
  278. */
  279. MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
  280. size_t ilen,
  281. unsigned char output[20] );
  282. #undef MBEDTLS_DEPRECATED
  283. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  284. /**
  285. * \brief The SHA-1 checkup routine.
  286. *
  287. * \warning SHA-1 is considered a weak message digest and its use
  288. * constitutes a security risk. We recommend considering
  289. * stronger message digests instead.
  290. *
  291. * \return \c 0 on success.
  292. * \return \c 1 on failure.
  293. *
  294. */
  295. int mbedtls_sha1_self_test( int verbose );
  296. #ifdef __cplusplus
  297. }
  298. #endif
  299. #endif /* mbedtls_sha1.h */