PolarSSL v1.3.3
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_SHA1_C)
60 #endif
61 
62 #if defined(POLARSSL_SHA256_C)
65 #endif
66 
67 #if defined(POLARSSL_SHA512_C)
70 #endif
71 
72  0
73 };
74 
75 const int *md_list( void )
76 {
77  return supported_digests;
78 }
79 
80 const md_info_t *md_info_from_string( const char *md_name )
81 {
82  if( NULL == md_name )
83  return NULL;
84 
85  /* Get the appropriate digest information */
86 #if defined(POLARSSL_MD2_C)
87  if( !strcasecmp( "MD2", md_name ) )
89 #endif
90 #if defined(POLARSSL_MD4_C)
91  if( !strcasecmp( "MD4", md_name ) )
93 #endif
94 #if defined(POLARSSL_MD5_C)
95  if( !strcasecmp( "MD5", md_name ) )
97 #endif
98 #if defined(POLARSSL_SHA1_C)
99  if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
101 #endif
102 #if defined(POLARSSL_SHA256_C)
103  if( !strcasecmp( "SHA224", md_name ) )
105  if( !strcasecmp( "SHA256", md_name ) )
107 #endif
108 #if defined(POLARSSL_SHA512_C)
109  if( !strcasecmp( "SHA384", md_name ) )
111  if( !strcasecmp( "SHA512", md_name ) )
113 #endif
114  return NULL;
115 }
116 
117 const md_info_t *md_info_from_type( md_type_t md_type )
118 {
119  switch( md_type )
120  {
121 #if defined(POLARSSL_MD2_C)
122  case POLARSSL_MD_MD2:
123  return &md2_info;
124 #endif
125 #if defined(POLARSSL_MD4_C)
126  case POLARSSL_MD_MD4:
127  return &md4_info;
128 #endif
129 #if defined(POLARSSL_MD5_C)
130  case POLARSSL_MD_MD5:
131  return &md5_info;
132 #endif
133 #if defined(POLARSSL_SHA1_C)
134  case POLARSSL_MD_SHA1:
135  return &sha1_info;
136 #endif
137 #if defined(POLARSSL_SHA256_C)
138  case POLARSSL_MD_SHA224:
139  return &sha224_info;
140  case POLARSSL_MD_SHA256:
141  return &sha256_info;
142 #endif
143 #if defined(POLARSSL_SHA512_C)
144  case POLARSSL_MD_SHA384:
145  return &sha384_info;
146  case POLARSSL_MD_SHA512:
147  return &sha512_info;
148 #endif
149  default:
150  return NULL;
151  }
152 }
153 
154 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
155 {
156  if( md_info == NULL || ctx == NULL )
158 
159  memset( ctx, 0, sizeof( md_context_t ) );
160 
161  if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
163 
164  ctx->md_info = md_info;
165 
166  md_info->starts_func( ctx->md_ctx );
167 
168  return 0;
169 }
170 
171 int md_free_ctx( md_context_t *ctx )
172 {
173  if( ctx == NULL || ctx->md_info == NULL )
175 
176  ctx->md_info->ctx_free_func( ctx->md_ctx );
177  ctx->md_ctx = NULL;
178 
179  return 0;
180 }
181 
182 int md_starts( md_context_t *ctx )
183 {
184  if( ctx == NULL || ctx->md_info == NULL )
186 
187  ctx->md_info->starts_func( ctx->md_ctx );
188 
189  return 0;
190 }
191 
192 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
193 {
194  if( ctx == NULL || ctx->md_info == NULL )
196 
197  ctx->md_info->update_func( ctx->md_ctx, input, ilen );
198 
199  return 0;
200 }
201 
202 int md_finish( md_context_t *ctx, unsigned char *output )
203 {
204  if( ctx == NULL || ctx->md_info == NULL )
206 
207  ctx->md_info->finish_func( ctx->md_ctx, output );
208 
209  return 0;
210 }
211 
212 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
213  unsigned char *output )
214 {
215  if ( md_info == NULL )
217 
218  md_info->digest_func( input, ilen, output );
219 
220  return 0;
221 }
222 
223 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
224 {
225 #if defined(POLARSSL_FS_IO)
226  int ret;
227 #endif
228 
229  if( md_info == NULL )
231 
232 #if defined(POLARSSL_FS_IO)
233  ret = md_info->file_func( path, output );
234  if( ret != 0 )
235  return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
236 
237  return( ret );
238 #else
239  ((void) path);
240  ((void) output);
241 
243 #endif
244 }
245 
246 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
247 {
248  if( ctx == NULL || ctx->md_info == NULL )
250 
251  ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
252 
253  return 0;
254 }
255 
256 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
257 {
258  if( ctx == NULL || ctx->md_info == NULL )
260 
261  ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
262 
263  return 0;
264 }
265 
266 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
267 {
268  if( ctx == NULL || ctx->md_info == NULL )
270 
271  ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
272 
273  return 0;
274 }
275 
276 int md_hmac_reset( md_context_t *ctx )
277 {
278  if( ctx == NULL || ctx->md_info == NULL )
280 
281  ctx->md_info->hmac_reset_func( ctx->md_ctx);
282 
283  return 0;
284 }
285 
286 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
287  const unsigned char *input, size_t ilen,
288  unsigned char *output )
289 {
290  if( md_info == NULL )
292 
293  md_info->hmac_func( key, keylen, input, ilen, output );
294 
295  return 0;
296 }
297 
298 int md_process( md_context_t *ctx, const unsigned char *data )
299 {
300  if( ctx == NULL || ctx->md_info == NULL )
302 
303  ctx->md_info->process_func( ctx->md_ctx, data );
304 
305  return 0;
306 }
307 
308 #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:109
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:100
const md_info_t * md_info
Information about the associated message digest.
Definition: md.h:131
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:93
void(* starts_func)(void *ctx)
Digest initialisation function.
Definition: md.h:84
void(* finish_func)(void *ctx, unsigned char *output)
Digest finalisation function.
Definition: md.h:90
int(* file_func)(const char *path, unsigned char *output)
Generic file digest function.
Definition: md.h:97
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:87
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:134
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:112
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:106
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.
void(* hmac_update_func)(void *ctx, const unsigned char *input, size_t ilen)
HMAC update function.
Definition: md.h:103
Message digest information.
Definition: md.h:73
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:123
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:117
Generic message digest context.
Definition: md.h:129
void(* ctx_free_func)(void *ctx)
Free the given context.
Definition: md.h:120