From 6cac3cb57cb4f430df80f8a21e358ad40ae59c89 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 13 Jun 2026 14:37:12 +0200 Subject: add .clang-format --- aesxx/include/aesxx/aes.hpp | 477 ++++++++++++++++++-------------------- aesxx/include/aesxx/algorithm.hpp | 7 +- aesxx/include/aesxx/api.hpp | 265 ++++++++++----------- aesxx/include/aesxx/box.hpp | 324 +++++++++++--------------- aesxx/include/aesxx/data.hpp | 79 +++---- aesxx/include/aesxx/debug.hpp | 262 +++++++++------------ aesxx/include/aesxx/error.hpp | 138 +++++------ aesxx/include/aesxx/mode.hpp | 214 ++++++++--------- 8 files changed, 790 insertions(+), 976 deletions(-) (limited to 'aesxx/include') diff --git a/aesxx/include/aesxx/aes.hpp b/aesxx/include/aesxx/aes.hpp index efac790..c3457c8 100644 --- a/aesxx/include/aesxx/aes.hpp +++ b/aesxx/include/aesxx/aes.hpp @@ -13,263 +13,226 @@ #include #include - #include -namespace aes -{ - namespace aes128 - { - typedef AES_AES128_Block Block; - typedef AES_AES128_RoundKeys RoundKeys; - typedef AES_AES128_Key Key; - } - - template <> - struct Types - { - typedef aes128::Block Block; - typedef aes128::RoundKeys RoundKeys; - typedef aes128::Key Key; - }; - - template <> - inline std::size_t get_number_of_rounds() - { - return 11; - } - - template <> - inline void from_string(aes128::Block& dest, const char* src) - { - aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes128::Block& src) - { - AES_AES128_BlockString str; - aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline std::string to_matrix_string(const aes128::Block& src) - { - AES_AES128_BlockMatrixString str; - aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void from_string(aes128::Key& dest, const char* src) - { - aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes128::Key& src) - { - AES_AES128_KeyString str; - aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void expand_key( - const aes128::Key& key, - aes128::RoundKeys& encryption_keys) - { - aes_AES128_expand_key(&key, &encryption_keys); - } - - template <> - inline void derive_decryption_keys( - const aes128::RoundKeys& encryption_keys, - aes128::RoundKeys& decryption_keys) - { - aes_AES128_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - AESXX_ENCRYPT_BLOCK_ECB(AES128); - AESXX_DECRYPT_BLOCK_ECB(AES128); - AESXX_ENCRYPT_BLOCK_CBC(AES128); - AESXX_DECRYPT_BLOCK_CBC(AES128); - AESXX_ENCRYPT_BLOCK_CFB(AES128); - AESXX_DECRYPT_BLOCK_CFB(AES128); - AESXX_ENCRYPT_BLOCK_OFB(AES128); - AESXX_DECRYPT_BLOCK_OFB(AES128); - AESXX_ENCRYPT_BLOCK_CTR(AES128); - AESXX_DECRYPT_BLOCK_CTR(AES128); - - namespace aes192 - { - typedef AES_AES192_Block Block; - typedef AES_AES192_RoundKeys RoundKeys; - typedef AES_AES192_Key Key; - } - - template <> - struct Types - { - typedef aes192::Block Block; - typedef aes192::RoundKeys RoundKeys; - typedef aes192::Key Key; - }; - - template <> - inline std::size_t get_number_of_rounds() - { - return 13; - } - - template <> - inline void from_string(aes192::Block& dest, const char* src) - { - aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes192::Block& src) - { - AES_AES192_BlockString str; - aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline std::string to_matrix_string(const aes192::Block& src) - { - AES_AES192_BlockMatrixString str; - aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void from_string(aes192::Key& dest, const char* src) - { - aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes192::Key& src) - { - AES_AES192_KeyString str; - aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void expand_key( - const aes192::Key& key, - aes192::RoundKeys& encryption_keys) - { - aes_AES192_expand_key(&key, &encryption_keys); - } - - template <> - inline void derive_decryption_keys( - const aes192::RoundKeys& encryption_keys, - aes192::RoundKeys& decryption_keys) - { - aes_AES192_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - AESXX_ENCRYPT_BLOCK_ECB(AES192); - AESXX_DECRYPT_BLOCK_ECB(AES192); - AESXX_ENCRYPT_BLOCK_CBC(AES192); - AESXX_DECRYPT_BLOCK_CBC(AES192); - AESXX_ENCRYPT_BLOCK_CFB(AES192); - AESXX_DECRYPT_BLOCK_CFB(AES192); - AESXX_ENCRYPT_BLOCK_OFB(AES192); - AESXX_DECRYPT_BLOCK_OFB(AES192); - AESXX_ENCRYPT_BLOCK_CTR(AES192); - AESXX_DECRYPT_BLOCK_CTR(AES192); - - namespace aes256 - { - typedef AES_AES256_Block Block; - typedef AES_AES256_RoundKeys RoundKeys; - typedef AES_AES256_Key Key; - } - - template <> - struct Types - { - typedef aes256::Block Block; - typedef aes256::RoundKeys RoundKeys; - typedef aes256::Key Key; - }; - - template <> - inline std::size_t get_number_of_rounds() - { - return 15; - } - - template <> - inline void from_string(aes256::Block& dest, const char* src) - { - aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes256::Block& src) - { - AES_AES256_BlockString str; - aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline std::string to_matrix_string(const aes256::Block& src) - { - AES_AES256_BlockMatrixString str; - aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void from_string(aes256::Key& dest, const char* src) - { - aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } - - template <> - inline std::string to_string(const aes256::Key& src) - { - AES_AES256_KeyString str; - aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } - - template <> - inline void expand_key( - const aes256::Key& key, - aes256::RoundKeys& encryption_keys) - { - aes_AES256_expand_key(&key, &encryption_keys); - } - - template <> - inline void derive_decryption_keys( - const aes256::RoundKeys& encryption_keys, - aes256::RoundKeys& decryption_keys) - { - aes_AES256_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } - - AESXX_ENCRYPT_BLOCK_ECB(AES256); - AESXX_DECRYPT_BLOCK_ECB(AES256); - AESXX_ENCRYPT_BLOCK_CBC(AES256); - AESXX_DECRYPT_BLOCK_CBC(AES256); - AESXX_ENCRYPT_BLOCK_CFB(AES256); - AESXX_DECRYPT_BLOCK_CFB(AES256); - AESXX_ENCRYPT_BLOCK_OFB(AES256); - AESXX_DECRYPT_BLOCK_OFB(AES256); - AESXX_ENCRYPT_BLOCK_CTR(AES256); - AESXX_DECRYPT_BLOCK_CTR(AES256); +namespace aes { +namespace aes128 { + +typedef AES_AES128_Block Block; +typedef AES_AES128_RoundKeys RoundKeys; +typedef AES_AES128_Key Key; + +} // namespace aes128 + +template <> +struct Types { + typedef aes128::Block Block; + typedef aes128::RoundKeys RoundKeys; + typedef aes128::Key Key; +}; + +template <> +inline std::size_t get_number_of_rounds() { + return 11; +} + +template <> +inline void from_string(aes128::Block& dest, const char* src) { + aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +} + +template <> +inline std::string to_string(const aes128::Block& src) { + AES_AES128_BlockString str; + aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline std::string to_matrix_string(const aes128::Block& src) { + AES_AES128_BlockMatrixString str; + aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void from_string(aes128::Key& dest, const char* src) { + aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +} + +template <> +inline std::string to_string(const aes128::Key& src) { + AES_AES128_KeyString str; + aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void expand_key(const aes128::Key& key, aes128::RoundKeys& encryption_keys) { + aes_AES128_expand_key(&key, &encryption_keys); +} + +template <> +inline void derive_decryption_keys(const aes128::RoundKeys& encryption_keys, + aes128::RoundKeys& decryption_keys) { + aes_AES128_derive_decryption_keys(&encryption_keys, &decryption_keys); +} + +AESXX_ENCRYPT_BLOCK_ECB(AES128); +AESXX_DECRYPT_BLOCK_ECB(AES128); +AESXX_ENCRYPT_BLOCK_CBC(AES128); +AESXX_DECRYPT_BLOCK_CBC(AES128); +AESXX_ENCRYPT_BLOCK_CFB(AES128); +AESXX_DECRYPT_BLOCK_CFB(AES128); +AESXX_ENCRYPT_BLOCK_OFB(AES128); +AESXX_DECRYPT_BLOCK_OFB(AES128); +AESXX_ENCRYPT_BLOCK_CTR(AES128); +AESXX_DECRYPT_BLOCK_CTR(AES128); + +namespace aes192 { + +typedef AES_AES192_Block Block; +typedef AES_AES192_RoundKeys RoundKeys; +typedef AES_AES192_Key Key; + +} // namespace aes192 + +template <> +struct Types { + typedef aes192::Block Block; + typedef aes192::RoundKeys RoundKeys; + typedef aes192::Key Key; +}; + +template <> +inline std::size_t get_number_of_rounds() { + return 13; +} + +template <> +inline void from_string(aes192::Block& dest, const char* src) { + aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +} + +template <> +inline std::string to_string(const aes192::Block& src) { + AES_AES192_BlockString str; + aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline std::string to_matrix_string(const aes192::Block& src) { + AES_AES192_BlockMatrixString str; + aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void from_string(aes192::Key& dest, const char* src) { + aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +} + +template <> +inline std::string to_string(const aes192::Key& src) { + AES_AES192_KeyString str; + aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void expand_key(const aes192::Key& key, aes192::RoundKeys& encryption_keys) { + aes_AES192_expand_key(&key, &encryption_keys); +} + +template <> +inline void derive_decryption_keys(const aes192::RoundKeys& encryption_keys, + aes192::RoundKeys& decryption_keys) { + aes_AES192_derive_decryption_keys(&encryption_keys, &decryption_keys); +} + +AESXX_ENCRYPT_BLOCK_ECB(AES192); +AESXX_DECRYPT_BLOCK_ECB(AES192); +AESXX_ENCRYPT_BLOCK_CBC(AES192); +AESXX_DECRYPT_BLOCK_CBC(AES192); +AESXX_ENCRYPT_BLOCK_CFB(AES192); +AESXX_DECRYPT_BLOCK_CFB(AES192); +AESXX_ENCRYPT_BLOCK_OFB(AES192); +AESXX_DECRYPT_BLOCK_OFB(AES192); +AESXX_ENCRYPT_BLOCK_CTR(AES192); +AESXX_DECRYPT_BLOCK_CTR(AES192); + +namespace aes256 { + +typedef AES_AES256_Block Block; +typedef AES_AES256_RoundKeys RoundKeys; +typedef AES_AES256_Key Key; + +} // namespace aes256 + +template <> +struct Types { + typedef aes256::Block Block; + typedef aes256::RoundKeys RoundKeys; + typedef aes256::Key Key; +}; + +template <> +inline std::size_t get_number_of_rounds() { + return 15; +} + +template <> +inline void from_string(aes256::Block& dest, const char* src) { + aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); } + +template <> +inline std::string to_string(const aes256::Block& src) { + AES_AES256_BlockString str; + aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline std::string to_matrix_string(const aes256::Block& src) { + AES_AES256_BlockMatrixString str; + aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void from_string(aes256::Key& dest, const char* src) { + aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +} + +template <> +inline std::string to_string(const aes256::Key& src) { + AES_AES256_KeyString str; + aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void expand_key(const aes256::Key& key, aes256::RoundKeys& encryption_keys) { + aes_AES256_expand_key(&key, &encryption_keys); +} + +template <> +inline void derive_decryption_keys(const aes256::RoundKeys& encryption_keys, + aes256::RoundKeys& decryption_keys) { + aes_AES256_derive_decryption_keys(&encryption_keys, &decryption_keys); +} + +AESXX_ENCRYPT_BLOCK_ECB(AES256); +AESXX_DECRYPT_BLOCK_ECB(AES256); +AESXX_ENCRYPT_BLOCK_CBC(AES256); +AESXX_DECRYPT_BLOCK_CBC(AES256); +AESXX_ENCRYPT_BLOCK_CFB(AES256); +AESXX_DECRYPT_BLOCK_CFB(AES256); +AESXX_ENCRYPT_BLOCK_OFB(AES256); +AESXX_DECRYPT_BLOCK_OFB(AES256); +AESXX_ENCRYPT_BLOCK_CTR(AES256); +AESXX_DECRYPT_BLOCK_CTR(AES256); + +} // namespace aes diff --git a/aesxx/include/aesxx/algorithm.hpp b/aesxx/include/aesxx/algorithm.hpp index 823e57f..933cd5a 100644 --- a/aesxx/include/aesxx/algorithm.hpp +++ b/aesxx/include/aesxx/algorithm.hpp @@ -7,7 +7,8 @@ #include -namespace aes -{ - typedef AES_Algorithm Algorithm; +namespace aes { + +typedef AES_Algorithm Algorithm; + } diff --git a/aesxx/include/aesxx/api.hpp b/aesxx/include/aesxx/api.hpp index 68112bf..759ff0c 100644 --- a/aesxx/include/aesxx/api.hpp +++ b/aesxx/include/aesxx/api.hpp @@ -9,166 +9,139 @@ #include "mode.hpp" #include - #include #include -namespace aes -{ - template - struct Types; - - template - std::size_t get_number_of_rounds(); - - template - void from_string( - typename Types::Block&, - const char*); - - template - void from_string( - typename Types::Block& dest, - const std::string& src) - { - from_string(dest, src.c_str()); - } +namespace aes { - template - std::string to_string(const typename Types::Block&); +template +struct Types; - template - std::string to_matrix_string(const typename Types::Block&); +template +std::size_t get_number_of_rounds(); - template - void from_string( - typename Types::Key&, - const char*); +template +void from_string(typename Types::Block&, const char*); - template - void from_string( - typename Types::Key& dest, - const std::string& src) - { - from_string(dest, src.c_str()); - } +template +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::Key&); - - template - void expand_key( - const typename Types::Key& key, - typename Types::RoundKeys& encryption_keys); - - template - void derive_decryption_keys( - const typename Types::RoundKeys& encryption_keys, - typename Types::RoundKeys& decryption_keys); - - template ::value>::type* = nullptr> - 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> - void encrypt_block( - const typename Types::Block& plaintext, - const typename Types::RoundKeys& round_keys, - typename Types::Block& ciphertext); - - template ::value>::type* = nullptr> - 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 +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 +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 +void expand_key(const typename Types::Key& key, + typename Types::RoundKeys& encryption_keys); + +template +void derive_decryption_keys(const typename Types::RoundKeys& encryption_keys, + typename Types::RoundKeys& decryption_keys); + +template ::value>::type* = nullptr> +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> +void encrypt_block(const typename Types::Block& plaintext, + const typename Types::RoundKeys& round_keys, + typename Types::Block& ciphertext); + +template ::value>::type* = nullptr> +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* = nullptr> - 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> - void decrypt_block( - const typename Types::Block& ciphertext, - const typename Types::RoundKeys& round_keys, - typename Types::Block& plaintext); - - template ::value>::type* = nullptr> - 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 ::value>::type* = nullptr> +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> +void decrypt_block(const typename Types::Block& ciphertext, + const typename Types::RoundKeys& round_keys, + typename Types::Block& plaintext); + +template ::value>::type* = nullptr> +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); } - template - struct EncryptWrapper - { - EncryptWrapper( - const typename Types::Key& key, - const typename Types::Block& iv) : iv{iv} - { - expand_key(key, encryption_keys); - } + void encrypt_block(const typename Types::Block& plaintext, + typename Types::Block& ciphertext) { + aes::encrypt_block(plaintext, encryption_keys, iv, ciphertext); + } - void encrypt_block( - const typename Types::Block& plaintext, - typename Types::Block& ciphertext) - { - aes::encrypt_block( - plaintext, encryption_keys, iv, ciphertext); - } + typename Types::Block iv; + typename Types::RoundKeys encryption_keys; +}; - typename Types::Block iv; +template +struct DecryptWrapper { + DecryptWrapper(const typename Types::Key& key, + const typename Types::Block& iv) + : iv{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); - } - } + expand_key(key, encryption_keys); - void decrypt_block( - const typename Types::Block& ciphertext, - typename Types::Block& plaintext) - { - aes::decrypt_block( - ciphertext, decryption_keys, iv, plaintext); + if (ModeUsesEncryptionKeysOnly::value) { + decryption_keys = encryption_keys; + } else { + derive_decryption_keys(encryption_keys, decryption_keys); } + } - typename Types::Block iv; - typename Types::RoundKeys decryption_keys; - }; -} + void decrypt_block(const typename Types::Block& ciphertext, + typename Types::Block& plaintext) { + aes::decrypt_block(ciphertext, decryption_keys, iv, plaintext); + } + + typename Types::Block iv; + typename Types::RoundKeys decryption_keys; +}; + +} // namespace aes diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index 076407c..540688b 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -12,196 +12,140 @@ #include #include - #include #include -namespace aes -{ - class Box - { - public: - typedef AES_BoxBlock Block; - typedef AES_BoxKey Key; - - static std::string format_key(const Key& src, Algorithm algorithm) - { - AES_BoxKeyString str; - aes_box_format_key( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast(&str); - } - - static std::string format_block(const Block& src, Algorithm algorithm) - { - AES_BoxBlockString str; - aes_box_format_block( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast(&str); - } - - static void parse_block( - Block& dest, - Algorithm algorithm, - const char* src) - { - aes_box_parse_block(&dest, algorithm, src, - ErrorDetailsThrowsInDestructor{}); - } - - static void parse_block( - Block& dest, - Algorithm algorithm, - const std::string& src) - { - parse_block(dest, algorithm, src.c_str()); - } - - static void parse_key( - Key& dest, - Algorithm algorithm, - const char* src) - { - aes_box_parse_key(&dest, algorithm, src, - ErrorDetailsThrowsInDestructor{}); - } - - static void parse_key( - Key& dest, - Algorithm algorithm, - const std::string& src) - { - parse_key(dest, algorithm, src.c_str()); - } - - Box(Algorithm algorithm, const Key& key) - : algorithm{algorithm} - , mode{AES_ECB} - { - aes_box_init(&impl, algorithm, &key, mode, nullptr, - ErrorDetailsThrowsInDestructor{}); - } - - Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) - : algorithm{algorithm} - , mode{mode} - { - aes_box_init(&impl, algorithm, &key, mode, &iv, - ErrorDetailsThrowsInDestructor{}); - } - - void encrypt_block(const Block& plaintext, Block& ciphertext) - { - aes_box_encrypt_block( - &impl, &plaintext, &ciphertext, - ErrorDetailsThrowsInDestructor{}); - } - - void decrypt_block(const Block& ciphertext, Block& plaintext) - { - aes_box_decrypt_block( - &impl, &ciphertext, &plaintext, - ErrorDetailsThrowsInDestructor{}); - } - - std::vector encrypt_buffer( - const void* src_buf, - std::size_t src_size) - { - std::size_t dest_size = 0; - - aes_box_encrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aes_box_encrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); - - dest_buf.resize(dest_size); - return dest_buf; - } - - std::vector decrypt_buffer( - const void* src_buf, - std::size_t src_size) - { - std::size_t dest_size = 0; - - aes_box_decrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aes_box_decrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); - - dest_buf.resize(dest_size); - return dest_buf; - } - - std::string format_block(const Block& src) - { - return format_block(src, get_algorithm()); - } - - std::string format_key(const Key& src) - { - return format_key(src, get_algorithm()); - } - - void parse_block(Block& dest, const char* src) - { - parse_block(dest, get_algorithm(), src); - } - - void parse_block(Block& dest, const std::string& src) - { - parse_block(dest, src.c_str()); - } - - void parse_key(Key& dest, const char* src) - { - parse_key(dest, get_algorithm(), src); - } - - void parse_key(Key& dest, const std::string& src) - { - parse_key(dest, src.c_str()); - } - - Algorithm get_algorithm() const { return algorithm; } - - Mode get_mode() const { return mode; } - - private: - Key key; - - Algorithm algorithm; - Mode mode; - - AES_Box impl; - }; -} +namespace aes { + +class Box { +public: + typedef AES_BoxBlock Block; + typedef AES_BoxKey Key; + + static std::string format_key(const Key& src, Algorithm algorithm) { + AES_BoxKeyString str; + aes_box_format_key(&str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast(&str); + } + + static std::string format_block(const Block& src, Algorithm algorithm) { + AES_BoxBlockString str; + aes_box_format_block(&str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast(&str); + } + + static void parse_block(Block& dest, Algorithm algorithm, const char* src) { + aes_box_parse_block(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); + } + + static void parse_block(Block& dest, Algorithm algorithm, const std::string& src) { + parse_block(dest, algorithm, src.c_str()); + } + + static void parse_key(Key& dest, Algorithm algorithm, const char* src) { + aes_box_parse_key(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); + } + + static void parse_key(Key& dest, Algorithm algorithm, const std::string& src) { + parse_key(dest, algorithm, src.c_str()); + } + + Box(Algorithm algorithm, const Key& key) : algorithm{algorithm}, mode{AES_ECB} { + aes_box_init(&impl, algorithm, &key, mode, nullptr, ErrorDetailsThrowsInDestructor{}); + } + + Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) + : algorithm{algorithm}, mode{mode} { + aes_box_init(&impl, algorithm, &key, mode, &iv, ErrorDetailsThrowsInDestructor{}); + } + + void encrypt_block(const Block& plaintext, Block& ciphertext) { + aes_box_encrypt_block(&impl, &plaintext, &ciphertext, ErrorDetailsThrowsInDestructor{}); + } + + void decrypt_block(const Block& ciphertext, Block& plaintext) { + aes_box_decrypt_block(&impl, &ciphertext, &plaintext, ErrorDetailsThrowsInDestructor{}); + } + + std::vector encrypt_buffer(const void* src_buf, std::size_t src_size) { + std::size_t dest_size = 0; + + aes_box_encrypt_buffer( + &impl, src_buf, src_size, nullptr, &dest_size, aes::ErrorDetailsThrowsInDestructor{}); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aes_box_encrypt_buffer(&impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aes::ErrorDetailsThrowsInDestructor{}); + + dest_buf.resize(dest_size); + return dest_buf; + } + + std::vector decrypt_buffer(const void* src_buf, std::size_t src_size) { + std::size_t dest_size = 0; + + aes_box_decrypt_buffer( + &impl, src_buf, src_size, nullptr, &dest_size, aes::ErrorDetailsThrowsInDestructor{}); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aes_box_decrypt_buffer(&impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aes::ErrorDetailsThrowsInDestructor{}); + + dest_buf.resize(dest_size); + return dest_buf; + } + + std::string format_block(const Block& src) { + return format_block(src, get_algorithm()); + } + + std::string format_key(const Key& src) { + return format_key(src, get_algorithm()); + } + + void parse_block(Block& dest, const char* src) { + parse_block(dest, get_algorithm(), src); + } + + void parse_block(Block& dest, const std::string& src) { + parse_block(dest, src.c_str()); + } + + void parse_key(Key& dest, const char* src) { + parse_key(dest, get_algorithm(), src); + } + + void parse_key(Key& dest, const std::string& src) { + parse_key(dest, src.c_str()); + } + + Algorithm get_algorithm() const { + return algorithm; + } + + Mode get_mode() const { + return mode; + } + +private: + Key key; + + Algorithm algorithm; + Mode mode; + + AES_Box impl; +}; + +} // namespace aes diff --git a/aesxx/include/aesxx/data.hpp b/aesxx/include/aesxx/data.hpp index f36df5e..e381995 100644 --- a/aesxx/include/aesxx/data.hpp +++ b/aesxx/include/aesxx/data.hpp @@ -9,47 +9,40 @@ #include -namespace aes -{ - typedef AES_Block128 Block128; - - inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) - { - dest = aes_make_block128(hi3, hi2, lo1, lo0); - } - - inline void load_block(Block128& dest, const void* src) - { - dest = aes_load_block128(src); - } - - inline void load_block_aligned(Block128& dest, const void* src) - { - dest = aes_load_block128_aligned(src); - } - - inline void store_block(void* dest, Block128& src) - { - aes_store_block128(dest, src); - } - - inline void store_block_aligned(void* dest, Block128& src) - { - aes_store_block128_aligned(dest, src); - } - - inline Block128 xor_blocks(Block128& a, Block128& b) - { - return aes_xor_block128(a, b); - } - - inline Block128 reverse_byte_order(Block128& block) - { - return aes_reverse_byte_order_block128(block); - } - - inline Block128 inc_block(Block128& block) - { - return aes_inc_block128(block); - } +namespace aes { + +typedef AES_Block128 Block128; + +inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) { + dest = aes_make_block128(hi3, hi2, lo1, lo0); } + +inline void load_block(Block128& dest, const void* src) { + dest = aes_load_block128(src); +} + +inline void load_block_aligned(Block128& dest, const void* src) { + dest = aes_load_block128_aligned(src); +} + +inline void store_block(void* dest, Block128& src) { + aes_store_block128(dest, src); +} + +inline void store_block_aligned(void* dest, Block128& src) { + aes_store_block128_aligned(dest, src); +} + +inline Block128 xor_blocks(Block128& a, Block128& b) { + return aes_xor_block128(a, b); +} + +inline Block128 reverse_byte_order(Block128& block) { + return aes_reverse_byte_order_block128(block); +} + +inline Block128 inc_block(Block128& block) { + return aes_inc_block128(block); +} + +} // namespace aes diff --git a/aesxx/include/aesxx/debug.hpp b/aesxx/include/aesxx/debug.hpp index d7d5d15..d54605d 100644 --- a/aesxx/include/aesxx/debug.hpp +++ b/aesxx/include/aesxx/debug.hpp @@ -7,168 +7,134 @@ #ifdef WIN32 #include + #include #pragma comment(lib, "dbghelp.lib") #endif #include - #include #include -namespace aes -{ - namespace aux - { - class CallStackFormatter - { - public: - CallStackFormatter() = default; - - std::string format_address(const void* addr) const - { - #ifdef WIN32 - return format_address_win32(addr); - #else - return format_address_fallback(addr); - #endif - } +namespace aes { +namespace aux { - private: - template - static std::string put_between_brackets(const T& x) - { - std::ostringstream oss; - oss << "[" << x << "]"; - return oss.str(); - } +class CallStackFormatter { +public: + CallStackFormatter() = default; - template - static std::string stringify(const T& x) - { - std::ostringstream oss; - oss << x; - return oss.str(); - } + std::string format_address(const void* addr) const { +#ifdef WIN32 + return format_address_win32(addr); +#else + return format_address_fallback(addr); +#endif + } - static std::string format_address_fallback(const void* addr) - { - return put_between_brackets(addr); - } +private: + template + static std::string put_between_brackets(const T& x) { + std::ostringstream oss; + oss << "[" << x << "]"; + return oss.str(); + } - static std::string format_module( - const std::string& module_name, - const void* offset = nullptr) - { - if (offset == nullptr) - return put_between_brackets(module_name); - else - return put_between_brackets(module_name + "+" + stringify(offset)); - } + template + static std::string stringify(const T& x) { + std::ostringstream oss; + oss << x; + return oss.str(); + } - static std::string format_symbol( - const std::string& symbol_name, - const void* offset = nullptr) - { - return format_module(symbol_name, offset); - } + static std::string format_address_fallback(const void* addr) { + return put_between_brackets(addr); + } - static std::string format_symbol( - const std::string& module_name, - const std::string& symbol_name, - const void* offset = nullptr) - { - return format_symbol(module_name + "!" + symbol_name, offset); - } + static std::string format_module(const std::string& module_name, const void* offset = nullptr) { + if (offset == nullptr) + return put_between_brackets(module_name); + else + return put_between_brackets(module_name + "+" + stringify(offset)); + } + + static std::string format_symbol(const std::string& symbol_name, const void* offset = nullptr) { + return format_module(symbol_name, offset); + } - #ifdef WIN32 - class DbgHelp - { - public: - DbgHelp() - { - initialized_flag = SymInitialize(GetCurrentProcess(), NULL, TRUE) != FALSE; - } - - bool initialized() const - { - return initialized_flag; - } - - ~DbgHelp() - { - if (initialized_flag) - SymCleanup(GetCurrentProcess()); - } - - private: - bool initialized_flag = false; - - DbgHelp(const DbgHelp&) = delete; - DbgHelp& operator=(const DbgHelp&) = delete; - }; - - DbgHelp dbghelp; - - std::string format_address_win32(const void* addr) const - { - if (!dbghelp.initialized()) - return format_address_fallback(addr); - - DWORD64 symbol_info_buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; - const auto symbol_info = reinterpret_cast(symbol_info_buf); - symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); - symbol_info->MaxNameLen = MAX_SYM_NAME; - - IMAGEHLP_MODULE64 module_info; - module_info.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); - - DWORD64 symbol_offset; - - const auto symbol_resolved = SymFromAddr( - GetCurrentProcess(), - reinterpret_cast(addr), - &symbol_offset, - symbol_info); - - if (symbol_resolved) - { - const auto module_resolved = SymGetModuleInfo64( - GetCurrentProcess(), - symbol_info->ModBase, - &module_info); - - if (module_resolved) - { - return format_symbol( - module_info.ModuleName, - symbol_info->Name, - reinterpret_cast(symbol_offset)); - } - else - { - return format_symbol(symbol_info->Name, addr); - } - } - else - { - const auto module_resolved = SymGetModuleInfo64( - GetCurrentProcess(), - reinterpret_cast(addr), - &module_info); - - if (module_resolved) - { - const auto module_offset = reinterpret_cast(addr) - module_info.BaseOfImage; - return format_module(module_info.ModuleName, module_offset); - } - else - { - return format_address_fallback(addr); - } - } + static std::string format_symbol(const std::string& module_name, + const std::string& symbol_name, + const void* offset = nullptr) { + return format_symbol(module_name + "!" + symbol_name, offset); + } + +#ifdef WIN32 + class DbgHelp { + public: + DbgHelp() { + initialized_flag = SymInitialize(GetCurrentProcess(), NULL, TRUE) != FALSE; + } + + bool initialized() const { + return initialized_flag; + } + + ~DbgHelp() { + if (initialized_flag) + SymCleanup(GetCurrentProcess()); + } + + private: + bool initialized_flag = false; + + DbgHelp(const DbgHelp&) = delete; + DbgHelp& operator=(const DbgHelp&) = delete; + }; + + DbgHelp dbghelp; + + std::string format_address_win32(const void* addr) const { + if (!dbghelp.initialized()) + return format_address_fallback(addr); + + DWORD64 symbol_info_buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; + const auto symbol_info = reinterpret_cast(symbol_info_buf); + symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); + symbol_info->MaxNameLen = MAX_SYM_NAME; + + IMAGEHLP_MODULE64 module_info; + module_info.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); + + DWORD64 symbol_offset; + + const auto symbol_resolved = SymFromAddr( + GetCurrentProcess(), reinterpret_cast(addr), &symbol_offset, symbol_info); + + if (symbol_resolved) { + const auto module_resolved = + SymGetModuleInfo64(GetCurrentProcess(), symbol_info->ModBase, &module_info); + + if (module_resolved) { + return format_symbol(module_info.ModuleName, + symbol_info->Name, + reinterpret_cast(symbol_offset)); + } else { + return format_symbol(symbol_info->Name, addr); } - #endif - }; + } else { + const auto module_resolved = SymGetModuleInfo64( + GetCurrentProcess(), reinterpret_cast(addr), &module_info); + + if (module_resolved) { + const auto module_offset = + reinterpret_cast(addr) - module_info.BaseOfImage; + return format_module(module_info.ModuleName, module_offset); + } else { + return format_address_fallback(addr); + } + } } -} +#endif +}; + +} // namespace aux +} // namespace aes diff --git a/aesxx/include/aesxx/error.hpp b/aesxx/include/aesxx/error.hpp index 4d2c783..e5c5803 100644 --- a/aesxx/include/aesxx/error.hpp +++ b/aesxx/include/aesxx/error.hpp @@ -11,94 +11,84 @@ #include +#include #include #include - -#include #include #include #include #include #include -namespace aes -{ - class Error : public std::runtime_error - { - public: - Error(const AES_ErrorDetails& err_details) - : std::runtime_error{format_error_message(err_details)} - { - fill_call_stack(err_details); - } - - void for_each_addr( - const std::function& callback) const - { - aux::CallStackFormatter formatter; - - std::for_each( - call_stack, - call_stack + call_stack_len, - [&formatter, &callback] (const void* addr) - { +namespace aes { + +class Error : public std::runtime_error { +public: + Error(const AES_ErrorDetails& err_details) + : std::runtime_error{format_error_message(err_details)} { + fill_call_stack(err_details); + } + + void for_each_addr(const std::function& callback) const { + aux::CallStackFormatter formatter; + + std::for_each( + call_stack, call_stack + call_stack_len, [&formatter, &callback](const void* addr) { callback(addr, formatter.format_address(addr)); }); - } - - private: - static std::string format_error_message(const AES_ErrorDetails& err_details) - { - std::vector buf; - buf.resize(aes_format_error(&err_details, NULL, 0)); - aes_format_error(&err_details, buf.data(), buf.size()); - return {buf.begin(), buf.end()}; - } - - void fill_call_stack(const AES_ErrorDetails& err_details) - { - call_stack_len = err_details.call_stack_len; - - if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) - call_stack_len = AES_MAX_CALL_STACK_LENGTH; - - std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); - } - - std::size_t call_stack_len = 0; - const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; - }; - - inline std::ostream& operator<<(std::ostream& os, const Error& error) - { - os << "AES error: " << error.what() << '\n'; - os << "Call stack:\n"; - error.for_each_addr([&os] (const void* addr, const std::string& name) - { - os << '\t' << addr << ' ' << name << '\n'; - }); - return os; } - class ErrorDetailsThrowsInDestructor - { - public: - ErrorDetailsThrowsInDestructor() - { - aes_success(get()); - } +private: + static std::string format_error_message(const AES_ErrorDetails& err_details) { + std::vector buf; + buf.resize(aes_format_error(&err_details, NULL, 0)); + aes_format_error(&err_details, buf.data(), buf.size()); + return {buf.begin(), buf.end()}; + } - ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) - { - if (aes_is_error(aes_get_error_code(get()))) - throw Error(impl); - } + void fill_call_stack(const AES_ErrorDetails& err_details) { + call_stack_len = err_details.call_stack_len; - AES_ErrorDetails* get() { return &impl; } + if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) + call_stack_len = AES_MAX_CALL_STACK_LENGTH; - operator AES_ErrorDetails*() { return get(); } + std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); + } - private: - AES_ErrorDetails impl; - }; + std::size_t call_stack_len = 0; + const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; +}; + +inline std::ostream& operator<<(std::ostream& os, const Error& error) { + os << "AES error: " << error.what() << '\n'; + os << "Call stack:\n"; + error.for_each_addr([&os](const void* addr, const std::string& name) { + os << '\t' << addr << ' ' << name << '\n'; + }); + return os; } + +class ErrorDetailsThrowsInDestructor { +public: + ErrorDetailsThrowsInDestructor() { + aes_success(get()); + } + + ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) { + if (aes_is_error(aes_get_error_code(get()))) + throw Error(impl); + } + + AES_ErrorDetails* get() { + return &impl; + } + + operator AES_ErrorDetails*() { + return get(); + } + +private: + AES_ErrorDetails impl; +}; + +} // namespace aes diff --git a/aesxx/include/aesxx/mode.hpp b/aesxx/include/aesxx/mode.hpp index 1c6c4cf..999db3a 100644 --- a/aesxx/include/aesxx/mode.hpp +++ b/aesxx/include/aesxx/mode.hpp @@ -9,145 +9,129 @@ #include -namespace aes -{ - typedef AES_Mode Mode; +namespace aes { - template - struct ModeRequiresInitVector : public std::true_type - { }; +typedef AES_Mode Mode; - template <> - struct ModeRequiresInitVector : public std::false_type - { }; +template +struct ModeRequiresInitVector : public std::true_type {}; - template - struct ModeUsesEncryptionKeysOnly : public std::true_type - { }; +template <> +struct ModeRequiresInitVector : public std::false_type {}; - inline bool mode_requires_init_vector(Mode mode) - { - return mode != AES_ECB; - } +template +struct ModeUsesEncryptionKeysOnly : public std::true_type {}; - template <> - struct ModeUsesEncryptionKeysOnly : public std::false_type - { }; +inline bool mode_requires_init_vector(Mode mode) { + return mode != AES_ECB; +} - template <> - struct ModeUsesEncryptionKeysOnly : public std::false_type - { }; +template <> +struct ModeUsesEncryptionKeysOnly : public std::false_type {}; - inline bool mode_uses_encryption_keys_only(Mode mode) - { - return mode != AES_ECB && mode != AES_CBC; - } +template <> +struct ModeUsesEncryptionKeysOnly : public std::false_type {}; -#define AESXX_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 = aes_## prefix ##_encrypt_block_ECB(plaintext, &encryption_keys); \ +inline bool mode_uses_encryption_keys_only(Mode mode) { + return mode != AES_ECB && mode != AES_CBC; +} + +#define AESXX_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 = aes_##prefix##_encrypt_block_ECB(plaintext, &encryption_keys); \ } -#define AESXX_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 = aes_## prefix ##_decrypt_block_ECB(ciphertext, &decryption_keys); \ +#define AESXX_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 = aes_##prefix##_decrypt_block_ECB(ciphertext, &decryption_keys); \ } -#define AESXX_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 = aes_## prefix ##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_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 = aes_## prefix ##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_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 = aes_##prefix##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \ } -} + +} // namespace aes -- cgit v1.3