blob: 76c480cafd3a2c4af97f88cea72b5233e7e74ad9 [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 *
170 * \param ctx Free the -specific context
171 * \param output Generic message digest checksum result
172 *
173 * \returns 0 on success, 1 if parameter verification fails.
174 */
175int md_free_ctx( md_context_t *ctx );
176
177/**
Paul Bakker17373852011-01-06 14:20:01 +0000178 * \brief Returns the size of the message digest output.
179 *
180 * \param md_info message digest info
181 *
182 * \return size of the message digest output.
183 */
184static inline unsigned char md_get_size ( const md_info_t *md_info)
185{
186 return md_info->size;
187}
188
189/**
190 * \brief Returns the type of the message digest output.
191 *
192 * \param md_info message digest info
193 *
194 * \return type of the message digest output.
195 */
196static inline md_type_t md_get_type ( const md_info_t *md_info )
197{
198 return md_info->type;
199}
200
201/**
202 * \brief Returns the name of the message digest output.
203 *
204 * \param md_info message digest info
205 *
206 * \return name of the message digest output.
207 */
208static inline const char *md_get_name ( const md_info_t *md_info )
209{
210 return md_info->name;
211}
212
213/**
Paul Bakker562535d2011-01-20 16:42:01 +0000214 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000215 *
Paul Bakker562535d2011-01-20 16:42:01 +0000216 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000217 *
218 * \returns 0 on success, 1 if parameter verification fails.
219 */
Paul Bakker562535d2011-01-20 16:42:01 +0000220int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000221
222/**
223 * \brief Generic message digest process buffer
224 *
225 * \param ctx Generic message digest context
226 * \param input buffer holding the datal
227 * \param ilen length of the input data
228 *
229 * \returns 0 on success, 1 if parameter verification fails.
230 */
231int md_update( md_context_t *ctx, const unsigned char *input, int ilen );
232
233/**
234 * \brief Generic message digest final digest
235 *
236 * \param ctx Generic message digest context
237 * \param output Generic message digest checksum result
238 *
239 * \returns 0 on success, 1 if parameter verification fails.
240 */
241int md_finish( md_context_t *ctx, unsigned char *output );
242
243/**
Paul Bakker17373852011-01-06 14:20:01 +0000244 * \brief Output = message_digest( input buffer )
245 *
246 * \param md_info message digest info
247 * \param input buffer holding the data
248 * \param ilen length of the input data
249 * \param output Generic message digest checksum result
250 *
251 * \returns 0 on success, 1 if parameter verification fails.
252 */
253int md( const md_info_t *md_info, const unsigned char *input, int ilen,
254 unsigned char *output );
255
256/**
257 * \brief Output = message_digest( file contents )
258 *
259 * \param md_info message digest info
260 * \param path input file name
261 * \param output generic message digest checksum result
262 *
263 * \return 0 if successful, 1 if fopen failed,
264 * 2 if fread failed, 3 if md_info was NULL
265 */
266int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
267
268/**
269 * \brief Generic HMAC context setup
270 *
Paul Bakker17373852011-01-06 14:20:01 +0000271 * \param ctx HMAC context to be initialized
272 * \param key HMAC secret key
273 * \param keylen length of the HMAC key
274 *
275 * \returns 0 on success, 1 if parameter verification fails.
276 */
Paul Bakker562535d2011-01-20 16:42:01 +0000277int md_hmac_starts( md_context_t *ctx, const unsigned char *key, int keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000278
279/**
280 * \brief Generic HMAC process buffer
281 *
282 * \param ctx HMAC context
283 * \param input buffer holding the data
284 * \param ilen length of the input data
285 *
286 * \returns 0 on success, 1 if parameter verification fails.
287 */
288int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen );
289
290/**
291 * \brief Generic HMAC final digest
292 *
293 * \param ctx HMAC context
294 * \param output Generic HMAC checksum result
295 *
296 * \returns 0 on success, 1 if parameter verification fails.
297 */
298int md_hmac_finish( md_context_t *ctx, unsigned char *output);
299
300/**
301 * \brief Generic HMAC context reset
302 *
303 * \param ctx HMAC context to be reset
304 *
305 * \returns 0 on success, 1 if ctx is NULL.
306 */
307int md_hmac_reset( md_context_t *ctx );
308
309/**
310 * \brief Output = Generic_HMAC( hmac key, input buffer )
311 *
312 * \param md_info message digest info
313 * \param key HMAC secret key
314 * \param keylen length of the HMAC key
315 * \param input buffer holding the data
316 * \param ilen length of the input data
317 * \param output Generic HMAC-result
318 *
319 * \returns 0 on success, 1 if parameter verification fails.
320 */
321int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
322 const unsigned char *input, int ilen,
323 unsigned char *output );
324
325#ifdef __cplusplus
326}
327#endif
328
329#endif /* POLARSSL_MD_H */