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