From 27d80744b46b6476ce4761a6199e6293d41c91ab Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 21 Jul 2026 18:57:25 +0200 Subject: minor refactoring --- aes/include/aes/box_aes.h | 6 +++--- aes/include/aes/box_data.h | 10 ++++++---- aes/src/box.c | 41 ++++++++++++++++++++--------------------- aes/src/box_aes.c | 6 +++--- aesxx/include/aesxx/box.hpp | 14 +++++--------- 5 files changed, 37 insertions(+), 40 deletions(-) diff --git a/aes/include/aes/box_aes.h b/aes/include/aes/box_aes.h index 476f692..c9b9b67 100644 --- a/aes/include/aes/box_aes.h +++ b/aes/include/aes/box_aes.h @@ -11,9 +11,9 @@ extern "C" { #endif -extern AES_BoxInterface aes128_box_interface; -extern AES_BoxInterface aes192_box_interface; -extern AES_BoxInterface aes256_box_interface; +extern AES_BoxOps aes128_box_ops; +extern AES_BoxOps aes192_box_ops; +extern AES_BoxOps aes256_box_ops; #ifdef __cplusplus } diff --git a/aes/include/aes/box_data.h b/aes/include/aes/box_data.h index b0a82a5..3e49fa0 100644 --- a/aes/include/aes/box_data.h +++ b/aes/include/aes/box_data.h @@ -6,6 +6,7 @@ #pragma once #include "aes.h" +#include "algorithm.h" #include "error.h" #include "mode.h" @@ -78,14 +79,15 @@ typedef struct { AES_BoxFormatKey format_key; AES_BoxEncryptBlock encrypt_block; AES_BoxDecryptBlock decrypt_block; -} AES_BoxInterface; +} AES_BoxOps; typedef struct { - const AES_BoxInterface* algorithm; - AES_BoxEncryptionRoundKeys encryption_keys; - AES_BoxDecryptionRoundKeys decryption_keys; + AES_Algorithm algorithm; AES_Mode mode; AES_Block iv; + AES_BoxEncryptionRoundKeys encryption_keys; + AES_BoxDecryptionRoundKeys decryption_keys; + const AES_BoxOps* ops; } AES_Box; #ifdef __cplusplus diff --git a/aes/src/box.c b/aes/src/box.c index 178f1dc..dd50f25 100644 --- a/aes/src/box.c +++ b/aes/src/box.c @@ -10,10 +10,10 @@ #include #include -static const AES_BoxInterface* aes_box_interfaces[] = { - &aes128_box_interface, - &aes192_box_interface, - &aes256_box_interface, +static const AES_BoxOps* aes_box_ops_list[] = { + &aes128_box_ops, + &aes192_box_ops, + &aes256_box_ops, }; AES_StatusCode aes_box_init( @@ -26,18 +26,18 @@ AES_StatusCode aes_box_init( ) { AES_StatusCode status = AES_SUCCESS; - box->algorithm = aes_box_interfaces[algorithm]; + box->algorithm = algorithm; + box->mode = mode; + if (iv != NULL) + box->iv = *iv; + box->ops = aes_box_ops_list[algorithm]; - status = box->algorithm->calc_round_keys( + status = box->ops->calc_round_keys( box_key, &box->encryption_keys, &box->decryption_keys, err_details ); if (aes_is_error(status)) return status; - box->mode = mode; - if (iv != NULL) - box->iv = *iv; - return status; } @@ -47,7 +47,7 @@ static AES_StatusCode aes_box_encrypt_block_ecb( AES_Block* output, AES_ErrorDetails* err_details ) { - return box->algorithm->encrypt_block(input, &box->encryption_keys, output, err_details); + return box->ops->encrypt_block(input, &box->encryption_keys, output, err_details); } static AES_StatusCode aes_box_encrypt_block_cbc( @@ -59,8 +59,7 @@ static AES_StatusCode aes_box_encrypt_block_cbc( AES_StatusCode status = AES_SUCCESS; AES_Block xored_input = aes_xor_blocks(*input, box->iv); - status = - box->algorithm->encrypt_block(&xored_input, &box->encryption_keys, output, err_details); + status = box->ops->encrypt_block(&xored_input, &box->encryption_keys, output, err_details); if (aes_is_error(status)) return status; @@ -76,7 +75,7 @@ static AES_StatusCode aes_box_encrypt_block_cfb( ) { AES_StatusCode status = AES_SUCCESS; - status = box->algorithm->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); + status = box->ops->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); if (aes_is_error(status)) return status; @@ -94,7 +93,7 @@ static AES_StatusCode aes_box_encrypt_block_ofb( ) { AES_StatusCode status = AES_SUCCESS; - status = box->algorithm->encrypt_block(&box->iv, &box->encryption_keys, &box->iv, err_details); + status = box->ops->encrypt_block(&box->iv, &box->encryption_keys, &box->iv, err_details); if (aes_is_error(status)) return status; @@ -112,7 +111,7 @@ static AES_StatusCode aes_box_encrypt_block_ctr( ) { AES_StatusCode status = AES_SUCCESS; - status = box->algorithm->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); + status = box->ops->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); if (aes_is_error(status)) return status; @@ -152,7 +151,7 @@ static AES_StatusCode aes_box_decrypt_block_ecb( AES_Block* output, AES_ErrorDetails* err_details ) { - return box->algorithm->decrypt_block(input, &box->decryption_keys, output, err_details); + return box->ops->decrypt_block(input, &box->decryption_keys, output, err_details); } static AES_StatusCode aes_box_decrypt_block_cbc( @@ -163,7 +162,7 @@ static AES_StatusCode aes_box_decrypt_block_cbc( ) { AES_StatusCode status = AES_SUCCESS; - status = box->algorithm->decrypt_block(input, &box->decryption_keys, output, err_details); + status = box->ops->decrypt_block(input, &box->decryption_keys, output, err_details); if (aes_is_error(status)) return status; @@ -181,7 +180,7 @@ static AES_StatusCode aes_box_decrypt_block_cfb( ) { AES_StatusCode status = AES_SUCCESS; - status = box->algorithm->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); + status = box->ops->encrypt_block(&box->iv, &box->encryption_keys, output, err_details); if (aes_is_error(status)) return status; @@ -546,7 +545,7 @@ AES_StatusCode aes_box_parse_key( if (src == NULL) return aes_error_null_argument(err_details, "src"); - return aes_box_interfaces[algorithm]->parse_key(dest, src, err_details); + return aes_box_ops_list[algorithm]->parse_key(dest, src, err_details); } AES_StatusCode aes_box_format_key( @@ -560,5 +559,5 @@ AES_StatusCode aes_box_format_key( if (src == NULL) return aes_error_null_argument(err_details, "src"); - return aes_box_interfaces[algorithm]->format_key(dest, src, err_details); + return aes_box_ops_list[algorithm]->format_key(dest, src, err_details); } diff --git a/aes/src/box_aes.c b/aes/src/box_aes.c index 368ec9e..d0c9a41 100644 --- a/aes/src/box_aes.c +++ b/aes/src/box_aes.c @@ -194,7 +194,7 @@ static AES_StatusCode aes_box_decrypt_block_aes256( return AES_SUCCESS; } -AES_BoxInterface aes128_box_interface = { +AES_BoxOps aes128_box_ops = { &aes_box_derive_params_aes128, &aes_box_parse_key_aes128, &aes_box_format_key_aes128, @@ -202,7 +202,7 @@ AES_BoxInterface aes128_box_interface = { &aes_box_decrypt_block_aes128, }; -AES_BoxInterface aes192_box_interface = { +AES_BoxOps aes192_box_ops = { &aes_box_derive_params_aes192, &aes_box_parse_key_aes192, &aes_box_format_key_aes192, @@ -210,7 +210,7 @@ AES_BoxInterface aes192_box_interface = { &aes_box_decrypt_block_aes192, }; -AES_BoxInterface aes256_box_interface = { +AES_BoxOps aes256_box_ops = { &aes_box_derive_params_aes256, &aes_box_parse_key_aes256, &aes_box_format_key_aes256, diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index e34c241..870b3a7 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -43,12 +43,11 @@ public: aes_box_parse_key(&dest, algorithm, src.data(), ErrorDetailsThrowsInDestructor{}); } - Box(Algorithm algorithm, const Key& key) : algorithm{algorithm}, mode{AES_ECB} { - aes_box_init(&impl, algorithm, &key, mode, nullptr, ErrorDetailsThrowsInDestructor{}); + Box(Algorithm algorithm, const Key& key) { + aes_box_init(&impl, algorithm, &key, AES_ECB, nullptr, ErrorDetailsThrowsInDestructor{}); } - Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) - : algorithm{algorithm}, mode{mode} { + Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) { aes_box_init(&impl, algorithm, &key, mode, &iv, ErrorDetailsThrowsInDestructor{}); } @@ -115,19 +114,16 @@ public: } Algorithm get_algorithm() const { - return algorithm; + return impl.algorithm; } Mode get_mode() const { - return mode; + return impl.mode; } private: Key key; - Algorithm algorithm; - Mode mode; - AES_Box impl; }; -- cgit v1.3