blob: fc7482a4b405a2d30fbc190231bb49ec59cb7080 [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
Manuel Pégourié-Gonnard20607bb2015-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é-Gonnard20607bb2015-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 Bakker407a0da2013-06-27 14:29:21 +020041#ifdef __cplusplus
42extern "C" {
43#endif
44
Paul Bakker17373852011-01-06 14:20:01 +000045typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000046 POLARSSL_MD_NONE=0,
47 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000048 POLARSSL_MD_MD4,
49 POLARSSL_MD_MD5,
50 POLARSSL_MD_SHA1,
51 POLARSSL_MD_SHA224,
52 POLARSSL_MD_SHA256,
53 POLARSSL_MD_SHA384,
54 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010055 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000056} md_type_t;
57
Paul Bakker7db01092013-09-10 11:10:57 +020058#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000059#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020060#else
61#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
62#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063
Paul Bakker17373852011-01-06 14:20:01 +000064/**
65 * Message digest information. Allows message digest functions to be called
66 * in a generic way.
67 */
68typedef struct {
69 /** Digest identifier */
70 md_type_t type;
71
72 /** Name of the message digest */
73 const char * name;
74
75 /** Output length of the digest function */
76 int size;
77
78 /** Digest initialisation function */
79 void (*starts_func)( void *ctx );
80
81 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000082 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000083
84 /** Digest finalisation function */
85 void (*finish_func)( void *ctx, unsigned char *output );
86
87 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000088 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020089 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +000090
91 /** Generic file digest function */
92 int (*file_func)( const char *path, unsigned char *output );
93
94 /** HMAC Initialisation function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020095 void (*hmac_starts_func)( void *ctx, const unsigned char *key,
96 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000097
98 /** HMAC update function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020099 void (*hmac_update_func)( void *ctx, const unsigned char *input,
100 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000101
102 /** HMAC finalisation function */
103 void (*hmac_finish_func)( void *ctx, unsigned char *output);
104
105 /** HMAC context reset function */
106 void (*hmac_reset_func)( void *ctx );
107
108 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000109 void (*hmac_func)( const unsigned char *key, size_t keylen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200110 const unsigned char *input, size_t ilen,
111 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000112
113 /** Allocate a new context */
114 void * (*ctx_alloc_func)( void );
115
116 /** Free the given context */
117 void (*ctx_free_func)( void *ctx );
118
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100119 /** Internal use only */
120 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000121} md_info_t;
122
123/**
124 * Generic message digest context.
125 */
126typedef struct {
127 /** Information about the associated message digest */
128 const md_info_t *md_info;
129
130 /** Digest-specific context */
131 void *md_ctx;
132} md_context_t;
133
134#define MD_CONTEXT_T_INIT { \
135 NULL, /* md_info */ \
136 NULL, /* md_ctx */ \
137}
138
Paul Bakker17373852011-01-06 14:20:01 +0000139/**
Paul Bakker72f62662011-01-16 21:27:44 +0000140 * \brief Returns the list of digests supported by the generic digest module.
141 *
142 * \return a statically allocated array of digests, the last entry
143 * is 0.
144 */
145const int *md_list( void );
146
147/**
Paul Bakker17373852011-01-06 14:20:01 +0000148 * \brief Returns the message digest information associated with the
149 * given digest name.
150 *
Paul Bakker23986e52011-04-24 08:57:21 +0000151 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000152 *
153 * \return The message digest information associated with md_name or
154 * NULL if not found.
155 */
156const md_info_t *md_info_from_string( const char *md_name );
157
158/**
159 * \brief Returns the message digest information associated with the
160 * given digest type.
161 *
162 * \param md_type type of digest to search for.
163 *
164 * \return The message digest information associated with md_type or
165 * NULL if not found.
166 */
167const md_info_t *md_info_from_type( md_type_t md_type );
168
169/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200170 * \brief Initialize a md_context (as NONE)
171 */
172void md_init( md_context_t *ctx );
173
174/**
175 * \brief Free and clear the message-specific context of ctx.
176 * Freeing ctx itself remains the responsibility of the
177 * caller.
178 */
179void md_free( md_context_t *ctx );
180
181/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200182 * \brief Initialises and fills the message digest context structure
183 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000184 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185 * \note Currently also clears structure. In future versions you
186 * will be required to call md_init() on the structure
187 * first.
188 *
Paul Bakker562535d2011-01-20 16:42:01 +0000189 * \param ctx context to initialise. May not be NULL. The
190 * digest-specific context (ctx->md_ctx) must be NULL. It will
191 * be allocated, and must be freed using md_free_ctx() later.
192 * \param md_info message digest to use.
193 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000194 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
195 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000196 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000197 */
198int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
199
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100200#if ! defined(POLARSSL_DEPRECATED_REMOVED)
201#if defined(POLARSSL_DEPRECATED_WARNING)
202#define DEPRECATED __attribute__((deprecated))
203#else
204#define DEPRECATED
205#endif
Paul Bakker562535d2011-01-20 16:42:01 +0000206/**
207 * \brief Free the message-specific context of ctx. Freeing ctx itself
208 * remains the responsibility of the caller.
209 *
Manuel Pégourié-Gonnard71432842015-03-20 16:19:35 +0000210 * \deprecated Use md_free() instead
Paul Bakker84bbeb52014-07-01 14:53:22 +0200211 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000212 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000213 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000215 */
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100216int md_free_ctx( md_context_t *ctx ) DEPRECATED;
217#undef DEPRECATED
218#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker562535d2011-01-20 16:42:01 +0000219
220/**
Paul Bakker17373852011-01-06 14:20:01 +0000221 * \brief Returns the size of the message digest output.
222 *
223 * \param md_info message digest info
224 *
225 * \return size of the message digest output.
226 */
Paul Bakker23986e52011-04-24 08:57:21 +0000227static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000228{
Paul Bakkerc295b832013-04-02 11:13:39 +0200229 if( md_info == NULL )
230 return( 0 );
231
Paul Bakker17373852011-01-06 14:20:01 +0000232 return md_info->size;
233}
234
235/**
236 * \brief Returns the type of the message digest output.
237 *
238 * \param md_info message digest info
239 *
240 * \return type of the message digest output.
241 */
Paul Bakker23986e52011-04-24 08:57:21 +0000242static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000243{
Paul Bakkerc295b832013-04-02 11:13:39 +0200244 if( md_info == NULL )
245 return( POLARSSL_MD_NONE );
246
Paul Bakker17373852011-01-06 14:20:01 +0000247 return md_info->type;
248}
249
250/**
251 * \brief Returns the name of the message digest output.
252 *
253 * \param md_info message digest info
254 *
255 * \return name of the message digest output.
256 */
Paul Bakker23986e52011-04-24 08:57:21 +0000257static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000258{
Paul Bakkerc295b832013-04-02 11:13:39 +0200259 if( md_info == NULL )
260 return( NULL );
261
Paul Bakker17373852011-01-06 14:20:01 +0000262 return md_info->name;
263}
264
265/**
Paul Bakker562535d2011-01-20 16:42:01 +0000266 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000267 *
Paul Bakker562535d2011-01-20 16:42:01 +0000268 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000269 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000270 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
271 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000272 */
Paul Bakker562535d2011-01-20 16:42:01 +0000273int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000274
275/**
276 * \brief Generic message digest process buffer
277 *
278 * \param ctx Generic message digest context
279 * \param input buffer holding the datal
280 * \param ilen length of the input data
281 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000282 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
283 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000284 */
Paul Bakker23986e52011-04-24 08:57:21 +0000285int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000286
287/**
288 * \brief Generic message digest final digest
289 *
290 * \param ctx Generic message digest context
291 * \param output Generic message digest checksum result
292 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000293 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
294 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000295 */
296int md_finish( md_context_t *ctx, unsigned char *output );
297
298/**
Paul Bakker17373852011-01-06 14:20:01 +0000299 * \brief Output = message_digest( input buffer )
300 *
301 * \param md_info message digest info
302 * \param input buffer holding the data
303 * \param ilen length of the input data
304 * \param output Generic message digest checksum result
305 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000306 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
307 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000308 */
Paul Bakker23986e52011-04-24 08:57:21 +0000309int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000310 unsigned char *output );
311
312/**
313 * \brief Output = message_digest( file contents )
314 *
315 * \param md_info message digest info
316 * \param path input file name
317 * \param output generic message digest checksum result
318 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000319 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
320 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
321 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000322 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200323int md_file( const md_info_t *md_info, const char *path,
324 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000325
326/**
327 * \brief Generic HMAC context setup
328 *
Paul Bakker17373852011-01-06 14:20:01 +0000329 * \param ctx HMAC context to be initialized
330 * \param key HMAC secret key
331 * \param keylen length of the HMAC key
332 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000333 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
334 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000335 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200336int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
337 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000338
339/**
340 * \brief Generic HMAC process buffer
341 *
342 * \param ctx HMAC context
343 * \param input buffer holding the data
344 * \param ilen length of the input data
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200349int md_hmac_update( md_context_t *ctx, const unsigned char *input,
350 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000351
352/**
353 * \brief Generic HMAC final digest
354 *
355 * \param ctx HMAC context
356 * \param output Generic HMAC checksum result
357 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000358 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
359 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000360 */
361int md_hmac_finish( md_context_t *ctx, unsigned char *output);
362
363/**
364 * \brief Generic HMAC context reset
365 *
366 * \param ctx HMAC context to be reset
367 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000368 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
369 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000370 */
371int md_hmac_reset( md_context_t *ctx );
372
373/**
374 * \brief Output = Generic_HMAC( hmac key, input buffer )
375 *
376 * \param md_info message digest info
377 * \param key HMAC secret key
378 * \param keylen length of the HMAC key
379 * \param input buffer holding the data
380 * \param ilen length of the input data
381 * \param output Generic HMAC-result
382 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000383 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
384 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000385 */
Paul Bakker23986e52011-04-24 08:57:21 +0000386int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
387 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000388 unsigned char *output );
389
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100390/* Internal use */
391int md_process( md_context_t *ctx, const unsigned char *data );
392
Paul Bakker17373852011-01-06 14:20:01 +0000393#ifdef __cplusplus
394}
395#endif
396
397#endif /* POLARSSL_MD_H */