blob: 73e4dd2c2a78fb661444da8705e3276326194d4d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md5.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief MD5 message digest algorithm (hash function)
Hanno Beckerbbca8c52017-09-25 14:53:51 +01005 *
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.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_MD5_H
27#define MBEDTLS_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Paul Bakker90995b52013-06-24 19:20:35 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020036#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000037
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020038/* MBEDTLS_ERR_MD5_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020039/** MD5 hardware accelerator failed */
40#define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED -0x002F
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020041
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
45
Ron Eldorb2aacec2017-05-18 16:53:08 +030046#if !defined(MBEDTLS_MD5_ALT)
47// Regular implementation
48//
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
51 * \brief MD5 context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010052 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
Dawid Drozd428cc522018-07-24 10:02:47 +020058typedef struct mbedtls_md5_context
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
Paul Bakker5c2364c2012-10-01 14:41:15 +000060 uint32_t total[2]; /*!< number of bytes processed */
61 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000062 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Ron Eldorb2aacec2017-05-18 16:53:08 +030066#else /* MBEDTLS_MD5_ALT */
67#include "md5_alt.h"
68#endif /* MBEDTLS_MD5_ALT */
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
Paul Bakker5b4af392014-06-26 12:09:34 +020071 * \brief Initialize MD5 context
72 *
73 * \param ctx MD5 context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010074 *
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 *
Paul Bakker5b4af392014-06-26 12:09:34 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_md5_init( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020081
82/**
83 * \brief Clear MD5 context
84 *
85 * \param ctx MD5 context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010086 *
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 *
Paul Bakker5b4af392014-06-26 12:09:34 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_md5_free( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020093
94/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095 * \brief Clone (the state of) an MD5 context
96 *
97 * \param dst The destination context
98 * \param src The context to be cloned
Hanno Beckerbbca8c52017-09-25 14:53:51 +010099 *
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 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200104 */
105void mbedtls_md5_clone( mbedtls_md5_context *dst,
106 const mbedtls_md5_context *src );
107
108/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 * \brief MD5 context setup
110 *
111 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100112 *
113 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100114 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100120int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122/**
123 * \brief MD5 process buffer
124 *
125 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100126 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100128 *
129 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100130 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100136int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100137 const unsigned char *input,
138 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief MD5 final digest
142 *
143 * \param ctx MD5 context
144 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100145 *
146 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100147 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100153int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100154 unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100156/**
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
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100163 *
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 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100168 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100169int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
170 const unsigned char data[64] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100171
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200172#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 */
190MBEDTLS_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 */
206MBEDTLS_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 */
223MBEDTLS_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 */
239MBEDTLS_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
Paul Bakker5121ce52009-01-03 21:22:43 +0000245/**
246 * \brief Output = MD5( input buffer )
247 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100248 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 * \param ilen length of the input data
250 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100251 *
252 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100253 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100259int mbedtls_md5_ret( const unsigned char *input,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100260 size_t ilen,
261 unsigned char output[16] );
262
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200263#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 */
283MBEDTLS_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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290#if defined(MBEDTLS_SELF_TEST)
291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 * \brief Checkup routine
294 *
295 * \return 0 if successful, or 1 if the test failed
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100296 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_md5_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500304#endif /* MBEDTLS_SELF_TEST */
305
Paul Bakker5121ce52009-01-03 21:22:43 +0000306#ifdef __cplusplus
307}
308#endif
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* mbedtls_md5.h */