diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-25 19:56:14 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-25 19:56:14 +0300 |
commit | a151a774f4ce6c4b13c819502efcfe919bbf050b (patch) | |
tree | 5cbed50713e2ea176c04327560b51df65bcd8b54 /utils | |
parent | utils: code style (diff) | |
download | aes-tools-a151a774f4ce6c4b13c819502efcfe919bbf050b.tar.gz aes-tools-a151a774f4ce6c4b13c819502efcfe919bbf050b.zip |
code style
Diffstat (limited to 'utils')
-rw-r--r-- | utils/block_cmd_parser.hpp | 175 | ||||
-rw-r--r-- | utils/block_dumper.hpp | 169 | ||||
-rw-r--r-- | utils/block_input.hpp | 33 | ||||
-rw-r--r-- | utils/data_parsers.hpp | 4 | ||||
-rw-r--r-- | utils/file_cmd_parser.hpp | 117 |
5 files changed, 243 insertions, 255 deletions
diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index c748b3d..58f86d8 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -19,109 +19,106 @@ #include <utility> #include <vector> -namespace +class BlockSettings : public command_line::SettingsParser { - class BlockSettings : public command_line::SettingsParser - { - public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; +public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - bool use_boxes = false; - bool verbose = false; + bool use_boxes = false; + bool verbose = false; - std::vector<Input> inputs; + std::vector<Input> inputs; - explicit BlockSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("verbose,v", - boost::program_options::bool_switch(&verbose), - "enable verbose output") - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("use-boxes,b", - boost::program_options::bool_switch(&use_boxes), - "use the \"boxes\" interface"); - hidden.add_options() - ("args", - boost::program_options::value<std::vector<std::string>>(&args), - "shouldn't be visible"); - positional.add("args", -1); - } + explicit BlockSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("verbose,v", + boost::program_options::bool_switch(&verbose), + "enable verbose output") + ("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value<aes::Mode>(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("use-boxes,b", + boost::program_options::bool_switch(&use_boxes), + "use the \"boxes\" interface"); + hidden.add_options() + ("args", + boost::program_options::value<std::vector<std::string>>(&args), + "shouldn't be visible"); + positional.add("args", -1); + } + + const char* get_short_description() const override + { + return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-- KEY [IV] [BLOCK]...]..."; + } - const char* get_short_description() const override - { - return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-- KEY [IV] [BLOCK]...]..."; - } + void parse(int argc, char* argv[]) override + { + SettingsParser::parse(argc, argv); + parse_inputs(std::deque<std::string>{ + std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())}); + } + +private: + void parse_inputs(std::deque<std::string>&& src) + { + while (!src.empty()) + inputs.emplace_back(parse_input(src)); + } - void parse(int argc, char* argv[]) override - { - SettingsParser::parse(argc, argv); - parse_inputs(std::deque<std::string>{ - std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end())}); - } + Input parse_input(std::deque<std::string>& src) const + { + std::string key{std::move(src.front())}; + src.pop_front(); - private: - void parse_inputs(std::deque<std::string>&& src) - { - while (!src.empty()) - inputs.emplace_back(parse_input(src)); - } + std::string iv; - Input parse_input(std::deque<std::string>& src) const + if (aes::mode_requires_init_vector(mode)) { - std::string key{std::move(src.front())}; - src.pop_front(); - - std::string iv; - - if (aes::mode_requires_init_vector(mode)) + if (src.empty()) { - if (src.empty()) - { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; - } - iv = std::move(src.front()); - src.pop_front(); + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; } - - auto blocks = parse_blocks(src); - - if (aes::mode_requires_init_vector(mode)) - return {key, iv, std::move(blocks)}; - else - return {key, std::move(blocks)}; + iv = std::move(src.front()); + src.pop_front(); } - static std::vector<std::string> parse_blocks(std::deque<std::string>& src) - { - std::vector<std::string> blocks; + auto blocks = parse_blocks(src); - while (!src.empty()) - { - std::string block{std::move(src.front())}; - src.pop_front(); - if (block == "--") - break; - blocks.emplace_back(std::move(block)); - } + if (aes::mode_requires_init_vector(mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; + } + + static std::vector<std::string> parse_blocks(std::deque<std::string>& src) + { + std::vector<std::string> blocks; - return blocks; + while (!src.empty()) + { + std::string block{std::move(src.front())}; + src.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); } - std::vector<std::string> args; - }; -} + return blocks; + } + + std::vector<std::string> args; +}; diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index ff28c75..00f75f1 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -12,91 +12,88 @@ #include <iostream> #include <type_traits> -namespace +template <aes::Algorithm algorithm> +void dump_block( + const char* header, + const typename aes::Types<algorithm>::Block& block) { - template <aes::Algorithm algorithm> - void dump_block( - const char* header, - const typename aes::Types<algorithm>::Block& block) - { - std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n"; - std::cout << aes::to_matrix_string<algorithm>(block) << "\n"; - } - - template <aes::Algorithm algorithm> - void dump_plaintext(const typename aes::Types<algorithm>::Block& block) - { - dump_block<algorithm>("Plaintext", block); - } - - template <aes::Algorithm algorithm> - void dump_key(const typename aes::Types<algorithm>::Key& key) - { - std::cout << "Key: " << aes::to_string<algorithm>(key) << "\n\n"; - } - - template <aes::Algorithm algorithm> - void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext) - { - dump_block<algorithm>("Ciphertext", ciphertext); - } - - template <aes::Algorithm algorithm> - void dump_iv(const typename aes::Types<algorithm>::Block& iv) - { - dump_block<algorithm>("Initialization vector", iv); - } - - template <aes::Algorithm algorithm> - void dump_round_keys(const char* header, const typename aes::Types<algorithm>::RoundKeys& round_keys) - { - 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"; - } - - template <aes::Algorithm algorithm> - void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) - { - dump_round_keys<algorithm>("Encryption round keys", round_keys); - } - - template <aes::Algorithm algorithm> - void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) - { - dump_round_keys<algorithm>("Decryption round keys", round_keys); - } - - template <aes::Algorithm algorithm, aes::Mode mode> - 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) - { - 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) - { - 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>&) - { } - - 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::ModeRequiresInitVector<mode>::value>::type* = nullptr> - void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&) - { } + std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n"; + std::cout << aes::to_matrix_string<algorithm>(block) << "\n"; } + +template <aes::Algorithm algorithm> +void dump_plaintext(const typename aes::Types<algorithm>::Block& block) +{ + dump_block<algorithm>("Plaintext", block); +} + +template <aes::Algorithm algorithm> +void dump_key(const typename aes::Types<algorithm>::Key& key) +{ + std::cout << "Key: " << aes::to_string<algorithm>(key) << "\n\n"; +} + +template <aes::Algorithm algorithm> +void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext) +{ + dump_block<algorithm>("Ciphertext", ciphertext); +} + +template <aes::Algorithm algorithm> +void dump_iv(const typename aes::Types<algorithm>::Block& iv) +{ + dump_block<algorithm>("Initialization vector", iv); +} + +template <aes::Algorithm algorithm> +void dump_round_keys(const char* header, const typename aes::Types<algorithm>::RoundKeys& round_keys) +{ + 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"; +} + +template <aes::Algorithm algorithm> +void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) +{ + dump_round_keys<algorithm>("Encryption round keys", round_keys); +} + +template <aes::Algorithm algorithm> +void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) +{ + dump_round_keys<algorithm>("Decryption round keys", round_keys); +} + +template <aes::Algorithm algorithm, aes::Mode mode> +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) +{ + 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) +{ + 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>&) +{ } + +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::ModeRequiresInitVector<mode>::value>::type* = nullptr> +void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&) +{ } diff --git a/utils/block_input.hpp b/utils/block_input.hpp index 549e3f2..f629062 100644 --- a/utils/block_input.hpp +++ b/utils/block_input.hpp @@ -9,24 +9,21 @@ #include <utility> #include <vector> -namespace +class Input { - class Input - { - public: - Input(const std::string& key, const std::string& iv, std::vector<std::string>&& blocks) - : key{key} - , iv{iv} - , blocks{std::move(blocks)} - { } +public: + 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, std::vector<std::string>&& blocks) - : key{key} - , blocks{std::move(blocks)} - { } + Input(const std::string& key, std::vector<std::string>&& blocks) + : key{key} + , blocks{std::move(blocks)} + { } - const std::string key; - const std::string iv; - const std::vector<std::string> blocks; - }; -} + 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 e4f0ae2..1207c7c 100644 --- a/utils/data_parsers.hpp +++ b/utils/data_parsers.hpp @@ -14,7 +14,7 @@ #include <string> #include <unordered_map> -static std::istream& operator>>(std::istream& is, aes::Mode& dest) +inline std::istream& operator>>(std::istream& is, aes::Mode& dest) { std::string src; is >> src; @@ -37,7 +37,7 @@ static std::istream& operator>>(std::istream& is, aes::Mode& dest) return is; } -static std::istream& operator>>(std::istream& is, aes::Algorithm& dest) +inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest) { std::string src; is >> src; diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index 6d7a962..e199409 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -17,70 +17,67 @@ #include <string> #include <utility> -namespace +class FileSettings : public command_line::SettingsParser { - class FileSettings : public command_line::SettingsParser - { - public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; +public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - std::string input_path; - std::string output_path; - std::string key; - std::string iv; + std::string input_path; + std::string output_path; + std::string key; + std::string iv; - explicit FileSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("key,k", - boost::program_options::value<std::string>(&key) - ->required() - ->value_name("KEY"), - "set encryption key") - ("iv,v", - boost::program_options::value<std::string>(&iv) - ->value_name("BLOCK"), - "set initialization vector") - ("input,i", - boost::program_options::value<std::string>(&input_path) - ->required() - ->value_name("PATH"), - "set input file path") - ("output,o", - boost::program_options::value<std::string>(&output_path) - ->required() - ->value_name("PATH"), - "set output file path"); - } + explicit FileSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value<aes::Mode>(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("key,k", + boost::program_options::value<std::string>(&key) + ->required() + ->value_name("KEY"), + "set encryption key") + ("iv,v", + boost::program_options::value<std::string>(&iv) + ->value_name("BLOCK"), + "set initialization vector") + ("input,i", + boost::program_options::value<std::string>(&input_path) + ->required() + ->value_name("PATH"), + "set input file path") + ("output,o", + boost::program_options::value<std::string>(&output_path) + ->required() + ->value_name("PATH"), + "set output file path"); + } - const char* get_short_description() const override - { - return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-k|--key KEY] [-v|--iv BLOCK]" - " [-i|--input PATH] [-o|--output PATH]"; - } + const char* get_short_description() const override + { + return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-k|--key KEY] [-v|--iv BLOCK]" + " [-i|--input PATH] [-o|--output PATH]"; + } - void parse(int argc, char** argv) override - { - SettingsParser::parse(argc, argv); + void parse(int argc, char** argv) override + { + SettingsParser::parse(argc, argv); - if (aes::mode_requires_init_vector(mode) && iv.empty()) - { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; - } + if (aes::mode_requires_init_vector(mode) && iv.empty()) + { + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; } - }; -} + } +}; |