diff options
Diffstat (limited to '')
-rw-r--r-- | utils/aes_block_common.hpp | 136 |
1 files changed, 135 insertions, 1 deletions
diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp index 327efad..a61217d 100644 --- a/utils/aes_block_common.hpp +++ b/utils/aes_block_common.hpp @@ -69,6 +69,7 @@ namespace : m_program_name(program_name) , m_options("Options") , m_boxes(false) + , m_verbose(false) { } bool parse_options(int argc, char** argv) @@ -79,7 +80,8 @@ namespace ("help,h", "show this message and exit") ("box,b", po::bool_switch(&m_boxes)->default_value(false), "use the \"boxes\" interface") ("mode,m", po::value<aesni::Mode>(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value<aesni::Algorithm>(&m_algorithm)->required(), "set algorithm"); + ("algorithm,a", po::value<aesni::Algorithm>(&m_algorithm)->required(), "set algorithm") + ("verbose,v", po::bool_switch(&m_verbose)->default_value(false), "enable verbose output"); po::options_description hidden_options; hidden_options.add_options() @@ -130,6 +132,11 @@ namespace return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; } + bool verbose() const + { + return m_verbose; + } + private: const std::string m_program_name; boost::program_options::options_description m_options; @@ -138,5 +145,132 @@ namespace aesni::Algorithm m_algorithm; bool m_boxes; std::vector<std::string> m_args; + bool m_verbose; + }; +} + +namespace +{ + void dump_block(const char* name, const aesni::aes::Block& block) + { + std::cout << name << ": " << aesni::aes::to_string(block) << "\n" << aesni::aes::to_matrix_string(block) << "\n"; + } + + void dump_plaintext(const aesni::aes::Block& block) + { + dump_block("Plaintext", block); + } + + template <typename KeyT> + void dump_key(const KeyT& key) + { + std::cout << "Key: " << aesni::aes::to_string(key) << "\n\n"; + } + + void dump_ciphertext(const aesni::aes::Block& ciphertext) + { + dump_block("Ciphertext", ciphertext); + } + + void dump_iv(const aesni::aes::Block& iv) + { + dump_block("Initialization vector", iv); + } + + void dump_next_iv(const aesni::aes::Block& next_iv) + { + dump_block("Next initialization vector", next_iv); + } + + template <typename RoundKeysT> + void dump_round_keys(const char* name, const RoundKeysT& round_keys) + { + std::cout << name << ":\n"; + for (std::size_t i = 0; i < aesni::aes::get_number_of_rounds(round_keys); ++i) + std::cout << "\t[" << i << "]: " << aesni::aes::to_string(round_keys.keys[i]) << "\n"; + std::cout << "\n"; + } + + template <typename RoundKeysT> + void dump_encryption_keys(const RoundKeysT& round_keys) + { + dump_round_keys("Encryption round keys", round_keys); + } + + template <typename RoundKeysT> + void dump_decryption_keys(const RoundKeysT& round_keys) + { + dump_round_keys("Decryption round keys", round_keys); + } + + template <aesni::Algorithm algo, aesni::Mode mode> + struct Dumper; + + template <aesni::Algorithm algo> + struct Dumper<algo, AESNI_ECB> + { + static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_ECB>& encrypt) + { + dump_encryption_keys(encrypt.encryption_keys); + dump_decryption_keys(encrypt.decryption_keys); + } + + static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_ECB>&) + { } + }; + + template <aesni::Algorithm algo> + struct Dumper<algo, AESNI_CBC> + { + static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CBC>& encrypt) + { + dump_encryption_keys(encrypt.encryption_keys); + dump_decryption_keys(encrypt.decryption_keys); + } + + static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CBC>&) + { } + }; + + template <aesni::Algorithm algo> + struct Dumper<algo, AESNI_CFB> + { + static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt) + { + dump_encryption_keys(encrypt.encryption_keys); + } + + static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt) + { + ::dump_next_iv(encrypt.iv); + } + }; + + template <aesni::Algorithm algo> + struct Dumper<algo, AESNI_OFB> + { + static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt) + { + dump_encryption_keys(encrypt.encryption_keys); + } + + static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt) + { + ::dump_next_iv(encrypt.iv); + } + }; + + template <aesni::Algorithm algo> + struct Dumper<algo, AESNI_CTR> + { + static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt) + { + dump_encryption_keys(encrypt.encryption_keys); + } + + static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt) + { + ::dump_next_iv(encrypt.iv); + } }; } |