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