md5.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * \file md5.h
  3. *
  4. * \brief MD5 message digest algorithm (hash function)
  5. *
  6. * \warning MD5 is considered a weak message digest and its use constitutes a
  7. * security risk. We recommend considering stronger message
  8. * digests instead.
  9. */
  10. /*
  11. * Copyright (C) 2006-2015, ARM Limited, 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_MD5_H
  29. #define MBEDTLS_MD5_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_MD5_HW_ACCEL_FAILED -0x002F /**< MD5 hardware accelerator failed */
  38. #if !defined(MBEDTLS_MD5_ALT)
  39. // Regular implementation
  40. //
  41. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  42. !defined(inline) && !defined(__cplusplus)
  43. #define inline __inline
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * \brief MD5 context structure
  50. *
  51. * \warning MD5 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]; /*!< number of bytes processed */
  59. uint32_t state[4]; /*!< intermediate digest state */
  60. unsigned char buffer[64]; /*!< data block being processed */
  61. }
  62. mbedtls_md5_context;
  63. /**
  64. * \brief Initialize MD5 context
  65. *
  66. * \param ctx MD5 context to be initialized
  67. *
  68. * \warning MD5 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_md5_init( mbedtls_md5_context *ctx );
  74. /**
  75. * \brief Clear MD5 context
  76. *
  77. * \param ctx MD5 context to be cleared
  78. *
  79. * \warning MD5 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_md5_free( mbedtls_md5_context *ctx );
  85. /**
  86. * \brief Clone (the state of) an MD5 context
  87. *
  88. * \param dst The destination context
  89. * \param src The context to be cloned
  90. *
  91. * \warning MD5 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_md5_clone( mbedtls_md5_context *dst,
  97. const mbedtls_md5_context *src );
  98. /**
  99. * \brief MD5 context setup
  100. *
  101. * \param ctx context to be initialized
  102. *
  103. * \return 0 if successful
  104. *
  105. * \warning MD5 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_md5_starts_ret( mbedtls_md5_context *ctx );
  111. /**
  112. * \brief MD5 process buffer
  113. *
  114. * \param ctx MD5 context
  115. * \param input buffer holding the data
  116. * \param ilen length of the input data
  117. *
  118. * \return 0 if successful
  119. *
  120. * \warning MD5 is considered a weak message digest and its use
  121. * constitutes a security risk. We recommend considering
  122. * stronger message digests instead.
  123. *
  124. */
  125. int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
  126. const unsigned char *input,
  127. size_t ilen );
  128. /**
  129. * \brief MD5 final digest
  130. *
  131. * \param ctx MD5 context
  132. * \param output MD5 checksum result
  133. *
  134. * \return 0 if successful
  135. *
  136. * \warning MD5 is considered a weak message digest and its use
  137. * constitutes a security risk. We recommend considering
  138. * stronger message digests instead.
  139. *
  140. */
  141. int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
  142. unsigned char output[16] );
  143. /**
  144. * \brief MD5 process data block (internal use only)
  145. *
  146. * \param ctx MD5 context
  147. * \param data buffer holding one block of data
  148. *
  149. * \return 0 if successful
  150. *
  151. * \warning MD5 is considered a weak message digest and its use
  152. * constitutes a security risk. We recommend considering
  153. * stronger message digests instead.
  154. *
  155. */
  156. int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
  157. const unsigned char data[64] );
  158. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  159. #if defined(MBEDTLS_DEPRECATED_WARNING)
  160. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  161. #else
  162. #define MBEDTLS_DEPRECATED
  163. #endif
  164. /**
  165. * \brief MD5 context setup
  166. *
  167. * \deprecated Superseded by mbedtls_md5_starts_ret() in 2.7.0
  168. *
  169. * \param ctx context to be initialized
  170. *
  171. * \warning MD5 is considered a weak message digest and its use
  172. * constitutes a security risk. We recommend considering
  173. * stronger message digests instead.
  174. *
  175. */
  176. MBEDTLS_DEPRECATED static inline void mbedtls_md5_starts(
  177. mbedtls_md5_context *ctx )
  178. {
  179. mbedtls_md5_starts_ret( ctx );
  180. }
  181. /**
  182. * \brief MD5 process buffer
  183. *
  184. * \deprecated Superseded by mbedtls_md5_update_ret() in 2.7.0
  185. *
  186. * \param ctx MD5 context
  187. * \param input buffer holding the data
  188. * \param ilen length of the input data
  189. *
  190. * \warning MD5 is considered a weak message digest and its use
  191. * constitutes a security risk. We recommend considering
  192. * stronger message digests instead.
  193. *
  194. */
  195. MBEDTLS_DEPRECATED static inline void mbedtls_md5_update(
  196. mbedtls_md5_context *ctx,
  197. const unsigned char *input,
  198. size_t ilen )
  199. {
  200. mbedtls_md5_update_ret( ctx, input, ilen );
  201. }
  202. /**
  203. * \brief MD5 final digest
  204. *
  205. * \deprecated Superseded by mbedtls_md5_finish_ret() in 2.7.0
  206. *
  207. * \param ctx MD5 context
  208. * \param output MD5 checksum result
  209. *
  210. * \warning MD5 is considered a weak message digest and its use
  211. * constitutes a security risk. We recommend considering
  212. * stronger message digests instead.
  213. *
  214. */
  215. MBEDTLS_DEPRECATED static inline void mbedtls_md5_finish(
  216. mbedtls_md5_context *ctx,
  217. unsigned char output[16] )
  218. {
  219. mbedtls_md5_finish_ret( ctx, output );
  220. }
  221. /**
  222. * \brief MD5 process data block (internal use only)
  223. *
  224. * \deprecated Superseded by mbedtls_internal_md5_process() in 2.7.0
  225. *
  226. * \param ctx MD5 context
  227. * \param data buffer holding one block of data
  228. *
  229. * \warning MD5 is considered a weak message digest and its use
  230. * constitutes a security risk. We recommend considering
  231. * stronger message digests instead.
  232. *
  233. */
  234. MBEDTLS_DEPRECATED static inline void mbedtls_md5_process(
  235. mbedtls_md5_context *ctx,
  236. const unsigned char data[64] )
  237. {
  238. mbedtls_internal_md5_process( ctx, data );
  239. }
  240. #undef MBEDTLS_DEPRECATED
  241. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  242. #ifdef __cplusplus
  243. }
  244. #endif
  245. #else /* MBEDTLS_MD5_ALT */
  246. #include "md5_alt.h"
  247. #endif /* MBEDTLS_MD5_ALT */
  248. #ifdef __cplusplus
  249. extern "C" {
  250. #endif
  251. /**
  252. * \brief Output = MD5( input buffer )
  253. *
  254. * \param input buffer holding the data
  255. * \param ilen length of the input data
  256. * \param output MD5 checksum result
  257. *
  258. * \return 0 if successful
  259. *
  260. * \warning MD5 is considered a weak message digest and its use
  261. * constitutes a security risk. We recommend considering
  262. * stronger message digests instead.
  263. *
  264. */
  265. int mbedtls_md5_ret( const unsigned char *input,
  266. size_t ilen,
  267. unsigned char output[16] );
  268. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  269. #if defined(MBEDTLS_DEPRECATED_WARNING)
  270. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  271. #else
  272. #define MBEDTLS_DEPRECATED
  273. #endif
  274. /**
  275. * \brief Output = MD5( input buffer )
  276. *
  277. * \deprecated Superseded by mbedtls_md5_ret() in 2.7.0
  278. *
  279. * \param input buffer holding the data
  280. * \param ilen length of the input data
  281. * \param output MD5 checksum result
  282. *
  283. * \warning MD5 is considered a weak message digest and its use
  284. * constitutes a security risk. We recommend considering
  285. * stronger message digests instead.
  286. *
  287. */
  288. MBEDTLS_DEPRECATED static inline void mbedtls_md5( const unsigned char *input,
  289. size_t ilen,
  290. unsigned char output[16] )
  291. {
  292. mbedtls_md5_ret( input, ilen, output );
  293. }
  294. #undef MBEDTLS_DEPRECATED
  295. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  296. /**
  297. * \brief Checkup routine
  298. *
  299. * \return 0 if successful, or 1 if the test failed
  300. *
  301. * \warning MD5 is considered a weak message digest and its use
  302. * constitutes a security risk. We recommend considering
  303. * stronger message digests instead.
  304. *
  305. */
  306. int mbedtls_md5_self_test( int verbose );
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #endif /* mbedtls_md5.h */