| 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 | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | *  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 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 | #ifndef MBEDTLS_MD5_H | 
|  | 27 | #define MBEDTLS_MD5_H | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 28 | #include "mbedtls/private_access.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 30 | #include "mbedtls/build_info.h" | 
| 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 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 35 | #ifdef __cplusplus | 
|  | 36 | extern "C" { | 
|  | 37 | #endif | 
|  | 38 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 39 | #if !defined(MBEDTLS_MD5_ALT) | 
|  | 40 | // Regular implementation | 
|  | 41 | // | 
|  | 42 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | /** | 
|  | 44 | * \brief          MD5 context structure | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 45 | * | 
|  | 46 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 47 | *                 constitutes a security risk. We recommend considering | 
|  | 48 | *                 stronger message digests instead. | 
|  | 49 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | typedef struct mbedtls_md5_context { | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 52 | uint32_t MBEDTLS_PRIVATE(total)[2];          /*!< number of bytes processed  */ | 
|  | 53 | uint32_t MBEDTLS_PRIVATE(state)[4];          /*!< intermediate digest state  */ | 
|  | 54 | unsigned char MBEDTLS_PRIVATE(buffer)[64];   /*!< data block being processed */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | mbedtls_md5_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 58 | #else  /* MBEDTLS_MD5_ALT */ | 
|  | 59 | #include "md5_alt.h" | 
|  | 60 | #endif /* MBEDTLS_MD5_ALT */ | 
|  | 61 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | /** | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 63 | * \brief          Initialize MD5 context | 
|  | 64 | * | 
|  | 65 | * \param ctx      MD5 context to be initialized | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 66 | * | 
|  | 67 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 68 | *                 constitutes a security risk. We recommend considering | 
|  | 69 | *                 stronger message digests instead. | 
|  | 70 | * | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 71 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | void mbedtls_md5_init(mbedtls_md5_context *ctx); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 73 |  | 
|  | 74 | /** | 
|  | 75 | * \brief          Clear MD5 context | 
|  | 76 | * | 
|  | 77 | * \param ctx      MD5 context to be cleared | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 78 | * | 
|  | 79 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 80 | *                 constitutes a security risk. We recommend considering | 
|  | 81 | *                 stronger message digests instead. | 
|  | 82 | * | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 83 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | void mbedtls_md5_free(mbedtls_md5_context *ctx); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 85 |  | 
|  | 86 | /** | 
| Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 87 | * \brief          Clone (the state of) an MD5 context | 
|  | 88 | * | 
|  | 89 | * \param dst      The destination context | 
|  | 90 | * \param src      The context to be cloned | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 91 | * | 
|  | 92 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 93 | *                 constitutes a security risk. We recommend considering | 
|  | 94 | *                 stronger message digests instead. | 
|  | 95 | * | 
| Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 96 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | void mbedtls_md5_clone(mbedtls_md5_context *dst, | 
|  | 98 | const mbedtls_md5_context *src); | 
| Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 99 |  | 
|  | 100 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | * \brief          MD5 context setup | 
|  | 102 | * | 
|  | 103 | * \param ctx      context to be initialized | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 104 | * | 
|  | 105 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 106 | * | 
|  | 107 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 108 | *                 constitutes a security risk. We recommend considering | 
|  | 109 | *                 stronger message digests instead. | 
|  | 110 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | int mbedtls_md5_starts(mbedtls_md5_context *ctx); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 |  | 
|  | 114 | /** | 
|  | 115 | * \brief          MD5 process buffer | 
|  | 116 | * | 
|  | 117 | * \param ctx      MD5 context | 
| Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 118 | * \param input    buffer holding the data | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | * \param ilen     length of the input data | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 120 | * | 
|  | 121 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 122 | * | 
|  | 123 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 124 | *                 constitutes a security risk. We recommend considering | 
|  | 125 | *                 stronger message digests instead. | 
|  | 126 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | int mbedtls_md5_update(mbedtls_md5_context *ctx, | 
|  | 129 | const unsigned char *input, | 
|  | 130 | size_t ilen); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 |  | 
|  | 132 | /** | 
|  | 133 | * \brief          MD5 final digest | 
|  | 134 | * | 
|  | 135 | * \param ctx      MD5 context | 
|  | 136 | * \param output   MD5 checksum result | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 137 | * | 
|  | 138 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 139 | * | 
|  | 140 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 141 | *                 constitutes a security risk. We recommend considering | 
|  | 142 | *                 stronger message digests instead. | 
|  | 143 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | int mbedtls_md5_finish(mbedtls_md5_context *ctx, | 
|  | 146 | unsigned char output[16]); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 |  | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 148 | /** | 
|  | 149 | * \brief          MD5 process data block (internal use only) | 
|  | 150 | * | 
|  | 151 | * \param ctx      MD5 context | 
|  | 152 | * \param data     buffer holding one block of data | 
|  | 153 | * | 
|  | 154 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 155 | * | 
|  | 156 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 157 | *                 constitutes a security risk. We recommend considering | 
|  | 158 | *                 stronger message digests instead. | 
|  | 159 | * | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 160 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | int mbedtls_internal_md5_process(mbedtls_md5_context *ctx, | 
|  | 162 | const unsigned char data[64]); | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 163 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | /** | 
|  | 165 | * \brief          Output = MD5( input buffer ) | 
|  | 166 | * | 
| Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 167 | * \param input    buffer holding the data | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | * \param ilen     length of the input data | 
|  | 169 | * \param output   MD5 checksum result | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 170 | * | 
|  | 171 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 172 | * | 
|  | 173 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 174 | *                 constitutes a security risk. We recommend considering | 
|  | 175 | *                 stronger message digests instead. | 
|  | 176 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | int mbedtls_md5(const unsigned char *input, | 
|  | 179 | size_t ilen, | 
|  | 180 | unsigned char output[16]); | 
| Andres Amaya Garcia | 2cfd7a9 | 2017-05-02 10:19:27 +0100 | [diff] [blame] | 181 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 182 | #if defined(MBEDTLS_SELF_TEST) | 
|  | 183 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | * \brief          Checkup routine | 
|  | 186 | * | 
|  | 187 | * \return         0 if successful, or 1 if the test failed | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 188 | * | 
|  | 189 | * \warning        MD5 is considered a weak message digest and its use | 
|  | 190 | *                 constitutes a security risk. We recommend considering | 
|  | 191 | *                 stronger message digests instead. | 
|  | 192 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | int mbedtls_md5_self_test(int verbose); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 196 | #endif /* MBEDTLS_SELF_TEST */ | 
|  | 197 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | #ifdef __cplusplus | 
|  | 199 | } | 
|  | 200 | #endif | 
|  | 201 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #endif /* mbedtls_md5.h */ |