blob: 3472f80b4529d44f7e367ee57d0b60f20f681648 [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
203/**
204 * \brief Free the message-specific context of ctx. Freeing ctx itself
205 * remains the responsibility of the caller.
206 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200207 * \note Deprecated: Redirects to md_free()
208 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000209 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000210 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200211 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000212 */
213int md_free_ctx( md_context_t *ctx );
214
215/**
Paul Bakker17373852011-01-06 14:20:01 +0000216 * \brief Returns the size of the message digest output.
217 *
218 * \param md_info message digest info
219 *
220 * \return size of the message digest output.
221 */
Paul Bakker23986e52011-04-24 08:57:21 +0000222static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000223{
Paul Bakkerc295b832013-04-02 11:13:39 +0200224 if( md_info == NULL )
225 return( 0 );
226
Paul Bakker17373852011-01-06 14:20:01 +0000227 return md_info->size;
228}
229
230/**
231 * \brief Returns the type of the message digest output.
232 *
233 * \param md_info message digest info
234 *
235 * \return type of the message digest output.
236 */
Paul Bakker23986e52011-04-24 08:57:21 +0000237static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000238{
Paul Bakkerc295b832013-04-02 11:13:39 +0200239 if( md_info == NULL )
240 return( POLARSSL_MD_NONE );
241
Paul Bakker17373852011-01-06 14:20:01 +0000242 return md_info->type;
243}
244
245/**
246 * \brief Returns the name of the message digest output.
247 *
248 * \param md_info message digest info
249 *
250 * \return name of the message digest output.
251 */
Paul Bakker23986e52011-04-24 08:57:21 +0000252static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000253{
Paul Bakkerc295b832013-04-02 11:13:39 +0200254 if( md_info == NULL )
255 return( NULL );
256
Paul Bakker17373852011-01-06 14:20:01 +0000257 return md_info->name;
258}
259
260/**
Paul Bakker562535d2011-01-20 16:42:01 +0000261 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000262 *
Paul Bakker562535d2011-01-20 16:42:01 +0000263 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000264 *
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 */
Paul Bakker562535d2011-01-20 16:42:01 +0000268int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000269
270/**
271 * \brief Generic message digest process buffer
272 *
273 * \param ctx Generic message digest context
274 * \param input buffer holding the datal
275 * \param ilen length of the input data
276 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000277 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
278 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000279 */
Paul Bakker23986e52011-04-24 08:57:21 +0000280int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000281
282/**
283 * \brief Generic message digest final digest
284 *
285 * \param ctx Generic message digest context
286 * \param output Generic message digest checksum result
287 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000288 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
289 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000290 */
291int md_finish( md_context_t *ctx, unsigned char *output );
292
293/**
Paul Bakker17373852011-01-06 14:20:01 +0000294 * \brief Output = message_digest( input buffer )
295 *
296 * \param md_info message digest info
297 * \param input buffer holding the data
298 * \param ilen length of the input data
299 * \param output Generic message digest checksum result
300 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000301 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
302 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000303 */
Paul Bakker23986e52011-04-24 08:57:21 +0000304int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000305 unsigned char *output );
306
307/**
308 * \brief Output = message_digest( file contents )
309 *
310 * \param md_info message digest info
311 * \param path input file name
312 * \param output generic message digest checksum result
313 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000314 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
315 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
316 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000317 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200318int md_file( const md_info_t *md_info, const char *path,
319 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000320
321/**
322 * \brief Generic HMAC context setup
323 *
Paul Bakker17373852011-01-06 14:20:01 +0000324 * \param ctx HMAC context to be initialized
325 * \param key HMAC secret key
326 * \param keylen length of the HMAC key
327 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000328 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
329 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000330 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200331int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
332 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000333
334/**
335 * \brief Generic HMAC process buffer
336 *
337 * \param ctx HMAC context
338 * \param input buffer holding the data
339 * \param ilen length of the input data
340 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000341 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
342 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000343 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200344int md_hmac_update( md_context_t *ctx, const unsigned char *input,
345 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000346
347/**
348 * \brief Generic HMAC final digest
349 *
350 * \param ctx HMAC context
351 * \param output Generic HMAC checksum result
352 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000353 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
354 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000355 */
356int md_hmac_finish( md_context_t *ctx, unsigned char *output);
357
358/**
359 * \brief Generic HMAC context reset
360 *
361 * \param ctx HMAC context to be reset
362 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000363 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
364 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000365 */
366int md_hmac_reset( md_context_t *ctx );
367
368/**
369 * \brief Output = Generic_HMAC( hmac key, input buffer )
370 *
371 * \param md_info message digest info
372 * \param key HMAC secret key
373 * \param keylen length of the HMAC key
374 * \param input buffer holding the data
375 * \param ilen length of the input data
376 * \param output Generic HMAC-result
377 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000378 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
379 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000380 */
Paul Bakker23986e52011-04-24 08:57:21 +0000381int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
382 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000383 unsigned char *output );
384
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100385/* Internal use */
386int md_process( md_context_t *ctx, const unsigned char *data );
387
Paul Bakker17373852011-01-06 14:20:01 +0000388#ifdef __cplusplus
389}
390#endif
391
392#endif /* POLARSSL_MD_H */