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) |
| 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_MD5_H |
| 24 | #define MBEDTLS_MD5_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 27 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 32 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 33 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if !defined(MBEDTLS_MD5_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 36 | // Regular implementation |
| 37 | // |
| 38 | |
Gilles Peskine | 2840f94 | 2018-01-23 11:57:19 +0100 | [diff] [blame^] | 39 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 40 | !defined(inline) && !defined(__cplusplus) |
| 41 | #define inline __inline |
| 42 | #endif |
| 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | /** |
| 49 | * \brief MD5 context structure |
| 50 | */ |
| 51 | typedef struct |
| 52 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 53 | uint32_t total[2]; /*!< number of bytes processed */ |
| 54 | uint32_t state[4]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | unsigned char buffer[64]; /*!< data block being processed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | mbedtls_md5_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 60 | * \brief Initialize MD5 context |
| 61 | * |
| 62 | * \param ctx MD5 context to be initialized |
| 63 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | void mbedtls_md5_init( mbedtls_md5_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * \brief Clear MD5 context |
| 68 | * |
| 69 | * \param ctx MD5 context to be cleared |
| 70 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | void mbedtls_md5_free( mbedtls_md5_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 74 | * \brief Clone (the state of) an MD5 context |
| 75 | * |
| 76 | * \param dst The destination context |
| 77 | * \param src The context to be cloned |
| 78 | */ |
| 79 | void mbedtls_md5_clone( mbedtls_md5_context *dst, |
| 80 | const mbedtls_md5_context *src ); |
| 81 | |
| 82 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | * \brief MD5 context setup |
| 84 | * |
| 85 | * \param ctx context to be initialized |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 86 | * |
| 87 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 89 | int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * \brief MD5 process buffer |
| 93 | * |
| 94 | * \param ctx MD5 context |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 95 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | * \param ilen length of the input data |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 97 | * |
| 98 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 100 | int mbedtls_md5_update_ret( mbedtls_md5_context *ctx, |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 101 | const unsigned char *input, |
| 102 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * \brief MD5 final digest |
| 106 | * |
| 107 | * \param ctx MD5 context |
| 108 | * \param output MD5 checksum result |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 109 | * |
| 110 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 112 | int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx, |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 113 | unsigned char output[16] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 115 | /** |
| 116 | * \brief MD5 process data block (internal use only) |
| 117 | * |
| 118 | * \param ctx MD5 context |
| 119 | * \param data buffer holding one block of data |
| 120 | * |
| 121 | * \return 0 if successful |
| 122 | */ |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 123 | int mbedtls_internal_md5_process( mbedtls_md5_context *ctx, |
| 124 | const unsigned char data[64] ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 125 | |
| 126 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 127 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 128 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 129 | #else |
| 130 | #define MBEDTLS_DEPRECATED |
| 131 | #endif |
| 132 | /** |
| 133 | * \brief MD5 context setup |
| 134 | * |
Gilles Peskine | 3e28d70 | 2018-01-22 12:18:59 +0100 | [diff] [blame] | 135 | * \deprecated Superseded by mbedtls_md5_starts_ret() in 2.7.0 |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 136 | * |
| 137 | * \param ctx context to be initialized |
| 138 | */ |
| 139 | MBEDTLS_DEPRECATED static inline void mbedtls_md5_starts( |
| 140 | mbedtls_md5_context *ctx ) |
| 141 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 142 | mbedtls_md5_starts_ret( ctx ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * \brief MD5 process buffer |
| 147 | * |
Gilles Peskine | 3e28d70 | 2018-01-22 12:18:59 +0100 | [diff] [blame] | 148 | * \deprecated Superseded by mbedtls_md5_update_ret() in 2.7.0 |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 149 | * |
| 150 | * \param ctx MD5 context |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 151 | * \param input buffer holding the data |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 152 | * \param ilen length of the input data |
| 153 | */ |
| 154 | MBEDTLS_DEPRECATED static inline void mbedtls_md5_update( |
| 155 | mbedtls_md5_context *ctx, |
| 156 | const unsigned char *input, |
| 157 | size_t ilen ) |
| 158 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 159 | mbedtls_md5_update_ret( ctx, input, ilen ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * \brief MD5 final digest |
| 164 | * |
Gilles Peskine | 3e28d70 | 2018-01-22 12:18:59 +0100 | [diff] [blame] | 165 | * \deprecated Superseded by mbedtls_md5_finish_ret() in 2.7.0 |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 166 | * |
| 167 | * \param ctx MD5 context |
| 168 | * \param output MD5 checksum result |
| 169 | */ |
| 170 | MBEDTLS_DEPRECATED static inline void mbedtls_md5_finish( |
| 171 | mbedtls_md5_context *ctx, |
| 172 | unsigned char output[16] ) |
| 173 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 174 | mbedtls_md5_finish_ret( ctx, output ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * \brief MD5 process data block (internal use only) |
| 179 | * |
Gilles Peskine | 3e28d70 | 2018-01-22 12:18:59 +0100 | [diff] [blame] | 180 | * \deprecated Superseded by mbedtls_internal_md5_process() in 2.7.0 |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 181 | * |
| 182 | * \param ctx MD5 context |
| 183 | * \param data buffer holding one block of data |
| 184 | */ |
| 185 | MBEDTLS_DEPRECATED static inline void mbedtls_md5_process( |
| 186 | mbedtls_md5_context *ctx, |
| 187 | const unsigned char data[64] ) |
| 188 | { |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 189 | mbedtls_internal_md5_process( ctx, data ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | #undef MBEDTLS_DEPRECATED |
| 193 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 194 | |
| 195 | #ifdef __cplusplus |
| 196 | } |
| 197 | #endif |
| 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #else /* MBEDTLS_MD5_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 200 | #include "md5_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #endif /* MBEDTLS_MD5_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 202 | |
| 203 | #ifdef __cplusplus |
| 204 | extern "C" { |
| 205 | #endif |
| 206 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 207 | /** |
| 208 | * \brief Output = MD5( input buffer ) |
| 209 | * |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 210 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | * \param ilen length of the input data |
| 212 | * \param output MD5 checksum result |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 213 | * |
| 214 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 216 | int mbedtls_md5_ret( const unsigned char *input, |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 217 | size_t ilen, |
| 218 | unsigned char output[16] ); |
| 219 | |
| 220 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 221 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 222 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 223 | #else |
| 224 | #define MBEDTLS_DEPRECATED |
| 225 | #endif |
| 226 | /** |
| 227 | * \brief Output = MD5( input buffer ) |
| 228 | * |
Gilles Peskine | 3e28d70 | 2018-01-22 12:18:59 +0100 | [diff] [blame] | 229 | * \deprecated Superseded by mbedtls_md5_ret() in 2.7.0 |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 230 | * |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 231 | * \param input buffer holding the data |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 232 | * \param ilen length of the input data |
| 233 | * \param output MD5 checksum result |
| 234 | */ |
| 235 | MBEDTLS_DEPRECATED static inline void mbedtls_md5( const unsigned char *input, |
| 236 | size_t ilen, |
| 237 | unsigned char output[16] ) |
| 238 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 239 | mbedtls_md5_ret( input, ilen, output ); |
Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | #undef MBEDTLS_DEPRECATED |
| 243 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | |
| 245 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 246 | * \brief Checkup routine |
| 247 | * |
| 248 | * \return 0 if successful, or 1 if the test failed |
| 249 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | int mbedtls_md5_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | #ifdef __cplusplus |
| 253 | } |
| 254 | #endif |
| 255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | #endif /* mbedtls_md5.h */ |