aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/decrypt_block.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-10-20 03:11:50 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-10-20 03:11:50 +0300
commitf9665896e3deb6c449c764ba8b30f700abe36cd5 (patch)
tree4203b64ccf2fb8ad4d40f20edad8aeff9448ab63 /utils/decrypt_block.cpp
parentadd missing `assert`s (diff)
downloadaes-tools-f9665896e3deb6c449c764ba8b30f700abe36cd5.tar.gz
aes-tools-f9665896e3deb6c449c764ba8b30f700abe36cd5.zip
cxx: implement "boxes"
Diffstat (limited to 'utils/decrypt_block.cpp')
-rw-r--r--utils/decrypt_block.cpp224
1 files changed, 104 insertions, 120 deletions
diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp
index 7c8e773..b9f9d19 100644
--- a/utils/decrypt_block.cpp
+++ b/utils/decrypt_block.cpp
@@ -9,8 +9,6 @@
#include "block_cmd_parser.hpp"
#include "block_dumper.hpp"
-#include <aesni/all.h>
-
#include <aesnixx/all.hpp>
#include <boost/program_options.hpp>
@@ -24,7 +22,7 @@
namespace
{
template <aesni::Algorithm algorithm, aesni::Mode mode>
- bool decrypt_with_mode(
+ void decrypt_with_mode(
const std::string& key_str,
std::deque<std::string>& ciphertexts,
bool verbose = false)
@@ -34,7 +32,7 @@ namespace
if (aesni::ModeRequiresInitializationVector<mode>())
{
if (ciphertexts.empty())
- return false;
+ throw_iv_required();
aesni::from_string<algorithm>(iv, ciphertexts.front());
ciphertexts.pop_front();
@@ -73,12 +71,10 @@ namespace
std::cout << aesni::to_string<algorithm>(plaintext) << "\n";
}
}
-
- return true;
}
template <aesni::Algorithm algorithm>
- bool decrypt_with_algorithm(
+ void decrypt_with_algorithm(
aesni::Mode mode,
const std::string& key_str,
std::deque<std::string>& ciphertexts,
@@ -87,126 +83,100 @@ namespace
switch (mode)
{
case AESNI_ECB:
- return decrypt_with_mode<algorithm, AESNI_ECB>(key_str, ciphertexts, verbose);
+ decrypt_with_mode<algorithm, AESNI_ECB>(key_str, ciphertexts, verbose);
+ break;
case AESNI_CBC:
- return decrypt_with_mode<algorithm, AESNI_CBC>(key_str, ciphertexts, verbose);
+ decrypt_with_mode<algorithm, AESNI_CBC>(key_str, ciphertexts, verbose);
+ break;
case AESNI_CFB:
- return decrypt_with_mode<algorithm, AESNI_CFB>(key_str, ciphertexts, verbose);
+ decrypt_with_mode<algorithm, AESNI_CFB>(key_str, ciphertexts, verbose);
+ break;
case AESNI_OFB:
- return decrypt_with_mode<algorithm, AESNI_OFB>(key_str, ciphertexts, verbose);
+ decrypt_with_mode<algorithm, AESNI_OFB>(key_str, ciphertexts, verbose);
+ break;
case AESNI_CTR:
- return decrypt_with_mode<algorithm, AESNI_CTR>(key_str, ciphertexts, verbose);
+ decrypt_with_mode<algorithm, AESNI_CTR>(key_str, ciphertexts, verbose);
+ break;
default:
- return false;
+ throw_not_implemented(mode);
+ break;
}
}
- bool decrypt_using_cxx_api(
+ void decrypt_using_cxx_api(
aesni::Algorithm algorithm,
aesni::Mode mode,
const std::string& key_str,
- std::deque<std::string> ciphertexts,
+ std::deque<std::string>& ciphertexts,
bool verbose = false)
{
switch (algorithm)
{
case AESNI_AES128:
- return decrypt_with_algorithm<AESNI_AES128>(mode, key_str, ciphertexts, verbose);
+ decrypt_with_algorithm<AESNI_AES128>(mode, key_str, ciphertexts, verbose);
+ break;
case AESNI_AES192:
- return decrypt_with_algorithm<AESNI_AES192>(mode, key_str, ciphertexts, verbose);
+ decrypt_with_algorithm<AESNI_AES192>(mode, key_str, ciphertexts, verbose);
+ break;
case AESNI_AES256:
- return decrypt_with_algorithm<AESNI_AES256>(mode, key_str, ciphertexts, verbose);
+ decrypt_with_algorithm<AESNI_AES256>(mode, key_str, ciphertexts, verbose);
+ break;
default:
- return false;
+ throw_not_implemented(algorithm);
+ break;
}
}
- template <aesni::Algorithm algorithm>
- bool decrypt_using_boxes_with_algorithm(
- const AesNI_BoxKey& box_key,
- aesni::Mode mode,
- const std::string& key,
- std::deque<std::string> ciphertexts)
+ void decrypt_using_particular_box(
+ aesni::Box& box,
+ std::deque<std::string>& ciphertexts)
{
- AesNI_BoxBlock iv;
- AesNI_BoxBlock* iv_ptr = nullptr;
-
- if (aesni::mode_requires_initialization_vector(mode))
- {
- if (ciphertexts.empty())
- return false;
-
- aesni::from_string<algorithm>(iv.aes_block, ciphertexts.front());
- iv_ptr = &iv;
- ciphertexts.pop_front();
- }
-
- AesNI_Box box;
- aesni_box_init(
- &box,
- algorithm,
- &box_key,
- mode,
- iv_ptr,
- aesni::ErrorDetailsThrowsInDestructor());
-
while (!ciphertexts.empty())
{
- AesNI_BoxBlock ciphertext;
- aesni::from_string<algorithm>(ciphertext.aes_block, ciphertexts.front());
+ aesni::Box::Block ciphertext;
+ box.parse_block(ciphertext, ciphertexts.front());
ciphertexts.pop_front();
- AesNI_BoxBlock plaintext;
- aesni_box_decrypt_block(
- &box,
- &ciphertext,
- &plaintext,
- aesni::ErrorDetailsThrowsInDestructor());
+ aesni::Box::Block plaintext;
+ box.decrypt_block(ciphertext, plaintext);
- std::cout << aesni::to_string<algorithm>(plaintext.aes_block) << "\n";
+ std::cout << box.format_block(plaintext) << "\n";
}
-
- return true;
}
- bool decrypt_using_boxes(
+ void decrypt_using_boxes(
aesni::Algorithm algorithm,
aesni::Mode mode,
- const std::string& key,
- std::deque<std::string> ciphertexts)
+ const std::string& key_str,
+ std::deque<std::string>& ciphertexts)
{
- AesNI_BoxKey box_key;
+ aesni::Box::Key key;
+ aesni::Box::parse_key(key, algorithm, key_str);
- switch (algorithm)
+ if (aesni::mode_requires_initialization_vector(mode))
{
- case AESNI_AES128:
- aesni::from_string<AESNI_AES128>(
- box_key.aes128_key, key);
- return decrypt_using_boxes_with_algorithm<AESNI_AES128>(
- box_key, mode, key, ciphertexts);
-
- case AESNI_AES192:
- aesni::from_string<AESNI_AES192>(
- box_key.aes192_key, key);
- return decrypt_using_boxes_with_algorithm<AESNI_AES192>(
- box_key, mode, key, ciphertexts);
+ if (ciphertexts.empty())
+ throw_iv_required();
- case AESNI_AES256:
- aesni::from_string<AESNI_AES256>(
- box_key.aes256_key, key);
- return decrypt_using_boxes_with_algorithm<AESNI_AES256>(
- box_key, mode, key, ciphertexts);
+ aesni::Box::Block iv;
+ aesni::Box::parse_block(iv, algorithm, ciphertexts.front());
+ ciphertexts.pop_front();
- default:
- return false;
+ decrypt_using_particular_box(
+ aesni::Box(algorithm, key, mode, iv), ciphertexts);
+ }
+ else
+ {
+ decrypt_using_particular_box(
+ aesni::Box(algorithm, key), ciphertexts);
}
}
}
@@ -216,62 +186,76 @@ int main(int argc, char** argv)
try
{
CommandLineParser cmd_parser("decrypt_block.exe");
- cmd_parser.parse(argc, argv);
-
- if (cmd_parser.requested_help())
+ try
{
- std::cout << cmd_parser;
- return 0;
- }
+ cmd_parser.parse(argc, argv);
- std::deque<std::string> 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();
+ if (cmd_parser.requested_help())
+ {
+ std::cout << cmd_parser;
+ return 0;
+ }
- std::deque<std::string> ciphertexts;
+ std::deque<std::string> args(
+ std::make_move_iterator(cmd_parser.args.begin()),
+ std::make_move_iterator(cmd_parser.args.end()));
while (!args.empty())
{
- if (args.front() == "--")
+ const auto key = args.front();
+ args.pop_front();
+
+ std::deque<std::string> ciphertexts;
+
+ while (!args.empty())
{
+ if (args.front() == "--")
+ {
+ args.pop_front();
+ break;
+ }
+
+ ciphertexts.push_back(args.front());
args.pop_front();
- break;
}
- ciphertexts.push_back(args.front());
- args.pop_front();
+ if (cmd_parser.use_boxes)
+ {
+ decrypt_using_boxes(
+ cmd_parser.algorithm,
+ cmd_parser.mode,
+ key,
+ ciphertexts);
+ }
+ else
+ {
+ decrypt_using_cxx_api(
+ cmd_parser.algorithm,
+ cmd_parser.mode,
+ key,
+ ciphertexts,
+ cmd_parser.verbose);
+ }
}
- const auto success = cmd_parser.use_boxes
- ? decrypt_using_boxes(cmd_parser.algorithm, cmd_parser.mode, key, ciphertexts)
- : decrypt_using_cxx_api(cmd_parser.algorithm, cmd_parser.mode, key, ciphertexts, 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";
+ std::cerr << cmd_parser;
+ return 1;
+ }
+ catch (const aesni::Error& e)
+ {
+ std::cerr << e;
+ 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;
}
+ return 0;
}