blob: 3e6d63fb9a649dbdc6c47f1323cfe0da267c3ca2 [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>
Ron Eldorbc3fa392017-09-07 16:58:41 +030030#if !defined(POLARSSL_CONFIG_FILE)
31#include "config.h"
32#else
33#include POLARSSL_CONFIG_FILE
34#endif
35
Ron Eldor3216c1a2017-09-07 17:15:47 +030036#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
37 !defined(inline) && !defined(__cplusplus)
38#define inline __inline
39#endif
40
Paul Bakker9d781402011-05-09 16:17:09 +000041#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000042#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
43#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000044#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000045
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Hanno Beckerce0c9db2017-09-28 15:39:45 +010050/**
51 * \brief Enumeration of supported message digests
52 *
53 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
54 * their use constitutes a security risk. We recommend considering
55 * stronger message digests instead.
56 */
Paul Bakker17373852011-01-06 14:20:01 +000057typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000058 POLARSSL_MD_NONE=0,
59 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000060 POLARSSL_MD_MD4,
61 POLARSSL_MD_MD5,
62 POLARSSL_MD_SHA1,
63 POLARSSL_MD_SHA224,
64 POLARSSL_MD_SHA256,
65 POLARSSL_MD_SHA384,
66 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010067 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000068} md_type_t;
69
Paul Bakker7db01092013-09-10 11:10:57 +020070#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000071#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020072#else
73#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
74#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000075
Paul Bakker17373852011-01-06 14:20:01 +000076/**
77 * Message digest information. Allows message digest functions to be called
78 * in a generic way.
79 */
80typedef struct {
81 /** Digest identifier */
82 md_type_t type;
83
84 /** Name of the message digest */
85 const char * name;
86
87 /** Output length of the digest function */
88 int size;
89
90 /** Digest initialisation function */
91 void (*starts_func)( void *ctx );
92
93 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000094 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000095
96 /** Digest finalisation function */
97 void (*finish_func)( void *ctx, unsigned char *output );
98
99 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +0000100 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200101 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000102
103 /** Generic file digest function */
104 int (*file_func)( const char *path, unsigned char *output );
105
106 /** HMAC Initialisation function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200107 void (*hmac_starts_func)( void *ctx, const unsigned char *key,
108 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000109
110 /** HMAC update function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200111 void (*hmac_update_func)( void *ctx, const unsigned char *input,
112 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000113
114 /** HMAC finalisation function */
115 void (*hmac_finish_func)( void *ctx, unsigned char *output);
116
117 /** HMAC context reset function */
118 void (*hmac_reset_func)( void *ctx );
119
120 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000121 void (*hmac_func)( const unsigned char *key, size_t keylen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200122 const unsigned char *input, size_t ilen,
123 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000124
125 /** Allocate a new context */
126 void * (*ctx_alloc_func)( void );
127
128 /** Free the given context */
129 void (*ctx_free_func)( void *ctx );
130
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100131 /** Internal use only */
132 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000133} md_info_t;
134
135/**
136 * Generic message digest context.
137 */
138typedef struct {
139 /** Information about the associated message digest */
140 const md_info_t *md_info;
141
142 /** Digest-specific context */
143 void *md_ctx;
144} md_context_t;
145
146#define MD_CONTEXT_T_INIT { \
147 NULL, /* md_info */ \
148 NULL, /* md_ctx */ \
149}
150
Paul Bakker17373852011-01-06 14:20:01 +0000151/**
Paul Bakker72f62662011-01-16 21:27:44 +0000152 * \brief Returns the list of digests supported by the generic digest module.
153 *
154 * \return a statically allocated array of digests, the last entry
155 * is 0.
156 */
157const int *md_list( void );
158
159/**
Paul Bakker17373852011-01-06 14:20:01 +0000160 * \brief Returns the message digest information associated with the
161 * given digest name.
162 *
Paul Bakker23986e52011-04-24 08:57:21 +0000163 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000164 *
165 * \return The message digest information associated with md_name or
166 * NULL if not found.
167 */
168const md_info_t *md_info_from_string( const char *md_name );
169
170/**
171 * \brief Returns the message digest information associated with the
172 * given digest type.
173 *
174 * \param md_type type of digest to search for.
175 *
176 * \return The message digest information associated with md_type or
177 * NULL if not found.
178 */
179const md_info_t *md_info_from_type( md_type_t md_type );
180
181/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182 * \brief Initialize a md_context (as NONE)
183 */
184void md_init( md_context_t *ctx );
185
186/**
187 * \brief Free and clear the message-specific context of ctx.
188 * Freeing ctx itself remains the responsibility of the
189 * caller.
190 */
191void md_free( md_context_t *ctx );
192
193/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200194 * \brief Initialises and fills the message digest context structure
195 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000196 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200197 * \note Currently also clears structure. In future versions you
198 * will be required to call md_init() on the structure
199 * first.
200 *
Paul Bakker562535d2011-01-20 16:42:01 +0000201 * \param ctx context to initialise. May not be NULL. The
202 * digest-specific context (ctx->md_ctx) must be NULL. It will
203 * be allocated, and must be freed using md_free_ctx() later.
204 * \param md_info message digest to use.
205 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000206 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
207 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000208 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000209 */
210int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
211
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100212#if ! defined(POLARSSL_DEPRECATED_REMOVED)
213#if defined(POLARSSL_DEPRECATED_WARNING)
214#define DEPRECATED __attribute__((deprecated))
215#else
216#define DEPRECATED
217#endif
Paul Bakker562535d2011-01-20 16:42:01 +0000218/**
219 * \brief Free the message-specific context of ctx. Freeing ctx itself
220 * remains the responsibility of the caller.
221 *
Manuel Pégourié-Gonnard71432842015-03-20 16:19:35 +0000222 * \deprecated Use md_free() instead
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000224 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000225 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200226 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000227 */
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100228int md_free_ctx( md_context_t *ctx ) DEPRECATED;
229#undef DEPRECATED
230#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker562535d2011-01-20 16:42:01 +0000231
232/**
Paul Bakker17373852011-01-06 14:20:01 +0000233 * \brief Returns the size of the message digest output.
234 *
235 * \param md_info message digest info
236 *
237 * \return size of the message digest output.
238 */
Paul Bakker23986e52011-04-24 08:57:21 +0000239static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000240{
Paul Bakkerc295b832013-04-02 11:13:39 +0200241 if( md_info == NULL )
242 return( 0 );
243
Paul Bakker17373852011-01-06 14:20:01 +0000244 return md_info->size;
245}
246
247/**
248 * \brief Returns the type of the message digest output.
249 *
250 * \param md_info message digest info
251 *
252 * \return type of the message digest output.
253 */
Paul Bakker23986e52011-04-24 08:57:21 +0000254static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000255{
Paul Bakkerc295b832013-04-02 11:13:39 +0200256 if( md_info == NULL )
257 return( POLARSSL_MD_NONE );
258
Paul Bakker17373852011-01-06 14:20:01 +0000259 return md_info->type;
260}
261
262/**
263 * \brief Returns the name of the message digest output.
264 *
265 * \param md_info message digest info
266 *
267 * \return name of the message digest output.
268 */
Paul Bakker23986e52011-04-24 08:57:21 +0000269static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000270{
Paul Bakkerc295b832013-04-02 11:13:39 +0200271 if( md_info == NULL )
272 return( NULL );
273
Paul Bakker17373852011-01-06 14:20:01 +0000274 return md_info->name;
275}
276
277/**
Paul Bakker562535d2011-01-20 16:42:01 +0000278 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000279 *
Paul Bakker562535d2011-01-20 16:42:01 +0000280 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000281 *
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 Bakker562535d2011-01-20 16:42:01 +0000285int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000286
287/**
288 * \brief Generic message digest process buffer
289 *
290 * \param ctx Generic message digest context
291 * \param input buffer holding the datal
292 * \param ilen length of the input data
293 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000294 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
295 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000296 */
Paul Bakker23986e52011-04-24 08:57:21 +0000297int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000298
299/**
300 * \brief Generic message digest final digest
301 *
302 * \param ctx Generic message digest context
303 * \param output Generic message digest checksum result
304 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000305 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
306 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000307 */
308int md_finish( md_context_t *ctx, unsigned char *output );
309
310/**
Paul Bakker17373852011-01-06 14:20:01 +0000311 * \brief Output = message_digest( input buffer )
312 *
313 * \param md_info message digest info
314 * \param input buffer holding the data
315 * \param ilen length of the input data
316 * \param output Generic message digest checksum result
317 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000318 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
319 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000320 */
Paul Bakker23986e52011-04-24 08:57:21 +0000321int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000322 unsigned char *output );
323
324/**
325 * \brief Output = message_digest( file contents )
326 *
327 * \param md_info message digest info
328 * \param path input file name
329 * \param output generic message digest checksum result
330 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000331 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
332 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
333 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000334 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200335int md_file( const md_info_t *md_info, const char *path,
336 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000337
338/**
339 * \brief Generic HMAC context setup
340 *
Paul Bakker17373852011-01-06 14:20:01 +0000341 * \param ctx HMAC context to be initialized
342 * \param key HMAC secret key
343 * \param keylen length of the HMAC key
344 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000345 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
346 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000347 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200348int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
349 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000350
351/**
352 * \brief Generic HMAC process buffer
353 *
354 * \param ctx HMAC context
355 * \param input buffer holding the data
356 * \param ilen length of the input data
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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200361int md_hmac_update( md_context_t *ctx, const unsigned char *input,
362 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000363
364/**
365 * \brief Generic HMAC final digest
366 *
367 * \param ctx HMAC context
368 * \param output Generic HMAC checksum result
369 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000370 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
371 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000372 */
373int md_hmac_finish( md_context_t *ctx, unsigned char *output);
374
375/**
376 * \brief Generic HMAC context reset
377 *
378 * \param ctx HMAC context to be reset
379 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000380 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
381 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000382 */
383int md_hmac_reset( md_context_t *ctx );
384
385/**
386 * \brief Output = Generic_HMAC( hmac key, input buffer )
387 *
388 * \param md_info message digest info
389 * \param key HMAC secret key
390 * \param keylen length of the HMAC key
391 * \param input buffer holding the data
392 * \param ilen length of the input data
393 * \param output Generic HMAC-result
394 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000395 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
396 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000397 */
Paul Bakker23986e52011-04-24 08:57:21 +0000398int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
399 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000400 unsigned char *output );
401
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100402/* Internal use */
403int md_process( md_context_t *ctx, const unsigned char *data );
404
Paul Bakker17373852011-01-06 14:20:01 +0000405#ifdef __cplusplus
406}
407#endif
408
409#endif /* POLARSSL_MD_H */