blob: 6a1bdd4113992291cfe8229c97aa8794926d958b [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 *
Paul Bakker20281562011-11-11 10:34:04 +00008 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker17373852011-01-06 14:20:01 +00009 *
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 */
Paul Bakker17373852011-01-06 14:20:01 +000029#ifndef POLARSSL_MD_H
30#define POLARSSL_MD_H
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker09b1ec62011-07-27 16:28:54 +000034#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000035#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000036#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000037#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000038#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000039#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000040#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000041
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000043#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
44#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000045#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000046
Paul Bakker17373852011-01-06 14:20:01 +000047typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000048 POLARSSL_MD_NONE=0,
49 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000050 POLARSSL_MD_MD4,
51 POLARSSL_MD_MD5,
52 POLARSSL_MD_SHA1,
53 POLARSSL_MD_SHA224,
54 POLARSSL_MD_SHA256,
55 POLARSSL_MD_SHA384,
56 POLARSSL_MD_SHA512,
57} md_type_t;
58
Paul Bakker1b57b062011-01-06 15:48:19 +000059#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
60
Paul Bakker17373852011-01-06 14:20:01 +000061/**
62 * Message digest information. Allows message digest functions to be called
63 * in a generic way.
64 */
65typedef struct {
66 /** Digest identifier */
67 md_type_t type;
68
69 /** Name of the message digest */
70 const char * name;
71
72 /** Output length of the digest function */
73 int size;
74
75 /** Digest initialisation function */
76 void (*starts_func)( void *ctx );
77
78 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000079 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000080
81 /** Digest finalisation function */
82 void (*finish_func)( void *ctx, unsigned char *output );
83
84 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000085 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000086 unsigned char *output );
87
88 /** Generic file digest function */
89 int (*file_func)( const char *path, unsigned char *output );
90
91 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000092 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000093
94 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000095 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000096
97 /** HMAC finalisation function */
98 void (*hmac_finish_func)( void *ctx, unsigned char *output);
99
100 /** HMAC context reset function */
101 void (*hmac_reset_func)( void *ctx );
102
103 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000104 void (*hmac_func)( const unsigned char *key, size_t keylen,
105 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000106 unsigned char *output );
107
108 /** Allocate a new context */
109 void * (*ctx_alloc_func)( void );
110
111 /** Free the given context */
112 void (*ctx_free_func)( void *ctx );
113
114} md_info_t;
115
116/**
117 * Generic message digest context.
118 */
119typedef struct {
120 /** Information about the associated message digest */
121 const md_info_t *md_info;
122
123 /** Digest-specific context */
124 void *md_ctx;
125} md_context_t;
126
127#define MD_CONTEXT_T_INIT { \
128 NULL, /* md_info */ \
129 NULL, /* md_ctx */ \
130}
131
132#ifdef __cplusplus
133extern "C" {
134#endif
135
136/**
Paul Bakker72f62662011-01-16 21:27:44 +0000137 * \brief Returns the list of digests supported by the generic digest module.
138 *
139 * \return a statically allocated array of digests, the last entry
140 * is 0.
141 */
142const int *md_list( void );
143
144/**
Paul Bakker17373852011-01-06 14:20:01 +0000145 * \brief Returns the message digest information associated with the
146 * given digest name.
147 *
Paul Bakker23986e52011-04-24 08:57:21 +0000148 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000149 *
150 * \return The message digest information associated with md_name or
151 * NULL if not found.
152 */
153const md_info_t *md_info_from_string( const char *md_name );
154
155/**
156 * \brief Returns the message digest information associated with the
157 * given digest type.
158 *
159 * \param md_type type of digest to search for.
160 *
161 * \return The message digest information associated with md_type or
162 * NULL if not found.
163 */
164const md_info_t *md_info_from_type( md_type_t md_type );
165
166/**
Paul Bakker562535d2011-01-20 16:42:01 +0000167 * \brief Initialises and fills the message digest context structure with
168 * the appropriate values.
169 *
170 * \param ctx context to initialise. May not be NULL. The
171 * digest-specific context (ctx->md_ctx) must be NULL. It will
172 * be allocated, and must be freed using md_free_ctx() later.
173 * \param md_info message digest to use.
174 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000175 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
176 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000177 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000178 */
179int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
180
181/**
182 * \brief Free the message-specific context of ctx. Freeing ctx itself
183 * remains the responsibility of the caller.
184 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000185 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000186 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000187 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
188 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000189 */
190int md_free_ctx( md_context_t *ctx );
191
192/**
Paul Bakker17373852011-01-06 14:20:01 +0000193 * \brief Returns the size of the message digest output.
194 *
195 * \param md_info message digest info
196 *
197 * \return size of the message digest output.
198 */
Paul Bakker23986e52011-04-24 08:57:21 +0000199static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000200{
Paul Bakkercecfd952013-04-12 13:18:07 +0200201 if( md_info == NULL )
202 return( 0 );
203
Paul Bakker17373852011-01-06 14:20:01 +0000204 return md_info->size;
205}
206
207/**
208 * \brief Returns the type of the message digest output.
209 *
210 * \param md_info message digest info
211 *
212 * \return type of the message digest output.
213 */
Paul Bakker23986e52011-04-24 08:57:21 +0000214static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000215{
Paul Bakkercecfd952013-04-12 13:18:07 +0200216 if( md_info == NULL )
217 return( POLARSSL_MD_NONE );
218
Paul Bakker17373852011-01-06 14:20:01 +0000219 return md_info->type;
220}
221
222/**
223 * \brief Returns the name of the message digest output.
224 *
225 * \param md_info message digest info
226 *
227 * \return name of the message digest output.
228 */
Paul Bakker23986e52011-04-24 08:57:21 +0000229static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000230{
Paul Bakkercecfd952013-04-12 13:18:07 +0200231 if( md_info == NULL )
232 return( NULL );
233
Paul Bakker17373852011-01-06 14:20:01 +0000234 return md_info->name;
235}
236
237/**
Paul Bakker562535d2011-01-20 16:42:01 +0000238 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000239 *
Paul Bakker562535d2011-01-20 16:42:01 +0000240 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000241 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000242 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
243 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000244 */
Paul Bakker562535d2011-01-20 16:42:01 +0000245int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000246
247/**
248 * \brief Generic message digest process buffer
249 *
250 * \param ctx Generic message digest context
251 * \param input buffer holding the datal
252 * \param ilen length of the input data
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 */
Paul Bakker23986e52011-04-24 08:57:21 +0000257int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000258
259/**
260 * \brief Generic message digest final digest
261 *
262 * \param ctx Generic message digest context
263 * \param output Generic message digest checksum result
264 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000265 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
266 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000267 */
268int md_finish( md_context_t *ctx, unsigned char *output );
269
270/**
Paul Bakker17373852011-01-06 14:20:01 +0000271 * \brief Output = message_digest( input buffer )
272 *
273 * \param md_info message digest info
274 * \param input buffer holding the data
275 * \param ilen length of the input data
276 * \param output Generic message digest checksum result
277 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000278 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
279 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000280 */
Paul Bakker23986e52011-04-24 08:57:21 +0000281int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000282 unsigned char *output );
283
284/**
285 * \brief Output = message_digest( file contents )
286 *
287 * \param md_info message digest info
288 * \param path input file name
289 * \param output generic message digest checksum result
290 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000291 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
292 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
293 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000294 */
295int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
296
297/**
298 * \brief Generic HMAC context setup
299 *
Paul Bakker17373852011-01-06 14:20:01 +0000300 * \param ctx HMAC context to be initialized
301 * \param key HMAC secret key
302 * \param keylen length of the HMAC key
303 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000304 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
305 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000306 */
Paul Bakker23986e52011-04-24 08:57:21 +0000307int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000308
309/**
310 * \brief Generic HMAC process buffer
311 *
312 * \param ctx HMAC context
313 * \param input buffer holding the data
314 * \param ilen length of the input data
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 */
Paul Bakker23986e52011-04-24 08:57:21 +0000319int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000320
321/**
322 * \brief Generic HMAC final digest
323 *
324 * \param ctx HMAC context
325 * \param output Generic HMAC checksum result
326 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000327 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
328 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000329 */
330int md_hmac_finish( md_context_t *ctx, unsigned char *output);
331
332/**
333 * \brief Generic HMAC context reset
334 *
335 * \param ctx HMAC context to be reset
336 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000337 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
338 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000339 */
340int md_hmac_reset( md_context_t *ctx );
341
342/**
343 * \brief Output = Generic_HMAC( hmac key, input buffer )
344 *
345 * \param md_info message digest info
346 * \param key HMAC secret key
347 * \param keylen length of the HMAC key
348 * \param input buffer holding the data
349 * \param ilen length of the input data
350 * \param output Generic HMAC-result
351 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000352 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
353 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000354 */
Paul Bakker23986e52011-04-24 08:57:21 +0000355int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
356 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000357 unsigned char *output );
358
359#ifdef __cplusplus
360}
361#endif
362
363#endif /* POLARSSL_MD_H */