aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-06-18 04:30:28 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-06-18 04:30:28 +0300
commit962e48d3ffb90308513fd8908d73753049a27f3d (patch)
treec54909153762ae8d017dc815721f6ea35fbe6086
parenttest/toolkit.py: capture stderr (diff)
downloadaes-tools-962e48d3ffb90308513fd8908d73753049a27f3d.tar.gz
aes-tools-962e48d3ffb90308513fd8908d73753049a27f3d.zip
better error messages + refactoring
-rw-r--r--cxx/include/aesnixx/error.hpp2
-rw-r--r--include/aesni/error.h29
-rw-r--r--src/aes.c72
-rw-r--r--src/box.c4
-rw-r--r--src/buffer.c14
-rw-r--r--src/error.c86
6 files changed, 123 insertions, 84 deletions
diff --git a/cxx/include/aesnixx/error.hpp b/cxx/include/aesnixx/error.hpp
index dd908c5..4c1fc98 100644
--- a/cxx/include/aesnixx/error.hpp
+++ b/cxx/include/aesnixx/error.hpp
@@ -23,7 +23,7 @@ namespace aesni
public:
ErrorDetailsThrowsInDestructor()
{
- aesni_initialize_error_details(get());
+ aesni_success(get());
}
~ErrorDetailsThrowsInDestructor()
diff --git a/include/aesni/error.h b/include/aesni/error.h
index 4fa7936..73b6696 100644
--- a/include/aesni/error.h
+++ b/include/aesni/error.h
@@ -48,7 +48,7 @@ typedef enum
AESNI_NULL_ARGUMENT_ERROR, ///< Invalid argument value NULL
AESNI_PARSE_ERROR, ///< Couldn't parse
AESNI_INVALID_PKCS7_PADDING_ERROR, ///< Invalid PKCS7 padding
- AESNI_NOT_IMPLEMENTED,
+ AESNI_NOT_IMPLEMENTED_ERROR,
}
AesNI_StatusCode;
@@ -83,19 +83,14 @@ typedef struct
union
{
+ struct { char param_name[32]; } null_arg;
struct
{
- char param_name[32]; ///< Name of the NULL argument
- }
- null_arg_error;
- ///< `NULL` argument error (AESNI_NULL_ARGUMENT_ERROR) parameters
-
- struct
- {
- char src[128]; ///< The string that failed to be parsed
+ char src[128];
+ char what[32];
}
parse_error;
- ///< Parse error (AESNI_PARSE_ERROR) parameters
+ struct { char what[128]; } not_implemented;
}
params;
}
@@ -133,7 +128,7 @@ size_t aesni_format_error(
*
* \param[out] err_details The error details structure to fill.
*/
-AesNI_StatusCode aesni_initialize_error_details(
+AesNI_StatusCode aesni_success(
AesNI_ErrorDetails* err_details);
/**
@@ -142,7 +137,7 @@ AesNI_StatusCode aesni_initialize_error_details(
* \param[out] err_details The error details structure to fill.
* \param[in] param_name The parameter name. Must not be `NULL`.
*/
-AesNI_StatusCode aesni_make_null_argument_error(
+AesNI_StatusCode aesni_error_null_argument(
AesNI_ErrorDetails* err_details,
const char* param_name);
@@ -152,20 +147,22 @@ AesNI_StatusCode aesni_make_null_argument_error(
* \param[out] err_details The error details structure to fill.
* \param[in] src The string that failed to be parsed.
*/
-AesNI_StatusCode aesni_make_parse_error(
+AesNI_StatusCode aesni_error_parse(
AesNI_ErrorDetails* err_details,
- const char* src);
+ const char* src,
+ const char* what);
/**
* \brief Builds error details from an invalid PKCS7 padding error.
*
* \param[out] err_details The error details structure to fill.
*/
-AesNI_StatusCode aesni_make_invalid_pkcs7_padding_error(
+AesNI_StatusCode aesni_error_invalid_pkcs7_padding(
AesNI_ErrorDetails* err_details);
AesNI_StatusCode aesni_error_not_implemented(
- AesNI_ErrorDetails* err_details);
+ AesNI_ErrorDetails* err_details,
+ const char* what);
#ifdef __cplusplus
}
diff --git a/src/aes.c b/src/aes.c
index 98cc9cd..7f21925 100644
--- a/src/aes.c
+++ b/src/aes.c
@@ -21,9 +21,9 @@ AesNI_StatusCode aesni_aes_format_block(
assert(block);
if (str == NULL)
- return aesni_make_null_argument_error(err_details, "str");
+ return aesni_error_null_argument(err_details, "str");
if (block == NULL)
- return aesni_make_null_argument_error(err_details, "block");
+ return aesni_error_null_argument(err_details, "block");
char* cursor = str->str;
@@ -46,9 +46,9 @@ AesNI_StatusCode aesni_aes_format_block_as_matrix(
assert(block);
if (str == NULL)
- return aesni_make_null_argument_error(err_details, "str");
+ return aesni_error_null_argument(err_details, "str");
if (block == NULL)
- return aesni_make_null_argument_error(err_details, "block");
+ return aesni_error_null_argument(err_details, "block");
char* cursor = str->str;
@@ -73,7 +73,7 @@ AesNI_StatusCode aesni_aes_print_block(
assert(block);
if (block == NULL)
- return aesni_make_null_argument_error(err_details, "block");
+ return aesni_error_null_argument(err_details, "block");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes_BlockString str;
@@ -92,7 +92,7 @@ AesNI_StatusCode aesni_aes_print_block_as_matrix(
assert(block);
if (block == NULL)
- return aesni_make_null_argument_error(err_details, "block");
+ return aesni_error_null_argument(err_details, "block");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes_BlockMatrixString str;
@@ -113,9 +113,11 @@ AesNI_StatusCode aesni_aes_parse_block(
assert(src);
if (dest == NULL)
- return aesni_make_null_argument_error(err_details, "dest");
+ return aesni_error_null_argument(err_details, "dest");
if (src == NULL)
- return aesni_make_null_argument_error(err_details, "src");
+ return aesni_error_null_argument(err_details, "src");
+
+ const char* cursor = src;
__declspec(align(16)) unsigned char bytes[16];
@@ -123,10 +125,10 @@ AesNI_StatusCode aesni_aes_parse_block(
{
int n;
unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return aesni_make_parse_error(err_details, src);
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aesni_error_parse(err_details, src, "a 128-bit block");
bytes[i] = (unsigned char) byte;
- src += n;
+ cursor += n;
}
*dest = aesni_load_block128_aligned(bytes);
@@ -150,9 +152,9 @@ AesNI_StatusCode aesni_aes192_format_key(
assert(key);
if (str == NULL)
- return aesni_make_null_argument_error(err_details, "str");
+ return aesni_error_null_argument(err_details, "str");
if (key == NULL)
- return aesni_make_null_argument_error(err_details, "key");
+ return aesni_error_null_argument(err_details, "key");
char* cursor = str->str;
@@ -185,9 +187,9 @@ AesNI_StatusCode aesni_aes256_format_key(
assert(key);
if (str == NULL)
- return aesni_make_null_argument_error(err_details, "str");
+ return aesni_error_null_argument(err_details, "str");
if (key == NULL)
- return aesni_make_null_argument_error(err_details, "key");
+ return aesni_error_null_argument(err_details, "key");
char* cursor = str->str;
@@ -225,7 +227,7 @@ AesNI_StatusCode aesni_aes192_print_key(
assert(key);
if (key == NULL)
- return aesni_make_null_argument_error(err_details, "key");
+ return aesni_error_null_argument(err_details, "key");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes192_KeyString str;
@@ -244,7 +246,7 @@ AesNI_StatusCode aesni_aes256_print_key(
assert(key);
if (key == NULL)
- return aesni_make_null_argument_error(err_details, "key");
+ return aesni_error_null_argument(err_details, "key");
AesNI_StatusCode ec = AESNI_SUCCESS;
AesNI_Aes256_KeyString str;
@@ -273,9 +275,11 @@ AesNI_StatusCode aesni_aes192_parse_key(
assert(src);
if (dest == NULL)
- return aesni_make_null_argument_error(err_details, "dest");
+ return aesni_error_null_argument(err_details, "dest");
if (src == NULL)
- return aesni_make_null_argument_error(err_details, "src");
+ return aesni_error_null_argument(err_details, "src");
+
+ const char* cursor = src;
{
__declspec(align(16)) unsigned char bytes[16];
@@ -284,10 +288,10 @@ AesNI_StatusCode aesni_aes192_parse_key(
{
int n;
unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return aesni_make_parse_error(err_details, src);
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aesni_error_parse(err_details, src, "a 192-bit block");
bytes[i] = (unsigned char) byte;
- src += n;
+ cursor += n;
}
dest->lo = aesni_load_block128_aligned(bytes);
@@ -300,10 +304,10 @@ AesNI_StatusCode aesni_aes192_parse_key(
{
int n;
unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return aesni_make_parse_error(err_details, src);
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aesni_error_parse(err_details, src, "a 192-bit block");
bytes[i] = (unsigned char) byte;
- src += n;
+ cursor += n;
}
memset(bytes + 8, 0x00, 8);
@@ -322,9 +326,11 @@ AesNI_StatusCode aesni_aes256_parse_key(
assert(src);
if (dest == NULL)
- return aesni_make_null_argument_error(err_details, "dest");
+ return aesni_error_null_argument(err_details, "dest");
if (src == NULL)
- return aesni_make_null_argument_error(err_details, "src");
+ return aesni_error_null_argument(err_details, "src");
+
+ const char* cursor = src;
{
__declspec(align(16)) unsigned char bytes[16];
@@ -333,10 +339,10 @@ AesNI_StatusCode aesni_aes256_parse_key(
{
int n;
unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return aesni_make_parse_error(err_details, src);
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aesni_error_parse(err_details, src, "a 256-bit block");
bytes[i] = (unsigned char) byte;
- src += n;
+ cursor += n;
}
dest->lo = aesni_load_block128_aligned(bytes);
@@ -349,10 +355,10 @@ AesNI_StatusCode aesni_aes256_parse_key(
{
int n;
unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return aesni_make_parse_error(err_details, src);
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aesni_error_parse(err_details, src, "a 256-bit block");
bytes[i] = (unsigned char) byte;
- src += n;
+ cursor += n;
}
dest->hi = aesni_load_block128_aligned(bytes);
diff --git a/src/box.c b/src/box.c
index 0fa7297..f49c79f 100644
--- a/src/box.c
+++ b/src/box.c
@@ -131,7 +131,7 @@ static AesNI_StatusCode aesni_box_encrypt_ctr(
AesNI_BoxBlock* output,
AesNI_ErrorDetails* err_details)
{
- return aesni_error_not_implemented(err_details);
+ return aesni_error_not_implemented(err_details, "box encryption in CTR mode");
}
typedef AesNI_StatusCode (*AesNI_BoxEncryptMode)(
@@ -254,7 +254,7 @@ static AesNI_StatusCode aesni_box_decrypt_ctr(
AesNI_BoxBlock* output,
AesNI_ErrorDetails* err_details)
{
- return aesni_error_not_implemented(err_details);
+ return aesni_error_not_implemented(err_details, "box decryption in CTR mode");
}
typedef AesNI_BoxEncryptMode AesNI_BoxDecryptMode;
diff --git a/src/buffer.c b/src/buffer.c
index 80120db..593a5b8 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -22,7 +22,7 @@ AesNI_StatusCode aesni_encrypt_buffer_ecb128(
AesNI_ErrorDetails* err_details)
{
if (dest_size == NULL)
- return aesni_make_null_argument_error(err_details, "dest_size");
+ return aesni_error_null_argument(err_details, "dest_size");
const size_t rem_size = src_size % 16;
const size_t padding_size = 16 - rem_size;
@@ -31,9 +31,9 @@ AesNI_StatusCode aesni_encrypt_buffer_ecb128(
if (dest == NULL)
return AESNI_SUCCESS;
if (src == NULL)
- return aesni_make_null_argument_error(err_details, "src");
+ return aesni_error_null_argument(err_details, "src");
if (encryption_keys == NULL)
- return aesni_make_null_argument_error(err_details, "encryption_keys");
+ return aesni_error_null_argument(err_details, "encryption_keys");
const size_t src_len = src_size / 16;
@@ -84,16 +84,16 @@ AesNI_StatusCode aesni_decrypt_buffer_ecb128(
AesNI_ErrorDetails* err_details)
{
if (dest_size == NULL)
- return aesni_make_null_argument_error(err_details, "dest_size");
+ return aesni_error_null_argument(err_details, "dest_size");
*dest_size = src_size;
if (dest == NULL)
return 0;
if (src == NULL)
- return aesni_make_null_argument_error(err_details, "src");
+ return aesni_error_null_argument(err_details, "src");
if (decryption_keys == NULL)
- return aesni_make_null_argument_error(err_details, "decryption_keys");
+ return aesni_error_null_argument(err_details, "decryption_keys");
const size_t src_len = src_size / 16;
@@ -112,7 +112,7 @@ AesNI_StatusCode aesni_decrypt_buffer_ecb128(
unsigned char padding_size = get_pkcs7_padding_size(padding);
if (padding_size == 0)
- return aesni_make_invalid_pkcs7_padding_error(err_details);
+ return aesni_error_invalid_pkcs7_padding(err_details);
memcpy(dest, padding, 16 - padding_size);
*dest_size -= padding_size;
diff --git a/src/error.c b/src/error.c
index 830520c..96f0e01 100644
--- a/src/error.c
+++ b/src/error.c
@@ -13,9 +13,15 @@
#include <stdlib.h>
#include <string.h>
-static const char* err_msgs[] =
+static void aesni_fill_string(char* dest, size_t dest_size, const char* src)
{
- "OK",
+ strncpy(dest, src, dest_size);
+ dest[dest_size - 1] = '\0';
+}
+
+static const char* aesni_strerror_messages[] =
+{
+ "Success",
"Invalid argument value NULL",
"Couldn't parse",
"Invalid PKCS7 padding (wrong key?)",
@@ -24,7 +30,7 @@ static const char* err_msgs[] =
const char* aesni_strerror(AesNI_StatusCode ec)
{
- return err_msgs[ec];
+ return aesni_strerror_messages[ec];
}
static size_t aesni_format_error_strerror(
@@ -35,11 +41,10 @@ static size_t aesni_format_error_strerror(
const AesNI_StatusCode ec = aesni_get_error_code(err_details);
const char* const msg = aesni_strerror(ec);
- if (dest == NULL && dest_size == 0)
+ if (dest == NULL)
return strlen(msg) + 1;
- strncpy(dest, msg, dest_size);
- dest[dest_size - 1] = '\0';
+ aesni_fill_string(dest, dest_size, msg);
return strlen(dest);
}
@@ -49,7 +54,7 @@ static size_t aesni_format_null_argument_error(
size_t dest_size)
{
static const char* const fmt = "Invalid argument value NULL (argument name: '%s')";
- const char* const param_name = err_details->params.null_arg_error.param_name;
+ const char* const param_name = err_details->params.null_arg.param_name;
if (dest == NULL && dest_size == 0)
return _snprintf(NULL, 0, fmt, param_name) + 1;
@@ -63,8 +68,24 @@ static size_t aesni_format_parse_error(
char* dest,
size_t dest_size)
{
- static const char* const fmt = "Couldn't parse '%s'";
+ static const char* const fmt = "Couldn't parse '%s' (possibly not complete input) as %s";
const char* const src = err_details->params.parse_error.src;
+ const char* const what = err_details->params.parse_error.what;
+
+ if (dest == NULL)
+ return _snprintf(NULL, 0, fmt, src, what) + 1;
+
+ _snprintf(dest, dest_size, fmt, src, what);
+ return strlen(dest);
+}
+
+static size_t aesni_format_not_implemented_error(
+ const AesNI_ErrorDetails* err_details,
+ char* dest,
+ size_t dest_size)
+{
+ static const char* const fmt = "Not implemented: %s";
+ const char* const src = err_details->params.not_implemented.what;
if (dest == NULL)
return _snprintf(NULL, 0, fmt, src) + 1;
@@ -79,9 +100,9 @@ static AesNI_ErrorFormatter err_formatters[] =
{
&aesni_format_error_strerror,
&aesni_format_null_argument_error,
+ &aesni_format_parse_error,
&aesni_format_error_strerror,
- &aesni_format_error_strerror,
- &aesni_format_error_strerror,
+ &aesni_format_not_implemented_error,
};
size_t aesni_format_error(
@@ -104,47 +125,62 @@ static AesNI_StatusCode aesni_make_error(
return err_details->ec = ec;
}
-AesNI_StatusCode aesni_initialize_error_details(
+AesNI_StatusCode aesni_success(
AesNI_ErrorDetails* err_details)
{
return aesni_make_error(err_details, AESNI_SUCCESS);
}
-AesNI_StatusCode aesni_make_null_argument_error(
+AesNI_StatusCode aesni_error_null_argument(
AesNI_ErrorDetails* err_details,
const char* param_name)
{
- AesNI_StatusCode status = aesni_make_error(
- err_details, AESNI_NULL_ARGUMENT_ERROR);
+ AesNI_StatusCode status = aesni_make_error(err_details, AESNI_NULL_ARGUMENT_ERROR);
- const size_t param_name_size = sizeof(err_details->params.null_arg_error.param_name);
- strncpy(err_details->params.null_arg_error.param_name, param_name, param_name_size);
- err_details->params.null_arg_error.param_name[param_name_size - 1] = '\0';
+ if (err_details != NULL)
+ aesni_fill_string(
+ err_details->params.null_arg.param_name,
+ sizeof(err_details->params.null_arg.param_name), param_name);
return status;
}
-AesNI_StatusCode aesni_make_parse_error(
+AesNI_StatusCode aesni_error_parse(
AesNI_ErrorDetails* err_details,
- const char* src)
+ const char* src,
+ const char* what)
{
AesNI_StatusCode status = aesni_make_error(err_details, AESNI_PARSE_ERROR);
- const size_t src_size = sizeof(err_details->params.parse_error.src);
- strncpy(err_details->params.parse_error.src, src, src_size);
- err_details->params.parse_error.src[src_size - 1] = '\0';
+ if (err_details != NULL)
+ {
+ aesni_fill_string(
+ err_details->params.parse_error.src,
+ sizeof(err_details->params.parse_error.src), src);
+ aesni_fill_string(
+ err_details->params.parse_error.what,
+ sizeof(err_details->params.parse_error.what), what);
+ }
return status;
}
-AesNI_StatusCode aesni_make_invalid_pkcs7_padding_error(
+AesNI_StatusCode aesni_error_invalid_pkcs7_padding(
AesNI_ErrorDetails* err_details)
{
return aesni_make_error(err_details, AESNI_INVALID_PKCS7_PADDING_ERROR);
}
AesNI_StatusCode aesni_error_not_implemented(
- AesNI_ErrorDetails* err_details)
+ AesNI_ErrorDetails* err_details,
+ const char* what)
{
- return aesni_make_error(err_details, AESNI_NOT_IMPLEMENTED);
+ AesNI_StatusCode status = aesni_make_error(err_details, AESNI_NOT_IMPLEMENTED_ERROR);
+
+ if (err_details != NULL)
+ aesni_fill_string(
+ err_details->params.not_implemented.what,
+ sizeof(err_details->params.not_implemented.what), what);
+
+ return status;
}