blob: aaafa8b88abfd8c46863172c44d191a359ac7864 [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 Bakker09b1ec62011-07-27 16:28:54 +000035#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000036#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000037#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000038#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000039#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000040#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000041#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000042
Paul Bakker9d781402011-05-09 16:17:09 +000043#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000044#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
45#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
46#define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */
47#define POLARSSL_ERR_MD_FILE_READ_FAILED -0x5280 /**< Failure when reading from file. */
Paul Bakker335db3f2011-04-25 15:28:35 +000048
Paul Bakker17373852011-01-06 14:20:01 +000049typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000050 POLARSSL_MD_NONE=0,
51 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000052 POLARSSL_MD_MD4,
53 POLARSSL_MD_MD5,
54 POLARSSL_MD_SHA1,
55 POLARSSL_MD_SHA224,
56 POLARSSL_MD_SHA256,
57 POLARSSL_MD_SHA384,
58 POLARSSL_MD_SHA512,
59} md_type_t;
60
Paul Bakker1b57b062011-01-06 15:48:19 +000061#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
62
Paul Bakker17373852011-01-06 14:20:01 +000063/**
64 * Message digest information. Allows message digest functions to be called
65 * in a generic way.
66 */
67typedef struct {
68 /** Digest identifier */
69 md_type_t type;
70
71 /** Name of the message digest */
72 const char * name;
73
74 /** Output length of the digest function */
75 int size;
76
77 /** Digest initialisation function */
78 void (*starts_func)( void *ctx );
79
80 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000081 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000082
83 /** Digest finalisation function */
84 void (*finish_func)( void *ctx, unsigned char *output );
85
86 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000087 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000088 unsigned char *output );
89
90 /** Generic file digest function */
91 int (*file_func)( const char *path, unsigned char *output );
92
93 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000094 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000095
96 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000097 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000098
99 /** HMAC finalisation function */
100 void (*hmac_finish_func)( void *ctx, unsigned char *output);
101
102 /** HMAC context reset function */
103 void (*hmac_reset_func)( void *ctx );
104
105 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000106 void (*hmac_func)( const unsigned char *key, size_t keylen,
107 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000108 unsigned char *output );
109
110 /** Allocate a new context */
111 void * (*ctx_alloc_func)( void );
112
113 /** Free the given context */
114 void (*ctx_free_func)( void *ctx );
115
116} md_info_t;
117
118/**
119 * Generic message digest context.
120 */
121typedef struct {
122 /** Information about the associated message digest */
123 const md_info_t *md_info;
124
125 /** Digest-specific context */
126 void *md_ctx;
127} md_context_t;
128
129#define MD_CONTEXT_T_INIT { \
130 NULL, /* md_info */ \
131 NULL, /* md_ctx */ \
132}
133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
138/**
Paul Bakker72f62662011-01-16 21:27:44 +0000139 * \brief Returns the list of digests supported by the generic digest module.
140 *
141 * \return a statically allocated array of digests, the last entry
142 * is 0.
143 */
144const int *md_list( void );
145
146/**
Paul Bakker17373852011-01-06 14:20:01 +0000147 * \brief Returns the message digest information associated with the
148 * given digest name.
149 *
Paul Bakker23986e52011-04-24 08:57:21 +0000150 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000151 *
152 * \return The message digest information associated with md_name or
153 * NULL if not found.
154 */
155const md_info_t *md_info_from_string( const char *md_name );
156
157/**
158 * \brief Returns the message digest information associated with the
159 * given digest type.
160 *
161 * \param md_type type of digest to search for.
162 *
163 * \return The message digest information associated with md_type or
164 * NULL if not found.
165 */
166const md_info_t *md_info_from_type( md_type_t md_type );
167
168/**
Paul Bakker562535d2011-01-20 16:42:01 +0000169 * \brief Initialises and fills the message digest context structure with
170 * the appropriate values.
171 *
172 * \param ctx context to initialise. May not be NULL. The
173 * digest-specific context (ctx->md_ctx) must be NULL. It will
174 * be allocated, and must be freed using md_free_ctx() later.
175 * \param md_info message digest to use.
176 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000177 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
178 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker562535d2011-01-20 16:42:01 +0000179 * allocation of the cipher-specific context failed.
180 */
181int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
182
183/**
184 * \brief Free the message-specific context of ctx. Freeing ctx itself
185 * remains the responsibility of the caller.
186 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000187 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000188 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000189 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
190 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000191 */
192int md_free_ctx( md_context_t *ctx );
193
194/**
Paul Bakker17373852011-01-06 14:20:01 +0000195 * \brief Returns the size of the message digest output.
196 *
197 * \param md_info message digest info
198 *
199 * \return size of the message digest output.
200 */
Paul Bakker23986e52011-04-24 08:57:21 +0000201static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000202{
203 return md_info->size;
204}
205
206/**
207 * \brief Returns the type of the message digest output.
208 *
209 * \param md_info message digest info
210 *
211 * \return type of the message digest output.
212 */
Paul Bakker23986e52011-04-24 08:57:21 +0000213static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000214{
215 return md_info->type;
216}
217
218/**
219 * \brief Returns the name of the message digest output.
220 *
221 * \param md_info message digest info
222 *
223 * \return name of the message digest output.
224 */
Paul Bakker23986e52011-04-24 08:57:21 +0000225static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000226{
227 return md_info->name;
228}
229
230/**
Paul Bakker562535d2011-01-20 16:42:01 +0000231 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000232 *
Paul Bakker562535d2011-01-20 16:42:01 +0000233 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000234 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000235 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
236 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000237 */
Paul Bakker562535d2011-01-20 16:42:01 +0000238int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000239
240/**
241 * \brief Generic message digest process buffer
242 *
243 * \param ctx Generic message digest context
244 * \param input buffer holding the datal
245 * \param ilen length of the input data
246 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000247 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
248 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000249 */
Paul Bakker23986e52011-04-24 08:57:21 +0000250int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000251
252/**
253 * \brief Generic message digest final digest
254 *
255 * \param ctx Generic message digest context
256 * \param output Generic message digest checksum result
257 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000258 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
259 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000260 */
261int md_finish( md_context_t *ctx, unsigned char *output );
262
263/**
Paul Bakker17373852011-01-06 14:20:01 +0000264 * \brief Output = message_digest( input buffer )
265 *
266 * \param md_info message digest info
267 * \param input buffer holding the data
268 * \param ilen length of the input data
269 * \param output Generic message digest checksum result
270 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000271 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
272 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000273 */
Paul Bakker23986e52011-04-24 08:57:21 +0000274int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000275 unsigned char *output );
276
277/**
278 * \brief Output = message_digest( file contents )
279 *
280 * \param md_info message digest info
281 * \param path input file name
282 * \param output generic message digest checksum result
283 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000284 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
285 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
286 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000287 */
288int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
289
290/**
291 * \brief Generic HMAC context setup
292 *
Paul Bakker17373852011-01-06 14:20:01 +0000293 * \param ctx HMAC context to be initialized
294 * \param key HMAC secret key
295 * \param keylen length of the HMAC key
296 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000297 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
298 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000299 */
Paul Bakker23986e52011-04-24 08:57:21 +0000300int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000301
302/**
303 * \brief Generic HMAC process buffer
304 *
305 * \param ctx HMAC context
306 * \param input buffer holding the data
307 * \param ilen length of the input data
308 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000309 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
310 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000311 */
Paul Bakker23986e52011-04-24 08:57:21 +0000312int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000313
314/**
315 * \brief Generic HMAC final digest
316 *
317 * \param ctx HMAC context
318 * \param output Generic HMAC checksum result
319 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000320 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
321 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000322 */
323int md_hmac_finish( md_context_t *ctx, unsigned char *output);
324
325/**
326 * \brief Generic HMAC context reset
327 *
328 * \param ctx HMAC context to be reset
329 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000330 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
331 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000332 */
333int md_hmac_reset( md_context_t *ctx );
334
335/**
336 * \brief Output = Generic_HMAC( hmac key, input buffer )
337 *
338 * \param md_info message digest info
339 * \param key HMAC secret key
340 * \param keylen length of the HMAC key
341 * \param input buffer holding the data
342 * \param ilen length of the input data
343 * \param output Generic HMAC-result
344 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000345 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
346 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000347 */
Paul Bakker23986e52011-04-24 08:57:21 +0000348int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
349 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000350 unsigned char *output );
351
352#ifdef __cplusplus
353}
354#endif
355
356#endif /* POLARSSL_MD_H */