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 /src | |
parent | error message update (diff) | |
download | aes-tools-a386820259790f08073bc84bb868c9907558bb84.tar.gz aes-tools-a386820259790f08073bc84bb868c9907558bb84.zip |
refactoring
Diffstat (limited to '')
-rw-r--r-- | src/box.c | 94 | ||||
-rw-r--r-- | src/box_aes.c | 51 |
2 files changed, 79 insertions, 66 deletions
@@ -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, }; |