PolarSSL v1.2.11
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 /* Implementation that should never be optimized out by the compiler */
44 static void polarssl_zeroize( void *v, size_t n ) {
45  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
46 }
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_SHA1_C)
64 #endif
65 
66 #if defined(POLARSSL_SHA2_C)
69 #endif
70 
71 #if defined(POLARSSL_SHA4_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_SHA1_C)
103  if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) )
105 #endif
106 #if defined(POLARSSL_SHA2_C)
107  if( !strcasecmp( "SHA224", md_name ) )
109  if( !strcasecmp( "SHA256", md_name ) )
111 #endif
112 #if defined(POLARSSL_SHA4_C)
113  if( !strcasecmp( "SHA384", md_name ) )
115  if( !strcasecmp( "SHA512", md_name ) )
117 #endif
118  return NULL;
119 }
120 
121 const md_info_t *md_info_from_type( md_type_t md_type )
122 {
123  switch( md_type )
124  {
125 #if defined(POLARSSL_MD2_C)
126  case POLARSSL_MD_MD2:
127  return &md2_info;
128 #endif
129 #if defined(POLARSSL_MD4_C)
130  case POLARSSL_MD_MD4:
131  return &md4_info;
132 #endif
133 #if defined(POLARSSL_MD5_C)
134  case POLARSSL_MD_MD5:
135  return &md5_info;
136 #endif
137 #if defined(POLARSSL_SHA1_C)
138  case POLARSSL_MD_SHA1:
139  return &sha1_info;
140 #endif
141 #if defined(POLARSSL_SHA2_C)
142  case POLARSSL_MD_SHA224:
143  return &sha224_info;
144  case POLARSSL_MD_SHA256:
145  return &sha256_info;
146 #endif
147 #if defined(POLARSSL_SHA4_C)
148  case POLARSSL_MD_SHA384:
149  return &sha384_info;
150  case POLARSSL_MD_SHA512:
151  return &sha512_info;
152 #endif
153  default:
154  return NULL;
155  }
156 }
157 
158 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
159 {
160  if( md_info == NULL || ctx == NULL )
162 
163  memset( ctx, 0, sizeof( md_context_t ) );
164 
165  if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
167 
168  ctx->md_info = md_info;
169 
170  md_info->starts_func( ctx->md_ctx );
171 
172  return 0;
173 }
174 
175 int md_free_ctx( md_context_t *ctx )
176 {
177  if( ctx == NULL || ctx->md_info == NULL )
179 
180  ctx->md_info->ctx_free_func( ctx->md_ctx );
181 
182  polarssl_zeroize( ctx, sizeof( md_context_t ) );
183 
184  return 0;
185 }
186 
187 int md_starts( md_context_t *ctx )
188 {
189  if( ctx == NULL || ctx->md_info == NULL )
191 
192  ctx->md_info->starts_func( ctx->md_ctx );
193 
194  return 0;
195 }
196 
197 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
198 {
199  if( ctx == NULL || ctx->md_info == NULL )
201 
202  ctx->md_info->update_func( ctx->md_ctx, input, ilen );
203 
204  return 0;
205 }
206 
207 int md_finish( md_context_t *ctx, unsigned char *output )
208 {
209  if( ctx == NULL || ctx->md_info == NULL )
211 
212  ctx->md_info->finish_func( ctx->md_ctx, output );
213 
214  return 0;
215 }
216 
217 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
218  unsigned char *output )
219 {
220  if ( md_info == NULL )
222 
223  md_info->digest_func( input, ilen, output );
224 
225  return 0;
226 }
227 
228 int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
229 {
230 #if defined(POLARSSL_FS_IO)
231  int ret;
232 #endif
233 
234  if( md_info == NULL )
236 
237 #if defined(POLARSSL_FS_IO)
238  ret = md_info->file_func( path, output );
239  if( ret != 0 )
240  return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret );
241 
242  return( ret );
243 #else
244  ((void) path);
245  ((void) output);
246 
248 #endif
249 }
250 
251 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
252 {
253  if( ctx == NULL || ctx->md_info == NULL )
255 
256  ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
257 
258  return 0;
259 }
260 
261 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
262 {
263  if( ctx == NULL || ctx->md_info == NULL )
265 
266  ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
267 
268  return 0;
269 }
270 
271 int md_hmac_finish( md_context_t *ctx, unsigned char *output)
272 {
273  if( ctx == NULL || ctx->md_info == NULL )
275 
276  ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
277 
278  return 0;
279 }
280 
281 int md_hmac_reset( md_context_t *ctx )
282 {
283  if( ctx == NULL || ctx->md_info == NULL )
285 
286  ctx->md_info->hmac_reset_func( ctx->md_ctx);
287 
288  return 0;
289 }
290 
291 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
292  const unsigned char *input, size_t ilen,
293  unsigned char *output )
294 {
295  if( md_info == NULL )
297 
298  md_info->hmac_func( key, keylen, input, ilen, output );
299 
300  return 0;
301 }
302 
303 #endif