aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/toolkit.py4
-rw-r--r--utils/block_cmd_parser.hpp90
-rw-r--r--utils/block_dumper.hpp29
-rw-r--r--utils/block_input.hpp36
-rw-r--r--utils/data_parsers.hpp6
-rw-r--r--utils/decrypt_block.cpp22
-rw-r--r--utils/encrypt_block.cpp14
-rw-r--r--utils/file_cmd_parser.hpp14
8 files changed, 98 insertions, 117 deletions
diff --git a/test/toolkit.py b/test/toolkit.py
index bc27dc8..f8a74ba 100644
--- a/test/toolkit.py
+++ b/test/toolkit.py
@@ -138,8 +138,8 @@ class Tools:
'--algorithm', str(algorithm),
'--mode', str(mode),
'--key', key,
- '--input-path', input_path,
- '--output-path', output_path
+ '--input', input_path,
+ '--output', output_path
]
if iv is not None:
args.extend(('--iv', iv))
diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp
index 70452e3..9c9d61c 100644
--- a/utils/block_cmd_parser.hpp
+++ b/utils/block_cmd_parser.hpp
@@ -13,8 +13,8 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
-#include <iterator>
#include <deque>
+#include <iterator>
#include <ostream>
#include <string>
#include <utility>
@@ -86,9 +86,9 @@ namespace
po::notify(vm);
- parse_inputs(settings, inputs, std::deque<std::string>(
+ inputs = parse_inputs(settings, std::deque<std::string>{
std::make_move_iterator(args.begin()),
- std::make_move_iterator(args.end())));
+ std::make_move_iterator(args.end())});
return settings;
}
@@ -96,57 +96,55 @@ namespace
bool exit_with_usage() const { return help_flag; }
private:
- static void parse_inputs(
+ static std::vector<Input> parse_inputs(
const Settings& settings,
- std::vector<Input>& inputs,
std::deque<std::string>&& args)
{
+ std::vector<Input> inputs;
while (!args.empty())
+ inputs.emplace_back(parse_input(settings, args));
+ return inputs;
+ }
+
+ static Input parse_input(
+ const Settings& settings,
+ std::deque<std::string>& args)
+ {
+ std::string key{std::move(args.front())};
+ args.pop_front();
+
+ std::string iv;
+
+ if (aes::mode_requires_init_vector(settings.mode))
{
- auto key_string = std::move(args.front());
+ if (args.empty())
+ throw boost::program_options::error{"an initialization vector is required for the selected mode of operation"};
+ iv = std::move(args.front());
args.pop_front();
+ }
- std::string iv_string;
-
- if (aes::mode_requires_init_vector(settings.mode))
- {
- if (args.empty())
- {
- throw boost::program_options::error(
- "an initialization vector is required for the selected mode of operation");
- }
- iv_string = std::move(args.front());
- args.pop_front();
- }
-
- std::vector<std::string> input_block_strings;
-
- while (!args.empty())
- {
- if (args.front() == "--")
- {
- args.pop_front();
- break;
- }
-
- input_block_strings.emplace_back(std::move(args.front()));
- args.pop_front();
- }
-
- if (aes::mode_requires_init_vector(settings.mode))
- {
- inputs.emplace_back(
- std::move(key_string),
- std::move(iv_string),
- std::move(input_block_strings));
- }
- else
- {
- inputs.emplace_back(
- std::move(key_string),
- std::move(input_block_strings));
- }
+ auto blocks = parse_blocks(args);
+
+ if (aes::mode_requires_init_vector(settings.mode))
+ return {key, iv, std::move(blocks)};
+ else
+ return {key, std::move(blocks)};
+ }
+
+ static std::vector<std::string> parse_blocks(std::deque<std::string>& args)
+ {
+ std::vector<std::string> blocks;
+
+ while (!args.empty())
+ {
+ std::string block{std::move(args.front())};
+ args.pop_front();
+ if (block == "--")
+ break;
+ blocks.emplace_back(std::move(block));
}
+
+ return blocks;
}
const std::string prog_name;
diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp
index df5b818..ff28c75 100644
--- a/utils/block_dumper.hpp
+++ b/utils/block_dumper.hpp
@@ -15,9 +15,12 @@
namespace
{
template <aes::Algorithm algorithm>
- void dump_block(const char* name, const typename aes::Types<algorithm>::Block& block)
+ void dump_block(
+ const char* header,
+ const typename aes::Types<algorithm>::Block& block)
{
- std::cout << name << ": " << aes::to_string<algorithm>(block) << "\n" << aes::to_matrix_string<algorithm>(block) << "\n";
+ std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n";
+ std::cout << aes::to_matrix_string<algorithm>(block) << "\n";
}
template <aes::Algorithm algorithm>
@@ -45,9 +48,9 @@ namespace
}
template <aes::Algorithm algorithm>
- void dump_round_keys(const char* name, const typename aes::Types<algorithm>::RoundKeys& round_keys)
+ void dump_round_keys(const char* header, const typename aes::Types<algorithm>::RoundKeys& round_keys)
{
- std::cout << name << ":\n";
+ std::cout << header << ":\n";
for (std::size_t i = 0; i < aes::get_number_of_rounds<algorithm>(); ++i)
std::cout << "\t[" << i << "]: " << aes::to_string<algorithm>(round_keys.keys[i]) << "\n";
std::cout << "\n";
@@ -66,40 +69,34 @@ namespace
}
template <aes::Algorithm algorithm, aes::Mode mode>
- void dump_wrapper(
- const aes::EncryptWrapper<algorithm, mode>& wrapper)
+ void dump_wrapper(const aes::EncryptWrapper<algorithm, mode>& wrapper)
{
dump_encryption_keys<algorithm>(wrapper.encryption_keys);
}
template <aes::Algorithm algorithm, aes::Mode mode>
- void dump_wrapper(
- const aes::DecryptWrapper<algorithm, mode>& wrapper)
+ void dump_wrapper(const aes::DecryptWrapper<algorithm, mode>& wrapper)
{
dump_decryption_keys<algorithm>(wrapper.decryption_keys);
}
template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr>
- void dump_next_iv(
- const aes::EncryptWrapper<algorithm, mode>& wrapper)
+ void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>& wrapper)
{
dump_block<algorithm>("Next initialization vector", wrapper.iv);
}
template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr>
- void dump_next_iv(
- const aes::EncryptWrapper<algorithm, mode>&)
+ void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>&)
{ }
template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr>
- void dump_next_iv(
- const aes::DecryptWrapper<algorithm, mode>& wrapper)
+ void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>& wrapper)
{
dump_block<algorithm>("Next initialization vector", wrapper.iv);
}
template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr>
- void dump_next_iv(
- const aes::DecryptWrapper<algorithm, mode>&)
+ void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&)
{ }
}
diff --git a/utils/block_input.hpp b/utils/block_input.hpp
index 0bcabbb..549e3f2 100644
--- a/utils/block_input.hpp
+++ b/utils/block_input.hpp
@@ -6,6 +6,7 @@
#pragma once
#include <string>
+#include <utility>
#include <vector>
namespace
@@ -13,34 +14,19 @@ namespace
class Input
{
public:
- Input(
- const std::string& key_string,
- const std::string& iv_string,
- const std::vector<std::string>& input_block_strings)
- : key_string(key_string)
- , iv_string(iv_string)
- , input_block_strings(input_block_strings)
+ Input(const std::string& key, const std::string& iv, std::vector<std::string>&& blocks)
+ : key{key}
+ , iv{iv}
+ , blocks{std::move(blocks)}
{ }
- Input(
- const std::string& key_string,
- const std::vector<std::string>& input_block_strings)
- : key_string(key_string)
- , input_block_strings(input_block_strings)
+ Input(const std::string& key, std::vector<std::string>&& blocks)
+ : key{key}
+ , blocks{std::move(blocks)}
{ }
- const std::string& get_key_string() const { return key_string; }
-
- const std::string& get_iv_string() const { return iv_string; }
-
- const std::vector<std::string>& get_input_block_strings() const
- {
- return input_block_strings;
- }
-
- private:
- const std::string key_string;
- const std::string iv_string;
- const std::vector<std::string> input_block_strings;
+ const std::string key;
+ const std::string iv;
+ const std::vector<std::string> blocks;
};
}
diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp
index 33c766a..e4f0ae2 100644
--- a/utils/data_parsers.hpp
+++ b/utils/data_parsers.hpp
@@ -11,15 +11,15 @@
#include <boost/program_options.hpp>
#include <istream>
-#include <map>
#include <string>
+#include <unordered_map>
static std::istream& operator>>(std::istream& is, aes::Mode& dest)
{
std::string src;
is >> src;
- static std::map<std::string, aes::Mode> lookup_table =
+ static const std::unordered_map<std::string, aes::Mode> lookup_table =
{
{"ecb", AES_ECB},
{"cbc", AES_CBC},
@@ -42,7 +42,7 @@ static std::istream& operator>>(std::istream& is, aes::Algorithm& dest)
std::string src;
is >> src;
- static std::map<std::string, aes::Algorithm> lookup_table =
+ static const std::unordered_map<std::string, aes::Algorithm> lookup_table =
{
{"aes128", AES_AES128},
{"aes192", AES_AES192},
diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp
index 4744d49..34f8abe 100644
--- a/utils/decrypt_block.cpp
+++ b/utils/decrypt_block.cpp
@@ -27,13 +27,13 @@ namespace
if (aes::ModeRequiresInitVector<mode>())
{
- aes::from_string<algorithm>(iv, input.get_iv_string());
+ aes::from_string<algorithm>(iv, input.iv);
if (verbose)
dump_iv<algorithm>(iv);
}
typename aes::Types<algorithm>::Key key;
- aes::from_string<algorithm>(key, input.get_key_string());
+ aes::from_string<algorithm>(key, input.key);
if (verbose)
dump_key<algorithm>(key);
@@ -41,10 +41,10 @@ namespace
if (verbose)
dump_wrapper<algorithm, mode>(decrypt);
- for (const auto& input_block_string : input.get_input_block_strings())
+ for (const auto& block : input.blocks)
{
typename aes::Types<algorithm>::Block ciphertext, plaintext;
- aes::from_string<algorithm>(ciphertext, input_block_string);
+ aes::from_string<algorithm>(ciphertext, block);
decrypt.decrypt_block(ciphertext, plaintext);
@@ -123,12 +123,12 @@ namespace
void decrypt_using_particular_box(
aes::Box& box,
- const std::vector<std::string>& input_block_strings)
+ const std::vector<std::string>& blocks)
{
- for (const auto& input_block_string : input_block_strings)
+ for (const auto& block : blocks)
{
aes::Box::Block ciphertext;
- box.parse_block(ciphertext, input_block_string);
+ box.parse_block(ciphertext, block);
aes::Box::Block plaintext;
box.decrypt_block(ciphertext, plaintext);
@@ -142,20 +142,20 @@ namespace
const Input& input)
{
aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, input.get_key_string());
+ aes::Box::parse_key(key, algorithm, input.key);
if (aes::mode_requires_init_vector(mode))
{
aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, input.get_iv_string());
+ aes::Box::parse_block(iv, algorithm, input.iv);
aes::Box box{algorithm, key, mode, iv};
- decrypt_using_particular_box(box, input.get_input_block_strings());
+ decrypt_using_particular_box(box, input.blocks);
}
else
{
aes::Box box{algorithm, key};
- decrypt_using_particular_box(box, input.get_input_block_strings());
+ decrypt_using_particular_box(box, input.blocks);
}
}
}
diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp
index 340e18b..5c02d92 100644
--- a/utils/encrypt_block.cpp
+++ b/utils/encrypt_block.cpp
@@ -27,13 +27,13 @@ namespace
if (aes::ModeRequiresInitVector<mode>::value)
{
- aes::from_string<algorithm>(iv, input.get_iv_string());
+ aes::from_string<algorithm>(iv, input.iv);
if (verbose)
dump_iv<algorithm>(iv);
}
typename aes::Types<algorithm>::Key key;
- aes::from_string<algorithm>(key, input.get_key_string());
+ aes::from_string<algorithm>(key, input.key);
if (verbose)
dump_key<algorithm>(key);
@@ -41,7 +41,7 @@ namespace
if (verbose)
dump_wrapper<algorithm, mode>(encrypt);
- for (const auto& input_block_string : input.get_input_block_strings())
+ for (const auto& input_block_string : input.blocks)
{
typename aes::Types<algorithm>::Block plaintext, ciphertext;
aes::from_string<algorithm>(plaintext, input_block_string);
@@ -142,20 +142,20 @@ namespace
const Input& input)
{
aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, input.get_key_string());
+ aes::Box::parse_key(key, algorithm, input.key);
if (aes::mode_requires_init_vector(mode))
{
aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, input.get_iv_string());
+ aes::Box::parse_block(iv, algorithm, input.iv);
aes::Box box{algorithm, key, mode, iv};
- encrypt_using_particular_box(box, input.get_input_block_strings());
+ encrypt_using_particular_box(box, input.blocks);
}
else
{
aes::Box box{algorithm, key};
- encrypt_using_particular_box(box, input.get_input_block_strings());
+ encrypt_using_particular_box(box, input.blocks);
}
}
}
diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp
index 0a6423f..e9cdaae 100644
--- a/utils/file_cmd_parser.hpp
+++ b/utils/file_cmd_parser.hpp
@@ -52,13 +52,13 @@ namespace
namespace po = boost::program_options;
options.add_options()
- ("help,h", "show this message and exit")
- ("mode,m", po::value<aes::Mode>(&settings.mode)->required(), "set mode of operation")
- ("algorithm,a", po::value<aes::Algorithm>(&settings.algorithm)->required(), "set algorithm")
- ("input-path,i", po::value<std::string>(&settings.input_path)->required(), "set input file path")
- ("output-path,o", po::value<std::string>(&settings.output_path)->required(), "set output file path")
- ("key,k", po::value<std::string>(&settings.key)->required(), "set encryption key")
- ("iv,v", po::value<std::string>(&settings.iv), "set initialization vector");
+ ("help,h", "show this message and exit")
+ ("mode,m", po::value<aes::Mode>(&settings.mode)->required(), "set mode of operation")
+ ("algorithm,a", po::value<aes::Algorithm>(&settings.algorithm)->required(), "set algorithm")
+ ("input,i", po::value<std::string>(&settings.input_path)->required(), "set input file path")
+ ("output,o", po::value<std::string>(&settings.output_path)->required(), "set output file path")
+ ("key,k", po::value<std::string>(&settings.key)->required(), "set encryption key")
+ ("iv,v", po::value<std::string>(&settings.iv), "set initialization vector");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);