blob: 94b85c38404cd7afb91aba0f38b8da5ee8692879 [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 Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000040#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
41#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
42#define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */
43#define POLARSSL_ERR_MD_FILE_READ_FAILED -0x5280 /**< Failure when reading from file. */
Paul Bakker335db3f2011-04-25 15:28:35 +000044
Paul Bakker17373852011-01-06 14:20:01 +000045typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000046 POLARSSL_MD_NONE=0,
47 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000048 POLARSSL_MD_MD4,
49 POLARSSL_MD_MD5,
50 POLARSSL_MD_SHA1,
51 POLARSSL_MD_SHA224,
52 POLARSSL_MD_SHA256,
53 POLARSSL_MD_SHA384,
54 POLARSSL_MD_SHA512,
55} md_type_t;
56
Paul Bakker1b57b062011-01-06 15:48:19 +000057#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
58
Paul Bakker17373852011-01-06 14:20:01 +000059/**
60 * Message digest information. Allows message digest functions to be called
61 * in a generic way.
62 */
63typedef struct {
64 /** Digest identifier */
65 md_type_t type;
66
67 /** Name of the message digest */
68 const char * name;
69
70 /** Output length of the digest function */
71 int size;
72
73 /** Digest initialisation function */
74 void (*starts_func)( void *ctx );
75
76 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000077 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000078
79 /** Digest finalisation function */
80 void (*finish_func)( void *ctx, unsigned char *output );
81
82 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000083 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000084 unsigned char *output );
85
86 /** Generic file digest function */
87 int (*file_func)( const char *path, unsigned char *output );
88
89 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000090 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000091
92 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000093 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000094
95 /** HMAC finalisation function */
96 void (*hmac_finish_func)( void *ctx, unsigned char *output);
97
98 /** HMAC context reset function */
99 void (*hmac_reset_func)( void *ctx );
100
101 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000102 void (*hmac_func)( const unsigned char *key, size_t keylen,
103 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000104 unsigned char *output );
105
106 /** Allocate a new context */
107 void * (*ctx_alloc_func)( void );
108
109 /** Free the given context */
110 void (*ctx_free_func)( void *ctx );
111
112} md_info_t;
113
114/**
115 * Generic message digest context.
116 */
117typedef struct {
118 /** Information about the associated message digest */
119 const md_info_t *md_info;
120
121 /** Digest-specific context */
122 void *md_ctx;
123} md_context_t;
124
125#define MD_CONTEXT_T_INIT { \
126 NULL, /* md_info */ \
127 NULL, /* md_ctx */ \
128}
129
130#ifdef __cplusplus
131extern "C" {
132#endif
133
134/**
Paul Bakker72f62662011-01-16 21:27:44 +0000135 * \brief Returns the list of digests supported by the generic digest module.
136 *
137 * \return a statically allocated array of digests, the last entry
138 * is 0.
139 */
140const int *md_list( void );
141
142/**
Paul Bakker17373852011-01-06 14:20:01 +0000143 * \brief Returns the message digest information associated with the
144 * given digest name.
145 *
Paul Bakker23986e52011-04-24 08:57:21 +0000146 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000147 *
148 * \return The message digest information associated with md_name or
149 * NULL if not found.
150 */
151const md_info_t *md_info_from_string( const char *md_name );
152
153/**
154 * \brief Returns the message digest information associated with the
155 * given digest type.
156 *
157 * \param md_type type of digest to search for.
158 *
159 * \return The message digest information associated with md_type or
160 * NULL if not found.
161 */
162const md_info_t *md_info_from_type( md_type_t md_type );
163
164/**
Paul Bakker562535d2011-01-20 16:42:01 +0000165 * \brief Initialises and fills the message digest context structure with
166 * the appropriate values.
167 *
168 * \param ctx context to initialise. May not be NULL. The
169 * digest-specific context (ctx->md_ctx) must be NULL. It will
170 * be allocated, and must be freed using md_free_ctx() later.
171 * \param md_info message digest to use.
172 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000173 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
174 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker562535d2011-01-20 16:42:01 +0000175 * allocation of the cipher-specific context failed.
176 */
177int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
178
179/**
180 * \brief Free the message-specific context of ctx. Freeing ctx itself
181 * remains the responsibility of the caller.
182 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000183 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000184 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000185 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
186 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000187 */
188int md_free_ctx( md_context_t *ctx );
189
190/**
Paul Bakker17373852011-01-06 14:20:01 +0000191 * \brief Returns the size of the message digest output.
192 *
193 * \param md_info message digest info
194 *
195 * \return size of the message digest output.
196 */
Paul Bakker23986e52011-04-24 08:57:21 +0000197static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000198{
199 return md_info->size;
200}
201
202/**
203 * \brief Returns the type of the message digest output.
204 *
205 * \param md_info message digest info
206 *
207 * \return type of the message digest output.
208 */
Paul Bakker23986e52011-04-24 08:57:21 +0000209static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000210{
211 return md_info->type;
212}
213
214/**
215 * \brief Returns the name of the message digest output.
216 *
217 * \param md_info message digest info
218 *
219 * \return name of the message digest output.
220 */
Paul Bakker23986e52011-04-24 08:57:21 +0000221static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000222{
223 return md_info->name;
224}
225
226/**
Paul Bakker562535d2011-01-20 16:42:01 +0000227 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000228 *
Paul Bakker562535d2011-01-20 16:42:01 +0000229 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000230 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000231 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
232 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000233 */
Paul Bakker562535d2011-01-20 16:42:01 +0000234int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000235
236/**
237 * \brief Generic message digest process buffer
238 *
239 * \param ctx Generic message digest context
240 * \param input buffer holding the datal
241 * \param ilen length of the input data
242 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000243 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
244 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000245 */
Paul Bakker23986e52011-04-24 08:57:21 +0000246int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000247
248/**
249 * \brief Generic message digest final digest
250 *
251 * \param ctx Generic message digest context
252 * \param output Generic message digest checksum result
253 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000254 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
255 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000256 */
257int md_finish( md_context_t *ctx, unsigned char *output );
258
259/**
Paul Bakker17373852011-01-06 14:20:01 +0000260 * \brief Output = message_digest( input buffer )
261 *
262 * \param md_info message digest info
263 * \param input buffer holding the data
264 * \param ilen length of the input data
265 * \param output Generic message digest checksum result
266 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000267 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
268 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000269 */
Paul Bakker23986e52011-04-24 08:57:21 +0000270int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000271 unsigned char *output );
272
273/**
274 * \brief Output = message_digest( file contents )
275 *
276 * \param md_info message digest info
277 * \param path input file name
278 * \param output generic message digest checksum result
279 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000280 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
281 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
282 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000283 */
284int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
285
286/**
287 * \brief Generic HMAC context setup
288 *
Paul Bakker17373852011-01-06 14:20:01 +0000289 * \param ctx HMAC context to be initialized
290 * \param key HMAC secret key
291 * \param keylen length of the HMAC key
292 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000293 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
294 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000295 */
Paul Bakker23986e52011-04-24 08:57:21 +0000296int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000297
298/**
299 * \brief Generic HMAC process buffer
300 *
301 * \param ctx HMAC context
302 * \param input buffer holding the data
303 * \param ilen length of the input data
304 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000305 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
306 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000307 */
Paul Bakker23986e52011-04-24 08:57:21 +0000308int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000309
310/**
311 * \brief Generic HMAC final digest
312 *
313 * \param ctx HMAC context
314 * \param output Generic HMAC checksum result
315 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000316 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
317 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000318 */
319int md_hmac_finish( md_context_t *ctx, unsigned char *output);
320
321/**
322 * \brief Generic HMAC context reset
323 *
324 * \param ctx HMAC context to be reset
325 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000326 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
327 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000328 */
329int md_hmac_reset( md_context_t *ctx );
330
331/**
332 * \brief Output = Generic_HMAC( hmac key, input buffer )
333 *
334 * \param md_info message digest info
335 * \param key HMAC secret key
336 * \param keylen length of the HMAC key
337 * \param input buffer holding the data
338 * \param ilen length of the input data
339 * \param output Generic HMAC-result
340 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000341 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
342 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000343 */
Paul Bakker23986e52011-04-24 08:57:21 +0000344int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
345 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000346 unsigned char *output );
347
348#ifdef __cplusplus
349}
350#endif
351
352#endif /* POLARSSL_MD_H */