blob: e3958702fbca88f49030b179d7c5ca12e96d7ade [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
Ron Eldorbc3fa392017-09-07 16:58:41 +030036#if !defined(POLARSSL_CONFIG_FILE)
37#include "config.h"
38#else
39#include POLARSSL_CONFIG_FILE
40#endif
41
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000043#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
44#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000045#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000046
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker17373852011-01-06 14:20:01 +000051typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000052 POLARSSL_MD_NONE=0,
53 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000054 POLARSSL_MD_MD4,
55 POLARSSL_MD_MD5,
56 POLARSSL_MD_SHA1,
57 POLARSSL_MD_SHA224,
58 POLARSSL_MD_SHA256,
59 POLARSSL_MD_SHA384,
60 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010061 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000062} md_type_t;
63
Paul Bakker7db01092013-09-10 11:10:57 +020064#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000065#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020066#else
67#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
68#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000069
Paul Bakker17373852011-01-06 14:20:01 +000070/**
71 * Message digest information. Allows message digest functions to be called
72 * in a generic way.
73 */
74typedef struct {
75 /** Digest identifier */
76 md_type_t type;
77
78 /** Name of the message digest */
79 const char * name;
80
81 /** Output length of the digest function */
82 int size;
83
84 /** Digest initialisation function */
85 void (*starts_func)( void *ctx );
86
87 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000088 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000089
90 /** Digest finalisation function */
91 void (*finish_func)( void *ctx, unsigned char *output );
92
93 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000094 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020095 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +000096
97 /** Generic file digest function */
98 int (*file_func)( const char *path, unsigned char *output );
99
100 /** HMAC Initialisation function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200101 void (*hmac_starts_func)( void *ctx, const unsigned char *key,
102 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000103
104 /** HMAC update function */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200105 void (*hmac_update_func)( void *ctx, const unsigned char *input,
106 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000107
108 /** HMAC finalisation function */
109 void (*hmac_finish_func)( void *ctx, unsigned char *output);
110
111 /** HMAC context reset function */
112 void (*hmac_reset_func)( void *ctx );
113
114 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000115 void (*hmac_func)( const unsigned char *key, size_t keylen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200116 const unsigned char *input, size_t ilen,
117 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000118
119 /** Allocate a new context */
120 void * (*ctx_alloc_func)( void );
121
122 /** Free the given context */
123 void (*ctx_free_func)( void *ctx );
124
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100125 /** Internal use only */
126 void (*process_func)( void *ctx, const unsigned char *input );
Paul Bakker17373852011-01-06 14:20:01 +0000127} md_info_t;
128
129/**
130 * Generic message digest context.
131 */
132typedef struct {
133 /** Information about the associated message digest */
134 const md_info_t *md_info;
135
136 /** Digest-specific context */
137 void *md_ctx;
138} md_context_t;
139
140#define MD_CONTEXT_T_INIT { \
141 NULL, /* md_info */ \
142 NULL, /* md_ctx */ \
143}
144
Paul Bakker17373852011-01-06 14:20:01 +0000145/**
Paul Bakker72f62662011-01-16 21:27:44 +0000146 * \brief Returns the list of digests supported by the generic digest module.
147 *
148 * \return a statically allocated array of digests, the last entry
149 * is 0.
150 */
151const int *md_list( void );
152
153/**
Paul Bakker17373852011-01-06 14:20:01 +0000154 * \brief Returns the message digest information associated with the
155 * given digest name.
156 *
Paul Bakker23986e52011-04-24 08:57:21 +0000157 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000158 *
159 * \return The message digest information associated with md_name or
160 * NULL if not found.
161 */
162const md_info_t *md_info_from_string( const char *md_name );
163
164/**
165 * \brief Returns the message digest information associated with the
166 * given digest type.
167 *
168 * \param md_type type of digest to search for.
169 *
170 * \return The message digest information associated with md_type or
171 * NULL if not found.
172 */
173const md_info_t *md_info_from_type( md_type_t md_type );
174
175/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176 * \brief Initialize a md_context (as NONE)
177 */
178void md_init( md_context_t *ctx );
179
180/**
181 * \brief Free and clear the message-specific context of ctx.
182 * Freeing ctx itself remains the responsibility of the
183 * caller.
184 */
185void md_free( md_context_t *ctx );
186
187/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200188 * \brief Initialises and fills the message digest context structure
189 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000190 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191 * \note Currently also clears structure. In future versions you
192 * will be required to call md_init() on the structure
193 * first.
194 *
Paul Bakker562535d2011-01-20 16:42:01 +0000195 * \param ctx context to initialise. May not be NULL. The
196 * digest-specific context (ctx->md_ctx) must be NULL. It will
197 * be allocated, and must be freed using md_free_ctx() later.
198 * \param md_info message digest to use.
199 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000200 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
201 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000202 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000203 */
204int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
205
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100206#if ! defined(POLARSSL_DEPRECATED_REMOVED)
207#if defined(POLARSSL_DEPRECATED_WARNING)
208#define DEPRECATED __attribute__((deprecated))
209#else
210#define DEPRECATED
211#endif
Paul Bakker562535d2011-01-20 16:42:01 +0000212/**
213 * \brief Free the message-specific context of ctx. Freeing ctx itself
214 * remains the responsibility of the caller.
215 *
Manuel Pégourié-Gonnard71432842015-03-20 16:19:35 +0000216 * \deprecated Use md_free() instead
Paul Bakker84bbeb52014-07-01 14:53:22 +0200217 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000218 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000219 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200220 * \returns 0
Paul Bakker562535d2011-01-20 16:42:01 +0000221 */
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100222int md_free_ctx( md_context_t *ctx ) DEPRECATED;
223#undef DEPRECATED
224#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker562535d2011-01-20 16:42:01 +0000225
226/**
Paul Bakker17373852011-01-06 14:20:01 +0000227 * \brief Returns the size of the message digest output.
228 *
229 * \param md_info message digest info
230 *
231 * \return size of the message digest output.
232 */
Paul Bakker23986e52011-04-24 08:57:21 +0000233static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000234{
Paul Bakkerc295b832013-04-02 11:13:39 +0200235 if( md_info == NULL )
236 return( 0 );
237
Paul Bakker17373852011-01-06 14:20:01 +0000238 return md_info->size;
239}
240
241/**
242 * \brief Returns the type of the message digest output.
243 *
244 * \param md_info message digest info
245 *
246 * \return type of the message digest output.
247 */
Paul Bakker23986e52011-04-24 08:57:21 +0000248static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000249{
Paul Bakkerc295b832013-04-02 11:13:39 +0200250 if( md_info == NULL )
251 return( POLARSSL_MD_NONE );
252
Paul Bakker17373852011-01-06 14:20:01 +0000253 return md_info->type;
254}
255
256/**
257 * \brief Returns the name of the message digest output.
258 *
259 * \param md_info message digest info
260 *
261 * \return name of the message digest output.
262 */
Paul Bakker23986e52011-04-24 08:57:21 +0000263static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000264{
Paul Bakkerc295b832013-04-02 11:13:39 +0200265 if( md_info == NULL )
266 return( NULL );
267
Paul Bakker17373852011-01-06 14:20:01 +0000268 return md_info->name;
269}
270
271/**
Paul Bakker562535d2011-01-20 16:42:01 +0000272 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000273 *
Paul Bakker562535d2011-01-20 16:42:01 +0000274 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000275 *
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 */
Paul Bakker562535d2011-01-20 16:42:01 +0000279int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000280
281/**
282 * \brief Generic message digest process buffer
283 *
284 * \param ctx Generic message digest context
285 * \param input buffer holding the datal
286 * \param ilen length of the input data
287 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000288 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
289 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000290 */
Paul Bakker23986e52011-04-24 08:57:21 +0000291int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000292
293/**
294 * \brief Generic message digest final digest
295 *
296 * \param ctx Generic message digest context
297 * \param output Generic message digest checksum result
298 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000299 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
300 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000301 */
302int md_finish( md_context_t *ctx, unsigned char *output );
303
304/**
Paul Bakker17373852011-01-06 14:20:01 +0000305 * \brief Output = message_digest( input buffer )
306 *
307 * \param md_info message digest info
308 * \param input buffer holding the data
309 * \param ilen length of the input data
310 * \param output Generic message digest checksum result
311 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000312 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
313 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000314 */
Paul Bakker23986e52011-04-24 08:57:21 +0000315int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000316 unsigned char *output );
317
318/**
319 * \brief Output = message_digest( file contents )
320 *
321 * \param md_info message digest info
322 * \param path input file name
323 * \param output generic message digest checksum result
324 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000325 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
326 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
327 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000328 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200329int md_file( const md_info_t *md_info, const char *path,
330 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000331
332/**
333 * \brief Generic HMAC context setup
334 *
Paul Bakker17373852011-01-06 14:20:01 +0000335 * \param ctx HMAC context to be initialized
336 * \param key HMAC secret key
337 * \param keylen length of the HMAC key
338 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000339 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
340 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000341 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200342int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
343 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000344
345/**
346 * \brief Generic HMAC process buffer
347 *
348 * \param ctx HMAC context
349 * \param input buffer holding the data
350 * \param ilen length of the input data
351 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000352 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
353 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000354 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200355int md_hmac_update( md_context_t *ctx, const unsigned char *input,
356 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000357
358/**
359 * \brief Generic HMAC final digest
360 *
361 * \param ctx HMAC context
362 * \param output Generic HMAC checksum result
363 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000364 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
365 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000366 */
367int md_hmac_finish( md_context_t *ctx, unsigned char *output);
368
369/**
370 * \brief Generic HMAC context reset
371 *
372 * \param ctx HMAC context to be reset
373 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000374 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
375 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000376 */
377int md_hmac_reset( md_context_t *ctx );
378
379/**
380 * \brief Output = Generic_HMAC( hmac key, input buffer )
381 *
382 * \param md_info message digest info
383 * \param key HMAC secret key
384 * \param keylen length of the HMAC key
385 * \param input buffer holding the data
386 * \param ilen length of the input data
387 * \param output Generic HMAC-result
388 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000389 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
390 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000391 */
Paul Bakker23986e52011-04-24 08:57:21 +0000392int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
393 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000394 unsigned char *output );
395
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100396/* Internal use */
397int md_process( md_context_t *ctx, const unsigned char *data );
398
Paul Bakker17373852011-01-06 14:20:01 +0000399#ifdef __cplusplus
400}
401#endif
402
403#endif /* POLARSSL_MD_H */