From cad16e0a916be5547a4b6a65afb3d5ee876835ed Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 18 Jun 2015 20:17:51 +0300 Subject: refactoring --- include/aesni/aes.h | 7 +-- include/aesni/box.h | 26 +++++----- include/aesni/box_aes.h | 131 ----------------------------------------------- include/aesni/box_data.h | 1 + include/aesni/buffer.h | 1 + include/aesni/data.h | 20 +------- include/aesni/error.h | 12 ++--- src/box.c | 1 + src/box_aes.c | 128 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 155 insertions(+), 172 deletions(-) diff --git a/include/aesni/aes.h b/include/aesni/aes.h index c4429eb..bcc1cf1 100644 --- a/include/aesni/aes.h +++ b/include/aesni/aes.h @@ -9,6 +9,7 @@ #pragma once #include "data.h" +#include "error.h" #include @@ -135,19 +136,19 @@ AesNI_StatusCode aesni_aes256_parse_key( typedef struct { - AesNI_Block128 keys[11]; + AesNI_Aes_Block keys[11]; } AesNI_Aes128_RoundKeys; typedef struct { - AesNI_Block128 keys[13]; + AesNI_Aes_Block keys[13]; } AesNI_Aes192_RoundKeys; typedef struct { - AesNI_Block128 keys[15]; + AesNI_Aes_Block keys[15]; } AesNI_Aes256_RoundKeys; diff --git a/include/aesni/box.h b/include/aesni/box.h index fd28cf1..bc270ee 100644 --- a/include/aesni/box.h +++ b/include/aesni/box.h @@ -17,24 +17,24 @@ extern "C" #endif AesNI_StatusCode aesni_box_init( - AesNI_Box*, - AesNI_BoxAlgorithm, - const AesNI_BoxAlgorithmParams*, - AesNI_BoxMode, + AesNI_Box* box, + AesNI_BoxAlgorithm algorithm, + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxMode mode, const AesNI_BoxBlock* iv, - AesNI_ErrorDetails*); + AesNI_ErrorDetails* err_details); AesNI_StatusCode aesni_box_encrypt( - AesNI_Box*, - const AesNI_BoxBlock*, - AesNI_BoxBlock*, - AesNI_ErrorDetails*); + AesNI_Box* box, + const AesNI_BoxBlock* plaintext, + AesNI_BoxBlock* ciphertext, + AesNI_ErrorDetails* err_details); AesNI_StatusCode aesni_box_decrypt( - AesNI_Box*, - const AesNI_BoxBlock*, - AesNI_BoxBlock*, - AesNI_ErrorDetails*); + AesNI_Box* box, + const AesNI_BoxBlock* ciphertext, + AesNI_BoxBlock* plaintext, + AesNI_ErrorDetails* err_details); #ifdef __cplusplus } diff --git a/include/aesni/box_aes.h b/include/aesni/box_aes.h index 98de721..62259a2 100644 --- a/include/aesni/box_aes.h +++ b/include/aesni/box_aes.h @@ -8,144 +8,13 @@ #pragma once -#include "aes.h" -#include "box_aes.h" #include "box_data.h" -#include "data.h" #ifdef __cplusplus extern "C" { #endif -static __inline AesNI_StatusCode aesni_box_derive_params_aes128( - const AesNI_BoxAlgorithmParams* algorithm_params, - AesNI_BoxEncryptionParams* encrypt_params, - AesNI_BoxDecryptionParams* decrypt_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes128_expand_key_( - algorithm_params->aes128_key.key, - &encrypt_params->aes128_encryption_keys); - aesni_aes128_derive_decryption_keys_( - &encrypt_params->aes128_encryption_keys, - &decrypt_params->aes128_decryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_derive_params_aes192( - const AesNI_BoxAlgorithmParams* algorithm_params, - AesNI_BoxEncryptionParams* encrypt_params, - AesNI_BoxDecryptionParams* decrypt_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes192_expand_key_( - algorithm_params->aes192_key.lo, - algorithm_params->aes192_key.hi, - &encrypt_params->aes192_encryption_keys); - aesni_aes192_derive_decryption_keys_( - &encrypt_params->aes192_encryption_keys, - &decrypt_params->aes192_decryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_derive_params_aes256( - const AesNI_BoxAlgorithmParams* algorithm_params, - AesNI_BoxEncryptionParams* encrypt_params, - AesNI_BoxDecryptionParams* decrypt_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes256_expand_key_( - algorithm_params->aes256_key.lo, - algorithm_params->aes256_key.hi, - &encrypt_params->aes256_encryption_keys); - aesni_aes256_derive_decryption_keys_( - &encrypt_params->aes256_encryption_keys, - &decrypt_params->aes256_decryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_xor_block_aes( - AesNI_BoxBlock* dest, - const AesNI_BoxBlock* src, - AesNI_ErrorDetails* err_details) -{ - dest->aes_block = aesni_xor_block128(dest->aes_block, src->aes_block); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_encrypt_aes128( - const AesNI_BoxBlock* input, - const AesNI_BoxEncryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes128_encrypt_block_( - input->aes_block, - ¶ms->aes128_encryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_decrypt_aes128( - const AesNI_BoxBlock* input, - const AesNI_BoxDecryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes128_decrypt_block_( - input->aes_block, - ¶ms->aes128_decryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_encrypt_aes192( - const AesNI_BoxBlock* input, - const AesNI_BoxEncryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes192_encrypt_block_( - input->aes_block, - ¶ms->aes192_encryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_decrypt_aes192( - const AesNI_BoxBlock* input, - const AesNI_BoxDecryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes192_decrypt_block_( - input->aes_block, - ¶ms->aes192_decryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_encrypt_aes256( - const AesNI_BoxBlock* input, - const AesNI_BoxEncryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes256_encrypt_block_( - input->aes_block, - ¶ms->aes256_encryption_keys); - return AESNI_SUCCESS; -} - -static __inline AesNI_StatusCode aesni_box_decrypt_aes256( - const AesNI_BoxBlock* input, - const AesNI_BoxDecryptionParams* params, - AesNI_BoxBlock* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes256_decrypt_block_( - input->aes_block, - ¶ms->aes256_decryption_keys); - return AESNI_SUCCESS; -} - extern AesNI_BoxAlgorithmInterface aesni_box_aes128_iface; extern AesNI_BoxAlgorithmInterface aesni_box_aes192_iface; extern AesNI_BoxAlgorithmInterface aesni_box_aes256_iface; diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h index 732f714..9acd901 100644 --- a/include/aesni/box_data.h +++ b/include/aesni/box_data.h @@ -8,6 +8,7 @@ #pragma once +#include "aes.h" #include "error.h" #ifdef __cplusplus diff --git a/include/aesni/buffer.h b/include/aesni/buffer.h index 489c206..3e74078 100644 --- a/include/aesni/buffer.h +++ b/include/aesni/buffer.h @@ -10,6 +10,7 @@ #pragma once +#include "aes.h" #include "error.h" #include diff --git a/include/aesni/data.h b/include/aesni/data.h index 381a32a..3ef1c48 100644 --- a/include/aesni/data.h +++ b/include/aesni/data.h @@ -4,22 +4,10 @@ * \date 2015 * \copyright This file is licensed under the terms of the MIT License. * See LICENSE.txt for details. - * - * \brief Declares necessary data structures (for blocks, keys, etc.) and - * auxiliary I/O functions. */ #pragma once -#include "error.h" - -/** - * \defgroup aesni_data Data - * \brief Data structures and I/O functions - * \ingroup aesni - * \{ - */ - #include #include @@ -114,10 +102,10 @@ static __inline AesNI_Block128 __fastcall aesni_xor_block128( * \param[in] hi2 The more significant 4-byte value. * \param[in] lo1 The less significant 4-byte value. * \param[in] lo0 The least significant 4-byte value. + * * \return The built 128-bit block. */ -static __inline AesNI_Block128 __fastcall aesni_make_block128( - int hi3, int hi2, int lo1, int lo0) +static __inline AesNI_Block128 __fastcall aesni_make_block128(int hi3, int hi2, int lo1, int lo0) { return _mm_set_epi32(hi3, hi2, lo1, lo0); } @@ -130,7 +118,3 @@ static __inline AesNI_Block128 __fastcall aesni_reverse_bytes_block128(AesNI_Blo #ifdef __cplusplus } #endif - -/** - * \} - */ diff --git a/include/aesni/error.h b/include/aesni/error.h index 73b6696..5aa2fb4 100644 --- a/include/aesni/error.h +++ b/include/aesni/error.h @@ -4,8 +4,6 @@ * \date 2015 * \copyright This file is licensed under the terms of the MIT License. * See LICENSE.txt for details. - * - * \brief Declares error codes, error formatting functions, etc. */ #pragma once @@ -44,11 +42,11 @@ extern "C" */ typedef enum { - 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_NOT_IMPLEMENTED_ERROR, + 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 while decrypting + AESNI_NOT_IMPLEMENTED_ERROR, ///< Not implemented } AesNI_StatusCode; diff --git a/src/box.c b/src/box.c index f49c79f..e48c5d7 100644 --- a/src/box.c +++ b/src/box.c @@ -32,6 +32,7 @@ AesNI_StatusCode aesni_box_init( &box->decrypt_params, err_details))) return status; + box->mode = mode; if (iv != NULL) box->iv = *iv; diff --git a/src/box_aes.c b/src/box_aes.c index 5253415..fa8bece 100644 --- a/src/box_aes.c +++ b/src/box_aes.c @@ -8,6 +8,134 @@ #include +static AesNI_StatusCode aesni_box_derive_params_aes128( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes128_expand_key_( + algorithm_params->aes128_key.key, + &encrypt_params->aes128_encryption_keys); + aesni_aes128_derive_decryption_keys_( + &encrypt_params->aes128_encryption_keys, + &decrypt_params->aes128_decryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_derive_params_aes192( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes192_expand_key_( + algorithm_params->aes192_key.lo, + algorithm_params->aes192_key.hi, + &encrypt_params->aes192_encryption_keys); + aesni_aes192_derive_decryption_keys_( + &encrypt_params->aes192_encryption_keys, + &decrypt_params->aes192_decryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_derive_params_aes256( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes256_expand_key_( + algorithm_params->aes256_key.lo, + algorithm_params->aes256_key.hi, + &encrypt_params->aes256_encryption_keys); + aesni_aes256_derive_decryption_keys_( + &encrypt_params->aes256_encryption_keys, + &decrypt_params->aes256_decryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_xor_block_aes( + AesNI_BoxBlock* dest, + const AesNI_BoxBlock* src, + AesNI_ErrorDetails* err_details) +{ + dest->aes_block = aesni_xor_block128(dest->aes_block, src->aes_block); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_encrypt_aes128( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes128_encrypt_block_( + input->aes_block, + ¶ms->aes128_encryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_decrypt_aes128( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes128_decrypt_block_( + input->aes_block, + ¶ms->aes128_decryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_encrypt_aes192( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes192_encrypt_block_( + input->aes_block, + ¶ms->aes192_encryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_decrypt_aes192( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes192_decrypt_block_( + input->aes_block, + ¶ms->aes192_decryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_encrypt_aes256( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes256_encrypt_block_( + input->aes_block, + ¶ms->aes256_encryption_keys); + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_decrypt_aes256( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes256_decrypt_block_( + input->aes_block, + ¶ms->aes256_decryption_keys); + return AESNI_SUCCESS; +} + AesNI_BoxAlgorithmInterface aesni_box_aes128_iface = { &aesni_box_derive_params_aes128, -- cgit v1.2.3