blob: 06ea4c5d44e47a54cac7786f17d5c5e770245c92 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +020011 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000025 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000026 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_MD5_H
29#define MBEDTLS_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020032#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakker90995b52013-06-24 19:20:35 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020038#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000039
Gilles Peskinea381fe82018-01-23 18:16:11 +010040#define MBEDTLS_ERR_MD5_HW_ACCEL_FAILED -0x002F /**< MD5 hardware accelerator failed */
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_MD5_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
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 */
58typedef struct
59{
Paul Bakker5c2364c2012-10-01 14:41:15 +000060 uint32_t total[2]; /*!< number of bytes processed */
61 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000062 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
Paul Bakker5b4af392014-06-26 12:09:34 +020067 * \brief Initialize MD5 context
68 *
69 * \param ctx MD5 context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010070 *
71 * \warning MD5 is considered a weak message digest and its use
72 * constitutes a security risk. We recommend considering
73 * stronger message digests instead.
74 *
Paul Bakker5b4af392014-06-26 12:09:34 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_md5_init( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020077
78/**
79 * \brief Clear MD5 context
80 *
81 * \param ctx MD5 context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010082 *
83 * \warning MD5 is considered a weak message digest and its use
84 * constitutes a security risk. We recommend considering
85 * stronger message digests instead.
86 *
Paul Bakker5b4af392014-06-26 12:09:34 +020087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_md5_free( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020089
90/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020091 * \brief Clone (the state of) an MD5 context
92 *
93 * \param dst The destination context
94 * \param src The context to be cloned
Hanno Beckerbbca8c52017-09-25 14:53:51 +010095 *
96 * \warning MD5 is considered a weak message digest and its use
97 * constitutes a security risk. We recommend considering
98 * stronger message digests instead.
99 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200100 */
101void mbedtls_md5_clone( mbedtls_md5_context *dst,
102 const mbedtls_md5_context *src );
103
104/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * \brief MD5 context setup
106 *
107 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100108 *
109 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100110 *
111 * \warning MD5 is considered a weak message digest and its use
112 * constitutes a security risk. We recommend considering
113 * stronger message digests instead.
114 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100116int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
119 * \brief MD5 process buffer
120 *
121 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100122 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100124 *
125 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100126 *
127 * \warning MD5 is considered a weak message digest and its use
128 * constitutes a security risk. We recommend considering
129 * stronger message digests instead.
130 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100132int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100133 const unsigned char *input,
134 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136/**
137 * \brief MD5 final digest
138 *
139 * \param ctx MD5 context
140 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100141 *
142 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100143 *
144 * \warning MD5 is considered a weak message digest and its use
145 * constitutes a security risk. We recommend considering
146 * stronger message digests instead.
147 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100149int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100150 unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100152/**
153 * \brief MD5 process data block (internal use only)
154 *
155 * \param ctx MD5 context
156 * \param data buffer holding one block of data
157 *
158 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100159 *
160 * \warning MD5 is considered a weak message digest and its use
161 * constitutes a security risk. We recommend considering
162 * stronger message digests instead.
163 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100164 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100165int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
166 const unsigned char data[64] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100167
168#if !defined(MBEDTLS_DEPRECATED_REMOVED)
169#if defined(MBEDTLS_DEPRECATED_WARNING)
170#define MBEDTLS_DEPRECATED __attribute__((deprecated))
171#else
172#define MBEDTLS_DEPRECATED
173#endif
174/**
175 * \brief MD5 context setup
176 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100177 * \deprecated Superseded by mbedtls_md5_starts_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100178 *
179 * \param ctx context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100180 *
181 * \warning MD5 is considered a weak message digest and its use
182 * constitutes a security risk. We recommend considering
183 * stronger message digests instead.
184 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100185 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000186MBEDTLS_DEPRECATED void mbedtls_md5_starts( mbedtls_md5_context *ctx );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100187
188/**
189 * \brief MD5 process buffer
190 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100191 * \deprecated Superseded by mbedtls_md5_update_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100192 *
193 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100194 * \param input buffer holding the data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100195 * \param ilen length of the input data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100196 *
197 * \warning MD5 is considered a weak message digest and its use
198 * constitutes a security risk. We recommend considering
199 * stronger message digests instead.
200 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100201 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000202MBEDTLS_DEPRECATED void mbedtls_md5_update( mbedtls_md5_context *ctx,
203 const unsigned char *input,
204 size_t ilen );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100205
206/**
207 * \brief MD5 final digest
208 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100209 * \deprecated Superseded by mbedtls_md5_finish_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100210 *
211 * \param ctx MD5 context
212 * \param output MD5 checksum result
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100213 *
214 * \warning MD5 is considered a weak message digest and its use
215 * constitutes a security risk. We recommend considering
216 * stronger message digests instead.
217 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100218 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000219MBEDTLS_DEPRECATED void mbedtls_md5_finish( mbedtls_md5_context *ctx,
220 unsigned char output[16] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100221
222/**
223 * \brief MD5 process data block (internal use only)
224 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100225 * \deprecated Superseded by mbedtls_internal_md5_process() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100226 *
227 * \param ctx MD5 context
228 * \param data buffer holding one block of data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100229 *
230 * \warning MD5 is considered a weak message digest and its use
231 * constitutes a security risk. We recommend considering
232 * stronger message digests instead.
233 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100234 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000235MBEDTLS_DEPRECATED void mbedtls_md5_process( mbedtls_md5_context *ctx,
236 const unsigned char data[64] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100237
238#undef MBEDTLS_DEPRECATED
239#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200240
241#ifdef __cplusplus
242}
243#endif
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#else /* MBEDTLS_MD5_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200246#include "md5_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_MD5_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200248
249#ifdef __cplusplus
250extern "C" {
251#endif
252
Paul Bakker5121ce52009-01-03 21:22:43 +0000253/**
254 * \brief Output = MD5( input buffer )
255 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100256 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 * \param ilen length of the input data
258 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100259 *
260 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100261 *
262 * \warning MD5 is considered a weak message digest and its use
263 * constitutes a security risk. We recommend considering
264 * stronger message digests instead.
265 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100267int mbedtls_md5_ret( const unsigned char *input,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100268 size_t ilen,
269 unsigned char output[16] );
270
271#if !defined(MBEDTLS_DEPRECATED_REMOVED)
272#if defined(MBEDTLS_DEPRECATED_WARNING)
273#define MBEDTLS_DEPRECATED __attribute__((deprecated))
274#else
275#define MBEDTLS_DEPRECATED
276#endif
277/**
278 * \brief Output = MD5( input buffer )
279 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100280 * \deprecated Superseded by mbedtls_md5_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100281 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100282 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 * \param ilen length of the input data
284 * \param output MD5 checksum result
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100285 *
286 * \warning MD5 is considered a weak message digest and its use
287 * constitutes a security risk. We recommend considering
288 * stronger message digests instead.
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000291MBEDTLS_DEPRECATED void mbedtls_md5( const unsigned char *input,
292 size_t ilen,
293 unsigned char output[16] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100294
295#undef MBEDTLS_DEPRECATED
296#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 * \brief Checkup routine
300 *
301 * \return 0 if successful, or 1 if the test failed
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100302 *
303 * \warning MD5 is considered a weak message digest and its use
304 * constitutes a security risk. We recommend considering
305 * stronger message digests instead.
306 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_md5_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Paul Bakker5121ce52009-01-03 21:22:43 +0000310#ifdef __cplusplus
311}
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* mbedtls_md5.h */