aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/encrypt_block.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/encrypt_block.cpp')
-rw-r--r--utils/encrypt_block.cpp118
1 files changed, 39 insertions, 79 deletions
diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp
index 022c237..724e8c1 100644
--- a/utils/encrypt_block.cpp
+++ b/utils/encrypt_block.cpp
@@ -13,7 +13,6 @@
#include <boost/program_options.hpp>
-#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
@@ -23,40 +22,32 @@ namespace
{
template <aesni::Algorithm algorithm, aesni::Mode mode>
void encrypt_with_mode(
- const std::string& key_str,
- std::deque<std::string>& plaintexts,
+ const Input& input,
bool verbose = false)
{
typename aesni::Types<algorithm>::Block iv;
if (aesni::ModeRequiresInitializationVector<mode>::value)
{
- if (plaintexts.empty())
- throw_iv_required();
-
- aesni::from_string<algorithm>(iv, plaintexts.front());
- plaintexts.pop_front();
-
+ aesni::from_string<algorithm>(iv, input.get_iv_string());
if (verbose)
dump_iv<algorithm>(iv);
}
typename aesni::Types<algorithm>::Key key;
- aesni::from_string<algorithm>(key, key_str);
-
+ aesni::from_string<algorithm>(key, input.get_key_string());
if (verbose)
dump_key<algorithm>(key);
aesni::EncryptWrapper<algorithm, mode> encrypt(key, iv);
-
if (verbose)
dump_wrapper<algorithm, mode>(encrypt);
- while (!plaintexts.empty())
+ for (const auto& input_block_string : input.get_input_block_strings())
{
typename aesni::Types<algorithm>::Block plaintext, ciphertext;
- aesni::from_string<algorithm>(plaintext, plaintexts.front());
- plaintexts.pop_front();
+ aesni::from_string<algorithm>(plaintext, input_block_string);
+
encrypt.encrypt_block(plaintext, ciphertext);
if (verbose)
@@ -67,7 +58,7 @@ namespace
}
else
{
- std::cout << aesni::to_string<algorithm>(ciphertext) << "\n";
+ std::cout << aesni::to_string<algorithm>(ciphertext) << '\n';
}
}
}
@@ -75,30 +66,29 @@ namespace
template <aesni::Algorithm algorithm>
void encrypt_with_algorithm(
aesni::Mode mode,
- const std::string& key_str,
- std::deque<std::string>& plaintexts,
+ const Input& input,
bool verbose = false)
{
switch (mode)
{
case AESNI_ECB:
- encrypt_with_mode<algorithm, AESNI_ECB>(key_str, plaintexts, verbose);
+ encrypt_with_mode<algorithm, AESNI_ECB>(input, verbose);
break;
case AESNI_CBC:
- encrypt_with_mode<algorithm, AESNI_CBC>(key_str, plaintexts, verbose);
+ encrypt_with_mode<algorithm, AESNI_CBC>(input, verbose);
break;
case AESNI_CFB:
- encrypt_with_mode<algorithm, AESNI_CFB>(key_str, plaintexts, verbose);
+ encrypt_with_mode<algorithm, AESNI_CFB>(input, verbose);
break;
case AESNI_OFB:
- encrypt_with_mode<algorithm, AESNI_OFB>(key_str, plaintexts, verbose);
+ encrypt_with_mode<algorithm, AESNI_OFB>(input, verbose);
break;
case AESNI_CTR:
- encrypt_with_mode<algorithm, AESNI_CTR>(key_str, plaintexts, verbose);
+ encrypt_with_mode<algorithm, AESNI_CTR>(input, verbose);
break;
default:
@@ -110,22 +100,21 @@ namespace
void encrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
- const std::string& key_str,
- std::deque<std::string>& plaintexts,
+ const Input& input,
bool verbose = false)
{
switch (algorithm)
{
case AESNI_AES128:
- encrypt_with_algorithm<AESNI_AES128>(mode, key_str, plaintexts, verbose);
+ encrypt_with_algorithm<AESNI_AES128>(mode, input, verbose);
break;
case AESNI_AES192:
- encrypt_with_algorithm<AESNI_AES192>(mode, key_str, plaintexts, verbose);
+ encrypt_with_algorithm<AESNI_AES192>(mode, input, verbose);
break;
case AESNI_AES256:
- encrypt_with_algorithm<AESNI_AES256>(mode, key_str, plaintexts, verbose);
+ encrypt_with_algorithm<AESNI_AES256>(mode, input, verbose);
break;
default:
@@ -136,47 +125,39 @@ namespace
void encrypt_using_particular_box(
aesni::Box& box,
- std::deque<std::string>& plaintexts)
+ const std::vector<std::string>& input_block_strings)
{
- while (!plaintexts.empty())
+ for (const auto& input_block_string : input_block_strings)
{
aesni::Box::Block plaintext;
- box.parse_block(
- plaintext, plaintexts.front());
- plaintexts.pop_front();
+ box.parse_block(plaintext, input_block_string);
aesni::Box::Block ciphertext;
box.encrypt_block(plaintext, ciphertext);
-
- std::cout << box.format_block(ciphertext) << "\n";
+ std::cout << box.format_block(ciphertext) << '\n';
}
}
void encrypt_using_boxes(
aesni::Algorithm algorithm,
aesni::Mode mode,
- const std::string& key_str,
- std::deque<std::string>& plaintexts)
+ const Input& input)
{
aesni::Box::Key key;
- aesni::Box::parse_key(key, algorithm, key_str);
+ aesni::Box::parse_key(key, algorithm, input.get_key_string());
if (aesni::mode_requires_initialization_vector(mode))
{
- if (plaintexts.empty())
- throw_iv_required();
-
aesni::Box::Block iv;
- aesni::Box::parse_block(iv, algorithm, plaintexts.front());
- plaintexts.pop_front();
+ aesni::Box::parse_block(iv, algorithm, input.get_iv_string());
encrypt_using_particular_box(
- aesni::Box(algorithm, key, mode, iv), plaintexts);
+ aesni::Box(algorithm, key, mode, iv), input.get_input_block_strings());
}
else
{
encrypt_using_particular_box(
- aesni::Box(algorithm, key), plaintexts);
+ aesni::Box(algorithm, key), input.get_input_block_strings());
}
}
}
@@ -188,53 +169,32 @@ int main(int argc, char** argv)
CommandLineParser cmd_parser(argv[0]);
try
{
- cmd_parser.parse(argc, argv);
+ std::vector<Input> inputs;
+ Settings settings;
+ cmd_parser.parse(settings, argc, argv, inputs);
- if (cmd_parser.requested_help())
+ if (cmd_parser.exit_with_usage())
{
std::cout << cmd_parser;
return 0;
}
- std::deque<std::string> args(
- std::make_move_iterator(cmd_parser.args.begin()),
- std::make_move_iterator(cmd_parser.args.end()));
-
- while (!args.empty())
+ for (const auto& input : inputs)
{
- const auto key = args.front();
- args.pop_front();
-
- std::deque<std::string> plaintexts;
-
- while (!args.empty())
- {
- if (args.front() == "--")
- {
- args.pop_front();
- break;
- }
-
- plaintexts.push_back(args.front());
- args.pop_front();
- }
-
- if (cmd_parser.use_boxes)
+ if (settings.use_boxes())
{
encrypt_using_boxes(
- cmd_parser.algorithm,
- cmd_parser.mode,
- key,
- plaintexts);
+ settings.get_algorithm(),
+ settings.get_mode(),
+ input);
}
else
{
encrypt_using_cxx_api(
- cmd_parser.algorithm,
- cmd_parser.mode,
- key,
- plaintexts,
- cmd_parser.verbose);
+ settings.get_algorithm(),
+ settings.get_mode(),
+ input,
+ settings.verbose());
}
}