aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils
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
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 '')
-rw-r--r--utils/aes_block_common.hpp140
-rw-r--r--utils/aes_decrypt_block.cpp98
-rw-r--r--utils/aes_decrypt_bmp.cpp132
-rw-r--r--utils/aes_decrypt_file.cpp133
-rw-r--r--utils/aes_encrypt_block.cpp102
-rw-r--r--utils/aes_encrypt_bmp.cpp132
-rw-r--r--utils/aes_encrypt_file.cpp133
7 files changed, 459 insertions, 411 deletions
diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp
index bdb6ac6..52b9ce3 100644
--- a/utils/aes_block_common.hpp
+++ b/utils/aes_block_common.hpp
@@ -151,126 +151,92 @@ namespace
namespace
{
- void dump_block(const char* name, const aesni::aes::Block& block)
+ template <aesni::Algorithm algorithm>
+ void dump_block(const char* name, const typename aesni::Types<algorithm>::Block& block)
{
- std::cout << name << ": " << aesni::aes::to_string(block) << "\n" << aesni::aes::to_matrix_string(block) << "\n";
+ std::cout << name << ": " << aesni::to_string<algorithm>(block) << "\n" << aesni::to_matrix_string<algorithm>(block) << "\n";
}
- void dump_plaintext(const aesni::aes::Block& block)
+ template <aesni::Algorithm algorithm>
+ void dump_plaintext(const typename aesni::Types<algorithm>::Block& block)
{
- dump_block("Plaintext", block);
+ dump_block<algorithm>("Plaintext", block);
}
- template <typename KeyT>
- void dump_key(const KeyT& key)
+ template <aesni::Algorithm algorithm>
+ void dump_key(const typename aesni::Types<algorithm>::Key& key)
{
- std::cout << "Key: " << aesni::aes::to_string(key) << "\n\n";
+ std::cout << "Key: " << aesni::to_string<algorithm>(key) << "\n\n";
}
- void dump_ciphertext(const aesni::aes::Block& ciphertext)
+ template <aesni::Algorithm algorithm>
+ void dump_ciphertext(const typename aesni::Types<algorithm>::Block& ciphertext)
{
- dump_block("Ciphertext", ciphertext);
+ dump_block<algorithm>("Ciphertext", ciphertext);
}
- void dump_iv(const aesni::aes::Block& iv)
+ template <aesni::Algorithm algorithm>
+ void dump_iv(const typename aesni::Types<algorithm>::Block& iv)
{
- dump_block("Initialization vector", iv);
+ dump_block<algorithm>("Initialization vector", iv);
}
- void dump_next_iv(const aesni::aes::Block& next_iv)
- {
- dump_block("Next initialization vector", next_iv);
- }
-
- template <typename RoundKeysT>
- void dump_round_keys(const char* name, const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_round_keys(const char* name, const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
std::cout << name << ":\n";
- for (std::size_t i = 0; i < aesni::aes::get_number_of_rounds(round_keys); ++i)
- std::cout << "\t[" << i << "]: " << aesni::aes::to_string(round_keys.keys[i]) << "\n";
+ for (std::size_t i = 0; i < aesni::get_number_of_rounds<algorithm>(); ++i)
+ std::cout << "\t[" << i << "]: " << aesni::to_string<algorithm>(round_keys.keys[i]) << "\n";
std::cout << "\n";
}
- template <typename RoundKeysT>
- void dump_encryption_keys(const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_encryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
- dump_round_keys("Encryption round keys", round_keys);
+ dump_round_keys<algorithm>("Encryption round keys", round_keys);
}
- template <typename RoundKeysT>
- void dump_decryption_keys(const RoundKeysT& round_keys)
+ template <aesni::Algorithm algorithm>
+ void dump_decryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
{
- dump_round_keys("Decryption round keys", round_keys);
+ dump_round_keys<algorithm>("Decryption round keys", round_keys);
}
- template <aesni::Algorithm algo, aesni::Mode mode>
- struct Dumper;
-
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_ECB>
- {
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_ECB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- dump_decryption_keys(encrypt.decryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_ECB>&)
- { }
- };
-
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CBC>
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CBC>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- dump_decryption_keys(encrypt.decryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CBC>&)
- { }
- };
+ dump_encryption_keys<algorithm>(wrapper.encryption_keys);
+ }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CFB>
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
-
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CFB>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ dump_decryption_keys<algorithm>(wrapper.decryption_keys);
+ }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_OFB>
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_OFB>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>&)
+ { }
- template <aesni::Algorithm algo>
- struct Dumper<algo, AESNI_CTR>
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
{
- static void dump_round_keys(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt)
- {
- dump_encryption_keys(encrypt.encryption_keys);
- }
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
- static void dump_next_iv(const aesni::aes::Encrypt<algo, AESNI_CTR>& encrypt)
- {
- ::dump_next_iv(encrypt.iv);
- }
- };
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>&)
+ { }
}
diff --git a/utils/aes_decrypt_block.cpp b/utils/aes_decrypt_block.cpp
index bab5f50..0f41b47 100644
--- a/utils/aes_decrypt_block.cpp
+++ b/utils/aes_decrypt_block.cpp
@@ -25,48 +25,48 @@ namespace
std::deque<std::string>& ciphertexts,
bool verbose = false)
{
- typename aesni::aes::Types<algorithm>::BlockT iv;
+ typename aesni::Types<algorithm>::Block iv;
if (aesni::ModeRequiresInitializationVector<mode>())
{
if (ciphertexts.empty())
return false;
- aesni::aes::from_string(iv, ciphertexts.front());
+ aesni::from_string<algorithm>(iv, ciphertexts.front());
ciphertexts.pop_front();
if (verbose)
- dump_iv(iv);
+ dump_iv<algorithm>(iv);
}
- typename aesni::aes::Types<algorithm>::KeyT key;
- aesni::aes::from_string(key, key_str);
+ typename aesni::Types<algorithm>::Key key;
+ aesni::from_string<algorithm>(key, key_str);
if (verbose)
- dump_key(key);
+ dump_key<algorithm>(key);
- aesni::aes::Encrypt<algorithm, mode> encrypt(key, iv);
+ aesni::DecryptWrapper<algorithm, mode> decrypt(key, iv);
if (verbose)
- Dumper<algorithm, mode>::dump_round_keys(encrypt);
+ dump_wrapper<algorithm, mode>(decrypt);
while (!ciphertexts.empty())
{
- typename aesni::aes::Types<algorithm>::BlockT ciphertext;
- aesni::aes::from_string(ciphertext, ciphertexts.front());
+ typename aesni::Types<algorithm>::Block ciphertext, plaintext;
+ aesni::from_string<algorithm>(ciphertext, ciphertexts.front());
ciphertexts.pop_front();
- const auto plaintext = encrypt.decrypt(ciphertext);
+ decrypt.decrypt_block(ciphertext, plaintext);
if (verbose)
{
- dump_ciphertext(ciphertext);
- dump_plaintext(plaintext);
- Dumper<algorithm, mode>::dump_next_iv(encrypt);
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_plaintext<algorithm>(plaintext);
+ dump_next_iv<algorithm, mode>(decrypt);
}
else
{
- std::cout << aesni::aes::to_string(plaintext) << "\n";
+ std::cout << aesni::to_string<algorithm>(plaintext) << "\n";
}
}
@@ -102,7 +102,7 @@ namespace
}
}
- bool decrypt(
+ bool decrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
const std::string& key_str,
@@ -125,32 +125,13 @@ namespace
}
}
- bool decrypt_using_boxes(
- aesni::Algorithm algorithm,
+ template <aesni::Algorithm algorithm>
+ bool decrypt_using_boxes_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
aesni::Mode mode,
const std::string& key,
std::deque<std::string> ciphertexts)
{
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (algorithm)
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, key);
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, key);
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, key);
- break;
-
- default:
- return false;
- }
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
@@ -159,7 +140,7 @@ namespace
if (ciphertexts.empty())
return false;
- aesni::aes::from_string(iv.aes_block, ciphertexts.front());
+ aesni::from_string<algorithm>(iv.aes_block, ciphertexts.front());
iv_ptr = &iv;
ciphertexts.pop_front();
}
@@ -176,7 +157,7 @@ namespace
while (!ciphertexts.empty())
{
AesNI_BoxBlock ciphertext;
- aesni::aes::from_string(ciphertext.aes_block, ciphertexts.front());
+ aesni::from_string<algorithm>(ciphertext.aes_block, ciphertexts.front());
ciphertexts.pop_front();
AesNI_BoxBlock plaintext;
@@ -186,11 +167,44 @@ namespace
&plaintext,
aesni::ErrorDetailsThrowsInDestructor());
- std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n";
+ std::cout << aesni::to_string<algorithm>(plaintext.aes_block) << "\n";
}
return true;
}
+
+ bool decrypt_using_boxes(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ const std::string& key,
+ std::deque<std::string> ciphertexts)
+ {
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, key, ciphertexts);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, key, ciphertexts);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, key);
+ return decrypt_using_boxes_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, key, ciphertexts);
+
+ default:
+ return false;
+ }
+ }
}
int main(int argc, char** argv)
@@ -228,7 +242,7 @@ int main(int argc, char** argv)
const auto success = cmd_parser.use_boxes()
? decrypt_using_boxes(algorithm, mode, key, ciphertexts)
- : decrypt(algorithm, mode, key, ciphertexts, cmd_parser.verbose());
+ : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose());
if (!success)
{
diff --git a/utils/aes_decrypt_bmp.cpp b/utils/aes_decrypt_bmp.cpp
index b9f669e..cf96847 100644
--- a/utils/aes_decrypt_bmp.cpp
+++ b/utils/aes_decrypt_bmp.cpp
@@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -58,72 +59,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_bmp_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- 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())
+ 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];
@@ -140,9 +97,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());
@@ -171,6 +128,63 @@ int main(int argc, char** argv)
dest_buf.resize(header_size + pixels_size);
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool decrypt_bmp(
+ 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_bmp_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_bmp_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_bmp_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_decrypt_bmp.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!decrypt_bmp(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)
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)
diff --git a/utils/aes_encrypt_block.cpp b/utils/aes_encrypt_block.cpp
index b814e99..1b6b788 100644
--- a/utils/aes_encrypt_block.cpp
+++ b/utils/aes_encrypt_block.cpp
@@ -25,47 +25,47 @@ namespace
std::deque<std::string>& plaintexts,
bool verbose = false)
{
- typename aesni::aes::Types<algorithm>::BlockT iv;
+ typename aesni::Types<algorithm>::Block iv;
- if (aesni::ModeRequiresInitializationVector<mode>())
+ if (aesni::ModeRequiresInitializationVector<mode>::value)
{
if (plaintexts.empty())
return false;
- aesni::aes::from_string(iv, plaintexts.front());
+ aesni::from_string<algorithm>(iv, plaintexts.front());
plaintexts.pop_front();
if (verbose)
- dump_iv(iv);
+ dump_iv<algorithm>(iv);
}
- typename aesni::aes::Types<algorithm>::KeyT key;
- aesni::aes::from_string(key, key_str);
+ typename aesni::Types<algorithm>::Key key;
+ aesni::from_string<algorithm>(key, key_str);
if (verbose)
- dump_key(key);
+ dump_key<algorithm>(key);
- aesni::aes::Encrypt<algorithm, mode> encrypt(key, iv);
+ aesni::EncryptWrapper<algorithm, mode> encrypt(key, iv);
if (verbose)
- Dumper<algorithm, mode>::dump_round_keys(encrypt);
+ dump_wrapper<algorithm, mode>(encrypt);
while (!plaintexts.empty())
{
- typename aesni::aes::Types<algorithm>::BlockT plaintext;
- aesni::aes::from_string(plaintext, plaintexts.front());
+ typename aesni::Types<algorithm>::Block plaintext, ciphertext;
+ aesni::from_string<algorithm>(plaintext, plaintexts.front());
plaintexts.pop_front();
- const auto ciphertext = encrypt.encrypt(plaintext);
+ encrypt.encrypt_block(plaintext, ciphertext);
if (verbose)
{
- dump_plaintext(plaintext);
- dump_ciphertext(ciphertext);
- Dumper<algorithm, mode>::dump_next_iv(encrypt);
+ dump_plaintext<algorithm>(plaintext);
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_next_iv<algorithm, mode>(encrypt);
}
else
{
- std::cout << aesni::aes::to_string(ciphertext) << "\n";
+ std::cout << aesni::to_string<algorithm>(ciphertext) << "\n";
}
}
@@ -101,11 +101,11 @@ namespace
}
}
- bool encrypt(
+ bool encrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
const std::string& key_str,
- std::deque<std::string> plaintexts,
+ std::deque<std::string>& plaintexts,
bool verbose = false)
{
switch (algorithm)
@@ -124,32 +124,13 @@ namespace
}
}
- bool encrypt_using_boxes(
- aesni::Algorithm algorithm,
+ template <aesni::Algorithm algorithm>
+ bool encrypt_using_boxes_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
aesni::Mode mode,
const std::string& key,
std::deque<std::string> plaintexts)
{
- AesNI_BoxAlgorithmParams algorithm_params;
-
- switch (algorithm)
- {
- case AESNI_AES128:
- aesni::aes::from_string(algorithm_params.aes128_key, key);
- break;
-
- case AESNI_AES192:
- aesni::aes::from_string(algorithm_params.aes192_key, key);
- break;
-
- case AESNI_AES256:
- aesni::aes::from_string(algorithm_params.aes256_key, key);
- break;
-
- default:
- return false;
- }
-
AesNI_BoxBlock iv;
AesNI_BoxBlock* iv_ptr = nullptr;
@@ -158,7 +139,7 @@ namespace
if (plaintexts.empty())
return false;
- aesni::aes::from_string(iv.aes_block, plaintexts.front());
+ aesni::from_string<AESNI_AES128>(iv.aes_block, plaintexts.front());
iv_ptr = &iv;
plaintexts.pop_front();
}
@@ -175,7 +156,7 @@ namespace
while (!plaintexts.empty())
{
AesNI_BoxBlock plaintext;
- aesni::aes::from_string(plaintext.aes_block, plaintexts.front());
+ aesni::from_string<algorithm>(plaintext.aes_block, plaintexts.front());
plaintexts.pop_front();
AesNI_BoxBlock ciphertext;
@@ -185,11 +166,44 @@ namespace
&ciphertext,
aesni::ErrorDetailsThrowsInDestructor());
- std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n";
+ std::cout << aesni::to_string<algorithm>(ciphertext.aes_block) << "\n";
}
return true;
}
+
+ bool encrypt_using_boxes(
+ aesni::Algorithm algorithm,
+ aesni::Mode mode,
+ const std::string& key,
+ std::deque<std::string> plaintexts)
+ {
+ AesNI_BoxAlgorithmParams algorithm_params;
+
+ switch (algorithm)
+ {
+ case AESNI_AES128:
+ aesni::from_string<AESNI_AES128>(
+ algorithm_params.aes128_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES128>(
+ algorithm_params, mode, key, plaintexts);
+
+ case AESNI_AES192:
+ aesni::from_string<AESNI_AES192>(
+ algorithm_params.aes192_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES192>(
+ algorithm_params, mode, key, plaintexts);
+
+ case AESNI_AES256:
+ aesni::from_string<AESNI_AES256>(
+ algorithm_params.aes256_key, key);
+ return encrypt_using_boxes_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, key, plaintexts);
+
+ default:
+ return false;
+ }
+ }
}
int main(int argc, char** argv)
@@ -227,7 +241,7 @@ int main(int argc, char** argv)
const auto success = cmd_parser.use_boxes()
? encrypt_using_boxes(algorithm, mode, key, plaintexts)
- : encrypt(algorithm, mode, key, plaintexts, cmd_parser.verbose());
+ : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose());
if (!success)
{
diff --git a/utils/aes_encrypt_bmp.cpp b/utils/aes_encrypt_bmp.cpp
index f36cfb0..2615889 100644
--- a/utils/aes_encrypt_bmp.cpp
+++ b/utils/aes_encrypt_bmp.cpp
@@ -17,6 +17,7 @@
#include <cstdlib>
#include <cstring>
+#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
@@ -58,72 +59,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 encrypt_bmp_with_algorithm(
+ const AesNI_BoxAlgorithmParams& algorithm_params,
+ aesni::Mode mode,
+ std::deque<std::string>& args)
{
- 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())
+ 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];
@@ -140,9 +97,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());
@@ -170,6 +127,63 @@ int main(int argc, char** argv)
write_file(dest_path, dest_buf);
+ return true;
+ }
+
+ bool encrypt_bmp(
+ 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 encrypt_bmp_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 encrypt_bmp_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 encrypt_bmp_with_algorithm<AESNI_AES256>(
+ algorithm_params, mode, args);
+
+ default:
+ return false;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ try
+ {
+ CommandLineParser cmd_parser("aes_encrypt_bmp.exe");
+
+ if (!cmd_parser.parse_options(argc, argv))
+ return 0;
+
+ if (!encrypt_bmp(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)
diff --git a/utils/aes_encrypt_file.cpp b/utils/aes_encrypt_file.cpp
index 93b7cbd..61b9ba1 100644
--- a/utils/aes_encrypt_file.cpp
+++ b/utils/aes_encrypt_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 encrypt_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 true;
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 encrypt_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 encrypt_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 encrypt_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 encrypt_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 (!encrypt_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)