PolarSSL v1.3.8
md.h
Go to the documentation of this file.
1 
29 #ifndef POLARSSL_MD_H
30 #define POLARSSL_MD_H
31 
32 #include <string.h>
33 
34 #if defined(_MSC_VER) && !defined(inline)
35 #define inline _inline
36 #else
37 #if defined(__ARMCC_VERSION) && !defined(inline)
38 #define inline __inline
39 #endif /* __ARMCC_VERSION */
40 #endif /*_MSC_VER */
41 
42 #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080
43 #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100
44 #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180
45 #define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 typedef enum {
62 } md_type_t;
63 
64 #if defined(POLARSSL_SHA512_C)
65 #define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
66 #else
67 #define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
68 #endif
69 
74 typedef struct {
77 
79  const char * name;
80 
82  int size;
83 
85  void (*starts_func)( void *ctx );
86 
88  void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
89 
91  void (*finish_func)( void *ctx, unsigned char *output );
92 
94  void (*digest_func)( const unsigned char *input, size_t ilen,
95  unsigned char *output );
96 
98  int (*file_func)( const char *path, unsigned char *output );
99 
101  void (*hmac_starts_func)( void *ctx, const unsigned char *key,
102  size_t keylen );
103 
105  void (*hmac_update_func)( void *ctx, const unsigned char *input,
106  size_t ilen );
107 
109  void (*hmac_finish_func)( void *ctx, unsigned char *output);
110 
112  void (*hmac_reset_func)( void *ctx );
113 
115  void (*hmac_func)( const unsigned char *key, size_t keylen,
116  const unsigned char *input, size_t ilen,
117  unsigned char *output );
118 
120  void * (*ctx_alloc_func)( void );
121 
123  void (*ctx_free_func)( void *ctx );
124 
126  void (*process_func)( void *ctx, const unsigned char *input );
127 } md_info_t;
128 
132 typedef struct {
135 
137  void *md_ctx;
138 } md_context_t;
139 
140 #define MD_CONTEXT_T_INIT { \
141  NULL, /* md_info */ \
142  NULL, /* md_ctx */ \
143 }
144 
151 const int *md_list( void );
152 
162 const md_info_t *md_info_from_string( const char *md_name );
163 
173 const md_info_t *md_info_from_type( md_type_t md_type );
174 
178 void md_init( md_context_t *ctx );
179 
185 void md_free( md_context_t *ctx );
186 
204 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
205 
216 int md_free_ctx( md_context_t *ctx );
217 
225 static inline unsigned char md_get_size( const md_info_t *md_info )
226 {
227  if( md_info == NULL )
228  return( 0 );
229 
230  return md_info->size;
231 }
232 
240 static inline md_type_t md_get_type( const md_info_t *md_info )
241 {
242  if( md_info == NULL )
243  return( POLARSSL_MD_NONE );
244 
245  return md_info->type;
246 }
247 
255 static inline const char *md_get_name( const md_info_t *md_info )
256 {
257  if( md_info == NULL )
258  return( NULL );
259 
260  return md_info->name;
261 }
262 
271 int md_starts( md_context_t *ctx );
272 
283 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
284 
294 int md_finish( md_context_t *ctx, unsigned char *output );
295 
307 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
308  unsigned char *output );
309 
321 int md_file( const md_info_t *md_info, const char *path,
322  unsigned char *output );
323 
334 int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
335  size_t keylen );
336 
347 int md_hmac_update( md_context_t *ctx, const unsigned char *input,
348  size_t ilen );
349 
359 int md_hmac_finish( md_context_t *ctx, unsigned char *output);
360 
369 int md_hmac_reset( md_context_t *ctx );
370 
384 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
385  const unsigned char *input, size_t ilen,
386  unsigned char *output );
387 
388 /* Internal use */
389 int md_process( md_context_t *ctx, const unsigned char *data );
390 
391 #ifdef __cplusplus
392 }
393 #endif
394 
395 #endif /* POLARSSL_MD_H */
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
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)
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
Definition: md.h:225
const md_info_t * md_info_from_string(const char *md_name)
Returns the message digest information associated with the given digest name.
static md_type_t md_get_type(const md_info_t *md_info)
Returns the type of the message digest output.
Definition: md.h:240
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.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
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
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 )
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
const char * name
Name of the message digest.
Definition: md.h:79
int size
Output length of the digest function.
Definition: md.h:82
static const char * md_get_name(const md_info_t *md_info)
Returns the name of the message digest output.
Definition: md.h:255
md_type_t type
Digest identifier.
Definition: md.h:76
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.
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.
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
Generic message digest context.
Definition: md.h:132