aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/aes_decrypt_file.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-08-02 20:35:38 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-08-02 20:35:38 +0300
commit2f7feb1d9222e0afaac8ae17db98d1556aa46aa4 (patch)
tree84adde32dc3ff844f7c7e63477c2d0cc0529e7ac /utils/aes_decrypt_file.cpp
parentcxx: more mode helpers (diff)
downloadaes-tools-2f7feb1d9222e0afaac8ae17db98d1556aa46aa4.tar.gz
aes-tools-2f7feb1d9222e0afaac8ae17db98d1556aa46aa4.zip
cxx: more algorithm-agnostic API
The code (in the utilities in particular) is a mess though, so a refactoring's coming up.
Diffstat (limited to 'utils/aes_decrypt_file.cpp')
-rw-r--r--utils/aes_decrypt_file.cpp133
1 files changed, 73 insertions, 60 deletions
diff --git a/utils/aes_decrypt_file.cpp b/utils/aes_decrypt_file.cpp
index 650ca5e..d456b67 100644
--- a/utils/aes_decrypt_file.cpp
+++ b/utils/aes_decrypt_file.cpp
@@ -16,6 +16,7 @@
#include <cstdlib>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -55,72 +56,28 @@ namespace
ofs.open(path, std::ofstream::binary);
ofs.write(src.data(), src.size());
}
-}
-int main(int argc, char** argv)
-{
- try
+ template <aesni::Algorithm algorithm>
+ bool decrypt_file_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- 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())
+ if (aesni::mode_requires_initialization_vector(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.empty())
+ return false;
+
+ aesni::from_string<algorithm>(iv.aes_block, args.front());
+ iv_ptr = &iv;
+ args.pop_front();
}
if (args.size() != 2)
- {
- cmd_parser.print_usage();
- return 1;
- }
+ return false;
const auto src_path = args[0];
const auto dest_path = args[1];
@@ -131,9 +88,9 @@ int main(int argc, char** argv)
aesni_box_init(
&box,
- cmd_parser.get_algorithm(),
+ algorithm,
&algorithm_params,
- cmd_parser.get_mode(),
+ mode,
iv_ptr,
aesni::ErrorDetailsThrowsInDestructor());
@@ -159,9 +116,65 @@ int main(int argc, char** argv)
aesni::ErrorDetailsThrowsInDestructor());
dest_buf.resize(dest_size);
-
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool decrypt_file(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
+ {
+ if (args.empty())
+ return false;
+
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, args);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, args.front());
+ args.pop_front();
+ return decrypt_file_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_encrypt_file.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!decrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args()))
+ {
+ cmd_parser.print_usage();
+ return 1;
+ }
+
return 0;
}
catch (const boost::program_options::error& e)