- Added regular error codes for generic message digest layer

This commit is contained in:
Paul Bakker 2011-06-09 15:55:11 +00:00
parent c65ab340a7
commit 9c021adeff
3 changed files with 60 additions and 28 deletions

View File

@ -37,6 +37,10 @@
#endif #endif
#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
#define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */
#define POLARSSL_ERR_MD_FILE_READ_FAILED -0x5280 /**< Failure when reading from file. */
typedef enum { typedef enum {
POLARSSL_MD_NONE=0, POLARSSL_MD_NONE=0,
@ -166,7 +170,8 @@ const md_info_t *md_info_from_type( md_type_t md_type );
* be allocated, and must be freed using md_free_ctx() later. * be allocated, and must be freed using md_free_ctx() later.
* \param md_info message digest to use. * \param md_info message digest to use.
* *
* \returns \c 0 on success, \c 1 on parameter failure, \c 2 if * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
* parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
* allocation of the cipher-specific context failed. * allocation of the cipher-specific context failed.
*/ */
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ); int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
@ -177,7 +182,8 @@ int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
* *
* \param ctx Free the message-specific context * \param ctx Free the message-specific context
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_free_ctx( md_context_t *ctx ); int md_free_ctx( md_context_t *ctx );
@ -222,7 +228,8 @@ static inline const char *md_get_name( const md_info_t *md_info )
* *
* \param ctx generic message digest context. * \param ctx generic message digest context.
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_starts( md_context_t *ctx ); int md_starts( md_context_t *ctx );
@ -233,7 +240,8 @@ int md_starts( md_context_t *ctx );
* \param input buffer holding the datal * \param input buffer holding the datal
* \param ilen length of the input data * \param ilen length of the input data
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen ); int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
@ -243,7 +251,8 @@ int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
* \param ctx Generic message digest context * \param ctx Generic message digest context
* \param output Generic message digest checksum result * \param output Generic message digest checksum result
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_finish( md_context_t *ctx, unsigned char *output ); int md_finish( md_context_t *ctx, unsigned char *output );
@ -255,7 +264,8 @@ int md_finish( md_context_t *ctx, unsigned char *output );
* \param ilen length of the input data * \param ilen length of the input data
* \param output Generic message digest checksum result * \param output Generic message digest checksum result
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md( const md_info_t *md_info, const unsigned char *input, size_t ilen, int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output ); unsigned char *output );
@ -267,8 +277,9 @@ int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
* \param path input file name * \param path input file name
* \param output generic message digest checksum result * \param output generic message digest checksum result
* *
* \return 0 if successful, 1 if fopen failed, * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
* 2 if fread failed, 3 if md_info was NULL * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
* POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
*/ */
int md_file( const md_info_t *md_info, const char *path, unsigned char *output ); int md_file( const md_info_t *md_info, const char *path, unsigned char *output );
@ -279,7 +290,8 @@ int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
* \param key HMAC secret key * \param key HMAC secret key
* \param keylen length of the HMAC key * \param keylen length of the HMAC key
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen ); int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen );
@ -290,7 +302,8 @@ int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen ); int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
@ -300,7 +313,8 @@ int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
* \param ctx HMAC context * \param ctx HMAC context
* \param output Generic HMAC checksum result * \param output Generic HMAC checksum result
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_hmac_finish( md_context_t *ctx, unsigned char *output); int md_hmac_finish( md_context_t *ctx, unsigned char *output);
@ -309,7 +323,8 @@ int md_hmac_finish( md_context_t *ctx, unsigned char *output);
* *
* \param ctx HMAC context to be reset * \param ctx HMAC context to be reset
* *
* \returns 0 on success, 1 if ctx is NULL. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_hmac_reset( md_context_t *ctx ); int md_hmac_reset( md_context_t *ctx );
@ -323,7 +338,8 @@ int md_hmac_reset( md_context_t *ctx );
* \param ilen length of the input data * \param ilen length of the input data
* \param output Generic HMAC-result * \param output Generic HMAC-result
* *
* \returns 0 on success, 1 if parameter verification fails. * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
* verification fails.
*/ */
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen, int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
const unsigned char *input, size_t ilen, const unsigned char *input, size_t ilen,

View File

@ -137,6 +137,14 @@ void error_strerror( int ret, char *buf, size_t buflen )
#if defined(POLARSSL_MD_C) #if defined(POLARSSL_MD_C)
if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) ) if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
snprintf( buf, buflen, "MD - The selected feature is not available" ); snprintf( buf, buflen, "MD - The selected feature is not available" );
if( use_ret == -(POLARSSL_ERR_MD_BAD_INPUT_DATA) )
snprintf( buf, buflen, "MD - Bad input parameters to function" );
if( use_ret == -(POLARSSL_ERR_MD_ALLOC_FAILED) )
snprintf( buf, buflen, "MD - Failed to allocate memory" );
if( use_ret == -(POLARSSL_ERR_MD_FILE_OPEN_FAILED) )
snprintf( buf, buflen, "MD - Opening of file failed" );
if( use_ret == -(POLARSSL_ERR_MD_FILE_READ_FAILED) )
snprintf( buf, buflen, "MD - Failure when reading from file" );
#endif /* POLARSSL_MD_C */ #endif /* POLARSSL_MD_C */
#if defined(POLARSSL_PEM_C) #if defined(POLARSSL_PEM_C)

View File

@ -153,13 +153,13 @@ const md_info_t *md_info_from_type( md_type_t md_type )
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
{ {
if( md_info == NULL ) if( md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
if( ctx == NULL || ctx->md_ctx != NULL ) if( ctx == NULL || ctx->md_ctx != NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
return 1; return POLARSSL_ERR_MD_ALLOC_FAILED;
ctx->md_info = md_info; ctx->md_info = md_info;
@ -171,7 +171,7 @@ int md_init_ctx( md_context_t *ctx, const md_info_t *md_info )
int md_free_ctx( md_context_t *ctx ) int md_free_ctx( md_context_t *ctx )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->ctx_free_func( ctx->md_ctx ); ctx->md_info->ctx_free_func( ctx->md_ctx );
ctx->md_ctx = NULL; ctx->md_ctx = NULL;
@ -182,7 +182,7 @@ int md_free_ctx( md_context_t *ctx )
int md_starts( md_context_t *ctx ) int md_starts( md_context_t *ctx )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->starts_func( ctx->md_ctx ); ctx->md_info->starts_func( ctx->md_ctx );
@ -192,7 +192,7 @@ int md_starts( md_context_t *ctx )
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen ) int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->update_func( ctx->md_ctx, input, ilen ); ctx->md_info->update_func( ctx->md_ctx, input, ilen );
@ -202,7 +202,7 @@ int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
int md_finish( md_context_t *ctx, unsigned char *output ) int md_finish( md_context_t *ctx, unsigned char *output )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->finish_func( ctx->md_ctx, output ); ctx->md_info->finish_func( ctx->md_ctx, output );
@ -213,7 +213,7 @@ int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output ) unsigned char *output )
{ {
if ( md_info == NULL ) if ( md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
md_info->digest_func( input, ilen, output ); md_info->digest_func( input, ilen, output );
@ -222,11 +222,19 @@ int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
int md_file( const md_info_t *md_info, const char *path, unsigned char *output ) int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
{ {
int ret;
if( md_info == NULL ) if( md_info == NULL )
return 3; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
#if defined(POLARSSL_FS_IO) #if defined(POLARSSL_FS_IO)
return md_info->file_func( path, output ); ret = md_info->file_func( path, output );
if( ret == 2 )
return POLARSSL_ERR_MD_FILE_OPEN_FAILED;
if( ret == 3 )
return POLARSSL_ERR_MD_FILE_READ_FAILED;
return ret;
#else #else
((void) path); ((void) path);
((void) output); ((void) output);
@ -238,7 +246,7 @@ int md_file( const md_info_t *md_info, const char *path, unsigned char *output )
int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen ) int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen); ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen);
@ -248,7 +256,7 @@ int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen )
int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen ) int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen ); ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen );
@ -258,7 +266,7 @@ int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
int md_hmac_finish( md_context_t *ctx, unsigned char *output) int md_hmac_finish( md_context_t *ctx, unsigned char *output)
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->hmac_finish_func( ctx->md_ctx, output); ctx->md_info->hmac_finish_func( ctx->md_ctx, output);
@ -268,7 +276,7 @@ int md_hmac_finish( md_context_t *ctx, unsigned char *output)
int md_hmac_reset( md_context_t *ctx ) int md_hmac_reset( md_context_t *ctx )
{ {
if( ctx == NULL || ctx->md_info == NULL ) if( ctx == NULL || ctx->md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
ctx->md_info->hmac_reset_func( ctx->md_ctx); ctx->md_info->hmac_reset_func( ctx->md_ctx);
@ -280,7 +288,7 @@ int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
unsigned char *output ) unsigned char *output )
{ {
if( md_info == NULL ) if( md_info == NULL )
return 1; return POLARSSL_ERR_MD_BAD_INPUT_DATA;
md_info->hmac_func( key, keylen, input, ilen, output ); md_info->hmac_func( key, keylen, input, ilen, output );