blob: f7b8bd3274fa693a9dadf749121de2fa44185c7c [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 Bakker407a0da2013-06-27 14:29:21 +02008 * Copyright (C) 2006-2013, 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 Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker17373852011-01-06 14:20:01 +000051typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000052 POLARSSL_MD_NONE=0,
53 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000054 POLARSSL_MD_MD4,
55 POLARSSL_MD_MD5,
56 POLARSSL_MD_SHA1,
57 POLARSSL_MD_SHA224,
58 POLARSSL_MD_SHA256,
59 POLARSSL_MD_SHA384,
60 POLARSSL_MD_SHA512,
61} md_type_t;
62
Paul Bakker1b57b062011-01-06 15:48:19 +000063#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
64
Paul Bakker17373852011-01-06 14:20:01 +000065/**
66 * Message digest information. Allows message digest functions to be called
67 * in a generic way.
68 */
69typedef struct {
70 /** Digest identifier */
71 md_type_t type;
72
73 /** Name of the message digest */
74 const char * name;
75
76 /** Output length of the digest function */
77 int size;
78
79 /** Digest initialisation function */
80 void (*starts_func)( void *ctx );
81
82 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000083 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000084
85 /** Digest finalisation function */
86 void (*finish_func)( void *ctx, unsigned char *output );
87
88 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000089 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000090 unsigned char *output );
91
92 /** Generic file digest function */
93 int (*file_func)( const char *path, unsigned char *output );
94
95 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000096 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000097
98 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000099 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000100
101 /** HMAC finalisation function */
102 void (*hmac_finish_func)( void *ctx, unsigned char *output);
103
104 /** HMAC context reset function */
105 void (*hmac_reset_func)( void *ctx );
106
107 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000108 void (*hmac_func)( const unsigned char *key, size_t keylen,
109 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000110 unsigned char *output );
111
112 /** Allocate a new context */
113 void * (*ctx_alloc_func)( void );
114
115 /** Free the given context */
116 void (*ctx_free_func)( void *ctx );
117
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100118 /** Internal use only */
119 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000120} md_info_t;
121
122/**
123 * Generic message digest context.
124 */
125typedef struct {
126 /** Information about the associated message digest */
127 const md_info_t *md_info;
128
129 /** Digest-specific context */
130 void *md_ctx;
131} md_context_t;
132
133#define MD_CONTEXT_T_INIT { \
134 NULL, /* md_info */ \
135 NULL, /* md_ctx */ \
136}
137
Paul Bakker17373852011-01-06 14:20:01 +0000138/**
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 Bakker20281562011-11-11 10:34:04 +0000179 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000180 */
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{
Paul Bakkerc295b832013-04-02 11:13:39 +0200203 if( md_info == NULL )
204 return( 0 );
205
Paul Bakker17373852011-01-06 14:20:01 +0000206 return md_info->size;
207}
208
209/**
210 * \brief Returns the type of the message digest output.
211 *
212 * \param md_info message digest info
213 *
214 * \return type of the message digest output.
215 */
Paul Bakker23986e52011-04-24 08:57:21 +0000216static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000217{
Paul Bakkerc295b832013-04-02 11:13:39 +0200218 if( md_info == NULL )
219 return( POLARSSL_MD_NONE );
220
Paul Bakker17373852011-01-06 14:20:01 +0000221 return md_info->type;
222}
223
224/**
225 * \brief Returns the name of the message digest output.
226 *
227 * \param md_info message digest info
228 *
229 * \return name of the message digest output.
230 */
Paul Bakker23986e52011-04-24 08:57:21 +0000231static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000232{
Paul Bakkerc295b832013-04-02 11:13:39 +0200233 if( md_info == NULL )
234 return( NULL );
235
Paul Bakker17373852011-01-06 14:20:01 +0000236 return md_info->name;
237}
238
239/**
Paul Bakker562535d2011-01-20 16:42:01 +0000240 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000241 *
Paul Bakker562535d2011-01-20 16:42:01 +0000242 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000243 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000244 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
245 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000246 */
Paul Bakker562535d2011-01-20 16:42:01 +0000247int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000248
249/**
250 * \brief Generic message digest process buffer
251 *
252 * \param ctx Generic message digest context
253 * \param input buffer holding the datal
254 * \param ilen length of the input data
255 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000256 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
257 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000258 */
Paul Bakker23986e52011-04-24 08:57:21 +0000259int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000260
261/**
262 * \brief Generic message digest final digest
263 *
264 * \param ctx Generic message digest context
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 */
270int md_finish( md_context_t *ctx, unsigned char *output );
271
272/**
Paul Bakker17373852011-01-06 14:20:01 +0000273 * \brief Output = message_digest( input buffer )
274 *
275 * \param md_info message digest info
276 * \param input buffer holding the data
277 * \param ilen length of the input data
278 * \param output Generic message digest checksum result
279 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000280 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
281 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000282 */
Paul Bakker23986e52011-04-24 08:57:21 +0000283int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000284 unsigned char *output );
285
286/**
287 * \brief Output = message_digest( file contents )
288 *
289 * \param md_info message digest info
290 * \param path input file name
291 * \param output generic message digest checksum result
292 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000293 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
294 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
295 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000296 */
297int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
298
299/**
300 * \brief Generic HMAC context setup
301 *
Paul Bakker17373852011-01-06 14:20:01 +0000302 * \param ctx HMAC context to be initialized
303 * \param key HMAC secret key
304 * \param keylen length of the HMAC key
305 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000306 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
307 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000308 */
Paul Bakker23986e52011-04-24 08:57:21 +0000309int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000310
311/**
312 * \brief Generic HMAC process buffer
313 *
314 * \param ctx HMAC context
315 * \param input buffer holding the data
316 * \param ilen length of the input data
317 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000318 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
319 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000320 */
Paul Bakker23986e52011-04-24 08:57:21 +0000321int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000322
323/**
324 * \brief Generic HMAC final digest
325 *
326 * \param ctx HMAC context
327 * \param output Generic HMAC checksum result
328 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000329 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
330 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000331 */
332int md_hmac_finish( md_context_t *ctx, unsigned char *output);
333
334/**
335 * \brief Generic HMAC context reset
336 *
337 * \param ctx HMAC context to be reset
338 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000339 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
340 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000341 */
342int md_hmac_reset( md_context_t *ctx );
343
344/**
345 * \brief Output = Generic_HMAC( hmac key, input buffer )
346 *
347 * \param md_info message digest info
348 * \param key HMAC secret key
349 * \param keylen length of the HMAC key
350 * \param input buffer holding the data
351 * \param ilen length of the input data
352 * \param output Generic HMAC-result
353 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000354 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
355 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000356 */
Paul Bakker23986e52011-04-24 08:57:21 +0000357int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
358 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000359 unsigned char *output );
360
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100361/* Internal use */
362int md_process( md_context_t *ctx, const unsigned char *data );
363
Paul Bakker17373852011-01-06 14:20:01 +0000364#ifdef __cplusplus
365}
366#endif
367
368#endif /* POLARSSL_MD_H */