From 225ef13cc7f58b69ef4c4db6308a225c726359f7 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 24 Jun 2015 05:59:11 +0300 Subject: move executables from test/ to utils/ --- utils/CMakeLists.txt | 32 +----- utils/README.md | 87 ---------------- utils/aes128ecb_decrypt_file.cpp | 104 ------------------ utils/aes128ecb_encrypt_file.cpp | 103 ------------------ utils/aes_common.hpp | 132 ----------------------- utils/aes_decrypt_bmp.cpp | 191 ---------------------------------- utils/aes_decrypt_file.cpp | 182 -------------------------------- utils/aes_encrypt_bmp.cpp | 190 --------------------------------- utils/aes_encrypt_file.cpp | 182 -------------------------------- utils/block/CMakeLists.txt | 27 +++++ utils/block/aes128cbc_decrypt_block.c | 76 ++++++++++++++ utils/block/aes128cbc_encrypt_block.c | 75 +++++++++++++ utils/block/aes128cfb_decrypt_block.c | 75 +++++++++++++ utils/block/aes128cfb_encrypt_block.c | 75 +++++++++++++ utils/block/aes128ctr_decrypt_block.c | 75 +++++++++++++ utils/block/aes128ctr_encrypt_block.c | 75 +++++++++++++ utils/block/aes128ecb_decrypt_block.c | 70 +++++++++++++ utils/block/aes128ecb_encrypt_block.c | 69 ++++++++++++ utils/block/aes128ofb_decrypt_block.c | 75 +++++++++++++ utils/block/aes128ofb_encrypt_block.c | 75 +++++++++++++ utils/block/aes192cbc_decrypt_block.c | 76 ++++++++++++++ utils/block/aes192cbc_encrypt_block.c | 75 +++++++++++++ utils/block/aes192cfb_decrypt_block.c | 75 +++++++++++++ utils/block/aes192cfb_encrypt_block.c | 75 +++++++++++++ utils/block/aes192ctr_decrypt_block.c | 75 +++++++++++++ utils/block/aes192ctr_encrypt_block.c | 75 +++++++++++++ utils/block/aes192ecb_decrypt_block.c | 70 +++++++++++++ utils/block/aes192ecb_encrypt_block.c | 69 ++++++++++++ utils/block/aes192ofb_decrypt_block.c | 75 +++++++++++++ utils/block/aes192ofb_encrypt_block.c | 75 +++++++++++++ utils/block/aes256cbc_decrypt_block.c | 76 ++++++++++++++ utils/block/aes256cbc_encrypt_block.c | 75 +++++++++++++ utils/block/aes256cfb_decrypt_block.c | 75 +++++++++++++ utils/block/aes256cfb_encrypt_block.c | 75 +++++++++++++ utils/block/aes256ctr_decrypt_block.c | 75 +++++++++++++ utils/block/aes256ctr_encrypt_block.c | 75 +++++++++++++ utils/block/aes256ecb_decrypt_block.c | 70 +++++++++++++ utils/block/aes256ecb_encrypt_block.c | 69 ++++++++++++ utils/block/aes256ofb_decrypt_block.c | 75 +++++++++++++ utils/block/aes256ofb_encrypt_block.c | 75 +++++++++++++ utils/block/cxx/CMakeLists.txt | 11 ++ utils/block/cxx/README.md | 60 +++++++++++ utils/block/cxx/aes_common.hpp | 134 ++++++++++++++++++++++++ utils/block/cxx/aes_decrypt_block.cpp | 122 ++++++++++++++++++++++ utils/block/cxx/aes_encrypt_block.cpp | 122 ++++++++++++++++++++++ utils/bmp/butterfly.bmp | Bin 503370 -> 0 bytes utils/bmp/cipherfly_cbc.bmp | Bin 503382 -> 0 bytes utils/bmp/cipherfly_ecb.bmp | Bin 503382 -> 0 bytes utils/file/CMakeLists.txt | 10 ++ utils/file/aes128ecb_decrypt_file.cpp | 104 ++++++++++++++++++ utils/file/aes128ecb_encrypt_file.cpp | 103 ++++++++++++++++++ utils/file/cxx/CMakeLists.txt | 21 ++++ utils/file/cxx/README.md | 87 ++++++++++++++++ utils/file/cxx/aes_common.hpp | 132 +++++++++++++++++++++++ utils/file/cxx/aes_decrypt_bmp.cpp | 191 ++++++++++++++++++++++++++++++++++ utils/file/cxx/aes_decrypt_file.cpp | 182 ++++++++++++++++++++++++++++++++ utils/file/cxx/aes_encrypt_bmp.cpp | 190 +++++++++++++++++++++++++++++++++ utils/file/cxx/aes_encrypt_file.cpp | 182 ++++++++++++++++++++++++++++++++ utils/file/cxx/bmp/butterfly.bmp | Bin 0 -> 503370 bytes utils/file/cxx/bmp/cipherfly_cbc.bmp | Bin 0 -> 503382 bytes utils/file/cxx/bmp/cipherfly_ecb.bmp | Bin 0 -> 503382 bytes 61 files changed, 3900 insertions(+), 1201 deletions(-) delete mode 100644 utils/README.md delete mode 100644 utils/aes128ecb_decrypt_file.cpp delete mode 100644 utils/aes128ecb_encrypt_file.cpp delete mode 100644 utils/aes_common.hpp delete mode 100644 utils/aes_decrypt_bmp.cpp delete mode 100644 utils/aes_decrypt_file.cpp delete mode 100644 utils/aes_encrypt_bmp.cpp delete mode 100644 utils/aes_encrypt_file.cpp create mode 100644 utils/block/CMakeLists.txt create mode 100644 utils/block/aes128cbc_decrypt_block.c create mode 100644 utils/block/aes128cbc_encrypt_block.c create mode 100644 utils/block/aes128cfb_decrypt_block.c create mode 100644 utils/block/aes128cfb_encrypt_block.c create mode 100644 utils/block/aes128ctr_decrypt_block.c create mode 100644 utils/block/aes128ctr_encrypt_block.c create mode 100644 utils/block/aes128ecb_decrypt_block.c create mode 100644 utils/block/aes128ecb_encrypt_block.c create mode 100644 utils/block/aes128ofb_decrypt_block.c create mode 100644 utils/block/aes128ofb_encrypt_block.c create mode 100644 utils/block/aes192cbc_decrypt_block.c create mode 100644 utils/block/aes192cbc_encrypt_block.c create mode 100644 utils/block/aes192cfb_decrypt_block.c create mode 100644 utils/block/aes192cfb_encrypt_block.c create mode 100644 utils/block/aes192ctr_decrypt_block.c create mode 100644 utils/block/aes192ctr_encrypt_block.c create mode 100644 utils/block/aes192ecb_decrypt_block.c create mode 100644 utils/block/aes192ecb_encrypt_block.c create mode 100644 utils/block/aes192ofb_decrypt_block.c create mode 100644 utils/block/aes192ofb_encrypt_block.c create mode 100644 utils/block/aes256cbc_decrypt_block.c create mode 100644 utils/block/aes256cbc_encrypt_block.c create mode 100644 utils/block/aes256cfb_decrypt_block.c create mode 100644 utils/block/aes256cfb_encrypt_block.c create mode 100644 utils/block/aes256ctr_decrypt_block.c create mode 100644 utils/block/aes256ctr_encrypt_block.c create mode 100644 utils/block/aes256ecb_decrypt_block.c create mode 100644 utils/block/aes256ecb_encrypt_block.c create mode 100644 utils/block/aes256ofb_decrypt_block.c create mode 100644 utils/block/aes256ofb_encrypt_block.c create mode 100644 utils/block/cxx/CMakeLists.txt create mode 100644 utils/block/cxx/README.md create mode 100644 utils/block/cxx/aes_common.hpp create mode 100644 utils/block/cxx/aes_decrypt_block.cpp create mode 100644 utils/block/cxx/aes_encrypt_block.cpp delete mode 100644 utils/bmp/butterfly.bmp delete mode 100644 utils/bmp/cipherfly_cbc.bmp delete mode 100644 utils/bmp/cipherfly_ecb.bmp create mode 100644 utils/file/CMakeLists.txt create mode 100644 utils/file/aes128ecb_decrypt_file.cpp create mode 100644 utils/file/aes128ecb_encrypt_file.cpp create mode 100644 utils/file/cxx/CMakeLists.txt create mode 100644 utils/file/cxx/README.md create mode 100644 utils/file/cxx/aes_common.hpp create mode 100644 utils/file/cxx/aes_decrypt_bmp.cpp create mode 100644 utils/file/cxx/aes_decrypt_file.cpp create mode 100644 utils/file/cxx/aes_encrypt_bmp.cpp create mode 100644 utils/file/cxx/aes_encrypt_file.cpp create mode 100644 utils/file/cxx/bmp/butterfly.bmp create mode 100644 utils/file/cxx/bmp/cipherfly_cbc.bmp create mode 100644 utils/file/cxx/bmp/cipherfly_ecb.bmp (limited to 'utils') diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index d13bdeb..ce18593 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,30 +1,2 @@ -macro(util prefix) - add_executable(util_${prefix} ${prefix}.cpp) - target_link_libraries(util_${prefix} libaesnixx libaesni) - set_target_properties(util_${prefix} PROPERTIES OUTPUT_NAME ${prefix}) -endmacro() - -util(aes128ecb_encrypt_file) -util(aes128ecb_decrypt_file) - -find_package(Boost REQUIRED COMPONENTS program_options) - -add_executable(util_aes_encrypt_file aes_encrypt_file.cpp aes_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_aes_decrypt_file aes_decrypt_file.cpp aes_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_aes_encrypt_bmp aes_encrypt_bmp.cpp aes_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_aes_decrypt_bmp aes_decrypt_bmp.cpp aes_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_subdirectory(block) +add_subdirectory(file) diff --git a/utils/README.md b/utils/README.md deleted file mode 100644 index ec23774..0000000 --- a/utils/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# Utilities - -Here are a couple of useful utilities built on top of the library. -Each of the utilities accepts `--help` flag, which can be used to examine utility's usage info. - -The included utilities are: - -* [file encryption](#file-encryption) utilities, -* and [bitmap encryption](#bitmap-encryption) utilities. - -On older CPUs, you can run the utilities [using Intel SDE](https://github.com/egor-tensin/aesni#running-on-older-cpus). - -## File encryption - -### aes_encrypt_file.exe - -Encrypts a file using AES-128/192/256 in the specified mode of operation. - -#### Usage examples - -For example, to encrypt the plaintext `input.txt` - -* using AES-128 in ECB mode -* with key `0x11111111111111111111111111111111` -* and write the ciphertext to `output.txt`, - -run: - - aes_encrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt - -To encrypt the plaintext from `input.txt` - -* using AES-192 in OFB mode -* with key `0x111111111111111111111111111111111111111111111111` -* and initialization vector `0x22222222222222222222222222222222` -* and write the ciphertext to `output.txt`: - -run - - aes_encrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt - -### aes_decrypt_file.exe - -Decrypts a file using AES-128/192/256 in the specified mode of operation. - -#### Usage examples - -To decrypt the ciphertext from `input.txt` - -* using AES-128 in ECB mode -* with key `0x11111111111111111111111111111111` -* and write the plaintext to `output.txt`, - -run - - aes_decrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt - -To decrypt the ciphertext from `input.txt` - -* using AES-192 in OFB mode -* with key `0x111111111111111111111111111111111111111111111111` -* and initialization vector `0x22222222222222222222222222222222` -* and write the plaintext to `output.txt`, - -run - - aes_decrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt - -## Bitmap encryption - -These utilities were developed primarily to demonstrate the drawbacks of using ECB mode -(namely, the fact that identical plaintext blocks get mapped to identical ciphertext blocks). -This can be explicitly shown using 8-bit-per-pixel bitmaps: - -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 - -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). - -### aes_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). diff --git a/utils/aes128ecb_decrypt_file.cpp b/utils/aes128ecb_decrypt_file.cpp deleted file mode 100644 index 9b8b15c..0000000 --- a/utils/aes128ecb_decrypt_file.cpp +++ /dev/null @@ -1,104 +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 -#include - -#include - -#include -#include -#include -#include -#include - -namespace -{ - void exit_with_usage() - { - std::cout << "Usage: aes128ecb_decrypt_file.exe KEY SRC DEST\n"; - std::exit(EXIT_FAILURE); - } - - 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(); - } -} - -int main(int argc, char** argv) -{ - if (argc != 4) - exit_with_usage(); - - try - { - aesni::aes::Key128 key; - aesni::aes::from_string(key, argv[1]); - - aesni::aes::RoundKeys128 encryption_keys, decryption_keys; - - const std::string src_path(argv[2]); - const std::string dest_path(argv[3]); - - const auto src_size = static_cast(get_file_size(src_path)); - - std::ifstream src_ifs; - src_ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - src_ifs.open(src_path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(src_size); - src_buf.assign(std::istreambuf_iterator(src_ifs), - std::istreambuf_iterator()); - - aesni_aes128_expand_key(&key, &encryption_keys); - aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); - - std::size_t dest_size; - - aesni_decrypt_buffer_ecb128( - src_buf.data(), - src_size, - NULL, - &dest_size, - &decryption_keys, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.reserve(dest_size); - - aesni_decrypt_buffer_ecb128( - src_buf.data(), - src_size, - dest_buf.data(), - &dest_size, - &decryption_keys, - aesni::ErrorDetailsThrowsInDestructor()); - - std::ofstream dest_ofs; - dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - dest_ofs.open(dest_path, std::ofstream::binary); - dest_ofs.write(dest_buf.data(), dest_size); - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/utils/aes128ecb_encrypt_file.cpp b/utils/aes128ecb_encrypt_file.cpp deleted file mode 100644 index a43c4d9..0000000 --- a/utils/aes128ecb_encrypt_file.cpp +++ /dev/null @@ -1,103 +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 -#include - -#include - -#include -#include -#include -#include -#include - -namespace -{ - void exit_with_usage() - { - std::cout << "Usage: aes128ecb_encrypt_file.exe KEY SRC DEST\n"; - std::exit(EXIT_FAILURE); - } - - 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(); - } -} - -int main(int argc, char** argv) -{ - if (argc != 4) - exit_with_usage(); - - try - { - aesni::aes::Key128 key; - aesni::aes::from_string(key, argv[1]); - - AesNI_Aes128_RoundKeys encryption_keys; - - const std::string src_path(argv[2]); - const std::string dest_path(argv[3]); - - const auto src_size = static_cast(get_file_size(src_path)); - - std::ifstream src_ifs; - src_ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - src_ifs.open(src_path, std::ifstream::binary); - - std::vector src_buf; - src_buf.reserve(src_size); - src_buf.assign(std::istreambuf_iterator(src_ifs), - std::istreambuf_iterator()); - - aesni_aes128_expand_key(&key, &encryption_keys); - - std::size_t dest_size; - - aesni_encrypt_buffer_ecb128( - src_buf.data(), - src_size, - NULL, - &dest_size, - &encryption_keys, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.reserve(dest_size); - - aesni_encrypt_buffer_ecb128( - src_buf.data(), - src_size, - dest_buf.data(), - &dest_size, - &encryption_keys, - aesni::ErrorDetailsThrowsInDestructor()); - - std::ofstream dest_ofs; - dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - dest_ofs.open(dest_path, std::ofstream::binary); - dest_ofs.write(dest_buf.data(), dest_size); - } - catch (const aesni::Error& e) - { - std::cerr << e; - return 1; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/utils/aes_common.hpp b/utils/aes_common.hpp deleted file mode 100644 index c15565f..0000000 --- a/utils/aes_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& program_name) - : m_program_name(program_name) - , m_options("Options") - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - m_options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&m_args)); - - po::options_description all_options; - all_options.add(m_options).add(hidden_options); - - po::positional_options_description positional_options; - positional_options.add("positional", -1); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); - - if (vm.count("help")) - { - print_usage(); - return false; - } - - po::notify(vm); - return true; - } - - void print_usage() - { - std::cout << "Usage: " << m_program_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n"; - std::cout << m_options << "\n"; - } - - AesNI_Mode get_mode() const - { - return m_mode; - } - - AesNI_Algorithm get_algorithm() const - { - return m_algorithm; - } - - std::deque get_args() - { - return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; - } - - private: - const std::string m_program_name; - boost::program_options::options_description m_options; - - AesNI_Mode m_mode; - AesNI_Algorithm m_algorithm; - std::vector m_args; - }; -} diff --git a/utils/aes_decrypt_bmp.cpp b/utils/aes_decrypt_bmp.cpp deleted file mode 100644 index 85dffe5..0000000 --- a/utils/aes_decrypt_bmp.cpp +++ /dev/null @@ -1,191 +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_common.hpp" - -#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()); - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_decrypt_bmp.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - - 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; - } - - if (args.size() != 2) - { - cmd_parser.print_usage(); - return 1; - } - - 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, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_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 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 af0d119..0000000 --- a/utils/aes_decrypt_file.cpp +++ /dev/null @@ -1,182 +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_common.hpp" - -#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()); - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("encrypt_file_aes.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - - 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; - } - - if (args.size() != 2) - { - cmd_parser.print_usage(); - return 1; - } - - 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, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_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 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 cf55b0b..0000000 --- a/utils/aes_encrypt_bmp.cpp +++ /dev/null @@ -1,190 +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_common.hpp" - -#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()); - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("aes_encrypt_bmp.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - - 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; - } - - if (args.size() != 2) - { - cmd_parser.print_usage(); - return 1; - } - - 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, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_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 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 0b367d9..0000000 --- a/utils/aes_encrypt_file.cpp +++ /dev/null @@ -1,182 +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_common.hpp" - -#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()); - } -} - -int main(int argc, char** argv) -{ - try - { - CommandLineParser cmd_parser("encrypt_file_aes.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - - 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; - } - - if (args.size() != 2) - { - cmd_parser.print_usage(); - return 1; - } - - 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, - cmd_parser.get_algorithm(), - &algorithm_params, - cmd_parser.get_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 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/CMakeLists.txt b/utils/block/CMakeLists.txt new file mode 100644 index 0000000..4b232bd --- /dev/null +++ b/utils/block/CMakeLists.txt @@ -0,0 +1,27 @@ +macro(util prefix) + add_executable(util_${prefix}_encrypt_block ${prefix}_encrypt_block.c) + target_link_libraries(util_${prefix}_encrypt_block libaesni) + set_target_properties(util_${prefix}_encrypt_block PROPERTIES OUTPUT_NAME ${prefix}_encrypt_block) + + add_executable(util_${prefix}_decrypt_block ${prefix}_decrypt_block.c) + target_link_libraries(util_${prefix}_decrypt_block libaesni) + set_target_properties(util_${prefix}_decrypt_block PROPERTIES OUTPUT_NAME ${prefix}_decrypt_block) +endmacro() + +util(aes128ecb) +util(aes128cbc) +util(aes128cfb) +util(aes128ofb) +util(aes128ctr) +util(aes192ecb) +util(aes192cbc) +util(aes192cfb) +util(aes192ofb) +util(aes192ctr) +util(aes256ecb) +util(aes256cbc) +util(aes256cfb) +util(aes256ofb) +util(aes256ctr) + +add_subdirectory(cxx) diff --git a/utils/block/aes128cbc_decrypt_block.c b/utils/block/aes128cbc_decrypt_block.c new file mode 100644 index 0000000..95120e7 --- /dev/null +++ b/utils/block/aes128cbc_decrypt_block.c @@ -0,0 +1,76 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128cbc_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys, decryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes128_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128cbc_encrypt_block.c b/utils/block/aes128cbc_encrypt_block.c new file mode 100644 index 0000000..68f95a3 --- /dev/null +++ b/utils/block/aes128cbc_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128cbc_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes128_encrypt_block_cbc(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128cfb_decrypt_block.c b/utils/block/aes128cfb_decrypt_block.c new file mode 100644 index 0000000..ef68e44 --- /dev/null +++ b/utils/block/aes128cfb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128cfb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes128_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128cfb_encrypt_block.c b/utils/block/aes128cfb_encrypt_block.c new file mode 100644 index 0000000..6f8fb4d --- /dev/null +++ b/utils/block/aes128cfb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128cfb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes128_encrypt_block_cfb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ctr_decrypt_block.c b/utils/block/aes128ctr_decrypt_block.c new file mode 100644 index 0000000..85d07f5 --- /dev/null +++ b/utils/block/aes128ctr_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ctr_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes128_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ctr_encrypt_block.c b/utils/block/aes128ctr_encrypt_block.c new file mode 100644 index 0000000..a62f3cd --- /dev/null +++ b/utils/block/aes128ctr_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ctr_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes128_encrypt_block_ctr(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ecb_decrypt_block.c b/utils/block/aes128ecb_decrypt_block.c new file mode 100644 index 0000000..9fbf26d --- /dev/null +++ b/utils/block/aes128ecb_decrypt_block.c @@ -0,0 +1,70 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ecb_decrypt_block.exe KEY0 [CIPHERTEXT0...] [-- KEY1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys, decryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes128_decrypt_block_ecb(ciphertext, &decryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ecb_encrypt_block.c b/utils/block/aes128ecb_encrypt_block.c new file mode 100644 index 0000000..45a1123 --- /dev/null +++ b/utils/block/aes128ecb_encrypt_block.c @@ -0,0 +1,69 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ecb_encrypt_block.exe KEY0 [PLAINTEXT0...] [-- KEY1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes128_encrypt_block_ecb(plaintext, &encryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ofb_decrypt_block.c b/utils/block/aes128ofb_decrypt_block.c new file mode 100644 index 0000000..e61e70f --- /dev/null +++ b/utils/block/aes128ofb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ofb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes128_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes128ofb_encrypt_block.c b/utils/block/aes128ofb_encrypt_block.c new file mode 100644 index 0000000..6613ca9 --- /dev/null +++ b/utils/block/aes128ofb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes128ofb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes128_Key key; + AesNI_Aes128_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes128_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes128_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes128_encrypt_block_ofb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192cbc_decrypt_block.c b/utils/block/aes192cbc_decrypt_block.c new file mode 100644 index 0000000..13edbe4 --- /dev/null +++ b/utils/block/aes192cbc_decrypt_block.c @@ -0,0 +1,76 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192cbc_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys, decryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + aesni_aes192_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes192_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192cbc_encrypt_block.c b/utils/block/aes192cbc_encrypt_block.c new file mode 100644 index 0000000..d89c12d --- /dev/null +++ b/utils/block/aes192cbc_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192cbc_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes192_encrypt_block_cbc(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192cfb_decrypt_block.c b/utils/block/aes192cfb_decrypt_block.c new file mode 100644 index 0000000..cedc7d0 --- /dev/null +++ b/utils/block/aes192cfb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192cfb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes192_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192cfb_encrypt_block.c b/utils/block/aes192cfb_encrypt_block.c new file mode 100644 index 0000000..357df8d --- /dev/null +++ b/utils/block/aes192cfb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192cfb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes192_encrypt_block_cfb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ctr_decrypt_block.c b/utils/block/aes192ctr_decrypt_block.c new file mode 100644 index 0000000..b094964 --- /dev/null +++ b/utils/block/aes192ctr_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ctr_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes192_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ctr_encrypt_block.c b/utils/block/aes192ctr_encrypt_block.c new file mode 100644 index 0000000..977c7bf --- /dev/null +++ b/utils/block/aes192ctr_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ctr_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes192_encrypt_block_ctr(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ecb_decrypt_block.c b/utils/block/aes192ecb_decrypt_block.c new file mode 100644 index 0000000..29e7274 --- /dev/null +++ b/utils/block/aes192ecb_decrypt_block.c @@ -0,0 +1,70 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ecb_decrypt_block.exe KEY0 [CIPHERTEXT0...] [-- KEY1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys, decryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + aesni_aes192_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes192_decrypt_block_ecb(ciphertext, &decryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ecb_encrypt_block.c b/utils/block/aes192ecb_encrypt_block.c new file mode 100644 index 0000000..a74759c --- /dev/null +++ b/utils/block/aes192ecb_encrypt_block.c @@ -0,0 +1,69 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ecb_encrypt_block.exe KEY0 [PLAINTEXT0...] [-- KEY1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes192_encrypt_block_ecb(plaintext, &encryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ofb_decrypt_block.c b/utils/block/aes192ofb_decrypt_block.c new file mode 100644 index 0000000..eaa9d2a --- /dev/null +++ b/utils/block/aes192ofb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ofb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes192_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes192ofb_encrypt_block.c b/utils/block/aes192ofb_encrypt_block.c new file mode 100644 index 0000000..fed0c5a --- /dev/null +++ b/utils/block/aes192ofb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes192ofb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes192_Key key; + AesNI_Aes192_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes192_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes192_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes192_encrypt_block_ofb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256cbc_decrypt_block.c b/utils/block/aes256cbc_decrypt_block.c new file mode 100644 index 0000000..0021007 --- /dev/null +++ b/utils/block/aes256cbc_decrypt_block.c @@ -0,0 +1,76 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256cbc_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys, decryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + aesni_aes256_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes256_decrypt_block_cbc(ciphertext, &decryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256cbc_encrypt_block.c b/utils/block/aes256cbc_encrypt_block.c new file mode 100644 index 0000000..e01ab95 --- /dev/null +++ b/utils/block/aes256cbc_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256cbc_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes256_encrypt_block_cbc(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256cfb_decrypt_block.c b/utils/block/aes256cfb_decrypt_block.c new file mode 100644 index 0000000..9d320a5 --- /dev/null +++ b/utils/block/aes256cfb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256cfb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes256_decrypt_block_cfb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256cfb_encrypt_block.c b/utils/block/aes256cfb_encrypt_block.c new file mode 100644 index 0000000..94e8619 --- /dev/null +++ b/utils/block/aes256cfb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256cfb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes256_encrypt_block_cfb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ctr_decrypt_block.c b/utils/block/aes256ctr_decrypt_block.c new file mode 100644 index 0000000..a0f9ef0 --- /dev/null +++ b/utils/block/aes256ctr_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ctr_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes256_decrypt_block_ctr(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ctr_encrypt_block.c b/utils/block/aes256ctr_encrypt_block.c new file mode 100644 index 0000000..fbae884 --- /dev/null +++ b/utils/block/aes256ctr_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ctr_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes256_encrypt_block_ctr(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ecb_decrypt_block.c b/utils/block/aes256ecb_decrypt_block.c new file mode 100644 index 0000000..16ffe77 --- /dev/null +++ b/utils/block/aes256ecb_decrypt_block.c @@ -0,0 +1,70 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ecb_decrypt_block.exe KEY0 [CIPHERTEXT0...] [-- KEY1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys, decryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + aesni_aes256_derive_decryption_keys(&encryption_keys, &decryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes256_decrypt_block_ecb(ciphertext, &decryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ecb_encrypt_block.c b/utils/block/aes256ecb_encrypt_block.c new file mode 100644 index 0000000..01ae7ed --- /dev/null +++ b/utils/block/aes256ecb_encrypt_block.c @@ -0,0 +1,69 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ecb_encrypt_block.exe KEY0 [PLAINTEXT0...] [-- KEY1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 1) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (--argc, ++argv; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes256_encrypt_block_ecb(plaintext, &encryption_keys); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ofb_decrypt_block.c b/utils/block/aes256ofb_decrypt_block.c new file mode 100644 index 0000000..a57c8db --- /dev/null +++ b/utils/block/aes256ofb_decrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ofb_decrypt_block.exe KEY0 IV0 [CIPHERTEXT0...] [-- KEY1 IV1 [CIPHERTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&ciphertext, *argv, NULL))) + { + print_error(status); + continue; + } + + plaintext = aesni_aes256_decrypt_block_ofb(ciphertext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&plaintext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/aes256ofb_encrypt_block.c b/utils/block/aes256ofb_encrypt_block.c new file mode 100644 index 0000000..7dc01b8 --- /dev/null +++ b/utils/block/aes256ofb_encrypt_block.c @@ -0,0 +1,75 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +static void exit_with_usage() +{ + puts("Usage: aes256ofb_encrypt_block.exe KEY0 IV0 [PLAINTEXT0...] [-- KEY1 IV1 [PLAINTEXT1...]...]"); + exit(EXIT_FAILURE); +} + +static void print_error(AesNI_StatusCode status) +{ + fprintf(stderr, "AesNI error: %s\n", aesni_strerror(status)); +} + +int main(int argc, char** argv) +{ + AesNI_StatusCode status = AESNI_SUCCESS; + + for (--argc, ++argv; argc > -1; --argc, ++argv) + { + AesNI_Block128 plaintext, ciphertext, iv; + AesNI_Aes256_Key key; + AesNI_Aes256_RoundKeys encryption_keys; + + if (argc < 2) + exit_with_usage(); + + if (aesni_is_error(status = aesni_aes256_parse_key(&key, *argv, NULL))) + { + print_error(status); + exit_with_usage(); + } + + if (aesni_is_error(status = aesni_aes_parse_block(&iv, argv[1], NULL))) + { + print_error(status); + exit_with_usage(); + } + + aesni_aes256_expand_key(&key, &encryption_keys); + + for (argc -= 2, argv += 2; argc > 0; --argc, ++argv) + { + if (strcmp("--", *argv) == 0) + break; + + if (aesni_is_error(status = aesni_aes_parse_block(&plaintext, *argv, NULL))) + { + print_error(status); + continue; + } + + ciphertext = aesni_aes256_encrypt_block_ofb(plaintext, &encryption_keys, iv, &iv); + + if (aesni_is_error(status = aesni_aes_print_block(&ciphertext, NULL))) + { + print_error(status); + continue; + } + } + } + + return 0; +} diff --git a/utils/block/cxx/CMakeLists.txt b/utils/block/cxx/CMakeLists.txt new file mode 100644 index 0000000..8df91d0 --- /dev/null +++ b/utils/block/cxx/CMakeLists.txt @@ -0,0 +1,11 @@ +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 new file mode 100644 index 0000000..bce2434 --- /dev/null +++ b/utils/block/cxx/README.md @@ -0,0 +1,60 @@ +# 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 new file mode 100644 index 0000000..78126e5 --- /dev/null +++ b/utils/block/cxx/aes_common.hpp @@ -0,0 +1,134 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +static std::istream& operator>>(std::istream& is, AesNI_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(&m_mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&m_args)); + + po::options_description all_options; + all_options.add(m_options).add(hidden_options); + + po::positional_options_description positional_options; + positional_options.add("positional", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); + + if (vm.count("help")) + { + print_usage(); + return false; + } + + po::notify(vm); + return true; + } + + void print_usage() + { + std::cout << "Usage: " << m_program_name << " [OPTIONS...] [-- KEY [IV] [PLAINTEXT...]...]\n"; + std::cout << m_options << "\n"; + } + + AesNI_Mode get_mode() const + { + return m_mode; + } + + AesNI_Algorithm get_algorithm() const + { + return m_algorithm; + } + + std::deque get_args() + { + return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; + } + + private: + const std::string m_program_name; + boost::program_options::options_description m_options; + + AesNI_Mode m_mode; + AesNI_Algorithm m_algorithm; + std::vector m_args; + }; +} diff --git a/utils/block/cxx/aes_decrypt_block.cpp b/utils/block/cxx/aes_decrypt_block.cpp new file mode 100644 index 0000000..9898d4e --- /dev/null +++ b/utils/block/cxx/aes_decrypt_block.cpp @@ -0,0 +1,122 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include +#include + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_block_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (cmd_parser.get_algorithm()) + { + case AESNI_AES128: + aesni::aes::from_string(algorithm_params.aes128_key, args.front()); + break; + + case AESNI_AES192: + aesni::aes::from_string(algorithm_params.aes192_key, args.front()); + break; + + case AESNI_AES256: + aesni::aes::from_string(algorithm_params.aes256_key, args.front()); + break; + } + + args.pop_front(); + + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + switch (cmd_parser.get_mode()) + { + case AESNI_ECB: + break; + + case AESNI_CBC: + case AESNI_CFB: + case AESNI_OFB: + case AESNI_CTR: + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + aesni::aes::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + break; + } + + AesNI_Box box; + aesni_box_init( + &box, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_mode(), + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + AesNI_BoxBlock ciphertext; + aesni::aes::from_string(ciphertext.aes_block, args.front()); + args.pop_front(); + + AesNI_BoxBlock plaintext; + aesni_box_decrypt_block( + &box, + &ciphertext, + &plaintext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n"; + } + } + + return 0; + } + catch (const boost::program_options::error& e) + { + std::cerr << "Usage error: " << e.what() << "\n"; + return 1; + } + catch (const aesni::Error& e) + { + std::cerr << e; + return 1; + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } +} diff --git a/utils/block/cxx/aes_encrypt_block.cpp b/utils/block/cxx/aes_encrypt_block.cpp new file mode 100644 index 0000000..9458831 --- /dev/null +++ b/utils/block/cxx/aes_encrypt_block.cpp @@ -0,0 +1,122 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include +#include + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_block_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + while (!args.empty()) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (cmd_parser.get_algorithm()) + { + case AESNI_AES128: + aesni::aes::from_string(algorithm_params.aes128_key, args.front()); + break; + + case AESNI_AES192: + aesni::aes::from_string(algorithm_params.aes192_key, args.front()); + break; + + case AESNI_AES256: + aesni::aes::from_string(algorithm_params.aes256_key, args.front()); + break; + } + + args.pop_front(); + + AesNI_BoxBlock iv; + AesNI_BoxBlock* iv_ptr = nullptr; + + switch (cmd_parser.get_mode()) + { + case AESNI_ECB: + break; + + case AESNI_CBC: + case AESNI_CFB: + case AESNI_OFB: + case AESNI_CTR: + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + aesni::aes::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); + break; + } + + AesNI_Box box; + aesni_box_init( + &box, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_mode(), + iv_ptr, + aesni::ErrorDetailsThrowsInDestructor()); + + while (!args.empty()) + { + if (args.front() == "--") + { + args.pop_front(); + break; + } + + AesNI_BoxBlock plaintext; + aesni::aes::from_string(plaintext.aes_block, args.front()); + args.pop_front(); + + AesNI_BoxBlock ciphertext; + aesni_box_encrypt_block( + &box, + &plaintext, + &ciphertext, + aesni::ErrorDetailsThrowsInDestructor()); + + std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n"; + } + } + + return 0; + } + catch (const boost::program_options::error& e) + { + std::cerr << "Usage error: " << e.what() << "\n"; + return 1; + } + catch (const aesni::Error& e) + { + std::cerr << e; + return 1; + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } +} diff --git a/utils/bmp/butterfly.bmp b/utils/bmp/butterfly.bmp deleted file mode 100644 index 105a55a..0000000 Binary files a/utils/bmp/butterfly.bmp and /dev/null differ diff --git a/utils/bmp/cipherfly_cbc.bmp b/utils/bmp/cipherfly_cbc.bmp deleted file mode 100644 index 664b557..0000000 Binary files a/utils/bmp/cipherfly_cbc.bmp and /dev/null differ diff --git a/utils/bmp/cipherfly_ecb.bmp b/utils/bmp/cipherfly_ecb.bmp deleted file mode 100644 index 78de9a8..0000000 Binary files a/utils/bmp/cipherfly_ecb.bmp and /dev/null differ diff --git a/utils/file/CMakeLists.txt b/utils/file/CMakeLists.txt new file mode 100644 index 0000000..ad949f7 --- /dev/null +++ b/utils/file/CMakeLists.txt @@ -0,0 +1,10 @@ +macro(util prefix) + add_executable(util_${prefix} ${prefix}.cpp) + target_link_libraries(util_${prefix} libaesnixx libaesni) + set_target_properties(util_${prefix} PROPERTIES OUTPUT_NAME ${prefix}) +endmacro() + +util(aes128ecb_encrypt_file) +util(aes128ecb_decrypt_file) + +add_subdirectory(cxx) diff --git a/utils/file/aes128ecb_decrypt_file.cpp b/utils/file/aes128ecb_decrypt_file.cpp new file mode 100644 index 0000000..9b8b15c --- /dev/null +++ b/utils/file/aes128ecb_decrypt_file.cpp @@ -0,0 +1,104 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace +{ + void exit_with_usage() + { + std::cout << "Usage: aes128ecb_decrypt_file.exe KEY SRC DEST\n"; + std::exit(EXIT_FAILURE); + } + + 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(); + } +} + +int main(int argc, char** argv) +{ + if (argc != 4) + exit_with_usage(); + + try + { + aesni::aes::Key128 key; + aesni::aes::from_string(key, argv[1]); + + aesni::aes::RoundKeys128 encryption_keys, decryption_keys; + + const std::string src_path(argv[2]); + const std::string dest_path(argv[3]); + + const auto src_size = static_cast(get_file_size(src_path)); + + std::ifstream src_ifs; + src_ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + src_ifs.open(src_path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(src_size); + src_buf.assign(std::istreambuf_iterator(src_ifs), + std::istreambuf_iterator()); + + aesni_aes128_expand_key(&key, &encryption_keys); + aesni_aes128_derive_decryption_keys(&encryption_keys, &decryption_keys); + + std::size_t dest_size; + + aesni_decrypt_buffer_ecb128( + src_buf.data(), + src_size, + NULL, + &dest_size, + &decryption_keys, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.reserve(dest_size); + + aesni_decrypt_buffer_ecb128( + src_buf.data(), + src_size, + dest_buf.data(), + &dest_size, + &decryption_keys, + aesni::ErrorDetailsThrowsInDestructor()); + + std::ofstream dest_ofs; + dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + dest_ofs.open(dest_path, std::ofstream::binary); + dest_ofs.write(dest_buf.data(), dest_size); + } + catch (const aesni::Error& e) + { + std::cerr << e; + return 1; + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + + return 0; +} diff --git a/utils/file/aes128ecb_encrypt_file.cpp b/utils/file/aes128ecb_encrypt_file.cpp new file mode 100644 index 0000000..a43c4d9 --- /dev/null +++ b/utils/file/aes128ecb_encrypt_file.cpp @@ -0,0 +1,103 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace +{ + void exit_with_usage() + { + std::cout << "Usage: aes128ecb_encrypt_file.exe KEY SRC DEST\n"; + std::exit(EXIT_FAILURE); + } + + 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(); + } +} + +int main(int argc, char** argv) +{ + if (argc != 4) + exit_with_usage(); + + try + { + aesni::aes::Key128 key; + aesni::aes::from_string(key, argv[1]); + + AesNI_Aes128_RoundKeys encryption_keys; + + const std::string src_path(argv[2]); + const std::string dest_path(argv[3]); + + const auto src_size = static_cast(get_file_size(src_path)); + + std::ifstream src_ifs; + src_ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + src_ifs.open(src_path, std::ifstream::binary); + + std::vector src_buf; + src_buf.reserve(src_size); + src_buf.assign(std::istreambuf_iterator(src_ifs), + std::istreambuf_iterator()); + + aesni_aes128_expand_key(&key, &encryption_keys); + + std::size_t dest_size; + + aesni_encrypt_buffer_ecb128( + src_buf.data(), + src_size, + NULL, + &dest_size, + &encryption_keys, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.reserve(dest_size); + + aesni_encrypt_buffer_ecb128( + src_buf.data(), + src_size, + dest_buf.data(), + &dest_size, + &encryption_keys, + aesni::ErrorDetailsThrowsInDestructor()); + + std::ofstream dest_ofs; + dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + dest_ofs.open(dest_path, std::ofstream::binary); + dest_ofs.write(dest_buf.data(), dest_size); + } + catch (const aesni::Error& e) + { + std::cerr << e; + return 1; + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + + return 0; +} diff --git a/utils/file/cxx/CMakeLists.txt b/utils/file/cxx/CMakeLists.txt new file mode 100644 index 0000000..75895a0 --- /dev/null +++ b/utils/file/cxx/CMakeLists.txt @@ -0,0 +1,21 @@ +find_package(Boost REQUIRED COMPONENTS program_options) + +add_executable(util_aes_encrypt_file aes_encrypt_file.cpp aes_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_aes_decrypt_file aes_decrypt_file.cpp aes_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_aes_encrypt_bmp aes_encrypt_bmp.cpp aes_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_aes_decrypt_bmp aes_decrypt_bmp.cpp aes_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) diff --git a/utils/file/cxx/README.md b/utils/file/cxx/README.md new file mode 100644 index 0000000..bf79cb2 --- /dev/null +++ b/utils/file/cxx/README.md @@ -0,0 +1,87 @@ +# File encryption utilities + +Here are a couple of useful file 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. + +The included utilities are: + +* [file encryption](#file-encryption) utilities, +* and [bitmap encryption](#bitmap-encryption) utilities. + +On older CPUs, you can run the utilities [using Intel SDE](https://github.com/egor-tensin/aesni#running-on-older-cpus). + +## File encryption + +### aes_encrypt_file.exe + +Encrypts a file using AES-128/192/256 in the specified mode of operation. + +#### Usage examples + +For example, to encrypt the plaintext `input.txt` + +* using AES-128 in ECB mode +* with key `0x11111111111111111111111111111111` +* and write the ciphertext to `output.txt`, + +run: + + aes_encrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt + +To encrypt the plaintext from `input.txt` + +* using AES-192 in OFB mode +* with key `0x111111111111111111111111111111111111111111111111` +* and initialization vector `0x22222222222222222222222222222222` +* and write the ciphertext to `output.txt`: + +run + + aes_encrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt + +### aes_decrypt_file.exe + +Decrypts a file using AES-128/192/256 in the specified mode of operation. + +#### Usage examples + +To decrypt the ciphertext from `input.txt` + +* using AES-128 in ECB mode +* with key `0x11111111111111111111111111111111` +* and write the plaintext to `output.txt`, + +run + + aes_decrypt_file.exe -a aes128 -m ecb 11111111111111111111111111111111 input.txt output.txt + +To decrypt the ciphertext from `input.txt` + +* using AES-192 in OFB mode +* with key `0x111111111111111111111111111111111111111111111111` +* and initialization vector `0x22222222222222222222222222222222` +* and write the plaintext to `output.txt`, + +run + + aes_decrypt_file.exe -a aes192 -m ofb 111111111111111111111111111111111111111111111111 22222222222222222222222222222222 input.txt output.txt + +## Bitmap encryption + +These utilities were developed primarily to demonstrate the drawbacks of using ECB mode +(namely, the fact that identical plaintext blocks get mapped to identical ciphertext blocks). +This can be explicitly shown using 8-bit-per-pixel bitmaps: + +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 + +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). + +### aes_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). diff --git a/utils/file/cxx/aes_common.hpp b/utils/file/cxx/aes_common.hpp new file mode 100644 index 0000000..c15565f --- /dev/null +++ b/utils/file/cxx/aes_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& program_name) + : m_program_name(program_name) + , m_options("Options") + { } + + bool parse_options(int argc, char** argv) + { + namespace po = boost::program_options; + + m_options.add_options() + ("help,h", "show this message and exit") + ("mode,m", po::value(&m_mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&m_algorithm)->required(), "set algorithm"); + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&m_args)); + + po::options_description all_options; + all_options.add(m_options).add(hidden_options); + + po::positional_options_description positional_options; + positional_options.add("positional", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); + + if (vm.count("help")) + { + print_usage(); + return false; + } + + po::notify(vm); + return true; + } + + void print_usage() + { + std::cout << "Usage: " << m_program_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n"; + std::cout << m_options << "\n"; + } + + AesNI_Mode get_mode() const + { + return m_mode; + } + + AesNI_Algorithm get_algorithm() const + { + return m_algorithm; + } + + std::deque get_args() + { + return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; + } + + private: + const std::string m_program_name; + boost::program_options::options_description m_options; + + AesNI_Mode m_mode; + AesNI_Algorithm m_algorithm; + std::vector m_args; + }; +} diff --git a/utils/file/cxx/aes_decrypt_bmp.cpp b/utils/file/cxx/aes_decrypt_bmp.cpp new file mode 100644 index 0000000..85dffe5 --- /dev/null +++ b/utils/file/cxx/aes_decrypt_bmp.cpp @@ -0,0 +1,191 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include + +#include +#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()); + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_decrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + + 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; + } + + if (args.size() != 2) + { + cmd_parser.print_usage(); + return 1; + } + + 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, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_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 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/cxx/aes_decrypt_file.cpp b/utils/file/cxx/aes_decrypt_file.cpp new file mode 100644 index 0000000..af0d119 --- /dev/null +++ b/utils/file/cxx/aes_decrypt_file.cpp @@ -0,0 +1,182 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include + +#include + +#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()); + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_file_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + + 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; + } + + if (args.size() != 2) + { + cmd_parser.print_usage(); + return 1; + } + + 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, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_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 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/cxx/aes_encrypt_bmp.cpp b/utils/file/cxx/aes_encrypt_bmp.cpp new file mode 100644 index 0000000..cf55b0b --- /dev/null +++ b/utils/file/cxx/aes_encrypt_bmp.cpp @@ -0,0 +1,190 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include + +#include +#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()); + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_encrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + + 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; + } + + if (args.size() != 2) + { + cmd_parser.print_usage(); + return 1; + } + + 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, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_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 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/cxx/aes_encrypt_file.cpp b/utils/file/cxx/aes_encrypt_file.cpp new file mode 100644 index 0000000..0b367d9 --- /dev/null +++ b/utils/file/cxx/aes_encrypt_file.cpp @@ -0,0 +1,182 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "aes_common.hpp" + +#include + +#include + +#include + +#include + +#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()); + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("encrypt_file_aes.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + auto args = cmd_parser.get_args(); + + if (args.empty()) + { + cmd_parser.print_usage(); + return 1; + } + + 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; + } + + if (args.size() != 2) + { + cmd_parser.print_usage(); + return 1; + } + + 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, + cmd_parser.get_algorithm(), + &algorithm_params, + cmd_parser.get_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 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/cxx/bmp/butterfly.bmp b/utils/file/cxx/bmp/butterfly.bmp new file mode 100644 index 0000000..105a55a Binary files /dev/null and b/utils/file/cxx/bmp/butterfly.bmp differ diff --git a/utils/file/cxx/bmp/cipherfly_cbc.bmp b/utils/file/cxx/bmp/cipherfly_cbc.bmp new file mode 100644 index 0000000..664b557 Binary files /dev/null and b/utils/file/cxx/bmp/cipherfly_cbc.bmp differ diff --git a/utils/file/cxx/bmp/cipherfly_ecb.bmp b/utils/file/cxx/bmp/cipherfly_ecb.bmp new file mode 100644 index 0000000..78de9a8 Binary files /dev/null and b/utils/file/cxx/bmp/cipherfly_ecb.bmp differ -- cgit v1.2.3