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