From c162067e7407655040128b510481c01a762ac7d3 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 19 Jun 2015 18:12:39 +0300 Subject: rename executables --- test/CMakeLists.txt | 16 +++--- test/aes_common.hpp | 134 +++++++++++++++++++++++++++++++++++++++++++++ test/aes_decrypt_block.cpp | 122 +++++++++++++++++++++++++++++++++++++++++ test/aes_encrypt_block.cpp | 122 +++++++++++++++++++++++++++++++++++++++++ test/common_aes.hpp | 134 --------------------------------------------- test/decrypt_block_aes.cpp | 122 ----------------------------------------- test/encrypt_block_aes.cpp | 122 ----------------------------------------- 7 files changed, 386 insertions(+), 386 deletions(-) create mode 100644 test/aes_common.hpp create mode 100644 test/aes_decrypt_block.cpp create mode 100644 test/aes_encrypt_block.cpp delete mode 100644 test/common_aes.hpp delete mode 100644 test/decrypt_block_aes.cpp delete mode 100644 test/encrypt_block_aes.cpp (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0ee556f..2d58160 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,12 +26,12 @@ test(aes256ctr) find_package(Boost REQUIRED COMPONENTS program_options) -add_executable(test_encrypt_block_aes encrypt_block_aes.cpp common_aes.hpp) -target_include_directories(test_encrypt_block_aes PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(test_encrypt_block_aes libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(test_encrypt_block_aes PROPERTIES OUTPUT_NAME encrypt_block_aes) +add_executable(test_aes_encrypt_block aes_encrypt_block.cpp aes_common.hpp) +target_include_directories(test_aes_encrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(test_aes_encrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(test_aes_encrypt_block PROPERTIES OUTPUT_NAME aes_encrypt_block) -add_executable(test_decrypt_block_aes decrypt_block_aes.cpp common_aes.hpp) -target_include_directories(test_decrypt_block_aes PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(test_decrypt_block_aes libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(test_decrypt_block_aes PROPERTIES OUTPUT_NAME decrypt_block_aes) +add_executable(test_aes_decrypt_block aes_decrypt_block.cpp aes_common.hpp) +target_include_directories(test_aes_decrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(test_aes_decrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(test_aes_decrypt_block PROPERTIES OUTPUT_NAME aes_decrypt_block) diff --git a/test/aes_common.hpp b/test/aes_common.hpp new file mode 100644 index 0000000..08b92f4 --- /dev/null +++ b/test/aes_common.hpp @@ -0,0 +1,134 @@ +/** + * \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 + +#include + +#include +#include +#include +#include +#include +#include + +static std::istream& operator>>(std::istream& is, AesNI_BoxMode& dest) +{ + std::string src; + is >> src; + + if (boost::iequals(src, "ecb")) + dest = AESNI_ECB; + else if (boost::iequals(src, "cbc")) + dest = AESNI_CBC; + else if (boost::iequals(src, "cfb")) + dest = AESNI_CFB; + else if (boost::iequals(src, "ofb")) + dest = AESNI_OFB; + else if (boost::iequals(src, "ctr")) + dest = AESNI_CTR; + else + throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); + + return is; +} + +static std::istream& operator>>(std::istream& is, AesNI_BoxAlgorithm& dest) +{ + std::string src; + is >> src; + + if (boost::iequals(src, "aes128")) + dest = AESNI_AES128; + else if (boost::iequals(src, "aes192")) + dest = AESNI_AES192; + else if (boost::iequals(src, "aes256")) + dest = AESNI_AES256; + else + throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); + + return is; +} + +namespace +{ + class CommandLineParser + { + public: + CommandLineParser(const std::string& program_name) + : m_program_name(program_name) + , m_options("Options") + { } + + bool parse_options(int argc, char** argv) + { + namespace po = boost::program_options; + + m_options.add_options() + ("help,h", "show this message and exit") + ("mode,m", po::value(&m_mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&m_args)); + + po::options_description all_options; + all_options.add(m_options).add(hidden_options); + + po::positional_options_description positional_options; + positional_options.add("positional", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); + + if (vm.count("help")) + { + print_usage(); + return false; + } + + po::notify(vm); + return true; + } + + void print_usage() + { + std::cout << "Usage: " << m_program_name << " [OPTIONS...] [-- KEY [IV] [PLAINTEXT...]...]\n"; + std::cout << m_options << "\n"; + } + + AesNI_BoxMode get_mode() const + { + return m_mode; + } + + AesNI_BoxAlgorithm get_algorithm() const + { + return m_algorithm; + } + + std::deque get_args() + { + return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; + } + + private: + const std::string m_program_name; + boost::program_options::options_description m_options; + + AesNI_BoxMode m_mode; + AesNI_BoxAlgorithm m_algorithm; + std::vector m_args; + }; +} diff --git a/test/aes_decrypt_block.cpp b/test/aes_decrypt_block.cpp new file mode 100644 index 0000000..9898d4e --- /dev/null +++ b/test/aes_decrypt_block.cpp @@ -0,0 +1,122 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include +#include + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_block_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (cmd_parser.get_algorithm()) + { + case AESNI_AES128: + aesni::aes::from_string(algorithm_params.aes128_key, args.front()); + break; + + case AESNI_AES192: + aesni::aes::from_string(algorithm_params.aes192_key, args.front()); + break; + + case AESNI_AES256: + aesni::aes::from_string(algorithm_params.aes256_key, args.front()); + break; + } + + args.pop_front(); + + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + switch (cmd_parser.get_mode()) + { + case AESNI_ECB: + break; + + case AESNI_CBC: + case AESNI_CFB: + case AESNI_OFB: + case AESNI_CTR: + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + aesni::aes::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + break; + } + + AesNI_Box box; + aesni_box_init( + &box, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_mode(), + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + AesNI_BoxBlock ciphertext; + aesni::aes::from_string(ciphertext.aes_block, args.front()); + args.pop_front(); + + AesNI_BoxBlock plaintext; + aesni_box_decrypt_block( + &box, + &ciphertext, + &plaintext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n"; + } + } + + return 0; + } + catch (const boost::program_options::error& e) + { + std::cerr << "Usage error: " << e.what() << "\n"; + return 1; + } + 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/test/aes_encrypt_block.cpp b/test/aes_encrypt_block.cpp new file mode 100644 index 0000000..9458831 --- /dev/null +++ b/test/aes_encrypt_block.cpp @@ -0,0 +1,122 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include +#include + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_block_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (cmd_parser.get_algorithm()) + { + case AESNI_AES128: + aesni::aes::from_string(algorithm_params.aes128_key, args.front()); + break; + + case AESNI_AES192: + aesni::aes::from_string(algorithm_params.aes192_key, args.front()); + break; + + case AESNI_AES256: + aesni::aes::from_string(algorithm_params.aes256_key, args.front()); + break; + } + + args.pop_front(); + + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + switch (cmd_parser.get_mode()) + { + case AESNI_ECB: + break; + + case AESNI_CBC: + case AESNI_CFB: + case AESNI_OFB: + case AESNI_CTR: + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + aesni::aes::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + break; + } + + AesNI_Box box; + aesni_box_init( + &box, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_mode(), + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + AesNI_BoxBlock plaintext; + aesni::aes::from_string(plaintext.aes_block, args.front()); + args.pop_front(); + + AesNI_BoxBlock ciphertext; + aesni_box_encrypt_block( + &box, + &plaintext, + &ciphertext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n"; + } + } + + return 0; + } + catch (const boost::program_options::error& e) + { + std::cerr << "Usage error: " << e.what() << "\n"; + return 1; + } + 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/test/common_aes.hpp b/test/common_aes.hpp deleted file mode 100644 index 08b92f4..0000000 --- a/test/common_aes.hpp +++ /dev/null @@ -1,134 +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 - -#include - -#include -#include -#include -#include -#include -#include - -static std::istream& operator>>(std::istream& is, AesNI_BoxMode& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "ecb")) - dest = AESNI_ECB; - else if (boost::iequals(src, "cbc")) - dest = AESNI_CBC; - else if (boost::iequals(src, "cfb")) - dest = AESNI_CFB; - else if (boost::iequals(src, "ofb")) - dest = AESNI_OFB; - else if (boost::iequals(src, "ctr")) - dest = AESNI_CTR; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); - - return is; -} - -static std::istream& operator>>(std::istream& is, AesNI_BoxAlgorithm& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "aes128")) - dest = AESNI_AES128; - else if (boost::iequals(src, "aes192")) - dest = AESNI_AES192; - else if (boost::iequals(src, "aes256")) - dest = AESNI_AES256; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); - - return is; -} - -namespace -{ - class CommandLineParser - { - public: - CommandLineParser(const std::string& program_name) - : m_program_name(program_name) - , m_options("Options") - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - m_options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&m_args)); - - po::options_description all_options; - all_options.add(m_options).add(hidden_options); - - po::positional_options_description positional_options; - positional_options.add("positional", -1); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); - - if (vm.count("help")) - { - print_usage(); - return false; - } - - po::notify(vm); - return true; - } - - void print_usage() - { - std::cout << "Usage: " << m_program_name << " [OPTIONS...] [-- KEY [IV] [PLAINTEXT...]...]\n"; - std::cout << m_options << "\n"; - } - - AesNI_BoxMode get_mode() const - { - return m_mode; - } - - AesNI_BoxAlgorithm get_algorithm() const - { - return m_algorithm; - } - - std::deque get_args() - { - return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; - } - - private: - const std::string m_program_name; - boost::program_options::options_description m_options; - - AesNI_BoxMode m_mode; - AesNI_BoxAlgorithm m_algorithm; - std::vector m_args; - }; -} diff --git a/test/decrypt_block_aes.cpp b/test/decrypt_block_aes.cpp deleted file mode 100644 index 6c5e6f0..0000000 --- a/test/decrypt_block_aes.cpp +++ /dev/null @@ -1,122 +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_aes.hpp" - -#include - -#include - -#include -#include - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("encrypt_block_aes.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - while (!args.empty()) - { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (cmd_parser.get_algorithm()) - { - case AESNI_AES128: - aesni::aes::from_string(algorithm_params.aes128_key, args.front()); - break; - - case AESNI_AES192: - aesni::aes::from_string(algorithm_params.aes192_key, args.front()); - break; - - case AESNI_AES256: - aesni::aes::from_string(algorithm_params.aes256_key, args.front()); - break; - } - - args.pop_front(); - - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - switch (cmd_parser.get_mode()) - { - case AESNI_ECB: - break; - - case AESNI_CBC: - case AESNI_CFB: - case AESNI_OFB: - case AESNI_CTR: - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - aesni::aes::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - break; - } - - AesNI_Box box; - aesni_box_init( - &box, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_mode(), - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - AesNI_BoxBlock ciphertext; - aesni::aes::from_string(ciphertext.aes_block, args.front()); - args.pop_front(); - - AesNI_BoxBlock plaintext; - aesni_box_decrypt_block( - &box, - &ciphertext, - &plaintext, - aesni::ErrorDetailsThrowsInDestructor()); - - std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n"; - } - } - - return 0; - } - catch (const boost::program_options::error& e) - { - std::cerr << "Usage error: " << e.what() << "\n"; - return 1; - } - 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/test/encrypt_block_aes.cpp b/test/encrypt_block_aes.cpp deleted file mode 100644 index d3d3705..0000000 --- a/test/encrypt_block_aes.cpp +++ /dev/null @@ -1,122 +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_aes.hpp" - -#include - -#include - -#include -#include - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("encrypt_block_aes.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - while (!args.empty()) - { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (cmd_parser.get_algorithm()) - { - case AESNI_AES128: - aesni::aes::from_string(algorithm_params.aes128_key, args.front()); - break; - - case AESNI_AES192: - aesni::aes::from_string(algorithm_params.aes192_key, args.front()); - break; - - case AESNI_AES256: - aesni::aes::from_string(algorithm_params.aes256_key, args.front()); - break; - } - - args.pop_front(); - - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - switch (cmd_parser.get_mode()) - { - case AESNI_ECB: - break; - - case AESNI_CBC: - case AESNI_CFB: - case AESNI_OFB: - case AESNI_CTR: - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - aesni::aes::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - break; - } - - AesNI_Box box; - aesni_box_init( - &box, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_mode(), - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - AesNI_BoxBlock plaintext; - aesni::aes::from_string(plaintext.aes_block, args.front()); - args.pop_front(); - - AesNI_BoxBlock ciphertext; - aesni_box_encrypt_block( - &box, - &plaintext, - &ciphertext, - aesni::ErrorDetailsThrowsInDestructor()); - - std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n"; - } - } - - return 0; - } - catch (const boost::program_options::error& e) - { - std::cerr << "Usage error: " << e.what() << "\n"; - return 1; - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } -} -- cgit v1.2.3