sha1.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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(MBEDTLS_SHA1_ALT)
  39. // Regular implementation
  40. //
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * \brief The SHA-1 context structure.
  46. *
  47. * \warning SHA-1 is considered a weak message digest and its use
  48. * constitutes a security risk. We recommend considering
  49. * stronger message digests instead.
  50. *
  51. */
  52. typedef struct
  53. {
  54. uint32_t total[2]; /*!< The number of Bytes processed. */
  55. uint32_t state[5]; /*!< The intermediate digest state. */
  56. unsigned char buffer[64]; /*!< The data block being processed. */
  57. }
  58. mbedtls_sha1_context;
  59. /**
  60. * \brief This function initializes a SHA-1 context.
  61. *
  62. * \param ctx The SHA-1 context to initialize.
  63. *
  64. * \warning SHA-1 is considered a weak message digest and its use
  65. * constitutes a security risk. We recommend considering
  66. * stronger message digests instead.
  67. *
  68. */
  69. void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
  70. /**
  71. * \brief This function clears a SHA-1 context.
  72. *
  73. * \param ctx The SHA-1 context to clear.
  74. *
  75. * \warning SHA-1 is considered a weak message digest and its use
  76. * constitutes a security risk. We recommend considering
  77. * stronger message digests instead.
  78. *
  79. */
  80. void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
  81. /**
  82. * \brief This function clones the state of a SHA-1 context.
  83. *
  84. * \param dst The destination context.
  85. * \param src The context to clone.
  86. *
  87. * \warning SHA-1 is considered a weak message digest and its use
  88. * constitutes a security risk. We recommend considering
  89. * stronger message digests instead.
  90. *
  91. */
  92. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  93. const mbedtls_sha1_context *src );
  94. /**
  95. * \brief This function starts a SHA-1 checksum calculation.
  96. *
  97. * \param ctx The context to initialize.
  98. *
  99. * \return \c 0 if successful
  100. *
  101. * \warning SHA-1 is considered a weak message digest and its use
  102. * constitutes a security risk. We recommend considering
  103. * stronger message digests instead.
  104. *
  105. */
  106. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
  107. /**
  108. * \brief This function feeds an input buffer into an ongoing SHA-1
  109. * checksum calculation.
  110. *
  111. * \param ctx The SHA-1 context.
  112. * \param input The buffer holding the input data.
  113. * \param ilen The length of the input data.
  114. *
  115. * \return \c 0 if successful
  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. */
  122. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  123. const unsigned char *input,
  124. size_t ilen );
  125. /**
  126. * \brief This function finishes the SHA-1 operation, and writes
  127. * the result to the output buffer.
  128. *
  129. * \param ctx The SHA-1 context.
  130. * \param output The SHA-1 checksum result.
  131. *
  132. * \return \c 0 if successful
  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. */
  139. int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
  140. unsigned char output[20] );
  141. /**
  142. * \brief SHA-1 process data block (internal use only)
  143. *
  144. * \param ctx SHA-1 context
  145. * \param data The data block being processed.
  146. *
  147. * \return \c 0 if successful
  148. *
  149. * \warning SHA-1 is considered a weak message digest and its use
  150. * constitutes a security risk. We recommend considering
  151. * stronger message digests instead.
  152. *
  153. */
  154. int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
  155. const unsigned char data[64] );
  156. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  157. #if defined(MBEDTLS_DEPRECATED_WARNING)
  158. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  159. #else
  160. #define MBEDTLS_DEPRECATED
  161. #endif
  162. /**
  163. * \brief SHA-1 context setup
  164. *
  165. * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
  166. *
  167. * \param ctx The SHA-1 context to be initialized.
  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. */
  174. MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
  175. /**
  176. * \brief SHA-1 process buffer
  177. *
  178. * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
  179. *
  180. * \param ctx The SHA-1 context.
  181. * \param input The buffer holding the input data.
  182. * \param ilen The length of the input data.
  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. */
  189. MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
  190. const unsigned char *input,
  191. size_t ilen );
  192. /**
  193. * \brief SHA-1 final digest
  194. *
  195. * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
  196. *
  197. * \param ctx The SHA-1 context.
  198. * \param output The SHA-1 checksum result.
  199. *
  200. * \warning SHA-1 is considered a weak message digest and its use
  201. * constitutes a security risk. We recommend considering
  202. * stronger message digests instead.
  203. *
  204. */
  205. MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
  206. unsigned char output[20] );
  207. /**
  208. * \brief SHA-1 process data block (internal use only)
  209. *
  210. * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
  211. *
  212. * \param ctx The SHA-1 context.
  213. * \param data The data block being processed.
  214. *
  215. * \warning SHA-1 is considered a weak message digest and its use
  216. * constitutes a security risk. We recommend considering
  217. * stronger message digests instead.
  218. *
  219. */
  220. MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
  221. const unsigned char data[64] );
  222. #undef MBEDTLS_DEPRECATED
  223. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #else /* MBEDTLS_SHA1_ALT */
  228. #include "sha1_alt.h"
  229. #endif /* MBEDTLS_SHA1_ALT */
  230. #ifdef __cplusplus
  231. extern "C" {
  232. #endif
  233. /**
  234. * \brief This function calculates the SHA-1 checksum of a buffer.
  235. *
  236. * The function allocates the context, performs the
  237. * calculation, and frees the context.
  238. *
  239. * The SHA-1 result is calculated as
  240. * output = SHA-1(input buffer).
  241. *
  242. * \param input The buffer holding the input data.
  243. * \param ilen The length of the input data.
  244. * \param output The SHA-1 checksum result.
  245. *
  246. * \return \c 0 if successful
  247. *
  248. * \warning SHA-1 is considered a weak message digest and its use
  249. * constitutes a security risk. We recommend considering
  250. * stronger message digests instead.
  251. *
  252. */
  253. int mbedtls_sha1_ret( const unsigned char *input,
  254. size_t ilen,
  255. unsigned char output[20] );
  256. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  257. #if defined(MBEDTLS_DEPRECATED_WARNING)
  258. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  259. #else
  260. #define MBEDTLS_DEPRECATED
  261. #endif
  262. /**
  263. * \brief Output = SHA-1( input buffer )
  264. *
  265. * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
  266. *
  267. * \param input The buffer holding the input data.
  268. * \param ilen The length of the input data.
  269. * \param output The SHA-1 checksum result.
  270. *
  271. * \warning SHA-1 is considered a weak message digest and its use
  272. * constitutes a security risk. We recommend considering
  273. * stronger message digests instead.
  274. *
  275. */
  276. MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
  277. size_t ilen,
  278. unsigned char output[20] );
  279. #undef MBEDTLS_DEPRECATED
  280. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  281. /**
  282. * \brief The SHA-1 checkup routine.
  283. *
  284. * \return \c 0 on success, or \c 1 on failure.
  285. *
  286. * \warning SHA-1 is considered a weak message digest and its use
  287. * constitutes a security risk. We recommend considering
  288. * stronger message digests instead.
  289. *
  290. */
  291. int mbedtls_sha1_self_test( int verbose );
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif /* mbedtls_sha1.h */