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 ++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'aesxx/include') 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 -- cgit v1.3