blob: cd88cecf5150e77651864a147e13b1caa67c554a [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 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker17373852011-01-06 14:20:01 +00009 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +000010 * This file is part of mbed TLS (https://www.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
Paul Bakker09b1ec62011-07-27 16:28:54 +000031#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000032#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000033#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000034#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000035#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000036#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000037#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000038
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
Paul Bakker9c021ad2011-06-09 15:55:11 +000040#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
41#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
Paul Bakker8913f822012-01-14 18:07:41 +000042#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000043
Paul Bakker17373852011-01-06 14:20:01 +000044typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000045 POLARSSL_MD_NONE=0,
46 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000047 POLARSSL_MD_MD4,
48 POLARSSL_MD_MD5,
49 POLARSSL_MD_SHA1,
50 POLARSSL_MD_SHA224,
51 POLARSSL_MD_SHA256,
52 POLARSSL_MD_SHA384,
53 POLARSSL_MD_SHA512,
54} md_type_t;
55
Paul Bakker1b57b062011-01-06 15:48:19 +000056#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
57
Paul Bakker17373852011-01-06 14:20:01 +000058/**
59 * Message digest information. Allows message digest functions to be called
60 * in a generic way.
61 */
62typedef struct {
63 /** Digest identifier */
64 md_type_t type;
65
66 /** Name of the message digest */
67 const char * name;
68
69 /** Output length of the digest function */
70 int size;
71
72 /** Digest initialisation function */
73 void (*starts_func)( void *ctx );
74
75 /** Digest update function */
Paul Bakker23986e52011-04-24 08:57:21 +000076 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000077
78 /** Digest finalisation function */
79 void (*finish_func)( void *ctx, unsigned char *output );
80
81 /** Generic digest function */
Paul Bakker23986e52011-04-24 08:57:21 +000082 void (*digest_func)( const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +000083 unsigned char *output );
84
85 /** Generic file digest function */
86 int (*file_func)( const char *path, unsigned char *output );
87
88 /** HMAC Initialisation function */
Paul Bakker23986e52011-04-24 08:57:21 +000089 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +000090
91 /** HMAC update function */
Paul Bakker23986e52011-04-24 08:57:21 +000092 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +000093
94 /** HMAC finalisation function */
95 void (*hmac_finish_func)( void *ctx, unsigned char *output);
96
97 /** HMAC context reset function */
98 void (*hmac_reset_func)( void *ctx );
99
100 /** Generic HMAC function */
Paul Bakker23986e52011-04-24 08:57:21 +0000101 void (*hmac_func)( const unsigned char *key, size_t keylen,
102 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000103 unsigned char *output );
104
105 /** Allocate a new context */
106 void * (*ctx_alloc_func)( void );
107
108 /** Free the given context */
109 void (*ctx_free_func)( void *ctx );
110
111} md_info_t;
112
113/**
114 * Generic message digest context.
115 */
116typedef struct {
117 /** Information about the associated message digest */
118 const md_info_t *md_info;
119
120 /** Digest-specific context */
121 void *md_ctx;
122} md_context_t;
123
124#define MD_CONTEXT_T_INIT { \
125 NULL, /* md_info */ \
126 NULL, /* md_ctx */ \
127}
128
129#ifdef __cplusplus
130extern "C" {
131#endif
132
133/**
Paul Bakker72f62662011-01-16 21:27:44 +0000134 * \brief Returns the list of digests supported by the generic digest module.
135 *
136 * \return a statically allocated array of digests, the last entry
137 * is 0.
138 */
139const int *md_list( void );
140
141/**
Paul Bakker17373852011-01-06 14:20:01 +0000142 * \brief Returns the message digest information associated with the
143 * given digest name.
144 *
Paul Bakker23986e52011-04-24 08:57:21 +0000145 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000146 *
147 * \return The message digest information associated with md_name or
148 * NULL if not found.
149 */
150const md_info_t *md_info_from_string( const char *md_name );
151
152/**
153 * \brief Returns the message digest information associated with the
154 * given digest type.
155 *
156 * \param md_type type of digest to search for.
157 *
158 * \return The message digest information associated with md_type or
159 * NULL if not found.
160 */
161const md_info_t *md_info_from_type( md_type_t md_type );
162
163/**
Paul Bakker562535d2011-01-20 16:42:01 +0000164 * \brief Initialises and fills the message digest context structure with
165 * the appropriate values.
166 *
167 * \param ctx context to initialise. May not be NULL. The
168 * digest-specific context (ctx->md_ctx) must be NULL. It will
169 * be allocated, and must be freed using md_free_ctx() later.
170 * \param md_info message digest to use.
171 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000172 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
173 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000174 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000175 */
176int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
177
178/**
179 * \brief Free the message-specific context of ctx. Freeing ctx itself
180 * remains the responsibility of the caller.
181 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000182 * \param ctx Free the message-specific context
Paul Bakker562535d2011-01-20 16:42:01 +0000183 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000184 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
185 * verification fails.
Paul Bakker562535d2011-01-20 16:42:01 +0000186 */
187int md_free_ctx( md_context_t *ctx );
188
189/**
Paul Bakker17373852011-01-06 14:20:01 +0000190 * \brief Returns the size of the message digest output.
191 *
192 * \param md_info message digest info
193 *
194 * \return size of the message digest output.
195 */
Paul Bakker23986e52011-04-24 08:57:21 +0000196static inline unsigned char md_get_size( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000197{
Paul Bakkercecfd952013-04-12 13:18:07 +0200198 if( md_info == NULL )
199 return( 0 );
200
Paul Bakker17373852011-01-06 14:20:01 +0000201 return md_info->size;
202}
203
204/**
205 * \brief Returns the type of the message digest output.
206 *
207 * \param md_info message digest info
208 *
209 * \return type of the message digest output.
210 */
Paul Bakker23986e52011-04-24 08:57:21 +0000211static inline md_type_t md_get_type( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000212{
Paul Bakkercecfd952013-04-12 13:18:07 +0200213 if( md_info == NULL )
214 return( POLARSSL_MD_NONE );
215
Paul Bakker17373852011-01-06 14:20:01 +0000216 return md_info->type;
217}
218
219/**
220 * \brief Returns the name of the message digest output.
221 *
222 * \param md_info message digest info
223 *
224 * \return name of the message digest output.
225 */
Paul Bakker23986e52011-04-24 08:57:21 +0000226static inline const char *md_get_name( const md_info_t *md_info )
Paul Bakker17373852011-01-06 14:20:01 +0000227{
Paul Bakkercecfd952013-04-12 13:18:07 +0200228 if( md_info == NULL )
229 return( NULL );
230
Paul Bakker17373852011-01-06 14:20:01 +0000231 return md_info->name;
232}
233
234/**
Paul Bakker562535d2011-01-20 16:42:01 +0000235 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000236 *
Paul Bakker562535d2011-01-20 16:42:01 +0000237 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000238 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000239 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
240 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000241 */
Paul Bakker562535d2011-01-20 16:42:01 +0000242int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000243
244/**
245 * \brief Generic message digest process buffer
246 *
247 * \param ctx Generic message digest context
248 * \param input buffer holding the datal
249 * \param ilen length of the input data
250 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000251 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
252 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000253 */
Paul Bakker23986e52011-04-24 08:57:21 +0000254int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000255
256/**
257 * \brief Generic message digest final digest
258 *
259 * \param ctx Generic message digest context
260 * \param output Generic message digest checksum result
261 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000262 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
263 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000264 */
265int md_finish( md_context_t *ctx, unsigned char *output );
266
267/**
Paul Bakker17373852011-01-06 14:20:01 +0000268 * \brief Output = message_digest( input buffer )
269 *
270 * \param md_info message digest info
271 * \param input buffer holding the data
272 * \param ilen length of the input data
273 * \param output Generic message digest checksum result
274 *
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 Bakker23986e52011-04-24 08:57:21 +0000278int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000279 unsigned char *output );
280
281/**
282 * \brief Output = message_digest( file contents )
283 *
284 * \param md_info message digest info
285 * \param path input file name
286 * \param output generic message digest checksum result
287 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000288 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
289 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
290 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000291 */
292int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
293
294/**
295 * \brief Generic HMAC context setup
296 *
Paul Bakker17373852011-01-06 14:20:01 +0000297 * \param ctx HMAC context to be initialized
298 * \param key HMAC secret key
299 * \param keylen length of the HMAC key
300 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000301 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
302 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000303 */
Paul Bakker23986e52011-04-24 08:57:21 +0000304int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000305
306/**
307 * \brief Generic HMAC process buffer
308 *
309 * \param ctx HMAC context
310 * \param input buffer holding the data
311 * \param ilen length of the input data
312 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000313 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
314 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000315 */
Paul Bakker23986e52011-04-24 08:57:21 +0000316int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000317
318/**
319 * \brief Generic HMAC final digest
320 *
321 * \param ctx HMAC context
322 * \param output Generic HMAC checksum result
323 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000324 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
325 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000326 */
327int md_hmac_finish( md_context_t *ctx, unsigned char *output);
328
329/**
330 * \brief Generic HMAC context reset
331 *
332 * \param ctx HMAC context to be reset
333 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000334 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
335 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000336 */
337int md_hmac_reset( md_context_t *ctx );
338
339/**
340 * \brief Output = Generic_HMAC( hmac key, input buffer )
341 *
342 * \param md_info message digest info
343 * \param key HMAC secret key
344 * \param keylen length of the HMAC key
345 * \param input buffer holding the data
346 * \param ilen length of the input data
347 * \param output Generic HMAC-result
348 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000349 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
350 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000351 */
Paul Bakker23986e52011-04-24 08:57:21 +0000352int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
353 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000354 unsigned char *output );
355
356#ifdef __cplusplus
357}
358#endif
359
360#endif /* POLARSSL_MD_H */