From 6b31d793706f8422e06b5148e366644074a48b5d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 26 Jul 2015 00:44:16 +0300 Subject: examples: merge to block utils --- CMakeLists.txt | 1 - TODO.md | 1 - cxx/include/aesnixx/aes.hpp | 86 +++++++++++++--------------- examples/CMakeLists.txt | 21 ------- examples/aes128cbc.cpp | 60 ------------------- examples/aes128cfb.cpp | 56 ------------------ examples/aes128ctr.cpp | 56 ------------------ examples/aes128ecb.cpp | 54 ------------------ examples/aes128ofb.cpp | 56 ------------------ examples/aes192cbc.cpp | 60 ------------------- examples/aes192cfb.cpp | 56 ------------------ examples/aes192ctr.cpp | 56 ------------------ examples/aes192ecb.cpp | 54 ------------------ examples/aes192ofb.cpp | 56 ------------------ examples/aes256cbc.cpp | 60 ------------------- examples/aes256cfb.cpp | 56 ------------------ examples/aes256ctr.cpp | 56 ------------------ examples/aes256ecb.cpp | 54 ------------------ examples/aes256ofb.cpp | 56 ------------------ examples/common.hpp | 105 ---------------------------------- utils/README.md | 7 +++ utils/aes_block_common.hpp | 136 +++++++++++++++++++++++++++++++++++++++++++- utils/aes_decrypt_block.cpp | 49 +++++++++++----- utils/aes_encrypt_block.cpp | 48 +++++++++++----- 24 files changed, 251 insertions(+), 1049 deletions(-) delete mode 100644 examples/CMakeLists.txt delete mode 100644 examples/aes128cbc.cpp delete mode 100644 examples/aes128cfb.cpp delete mode 100644 examples/aes128ctr.cpp delete mode 100644 examples/aes128ecb.cpp delete mode 100644 examples/aes128ofb.cpp delete mode 100644 examples/aes192cbc.cpp delete mode 100644 examples/aes192cfb.cpp delete mode 100644 examples/aes192ctr.cpp delete mode 100644 examples/aes192ecb.cpp delete mode 100644 examples/aes192ofb.cpp delete mode 100644 examples/aes256cbc.cpp delete mode 100644 examples/aes256cfb.cpp delete mode 100644 examples/aes256ctr.cpp delete mode 100644 examples/aes256ecb.cpp delete mode 100644 examples/aes256ofb.cpp delete mode 100644 examples/common.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 19b6707..582309a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,5 +16,4 @@ target_compile_definitions(libaesni PRIVATE _CRT_SECURE_NO_WARNINGS) add_subdirectory(cxx) -add_subdirectory(examples) add_subdirectory(utils) diff --git a/TODO.md b/TODO.md index cebb095..6084659 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ * Add file encryption tests. * Add unit tests to the library. * Using Boost.Test, perhaps? I'm using Boost anyway. -* Merge "examples" into block utilities. * Add algorithm benchmarks. diff --git a/cxx/include/aesnixx/aes.hpp b/cxx/include/aesnixx/aes.hpp index 1273c56..231e223 100644 --- a/cxx/include/aesnixx/aes.hpp +++ b/cxx/include/aesnixx/aes.hpp @@ -462,138 +462,128 @@ namespace aesni }; template - class Encrypt; + struct Encrypt; template - class Encrypt + struct Encrypt { - public: Encrypt(const typename Types::KeyT& key, const typename Types::BlockT& iv) { - expand_key(key, m_encryption_keys); - derive_decryption_keys(m_encryption_keys, m_decryption_keys); + expand_key(key, encryption_keys); + derive_decryption_keys(encryption_keys, decryption_keys); } inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) { - return encrypt_ecb(plaintext, m_encryption_keys); + return encrypt_ecb(plaintext, encryption_keys); } inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) { - return decrypt_ecb(ciphertext, m_decryption_keys); + return decrypt_ecb(ciphertext, decryption_keys); } - private: - typename Types::RoundKeysT m_encryption_keys; - typename Types::RoundKeysT m_decryption_keys; + typename Types::RoundKeysT encryption_keys; + typename Types::RoundKeysT decryption_keys; }; template - class Encrypt + struct Encrypt { - public: Encrypt(const typename Types::KeyT& key, const typename Types::BlockT& iv) - : m_iv(iv) + : iv(iv) { - expand_key(key, m_encryption_keys); - derive_decryption_keys(m_encryption_keys, m_decryption_keys); + expand_key(key, encryption_keys); + derive_decryption_keys(encryption_keys, decryption_keys); } inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) { - return encrypt_cbc(plaintext, m_encryption_keys, m_iv, m_iv); + return encrypt_cbc(plaintext, encryption_keys, iv, iv); } inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) { - return decrypt_cbc(ciphertext, m_decryption_keys, m_iv, m_iv); + return decrypt_cbc(ciphertext, decryption_keys, iv, iv); } - private: - typename Types::BlockT m_iv; - typename Types::RoundKeysT m_encryption_keys; - typename Types::RoundKeysT m_decryption_keys; + typename Types::BlockT iv; + typename Types::RoundKeysT encryption_keys; + typename Types::RoundKeysT decryption_keys; }; template - class Encrypt + struct Encrypt { - public: Encrypt(const typename Types::KeyT& key, const typename Types::BlockT& iv) - : m_iv(iv) + : iv(iv) { - expand_key(key, m_encryption_keys); + expand_key(key, encryption_keys); } inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) { - return encrypt_cfb(plaintext, m_encryption_keys, m_iv, m_iv); + return encrypt_cfb(plaintext, encryption_keys, iv, iv); } inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) { - return decrypt_cfb(ciphertext, m_encryption_keys, m_iv, m_iv); + return decrypt_cfb(ciphertext, encryption_keys, iv, iv); } - private: - typename Types::BlockT m_iv; - typename Types::RoundKeysT m_encryption_keys; + typename Types::BlockT iv; + typename Types::RoundKeysT encryption_keys; }; template - class Encrypt + struct Encrypt { - public: Encrypt(const typename Types::KeyT& key, const typename Types::BlockT& iv) - : m_iv(iv) + : iv(iv) { - expand_key(key, m_encryption_keys); + expand_key(key, encryption_keys); } inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) { - return encrypt_ofb(plaintext, m_encryption_keys, m_iv, m_iv); + return encrypt_ofb(plaintext, encryption_keys, iv, iv); } inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) { - return decrypt_ofb(ciphertext, m_encryption_keys, m_iv, m_iv); + return decrypt_ofb(ciphertext, encryption_keys, iv, iv); } - private: - typename Types::BlockT m_iv; - typename Types::RoundKeysT m_encryption_keys; + typename Types::BlockT iv; + typename Types::RoundKeysT encryption_keys; }; template - class Encrypt + struct Encrypt { - public: Encrypt(const typename Types::KeyT& key, const typename Types::BlockT& iv) - : m_iv(iv) + : iv(iv) { - expand_key(key, m_encryption_keys); + expand_key(key, encryption_keys); } inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) { - return encrypt_ctr(plaintext, m_encryption_keys, m_iv, m_iv); + return encrypt_ctr(plaintext, encryption_keys, iv, iv); } inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) { - return decrypt_ctr(ciphertext, m_encryption_keys, m_iv, m_iv); + return decrypt_ctr(ciphertext, encryption_keys, iv, iv); } - private: - typename Types::RoundKeysT m_encryption_keys; - typename Types::BlockT m_iv; + typename Types::RoundKeysT encryption_keys; + typename Types::BlockT iv; }; } } diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt deleted file mode 100644 index cb82577..0000000 --- a/examples/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -macro(example prefix) - add_executable(example_${prefix} ${prefix}.cpp) - target_link_libraries(example_${prefix} libaesni libaesnixx) - set_target_properties(example_${prefix} PROPERTIES OUTPUT_NAME ${prefix}) -endmacro() - -example(aes128ecb) -example(aes128cbc) -example(aes128cfb) -example(aes128ofb) -example(aes128ctr) -example(aes192ecb) -example(aes192cbc) -example(aes192cfb) -example(aes192ofb) -example(aes192ctr) -example(aes256ecb) -example(aes256cbc) -example(aes256cfb) -example(aes256ofb) -example(aes256ctr) diff --git a/examples/aes128cbc.cpp b/examples/aes128cbc.cpp deleted file mode 100644 index cf61604..0000000 --- a/examples/aes128cbc.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key128 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys128 encryption_keys; - aesni_aes128_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes128_encrypt_block_cbc(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - aesni::aes::RoundKeys128 decryption_keys; - aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - aesni::aes::Block decrypted = aesni_aes128_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes128cfb.cpp b/examples/aes128cfb.cpp deleted file mode 100644 index c8f7230..0000000 --- a/examples/aes128cfb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key128 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys128 encryption_keys; - aesni_aes128_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes128_encrypt_block_cfb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes128_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes128ctr.cpp b/examples/aes128ctr.cpp deleted file mode 100644 index 5419ce2..0000000 --- a/examples/aes128ctr.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key128 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys128 encryption_keys; - aesni_aes128_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes128_encrypt_block_ctr(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes128_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes128ecb.cpp b/examples/aes128ecb.cpp deleted file mode 100644 index d999f81..0000000 --- a/examples/aes128ecb.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key128 key; - make_default_key(key); - - aesni::aes::RoundKeys128 encryption_keys; - aesni_aes128_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - const auto ciphertext = aesni_aes128_encrypt_block_ecb(plaintext, &encryption_keys); - dump_ciphertext(ciphertext); - - aesni::aes::RoundKeys128 decryption_keys; - aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - const auto decrypted = aesni_aes128_decrypt_block_ecb(ciphertext, &decryption_keys); - dump_decrypted(decrypted); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes128ofb.cpp b/examples/aes128ofb.cpp deleted file mode 100644 index f5f93d0..0000000 --- a/examples/aes128ofb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key128 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys128 encryption_keys; - aesni_aes128_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes128_encrypt_block_ofb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes128_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes192cbc.cpp b/examples/aes192cbc.cpp deleted file mode 100644 index b2adabd..0000000 --- a/examples/aes192cbc.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key192 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys192 encryption_keys; - aesni_aes192_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes192_encrypt_block_cbc(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - aesni::aes::RoundKeys192 decryption_keys; - aesni_aes192_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - const auto decrypted = aesni_aes192_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes192cfb.cpp b/examples/aes192cfb.cpp deleted file mode 100644 index d7fd9b5..0000000 --- a/examples/aes192cfb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key192 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys192 encryption_keys; - aesni_aes192_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes192_encrypt_block_cfb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes192_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes192ctr.cpp b/examples/aes192ctr.cpp deleted file mode 100644 index 9cad355..0000000 --- a/examples/aes192ctr.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key192 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys192 encryption_keys; - aesni_aes192_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes192_encrypt_block_ctr(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes192_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/examples/aes192ecb.cpp b/examples/aes192ecb.cpp deleted file mode 100644 index 313f084..0000000 --- a/examples/aes192ecb.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key192 key; - make_default_key(key); - - aesni::aes::RoundKeys192 encryption_keys; - aesni_aes192_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - const auto ciphertext = aesni_aes192_encrypt_block_ecb(plaintext, &encryption_keys); - dump_ciphertext(ciphertext); - - aesni::aes::RoundKeys192 decryption_keys; - aesni_aes192_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - const auto decrypted = aesni_aes192_decrypt_block_ecb(ciphertext, &decryption_keys); - dump_decrypted(decrypted); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes192ofb.cpp b/examples/aes192ofb.cpp deleted file mode 100644 index e2d96c4..0000000 --- a/examples/aes192ofb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key192 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys192 encryption_keys; - aesni_aes192_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes192_encrypt_block_ofb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes192_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes256cbc.cpp b/examples/aes256cbc.cpp deleted file mode 100644 index c5ab427..0000000 --- a/examples/aes256cbc.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key256 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys256 encryption_keys; - aesni_aes256_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes256_encrypt_block_cbc(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - aesni::aes::RoundKeys256 decryption_keys; - aesni_aes256_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - const auto decrypted = aesni_aes256_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes256cfb.cpp b/examples/aes256cfb.cpp deleted file mode 100644 index fb43d3a..0000000 --- a/examples/aes256cfb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key256 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys256 encryption_keys; - aesni_aes256_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes256_encrypt_block_cfb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes256_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes256ctr.cpp b/examples/aes256ctr.cpp deleted file mode 100644 index 9827e79..0000000 --- a/examples/aes256ctr.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key256 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys256 encryption_keys; - aesni_aes256_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes256_encrypt_block_ctr(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes256_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/examples/aes256ecb.cpp b/examples/aes256ecb.cpp deleted file mode 100644 index e8777e2..0000000 --- a/examples/aes256ecb.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key256 key; - make_default_key(key); - - aesni::aes::RoundKeys256 encryption_keys; - aesni_aes256_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - const auto ciphertext = aesni_aes256_encrypt_block_ecb(plaintext, &encryption_keys); - dump_ciphertext(ciphertext); - - aesni::aes::RoundKeys256 decryption_keys; - aesni_aes256_derive_decryption_keys(&encryption_keys, &decryption_keys); - dump_decryption_keys(decryption_keys); - - const auto decrypted = aesni_aes256_decrypt_block_ecb(ciphertext, &decryption_keys); - dump_decrypted(decrypted); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/aes256ofb.cpp b/examples/aes256ofb.cpp deleted file mode 100644 index 1d806aa..0000000 --- a/examples/aes256ofb.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "common.hpp" - -#include - -#include - -#include -#include - -int main() -{ - try - { - aesni::aes::Block plaintext; - make_default_plaintext(plaintext); - - aesni::aes::Key256 key; - make_default_key(key); - - aesni::aes::Block iv; - make_default_iv(iv); - - aesni::aes::RoundKeys256 encryption_keys; - aesni_aes256_expand_key(&key, &encryption_keys); - dump_encryption_keys(encryption_keys); - - aesni::aes::Block next_iv; - const auto ciphertext = aesni_aes256_encrypt_block_ofb(plaintext, &encryption_keys, iv, &next_iv); - dump_ciphertext(ciphertext); - dump_next_iv(next_iv); - - const auto decrypted = aesni_aes256_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &next_iv); - dump_decrypted(decrypted); - dump_next_iv(next_iv); - - return 0; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} diff --git a/examples/common.hpp b/examples/common.hpp deleted file mode 100644 index 019a8da..0000000 --- a/examples/common.hpp +++ /dev/null @@ -1,105 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -#include - -#include - -#include - -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 - 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); - } - - void dump_decrypted(const aesni::aes::Block& decrypted) - { - dump_block("Decrypted", decrypted); - } - - void make_default_plaintext(aesni::aes::Block& plaintext) - { - aesni::make_block(plaintext, 0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); - dump_plaintext(plaintext); - } - - void make_default_key(aesni::aes::Key128& key) - { - aesni::aes::make_key(key, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); - dump_key(key); - } - - void make_default_key(aesni::aes::Key192& key) - { - aesni::aes::make_key(key, 0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); - dump_key(key); - } - - void make_default_key(aesni::aes::Key256& key) - { - aesni::aes::make_key(key, 0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); - dump_key(key); - } - - void make_default_iv(aesni::aes::Block& iv) - { - aesni::make_block(iv, 0xfedcba98, 0x76543210, 0xfedcba98, 0x76543210); - dump_iv(iv); - } - - template - 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 - void dump_encryption_keys(const RoundKeysT& round_keys) - { - dump_round_keys("Encryption round keys", round_keys); - } - - template - void dump_decryption_keys(const RoundKeysT& round_keys) - { - dump_round_keys("Decryption round keys", round_keys); - } -} diff --git a/utils/README.md b/utils/README.md index 61204c6..8403ee0 100644 --- a/utils/README.md +++ b/utils/README.md @@ -15,6 +15,13 @@ On older CPUs, you can run the utilities ## Block encryption +The block encryption utilities can produce verbose human-readable output, +including round keys, intermediate initialization vector values, etc. +This is primarily intended for debugging purposes. +Enable verbose output by passing the `--verbose` flag to the utilities. +Please note that verbose output can only be produced when *not* using "boxes" +(the `--boxes` flag). + ### aes_encrypt_block.exe Encrypts 16-byte blocks using AES-128/192/256 in the specified mode of 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(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); + ("algorithm,a", po::value(&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 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 + 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 + 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 + void dump_encryption_keys(const RoundKeysT& round_keys) + { + dump_round_keys("Encryption round keys", round_keys); + } + + template + void dump_decryption_keys(const RoundKeysT& 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 + { + 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 + { + 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); + } + }; + + template + struct Dumper + { + 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); + } + }; + + template + struct Dumper + { + 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); + } }; } diff --git a/utils/aes_decrypt_block.cpp b/utils/aes_decrypt_block.cpp index 778aecb..bab5f50 100644 --- a/utils/aes_decrypt_block.cpp +++ b/utils/aes_decrypt_block.cpp @@ -22,7 +22,8 @@ namespace template bool decrypt_with_mode( const std::string& key_str, - std::deque& ciphertexts) + std::deque& ciphertexts, + bool verbose = false) { typename aesni::aes::Types::BlockT iv; @@ -33,20 +34,40 @@ namespace aesni::aes::from_string(iv, ciphertexts.front()); ciphertexts.pop_front(); + + if (verbose) + dump_iv(iv); } typename aesni::aes::Types::KeyT key; aesni::aes::from_string(key, key_str); + if (verbose) + dump_key(key); + aesni::aes::Encrypt encrypt(key, iv); + if (verbose) + Dumper::dump_round_keys(encrypt); + while (!ciphertexts.empty()) { typename aesni::aes::Types::BlockT ciphertext; aesni::aes::from_string(ciphertext, ciphertexts.front()); ciphertexts.pop_front(); - std::cout << aesni::aes::to_string(encrypt.decrypt(ciphertext)) << "\n"; + const auto plaintext = encrypt.decrypt(ciphertext); + + if (verbose) + { + dump_ciphertext(ciphertext); + dump_plaintext(plaintext); + Dumper::dump_next_iv(encrypt); + } + else + { + std::cout << aesni::aes::to_string(plaintext) << "\n"; + } } return true; @@ -56,24 +77,25 @@ namespace bool decrypt_with_algorithm( aesni::Mode mode, const std::string& key_str, - std::deque& ciphertexts) + std::deque& ciphertexts, + bool verbose = false) { switch (mode) { case AESNI_ECB: - return decrypt_with_mode(key_str, ciphertexts); + return decrypt_with_mode(key_str, ciphertexts, verbose); case AESNI_CBC: - return decrypt_with_mode(key_str, ciphertexts); + return decrypt_with_mode(key_str, ciphertexts, verbose); case AESNI_CFB: - return decrypt_with_mode(key_str, ciphertexts); + return decrypt_with_mode(key_str, ciphertexts, verbose); case AESNI_OFB: - return decrypt_with_mode(key_str, ciphertexts); + return decrypt_with_mode(key_str, ciphertexts, verbose); case AESNI_CTR: - return decrypt_with_mode(key_str, ciphertexts); + return decrypt_with_mode(key_str, ciphertexts, verbose); default: return false; @@ -84,18 +106,19 @@ namespace aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key_str, - std::deque ciphertexts) + std::deque ciphertexts, + bool verbose = false) { switch (algorithm) { case AESNI_AES128: - return decrypt_with_algorithm(mode, key_str, ciphertexts); + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); case AESNI_AES192: - return decrypt_with_algorithm(mode, key_str, ciphertexts); + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); case AESNI_AES256: - return decrypt_with_algorithm(mode, key_str, ciphertexts); + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); default: return false; @@ -205,7 +228,7 @@ int main(int argc, char** argv) const auto success = cmd_parser.use_boxes() ? decrypt_using_boxes(algorithm, mode, key, ciphertexts) - : decrypt(algorithm, mode, key, ciphertexts); + : decrypt(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); if (!success) { diff --git a/utils/aes_encrypt_block.cpp b/utils/aes_encrypt_block.cpp index 89e935b..b814e99 100644 --- a/utils/aes_encrypt_block.cpp +++ b/utils/aes_encrypt_block.cpp @@ -22,7 +22,8 @@ namespace template bool encrypt_with_mode( const std::string& key_str, - std::deque& plaintexts) + std::deque& plaintexts, + bool verbose = false) { typename aesni::aes::Types::BlockT iv; @@ -33,20 +34,39 @@ namespace aesni::aes::from_string(iv, plaintexts.front()); plaintexts.pop_front(); + + if (verbose) + dump_iv(iv); } typename aesni::aes::Types::KeyT key; aesni::aes::from_string(key, key_str); + if (verbose) + dump_key(key); + aesni::aes::Encrypt encrypt(key, iv); + if (verbose) + Dumper::dump_round_keys(encrypt); + while (!plaintexts.empty()) { typename aesni::aes::Types::BlockT plaintext; aesni::aes::from_string(plaintext, plaintexts.front()); plaintexts.pop_front(); + const auto ciphertext = encrypt.encrypt(plaintext); - std::cout << aesni::aes::to_string(encrypt.encrypt(plaintext)) << "\n"; + if (verbose) + { + dump_plaintext(plaintext); + dump_ciphertext(ciphertext); + Dumper::dump_next_iv(encrypt); + } + else + { + std::cout << aesni::aes::to_string(ciphertext) << "\n"; + } } return true; @@ -56,24 +76,25 @@ namespace bool encrypt_with_algorithm( aesni::Mode mode, const std::string& key_str, - std::deque& plaintexts) + std::deque& plaintexts, + bool verbose = false) { switch (mode) { case AESNI_ECB: - return encrypt_with_mode(key_str, plaintexts); + return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CBC: - return encrypt_with_mode(key_str, plaintexts); + return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CFB: - return encrypt_with_mode(key_str, plaintexts); + return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_OFB: - return encrypt_with_mode(key_str, plaintexts); + return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CTR: - return encrypt_with_mode(key_str, plaintexts); + return encrypt_with_mode(key_str, plaintexts, verbose); default: return false; @@ -84,18 +105,19 @@ namespace aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key_str, - std::deque plaintexts) + std::deque plaintexts, + bool verbose = false) { switch (algorithm) { case AESNI_AES128: - return encrypt_with_algorithm(mode, key_str, plaintexts); + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); case AESNI_AES192: - return encrypt_with_algorithm(mode, key_str, plaintexts); + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); case AESNI_AES256: - return encrypt_with_algorithm(mode, key_str, plaintexts); + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); default: return false; @@ -205,7 +227,7 @@ int main(int argc, char** argv) const auto success = cmd_parser.use_boxes() ? encrypt_using_boxes(algorithm, mode, key, plaintexts) - : encrypt(algorithm, mode, key, plaintexts); + : encrypt(algorithm, mode, key, plaintexts, cmd_parser.verbose()); if (!success) { -- cgit v1.2.3