blob: 05f6eb0631cd68331ab6864c94560fec1023d015 [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 {
34 POLARSSL_MD_MD2=0,
35 POLARSSL_MD_MD4,
36 POLARSSL_MD_MD5,
37 POLARSSL_MD_SHA1,
38 POLARSSL_MD_SHA224,
39 POLARSSL_MD_SHA256,
40 POLARSSL_MD_SHA384,
41 POLARSSL_MD_SHA512,
42} md_type_t;
43
Paul Bakker1b57b062011-01-06 15:48:19 +000044#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
45
Paul Bakker17373852011-01-06 14:20:01 +000046/**
47 * Message digest information. Allows message digest functions to be called
48 * in a generic way.
49 */
50typedef struct {
51 /** Digest identifier */
52 md_type_t type;
53
54 /** Name of the message digest */
55 const char * name;
56
57 /** Output length of the digest function */
58 int size;
59
60 /** Digest initialisation function */
61 void (*starts_func)( void *ctx );
62
63 /** Digest update function */
64 void (*update_func)( void *ctx, const unsigned char *input, int ilen );
65
66 /** Digest finalisation function */
67 void (*finish_func)( void *ctx, unsigned char *output );
68
69 /** Generic digest function */
70 void (*digest_func)( const unsigned char *input, int ilen,
71 unsigned char *output );
72
73 /** Generic file digest function */
74 int (*file_func)( const char *path, unsigned char *output );
75
76 /** HMAC Initialisation function */
77 void (*hmac_starts_func)( void *ctx, const unsigned char *key, int keylen );
78
79 /** HMAC update function */
80 void (*hmac_update_func)( void *ctx, const unsigned char *input, int ilen );
81
82 /** HMAC finalisation function */
83 void (*hmac_finish_func)( void *ctx, unsigned char *output);
84
85 /** HMAC context reset function */
86 void (*hmac_reset_func)( void *ctx );
87
88 /** Generic HMAC function */
89 void (*hmac_func)( const unsigned char *key, int keylen,
90 const unsigned char *input, int ilen,
91 unsigned char *output );
92
93 /** Allocate a new context */
94 void * (*ctx_alloc_func)( void );
95
96 /** Free the given context */
97 void (*ctx_free_func)( void *ctx );
98
99} md_info_t;
100
101/**
102 * Generic message digest context.
103 */
104typedef struct {
105 /** Information about the associated message digest */
106 const md_info_t *md_info;
107
108 /** Digest-specific context */
109 void *md_ctx;
110} md_context_t;
111
112#define MD_CONTEXT_T_INIT { \
113 NULL, /* md_info */ \
114 NULL, /* md_ctx */ \
115}
116
117#ifdef __cplusplus
118extern "C" {
119#endif
120
121/**
Paul Bakker72f62662011-01-16 21:27:44 +0000122 * \brief Returns the list of digests supported by the generic digest module.
123 *
124 * \return a statically allocated array of digests, the last entry
125 * is 0.
126 */
127const int *md_list( void );
128
129/**
Paul Bakker17373852011-01-06 14:20:01 +0000130 * \brief Returns the message digest information associated with the
131 * given digest name.
132 *
133 * \param md_name Name of the digest to search for.
134 *
135 * \return The message digest information associated with md_name or
136 * NULL if not found.
137 */
138const md_info_t *md_info_from_string( const char *md_name );
139
140/**
141 * \brief Returns the message digest information associated with the
142 * given digest type.
143 *
144 * \param md_type type of digest to search for.
145 *
146 * \return The message digest information associated with md_type or
147 * NULL if not found.
148 */
149const md_info_t *md_info_from_type( md_type_t md_type );
150
151/**
152 * \brief Returns the size of the message digest output.
153 *
154 * \param md_info message digest info
155 *
156 * \return size of the message digest output.
157 */
158static inline unsigned char md_get_size ( const md_info_t *md_info)
159{
160 return md_info->size;
161}
162
163/**
164 * \brief Returns the type of the message digest output.
165 *
166 * \param md_info message digest info
167 *
168 * \return type of the message digest output.
169 */
170static inline md_type_t md_get_type ( const md_info_t *md_info )
171{
172 return md_info->type;
173}
174
175/**
176 * \brief Returns the name of the message digest output.
177 *
178 * \param md_info message digest info
179 *
180 * \return name of the message digest output.
181 */
182static inline const char *md_get_name ( const md_info_t *md_info )
183{
184 return md_info->name;
185}
186
187/**
188 * \brief Generic message digest context setup.
189 *
190 * \param md_info message digest info
191 * \param ctx generic message digest context. May not be NULL. The
192 * digest-specific context (ctx->md_ctx) must be NULL. It will
193 * be allocated, and must be freed using md_free() later.
194 *
195 * \returns 0 on success, 1 if parameter verification fails.
196 */
197int md_starts( const md_info_t *md_info, md_context_t *ctx );
198
199/**
200 * \brief Generic message digest process buffer
201 *
202 * \param ctx Generic message digest context
203 * \param input buffer holding the datal
204 * \param ilen length of the input data
205 *
206 * \returns 0 on success, 1 if parameter verification fails.
207 */
208int md_update( md_context_t *ctx, const unsigned char *input, int ilen );
209
210/**
211 * \brief Generic message digest final digest
212 *
213 * \param ctx Generic message digest context
214 * \param output Generic message digest checksum result
215 *
216 * \returns 0 on success, 1 if parameter verification fails.
217 */
218int md_finish( md_context_t *ctx, unsigned char *output );
219
220/**
221 * \brief Free the message-specific context of ctx. Freeing ctx itself
222 * remains the responsibility of the caller.
223 *
224 * \param ctx Free the -specific context
225 * \param output Generic message digest checksum result
226 *
227 * \returns 0 on success, 1 if parameter verification fails.
228 */
229int md_free_ctx( md_context_t *ctx );
230
231/**
232 * \brief Output = message_digest( input buffer )
233 *
234 * \param md_info message digest info
235 * \param input buffer holding the data
236 * \param ilen length of the input data
237 * \param output Generic message digest checksum result
238 *
239 * \returns 0 on success, 1 if parameter verification fails.
240 */
241int md( const md_info_t *md_info, const unsigned char *input, int ilen,
242 unsigned char *output );
243
244/**
245 * \brief Output = message_digest( file contents )
246 *
247 * \param md_info message digest info
248 * \param path input file name
249 * \param output generic message digest checksum result
250 *
251 * \return 0 if successful, 1 if fopen failed,
252 * 2 if fread failed, 3 if md_info was NULL
253 */
254int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
255
256/**
257 * \brief Generic HMAC context setup
258 *
259 * \param md_info message digest info
260 * \param ctx HMAC context to be initialized
261 * \param key HMAC secret key
262 * \param keylen length of the HMAC key
263 *
264 * \returns 0 on success, 1 if parameter verification fails.
265 */
266int md_hmac_starts( const md_info_t *md_info, md_context_t *ctx,
267 const unsigned char *key, int keylen );
268
269/**
270 * \brief Generic HMAC process buffer
271 *
272 * \param ctx HMAC context
273 * \param input buffer holding the data
274 * \param ilen length of the input data
275 *
276 * \returns 0 on success, 1 if parameter verification fails.
277 */
278int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen );
279
280/**
281 * \brief Generic HMAC final digest
282 *
283 * \param ctx HMAC context
284 * \param output Generic HMAC checksum result
285 *
286 * \returns 0 on success, 1 if parameter verification fails.
287 */
288int md_hmac_finish( md_context_t *ctx, unsigned char *output);
289
290/**
291 * \brief Generic HMAC context reset
292 *
293 * \param ctx HMAC context to be reset
294 *
295 * \returns 0 on success, 1 if ctx is NULL.
296 */
297int md_hmac_reset( md_context_t *ctx );
298
299/**
300 * \brief Output = Generic_HMAC( hmac key, input buffer )
301 *
302 * \param md_info message digest info
303 * \param key HMAC secret key
304 * \param keylen length of the HMAC key
305 * \param input buffer holding the data
306 * \param ilen length of the input data
307 * \param output Generic HMAC-result
308 *
309 * \returns 0 on success, 1 if parameter verification fails.
310 */
311int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
312 const unsigned char *input, int ilen,
313 unsigned char *output );
314
315#ifdef __cplusplus
316}
317#endif
318
319#endif /* POLARSSL_MD_H */