blob: 1ee742f24e8bca0cf714ddcfa2771bae8393353f [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md.h
3 *
4 * \brief Generic message digest wrapper
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#ifndef POLARSSL_MD_H
31#define POLARSSL_MD_H
32
33typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000034 POLARSSL_MD_NONE=0,
35 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000036 POLARSSL_MD_MD4,
37 POLARSSL_MD_MD5,
38 POLARSSL_MD_SHA1,
39 POLARSSL_MD_SHA224,
40 POLARSSL_MD_SHA256,
41 POLARSSL_MD_SHA384,
42 POLARSSL_MD_SHA512,
43} md_type_t;
44
Paul Bakker1b57b062011-01-06 15:48:19 +000045#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
46
Paul Bakker17373852011-01-06 14:20:01 +000047/**
48 * Message digest information. Allows message digest functions to be called
49 * in a generic way.
50 */
51typedef struct {
52 /** Digest identifier */
53 md_type_t type;
54
55 /** Name of the message digest */
56 const char * name;
57
58 /** Output length of the digest function */
59 int size;
60
61 /** Digest initialisation function */
62 void (*starts_func)( void *ctx );
63
64 /** Digest update function */
65 void (*update_func)( void *ctx, const unsigned char *input, int ilen );
66
67 /** Digest finalisation function */
68 void (*finish_func)( void *ctx, unsigned char *output );
69
70 /** Generic digest function */
71 void (*digest_func)( const unsigned char *input, int ilen,
72 unsigned char *output );
73
74 /** Generic file digest function */
75 int (*file_func)( const char *path, unsigned char *output );
76
77 /** HMAC Initialisation function */
78 void (*hmac_starts_func)( void *ctx, const unsigned char *key, int keylen );
79
80 /** HMAC update function */
81 void (*hmac_update_func)( void *ctx, const unsigned char *input, int ilen );
82
83 /** HMAC finalisation function */
84 void (*hmac_finish_func)( void *ctx, unsigned char *output);
85
86 /** HMAC context reset function */
87 void (*hmac_reset_func)( void *ctx );
88
89 /** Generic HMAC function */
90 void (*hmac_func)( const unsigned char *key, int keylen,
91 const unsigned char *input, int ilen,
92 unsigned char *output );
93
94 /** Allocate a new context */
95 void * (*ctx_alloc_func)( void );
96
97 /** Free the given context */
98 void (*ctx_free_func)( void *ctx );
99
100} md_info_t;
101
102/**
103 * Generic message digest context.
104 */
105typedef struct {
106 /** Information about the associated message digest */
107 const md_info_t *md_info;
108
109 /** Digest-specific context */
110 void *md_ctx;
111} md_context_t;
112
113#define MD_CONTEXT_T_INIT { \
114 NULL, /* md_info */ \
115 NULL, /* md_ctx */ \
116}
117
118#ifdef __cplusplus
119extern "C" {
120#endif
121
122/**
Paul Bakker72f62662011-01-16 21:27:44 +0000123 * \brief Returns the list of digests supported by the generic digest module.
124 *
125 * \return a statically allocated array of digests, the last entry
126 * is 0.
127 */
128const int *md_list( void );
129
130/**
Paul Bakker17373852011-01-06 14:20:01 +0000131 * \brief Returns the message digest information associated with the
132 * given digest name.
133 *
134 * \param md_name Name of the digest to search for.
135 *
136 * \return The message digest information associated with md_name or
137 * NULL if not found.
138 */
139const md_info_t *md_info_from_string( const char *md_name );
140
141/**
142 * \brief Returns the message digest information associated with the
143 * given digest type.
144 *
145 * \param md_type type of digest to search for.
146 *
147 * \return The message digest information associated with md_type or
148 * NULL if not found.
149 */
150const md_info_t *md_info_from_type( md_type_t md_type );
151
152/**
Paul Bakker562535d2011-01-20 16:42:01 +0000153 * \brief Initialises and fills the message digest context structure with
154 * the appropriate values.
155 *
156 * \param ctx context to initialise. May not be NULL. The
157 * digest-specific context (ctx->md_ctx) must be NULL. It will
158 * be allocated, and must be freed using md_free_ctx() later.
159 * \param md_info message digest to use.
160 *
161 * \returns \c 0 on success, \c 1 on parameter failure, \c 2 if
162 * allocation of the cipher-specific context failed.
163 */
164int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
165
166/**
167 * \brief Free the message-specific context of ctx. Freeing ctx itself
168 * remains the responsibility of the caller.
169 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000170 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000171 *
172 * \returns 0 on success, 1 if parameter verification fails.
173 */
174int md_free_ctx( md_context_t *ctx );
175
176/**
Paul Bakker17373852011-01-06 14:20:01 +0000177 * \brief Returns the size of the message digest output.
178 *
179 * \param md_info message digest info
180 *
181 * \return size of the message digest output.
182 */
183static inline unsigned char md_get_size ( const md_info_t *md_info)
184{
185 return md_info->size;
186}
187
188/**
189 * \brief Returns the type of the message digest output.
190 *
191 * \param md_info message digest info
192 *
193 * \return type of the message digest output.
194 */
195static inline md_type_t md_get_type ( const md_info_t *md_info )
196{
197 return md_info->type;
198}
199
200/**
201 * \brief Returns the name of the message digest output.
202 *
203 * \param md_info message digest info
204 *
205 * \return name of the message digest output.
206 */
207static inline const char *md_get_name ( const md_info_t *md_info )
208{
209 return md_info->name;
210}
211
212/**
Paul Bakker562535d2011-01-20 16:42:01 +0000213 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000214 *
Paul Bakker562535d2011-01-20 16:42:01 +0000215 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000216 *
217 * \returns 0 on success, 1 if parameter verification fails.
218 */
Paul Bakker562535d2011-01-20 16:42:01 +0000219int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000220
221/**
222 * \brief Generic message digest process buffer
223 *
224 * \param ctx Generic message digest context
225 * \param input buffer holding the datal
226 * \param ilen length of the input data
227 *
228 * \returns 0 on success, 1 if parameter verification fails.
229 */
230int md_update( md_context_t *ctx, const unsigned char *input, int ilen );
231
232/**
233 * \brief Generic message digest final digest
234 *
235 * \param ctx Generic message digest context
236 * \param output Generic message digest checksum result
237 *
238 * \returns 0 on success, 1 if parameter verification fails.
239 */
240int md_finish( md_context_t *ctx, unsigned char *output );
241
242/**
Paul Bakker17373852011-01-06 14:20:01 +0000243 * \brief Output = message_digest( input buffer )
244 *
245 * \param md_info message digest info
246 * \param input buffer holding the data
247 * \param ilen length of the input data
248 * \param output Generic message digest checksum result
249 *
250 * \returns 0 on success, 1 if parameter verification fails.
251 */
252int md( const md_info_t *md_info, const unsigned char *input, int ilen,
253 unsigned char *output );
254
255/**
256 * \brief Output = message_digest( file contents )
257 *
258 * \param md_info message digest info
259 * \param path input file name
260 * \param output generic message digest checksum result
261 *
262 * \return 0 if successful, 1 if fopen failed,
263 * 2 if fread failed, 3 if md_info was NULL
264 */
265int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
266
267/**
268 * \brief Generic HMAC context setup
269 *
Paul Bakker17373852011-01-06 14:20:01 +0000270 * \param ctx HMAC context to be initialized
271 * \param key HMAC secret key
272 * \param keylen length of the HMAC key
273 *
274 * \returns 0 on success, 1 if parameter verification fails.
275 */
Paul Bakker562535d2011-01-20 16:42:01 +0000276int md_hmac_starts( md_context_t *ctx, const unsigned char *key, int keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000277
278/**
279 * \brief Generic HMAC process buffer
280 *
281 * \param ctx HMAC context
282 * \param input buffer holding the data
283 * \param ilen length of the input data
284 *
285 * \returns 0 on success, 1 if parameter verification fails.
286 */
287int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen );
288
289/**
290 * \brief Generic HMAC final digest
291 *
292 * \param ctx HMAC context
293 * \param output Generic HMAC checksum result
294 *
295 * \returns 0 on success, 1 if parameter verification fails.
296 */
297int md_hmac_finish( md_context_t *ctx, unsigned char *output);
298
299/**
300 * \brief Generic HMAC context reset
301 *
302 * \param ctx HMAC context to be reset
303 *
304 * \returns 0 on success, 1 if ctx is NULL.
305 */
306int md_hmac_reset( md_context_t *ctx );
307
308/**
309 * \brief Output = Generic_HMAC( hmac key, input buffer )
310 *
311 * \param md_info message digest info
312 * \param key HMAC secret key
313 * \param keylen length of the HMAC key
314 * \param input buffer holding the data
315 * \param ilen length of the input data
316 * \param output Generic HMAC-result
317 *
318 * \returns 0 on success, 1 if parameter verification fails.
319 */
320int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
321 const unsigned char *input, int ilen,
322 unsigned char *output );
323
324#ifdef __cplusplus
325}
326#endif
327
328#endif /* POLARSSL_MD_H */