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/encrypt_block.cpp | 136 +++--------------------------------------- 1 file changed, 7 insertions(+), 129 deletions(-) (limited to 'aesxx/utils/encrypt_block.cpp') diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index a796483..40d9f1a 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_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,131 +17,16 @@ namespace { -template -void encrypt_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::EncryptWrapper encrypt{key, iv}; - if (verbose) - dump_wrapper(encrypt); - - for (const auto& input_block_string : input.get_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 << std::format("{}\n", aes::to_string(ciphertext)); - } - } -} - -template -void encrypt_with_algorithm( - aes::Mode mode, - const BlockSettings::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 BlockSettings::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 << std::format("{}\n", box.format_block(ciphertext)); - } -} - -void encrypt_using_boxes( - aes::Algorithm algorithm, - aes::Mode mode, - const BlockSettings::Input& input -) { +void encrypt_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()}; - encrypt_using_particular_box(box, input.get_blocks()); - } else { - aes::Box box{algorithm, key}; - encrypt_using_particular_box(box, input.get_blocks()); + for (const auto& plaintext : input.get_blocks()) { + aes::Block ciphertext; + box.encrypt_block(plaintext, ciphertext); + std::cout << std::format("{}\n", ciphertext.to_string()); } } @@ -165,13 +49,7 @@ int main(int argc, char** argv) { } for (const auto& input : settings.get_inputs()) { - if (settings.use_boxes()) { - encrypt_using_boxes(settings.get_algorithm(), settings.get_mode(), input); - } else { - encrypt_using_cxx_api( - settings.get_algorithm(), settings.get_mode(), input, settings.verbose() - ); - } + encrypt_blocks(settings.get_algorithm(), settings.get_mode(), input); } } catch (const aes::Error& e) { std::cerr << e; -- cgit v1.3