blob: d5ce136a2b6005fd3d2947da3db7032526efbf51 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_MD5_H
25#define POLARSSL_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Paul Bakker69e095c2011-12-10 21:55:01 +000042#define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */
43
Paul Bakker90995b52013-06-24 19:20:35 +020044#if !defined(POLARSSL_MD5_ALT)
45// Regular implementation
46//
47
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
53 * \brief MD5 context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t total[2]; /*!< number of bytes processed */
58 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000059 unsigned char buffer[64]; /*!< data block being processed */
60
61 unsigned char ipad[64]; /*!< HMAC: inner padding */
62 unsigned char opad[64]; /*!< HMAC: outer padding */
63}
64md5_context;
65
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
70 */
71void md5_init( md5_context *ctx );
72
73/**
74 * \brief Clear MD5 context
75 *
76 * \param ctx MD5 context to be cleared
77 */
78void md5_free( md5_context *ctx );
79
80/**
Paul Bakker5121ce52009-01-03 21:22:43 +000081 * \brief MD5 context setup
82 *
83 * \param ctx context to be initialized
84 */
85void md5_starts( md5_context *ctx );
86
87/**
88 * \brief MD5 process buffer
89 *
90 * \param ctx MD5 context
91 * \param input buffer holding the data
92 * \param ilen length of the input data
93 */
Paul Bakker23986e52011-04-24 08:57:21 +000094void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/**
97 * \brief MD5 final digest
98 *
99 * \param ctx MD5 context
100 * \param output MD5 checksum result
101 */
102void md5_finish( md5_context *ctx, unsigned char output[16] );
103
Paul Bakker90995b52013-06-24 19:20:35 +0200104/* Internal use */
105void md5_process( md5_context *ctx, const unsigned char data[64] );
106
107#ifdef __cplusplus
108}
109#endif
110
111#else /* POLARSSL_MD5_ALT */
112#include "md5_alt.h"
113#endif /* POLARSSL_MD5_ALT */
114
115#ifdef __cplusplus
116extern "C" {
117#endif
118
Paul Bakker5121ce52009-01-03 21:22:43 +0000119/**
120 * \brief Output = MD5( input buffer )
121 *
122 * \param input buffer holding the data
123 * \param ilen length of the input data
124 * \param output MD5 checksum result
125 */
Paul Bakker23986e52011-04-24 08:57:21 +0000126void md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128/**
129 * \brief Output = MD5( file contents )
130 *
131 * \param path input file name
132 * \param output MD5 checksum result
133 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000134 * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief MD5 HMAC context setup
140 *
141 * \param ctx HMAC context to be initialized
142 * \param key HMAC secret key
143 * \param keylen length of the HMAC key
144 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000145void md5_hmac_starts( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000146 const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148/**
149 * \brief MD5 HMAC process buffer
150 *
151 * \param ctx HMAC context
152 * \param input buffer holding the data
153 * \param ilen length of the input data
154 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155void md5_hmac_update( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000156 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief MD5 HMAC final digest
160 *
161 * \param ctx HMAC context
162 * \param output MD5 HMAC checksum result
163 */
164void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
165
166/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000167 * \brief MD5 HMAC context reset
168 *
169 * \param ctx HMAC context to be reset
170 */
171void md5_hmac_reset( md5_context *ctx );
172
173/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 * \brief Output = HMAC-MD5( hmac key, input buffer )
175 *
176 * \param key HMAC secret key
177 * \param keylen length of the HMAC key
178 * \param input buffer holding the data
179 * \param ilen length of the input data
180 * \param output HMAC-MD5 result
181 */
Paul Bakker23986e52011-04-24 08:57:21 +0000182void md5_hmac( const unsigned char *key, size_t keylen,
183 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 unsigned char output[16] );
185
186/**
187 * \brief Checkup routine
188 *
189 * \return 0 if successful, or 1 if the test failed
190 */
191int md5_self_test( int verbose );
192
Paul Bakker5121ce52009-01-03 21:22:43 +0000193#ifdef __cplusplus
194}
195#endif
196
197#endif /* md5.h */