00001
00029 #ifndef POLARSSL_MD_H
00030 #define POLARSSL_MD_H
00031
00032 #include <string.h>
00033
00034 #if defined(_MSC_VER) && !defined(inline)
00035 #define inline _inline
00036 #else
00037 #if defined(__ARMCC_VERSION) && !defined(inline)
00038 #define inline __inline
00039 #endif
00040 #endif
00041
00042 #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080
00043 #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100
00044 #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180
00045 #define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200
00047 #ifdef __cplusplus
00048 extern "C" {
00049 #endif
00050
00051 typedef enum {
00052 POLARSSL_MD_NONE=0,
00053 POLARSSL_MD_MD2,
00054 POLARSSL_MD_MD4,
00055 POLARSSL_MD_MD5,
00056 POLARSSL_MD_SHA1,
00057 POLARSSL_MD_SHA224,
00058 POLARSSL_MD_SHA256,
00059 POLARSSL_MD_SHA384,
00060 POLARSSL_MD_SHA512,
00061 } md_type_t;
00062
00063 #if defined(POLARSSL_SHA512_C)
00064 #define POLARSSL_MD_MAX_SIZE 64
00065 #else
00066 #define POLARSSL_MD_MAX_SIZE 32
00067 #endif
00068
00073 typedef struct {
00075 md_type_t type;
00076
00078 const char * name;
00079
00081 int size;
00082
00084 void (*starts_func)( void *ctx );
00085
00087 void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
00088
00090 void (*finish_func)( void *ctx, unsigned char *output );
00091
00093 void (*digest_func)( const unsigned char *input, size_t ilen,
00094 unsigned char *output );
00095
00097 int (*file_func)( const char *path, unsigned char *output );
00098
00100 void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen );
00101
00103 void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen );
00104
00106 void (*hmac_finish_func)( void *ctx, unsigned char *output);
00107
00109 void (*hmac_reset_func)( void *ctx );
00110
00112 void (*hmac_func)( const unsigned char *key, size_t keylen,
00113 const unsigned char *input, size_t ilen,
00114 unsigned char *output );
00115
00117 void * (*ctx_alloc_func)( void );
00118
00120 void (*ctx_free_func)( void *ctx );
00121
00123 void (*process_func)( void *ctx, const unsigned char *input );
00124 } md_info_t;
00125
00129 typedef struct {
00131 const md_info_t *md_info;
00132
00134 void *md_ctx;
00135 } md_context_t;
00136
00137 #define MD_CONTEXT_T_INIT { \
00138 NULL, \
00139 NULL, \
00140 }
00141
00148 const int *md_list( void );
00149
00159 const md_info_t *md_info_from_string( const char *md_name );
00160
00170 const md_info_t *md_info_from_type( md_type_t md_type );
00171
00185 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
00186
00196 int md_free_ctx( md_context_t *ctx );
00197
00205 static inline unsigned char md_get_size( const md_info_t *md_info )
00206 {
00207 if( md_info == NULL )
00208 return( 0 );
00209
00210 return md_info->size;
00211 }
00212
00220 static inline md_type_t md_get_type( const md_info_t *md_info )
00221 {
00222 if( md_info == NULL )
00223 return( POLARSSL_MD_NONE );
00224
00225 return md_info->type;
00226 }
00227
00235 static inline const char *md_get_name( const md_info_t *md_info )
00236 {
00237 if( md_info == NULL )
00238 return( NULL );
00239
00240 return md_info->name;
00241 }
00242
00251 int md_starts( md_context_t *ctx );
00252
00263 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
00264
00274 int md_finish( md_context_t *ctx, unsigned char *output );
00275
00287 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
00288 unsigned char *output );
00289
00301 int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
00302
00313 int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
00314
00325 int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
00326
00336 int md_hmac_finish( md_context_t *ctx, unsigned char *output);
00337
00346 int md_hmac_reset( md_context_t *ctx );
00347
00361 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
00362 const unsigned char *input, size_t ilen,
00363 unsigned char *output );
00364
00365
00366 int md_process( md_context_t *ctx, const unsigned char *data );
00367
00368 #ifdef __cplusplus
00369 }
00370 #endif
00371
00372 #endif