From 6cac3cb57cb4f430df80f8a21e358ad40ae59c89 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 13 Jun 2026 14:37:12 +0200 Subject: add .clang-format --- aesxx/utils/encrypt_block.cpp | 272 ++++++++++++++++++------------------------ 1 file changed, 113 insertions(+), 159 deletions(-) (limited to 'aesxx/utils/encrypt_block.cpp') diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index 0b87364..b6bdca9 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_block.cpp @@ -16,198 +16,152 @@ #include #include -namespace -{ - template - void encrypt_with_mode( - const Input& input, - bool verbose = false) - { - typename aes::Types::Block iv; - - if (aes::ModeRequiresInitVector::value) - { - aes::from_string(iv, input.iv); - if (verbose) - dump_iv(iv); - } +namespace { - typename aes::Types::Key key; - aes::from_string(key, input.key); - if (verbose) - dump_key(key); +template +void encrypt_with_mode(const Input& input, bool verbose = false) { + typename aes::Types::Block iv; - aes::EncryptWrapper encrypt{key, iv}; + if (aes::ModeRequiresInitVector::value) { + aes::from_string(iv, input.iv); if (verbose) - dump_wrapper(encrypt); + dump_iv(iv); + } - for (const auto& input_block_string : input.blocks) - { - typename aes::Types::Block plaintext, ciphertext; - aes::from_string(plaintext, input_block_string); + typename aes::Types::Key key; + aes::from_string(key, input.key); + if (verbose) + dump_key(key); - encrypt.encrypt_block(plaintext, ciphertext); + aes::EncryptWrapper encrypt{key, iv}; + if (verbose) + dump_wrapper(encrypt); - if (verbose) - { - dump_plaintext(plaintext); - dump_ciphertext(ciphertext); - dump_next_iv(encrypt); - } - else - { - std::cout << aes::to_string(ciphertext) << '\n'; - } + for (const auto& input_block_string : input.blocks) { + typename aes::Types::Block plaintext, ciphertext; + aes::from_string(plaintext, input_block_string); + + encrypt.encrypt_block(plaintext, ciphertext); + + if (verbose) { + dump_plaintext(plaintext); + dump_ciphertext(ciphertext); + dump_next_iv(encrypt); + } else { + std::cout << aes::to_string(ciphertext) << '\n'; } } +} - template - void encrypt_with_algorithm( - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (mode) - { - case AES_ECB: - encrypt_with_mode(input, verbose); - break; - - case AES_CBC: - encrypt_with_mode(input, verbose); - break; - - case AES_CFB: - encrypt_with_mode(input, verbose); - break; - - case AES_OFB: - encrypt_with_mode(input, verbose); - break; - - case AES_CTR: - encrypt_with_mode(input, verbose); - break; - - default: - throw std::runtime_error("the selected mode of operation is not implemented"); - break; - } +template +void encrypt_with_algorithm(aes::Mode mode, const Input& input, bool verbose = false) { + switch (mode) { + case AES_ECB: + encrypt_with_mode(input, verbose); + break; + + case AES_CBC: + encrypt_with_mode(input, verbose); + break; + + case AES_CFB: + encrypt_with_mode(input, verbose); + break; + + case AES_OFB: + encrypt_with_mode(input, verbose); + break; + + case AES_CTR: + encrypt_with_mode(input, verbose); + break; + + default: + throw std::runtime_error("the selected mode of operation is not implemented"); + break; } +} - void encrypt_using_cxx_api( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (algorithm) - { - case AES_AES128: - encrypt_with_algorithm(mode, input, verbose); - break; - - case AES_AES192: - encrypt_with_algorithm(mode, input, verbose); - break; - - case AES_AES256: - encrypt_with_algorithm(mode, input, verbose); - break; - - default: - throw std::runtime_error("the selected algorithm is not implemented"); - break; - } +void encrypt_using_cxx_api(aes::Algorithm algorithm, + aes::Mode mode, + const Input& input, + bool verbose = false) { + switch (algorithm) { + case AES_AES128: + encrypt_with_algorithm(mode, input, verbose); + break; + + case AES_AES192: + encrypt_with_algorithm(mode, input, verbose); + break; + + case AES_AES256: + encrypt_with_algorithm(mode, input, verbose); + break; + + default: + throw std::runtime_error("the selected algorithm is not implemented"); + break; } +} - void encrypt_using_particular_box( - aes::Box& box, - const std::vector& input_block_strings) - { - for (const auto& input_block_string : input_block_strings) - { - aes::Box::Block plaintext; - box.parse_block(plaintext, input_block_string); - - aes::Box::Block ciphertext; - box.encrypt_block(plaintext, ciphertext); - std::cout << box.format_block(ciphertext) << '\n'; - } +void encrypt_using_particular_box(aes::Box& box, + const std::vector& input_block_strings) { + for (const auto& input_block_string : input_block_strings) { + aes::Box::Block plaintext; + box.parse_block(plaintext, input_block_string); + + aes::Box::Block ciphertext; + box.encrypt_block(plaintext, ciphertext); + std::cout << box.format_block(ciphertext) << '\n'; } +} - void encrypt_using_boxes( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input) - { - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.key); - - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, input.iv); - aes::Box box{algorithm, key, mode, iv}; - - encrypt_using_particular_box(box, input.blocks); - } - else - { - aes::Box box{algorithm, key}; - encrypt_using_particular_box(box, input.blocks); - } +void encrypt_using_boxes(aes::Algorithm algorithm, aes::Mode mode, const Input& input) { + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, input.key); + + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, input.iv); + aes::Box box{algorithm, key, mode, iv}; + + encrypt_using_particular_box(box, input.blocks); + } else { + aes::Box box{algorithm, key}; + encrypt_using_particular_box(box, input.blocks); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { BlockSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } - for (const auto& input : settings.inputs) - { - if (settings.use_boxes) - { - encrypt_using_boxes( - settings.algorithm, - settings.mode, - input); - } - else - { - encrypt_using_cxx_api( - settings.algorithm, - settings.mode, - input, - settings.verbose); + for (const auto& input : settings.inputs) { + if (settings.use_boxes) { + encrypt_using_boxes(settings.algorithm, settings.mode, input); + } else { + encrypt_using_cxx_api(settings.algorithm, settings.mode, input, settings.verbose); } } - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } -- cgit v1.3