blob: 04f71ee3f5aa061143ad7a5c99ce4b7da2091c7a [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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058typedef struct mbedtls_md5_context {
Paul Bakker5c2364c2012-10-01 14:41:15 +000059 uint32_t total[2]; /*!< number of bytes processed */
60 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000061 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Ron Eldorb2aacec2017-05-18 16:53:08 +030065#else /* MBEDTLS_MD5_ALT */
66#include "md5_alt.h"
67#endif /* MBEDTLS_MD5_ALT */
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/**
Paul Bakker5b4af392014-06-26 12:09:34 +020070 * \brief Initialize MD5 context
71 *
72 * \param ctx MD5 context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010073 *
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 *
Paul Bakker5b4af392014-06-26 12:09:34 +020078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079void mbedtls_md5_init(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020080
81/**
82 * \brief Clear MD5 context
83 *
84 * \param ctx MD5 context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010085 *
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 *
Paul Bakker5b4af392014-06-26 12:09:34 +020090 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091void mbedtls_md5_free(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020092
93/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020094 * \brief Clone (the state of) an MD5 context
95 *
96 * \param dst The destination context
97 * \param src The context to be cloned
Hanno Beckerbbca8c52017-09-25 14:53:51 +010098 *
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 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104void mbedtls_md5_clone(mbedtls_md5_context *dst,
105 const mbedtls_md5_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200106
107/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * \brief MD5 context setup
109 *
110 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100111 *
112 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100113 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief MD5 process buffer
123 *
124 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100125 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100127 *
128 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100129 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
136 const unsigned char *input,
137 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief MD5 final digest
141 *
142 * \param ctx MD5 context
143 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100144 *
145 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100146 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
153 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100155/**
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
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100162 *
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 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100167 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
169 const unsigned char data[64]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100170
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200171#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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189MBEDTLS_DEPRECATED void mbedtls_md5_starts(mbedtls_md5_context *ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200190
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205MBEDTLS_DEPRECATED void mbedtls_md5_update(mbedtls_md5_context *ctx,
206 const unsigned char *input,
207 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200208
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222MBEDTLS_DEPRECATED void mbedtls_md5_finish(mbedtls_md5_context *ctx,
223 unsigned char output[16]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200224
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238MBEDTLS_DEPRECATED void mbedtls_md5_process(mbedtls_md5_context *ctx,
239 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200240
241#undef MBEDTLS_DEPRECATED
242#endif /* !MBEDTLS_DEPRECATED_REMOVED */
243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244/**
245 * \brief Output = MD5( input buffer )
246 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100247 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 * \param ilen length of the input data
249 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100250 *
251 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100252 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258int mbedtls_md5_ret(const unsigned char *input,
259 size_t ilen,
260 unsigned char output[16]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100261
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200262#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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282MBEDTLS_DEPRECATED void mbedtls_md5(const unsigned char *input,
283 size_t ilen,
284 unsigned char output[16]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200285
286#undef MBEDTLS_DEPRECATED
287#endif /* !MBEDTLS_DEPRECATED_REMOVED */
288
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289#if defined(MBEDTLS_SELF_TEST)
290
Paul Bakker5121ce52009-01-03 21:22:43 +0000291/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * \brief Checkup routine
293 *
294 * \return 0 if successful, or 1 if the test failed
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100295 *
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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301int mbedtls_md5_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303#endif /* MBEDTLS_SELF_TEST */
304
Paul Bakker5121ce52009-01-03 21:22:43 +0000305#ifdef __cplusplus
306}
307#endif
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* mbedtls_md5.h */