blob: 9be55a98980c48f043cd52897c56742378cbde0c [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
Paul Bakker17373852011-01-06 14:20:01 +000050typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000051 POLARSSL_MD_NONE=0,
52 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000053 POLARSSL_MD_MD4,
54 POLARSSL_MD_MD5,
55 POLARSSL_MD_SHA1,
56 POLARSSL_MD_SHA224,
57 POLARSSL_MD_SHA256,
58 POLARSSL_MD_SHA384,
59 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010060 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000061} md_type_t;
62
Paul Bakker7db01092013-09-10 11:10:57 +020063#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000064#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020065#else
66#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
67#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000068
Paul Bakker17373852011-01-06 14:20:01 +000069/**
70 * Message digest information. Allows message digest functions to be called
71 * in a generic way.
72 */
73typedef struct {
74 /** Digest identifier */
75 md_type_t type;
76
77 /** Name of the message digest */
78 const char * name;
79
80 /** Output length of the digest function */
81 int size;
82
83 /** Digest initialisation function */
84 void (*starts_func)( void *ctx );
85
86 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000087 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000088
89 /** Digest finalisation function */
90 void (*finish_func)( void *ctx, unsigned char *output );
91
92 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000093 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020094 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +000095
96 /** Generic file digest function */
97 int (*file_func)( const char *path, unsigned char *output );
98
99 /** HMAC Initialisation function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200100 void (*hmac_starts_func)( void *ctx, const unsigned char *key,
101 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000102
103 /** HMAC update function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200104 void (*hmac_update_func)( void *ctx, const unsigned char *input,
105 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000106
107 /** HMAC finalisation function */
108 void (*hmac_finish_func)( void *ctx, unsigned char *output);
109
110 /** HMAC context reset function */
111 void (*hmac_reset_func)( void *ctx );
112
113 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000114 void (*hmac_func)( const unsigned char *key, size_t keylen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200115 const unsigned char *input, size_t ilen,
116 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000117
118 /** Allocate a new context */
119 void * (*ctx_alloc_func)( void );
120
121 /** Free the given context */
122 void (*ctx_free_func)( void *ctx );
123
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100124 /** Internal use only */
125 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000126} md_info_t;
127
128/**
129 * Generic message digest context.
130 */
131typedef struct {
132 /** Information about the associated message digest */
133 const md_info_t *md_info;
134
135 /** Digest-specific context */
136 void *md_ctx;
137} md_context_t;
138
139#define MD_CONTEXT_T_INIT { \
140 NULL, /* md_info */ \
141 NULL, /* md_ctx */ \
142}
143
Paul Bakker17373852011-01-06 14:20:01 +0000144/**
Paul Bakker72f62662011-01-16 21:27:44 +0000145 * \brief Returns the list of digests supported by the generic digest module.
146 *
147 * \return a statically allocated array of digests, the last entry
148 * is 0.
149 */
150const int *md_list( void );
151
152/**
Paul Bakker17373852011-01-06 14:20:01 +0000153 * \brief Returns the message digest information associated with the
154 * given digest name.
155 *
Paul Bakker23986e52011-04-24 08:57:21 +0000156 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000157 *
158 * \return The message digest information associated with md_name or
159 * NULL if not found.
160 */
161const md_info_t *md_info_from_string( const char *md_name );
162
163/**
164 * \brief Returns the message digest information associated with the
165 * given digest type.
166 *
167 * \param md_type type of digest to search for.
168 *
169 * \return The message digest information associated with md_type or
170 * NULL if not found.
171 */
172const md_info_t *md_info_from_type( md_type_t md_type );
173
174/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200175 * \brief Initialize a md_context (as NONE)
176 */
177void md_init( md_context_t *ctx );
178
179/**
180 * \brief Free and clear the message-specific context of ctx.
181 * Freeing ctx itself remains the responsibility of the
182 * caller.
183 */
184void md_free( md_context_t *ctx );
185
186/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200187 * \brief Initialises and fills the message digest context structure
188 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000189 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190 * \note Currently also clears structure. In future versions you
191 * will be required to call md_init() on the structure
192 * first.
193 *
Paul Bakker562535d2011-01-20 16:42:01 +0000194 * \param ctx context to initialise. May not be NULL. The
195 * digest-specific context (ctx->md_ctx) must be NULL. It will
196 * be allocated, and must be freed using md_free_ctx() later.
197 * \param md_info message digest to use.
198 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000199 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
200 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000201 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000202 */
203int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
204
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100205#if ! defined(POLARSSL_DEPRECATED_REMOVED)
206#if defined(POLARSSL_DEPRECATED_WARNING)
207#define DEPRECATED __attribute__((deprecated))
208#else
209#define DEPRECATED
210#endif
Paul Bakker562535d2011-01-20 16:42:01 +0000211/**
212 * \brief Free the message-specific context of ctx. Freeing ctx itself
213 * remains the responsibility of the caller.
214 *
Manuel Pégourié-Gonnard71432842015-03-20 16:19:35 +0000215 * \deprecated Use md_free() instead
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000217 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000218 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000220 */
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100221int md_free_ctx( md_context_t *ctx ) DEPRECATED;
222#undef DEPRECATED
223#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker562535d2011-01-20 16:42:01 +0000224
225/**
Paul Bakker17373852011-01-06 14:20:01 +0000226 * \brief Returns the size of the message digest output.
227 *
228 * \param md_info message digest info
229 *
230 * \return size of the message digest output.
231 */
Paul Bakker23986e52011-04-24 08:57:21 +0000232static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000233{
Paul Bakkerc295b832013-04-02 11:13:39 +0200234 if( md_info == NULL )
235 return( 0 );
236
Paul Bakker17373852011-01-06 14:20:01 +0000237 return md_info->size;
238}
239
240/**
241 * \brief Returns the type of the message digest output.
242 *
243 * \param md_info message digest info
244 *
245 * \return type of the message digest output.
246 */
Paul Bakker23986e52011-04-24 08:57:21 +0000247static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000248{
Paul Bakkerc295b832013-04-02 11:13:39 +0200249 if( md_info == NULL )
250 return( POLARSSL_MD_NONE );
251
Paul Bakker17373852011-01-06 14:20:01 +0000252 return md_info->type;
253}
254
255/**
256 * \brief Returns the name of the message digest output.
257 *
258 * \param md_info message digest info
259 *
260 * \return name of the message digest output.
261 */
Paul Bakker23986e52011-04-24 08:57:21 +0000262static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000263{
Paul Bakkerc295b832013-04-02 11:13:39 +0200264 if( md_info == NULL )
265 return( NULL );
266
Paul Bakker17373852011-01-06 14:20:01 +0000267 return md_info->name;
268}
269
270/**
Paul Bakker562535d2011-01-20 16:42:01 +0000271 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000272 *
Paul Bakker562535d2011-01-20 16:42:01 +0000273 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000274 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000275 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
276 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000277 */
Paul Bakker562535d2011-01-20 16:42:01 +0000278int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000279
280/**
281 * \brief Generic message digest process buffer
282 *
283 * \param ctx Generic message digest context
284 * \param input buffer holding the datal
285 * \param ilen length of the input data
286 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000287 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
288 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000289 */
Paul Bakker23986e52011-04-24 08:57:21 +0000290int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000291
292/**
293 * \brief Generic message digest final digest
294 *
295 * \param ctx Generic message digest context
296 * \param output Generic message digest checksum result
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 */
301int md_finish( md_context_t *ctx, unsigned char *output );
302
303/**
Paul Bakker17373852011-01-06 14:20:01 +0000304 * \brief Output = message_digest( input buffer )
305 *
306 * \param md_info message digest info
307 * \param input buffer holding the data
308 * \param ilen length of the input data
309 * \param output Generic message digest checksum result
310 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000311 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
312 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000313 */
Paul Bakker23986e52011-04-24 08:57:21 +0000314int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000315 unsigned char *output );
316
317/**
318 * \brief Output = message_digest( file contents )
319 *
320 * \param md_info message digest info
321 * \param path input file name
322 * \param output generic message digest checksum result
323 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000324 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
325 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
326 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000327 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200328int md_file( const md_info_t *md_info, const char *path,
329 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000330
331/**
332 * \brief Generic HMAC context setup
333 *
Paul Bakker17373852011-01-06 14:20:01 +0000334 * \param ctx HMAC context to be initialized
335 * \param key HMAC secret key
336 * \param keylen length of the HMAC key
337 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000338 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
339 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000340 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200341int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
342 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000343
344/**
345 * \brief Generic HMAC process buffer
346 *
347 * \param ctx HMAC context
348 * \param input buffer holding the data
349 * \param ilen length of the input data
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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200354int md_hmac_update( md_context_t *ctx, const unsigned char *input,
355 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000356
357/**
358 * \brief Generic HMAC final digest
359 *
360 * \param ctx HMAC context
361 * \param output Generic HMAC checksum result
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_finish( md_context_t *ctx, unsigned char *output);
367
368/**
369 * \brief Generic HMAC context reset
370 *
371 * \param ctx HMAC context to be reset
372 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000373 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
374 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000375 */
376int md_hmac_reset( md_context_t *ctx );
377
378/**
379 * \brief Output = Generic_HMAC( hmac key, input buffer )
380 *
381 * \param md_info message digest info
382 * \param key HMAC secret key
383 * \param keylen length of the HMAC key
384 * \param input buffer holding the data
385 * \param ilen length of the input data
386 * \param output Generic HMAC-result
387 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000388 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
389 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000390 */
Paul Bakker23986e52011-04-24 08:57:21 +0000391int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
392 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000393 unsigned char *output );
394
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100395/* Internal use */
396int md_process( md_context_t *ctx, const unsigned char *data );
397
Paul Bakker17373852011-01-06 14:20:01 +0000398#ifdef __cplusplus
399}
400#endif
401
402#endif /* POLARSSL_MD_H */