blob: 424b89f8f95f540a333d132fc0ec64812957c3f1 [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,
Manuel Pégourié-Gonnarde4d47a62014-01-17 20:41:32 +010061 POLARSSL_MD_RMD160,
Paul Bakker17373852011-01-06 14:20:01 +000062} md_type_t;
63
Paul Bakker7db01092013-09-10 11:10:57 +020064#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000065#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020066#else
67#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
68#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000069
Paul Bakker17373852011-01-06 14:20:01 +000070/**
71 * Message digest information. Allows message digest functions to be called
72 * in a generic way.
73 */
74typedef struct {
75 /** Digest identifier */
76 md_type_t type;
77
78 /** Name of the message digest */
79 const char * name;
80
81 /** Output length of the digest function */
82 int size;
83
84 /** Digest initialisation function */
85 void (*starts_func)( void *ctx );
86
87 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000088 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000089
90 /** Digest finalisation function */
91 void (*finish_func)( void *ctx, unsigned char *output );
92
93 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000094 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000095 unsigned char *output );
96
97 /** Generic file digest function */
98 int (*file_func)( const char *path, unsigned char *output );
99
100 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +0000101 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000102
103 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +0000104 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000105
106 /** HMAC finalisation function */
107 void (*hmac_finish_func)( void *ctx, unsigned char *output);
108
109 /** HMAC context reset function */
110 void (*hmac_reset_func)( void *ctx );
111
112 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000113 void (*hmac_func)( const unsigned char *key, size_t keylen,
114 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000115 unsigned char *output );
116
117 /** Allocate a new context */
118 void * (*ctx_alloc_func)( void );
119
120 /** Free the given context */
121 void (*ctx_free_func)( void *ctx );
122
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100123 /** Internal use only */
124 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000125} md_info_t;
126
127/**
128 * Generic message digest context.
129 */
130typedef struct {
131 /** Information about the associated message digest */
132 const md_info_t *md_info;
133
134 /** Digest-specific context */
135 void *md_ctx;
136} md_context_t;
137
138#define MD_CONTEXT_T_INIT { \
139 NULL, /* md_info */ \
140 NULL, /* md_ctx */ \
141}
142
Paul Bakker17373852011-01-06 14:20:01 +0000143/**
Paul Bakker72f62662011-01-16 21:27:44 +0000144 * \brief Returns the list of digests supported by the generic digest module.
145 *
146 * \return a statically allocated array of digests, the last entry
147 * is 0.
148 */
149const int *md_list( void );
150
151/**
Paul Bakker17373852011-01-06 14:20:01 +0000152 * \brief Returns the message digest information associated with the
153 * given digest name.
154 *
Paul Bakker23986e52011-04-24 08:57:21 +0000155 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000156 *
157 * \return The message digest information associated with md_name or
158 * NULL if not found.
159 */
160const md_info_t *md_info_from_string( const char *md_name );
161
162/**
163 * \brief Returns the message digest information associated with the
164 * given digest type.
165 *
166 * \param md_type type of digest to search for.
167 *
168 * \return The message digest information associated with md_type or
169 * NULL if not found.
170 */
171const md_info_t *md_info_from_type( md_type_t md_type );
172
173/**
Paul Bakker562535d2011-01-20 16:42:01 +0000174 * \brief Initialises and fills the message digest context structure with
175 * the appropriate values.
176 *
177 * \param ctx context to initialise. May not be NULL. The
178 * digest-specific context (ctx->md_ctx) must be NULL. It will
179 * be allocated, and must be freed using md_free_ctx() later.
180 * \param md_info message digest to use.
181 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000182 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
183 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000184 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000185 */
186int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
187
188/**
189 * \brief Free the message-specific context of ctx. Freeing ctx itself
190 * remains the responsibility of the caller.
191 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000192 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000193 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000194 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
195 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000196 */
197int md_free_ctx( md_context_t *ctx );
198
199/**
Paul Bakker17373852011-01-06 14:20:01 +0000200 * \brief Returns the size of the message digest output.
201 *
202 * \param md_info message digest info
203 *
204 * \return size of the message digest output.
205 */
Paul Bakker23986e52011-04-24 08:57:21 +0000206static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000207{
Paul Bakkerc295b832013-04-02 11:13:39 +0200208 if( md_info == NULL )
209 return( 0 );
210
Paul Bakker17373852011-01-06 14:20:01 +0000211 return md_info->size;
212}
213
214/**
215 * \brief Returns the type of the message digest output.
216 *
217 * \param md_info message digest info
218 *
219 * \return type of the message digest output.
220 */
Paul Bakker23986e52011-04-24 08:57:21 +0000221static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000222{
Paul Bakkerc295b832013-04-02 11:13:39 +0200223 if( md_info == NULL )
224 return( POLARSSL_MD_NONE );
225
Paul Bakker17373852011-01-06 14:20:01 +0000226 return md_info->type;
227}
228
229/**
230 * \brief Returns the name of the message digest output.
231 *
232 * \param md_info message digest info
233 *
234 * \return name of the message digest output.
235 */
Paul Bakker23986e52011-04-24 08:57:21 +0000236static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000237{
Paul Bakkerc295b832013-04-02 11:13:39 +0200238 if( md_info == NULL )
239 return( NULL );
240
Paul Bakker17373852011-01-06 14:20:01 +0000241 return md_info->name;
242}
243
244/**
Paul Bakker562535d2011-01-20 16:42:01 +0000245 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000246 *
Paul Bakker562535d2011-01-20 16:42:01 +0000247 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000248 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000249 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
250 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000251 */
Paul Bakker562535d2011-01-20 16:42:01 +0000252int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000253
254/**
255 * \brief Generic message digest process buffer
256 *
257 * \param ctx Generic message digest context
258 * \param input buffer holding the datal
259 * \param ilen length of the input data
260 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000261 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
262 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000263 */
Paul Bakker23986e52011-04-24 08:57:21 +0000264int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000265
266/**
267 * \brief Generic message digest final digest
268 *
269 * \param ctx Generic message digest context
270 * \param output Generic message digest checksum result
271 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000272 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
273 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000274 */
275int md_finish( md_context_t *ctx, unsigned char *output );
276
277/**
Paul Bakker17373852011-01-06 14:20:01 +0000278 * \brief Output = message_digest( input buffer )
279 *
280 * \param md_info message digest info
281 * \param input buffer holding the data
282 * \param ilen length of the input data
283 * \param output Generic message digest checksum result
284 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000285 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
286 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000287 */
Paul Bakker23986e52011-04-24 08:57:21 +0000288int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000289 unsigned char *output );
290
291/**
292 * \brief Output = message_digest( file contents )
293 *
294 * \param md_info message digest info
295 * \param path input file name
296 * \param output generic message digest checksum result
297 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000298 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
299 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
300 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000301 */
302int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
303
304/**
305 * \brief Generic HMAC context setup
306 *
Paul Bakker17373852011-01-06 14:20:01 +0000307 * \param ctx HMAC context to be initialized
308 * \param key HMAC secret key
309 * \param keylen length of the HMAC key
310 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000311 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
312 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000313 */
Paul Bakker23986e52011-04-24 08:57:21 +0000314int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000315
316/**
317 * \brief Generic HMAC process buffer
318 *
319 * \param ctx HMAC context
320 * \param input buffer holding the data
321 * \param ilen length of the input data
322 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000323 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
324 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000325 */
Paul Bakker23986e52011-04-24 08:57:21 +0000326int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000327
328/**
329 * \brief Generic HMAC final digest
330 *
331 * \param ctx HMAC context
332 * \param output Generic HMAC checksum result
333 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000334 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
335 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000336 */
337int md_hmac_finish( md_context_t *ctx, unsigned char *output);
338
339/**
340 * \brief Generic HMAC context reset
341 *
342 * \param ctx HMAC context to be reset
343 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000344 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
345 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000346 */
347int md_hmac_reset( md_context_t *ctx );
348
349/**
350 * \brief Output = Generic_HMAC( hmac key, input buffer )
351 *
352 * \param md_info message digest info
353 * \param key HMAC secret key
354 * \param keylen length of the HMAC key
355 * \param input buffer holding the data
356 * \param ilen length of the input data
357 * \param output Generic HMAC-result
358 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000359 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
360 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000361 */
Paul Bakker23986e52011-04-24 08:57:21 +0000362int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
363 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000364 unsigned char *output );
365
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100366/* Internal use */
367int md_process( md_context_t *ctx, const unsigned char *data );
368
Paul Bakker17373852011-01-06 14:20:01 +0000369#ifdef __cplusplus
370}
371#endif
372
373#endif /* POLARSSL_MD_H */