blob: 6728d8d33a5391cca1d7c5b0ddabc6b9b6a4c333 [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;
81} md_context_t;
82
83#define MD_CONTEXT_T_INIT { \
84 NULL, /* md_info */ \
85 NULL, /* md_ctx */ \
86}
87
Paul Bakker17373852011-01-06 14:20:01 +000088/**
Paul Bakker72f62662011-01-16 21:27:44 +000089 * \brief Returns the list of digests supported by the generic digest module.
90 *
91 * \return a statically allocated array of digests, the last entry
92 * is 0.
93 */
94const int *md_list( void );
95
96/**
Paul Bakker17373852011-01-06 14:20:01 +000097 * \brief Returns the message digest information associated with the
98 * given digest name.
99 *
Paul Bakker23986e52011-04-24 08:57:21 +0000100 * \param md_name Name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000101 *
102 * \return The message digest information associated with md_name or
103 * NULL if not found.
104 */
105const md_info_t *md_info_from_string( const char *md_name );
106
107/**
108 * \brief Returns the message digest information associated with the
109 * given digest type.
110 *
111 * \param md_type type of digest to search for.
112 *
113 * \return The message digest information associated with md_type or
114 * NULL if not found.
115 */
116const md_info_t *md_info_from_type( md_type_t md_type );
117
118/**
Paul Bakker84bbeb52014-07-01 14:53:22 +0200119 * \brief Initialize a md_context (as NONE)
120 */
121void md_init( md_context_t *ctx );
122
123/**
124 * \brief Free and clear the message-specific context of ctx.
125 * Freeing ctx itself remains the responsibility of the
126 * caller.
127 */
128void md_free( md_context_t *ctx );
129
130/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200131 * \brief Initialises and fills the message digest context structure
132 * with the appropriate values.
Paul Bakker562535d2011-01-20 16:42:01 +0000133 *
Paul Bakker84bbeb52014-07-01 14:53:22 +0200134 * \note Currently also clears structure. In future versions you
135 * will be required to call md_init() on the structure
136 * first.
137 *
Paul Bakker562535d2011-01-20 16:42:01 +0000138 * \param ctx context to initialise. May not be NULL. The
139 * digest-specific context (ctx->md_ctx) must be NULL. It will
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100140 * be allocated, and must be freed using md_free() later.
Paul Bakker562535d2011-01-20 16:42:01 +0000141 * \param md_info message digest to use.
142 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000143 * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
144 * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
Paul Bakker20281562011-11-11 10:34:04 +0000145 * allocation of the digest-specific context failed.
Paul Bakker562535d2011-01-20 16:42:01 +0000146 */
147int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
148
149/**
Paul Bakker17373852011-01-06 14:20:01 +0000150 * \brief Returns the size of the message digest output.
151 *
152 * \param md_info message digest info
153 *
154 * \return size of the message digest output.
155 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100156unsigned char md_get_size( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000157
158/**
159 * \brief Returns the type of the message digest output.
160 *
161 * \param md_info message digest info
162 *
163 * \return type of the message digest output.
164 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100165md_type_t md_get_type( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000166
167/**
168 * \brief Returns the name of the message digest output.
169 *
170 * \param md_info message digest info
171 *
172 * \return name of the message digest output.
173 */
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +0100174const char *md_get_name( const md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000175
176/**
Paul Bakker562535d2011-01-20 16:42:01 +0000177 * \brief Set-up the given context for a new message digest
Paul Bakker17373852011-01-06 14:20:01 +0000178 *
Paul Bakker562535d2011-01-20 16:42:01 +0000179 * \param ctx generic message digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000180 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000181 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
182 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000183 */
Paul Bakker562535d2011-01-20 16:42:01 +0000184int md_starts( md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000185
186/**
187 * \brief Generic message digest process buffer
188 *
189 * \param ctx Generic message digest context
190 * \param input buffer holding the datal
191 * \param ilen length of the input data
192 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000193 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
194 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000195 */
Paul Bakker23986e52011-04-24 08:57:21 +0000196int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000197
198/**
199 * \brief Generic message digest final digest
200 *
201 * \param ctx Generic message digest context
202 * \param output Generic message digest checksum result
203 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000204 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
205 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000206 */
207int md_finish( md_context_t *ctx, unsigned char *output );
208
209/**
Paul Bakker17373852011-01-06 14:20:01 +0000210 * \brief Output = message_digest( input buffer )
211 *
212 * \param md_info message digest info
213 * \param input buffer holding the data
214 * \param ilen length of the input data
215 * \param output Generic message digest checksum result
216 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000217 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
218 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000219 */
Paul Bakker23986e52011-04-24 08:57:21 +0000220int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000221 unsigned char *output );
222
223/**
224 * \brief Output = message_digest( file contents )
225 *
226 * \param md_info message digest info
227 * \param path input file name
228 * \param output generic message digest checksum result
229 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000230 * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
231 * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
232 * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000233 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200234int md_file( const md_info_t *md_info, const char *path,
235 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000236
237/**
238 * \brief Generic HMAC context setup
239 *
Paul Bakker17373852011-01-06 14:20:01 +0000240 * \param ctx HMAC context to be initialized
241 * \param key HMAC secret key
242 * \param keylen length of the HMAC key
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 Bakkerb9e4e2c2014-05-01 14:18:25 +0200247int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
248 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000249
250/**
251 * \brief Generic HMAC process buffer
252 *
253 * \param ctx HMAC context
254 * \param input buffer holding the data
255 * \param ilen length of the input data
256 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000257 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
258 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000259 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200260int md_hmac_update( md_context_t *ctx, const unsigned char *input,
261 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000262
263/**
264 * \brief Generic HMAC final digest
265 *
266 * \param ctx HMAC context
267 * \param output Generic HMAC checksum result
268 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000269 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
270 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000271 */
272int md_hmac_finish( md_context_t *ctx, unsigned char *output);
273
274/**
275 * \brief Generic HMAC context reset
276 *
277 * \param ctx HMAC context to be reset
278 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000279 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
280 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000281 */
282int md_hmac_reset( md_context_t *ctx );
283
284/**
285 * \brief Output = Generic_HMAC( hmac key, input buffer )
286 *
287 * \param md_info message digest info
288 * \param key HMAC secret key
289 * \param keylen length of the HMAC key
290 * \param input buffer holding the data
291 * \param ilen length of the input data
292 * \param output Generic HMAC-result
293 *
Paul Bakker9c021ad2011-06-09 15:55:11 +0000294 * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
295 * verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000296 */
Paul Bakker23986e52011-04-24 08:57:21 +0000297int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
298 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000299 unsigned char *output );
300
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100301/* Internal use */
302int md_process( md_context_t *ctx, const unsigned char *data );
303
Paul Bakker17373852011-01-06 14:20:01 +0000304#ifdef __cplusplus
305}
306#endif
307
308#endif /* POLARSSL_MD_H */