PolarSSL v1.1.5
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
40 #define strcasecmp _stricmp
41 #endif
42 
43 static const int supported_digests[] = {
44 
45 #if defined(POLARSSL_MD2_C)
47 #endif
48 
49 #if defined(POLARSSL_MD4_C)
51 #endif
52 
53 #if defined(POLARSSL_MD5_C)
55 #endif
56 
57 #if defined(POLARSSL_SHA1_C)
59 #endif
60 
61 #if defined(POLARSSL_SHA2_C)
64 #endif
65 
66 #if defined(POLARSSL_SHA4_C)
69 #endif
70 
71  0
72 };
73 
74 const int *md_list( void )
75 {
76  return supported_digests;
77 }
78 
79 const md_info_t *md_info_from_string( const char *md_name )
80 {
81  if( NULL == md_name )
82  return NULL;
83 
84  /* Get the appropriate digest information */
85 #if defined(POLARSSL_MD2_C)
86  if( !strcasecmp( "MD2", md_name ) )
88 #endif
89 #if defined(POLARSSL_MD4_C)
90  if( !strcasecmp( "MD4", md_name ) )
92 #endif
93 #if defined(POLARSSL_MD5_C)
94  if( !strcasecmp( "MD5", md_name ) )
96 #endif
97 #if defined(POLARSSL_SHA1_C)
98  if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
100 #endif
101 #if defined(POLARSSL_SHA2_C)
102  if( !strcasecmp( "SHA224", md_name ) )
104  if( !strcasecmp( "SHA256", md_name ) )
106 #endif
107 #if defined(POLARSSL_SHA4_C)
108  if( !strcasecmp( "SHA384", md_name ) )
110  if( !strcasecmp( "SHA512", md_name ) )
112 #endif
113  return NULL;
114 }
115 
116 const md_info_t *md_info_from_type( md_type_t md_type )
117 {
118  switch( md_type )
119  {
120 #if defined(POLARSSL_MD2_C)
121  case POLARSSL_MD_MD2:
122  return &md2_info;
123 #endif
124 #if defined(POLARSSL_MD4_C)
125  case POLARSSL_MD_MD4:
126  return &md4_info;
127 #endif
128 #if defined(POLARSSL_MD5_C)
129  case POLARSSL_MD_MD5:
130  return &md5_info;
131 #endif
132 #if defined(POLARSSL_SHA1_C)
133  case POLARSSL_MD_SHA1:
134  return &sha1_info;
135 #endif
136 #if defined(POLARSSL_SHA2_C)
137  case POLARSSL_MD_SHA224:
138  return &sha224_info;
139  case POLARSSL_MD_SHA256:
140  return &sha256_info;
141 #endif
142 #if defined(POLARSSL_SHA4_C)
143  case POLARSSL_MD_SHA384:
144  return &sha384_info;
145  case POLARSSL_MD_SHA512:
146  return &sha512_info;
147 #endif
148  default:
149  return NULL;
150  }
151 }
152 
153 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
154 {
155  if( md_info == NULL || ctx == NULL )
157 
158  memset( ctx, 0, sizeof( md_context_t ) );
159 
160  if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
162 
163  ctx->md_info = md_info;
164 
165  md_info->starts_func( ctx->md_ctx );
166 
167  return 0;
168 }
169 
170 int md_free_ctx( md_context_t *ctx )
171 {
172  if( ctx == NULL || ctx->md_info == NULL )
174 
175  ctx->md_info->ctx_free_func( ctx->md_ctx );
176  ctx->md_ctx = NULL;
177 
178  return 0;
179 }
180 
181 int md_starts( md_context_t *ctx )
182 {
183  if( ctx == NULL || ctx->md_info == NULL )
185 
186  ctx->md_info->starts_func( ctx->md_ctx );
187 
188  return 0;
189 }
190 
191 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
192 {
193  if( ctx == NULL || ctx->md_info == NULL )
195 
196  ctx->md_info->update_func( ctx->md_ctx, input, ilen );
197 
198  return 0;
199 }
200 
201 int md_finish( md_context_t *ctx, unsigned char *output )
202 {
203  if( ctx == NULL || ctx->md_info == NULL )
205 
206  ctx->md_info->finish_func( ctx->md_ctx, output );
207 
208  return 0;
209 }
210 
211 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
212  unsigned char *output )
213 {
214  if ( md_info == NULL )
216 
217  md_info->digest_func( input, ilen, output );
218 
219  return 0;
220 }
221 
222 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
223 {
224 #if defined(POLARSSL_FS_IO)
225  int ret;
226 #endif
227 
228  if( md_info == NULL )
230 
231 #if defined(POLARSSL_FS_IO)
232  ret = md_info->file_func( path, output );
233  if( ret != 0 )
234  return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
235 
236  return( ret );
237 #else
238  ((void) path);
239  ((void) output);
240 
242 #endif
243 }
244 
245 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
246 {
247  if( ctx == NULL || ctx->md_info == NULL )
249 
250  ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
251 
252  return 0;
253 }
254 
255 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
256 {
257  if( ctx == NULL || ctx->md_info == NULL )
259 
260  ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
261 
262  return 0;
263 }
264 
265 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
266 {
267  if( ctx == NULL || ctx->md_info == NULL )
269 
270  ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
271 
272  return 0;
273 }
274 
275 int md_hmac_reset( md_context_t *ctx )
276 {
277  if( ctx == NULL || ctx->md_info == NULL )
279 
280  ctx->md_info->hmac_reset_func( ctx->md_ctx);
281 
282  return 0;
283 }
284 
285 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
286  const unsigned char *input, size_t ilen,
287  unsigned char *output )
288 {
289  if( md_info == NULL )
291 
292  md_info->hmac_func( key, keylen, input, ilen, output );
293 
294  return 0;
295 }
296 
297 #endif