diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-25 05:10:40 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-25 05:10:40 +0300 |
commit | 8ebebf9f27070bccd4df8949aadf4ca2860bf4b1 (patch) | |
tree | 9c1c9519dcbaf6cbaabeef3d6785b2d1b7f153ca /utils/block/cxx | |
parent | cxx: import more stuff (diff) | |
download | aes-tools-8ebebf9f27070bccd4df8949aadf4ca2860bf4b1.tar.gz aes-tools-8ebebf9f27070bccd4df8949aadf4ca2860bf4b1.zip |
merge specialized block utils into general ones
... taking advantage of the new template interface.
Diffstat (limited to 'utils/block/cxx')
-rw-r--r-- | utils/block/cxx/CMakeLists.txt | 11 | ||||
-rw-r--r-- | utils/block/cxx/README.md | 60 | ||||
-rw-r--r-- | utils/block/cxx/aes_common.hpp | 134 | ||||
-rw-r--r-- | utils/block/cxx/aes_decrypt_block.cpp | 122 | ||||
-rw-r--r-- | utils/block/cxx/aes_encrypt_block.cpp | 122 |
5 files changed, 0 insertions, 449 deletions
diff --git a/utils/block/cxx/CMakeLists.txt b/utils/block/cxx/CMakeLists.txt deleted file mode 100644 index 8df91d0..0000000 --- a/utils/block/cxx/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -find_package(Boost REQUIRED COMPONENTS program_options) - -add_executable(util_aes_encrypt_block aes_encrypt_block.cpp aes_common.hpp) -target_include_directories(util_aes_encrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_encrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_encrypt_block PROPERTIES OUTPUT_NAME aes_encrypt_block) - -add_executable(util_aes_decrypt_block aes_decrypt_block.cpp aes_common.hpp) -target_include_directories(util_aes_decrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_decrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_decrypt_block PROPERTIES OUTPUT_NAME aes_decrypt_block) diff --git a/utils/block/cxx/README.md b/utils/block/cxx/README.md deleted file mode 100644 index bce2434..0000000 --- a/utils/block/cxx/README.md +++ /dev/null @@ -1,60 +0,0 @@ -# Block encryption utilities - -Here are a couple of useful block encryption utilities built on top of the library. -Each of the utilities accepts `--help` flag, which can be used to examine utility's usage info. - -On older CPUs, you can run the utilities [using Intel SDE](https://github.com/egor-tensin/aesni#running-on-older-cpus). - -## aes_encrypt_block.exe - -Encrypts 16-byte blocks using AES-128/192/256 in the specified mode of operation. - -### Usage examples - -For example, to encrypt - -* the plaintext block `0x00112233445566778899aabbccddeeff` -* using AES-128 in ECB mode -* with key `0x000102030405060708090a0b0c0d0e0f`, - -run: - - aes_encrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccddeeff - -To encrypt - -* the plaintext block `0x00112233445566778899aabbccddeeff` -* using AES-192 in OFB mode -* with initialization vector `0x22222222222222222222222222222222` -* and key `0x000102030405060708090a0b0c0d0e0f101112131415161718`, - -run: - - aes_encrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 00112233445566778899aabbccddeeff - -## aes_decrypt_block.exe - -Decrypts 16-byte blocks using AES-128/192/256 in the specified mode of operation. - -### Usage examples - -For example, to decrypt - -* the ciphertext block `0x69c4e0d86a7b0430d8cdb78070b4c55a` -* using AES-128 in ECB mode -* with key `0x000102030405060708090a0b0c0d0e0f`, - -run: - - aes_decrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 69c4e0d86a7b0430d8cdb78070b4c55a - -To decrypt - -* the ciphertext block `0x762a5ab50929189cefdb99434790aad8` -* using AES-192 in OFB mode -* with initialization vector `0x22222222222222222222222222222222` -* and key `0x000102030405060708090a0b0c0d0e0f101112131415161718`, - -run: - - aes_decrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 bda298884f5c3a9eb7068aa7063a3b75 diff --git a/utils/block/cxx/aes_common.hpp b/utils/block/cxx/aes_common.hpp deleted file mode 100644 index cd5d669..0000000 --- a/utils/block/cxx/aes_common.hpp +++ /dev/null @@ -1,134 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -#include <aesnixx/all.hpp> - -#include <boost/algorithm/string.hpp> -#include <boost/program_options.hpp> - -#include <cstdlib> - -#include <deque> -#include <iostream> -#include <iterator> -#include <istream> -#include <string> -#include <vector> - -static std::istream& operator>>(std::istream& is, aesni::Mode& 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::Algorithm& 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<aesni::Mode>(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value<aesni::Algorithm>(&m_algorithm)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value<std::vector<std::string>>(&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::Mode get_mode() const - { - return m_mode; - } - - aesni::Algorithm get_algorithm() const - { - return m_algorithm; - } - - std::deque<std::string> 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::Mode m_mode; - aesni::Algorithm m_algorithm; - std::vector<std::string> m_args; - }; -} diff --git a/utils/block/cxx/aes_decrypt_block.cpp b/utils/block/cxx/aes_decrypt_block.cpp deleted file mode 100644 index 835d945..0000000 --- a/utils/block/cxx/aes_decrypt_block.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <exception> -#include <iostream> - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_block.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/utils/block/cxx/aes_encrypt_block.cpp b/utils/block/cxx/aes_encrypt_block.cpp deleted file mode 100644 index cd92fa3..0000000 --- a/utils/block/cxx/aes_encrypt_block.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <exception> -#include <iostream> - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_block.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; - } -} |