From 522a5b906d7620bcebddb5a8f476b022c140ab27 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 17 Jun 2015 20:07:32 +0300 Subject: factoring out AES-specific stuff --- include/aesni/algorithm.h | 17 --- include/aesni/all.h | 4 +- include/aesni/box.h | 60 ++-------- include/aesni/box_aes.h | 155 +++++++++++++++++++++++++ include/aesni/box_data.h | 110 ++++++++++++++++++ include/aesni/mode.h | 19 ---- src/box.c | 274 +++++++++------------------------------------ src/box_aes.c | 33 ++++++ test/decrypt_block_aes.cpp | 8 +- test/encrypt_block_aes.cpp | 8 +- 10 files changed, 372 insertions(+), 316 deletions(-) delete mode 100644 include/aesni/algorithm.h create mode 100644 include/aesni/box_aes.h create mode 100644 include/aesni/box_data.h delete mode 100644 include/aesni/mode.h create mode 100644 src/box_aes.c diff --git a/include/aesni/algorithm.h b/include/aesni/algorithm.h deleted file mode 100644 index 8aacfdc..0000000 --- a/include/aesni/algorithm.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -typedef enum -{ - AESNI_AES128, - AESNI_AES192, - AESNI_AES256, -} -AesNI_Algorithm; diff --git a/include/aesni/all.h b/include/aesni/all.h index 26f7a41..dfe9494 100644 --- a/include/aesni/all.h +++ b/include/aesni/all.h @@ -16,9 +16,9 @@ */ #include "aes.h" -#include "algorithm.h" #include "box.h" +#include "box_aes.h" +#include "box_data.h" #include "buffer.h" #include "data.h" #include "error.h" -#include "mode.h" diff --git a/include/aesni/box.h b/include/aesni/box.h index 70b2787..fd28cf1 100644 --- a/include/aesni/box.h +++ b/include/aesni/box.h @@ -8,74 +8,32 @@ #pragma once -#include "algorithm.h" -#include "data.h" +#include "box_data.h" #include "error.h" -#include "mode.h" #ifdef __cplusplus extern "C" { #endif -typedef union -{ - AesNI_Aes128_RoundKeys aes128_key_schedule; - AesNI_Aes192_RoundKeys aes192_key_schedule; - AesNI_Aes256_RoundKeys aes256_key_schedule; -} -AesNI_EncryptionParams; - -typedef union -{ - AesNI_Aes128_RoundKeys aes128_key_schedule; - AesNI_Aes192_RoundKeys aes192_key_schedule; - AesNI_Aes256_RoundKeys aes256_key_schedule; -} -AesNI_DecryptionParams; - -typedef union -{ - AesNI_Block128 aes_block; -} -AesNI_State; - -typedef union -{ - AesNI_Block128 aes128_key; - AesNI_Block192 aes192_key; - AesNI_Block256 aes256_key; -} -AesNI_AlgorithmParams; - -typedef struct -{ - AesNI_Algorithm algorithm; - AesNI_EncryptionParams encrypt_params; - AesNI_DecryptionParams decrypt_params; - AesNI_Mode mode; - AesNI_State iv; -} -AesNI_Box; - AesNI_StatusCode aesni_box_init( AesNI_Box*, - AesNI_Algorithm, - const AesNI_AlgorithmParams*, - AesNI_Mode, - const AesNI_State* iv, + AesNI_BoxAlgorithm, + const AesNI_BoxAlgorithmParams*, + AesNI_BoxMode, + const AesNI_BoxBlock* iv, AesNI_ErrorDetails*); AesNI_StatusCode aesni_box_encrypt( AesNI_Box*, - const AesNI_State*, - AesNI_State*, + const AesNI_BoxBlock*, + AesNI_BoxBlock*, AesNI_ErrorDetails*); AesNI_StatusCode aesni_box_decrypt( AesNI_Box*, - const AesNI_State*, - AesNI_State*, + const AesNI_BoxBlock*, + AesNI_BoxBlock*, AesNI_ErrorDetails*); #ifdef __cplusplus diff --git a/include/aesni/box_aes.h b/include/aesni/box_aes.h new file mode 100644 index 0000000..391f13e --- /dev/null +++ b/include/aesni/box_aes.h @@ -0,0 +1,155 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#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, + &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; + +#ifdef __cplusplus +} +#endif diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h new file mode 100644 index 0000000..160cf34 --- /dev/null +++ b/include/aesni/box_data.h @@ -0,0 +1,110 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "error.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef union +{ + AesNI_Block128 aes128_key; + AesNI_Block192 aes192_key; + AesNI_Block256 aes256_key; +} +AesNI_BoxAlgorithmParams; + +typedef enum +{ + AESNI_AES128, + AESNI_AES192, + AESNI_AES256, +} +AesNI_BoxAlgorithm; + +typedef enum +{ + AESNI_ECB, + AESNI_CBC, + AESNI_CFB, + AESNI_OFB, + AESNI_CTR, +} +AesNI_BoxMode; + +typedef union +{ + AesNI_Aes128_RoundKeys aes128_encryption_keys; + AesNI_Aes192_RoundKeys aes192_encryption_keys; + AesNI_Aes256_RoundKeys aes256_encryption_keys; +} +AesNI_BoxEncryptionParams; + +typedef union +{ + AesNI_Aes128_RoundKeys aes128_decryption_keys; + AesNI_Aes192_RoundKeys aes192_decryption_keys; + AesNI_Aes256_RoundKeys aes256_decryption_keys; +} +AesNI_BoxDecryptionParams; + +typedef union +{ + AesNI_Block128 aes_block; +} +AesNI_BoxBlock; + +typedef AesNI_StatusCode (*AesNI_BoxDeriveParams)( + const AesNI_BoxAlgorithmParams* params, + AesNI_BoxEncryptionParams*, + AesNI_BoxDecryptionParams*, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxEncrypt)( + const AesNI_BoxBlock* plaintext, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* ciphertext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxDecrypt)( + const AesNI_BoxBlock* ciphertext, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* plaintext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxXorBlock)( + AesNI_BoxBlock*, + const AesNI_BoxBlock*, + AesNI_ErrorDetails*); + +typedef struct +{ + AesNI_BoxDeriveParams derive_params; + AesNI_BoxEncrypt encrypt; + AesNI_BoxDecrypt decrypt; + AesNI_BoxXorBlock xor_block; +} +AesNI_BoxAlgorithmInterface; + +typedef struct +{ + const AesNI_BoxAlgorithmInterface* algorithm_iface; + AesNI_BoxEncryptionParams encrypt_params; + AesNI_BoxDecryptionParams decrypt_params; + AesNI_BoxMode mode; + AesNI_BoxBlock iv; +} +AesNI_Box; + +#ifdef __cplusplus +} +#endif diff --git a/include/aesni/mode.h b/include/aesni/mode.h deleted file mode 100644 index fc00e9c..0000000 --- a/include/aesni/mode.h +++ /dev/null @@ -1,19 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -typedef enum -{ - AESNI_ECB, - AESNI_CBC, - AESNI_CFB, - AESNI_OFB, - AESNI_CTR, -} -AesNI_Mode; diff --git a/src/box.c b/src/box.c index 4484390..0fa7297 100644 --- a/src/box.c +++ b/src/box.c @@ -8,193 +8,29 @@ #include -static AesNI_StatusCode aesni_box_xor_state_aes( - AesNI_State* dest, - const AesNI_State* 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_State* input, - const AesNI_EncryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes128_encrypt_block_( - input->aes_block, - ¶ms->aes128_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_decrypt_aes128( - const AesNI_State* input, - const AesNI_DecryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes128_decrypt_block_( - input->aes_block, - ¶ms->aes128_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_encrypt_aes192( - const AesNI_State* input, - const AesNI_EncryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes192_encrypt_block_( - input->aes_block, - ¶ms->aes192_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_decrypt_aes192( - const AesNI_State* input, - const AesNI_DecryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes192_decrypt_block_( - input->aes_block, - ¶ms->aes192_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_encrypt_aes256( - const AesNI_State* input, - const AesNI_EncryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes256_encrypt_block_( - input->aes_block, - ¶ms->aes256_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_decrypt_aes256( - const AesNI_State* input, - const AesNI_DecryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details) -{ - output->aes_block = aesni_aes256_decrypt_block_( - input->aes_block, - ¶ms->aes256_key_schedule); - return AESNI_SUCCESS; -} - -typedef AesNI_StatusCode (*AesNI_BoxEncrypt)( - const AesNI_State*, - const AesNI_EncryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details); - -static AesNI_BoxEncrypt aesni_box_encrypt_algorithm[] = -{ - &aesni_box_encrypt_aes128, - &aesni_box_encrypt_aes192, - &aesni_box_encrypt_aes256, -}; - -typedef AesNI_StatusCode (*AesNI_BoxDecrypt)( - const AesNI_State*, - const AesNI_DecryptionParams* params, - AesNI_State* output, - AesNI_ErrorDetails* err_details); - -static AesNI_BoxDecrypt aesni_box_decrypt_algorithm[] = -{ - &aesni_box_decrypt_aes128, - &aesni_box_decrypt_aes192, - &aesni_box_decrypt_aes256, -}; - -typedef AesNI_StatusCode (*AesNI_BoxXorState)( - AesNI_State*, - const AesNI_State*, - AesNI_ErrorDetails*); - -static AesNI_BoxXorState aesni_box_xor_state[] = -{ - &aesni_box_xor_state_aes, - &aesni_box_xor_state_aes, - &aesni_box_xor_state_aes, -}; - -static AesNI_StatusCode aesni_box_init_aes128( - AesNI_Box* box, - const AesNI_AlgorithmParams* algorithm_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes128_expand_key_( - algorithm_params->aes128_key, - &box->encrypt_params.aes128_key_schedule); - aesni_aes128_derive_decryption_keys_( - &box->encrypt_params.aes128_key_schedule, - &box->decrypt_params.aes128_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_init_aes192( - AesNI_Box* box, - const AesNI_AlgorithmParams* algorithm_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes192_expand_key_( - algorithm_params->aes192_key.lo, - algorithm_params->aes192_key.hi, - &box->encrypt_params.aes192_key_schedule); - aesni_aes192_derive_decryption_keys_( - &box->encrypt_params.aes192_key_schedule, - &box->decrypt_params.aes192_key_schedule); - return AESNI_SUCCESS; -} - -static AesNI_StatusCode aesni_box_init_aes256( - AesNI_Box* box, - const AesNI_AlgorithmParams* algorithm_params, - AesNI_ErrorDetails* err_details) -{ - aesni_aes256_expand_key_( - algorithm_params->aes256_key.lo, - algorithm_params->aes256_key.hi, - &box->encrypt_params.aes256_key_schedule); - aesni_aes256_derive_decryption_keys_( - &box->encrypt_params.aes256_key_schedule, - &box->decrypt_params.aes256_key_schedule); - return AESNI_SUCCESS; -} - -typedef AesNI_StatusCode (*AesNI_BoxInitializeAlgorithm)( - AesNI_Box*, - const AesNI_AlgorithmParams*, - AesNI_ErrorDetails*); - -static AesNI_BoxInitializeAlgorithm aesni_box_init_algorithm[] = +static const AesNI_BoxAlgorithmInterface* aesni_box_algorithm_ifaces[] = { - &aesni_box_init_aes128, - &aesni_box_init_aes192, - &aesni_box_init_aes256, + &aesni_box_aes128_iface, + &aesni_box_aes192_iface, + &aesni_box_aes256_iface, }; AesNI_StatusCode aesni_box_init( AesNI_Box* box, - AesNI_Algorithm algorithm, - const AesNI_AlgorithmParams* algorithm_params, - AesNI_Mode mode, - const AesNI_State* iv, + AesNI_BoxAlgorithm algorithm, + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxMode mode, + const AesNI_BoxBlock* iv, AesNI_ErrorDetails* err_details) { AesNI_StatusCode status = AESNI_SUCCESS; - box->algorithm = algorithm; - if (aesni_is_error(status = aesni_box_init_algorithm[algorithm](box, algorithm_params, err_details))) + box->algorithm_iface = aesni_box_algorithm_ifaces[algorithm]; + if (aesni_is_error(status = box->algorithm_iface->derive_params( + algorithm_params, + &box->encrypt_params, + &box->decrypt_params, + err_details))) return status; box->mode = mode; if (iv != NULL) @@ -205,11 +41,11 @@ AesNI_StatusCode aesni_box_init( static AesNI_StatusCode aesni_box_encrypt_ecb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return aesni_box_encrypt_algorithm[box->algorithm]( + return box->algorithm_iface->encrypt( input, &box->encrypt_params, output, @@ -218,21 +54,21 @@ static AesNI_StatusCode aesni_box_encrypt_ecb( static AesNI_StatusCode aesni_box_encrypt_cbc( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { AesNI_StatusCode status = AESNI_SUCCESS; - AesNI_State xored_input = *input; - status = aesni_box_xor_state[box->algorithm]( + AesNI_BoxBlock xored_input = *input; + status = box->algorithm_iface->xor_block( &xored_input, &box->iv, err_details); if (aesni_is_error(status)) return status; - status = aesni_box_encrypt_algorithm[box->algorithm]( + status = box->algorithm_iface->encrypt( &xored_input, &box->encrypt_params, output, @@ -246,11 +82,11 @@ static AesNI_StatusCode aesni_box_encrypt_cbc( static AesNI_StatusCode aesni_box_encrypt_cfb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = aesni_box_encrypt_algorithm[box->algorithm]( + AesNI_StatusCode status = box->algorithm_iface->encrypt( &box->iv, &box->encrypt_params, output, @@ -258,7 +94,7 @@ static AesNI_StatusCode aesni_box_encrypt_cfb( if (aesni_is_error(status)) return status; - status = aesni_box_xor_state[box->algorithm](output, input, err_details); + status = box->algorithm_iface->xor_block(output, input, err_details); if (aesni_is_error(status)) return status; @@ -268,11 +104,11 @@ static AesNI_StatusCode aesni_box_encrypt_cfb( static AesNI_StatusCode aesni_box_encrypt_ofb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = aesni_box_encrypt_algorithm[box->algorithm]( + AesNI_StatusCode status = box->algorithm_iface->encrypt( &box->iv, &box->encrypt_params, &box->iv, @@ -282,7 +118,7 @@ static AesNI_StatusCode aesni_box_encrypt_ofb( *output = box->iv; - status = aesni_box_xor_state[box->algorithm](output, input, err_details); + status = box->algorithm_iface->xor_block(output, input, err_details); if (aesni_is_error(status)) return status; @@ -291,8 +127,8 @@ static AesNI_StatusCode aesni_box_encrypt_ofb( static AesNI_StatusCode aesni_box_encrypt_ctr( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { return aesni_error_not_implemented(err_details); @@ -300,8 +136,8 @@ static AesNI_StatusCode aesni_box_encrypt_ctr( typedef AesNI_StatusCode (*AesNI_BoxEncryptMode)( AesNI_Box*, - const AesNI_State*, - AesNI_State*, + const AesNI_BoxBlock*, + AesNI_BoxBlock*, AesNI_ErrorDetails*); static AesNI_BoxEncryptMode aesni_box_encrypt_mode[] = @@ -315,8 +151,8 @@ static AesNI_BoxEncryptMode aesni_box_encrypt_mode[] = AesNI_StatusCode aesni_box_encrypt( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { return aesni_box_encrypt_mode[box->mode](box, input, output, err_details); @@ -324,11 +160,11 @@ AesNI_StatusCode aesni_box_encrypt( static AesNI_StatusCode aesni_box_decrypt_ecb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return aesni_box_decrypt_algorithm[box->algorithm]( + return box->algorithm_iface->decrypt( input, &box->decrypt_params, output, @@ -337,11 +173,11 @@ static AesNI_StatusCode aesni_box_decrypt_ecb( static AesNI_StatusCode aesni_box_decrypt_cbc( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = aesni_box_decrypt_algorithm[box->algorithm]( + AesNI_StatusCode status = box->algorithm_iface->decrypt( input, &box->decrypt_params, output, @@ -349,7 +185,7 @@ static AesNI_StatusCode aesni_box_decrypt_cbc( if (aesni_is_error(status)) return status; - status = aesni_box_xor_state[box->algorithm]( + status = box->algorithm_iface->xor_block( output, &box->iv, err_details); @@ -362,11 +198,11 @@ static AesNI_StatusCode aesni_box_decrypt_cbc( static AesNI_StatusCode aesni_box_decrypt_cfb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = aesni_box_encrypt_algorithm[box->algorithm]( + AesNI_StatusCode status = box->algorithm_iface->encrypt( &box->iv, &box->encrypt_params, output, @@ -374,7 +210,7 @@ static AesNI_StatusCode aesni_box_decrypt_cfb( if (aesni_is_error(status)) return status; - status = aesni_box_xor_state[box->algorithm]( + status = box->algorithm_iface->xor_block( output, input, err_details); @@ -388,11 +224,11 @@ static AesNI_StatusCode aesni_box_decrypt_cfb( static AesNI_StatusCode aesni_box_decrypt_ofb( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = aesni_box_encrypt_algorithm[box->algorithm]( + AesNI_StatusCode status = box->algorithm_iface->encrypt( &box->iv, &box->encrypt_params, output, @@ -402,7 +238,7 @@ static AesNI_StatusCode aesni_box_decrypt_ofb( box->iv = *output; - status = aesni_box_xor_state[box->algorithm]( + status = box->algorithm_iface->xor_block( output, input, err_details); @@ -414,8 +250,8 @@ static AesNI_StatusCode aesni_box_decrypt_ofb( static AesNI_StatusCode aesni_box_decrypt_ctr( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { return aesni_error_not_implemented(err_details); @@ -434,8 +270,8 @@ static AesNI_BoxDecryptMode aesni_box_decrypt_mode[] = AesNI_StatusCode aesni_box_decrypt( AesNI_Box* box, - const AesNI_State* input, - AesNI_State* output, + const AesNI_BoxBlock* input, + AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { return aesni_box_decrypt_mode[box->mode](box, input, output, err_details); diff --git a/src/box_aes.c b/src/box_aes.c new file mode 100644 index 0000000..5253415 --- /dev/null +++ b/src/box_aes.c @@ -0,0 +1,33 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +AesNI_BoxAlgorithmInterface aesni_box_aes128_iface = +{ + &aesni_box_derive_params_aes128, + &aesni_box_encrypt_aes128, + &aesni_box_decrypt_aes128, + &aesni_box_xor_block_aes, +}; + +AesNI_BoxAlgorithmInterface aesni_box_aes192_iface = +{ + &aesni_box_derive_params_aes192, + &aesni_box_encrypt_aes192, + &aesni_box_decrypt_aes192, + &aesni_box_xor_block_aes, +}; + +AesNI_BoxAlgorithmInterface aesni_box_aes256_iface = +{ + &aesni_box_derive_params_aes256, + &aesni_box_encrypt_aes256, + &aesni_box_decrypt_aes256, + &aesni_box_xor_block_aes, +}; diff --git a/test/decrypt_block_aes.cpp b/test/decrypt_block_aes.cpp index acc9c6b..6cb9ce7 100644 --- a/test/decrypt_block_aes.cpp +++ b/test/decrypt_block_aes.cpp @@ -34,10 +34,10 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - AesNI_AlgorithmParams algorithm_params; + AesNI_BoxAlgorithmParams algorithm_params; aesni::from_string(algorithm_params.aes128_key, argv[0]); - AesNI_State iv; + AesNI_BoxBlock iv; aesni::from_string(iv.aes_block, argv[1]); AesNI_Box box; @@ -54,10 +54,10 @@ int main(int argc, char** argv) if (std::strcmp("--", argv[0]) == 0) break; - AesNI_State ciphertext; + AesNI_BoxBlock ciphertext; aesni::from_string(ciphertext.aes_block, argv[0]); - AesNI_State plaintext; + AesNI_BoxBlock plaintext; aesni_box_decrypt( &box, &ciphertext, diff --git a/test/encrypt_block_aes.cpp b/test/encrypt_block_aes.cpp index 9db6e32..f15ddda 100644 --- a/test/encrypt_block_aes.cpp +++ b/test/encrypt_block_aes.cpp @@ -34,10 +34,10 @@ int main(int argc, char** argv) if (argc < 2) exit_with_usage(); - AesNI_AlgorithmParams algorithm_params; + AesNI_BoxAlgorithmParams algorithm_params; aesni::from_string(algorithm_params.aes128_key, argv[0]); - AesNI_State iv; + AesNI_BoxBlock iv; aesni::from_string(iv.aes_block, argv[1]); AesNI_Box box; @@ -54,10 +54,10 @@ int main(int argc, char** argv) if (std::strcmp("--", argv[0]) == 0) break; - AesNI_State plaintext; + AesNI_BoxBlock plaintext; aesni::from_string(plaintext.aes_block, argv[0]); - AesNI_State ciphertext; + AesNI_BoxBlock ciphertext; aesni_box_encrypt( &box, &plaintext, -- cgit v1.2.3