sha1.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * \file sha1.h
  3. *
  4. * \brief The SHA-1 cryptographic hash function.
  5. *
  6. * \warning SHA-1 is considered a weak message digest and its use constitutes
  7. * a security risk. We recommend considering stronger message
  8. * digests instead.
  9. */
  10. /*
  11. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. * This file is part of Mbed TLS (https://tls.mbed.org)
  27. */
  28. #ifndef MBEDTLS_SHA1_H
  29. #define MBEDTLS_SHA1_H
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
  38. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  39. !defined(inline) && !defined(__cplusplus)
  40. #define inline __inline
  41. #endif
  42. #if !defined(MBEDTLS_SHA1_ALT)
  43. // Regular implementation
  44. //
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * \brief The SHA-1 context structure.
  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. */
  56. typedef struct
  57. {
  58. uint32_t total[2]; /*!< The number of Bytes processed. */
  59. uint32_t state[5]; /*!< The intermediate digest state. */
  60. unsigned char buffer[64]; /*!< The data block being processed. */
  61. }
  62. mbedtls_sha1_context;
  63. /**
  64. * \brief This function initializes a SHA-1 context.
  65. *
  66. * \param ctx The SHA-1 context to initialize.
  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. */
  73. void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
  74. /**
  75. * \brief This function clears a SHA-1 context.
  76. *
  77. * \param ctx The SHA-1 context to clear.
  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. */
  84. void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
  85. /**
  86. * \brief This function clones the state of a SHA-1 context.
  87. *
  88. * \param dst The destination context.
  89. * \param src The context to clone.
  90. *
  91. * \warning SHA-1 is considered a weak message digest and its use
  92. * constitutes a security risk. We recommend considering
  93. * stronger message digests instead.
  94. *
  95. */
  96. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  97. const mbedtls_sha1_context *src );
  98. /**
  99. * \brief This function starts a SHA-1 checksum calculation.
  100. *
  101. * \param ctx The context to initialize.
  102. *
  103. * \return \c 0 if successful
  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. */
  110. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
  111. /**
  112. * \brief This function feeds an input buffer into an ongoing SHA-1
  113. * checksum calculation.
  114. *
  115. * \param ctx The SHA-1 context.
  116. * \param input The buffer holding the input data.
  117. * \param ilen The length of the input data.
  118. *
  119. * \return \c 0 if successful
  120. *
  121. * \warning SHA-1 is considered a weak message digest and its use
  122. * constitutes a security risk. We recommend considering
  123. * stronger message digests instead.
  124. *
  125. */
  126. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  127. const unsigned char *input,
  128. size_t ilen );
  129. /**
  130. * \brief This function finishes the SHA-1 operation, and writes
  131. * the result to the output buffer.
  132. *
  133. * \param ctx The SHA-1 context.
  134. * \param output The SHA-1 checksum result.
  135. *
  136. * \return \c 0 if successful
  137. *
  138. * \warning SHA-1 is considered a weak message digest and its use
  139. * constitutes a security risk. We recommend considering
  140. * stronger message digests instead.
  141. *
  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. * \param ctx SHA-1 context
  149. * \param data The data block being processed.
  150. *
  151. * \return \c 0 if successful
  152. *
  153. * \warning SHA-1 is considered a weak message digest and its use
  154. * constitutes a security risk. We recommend considering
  155. * stronger message digests instead.
  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 SHA-1 context setup
  168. *
  169. * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
  170. *
  171. * \param ctx The SHA-1 context to be initialized.
  172. *
  173. * \warning SHA-1 is considered a weak message digest and its use
  174. * constitutes a security risk. We recommend considering
  175. * stronger message digests instead.
  176. *
  177. */
  178. MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
  179. mbedtls_sha1_context *ctx )
  180. {
  181. mbedtls_sha1_starts_ret( ctx );
  182. }
  183. /**
  184. * \brief SHA-1 process buffer
  185. *
  186. * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
  187. *
  188. * \param ctx The SHA-1 context.
  189. * \param input The buffer holding the input data.
  190. * \param ilen The length of the input data.
  191. *
  192. * \warning SHA-1 is considered a weak message digest and its use
  193. * constitutes a security risk. We recommend considering
  194. * stronger message digests instead.
  195. *
  196. */
  197. MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
  198. mbedtls_sha1_context *ctx,
  199. const unsigned char *input,
  200. size_t ilen )
  201. {
  202. mbedtls_sha1_update_ret( ctx, input, ilen );
  203. }
  204. /**
  205. * \brief SHA-1 final digest
  206. *
  207. * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
  208. *
  209. * \param ctx The SHA-1 context.
  210. * \param output The SHA-1 checksum result.
  211. *
  212. * \warning SHA-1 is considered a weak message digest and its use
  213. * constitutes a security risk. We recommend considering
  214. * stronger message digests instead.
  215. *
  216. */
  217. MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
  218. mbedtls_sha1_context *ctx,
  219. unsigned char output[20] )
  220. {
  221. mbedtls_sha1_finish_ret( ctx, output );
  222. }
  223. /**
  224. * \brief SHA-1 process data block (internal use only)
  225. *
  226. * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
  227. *
  228. * \param ctx The SHA-1 context.
  229. * \param data The data block being processed.
  230. *
  231. * \warning SHA-1 is considered a weak message digest and its use
  232. * constitutes a security risk. We recommend considering
  233. * stronger message digests instead.
  234. *
  235. */
  236. MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
  237. mbedtls_sha1_context *ctx,
  238. const unsigned char data[64] )
  239. {
  240. mbedtls_internal_sha1_process( ctx, data );
  241. }
  242. #undef MBEDTLS_DEPRECATED
  243. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #else /* MBEDTLS_SHA1_ALT */
  248. #include "sha1_alt.h"
  249. #endif /* MBEDTLS_SHA1_ALT */
  250. #ifdef __cplusplus
  251. extern "C" {
  252. #endif
  253. /**
  254. * \brief This function calculates the SHA-1 checksum of a buffer.
  255. *
  256. * The function allocates the context, performs the
  257. * calculation, and frees the context.
  258. *
  259. * The SHA-1 result is calculated as
  260. * output = SHA-1(input buffer).
  261. *
  262. * \param input The buffer holding the input data.
  263. * \param ilen The length of the input data.
  264. * \param output The SHA-1 checksum result.
  265. *
  266. * \return \c 0 if successful
  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. */
  273. int mbedtls_sha1_ret( const unsigned char *input,
  274. size_t ilen,
  275. unsigned char output[20] );
  276. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  277. #if defined(MBEDTLS_DEPRECATED_WARNING)
  278. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  279. #else
  280. #define MBEDTLS_DEPRECATED
  281. #endif
  282. /**
  283. * \brief Output = SHA-1( input buffer )
  284. *
  285. * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
  286. *
  287. * \param input The buffer holding the input data.
  288. * \param ilen The length of the input data.
  289. * \param output The SHA-1 checksum result.
  290. *
  291. * \warning SHA-1 is considered a weak message digest and its use
  292. * constitutes a security risk. We recommend considering
  293. * stronger message digests instead.
  294. *
  295. */
  296. MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
  297. size_t ilen,
  298. unsigned char output[20] )
  299. {
  300. mbedtls_sha1_ret( input, ilen, output );
  301. }
  302. #undef MBEDTLS_DEPRECATED
  303. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  304. /**
  305. * \brief The SHA-1 checkup routine.
  306. *
  307. * \return \c 0 on success, or \c 1 on failure.
  308. *
  309. * \warning SHA-1 is considered a weak message digest and its use
  310. * constitutes a security risk. We recommend considering
  311. * stronger message digests instead.
  312. *
  313. */
  314. int mbedtls_sha1_self_test( int verbose );
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. #endif /* mbedtls_sha1.h */