blob: 7914d88948407b0abf14effd9a312a60a97ef5b6 [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
Paul Bakker23986e52011-04-24 08:57:21 +000033#include <string.h>
34
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000035#ifdef _MSC_VER
36#define inline _inline
37#endif
38
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker335db3f2011-04-25 15:28:35 +000040
Paul Bakker17373852011-01-06 14:20:01 +000041typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000042 POLARSSL_MD_NONE=0,
43 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000044 POLARSSL_MD_MD4,
45 POLARSSL_MD_MD5,
46 POLARSSL_MD_SHA1,
47 POLARSSL_MD_SHA224,
48 POLARSSL_MD_SHA256,
49 POLARSSL_MD_SHA384,
50 POLARSSL_MD_SHA512,
51} md_type_t;
52
Paul Bakker1b57b062011-01-06 15:48:19 +000053#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
54
Paul Bakker17373852011-01-06 14:20:01 +000055/**
56 * Message digest information. Allows message digest functions to be called
57 * in a generic way.
58 */
59typedef struct {
60 /** Digest identifier */
61 md_type_t type;
62
63 /** Name of the message digest */
64 const char * name;
65
66 /** Output length of the digest function */
67 int size;
68
69 /** Digest initialisation function */
70 void (*starts_func)( void *ctx );
71
72 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000073 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000074
75 /** Digest finalisation function */
76 void (*finish_func)( void *ctx, unsigned char *output );
77
78 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000079 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000080 unsigned char *output );
81
82 /** Generic file digest function */
83 int (*file_func)( const char *path, unsigned char *output );
84
85 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000086 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000087
88 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000089 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000090
91 /** HMAC finalisation function */
92 void (*hmac_finish_func)( void *ctx, unsigned char *output);
93
94 /** HMAC context reset function */
95 void (*hmac_reset_func)( void *ctx );
96
97 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +000098 void (*hmac_func)( const unsigned char *key, size_t keylen,
99 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000100 unsigned char *output );
101
102 /** Allocate a new context */
103 void * (*ctx_alloc_func)( void );
104
105 /** Free the given context */
106 void (*ctx_free_func)( void *ctx );
107
108} md_info_t;
109
110/**
111 * Generic message digest context.
112 */
113typedef struct {
114 /** Information about the associated message digest */
115 const md_info_t *md_info;
116
117 /** Digest-specific context */
118 void *md_ctx;
119} md_context_t;
120
121#define MD_CONTEXT_T_INIT { \
122 NULL, /* md_info */ \
123 NULL, /* md_ctx */ \
124}
125
126#ifdef __cplusplus
127extern "C" {
128#endif
129
130/**
Paul Bakker72f62662011-01-16 21:27:44 +0000131 * \brief Returns the list of digests supported by the generic digest module.
132 *
133 * \return a statically allocated array of digests, the last entry
134 * is 0.
135 */
136const int *md_list( void );
137
138/**
Paul Bakker17373852011-01-06 14:20:01 +0000139 * \brief Returns the message digest information associated with the
140 * given digest name.
141 *
Paul Bakker23986e52011-04-24 08:57:21 +0000142 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000143 *
144 * \return The message digest information associated with md_name or
145 * NULL if not found.
146 */
147const md_info_t *md_info_from_string( const char *md_name );
148
149/**
150 * \brief Returns the message digest information associated with the
151 * given digest type.
152 *
153 * \param md_type type of digest to search for.
154 *
155 * \return The message digest information associated with md_type or
156 * NULL if not found.
157 */
158const md_info_t *md_info_from_type( md_type_t md_type );
159
160/**
Paul Bakker562535d2011-01-20 16:42:01 +0000161 * \brief Initialises and fills the message digest context structure with
162 * the appropriate values.
163 *
164 * \param ctx context to initialise. May not be NULL. The
165 * digest-specific context (ctx->md_ctx) must be NULL. It will
166 * be allocated, and must be freed using md_free_ctx() later.
167 * \param md_info message digest to use.
168 *
169 * \returns \c 0 on success, \c 1 on parameter failure, \c 2 if
170 * allocation of the cipher-specific context failed.
171 */
172int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
173
174/**
175 * \brief Free the message-specific context of ctx. Freeing ctx itself
176 * remains the responsibility of the caller.
177 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000178 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000179 *
180 * \returns 0 on success, 1 if parameter verification fails.
181 */
182int md_free_ctx( md_context_t *ctx );
183
184/**
Paul Bakker17373852011-01-06 14:20:01 +0000185 * \brief Returns the size of the message digest output.
186 *
187 * \param md_info message digest info
188 *
189 * \return size of the message digest output.
190 */
Paul Bakker23986e52011-04-24 08:57:21 +0000191static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000192{
193 return md_info->size;
194}
195
196/**
197 * \brief Returns the type of the message digest output.
198 *
199 * \param md_info message digest info
200 *
201 * \return type of the message digest output.
202 */
Paul Bakker23986e52011-04-24 08:57:21 +0000203static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000204{
205 return md_info->type;
206}
207
208/**
209 * \brief Returns the name of the message digest output.
210 *
211 * \param md_info message digest info
212 *
213 * \return name of the message digest output.
214 */
Paul Bakker23986e52011-04-24 08:57:21 +0000215static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000216{
217 return md_info->name;
218}
219
220/**
Paul Bakker562535d2011-01-20 16:42:01 +0000221 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000222 *
Paul Bakker562535d2011-01-20 16:42:01 +0000223 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000224 *
225 * \returns 0 on success, 1 if parameter verification fails.
226 */
Paul Bakker562535d2011-01-20 16:42:01 +0000227int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000228
229/**
230 * \brief Generic message digest process buffer
231 *
232 * \param ctx Generic message digest context
233 * \param input buffer holding the datal
234 * \param ilen length of the input data
235 *
236 * \returns 0 on success, 1 if parameter verification fails.
237 */
Paul Bakker23986e52011-04-24 08:57:21 +0000238int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000239
240/**
241 * \brief Generic message digest final digest
242 *
243 * \param ctx Generic message digest context
244 * \param output Generic message digest checksum result
245 *
246 * \returns 0 on success, 1 if parameter verification fails.
247 */
248int md_finish( md_context_t *ctx, unsigned char *output );
249
250/**
Paul Bakker17373852011-01-06 14:20:01 +0000251 * \brief Output = message_digest( input buffer )
252 *
253 * \param md_info message digest info
254 * \param input buffer holding the data
255 * \param ilen length of the input data
256 * \param output Generic message digest checksum result
257 *
258 * \returns 0 on success, 1 if parameter verification fails.
259 */
Paul Bakker23986e52011-04-24 08:57:21 +0000260int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000261 unsigned char *output );
262
263/**
264 * \brief Output = message_digest( file contents )
265 *
266 * \param md_info message digest info
267 * \param path input file name
268 * \param output generic message digest checksum result
269 *
270 * \return 0 if successful, 1 if fopen failed,
271 * 2 if fread failed, 3 if md_info was NULL
272 */
273int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
274
275/**
276 * \brief Generic HMAC context setup
277 *
Paul Bakker17373852011-01-06 14:20:01 +0000278 * \param ctx HMAC context to be initialized
279 * \param key HMAC secret key
280 * \param keylen length of the HMAC key
281 *
282 * \returns 0 on success, 1 if parameter verification fails.
283 */
Paul Bakker23986e52011-04-24 08:57:21 +0000284int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000285
286/**
287 * \brief Generic HMAC process buffer
288 *
289 * \param ctx HMAC context
290 * \param input buffer holding the data
291 * \param ilen length of the input data
292 *
293 * \returns 0 on success, 1 if parameter verification fails.
294 */
Paul Bakker23986e52011-04-24 08:57:21 +0000295int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000296
297/**
298 * \brief Generic HMAC final digest
299 *
300 * \param ctx HMAC context
301 * \param output Generic HMAC checksum result
302 *
303 * \returns 0 on success, 1 if parameter verification fails.
304 */
305int md_hmac_finish( md_context_t *ctx, unsigned char *output);
306
307/**
308 * \brief Generic HMAC context reset
309 *
310 * \param ctx HMAC context to be reset
311 *
312 * \returns 0 on success, 1 if ctx is NULL.
313 */
314int md_hmac_reset( md_context_t *ctx );
315
316/**
317 * \brief Output = Generic_HMAC( hmac key, input buffer )
318 *
319 * \param md_info message digest info
320 * \param key HMAC secret key
321 * \param keylen length of the HMAC key
322 * \param input buffer holding the data
323 * \param ilen length of the input data
324 * \param output Generic HMAC-result
325 *
326 * \returns 0 on success, 1 if parameter verification fails.
327 */
Paul Bakker23986e52011-04-24 08:57:21 +0000328int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
329 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000330 unsigned char *output );
331
332#ifdef __cplusplus
333}
334#endif
335
336#endif /* POLARSSL_MD_H */