diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-19 05:36:34 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-19 05:36:34 +0300 |
commit | a386820259790f08073bc84bb868c9907558bb84 (patch) | |
tree | bc46ca07ef0d0a3671f8d0ecc1a45295881ec289 | |
parent | error message update (diff) | |
download | aes-tools-a386820259790f08073bc84bb868c9907558bb84.tar.gz aes-tools-a386820259790f08073bc84bb868c9907558bb84.zip |
refactoring
-rw-r--r-- | include/aesni/box.h | 4 | ||||
-rw-r--r-- | include/aesni/box_aes.h | 6 | ||||
-rw-r--r-- | include/aesni/box_data.h | 21 | ||||
-rw-r--r-- | src/box.c | 94 | ||||
-rw-r--r-- | src/box_aes.c | 51 | ||||
-rw-r--r-- | test/decrypt_block_aes.cpp | 2 | ||||
-rw-r--r-- | test/encrypt_block_aes.cpp | 2 |
7 files changed, 100 insertions, 80 deletions
diff --git a/include/aesni/box.h b/include/aesni/box.h index bc270ee..3dfbc9e 100644 --- a/include/aesni/box.h +++ b/include/aesni/box.h @@ -24,13 +24,13 @@ AesNI_StatusCode aesni_box_init( const AesNI_BoxBlock* iv, AesNI_ErrorDetails* err_details); -AesNI_StatusCode aesni_box_encrypt( +AesNI_StatusCode aesni_box_encrypt_block( AesNI_Box* box, const AesNI_BoxBlock* plaintext, AesNI_BoxBlock* ciphertext, AesNI_ErrorDetails* err_details); -AesNI_StatusCode aesni_box_decrypt( +AesNI_StatusCode aesni_box_decrypt_block( AesNI_Box* box, const AesNI_BoxBlock* ciphertext, AesNI_BoxBlock* plaintext, diff --git a/include/aesni/box_aes.h b/include/aesni/box_aes.h index 62259a2..3d7faec 100644 --- a/include/aesni/box_aes.h +++ b/include/aesni/box_aes.h @@ -15,9 +15,9 @@ extern "C" { #endif -extern AesNI_BoxAlgorithmInterface aesni_box_aes128_iface; -extern AesNI_BoxAlgorithmInterface aesni_box_aes192_iface; -extern AesNI_BoxAlgorithmInterface aesni_box_aes256_iface; +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes128; +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192; +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256; #ifdef __cplusplus } diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h index ecd98a9..327c9bc 100644 --- a/include/aesni/box_data.h +++ b/include/aesni/box_data.h @@ -11,6 +11,8 @@ #include "aes.h" #include "error.h" +#include <stdlib.h> + #ifdef __cplusplus extern "C" { @@ -70,13 +72,13 @@ typedef AesNI_StatusCode (*AesNI_BoxDeriveParams)( AesNI_BoxDecryptionParams*, AesNI_ErrorDetails* err_details); -typedef AesNI_StatusCode (*AesNI_BoxEncrypt)( +typedef AesNI_StatusCode (*AesNI_BoxEncryptBlock)( const AesNI_BoxBlock* plaintext, const AesNI_BoxEncryptionParams* params, AesNI_BoxBlock* ciphertext, AesNI_ErrorDetails* err_details); -typedef AesNI_StatusCode (*AesNI_BoxDecrypt)( +typedef AesNI_StatusCode (*AesNI_BoxDecryptBlock)( const AesNI_BoxBlock* ciphertext, const AesNI_BoxDecryptionParams* params, AesNI_BoxBlock* plaintext, @@ -87,23 +89,28 @@ typedef AesNI_StatusCode (*AesNI_BoxXorBlock)( const AesNI_BoxBlock*, AesNI_ErrorDetails*); -typedef AesNI_StatusCode (*AesNI_BoxIncCounter)( +typedef AesNI_StatusCode (*AesNI_BoxNextCounter)( AesNI_BoxBlock*, AesNI_ErrorDetails*); +typedef AesNI_StatusCode (*AesNI_BoxGetBlockSize)( + size_t*, + AesNI_ErrorDetails*); + typedef struct { AesNI_BoxDeriveParams derive_params; - AesNI_BoxEncrypt encrypt; - AesNI_BoxDecrypt decrypt; + AesNI_BoxEncryptBlock encrypt_block; + AesNI_BoxDecryptBlock decrypt_block; AesNI_BoxXorBlock xor_block; - AesNI_BoxIncCounter inc_counter; + AesNI_BoxNextCounter next_counter; + AesNI_BoxGetBlockSize get_block_size; } AesNI_BoxAlgorithmInterface; typedef struct { - const AesNI_BoxAlgorithmInterface* algorithm_iface; + const AesNI_BoxAlgorithmInterface* algorithm; AesNI_BoxEncryptionParams encrypt_params; AesNI_BoxDecryptionParams decrypt_params; AesNI_BoxMode mode; @@ -8,11 +8,11 @@ #include <aesni/all.h> -static const AesNI_BoxAlgorithmInterface* aesni_box_algorithm_ifaces[] = +static const AesNI_BoxAlgorithmInterface* aesni_box_algorithms[] = { - &aesni_box_aes128_iface, - &aesni_box_aes192_iface, - &aesni_box_aes256_iface, + &aesni_box_algorithm_aes128, + &aesni_box_algorithm_aes192, + &aesni_box_algorithm_aes256, }; AesNI_StatusCode aesni_box_init( @@ -25,8 +25,8 @@ AesNI_StatusCode aesni_box_init( { AesNI_StatusCode status = AESNI_SUCCESS; - box->algorithm_iface = aesni_box_algorithm_ifaces[algorithm]; - if (aesni_is_error(status = box->algorithm_iface->derive_params( + box->algorithm = aesni_box_algorithms[algorithm]; + if (aesni_is_error(status = box->algorithm->derive_params( algorithm_params, &box->encrypt_params, &box->decrypt_params, @@ -40,20 +40,20 @@ AesNI_StatusCode aesni_box_init( return status; } -static AesNI_StatusCode aesni_box_encrypt_ecb( +static AesNI_StatusCode aesni_box_encrypt_block_ecb( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return box->algorithm_iface->encrypt( + return box->algorithm->encrypt_block( input, &box->encrypt_params, output, err_details); } -static AesNI_StatusCode aesni_box_encrypt_cbc( +static AesNI_StatusCode aesni_box_encrypt_block_cbc( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, @@ -62,14 +62,14 @@ static AesNI_StatusCode aesni_box_encrypt_cbc( AesNI_StatusCode status = AESNI_SUCCESS; AesNI_BoxBlock xored_input = *input; - status = box->algorithm_iface->xor_block( + status = box->algorithm->xor_block( &xored_input, &box->iv, err_details); if (aesni_is_error(status)) return status; - status = box->algorithm_iface->encrypt( + status = box->algorithm->encrypt_block( &xored_input, &box->encrypt_params, output, @@ -81,13 +81,13 @@ static AesNI_StatusCode aesni_box_encrypt_cbc( return status; } -static AesNI_StatusCode aesni_box_encrypt_cfb( +static AesNI_StatusCode aesni_box_encrypt_block_cfb( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = box->algorithm_iface->encrypt( + AesNI_StatusCode status = box->algorithm->encrypt_block( &box->iv, &box->encrypt_params, output, @@ -95,7 +95,7 @@ static AesNI_StatusCode aesni_box_encrypt_cfb( if (aesni_is_error(status)) return status; - status = box->algorithm_iface->xor_block(output, input, err_details); + status = box->algorithm->xor_block(output, input, err_details); if (aesni_is_error(status)) return status; @@ -103,13 +103,13 @@ static AesNI_StatusCode aesni_box_encrypt_cfb( return status; } -static AesNI_StatusCode aesni_box_encrypt_ofb( +static AesNI_StatusCode aesni_box_encrypt_block_ofb( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = box->algorithm_iface->encrypt( + AesNI_StatusCode status = box->algorithm->encrypt_block( &box->iv, &box->encrypt_params, &box->iv, @@ -119,20 +119,20 @@ static AesNI_StatusCode aesni_box_encrypt_ofb( *output = box->iv; - status = box->algorithm_iface->xor_block(output, input, err_details); + status = box->algorithm->xor_block(output, input, err_details); if (aesni_is_error(status)) return status; return status; } -static AesNI_StatusCode aesni_box_encrypt_ctr( +static AesNI_StatusCode aesni_box_encrypt_block_ctr( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = box->algorithm_iface->encrypt( + AesNI_StatusCode status = box->algorithm->encrypt_block( &box->iv, &box->encrypt_params, output, @@ -140,57 +140,57 @@ static AesNI_StatusCode aesni_box_encrypt_ctr( if (aesni_is_error(status)) return status; - status = box->algorithm_iface->xor_block(output, input, err_details); + status = box->algorithm->xor_block(output, input, err_details); if (aesni_is_error(status)) return status; - return box->algorithm_iface->inc_counter(&box->iv, err_details); + return box->algorithm->next_counter(&box->iv, err_details); } -typedef AesNI_StatusCode (*AesNI_BoxEncryptMode)( +typedef AesNI_StatusCode (*AesNI_BoxEncryptBlockInMode)( AesNI_Box*, const AesNI_BoxBlock*, AesNI_BoxBlock*, AesNI_ErrorDetails*); -static AesNI_BoxEncryptMode aesni_box_encrypt_mode[] = +static AesNI_BoxEncryptBlockInMode aesni_box_encrypt_block_in_mode[] = { - &aesni_box_encrypt_ecb, - &aesni_box_encrypt_cbc, - &aesni_box_encrypt_cfb, - &aesni_box_encrypt_ofb, - &aesni_box_encrypt_ctr, + &aesni_box_encrypt_block_ecb, + &aesni_box_encrypt_block_cbc, + &aesni_box_encrypt_block_cfb, + &aesni_box_encrypt_block_ofb, + &aesni_box_encrypt_block_ctr, }; -AesNI_StatusCode aesni_box_encrypt( +AesNI_StatusCode aesni_box_encrypt_block( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return aesni_box_encrypt_mode[box->mode](box, input, output, err_details); + return aesni_box_encrypt_block_in_mode[box->mode](box, input, output, err_details); } -static AesNI_StatusCode aesni_box_decrypt_ecb( +static AesNI_StatusCode aesni_box_decrypt_block_ecb( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return box->algorithm_iface->decrypt( + return box->algorithm->decrypt_block( input, &box->decrypt_params, output, err_details); } -static AesNI_StatusCode aesni_box_decrypt_cbc( +static AesNI_StatusCode aesni_box_decrypt_block_cbc( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = box->algorithm_iface->decrypt( + AesNI_StatusCode status = box->algorithm->decrypt_block( input, &box->decrypt_params, output, @@ -198,7 +198,7 @@ static AesNI_StatusCode aesni_box_decrypt_cbc( if (aesni_is_error(status)) return status; - status = box->algorithm_iface->xor_block( + status = box->algorithm->xor_block( output, &box->iv, err_details); @@ -209,13 +209,13 @@ static AesNI_StatusCode aesni_box_decrypt_cbc( return status; } -static AesNI_StatusCode aesni_box_decrypt_cfb( +static AesNI_StatusCode aesni_box_decrypt_block_cfb( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - AesNI_StatusCode status = box->algorithm_iface->encrypt( + AesNI_StatusCode status = box->algorithm->encrypt_block( &box->iv, &box->encrypt_params, output, @@ -223,7 +223,7 @@ static AesNI_StatusCode aesni_box_decrypt_cfb( if (aesni_is_error(status)) return status; - status = box->algorithm_iface->xor_block( + status = box->algorithm->xor_block( output, input, err_details); @@ -234,22 +234,22 @@ static AesNI_StatusCode aesni_box_decrypt_cfb( return status; } -typedef AesNI_BoxEncryptMode AesNI_BoxDecryptMode; +typedef AesNI_BoxEncryptBlockInMode AesNI_BoxDecryptBlockInMode; -static AesNI_BoxDecryptMode aesni_box_decrypt_mode[] = +static AesNI_BoxDecryptBlockInMode aesni_box_decrypt_block_in_mode[] = { - &aesni_box_decrypt_ecb, - &aesni_box_decrypt_cbc, - &aesni_box_decrypt_cfb, - &aesni_box_encrypt_ofb, - &aesni_box_encrypt_ctr, + &aesni_box_decrypt_block_ecb, + &aesni_box_decrypt_block_cbc, + &aesni_box_decrypt_block_cfb, + &aesni_box_encrypt_block_ofb, + &aesni_box_encrypt_block_ctr, }; -AesNI_StatusCode aesni_box_decrypt( +AesNI_StatusCode aesni_box_decrypt_block( AesNI_Box* box, const AesNI_BoxBlock* input, AesNI_BoxBlock* output, AesNI_ErrorDetails* err_details) { - return aesni_box_decrypt_mode[box->mode](box, input, output, err_details); + return aesni_box_decrypt_block_in_mode[box->mode](box, input, output, err_details); } diff --git a/src/box_aes.c b/src/box_aes.c index aa2aa21..12f369d 100644 --- a/src/box_aes.c +++ b/src/box_aes.c @@ -8,6 +8,8 @@ #include <aesni/all.h> +#include <stdlib.h> + static AesNI_StatusCode aesni_box_derive_params_aes128( const AesNI_BoxAlgorithmParams* algorithm_params, AesNI_BoxEncryptionParams* encrypt_params, @@ -64,7 +66,7 @@ static AesNI_StatusCode aesni_box_xor_block_aes( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_inc_counter_aes( +static AesNI_StatusCode aesni_box_next_counter_aes( AesNI_BoxBlock* ctr, AesNI_ErrorDetails* err_details) { @@ -72,7 +74,15 @@ static AesNI_StatusCode aesni_box_inc_counter_aes( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_encrypt_aes128( +static AesNI_StatusCode aesni_box_get_block_size_aes( + size_t* block_size, + AesNI_ErrorDetails* err_details) +{ + *block_size = 16; + return AESNI_SUCCESS; +} + +static AesNI_StatusCode aesni_box_encrypt_block_aes128( const AesNI_BoxBlock* input, const AesNI_BoxEncryptionParams* params, AesNI_BoxBlock* output, @@ -84,7 +94,7 @@ static AesNI_StatusCode aesni_box_encrypt_aes128( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_decrypt_aes128( +static AesNI_StatusCode aesni_box_decrypt_block_aes128( const AesNI_BoxBlock* input, const AesNI_BoxDecryptionParams* params, AesNI_BoxBlock* output, @@ -96,7 +106,7 @@ static AesNI_StatusCode aesni_box_decrypt_aes128( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_encrypt_aes192( +static AesNI_StatusCode aesni_box_encrypt_block_aes192( const AesNI_BoxBlock* input, const AesNI_BoxEncryptionParams* params, AesNI_BoxBlock* output, @@ -108,7 +118,7 @@ static AesNI_StatusCode aesni_box_encrypt_aes192( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_decrypt_aes192( +static AesNI_StatusCode aesni_box_decrypt_block_aes192( const AesNI_BoxBlock* input, const AesNI_BoxDecryptionParams* params, AesNI_BoxBlock* output, @@ -120,7 +130,7 @@ static AesNI_StatusCode aesni_box_decrypt_aes192( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_encrypt_aes256( +static AesNI_StatusCode aesni_box_encrypt_block_aes256( const AesNI_BoxBlock* input, const AesNI_BoxEncryptionParams* params, AesNI_BoxBlock* output, @@ -132,7 +142,7 @@ static AesNI_StatusCode aesni_box_encrypt_aes256( return AESNI_SUCCESS; } -static AesNI_StatusCode aesni_box_decrypt_aes256( +static AesNI_StatusCode aesni_box_decrypt_block_aes256( const AesNI_BoxBlock* input, const AesNI_BoxDecryptionParams* params, AesNI_BoxBlock* output, @@ -144,29 +154,32 @@ static AesNI_StatusCode aesni_box_decrypt_aes256( return AESNI_SUCCESS; } -AesNI_BoxAlgorithmInterface aesni_box_aes128_iface = +AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes128 = { &aesni_box_derive_params_aes128, - &aesni_box_encrypt_aes128, - &aesni_box_decrypt_aes128, + &aesni_box_encrypt_block_aes128, + &aesni_box_decrypt_block_aes128, &aesni_box_xor_block_aes, - &aesni_box_inc_counter_aes, + &aesni_box_next_counter_aes, + &aesni_box_get_block_size_aes, }; -AesNI_BoxAlgorithmInterface aesni_box_aes192_iface = +AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192 = { &aesni_box_derive_params_aes192, - &aesni_box_encrypt_aes192, - &aesni_box_decrypt_aes192, + &aesni_box_encrypt_block_aes192, + &aesni_box_decrypt_block_aes192, &aesni_box_xor_block_aes, - &aesni_box_inc_counter_aes, + &aesni_box_next_counter_aes, + &aesni_box_get_block_size_aes, }; -AesNI_BoxAlgorithmInterface aesni_box_aes256_iface = +AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256 = { &aesni_box_derive_params_aes256, - &aesni_box_encrypt_aes256, - &aesni_box_decrypt_aes256, + &aesni_box_encrypt_block_aes256, + &aesni_box_decrypt_block_aes256, &aesni_box_xor_block_aes, - &aesni_box_inc_counter_aes, + &aesni_box_next_counter_aes, + &aesni_box_get_block_size_aes, }; diff --git a/test/decrypt_block_aes.cpp b/test/decrypt_block_aes.cpp index 3bb8e44..6c5e6f0 100644 --- a/test/decrypt_block_aes.cpp +++ b/test/decrypt_block_aes.cpp @@ -92,7 +92,7 @@ int main(int argc, char** argv) args.pop_front(); AesNI_BoxBlock plaintext; - aesni_box_decrypt( + aesni_box_decrypt_block( &box, &ciphertext, &plaintext, diff --git a/test/encrypt_block_aes.cpp b/test/encrypt_block_aes.cpp index 6de9288..d3d3705 100644 --- a/test/encrypt_block_aes.cpp +++ b/test/encrypt_block_aes.cpp @@ -92,7 +92,7 @@ int main(int argc, char** argv) args.pop_front(); AesNI_BoxBlock ciphertext; - aesni_box_encrypt( + aesni_box_encrypt_block( &box, &plaintext, &ciphertext, |