diff options
| author | Egor Tensin <egor@tensin.name> | 2026-07-22 18:31:50 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-07-22 21:07:53 +0200 |
| commit | 4720b5ac21163936fb47f524037a6f12ce6e6ce2 (patch) | |
| tree | 7b1f3f7f0e4047134048bb9b9967498b6f8ead12 /aesxx/utils/decrypt_block.cpp | |
| parent | minor refactoring (diff) | |
| download | aes-tools-4720b5ac21163936fb47f524037a6f12ce6e6ce2.tar.gz aes-tools-4720b5ac21163936fb47f524037a6f12ce6e6ce2.zip | |
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.
Diffstat (limited to 'aesxx/utils/decrypt_block.cpp')
| -rw-r--r-- | aesxx/utils/decrypt_block.cpp | 133 |
1 files changed, 7 insertions, 126 deletions
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 <aesxx/all.hpp> @@ -18,128 +17,16 @@ namespace { -template <aes::Algorithm algorithm, aes::Mode mode> -void decrypt_with_mode(const BlockSettings::Input& input, bool verbose = false) { - typename aes::Types<algorithm>::Block iv; - aes::make_block(iv, 0, 0, 0, 0); - - if (input.has_iv()) { - aes::from_string<algorithm>(iv, input.get_iv()); - if (verbose) - dump_iv<algorithm>(iv); - } - - typename aes::Types<algorithm>::Key key; - aes::from_string<algorithm>(key, input.get_key()); - if (verbose) - dump_key<algorithm>(key); - - aes::DecryptWrapper<algorithm, mode> decrypt{key, iv}; - if (verbose) - dump_wrapper<algorithm, mode>(decrypt); - - for (const auto& block : input.get_blocks()) { - typename aes::Types<algorithm>::Block ciphertext, plaintext; - aes::from_string<algorithm>(ciphertext, block); - - decrypt.decrypt_block(ciphertext, plaintext); - - if (verbose) { - dump_ciphertext<algorithm>(ciphertext); - dump_plaintext<algorithm>(plaintext); - dump_next_iv<algorithm, mode>(decrypt); - } else { - std::cout << std::format("{}\n", aes::to_string<algorithm>(plaintext)); - } - } -} - -template <aes::Algorithm algorithm> -void decrypt_with_algorithm( - aes::Mode mode, - const BlockSettings::Input& input, - bool verbose = false -) { - switch (mode) { - case AES_ECB: - decrypt_with_mode<algorithm, AES_ECB>(input, verbose); - break; - - case AES_CBC: - decrypt_with_mode<algorithm, AES_CBC>(input, verbose); - break; - - case AES_CFB: - decrypt_with_mode<algorithm, AES_CFB>(input, verbose); - break; - - case AES_OFB: - decrypt_with_mode<algorithm, AES_OFB>(input, verbose); - break; - - case AES_CTR: - decrypt_with_mode<algorithm, AES_CTR>(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<AES_AES128>(mode, input, verbose); - break; - - case AES_AES192: - decrypt_with_algorithm<AES_AES192>(mode, input, verbose); - break; - - case AES_AES256: - decrypt_with_algorithm<AES_AES256>(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<std::string>& 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; |
