blob: ae38aadba3da39ae8199018a42175120b0314d49 [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 Bakker530927b2015-02-13 14:24:10 +01008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker17373852011-01-06 14:20:01 +000011 *
12 * 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
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Manuel Pégourié-Gonnard01234052015-10-05 11:40:01 +010031#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
32 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000033#define inline __inline
Manuel Pégourié-Gonnard01234052015-10-05 11:40:01 +010034#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000035
Paul Bakker9d781402011-05-09 16:17:09 +000036#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000037#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
38#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000039#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000040
Paul Bakker17373852011-01-06 14:20:01 +000041typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000042 POLARSSL_MD_NONE=0,
43 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000044 POLARSSL_MD_MD4,
45 POLARSSL_MD_MD5,
46 POLARSSL_MD_SHA1,
47 POLARSSL_MD_SHA224,
48 POLARSSL_MD_SHA256,
49 POLARSSL_MD_SHA384,
50 POLARSSL_MD_SHA512,
51} md_type_t;
52
Paul Bakker1b57b062011-01-06 15:48:19 +000053#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
54
Paul Bakker17373852011-01-06 14:20:01 +000055/**
56 * Message digest information. Allows message digest functions to be called
57 * in a generic way.
58 */
59typedef struct {
60 /** Digest identifier */
61 md_type_t type;
62
63 /** Name of the message digest */
64 const char * name;
65
66 /** Output length of the digest function */
67 int size;
68
69 /** Digest initialisation function */
70 void (*starts_func)( void *ctx );
71
72 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000073 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000074
75 /** Digest finalisation function */
76 void (*finish_func)( void *ctx, unsigned char *output );
77
78 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000079 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000080 unsigned char *output );
81
82 /** Generic file digest function */
83 int (*file_func)( const char *path, unsigned char *output );
84
85 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000086 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000087
88 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000089 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000090
91 /** HMAC finalisation function */
92 void (*hmac_finish_func)( void *ctx, unsigned char *output);
93
94 /** HMAC context reset function */
95 void (*hmac_reset_func)( void *ctx );
96
97 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +000098 void (*hmac_func)( const unsigned char *key, size_t keylen,
99 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000100 unsigned char *output );
101
102 /** Allocate a new context */
103 void * (*ctx_alloc_func)( void );
104
105 /** Free the given context */
106 void (*ctx_free_func)( void *ctx );
107
108} md_info_t;
109
110/**
111 * Generic message digest context.
112 */
113typedef struct {
114 /** Information about the associated message digest */
115 const md_info_t *md_info;
116
117 /** Digest-specific context */
118 void *md_ctx;
119} md_context_t;
120
121#define MD_CONTEXT_T_INIT { \
122 NULL, /* md_info */ \
123 NULL, /* md_ctx */ \
124}
125
126#ifdef __cplusplus
127extern "C" {
128#endif
129
130/**
Paul Bakker72f62662011-01-16 21:27:44 +0000131 * \brief Returns the list of digests supported by the generic digest module.
132 *
133 * \return a statically allocated array of digests, the last entry
134 * is 0.
135 */
136const int *md_list( void );
137
138/**
Paul Bakker17373852011-01-06 14:20:01 +0000139 * \brief Returns the message digest information associated with the
140 * given digest name.
141 *
Paul Bakker23986e52011-04-24 08:57:21 +0000142 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000143 *
144 * \return The message digest information associated with md_name or
145 * NULL if not found.
146 */
147const md_info_t *md_info_from_string( const char *md_name );
148
149/**
150 * \brief Returns the message digest information associated with the
151 * given digest type.
152 *
153 * \param md_type type of digest to search for.
154 *
155 * \return The message digest information associated with md_type or
156 * NULL if not found.
157 */
158const md_info_t *md_info_from_type( md_type_t md_type );
159
160/**
Paul Bakker562535d2011-01-20 16:42:01 +0000161 * \brief Initialises and fills the message digest context structure with
162 * the appropriate values.
163 *
164 * \param ctx context to initialise. May not be NULL. The
165 * digest-specific context (ctx->md_ctx) must be NULL. It will
166 * be allocated, and must be freed using md_free_ctx() later.
167 * \param md_info message digest to use.
168 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000169 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
170 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000171 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000172 */
173int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
174
175/**
176 * \brief Free the message-specific context of ctx. Freeing ctx itself
177 * remains the responsibility of the caller.
178 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000179 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000180 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000181 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
182 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000183 */
184int md_free_ctx( md_context_t *ctx );
185
186/**
Paul Bakker17373852011-01-06 14:20:01 +0000187 * \brief Returns the size of the message digest output.
188 *
189 * \param md_info message digest info
190 *
191 * \return size of the message digest output.
192 */
Paul Bakker23986e52011-04-24 08:57:21 +0000193static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000194{
Paul Bakkercecfd952013-04-12 13:18:07 +0200195 if( md_info == NULL )
196 return( 0 );
197
Paul Bakker17373852011-01-06 14:20:01 +0000198 return md_info->size;
199}
200
201/**
202 * \brief Returns the type of the message digest output.
203 *
204 * \param md_info message digest info
205 *
206 * \return type of the message digest output.
207 */
Paul Bakker23986e52011-04-24 08:57:21 +0000208static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000209{
Paul Bakkercecfd952013-04-12 13:18:07 +0200210 if( md_info == NULL )
211 return( POLARSSL_MD_NONE );
212
Paul Bakker17373852011-01-06 14:20:01 +0000213 return md_info->type;
214}
215
216/**
217 * \brief Returns the name of the message digest output.
218 *
219 * \param md_info message digest info
220 *
221 * \return name of the message digest output.
222 */
Paul Bakker23986e52011-04-24 08:57:21 +0000223static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000224{
Paul Bakkercecfd952013-04-12 13:18:07 +0200225 if( md_info == NULL )
226 return( NULL );
227
Paul Bakker17373852011-01-06 14:20:01 +0000228 return md_info->name;
229}
230
231/**
Paul Bakker562535d2011-01-20 16:42:01 +0000232 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000233 *
Paul Bakker562535d2011-01-20 16:42:01 +0000234 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000235 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000236 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
237 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000238 */
Paul Bakker562535d2011-01-20 16:42:01 +0000239int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000240
241/**
242 * \brief Generic message digest process buffer
243 *
244 * \param ctx Generic message digest context
245 * \param input buffer holding the datal
246 * \param ilen length of the input data
247 *
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 Bakker23986e52011-04-24 08:57:21 +0000251int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000252
253/**
254 * \brief Generic message digest final digest
255 *
256 * \param ctx Generic message digest context
257 * \param output Generic message digest checksum result
258 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000259 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
260 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000261 */
262int md_finish( md_context_t *ctx, unsigned char *output );
263
264/**
Paul Bakker17373852011-01-06 14:20:01 +0000265 * \brief Output = message_digest( input buffer )
266 *
267 * \param md_info message digest info
268 * \param input buffer holding the data
269 * \param ilen length of the input data
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 */
Paul Bakker23986e52011-04-24 08:57:21 +0000275int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000276 unsigned char *output );
277
278/**
279 * \brief Output = message_digest( file contents )
280 *
281 * \param md_info message digest info
282 * \param path input file name
283 * \param output generic message digest checksum result
284 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000285 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
286 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
287 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000288 */
289int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
290
291/**
292 * \brief Generic HMAC context setup
293 *
Paul Bakker17373852011-01-06 14:20:01 +0000294 * \param ctx HMAC context to be initialized
295 * \param key HMAC secret key
296 * \param keylen length of the HMAC key
297 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000298 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
299 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000300 */
Paul Bakker23986e52011-04-24 08:57:21 +0000301int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000302
303/**
304 * \brief Generic HMAC process buffer
305 *
306 * \param ctx HMAC context
307 * \param input buffer holding the data
308 * \param ilen length of the input data
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_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000314
315/**
316 * \brief Generic HMAC final digest
317 *
318 * \param ctx HMAC context
319 * \param output Generic HMAC checksum result
320 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000321 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
322 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000323 */
324int md_hmac_finish( md_context_t *ctx, unsigned char *output);
325
326/**
327 * \brief Generic HMAC context reset
328 *
329 * \param ctx HMAC context to be reset
330 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000331 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
332 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000333 */
334int md_hmac_reset( md_context_t *ctx );
335
336/**
337 * \brief Output = Generic_HMAC( hmac key, input buffer )
338 *
339 * \param md_info message digest info
340 * \param key HMAC secret key
341 * \param keylen length of the HMAC key
342 * \param input buffer holding the data
343 * \param ilen length of the input data
344 * \param output Generic HMAC-result
345 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000346 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
347 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000348 */
Paul Bakker23986e52011-04-24 08:57:21 +0000349int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
350 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000351 unsigned char *output );
352
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* POLARSSL_MD_H */