PolarSSL v1.3.7
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 static const int supported_digests[] = {
49 
50 #if defined(POLARSSL_MD2_C)
52 #endif
53 
54 #if defined(POLARSSL_MD4_C)
56 #endif
57 
58 #if defined(POLARSSL_MD5_C)
60 #endif
61 
62 #if defined(POLARSSL_RIPEMD160_C)
64 #endif
65 
66 #if defined(POLARSSL_SHA1_C)
68 #endif
69 
70 #if defined(POLARSSL_SHA256_C)
73 #endif
74 
75 #if defined(POLARSSL_SHA512_C)
78 #endif
79 
80  0
81 };
82 
83 const int *md_list( void )
84 {
85  return supported_digests;
86 }
87 
88 const md_info_t *md_info_from_string( const char *md_name )
89 {
90  if( NULL == md_name )
91  return NULL;
92 
93  /* Get the appropriate digest information */
94 #if defined(POLARSSL_MD2_C)
95  if( !strcasecmp( "MD2", md_name ) )
97 #endif
98 #if defined(POLARSSL_MD4_C)
99  if( !strcasecmp( "MD4", md_name ) )
101 #endif
102 #if defined(POLARSSL_MD5_C)
103  if( !strcasecmp( "MD5", md_name ) )
105 #endif
106 #if defined(POLARSSL_RIPEMD160_C)
107  if( !strcasecmp( "RIPEMD160", md_name ) )
109 #endif
110 #if defined(POLARSSL_SHA1_C)
111  if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
113 #endif
114 #if defined(POLARSSL_SHA256_C)
115  if( !strcasecmp( "SHA224", md_name ) )
117  if( !strcasecmp( "SHA256", md_name ) )
119 #endif
120 #if defined(POLARSSL_SHA512_C)
121  if( !strcasecmp( "SHA384", md_name ) )
123  if( !strcasecmp( "SHA512", md_name ) )
125 #endif
126  return NULL;
127 }
128 
129 const md_info_t *md_info_from_type( md_type_t md_type )
130 {
131  switch( md_type )
132  {
133 #if defined(POLARSSL_MD2_C)
134  case POLARSSL_MD_MD2:
135  return &md2_info;
136 #endif
137 #if defined(POLARSSL_MD4_C)
138  case POLARSSL_MD_MD4:
139  return &md4_info;
140 #endif
141 #if defined(POLARSSL_MD5_C)
142  case POLARSSL_MD_MD5:
143  return &md5_info;
144 #endif
145 #if defined(POLARSSL_RIPEMD160_C)
147  return &ripemd160_info;
148 #endif
149 #if defined(POLARSSL_SHA1_C)
150  case POLARSSL_MD_SHA1:
151  return &sha1_info;
152 #endif
153 #if defined(POLARSSL_SHA256_C)
154  case POLARSSL_MD_SHA224:
155  return &sha224_info;
156  case POLARSSL_MD_SHA256:
157  return &sha256_info;
158 #endif
159 #if defined(POLARSSL_SHA512_C)
160  case POLARSSL_MD_SHA384:
161  return &sha384_info;
162  case POLARSSL_MD_SHA512:
163  return &sha512_info;
164 #endif
165  default:
166  return NULL;
167  }
168 }
169 
170 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
171 {
172  if( md_info == NULL || ctx == NULL )
174 
175  memset( ctx, 0, sizeof( md_context_t ) );
176 
177  if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
179 
180  ctx->md_info = md_info;
181 
182  md_info->starts_func( ctx->md_ctx );
183 
184  return 0;
185 }
186 
187 int md_free_ctx( md_context_t *ctx )
188 {
189  if( ctx == NULL || ctx->md_info == NULL )
191 
192  ctx->md_info->ctx_free_func( ctx->md_ctx );
193  ctx->md_ctx = NULL;
194 
195  return 0;
196 }
197 
198 int md_starts( md_context_t *ctx )
199 {
200  if( ctx == NULL || ctx->md_info == NULL )
202 
203  ctx->md_info->starts_func( ctx->md_ctx );
204 
205  return 0;
206 }
207 
208 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
209 {
210  if( ctx == NULL || ctx->md_info == NULL )
212 
213  ctx->md_info->update_func( ctx->md_ctx, input, ilen );
214 
215  return 0;
216 }
217 
218 int md_finish( md_context_t *ctx, unsigned char *output )
219 {
220  if( ctx == NULL || ctx->md_info == NULL )
222 
223  ctx->md_info->finish_func( ctx->md_ctx, output );
224 
225  return 0;
226 }
227 
228 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
229  unsigned char *output )
230 {
231  if ( md_info == NULL )
233 
234  md_info->digest_func( input, ilen, output );
235 
236  return 0;
237 }
238 
239 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
240 {
241 #if defined(POLARSSL_FS_IO)
242  int ret;
243 #endif
244 
245  if( md_info == NULL )
247 
248 #if defined(POLARSSL_FS_IO)
249  ret = md_info->file_func( path, output );
250  if( ret != 0 )
251  return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
252 
253  return( ret );
254 #else
255  ((void) path);
256  ((void) output);
257 
259 #endif /* POLARSSL_FS_IO */
260 }
261 
262 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
263 {
264  if( ctx == NULL || ctx->md_info == NULL )
266 
267  ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
268 
269  return 0;
270 }
271 
272 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
273 {
274  if( ctx == NULL || ctx->md_info == NULL )
276 
277  ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
278 
279  return 0;
280 }
281 
282 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
283 {
284  if( ctx == NULL || ctx->md_info == NULL )
286 
287  ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
288 
289  return 0;
290 }
291 
292 int md_hmac_reset( md_context_t *ctx )
293 {
294  if( ctx == NULL || ctx->md_info == NULL )
296 
297  ctx->md_info->hmac_reset_func( ctx->md_ctx);
298 
299  return 0;
300 }
301 
302 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
303  const unsigned char *input, size_t ilen,
304  unsigned char *output )
305 {
306  if( md_info == NULL )
308 
309  md_info->hmac_func( key, keylen, input, ilen, output );
310 
311  return 0;
312 }
313 
314 int md_process( md_context_t *ctx, const unsigned char *data )
315 {
316  if( ctx == NULL || ctx->md_info == NULL )
318 
319  ctx->md_info->process_func( ctx->md_ctx, data );
320 
321  return 0;
322 }
323 
324 #endif /* POLARSSL_MD_C */
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:112
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:134
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:137
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:115
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:109
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:105
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:126
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:120
Generic message digest context.
Definition: md.h:132
void(* ctx_free_func)(void *ctx)
Free the given context.
Definition: md.h:123