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