diff options
Diffstat (limited to '')
38 files changed, 336 insertions, 482 deletions
diff --git a/include/aesni/data.h b/include/aesni/data.h index 8b8162e..188b3e4 100644 --- a/include/aesni/data.h +++ b/include/aesni/data.h @@ -11,6 +11,8 @@ #pragma once +#include "error.h" + /** * \defgroup aesni_data Data * \brief Data structures and I/O functions @@ -49,7 +51,7 @@ static __inline AesNI_Block128 aesni_load_block128(const void* src) * `NULL`. * \return The loaded 128-bit block. */ -static __inline AesNI_Block128 aseni_load_block128_aligned(const void* src) +static __inline AesNI_Block128 aesni_load_block128_aligned(const void* src) { return _mm_load_si128((AesNI_Block128*) src); } @@ -201,11 +203,9 @@ typedef struct } AesNI_KeySchedule256; -static __inline AesNI_Block128 __fastcall aesni_reverse_byte_order128( - AesNI_Block128 block) +static __inline AesNI_Block128 __fastcall aesni_reverse_byte_order128(AesNI_Block128 block) { - return _mm_shuffle_epi8(block, aesni_make_block128( - 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); + return _mm_shuffle_epi8(block, aesni_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); } static __inline AesNI_Block128 __fastcall aesni_le2be128(AesNI_Block128 block) @@ -226,14 +226,6 @@ AesNI_BlockString128 aesni_format_block128(AesNI_Block128*); AesNI_BlockString192 aesni_format_block192(AesNI_Block192*); AesNI_BlockString256 aesni_format_block256(AesNI_Block256*); -AesNI_BlockString128 aesni_format_block128_le(AesNI_Block128*); -AesNI_BlockString192 aesni_format_block192_le(AesNI_Block192*); -AesNI_BlockString256 aesni_format_block256_le(AesNI_Block256*); - -AesNI_BlockString128 aesni_format_block128_be(AesNI_Block128*); -AesNI_BlockString192 aesni_format_block192_be(AesNI_Block192*); -AesNI_BlockString256 aesni_format_block256_be(AesNI_Block256*); - typedef struct { char str[49]; } AesNI_BlockMatrixString128; typedef struct { char str[73]; } AesNI_BlockMatrixString192; typedef struct { char str[97]; } AesNI_BlockMatrixString256; @@ -242,41 +234,44 @@ AesNI_BlockMatrixString128 aesni_format_block128_as_matrix(AesNI_Block128*); AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192*); AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256*); -AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128*); -AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192*); -AesNI_BlockMatrixString256 aesni_format_block256_be_as_matrix(AesNI_Block256*); - void aesni_print_block128(AesNI_Block128*); void aesni_print_block192(AesNI_Block192*); void aesni_print_block256(AesNI_Block256*); -void aesni_print_block128_le(AesNI_Block128*); -void aesni_print_block192_le(AesNI_Block192*); -void aesni_print_block256_le(AesNI_Block256*); - -void aesni_print_block128_be(AesNI_Block128*); -void aesni_print_block192_be(AesNI_Block192*); -void aesni_print_block256_be(AesNI_Block256*); - void aesni_print_block128_as_matrix(AesNI_Block128*); void aesni_print_block192_as_matrix(AesNI_Block192*); void aesni_print_block256_as_matrix(AesNI_Block256*); -void aesni_print_block128_be_as_matrix(AesNI_Block128*); -void aesni_print_block192_be_as_matrix(AesNI_Block192*); -void aesni_print_block256_be_as_matrix(AesNI_Block256*); +AesNI_StatusCode aesni_parse_block128( + AesNI_Block128* dest, + const char* src, + AesNI_ErrorDetails* err_details); -int aesni_parse_block128(AesNI_Block128*, const char*); -int aesni_parse_block192(AesNI_Block192*, const char*); -int aesni_parse_block256(AesNI_Block256*, const char*); +AesNI_StatusCode aesni_parse_block192( + AesNI_Block192* dest, + const char* src, + AesNI_ErrorDetails* err_details); -int aesni_parse_block128_le(AesNI_Block128*, const char*); -int aesni_parse_block192_le(AesNI_Block192*, const char*); -int aesni_parse_block256_le(AesNI_Block256*, const char*); - -int aesni_parse_block128_be(AesNI_Block128*, const char*); -int aesni_parse_block192_be(AesNI_Block192*, const char*); -int aesni_parse_block256_be(AesNI_Block256*, const char*); +/** + * \brief Parses a 256-bit block, from the least significant to the most significant byte. + * + * The block is parsed from a hexadecimal number represented using the big endian notation. + * + * The source string may optionally start with "0x" or "0X". + * Then 64 characters in the range [0-9a-fA-F] must follow. + * + * \param[out] dest The pointer to the parsed block. Must not be `NULL`. + * \param[in] src The pointer to the source C string. Must not be `NULL`. + * \param[out] err_details The error details structure. + * \retval AESNI_SUCCESS If parsed successfully. + * \retval AESNI_NULL_ARGUMENT_ERROR If either `dest` or `src` is `NULL`. + * \retval AESNI_PARSE_ERROR If `src` couldn't be parsed as a valid 256-bit block. + * \sa aesni_error_handling. + */ +AesNI_StatusCode aesni_parse_block256( + AesNI_Block256* dest, + const char* src, + AesNI_ErrorDetails* err_details); #ifdef __cplusplus } diff --git a/include/aesni/error.h b/include/aesni/error.h index 349ebe4..d4dcb81 100644 --- a/include/aesni/error.h +++ b/include/aesni/error.h @@ -44,11 +44,10 @@ extern "C" */ typedef enum { - AESNI_SUCCESS, ///< Indicates an operation - - AESNI_ERROR_NULL_ARGUMENT, ///< Invalid argument value NULL - - AESNI_ERROR_INVALID_PKCS7_PADDING, ///< Invalid PKCS7 padding + AESNI_SUCCESS, ///< Everything went fine + AESNI_NULL_ARGUMENT_ERROR, ///< Invalid argument value NULL + AESNI_PARSE_ERROR, ///< Couldn't parse + AESNI_INVALID_PKCS7_PADDING_ERROR, ///< Invalid PKCS7 padding } AesNI_StatusCode; @@ -62,7 +61,7 @@ static __inline int aesni_is_error(AesNI_StatusCode ec) * * For example, * \code{.c} - * printf("%s\n", aesni_strerror(AESNI_ERROR_NULL_ARGUMENT)); + * printf("%s\n", aesni_strerror(AESNI_NULL_ARGUMENT_ERROR)); * \endcode * would print * \code @@ -85,9 +84,17 @@ typedef struct { struct { - char arg_name[32]; ///< Name of the NULL argument + 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 } - null_arg; ///< `NULL` argument error parameters + parse_error; + ///< Parse error (AESNI_PARSE_ERROR) parameters } params; } @@ -121,29 +128,39 @@ size_t aesni_format_error( size_t dest_size); /** - * \brief Initializes error details structure. + * \brief Initializes an error details structure. * * \param[out] err_details The error details structure to fill. */ -AesNI_StatusCode aesni_make_error_success( +AesNI_StatusCode aesni_initialize_error_details( AesNI_ErrorDetails* err_details); /** * \brief Builds error details from a `NULL` argument error. * * \param[out] err_details The error details structure to fill. - * \param[in] arg_name The `NULL` argument name. Must not be `NULL`. + * \param[in] param_name The parameter name. Must not be `NULL`. + */ +AesNI_StatusCode aesni_make_null_argument_error( + AesNI_ErrorDetails* err_details, + const char* param_name); + +/** + * \brief Builds error details from a parse 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_error_null_argument( +AesNI_StatusCode aesni_make_parse_error( AesNI_ErrorDetails* err_details, - const char* arg_name); + const char* src); /** * \brief Builds error details from an invalid PKCS7 padding error. * * \param[out] err_details The error details structure to fill. */ -AesNI_StatusCode aesni_make_error_invalid_pkcs7_padding( +AesNI_StatusCode aesni_make_invalid_pkcs7_padding_error( AesNI_ErrorDetails* err_details); #ifdef __cplusplus 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); } diff --git a/test/aes128cbc_decrypt_block.c b/test/aes128cbc_decrypt_block.c index c7ab933..f702e4a 100644 --- a/test/aes128cbc_decrypt_block.c +++ b/test/aes128cbc_decrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128cbc_encrypt_block.c b/test/aes128cbc_encrypt_block.c index 301d791..7ee7734 100644 --- a/test/aes128cbc_encrypt_block.c +++ b/test/aes128cbc_encrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128cfb_decrypt_block.c b/test/aes128cfb_decrypt_block.c index 8e14fcc..e84e5fa 100644 --- a/test/aes128cfb_decrypt_block.c +++ b/test/aes128cfb_decrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128cfb_encrypt_block.c b/test/aes128cfb_encrypt_block.c index d322fce..9ef1df4 100644 --- a/test/aes128cfb_encrypt_block.c +++ b/test/aes128cfb_encrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ctr_decrypt_block.c b/test/aes128ctr_decrypt_block.c index 1b25653..354626a 100644 --- a/test/aes128ctr_decrypt_block.c +++ b/test/aes128ctr_decrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -49,7 +49,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ctr_encrypt_block.c b/test/aes128ctr_encrypt_block.c index 5ab0689..3bab709 100644 --- a/test/aes128ctr_encrypt_block.c +++ b/test/aes128ctr_encrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -49,7 +49,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ecb_decrypt_block.c b/test/aes128ecb_decrypt_block.c index 156dbdf..7b34641 100644 --- a/test/aes128ecb_decrypt_block.c +++ b/test/aes128ecb_decrypt_block.c @@ -28,7 +28,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -42,7 +42,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ecb_encrypt_block.c b/test/aes128ecb_encrypt_block.c index c071c6c..502cc65 100644 --- a/test/aes128ecb_encrypt_block.c +++ b/test/aes128ecb_encrypt_block.c @@ -28,7 +28,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -41,7 +41,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ofb_decrypt_block.c b/test/aes128ofb_decrypt_block.c index d1c60b8..ac29f05 100644 --- a/test/aes128ofb_decrypt_block.c +++ b/test/aes128ofb_decrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes128ofb_encrypt_block.c b/test/aes128ofb_encrypt_block.c index 7839bc3..e25aac8 100644 --- a/test/aes128ofb_encrypt_block.c +++ b/test/aes128ofb_encrypt_block.c @@ -28,13 +28,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block128(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192cbc_decrypt_block.c b/test/aes192cbc_decrypt_block.c index 85fafca..8a034ba 100644 --- a/test/aes192cbc_decrypt_block.c +++ b/test/aes192cbc_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -49,7 +49,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192cbc_encrypt_block.c b/test/aes192cbc_encrypt_block.c index 27b5384..6d8f15e 100644 --- a/test/aes192cbc_encrypt_block.c +++ b/test/aes192cbc_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192cfb_decrypt_block.c b/test/aes192cfb_decrypt_block.c index 38df143..13abb88 100644 --- a/test/aes192cfb_decrypt_block.c +++ b/test/aes192cfb_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192cfb_encrypt_block.c b/test/aes192cfb_encrypt_block.c index cee6f11..19137b2 100644 --- a/test/aes192cfb_encrypt_block.c +++ b/test/aes192cfb_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ctr_decrypt_block.c b/test/aes192ctr_decrypt_block.c index 64eb9e8..ce90f3a 100644 --- a/test/aes192ctr_decrypt_block.c +++ b/test/aes192ctr_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -50,7 +50,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ctr_encrypt_block.c b/test/aes192ctr_encrypt_block.c index 1b0985a..5258efa 100644 --- a/test/aes192ctr_encrypt_block.c +++ b/test/aes192ctr_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -50,7 +50,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ecb_decrypt_block.c b/test/aes192ecb_decrypt_block.c index 4e247f4..5d209ae 100644 --- a/test/aes192ecb_decrypt_block.c +++ b/test/aes192ecb_decrypt_block.c @@ -29,7 +29,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -43,7 +43,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ecb_encrypt_block.c b/test/aes192ecb_encrypt_block.c index 095307c..82f6821 100644 --- a/test/aes192ecb_encrypt_block.c +++ b/test/aes192ecb_encrypt_block.c @@ -29,7 +29,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -42,7 +42,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ofb_decrypt_block.c b/test/aes192ofb_decrypt_block.c index 6b49a54..64bb708 100644 --- a/test/aes192ofb_decrypt_block.c +++ b/test/aes192ofb_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes192ofb_encrypt_block.c b/test/aes192ofb_encrypt_block.c index eb40be2..986ac37 100644 --- a/test/aes192ofb_encrypt_block.c +++ b/test/aes192ofb_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block192(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block192(&key, *argv, NULL))) { fprintf(stderr, "Invalid 192-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256cbc_decrypt_block.c b/test/aes256cbc_decrypt_block.c index 1066b7f..89ead21 100644 --- a/test/aes256cbc_decrypt_block.c +++ b/test/aes256cbc_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -49,7 +49,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256cbc_encrypt_block.c b/test/aes256cbc_encrypt_block.c index 965a962..f6e067e 100644 --- a/test/aes256cbc_encrypt_block.c +++ b/test/aes256cbc_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256cfb_decrypt_block.c b/test/aes256cfb_decrypt_block.c index 9662431..249f413 100644 --- a/test/aes256cfb_decrypt_block.c +++ b/test/aes256cfb_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256cfb_encrypt_block.c b/test/aes256cfb_encrypt_block.c index 0da1799..7713db5 100644 --- a/test/aes256cfb_encrypt_block.c +++ b/test/aes256cfb_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ctr_decrypt_block.c b/test/aes256ctr_decrypt_block.c index 7923f4c..e2669e6 100644 --- a/test/aes256ctr_decrypt_block.c +++ b/test/aes256ctr_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -50,7 +50,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ctr_encrypt_block.c b/test/aes256ctr_encrypt_block.c index df52bcd..7d1d8e7 100644 --- a/test/aes256ctr_encrypt_block.c +++ b/test/aes256ctr_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -50,7 +50,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ecb_decrypt_block.c b/test/aes256ecb_decrypt_block.c index 4583bed..134b33f 100644 --- a/test/aes256ecb_decrypt_block.c +++ b/test/aes256ecb_decrypt_block.c @@ -29,7 +29,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -43,7 +43,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ecb_encrypt_block.c b/test/aes256ecb_encrypt_block.c index 9aa771d..3d0a8ee 100644 --- a/test/aes256ecb_encrypt_block.c +++ b/test/aes256ecb_encrypt_block.c @@ -29,7 +29,7 @@ int main(int argc, char** argv) if (argc < 1) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); @@ -42,7 +42,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ofb_decrypt_block.c b/test/aes256ofb_decrypt_block.c index 8eed57e..1033df7 100644 --- a/test/aes256ofb_decrypt_block.c +++ b/test/aes256ofb_decrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&cipher, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&cipher, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/test/aes256ofb_encrypt_block.c b/test/aes256ofb_encrypt_block.c index 98ed6a8..71dfea9 100644 --- a/test/aes256ofb_encrypt_block.c +++ b/test/aes256ofb_encrypt_block.c @@ -29,13 +29,13 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - if (aesni_parse_block256(&key, *argv) != 0) + if (aesni_is_error(aesni_parse_block256(&key, *argv, NULL))) { fprintf(stderr, "Invalid 256-bit AES block '%s'\n", *argv); exit_with_usage(); } - if (aesni_parse_block128(&iv, argv[1]) != 0) + if (aesni_is_error(aesni_parse_block128(&iv, argv[1], NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]); exit_with_usage(); @@ -48,7 +48,7 @@ int main(int argc, char** argv) if (strcmp("--", *argv) == 0) break; - if (aesni_parse_block128(&plain, *argv) != 0) + if (aesni_is_error(aesni_parse_block128(&plain, *argv, NULL))) { fprintf(stderr, "Invalid 128-bit AES block '%s'\n", *argv); continue; diff --git a/utils/aes128ecb_decrypt_file.cpp b/utils/aes128ecb_decrypt_file.cpp index f0811ce..5d9afd2 100644 --- a/utils/aes128ecb_decrypt_file.cpp +++ b/utils/aes128ecb_decrypt_file.cpp @@ -5,7 +5,7 @@ * \copyright This file is licensed under the terms of the MIT License. * See LICENSE.txt for details. */ - + #include <aesni/all.h> #include <aesnixx/all.hpp> @@ -42,14 +42,10 @@ int main(int argc, char** argv) if (argc != 4) exit_with_usage(); - if (aesni_parse_block128(&key, argv[1]) != 0) - { - std::cerr << "Invalid 128-bit AES block '" << argv[1] << "'\n"; - exit_with_usage(); - } - try { + aesni_parse_block128(&key, argv[1], aesni::ErrorDetailsThrowsInDestructor()); + const std::string src_path(argv[2]); const std::string dest_path(argv[3]); diff --git a/utils/aes128ecb_encrypt_file.cpp b/utils/aes128ecb_encrypt_file.cpp index 694d8fe..a2d13c6 100644 --- a/utils/aes128ecb_encrypt_file.cpp +++ b/utils/aes128ecb_encrypt_file.cpp @@ -42,14 +42,10 @@ int main(int argc, char** argv) if (argc != 4) exit_with_usage(); - if (aesni_parse_block128(&key, argv[1]) != 0) - { - std::cerr << "Invalid 128-bit AES block '" << argv[1] << "'\n"; - exit_with_usage(); - } - try { + aesni_parse_block128(&key, argv[1], aesni::ErrorDetailsThrowsInDestructor()); + const std::string src_path(argv[2]); const std::string dest_path(argv[3]); diff --git a/utils/cxx/include/aesnixx/error.hpp b/utils/cxx/include/aesnixx/error.hpp index 81e63c6..dd908c5 100644 --- a/utils/cxx/include/aesnixx/error.hpp +++ b/utils/cxx/include/aesnixx/error.hpp @@ -23,7 +23,7 @@ namespace aesni public: ErrorDetailsThrowsInDestructor() { - aesni_make_error_success(get()); + aesni_initialize_error_details(get()); } ~ErrorDetailsThrowsInDestructor() |