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