blob: e728c38cab246321c60d99dd41280980183b5fb8 [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
30/**
31 * \brief MD5 context structure
32 */
33typedef struct
34{
35 unsigned long total[2]; /*!< number of bytes processed */
36 unsigned long state[4]; /*!< intermediate digest state */
37 unsigned char buffer[64]; /*!< data block being processed */
38
39 unsigned char ipad[64]; /*!< HMAC: inner padding */
40 unsigned char opad[64]; /*!< HMAC: outer padding */
41}
42md5_context;
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/**
49 * \brief MD5 context setup
50 *
51 * \param ctx context to be initialized
52 */
53void md5_starts( md5_context *ctx );
54
55/**
56 * \brief MD5 process buffer
57 *
58 * \param ctx MD5 context
59 * \param input buffer holding the data
60 * \param ilen length of the input data
61 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000062void md5_update( md5_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/**
65 * \brief MD5 final digest
66 *
67 * \param ctx MD5 context
68 * \param output MD5 checksum result
69 */
70void md5_finish( md5_context *ctx, unsigned char output[16] );
71
72/**
73 * \brief Output = MD5( input buffer )
74 *
75 * \param input buffer holding the data
76 * \param ilen length of the input data
77 * \param output MD5 checksum result
78 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000079void md5( const unsigned char *input, int ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/**
82 * \brief Output = MD5( file contents )
83 *
84 * \param path input file name
85 * \param output MD5 checksum result
86 *
87 * \return 0 if successful, 1 if fopen failed,
88 * or 2 if fread failed
89 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000090int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92/**
93 * \brief MD5 HMAC context setup
94 *
95 * \param ctx HMAC context to be initialized
96 * \param key HMAC secret key
97 * \param keylen length of the HMAC key
98 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000099void md5_hmac_starts( md5_context *ctx,
100 const unsigned char *key, int keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/**
103 * \brief MD5 HMAC process buffer
104 *
105 * \param ctx HMAC context
106 * \param input buffer holding the data
107 * \param ilen length of the input data
108 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000109void md5_hmac_update( md5_context *ctx,
110 const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112/**
113 * \brief MD5 HMAC final digest
114 *
115 * \param ctx HMAC context
116 * \param output MD5 HMAC checksum result
117 */
118void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
119
120/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000121 * \brief MD5 HMAC context reset
122 *
123 * \param ctx HMAC context to be reset
124 */
125void md5_hmac_reset( md5_context *ctx );
126
127/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 * \brief Output = HMAC-MD5( hmac key, input buffer )
129 *
130 * \param key HMAC secret key
131 * \param keylen length of the HMAC key
132 * \param input buffer holding the data
133 * \param ilen length of the input data
134 * \param output HMAC-MD5 result
135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136void md5_hmac( const unsigned char *key, int keylen,
137 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 unsigned char output[16] );
139
140/**
141 * \brief Checkup routine
142 *
143 * \return 0 if successful, or 1 if the test failed
144 */
145int md5_self_test( int verbose );
146
147#ifdef __cplusplus
148}
149#endif
150
151#endif /* md5.h */