/** * \file * \author Egor Tensin * \date 2015 * \copyright This file is licensed under the terms of the MIT License. * See LICENSE.txt for details. */ #include "block_cmd_parser.hpp" #include "block_dumper.hpp" #include #include #include #include #include #include #include #include namespace { template bool encrypt_with_mode( const std::string& key_str, std::deque& plaintexts, bool verbose = false) { typename aesni::Types::Block iv; if (aesni::ModeRequiresInitializationVector::value) { if (plaintexts.empty()) return false; aesni::from_string(iv, plaintexts.front()); plaintexts.pop_front(); if (verbose) dump_iv(iv); } typename aesni::Types::Key key; aesni::from_string(key, key_str); if (verbose) dump_key(key); aesni::EncryptWrapper encrypt(key, iv); if (verbose) dump_wrapper(encrypt); while (!plaintexts.empty()) { typename aesni::Types::Block plaintext, ciphertext; aesni::from_string(plaintext, plaintexts.front()); plaintexts.pop_front(); encrypt.encrypt_block(plaintext, ciphertext); if (verbose) { dump_plaintext(plaintext); dump_ciphertext(ciphertext); dump_next_iv(encrypt); } else { std::cout << aesni::to_string(ciphertext) << "\n"; } } return true; } template bool encrypt_with_algorithm( aesni::Mode mode, const std::string& key_str, std::deque& plaintexts, bool verbose = false) { switch (mode) { case AESNI_ECB: return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CBC: return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CFB: return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_OFB: return encrypt_with_mode(key_str, plaintexts, verbose); case AESNI_CTR: return encrypt_with_mode(key_str, plaintexts, verbose); default: return false; } } bool encrypt_using_cxx_api( aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key_str, std::deque& plaintexts, bool verbose = false) { switch (algorithm) { case AESNI_AES128: return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); case AESNI_AES192: return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); case AESNI_AES256: return encrypt_with_algorithm(mode, key_str, plaintexts, verbose); default: return false; } } template bool encrypt_using_boxes_with_algorithm( const AesNI_BoxAlgorithmParams& algorithm_params, aesni::Mode mode, const std::string& key, std::deque plaintexts) { AesNI_BoxBlock iv; AesNI_BoxBlock* iv_ptr = nullptr; if (aesni::mode_requires_initialization_vector(mode)) { if (plaintexts.empty()) return false; aesni::from_string(iv.aes_block, plaintexts.front()); iv_ptr = &iv; plaintexts.pop_front(); } AesNI_Box box; aesni_box_init( &box, algorithm, &algorithm_params, mode, iv_ptr, aesni::ErrorDetailsThrowsInDestructor()); while (!plaintexts.empty()) { AesNI_BoxBlock plaintext; aesni::from_string(plaintext.aes_block, plaintexts.front()); plaintexts.pop_front(); AesNI_BoxBlock ciphertext; aesni_box_encrypt_block( &box, &plaintext, &ciphertext, aesni::ErrorDetailsThrowsInDestructor()); std::cout << aesni::to_string(ciphertext.aes_block) << "\n"; } return true; } bool encrypt_using_boxes( aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key, std::deque plaintexts) { AesNI_BoxAlgorithmParams algorithm_params; switch (algorithm) { case AESNI_AES128: aesni::from_string( algorithm_params.aes128_key, key); return encrypt_using_boxes_with_algorithm( algorithm_params, mode, key, plaintexts); case AESNI_AES192: aesni::from_string( algorithm_params.aes192_key, key); return encrypt_using_boxes_with_algorithm( algorithm_params, mode, key, plaintexts); case AESNI_AES256: aesni::from_string( algorithm_params.aes256_key, key); return encrypt_using_boxes_with_algorithm( algorithm_params, mode, key, plaintexts); default: return false; } } } int main(int argc, char** argv) { try { CommandLineParser cmd_parser("encrypt_block.exe"); cmd_parser.parse(argc, argv); if (cmd_parser.requested_help()) { std::cout << cmd_parser; return 0; } std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), std::make_move_iterator(cmd_parser.args.end()) }; while (!args.empty()) { const auto key = args.front(); args.pop_front(); std::deque plaintexts; while (!args.empty()) { if (args.front() == "--") { args.pop_front(); break; } plaintexts.push_back(args.front()); args.pop_front(); } const auto success = cmd_parser.use_boxes ? encrypt_using_boxes(cmd_parser.algorithm, cmd_parser.mode, key, plaintexts) : encrypt_using_cxx_api(cmd_parser.algorithm, cmd_parser.mode, key, plaintexts, cmd_parser.verbose); if (!success) { std::cout << cmd_parser; return 1; } } return 0; } catch (const boost::program_options::error& e) { std::cerr << "Usage error: " << e.what() << "\n"; return 1; } catch (const aesni::Error& e) { std::cerr << e; return 1; } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } }