From 4720b5ac21163936fb47f524037a6f12ce6e6ce2 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 22 Jul 2026 18:31:50 +0200 Subject: rewrite: the beginning This a giant fuck-you to myself while I have nothing (actually, tons of things) to do in a hospital. * The giant screens of preprocessor code are gone, they always sucked, fuck them. * Switched everything to the "boxes" interface as the only one remaining. * Restuctured things in aes/ so that the files they're in make at least a little bit of sense. * Removed a bunch of cruft and prematurely generic code. * This is half-baked at best: I just made sure that the tests pass. A lot of cleanup awaits. --- aesxx/utils/decrypt_block.cpp | 133 +++--------------------------------------- 1 file changed, 7 insertions(+), 126 deletions(-) (limited to 'aesxx/utils/decrypt_block.cpp') diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp index c8edb34..7a296ff 100644 --- a/aesxx/utils/decrypt_block.cpp +++ b/aesxx/utils/decrypt_block.cpp @@ -3,7 +3,6 @@ // For details, see https://github.com/egor-tensin/aes-tools. // Distributed under the MIT License. -#include "helpers/block_dumper.hpp" #include "helpers/cmd_parser_block.hpp" #include @@ -18,128 +17,16 @@ namespace { -template -void decrypt_with_mode(const BlockSettings::Input& input, bool verbose = false) { - typename aes::Types::Block iv; - aes::make_block(iv, 0, 0, 0, 0); - - if (input.has_iv()) { - aes::from_string(iv, input.get_iv()); - if (verbose) - dump_iv(iv); - } - - typename aes::Types::Key key; - aes::from_string(key, input.get_key()); - if (verbose) - dump_key(key); - - aes::DecryptWrapper decrypt{key, iv}; - if (verbose) - dump_wrapper(decrypt); - - for (const auto& block : input.get_blocks()) { - typename aes::Types::Block ciphertext, plaintext; - aes::from_string(ciphertext, block); - - decrypt.decrypt_block(ciphertext, plaintext); - - if (verbose) { - dump_ciphertext(ciphertext); - dump_plaintext(plaintext); - dump_next_iv(decrypt); - } else { - std::cout << std::format("{}\n", aes::to_string(plaintext)); - } - } -} - -template -void decrypt_with_algorithm( - aes::Mode mode, - const BlockSettings::Input& input, - bool verbose = false -) { - switch (mode) { - case AES_ECB: - decrypt_with_mode(input, verbose); - break; - - case AES_CBC: - decrypt_with_mode(input, verbose); - break; - - case AES_CFB: - decrypt_with_mode(input, verbose); - break; - - case AES_OFB: - decrypt_with_mode(input, verbose); - break; - - case AES_CTR: - decrypt_with_mode(input, verbose); - break; - - default: - throw std::runtime_error("the selected mode of operation is not implemented"); - break; - } -} - -void decrypt_using_cxx_api( - aes::Algorithm algorithm, - aes::Mode mode, - const BlockSettings::Input& input, - bool verbose = false -) { - switch (algorithm) { - case AES_AES128: - decrypt_with_algorithm(mode, input, verbose); - break; - - case AES_AES192: - decrypt_with_algorithm(mode, input, verbose); - break; - - case AES_AES256: - decrypt_with_algorithm(mode, input, verbose); - break; - - default: - throw std::runtime_error("the selected algorithm is not implemented"); - break; - } -} - -void decrypt_using_particular_box(aes::Box& box, const std::vector& blocks) { - for (const auto& block : blocks) { - aes::Box::Block ciphertext; - box.parse_block(ciphertext, block); - - aes::Box::Block plaintext; - box.decrypt_block(ciphertext, plaintext); - std::cout << std::format("{}\n", box.format_block(plaintext)); - } -} - -void decrypt_using_boxes( - aes::Algorithm algorithm, - aes::Mode mode, - const BlockSettings::Input& input -) { +void decrypt_blocks(aes::Algorithm algorithm, aes::Mode mode, const BlockSettings::Input& input) { aes::Box::Key key; aes::Box::parse_key(key, algorithm, input.get_key()); - if (input.has_iv()) { - aes::Box::Block iv; - aes::Box::parse_block(iv, input.get_iv()); - aes::Box box{algorithm, key, mode, iv}; + aes::Box box{algorithm, key, mode, input.get_iv()}; - decrypt_using_particular_box(box, input.get_blocks()); - } else { - aes::Box box{algorithm, key}; - decrypt_using_particular_box(box, input.get_blocks()); + for (const auto& ciphertext : input.get_blocks()) { + aes::Block plaintext; + box.decrypt_block(ciphertext, plaintext); + std::cout << std::format("{}\n", plaintext.to_string()); } } @@ -162,13 +49,7 @@ int main(int argc, char** argv) { } for (const auto& input : settings.get_inputs()) { - if (settings.use_boxes()) { - decrypt_using_boxes(settings.get_algorithm(), settings.get_mode(), input); - } else { - decrypt_using_cxx_api( - settings.get_algorithm(), settings.get_mode(), input, settings.verbose() - ); - } + decrypt_blocks(settings.get_algorithm(), settings.get_mode(), input); } } catch (const aes::Error& e) { std::cerr << e; -- cgit v1.3