From 3d992e76ef7991cdff413ae58c07066f1a19a5d3 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 3 Aug 2015 14:23:27 +0300 Subject: rename the utilities --- utils/CMakeLists.txt | 48 ++++---- utils/README.md | 32 +++--- utils/aes_block_common.hpp | 242 --------------------------------------- utils/aes_decrypt_block.cpp | 271 -------------------------------------------- utils/aes_decrypt_bmp.cpp | 205 --------------------------------- utils/aes_decrypt_file.cpp | 195 ------------------------------- utils/aes_encrypt_block.cpp | 270 ------------------------------------------- utils/aes_encrypt_bmp.cpp | 204 --------------------------------- utils/aes_encrypt_file.cpp | 195 ------------------------------- utils/aes_file_common.hpp | 132 --------------------- utils/block_common.hpp | 242 +++++++++++++++++++++++++++++++++++++++ utils/decrypt_block.cpp | 271 ++++++++++++++++++++++++++++++++++++++++++++ utils/decrypt_bmp.cpp | 205 +++++++++++++++++++++++++++++++++ utils/decrypt_file.cpp | 195 +++++++++++++++++++++++++++++++ utils/encrypt_block.cpp | 270 +++++++++++++++++++++++++++++++++++++++++++ utils/encrypt_bmp.cpp | 204 +++++++++++++++++++++++++++++++++ utils/encrypt_file.cpp | 195 +++++++++++++++++++++++++++++++ utils/file_common.hpp | 132 +++++++++++++++++++++ 18 files changed, 1754 insertions(+), 1754 deletions(-) delete mode 100644 utils/aes_block_common.hpp delete mode 100644 utils/aes_decrypt_block.cpp delete mode 100644 utils/aes_decrypt_bmp.cpp delete mode 100644 utils/aes_decrypt_file.cpp delete mode 100644 utils/aes_encrypt_block.cpp delete mode 100644 utils/aes_encrypt_bmp.cpp delete mode 100644 utils/aes_encrypt_file.cpp delete mode 100644 utils/aes_file_common.hpp create mode 100644 utils/block_common.hpp create mode 100644 utils/decrypt_block.cpp create mode 100644 utils/decrypt_bmp.cpp create mode 100644 utils/decrypt_file.cpp create mode 100644 utils/encrypt_block.cpp create mode 100644 utils/encrypt_bmp.cpp create mode 100644 utils/encrypt_file.cpp create mode 100644 utils/file_common.hpp (limited to 'utils') diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index e4a768a..643dc31 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,31 +1,31 @@ find_package(Boost REQUIRED COMPONENTS program_options) -add_executable(util_aes_encrypt_block aes_encrypt_block.cpp aes_block_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_encrypt_block encrypt_block.cpp block_common.hpp) +target_include_directories(util_encrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_encrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_encrypt_block PROPERTIES OUTPUT_NAME encrypt_block) -add_executable(util_aes_decrypt_block aes_decrypt_block.cpp aes_block_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) +add_executable(util_decrypt_block decrypt_block.cpp block_common.hpp) +target_include_directories(util_decrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_decrypt_block libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_decrypt_block PROPERTIES OUTPUT_NAME decrypt_block) -add_executable(util_aes_encrypt_file aes_encrypt_file.cpp aes_file_common.hpp) -target_include_directories(util_aes_encrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_encrypt_file libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_encrypt_file PROPERTIES OUTPUT_NAME aes_encrypt_file) +add_executable(util_encrypt_file encrypt_file.cpp file_common.hpp) +target_include_directories(util_encrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_encrypt_file libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_encrypt_file PROPERTIES OUTPUT_NAME encrypt_file) -add_executable(util_aes_decrypt_file aes_decrypt_file.cpp aes_file_common.hpp) -target_include_directories(util_aes_decrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_decrypt_file libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_decrypt_file PROPERTIES OUTPUT_NAME aes_decrypt_file) +add_executable(util_decrypt_file decrypt_file.cpp file_common.hpp) +target_include_directories(util_decrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_decrypt_file libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_decrypt_file PROPERTIES OUTPUT_NAME decrypt_file) -add_executable(util_aes_encrypt_bmp aes_encrypt_bmp.cpp aes_file_common.hpp) -target_include_directories(util_aes_encrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_encrypt_bmp libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_encrypt_bmp PROPERTIES OUTPUT_NAME aes_encrypt_bmp) +add_executable(util_encrypt_bmp encrypt_bmp.cpp file_common.hpp) +target_include_directories(util_encrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_encrypt_bmp libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_encrypt_bmp PROPERTIES OUTPUT_NAME encrypt_bmp) -add_executable(util_aes_decrypt_bmp aes_decrypt_bmp.cpp aes_file_common.hpp) -target_include_directories(util_aes_decrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_aes_decrypt_bmp libaesni libaesnixx ${Boost_LIBRARIES}) -set_target_properties(util_aes_decrypt_bmp PROPERTIES OUTPUT_NAME aes_decrypt_bmp) +add_executable(util_decrypt_bmp decrypt_bmp.cpp file_common.hpp) +target_include_directories(util_decrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(util_decrypt_bmp libaesni libaesnixx ${Boost_LIBRARIES}) +set_target_properties(util_decrypt_bmp PROPERTIES OUTPUT_NAME decrypt_bmp) diff --git a/utils/README.md b/utils/README.md index 8403ee0..988a04a 100644 --- a/utils/README.md +++ b/utils/README.md @@ -22,7 +22,7 @@ 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 +### encrypt_block.exe Encrypts 16-byte blocks using AES-128/192/256 in the specified mode of operation. @@ -37,7 +37,7 @@ For example, to encrypt run: - aes_encrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccddeeff + encrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 00112233445566778899aabbccddeeff To encrypt @@ -48,9 +48,9 @@ To encrypt run: - aes_encrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 00112233445566778899aabbccddeeff + encrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 00112233445566778899aabbccddeeff -### aes_decrypt_block.exe +### decrypt_block.exe Decrypts 16-byte blocks using AES-128/192/256 in the specified mode of operation. @@ -65,7 +65,7 @@ For example, to decrypt run: - aes_decrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 69c4e0d86a7b0430d8cdb78070b4c55a + decrypt_block.exe -a aes128 -m ecb 000102030405060708090a0b0c0d0e0f 69c4e0d86a7b0430d8cdb78070b4c55a To decrypt @@ -76,11 +76,11 @@ To decrypt run: - aes_decrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 bda298884f5c3a9eb7068aa7063a3b75 + decrypt_block.exe -a aes192 -m ofb 000102030405060708090a0b0c0d0e0f101112131415161718 22222222222222222222222222222222 bda298884f5c3a9eb7068aa7063a3b75 ## File encryption -### aes_encrypt_file.exe +### encrypt_file.exe Encrypts a file using AES-128/192/256 in the specified mode of operation. @@ -94,7 +94,7 @@ For example, to encrypt the plaintext `input.txt` run: - aes_encrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt + encrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt To encrypt the plaintext from `input.txt` @@ -105,9 +105,9 @@ To encrypt the plaintext from `input.txt` run - aes_encrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt + encrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt -### aes_decrypt_file.exe +### decrypt_file.exe Decrypts a file using AES-128/192/256 in the specified mode of operation. @@ -121,7 +121,7 @@ To decrypt the ciphertext from `input.txt` run - aes_decrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt + decrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt To decrypt the ciphertext from `input.txt` @@ -132,7 +132,7 @@ To decrypt the ciphertext from `input.txt` run - aes_decrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt + decrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt ## Bitmap encryption @@ -145,12 +145,12 @@ Plaintext BMP | Encrypted in ECB mode | Encrypted in CBC mode ------------- | --------------------- | --------------------- ![Plaintext butterfly](bmp/butterfly.bmp?raw=true) | ![Ciphertext butterfly in ECB mode](bmp/cipherfly_ecb.bmp?raw=true) | ![Ciphertext butterfly in CBC mode](bmp/cipherfly_cbc.bmp?raw=true) -### aes_encrypt_bmp.exe +### encrypt_bmp.exe Encrypts the pixels in a BMP image file, preserving the header. -The usage is the same as for [aes_encrypt_file.exe](#aes_encrypt_fileexe). +The usage is the same as for [encrypt_file.exe](#encrypt_fileexe). -### aes_decrypt_bmp.exe +### decrypt_bmp.exe Decrypts the pixels in a BMP image file, preserving the header. -The usage is the same as for [aes_decrypt_file.exe](#aes_decrypt_fileexe). +The usage is the same as for [decrypt_file.exe](#decrypt_fileexe). diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp deleted file mode 100644 index 52b9ce3..0000000 --- a/utils/aes_block_common.hpp +++ /dev/null @@ -1,242 +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::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& prog_name) - : prog_name(prog_name) - , options("Options") - , boxes_flag(false) - , verbose_flag(false) - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - options.add_options() - ("help,h", "show this message and exit") - ("box,b", po::bool_switch(&boxes_flag)->default_value(false), "use the \"boxes\" interface") - ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm") - ("verbose,v", po::bool_switch(&verbose_flag)->default_value(false), "enable verbose output"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&args)); - - po::options_description all_options; - all_options.add(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: " << prog_name << " [OPTIONS...] [-- KEY [IV] [BLOCK...]...]\n"; - std::cout << options << "\n"; - } - - aesni::Mode get_mode() const - { - return encryption_mode; - } - - aesni::Algorithm get_algorithm() const - { - return encryption_algo; - } - - bool use_boxes() const - { - return boxes_flag; - } - - std::deque get_args() - { - return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; - } - - bool verbose() const - { - return verbose_flag; - } - - private: - const std::string prog_name; - boost::program_options::options_description options; - - aesni::Mode encryption_mode; - aesni::Algorithm encryption_algo; - bool boxes_flag; - std::vector args; - bool verbose_flag; - }; -} - -namespace -{ - template - void dump_block(const char* name, const typename aesni::Types::Block& block) - { - std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; - } - - template - void dump_plaintext(const typename aesni::Types::Block& block) - { - dump_block("Plaintext", block); - } - - template - void dump_key(const typename aesni::Types::Key& key) - { - std::cout << "Key: " << aesni::to_string(key) << "\n\n"; - } - - template - void dump_ciphertext(const typename aesni::Types::Block& ciphertext) - { - dump_block("Ciphertext", ciphertext); - } - - template - void dump_iv(const typename aesni::Types::Block& iv) - { - dump_block("Initialization vector", iv); - } - - 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::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 typename aesni::Types::RoundKeys& round_keys) - { - dump_round_keys("Encryption round keys", round_keys); - } - - template - void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) - { - dump_round_keys("Decryption round keys", round_keys); - } - - template - void dump_wrapper( - const aesni::EncryptWrapper& wrapper) - { - dump_encryption_keys(wrapper.encryption_keys); - } - - template - void dump_wrapper( - const aesni::DecryptWrapper& wrapper) - { - dump_decryption_keys(wrapper.decryption_keys); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::EncryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::EncryptWrapper&) - { } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::DecryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::DecryptWrapper&) - { } -} diff --git a/utils/aes_decrypt_block.cpp b/utils/aes_decrypt_block.cpp deleted file mode 100644 index 0f41b47..0000000 --- a/utils/aes_decrypt_block.cpp +++ /dev/null @@ -1,271 +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 "aes_block_common.hpp" - -#include - -#include - -#include -#include -#include -#include - -namespace -{ - template - bool decrypt_with_mode( - const std::string& key_str, - std::deque& ciphertexts, - bool verbose = false) - { - typename aesni::Types::Block iv; - - if (aesni::ModeRequiresInitializationVector()) - { - if (ciphertexts.empty()) - return false; - - aesni::from_string(iv, ciphertexts.front()); - ciphertexts.pop_front(); - - if (verbose) - dump_iv(iv); - } - - typename aesni::Types::Key key; - aesni::from_string(key, key_str); - - if (verbose) - dump_key(key); - - aesni::DecryptWrapper decrypt(key, iv); - - if (verbose) - dump_wrapper(decrypt); - - while (!ciphertexts.empty()) - { - typename aesni::Types::Block ciphertext, plaintext; - aesni::from_string(ciphertext, ciphertexts.front()); - ciphertexts.pop_front(); - - decrypt.decrypt_block(ciphertext, plaintext); - - if (verbose) - { - dump_ciphertext(ciphertext); - dump_plaintext(plaintext); - dump_next_iv(decrypt); - } - else - { - std::cout << aesni::to_string(plaintext) << "\n"; - } - } - - return true; - } - - template - bool decrypt_with_algorithm( - aesni::Mode mode, - const std::string& key_str, - std::deque& ciphertexts, - bool verbose = false) - { - switch (mode) - { - case AESNI_ECB: - return decrypt_with_mode(key_str, ciphertexts, verbose); - - case AESNI_CBC: - return decrypt_with_mode(key_str, ciphertexts, verbose); - - case AESNI_CFB: - return decrypt_with_mode(key_str, ciphertexts, verbose); - - case AESNI_OFB: - return decrypt_with_mode(key_str, ciphertexts, verbose); - - case AESNI_CTR: - return decrypt_with_mode(key_str, ciphertexts, verbose); - - default: - return false; - } - } - - bool decrypt_using_cxx_api( - aesni::Algorithm algorithm, - aesni::Mode mode, - const std::string& key_str, - std::deque ciphertexts, - bool verbose = false) - { - switch (algorithm) - { - case AESNI_AES128: - return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); - - case AESNI_AES192: - return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); - - case AESNI_AES256: - return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); - - default: - return false; - } - } - - template - bool decrypt_using_boxes_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - const std::string& key, - std::deque ciphertexts) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (ciphertexts.empty()) - return false; - - aesni::from_string(iv.aes_block, ciphertexts.front()); - iv_ptr = &iv; - ciphertexts.pop_front(); - } - - AesNI_Box box; - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - while (!ciphertexts.empty()) - { - AesNI_BoxBlock ciphertext; - aesni::from_string(ciphertext.aes_block, ciphertexts.front()); - ciphertexts.pop_front(); - - AesNI_BoxBlock plaintext; - aesni_box_decrypt_block( - &box, - &ciphertext, - &plaintext, - aesni::ErrorDetailsThrowsInDestructor()); - - std::cout << aesni::to_string(plaintext.aes_block) << "\n"; - } - - return true; - } - - bool decrypt_using_boxes( - aesni::Algorithm algorithm, - aesni::Mode mode, - const std::string& key, - std::deque ciphertexts) - { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, key); - return decrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, ciphertexts); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, key); - return decrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, ciphertexts); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, key); - return decrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, ciphertexts); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_decrypt_block.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - const auto algorithm = cmd_parser.get_algorithm(); - const auto mode = cmd_parser.get_mode(); - - auto args = cmd_parser.get_args(); - - while (!args.empty()) - { - const auto key = args.front(); - args.pop_front(); - - std::deque ciphertexts; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - ciphertexts.push_back(args.front()); - args.pop_front(); - } - - const auto success = cmd_parser.use_boxes() - ? decrypt_using_boxes(algorithm, mode, key, ciphertexts) - : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); - - if (!success) - { - cmd_parser.print_usage(); - return 1; - } - } - - 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/aes_decrypt_bmp.cpp b/utils/aes_decrypt_bmp.cpp deleted file mode 100644 index cf96847..0000000 --- a/utils/aes_decrypt_bmp.cpp +++ /dev/null @@ -1,205 +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 "aes_file_common.hpp" - -#include - -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace -{ - std::ifstream::pos_type get_file_size(const std::string& path) - { - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); - } - - std::vector read_file(const std::string& path) - { - const auto size = static_cast(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector& src) - { - std::ofstream ofs; - ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - ofs.open(path, std::ofstream::binary); - ofs.write(src.data(), src.size()); - } - - template - bool decrypt_bmp_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - std::deque& args) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (args.empty()) - return false; - - aesni::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - } - - if (args.size() != 2) - return false; - - const auto src_path = args[0]; - const auto dest_path = args[1]; - - const auto src_buf = read_file(src_path); - - const auto bmp_header = reinterpret_cast(src_buf.data()); - - const auto header_size = bmp_header->bfOffBits; - const auto cipherpixels = src_buf.data() + header_size; - const auto cipherpixels_size = src_buf.size() - header_size; - - AesNI_Box box; - - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - std::size_t pixels_size; - - aesni_box_decrypt_buffer( - &box, - cipherpixels, - cipherpixels_size, - nullptr, - &pixels_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(header_size + pixels_size); - std::memcpy(dest_buf.data(), src_buf.data(), header_size); - - aesni_box_decrypt_buffer( - &box, - cipherpixels, - cipherpixels_size, - dest_buf.data() + header_size, - &pixels_size, - aesni::ErrorDetailsThrowsInDestructor()); - - dest_buf.resize(header_size + pixels_size); - write_file(dest_path, dest_buf); - - return true; - } - - bool decrypt_bmp( - aesni::Algorithm algorithm, - aesni::Mode mode, - std::deque& args) - { - if (args.empty()) - return false; - - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, args.front()); - args.pop_front(); - return decrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, args.front()); - args.pop_front(); - return decrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, args.front()); - args.pop_front(); - return decrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_decrypt_bmp.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - if (!decrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) - { - cmd_parser.print_usage(); - return 1; - } - - 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/aes_decrypt_file.cpp b/utils/aes_decrypt_file.cpp deleted file mode 100644 index d456b67..0000000 --- a/utils/aes_decrypt_file.cpp +++ /dev/null @@ -1,195 +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 "aes_file_common.hpp" - -#include - -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace -{ - std::ifstream::pos_type get_file_size(const std::string& path) - { - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); - } - - std::vector read_file(const std::string& path) - { - const auto size = static_cast(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector& src) - { - std::ofstream ofs; - ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - ofs.open(path, std::ofstream::binary); - ofs.write(src.data(), src.size()); - } - - template - bool decrypt_file_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - std::deque& args) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (args.empty()) - return false; - - aesni::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - } - - if (args.size() != 2) - return false; - - const auto src_path = args[0]; - const auto dest_path = args[1]; - - const auto src_buf = read_file(src_path); - - AesNI_Box box; - - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - std::size_t dest_size; - - aesni_box_decrypt_buffer( - &box, - src_buf.data(), - src_buf.size(), - nullptr, - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aesni_box_decrypt_buffer( - &box, - src_buf.data(), - src_buf.size(), - dest_buf.data(), - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - dest_buf.resize(dest_size); - write_file(dest_path, dest_buf); - - return true; - } - - bool decrypt_file( - aesni::Algorithm algorithm, - aesni::Mode mode, - std::deque& args) - { - if (args.empty()) - return false; - - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, args.front()); - args.pop_front(); - return decrypt_file_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, args.front()); - args.pop_front(); - return decrypt_file_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, args.front()); - args.pop_front(); - return decrypt_file_with_algorithm( - algorithm_params, mode, args); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_file.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - if (!decrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) - { - cmd_parser.print_usage(); - return 1; - } - - 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/aes_encrypt_block.cpp b/utils/aes_encrypt_block.cpp deleted file mode 100644 index 1b6b788..0000000 --- a/utils/aes_encrypt_block.cpp +++ /dev/null @@ -1,270 +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 "aes_block_common.hpp" - -#include - -#include - -#include -#include -#include -#include - -namespace -{ - template - bool encrypt_with_mode( - const std::string& key_str, - std::deque& plaintexts, - bool verbose = false) - { - typename aesni::Types::Block iv; - - if (aesni::ModeRequiresInitializationVector::value) - { - if (plaintexts.empty()) - return false; - - aesni::from_string(iv, plaintexts.front()); - plaintexts.pop_front(); - - if (verbose) - dump_iv(iv); - } - - typename aesni::Types::Key key; - aesni::from_string(key, key_str); - - if (verbose) - dump_key(key); - - aesni::EncryptWrapper encrypt(key, iv); - - if (verbose) - dump_wrapper(encrypt); - - while (!plaintexts.empty()) - { - typename aesni::Types::Block plaintext, ciphertext; - aesni::from_string(plaintext, plaintexts.front()); - plaintexts.pop_front(); - encrypt.encrypt_block(plaintext, ciphertext); - - if (verbose) - { - dump_plaintext(plaintext); - dump_ciphertext(ciphertext); - dump_next_iv(encrypt); - } - else - { - std::cout << aesni::to_string(ciphertext) << "\n"; - } - } - - return true; - } - - template - bool encrypt_with_algorithm( - aesni::Mode mode, - const std::string& key_str, - std::deque& plaintexts, - bool verbose = false) - { - switch (mode) - { - case AESNI_ECB: - return encrypt_with_mode(key_str, plaintexts, verbose); - - case AESNI_CBC: - return encrypt_with_mode(key_str, plaintexts, verbose); - - case AESNI_CFB: - return encrypt_with_mode(key_str, plaintexts, verbose); - - case AESNI_OFB: - return encrypt_with_mode(key_str, plaintexts, verbose); - - case AESNI_CTR: - return encrypt_with_mode(key_str, plaintexts, verbose); - - default: - return false; - } - } - - bool encrypt_using_cxx_api( - aesni::Algorithm algorithm, - aesni::Mode mode, - const std::string& key_str, - std::deque& plaintexts, - bool verbose = false) - { - switch (algorithm) - { - case AESNI_AES128: - return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); - - case AESNI_AES192: - return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); - - case AESNI_AES256: - return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); - - default: - return false; - } - } - - template - bool encrypt_using_boxes_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - const std::string& key, - std::deque plaintexts) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (plaintexts.empty()) - return false; - - aesni::from_string(iv.aes_block, plaintexts.front()); - iv_ptr = &iv; - plaintexts.pop_front(); - } - - AesNI_Box box; - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - while (!plaintexts.empty()) - { - AesNI_BoxBlock plaintext; - aesni::from_string(plaintext.aes_block, plaintexts.front()); - plaintexts.pop_front(); - - AesNI_BoxBlock ciphertext; - aesni_box_encrypt_block( - &box, - &plaintext, - &ciphertext, - aesni::ErrorDetailsThrowsInDestructor()); - - std::cout << aesni::to_string(ciphertext.aes_block) << "\n"; - } - - return true; - } - - bool encrypt_using_boxes( - aesni::Algorithm algorithm, - aesni::Mode mode, - const std::string& key, - std::deque plaintexts) - { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, key); - return encrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, plaintexts); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, key); - return encrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, plaintexts); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, key); - return encrypt_using_boxes_with_algorithm( - algorithm_params, mode, key, plaintexts); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_block.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - const auto algorithm = cmd_parser.get_algorithm(); - const auto mode = cmd_parser.get_mode(); - - auto args = cmd_parser.get_args(); - - while (!args.empty()) - { - const auto key = args.front(); - args.pop_front(); - - std::deque plaintexts; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - plaintexts.push_back(args.front()); - args.pop_front(); - } - - const auto success = cmd_parser.use_boxes() - ? encrypt_using_boxes(algorithm, mode, key, plaintexts) - : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose()); - - if (!success) - { - cmd_parser.print_usage(); - return 1; - } - } - - 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/aes_encrypt_bmp.cpp b/utils/aes_encrypt_bmp.cpp deleted file mode 100644 index 2615889..0000000 --- a/utils/aes_encrypt_bmp.cpp +++ /dev/null @@ -1,204 +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 "aes_file_common.hpp" - -#include - -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace -{ - std::ifstream::pos_type get_file_size(const std::string& path) - { - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); - } - - std::vector read_file(const std::string& path) - { - const auto size = static_cast(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector& src) - { - std::ofstream ofs; - ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - ofs.open(path, std::ofstream::binary); - ofs.write(src.data(), src.size()); - } - - template - bool encrypt_bmp_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - std::deque& args) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (args.empty()) - return false; - - aesni::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - } - - if (args.size() != 2) - return false; - - const auto src_path = args[0]; - const auto dest_path = args[1]; - - const auto src_buf = read_file(src_path); - - const auto bmp_header = reinterpret_cast(src_buf.data()); - - const auto header_size = bmp_header->bfOffBits; - const auto pixels = src_buf.data() + header_size; - const auto pixels_size = src_buf.size() - header_size; - - AesNI_Box box; - - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - std::size_t cipherpixels_size; - - aesni_box_encrypt_buffer( - &box, - pixels, - pixels_size, - nullptr, - &cipherpixels_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(header_size + cipherpixels_size); - std::memcpy(dest_buf.data(), src_buf.data(), header_size); - - aesni_box_encrypt_buffer( - &box, - pixels, - pixels_size, - dest_buf.data() + header_size, - &cipherpixels_size, - aesni::ErrorDetailsThrowsInDestructor()); - - write_file(dest_path, dest_buf); - - return true; - } - - bool encrypt_bmp( - aesni::Algorithm algorithm, - aesni::Mode mode, - std::deque& args) - { - if (args.empty()) - return false; - - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, args.front()); - args.pop_front(); - return encrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, args.front()); - args.pop_front(); - return encrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, args.front()); - args.pop_front(); - return encrypt_bmp_with_algorithm( - algorithm_params, mode, args); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_bmp.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - if (!encrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) - { - cmd_parser.print_usage(); - return 1; - } - - 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/aes_encrypt_file.cpp b/utils/aes_encrypt_file.cpp deleted file mode 100644 index 61b9ba1..0000000 --- a/utils/aes_encrypt_file.cpp +++ /dev/null @@ -1,195 +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 "aes_file_common.hpp" - -#include - -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace -{ - std::ifstream::pos_type get_file_size(const std::string& path) - { - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); - } - - std::vector read_file(const std::string& path) - { - const auto size = static_cast(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector& src) - { - std::ofstream ofs; - ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - ofs.open(path, std::ofstream::binary); - ofs.write(src.data(), src.size()); - } - - template - bool encrypt_file_with_algorithm( - const AesNI_BoxAlgorithmParams& algorithm_params, - aesni::Mode mode, - std::deque& args) - { - AesNI_BoxBlock iv; - AesNI_BoxBlock* iv_ptr = nullptr; - - if (aesni::mode_requires_initialization_vector(mode)) - { - if (args.empty()) - return false; - - aesni::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - } - - if (args.size() != 2) - return true; - - const auto src_path = args[0]; - const auto dest_path = args[1]; - - const auto src_buf = read_file(src_path); - - AesNI_Box box; - - aesni_box_init( - &box, - algorithm, - &algorithm_params, - mode, - iv_ptr, - aesni::ErrorDetailsThrowsInDestructor()); - - std::size_t dest_size; - - aesni_box_encrypt_buffer( - &box, - src_buf.data(), - src_buf.size(), - nullptr, - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aesni_box_encrypt_buffer( - &box, - src_buf.data(), - src_buf.size(), - dest_buf.data(), - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - dest_buf.resize(dest_size); - write_file(dest_path, dest_buf); - - return true; - } - - bool encrypt_file( - aesni::Algorithm algorithm, - aesni::Mode mode, - std::deque& args) - { - if (args.empty()) - return false; - - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::from_string( - algorithm_params.aes128_key, args.front()); - args.pop_front(); - return encrypt_file_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES192: - aesni::from_string( - algorithm_params.aes192_key, args.front()); - args.pop_front(); - return encrypt_file_with_algorithm( - algorithm_params, mode, args); - - case AESNI_AES256: - aesni::from_string( - algorithm_params.aes256_key, args.front()); - args.pop_front(); - return encrypt_file_with_algorithm( - algorithm_params, mode, args); - - default: - return false; - } - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_file.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - if (!encrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) - { - cmd_parser.print_usage(); - return 1; - } - - 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/aes_file_common.hpp b/utils/aes_file_common.hpp deleted file mode 100644 index 8a13b48..0000000 --- a/utils/aes_file_common.hpp +++ /dev/null @@ -1,132 +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 - -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& prog_name) - : prog_name(prog_name) - , options("Options") - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&args)); - - po::options_description all_options; - all_options.add(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: " << prog_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n"; - std::cout << options << "\n"; - } - - aesni::Mode get_mode() const - { - return encryption_mode; - } - - aesni::Algorithm get_algorithm() const - { - return encryption_algo; - } - - std::deque get_args() - { - return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; - } - - private: - const std::string prog_name; - boost::program_options::options_description options; - - aesni::Mode encryption_mode; - aesni::Algorithm encryption_algo; - std::vector args; - }; -} diff --git a/utils/block_common.hpp b/utils/block_common.hpp new file mode 100644 index 0000000..52b9ce3 --- /dev/null +++ b/utils/block_common.hpp @@ -0,0 +1,242 @@ +/** + * \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::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& prog_name) + : prog_name(prog_name) + , options("Options") + , boxes_flag(false) + , verbose_flag(false) + { } + + bool parse_options(int argc, char** argv) + { + namespace po = boost::program_options; + + options.add_options() + ("help,h", "show this message and exit") + ("box,b", po::bool_switch(&boxes_flag)->default_value(false), "use the \"boxes\" interface") + ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm") + ("verbose,v", po::bool_switch(&verbose_flag)->default_value(false), "enable verbose output"); + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&args)); + + po::options_description all_options; + all_options.add(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: " << prog_name << " [OPTIONS...] [-- KEY [IV] [BLOCK...]...]\n"; + std::cout << options << "\n"; + } + + aesni::Mode get_mode() const + { + return encryption_mode; + } + + aesni::Algorithm get_algorithm() const + { + return encryption_algo; + } + + bool use_boxes() const + { + return boxes_flag; + } + + std::deque get_args() + { + return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; + } + + bool verbose() const + { + return verbose_flag; + } + + private: + const std::string prog_name; + boost::program_options::options_description options; + + aesni::Mode encryption_mode; + aesni::Algorithm encryption_algo; + bool boxes_flag; + std::vector args; + bool verbose_flag; + }; +} + +namespace +{ + template + void dump_block(const char* name, const typename aesni::Types::Block& block) + { + std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; + } + + template + void dump_plaintext(const typename aesni::Types::Block& block) + { + dump_block("Plaintext", block); + } + + template + void dump_key(const typename aesni::Types::Key& key) + { + std::cout << "Key: " << aesni::to_string(key) << "\n\n"; + } + + template + void dump_ciphertext(const typename aesni::Types::Block& ciphertext) + { + dump_block("Ciphertext", ciphertext); + } + + template + void dump_iv(const typename aesni::Types::Block& iv) + { + dump_block("Initialization vector", iv); + } + + 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::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 typename aesni::Types::RoundKeys& round_keys) + { + dump_round_keys("Encryption round keys", round_keys); + } + + template + void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) + { + dump_round_keys("Decryption round keys", round_keys); + } + + template + void dump_wrapper( + const aesni::EncryptWrapper& wrapper) + { + dump_encryption_keys(wrapper.encryption_keys); + } + + template + void dump_wrapper( + const aesni::DecryptWrapper& wrapper) + { + dump_decryption_keys(wrapper.decryption_keys); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper& wrapper) + { + dump_block("Next initialization vector", wrapper.iv); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper&) + { } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper& wrapper) + { + dump_block("Next initialization vector", wrapper.iv); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper&) + { } +} diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp new file mode 100644 index 0000000..9784657 --- /dev/null +++ b/utils/decrypt_block.cpp @@ -0,0 +1,271 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "block_common.hpp" + +#include + +#include + +#include +#include +#include +#include + +namespace +{ + template + bool decrypt_with_mode( + const std::string& key_str, + std::deque& ciphertexts, + bool verbose = false) + { + typename aesni::Types::Block iv; + + if (aesni::ModeRequiresInitializationVector()) + { + if (ciphertexts.empty()) + return false; + + aesni::from_string(iv, ciphertexts.front()); + ciphertexts.pop_front(); + + if (verbose) + dump_iv(iv); + } + + typename aesni::Types::Key key; + aesni::from_string(key, key_str); + + if (verbose) + dump_key(key); + + aesni::DecryptWrapper decrypt(key, iv); + + if (verbose) + dump_wrapper(decrypt); + + while (!ciphertexts.empty()) + { + typename aesni::Types::Block ciphertext, plaintext; + aesni::from_string(ciphertext, ciphertexts.front()); + ciphertexts.pop_front(); + + decrypt.decrypt_block(ciphertext, plaintext); + + if (verbose) + { + dump_ciphertext(ciphertext); + dump_plaintext(plaintext); + dump_next_iv(decrypt); + } + else + { + std::cout << aesni::to_string(plaintext) << "\n"; + } + } + + return true; + } + + template + bool decrypt_with_algorithm( + aesni::Mode mode, + const std::string& key_str, + std::deque& ciphertexts, + bool verbose = false) + { + switch (mode) + { + case AESNI_ECB: + return decrypt_with_mode(key_str, ciphertexts, verbose); + + case AESNI_CBC: + return decrypt_with_mode(key_str, ciphertexts, verbose); + + case AESNI_CFB: + return decrypt_with_mode(key_str, ciphertexts, verbose); + + case AESNI_OFB: + return decrypt_with_mode(key_str, ciphertexts, verbose); + + case AESNI_CTR: + return decrypt_with_mode(key_str, ciphertexts, verbose); + + default: + return false; + } + } + + bool decrypt_using_cxx_api( + aesni::Algorithm algorithm, + aesni::Mode mode, + const std::string& key_str, + std::deque ciphertexts, + bool verbose = false) + { + switch (algorithm) + { + case AESNI_AES128: + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); + + case AESNI_AES192: + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); + + case AESNI_AES256: + return decrypt_with_algorithm(mode, key_str, ciphertexts, verbose); + + default: + return false; + } + } + + template + bool decrypt_using_boxes_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + const std::string& key, + std::deque ciphertexts) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (ciphertexts.empty()) + return false; + + aesni::from_string(iv.aes_block, ciphertexts.front()); + iv_ptr = &iv; + ciphertexts.pop_front(); + } + + AesNI_Box box; + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!ciphertexts.empty()) + { + AesNI_BoxBlock ciphertext; + aesni::from_string(ciphertext.aes_block, ciphertexts.front()); + ciphertexts.pop_front(); + + AesNI_BoxBlock plaintext; + aesni_box_decrypt_block( + &box, + &ciphertext, + &plaintext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::to_string(plaintext.aes_block) << "\n"; + } + + return true; + } + + bool decrypt_using_boxes( + aesni::Algorithm algorithm, + aesni::Mode mode, + const std::string& key, + std::deque ciphertexts) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("decrypt_block.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + const auto algorithm = cmd_parser.get_algorithm(); + const auto mode = cmd_parser.get_mode(); + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + const auto key = args.front(); + args.pop_front(); + + std::deque ciphertexts; + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + ciphertexts.push_back(args.front()); + args.pop_front(); + } + + const auto success = cmd_parser.use_boxes() + ? decrypt_using_boxes(algorithm, mode, key, ciphertexts) + : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); + + if (!success) + { + cmd_parser.print_usage(); + return 1; + } + } + + 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/decrypt_bmp.cpp b/utils/decrypt_bmp.cpp new file mode 100644 index 0000000..3b075a4 --- /dev/null +++ b/utils/decrypt_bmp.cpp @@ -0,0 +1,205 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "file_common.hpp" + +#include + +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace +{ + std::ifstream::pos_type get_file_size(const std::string& path) + { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return ifs.tellg(); + } + + std::vector read_file(const std::string& path) + { + const auto size = static_cast(get_file_size(path)); + + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(size); + src_buf.assign(std::istreambuf_iterator(ifs), + std::istreambuf_iterator()); + return src_buf; + } + + void write_file(const std::string& path, const std::vector& src) + { + std::ofstream ofs; + ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + ofs.open(path, std::ofstream::binary); + ofs.write(src.data(), src.size()); + } + + template + bool decrypt_bmp_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (args.empty()) + return false; + + aesni::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + } + + if (args.size() != 2) + return false; + + const auto src_path = args[0]; + const auto dest_path = args[1]; + + const auto src_buf = read_file(src_path); + + const auto bmp_header = reinterpret_cast(src_buf.data()); + + const auto header_size = bmp_header->bfOffBits; + const auto cipherpixels = src_buf.data() + header_size; + const auto cipherpixels_size = src_buf.size() - header_size; + + AesNI_Box box; + + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + std::size_t pixels_size; + + aesni_box_decrypt_buffer( + &box, + cipherpixels, + cipherpixels_size, + nullptr, + &pixels_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(header_size + pixels_size); + std::memcpy(dest_buf.data(), src_buf.data(), header_size); + + aesni_box_decrypt_buffer( + &box, + cipherpixels, + cipherpixels_size, + dest_buf.data() + header_size, + &pixels_size, + aesni::ErrorDetailsThrowsInDestructor()); + + dest_buf.resize(header_size + pixels_size); + write_file(dest_path, dest_buf); + + return true; + } + + bool decrypt_bmp( + aesni::Algorithm algorithm, + aesni::Mode mode, + std::deque& args) + { + if (args.empty()) + return false; + + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("decrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!decrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + { + cmd_parser.print_usage(); + return 1; + } + + 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/decrypt_file.cpp b/utils/decrypt_file.cpp new file mode 100644 index 0000000..3e08d6f --- /dev/null +++ b/utils/decrypt_file.cpp @@ -0,0 +1,195 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "file_common.hpp" + +#include + +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + std::ifstream::pos_type get_file_size(const std::string& path) + { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return ifs.tellg(); + } + + std::vector read_file(const std::string& path) + { + const auto size = static_cast(get_file_size(path)); + + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(size); + src_buf.assign(std::istreambuf_iterator(ifs), + std::istreambuf_iterator()); + return src_buf; + } + + void write_file(const std::string& path, const std::vector& src) + { + std::ofstream ofs; + ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + ofs.open(path, std::ofstream::binary); + ofs.write(src.data(), src.size()); + } + + template + bool decrypt_file_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (args.empty()) + return false; + + aesni::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + } + + if (args.size() != 2) + return false; + + const auto src_path = args[0]; + const auto dest_path = args[1]; + + const auto src_buf = read_file(src_path); + + AesNI_Box box; + + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + std::size_t dest_size; + + aesni_box_decrypt_buffer( + &box, + src_buf.data(), + src_buf.size(), + nullptr, + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aesni_box_decrypt_buffer( + &box, + src_buf.data(), + src_buf.size(), + dest_buf.data(), + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + dest_buf.resize(dest_size); + write_file(dest_path, dest_buf); + + return true; + } + + bool decrypt_file( + aesni::Algorithm algorithm, + aesni::Mode mode, + std::deque& args) + { + if (args.empty()) + return false; + + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, args.front()); + args.pop_front(); + return decrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return decrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return decrypt_file_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("decrypt_file.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!decrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + { + cmd_parser.print_usage(); + return 1; + } + + 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/encrypt_block.cpp b/utils/encrypt_block.cpp new file mode 100644 index 0000000..a40cc67 --- /dev/null +++ b/utils/encrypt_block.cpp @@ -0,0 +1,270 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "block_common.hpp" + +#include + +#include + +#include +#include +#include +#include + +namespace +{ + template + bool encrypt_with_mode( + const std::string& key_str, + std::deque& plaintexts, + bool verbose = false) + { + typename aesni::Types::Block iv; + + if (aesni::ModeRequiresInitializationVector::value) + { + if (plaintexts.empty()) + return false; + + aesni::from_string(iv, plaintexts.front()); + plaintexts.pop_front(); + + if (verbose) + dump_iv(iv); + } + + typename aesni::Types::Key key; + aesni::from_string(key, key_str); + + if (verbose) + dump_key(key); + + aesni::EncryptWrapper encrypt(key, iv); + + if (verbose) + dump_wrapper(encrypt); + + while (!plaintexts.empty()) + { + typename aesni::Types::Block plaintext, ciphertext; + aesni::from_string(plaintext, plaintexts.front()); + plaintexts.pop_front(); + encrypt.encrypt_block(plaintext, ciphertext); + + if (verbose) + { + dump_plaintext(plaintext); + dump_ciphertext(ciphertext); + dump_next_iv(encrypt); + } + else + { + std::cout << aesni::to_string(ciphertext) << "\n"; + } + } + + return true; + } + + template + bool encrypt_with_algorithm( + aesni::Mode mode, + const std::string& key_str, + std::deque& plaintexts, + bool verbose = false) + { + switch (mode) + { + case AESNI_ECB: + return encrypt_with_mode(key_str, plaintexts, verbose); + + case AESNI_CBC: + return encrypt_with_mode(key_str, plaintexts, verbose); + + case AESNI_CFB: + return encrypt_with_mode(key_str, plaintexts, verbose); + + case AESNI_OFB: + return encrypt_with_mode(key_str, plaintexts, verbose); + + case AESNI_CTR: + return encrypt_with_mode(key_str, plaintexts, verbose); + + default: + return false; + } + } + + bool encrypt_using_cxx_api( + aesni::Algorithm algorithm, + aesni::Mode mode, + const std::string& key_str, + std::deque& plaintexts, + bool verbose = false) + { + switch (algorithm) + { + case AESNI_AES128: + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); + + case AESNI_AES192: + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); + + case AESNI_AES256: + return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); + + default: + return false; + } + } + + template + bool encrypt_using_boxes_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + const std::string& key, + std::deque plaintexts) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (plaintexts.empty()) + return false; + + aesni::from_string(iv.aes_block, plaintexts.front()); + iv_ptr = &iv; + plaintexts.pop_front(); + } + + AesNI_Box box; + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!plaintexts.empty()) + { + AesNI_BoxBlock plaintext; + aesni::from_string(plaintext.aes_block, plaintexts.front()); + plaintexts.pop_front(); + + AesNI_BoxBlock ciphertext; + aesni_box_encrypt_block( + &box, + &plaintext, + &ciphertext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::to_string(ciphertext.aes_block) << "\n"; + } + + return true; + } + + bool encrypt_using_boxes( + aesni::Algorithm algorithm, + aesni::Mode mode, + const std::string& key, + std::deque plaintexts) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, key); + return encrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, plaintexts); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, key); + return encrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, plaintexts); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, key); + return encrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, plaintexts); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_block.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + const auto algorithm = cmd_parser.get_algorithm(); + const auto mode = cmd_parser.get_mode(); + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + const auto key = args.front(); + args.pop_front(); + + std::deque plaintexts; + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + plaintexts.push_back(args.front()); + args.pop_front(); + } + + const auto success = cmd_parser.use_boxes() + ? encrypt_using_boxes(algorithm, mode, key, plaintexts) + : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose()); + + if (!success) + { + cmd_parser.print_usage(); + return 1; + } + } + + 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/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp new file mode 100644 index 0000000..acdd893 --- /dev/null +++ b/utils/encrypt_bmp.cpp @@ -0,0 +1,204 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "file_common.hpp" + +#include + +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace +{ + std::ifstream::pos_type get_file_size(const std::string& path) + { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return ifs.tellg(); + } + + std::vector read_file(const std::string& path) + { + const auto size = static_cast(get_file_size(path)); + + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(size); + src_buf.assign(std::istreambuf_iterator(ifs), + std::istreambuf_iterator()); + return src_buf; + } + + void write_file(const std::string& path, const std::vector& src) + { + std::ofstream ofs; + ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + ofs.open(path, std::ofstream::binary); + ofs.write(src.data(), src.size()); + } + + template + bool encrypt_bmp_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (args.empty()) + return false; + + aesni::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + } + + if (args.size() != 2) + return false; + + const auto src_path = args[0]; + const auto dest_path = args[1]; + + const auto src_buf = read_file(src_path); + + const auto bmp_header = reinterpret_cast(src_buf.data()); + + const auto header_size = bmp_header->bfOffBits; + const auto pixels = src_buf.data() + header_size; + const auto pixels_size = src_buf.size() - header_size; + + AesNI_Box box; + + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + std::size_t cipherpixels_size; + + aesni_box_encrypt_buffer( + &box, + pixels, + pixels_size, + nullptr, + &cipherpixels_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(header_size + cipherpixels_size); + std::memcpy(dest_buf.data(), src_buf.data(), header_size); + + aesni_box_encrypt_buffer( + &box, + pixels, + pixels_size, + dest_buf.data() + header_size, + &cipherpixels_size, + aesni::ErrorDetailsThrowsInDestructor()); + + write_file(dest_path, dest_buf); + + return true; + } + + bool encrypt_bmp( + aesni::Algorithm algorithm, + aesni::Mode mode, + std::deque& args) + { + if (args.empty()) + return false; + + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, args.front()); + args.pop_front(); + return encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!encrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + { + cmd_parser.print_usage(); + return 1; + } + + 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/encrypt_file.cpp b/utils/encrypt_file.cpp new file mode 100644 index 0000000..5747536 --- /dev/null +++ b/utils/encrypt_file.cpp @@ -0,0 +1,195 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "file_common.hpp" + +#include + +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + std::ifstream::pos_type get_file_size(const std::string& path) + { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return ifs.tellg(); + } + + std::vector read_file(const std::string& path) + { + const auto size = static_cast(get_file_size(path)); + + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(size); + src_buf.assign(std::istreambuf_iterator(ifs), + std::istreambuf_iterator()); + return src_buf; + } + + void write_file(const std::string& path, const std::vector& src) + { + std::ofstream ofs; + ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + ofs.open(path, std::ofstream::binary); + ofs.write(src.data(), src.size()); + } + + template + bool encrypt_file_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) + { + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + if (aesni::mode_requires_initialization_vector(mode)) + { + if (args.empty()) + return false; + + aesni::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + } + + if (args.size() != 2) + return true; + + const auto src_path = args[0]; + const auto dest_path = args[1]; + + const auto src_buf = read_file(src_path); + + AesNI_Box box; + + aesni_box_init( + &box, + algorithm, + &algorithm_params, + mode, + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + std::size_t dest_size; + + aesni_box_encrypt_buffer( + &box, + src_buf.data(), + src_buf.size(), + nullptr, + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aesni_box_encrypt_buffer( + &box, + src_buf.data(), + src_buf.size(), + dest_buf.data(), + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + dest_buf.resize(dest_size); + write_file(dest_path, dest_buf); + + return true; + } + + bool encrypt_file( + aesni::Algorithm algorithm, + aesni::Mode mode, + std::deque& args) + { + if (args.empty()) + return false; + + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, args.front()); + args.pop_front(); + return encrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return encrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return encrypt_file_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_file.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!encrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + { + cmd_parser.print_usage(); + return 1; + } + + 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/file_common.hpp b/utils/file_common.hpp new file mode 100644 index 0000000..8a13b48 --- /dev/null +++ b/utils/file_common.hpp @@ -0,0 +1,132 @@ +/** + * \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 + +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& prog_name) + : prog_name(prog_name) + , options("Options") + { } + + bool parse_options(int argc, char** argv) + { + namespace po = boost::program_options; + + options.add_options() + ("help,h", "show this message and exit") + ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm"); + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&args)); + + po::options_description all_options; + all_options.add(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: " << prog_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n"; + std::cout << options << "\n"; + } + + aesni::Mode get_mode() const + { + return encryption_mode; + } + + aesni::Algorithm get_algorithm() const + { + return encryption_algo; + } + + std::deque get_args() + { + return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; + } + + private: + const std::string prog_name; + boost::program_options::options_description options; + + aesni::Mode encryption_mode; + aesni::Algorithm encryption_algo; + std::vector args; + }; +} -- cgit v1.2.3