blob: 303aee8209505d27ffc38d9ee4cc6d2b8d188400 [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001/**
2 * \file md.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Paul Bakker17373852011-01-06 14:20:01 +00004 * \brief Generic message digest wrapper
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
Paul Bakker17373852011-01-06 14:20:01 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
Paul Bakker17373852011-01-06 14:20:01 +000026#ifndef POLARSSL_MD_H
27#define POLARSSL_MD_H
28
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000030
Paul Bakker09b1ec62011-07-27 16:28:54 +000031#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000032#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000033#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000034#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000035#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000036#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000037#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000038
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000040#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
41#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000042#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker17373852011-01-06 14:20:01 +000048typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000049 POLARSSL_MD_NONE=0,
50 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000051 POLARSSL_MD_MD4,
52 POLARSSL_MD_MD5,
53 POLARSSL_MD_SHA1,
54 POLARSSL_MD_SHA224,
55 POLARSSL_MD_SHA256,
56 POLARSSL_MD_SHA384,
57 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010058 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000059} md_type_t;
60
Paul Bakker7db01092013-09-10 11:10:57 +020061#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000062#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020063#else
64#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
65#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000066
Paul Bakker17373852011-01-06 14:20:01 +000067/**
68 * Message digest information. Allows message digest functions to be called
69 * in a generic way.
70 */
71typedef struct {
72 /** Digest identifier */
73 md_type_t type;
74
75 /** Name of the message digest */
76 const char * name;
77
78 /** Output length of the digest function */
79 int size;
80
81 /** Digest initialisation function */
82 void (*starts_func)( void *ctx );
83
84 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000085 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000086
87 /** Digest finalisation function */
88 void (*finish_func)( void *ctx, unsigned char *output );
89
90 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000091 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020092 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +000093
94 /** Generic file digest function */
95 int (*file_func)( const char *path, unsigned char *output );
96
97 /** HMAC Initialisation function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020098 void (*hmac_starts_func)( void *ctx, const unsigned char *key,
99 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000100
101 /** HMAC update function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200102 void (*hmac_update_func)( void *ctx, const unsigned char *input,
103 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,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200113 const unsigned char *input, size_t ilen,
114 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000115
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 Bakker84bbeb52014-07-01 14:53:22 +0200173 * \brief Initialize a md_context (as NONE)
174 */
175void md_init( md_context_t *ctx );
176
177/**
178 * \brief Free and clear the message-specific context of ctx.
179 * Freeing ctx itself remains the responsibility of the
180 * caller.
181 */
182void md_free( md_context_t *ctx );
183
184/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200185 * \brief Initialises and fills the message digest context structure
186 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000187 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200188 * \note Currently also clears structure. In future versions you
189 * will be required to call md_init() on the structure
190 * first.
191 *
Paul Bakker562535d2011-01-20 16:42:01 +0000192 * \param ctx context to initialise. May not be NULL. The
193 * digest-specific context (ctx->md_ctx) must be NULL. It will
194 * be allocated, and must be freed using md_free_ctx() later.
195 * \param md_info message digest to use.
196 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000197 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
198 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000199 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000200 */
201int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
202
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100203#if ! defined(POLARSSL_DEPRECATED_REMOVED)
204#if defined(POLARSSL_DEPRECATED_WARNING)
205#define DEPRECATED __attribute__((deprecated))
206#else
207#define DEPRECATED
208#endif
Paul Bakker562535d2011-01-20 16:42:01 +0000209/**
210 * \brief Free the message-specific context of ctx. Freeing ctx itself
211 * remains the responsibility of the caller.
212 *
Manuel Pégourié-Gonnard71432842015-03-20 16:19:35 +0000213 * \deprecated Use md_free() instead
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000215 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000216 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200217 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000218 */
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100219int md_free_ctx( md_context_t *ctx ) DEPRECATED;
220#undef DEPRECATED
221#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker562535d2011-01-20 16:42:01 +0000222
223/**
Paul Bakker17373852011-01-06 14:20:01 +0000224 * \brief Returns the size of the message digest output.
225 *
226 * \param md_info message digest info
227 *
228 * \return size of the message digest output.
229 */
Paul Bakker23986e52011-04-24 08:57:21 +0000230static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000231{
Paul Bakkerc295b832013-04-02 11:13:39 +0200232 if( md_info == NULL )
233 return( 0 );
234
Paul Bakker17373852011-01-06 14:20:01 +0000235 return md_info->size;
236}
237
238/**
239 * \brief Returns the type of the message digest output.
240 *
241 * \param md_info message digest info
242 *
243 * \return type of the message digest output.
244 */
Paul Bakker23986e52011-04-24 08:57:21 +0000245static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000246{
Paul Bakkerc295b832013-04-02 11:13:39 +0200247 if( md_info == NULL )
248 return( POLARSSL_MD_NONE );
249
Paul Bakker17373852011-01-06 14:20:01 +0000250 return md_info->type;
251}
252
253/**
254 * \brief Returns the name of the message digest output.
255 *
256 * \param md_info message digest info
257 *
258 * \return name of the message digest output.
259 */
Paul Bakker23986e52011-04-24 08:57:21 +0000260static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000261{
Paul Bakkerc295b832013-04-02 11:13:39 +0200262 if( md_info == NULL )
263 return( NULL );
264
Paul Bakker17373852011-01-06 14:20:01 +0000265 return md_info->name;
266}
267
268/**
Paul Bakker562535d2011-01-20 16:42:01 +0000269 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000270 *
Paul Bakker562535d2011-01-20 16:42:01 +0000271 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000272 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000273 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
274 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000275 */
Paul Bakker562535d2011-01-20 16:42:01 +0000276int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000277
278/**
279 * \brief Generic message digest process buffer
280 *
281 * \param ctx Generic message digest context
282 * \param input buffer holding the datal
283 * \param ilen length of the input data
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_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000289
290/**
291 * \brief Generic message digest final digest
292 *
293 * \param ctx Generic message digest context
294 * \param output Generic message digest checksum result
295 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000296 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
297 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000298 */
299int md_finish( md_context_t *ctx, unsigned char *output );
300
301/**
Paul Bakker17373852011-01-06 14:20:01 +0000302 * \brief Output = message_digest( input buffer )
303 *
304 * \param md_info message digest info
305 * \param input buffer holding the data
306 * \param ilen length of the input data
307 * \param output Generic message digest checksum result
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( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000313 unsigned char *output );
314
315/**
316 * \brief Output = message_digest( file contents )
317 *
318 * \param md_info message digest info
319 * \param path input file name
320 * \param output generic message digest checksum result
321 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000322 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
323 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
324 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000325 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200326int md_file( const md_info_t *md_info, const char *path,
327 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000328
329/**
330 * \brief Generic HMAC context setup
331 *
Paul Bakker17373852011-01-06 14:20:01 +0000332 * \param ctx HMAC context to be initialized
333 * \param key HMAC secret key
334 * \param keylen length of the HMAC key
335 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000336 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
337 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000338 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200339int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
340 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000341
342/**
343 * \brief Generic HMAC process buffer
344 *
345 * \param ctx HMAC context
346 * \param input buffer holding the data
347 * \param ilen length of the input data
348 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000349 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
350 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000351 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200352int md_hmac_update( md_context_t *ctx, const unsigned char *input,
353 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000354
355/**
356 * \brief Generic HMAC final digest
357 *
358 * \param ctx HMAC context
359 * \param output Generic HMAC checksum result
360 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000361 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
362 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000363 */
364int md_hmac_finish( md_context_t *ctx, unsigned char *output);
365
366/**
367 * \brief Generic HMAC context reset
368 *
369 * \param ctx HMAC context to be reset
370 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000371 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
372 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000373 */
374int md_hmac_reset( md_context_t *ctx );
375
376/**
377 * \brief Output = Generic_HMAC( hmac key, input buffer )
378 *
379 * \param md_info message digest info
380 * \param key HMAC secret key
381 * \param keylen length of the HMAC key
382 * \param input buffer holding the data
383 * \param ilen length of the input data
384 * \param output Generic HMAC-result
385 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000386 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
387 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000388 */
Paul Bakker23986e52011-04-24 08:57:21 +0000389int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
390 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000391 unsigned char *output );
392
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100393/* Internal use */
394int md_process( md_context_t *ctx, const unsigned char *data );
395
Paul Bakker17373852011-01-06 14:20:01 +0000396#ifdef __cplusplus
397}
398#endif
399
400#endif /* POLARSSL_MD_H */