blob: a1d6873c8d6eaf9242a907e8a7cac518666c656b [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/**
Paul Bakker17373852011-01-06 14:20:01 +0000204 * \brief Returns the size of the message digest output.
205 *
206 * \param md_info message digest info
207 *
208 * \return size of the message digest output.
209 */
Paul Bakker23986e52011-04-24 08:57:21 +0000210static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000211{
Paul Bakkerc295b832013-04-02 11:13:39 +0200212 if( md_info == NULL )
213 return( 0 );
214
Paul Bakker17373852011-01-06 14:20:01 +0000215 return md_info->size;
216}
217
218/**
219 * \brief Returns the type of the message digest output.
220 *
221 * \param md_info message digest info
222 *
223 * \return type of the message digest output.
224 */
Paul Bakker23986e52011-04-24 08:57:21 +0000225static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000226{
Paul Bakkerc295b832013-04-02 11:13:39 +0200227 if( md_info == NULL )
228 return( POLARSSL_MD_NONE );
229
Paul Bakker17373852011-01-06 14:20:01 +0000230 return md_info->type;
231}
232
233/**
234 * \brief Returns the name of the message digest output.
235 *
236 * \param md_info message digest info
237 *
238 * \return name of the message digest output.
239 */
Paul Bakker23986e52011-04-24 08:57:21 +0000240static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000241{
Paul Bakkerc295b832013-04-02 11:13:39 +0200242 if( md_info == NULL )
243 return( NULL );
244
Paul Bakker17373852011-01-06 14:20:01 +0000245 return md_info->name;
246}
247
248/**
Paul Bakker562535d2011-01-20 16:42:01 +0000249 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000250 *
Paul Bakker562535d2011-01-20 16:42:01 +0000251 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000252 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000253 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
254 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000255 */
Paul Bakker562535d2011-01-20 16:42:01 +0000256int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000257
258/**
259 * \brief Generic message digest process buffer
260 *
261 * \param ctx Generic message digest context
262 * \param input buffer holding the datal
263 * \param ilen length of the input data
264 *
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 Bakker23986e52011-04-24 08:57:21 +0000268int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000269
270/**
271 * \brief Generic message digest final digest
272 *
273 * \param ctx Generic message digest context
274 * \param output Generic message digest checksum result
275 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000276 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
277 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000278 */
279int md_finish( md_context_t *ctx, unsigned char *output );
280
281/**
Paul Bakker17373852011-01-06 14:20:01 +0000282 * \brief Output = message_digest( input buffer )
283 *
284 * \param md_info message digest info
285 * \param input buffer holding the data
286 * \param ilen length of the input data
287 * \param output Generic message digest checksum result
288 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000289 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
290 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000291 */
Paul Bakker23986e52011-04-24 08:57:21 +0000292int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000293 unsigned char *output );
294
295/**
296 * \brief Output = message_digest( file contents )
297 *
298 * \param md_info message digest info
299 * \param path input file name
300 * \param output generic message digest checksum result
301 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000302 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
303 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
304 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000305 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200306int md_file( const md_info_t *md_info, const char *path,
307 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000308
309/**
310 * \brief Generic HMAC context setup
311 *
Paul Bakker17373852011-01-06 14:20:01 +0000312 * \param ctx HMAC context to be initialized
313 * \param key HMAC secret key
314 * \param keylen length of the HMAC key
315 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000316 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
317 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000318 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200319int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
320 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000321
322/**
323 * \brief Generic HMAC process buffer
324 *
325 * \param ctx HMAC context
326 * \param input buffer holding the data
327 * \param ilen length of the input data
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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200332int md_hmac_update( md_context_t *ctx, const unsigned char *input,
333 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000334
335/**
336 * \brief Generic HMAC final digest
337 *
338 * \param ctx HMAC context
339 * \param output Generic HMAC checksum result
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 */
344int md_hmac_finish( md_context_t *ctx, unsigned char *output);
345
346/**
347 * \brief Generic HMAC context reset
348 *
349 * \param ctx HMAC context to be reset
350 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000351 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
352 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000353 */
354int md_hmac_reset( md_context_t *ctx );
355
356/**
357 * \brief Output = Generic_HMAC( hmac key, input buffer )
358 *
359 * \param md_info message digest info
360 * \param key HMAC secret key
361 * \param keylen length of the HMAC key
362 * \param input buffer holding the data
363 * \param ilen length of the input data
364 * \param output Generic HMAC-result
365 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000366 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
367 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000368 */
Paul Bakker23986e52011-04-24 08:57:21 +0000369int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
370 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000371 unsigned char *output );
372
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100373/* Internal use */
374int md_process( md_context_t *ctx, const unsigned char *data );
375
Paul Bakker17373852011-01-06 14:20:01 +0000376#ifdef __cplusplus
377}
378#endif
379
380#endif /* POLARSSL_MD_H */