From a151a774f4ce6c4b13c819502efcfe919bbf050b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 25 Jun 2017 19:56:14 +0300 Subject: code style --- cxx/include/aesxx/aes.hpp | 36 ++++----- cxx/include/aesxx/api.hpp | 24 +++--- cxx/include/aesxx/error.hpp | 2 +- src/error.c | 4 +- utils/block_cmd_parser.hpp | 175 ++++++++++++++++++++++---------------------- utils/block_dumper.hpp | 169 +++++++++++++++++++++--------------------- utils/block_input.hpp | 33 ++++----- utils/data_parsers.hpp | 4 +- utils/file_cmd_parser.hpp | 117 +++++++++++++++-------------- 9 files changed, 276 insertions(+), 288 deletions(-) diff --git a/cxx/include/aesxx/aes.hpp b/cxx/include/aesxx/aes.hpp index d1f68d2..efac790 100644 --- a/cxx/include/aesxx/aes.hpp +++ b/cxx/include/aesxx/aes.hpp @@ -34,19 +34,19 @@ namespace aes }; template <> - std::size_t get_number_of_rounds() + inline std::size_t get_number_of_rounds() { return 11; } template <> - void from_string(aes128::Block& dest, const char* src) + inline void from_string(aes128::Block& dest, const char* src) { aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes128::Block& src) + inline std::string to_string(const aes128::Block& src) { AES_AES128_BlockString str; aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -54,7 +54,7 @@ namespace aes } template <> - std::string to_matrix_string(const aes128::Block& src) + inline std::string to_matrix_string(const aes128::Block& src) { AES_AES128_BlockMatrixString str; aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -62,13 +62,13 @@ namespace aes } template <> - void from_string(aes128::Key& dest, const char* src) + inline void from_string(aes128::Key& dest, const char* src) { aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes128::Key& src) + inline std::string to_string(const aes128::Key& src) { AES_AES128_KeyString str; aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -119,19 +119,19 @@ namespace aes }; template <> - std::size_t get_number_of_rounds() + inline std::size_t get_number_of_rounds() { return 13; } template <> - void from_string(aes192::Block& dest, const char* src) + inline void from_string(aes192::Block& dest, const char* src) { aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes192::Block& src) + inline std::string to_string(const aes192::Block& src) { AES_AES192_BlockString str; aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -139,7 +139,7 @@ namespace aes } template <> - std::string to_matrix_string(const aes192::Block& src) + inline std::string to_matrix_string(const aes192::Block& src) { AES_AES192_BlockMatrixString str; aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -147,13 +147,13 @@ namespace aes } template <> - void from_string(aes192::Key& dest, const char* src) + inline void from_string(aes192::Key& dest, const char* src) { aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes192::Key& src) + inline std::string to_string(const aes192::Key& src) { AES_AES192_KeyString str; aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -204,19 +204,19 @@ namespace aes }; template <> - std::size_t get_number_of_rounds() + inline std::size_t get_number_of_rounds() { return 15; } template <> - void from_string(aes256::Block& dest, const char* src) + inline void from_string(aes256::Block& dest, const char* src) { aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes256::Block& src) + inline std::string to_string(const aes256::Block& src) { AES_AES256_BlockString str; aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -224,7 +224,7 @@ namespace aes } template <> - std::string to_matrix_string(const aes256::Block& src) + inline std::string to_matrix_string(const aes256::Block& src) { AES_AES256_BlockMatrixString str; aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); @@ -232,13 +232,13 @@ namespace aes } template <> - void from_string(aes256::Key& dest, const char* src) + inline void from_string(aes256::Key& dest, const char* src) { aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); } template <> - std::string to_string(const aes256::Key& src) + inline std::string to_string(const aes256::Key& src) { AES_AES256_KeyString str; aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); diff --git a/cxx/include/aesxx/api.hpp b/cxx/include/aesxx/api.hpp index 2d0871f..789320f 100644 --- a/cxx/include/aesxx/api.hpp +++ b/cxx/include/aesxx/api.hpp @@ -27,7 +27,7 @@ namespace aes const char*); template - inline void from_string( + void from_string( typename Types::Block& dest, const std::string& src) { @@ -46,7 +46,7 @@ namespace aes const char*); template - inline void from_string( + void from_string( typename Types::Key& dest, const std::string& src) { @@ -57,30 +57,30 @@ namespace aes std::string to_string(const typename Types::Key&); template - inline void expand_key( + void expand_key( const typename Types::Key& key, typename Types::RoundKeys& encryption_keys); template - inline void derive_decryption_keys( + void derive_decryption_keys( const typename Types::RoundKeys& encryption_keys, typename Types::RoundKeys& decryption_keys); template ::value>::type* = nullptr> - inline void encrypt_block( + void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block& iv, typename Types::Block& ciphertext); template ::value>::type* = nullptr> - inline void encrypt_block( + void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block& ciphertext); template ::value>::type* = nullptr> - inline void encrypt_block( + void encrypt_block( const typename Types::Block& plaintext, const typename Types::RoundKeys& round_keys, typename Types::Block&, @@ -90,20 +90,20 @@ namespace aes } template ::value>::type* = nullptr> - inline void decrypt_block( + void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block& iv, typename Types::Block& plaintext); template ::value>::type* = nullptr> - inline void decrypt_block( + void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block& plaintext); template ::value>::type* = nullptr> - inline void decrypt_block( + void decrypt_block( const typename Types::Block& ciphertext, const typename Types::RoundKeys& round_keys, typename Types::Block&, @@ -122,7 +122,7 @@ namespace aes expand_key(key, encryption_keys); } - inline void encrypt_block( + void encrypt_block( const typename Types::Block& plaintext, typename Types::Block& ciphertext) { @@ -154,7 +154,7 @@ namespace aes } } - inline void decrypt_block( + void decrypt_block( const typename Types::Block& ciphertext, typename Types::Block& plaintext) { diff --git a/cxx/include/aesxx/error.hpp b/cxx/include/aesxx/error.hpp index 21f65d5..4d2c783 100644 --- a/cxx/include/aesxx/error.hpp +++ b/cxx/include/aesxx/error.hpp @@ -69,7 +69,7 @@ namespace aes const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; }; - std::ostream& operator<<(std::ostream& os, const Error& error) + inline std::ostream& operator<<(std::ostream& os, const Error& error) { os << "AES error: " << error.what() << '\n'; os << "Call stack:\n"; diff --git a/src/error.c b/src/error.c index fefe7a2..654c7a9 100644 --- a/src/error.c +++ b/src/error.c @@ -19,7 +19,7 @@ #define snprintf c99_snprintf #define vsnprintf c99_vsnprintf -__inline int c99_vsnprintf(char *dest, size_t dest_size, const char *fmt, va_list ap) +static int c99_vsnprintf(char *dest, size_t dest_size, const char *fmt, va_list ap) { int dest_len = -1; @@ -31,7 +31,7 @@ __inline int c99_vsnprintf(char *dest, size_t dest_size, const char *fmt, va_lis return dest_len; } -__inline int c99_snprintf(char *dest, size_t dest_size, const char *fmt, ...) +static int c99_snprintf(char *dest, size_t dest_size, const char *fmt, ...) { va_list ap; va_start(ap, fmt); 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 #include -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 inputs; + std::vector 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(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value(&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>(&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(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value(&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>(&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::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())}); + } + +private: + void parse_inputs(std::deque&& 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::make_move_iterator(args.begin()), - std::make_move_iterator(args.end())}); - } + Input parse_input(std::deque& src) const + { + std::string key{std::move(src.front())}; + src.pop_front(); - private: - void parse_inputs(std::deque&& src) - { - while (!src.empty()) - inputs.emplace_back(parse_input(src)); - } + std::string iv; - Input parse_input(std::deque& 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 parse_blocks(std::deque& src) - { - std::vector 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 parse_blocks(std::deque& src) + { + std::vector 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 args; - }; -} + return blocks; + } + + std::vector 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 #include -namespace +template +void dump_block( + const char* header, + const typename aes::Types::Block& block) { - template - void dump_block( - const char* header, - const typename aes::Types::Block& block) - { - std::cout << header << ": " << aes::to_string(block) << "\n"; - std::cout << aes::to_matrix_string(block) << "\n"; - } - - template - void dump_plaintext(const typename aes::Types::Block& block) - { - dump_block("Plaintext", block); - } - - template - void dump_key(const typename aes::Types::Key& key) - { - std::cout << "Key: " << aes::to_string(key) << "\n\n"; - } - - template - void dump_ciphertext(const typename aes::Types::Block& ciphertext) - { - dump_block("Ciphertext", ciphertext); - } - - template - void dump_iv(const typename aes::Types::Block& iv) - { - dump_block("Initialization vector", iv); - } - - template - void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) - { - std::cout << header << ":\n"; - for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) - std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; - std::cout << "\n"; - } - - template - void dump_encryption_keys(const typename aes::Types::RoundKeys& round_keys) - { - dump_round_keys("Encryption round keys", round_keys); - } - - template - void dump_decryption_keys(const typename aes::Types::RoundKeys& round_keys) - { - dump_round_keys("Decryption round keys", round_keys); - } - - template - void dump_wrapper(const aes::EncryptWrapper& wrapper) - { - dump_encryption_keys(wrapper.encryption_keys); - } - - template - void dump_wrapper(const aes::DecryptWrapper& wrapper) - { - dump_decryption_keys(wrapper.decryption_keys); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::EncryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::EncryptWrapper&) - { } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::DecryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::DecryptWrapper&) - { } + std::cout << header << ": " << aes::to_string(block) << "\n"; + std::cout << aes::to_matrix_string(block) << "\n"; } + +template +void dump_plaintext(const typename aes::Types::Block& block) +{ + dump_block("Plaintext", block); +} + +template +void dump_key(const typename aes::Types::Key& key) +{ + std::cout << "Key: " << aes::to_string(key) << "\n\n"; +} + +template +void dump_ciphertext(const typename aes::Types::Block& ciphertext) +{ + dump_block("Ciphertext", ciphertext); +} + +template +void dump_iv(const typename aes::Types::Block& iv) +{ + dump_block("Initialization vector", iv); +} + +template +void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) +{ + std::cout << header << ":\n"; + for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) + std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; + std::cout << "\n"; +} + +template +void dump_encryption_keys(const typename aes::Types::RoundKeys& round_keys) +{ + dump_round_keys("Encryption round keys", round_keys); +} + +template +void dump_decryption_keys(const typename aes::Types::RoundKeys& round_keys) +{ + dump_round_keys("Decryption round keys", round_keys); +} + +template +void dump_wrapper(const aes::EncryptWrapper& wrapper) +{ + dump_encryption_keys(wrapper.encryption_keys); +} + +template +void dump_wrapper(const aes::DecryptWrapper& wrapper) +{ + dump_decryption_keys(wrapper.decryption_keys); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::EncryptWrapper& wrapper) +{ + dump_block("Next initialization vector", wrapper.iv); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::EncryptWrapper&) +{ } + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::DecryptWrapper& wrapper) +{ + dump_block("Next initialization vector", wrapper.iv); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::DecryptWrapper&) +{ } 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 #include -namespace +class Input { - class Input - { - public: - Input(const std::string& key, const std::string& iv, std::vector&& blocks) - : key{key} - , iv{iv} - , blocks{std::move(blocks)} - { } +public: + Input(const std::string& key, const std::string& iv, std::vector&& blocks) + : key{key} + , iv{iv} + , blocks{std::move(blocks)} + { } - Input(const std::string& key, std::vector&& blocks) - : key{key} - , blocks{std::move(blocks)} - { } + Input(const std::string& key, std::vector&& blocks) + : key{key} + , blocks{std::move(blocks)} + { } - const std::string key; - const std::string iv; - const std::vector blocks; - }; -} + const std::string key; + const std::string iv; + const std::vector 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 #include -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 #include -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(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("key,k", - boost::program_options::value(&key) - ->required() - ->value_name("KEY"), - "set encryption key") - ("iv,v", - boost::program_options::value(&iv) - ->value_name("BLOCK"), - "set initialization vector") - ("input,i", - boost::program_options::value(&input_path) - ->required() - ->value_name("PATH"), - "set input file path") - ("output,o", - boost::program_options::value(&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(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("key,k", + boost::program_options::value(&key) + ->required() + ->value_name("KEY"), + "set encryption key") + ("iv,v", + boost::program_options::value(&iv) + ->value_name("BLOCK"), + "set initialization vector") + ("input,i", + boost::program_options::value(&input_path) + ->required() + ->value_name("PATH"), + "set input file path") + ("output,o", + boost::program_options::value(&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"}; } - }; -} + } +}; -- cgit v1.2.3