diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-28 03:26:46 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-28 03:26:46 +0300 |
commit | 6fe0f155b0a0daa5affe1482da50cbe152358b07 (patch) | |
tree | a305e26159bcb266c02cd8e9f9292bad839bda79 | |
parent | utils: refactoring (diff) | |
download | aes-tools-6fe0f155b0a0daa5affe1482da50cbe152358b07.tar.gz aes-tools-6fe0f155b0a0daa5affe1482da50cbe152358b07.zip |
utils: interface update & refactoring
-rw-r--r-- | test/cavp.py | 4 | ||||
-rw-r--r-- | test/nist-sp-800-38a.py | 6 | ||||
-rw-r--r-- | test/toolkit.py | 4 | ||||
-rw-r--r-- | utils/CMakeLists.txt | 4 | ||||
-rw-r--r-- | utils/README.md | 4 | ||||
-rw-r--r-- | utils/block_cmd_parser.hpp | 67 | ||||
-rw-r--r-- | utils/block_input.hpp | 47 | ||||
-rw-r--r-- | utils/decrypt_block.cpp | 7 | ||||
-rw-r--r-- | utils/encrypt_block.cpp | 7 | ||||
-rw-r--r-- | utils/file_cmd_parser.hpp | 35 |
10 files changed, 88 insertions, 97 deletions
diff --git a/test/cavp.py b/test/cavp.py index fb719f4..01d1e8d 100644 --- a/test/cavp.py +++ b/test/cavp.py @@ -184,7 +184,7 @@ if __name__ == '__main__': help='set path to block encryption utilities') parser.add_argument('--sde', '-e', action='store_true', help='use Intel SDE to run *.exe files') - parser.add_argument('--box', '-b', action='store_true', + parser.add_argument('--use-boxes', '-b', action='store_true', help='use the "boxes" interface') parser.add_argument('--archive', '-a', default='KAT_AES.zip', help='set path of the archive with the test vectors') @@ -202,4 +202,4 @@ if __name__ == '__main__': logging.basicConfig(**logging_options) tools = toolkit.Tools(args.path, use_sde=args.sde) - _parse_archive_and_run_tests(tools, args.archive, use_boxes=args.box) + _parse_archive_and_run_tests(tools, args.archive, use_boxes=args.use_boxes) diff --git a/test/nist-sp-800-38a.py b/test/nist-sp-800-38a.py index 05f0a39..a096a42 100644 --- a/test/nist-sp-800-38a.py +++ b/test/nist-sp-800-38a.py @@ -181,7 +181,7 @@ if __name__ == '__main__': help='set path to block encryption utilities') parser.add_argument('--sde', '-e', action='store_true', help='use Intel SDE to run *.exe files') - parser.add_argument('--box', '-b', action='store_true', + parser.add_argument('--use-boxes', '-b', action='store_true', help='use the "boxes" interface') parser.add_argument('--log', '-l', help='set log file path') args = parser.parse_args() @@ -216,13 +216,13 @@ if __name__ == '__main__': mode = maybe_mode logging.info('Mode: ' + mode) try: - exit_codes.append(_run_encryption_tests(tools, algo, mode, use_boxes=args.box)) + exit_codes.append(_run_encryption_tests(tools, algo, mode, use_boxes=args.use_boxes)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) exit_codes.append(_TestExitCode.ERROR) try: - exit_codes.append(_run_decryption_tests(tools, algo, mode, use_boxes=args.box)) + exit_codes.append(_run_decryption_tests(tools, algo, mode, use_boxes=args.use_boxes)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) diff --git a/test/toolkit.py b/test/toolkit.py index 170bccf..8798e6d 100644 --- a/test/toolkit.py +++ b/test/toolkit.py @@ -128,7 +128,7 @@ class Tools: '--mode', mode, ] if use_boxes: - args.append('--box') + args.append('--use-boxes') if isinstance(inputs, collections.Iterable): args.extend(self._block_inputs_to_args(iter(inputs))) else: @@ -141,7 +141,7 @@ class Tools: '--mode', mode, ] if use_boxes: - args.append('--box') + args.append('--use-boxes') if isinstance(inputs, collections.Iterable): args.extend(self._block_inputs_to_args(iter(inputs))) else: diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 97ecfb6..0d57e27 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,11 +1,11 @@ find_package(Boost REQUIRED COMPONENTS filesystem program_options system) -add_executable(util_encrypt_block encrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp data_parsers.hpp) +add_executable(util_encrypt_block encrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp block_input.hpp data_parsers.hpp) target_include_directories(util_encrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) target_link_libraries(util_encrypt_block libaesnixx ${Boost_LIBRARIES}) set_target_properties(util_encrypt_block PROPERTIES OUTPUT_NAME encrypt_block) -add_executable(util_decrypt_block decrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp data_parsers.hpp) +add_executable(util_decrypt_block decrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp block_input.hpp data_parsers.hpp) target_include_directories(util_decrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) target_link_libraries(util_decrypt_block libaesnixx ${Boost_LIBRARIES}) set_target_properties(util_decrypt_block PROPERTIES OUTPUT_NAME decrypt_block) diff --git a/utils/README.md b/utils/README.md index 8055d87..a1cfeb5 100644 --- a/utils/README.md +++ b/utils/README.md @@ -19,8 +19,8 @@ The block encryption utilities can produce verbose human-readable output, including round keys, intermediate initialization vector values, etc. This is primarily intended for debugging purposes. Enable verbose output by passing the `--verbose` flag to the utilities. -Please note that verbose output can only be produced when *not* using "boxes" -(the `--box` flag). +Please note that verbose output can only be produced when *not* using the +"boxes" interface (the `--use-boxes` flag). ### encrypt_block.exe diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 988ef52..62b0f36 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -8,11 +8,11 @@ #pragma once +#include "block_input.hpp" #include "data_parsers.hpp" #include <aesnixx/all.hpp> -#include <boost/config.hpp> #include <boost/filesystem.hpp> #include <boost/program_options.hpp> @@ -25,66 +25,8 @@ namespace { - BOOST_NORETURN inline void throw_iv_required() - { - throw boost::program_options::error( - "initialization vector is required for the selected mode of operation"); - } - - BOOST_NORETURN inline void throw_not_implemented(aesni::Algorithm algorithm) - { - throw boost::program_options::error( - "the selected algorithm is not implemented"); - } - - BOOST_NORETURN inline void throw_not_implemented(aesni::Mode mode) - { - throw boost::program_options::error( - "the selected mode of operation is not implemented"); - } - class CommandLineParser; - class Input - { - public: - Input( - std::string&& key_string, - std::string&& iv_string, - std::vector<std::string>&& input_block_strings) - : key_string(std::move(key_string)) - , iv_string(std::make_pair<bool, std::string>(true, std::move(iv_string))) - , input_block_strings(input_block_strings) - { } - - Input( - std::string&& key_string, - std::vector<std::string>&& input_block_strings) - : key_string(std::move(key_string)) - , iv_string(std::make_pair<bool, std::string>(false, std::string())) - , input_block_strings(std::move(input_block_strings)) - { } - - const std::string& get_key_string() const { return key_string; } - - const std::string& get_iv_string() const - { - if (!iv_string.first) - throw_iv_required(); - return iv_string.second; - } - - const std::vector<std::string>& get_input_block_strings() const - { - return input_block_strings; - } - - private: - const std::string key_string; - const std::pair<bool, std::string> iv_string; - const std::vector<std::string> input_block_strings; - }; - class Settings { public: @@ -118,7 +60,7 @@ namespace options.add_options() ("help,h", "show this message and exit") - ("box,b", po::bool_switch(&settings.use_boxes_flag)->default_value(false), "use the \"boxes\" interface") + ("use-boxes,b", po::bool_switch(&settings.use_boxes_flag)->default_value(false), "use the \"boxes\" interface") ("mode,m", po::value<aesni::Mode>(&settings.mode)->required(), "set mode of operation") ("algorithm,a", po::value<aesni::Algorithm>(&settings.algorithm)->required(), "set algorithm") ("verbose,v", po::bool_switch(&settings.verbose_flag)->default_value(false), "enable verbose output"); @@ -173,7 +115,10 @@ namespace if (aesni::mode_requires_initialization_vector(settings.get_mode())) { if (args.empty()) - throw_iv_required(); + { + 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(); } diff --git a/utils/block_input.hpp b/utils/block_input.hpp new file mode 100644 index 0000000..343bb2a --- /dev/null +++ b/utils/block_input.hpp @@ -0,0 +1,47 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include <string> +#include <vector> + +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_string, + const std::vector<std::string>& input_block_strings) + : key_string(key_string) + , input_block_strings(input_block_strings) + { } + + 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; + }; +} diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index a8c9625..456ef6c 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -8,6 +8,7 @@ #include "block_cmd_parser.hpp" #include "block_dumper.hpp" +#include "block_input.hpp" #include <aesnixx/all.hpp> @@ -15,7 +16,7 @@ #include <exception> #include <iostream> -#include <iterator> +#include <stdexcept> #include <string> namespace @@ -92,7 +93,7 @@ namespace break; default: - throw_not_implemented(mode); + throw std::runtime_error("the selected mode of operation is not implemented"); break; } } @@ -118,7 +119,7 @@ namespace break; default: - throw_not_implemented(algorithm); + throw std::runtime_error("the selected algorithm is not implemented"); break; } } diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp index 724e8c1..98fdec6 100644 --- a/utils/encrypt_block.cpp +++ b/utils/encrypt_block.cpp @@ -8,6 +8,7 @@ #include "block_cmd_parser.hpp" #include "block_dumper.hpp" +#include "block_input.hpp" #include <aesnixx/all.hpp> @@ -15,7 +16,7 @@ #include <exception> #include <iostream> -#include <iterator> +#include <stdexcept> #include <string> namespace @@ -92,7 +93,7 @@ namespace break; default: - throw_not_implemented(mode); + throw std::runtime_error("the selected mode of operation is not implemented"); break; } } @@ -118,7 +119,7 @@ namespace break; default: - throw_not_implemented(algorithm); + throw std::runtime_error("the selected algorithm is not implemented"); break; } } diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index 7d1470d..b6312f3 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -26,24 +26,16 @@ namespace class Settings { public: - Settings() - : iv(false, std::string()) - { } + Settings() = default; aesni::Mode get_mode() const { return mode; } aesni::Algorithm get_algorithm() const { return algorithm; } - std::string get_input_path() const { return input_path; } - std::string get_output_path() const { return output_path; } + const std::string& get_input_path() const { return input_path; } + const std::string& get_output_path() const { return output_path; } - std::string get_key_string() const { return key; } - - std::string get_iv_string() const - { - if (!iv.first) - throw boost::program_options::error("initialization vector is required for the selected mode of operation"); - return iv.second; - } + const std::string& get_key_string() const { return key; } + const std::string& get_iv_string() const { return iv; } private: aesni::Mode mode; @@ -52,8 +44,7 @@ namespace std::string input_path; std::string output_path; std::string key; - - std::pair<bool, std::string> iv; + std::string iv; friend class CommandLineParser; }; @@ -77,7 +68,7 @@ namespace ("input-path,i", po::value<std::string>(&settings.input_path)->required(), "set input file") ("output-path,o", po::value<std::string>(&settings.output_path)->required(), "set output file") ("key,k", po::value<std::string>(&settings.key)->required(), "set encryption key") - ("iv,v", po::value<std::string>(&settings.iv.second), "set initialization vector"); + ("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); @@ -88,10 +79,16 @@ namespace return; } - if (vm.count("iv")) - settings.iv.first = true; - po::notify(vm); + + if (aesni::mode_requires_initialization_vector(settings.get_mode())) + { + if (!vm.count("iv")) + { + throw boost::program_options::error( + "an initialization vector is required for the selected mode of operation"); + } + } } bool exit_with_usage() const { return help_flag; } |