aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--utils/CMakeLists.txt8
-rw-r--r--utils/aes128ecb_decrypt_file.cpp89
-rw-r--r--utils/aes128ecb_encrypt_file.cpp90
3 files changed, 187 insertions, 0 deletions
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt
new file mode 100644
index 0000000..f6ca7d1
--- /dev/null
+++ b/utils/CMakeLists.txt
@@ -0,0 +1,8 @@
+macro(util prefix)
+ add_executable(util_${prefix} ${prefix}.cpp)
+ target_link_libraries(util_${prefix} libaesni)
+ set_target_properties(util_${prefix} PROPERTIES OUTPUT_NAME ${prefix})
+endmacro()
+
+util(aes128ecb_encrypt_file)
+util(aes128ecb_decrypt_file)
diff --git a/utils/aes128ecb_decrypt_file.cpp b/utils/aes128ecb_decrypt_file.cpp
new file mode 100644
index 0000000..a6757d4
--- /dev/null
+++ b/utils/aes128ecb_decrypt_file.cpp
@@ -0,0 +1,89 @@
+/**
+ * \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.
+ */
+
+extern "C"
+{
+#include <aesni/all.h>
+}
+
+#include <cstdio>
+
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+
+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(path, std::ifstream::binary | std::ifstream::ate);
+ return ifs.tellg();
+ }
+}
+
+int main(int argc, char** argv)
+{
+ AesBlock128 key;
+ Aes128KeySchedule key_schedule, inverted_schedule;
+
+ if (argc != 4)
+ exit_with_usage();
+
+ if (parse_aes_block128(&key, argv[1]) != 0)
+ {
+ std::cerr << "Invalid 128-bit AES block '" << argv[1] << "'\n";
+ exit_with_usage();
+ }
+
+ try
+ {
+ const std::string src_path(argv[2]);
+ const std::string dest_path(argv[3]);
+
+ const auto src_size = 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<unsigned char> src_buf;
+ src_buf.reserve(static_cast<std::vector<char>::size_type>(src_size));
+ src_buf.assign(std::istreambuf_iterator<char>(src_ifs),
+ std::istreambuf_iterator<char>());
+
+ aes128_expand_key_schedule(key, &key_schedule);
+ aes128_invert_key_schedule(&key_schedule, &inverted_schedule);
+
+ auto dest_size = aes128ecb_decrypt_file(
+ src_buf.data(), static_cast<std::size_t>(src_size), NULL, &inverted_schedule);
+
+ std::vector<unsigned char> dest_buf(static_cast<std::vector<char>::size_type>(dest_size));
+
+ dest_size = aes128ecb_decrypt_file(
+ src_buf.data(), static_cast<std::size_t>(src_size), dest_buf.data(), &inverted_schedule);
+
+ std::ofstream dest_ofs;
+ dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
+ dest_ofs.open(dest_path, std::ofstream::binary);
+ dest_ofs.write(reinterpret_cast<const char*>(dest_buf.data()), dest_size);
+ }
+ 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
new file mode 100644
index 0000000..aba8569
--- /dev/null
+++ b/utils/aes128ecb_encrypt_file.cpp
@@ -0,0 +1,90 @@
+/**
+ * \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.
+ */
+
+extern "C"
+{
+#include <aesni/all.h>
+}
+
+#include <cstdio>
+
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+
+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(path, std::ifstream::binary | std::ifstream::ate);
+ return ifs.tellg();
+ }
+}
+
+int main(int argc, char** argv)
+{
+ AesBlock128 key;
+ Aes128KeySchedule key_schedule;
+
+ if (argc != 4)
+ exit_with_usage();
+
+ if (parse_aes_block128(&key, argv[1]) != 0)
+ {
+ std::cerr << "Invalid 128-bit AES block '" << argv[1] << "'\n";
+ exit_with_usage();
+ }
+
+ try
+ {
+ const std::string src_path(argv[2]);
+ const std::string dest_path(argv[3]);
+
+ const auto src_size = 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<unsigned char> src_buf;
+ src_buf.reserve(static_cast<std::vector<char>::size_type>(src_size));
+ src_buf.assign(std::istreambuf_iterator<char>(src_ifs),
+ std::istreambuf_iterator<char>());
+
+ aes128_expand_key_schedule(key, &key_schedule);
+
+ const auto dest_size = aes128ecb_encrypt_file(
+ src_buf.data(), static_cast<std::size_t>(src_size), NULL, &key_schedule);
+
+ std::vector<unsigned char> dest_buf(static_cast<std::vector<char>::size_type>(dest_size));
+
+ aes128ecb_encrypt_file(src_buf.data(),
+ static_cast<std::size_t>(src_size),
+ dest_buf.data(),
+ &key_schedule);
+
+ std::ofstream dest_ofs;
+ dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
+ dest_ofs.open(dest_path, std::ofstream::binary);
+ dest_ofs.write(reinterpret_cast<const char*>(dest_buf.data()), dest_buf.size());
+ }
+ catch (const std::exception& e)
+ {
+ std::cerr << e.what() << "\n";
+ return 1;
+ }
+
+ return 0;
+}