blob: eecf781139f1a506592d7632660cc16c6303466d [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 Bakker7db01092013-09-10 11:10:57 +020063#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000064#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020065#else
66#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
67#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000068
Paul Bakker17373852011-01-06 14:20:01 +000069/**
70 * Message digest information. Allows message digest functions to be called
71 * in a generic way.
72 */
73typedef struct {
74 /** Digest identifier */
75 md_type_t type;
76
77 /** Name of the message digest */
78 const char * name;
79
80 /** Output length of the digest function */
81 int size;
82
83 /** Digest initialisation function */
84 void (*starts_func)( void *ctx );
85
86 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000087 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000088
89 /** Digest finalisation function */
90 void (*finish_func)( void *ctx, unsigned char *output );
91
92 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000093 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000094 unsigned char *output );
95
96 /** Generic file digest function */
97 int (*file_func)( const char *path, unsigned char *output );
98
99 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +0000100 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000101
102 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +0000103 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000104
105 /** HMAC finalisation function */
106 void (*hmac_finish_func)( void *ctx, unsigned char *output);
107
108 /** HMAC context reset function */
109 void (*hmac_reset_func)( void *ctx );
110
111 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000112 void (*hmac_func)( const unsigned char *key, size_t keylen,
113 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000114 unsigned char *output );
115
116 /** Allocate a new context */
117 void * (*ctx_alloc_func)( void );
118
119 /** Free the given context */
120 void (*ctx_free_func)( void *ctx );
121
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100122 /** Internal use only */
123 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000124} md_info_t;
125
126/**
127 * Generic message digest context.
128 */
129typedef struct {
130 /** Information about the associated message digest */
131 const md_info_t *md_info;
132
133 /** Digest-specific context */
134 void *md_ctx;
135} md_context_t;
136
137#define MD_CONTEXT_T_INIT { \
138 NULL, /* md_info */ \
139 NULL, /* md_ctx */ \
140}
141
Paul Bakker17373852011-01-06 14:20:01 +0000142/**
Paul Bakker72f62662011-01-16 21:27:44 +0000143 * \brief Returns the list of digests supported by the generic digest module.
144 *
145 * \return a statically allocated array of digests, the last entry
146 * is 0.
147 */
148const int *md_list( void );
149
150/**
Paul Bakker17373852011-01-06 14:20:01 +0000151 * \brief Returns the message digest information associated with the
152 * given digest name.
153 *
Paul Bakker23986e52011-04-24 08:57:21 +0000154 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000155 *
156 * \return The message digest information associated with md_name or
157 * NULL if not found.
158 */
159const md_info_t *md_info_from_string( const char *md_name );
160
161/**
162 * \brief Returns the message digest information associated with the
163 * given digest type.
164 *
165 * \param md_type type of digest to search for.
166 *
167 * \return The message digest information associated with md_type or
168 * NULL if not found.
169 */
170const md_info_t *md_info_from_type( md_type_t md_type );
171
172/**
Paul Bakker562535d2011-01-20 16:42:01 +0000173 * \brief Initialises and fills the message digest context structure with
174 * the appropriate values.
175 *
176 * \param ctx context to initialise. May not be NULL. The
177 * digest-specific context (ctx->md_ctx) must be NULL. It will
178 * be allocated, and must be freed using md_free_ctx() later.
179 * \param md_info message digest to use.
180 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000181 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
182 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000183 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000184 */
185int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
186
187/**
188 * \brief Free the message-specific context of ctx. Freeing ctx itself
189 * remains the responsibility of the caller.
190 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000191 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000192 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000193 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
194 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000195 */
196int md_free_ctx( md_context_t *ctx );
197
198/**
Paul Bakker17373852011-01-06 14:20:01 +0000199 * \brief Returns the size of the message digest output.
200 *
201 * \param md_info message digest info
202 *
203 * \return size of the message digest output.
204 */
Paul Bakker23986e52011-04-24 08:57:21 +0000205static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000206{
Paul Bakkerc295b832013-04-02 11:13:39 +0200207 if( md_info == NULL )
208 return( 0 );
209
Paul Bakker17373852011-01-06 14:20:01 +0000210 return md_info->size;
211}
212
213/**
214 * \brief Returns the type of the message digest output.
215 *
216 * \param md_info message digest info
217 *
218 * \return type of the message digest output.
219 */
Paul Bakker23986e52011-04-24 08:57:21 +0000220static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000221{
Paul Bakkerc295b832013-04-02 11:13:39 +0200222 if( md_info == NULL )
223 return( POLARSSL_MD_NONE );
224
Paul Bakker17373852011-01-06 14:20:01 +0000225 return md_info->type;
226}
227
228/**
229 * \brief Returns the name of the message digest output.
230 *
231 * \param md_info message digest info
232 *
233 * \return name of the message digest output.
234 */
Paul Bakker23986e52011-04-24 08:57:21 +0000235static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000236{
Paul Bakkerc295b832013-04-02 11:13:39 +0200237 if( md_info == NULL )
238 return( NULL );
239
Paul Bakker17373852011-01-06 14:20:01 +0000240 return md_info->name;
241}
242
243/**
Paul Bakker562535d2011-01-20 16:42:01 +0000244 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000245 *
Paul Bakker562535d2011-01-20 16:42:01 +0000246 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000247 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000248 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
249 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000250 */
Paul Bakker562535d2011-01-20 16:42:01 +0000251int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000252
253/**
254 * \brief Generic message digest process buffer
255 *
256 * \param ctx Generic message digest context
257 * \param input buffer holding the datal
258 * \param ilen length of the input data
259 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000260 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
261 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000262 */
Paul Bakker23986e52011-04-24 08:57:21 +0000263int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000264
265/**
266 * \brief Generic message digest final digest
267 *
268 * \param ctx Generic message digest context
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 */
274int md_finish( md_context_t *ctx, unsigned char *output );
275
276/**
Paul Bakker17373852011-01-06 14:20:01 +0000277 * \brief Output = message_digest( input buffer )
278 *
279 * \param md_info message digest info
280 * \param input buffer holding the data
281 * \param ilen length of the input data
282 * \param output Generic message digest checksum result
283 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000284 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
285 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000286 */
Paul Bakker23986e52011-04-24 08:57:21 +0000287int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000288 unsigned char *output );
289
290/**
291 * \brief Output = message_digest( file contents )
292 *
293 * \param md_info message digest info
294 * \param path input file name
295 * \param output generic message digest checksum result
296 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000297 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
298 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
299 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000300 */
301int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
302
303/**
304 * \brief Generic HMAC context setup
305 *
Paul Bakker17373852011-01-06 14:20:01 +0000306 * \param ctx HMAC context to be initialized
307 * \param key HMAC secret key
308 * \param keylen length of the HMAC key
309 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000310 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
311 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000312 */
Paul Bakker23986e52011-04-24 08:57:21 +0000313int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000314
315/**
316 * \brief Generic HMAC process buffer
317 *
318 * \param ctx HMAC context
319 * \param input buffer holding the data
320 * \param ilen length of the input data
321 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000322 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
323 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000324 */
Paul Bakker23986e52011-04-24 08:57:21 +0000325int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000326
327/**
328 * \brief Generic HMAC final digest
329 *
330 * \param ctx HMAC context
331 * \param output Generic HMAC checksum result
332 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000333 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
334 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000335 */
336int md_hmac_finish( md_context_t *ctx, unsigned char *output);
337
338/**
339 * \brief Generic HMAC context reset
340 *
341 * \param ctx HMAC context to be reset
342 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000343 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
344 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000345 */
346int md_hmac_reset( md_context_t *ctx );
347
348/**
349 * \brief Output = Generic_HMAC( hmac key, input buffer )
350 *
351 * \param md_info message digest info
352 * \param key HMAC secret key
353 * \param keylen length of the HMAC key
354 * \param input buffer holding the data
355 * \param ilen length of the input data
356 * \param output Generic HMAC-result
357 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000358 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
359 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000360 */
Paul Bakker23986e52011-04-24 08:57:21 +0000361int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
362 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000363 unsigned char *output );
364
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100365/* Internal use */
366int md_process( md_context_t *ctx, const unsigned char *data );
367
Paul Bakker17373852011-01-06 14:20:01 +0000368#ifdef __cplusplus
369}
370#endif
371
372#endif /* POLARSSL_MD_H */