blob: 13250dd8f3ff9afb38b333d3cd047ef3981e3eb1 [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 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#ifndef POLARSSL_MD_H
31#define POLARSSL_MD_H
32
33typedef enum {
34 POLARSSL_MD_MD2=0,
35 POLARSSL_MD_MD4,
36 POLARSSL_MD_MD5,
37 POLARSSL_MD_SHA1,
38 POLARSSL_MD_SHA224,
39 POLARSSL_MD_SHA256,
40 POLARSSL_MD_SHA384,
41 POLARSSL_MD_SHA512,
42} md_type_t;
43
Paul Bakker1b57b062011-01-06 15:48:19 +000044#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
45
Paul Bakker17373852011-01-06 14:20:01 +000046/**
47 * Message digest information. Allows message digest functions to be called
48 * in a generic way.
49 */
50typedef struct {
51 /** Digest identifier */
52 md_type_t type;
53
54 /** Name of the message digest */
55 const char * name;
56
57 /** Output length of the digest function */
58 int size;
59
60 /** Digest initialisation function */
61 void (*starts_func)( void *ctx );
62
63 /** Digest update function */
64 void (*update_func)( void *ctx, const unsigned char *input, int ilen );
65
66 /** Digest finalisation function */
67 void (*finish_func)( void *ctx, unsigned char *output );
68
69 /** Generic digest function */
70 void (*digest_func)( const unsigned char *input, int ilen,
71 unsigned char *output );
72
73 /** Generic file digest function */
74 int (*file_func)( const char *path, unsigned char *output );
75
76 /** HMAC Initialisation function */
77 void (*hmac_starts_func)( void *ctx, const unsigned char *key, int keylen );
78
79 /** HMAC update function */
80 void (*hmac_update_func)( void *ctx, const unsigned char *input, int ilen );
81
82 /** HMAC finalisation function */
83 void (*hmac_finish_func)( void *ctx, unsigned char *output);
84
85 /** HMAC context reset function */
86 void (*hmac_reset_func)( void *ctx );
87
88 /** Generic HMAC function */
89 void (*hmac_func)( const unsigned char *key, int keylen,
90 const unsigned char *input, int ilen,
91 unsigned char *output );
92
93 /** Allocate a new context */
94 void * (*ctx_alloc_func)( void );
95
96 /** Free the given context */
97 void (*ctx_free_func)( void *ctx );
98
99} md_info_t;
100
101/**
102 * Generic message digest context.
103 */
104typedef struct {
105 /** Information about the associated message digest */
106 const md_info_t *md_info;
107
108 /** Digest-specific context */
109 void *md_ctx;
110} md_context_t;
111
112#define MD_CONTEXT_T_INIT { \
113 NULL, /* md_info */ \
114 NULL, /* md_ctx */ \
115}
116
117#ifdef __cplusplus
118extern "C" {
119#endif
120
121/**
122 * \brief Returns the message digest information associated with the
123 * given digest name.
124 *
125 * \param md_name Name of the digest to search for.
126 *
127 * \return The message digest information associated with md_name or
128 * NULL if not found.
129 */
130const md_info_t *md_info_from_string( const char *md_name );
131
132/**
133 * \brief Returns the message digest information associated with the
134 * given digest type.
135 *
136 * \param md_type type of digest to search for.
137 *
138 * \return The message digest information associated with md_type or
139 * NULL if not found.
140 */
141const md_info_t *md_info_from_type( md_type_t md_type );
142
143/**
144 * \brief Returns the size of the message digest output.
145 *
146 * \param md_info message digest info
147 *
148 * \return size of the message digest output.
149 */
150static inline unsigned char md_get_size ( const md_info_t *md_info)
151{
152 return md_info->size;
153}
154
155/**
156 * \brief Returns the type of the message digest output.
157 *
158 * \param md_info message digest info
159 *
160 * \return type of the message digest output.
161 */
162static inline md_type_t md_get_type ( const md_info_t *md_info )
163{
164 return md_info->type;
165}
166
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 */
174static inline const char *md_get_name ( const md_info_t *md_info )
175{
176 return md_info->name;
177}
178
179/**
180 * \brief Generic message digest context setup.
181 *
182 * \param md_info message digest info
183 * \param ctx generic message digest context. May not be NULL. The
184 * digest-specific context (ctx->md_ctx) must be NULL. It will
185 * be allocated, and must be freed using md_free() later.
186 *
187 * \returns 0 on success, 1 if parameter verification fails.
188 */
189int md_starts( const md_info_t *md_info, md_context_t *ctx );
190
191/**
192 * \brief Generic message digest process buffer
193 *
194 * \param ctx Generic message digest context
195 * \param input buffer holding the datal
196 * \param ilen length of the input data
197 *
198 * \returns 0 on success, 1 if parameter verification fails.
199 */
200int md_update( md_context_t *ctx, const unsigned char *input, int ilen );
201
202/**
203 * \brief Generic message digest final digest
204 *
205 * \param ctx Generic message digest context
206 * \param output Generic message digest checksum result
207 *
208 * \returns 0 on success, 1 if parameter verification fails.
209 */
210int md_finish( md_context_t *ctx, unsigned char *output );
211
212/**
213 * \brief Free the message-specific context of ctx. Freeing ctx itself
214 * remains the responsibility of the caller.
215 *
216 * \param ctx Free the -specific context
217 * \param output Generic message digest checksum result
218 *
219 * \returns 0 on success, 1 if parameter verification fails.
220 */
221int md_free_ctx( md_context_t *ctx );
222
223/**
224 * \brief Output = message_digest( input buffer )
225 *
226 * \param md_info message digest info
227 * \param input buffer holding the data
228 * \param ilen length of the input data
229 * \param output Generic message digest checksum result
230 *
231 * \returns 0 on success, 1 if parameter verification fails.
232 */
233int md( const md_info_t *md_info, const unsigned char *input, int ilen,
234 unsigned char *output );
235
236/**
237 * \brief Output = message_digest( file contents )
238 *
239 * \param md_info message digest info
240 * \param path input file name
241 * \param output generic message digest checksum result
242 *
243 * \return 0 if successful, 1 if fopen failed,
244 * 2 if fread failed, 3 if md_info was NULL
245 */
246int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
247
248/**
249 * \brief Generic HMAC context setup
250 *
251 * \param md_info message digest info
252 * \param ctx HMAC context to be initialized
253 * \param key HMAC secret key
254 * \param keylen length of the HMAC key
255 *
256 * \returns 0 on success, 1 if parameter verification fails.
257 */
258int md_hmac_starts( const md_info_t *md_info, md_context_t *ctx,
259 const unsigned char *key, int keylen );
260
261/**
262 * \brief Generic HMAC process buffer
263 *
264 * \param ctx HMAC context
265 * \param input buffer holding the data
266 * \param ilen length of the input data
267 *
268 * \returns 0 on success, 1 if parameter verification fails.
269 */
270int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen );
271
272/**
273 * \brief Generic HMAC final digest
274 *
275 * \param ctx HMAC context
276 * \param output Generic HMAC checksum result
277 *
278 * \returns 0 on success, 1 if parameter verification fails.
279 */
280int md_hmac_finish( md_context_t *ctx, unsigned char *output);
281
282/**
283 * \brief Generic HMAC context reset
284 *
285 * \param ctx HMAC context to be reset
286 *
287 * \returns 0 on success, 1 if ctx is NULL.
288 */
289int md_hmac_reset( md_context_t *ctx );
290
291/**
292 * \brief Output = Generic_HMAC( hmac key, input buffer )
293 *
294 * \param md_info message digest info
295 * \param key HMAC secret key
296 * \param keylen length of the HMAC key
297 * \param input buffer holding the data
298 * \param ilen length of the input data
299 * \param output Generic HMAC-result
300 *
301 * \returns 0 on success, 1 if parameter verification fails.
302 */
303int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen,
304 const unsigned char *input, int ilen,
305 unsigned char *output );
306
307#ifdef __cplusplus
308}
309#endif
310
311#endif /* POLARSSL_MD_H */