aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c76
1 files changed, 59 insertions, 17 deletions
diff --git a/src/buffer.c b/src/buffer.c
index b4896f7..71ebad2 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -13,20 +13,40 @@
static unsigned char FULL_BLOCK_PADDING[16] = { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 };
-size_t aesni_encrypt_buffer_ecb128(
+int aesni_encrypt_buffer_ecb128(
const void* src,
size_t src_size,
void* dest,
- AesNI_KeySchedule128* key_schedule)
+ size_t* dest_size,
+ AesNI_KeySchedule128* key_schedule,
+ AesNI_ErrorDetails* err_details)
{
- size_t rem_size = src_size % 16;
- size_t padding_size = 16 - rem_size;
- size_t dest_size = src_size + padding_size;
+ if (dest_size == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "dest_size");
+ return 1;
+ }
+
+ 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 dest_size;
+ return 0;
- size_t src_len = src_size / 16;
+ if (src == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "src");
+ return 1;
+ }
+
+ if (key_schedule == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "key_schedule");
+ return 1;
+ }
+
+ const size_t src_len = src_size / 16;
for (size_t i = 0; i < src_len; ++i, (char*) src += 16, (char*) dest += 16)
{
@@ -51,10 +71,10 @@ size_t aesni_encrypt_buffer_ecb128(
AesNI_Block128 ciphertext = aesni_encrypt_block_ecb128(plaintext, key_schedule);
aesni_store_block128(dest, ciphertext);
- return dest_size;
+ return 0;
}
-static unsigned char get_padding_size(const unsigned char* padding)
+static unsigned char get_pkcs7_padding_size(const unsigned char* padding)
{
if (padding[15] < 0x01 || padding[15] > 0x10)
return 0;
@@ -66,18 +86,38 @@ static unsigned char get_padding_size(const unsigned char* padding)
return padding[15];
}
-size_t aesni_decrypt_buffer_ecb128(
+int aesni_decrypt_buffer_ecb128(
const void* src,
size_t src_size,
void* dest,
- AesNI_KeySchedule128* inverted_schedule)
+ size_t* dest_size,
+ AesNI_KeySchedule128* inverted_schedule,
+ AesNI_ErrorDetails* err_details)
{
- size_t dest_size = src_size;
+ if (dest_size == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "dest_size");
+ return 1;
+ }
+
+ *dest_size = src_size;
if (dest == NULL)
- return dest_size;
+ return 0;
+
+ if (src == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "src");
+ return 1;
+ }
- size_t src_len = src_size / 16;
+ if (inverted_schedule == NULL)
+ {
+ aesni_make_error_null_argument(err_details, "inverted_schedule");
+ return 1;
+ }
+
+ const size_t src_len = src_size / 16;
for (size_t i = 0; i < src_len - 1; ++i, (char*) src += 16, (char*) dest += 16)
{
@@ -91,15 +131,17 @@ size_t aesni_decrypt_buffer_ecb128(
unsigned char padding[16];
aesni_store_block128(padding, plaintext);
- unsigned char padding_size = get_padding_size(padding);
+ unsigned char padding_size = get_pkcs7_padding_size(padding);
if (padding_size == 0)
{
- return dest_size - 16;
+ aesni_make_error_invalid_pkcs7_padding(err_details);
+ return 1;
}
else
{
memcpy(dest, padding, 16 - padding_size);
- return dest_size - padding_size;
+ *dest_size -= padding_size;
+ return 0;
}
}