blob: 74534f505fadb01653f4d44a777e8b60701affb0 [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é-Gonnardca878db2015-03-24 12:13:30 +01008 * Copyright (C) 2006-2015, 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
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 Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker17373852011-01-06 14:20:01 +000048typedef enum {
Paul Bakker562535d2011-01-20 16:42:01 +000049 POLARSSL_MD_NONE=0,
50 POLARSSL_MD_MD2,
Paul Bakker17373852011-01-06 14:20:01 +000051 POLARSSL_MD_MD4,
52 POLARSSL_MD_MD5,
53 POLARSSL_MD_SHA1,
54 POLARSSL_MD_SHA224,
55 POLARSSL_MD_SHA256,
56 POLARSSL_MD_SHA384,
57 POLARSSL_MD_SHA512,
Paul Bakker61b699e2014-01-22 13:35:29 +010058 POLARSSL_MD_RIPEMD160,
Paul Bakker17373852011-01-06 14:20:01 +000059} md_type_t;
60
Paul Bakker7db01092013-09-10 11:10:57 +020061#if defined(POLARSSL_SHA512_C)
Paul Bakker1b57b062011-01-06 15:48:19 +000062#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020063#else
64#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
65#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000066
Paul Bakker17373852011-01-06 14:20:01 +000067/**
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +010068 * Opaque struct defined in md_wrap.h
Paul Bakker17373852011-01-06 14:20:01 +000069 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +010070typedef struct _md_info_t md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +000071
72/**
73 * Generic message digest context.
74 */
75typedef struct {
76 /** Information about the associated message digest */
77 const md_info_t *md_info;
78
79 /** Digest-specific context */
80 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +010081
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010082 /** HMAC part of the context */
83 void *hmac_ctx;
Paul Bakker17373852011-01-06 14:20:01 +000084} md_context_t;
85
Paul Bakker17373852011-01-06 14:20:01 +000086/**
Paul Bakker72f62662011-01-16 21:27:44 +000087 * \brief Returns the list of digests supported by the generic digest module.
88 *
89 * \return a statically allocated array of digests, the last entry
90 * is 0.
91 */
92const int *md_list( void );
93
94/**
Paul Bakker17373852011-01-06 14:20:01 +000095 * \brief Returns the message digest information associated with the
96 * given digest name.
97 *
Paul Bakker23986e52011-04-24 08:57:21 +000098 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +000099 *
100 * \return The message digest information associated with md_name or
101 * NULL if not found.
102 */
103const md_info_t *md_info_from_string( const char *md_name );
104
105/**
106 * \brief Returns the message digest information associated with the
107 * given digest type.
108 *
109 * \param md_type type of digest to search for.
110 *
111 * \return The message digest information associated with md_type or
112 * NULL if not found.
113 */
114const md_info_t *md_info_from_type( md_type_t md_type );
115
116/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100117 * \brief Initialize a md_context (as NONE)
118 * This should always be called first.
119 * Prepares the context for md_setup() or md_free().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200120 */
121void md_init( md_context_t *ctx );
122
123/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100124 * \brief Free and clear the internal structures of ctx.
125 * Can be called at any time after md_init().
126 * Mandatory once md_setup() has been called.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200127 */
128void md_free( md_context_t *ctx );
129
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100130#if ! defined(POLARSSL_DEPRECATED_REMOVED)
131#if defined(POLARSSL_DEPRECATED_WARNING)
132#define DEPRECATED __attribute__((deprecated))
133#else
134#define DEPRECATED
135#endif
136/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100137 * \brief Select MD to use and allocate internal structures.
138 * Should be called after md_init() or md_free().
139 * Makes it necessary to call md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100140 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100141 * \deprecated Superseded by md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100142 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100143 * \param ctx Context to set up.
144 * \param md_info Message digest to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100145 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100146 * \returns \c 0 on success,
147 * \c POLARSSL_ERR_MD_BAD_INPUT_DATA on parameter failure,
148 * \c POLARSSL_ERR_MD_ALLOC_FAILED memory allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100149 */
150int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) DEPRECATED;
151#undef DEPRECATED
152#endif /* POLARSSL_DEPRECATED_REMOVED */
153
Paul Bakker84bbeb52014-07-01 14:53:22 +0200154/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100155 * \brief Select MD to use and allocate internal structures.
156 * Should be called after md_init() or md_free().
157 * Makes it necessary to call md_free() later.
Paul Bakker562535d2011-01-20 16:42:01 +0000158 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100159 * \param ctx Context to set up.
160 * \param md_info Message digest to use.
161 * \param hmac 0 to save some meory is HMAC will not be use,
162 * non-zero is HMAC is going to be used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000163 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100164 * \returns \c 0 on success,
165 * \c POLARSSL_ERR_MD_BAD_INPUT_DATA on parameter failure,
166 * \c POLARSSL_ERR_MD_ALLOC_FAILED memory allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000167 */
Manuel Pégourié-Gonnardabb67442015-03-25 16:29:51 +0100168int md_setup( md_context_t *ctx, const md_info_t *md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000169
170/**
Paul Bakker17373852011-01-06 14:20:01 +0000171 * \brief Returns the size of the message digest output.
172 *
173 * \param md_info message digest info
174 *
175 * \return size of the message digest output.
176 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100177unsigned char md_get_size( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000178
179/**
180 * \brief Returns the type of the message digest output.
181 *
182 * \param md_info message digest info
183 *
184 * \return type of the message digest output.
185 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100186md_type_t md_get_type( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000187
188/**
189 * \brief Returns the name of the message digest output.
190 *
191 * \param md_info message digest info
192 *
193 * \return name of the message digest output.
194 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100195const char *md_get_name( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000196
197/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100198 * \brief Prepare the context to digest a new message.
199 * Generally called after md_setup() or md_finish().
200 * Followed by md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000201 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100202 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000203 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100204 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
205 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000206 */
Paul Bakker562535d2011-01-20 16:42:01 +0000207int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000208
209/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100210 * \brief Generic message digest process buffer
211 * Called between md_starts() and md_finish().
212 * May be called repeatedly.
Paul Bakker17373852011-01-06 14:20:01 +0000213 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100214 * \param ctx Generic message digest context
215 * \param input buffer holding the datal
216 * \param ilen length of the input data
Paul Bakker17373852011-01-06 14:20:01 +0000217 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100218 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
219 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000220 */
Paul Bakker23986e52011-04-24 08:57:21 +0000221int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000222
223/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100224 * \brief Generic message digest final digest
225 * Called after md_update().
226 * Usually followed by md_free() or md_starts().
Paul Bakker17373852011-01-06 14:20:01 +0000227 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100228 * \param ctx Generic message digest context
229 * \param output Generic message digest checksum result
Paul Bakker17373852011-01-06 14:20:01 +0000230 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100231 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
232 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000233 */
234int md_finish( md_context_t *ctx, unsigned char *output );
235
236/**
Paul Bakker17373852011-01-06 14:20:01 +0000237 * \brief Output = message_digest( input buffer )
238 *
239 * \param md_info message digest info
240 * \param input buffer holding the data
241 * \param ilen length of the input data
242 * \param output Generic message digest checksum result
243 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000244 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
245 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000246 */
Paul Bakker23986e52011-04-24 08:57:21 +0000247int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000248 unsigned char *output );
249
250/**
251 * \brief Output = message_digest( file contents )
252 *
253 * \param md_info message digest info
254 * \param path input file name
255 * \param output generic message digest checksum result
256 *
Manuel Pégourié-Gonnard932e3932015-04-03 16:37:14 +0200257 * \return 0 if successful,
258 * POLARSSL_ERR_MD_FILE_IO_ERROR if file input failed,
Paul Bakker9c021ad2011-06-09 15:55:11 +0000259 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000260 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200261int md_file( const md_info_t *md_info, const char *path,
262 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000263
264/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100265 * \brief Set HMAC key and prepare to authenticate a new message.
266 * Usually called after md_setup() or md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000267 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100268 * \param ctx HMAC context
269 * \param key HMAC secret key
270 * \param keylen length of the HMAC key
Paul Bakker17373852011-01-06 14:20:01 +0000271 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100272 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
273 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000274 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200275int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
276 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000277
278/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100279 * \brief Generic HMAC process buffer.
280 * Called between md_hmac_starts() or md_hmac_reset()
281 * and md_hmac_finish().
282 * May be called repeatedly.
Paul Bakker17373852011-01-06 14:20:01 +0000283 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100284 * \param ctx HMAC context
285 * \param input buffer holding the data
286 * \param ilen length of the input data
Paul Bakker17373852011-01-06 14:20:01 +0000287 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100288 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
289 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000290 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200291int md_hmac_update( md_context_t *ctx, const unsigned char *input,
292 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000293
294/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100295 * \brief Output HMAC.
296 * Called after md_hmac_update().
297 * Usually followed my md_hmac_reset(), md_hmac_starts(),
298 * or md_free().
Paul Bakker17373852011-01-06 14:20:01 +0000299 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100300 * \param ctx HMAC context
301 * \param output Generic HMAC checksum result
Paul Bakker17373852011-01-06 14:20:01 +0000302 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100303 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
304 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000305 */
306int md_hmac_finish( md_context_t *ctx, unsigned char *output);
307
308/**
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100309 * \brief Prepare to authenticate a new message with the same key.
310 * Called after md_hmac_finish() and before md_hmac_update().
Paul Bakker17373852011-01-06 14:20:01 +0000311 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100312 * \param ctx HMAC context to be reset
Paul Bakker17373852011-01-06 14:20:01 +0000313 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100314 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
315 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000316 */
317int md_hmac_reset( md_context_t *ctx );
318
319/**
320 * \brief Output = Generic_HMAC( hmac key, input buffer )
321 *
322 * \param md_info message digest info
323 * \param key HMAC secret key
324 * \param keylen length of the HMAC key
325 * \param input buffer holding the data
326 * \param ilen length of the input data
327 * \param output Generic HMAC-result
328 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000329 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
330 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000331 */
Paul Bakker23986e52011-04-24 08:57:21 +0000332int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
333 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000334 unsigned char *output );
335
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100336/* Internal use */
337int md_process( md_context_t *ctx, const unsigned char *data );
338
Paul Bakker17373852011-01-06 14:20:01 +0000339#ifdef __cplusplus
340}
341#endif
342
343#endif /* POLARSSL_MD_H */