• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 The Mbed TLS Contributors
12  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
13  */
14 #ifndef MBEDTLS_MD5_H
15 #define MBEDTLS_MD5_H
16 
17 #if !defined(MBEDTLS_CONFIG_FILE)
18 #include "mbedtls/config.h"
19 #else
20 #include MBEDTLS_CONFIG_FILE
21 #endif
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 /* MBEDTLS_ERR_MD5_HW_ACCEL_FAILED is deprecated and should not be used. */
27 /** MD5 hardware accelerator failed */
28 #define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED                   -0x002F
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #if !defined(MBEDTLS_MD5_ALT)
35 // Regular implementation
36 //
37 
38 /**
39  * \brief          MD5 context structure
40  *
41  * \warning        MD5 is considered a weak message digest and its use
42  *                 constitutes a security risk. We recommend considering
43  *                 stronger message digests instead.
44  *
45  */
46 typedef struct mbedtls_md5_context {
47     uint32_t total[2];          /*!< number of bytes processed  */
48     uint32_t state[4];          /*!< intermediate digest state  */
49     unsigned char buffer[64];   /*!< data block being processed */
50 }
51 mbedtls_md5_context;
52 
53 #else  /* MBEDTLS_MD5_ALT */
54 #include "md5_alt.h"
55 #endif /* MBEDTLS_MD5_ALT */
56 
57 /**
58  * \brief          Initialize MD5 context
59  *
60  * \param ctx      MD5 context to be initialized
61  *
62  * \warning        MD5 is considered a weak message digest and its use
63  *                 constitutes a security risk. We recommend considering
64  *                 stronger message digests instead.
65  *
66  */
67 void mbedtls_md5_init(mbedtls_md5_context *ctx);
68 
69 /**
70  * \brief          Clear MD5 context
71  *
72  * \param ctx      MD5 context to be cleared
73  *
74  * \warning        MD5 is considered a weak message digest and its use
75  *                 constitutes a security risk. We recommend considering
76  *                 stronger message digests instead.
77  *
78  */
79 void mbedtls_md5_free(mbedtls_md5_context *ctx);
80 
81 /**
82  * \brief          Clone (the state of) an MD5 context
83  *
84  * \param dst      The destination context
85  * \param src      The context to be cloned
86  *
87  * \warning        MD5 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_md5_clone(mbedtls_md5_context *dst,
93                        const mbedtls_md5_context *src);
94 
95 /**
96  * \brief          MD5 context setup
97  *
98  * \param ctx      context to be initialized
99  *
100  * \return         0 if successful
101  *
102  * \warning        MD5 is considered a weak message digest and its use
103  *                 constitutes a security risk. We recommend considering
104  *                 stronger message digests instead.
105  *
106  */
107 int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx);
108 
109 /**
110  * \brief          MD5 process buffer
111  *
112  * \param ctx      MD5 context
113  * \param input    buffer holding the data
114  * \param ilen     length of the input data
115  *
116  * \return         0 if successful
117  *
118  * \warning        MD5 is considered a weak message digest and its use
119  *                 constitutes a security risk. We recommend considering
120  *                 stronger message digests instead.
121  *
122  */
123 int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
124                            const unsigned char *input,
125                            size_t ilen);
126 
127 /**
128  * \brief          MD5 final digest
129  *
130  * \param ctx      MD5 context
131  * \param output   MD5 checksum result
132  *
133  * \return         0 if successful
134  *
135  * \warning        MD5 is considered a weak message digest and its use
136  *                 constitutes a security risk. We recommend considering
137  *                 stronger message digests instead.
138  *
139  */
140 int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
141                            unsigned char output[16]);
142 
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 
159 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
160 #if defined(MBEDTLS_DEPRECATED_WARNING)
161 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
162 #else
163 #define MBEDTLS_DEPRECATED
164 #endif
165 /**
166  * \brief          MD5 context setup
167  *
168  * \deprecated     Superseded by mbedtls_md5_starts_ret() in 2.7.0
169  *
170  * \param ctx      context to be initialized
171  *
172  * \warning        MD5 is considered a weak message digest and its use
173  *                 constitutes a security risk. We recommend considering
174  *                 stronger message digests instead.
175  *
176  */
177 MBEDTLS_DEPRECATED void mbedtls_md5_starts(mbedtls_md5_context *ctx);
178 
179 /**
180  * \brief          MD5 process buffer
181  *
182  * \deprecated     Superseded by mbedtls_md5_update_ret() in 2.7.0
183  *
184  * \param ctx      MD5 context
185  * \param input    buffer holding the data
186  * \param ilen     length of the input data
187  *
188  * \warning        MD5 is considered a weak message digest and its use
189  *                 constitutes a security risk. We recommend considering
190  *                 stronger message digests instead.
191  *
192  */
193 MBEDTLS_DEPRECATED void mbedtls_md5_update(mbedtls_md5_context *ctx,
194                                            const unsigned char *input,
195                                            size_t ilen);
196 
197 /**
198  * \brief          MD5 final digest
199  *
200  * \deprecated     Superseded by mbedtls_md5_finish_ret() in 2.7.0
201  *
202  * \param ctx      MD5 context
203  * \param output   MD5 checksum result
204  *
205  * \warning        MD5 is considered a weak message digest and its use
206  *                 constitutes a security risk. We recommend considering
207  *                 stronger message digests instead.
208  *
209  */
210 MBEDTLS_DEPRECATED void mbedtls_md5_finish(mbedtls_md5_context *ctx,
211                                            unsigned char output[16]);
212 
213 /**
214  * \brief          MD5 process data block (internal use only)
215  *
216  * \deprecated     Superseded by mbedtls_internal_md5_process() in 2.7.0
217  *
218  * \param ctx      MD5 context
219  * \param data     buffer holding one block of data
220  *
221  * \warning        MD5 is considered a weak message digest and its use
222  *                 constitutes a security risk. We recommend considering
223  *                 stronger message digests instead.
224  *
225  */
226 MBEDTLS_DEPRECATED void mbedtls_md5_process(mbedtls_md5_context *ctx,
227                                             const unsigned char data[64]);
228 
229 #undef MBEDTLS_DEPRECATED
230 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
231 
232 /**
233  * \brief          Output = MD5( input buffer )
234  *
235  * \param input    buffer holding the data
236  * \param ilen     length of the input data
237  * \param output   MD5 checksum result
238  *
239  * \return         0 if successful
240  *
241  * \warning        MD5 is considered a weak message digest and its use
242  *                 constitutes a security risk. We recommend considering
243  *                 stronger message digests instead.
244  *
245  */
246 int mbedtls_md5_ret(const unsigned char *input,
247                     size_t ilen,
248                     unsigned char output[16]);
249 
250 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
251 #if defined(MBEDTLS_DEPRECATED_WARNING)
252 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
253 #else
254 #define MBEDTLS_DEPRECATED
255 #endif
256 /**
257  * \brief          Output = MD5( input buffer )
258  *
259  * \deprecated     Superseded by mbedtls_md5_ret() in 2.7.0
260  *
261  * \param input    buffer holding the data
262  * \param ilen     length of the input data
263  * \param output   MD5 checksum result
264  *
265  * \warning        MD5 is considered a weak message digest and its use
266  *                 constitutes a security risk. We recommend considering
267  *                 stronger message digests instead.
268  *
269  */
270 MBEDTLS_DEPRECATED void mbedtls_md5(const unsigned char *input,
271                                     size_t ilen,
272                                     unsigned char output[16]);
273 
274 #undef MBEDTLS_DEPRECATED
275 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
276 
277 #if defined(MBEDTLS_SELF_TEST)
278 
279 /**
280  * \brief          Checkup routine
281  *
282  * \return         0 if successful, or 1 if the test failed
283  *
284  * \warning        MD5 is considered a weak message digest and its use
285  *                 constitutes a security risk. We recommend considering
286  *                 stronger message digests instead.
287  *
288  */
289 int mbedtls_md5_self_test(int verbose);
290 
291 #endif /* MBEDTLS_SELF_TEST */
292 
293 #ifdef __cplusplus
294 }
295 #endif
296 
297 #endif /* mbedtls_md5.h */
298