aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--utils/CMakeLists.txt2
-rw-r--r--utils/aes128ecb_decrypt_file.cpp36
-rw-r--r--utils/aes128ecb_encrypt_file.cpp30
-rw-r--r--utils/common.hpp38
4 files changed, 85 insertions, 21 deletions
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt
index f6ca7d1..a667663 100644
--- a/utils/CMakeLists.txt
+++ b/utils/CMakeLists.txt
@@ -1,5 +1,5 @@
macro(util prefix)
- add_executable(util_${prefix} ${prefix}.cpp)
+ add_executable(util_${prefix} ${prefix}.cpp common.hpp)
target_link_libraries(util_${prefix} libaesni)
set_target_properties(util_${prefix} PROPERTIES OUTPUT_NAME ${prefix})
endmacro()
diff --git a/utils/aes128ecb_decrypt_file.cpp b/utils/aes128ecb_decrypt_file.cpp
index 33e0515..23545f9 100644
--- a/utils/aes128ecb_decrypt_file.cpp
+++ b/utils/aes128ecb_decrypt_file.cpp
@@ -6,10 +6,13 @@
* See LICENSE.txt for details.
*/
+#include "common.hpp"
+
#include <aesni/all.h>
-#include <cstdio>
+#include <cstdlib>
+#include <exception>
#include <iostream>
#include <fstream>
#include <string>
@@ -51,27 +54,40 @@ int main(int argc, char** argv)
const std::string src_path(argv[2]);
const std::string dest_path(argv[3]);
- const auto src_size = get_file_size(src_path);
+ const auto src_size = static_cast<std::size_t>(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<char> src_buf;
- src_buf.reserve(static_cast<std::vector<char>::size_type>(src_size));
+ src_buf.reserve(src_size);
src_buf.assign(std::istreambuf_iterator<char>(src_ifs),
std::istreambuf_iterator<char>());
aesni_expand_key_schedule128(key, &key_schedule);
aesni_invert_key_schedule128(&key_schedule, &inverted_schedule);
- auto dest_size = aesni_decrypt_buffer_ecb128(
- src_buf.data(), static_cast<std::size_t>(src_size), NULL, &inverted_schedule);
-
- std::vector<char> dest_buf(static_cast<std::vector<char>::size_type>(dest_size));
-
- dest_size = aesni_decrypt_buffer_ecb128(
- src_buf.data(), static_cast<std::size_t>(src_size), dest_buf.data(), &inverted_schedule);
+ std::size_t dest_size;
+
+ aesni_decrypt_buffer_ecb128(
+ src_buf.data(),
+ src_size,
+ NULL,
+ &dest_size,
+ &inverted_schedule,
+ aesni::ErrorDetailsThrowsInDestructor());
+
+ std::vector<char> dest_buf;
+ dest_buf.reserve(dest_size);
+
+ aesni_decrypt_buffer_ecb128(
+ src_buf.data(),
+ src_size,
+ dest_buf.data(),
+ &dest_size,
+ &inverted_schedule,
+ aesni::ErrorDetailsThrowsInDestructor());
std::ofstream dest_ofs;
dest_ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
diff --git a/utils/aes128ecb_encrypt_file.cpp b/utils/aes128ecb_encrypt_file.cpp
index 07c3e91..42e297c 100644
--- a/utils/aes128ecb_encrypt_file.cpp
+++ b/utils/aes128ecb_encrypt_file.cpp
@@ -6,10 +6,13 @@
* See LICENSE.txt for details.
*/
+#include "common.hpp"
+
#include <aesni/all.h>
-#include <cstdio>
+#include <cstdlib>
+#include <exception>
#include <iostream>
#include <fstream>
#include <string>
@@ -51,37 +54,44 @@ int main(int argc, char** argv)
const std::string src_path(argv[2]);
const std::string dest_path(argv[3]);
- const auto src_size = get_file_size(src_path);
+ const auto src_size = static_cast<std::size_t>(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<char> src_buf;
- src_buf.reserve(static_cast<std::vector<char>::size_type>(src_size));
+ src_buf.reserve(src_size);
src_buf.assign(std::istreambuf_iterator<char>(src_ifs),
std::istreambuf_iterator<char>());
aesni_expand_key_schedule128(key, &key_schedule);
- const auto dest_size = aesni_encrypt_buffer_ecb128(
+ std::size_t dest_size;
+
+ aesni_encrypt_buffer_ecb128(
src_buf.data(),
- static_cast<std::size_t>(src_size),
+ src_size,
NULL,
- &key_schedule);
+ &dest_size,
+ &key_schedule,
+ aesni::ErrorDetailsThrowsInDestructor());
- std::vector<char> dest_buf(static_cast<std::vector<char>::size_type>(dest_size));
+ std::vector<char> dest_buf;
+ dest_buf.reserve(dest_size);
aesni_encrypt_buffer_ecb128(
src_buf.data(),
- static_cast<std::size_t>(src_size),
+ src_size,
dest_buf.data(),
- &key_schedule);
+ &dest_size,
+ &key_schedule,
+ 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_buf.size());
+ dest_ofs.write(dest_buf.data(), dest_size);
}
catch (const std::exception& e)
{
diff --git a/utils/common.hpp b/utils/common.hpp
new file mode 100644
index 0000000..af61a32
--- /dev/null
+++ b/utils/common.hpp
@@ -0,0 +1,38 @@
+/**
+ * \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 <stdexcept>
+
+namespace aesni
+{
+ class ErrorDetailsThrowsInDestructor
+ {
+ public:
+ ErrorDetailsThrowsInDestructor()
+ {
+ aesni_make_error_success(get());
+ }
+
+ ~ErrorDetailsThrowsInDestructor()
+ {
+ if (m_impl.ec != AESNI_ERROR_SUCCESS)
+ throw std::runtime_error(aesni_strerror(m_impl.ec));
+ }
+
+ AesNI_ErrorDetails* get() { return &m_impl; }
+
+ operator AesNI_ErrorDetails*() { return get(); }
+
+ private:
+ AesNI_ErrorDetails m_impl;
+ };
+}