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  *  This file is provided under the Apache License 2.0, or the
15  *  GNU General Public License v2.0 or later.
16  *
17  *  **********
18  *  Apache License 2.0:
19  *
20  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
21  *  not use this file except in compliance with the License.
22  *  You may obtain a copy of the License at
23  *
24  *  http://www.apache.org/licenses/LICENSE-2.0
25  *
26  *  Unless required by applicable law or agreed to in writing, software
27  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  *  See the License for the specific language governing permissions and
30  *  limitations under the License.
31  *
32  *  **********
33  *
34  *  **********
35  *  GNU General Public License v2.0 or later:
36  *
37  *  This program is free software; you can redistribute it and/or modify
38  *  it under the terms of the GNU General Public License as published by
39  *  the Free Software Foundation; either version 2 of the License, or
40  *  (at your option) any later version.
41  *
42  *  This program is distributed in the hope that it will be useful,
43  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
44  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  *  GNU General Public License for more details.
46  *
47  *  You should have received a copy of the GNU General Public License along
48  *  with this program; if not, write to the Free Software Foundation, Inc.,
49  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50  *
51  *  **********
52  */
53 #ifndef MBEDTLS_MD5_H
54 #define MBEDTLS_MD5_H
55 
56 #if !defined(MBEDTLS_CONFIG_FILE)
57 #include "config.h"
58 #else
59 #include MBEDTLS_CONFIG_FILE
60 #endif
61 
62 #include <stddef.h>
63 #include <stdint.h>
64 
65 /* MBEDTLS_ERR_MD5_HW_ACCEL_FAILED is deprecated and should not be used. */
66 #define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED                   -0x002F  /**< MD5 hardware accelerator failed */
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 #if !defined(MBEDTLS_MD5_ALT)
73 // Regular implementation
74 //
75 
76 /**
77  * \brief          MD5 context structure
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 typedef struct mbedtls_md5_context
85 {
86     uint32_t total[2];          /*!< number of bytes processed  */
87     uint32_t state[4];          /*!< intermediate digest state  */
88     unsigned char buffer[64];   /*!< data block being processed */
89 }
90 mbedtls_md5_context;
91 
92 #else  /* MBEDTLS_MD5_ALT */
93 #include "md5_alt.h"
94 #endif /* MBEDTLS_MD5_ALT */
95 
96 /**
97  * \brief          Initialize MD5 context
98  *
99  * \param ctx      MD5 context to be initialized
100  *
101  * \warning        MD5 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 void mbedtls_md5_init( mbedtls_md5_context *ctx );
107 
108 /**
109  * \brief          Clear MD5 context
110  *
111  * \param ctx      MD5 context to be cleared
112  *
113  * \warning        MD5 is considered a weak message digest and its use
114  *                 constitutes a security risk. We recommend considering
115  *                 stronger message digests instead.
116  *
117  */
118 void mbedtls_md5_free( mbedtls_md5_context *ctx );
119 
120 /**
121  * \brief          Clone (the state of) an MD5 context
122  *
123  * \param dst      The destination context
124  * \param src      The context to be cloned
125  *
126  * \warning        MD5 is considered a weak message digest and its use
127  *                 constitutes a security risk. We recommend considering
128  *                 stronger message digests instead.
129  *
130  */
131 void mbedtls_md5_clone( mbedtls_md5_context *dst,
132                         const mbedtls_md5_context *src );
133 
134 /**
135  * \brief          MD5 context setup
136  *
137  * \param ctx      context to be initialized
138  *
139  * \return         0 if successful
140  *
141  * \warning        MD5 is considered a weak message digest and its use
142  *                 constitutes a security risk. We recommend considering
143  *                 stronger message digests instead.
144  *
145  */
146 int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx );
147 
148 /**
149  * \brief          MD5 process buffer
150  *
151  * \param ctx      MD5 context
152  * \param input    buffer holding the data
153  * \param ilen     length of the input data
154  *
155  * \return         0 if successful
156  *
157  * \warning        MD5 is considered a weak message digest and its use
158  *                 constitutes a security risk. We recommend considering
159  *                 stronger message digests instead.
160  *
161  */
162 int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
163                             const unsigned char *input,
164                             size_t ilen );
165 
166 /**
167  * \brief          MD5 final digest
168  *
169  * \param ctx      MD5 context
170  * \param output   MD5 checksum result
171  *
172  * \return         0 if successful
173  *
174  * \warning        MD5 is considered a weak message digest and its use
175  *                 constitutes a security risk. We recommend considering
176  *                 stronger message digests instead.
177  *
178  */
179 int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
180                             unsigned char output[16] );
181 
182 /**
183  * \brief          MD5 process data block (internal use only)
184  *
185  * \param ctx      MD5 context
186  * \param data     buffer holding one block of data
187  *
188  * \return         0 if successful
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 int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
196                                   const unsigned char data[64] );
197 
198 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
199 #if defined(MBEDTLS_DEPRECATED_WARNING)
200 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
201 #else
202 #define MBEDTLS_DEPRECATED
203 #endif
204 /**
205  * \brief          MD5 context setup
206  *
207  * \deprecated     Superseded by mbedtls_md5_starts_ret() in 2.7.0
208  *
209  * \param ctx      context to be initialized
210  *
211  * \warning        MD5 is considered a weak message digest and its use
212  *                 constitutes a security risk. We recommend considering
213  *                 stronger message digests instead.
214  *
215  */
216 MBEDTLS_DEPRECATED void mbedtls_md5_starts( mbedtls_md5_context *ctx );
217 
218 /**
219  * \brief          MD5 process buffer
220  *
221  * \deprecated     Superseded by mbedtls_md5_update_ret() in 2.7.0
222  *
223  * \param ctx      MD5 context
224  * \param input    buffer holding the data
225  * \param ilen     length of the input data
226  *
227  * \warning        MD5 is considered a weak message digest and its use
228  *                 constitutes a security risk. We recommend considering
229  *                 stronger message digests instead.
230  *
231  */
232 MBEDTLS_DEPRECATED void mbedtls_md5_update( mbedtls_md5_context *ctx,
233                                             const unsigned char *input,
234                                             size_t ilen );
235 
236 /**
237  * \brief          MD5 final digest
238  *
239  * \deprecated     Superseded by mbedtls_md5_finish_ret() in 2.7.0
240  *
241  * \param ctx      MD5 context
242  * \param output   MD5 checksum result
243  *
244  * \warning        MD5 is considered a weak message digest and its use
245  *                 constitutes a security risk. We recommend considering
246  *                 stronger message digests instead.
247  *
248  */
249 MBEDTLS_DEPRECATED void mbedtls_md5_finish( mbedtls_md5_context *ctx,
250                                             unsigned char output[16] );
251 
252 /**
253  * \brief          MD5 process data block (internal use only)
254  *
255  * \deprecated     Superseded by mbedtls_internal_md5_process() in 2.7.0
256  *
257  * \param ctx      MD5 context
258  * \param data     buffer holding one block of data
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 MBEDTLS_DEPRECATED void mbedtls_md5_process( mbedtls_md5_context *ctx,
266                                              const unsigned char data[64] );
267 
268 #undef MBEDTLS_DEPRECATED
269 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
270 
271 /**
272  * \brief          Output = MD5( input buffer )
273  *
274  * \param input    buffer holding the data
275  * \param ilen     length of the input data
276  * \param output   MD5 checksum result
277  *
278  * \return         0 if successful
279  *
280  * \warning        MD5 is considered a weak message digest and its use
281  *                 constitutes a security risk. We recommend considering
282  *                 stronger message digests instead.
283  *
284  */
285 int mbedtls_md5_ret( const unsigned char *input,
286                      size_t ilen,
287                      unsigned char output[16] );
288 
289 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
290 #if defined(MBEDTLS_DEPRECATED_WARNING)
291 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
292 #else
293 #define MBEDTLS_DEPRECATED
294 #endif
295 /**
296  * \brief          Output = MD5( input buffer )
297  *
298  * \deprecated     Superseded by mbedtls_md5_ret() in 2.7.0
299  *
300  * \param input    buffer holding the data
301  * \param ilen     length of the input data
302  * \param output   MD5 checksum result
303  *
304  * \warning        MD5 is considered a weak message digest and its use
305  *                 constitutes a security risk. We recommend considering
306  *                 stronger message digests instead.
307  *
308  */
309 MBEDTLS_DEPRECATED void mbedtls_md5( const unsigned char *input,
310                                      size_t ilen,
311                                      unsigned char output[16] );
312 
313 #undef MBEDTLS_DEPRECATED
314 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
315 
316 #if defined(MBEDTLS_SELF_TEST)
317 
318 /**
319  * \brief          Checkup routine
320  *
321  * \return         0 if successful, or 1 if the test failed
322  *
323  * \warning        MD5 is considered a weak message digest and its use
324  *                 constitutes a security risk. We recommend considering
325  *                 stronger message digests instead.
326  *
327  */
328 int mbedtls_md5_self_test( int verbose );
329 
330 #endif /* MBEDTLS_SELF_TEST */
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif /* mbedtls_md5.h */
337