mbed TLS v1.3.19
md.h
Go to the documentation of this file.
1 
26 #ifndef POLARSSL_MD_H
27 #define POLARSSL_MD_H
28 
29 #include <stddef.h>
30 
31 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
32  !defined(inline) && !defined(__cplusplus)
33 #define inline __inline
34 #endif
35 
36 #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080
37 #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100
38 #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180
39 #define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 typedef enum {
56 } md_type_t;
57 
58 #if defined(POLARSSL_SHA512_C)
59 #define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
60 #else
61 #define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
62 #endif
63 
68 typedef struct {
71 
73  const char * name;
74 
76  int size;
77 
79  void (*starts_func)( void *ctx );
80 
82  void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
83 
85  void (*finish_func)( void *ctx, unsigned char *output );
86 
88  void (*digest_func)( const unsigned char *input, size_t ilen,
89  unsigned char *output );
90 
92  int (*file_func)( const char *path, unsigned char *output );
93 
95  void (*hmac_starts_func)( void *ctx, const unsigned char *key,
96  size_t keylen );
97 
99  void (*hmac_update_func)( void *ctx, const unsigned char *input,
100  size_t ilen );
101 
103  void (*hmac_finish_func)( void *ctx, unsigned char *output);
104 
106  void (*hmac_reset_func)( void *ctx );
107 
109  void (*hmac_func)( const unsigned char *key, size_t keylen,
110  const unsigned char *input, size_t ilen,
111  unsigned char *output );
112 
114  void * (*ctx_alloc_func)( void );
115 
117  void (*ctx_free_func)( void *ctx );
118 
120  void (*process_func)( void *ctx, const unsigned char *input );
121 } md_info_t;
122 
126 typedef struct {
129 
131  void *md_ctx;
132 } md_context_t;
133 
134 #define MD_CONTEXT_T_INIT { \
135  NULL, /* md_info */ \
136  NULL, /* md_ctx */ \
137 }
138 
145 const int *md_list( void );
146 
156 const md_info_t *md_info_from_string( const char *md_name );
157 
167 const md_info_t *md_info_from_type( md_type_t md_type );
168 
172 void md_init( md_context_t *ctx );
173 
179 void md_free( md_context_t *ctx );
180 
198 int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
199 
200 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
201 #if defined(POLARSSL_DEPRECATED_WARNING)
202 #define DEPRECATED __attribute__((deprecated))
203 #else
204 #define DEPRECATED
205 #endif
206 
217 #undef DEPRECATED
218 #endif /* POLARSSL_DEPRECATED_REMOVED */
219 
227 static inline unsigned char md_get_size( const md_info_t *md_info )
228 {
229  if( md_info == NULL )
230  return( 0 );
231 
232  return md_info->size;
233 }
234 
242 static inline md_type_t md_get_type( const md_info_t *md_info )
243 {
244  if( md_info == NULL )
245  return( POLARSSL_MD_NONE );
246 
247  return md_info->type;
248 }
249 
257 static inline const char *md_get_name( const md_info_t *md_info )
258 {
259  if( md_info == NULL )
260  return( NULL );
261 
262  return md_info->name;
263 }
264 
273 int md_starts( md_context_t *ctx );
274 
285 int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
286 
296 int md_finish( md_context_t *ctx, unsigned char *output );
297 
309 int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
310  unsigned char *output );
311 
323 int md_file( const md_info_t *md_info, const char *path,
324  unsigned char *output );
325 
336 int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
337  size_t keylen );
338 
349 int md_hmac_update( md_context_t *ctx, const unsigned char *input,
350  size_t ilen );
351 
361 int md_hmac_finish( md_context_t *ctx, unsigned char *output);
362 
371 int md_hmac_reset( md_context_t *ctx );
372 
386 int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
387  const unsigned char *input, size_t ilen,
388  unsigned char *output );
389 
390 /* Internal use */
391 int md_process( md_context_t *ctx, const unsigned char *data );
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 
397 #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:227
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:242
const md_info_t * md_info
Information about the associated message digest.
Definition: md.h:128
md_type_t
Definition: md.h:45
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:131
int md_free_ctx(md_context_t *ctx) DEPRECATED
Free the message-specific context of ctx.
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:73
int size
Output length of the digest function.
Definition: md.h:76
static const char * md_get_name(const md_info_t *md_info)
Returns the name of the message digest output.
Definition: md.h:257
md_type_t type
Digest identifier.
Definition: md.h:70
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
Message digest information.
Definition: md.h:68
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
#define DEPRECATED
Definition: md.h:204
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
Generic message digest context.
Definition: md.h:126