diff options
Diffstat (limited to '')
-rw-r--r-- | utils/block_cmd_parser.hpp | 43 | ||||
-rw-r--r-- | utils/block_dumper.hpp | 8 | ||||
-rw-r--r-- | utils/data_parsers.hpp | 16 | ||||
-rw-r--r-- | utils/decrypt_block.cpp | 21 | ||||
-rw-r--r-- | utils/decrypt_bmp.cpp | 22 | ||||
-rw-r--r-- | utils/decrypt_file.cpp | 24 | ||||
-rw-r--r-- | utils/encrypt_block.cpp | 21 | ||||
-rw-r--r-- | utils/encrypt_bmp.cpp | 24 | ||||
-rw-r--r-- | utils/encrypt_file.cpp | 24 | ||||
-rw-r--r-- | utils/file_cmd_parser.hpp | 45 |
10 files changed, 112 insertions, 136 deletions
diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index ef92dce..70452e3 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -27,18 +27,14 @@ namespace class Settings { public: - aes::Algorithm get_algorithm() const { return algorithm; } - aes::Mode get_mode() const { return mode; } + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - bool use_boxes() const { return use_boxes_flag; } - bool verbose() const { return verbose_flag; } + bool use_boxes = false; + bool verbose = false; private: - aes::Algorithm algorithm; - aes::Mode mode; - - bool use_boxes_flag = false; - bool verbose_flag = false; + Settings() = default; friend class CommandLineParser; }; @@ -46,21 +42,23 @@ namespace class CommandLineParser { public: - CommandLineParser(const std::string& argv0) - : prog_name(boost::filesystem::path(argv0).filename().string()) - , options("Options") + explicit CommandLineParser(const std::string& argv0) + : prog_name{boost::filesystem::path{argv0}.filename().string()} + , options{"Options"} { } - void parse(Settings& settings, int argc, char** argv, std::vector<Input>& inputs) + Settings parse(int argc, char** argv, std::vector<Input>& inputs) { + Settings settings; + namespace po = boost::program_options; options.add_options() - ("help,h", "show this message and exit") - ("use-boxes,b", po::bool_switch(&settings.use_boxes_flag)->default_value(false), "use the \"boxes\" interface") - ("mode,m", po::value<aes::Mode>(&settings.mode)->required(), "set mode of operation") + ("help,h", "show this message and exit") + ("use-boxes,b", po::bool_switch(&settings.use_boxes)->default_value(false), "use the \"boxes\" interface") + ("mode,m", po::value<aes::Mode>(&settings.mode)->required(), "set mode of operation") ("algorithm,a", po::value<aes::Algorithm>(&settings.algorithm)->required(), "set algorithm") - ("verbose,v", po::bool_switch(&settings.verbose_flag)->default_value(false), "enable verbose output"); + ("verbose,v", po::bool_switch(&settings.verbose)->default_value(false), "enable verbose output"); std::vector<std::string> args; @@ -83,15 +81,16 @@ namespace if (vm.count("help")) { help_flag = true; - return; + return settings; } po::notify(vm); parse_inputs(settings, inputs, std::deque<std::string>( std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end()) - )); + std::make_move_iterator(args.end()))); + + return settings; } bool exit_with_usage() const { return help_flag; } @@ -109,7 +108,7 @@ namespace std::string iv_string; - if (aes::mode_requires_initialization_vector(settings.get_mode())) + if (aes::mode_requires_init_vector(settings.mode)) { if (args.empty()) { @@ -134,7 +133,7 @@ namespace args.pop_front(); } - if (aes::mode_requires_initialization_vector(settings.get_mode())) + if (aes::mode_requires_init_vector(settings.mode)) { inputs.emplace_back( std::move(key_string), diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index 91ad2c6..df5b818 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -79,26 +79,26 @@ namespace dump_decryption_keys<algorithm>(wrapper.decryption_keys); } - template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitializationVector<mode>::value>::type* = nullptr> + 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) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } - template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitializationVector<mode>::value>::type* = nullptr> + 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>&) { } - template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitializationVector<mode>::value>::type* = nullptr> + 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) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } - template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitializationVector<mode>::value>::type* = nullptr> + 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>&) { } diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp index 89270fe..33c766a 100644 --- a/utils/data_parsers.hpp +++ b/utils/data_parsers.hpp @@ -21,11 +21,11 @@ static std::istream& operator>>(std::istream& is, aes::Mode& dest) static std::map<std::string, aes::Mode> lookup_table = { - { "ecb", AES_ECB }, - { "cbc", AES_CBC }, - { "cfb", AES_CFB }, - { "ofb", AES_OFB }, - { "ctr", AES_CTR }, + {"ecb", AES_ECB}, + {"cbc", AES_CBC}, + {"cfb", AES_CFB}, + {"ofb", AES_OFB}, + {"ctr", AES_CTR}, }; const auto it = lookup_table.find(boost::algorithm::to_lower_copy(src)); @@ -44,9 +44,9 @@ static std::istream& operator>>(std::istream& is, aes::Algorithm& dest) static std::map<std::string, aes::Algorithm> lookup_table = { - { "aes128", AES_AES128 }, - { "aes192", AES_AES192 }, - { "aes256", AES_AES256 }, + {"aes128", AES_AES128}, + {"aes192", AES_AES192}, + {"aes256", AES_AES256}, }; const auto it = lookup_table.find(boost::algorithm::to_lower_copy(src)); diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index 9e53d2b..4744d49 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -25,7 +25,7 @@ namespace { typename aes::Types<algorithm>::Block iv; - if (aes::ModeRequiresInitializationVector<mode>()) + if (aes::ModeRequiresInitVector<mode>()) { aes::from_string<algorithm>(iv, input.get_iv_string()); if (verbose) @@ -144,7 +144,7 @@ namespace aes::Box::Key key; aes::Box::parse_key(key, algorithm, input.get_key_string()); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; aes::Box::parse_block(iv, algorithm, input.get_iv_string()); @@ -164,12 +164,11 @@ int main(int argc, char** argv) { try { - CommandLineParser cmd_parser(argv[0]); + CommandLineParser cmd_parser{argv[0]}; try { - Settings settings; std::vector<Input> inputs; - cmd_parser.parse(settings, argc, argv, inputs); + const auto settings = cmd_parser.parse(argc, argv, inputs); if (cmd_parser.exit_with_usage()) { @@ -179,20 +178,20 @@ int main(int argc, char** argv) for (const auto& input : inputs) { - if (settings.use_boxes()) + if (settings.use_boxes) { decrypt_using_boxes( - settings.get_algorithm(), - settings.get_mode(), + settings.algorithm, + settings.mode, input); } else { decrypt_using_cxx_api( - settings.get_algorithm(), - settings.get_mode(), + settings.algorithm, + settings.mode, input, - settings.verbose()); + settings.verbose); } } } diff --git a/utils/decrypt_bmp.cpp b/utils/decrypt_bmp.cpp index 17a64dd..a68b1d3 100644 --- a/utils/decrypt_bmp.cpp +++ b/utils/decrypt_bmp.cpp @@ -33,27 +33,24 @@ namespace void decrypt_bmp(const Settings& settings) { - const auto algorithm = settings.get_algorithm(); - const auto mode = settings.get_mode(); - - const auto& ciphertext_path = settings.get_input_path(); - const auto& plaintext_path = settings.get_output_path(); + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key_string()); + aes::Box::parse_key(key, algorithm, settings.key); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.get_iv_string()); - aes::Box box{algorithm, key, mode, iv}; + aes::Box::parse_block(iv, algorithm, settings.iv); - decrypt_bmp(box, ciphertext_path, plaintext_path); + aes::Box box{algorithm, key, mode, iv}; + decrypt_bmp(box, settings.input_path, settings.output_path); } else { aes::Box box{algorithm, key}; - decrypt_bmp(box, ciphertext_path, plaintext_path); + decrypt_bmp(box, settings.input_path, settings.output_path); } } } @@ -65,8 +62,7 @@ int main(int argc, char** argv) CommandLineParser cmd_parser(argv[0]); try { - Settings settings; - cmd_parser.parse(settings, argc, argv); + const auto settings = cmd_parser.parse(argc, argv); if (cmd_parser.exit_with_usage()) { diff --git a/utils/decrypt_file.cpp b/utils/decrypt_file.cpp index 75f8fb8..644447c 100644 --- a/utils/decrypt_file.cpp +++ b/utils/decrypt_file.cpp @@ -30,27 +30,24 @@ namespace void decrypt_file(const Settings& settings) { - const auto algorithm = settings.get_algorithm(); - const auto mode = settings.get_mode(); - - const auto& ciphertext_path = settings.get_input_path(); - const auto& plaintext_path = settings.get_output_path(); + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key_string()); + aes::Box::parse_key(key, algorithm, settings.key); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.get_iv_string()); - aes::Box box{algorithm, key, mode, iv}; + aes::Box::parse_block(iv, algorithm, settings.iv); - decrypt_file(box, ciphertext_path, plaintext_path); + aes::Box box{algorithm, key, mode, iv}; + decrypt_file(box, settings.input_path, settings.output_path); } else { aes::Box box{algorithm, key}; - decrypt_file(box, ciphertext_path, plaintext_path); + decrypt_file(box, settings.input_path, settings.output_path); } } } @@ -59,11 +56,10 @@ int main(int argc, char** argv) { try { - CommandLineParser cmd_parser(argv[0]); + CommandLineParser cmd_parser{argv[0]}; try { - Settings settings; - cmd_parser.parse(settings, argc, argv); + const auto settings = cmd_parser.parse(argc, argv); if (cmd_parser.exit_with_usage()) { diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp index 714dab7..340e18b 100644 --- a/utils/encrypt_block.cpp +++ b/utils/encrypt_block.cpp @@ -25,7 +25,7 @@ namespace { typename aes::Types<algorithm>::Block iv; - if (aes::ModeRequiresInitializationVector<mode>::value) + if (aes::ModeRequiresInitVector<mode>::value) { aes::from_string<algorithm>(iv, input.get_iv_string()); if (verbose) @@ -144,7 +144,7 @@ namespace aes::Box::Key key; aes::Box::parse_key(key, algorithm, input.get_key_string()); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; aes::Box::parse_block(iv, algorithm, input.get_iv_string()); @@ -164,12 +164,11 @@ int main(int argc, char** argv) { try { - CommandLineParser cmd_parser(argv[0]); + CommandLineParser cmd_parser{argv[0]}; try { std::vector<Input> inputs; - Settings settings; - cmd_parser.parse(settings, argc, argv, inputs); + const auto settings = cmd_parser.parse(argc, argv, inputs); if (cmd_parser.exit_with_usage()) { @@ -179,20 +178,20 @@ int main(int argc, char** argv) for (const auto& input : inputs) { - if (settings.use_boxes()) + if (settings.use_boxes) { encrypt_using_boxes( - settings.get_algorithm(), - settings.get_mode(), + settings.algorithm, + settings.mode, input); } else { encrypt_using_cxx_api( - settings.get_algorithm(), - settings.get_mode(), + settings.algorithm, + settings.mode, input, - settings.verbose()); + settings.verbose); } } } diff --git a/utils/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp index ac6cb92..ad61266 100644 --- a/utils/encrypt_bmp.cpp +++ b/utils/encrypt_bmp.cpp @@ -31,27 +31,24 @@ namespace void encrypt_bmp(const Settings& settings) { - const auto algorithm = settings.get_algorithm(); - const auto mode = settings.get_mode(); - - const auto& plaintext_path = settings.get_input_path(); - const auto& ciphertext_path = settings.get_output_path(); + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key_string()); + aes::Box::parse_key(key, algorithm, settings.key); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.get_iv_string()); - aes::Box box{algorithm, key, mode, iv}; + aes::Box::parse_block(iv, algorithm, settings.iv); - encrypt_bmp(box, plaintext_path, ciphertext_path); + aes::Box box{algorithm, key, mode, iv}; + encrypt_bmp(box, settings.input_path, settings.output_path); } else { aes::Box box{algorithm, key}; - encrypt_bmp(box, plaintext_path, ciphertext_path); + encrypt_bmp(box, settings.input_path, settings.output_path); } } } @@ -60,11 +57,10 @@ int main(int argc, char** argv) { try { - CommandLineParser cmd_parser(argv[0]); + CommandLineParser cmd_parser{argv[0]}; try { - Settings settings; - cmd_parser.parse(settings, argc, argv); + const auto settings = cmd_parser.parse(argc, argv); if (cmd_parser.exit_with_usage()) { diff --git a/utils/encrypt_file.cpp b/utils/encrypt_file.cpp index 9a4ee5c..007ed18 100644 --- a/utils/encrypt_file.cpp +++ b/utils/encrypt_file.cpp @@ -30,27 +30,24 @@ namespace void encrypt_file(const Settings& settings) { - const auto algorithm = settings.get_algorithm(); - const auto mode = settings.get_mode(); - - const auto& plaintext_path = settings.get_input_path(); - const auto& ciphertext_path = settings.get_output_path(); + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.get_key_string()); + aes::Box::parse_key(key, algorithm, settings.key); - if (aes::mode_requires_initialization_vector(mode)) + if (aes::mode_requires_init_vector(mode)) { aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.get_iv_string()); - aes::Box box{algorithm, key, mode, iv}; + aes::Box::parse_block(iv, algorithm, settings.iv); - encrypt_file(box, plaintext_path, ciphertext_path); + aes::Box box{algorithm, key, mode, iv}; + encrypt_file(box, settings.input_path, settings.output_path); } else { aes::Box box{algorithm, key}; - encrypt_file(box, plaintext_path, ciphertext_path); + encrypt_file(box, settings.input_path, settings.output_path); } } } @@ -59,11 +56,10 @@ int main(int argc, char** argv) { try { - CommandLineParser cmd_parser(argv[0]); + CommandLineParser cmd_parser{argv[0]}; try { - Settings settings; - cmd_parser.parse(settings, argc, argv); + const auto settings = cmd_parser.parse(argc, argv); if (cmd_parser.exit_with_usage()) { diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index c755c7a..0a6423f 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -23,49 +23,42 @@ namespace class Settings { public: - Settings() = default; - - aes::Mode get_mode() const { return mode; } - aes::Algorithm get_algorithm() const { return algorithm; } - - const std::string& get_input_path() const { return input_path; } - const std::string& get_output_path() const { return output_path; } - - const std::string& get_key_string() const { return key; } - const std::string& get_iv_string() const { return iv; } - - private: - aes::Mode mode; - aes::Algorithm algorithm; + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; std::string input_path; std::string output_path; std::string key; std::string iv; + private: + Settings() = default; + friend class CommandLineParser; }; class CommandLineParser { public: - CommandLineParser(const std::string& argv0) - : prog_name(boost::filesystem::path(argv0).filename().string()) - , options("Options") + explicit CommandLineParser(const std::string& argv0) + : prog_name{boost::filesystem::path{argv0}.filename().string()} + , options{"Options"} { } - void parse(Settings& settings, int argc, char** argv) + Settings parse(int argc, char** argv) { + Settings settings; + 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") + ("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"); + ("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); @@ -73,12 +66,12 @@ namespace if (vm.count("help")) { help_flag = true; - return; + return settings; } po::notify(vm); - if (aes::mode_requires_initialization_vector(settings.get_mode())) + if (aes::mode_requires_init_vector(settings.mode)) { if (!vm.count("iv")) { @@ -86,6 +79,8 @@ namespace "an initialization vector is required for the selected mode of operation"); } } + + return settings; } bool exit_with_usage() const { return help_flag; } |