diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 14 | ||||
-rw-r--r-- | src/data.c | 439 | ||||
-rw-r--r-- | src/error.c | 61 |
3 files changed, 182 insertions, 332 deletions
diff --git a/src/buffer.c b/src/buffer.c index 74ff519..a4aed96 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_error_null_argument(err_details, "dest_size"); + return aesni_make_null_argument_error(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_error_null_argument(err_details, "src"); + return aesni_make_null_argument_error(err_details, "src"); if (key_schedule == NULL) - return aesni_make_error_null_argument(err_details, "key_schedule"); + return aesni_make_null_argument_error(err_details, "key_schedule"); 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_error_null_argument(err_details, "dest_size"); + return aesni_make_null_argument_error(err_details, "dest_size"); *dest_size = src_size; if (dest == NULL) return 0; if (src == NULL) - return aesni_make_error_null_argument(err_details, "src"); + return aesni_make_null_argument_error(err_details, "src"); if (inverted_schedule == NULL) - return aesni_make_error_null_argument(err_details, "inverted_schedule"); + return aesni_make_null_argument_error(err_details, "inverted_schedule"); 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_error_invalid_pkcs7_padding(err_details); + return aesni_make_invalid_pkcs7_padding_error(err_details); memcpy(dest, padding, 16 - padding_size); *dest_size -= padding_size; @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "aesni/all.h" +#include <aesni/all.h> #include <intrin.h> @@ -18,122 +18,68 @@ AesNI_BlockString128 aesni_format_block128(AesNI_Block128* block) { assert(block); -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_format_block128_le(block); -#else - return aesni_format_block128_be(block); -#endif -} - -AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block) -{ - assert(block); - -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_format_block192_le(block); -#else - return aesni_format_block192_be(block); -#endif -} - -AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block) -{ - assert(block); - -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_format_block256_le(block); -#else - return aesni_format_block256_be(block); -#endif -} - -AesNI_BlockString128 aesni_format_block128_le(AesNI_Block128* block) -{ - assert(block); - AesNI_BlockString128 result; char *cursor = result.str; - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) block + 15 - i)); - - *cursor = '\0'; - return result; -} - -AesNI_BlockString192 aesni_format_block192_le(AesNI_Block192* block) -{ - assert(block); + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, *block); - AesNI_BlockString192 result; - char *cursor = result.str; - - for (int i = 0; i < 8; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->hi + 7 - i)); for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->lo + 15 - i)); + sprintf(cursor, "%02x", bytes[i]); *cursor = '\0'; return result; } -AesNI_BlockString256 aesni_format_block256_le(AesNI_Block256* block) +AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block) { assert(block); - AesNI_BlockString256 result; + AesNI_BlockString192 result; char *cursor = result.str; - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->hi + 15 - i)); - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->lo + 15 - i)); - - *cursor = '\0'; - return result; -} + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, block->lo); -AesNI_BlockString128 aesni_format_block128_be(AesNI_Block128* block) -{ - assert(block); + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } - AesNI_BlockString128 result; - char *cursor = result.str; + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, block->hi); - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) block + i)); + for (int i = 0; i < 8; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } *cursor = '\0'; return result; } -AesNI_BlockString192 aesni_format_block192_be(AesNI_Block192* block) +AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block) { assert(block); - AesNI_BlockString192 result; + AesNI_BlockString256 result; char *cursor = result.str; - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->lo + i)); - for (int i = 0; i < 8; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->hi + i)); - - *cursor = '\0'; - return result; -} + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, block->lo); -AesNI_BlockString256 aesni_format_block256_be(AesNI_Block256* block) -{ - assert(block); + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } - AesNI_BlockString256 result; - char *cursor = result.str; + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, block->hi); - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->lo + i)); - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", *((unsigned char*) &block->hi + i)); + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } *cursor = '\0'; return result; @@ -143,32 +89,11 @@ AesNI_BlockMatrixString128 aesni_format_block128_as_matrix(AesNI_Block128* block { assert(block); - return aesni_format_block128_be_as_matrix(block); -} - -AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192* block) -{ - assert(block); - - return aesni_format_block192_be_as_matrix(block); -} - -AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256* block) -{ - assert(block); - - return aesni_format_block256_be_as_matrix(block); -} - -AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* block) -{ - assert(block); - - __declspec(align(16)) unsigned char bytes[4][4]; AesNI_BlockMatrixString128 result; char* cursor = result.str; - _mm_store_si128((AesNI_Block128*) bytes, *block); + __declspec(align(16)) unsigned char bytes[4][4]; + aesni_store_block128_aligned(bytes, *block); for (int i = 0; i < 4; ++i, cursor += 3) { @@ -181,16 +106,16 @@ AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* bl return result; } -AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* block) +AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192* block) { assert(block); - __declspec(align(16)) unsigned char bytes[8][4]; AesNI_BlockMatrixString192 result; char* cursor = result.str; - _mm_store_si128((AesNI_Block128*) bytes, block->lo); - _mm_store_si128((AesNI_Block128*) bytes + 1, block->hi); + __declspec(align(16)) unsigned char bytes[8][4]; + aesni_store_block128_aligned(bytes, block->lo); + aesni_store_block128_aligned(bytes + 16, block->hi); for (int i = 0; i < 4; ++i, cursor += 3) { @@ -203,16 +128,16 @@ AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* bl return result; } -AesNI_BlockMatrixString256 aesni_format_block256_be_as_matrix(AesNI_Block256* block) +AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256* block) { assert(block); - __declspec(align(16)) unsigned char bytes[8][4]; AesNI_BlockMatrixString256 result; char* cursor = result.str; - _mm_store_si128((AesNI_Block128*) bytes, block->lo); - _mm_store_si128((AesNI_Block128*) bytes + 1, block->hi); + __declspec(align(16)) unsigned char bytes[8][4]; + aesni_store_block128_aligned(bytes, block->lo); + aesni_store_block128_aligned(bytes + 16, block->hi); for (int i = 0; i < 4; ++i, cursor += 3) { @@ -246,48 +171,6 @@ void aesni_print_block256(AesNI_Block256* block) printf("%s\n", aesni_format_block256(block).str); } -void aesni_print_block128_le(AesNI_Block128* block) -{ - assert(block); - - printf("%s\n", aesni_format_block128_le(block).str); -} - -void aesni_print_block192_le(AesNI_Block192* block) -{ - assert(block); - - printf("%s\n", aesni_format_block192_le(block).str); -} - -void aesni_print_block256_le(AesNI_Block256* block) -{ - assert(block); - - printf("%s\n", aesni_format_block256_le(block).str); -} - -void aesni_print_block128_be(AesNI_Block128* block) -{ - assert(block); - - printf("%s\n", aesni_format_block128_be(block).str); -} - -void aesni_print_block192_be(AesNI_Block192* block) -{ - assert(block); - - printf("%s\n", aesni_format_block192_be(block).str); -} - -void aesni_print_block256_be(AesNI_Block256* block) -{ - assert(block); - - printf("%s\n", aesni_format_block256_be(block).str); -} - void aesni_print_block128_as_matrix(AesNI_Block128* block) { assert(block); @@ -309,193 +192,129 @@ void aesni_print_block256_as_matrix(AesNI_Block256* block) printf("%s", aesni_format_block256_as_matrix(block).str); } -void aesni_print_block128_be_as_matrix(AesNI_Block128* block) -{ - assert(block); - - printf("%s", aesni_format_block128_be_as_matrix(block).str); -} - -void aesni_print_block192_be_as_matrix(AesNI_Block192* block) -{ - assert(block); - - printf("%s", aesni_format_block192_be_as_matrix(block).str); -} - -void aesni_print_block256_be_as_matrix(AesNI_Block256* block) -{ - assert(block); - - printf("%s", aesni_format_block256_be_as_matrix(block).str); -} - -int aesni_parse_block128(AesNI_Block128* block, const char* src) -{ - assert(block); - assert(src); - -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_parse_block128_le(block, src); -#else - return aesni_parse_block128_be(block, src); -#endif -} - -int aesni_parse_block192(AesNI_Block192* block, const char* src) -{ - assert(block); - assert(src); - -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_parse_block192_le(block, src); -#else - return aesni_parse_block192_be(block, src); -#endif -} - -int aesni_parse_block256(AesNI_Block256* block, const char* src) -{ - assert(block); - assert(src); - -#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO - return aesni_parse_block256_le(block, src); -#else - return aesni_parse_block256_be(block, src); -#endif -} - -int aesni_parse_block128_le(AesNI_Block128* block, const char* src) +AesNI_StatusCode aesni_parse_block128( + AesNI_Block128* dest, + const char* src, + AesNI_ErrorDetails* err_details) { - assert(block); + assert(dest); assert(src); - int n, xs[4]; - if (sscanf(src, "%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &n) != 4 - || n != strlen(src)) - return 1; - *block = aesni_make_block128(xs[0], xs[1], xs[2], xs[3]); - return 0; -} + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); -int aesni_parse_block192_le(AesNI_Block192* block, const char* src) -{ - assert(block); - assert(src); - - int n, xs[6]; - if (sscanf(src, "%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &n) != 6 - || n != strlen(src)) - return 1; - *block = aesni_make_block192(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5]); - return 0; -} - -int aesni_parse_block256_le(AesNI_Block256* block, const char* src) -{ - assert(block); - assert(src); - - int n, xs[8]; - if (sscanf(src, "%8x%8x%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &xs[6], &xs[7], &n) != 8 - || n != strlen(src)) - return 1; - *block = aesni_make_block256(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7]); - return 0; -} - -int aesni_parse_block128_be(AesNI_Block128* block, const char* src) -{ - assert(block); - assert(src); - - unsigned char bytes[16]; + __declspec(align(16)) unsigned char bytes[16]; for (int i = 0; i < 16; ++i) { int n; unsigned int byte; if (sscanf(src, "%2x%n", &byte, &n) != 1) - return 1; + return aesni_make_parse_error(err_details, src); bytes[i] = (unsigned char) byte; src += n; } - *block = _mm_loadu_si128((AesNI_Block128*) bytes); - return 0; + *dest = aesni_load_block128_aligned(bytes); + + return aesni_initialize_error_details(err_details); } -int aesni_parse_block192_be(AesNI_Block192* block, const char* src) +AesNI_StatusCode aesni_parse_block192( + AesNI_Block192* dest, + const char* src, + AesNI_ErrorDetails* err_details) { - assert(block); + assert(dest); assert(src); - AesNI_Block128 lo, hi; - unsigned char lo_bytes[16], hi_bytes[16] = { 0 }; + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); - for (int i = 0; i < 16; ++i) { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return 1; - lo_bytes[i] = (unsigned char) byte; - src += n; + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->lo = aesni_load_block128_aligned(bytes); } - lo = _mm_loadu_si128((AesNI_Block128*) lo_bytes); - - for (int i = 0; i < 8; ++i) { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return 1; - hi_bytes[i] = (unsigned char) byte; - src += n; + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 8; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + memset(bytes + 8, 0x00, 8); + dest->hi = aesni_load_block128_aligned(bytes); } - hi = _mm_loadu_si128((AesNI_Block128*) hi_bytes); - - block->hi = hi; - block->lo = lo; - return 0; + return aesni_initialize_error_details(err_details); } -int aesni_parse_block256_be(AesNI_Block256* block, const char* src) +AesNI_StatusCode aesni_parse_block256( + AesNI_Block256* dest, + const char* src, + AesNI_ErrorDetails* err_details) { - assert(block); + assert(dest); assert(src); - AesNI_Block128 lo, hi; - unsigned char lo_bytes[16], hi_bytes[16]; + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); - for (int i = 0; i < 16; ++i) { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return 1; - lo_bytes[i] = (unsigned char) byte; - src += n; + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->lo = aesni_load_block128_aligned(bytes); } - lo = _mm_loadu_si128((AesNI_Block128*) lo_bytes); - - for (int i = 0; i < 16; ++i) { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return 1; - hi_bytes[i] = (unsigned char) byte; - src += n; + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->hi = aesni_load_block128_aligned(bytes); } - hi = _mm_loadu_si128((AesNI_Block128*) hi_bytes); - - block->hi = hi; - block->lo = lo; - return 0; + return aesni_initialize_error_details(err_details); } diff --git a/src/error.c b/src/error.c index 034aa72..5e25104 100644 --- a/src/error.c +++ b/src/error.c @@ -15,8 +15,9 @@ static const char* err_msgs[] = { - "Success", + "OK", "Invalid argument value NULL", + "Couldn't parse", "Invalid PKCS7 padding (wrong key?)", }; @@ -41,18 +42,33 @@ static size_t aesni_format_error_strerror( return strlen(dest); } -static size_t aesni_format_error_null_argument( +static size_t aesni_format_null_argument_error( const AesNI_ErrorDetails* err_details, char* dest, size_t dest_size) { static const char* const fmt = "Invalid argument value NULL (argument name: '%s')"; - const char* const arg_name = err_details->params.null_arg.arg_name; + const char* const param_name = err_details->params.null_arg_error.param_name; if (dest == NULL && dest_size == 0) - return _snprintf(NULL, 0, fmt, arg_name) + 1; + return _snprintf(NULL, 0, fmt, param_name) + 1; - _snprintf(dest, dest_size, fmt, arg_name); + _snprintf(dest, dest_size, fmt, param_name); + return strlen(dest); +} + +static size_t aesni_format_parse_error( + const AesNI_ErrorDetails* err_details, + char* dest, + size_t dest_size) +{ + static const char* const fmt = "Couldn't parse '%s'"; + const char* const src = err_details->params.parse_error.src; + + if (dest == NULL) + return _snprintf(NULL, 0, fmt, src) + 1; + + _snprintf(dest, dest_size, fmt, src); return strlen(dest); } @@ -61,7 +77,8 @@ typedef size_t (*AesNI_ErrorFormatter)(const AesNI_ErrorDetails*, char*, size_t) static AesNI_ErrorFormatter err_formatters[] = { &aesni_format_error_strerror, - &aesni_format_error_null_argument, + &aesni_format_null_argument_error, + &aesni_format_error_strerror, &aesni_format_error_strerror, }; @@ -85,27 +102,41 @@ static AesNI_StatusCode aesni_make_error( return err_details->ec = ec; } -AesNI_StatusCode aesni_make_error_success( +AesNI_StatusCode aesni_initialize_error_details( AesNI_ErrorDetails* err_details) { return aesni_make_error(err_details, AESNI_SUCCESS); } -AesNI_StatusCode aesni_make_error_null_argument( +AesNI_StatusCode aesni_make_null_argument_error( + AesNI_ErrorDetails* err_details, + const char* param_name) +{ + 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'; + + return status; +} + +AesNI_StatusCode aesni_make_parse_error( AesNI_ErrorDetails* err_details, - const char* arg_name) + const char* src) { - AesNI_StatusCode status = aesni_make_error(err_details, AESNI_ERROR_NULL_ARGUMENT); + AesNI_StatusCode status = aesni_make_error(err_details, AESNI_PARSE_ERROR); - 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'; + 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'; return status; } -AesNI_StatusCode aesni_make_error_invalid_pkcs7_padding( +AesNI_StatusCode aesni_make_invalid_pkcs7_padding_error( AesNI_ErrorDetails* err_details) { - return aesni_make_error(err_details, AESNI_ERROR_INVALID_PKCS7_PADDING); + return aesni_make_error(err_details, AESNI_INVALID_PKCS7_PADDING_ERROR); } |