diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-07-07 17:04:48 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-07-07 17:04:48 +0300 |
commit | 1bafa86f2f7b1019314b5629f4fd80c7cf9c837c (patch) | |
tree | 9601e8dcc4b3b7d9e76fe398c3d0aa61325ee1bb /utils/file | |
parent | code style (diff) | |
download | aes-tools-1bafa86f2f7b1019314b5629f4fd80c7cf9c837c.tar.gz aes-tools-1bafa86f2f7b1019314b5629f4fd80c7cf9c837c.zip |
utils: rearrange files
Diffstat (limited to 'utils/file')
-rw-r--r-- | utils/file/CMakeLists.txt | 21 | ||||
-rw-r--r-- | utils/file/README.md | 87 | ||||
-rw-r--r-- | utils/file/aes_common.hpp | 132 | ||||
-rw-r--r-- | utils/file/aes_decrypt_bmp.cpp | 191 | ||||
-rw-r--r-- | utils/file/aes_decrypt_file.cpp | 182 | ||||
-rw-r--r-- | utils/file/aes_encrypt_bmp.cpp | 190 | ||||
-rw-r--r-- | utils/file/aes_encrypt_file.cpp | 182 | ||||
-rw-r--r-- | utils/file/bmp/butterfly.bmp | bin | 503370 -> 0 bytes | |||
-rw-r--r-- | utils/file/bmp/cipherfly_cbc.bmp | bin | 503382 -> 0 bytes | |||
-rw-r--r-- | utils/file/bmp/cipherfly_ecb.bmp | bin | 503382 -> 0 bytes |
10 files changed, 0 insertions, 985 deletions
diff --git a/utils/file/CMakeLists.txt b/utils/file/CMakeLists.txt deleted file mode 100644 index 75895a0..0000000 --- a/utils/file/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -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/README.md b/utils/file/README.md deleted file mode 100644 index bf79cb2..0000000 --- a/utils/file/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# 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/aes_common.hpp b/utils/file/aes_common.hpp deleted file mode 100644 index 28e16df..0000000 --- a/utils/file/aes_common.hpp +++ /dev/null @@ -1,132 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -#include <aesnixx/all.hpp> - -#include <boost/algorithm/string.hpp> -#include <boost/program_options.hpp> - -#include <deque> -#include <iostream> -#include <iterator> -#include <istream> -#include <string> -#include <vector> - -static std::istream& operator>>(std::istream& is, aesni::Mode& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "ecb")) - dest = AESNI_ECB; - else if (boost::iequals(src, "cbc")) - dest = AESNI_CBC; - else if (boost::iequals(src, "cfb")) - dest = AESNI_CFB; - else if (boost::iequals(src, "ofb")) - dest = AESNI_OFB; - else if (boost::iequals(src, "ctr")) - dest = AESNI_CTR; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); - - return is; -} - -static std::istream& operator>>(std::istream& is, aesni::Algorithm& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "aes128")) - dest = AESNI_AES128; - else if (boost::iequals(src, "aes192")) - dest = AESNI_AES192; - else if (boost::iequals(src, "aes256")) - dest = AESNI_AES256; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); - - return is; -} - -namespace -{ - class CommandLineParser - { - public: - CommandLineParser(const std::string& program_name) - : m_program_name(program_name) - , m_options("Options") - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - m_options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value<aesni::Mode>(&m_mode)->required(), "set mode of operation") - ("algorithm,a", po::value<aesni::Algorithm>(&m_algorithm)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value<std::vector<std::string>>(&m_args)); - - po::options_description all_options; - all_options.add(m_options).add(hidden_options); - - po::positional_options_description positional_options; - positional_options.add("positional", -1); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); - - if (vm.count("help")) - { - print_usage(); - return false; - } - - po::notify(vm); - return true; - } - - void print_usage() - { - std::cout << "Usage: " << m_program_name << " [OPTIONS...] KEY [IV] 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<std::string> get_args() - { - return { std::make_move_iterator(m_args.begin()), std::make_move_iterator(m_args.end()) }; - } - - private: - const std::string m_program_name; - boost::program_options::options_description m_options; - - aesni::Mode m_mode; - aesni::Algorithm m_algorithm; - std::vector<std::string> m_args; - }; -} diff --git a/utils/file/aes_decrypt_bmp.cpp b/utils/file/aes_decrypt_bmp.cpp deleted file mode 100644 index 85dffe5..0000000 --- a/utils/file/aes_decrypt_bmp.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <boost/program_options.hpp> - -#include <cstdlib> -#include <cstring> - -#include <exception> -#include <fstream> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -#include <Windows.h> - -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<char> read_file(const std::string& path) - { - const auto size = static_cast<std::size_t>(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator<char>(ifs), - std::istreambuf_iterator<char>()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector<char>& 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<const BITMAPFILEHEADER*>(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<char> 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/aes_decrypt_file.cpp b/utils/file/aes_decrypt_file.cpp deleted file mode 100644 index e2da979..0000000 --- a/utils/file/aes_decrypt_file.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <boost/program_options.hpp> - -#include <cstdlib> - -#include <exception> -#include <fstream> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -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<char> read_file(const std::string& path) - { - const auto size = static_cast<std::size_t>(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator<char>(ifs), - std::istreambuf_iterator<char>()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector<char>& 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_file.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<char> 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/aes_encrypt_bmp.cpp b/utils/file/aes_encrypt_bmp.cpp deleted file mode 100644 index cf55b0b..0000000 --- a/utils/file/aes_encrypt_bmp.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <boost/program_options.hpp> - -#include <cstdlib> -#include <cstring> - -#include <exception> -#include <fstream> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -#include <Windows.h> - -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<char> read_file(const std::string& path) - { - const auto size = static_cast<std::size_t>(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator<char>(ifs), - std::istreambuf_iterator<char>()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector<char>& 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<const BITMAPFILEHEADER*>(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<char> 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/aes_encrypt_file.cpp b/utils/file/aes_encrypt_file.cpp deleted file mode 100644 index ff6753f..0000000 --- a/utils/file/aes_encrypt_file.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include "aes_common.hpp" - -#include <aesni/all.h> - -#include <aesnixx/all.hpp> - -#include <boost/program_options.hpp> - -#include <cstdlib> - -#include <exception> -#include <fstream> -#include <iostream> -#include <string> -#include <utility> -#include <vector> - -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<char> read_file(const std::string& path) - { - const auto size = static_cast<std::size_t>(get_file_size(path)); - - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); - - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign(std::istreambuf_iterator<char>(ifs), - std::istreambuf_iterator<char>()); - return src_buf; - } - - void write_file(const std::string& path, const std::vector<char>& 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_file.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<char> 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/bmp/butterfly.bmp b/utils/file/bmp/butterfly.bmp Binary files differdeleted file mode 100644 index 105a55a..0000000 --- a/utils/file/bmp/butterfly.bmp +++ /dev/null diff --git a/utils/file/bmp/cipherfly_cbc.bmp b/utils/file/bmp/cipherfly_cbc.bmp Binary files differdeleted file mode 100644 index 664b557..0000000 --- a/utils/file/bmp/cipherfly_cbc.bmp +++ /dev/null diff --git a/utils/file/bmp/cipherfly_ecb.bmp b/utils/file/bmp/cipherfly_ecb.bmp Binary files differdeleted file mode 100644 index 78de9a8..0000000 --- a/utils/file/bmp/cipherfly_ecb.bmp +++ /dev/null |