aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-06-19 09:42:55 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-06-19 09:42:55 +0300
commit3716501690fb4e1ddd1af385b4d012cce2d107ac (patch)
tree1b4f48b22cc980ba7cfb93bd5a83ec5e078157de
parentrefactoring (diff)
downloadaes-tools-3716501690fb4e1ddd1af385b4d012cce2d107ac.tar.gz
aes-tools-3716501690fb4e1ddd1af385b4d012cce2d107ac.zip
add buffer encryption to "boxes"
-rw-r--r--include/aesni/box.h18
-rw-r--r--include/aesni/box_data.h33
-rw-r--r--include/aesni/error.h4
-rw-r--r--src/box.c294
-rw-r--r--src/box_aes.c72
-rw-r--r--src/error.c8
-rw-r--r--utils/CMakeLists.txt12
-rw-r--r--utils/common_aes.hpp132
-rw-r--r--utils/decrypt_file_aes.cpp182
-rw-r--r--utils/encrypt_file_aes.cpp182
10 files changed, 937 insertions, 0 deletions
diff --git a/include/aesni/box.h b/include/aesni/box.h
index 3dfbc9e..12daa2e 100644
--- a/include/aesni/box.h
+++ b/include/aesni/box.h
@@ -11,6 +11,8 @@
#include "box_data.h"
#include "error.h"
+#include <stdlib.h>
+
#ifdef __cplusplus
extern "C"
{
@@ -36,6 +38,22 @@ AesNI_StatusCode aesni_box_decrypt_block(
AesNI_BoxBlock* plaintext,
AesNI_ErrorDetails* err_details);
+AesNI_StatusCode aesni_box_encrypt_buffer(
+ AesNI_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AesNI_ErrorDetails* err_details);
+
+AesNI_StatusCode aesni_box_decrypt_buffer(
+ AesNI_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AesNI_ErrorDetails* err_details);
+
#ifdef __cplusplus
}
#endif
diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h
index 327c9bc..9789f0f 100644
--- a/include/aesni/box_data.h
+++ b/include/aesni/box_data.h
@@ -97,6 +97,34 @@ typedef AesNI_StatusCode (*AesNI_BoxGetBlockSize)(
size_t*,
AesNI_ErrorDetails*);
+typedef AesNI_StatusCode (*AesNI_BoxStoreBlock)(
+ void*,
+ const AesNI_BoxBlock*,
+ AesNI_ErrorDetails*);
+
+typedef AesNI_StatusCode (*AesNI_BoxStorePartialBlock)(
+ void*,
+ const AesNI_BoxBlock*,
+ size_t,
+ AesNI_ErrorDetails*);
+
+typedef AesNI_StatusCode (*AesNI_BoxLoadBlock)(
+ AesNI_BoxBlock*,
+ const void*,
+ AesNI_ErrorDetails*);
+
+typedef AesNI_StatusCode (*AesNI_BoxLoadPartialBlock)(
+ AesNI_BoxBlock*,
+ const void*,
+ size_t,
+ AesNI_ErrorDetails*);
+
+typedef AesNI_StatusCode (*AesNI_BoxLoadBlockWithPadding)(
+ AesNI_BoxBlock*,
+ const void*,
+ size_t,
+ AesNI_ErrorDetails*);
+
typedef struct
{
AesNI_BoxDeriveParams derive_params;
@@ -105,6 +133,11 @@ typedef struct
AesNI_BoxXorBlock xor_block;
AesNI_BoxNextCounter next_counter;
AesNI_BoxGetBlockSize get_block_size;
+ AesNI_BoxStoreBlock store_block;
+ AesNI_BoxStorePartialBlock store_partial_block;
+ AesNI_BoxLoadBlock load_block;
+ AesNI_BoxLoadPartialBlock load_partial_block;
+ AesNI_BoxLoadBlockWithPadding load_block_with_padding;
}
AesNI_BoxAlgorithmInterface;
diff --git a/include/aesni/error.h b/include/aesni/error.h
index 015b16d..e78406a 100644
--- a/include/aesni/error.h
+++ b/include/aesni/error.h
@@ -47,6 +47,7 @@ typedef enum
AESNI_PARSE_ERROR, ///< Couldn't parse
AESNI_INVALID_PKCS7_PADDING_ERROR, ///< Invalid PKCS7 padding while decrypting
AESNI_NOT_IMPLEMENTED_ERROR, ///< Not implemented
+ AESNI_INVALID_PLAINTEXT_LENGTH_ERROR,
}
AesNI_StatusCode;
@@ -167,6 +168,9 @@ AesNI_StatusCode aesni_error_not_implemented(
AesNI_ErrorDetails* err_details,
const char* what);
+AesNI_StatusCode aesni_error_invalid_plaintext_length(
+ AesNI_ErrorDetails* err_details);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/box.c b/src/box.c
index 6d0f5d2..75d471d 100644
--- a/src/box.c
+++ b/src/box.c
@@ -8,6 +8,8 @@
#include <aesni/all.h>
+#include <stdlib.h>
+
static const AesNI_BoxAlgorithmInterface* aesni_box_algorithms[] =
{
&aesni_box_algorithm_aes128,
@@ -253,3 +255,295 @@ AesNI_StatusCode aesni_box_decrypt_block(
{
return aesni_box_decrypt_block_in_mode[box->mode](box, input, output, err_details);
}
+
+static AesNI_StatusCode aesni_box_get_encrypted_buffer_size(
+ AesNI_Box* box,
+ size_t src_size,
+ size_t* dest_size,
+ size_t* padding_size,
+ AesNI_ErrorDetails* err_details)
+{
+ AesNI_StatusCode status = AESNI_SUCCESS;
+
+ switch (box->mode)
+ {
+ case AESNI_ECB:
+ case AESNI_CBC:
+ {
+ size_t block_size;
+
+ if (aesni_is_error(status = box->algorithm->get_block_size(
+ &block_size, err_details)))
+ return status;
+
+ *padding_size = block_size - src_size % block_size;
+ *dest_size = src_size + *padding_size;
+ return status;
+ }
+
+ case AESNI_CFB:
+ case AESNI_OFB:
+ case AESNI_CTR:
+ *dest_size = src_size;
+ *padding_size = 0;
+ return status;
+
+ default:
+ return aesni_error_not_implemented(err_details, "unsupported mode of operation");
+ }
+}
+
+static AesNI_StatusCode aesni_box_encrypt_buffer_block(
+ AesNI_Box* box,
+ const void* src,
+ void* dest,
+ AesNI_ErrorDetails* err_details)
+{
+ AesNI_StatusCode status = AESNI_SUCCESS;
+
+ AesNI_BoxBlock plaintext;
+
+ if (aesni_is_error(status = box->algorithm->load_block(
+ &plaintext, src, err_details)))
+ return status;
+
+ AesNI_BoxBlock ciphertext;
+
+ if (aesni_is_error(status = aesni_box_encrypt_block(
+ box, &plaintext, &ciphertext, err_details)))
+ return status;
+
+ if (aesni_is_error(status = box->algorithm->store_block(
+ dest, &ciphertext, err_details)))
+ return status;
+
+ return status;
+}
+
+AesNI_StatusCode aesni_box_encrypt_buffer(
+ AesNI_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AesNI_ErrorDetails* err_details)
+{
+ if (box == NULL)
+ return aesni_error_null_argument(err_details, "box");
+ if (dest_size == NULL)
+ return aesni_error_null_argument(err_details, "dest_size");
+
+ AesNI_StatusCode status = AESNI_SUCCESS;
+ size_t padding_size = 0;
+
+ if (aesni_is_error(status = aesni_box_get_encrypted_buffer_size(
+ box, src_size, dest_size, &padding_size, err_details)))
+ return status;
+
+ if (dest == NULL)
+ return AESNI_SUCCESS;
+ if (src == NULL)
+ return aesni_error_null_argument(err_details, "src");
+
+ size_t block_size;
+
+ if (aesni_is_error(status = box->algorithm->get_block_size(
+ &block_size, err_details)))
+ return status;
+
+ const size_t src_len = src_size / block_size;
+
+ for (size_t i = 0; i < src_len; ++i, (char*) src += block_size, (char*) dest += block_size)
+ if (aesni_is_error(status = aesni_box_encrypt_buffer_block(
+ box, src, dest, err_details)))
+ return status;
+
+ if (padding_size == 0)
+ {
+ const size_t partial_block_size = src_size % block_size;
+
+ if (partial_block_size != 0)
+ {
+ AesNI_BoxBlock plaintext;
+
+ if (aesni_is_error(status = box->algorithm->load_partial_block(
+ &plaintext, src, partial_block_size, err_details)))
+ return status;
+
+ AesNI_BoxBlock ciphertext;
+
+ if (aesni_is_error(status = aesni_box_encrypt_block(
+ box, &plaintext, &ciphertext, err_details)))
+ return status;
+
+ if (aesni_is_error(status = box->algorithm->store_partial_block(
+ dest, &ciphertext, partial_block_size, err_details)))
+ return status;
+ }
+ }
+ else
+ {
+ AesNI_BoxBlock plaintext;
+
+ if (aesni_is_error(status = box->algorithm->load_block_with_padding(
+ &plaintext, src, src_size % block_size, err_details)))
+ return status;
+
+ AesNI_BoxBlock ciphertext;
+
+ if (aesni_is_error(status = aesni_box_encrypt_block(
+ box, &plaintext, &ciphertext, err_details)))
+ return status;
+
+ if (aesni_is_error(status = box->algorithm->store_block(
+ dest, &ciphertext, err_details)))
+ return status;
+ }
+
+ return status;
+}
+
+static AesNI_StatusCode aesni_box_get_decrypted_buffer_size(
+ AesNI_Box* box,
+ size_t src_size,
+ size_t* dest_size,
+ size_t* max_padding_size,
+ AesNI_ErrorDetails* err_details)
+{
+ AesNI_StatusCode status = AESNI_SUCCESS;
+
+ switch (box->mode)
+ {
+ case AESNI_ECB:
+ case AESNI_CBC:
+ {
+ size_t block_size;
+
+ if (aesni_is_error(status = box->algorithm->get_block_size(
+ &block_size, err_details)))
+ return status;
+
+ if (src_size % block_size != 0)
+ return aesni_error_invalid_plaintext_length(err_details);
+
+ *dest_size = src_size;
+ *max_padding_size = block_size;
+ return status;
+ }
+
+ case AESNI_CFB:
+ case AESNI_OFB:
+ case AESNI_CTR:
+ *dest_size = src_size;
+ *max_padding_size = 0;
+ return status;
+
+ default:
+ return aesni_error_not_implemented(err_details, "unsupported mode of operation");
+ }
+}
+
+static AesNI_StatusCode aesni_box_decrypt_buffer_block(
+ AesNI_Box* box,
+ const void* src,
+ void* dest,
+ AesNI_ErrorDetails* err_details)
+{
+ AesNI_StatusCode status = AESNI_SUCCESS;
+
+ AesNI_BoxBlock ciphertext;
+
+ if (aesni_is_error(status = box->algorithm->load_block(
+ &ciphertext, src, err_details)))
+ return status;
+
+ AesNI_BoxBlock plaintext;
+
+ if (aesni_is_error(status = aesni_box_decrypt_block(
+ box, &ciphertext, &plaintext, err_details)))
+ return status;
+
+ if (aesni_is_error(status = box->algorithm->store_block(
+ dest, &plaintext, err_details)))
+ return status;
+
+ return status;
+}
+
+AesNI_StatusCode aesni_box_decrypt_buffer(
+ AesNI_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AesNI_ErrorDetails* err_details)
+{
+ if (box == NULL)
+ return aesni_error_null_argument(err_details, "box");
+ if (dest_size == NULL)
+ return aesni_error_null_argument(err_details, "dest_size");
+
+ AesNI_StatusCode status = AESNI_SUCCESS;
+ size_t padding_size = 0;
+
+ if (aesni_is_error(status = aesni_box_get_decrypted_buffer_size(
+ box, src_size, dest_size, &padding_size, err_details)))
+ return status;
+
+ if (dest == NULL)
+ return AESNI_SUCCESS;
+ if (src == NULL)
+ return aesni_error_null_argument(err_details, "src");
+
+ size_t block_size;
+
+ if (aesni_is_error(status = box->algorithm->get_block_size(
+ &block_size, err_details)))
+ return status;
+
+ const size_t src_len = src_size / block_size;
+
+ for (size_t i = 0; i < src_len; ++i, (char*) src += block_size, (char*) dest += block_size)
+ if (aesni_is_error(status = aesni_box_decrypt_buffer_block(
+ box, src, dest, err_details)))
+ return status;
+
+ if (padding_size == 0)
+ {
+ const size_t partial_block_size = src_size % block_size;
+
+ if (partial_block_size != 0)
+ {
+ AesNI_BoxBlock ciphertext;
+
+ if (aesni_is_error(status = box->algorithm->load_partial_block(
+ &ciphertext, src, partial_block_size, err_details)))
+ return status;
+
+ AesNI_BoxBlock plaintext;
+
+ if (aesni_is_error(status = aesni_box_decrypt_block(
+ box, &ciphertext, &plaintext, err_details)))
+ return status;
+
+ if (aesni_is_error(status = box->algorithm->store_partial_block(
+ dest, &plaintext, partial_block_size, err_details)))
+ return status;
+ }
+ }
+ else
+ {
+ padding_size = ((unsigned char*) dest)[-1];
+
+ if (padding_size > block_size)
+ return aesni_error_invalid_pkcs7_padding(err_details);
+
+ for (size_t i = 1; i < padding_size; ++i)
+ if (((unsigned char*) dest)[0 - i] != padding_size)
+ return aesni_error_invalid_pkcs7_padding(err_details);
+
+ *dest_size -= padding_size;
+ }
+
+ return status;
+}
diff --git a/src/box_aes.c b/src/box_aes.c
index 12f369d..49c5326 100644
--- a/src/box_aes.c
+++ b/src/box_aes.c
@@ -9,6 +9,7 @@
#include <aesni/all.h>
#include <stdlib.h>
+#include <string.h>
static AesNI_StatusCode aesni_box_derive_params_aes128(
const AesNI_BoxAlgorithmParams* algorithm_params,
@@ -82,6 +83,62 @@ static AesNI_StatusCode aesni_box_get_block_size_aes(
return AESNI_SUCCESS;
}
+static AesNI_StatusCode aesni_box_store_block_aes(
+ void* dest,
+ const AesNI_BoxBlock* src,
+ AesNI_ErrorDetails* err_details)
+{
+ aesni_store_block128(dest, src->aes_block);
+ return AESNI_SUCCESS;
+}
+
+static AesNI_StatusCode aesni_box_store_partial_block_aes(
+ void* dest,
+ const AesNI_BoxBlock* src,
+ size_t src_size,
+ AesNI_ErrorDetails* err_details)
+{
+ __declspec(align(16)) unsigned char buf[16];
+ aesni_store_block128(buf, src->aes_block);
+ memcpy(dest, buf, src_size);
+ return AESNI_SUCCESS;
+}
+
+static AesNI_StatusCode aesni_box_load_block_aes(
+ AesNI_BoxBlock* dest,
+ const void* src,
+ AesNI_ErrorDetails* err_details)
+{
+ dest->aes_block = aesni_load_block128(src);
+ return AESNI_SUCCESS;
+}
+
+static AesNI_StatusCode aesni_box_load_partial_block_aes(
+ AesNI_BoxBlock* dest,
+ const void* src,
+ size_t src_size,
+ AesNI_ErrorDetails* err_details)
+{
+ __declspec(align(16)) unsigned char buf[16];
+ memset(buf, 0x00, 16);
+ memcpy(buf, src, src_size);
+ dest->aes_block = aesni_load_block128_aligned(buf);
+ return AESNI_SUCCESS;
+}
+
+static AesNI_StatusCode aesni_box_load_block_with_padding_aes(
+ AesNI_BoxBlock* dest,
+ const void* src,
+ size_t src_size,
+ AesNI_ErrorDetails* err_details)
+{
+ __declspec(align(16)) unsigned char padding[16];
+ memset(padding + src_size, 16 - src_size, 16 - src_size);
+ memcpy(padding, src, src_size);
+ dest->aes_block = aesni_load_block128_aligned(padding);
+ return AESNI_SUCCESS;
+}
+
static AesNI_StatusCode aesni_box_encrypt_block_aes128(
const AesNI_BoxBlock* input,
const AesNI_BoxEncryptionParams* params,
@@ -162,6 +219,11 @@ AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes128 =
&aesni_box_xor_block_aes,
&aesni_box_next_counter_aes,
&aesni_box_get_block_size_aes,
+ &aesni_box_store_block_aes,
+ &aesni_box_store_partial_block_aes,
+ &aesni_box_load_block_aes,
+ &aesni_box_load_partial_block_aes,
+ &aesni_box_load_block_with_padding_aes,
};
AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192 =
@@ -172,6 +234,11 @@ AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192 =
&aesni_box_xor_block_aes,
&aesni_box_next_counter_aes,
&aesni_box_get_block_size_aes,
+ &aesni_box_store_block_aes,
+ &aesni_box_store_partial_block_aes,
+ &aesni_box_load_block_aes,
+ &aesni_box_load_partial_block_aes,
+ &aesni_box_load_block_with_padding_aes,
};
AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256 =
@@ -182,4 +249,9 @@ AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256 =
&aesni_box_xor_block_aes,
&aesni_box_next_counter_aes,
&aesni_box_get_block_size_aes,
+ &aesni_box_store_block_aes,
+ &aesni_box_store_partial_block_aes,
+ &aesni_box_load_block_aes,
+ &aesni_box_load_partial_block_aes,
+ &aesni_box_load_block_with_padding_aes,
};
diff --git a/src/error.c b/src/error.c
index bf35668..fd9136a 100644
--- a/src/error.c
+++ b/src/error.c
@@ -26,6 +26,7 @@ static const char* aesni_strerror_messages[] =
"Couldn't parse",
"Invalid PKCS7 padding (wrong key?)",
"Not implemented",
+ "Invalid plaintext length",
};
const char* aesni_strerror(AesNI_StatusCode ec)
@@ -103,6 +104,7 @@ static AesNI_ErrorFormatter err_formatters[] =
&aesni_format_parse_error,
&aesni_format_error_strerror,
&aesni_format_not_implemented_error,
+ &aesni_format_error_strerror,
};
size_t aesni_format_error(
@@ -201,3 +203,9 @@ AesNI_StatusCode aesni_error_not_implemented(
return status;
}
+
+AesNI_StatusCode aesni_error_invalid_plaintext_length(
+ AesNI_ErrorDetails* err_details)
+{
+ return aesni_make_error(err_details, AESNI_INVALID_PLAINTEXT_LENGTH_ERROR);
+}
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt
index e1099af..f38eeb8 100644
--- a/utils/CMakeLists.txt
+++ b/utils/CMakeLists.txt
@@ -6,3 +6,15 @@ endmacro()
util(aes128ecb_encrypt_file)
util(aes128ecb_decrypt_file)
+
+find_package(Boost REQUIRED COMPONENTS program_options)
+
+add_executable(util_encrypt_file_aes encrypt_file_aes.cpp common_aes.hpp)
+target_include_directories(util_encrypt_file_aes PRIVATE ${Boost_INCLUDE_DIRS})
+target_link_libraries(util_encrypt_file_aes libaesni libaesnixx ${Boost_LIBRARIES})
+set_target_properties(util_encrypt_file_aes PROPERTIES OUTPUT_NAME encrypt_file_aes)
+
+add_executable(util_decrypt_file_aes decrypt_file_aes.cpp common_aes.hpp)
+target_include_directories(util_decrypt_file_aes PRIVATE ${Boost_INCLUDE_DIRS})
+target_link_libraries(util_decrypt_file_aes libaesni libaesnixx ${Boost_LIBRARIES})
+set_target_properties(util_decrypt_file_aes PROPERTIES OUTPUT_NAME decrypt_file_aes)
diff --git a/utils/common_aes.hpp b/utils/common_aes.hpp
new file mode 100644
index 0000000..630f143
--- /dev/null
+++ b/utils/common_aes.hpp
@@ -0,0 +1,132 @@
+/**
+ * \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 <aesni/all.h>
+
+#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_BoxMode& dest)
+{
+ std::string src;
+ is >> src;
+
+ if (boost::iequals(src, "ecb"))
+ dest = AESNI_ECB;
+ else if (boost::iequals(src, "cbc"))
+ dest = AESNI_CBC;
+ else if (boost::iequals(src, "cfb"))
+ dest = AESNI_CFB;
+ else if (boost::iequals(src, "ofb"))
+ dest = AESNI_OFB;
+ else if (boost::iequals(src, "ctr"))
+ dest = AESNI_CTR;
+ else
+ throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src);
+
+ return is;
+}
+
+static std::istream& operator>>(std::istream& is, AesNI_BoxAlgorithm& dest)
+{
+ std::string src;
+ is >> src;
+
+ if (boost::iequals(src, "aes128"))
+ dest = AESNI_AES128;
+ else if (boost::iequals(src, "aes192"))
+ dest = AESNI_AES192;
+ else if (boost::iequals(src, "aes256"))
+ dest = AESNI_AES256;
+ else
+ throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src);
+
+ return is;
+}
+
+namespace
+{
+ class CommandLineParser
+ {
+ public:
+ CommandLineParser(const std::string& program_name)
+ : m_program_name(program_name)
+ , m_options("Options")
+ { }
+
+ bool parse_options(int argc, char** argv)
+ {
+ namespace po = boost::program_options;
+
+ m_options.add_options()
+ ("help,h", "show this message and exit")
+ ("mode,m", po::value<AesNI_BoxMode>(&m_mode)->required(), "set mode of operation")
+ ("algorithm,a", po::value<AesNI_BoxAlgorithm>(&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_BoxMode get_mode() const
+ {
+ return m_mode;
+ }
+
+ AesNI_BoxAlgorithm 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_BoxMode m_mode;
+ AesNI_BoxAlgorithm m_algorithm;
+ std::vector<std::string> m_args;
+ };
+}
diff --git a/utils/decrypt_file_aes.cpp b/utils/decrypt_file_aes.cpp
new file mode 100644
index 0000000..0112f4a
--- /dev/null
+++ b/utils/decrypt_file_aes.cpp
@@ -0,0 +1,182 @@
+/**
+ * \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 "common_aes.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("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<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/encrypt_file_aes.cpp b/utils/encrypt_file_aes.cpp
new file mode 100644
index 0000000..dae7c57
--- /dev/null
+++ b/utils/encrypt_file_aes.cpp
@@ -0,0 +1,182 @@
+/**
+ * \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 "common_aes.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("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<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;
+ }
+}