Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 2 | * \file md5.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief MD5 message digest algorithm (hash function) |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 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. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 9 | */ |
| 10 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 11 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 12 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 13 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #ifndef MBEDTLS_MD5_H |
| 15 | #define MBEDTLS_MD5_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 16 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 18 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 19 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 21 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 22 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 23 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 26 | /* MBEDTLS_ERR_MD5_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 27 | /** MD5 hardware accelerator failed */ |
| 28 | #define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED -0x002F |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 29 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 34 | #if !defined(MBEDTLS_MD5_ALT) |
| 35 | // Regular implementation |
| 36 | // |
| 37 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | /** |
| 39 | * \brief MD5 context structure |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 40 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 46 | typedef struct mbedtls_md5_context { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 47 | uint32_t total[2]; /*!< number of bytes processed */ |
| 48 | uint32_t state[4]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | unsigned char buffer[64]; /*!< data block being processed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | mbedtls_md5_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 53 | #else /* MBEDTLS_MD5_ALT */ |
| 54 | #include "md5_alt.h" |
| 55 | #endif /* MBEDTLS_MD5_ALT */ |
| 56 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 58 | * \brief Initialize MD5 context |
| 59 | * |
| 60 | * \param ctx MD5 context to be initialized |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 61 | * |
| 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 Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 66 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | void mbedtls_md5_init(mbedtls_md5_context *ctx); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * \brief Clear MD5 context |
| 71 | * |
| 72 | * \param ctx MD5 context to be cleared |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 73 | * |
| 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 Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 78 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | void mbedtls_md5_free(mbedtls_md5_context *ctx); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 82 | * \brief Clone (the state of) an MD5 context |
| 83 | * |
| 84 | * \param dst The destination context |
| 85 | * \param src The context to be cloned |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 86 | * |
| 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é-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 91 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | void mbedtls_md5_clone(mbedtls_md5_context *dst, |
| 93 | const mbedtls_md5_context *src); |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | * \brief MD5 context setup |
| 97 | * |
| 98 | * \param ctx context to be initialized |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 99 | * |
| 100 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 101 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * \brief MD5 process buffer |
| 111 | * |
| 112 | * \param ctx MD5 context |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 113 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | * \param ilen length of the input data |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 115 | * |
| 116 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 117 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | int mbedtls_md5_update_ret(mbedtls_md5_context *ctx, |
| 124 | const unsigned char *input, |
| 125 | size_t ilen); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * \brief MD5 final digest |
| 129 | * |
| 130 | * \param ctx MD5 context |
| 131 | * \param output MD5 checksum result |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 132 | * |
| 133 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 134 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx, |
| 141 | unsigned char output[16]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 143 | /** |
| 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 Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 150 | * |
| 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 Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 155 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | int mbedtls_internal_md5_process(mbedtls_md5_context *ctx, |
| 157 | const unsigned char data[64]); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 159 | #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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | MBEDTLS_DEPRECATED void mbedtls_md5_starts(mbedtls_md5_context *ctx); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 178 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | MBEDTLS_DEPRECATED void mbedtls_md5_update(mbedtls_md5_context *ctx, |
| 194 | const unsigned char *input, |
| 195 | size_t ilen); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 196 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | MBEDTLS_DEPRECATED void mbedtls_md5_finish(mbedtls_md5_context *ctx, |
| 211 | unsigned char output[16]); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 212 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | MBEDTLS_DEPRECATED void mbedtls_md5_process(mbedtls_md5_context *ctx, |
| 227 | const unsigned char data[64]); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 228 | |
| 229 | #undef MBEDTLS_DEPRECATED |
| 230 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 231 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | /** |
| 233 | * \brief Output = MD5( input buffer ) |
| 234 | * |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 235 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 | * \param ilen length of the input data |
| 237 | * \param output MD5 checksum result |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 238 | * |
| 239 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 240 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | int mbedtls_md5_ret(const unsigned char *input, |
| 247 | size_t ilen, |
| 248 | unsigned char output[16]); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 250 | #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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | MBEDTLS_DEPRECATED void mbedtls_md5(const unsigned char *input, |
| 271 | size_t ilen, |
| 272 | unsigned char output[16]); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 273 | |
| 274 | #undef MBEDTLS_DEPRECATED |
| 275 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 276 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 277 | #if defined(MBEDTLS_SELF_TEST) |
| 278 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 280 | * \brief Checkup routine |
| 281 | * |
| 282 | * \return 0 if successful, or 1 if the test failed |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 283 | * |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | int mbedtls_md5_self_test(int verbose); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 291 | #endif /* MBEDTLS_SELF_TEST */ |
| 292 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | #ifdef __cplusplus |
| 294 | } |
| 295 | #endif |
| 296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | #endif /* mbedtls_md5.h */ |