blob: cf0459d06be52ea933db38931a8bc920d6438681 [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker5121ce52009-01-03 21:22:43 +000032/**
33 * \brief MD5 context structure
34 */
35typedef struct
36{
37 unsigned long total[2]; /*!< number of bytes processed */
38 unsigned long state[4]; /*!< intermediate digest state */
39 unsigned char buffer[64]; /*!< data block being processed */
40
41 unsigned char ipad[64]; /*!< HMAC: inner padding */
42 unsigned char opad[64]; /*!< HMAC: outer padding */
43}
44md5_context;
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/**
51 * \brief MD5 context setup
52 *
53 * \param ctx context to be initialized
54 */
55void md5_starts( md5_context *ctx );
56
57/**
58 * \brief MD5 process buffer
59 *
60 * \param ctx MD5 context
61 * \param input buffer holding the data
62 * \param ilen length of the input data
63 */
Paul Bakker23986e52011-04-24 08:57:21 +000064void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/**
67 * \brief MD5 final digest
68 *
69 * \param ctx MD5 context
70 * \param output MD5 checksum result
71 */
72void md5_finish( md5_context *ctx, unsigned char output[16] );
73
74/**
75 * \brief Output = MD5( input buffer )
76 *
77 * \param input buffer holding the data
78 * \param ilen length of the input data
79 * \param output MD5 checksum result
80 */
Paul Bakker23986e52011-04-24 08:57:21 +000081void md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/**
84 * \brief Output = MD5( file contents )
85 *
86 * \param path input file name
87 * \param output MD5 checksum result
88 *
89 * \return 0 if successful, 1 if fopen failed,
90 * or 2 if fread failed
91 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000092int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94/**
95 * \brief MD5 HMAC context setup
96 *
97 * \param ctx HMAC context to be initialized
98 * \param key HMAC secret key
99 * \param keylen length of the HMAC key
100 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101void md5_hmac_starts( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000102 const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104/**
105 * \brief MD5 HMAC process buffer
106 *
107 * \param ctx HMAC context
108 * \param input buffer holding the data
109 * \param ilen length of the input data
110 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111void md5_hmac_update( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000112 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief MD5 HMAC final digest
116 *
117 * \param ctx HMAC context
118 * \param output MD5 HMAC checksum result
119 */
120void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
121
122/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000123 * \brief MD5 HMAC context reset
124 *
125 * \param ctx HMAC context to be reset
126 */
127void md5_hmac_reset( md5_context *ctx );
128
129/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 * \brief Output = HMAC-MD5( hmac key, input buffer )
131 *
132 * \param key HMAC secret key
133 * \param keylen length of the HMAC key
134 * \param input buffer holding the data
135 * \param ilen length of the input data
136 * \param output HMAC-MD5 result
137 */
Paul Bakker23986e52011-04-24 08:57:21 +0000138void md5_hmac( const unsigned char *key, size_t keylen,
139 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 unsigned char output[16] );
141
142/**
143 * \brief Checkup routine
144 *
145 * \return 0 if successful, or 1 if the test failed
146 */
147int md5_self_test( int verbose );
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif /* md5.h */