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. --- cxx/include/aesnixx/aes.hpp | 823 ++++++++++++++----------------------------- cxx/include/aesnixx/all.hpp | 1 + cxx/include/aesnixx/api.hpp | 171 +++++++++ cxx/include/aesnixx/mode.hpp | 108 ++++++ utils/aes_block_common.hpp | 140 +++----- utils/aes_decrypt_block.cpp | 98 +++--- utils/aes_decrypt_bmp.cpp | 132 +++---- utils/aes_decrypt_file.cpp | 133 +++---- utils/aes_encrypt_block.cpp | 102 +++--- utils/aes_encrypt_bmp.cpp | 132 +++---- utils/aes_encrypt_file.cpp | 133 +++---- 11 files changed, 995 insertions(+), 978 deletions(-) create mode 100644 cxx/include/aesnixx/api.hpp diff --git a/cxx/include/aesnixx/aes.hpp b/cxx/include/aesnixx/aes.hpp index 5d293ca..b2febf5 100644 --- a/cxx/include/aesnixx/aes.hpp +++ b/cxx/include/aesnixx/aes.hpp @@ -9,581 +9,270 @@ #pragma once #include "algorithm.hpp" -#include "data.hpp" +#include "api.hpp" +#include "error.hpp" #include "mode.hpp" #include +#include + #include namespace aesni { - namespace aes + namespace aes128 + { + typedef AesNI_AES128_Block Block; + typedef AesNI_AES128_RoundKeys RoundKeys; + typedef AesNI_AES128_Key Key; + } + + template <> + struct Types + { + typedef aes128::Block Block; + typedef aes128::RoundKeys RoundKeys; + typedef aes128::Key Key; + }; + + template <> + std::size_t get_number_of_rounds() + { + return 11; + } + + template <> + void from_string(aes128::Block& dest, const char* src) + { + aesni_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes128::Block& src) + { + AesNI_AES128_BlockString str; + aesni_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + std::string to_matrix_string(const aes128::Block& src) + { + AesNI_AES128_BlockMatrixString str; + aesni_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + void from_string(aes128::Key& dest, const char* src) + { + aesni_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes128::Key& src) + { + AesNI_AES128_KeyString str; + aesni_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + inline void expand_key( + const aes128::Key& key, + aes128::RoundKeys& encryption_keys) + { + aesni_AES128_expand_key(&key, &encryption_keys); + } + + template <> + inline void derive_decryption_keys( + const aes128::RoundKeys& encryption_keys, + aes128::RoundKeys& decryption_keys) + { + aesni_AES128_derive_decryption_keys( + &encryption_keys, &decryption_keys); + } + + AESNIXX_ENCRYPT_BLOCK_ECB(AES128); + AESNIXX_DECRYPT_BLOCK_ECB(AES128); + AESNIXX_ENCRYPT_BLOCK_CBC(AES128); + AESNIXX_DECRYPT_BLOCK_CBC(AES128); + AESNIXX_ENCRYPT_BLOCK_CFB(AES128); + AESNIXX_DECRYPT_BLOCK_CFB(AES128); + AESNIXX_ENCRYPT_BLOCK_OFB(AES128); + AESNIXX_DECRYPT_BLOCK_OFB(AES128); + AESNIXX_ENCRYPT_BLOCK_CTR(AES128); + AESNIXX_DECRYPT_BLOCK_CTR(AES128); + + namespace aes192 + { + typedef AesNI_AES192_Block Block; + typedef AesNI_AES192_RoundKeys RoundKeys; + typedef AesNI_AES192_Key Key; + } + + template <> + struct Types + { + typedef aes192::Block Block; + typedef aes192::RoundKeys RoundKeys; + typedef aes192::Key Key; + }; + + template <> + std::size_t get_number_of_rounds() + { + return 13; + } + + template <> + void from_string(aes192::Block& dest, const char* src) + { + aesni_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes192::Block& src) + { + AesNI_AES192_BlockString str; + aesni_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + std::string to_matrix_string(const aes192::Block& src) + { + AesNI_AES192_BlockMatrixString str; + aesni_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + void from_string(aes192::Key& dest, const char* src) + { + aesni_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes192::Key& src) + { + AesNI_AES192_KeyString str; + aesni_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + inline void expand_key( + const aes192::Key& key, + aes192::RoundKeys& encryption_keys) + { + aesni_AES192_expand_key(&key, &encryption_keys); + } + + template <> + inline void derive_decryption_keys( + const aes192::RoundKeys& encryption_keys, + aes192::RoundKeys& decryption_keys) + { + aesni_AES192_derive_decryption_keys( + &encryption_keys, &decryption_keys); + } + + AESNIXX_ENCRYPT_BLOCK_ECB(AES192); + AESNIXX_DECRYPT_BLOCK_ECB(AES192); + AESNIXX_ENCRYPT_BLOCK_CBC(AES192); + AESNIXX_DECRYPT_BLOCK_CBC(AES192); + AESNIXX_ENCRYPT_BLOCK_CFB(AES192); + AESNIXX_DECRYPT_BLOCK_CFB(AES192); + AESNIXX_ENCRYPT_BLOCK_OFB(AES192); + AESNIXX_DECRYPT_BLOCK_OFB(AES192); + AESNIXX_ENCRYPT_BLOCK_CTR(AES192); + AESNIXX_DECRYPT_BLOCK_CTR(AES192); + + namespace aes256 { - typedef AesNI_AES_Block Block; - - typedef AesNI_AES128_Key Key128; - typedef AesNI_AES192_Key Key192; - typedef AesNI_AES256_Key Key256; - - inline void make_block(Block& dest, int hi3, int hi2, int lo1, int lo0) - { - aesni_AES_make_block(&dest, hi3, hi2, lo1, lo0); - } - - inline void make_key(Key128& dest, int hi3, int hi2, int lo1, int lo0) - { - aesni_AES128_make_key(&dest, hi3, hi2, lo1, lo0); - } - - inline void make_key(Key192& dest, int hi5, int hi4, int hi3, int lo2, int lo1, int lo0) - { - aesni_AES192_make_key(&dest, hi5, hi4, hi3, lo2, lo1, lo0); - } - - inline void make_key(Key256& dest, int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) - { - aesni_AES256_make_key(&dest, hi7, hi6, hi5, hi4, lo3, lo2, lo1, lo0); - } - - std::string to_string(const Block& block) - { - AesNI_AES_BlockString str; - aesni_AES_format_block(&str, &block, ErrorDetailsThrowsInDestructor()); - return std::string(str.str); - } - - std::string to_matrix_string(const Block& block) - { - AesNI_AES_BlockMatrixString str; - aesni_AES_format_block_as_matrix(&str, &block, ErrorDetailsThrowsInDestructor()); - return std::string(str.str); - } - - inline void from_string(Block& dest, const char* src) - { - aesni_AES_parse_block(&dest, src, ErrorDetailsThrowsInDestructor()); - } - - inline void from_string(Block& dest, const std::string& src) - { - from_string(dest, src.c_str()); - } - - std::string to_string(const Key128& block) - { - AesNI_AES128_KeyString str; - aesni_AES128_format_key(&str, &block, ErrorDetailsThrowsInDestructor()); - return std::string(str.str); - } - - std::string to_string(const Key192& block) - { - AesNI_AES192_KeyString str; - aesni_AES192_format_key(&str, &block, ErrorDetailsThrowsInDestructor()); - return std::string(str.str); - } - - std::string to_string(const Key256& block) - { - AesNI_AES256_KeyString str; - aesni_AES256_format_key(&str, &block, ErrorDetailsThrowsInDestructor()); - return std::string(str.str); - } - - inline void from_string(Key128& dest, const char* src) - { - aesni_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); - } - - inline void from_string(Key192& dest, const char* src) - { - aesni_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); - } - - inline void from_string(Key256& dest, const char* src) - { - aesni_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); - } - - inline void from_string(Key128& dest, const std::string& src) - { - return from_string(dest, src.c_str()); - } - - inline void from_string(Key192& dest, const std::string& src) - { - return from_string(dest, src.c_str()); - } - - inline void from_string(Key256& dest, const std::string& src) - { - return from_string(dest, src.c_str()); - } - - typedef AesNI_AES128_RoundKeys RoundKeys128; - typedef AesNI_AES192_RoundKeys RoundKeys192; - typedef AesNI_AES256_RoundKeys RoundKeys256; - - template - inline std::size_t get_number_of_rounds(const RoundKeysT& round_keys) - { - return sizeof(round_keys) / sizeof(Block128); - } - - inline void expand_key( - const Key128& key, - RoundKeys128& encryption_keys) - { - aesni_AES128_expand_key(&key, &encryption_keys); - } - - inline void expand_key( - const Key192& key, - RoundKeys192& encryption_keys) - { - aesni_AES192_expand_key(&key, &encryption_keys); - } - - inline void expand_key( - const Key256& key, - RoundKeys256& encryption_keys) - { - aesni_AES256_expand_key(&key, &encryption_keys); - } - - inline void derive_decryption_keys( - const RoundKeys128& encryption_keys, - RoundKeys128& decryption_keys) - { - aesni_AES128_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - inline void derive_decryption_keys( - const RoundKeys192& encryption_keys, - RoundKeys192& decryption_keys) - { - aesni_AES192_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - inline void derive_decryption_keys( - const RoundKeys256& encryption_keys, - RoundKeys256& decryption_keys) - { - aesni_AES256_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - inline Block encrypt_ecb( - const Block& plaintext, - const RoundKeys128& encryption_keys) - { - return aesni_AES128_encrypt_block_ECB(plaintext, &encryption_keys); - } - - inline Block decrypt_ecb( - const Block& ciphertext, - const RoundKeys128& decryption_keys) - { - return aesni_AES128_decrypt_block_ECB(ciphertext, &decryption_keys); - } - - inline Block encrypt_cbc( - const Block& plaintext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cbc( - const Block& ciphertext, - const RoundKeys128& decryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv); - } - - inline Block encrypt_cfb( - const Block& plaintext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cfb( - const Block& ciphertext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ofb( - const Block& plaintext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ofb( - const Block& ciphertext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ctr( - const Block& plaintext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ctr( - const Block& ciphertext, - const RoundKeys128& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES128_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ecb( - const Block& plaintext, - const RoundKeys192& encryption_keys) - { - return aesni_AES192_encrypt_block_ECB(plaintext, &encryption_keys); - } - - inline Block decrypt_ecb( - const Block& ciphertext, - const RoundKeys192& decryption_keys) - { - return aesni_AES192_decrypt_block_ECB(ciphertext, &decryption_keys); - } - - inline Block encrypt_cbc( - const Block& plaintext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cbc( - const Block& ciphertext, - const RoundKeys192& decryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv); - } - - inline Block encrypt_cfb( - const Block& plaintext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cfb( - const Block& ciphertext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ofb( - const Block& plaintext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ofb( - const Block& ciphertext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ctr( - const Block& plaintext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ctr( - const Block& ciphertext, - const RoundKeys192& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES192_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ecb( - const Block& plaintext, - const RoundKeys256& encryption_keys) - { - return aesni_AES256_encrypt_block_ECB(plaintext, &encryption_keys); - } - - inline Block decrypt_ecb( - const Block& ciphertext, - const RoundKeys256& decryption_keys) - { - return aesni_AES256_decrypt_block_ECB(ciphertext, &decryption_keys); - } - - inline Block encrypt_cbc( - const Block& plaintext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_encrypt_block_CBC(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cbc( - const Block& ciphertext, - const RoundKeys256& decryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &next_iv); - } - - inline Block encrypt_cfb( - const Block& plaintext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_encrypt_block_CFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_cfb( - const Block& ciphertext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ofb( - const Block& plaintext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_encrypt_block_OFB(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ofb( - const Block& ciphertext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &next_iv); - } - - inline Block encrypt_ctr( - const Block& plaintext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_encrypt_block_CTR(plaintext, &encryption_keys, iv, &next_iv); - } - - inline Block decrypt_ctr( - const Block& ciphertext, - const RoundKeys256& encryption_keys, - const Block& iv, - Block& next_iv) - { - return aesni_AES256_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &next_iv); - } - - template - struct Types; - - template <> - struct Types - { - typedef aesni::aes::Block BlockT; - typedef aesni::aes::Key128 KeyT; - typedef aesni::aes::RoundKeys128 RoundKeysT; - }; - - template <> - struct Types - { - typedef aesni::aes::Block BlockT; - typedef aesni::aes::Key192 KeyT; - typedef aesni::aes::RoundKeys192 RoundKeysT; - }; - - template <> - struct Types - { - typedef aesni::aes::Block BlockT; - typedef aesni::aes::Key256 KeyT; - typedef aesni::aes::RoundKeys256 RoundKeysT; - }; - - template - struct Encrypt; - - template - struct Encrypt - { - Encrypt(const typename Types::KeyT& key, - const typename Types::BlockT& iv) - { - expand_key(key, encryption_keys); - derive_decryption_keys(encryption_keys, decryption_keys); - } - - inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) - { - return encrypt_ecb(plaintext, encryption_keys); - } - - inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) - { - return decrypt_ecb(ciphertext, decryption_keys); - } - - typename Types::RoundKeysT encryption_keys; - typename Types::RoundKeysT decryption_keys; - }; - - template - struct Encrypt - { - Encrypt(const typename Types::KeyT& key, - const typename Types::BlockT& iv) - : iv(iv) - { - expand_key(key, encryption_keys); - derive_decryption_keys(encryption_keys, decryption_keys); - } - - inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) - { - return encrypt_cbc(plaintext, encryption_keys, iv, iv); - } - - inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) - { - return decrypt_cbc(ciphertext, decryption_keys, iv, iv); - } - - typename Types::BlockT iv; - typename Types::RoundKeysT encryption_keys; - typename Types::RoundKeysT decryption_keys; - }; - - template - struct Encrypt - { - Encrypt(const typename Types::KeyT& key, - const typename Types::BlockT& iv) - : iv(iv) - { - expand_key(key, encryption_keys); - } - - inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) - { - return encrypt_cfb(plaintext, encryption_keys, iv, iv); - } - - inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) - { - return decrypt_cfb(ciphertext, encryption_keys, iv, iv); - } - - typename Types::BlockT iv; - typename Types::RoundKeysT encryption_keys; - }; - - template - struct Encrypt - { - Encrypt(const typename Types::KeyT& key, - const typename Types::BlockT& iv) - : iv(iv) - { - expand_key(key, encryption_keys); - } - - inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) - { - return encrypt_ofb(plaintext, encryption_keys, iv, iv); - } - - inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) - { - return decrypt_ofb(ciphertext, encryption_keys, iv, iv); - } - - typename Types::BlockT iv; - typename Types::RoundKeysT encryption_keys; - }; - - template - struct Encrypt - { - Encrypt(const typename Types::KeyT& key, - const typename Types::BlockT& iv) - : iv(iv) - { - expand_key(key, encryption_keys); - } - - inline typename Types::BlockT encrypt(const typename Types::BlockT& plaintext) - { - return encrypt_ctr(plaintext, encryption_keys, iv, iv); - } - - inline typename Types::BlockT decrypt(const typename Types::BlockT& ciphertext) - { - return decrypt_ctr(ciphertext, encryption_keys, iv, iv); - } - - typename Types::RoundKeysT encryption_keys; - typename Types::BlockT iv; - }; + typedef AesNI_AES256_Block Block; + typedef AesNI_AES256_RoundKeys RoundKeys; + typedef AesNI_AES256_Key Key; } + + template <> + struct Types + { + typedef aes256::Block Block; + typedef aes256::RoundKeys RoundKeys; + typedef aes256::Key Key; + }; + + template <> + std::size_t get_number_of_rounds() + { + return 15; + } + + template <> + void from_string(aes256::Block& dest, const char* src) + { + aesni_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes256::Block& src) + { + AesNI_AES256_BlockString str; + aesni_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + std::string to_matrix_string(const aes256::Block& src) + { + AesNI_AES256_BlockMatrixString str; + aesni_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + void from_string(aes256::Key& dest, const char* src) + { + aesni_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor()); + } + + template <> + std::string to_string(const aes256::Key& src) + { + AesNI_AES256_KeyString str; + aesni_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor()); + return { str.str }; + } + + template <> + inline void expand_key( + const aes256::Key& key, + aes256::RoundKeys& encryption_keys) + { + aesni_AES256_expand_key(&key, &encryption_keys); + } + + template <> + inline void derive_decryption_keys( + const aes256::RoundKeys& encryption_keys, + aes256::RoundKeys& decryption_keys) + { + aesni_AES256_derive_decryption_keys( + &encryption_keys, &decryption_keys); + } + + AESNIXX_ENCRYPT_BLOCK_ECB(AES256); + AESNIXX_DECRYPT_BLOCK_ECB(AES256); + AESNIXX_ENCRYPT_BLOCK_CBC(AES256); + AESNIXX_DECRYPT_BLOCK_CBC(AES256); + AESNIXX_ENCRYPT_BLOCK_CFB(AES256); + AESNIXX_DECRYPT_BLOCK_CFB(AES256); + AESNIXX_ENCRYPT_BLOCK_OFB(AES256); + AESNIXX_DECRYPT_BLOCK_OFB(AES256); + AESNIXX_ENCRYPT_BLOCK_CTR(AES256); + AESNIXX_DECRYPT_BLOCK_CTR(AES256); } diff --git a/cxx/include/aesnixx/all.hpp b/cxx/include/aesnixx/all.hpp index 2bdf301..417d9ea 100644 --- a/cxx/include/aesnixx/all.hpp +++ b/cxx/include/aesnixx/all.hpp @@ -10,6 +10,7 @@ #include "aes.hpp" #include "algorithm.hpp" +#include "api.hpp" #include "data.hpp" #include "debug.hpp" #include "error.hpp" diff --git a/cxx/include/aesnixx/api.hpp b/cxx/include/aesnixx/api.hpp new file mode 100644 index 0000000..6995ae2 --- /dev/null +++ b/cxx/include/aesnixx/api.hpp @@ -0,0 +1,171 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "algorithm.hpp" +#include "mode.hpp" + +#include + +#include +#include + +namespace aesni +{ + template + struct Types; + + template + std::size_t get_number_of_rounds(); + + template + void from_string( + typename Types::Block&, + const char*); + + template + inline void from_string( + typename Types::Block& dest, + const std::string& src) + { + from_string(dest, src.c_str()); + } + + template + std::string to_string(const typename Types::Block&); + + template + std::string to_matrix_string(const typename Types::Block&); + + template + void from_string( + typename Types::Key&, + const char*); + + template + inline void from_string( + typename Types::Key& dest, + const std::string& src) + { + from_string(dest, src.c_str()); + } + + template + std::string to_string(const typename Types::Key&); + + template + inline void expand_key( + const typename Types::Key& key, + typename Types::RoundKeys& encryption_keys); + + template + inline void derive_decryption_keys( + const typename Types::RoundKeys& encryption_keys, + typename Types::RoundKeys& decryption_keys); + + template ::value>::type* = 0> + inline 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* = 0> + inline void encrypt_block( + const typename Types::Block& plaintext, + const typename Types::RoundKeys& round_keys, + typename Types::Block& ciphertext); + + template ::value>::type* = 0> + inline void encrypt_block( + const typename Types::Block& plaintext, + const typename Types::RoundKeys& round_keys, + typename Types::Block&, + typename Types::Block& ciphertext) + { + encrypt_block(plaintext, round_keys, ciphertext); + } + + template ::value>::type* = 0> + inline 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* = 0> + inline void decrypt_block( + const typename Types::Block& ciphertext, + const typename Types::RoundKeys& round_keys, + typename Types::Block& plaintext); + + template ::value>::type* = 0> + inline void decrypt_block( + const typename Types::Block& ciphertext, + const typename Types::RoundKeys& round_keys, + typename Types::Block&, + typename Types::Block& plaintext) + { + decrypt_block(ciphertext, round_keys, plaintext); + } + + template + struct EncryptWrapper + { + EncryptWrapper( + const typename Types::Key& key, + const typename Types::Block& iv) : iv(iv) + { + expand_key(key, encryption_keys); + } + + inline void encrypt_block( + const typename Types::Block& plaintext, + typename Types::Block& ciphertext) + { + aesni::encrypt_block( + plaintext, encryption_keys, iv, ciphertext); + } + + typename Types::Block iv; + typename Types::RoundKeys encryption_keys; + }; + + template + struct DecryptWrapper + { + DecryptWrapper( + const typename Types::Key& key, + const typename Types::Block& iv) : iv(iv) + { + typename Types::RoundKeys encryption_keys; + expand_key(key, encryption_keys); + + if (ModeUsesEncryptionKeysOnly::value) + { + decryption_keys = encryption_keys; + } + else + { + derive_decryption_keys(encryption_keys, decryption_keys); + } + } + + inline void decrypt_block( + const typename Types::Block& ciphertext, + typename Types::Block& plaintext) + { + aesni::decrypt_block( + ciphertext, decryption_keys, iv, plaintext); + } + + typename Types::Block iv; + typename Types::RoundKeys decryption_keys; + }; +} diff --git a/cxx/include/aesnixx/mode.hpp b/cxx/include/aesnixx/mode.hpp index a0af051..e19dbbd 100644 --- a/cxx/include/aesnixx/mode.hpp +++ b/cxx/include/aesnixx/mode.hpp @@ -45,4 +45,112 @@ namespace aesni { return mode != AESNI_ECB && mode != AESNI_CBC; } + +#define AESNIXX_ENCRYPT_BLOCK_ECB(prefix) \ + template <> \ + inline void encrypt_block( \ + const typename Types::Block& plaintext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& ciphertext) \ + { \ + ciphertext = aesni_## prefix ##_encrypt_block_ECB(plaintext, &encryption_keys); \ + } + +#define AESNIXX_DECRYPT_BLOCK_ECB(prefix) \ + template <> \ + inline void decrypt_block( \ + const typename Types::Block& ciphertext, \ + const typename Types::RoundKeys& decryption_keys, \ + typename Types::Block& plaintext) \ + { \ + plaintext = aesni_## prefix ##_decrypt_block_ECB(ciphertext, &decryption_keys); \ + } + +#define AESNIXX_ENCRYPT_BLOCK_CBC(prefix) \ + template <> \ + inline void encrypt_block( \ + const typename Types::Block& plaintext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& ciphertext) \ + { \ + ciphertext = aesni_## prefix ##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_DECRYPT_BLOCK_CBC(prefix) \ + template <> \ + inline void decrypt_block( \ + const typename Types::Block& ciphertext, \ + const typename Types::RoundKeys& decryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& plaintext) \ + { \ + plaintext = aesni_## prefix ##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \ + } + +#define AESNIXX_ENCRYPT_BLOCK_CFB(prefix) \ + template <> \ + inline void encrypt_block( \ + const typename Types::Block& plaintext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& ciphertext) \ + { \ + ciphertext = aesni_## prefix ##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_DECRYPT_BLOCK_CFB(prefix) \ + template <> \ + inline void decrypt_block( \ + const typename Types::Block& ciphertext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& plaintext) \ + { \ + plaintext = aesni_## prefix ##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_ENCRYPT_BLOCK_OFB(prefix) \ + template <> \ + inline void encrypt_block( \ + const typename Types::Block& plaintext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& ciphertext) \ + { \ + ciphertext = aesni_## prefix ##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_DECRYPT_BLOCK_OFB(prefix) \ + template <> \ + inline void decrypt_block( \ + const typename Types::Block& ciphertext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& plaintext) \ + { \ + plaintext = aesni_## prefix ##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_ENCRYPT_BLOCK_CTR(prefix) \ + template <> \ + inline void encrypt_block( \ + const typename Types::Block& plaintext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& ciphertext) \ + { \ + ciphertext = aesni_## prefix ##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \ + } + +#define AESNIXX_DECRYPT_BLOCK_CTR(prefix) \ + template <> \ + inline void decrypt_block( \ + const typename Types::Block& ciphertext, \ + const typename Types::RoundKeys& encryption_keys, \ + typename Types::Block& iv, \ + typename Types::Block& plaintext) \ + { \ + plaintext = aesni_## prefix ##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \ + } } diff --git a/utils/aes_block_common.hpp b/utils/aes_block_common.hpp index bdb6ac6..52b9ce3 100644 --- a/utils/aes_block_common.hpp +++ b/utils/aes_block_common.hpp @@ -151,126 +151,92 @@ namespace namespace { - void dump_block(const char* name, const aesni::aes::Block& block) + template + void dump_block(const char* name, const typename aesni::Types::Block& block) { - std::cout << name << ": " << aesni::aes::to_string(block) << "\n" << aesni::aes::to_matrix_string(block) << "\n"; + std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; } - void dump_plaintext(const aesni::aes::Block& block) + template + void dump_plaintext(const typename aesni::Types::Block& block) { - dump_block("Plaintext", block); + dump_block("Plaintext", block); } - template - void dump_key(const KeyT& key) + template + void dump_key(const typename aesni::Types::Key& key) { - std::cout << "Key: " << aesni::aes::to_string(key) << "\n\n"; + std::cout << "Key: " << aesni::to_string(key) << "\n\n"; } - void dump_ciphertext(const aesni::aes::Block& ciphertext) + template + void dump_ciphertext(const typename aesni::Types::Block& ciphertext) { - dump_block("Ciphertext", ciphertext); + dump_block("Ciphertext", ciphertext); } - void dump_iv(const aesni::aes::Block& iv) + template + void dump_iv(const typename aesni::Types::Block& iv) { - dump_block("Initialization vector", iv); + dump_block("Initialization vector", iv); } - void dump_next_iv(const aesni::aes::Block& next_iv) - { - dump_block("Next initialization vector", next_iv); - } - - template - void dump_round_keys(const char* name, const RoundKeysT& round_keys) + template + void dump_round_keys(const char* name, const typename aesni::Types::RoundKeys& round_keys) { std::cout << name << ":\n"; - for (std::size_t i = 0; i < aesni::aes::get_number_of_rounds(round_keys); ++i) - std::cout << "\t[" << i << "]: " << aesni::aes::to_string(round_keys.keys[i]) << "\n"; + for (std::size_t i = 0; i < aesni::get_number_of_rounds(); ++i) + std::cout << "\t[" << i << "]: " << aesni::to_string(round_keys.keys[i]) << "\n"; std::cout << "\n"; } - template - void dump_encryption_keys(const RoundKeysT& round_keys) + template + void dump_encryption_keys(const typename aesni::Types::RoundKeys& round_keys) { - dump_round_keys("Encryption round keys", round_keys); + dump_round_keys("Encryption round keys", round_keys); } - template - void dump_decryption_keys(const RoundKeysT& round_keys) + template + void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) { - dump_round_keys("Decryption round keys", round_keys); + dump_round_keys("Decryption round keys", round_keys); } - template - struct Dumper; - - template - struct Dumper - { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - dump_decryption_keys(encrypt.decryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt&) - { } - }; - - template - struct Dumper + template + void dump_wrapper( + const aesni::EncryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - dump_decryption_keys(encrypt.decryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt&) - { } - }; + dump_encryption_keys(wrapper.encryption_keys); + } - template - struct Dumper + template + void dump_wrapper( + const aesni::DecryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } - - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + dump_decryption_keys(wrapper.decryption_keys); + } - template - struct Dumper + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } + dump_block("Next initialization vector", wrapper.iv); + } - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper&) + { } - template - struct Dumper + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper& wrapper) { - static void dump_round_keys(const aesni::aes::Encrypt& encrypt) - { - dump_encryption_keys(encrypt.encryption_keys); - } + dump_block("Next initialization vector", wrapper.iv); + } - static void dump_next_iv(const aesni::aes::Encrypt& encrypt) - { - ::dump_next_iv(encrypt.iv); - } - }; + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper&) + { } } diff --git a/utils/aes_decrypt_block.cpp b/utils/aes_decrypt_block.cpp index bab5f50..0f41b47 100644 --- a/utils/aes_decrypt_block.cpp +++ b/utils/aes_decrypt_block.cpp @@ -25,48 +25,48 @@ namespace std::deque& ciphertexts, bool verbose = false) { - typename aesni::aes::Types::BlockT iv; + typename aesni::Types::Block iv; if (aesni::ModeRequiresInitializationVector()) { if (ciphertexts.empty()) return false; - aesni::aes::from_string(iv, ciphertexts.front()); + aesni::from_string(iv, ciphertexts.front()); ciphertexts.pop_front(); if (verbose) - dump_iv(iv); + dump_iv(iv); } - typename aesni::aes::Types::KeyT key; - aesni::aes::from_string(key, key_str); + typename aesni::Types::Key key; + aesni::from_string(key, key_str); if (verbose) - dump_key(key); + dump_key(key); - aesni::aes::Encrypt encrypt(key, iv); + aesni::DecryptWrapper decrypt(key, iv); if (verbose) - Dumper::dump_round_keys(encrypt); + dump_wrapper(decrypt); while (!ciphertexts.empty()) { - typename aesni::aes::Types::BlockT ciphertext; - aesni::aes::from_string(ciphertext, ciphertexts.front()); + typename aesni::Types::Block ciphertext, plaintext; + aesni::from_string(ciphertext, ciphertexts.front()); ciphertexts.pop_front(); - const auto plaintext = encrypt.decrypt(ciphertext); + decrypt.decrypt_block(ciphertext, plaintext); if (verbose) { - dump_ciphertext(ciphertext); - dump_plaintext(plaintext); - Dumper::dump_next_iv(encrypt); + dump_ciphertext(ciphertext); + dump_plaintext(plaintext); + dump_next_iv(decrypt); } else { - std::cout << aesni::aes::to_string(plaintext) << "\n"; + std::cout << aesni::to_string(plaintext) << "\n"; } } @@ -102,7 +102,7 @@ namespace } } - bool decrypt( + bool decrypt_using_cxx_api( aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key_str, @@ -125,32 +125,13 @@ namespace } } - bool decrypt_using_boxes( - aesni::Algorithm algorithm, + template + bool decrypt_using_boxes_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, aesni::Mode mode, const std::string& key, std::deque ciphertexts) { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::aes::from_string(algorithm_params.aes128_key, key); - break; - - case AESNI_AES192: - aesni::aes::from_string(algorithm_params.aes192_key, key); - break; - - case AESNI_AES256: - aesni::aes::from_string(algorithm_params.aes256_key, key); - break; - - default: - return false; - } - AesNI_BoxBlock iv; AesNI_BoxBlock* iv_ptr = nullptr; @@ -159,7 +140,7 @@ namespace if (ciphertexts.empty()) return false; - aesni::aes::from_string(iv.aes_block, ciphertexts.front()); + aesni::from_string(iv.aes_block, ciphertexts.front()); iv_ptr = &iv; ciphertexts.pop_front(); } @@ -176,7 +157,7 @@ namespace while (!ciphertexts.empty()) { AesNI_BoxBlock ciphertext; - aesni::aes::from_string(ciphertext.aes_block, ciphertexts.front()); + aesni::from_string(ciphertext.aes_block, ciphertexts.front()); ciphertexts.pop_front(); AesNI_BoxBlock plaintext; @@ -186,11 +167,44 @@ namespace &plaintext, aesni::ErrorDetailsThrowsInDestructor()); - std::cout << aesni::aes::to_string(plaintext.aes_block) << "\n"; + std::cout << aesni::to_string(plaintext.aes_block) << "\n"; } return true; } + + bool decrypt_using_boxes( + aesni::Algorithm algorithm, + aesni::Mode mode, + const std::string& key, + std::deque ciphertexts) + { + AesNI_BoxAlgorithmParams algorithm_params; + + switch (algorithm) + { + case AESNI_AES128: + aesni::from_string( + algorithm_params.aes128_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, key); + return decrypt_using_boxes_with_algorithm( + algorithm_params, mode, key, ciphertexts); + + default: + return false; + } + } } int main(int argc, char** argv) @@ -228,7 +242,7 @@ int main(int argc, char** argv) const auto success = cmd_parser.use_boxes() ? decrypt_using_boxes(algorithm, mode, key, ciphertexts) - : decrypt(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); + : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); if (!success) { 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) diff --git a/utils/aes_decrypt_file.cpp b/utils/aes_decrypt_file.cpp index 650ca5e..d456b67 100644 --- a/utils/aes_decrypt_file.cpp +++ b/utils/aes_decrypt_file.cpp @@ -16,6 +16,7 @@ #include +#include #include #include #include @@ -55,72 +56,28 @@ namespace ofs.open(path, std::ofstream::binary); ofs.write(src.data(), src.size()); } -} -int main(int argc, char** argv) -{ - try + template + bool decrypt_file_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) { - CommandLineParser cmd_parser("aes_encrypt_file.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]; @@ -131,9 +88,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()); @@ -159,9 +116,65 @@ int main(int argc, char** argv) aesni::ErrorDetailsThrowsInDestructor()); dest_buf.resize(dest_size); - write_file(dest_path, dest_buf); + return true; + } + + bool decrypt_file( + 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_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return decrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return decrypt_file_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_encrypt_file.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!decrypt_file(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) diff --git a/utils/aes_encrypt_block.cpp b/utils/aes_encrypt_block.cpp index b814e99..1b6b788 100644 --- a/utils/aes_encrypt_block.cpp +++ b/utils/aes_encrypt_block.cpp @@ -25,47 +25,47 @@ namespace std::deque& plaintexts, bool verbose = false) { - typename aesni::aes::Types::BlockT iv; + typename aesni::Types::Block iv; - if (aesni::ModeRequiresInitializationVector()) + if (aesni::ModeRequiresInitializationVector::value) { if (plaintexts.empty()) return false; - aesni::aes::from_string(iv, plaintexts.front()); + aesni::from_string(iv, plaintexts.front()); plaintexts.pop_front(); if (verbose) - dump_iv(iv); + dump_iv(iv); } - typename aesni::aes::Types::KeyT key; - aesni::aes::from_string(key, key_str); + typename aesni::Types::Key key; + aesni::from_string(key, key_str); if (verbose) - dump_key(key); + dump_key(key); - aesni::aes::Encrypt encrypt(key, iv); + aesni::EncryptWrapper encrypt(key, iv); if (verbose) - Dumper::dump_round_keys(encrypt); + dump_wrapper(encrypt); while (!plaintexts.empty()) { - typename aesni::aes::Types::BlockT plaintext; - aesni::aes::from_string(plaintext, plaintexts.front()); + typename aesni::Types::Block plaintext, ciphertext; + aesni::from_string(plaintext, plaintexts.front()); plaintexts.pop_front(); - const auto ciphertext = encrypt.encrypt(plaintext); + encrypt.encrypt_block(plaintext, ciphertext); if (verbose) { - dump_plaintext(plaintext); - dump_ciphertext(ciphertext); - Dumper::dump_next_iv(encrypt); + dump_plaintext(plaintext); + dump_ciphertext(ciphertext); + dump_next_iv(encrypt); } else { - std::cout << aesni::aes::to_string(ciphertext) << "\n"; + std::cout << aesni::to_string(ciphertext) << "\n"; } } @@ -101,11 +101,11 @@ namespace } } - bool encrypt( + bool encrypt_using_cxx_api( aesni::Algorithm algorithm, aesni::Mode mode, const std::string& key_str, - std::deque plaintexts, + std::deque& plaintexts, bool verbose = false) { switch (algorithm) @@ -124,32 +124,13 @@ namespace } } - bool encrypt_using_boxes( - aesni::Algorithm algorithm, + template + bool encrypt_using_boxes_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, aesni::Mode mode, const std::string& key, std::deque plaintexts) { - AesNI_BoxAlgorithmParams algorithm_params; - - switch (algorithm) - { - case AESNI_AES128: - aesni::aes::from_string(algorithm_params.aes128_key, key); - break; - - case AESNI_AES192: - aesni::aes::from_string(algorithm_params.aes192_key, key); - break; - - case AESNI_AES256: - aesni::aes::from_string(algorithm_params.aes256_key, key); - break; - - default: - return false; - } - AesNI_BoxBlock iv; AesNI_BoxBlock* iv_ptr = nullptr; @@ -158,7 +139,7 @@ namespace if (plaintexts.empty()) return false; - aesni::aes::from_string(iv.aes_block, plaintexts.front()); + aesni::from_string(iv.aes_block, plaintexts.front()); iv_ptr = &iv; plaintexts.pop_front(); } @@ -175,7 +156,7 @@ namespace while (!plaintexts.empty()) { AesNI_BoxBlock plaintext; - aesni::aes::from_string(plaintext.aes_block, plaintexts.front()); + aesni::from_string(plaintext.aes_block, plaintexts.front()); plaintexts.pop_front(); AesNI_BoxBlock ciphertext; @@ -185,11 +166,44 @@ namespace &ciphertext, aesni::ErrorDetailsThrowsInDestructor()); - std::cout << aesni::aes::to_string(ciphertext.aes_block) << "\n"; + 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) @@ -227,7 +241,7 @@ int main(int argc, char** argv) const auto success = cmd_parser.use_boxes() ? encrypt_using_boxes(algorithm, mode, key, plaintexts) - : encrypt(algorithm, mode, key, plaintexts, cmd_parser.verbose()); + : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose()); if (!success) { diff --git a/utils/aes_encrypt_bmp.cpp b/utils/aes_encrypt_bmp.cpp index f36cfb0..2615889 100644 --- a/utils/aes_encrypt_bmp.cpp +++ b/utils/aes_encrypt_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 encrypt_bmp_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) { - CommandLineParser cmd_parser("aes_encrypt_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()); @@ -170,6 +127,63 @@ int main(int argc, char** argv) write_file(dest_path, dest_buf); + return true; + } + + bool encrypt_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 encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return encrypt_bmp_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_encrypt_bmp.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!encrypt_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) diff --git a/utils/aes_encrypt_file.cpp b/utils/aes_encrypt_file.cpp index 93b7cbd..61b9ba1 100644 --- a/utils/aes_encrypt_file.cpp +++ b/utils/aes_encrypt_file.cpp @@ -16,6 +16,7 @@ #include +#include #include #include #include @@ -55,72 +56,28 @@ namespace ofs.open(path, std::ofstream::binary); ofs.write(src.data(), src.size()); } -} -int main(int argc, char** argv) -{ - try + template + bool encrypt_file_with_algorithm( + const AesNI_BoxAlgorithmParams& algorithm_params, + aesni::Mode mode, + std::deque& args) { - CommandLineParser cmd_parser("aes_encrypt_file.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 true; const auto src_path = args[0]; const auto dest_path = args[1]; @@ -131,9 +88,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()); @@ -159,9 +116,65 @@ int main(int argc, char** argv) aesni::ErrorDetailsThrowsInDestructor()); dest_buf.resize(dest_size); - write_file(dest_path, dest_buf); + return true; + } + + bool encrypt_file( + 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 encrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES192: + aesni::from_string( + algorithm_params.aes192_key, args.front()); + args.pop_front(); + return encrypt_file_with_algorithm( + algorithm_params, mode, args); + + case AESNI_AES256: + aesni::from_string( + algorithm_params.aes256_key, args.front()); + args.pop_front(); + return encrypt_file_with_algorithm( + algorithm_params, mode, args); + + default: + return false; + } + } +} + +int main(int argc, char** argv) +{ + try + { + CommandLineParser cmd_parser("aes_encrypt_file.exe"); + + if (!cmd_parser.parse_options(argc, argv)) + return 0; + + if (!encrypt_file(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