blob: fdc530a16b30beb579611b439b7e1a7d275176ca [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
Dave Rodgman7ff79652023-11-03 12:04:52 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#ifndef MBEDTLS_MD5_H
15#define MBEDTLS_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010018#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020019#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020021#endif
Paul Bakker90995b52013-06-24 19:20:35 +020022
Rich Evans00ab4702015-02-06 13:43:58 +000023#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020024#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000025
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020026/* MBEDTLS_ERR_MD5_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020027/** MD5 hardware accelerator failed */
28#define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED -0x002F
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020029
Paul Bakker407a0da2013-06-27 14:29:21 +020030#ifdef __cplusplus
31extern "C" {
32#endif
33
Ron Eldorb2aacec2017-05-18 16:53:08 +030034#if !defined(MBEDTLS_MD5_ALT)
35// Regular implementation
36//
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038/**
39 * \brief MD5 context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010040 *
41 * \warning MD5 is considered a weak message digest and its use
42 * constitutes a security risk. We recommend considering
43 * stronger message digests instead.
44 *
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046typedef struct mbedtls_md5_context {
Paul Bakker5c2364c2012-10-01 14:41:15 +000047 uint32_t total[2]; /*!< number of bytes processed */
48 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000049 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Ron Eldorb2aacec2017-05-18 16:53:08 +030053#else /* MBEDTLS_MD5_ALT */
54#include "md5_alt.h"
55#endif /* MBEDTLS_MD5_ALT */
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
Paul Bakker5b4af392014-06-26 12:09:34 +020058 * \brief Initialize MD5 context
59 *
60 * \param ctx MD5 context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010061 *
62 * \warning MD5 is considered a weak message digest and its use
63 * constitutes a security risk. We recommend considering
64 * stronger message digests instead.
65 *
Paul Bakker5b4af392014-06-26 12:09:34 +020066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067void mbedtls_md5_init(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020068
69/**
70 * \brief Clear MD5 context
71 *
72 * \param ctx MD5 context to be cleared
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_free(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020080
81/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020082 * \brief Clone (the state of) an MD5 context
83 *
84 * \param dst The destination context
85 * \param src The context to be cloned
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 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020091 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092void mbedtls_md5_clone(mbedtls_md5_context *dst,
93 const mbedtls_md5_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020094
95/**
Paul Bakker5121ce52009-01-03 21:22:43 +000096 * \brief MD5 context setup
97 *
98 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +010099 *
100 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100101 *
102 * \warning MD5 is considered a weak message digest and its use
103 * constitutes a security risk. We recommend considering
104 * stronger message digests instead.
105 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
110 * \brief MD5 process buffer
111 *
112 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100113 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100115 *
116 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100117 *
118 * \warning MD5 is considered a weak message digest and its use
119 * constitutes a security risk. We recommend considering
120 * stronger message digests instead.
121 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
124 const unsigned char *input,
125 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
128 * \brief MD5 final digest
129 *
130 * \param ctx MD5 context
131 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100132 *
133 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100134 *
135 * \warning MD5 is considered a weak message digest and its use
136 * constitutes a security risk. We recommend considering
137 * stronger message digests instead.
138 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
141 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100143/**
144 * \brief MD5 process data block (internal use only)
145 *
146 * \param ctx MD5 context
147 * \param data buffer holding one block of data
148 *
149 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100150 *
151 * \warning MD5 is considered a weak message digest and its use
152 * constitutes a security risk. We recommend considering
153 * stronger message digests instead.
154 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100155 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
157 const unsigned char data[64]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100158
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200159#if !defined(MBEDTLS_DEPRECATED_REMOVED)
160#if defined(MBEDTLS_DEPRECATED_WARNING)
161#define MBEDTLS_DEPRECATED __attribute__((deprecated))
162#else
163#define MBEDTLS_DEPRECATED
164#endif
165/**
166 * \brief MD5 context setup
167 *
168 * \deprecated Superseded by mbedtls_md5_starts_ret() in 2.7.0
169 *
170 * \param ctx context to be initialized
171 *
172 * \warning MD5 is considered a weak message digest and its use
173 * constitutes a security risk. We recommend considering
174 * stronger message digests instead.
175 *
176 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177MBEDTLS_DEPRECATED void mbedtls_md5_starts(mbedtls_md5_context *ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200178
179/**
180 * \brief MD5 process buffer
181 *
182 * \deprecated Superseded by mbedtls_md5_update_ret() in 2.7.0
183 *
184 * \param ctx MD5 context
185 * \param input buffer holding the data
186 * \param ilen length of the input data
187 *
188 * \warning MD5 is considered a weak message digest and its use
189 * constitutes a security risk. We recommend considering
190 * stronger message digests instead.
191 *
192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193MBEDTLS_DEPRECATED void mbedtls_md5_update(mbedtls_md5_context *ctx,
194 const unsigned char *input,
195 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200196
197/**
198 * \brief MD5 final digest
199 *
200 * \deprecated Superseded by mbedtls_md5_finish_ret() in 2.7.0
201 *
202 * \param ctx MD5 context
203 * \param output MD5 checksum result
204 *
205 * \warning MD5 is considered a weak message digest and its use
206 * constitutes a security risk. We recommend considering
207 * stronger message digests instead.
208 *
209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210MBEDTLS_DEPRECATED void mbedtls_md5_finish(mbedtls_md5_context *ctx,
211 unsigned char output[16]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200212
213/**
214 * \brief MD5 process data block (internal use only)
215 *
216 * \deprecated Superseded by mbedtls_internal_md5_process() in 2.7.0
217 *
218 * \param ctx MD5 context
219 * \param data buffer holding one block of data
220 *
221 * \warning MD5 is considered a weak message digest and its use
222 * constitutes a security risk. We recommend considering
223 * stronger message digests instead.
224 *
225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226MBEDTLS_DEPRECATED void mbedtls_md5_process(mbedtls_md5_context *ctx,
227 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200228
229#undef MBEDTLS_DEPRECATED
230#endif /* !MBEDTLS_DEPRECATED_REMOVED */
231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232/**
233 * \brief Output = MD5( input buffer )
234 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100235 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 * \param ilen length of the input data
237 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100238 *
239 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100240 *
241 * \warning MD5 is considered a weak message digest and its use
242 * constitutes a security risk. We recommend considering
243 * stronger message digests instead.
244 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246int mbedtls_md5_ret(const unsigned char *input,
247 size_t ilen,
248 unsigned char output[16]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100249
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200250#if !defined(MBEDTLS_DEPRECATED_REMOVED)
251#if defined(MBEDTLS_DEPRECATED_WARNING)
252#define MBEDTLS_DEPRECATED __attribute__((deprecated))
253#else
254#define MBEDTLS_DEPRECATED
255#endif
256/**
257 * \brief Output = MD5( input buffer )
258 *
259 * \deprecated Superseded by mbedtls_md5_ret() in 2.7.0
260 *
261 * \param input buffer holding the data
262 * \param ilen length of the input data
263 * \param output MD5 checksum result
264 *
265 * \warning MD5 is considered a weak message digest and its use
266 * constitutes a security risk. We recommend considering
267 * stronger message digests instead.
268 *
269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270MBEDTLS_DEPRECATED void mbedtls_md5(const unsigned char *input,
271 size_t ilen,
272 unsigned char output[16]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200273
274#undef MBEDTLS_DEPRECATED
275#endif /* !MBEDTLS_DEPRECATED_REMOVED */
276
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277#if defined(MBEDTLS_SELF_TEST)
278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 * \brief Checkup routine
281 *
282 * \return 0 if successful, or 1 if the test failed
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100283 *
284 * \warning MD5 is considered a weak message digest and its use
285 * constitutes a security risk. We recommend considering
286 * stronger message digests instead.
287 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289int mbedtls_md5_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291#endif /* MBEDTLS_SELF_TEST */
292
Paul Bakker5121ce52009-01-03 21:22:43 +0000293#ifdef __cplusplus
294}
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* mbedtls_md5.h */