From 2f7feb1d9222e0afaac8ae17db98d1556aa46aa4 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 2 Aug 2015 20:35:38 +0300 Subject: cxx: more algorithm-agnostic API The code (in the utilities in particular) is a mess though, so a refactoring's coming up. --- utils/aes_decrypt_bmp.cpp | 132 +++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 59 deletions(-) (limited to 'utils/aes_decrypt_bmp.cpp') diff --git a/utils/aes_decrypt_bmp.cpp b/utils/aes_decrypt_bmp.cpp index b9f669e..cf96847 100644 --- a/utils/aes_decrypt_bmp.cpp +++ b/utils/aes_decrypt_bmp.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -58,72 +59,28 @@ namespace ofs.open(path, std::ofstream::binary); ofs.write(src.data(), src.size()); } -} -int main(int argc, char** argv) -{ - try + template + bool decrypt_bmp_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) { - CommandLineParser cmd_parser("aes_decrypt_bmp.exe"); - - if (!cmd_parser.parse_options(argc, argv)) - return 0; - - auto args = cmd_parser.get_args(); - - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - - AesNI_BoxAlgorithmParams algorithm_params; - - switch (cmd_parser.get_algorithm()) - { - case AESNI_AES128: - aesni::aes::from_string(algorithm_params.aes128_key, args.front()); - break; - - case AESNI_AES192: - aesni::aes::from_string(algorithm_params.aes192_key, args.front()); - break; - - case AESNI_AES256: - aesni::aes::from_string(algorithm_params.aes256_key, args.front()); - break; - } - - args.pop_front(); - AesNI_BoxBlock iv; AesNI_BoxBlock* iv_ptr = nullptr; - switch (cmd_parser.get_mode()) + if (aesni::mode_requires_initialization_vector(mode)) { - case AESNI_ECB: - break; - - case AESNI_CBC: - case AESNI_CFB: - case AESNI_OFB: - case AESNI_CTR: - if (args.empty()) - { - cmd_parser.print_usage(); - return 1; - } - aesni::aes::from_string(iv.aes_block, args.front()); - iv_ptr = &iv; - args.pop_front(); - break; + if (args.empty()) + return false; + + aesni::from_string(iv.aes_block, args.front()); + iv_ptr = &iv; + args.pop_front(); } if (args.size() != 2) - { - cmd_parser.print_usage(); - return 1; - } + return false; const auto src_path = args[0]; const auto dest_path = args[1]; @@ -140,9 +97,9 @@ int main(int argc, char** argv) aesni_box_init( &box, - cmd_parser.get_algorithm(), + algorithm, &algorithm_params, - cmd_parser.get_mode(), + mode, iv_ptr, aesni::ErrorDetailsThrowsInDestructor()); @@ -171,6 +128,63 @@ int main(int argc, char** argv) dest_buf.resize(header_size + pixels_size); write_file(dest_path, dest_buf); + return true; + } + + bool decrypt_bmp( + aesni::Algorithm algorithm, + aesni::Mode mode, + std::deque& args) + { + if (args.empty()) + return false; + + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return decrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_decrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!decrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + { + cmd_parser.print_usage(); + return 1; + } + return 0; } catch (const boost::program_options::error& e) -- cgit v1.2.3