blob: db857019a12b8f0004fd1712a05feef277714392 [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/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200117 * \brief Initialize a md_context (as NONE)
118 */
119void md_init( md_context_t *ctx );
120
121/**
122 * \brief Free and clear the message-specific context of ctx.
123 * Freeing ctx itself remains the responsibility of the
124 * caller.
125 */
126void md_free( md_context_t *ctx );
127
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100128#if ! defined(POLARSSL_DEPRECATED_REMOVED)
129#if defined(POLARSSL_DEPRECATED_WARNING)
130#define DEPRECATED __attribute__((deprecated))
131#else
132#define DEPRECATED
133#endif
134/**
135 * \brief Initialises and fills the message digest context structure
136 * with the appropriate values.
137 *
138 * \deprecated Superseded by md_setup() in 2.0.0
139 *
140 * \param ctx context to initialise. May not be NULL. The
141 * digest-specific context (ctx->md_ctx) must be NULL. It will
142 * be allocated, and must be freed using md_free() later.
143 * \param md_info message digest to use.
144 *
145 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
146 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
147 * allocation of the digest-specific context failed.
148 */
149int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) DEPRECATED;
150#undef DEPRECATED
151#endif /* POLARSSL_DEPRECATED_REMOVED */
152
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200154 * \brief Initialises and fills the message digest context structure
155 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000156 *
157 * \param ctx context to initialise. May not be NULL. The
158 * digest-specific context (ctx->md_ctx) must be NULL. It will
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100159 * be allocated, and must be freed using md_free() later.
Paul Bakker562535d2011-01-20 16:42:01 +0000160 * \param md_info message digest to use.
Manuel Pégourié-Gonnard4063ceb2015-03-25 16:08:53 +0100161 * \param hmac non-zero if you want to use this context for hmac too,
162 * zero otherwise (saves some memory).
Paul Bakker562535d2011-01-20 16:42:01 +0000163 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000164 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
165 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000166 * allocation of the digest-specific context failed.
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/**
Paul Bakker562535d2011-01-20 16:42:01 +0000198 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000199 *
Paul Bakker562535d2011-01-20 16:42:01 +0000200 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000201 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000202 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
203 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000204 */
Paul Bakker562535d2011-01-20 16:42:01 +0000205int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000206
207/**
208 * \brief Generic message digest process buffer
209 *
210 * \param ctx Generic message digest context
211 * \param input buffer holding the datal
212 * \param ilen length of the input data
213 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000214 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
215 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000216 */
Paul Bakker23986e52011-04-24 08:57:21 +0000217int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000218
219/**
220 * \brief Generic message digest final digest
221 *
222 * \param ctx Generic message digest context
223 * \param output Generic message digest checksum result
224 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000225 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
226 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000227 */
228int md_finish( md_context_t *ctx, unsigned char *output );
229
230/**
Paul Bakker17373852011-01-06 14:20:01 +0000231 * \brief Output = message_digest( input buffer )
232 *
233 * \param md_info message digest info
234 * \param input buffer holding the data
235 * \param ilen length of the input data
236 * \param output Generic message digest checksum result
237 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000238 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
239 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000240 */
Paul Bakker23986e52011-04-24 08:57:21 +0000241int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000242 unsigned char *output );
243
244/**
245 * \brief Output = message_digest( file contents )
246 *
247 * \param md_info message digest info
248 * \param path input file name
249 * \param output generic message digest checksum result
250 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000251 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
252 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
253 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000254 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200255int md_file( const md_info_t *md_info, const char *path,
256 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000257
258/**
259 * \brief Generic HMAC context setup
260 *
Paul Bakker17373852011-01-06 14:20:01 +0000261 * \param ctx HMAC context to be initialized
262 * \param key HMAC secret key
263 * \param keylen length of the HMAC key
264 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000265 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
266 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000267 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200268int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
269 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000270
271/**
272 * \brief Generic HMAC process buffer
273 *
274 * \param ctx HMAC context
275 * \param input buffer holding the data
276 * \param ilen length of the input data
277 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000278 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
279 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000280 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200281int md_hmac_update( md_context_t *ctx, const unsigned char *input,
282 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000283
284/**
285 * \brief Generic HMAC final digest
286 *
287 * \param ctx HMAC context
288 * \param output Generic HMAC checksum result
289 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000290 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
291 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000292 */
293int md_hmac_finish( md_context_t *ctx, unsigned char *output);
294
295/**
296 * \brief Generic HMAC context reset
297 *
298 * \param ctx HMAC context to be reset
299 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000300 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
301 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000302 */
303int md_hmac_reset( md_context_t *ctx );
304
305/**
306 * \brief Output = Generic_HMAC( hmac key, input buffer )
307 *
308 * \param md_info message digest info
309 * \param key HMAC secret key
310 * \param keylen length of the HMAC key
311 * \param input buffer holding the data
312 * \param ilen length of the input data
313 * \param output Generic HMAC-result
314 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000315 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
316 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000317 */
Paul Bakker23986e52011-04-24 08:57:21 +0000318int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
319 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000320 unsigned char *output );
321
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100322/* Internal use */
323int md_process( md_context_t *ctx, const unsigned char *data );
324
Paul Bakker17373852011-01-06 14:20:01 +0000325#ifdef __cplusplus
326}
327#endif
328
329#endif /* POLARSSL_MD_H */