PolarSSL v1.3.4
md.c
Go to the documentation of this file.
1 
30 #include "polarssl/config.h"
31 
32 #if defined(POLARSSL_MD_C)
33 
34 #include "polarssl/md.h"
35 #include "polarssl/md_wrap.h"
36 
37 #include <stdlib.h>
38 
39 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
40  !defined(EFI32)
41 #define strcasecmp _stricmp
42 #endif
43 
44 static const int supported_digests[] = {
45 
46 #if defined(POLARSSL_MD2_C)
48 #endif
49 
50 #if defined(POLARSSL_MD4_C)
52 #endif
53 
54 #if defined(POLARSSL_MD5_C)
56 #endif
57 
58 #if defined(POLARSSL_RIPEMD160_C)
60 #endif
61 
62 #if defined(POLARSSL_SHA1_C)
64 #endif
65 
66 #if defined(POLARSSL_SHA256_C)
69 #endif
70 
71 #if defined(POLARSSL_SHA512_C)
74 #endif
75 
76  0
77 };
78 
79 const int *md_list( void )
80 {
81  return supported_digests;
82 }
83 
84 const md_info_t *md_info_from_string( const char *md_name )
85 {
86  if( NULL == md_name )
87  return NULL;
88 
89  /* Get the appropriate digest information */
90 #if defined(POLARSSL_MD2_C)
91  if( !strcasecmp( "MD2", md_name ) )
93 #endif
94 #if defined(POLARSSL_MD4_C)
95  if( !strcasecmp( "MD4", md_name ) )
97 #endif
98 #if defined(POLARSSL_MD5_C)
99  if( !strcasecmp( "MD5", md_name ) )
101 #endif
102 #if defined(POLARSSL_RIPEMD160_C)
103  if( !strcasecmp( "RIPEMD160", md_name ) )
105 #endif
106 #if defined(POLARSSL_SHA1_C)
107  if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
109 #endif
110 #if defined(POLARSSL_SHA256_C)
111  if( !strcasecmp( "SHA224", md_name ) )
113  if( !strcasecmp( "SHA256", md_name ) )
115 #endif
116 #if defined(POLARSSL_SHA512_C)
117  if( !strcasecmp( "SHA384", md_name ) )
119  if( !strcasecmp( "SHA512", md_name ) )
121 #endif
122  return NULL;
123 }
124 
125 const md_info_t *md_info_from_type( md_type_t md_type )
126 {
127  switch( md_type )
128  {
129 #if defined(POLARSSL_MD2_C)
130  case POLARSSL_MD_MD2:
131  return &md2_info;
132 #endif
133 #if defined(POLARSSL_MD4_C)
134  case POLARSSL_MD_MD4:
135  return &md4_info;
136 #endif
137 #if defined(POLARSSL_MD5_C)
138  case POLARSSL_MD_MD5:
139  return &md5_info;
140 #endif
141 #if defined(POLARSSL_RIPEMD160_C)
143  return &ripemd160_info;
144 #endif
145 #if defined(POLARSSL_SHA1_C)
146  case POLARSSL_MD_SHA1:
147  return &sha1_info;
148 #endif
149 #if defined(POLARSSL_SHA256_C)
150  case POLARSSL_MD_SHA224:
151  return &sha224_info;
152  case POLARSSL_MD_SHA256:
153  return &sha256_info;
154 #endif
155 #if defined(POLARSSL_SHA512_C)
156  case POLARSSL_MD_SHA384:
157  return &sha384_info;
158  case POLARSSL_MD_SHA512:
159  return &sha512_info;
160 #endif
161  default:
162  return NULL;
163  }
164 }
165 
166 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
167 {
168  if( md_info == NULL || ctx == NULL )
170 
171  memset( ctx, 0, sizeof( md_context_t ) );
172 
173  if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
175 
176  ctx->md_info = md_info;
177 
178  md_info->starts_func( ctx->md_ctx );
179 
180  return 0;
181 }
182 
183 int md_free_ctx( md_context_t *ctx )
184 {
185  if( ctx == NULL || ctx->md_info == NULL )
187 
188  ctx->md_info->ctx_free_func( ctx->md_ctx );
189  ctx->md_ctx = NULL;
190 
191  return 0;
192 }
193 
194 int md_starts( md_context_t *ctx )
195 {
196  if( ctx == NULL || ctx->md_info == NULL )
198 
199  ctx->md_info->starts_func( ctx->md_ctx );
200 
201  return 0;
202 }
203 
204 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
205 {
206  if( ctx == NULL || ctx->md_info == NULL )
208 
209  ctx->md_info->update_func( ctx->md_ctx, input, ilen );
210 
211  return 0;
212 }
213 
214 int md_finish( md_context_t *ctx, unsigned char *output )
215 {
216  if( ctx == NULL || ctx->md_info == NULL )
218 
219  ctx->md_info->finish_func( ctx->md_ctx, output );
220 
221  return 0;
222 }
223 
224 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
225  unsigned char *output )
226 {
227  if ( md_info == NULL )
229 
230  md_info->digest_func( input, ilen, output );
231 
232  return 0;
233 }
234 
235 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
236 {
237 #if defined(POLARSSL_FS_IO)
238  int ret;
239 #endif
240 
241  if( md_info == NULL )
243 
244 #if defined(POLARSSL_FS_IO)
245  ret = md_info->file_func( path, output );
246  if( ret != 0 )
247  return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
248 
249  return( ret );
250 #else
251  ((void) path);
252  ((void) output);
253 
255 #endif
256 }
257 
258 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
259 {
260  if( ctx == NULL || ctx->md_info == NULL )
262 
263  ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
264 
265  return 0;
266 }
267 
268 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
269 {
270  if( ctx == NULL || ctx->md_info == NULL )
272 
273  ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
274 
275  return 0;
276 }
277 
278 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
279 {
280  if( ctx == NULL || ctx->md_info == NULL )
282 
283  ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
284 
285  return 0;
286 }
287 
288 int md_hmac_reset( md_context_t *ctx )
289 {
290  if( ctx == NULL || ctx->md_info == NULL )
292 
293  ctx->md_info->hmac_reset_func( ctx->md_ctx);
294 
295  return 0;
296 }
297 
298 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
299  const unsigned char *input, size_t ilen,
300  unsigned char *output )
301 {
302  if( md_info == NULL )
304 
305  md_info->hmac_func( key, keylen, input, ilen, output );
306 
307  return 0;
308 }
309 
310 int md_process( md_context_t *ctx, const unsigned char *data )
311 {
312  if( ctx == NULL || ctx->md_info == NULL )
314 
315  ctx->md_info->process_func( ctx->md_ctx, data );
316 
317  return 0;
318 }
319 
320 #endif
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_ALLOC_FAILED
Failed to allocate memory.
Definition: md.h:44
const md_info_t md5_info
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE
The selected feature is not available.
Definition: md.h:42
int md_file(const md_info_t *md_info, const char *path, unsigned char *output)
Output = message_digest( file contents )
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_process(md_context_t *ctx, const unsigned char *data)
void(* hmac_reset_func)(void *ctx)
HMAC context reset function.
Definition: md.h:110
Configuration options (set of defines)
#define POLARSSL_ERR_MD_FILE_IO_ERROR
Opening or reading of file failed.
Definition: md.h:45
const md_info_t * md_info_from_string(const char *md_name)
Returns the message digest information associated with the given digest name.
void(* hmac_starts_func)(void *ctx, const unsigned char *key, size_t keylen)
HMAC Initialisation function.
Definition: md.h:101
const md_info_t * md_info
Information about the associated message digest.
Definition: md.h:132
md_type_t
Definition: md.h:51
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define POLARSSL_ERR_MD_BAD_INPUT_DATA
Bad input parameters to function.
Definition: md.h:43
void(* digest_func)(const unsigned char *input, size_t ilen, unsigned char *output)
Generic digest function.
Definition: md.h:94
void(* starts_func)(void *ctx)
Digest initialisation function.
Definition: md.h:85
void(* finish_func)(void *ctx, unsigned char *output)
Digest finalisation function.
Definition: md.h:91
int(* file_func)(const char *path, unsigned char *output)
Generic file digest function.
Definition: md.h:98
const md_info_t sha224_info
Message digest wrappers.
void(* update_func)(void *ctx, const unsigned char *input, size_t ilen)
Digest update function.
Definition: md.h:88
const int * md_list(void)
Returns the list of digests supported by the generic digest module.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
void * md_ctx
Digest-specific context.
Definition: md.h:135
Generic message digest wrapper.
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 )
const md_info_t sha1_info
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
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:113
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
const md_info_t sha512_info
void(* hmac_finish_func)(void *ctx, unsigned char *output)
HMAC finalisation function.
Definition: md.h:107
const md_info_t sha256_info
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
const md_info_t ripemd160_info
void(* hmac_update_func)(void *ctx, const unsigned char *input, size_t ilen)
HMAC update function.
Definition: md.h:104
Message digest information.
Definition: md.h:74
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
void(* process_func)(void *ctx, const unsigned char *input)
Internal use only.
Definition: md.h:124
const md_info_t sha384_info
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
void *(* ctx_alloc_func)(void)
Allocate a new context.
Definition: md.h:118
Generic message digest context.
Definition: md.h:130
void(* ctx_free_func)(void *ctx)
Free the given context.
Definition: md.h:121