From bf65cb89410cdb2d831094980631ecf4ef8f2928 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 24 Jul 2026 12:03:01 +0200 Subject: aesxx/utils: fix the --verbose option --- aesxx/include/aesxx/box.hpp | 62 ++++++++++++++++++++++---------- aesxx/utils/decrypt_block.cpp | 13 +++++-- aesxx/utils/encrypt_block.cpp | 13 +++++-- aesxx/utils/helpers/cmd_parser_block.hpp | 9 +++-- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index 1a4eec1..2a9659a 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -24,22 +24,20 @@ class Box { public: using Key = AES_Key; - /* - static std::string format_key(const Key& src, Algorithm algorithm) { - AES_KeyString str; - aes_format_key(&str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast(&str); - } - */ - static void parse_key(Key& dest, Algorithm algorithm, std::string_view src) { aes_parse_key(algorithm, &dest, src.data(), ErrorDetailsThrowsInDestructor{}); } - Box(Algorithm algorithm, const Key& key, Mode mode, const std::optional& iv) { + Box(Algorithm algorithm, + const Key& key, + Mode mode, + const std::optional& iv, + bool verbose = false) + : verbose{verbose} { aes_box_init( &impl, algorithm, &key, mode, iv ? iv->ptr() : NULL, ErrorDetailsThrowsInDestructor{} ); + dump_key(key); } Algorithm get_algorithm() const { @@ -51,19 +49,21 @@ public: } void encrypt_block(const Block& plaintext, Block& ciphertext) { - dump_block("Plaintext", plaintext); + dump_iv(); + dump_plaintext(plaintext); aes_box_encrypt_block( &impl, plaintext.ptr(), ciphertext.ptr(), ErrorDetailsThrowsInDestructor{} ); - dump_block("Ciphertext", ciphertext); + dump_ciphertext(ciphertext); } void decrypt_block(const Block& ciphertext, Block& plaintext) { - dump_block("Ciphertext", ciphertext); + dump_iv(); + dump_ciphertext(ciphertext); aes_box_decrypt_block( &impl, ciphertext.ptr(), plaintext.ptr(), ErrorDetailsThrowsInDestructor{} ); - dump_block("Plaintext", plaintext); + dump_plaintext(plaintext); } std::vector encrypt_buffer(const void* src_buf, std::size_t src_size) { @@ -113,15 +113,41 @@ public: } private: - void dump_block(std::string_view header, const Block& block) { - if (verbose > 0) + static std::string format_key(const Key& src, Algorithm algorithm) { + AES_KeyString str; + aes_format_key(algorithm, &str, &src, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast(&str); + } + + void dump_key(const Key& src) const { + if (verbose) + std::cout << std::format("Key : {}\n", format_key(src, get_algorithm())); + } + + Block get_iv() const { + return Block{impl.iv}; + } + + void dump_iv() const { + if (verbose) + std::cout << std::format("Init vector : {}\n", get_iv().to_string()); + } + + void dump_block(std::string_view header, const Block& block) const { + if (verbose) std::cout << std::format("{}: {}\n", header, block.to_string()); - if (verbose > 1) - std::cout << std::format("{}\n", block.to_matrix_string()); + } + + void dump_plaintext(const Block& src) const { + dump_block("Plaintext ", src); + } + + void dump_ciphertext(const Block& src) const { + dump_block("Ciphertext ", src); } AES_Box impl; - int verbose = 0; + bool verbose = false; }; } // namespace aes diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp index c27afd6..9f9ee2a 100644 --- a/aesxx/utils/decrypt_block.cpp +++ b/aesxx/utils/decrypt_block.cpp @@ -17,11 +17,16 @@ namespace { -void decrypt_blocks(aes::Algorithm algorithm, aes::Mode mode, const BlockSettings::Input& input) { +void decrypt_blocks( + aes::Algorithm algorithm, + aes::Mode mode, + const BlockSettings::Input& input, + bool verbose = false +) { aes::Box::Key key; aes::Box::parse_key(key, algorithm, input.get_key()); - aes::Box box{algorithm, key, mode, input.get_iv()}; + aes::Box box{algorithm, key, mode, input.get_iv(), verbose}; for (const auto& ciphertext : input.get_blocks()) { aes::Block plaintext; @@ -49,7 +54,9 @@ int main(int argc, char** argv) { } for (const auto& input : settings.get_inputs()) { - decrypt_blocks(settings.get_algorithm(), settings.get_mode(), input); + decrypt_blocks( + settings.get_algorithm(), settings.get_mode(), input, settings.get_verbose() + ); } } catch (const aes::Error& e) { std::cerr << e; diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index 246218a..5f84922 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_block.cpp @@ -17,11 +17,16 @@ namespace { -void encrypt_blocks(aes::Algorithm algorithm, aes::Mode mode, const BlockSettings::Input& input) { +void encrypt_blocks( + aes::Algorithm algorithm, + aes::Mode mode, + const BlockSettings::Input& input, + bool verbose = false +) { aes::Box::Key key; aes::Box::parse_key(key, algorithm, input.get_key()); - aes::Box box{algorithm, key, mode, input.get_iv()}; + aes::Box box{algorithm, key, mode, input.get_iv(), verbose}; for (const auto& plaintext : input.get_blocks()) { aes::Block ciphertext; @@ -49,7 +54,9 @@ int main(int argc, char** argv) { } for (const auto& input : settings.get_inputs()) { - encrypt_blocks(settings.get_algorithm(), settings.get_mode(), input); + encrypt_blocks( + settings.get_algorithm(), settings.get_mode(), input, settings.get_verbose() + ); } } catch (const aes::Error& e) { std::cerr << e; diff --git a/aesxx/utils/helpers/cmd_parser_block.hpp b/aesxx/utils/helpers/cmd_parser_block.hpp index b463656..e8ba1b6 100644 --- a/aesxx/utils/helpers/cmd_parser_block.hpp +++ b/aesxx/utils/helpers/cmd_parser_block.hpp @@ -52,7 +52,7 @@ public: explicit BlockSettings(std::string_view argv0) : SettingsParser{argv0} { namespace po = boost::program_options; - visible.add_options()("verbose,v", po::bool_switch(&_verbose), "enable verbose output"); + visible.add_options()("verbose,v", po::bool_switch(&verbose), "enable verbose output"); visible.add_options()( "algorithm,a", po::value(&algorithm)->required()->value_name("NAME"), "set algorithm" ); @@ -94,8 +94,8 @@ public: return inputs; } - bool verbose() const { - return _verbose; + bool get_verbose() const { + return verbose; } private: @@ -140,6 +140,5 @@ private: aes::Algorithm algorithm; aes::Mode mode; std::vector inputs; - - bool _verbose = false; + bool verbose = false; }; -- cgit v1.3