PolarSSL v1.3.9
md.c
Go to the documentation of this file.
1
30#if !defined(POLARSSL_CONFIG_FILE)
31#include "polarssl/config.h"
32#else
33#include POLARSSL_CONFIG_FILE
34#endif
35
36#if defined(POLARSSL_MD_C)
37
38#include "polarssl/md.h"
39#include "polarssl/md_wrap.h"
40
41#include <stdlib.h>
42
43#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
44 !defined(EFI32)
45#define strcasecmp _stricmp
46#endif
47
48/* Implementation that should never be optimized out by the compiler */
49static void polarssl_zeroize( void *v, size_t n ) {
50 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
53static const int supported_digests[] = {
54
55#if defined(POLARSSL_SHA512_C)
58#endif
59
60#if defined(POLARSSL_SHA256_C)
63#endif
64
65#if defined(POLARSSL_SHA1_C)
67#endif
68
69#if defined(POLARSSL_RIPEMD160_C)
71#endif
72
73#if defined(POLARSSL_MD5_C)
75#endif
76
77#if defined(POLARSSL_MD4_C)
79#endif
80
81#if defined(POLARSSL_MD2_C)
83#endif
84
86};
87
88const int *md_list( void )
89{
90 return( supported_digests );
91}
92
93const md_info_t *md_info_from_string( const char *md_name )
94{
95 if( NULL == md_name )
96 return( NULL );
97
98 /* Get the appropriate digest information */
99#if defined(POLARSSL_MD2_C)
100 if( !strcasecmp( "MD2", md_name ) )
102#endif
103#if defined(POLARSSL_MD4_C)
104 if( !strcasecmp( "MD4", md_name ) )
106#endif
107#if defined(POLARSSL_MD5_C)
108 if( !strcasecmp( "MD5", md_name ) )
110#endif
111#if defined(POLARSSL_RIPEMD160_C)
112 if( !strcasecmp( "RIPEMD160", md_name ) )
114#endif
115#if defined(POLARSSL_SHA1_C)
116 if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
118#endif
119#if defined(POLARSSL_SHA256_C)
120 if( !strcasecmp( "SHA224", md_name ) )
122 if( !strcasecmp( "SHA256", md_name ) )
124#endif
125#if defined(POLARSSL_SHA512_C)
126 if( !strcasecmp( "SHA384", md_name ) )
128 if( !strcasecmp( "SHA512", md_name ) )
130#endif
131 return( NULL );
132}
133
134const md_info_t *md_info_from_type( md_type_t md_type )
135{
136 switch( md_type )
137 {
138#if defined(POLARSSL_MD2_C)
139 case POLARSSL_MD_MD2:
140 return( &md2_info );
141#endif
142#if defined(POLARSSL_MD4_C)
143 case POLARSSL_MD_MD4:
144 return( &md4_info );
145#endif
146#if defined(POLARSSL_MD5_C)
147 case POLARSSL_MD_MD5:
148 return( &md5_info );
149#endif
150#if defined(POLARSSL_RIPEMD160_C)
152 return( &ripemd160_info );
153#endif
154#if defined(POLARSSL_SHA1_C)
155 case POLARSSL_MD_SHA1:
156 return( &sha1_info );
157#endif
158#if defined(POLARSSL_SHA256_C)
160 return( &sha224_info );
162 return( &sha256_info );
163#endif
164#if defined(POLARSSL_SHA512_C)
166 return( &sha384_info );
168 return( &sha512_info );
169#endif
170 default:
171 return( NULL );
172 }
173}
174
175void md_init( md_context_t *ctx )
176{
177 memset( ctx, 0, sizeof( md_context_t ) );
178}
179
180void md_free( md_context_t *ctx )
181{
182 if( ctx == NULL )
183 return;
184
185 if( ctx->md_ctx )
186 ctx->md_info->ctx_free_func( ctx->md_ctx );
187
188 polarssl_zeroize( ctx, sizeof( md_context_t ) );
189}
190
191int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
192{
193 if( md_info == NULL || ctx == NULL )
195
196 memset( ctx, 0, sizeof( md_context_t ) );
197
198 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
200
201 ctx->md_info = md_info;
202
203 md_info->starts_func( ctx->md_ctx );
204
205 return( 0 );
206}
207
208int md_free_ctx( md_context_t *ctx )
209{
210 md_free( ctx );
211
212 return( 0 );
213}
214
215int md_starts( md_context_t *ctx )
216{
217 if( ctx == NULL || ctx->md_info == NULL )
219
220 ctx->md_info->starts_func( ctx->md_ctx );
221
222 return( 0 );
223}
224
225int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
226{
227 if( ctx == NULL || ctx->md_info == NULL )
229
230 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
231
232 return( 0 );
233}
234
235int md_finish( md_context_t *ctx, unsigned char *output )
236{
237 if( ctx == NULL || ctx->md_info == NULL )
239
240 ctx->md_info->finish_func( ctx->md_ctx, output );
241
242 return( 0 );
243}
244
245int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
246 unsigned char *output )
247{
248 if( md_info == NULL )
250
251 md_info->digest_func( input, ilen, output );
252
253 return( 0 );
254}
255
256int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
257{
258#if defined(POLARSSL_FS_IO)
259 int ret;
260#endif
261
262 if( md_info == NULL )
264
265#if defined(POLARSSL_FS_IO)
266 ret = md_info->file_func( path, output );
267 if( ret != 0 )
268 return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
269
270 return( ret );
271#else
272 ((void) path);
273 ((void) output);
274
276#endif /* POLARSSL_FS_IO */
277}
278
279int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
280{
281 if( ctx == NULL || ctx->md_info == NULL )
283
284 ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen );
285
286 return( 0 );
287}
288
289int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
290{
291 if( ctx == NULL || ctx->md_info == NULL )
293
294 ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
295
296 return( 0 );
297}
298
299int md_hmac_finish( md_context_t *ctx, unsigned char *output )
300{
301 if( ctx == NULL || ctx->md_info == NULL )
303
304 ctx->md_info->hmac_finish_func( ctx->md_ctx, output );
305
306 return( 0 );
307}
308
309int md_hmac_reset( md_context_t *ctx )
310{
311 if( ctx == NULL || ctx->md_info == NULL )
313
314 ctx->md_info->hmac_reset_func( ctx->md_ctx );
315
316 return( 0 );
317}
318
319int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
320 const unsigned char *input, size_t ilen,
321 unsigned char *output )
322{
323 if( md_info == NULL )
325
326 md_info->hmac_func( key, keylen, input, ilen, output );
327
328 return( 0 );
329}
330
331int md_process( md_context_t *ctx, const unsigned char *data )
332{
333 if( ctx == NULL || ctx->md_info == NULL )
335
336 ctx->md_info->process_func( ctx->md_ctx, data );
337
338 return( 0 );
339}
340
341#endif /* POLARSSL_MD_C */
Configuration options (set of defines)
Generic message digest wrapper.
const md_info_t * md_info_from_string(const char *md_name)
Returns the message digest information associated with the given digest name.
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
#define POLARSSL_ERR_MD_FILE_IO_ERROR
Opening or reading of file failed.
Definition md.h:45
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
const int * md_list(void)
Returns the list of digests supported by the generic digest module.
int md_file(const md_info_t *md_info, const char *path, unsigned char *output)
Output = message_digest( file contents )
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
The selected feature is not available.
Definition md.h:42
#define POLARSSL_ERR_MD_BAD_INPUT_DATA
Bad input parameters to function.
Definition md.h:43
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
int md_process(md_context_t *ctx, const unsigned char *data)
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int md_hmac(const md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
Output = Generic_HMAC( hmac key, input buffer )
#define POLARSSL_ERR_MD_ALLOC_FAILED
Failed to allocate memory.
Definition md.h:44
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
md_type_t
Definition md.h:51
@ POLARSSL_MD_MD5
Definition md.h:55
@ POLARSSL_MD_NONE
Definition md.h:52
@ POLARSSL_MD_SHA224
Definition md.h:57
@ POLARSSL_MD_SHA1
Definition md.h:56
@ POLARSSL_MD_SHA384
Definition md.h:59
@ POLARSSL_MD_MD2
Definition md.h:53
@ POLARSSL_MD_MD4
Definition md.h:54
@ POLARSSL_MD_RIPEMD160
Definition md.h:61
@ POLARSSL_MD_SHA512
Definition md.h:60
@ POLARSSL_MD_SHA256
Definition md.h:58
Message digest wrappers.
const md_info_t md5_info
const md_info_t sha256_info
const md_info_t sha224_info
const md_info_t ripemd160_info
const md_info_t sha1_info
const md_info_t sha384_info
const md_info_t sha512_info
Generic message digest context.
Definition md.h:132
const md_info_t * md_info
Information about the associated message digest.
Definition md.h:134
void * md_ctx
Digest-specific context.
Definition md.h:137
Message digest information.
Definition md.h:74
void(* ctx_free_func)(void *ctx)
Free the given context.
Definition md.h:123
void(* update_func)(void *ctx, const unsigned char *input, size_t ilen)
Digest update function.
Definition md.h:88
void(* starts_func)(void *ctx)
Digest initialisation function.
Definition md.h:85
void(* process_func)(void *ctx, const unsigned char *input)
Internal use only.
Definition md.h:126
void(* digest_func)(const unsigned char *input, size_t ilen, unsigned char *output)
Generic digest function.
Definition md.h:94
void(* finish_func)(void *ctx, unsigned char *output)
Digest finalisation function.
Definition md.h:91
void(* hmac_finish_func)(void *ctx, unsigned char *output)
HMAC finalisation function.
Definition md.h:109
void *(* ctx_alloc_func)(void)
Allocate a new context.
Definition md.h:120
void(* hmac_reset_func)(void *ctx)
HMAC context reset function.
Definition md.h:112
int(* file_func)(const char *path, unsigned char *output)
Generic file digest function.
Definition md.h:98
void(* hmac_starts_func)(void *ctx, const unsigned char *key, size_t keylen)
HMAC Initialisation function.
Definition md.h:101
void(* hmac_update_func)(void *ctx, const unsigned char *input, size_t ilen)
HMAC update function.
Definition md.h:105
void(* hmac_func)(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
Generic HMAC function.
Definition md.h:115