blob: 9bda3ee573b7d46cf094a6d6ea1e3f7c6c182f1e [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 Bakker17373852011-01-06 14:20:01 +000039typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000040 POLARSSL_MD_NONE=0,
41 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000042 POLARSSL_MD_MD4,
43 POLARSSL_MD_MD5,
44 POLARSSL_MD_SHA1,
45 POLARSSL_MD_SHA224,
46 POLARSSL_MD_SHA256,
47 POLARSSL_MD_SHA384,
48 POLARSSL_MD_SHA512,
49} md_type_t;
50
Paul Bakker1b57b062011-01-06 15:48:19 +000051#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
52
Paul Bakker17373852011-01-06 14:20:01 +000053/**
54 * Message digest information. Allows message digest functions to be called
55 * in a generic way.
56 */
57typedef struct {
58 /** Digest identifier */
59 md_type_t type;
60
61 /** Name of the message digest */
62 const char * name;
63
64 /** Output length of the digest function */
65 int size;
66
67 /** Digest initialisation function */
68 void (*starts_func)( void *ctx );
69
70 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000071 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000072
73 /** Digest finalisation function */
74 void (*finish_func)( void *ctx, unsigned char *output );
75
76 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000077 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000078 unsigned char *output );
79
80 /** Generic file digest function */
81 int (*file_func)( const char *path, unsigned char *output );
82
83 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000084 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000085
86 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000087 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000088
89 /** HMAC finalisation function */
90 void (*hmac_finish_func)( void *ctx, unsigned char *output);
91
92 /** HMAC context reset function */
93 void (*hmac_reset_func)( void *ctx );
94
95 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +000096 void (*hmac_func)( const unsigned char *key, size_t keylen,
97 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000098 unsigned char *output );
99
100 /** Allocate a new context */
101 void * (*ctx_alloc_func)( void );
102
103 /** Free the given context */
104 void (*ctx_free_func)( void *ctx );
105
106} md_info_t;
107
108/**
109 * Generic message digest context.
110 */
111typedef struct {
112 /** Information about the associated message digest */
113 const md_info_t *md_info;
114
115 /** Digest-specific context */
116 void *md_ctx;
117} md_context_t;
118
119#define MD_CONTEXT_T_INIT { \
120 NULL, /* md_info */ \
121 NULL, /* md_ctx */ \
122}
123
124#ifdef __cplusplus
125extern "C" {
126#endif
127
128/**
Paul Bakker72f62662011-01-16 21:27:44 +0000129 * \brief Returns the list of digests supported by the generic digest module.
130 *
131 * \return a statically allocated array of digests, the last entry
132 * is 0.
133 */
134const int *md_list( void );
135
136/**
Paul Bakker17373852011-01-06 14:20:01 +0000137 * \brief Returns the message digest information associated with the
138 * given digest name.
139 *
Paul Bakker23986e52011-04-24 08:57:21 +0000140 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000141 *
142 * \return The message digest information associated with md_name or
143 * NULL if not found.
144 */
145const md_info_t *md_info_from_string( const char *md_name );
146
147/**
148 * \brief Returns the message digest information associated with the
149 * given digest type.
150 *
151 * \param md_type type of digest to search for.
152 *
153 * \return The message digest information associated with md_type or
154 * NULL if not found.
155 */
156const md_info_t *md_info_from_type( md_type_t md_type );
157
158/**
Paul Bakker562535d2011-01-20 16:42:01 +0000159 * \brief Initialises and fills the message digest context structure with
160 * the appropriate values.
161 *
162 * \param ctx context to initialise. May not be NULL. The
163 * digest-specific context (ctx->md_ctx) must be NULL. It will
164 * be allocated, and must be freed using md_free_ctx() later.
165 * \param md_info message digest to use.
166 *
167 * \returns \c 0 on success, \c 1 on parameter failure, \c 2 if
168 * allocation of the cipher-specific context failed.
169 */
170int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
171
172/**
173 * \brief Free the message-specific context of ctx. Freeing ctx itself
174 * remains the responsibility of the caller.
175 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000176 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000177 *
178 * \returns 0 on success, 1 if parameter verification fails.
179 */
180int md_free_ctx( md_context_t *ctx );
181
182/**
Paul Bakker17373852011-01-06 14:20:01 +0000183 * \brief Returns the size of the message digest output.
184 *
185 * \param md_info message digest info
186 *
187 * \return size of the message digest output.
188 */
Paul Bakker23986e52011-04-24 08:57:21 +0000189static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000190{
191 return md_info->size;
192}
193
194/**
195 * \brief Returns the type of the message digest output.
196 *
197 * \param md_info message digest info
198 *
199 * \return type of the message digest output.
200 */
Paul Bakker23986e52011-04-24 08:57:21 +0000201static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000202{
203 return md_info->type;
204}
205
206/**
207 * \brief Returns the name of the message digest output.
208 *
209 * \param md_info message digest info
210 *
211 * \return name of the message digest output.
212 */
Paul Bakker23986e52011-04-24 08:57:21 +0000213static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000214{
215 return md_info->name;
216}
217
218/**
Paul Bakker562535d2011-01-20 16:42:01 +0000219 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000220 *
Paul Bakker562535d2011-01-20 16:42:01 +0000221 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000222 *
223 * \returns 0 on success, 1 if parameter verification fails.
224 */
Paul Bakker562535d2011-01-20 16:42:01 +0000225int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000226
227/**
228 * \brief Generic message digest process buffer
229 *
230 * \param ctx Generic message digest context
231 * \param input buffer holding the datal
232 * \param ilen length of the input data
233 *
234 * \returns 0 on success, 1 if parameter verification fails.
235 */
Paul Bakker23986e52011-04-24 08:57:21 +0000236int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000237
238/**
239 * \brief Generic message digest final digest
240 *
241 * \param ctx Generic message digest context
242 * \param output Generic message digest checksum result
243 *
244 * \returns 0 on success, 1 if parameter verification fails.
245 */
246int md_finish( md_context_t *ctx, unsigned char *output );
247
248/**
Paul Bakker17373852011-01-06 14:20:01 +0000249 * \brief Output = message_digest( input buffer )
250 *
251 * \param md_info message digest info
252 * \param input buffer holding the data
253 * \param ilen length of the input data
254 * \param output Generic message digest checksum result
255 *
256 * \returns 0 on success, 1 if parameter verification fails.
257 */
Paul Bakker23986e52011-04-24 08:57:21 +0000258int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000259 unsigned char *output );
260
261/**
262 * \brief Output = message_digest( file contents )
263 *
264 * \param md_info message digest info
265 * \param path input file name
266 * \param output generic message digest checksum result
267 *
268 * \return 0 if successful, 1 if fopen failed,
269 * 2 if fread failed, 3 if md_info was NULL
270 */
271int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
272
273/**
274 * \brief Generic HMAC context setup
275 *
Paul Bakker17373852011-01-06 14:20:01 +0000276 * \param ctx HMAC context to be initialized
277 * \param key HMAC secret key
278 * \param keylen length of the HMAC key
279 *
280 * \returns 0 on success, 1 if parameter verification fails.
281 */
Paul Bakker23986e52011-04-24 08:57:21 +0000282int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000283
284/**
285 * \brief Generic HMAC process buffer
286 *
287 * \param ctx HMAC context
288 * \param input buffer holding the data
289 * \param ilen length of the input data
290 *
291 * \returns 0 on success, 1 if parameter verification fails.
292 */
Paul Bakker23986e52011-04-24 08:57:21 +0000293int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000294
295/**
296 * \brief Generic HMAC final digest
297 *
298 * \param ctx HMAC context
299 * \param output Generic HMAC checksum result
300 *
301 * \returns 0 on success, 1 if parameter verification fails.
302 */
303int md_hmac_finish( md_context_t *ctx, unsigned char *output);
304
305/**
306 * \brief Generic HMAC context reset
307 *
308 * \param ctx HMAC context to be reset
309 *
310 * \returns 0 on success, 1 if ctx is NULL.
311 */
312int md_hmac_reset( md_context_t *ctx );
313
314/**
315 * \brief Output = Generic_HMAC( hmac key, input buffer )
316 *
317 * \param md_info message digest info
318 * \param key HMAC secret key
319 * \param keylen length of the HMAC key
320 * \param input buffer holding the data
321 * \param ilen length of the input data
322 * \param output Generic HMAC-result
323 *
324 * \returns 0 on success, 1 if parameter verification fails.
325 */
Paul Bakker23986e52011-04-24 08:57:21 +0000326int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
327 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000328 unsigned char *output );
329
330#ifdef __cplusplus
331}
332#endif
333
334#endif /* POLARSSL_MD_H */