blob: 87474a79d0d91cdfa6d8eb9652095880d788e73c [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 *
Paul Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_MD5_H
28#define POLARSSL_MD5_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker5c2364c2012-10-01 14:41:15 +000034#ifdef _MSC_VER
35#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
39#endif
40
Paul Bakker69e095c2011-12-10 21:55:01 +000041#define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */
42
Paul Bakker90995b52013-06-24 19:20:35 +020043#if !defined(POLARSSL_MD5_ALT)
44// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
52 * \brief MD5 context structure
53 */
54typedef struct
55{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint32_t total[2]; /*!< number of bytes processed */
57 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buffer[64]; /*!< data block being processed */
59
60 unsigned char ipad[64]; /*!< HMAC: inner padding */
61 unsigned char opad[64]; /*!< HMAC: outer padding */
62}
63md5_context;
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/**
66 * \brief MD5 context setup
67 *
68 * \param ctx context to be initialized
69 */
70void md5_starts( md5_context *ctx );
71
72/**
73 * \brief MD5 process buffer
74 *
75 * \param ctx MD5 context
76 * \param input buffer holding the data
77 * \param ilen length of the input data
78 */
Paul Bakker23986e52011-04-24 08:57:21 +000079void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/**
82 * \brief MD5 final digest
83 *
84 * \param ctx MD5 context
85 * \param output MD5 checksum result
86 */
87void md5_finish( md5_context *ctx, unsigned char output[16] );
88
Paul Bakker90995b52013-06-24 19:20:35 +020089/* Internal use */
90void md5_process( md5_context *ctx, const unsigned char data[64] );
91
92#ifdef __cplusplus
93}
94#endif
95
96#else /* POLARSSL_MD5_ALT */
97#include "md5_alt.h"
98#endif /* POLARSSL_MD5_ALT */
99
100#ifdef __cplusplus
101extern "C" {
102#endif
103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/**
105 * \brief Output = MD5( input buffer )
106 *
107 * \param input buffer holding the data
108 * \param ilen length of the input data
109 * \param output MD5 checksum result
110 */
Paul Bakker23986e52011-04-24 08:57:21 +0000111void md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
114 * \brief Output = MD5( file contents )
115 *
116 * \param path input file name
117 * \param output MD5 checksum result
118 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000119 * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123/**
124 * \brief MD5 HMAC context setup
125 *
126 * \param ctx HMAC context to be initialized
127 * \param key HMAC secret key
128 * \param keylen length of the HMAC key
129 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130void md5_hmac_starts( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000131 const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133/**
134 * \brief MD5 HMAC process buffer
135 *
136 * \param ctx HMAC context
137 * \param input buffer holding the data
138 * \param ilen length of the input data
139 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000140void md5_hmac_update( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000141 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
144 * \brief MD5 HMAC final digest
145 *
146 * \param ctx HMAC context
147 * \param output MD5 HMAC checksum result
148 */
149void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
150
151/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000152 * \brief MD5 HMAC context reset
153 *
154 * \param ctx HMAC context to be reset
155 */
156void md5_hmac_reset( md5_context *ctx );
157
158/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \brief Output = HMAC-MD5( hmac key, input buffer )
160 *
161 * \param key HMAC secret key
162 * \param keylen length of the HMAC key
163 * \param input buffer holding the data
164 * \param ilen length of the input data
165 * \param output HMAC-MD5 result
166 */
Paul Bakker23986e52011-04-24 08:57:21 +0000167void md5_hmac( const unsigned char *key, size_t keylen,
168 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 unsigned char output[16] );
170
171/**
172 * \brief Checkup routine
173 *
174 * \return 0 if successful, or 1 if the test failed
175 */
176int md5_self_test( int verbose );
177
Paul Bakker5121ce52009-01-03 21:22:43 +0000178#ifdef __cplusplus
179}
180#endif
181
182#endif /* md5.h */