blob: 4f8c92197fa278dd75b3143b773cd5a240522064 [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)
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * 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 Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_MD5_H
24#define MBEDTLS_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_MD5_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020036// Regular implementation
37//
38
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043/**
44 * \brief MD5 context structure
45 */
46typedef struct
47{
Paul Bakker5c2364c2012-10-01 14:41:15 +000048 uint32_t total[2]; /*!< number of bytes processed */
49 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000050 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakker5121ce52009-01-03 21:22:43 +000054/**
Paul Bakker5b4af392014-06-26 12:09:34 +020055 * \brief Initialize MD5 context
56 *
57 * \param ctx MD5 context to be initialized
58 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059void mbedtls_md5_init( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020060
61/**
62 * \brief Clear MD5 context
63 *
64 * \param ctx MD5 context to be cleared
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_md5_free( mbedtls_md5_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020067
68/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020069 * \brief Clone (the state of) an MD5 context
70 *
71 * \param dst The destination context
72 * \param src The context to be cloned
73 */
74void mbedtls_md5_clone( mbedtls_md5_context *dst,
75 const mbedtls_md5_context *src );
76
77/**
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * \brief MD5 context setup
79 *
80 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +010081 *
82 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010084int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86/**
87 * \brief MD5 process buffer
88 *
89 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +010090 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +000091 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +010092 *
93 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010095int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +010096 const unsigned char *input,
97 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief MD5 final digest
101 *
102 * \param ctx MD5 context
103 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100104 *
105 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100107int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100108 unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100110/**
111 * \brief MD5 process data block (internal use only)
112 *
113 * \param ctx MD5 context
114 * \param data buffer holding one block of data
115 *
116 * \return 0 if successful
117 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100118int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
119 const unsigned char data[64] );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100120
121#if !defined(MBEDTLS_DEPRECATED_REMOVED)
122#if defined(MBEDTLS_DEPRECATED_WARNING)
123#define MBEDTLS_DEPRECATED __attribute__((deprecated))
124#else
125#define MBEDTLS_DEPRECATED
126#endif
127/**
128 * \brief MD5 context setup
129 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100130 * \deprecated Superseded by mbedtls_md5_starts_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100131 *
132 * \param ctx context to be initialized
133 */
134MBEDTLS_DEPRECATED static inline void mbedtls_md5_starts(
135 mbedtls_md5_context *ctx )
136{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100137 mbedtls_md5_starts_ret( ctx );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100138}
139
140/**
141 * \brief MD5 process buffer
142 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100143 * \deprecated Superseded by mbedtls_md5_update_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100144 *
145 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100146 * \param input buffer holding the data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100147 * \param ilen length of the input data
148 */
149MBEDTLS_DEPRECATED static inline void mbedtls_md5_update(
150 mbedtls_md5_context *ctx,
151 const unsigned char *input,
152 size_t ilen )
153{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100154 mbedtls_md5_update_ret( ctx, input, ilen );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100155}
156
157/**
158 * \brief MD5 final digest
159 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100160 * \deprecated Superseded by mbedtls_md5_finish_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100161 *
162 * \param ctx MD5 context
163 * \param output MD5 checksum result
164 */
165MBEDTLS_DEPRECATED static inline void mbedtls_md5_finish(
166 mbedtls_md5_context *ctx,
167 unsigned char output[16] )
168{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100169 mbedtls_md5_finish_ret( ctx, output );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100170}
171
172/**
173 * \brief MD5 process data block (internal use only)
174 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100175 * \deprecated Superseded by mbedtls_internal_md5_process() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100176 *
177 * \param ctx MD5 context
178 * \param data buffer holding one block of data
179 */
180MBEDTLS_DEPRECATED static inline void mbedtls_md5_process(
181 mbedtls_md5_context *ctx,
182 const unsigned char data[64] )
183{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100184 mbedtls_internal_md5_process( ctx, data );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100185}
186
187#undef MBEDTLS_DEPRECATED
188#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200189
190#ifdef __cplusplus
191}
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#else /* MBEDTLS_MD5_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200195#include "md5_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_MD5_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200197
198#ifdef __cplusplus
199extern "C" {
200#endif
201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202/**
203 * \brief Output = MD5( input buffer )
204 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100205 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 * \param ilen length of the input data
207 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100208 *
209 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100211int mbedtls_md5_ret( const unsigned char *input,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100212 size_t ilen,
213 unsigned char output[16] );
214
215#if !defined(MBEDTLS_DEPRECATED_REMOVED)
216#if defined(MBEDTLS_DEPRECATED_WARNING)
217#define MBEDTLS_DEPRECATED __attribute__((deprecated))
218#else
219#define MBEDTLS_DEPRECATED
220#endif
221/**
222 * \brief Output = MD5( input buffer )
223 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100224 * \deprecated Superseded by mbedtls_md5_ret() in 2.7.0
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100225 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100226 * \param input buffer holding the data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100227 * \param ilen length of the input data
228 * \param output MD5 checksum result
229 */
230MBEDTLS_DEPRECATED static inline void mbedtls_md5( const unsigned char *input,
231 size_t ilen,
232 unsigned char output[16] )
233{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100234 mbedtls_md5_ret( input, ilen, output );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100235}
236
237#undef MBEDTLS_DEPRECATED
238#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 * \brief Checkup routine
242 *
243 * \return 0 if successful, or 1 if the test failed
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_md5_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247#ifdef __cplusplus
248}
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* mbedtls_md5.h */