blob: 048882d1133436eb4d960b54be1e69df04697387 [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 Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +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
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Paul Bakker5c2364c2012-10-01 14:41:15 +000031#ifdef _MSC_VER
32#include <basetsd.h>
33typedef UINT32 uint32_t;
34#else
35#include <inttypes.h>
36#endif
37
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */
39
Paul Bakker4087c472013-06-12 16:49:10 +020040#if !defined(POLARSSL_MD5_ALT)
41// Regular implementation
42//
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044/**
45 * \brief MD5 context structure
46 */
47typedef struct
48{
Paul Bakker5c2364c2012-10-01 14:41:15 +000049 uint32_t total[2]; /*!< number of bytes processed */
50 uint32_t state[4]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000051 unsigned char buffer[64]; /*!< data block being processed */
52
53 unsigned char ipad[64]; /*!< HMAC: inner padding */
54 unsigned char opad[64]; /*!< HMAC: outer padding */
55}
56md5_context;
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
63 * \brief MD5 context setup
64 *
65 * \param ctx context to be initialized
66 */
67void md5_starts( md5_context *ctx );
68
69/**
70 * \brief MD5 process buffer
71 *
72 * \param ctx MD5 context
73 * \param input buffer holding the data
74 * \param ilen length of the input data
75 */
Paul Bakker23986e52011-04-24 08:57:21 +000076void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/**
79 * \brief MD5 final digest
80 *
81 * \param ctx MD5 context
82 * \param output MD5 checksum result
83 */
84void md5_finish( md5_context *ctx, unsigned char output[16] );
85
Paul Bakker4087c472013-06-12 16:49:10 +020086/* Internal use */
87void md5_process( md5_context *ctx, const unsigned char data[64] );
88
89#ifdef __cplusplus
90}
91#endif
92
93#else /* POLARSSL_MD5_ALT */
94#include "md5_alt.h"
95#endif /* POLARSSL_MD5_ALT */
96
97#ifdef __cplusplus
98extern "C" {
99#endif
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/**
102 * \brief Output = MD5( input buffer )
103 *
104 * \param input buffer holding the data
105 * \param ilen length of the input data
106 * \param output MD5 checksum result
107 */
Paul Bakker23986e52011-04-24 08:57:21 +0000108void md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
111 * \brief Output = MD5( file contents )
112 *
113 * \param path input file name
114 * \param output MD5 checksum result
115 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000116 * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118int md5_file( const char *path, unsigned char output[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
121 * \brief MD5 HMAC context setup
122 *
123 * \param ctx HMAC context to be initialized
124 * \param key HMAC secret key
125 * \param keylen length of the HMAC key
126 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000127void md5_hmac_starts( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000128 const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
131 * \brief MD5 HMAC process buffer
132 *
133 * \param ctx HMAC context
134 * \param input buffer holding the data
135 * \param ilen length of the input data
136 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000137void md5_hmac_update( md5_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000138 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief MD5 HMAC final digest
142 *
143 * \param ctx HMAC context
144 * \param output MD5 HMAC checksum result
145 */
146void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
147
148/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000149 * \brief MD5 HMAC context reset
150 *
151 * \param ctx HMAC context to be reset
152 */
153void md5_hmac_reset( md5_context *ctx );
154
155/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 * \brief Output = HMAC-MD5( hmac key, input buffer )
157 *
158 * \param key HMAC secret key
159 * \param keylen length of the HMAC key
160 * \param input buffer holding the data
161 * \param ilen length of the input data
162 * \param output HMAC-MD5 result
163 */
Paul Bakker23986e52011-04-24 08:57:21 +0000164void md5_hmac( const unsigned char *key, size_t keylen,
165 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 unsigned char output[16] );
167
168/**
169 * \brief Checkup routine
170 *
171 * \return 0 if successful, or 1 if the test failed
172 */
173int md5_self_test( int verbose );
174
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#ifdef __cplusplus
176}
177#endif
178
179#endif /* md5.h */