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 */
49 static void polarssl_zeroize( void *v, size_t n ) {
50  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51 }
52 
53 static 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 
88 const int *md_list( void )
89 {
90  return( supported_digests );
91 }
92 
93 const 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 
134 const 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)
159  case POLARSSL_MD_SHA224:
160  return( &sha224_info );
161  case POLARSSL_MD_SHA256:
162  return( &sha256_info );
163 #endif
164 #if defined(POLARSSL_SHA512_C)
165  case POLARSSL_MD_SHA384:
166  return( &sha384_info );
167  case POLARSSL_MD_SHA512:
168  return( &sha512_info );
169 #endif
170  default:
171  return( NULL );
172  }
173 }
174 
175 void md_init( md_context_t *ctx )
176 {
177  memset( ctx, 0, sizeof( md_context_t ) );
178 }
179 
180 void 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 
191 int 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 
208 int md_free_ctx( md_context_t *ctx )
209 {
210  md_free( ctx );
211 
212  return( 0 );
213 }
214 
215 int 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 
225 int 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 
235 int 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 
245 int 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 
256 int 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 
279 int 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 
289 int 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 
299 int 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 
309 int 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 
319 int 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 
331 int 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 */