aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-06-15 01:04:48 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-06-15 01:04:48 +0300
commitc11a0bd2abef4c9536432664600ce6b6ed6ccbe4 (patch)
tree91b8a928c82aeabc48788752944c2269d4c63b79 /src
parentupdate the docs (diff)
downloadaes-tools-c11a0bd2abef4c9536432664600ce6b6ed6ccbe4.tar.gz
aes-tools-c11a0bd2abef4c9536432664600ce6b6ed6ccbe4.zip
error handling refactoring + more docs
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c57
-rw-r--r--src/error.c41
2 files changed, 32 insertions, 66 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 71ebad2..74ff519 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -13,7 +13,7 @@
static unsigned char FULL_BLOCK_PADDING[16] = { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 };
-int aesni_encrypt_buffer_ecb128(
+AesNI_StatusCode aesni_encrypt_buffer_ecb128(
const void* src,
size_t src_size,
void* dest,
@@ -22,29 +22,18 @@ int aesni_encrypt_buffer_ecb128(
AesNI_ErrorDetails* err_details)
{
if (dest_size == NULL)
- {
- aesni_make_error_null_argument(err_details, "dest_size");
- return 1;
- }
+ return aesni_make_error_null_argument(err_details, "dest_size");
const size_t rem_size = src_size % 16;
const size_t padding_size = 16 - rem_size;
*dest_size = src_size + padding_size;
if (dest == NULL)
- return 0;
-
+ return AESNI_SUCCESS;
if (src == NULL)
- {
- aesni_make_error_null_argument(err_details, "src");
- return 1;
- }
-
+ return aesni_make_error_null_argument(err_details, "src");
if (key_schedule == NULL)
- {
- aesni_make_error_null_argument(err_details, "key_schedule");
- return 1;
- }
+ return aesni_make_error_null_argument(err_details, "key_schedule");
const size_t src_len = src_size / 16;
@@ -71,7 +60,7 @@ int aesni_encrypt_buffer_ecb128(
AesNI_Block128 ciphertext = aesni_encrypt_block_ecb128(plaintext, key_schedule);
aesni_store_block128(dest, ciphertext);
- return 0;
+ return AESNI_SUCCESS;
}
static unsigned char get_pkcs7_padding_size(const unsigned char* padding)
@@ -86,7 +75,7 @@ static unsigned char get_pkcs7_padding_size(const unsigned char* padding)
return padding[15];
}
-int aesni_decrypt_buffer_ecb128(
+AesNI_StatusCode aesni_decrypt_buffer_ecb128(
const void* src,
size_t src_size,
void* dest,
@@ -95,27 +84,16 @@ int aesni_decrypt_buffer_ecb128(
AesNI_ErrorDetails* err_details)
{
if (dest_size == NULL)
- {
- aesni_make_error_null_argument(err_details, "dest_size");
- return 1;
- }
+ return aesni_make_error_null_argument(err_details, "dest_size");
*dest_size = src_size;
if (dest == NULL)
return 0;
-
if (src == NULL)
- {
- aesni_make_error_null_argument(err_details, "src");
- return 1;
- }
-
+ return aesni_make_error_null_argument(err_details, "src");
if (inverted_schedule == NULL)
- {
- aesni_make_error_null_argument(err_details, "inverted_schedule");
- return 1;
- }
+ return aesni_make_error_null_argument(err_details, "inverted_schedule");
const size_t src_len = src_size / 16;
@@ -134,14 +112,9 @@ int aesni_decrypt_buffer_ecb128(
unsigned char padding_size = get_pkcs7_padding_size(padding);
if (padding_size == 0)
- {
- aesni_make_error_invalid_pkcs7_padding(err_details);
- return 1;
- }
- else
- {
- memcpy(dest, padding, 16 - padding_size);
- *dest_size -= padding_size;
- return 0;
- }
+ return aesni_make_error_invalid_pkcs7_padding(err_details);
+
+ memcpy(dest, padding, 16 - padding_size);
+ *dest_size -= padding_size;
+ return AESNI_SUCCESS;
}
diff --git a/src/error.c b/src/error.c
index e52cd4d..034aa72 100644
--- a/src/error.c
+++ b/src/error.c
@@ -20,17 +20,17 @@ static const char* err_msgs[] =
"Invalid PKCS7 padding (wrong key?)",
};
-const char* aesni_strerror(AesNI_ErrorCode ec)
+const char* aesni_strerror(AesNI_StatusCode ec)
{
return err_msgs[ec];
}
-static size_t aesni_format_error_simple(
+static size_t aesni_format_error_strerror(
const AesNI_ErrorDetails* err_details,\
char* dest,
size_t dest_size)
{
- const AesNI_ErrorCode ec = aesni_get_error_code(err_details);
+ const AesNI_StatusCode ec = aesni_get_error_code(err_details);
const char* const msg = aesni_strerror(ec);
if (dest == NULL && dest_size == 0)
@@ -60,9 +60,9 @@ typedef size_t (*AesNI_ErrorFormatter)(const AesNI_ErrorDetails*, char*, size_t)
static AesNI_ErrorFormatter err_formatters[] =
{
- &aesni_format_error_simple,
+ &aesni_format_error_strerror,
&aesni_format_error_null_argument,
- &aesni_format_error_simple,
+ &aesni_format_error_strerror,
};
size_t aesni_format_error(
@@ -75,44 +75,37 @@ size_t aesni_format_error(
return err_formatters[err_details->ec](err_details, dest, dest_size);
}
-static void aesni_make_error(
+static AesNI_StatusCode aesni_make_error(
AesNI_ErrorDetails* err_details,
- AesNI_ErrorCode ec)
+ AesNI_StatusCode ec)
{
if (err_details == NULL)
- return;
+ return ec;
- err_details->ec = ec;
+ return err_details->ec = ec;
}
-void aesni_make_error_success(
+AesNI_StatusCode aesni_make_error_success(
AesNI_ErrorDetails* err_details)
{
- if (err_details == NULL)
- return;
-
- aesni_make_error(err_details, AESNI_ERROR_SUCCESS);
+ return aesni_make_error(err_details, AESNI_SUCCESS);
}
-void aesni_make_error_null_argument(
+AesNI_StatusCode aesni_make_error_null_argument(
AesNI_ErrorDetails* err_details,
const char* arg_name)
{
- if (err_details == NULL)
- return;
-
- aesni_make_error(err_details, AESNI_ERROR_NULL_ARGUMENT);
+ AesNI_StatusCode status = aesni_make_error(err_details, AESNI_ERROR_NULL_ARGUMENT);
const size_t arg_name_size = sizeof(err_details->params.null_arg.arg_name);
strncpy(err_details->params.null_arg.arg_name, arg_name, arg_name_size);
err_details->params.null_arg.arg_name[arg_name_size - 1] = '\0';
+
+ return status;
}
-void aesni_make_error_invalid_pkcs7_padding(
+AesNI_StatusCode aesni_make_error_invalid_pkcs7_padding(
AesNI_ErrorDetails* err_details)
{
- if (err_details == NULL)
- return;
-
- aesni_make_error(err_details, AESNI_ERROR_INVALID_PKCS7_PADDING);
+ return aesni_make_error(err_details, AESNI_ERROR_INVALID_PKCS7_PADDING);
}