From 3f5df3abf1386ce9071046bb00e5eae27a10e2ef Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 15 Jun 2015 21:32:42 +0300 Subject: handle block parsing errors the standard way --- include/aesni/data.h | 71 ++++++++++++++++++++++++--------------------------- include/aesni/error.h | 45 ++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 52 deletions(-) (limited to 'include/aesni') 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 -- cgit v1.2.3