From 2f7feb1d9222e0afaac8ae17db98d1556aa46aa4 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 2 Aug 2015 20:35:38 +0300 Subject: cxx: more algorithm-agnostic API The code (in the utilities in particular) is a mess though, so a refactoring's coming up. --- utils/aes_block_common.hpp | 140 +++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 87 deletions(-) (limited to 'utils/aes_block_common.hpp') diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp index bdb6ac6..52b9ce3 100644 --- a/utils/aes_block_common.hpp +++ b/utils/aes_block_common.hpp @@ -151,126 +151,92 @@ namespace namespace { - void dump_block(const char* name, const aesni::aes::Block& block) + template + void dump_block(const char* name, const typename aesni::Types::Block& block) { - std::cout << name << ": " << aesni::aes::to_string(block) << "\n" << aesni::aes::to_matrix_string(block) << "\n"; + std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; } - void dump_plaintext(const aesni::aes::Block& block) + template + void dump_plaintext(const typename aesni::Types::Block& block) { - dump_block("Plaintext", block); + dump_block("Plaintext", block); } - template - void dump_key(const KeyT& key) + template + void dump_key(const typename aesni::Types::Key& key) { - std::cout << "Key: " << aesni::aes::to_string(key) << "\n\n"; + std::cout << "Key: " << aesni::to_string(key) << "\n\n"; } - void dump_ciphertext(const aesni::aes::Block& ciphertext) + template + void dump_ciphertext(const typename aesni::Types::Block& ciphertext) { - dump_block("Ciphertext", ciphertext); + dump_block("Ciphertext", ciphertext); } - void dump_iv(const aesni::aes::Block& iv) + template + void dump_iv(const typename aesni::Types::Block& iv) { - dump_block("Initialization vector", iv); + dump_block("Initialization vector", iv); } - void dump_next_iv(const aesni::aes::Block& next_iv) - { - dump_block("Next initialization vector", next_iv); - } - - template - void dump_round_keys(const char* name, const RoundKeysT& round_keys) + template + void dump_round_keys(const char* name, const typename aesni::Types::RoundKeys& 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"; + for (std::size_t i = 0; i < aesni::get_number_of_rounds(); ++i) + std::cout << "\t[" << i << "]: " << aesni::to_string(round_keys.keys[i]) << "\n"; std::cout << "\n"; } - template - void dump_encryption_keys(const RoundKeysT& round_keys) + template + void dump_encryption_keys(const typename aesni::Types::RoundKeys& round_keys) { - dump_round_keys("Encryption round keys", round_keys); + dump_round_keys("Encryption round keys", round_keys); } - template - void dump_decryption_keys(const RoundKeysT& round_keys) + template + void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) { - dump_round_keys("Decryption round keys", round_keys); + dump_round_keys("Decryption round keys", round_keys); } - template - struct Dumper; - - template - struct Dumper - { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - dump_decryption_keys(encrypt.decryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt&) - { } - }; - - template - struct Dumper + template + void dump_wrapper( + const aesni::EncryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - dump_decryption_keys(encrypt.decryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt&) - { } - }; + dump_encryption_keys(wrapper.encryption_keys); + } - template - struct Dumper + template + void dump_wrapper( + const aesni::DecryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + dump_decryption_keys(wrapper.decryption_keys); + } - template - struct Dumper + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } + dump_block("Next initialization vector", wrapper.iv); + } - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper&) + { } - template - struct Dumper + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } + dump_block("Next initialization vector", wrapper.iv); + } - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper&) + { } } -- cgit v1.2.3