blob: 936e9c958bf3b452254af5e6c28c8013562fb092 [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 Bakker69e095c2011-12-10 21:55:01 +000032#define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */
33
Paul Bakker5121ce52009-01-03 21:22:43 +000034/**
35 * \brief MD5 context structure
36 */
37typedef struct
38{
39 unsigned long total[2]; /*!< number of bytes processed */
40 unsigned long state[4]; /*!< intermediate digest state */
41 unsigned char buffer[64]; /*!< data block being processed */
42
43 unsigned char ipad[64]; /*!< HMAC: inner padding */
44 unsigned char opad[64]; /*!< HMAC: outer padding */
45}
46md5_context;
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/**
53 * \brief MD5 context setup
54 *
55 * \param ctx context to be initialized
56 */
57void md5_starts( md5_context *ctx );
58
59/**
60 * \brief MD5 process buffer
61 *
62 * \param ctx MD5 context
63 * \param input buffer holding the data
64 * \param ilen length of the input data
65 */
Paul Bakker23986e52011-04-24 08:57:21 +000066void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68/**
69 * \brief MD5 final digest
70 *
71 * \param ctx MD5 context
72 * \param output MD5 checksum result
73 */
74void md5_finish( md5_context *ctx, unsigned char output[16] );
75
76/**
77 * \brief Output = MD5( input buffer )
78 *
79 * \param input buffer holding the data
80 * \param ilen length of the input data
81 * \param output MD5 checksum result
82 */
Paul Bakker23986e52011-04-24 08:57:21 +000083void md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85/**
86 * \brief Output = MD5( file contents )
87 *
88 * \param path input file name
89 * \param output MD5 checksum result
90 *
Paul Bakker69e095c2011-12-10 21:55:01 +000091 * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +000092 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000093int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/**
96 * \brief MD5 HMAC context setup
97 *
98 * \param ctx HMAC context to be initialized
99 * \param key HMAC secret key
100 * \param keylen length of the HMAC key
101 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000102void md5_hmac_starts( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000103 const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
106 * \brief MD5 HMAC process buffer
107 *
108 * \param ctx HMAC context
109 * \param input buffer holding the data
110 * \param ilen length of the input data
111 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000112void md5_hmac_update( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000113 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/**
116 * \brief MD5 HMAC final digest
117 *
118 * \param ctx HMAC context
119 * \param output MD5 HMAC checksum result
120 */
121void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
122
123/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000124 * \brief MD5 HMAC context reset
125 *
126 * \param ctx HMAC context to be reset
127 */
128void md5_hmac_reset( md5_context *ctx );
129
130/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \brief Output = HMAC-MD5( hmac key, input buffer )
132 *
133 * \param key HMAC secret key
134 * \param keylen length of the HMAC key
135 * \param input buffer holding the data
136 * \param ilen length of the input data
137 * \param output HMAC-MD5 result
138 */
Paul Bakker23986e52011-04-24 08:57:21 +0000139void md5_hmac( const unsigned char *key, size_t keylen,
140 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 unsigned char output[16] );
142
143/**
144 * \brief Checkup routine
145 *
146 * \return 0 if successful, or 1 if the test failed
147 */
148int md5_self_test( int verbose );
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* md5.h */