From 9c021adefff3c5ceaf14966f8ae5d0826dff1b5b Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Thu, 9 Jun 2011 15:55:11 +0000
Subject: [PATCH] - Added regular error codes for generic message digest layer
---
include/polarssl/md.h | 42 +++++++++++++++++++++++++++++-------------
library/error.c | 8 ++++++++
library/md.c | 38 +++++++++++++++++++++++---------------
3 files changed, 60 insertions(+), 28 deletions(-)
diff --git a/include/polarssl/md.h b/include/polarssl/md.h
index 7914d8894..94b85c384 100644
--- a/include/polarssl/md.h
+++ b/include/polarssl/md.h
@@ -37,6 +37,10 @@
#endif
#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 {
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.
* \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.
*/
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
*
- * \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 );
@@ -222,7 +228,8 @@ static inline const char *md_get_name( const md_info_t *md_info )
*
* \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 );
@@ -233,7 +240,8 @@ int md_starts( md_context_t *ctx );
* \param input buffer holding the datal
* \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 );
@@ -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 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 );
@@ -255,7 +264,8 @@ int md_finish( md_context_t *ctx, unsigned char *output );
* \param ilen length of the input data
* \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,
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 output generic message digest checksum result
*
- * \return 0 if successful, 1 if fopen failed,
- * 2 if fread failed, 3 if md_info was NULL
+ * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
+ * 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 );
@@ -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 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 );
@@ -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 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 );
@@ -300,7 +313,8 @@ int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen )
* \param ctx HMAC context
* \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);
@@ -309,7 +323,8 @@ int md_hmac_finish( md_context_t *ctx, unsigned char *output);
*
* \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 );
@@ -323,7 +338,8 @@ int md_hmac_reset( md_context_t *ctx );
* \param ilen length of the input data
* \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,
const unsigned char *input, size_t ilen,
diff --git a/library/error.c b/library/error.c
index eb03ce15b..c9a48db36 100644
--- a/library/error.c
+++ b/library/error.c
@@ -137,6 +137,14 @@ void error_strerror( int ret, char *buf, size_t buflen )
#if defined(POLARSSL_MD_C)
if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
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 */
#if defined(POLARSSL_PEM_C)
diff --git a/library/md.c b/library/md.c
index cb720bb81..51fb82e15 100644
--- a/library/md.c
+++ b/library/md.c
@@ -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 )
{
if( md_info == NULL )
- return 1;
+ return POLARSSL_ERR_MD_BAD_INPUT_DATA;
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 )
- return 1;
+ return POLARSSL_ERR_MD_ALLOC_FAILED;
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 )
{
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_ctx = NULL;
@@ -182,7 +182,7 @@ int md_free_ctx( md_context_t *ctx )
int md_starts( md_context_t *ctx )
{
if( ctx == NULL || ctx->md_info == NULL )
- return 1;
+ return POLARSSL_ERR_MD_BAD_INPUT_DATA;
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 )
{
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 );
@@ -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 )
{
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 );
@@ -213,7 +213,7 @@ int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
unsigned char *output )
{
if ( md_info == NULL )
- return 1;
+ return POLARSSL_ERR_MD_BAD_INPUT_DATA;
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 ret;
+
if( md_info == NULL )
- return 3;
+ return POLARSSL_ERR_MD_BAD_INPUT_DATA;
#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
((void) path);
((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 )
{
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);
@@ -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 )
{
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 );
@@ -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)
{
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);
@@ -268,7 +276,7 @@ int md_hmac_finish( md_context_t *ctx, unsigned char *output)
int md_hmac_reset( md_context_t *ctx )
{
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);
@@ -280,7 +288,7 @@ int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
unsigned char *output )
{
if( md_info == NULL )
- return 1;
+ return POLARSSL_ERR_MD_BAD_INPUT_DATA;
md_info->hmac_func( key, keylen, input, ilen, output );