From aebc96e6efc369c09a95fb641ca90935930cf19b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 19 May 2016 04:13:47 +0300 Subject: rename the project --- cxx/CMakeLists.txt | 6 +- cxx/include/aesnixx/aes.hpp | 278 -------------------------------------- cxx/include/aesnixx/algorithm.hpp | 16 --- cxx/include/aesnixx/all.hpp | 18 --- cxx/include/aesnixx/api.hpp | 171 ----------------------- cxx/include/aesnixx/box.hpp | 212 ----------------------------- cxx/include/aesnixx/data.hpp | 58 -------- cxx/include/aesnixx/debug.hpp | 177 ------------------------ cxx/include/aesnixx/error.hpp | 98 -------------- cxx/include/aesnixx/mode.hpp | 156 --------------------- cxx/include/aesxx/aes.hpp | 278 ++++++++++++++++++++++++++++++++++++++ cxx/include/aesxx/algorithm.hpp | 16 +++ cxx/include/aesxx/all.hpp | 18 +++ cxx/include/aesxx/api.hpp | 171 +++++++++++++++++++++++ cxx/include/aesxx/box.hpp | 212 +++++++++++++++++++++++++++++ cxx/include/aesxx/data.hpp | 58 ++++++++ cxx/include/aesxx/debug.hpp | 177 ++++++++++++++++++++++++ cxx/include/aesxx/error.hpp | 98 ++++++++++++++ cxx/include/aesxx/mode.hpp | 156 +++++++++++++++++++++ 19 files changed, 1187 insertions(+), 1187 deletions(-) delete mode 100644 cxx/include/aesnixx/aes.hpp delete mode 100644 cxx/include/aesnixx/algorithm.hpp delete mode 100644 cxx/include/aesnixx/all.hpp delete mode 100644 cxx/include/aesnixx/api.hpp delete mode 100644 cxx/include/aesnixx/box.hpp delete mode 100644 cxx/include/aesnixx/data.hpp delete mode 100644 cxx/include/aesnixx/debug.hpp delete mode 100644 cxx/include/aesnixx/error.hpp delete mode 100644 cxx/include/aesnixx/mode.hpp create mode 100644 cxx/include/aesxx/aes.hpp create mode 100644 cxx/include/aesxx/algorithm.hpp create mode 100644 cxx/include/aesxx/all.hpp create mode 100644 cxx/include/aesxx/api.hpp create mode 100644 cxx/include/aesxx/box.hpp create mode 100644 cxx/include/aesxx/data.hpp create mode 100644 cxx/include/aesxx/debug.hpp create mode 100644 cxx/include/aesxx/error.hpp create mode 100644 cxx/include/aesxx/mode.hpp (limited to 'cxx') diff --git a/cxx/CMakeLists.txt b/cxx/CMakeLists.txt index 71173b5..418f004 100644 --- a/cxx/CMakeLists.txt +++ b/cxx/CMakeLists.txt @@ -1,5 +1,5 @@ find_package(Boost REQUIRED) -add_library(libaesnixx INTERFACE) -target_include_directories(libaesnixx INTERFACE include/ ${Boost_INCLUDE_DIRS}) -target_link_libraries(libaesnixx INTERFACE libaesni) +add_library(libaesxx INTERFACE) +target_include_directories(libaesxx INTERFACE include/ ${Boost_INCLUDE_DIRS}) +target_link_libraries(libaesxx INTERFACE libaes) diff --git a/cxx/include/aesnixx/aes.hpp b/cxx/include/aesnixx/aes.hpp deleted file mode 100644 index b2febf5..0000000 --- a/cxx/include/aesnixx/aes.hpp +++ /dev/null @@ -1,278 +0,0 @@ -/** - * \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 "api.hpp" -#include "error.hpp" -#include "mode.hpp" - -#include - -#include - -#include - -namespace aesni -{ - 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_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/algorithm.hpp b/cxx/include/aesnixx/algorithm.hpp deleted file mode 100644 index 85d2fc4..0000000 --- a/cxx/include/aesnixx/algorithm.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/** - * \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 - -namespace aesni -{ - typedef AesNI_Algorithm Algorithm; -} diff --git a/cxx/include/aesnixx/all.hpp b/cxx/include/aesnixx/all.hpp deleted file mode 100644 index 7c80d3b..0000000 --- a/cxx/include/aesnixx/all.hpp +++ /dev/null @@ -1,18 +0,0 @@ -/** - * \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 "aes.hpp" -#include "algorithm.hpp" -#include "api.hpp" -#include "box.hpp" -#include "data.hpp" -#include "debug.hpp" -#include "error.hpp" -#include "mode.hpp" diff --git a/cxx/include/aesnixx/api.hpp b/cxx/include/aesnixx/api.hpp deleted file mode 100644 index 6995ae2..0000000 --- a/cxx/include/aesnixx/api.hpp +++ /dev/null @@ -1,171 +0,0 @@ -/** - * \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/box.hpp b/cxx/include/aesnixx/box.hpp deleted file mode 100644 index 69383a8..0000000 --- a/cxx/include/aesnixx/box.hpp +++ /dev/null @@ -1,212 +0,0 @@ -/** - * \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 "error.hpp" -#include "mode.hpp" - -#include - -#include -#include - -#include -#include -#include - -namespace aesni -{ - class Box - { - public: - typedef AesNI_BoxBlock Block; - typedef AesNI_BoxKey Key; - - static std::string format_key(const Key& src, Algorithm algorithm) - { - AesNI_BoxKeyString str; - aesni_box_format_key( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor()); - return reinterpret_cast(&str); - } - - static std::string format_block(const Block& src, Algorithm algorithm) - { - AesNI_BoxBlockString str; - aesni_box_format_block( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor()); - return reinterpret_cast(&str); - } - - static void parse_block( - Block& dest, - Algorithm algorithm, - const char* src) - { - aesni_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) - { - aesni_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(AESNI_ECB) - { - aesni_box_init(&impl, algorithm, &key, mode, nullptr, - ErrorDetailsThrowsInDestructor()); - } - - Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) - : algorithm(algorithm) - , mode(mode) - { - aesni_box_init(&impl, algorithm, &key, mode, &iv, - ErrorDetailsThrowsInDestructor()); - } - - void encrypt_block(const Block& plaintext, Block& ciphertext) - { - aesni_box_encrypt_block( - &impl, &plaintext, &ciphertext, - ErrorDetailsThrowsInDestructor()); - } - - void decrypt_block(const Block& ciphertext, Block& plaintext) - { - aesni_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; - - aesni_box_encrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aesni_box_encrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aesni::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; - - aesni_box_decrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aesni::ErrorDetailsThrowsInDestructor()); - - std::vector dest_buf; - dest_buf.resize(dest_size); - - aesni_box_decrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aesni::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; - - AesNI_Box impl; - }; -} diff --git a/cxx/include/aesnixx/data.hpp b/cxx/include/aesnixx/data.hpp deleted file mode 100644 index 1a93b7c..0000000 --- a/cxx/include/aesnixx/data.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/** - * \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 "error.hpp" - -#include - -namespace aesni -{ - typedef AesNI_Block128 Block128; - - inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) - { - dest = aesni_make_block128(hi3, hi2, lo1, lo0); - } - - inline void load_block(Block128& dest, const void* src) - { - dest = aesni_load_block128(src); - } - - inline void load_block_aligned(Block128& dest, const void* src) - { - dest = aesni_load_block128_aligned(src); - } - - inline void store_block(void* dest, Block128& src) - { - aesni_store_block128(dest, src); - } - - inline void store_block_aligned(void* dest, Block128& src) - { - aesni_store_block128_aligned(dest, src); - } - - inline Block128 xor_blocks(Block128& a, Block128& b) - { - return aesni_xor_block128(a, b); - } - - inline Block128 reverse_byte_order(Block128& block) - { - return aesni_reverse_byte_order_block128(block); - } - - inline Block128 inc_block(Block128& block) - { - return aesni_inc_block128(block); - } -} diff --git a/cxx/include/aesnixx/debug.hpp b/cxx/include/aesnixx/debug.hpp deleted file mode 100644 index f3e35f4..0000000 --- a/cxx/include/aesnixx/debug.hpp +++ /dev/null @@ -1,177 +0,0 @@ -/** - * \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 - -#ifdef WIN32 -#include -#include -#pragma comment(lib, "DbgHelp.Lib") -#endif - -#include - -#include -#include - -namespace aesni -{ - 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 - } - - private: - template - static std::string put_between_brackets(const T& x) - { - std::ostringstream oss; - oss << "[" << x << "]"; - return oss.str(); - } - - template - static std::string stringify(const T& x) - { - std::ostringstream oss; - oss << x; - return oss.str(); - } - - static std::string format_address_fallback(const void* addr) - { - return put_between_brackets(addr); - } - - 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); - } - - 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); - } - } - 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 - }; - } -} diff --git a/cxx/include/aesnixx/error.hpp b/cxx/include/aesnixx/error.hpp deleted file mode 100644 index 067d563..0000000 --- a/cxx/include/aesnixx/error.hpp +++ /dev/null @@ -1,98 +0,0 @@ -/** - * \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 "debug.hpp" - -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace aesni -{ - class Error : public std::runtime_error - { - public: - Error(const AesNI_ErrorDetails& err_details) - : std::runtime_error(format_error_message(err_details)) - { - copy_call_stack(err_details); - } - - void for_each_in_call_stack(const std::function& callback) const - { - aux::CallStackFormatter formatter; - std::for_each(call_stack, call_stack + call_stack_size, [&formatter, &callback] (void* addr) - { - callback(addr, formatter.format_address(addr)); - }); - } - - private: - static std::string format_error_message(const AesNI_ErrorDetails& err_details) - { - std::vector buf; - buf.resize(aesni_format_error(&err_details, NULL, 0)); - aesni_format_error(&err_details, buf.data(), buf.size()); - return { buf.begin(), buf.end() }; - } - - void copy_call_stack(const AesNI_ErrorDetails& err_details) - { - call_stack_size = err_details.call_stack_size; - std::memcpy(call_stack, err_details.call_stack, call_stack_size * sizeof(void*)); - } - - void* call_stack[AESNI_MAX_CALL_STACK_LENGTH]; - std::size_t call_stack_size; - }; - - std::ostream& operator<<(std::ostream& os, const Error& e) - { - os << "AesNI error: " << e.what() << '\n'; - os << "Call stack:\n"; - e.for_each_in_call_stack([&os] (void* addr, const std::string& name) - { - os << '\t' << addr << ' ' << name << '\n'; - }); - return os; - } - - class ErrorDetailsThrowsInDestructor - { - public: - ErrorDetailsThrowsInDestructor() - { - aesni_success(get()); - } - - ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) - { - if (aesni_is_error(aesni_get_error_code(get()))) - throw Error(impl); - } - - AesNI_ErrorDetails* get() { return &impl; } - - operator AesNI_ErrorDetails*() { return get(); } - - private: - AesNI_ErrorDetails impl; - }; -} diff --git a/cxx/include/aesnixx/mode.hpp b/cxx/include/aesnixx/mode.hpp deleted file mode 100644 index e19dbbd..0000000 --- a/cxx/include/aesnixx/mode.hpp +++ /dev/null @@ -1,156 +0,0 @@ -/** - * \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 - -#include - -namespace aesni -{ - typedef AesNI_Mode Mode; - - template - struct ModeRequiresInitializationVector : public std::true_type - { }; - - template <> - struct ModeRequiresInitializationVector : public std::false_type - { }; - - template - struct ModeUsesEncryptionKeysOnly : public std::true_type - { }; - - inline bool mode_requires_initialization_vector(Mode mode) - { - return mode != AESNI_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 != 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/cxx/include/aesxx/aes.hpp b/cxx/include/aesxx/aes.hpp new file mode 100644 index 0000000..698c0ef --- /dev/null +++ b/cxx/include/aesxx/aes.hpp @@ -0,0 +1,278 @@ +/** + * \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 "api.hpp" +#include "error.hpp" +#include "mode.hpp" + +#include + +#include + +#include + +namespace aesni +{ + 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_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/aesxx/algorithm.hpp b/cxx/include/aesxx/algorithm.hpp new file mode 100644 index 0000000..e2c73ac --- /dev/null +++ b/cxx/include/aesxx/algorithm.hpp @@ -0,0 +1,16 @@ +/** + * \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 + +namespace aesni +{ + typedef AesNI_Algorithm Algorithm; +} diff --git a/cxx/include/aesxx/all.hpp b/cxx/include/aesxx/all.hpp new file mode 100644 index 0000000..7c80d3b --- /dev/null +++ b/cxx/include/aesxx/all.hpp @@ -0,0 +1,18 @@ +/** + * \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 "aes.hpp" +#include "algorithm.hpp" +#include "api.hpp" +#include "box.hpp" +#include "data.hpp" +#include "debug.hpp" +#include "error.hpp" +#include "mode.hpp" diff --git a/cxx/include/aesxx/api.hpp b/cxx/include/aesxx/api.hpp new file mode 100644 index 0000000..6995ae2 --- /dev/null +++ b/cxx/include/aesxx/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/aesxx/box.hpp b/cxx/include/aesxx/box.hpp new file mode 100644 index 0000000..ffca6ce --- /dev/null +++ b/cxx/include/aesxx/box.hpp @@ -0,0 +1,212 @@ +/** + * \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 "error.hpp" +#include "mode.hpp" + +#include + +#include +#include + +#include +#include +#include + +namespace aesni +{ + class Box + { + public: + typedef AesNI_BoxBlock Block; + typedef AesNI_BoxKey Key; + + static std::string format_key(const Key& src, Algorithm algorithm) + { + AesNI_BoxKeyString str; + aesni_box_format_key( + &str, algorithm, &src, ErrorDetailsThrowsInDestructor()); + return reinterpret_cast(&str); + } + + static std::string format_block(const Block& src, Algorithm algorithm) + { + AesNI_BoxBlockString str; + aesni_box_format_block( + &str, algorithm, &src, ErrorDetailsThrowsInDestructor()); + return reinterpret_cast(&str); + } + + static void parse_block( + Block& dest, + Algorithm algorithm, + const char* src) + { + aesni_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) + { + aesni_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(AESNI_ECB) + { + aesni_box_init(&impl, algorithm, &key, mode, nullptr, + ErrorDetailsThrowsInDestructor()); + } + + Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) + : algorithm(algorithm) + , mode(mode) + { + aesni_box_init(&impl, algorithm, &key, mode, &iv, + ErrorDetailsThrowsInDestructor()); + } + + void encrypt_block(const Block& plaintext, Block& ciphertext) + { + aesni_box_encrypt_block( + &impl, &plaintext, &ciphertext, + ErrorDetailsThrowsInDestructor()); + } + + void decrypt_block(const Block& ciphertext, Block& plaintext) + { + aesni_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; + + aesni_box_encrypt_buffer( + &impl, + src_buf, + src_size, + nullptr, + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aesni_box_encrypt_buffer( + &impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aesni::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; + + aesni_box_decrypt_buffer( + &impl, + src_buf, + src_size, + nullptr, + &dest_size, + aesni::ErrorDetailsThrowsInDestructor()); + + std::vector dest_buf; + dest_buf.resize(dest_size); + + aesni_box_decrypt_buffer( + &impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aesni::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; + + AesNI_Box impl; + }; +} diff --git a/cxx/include/aesxx/data.hpp b/cxx/include/aesxx/data.hpp new file mode 100644 index 0000000..f52fe3e --- /dev/null +++ b/cxx/include/aesxx/data.hpp @@ -0,0 +1,58 @@ +/** + * \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 "error.hpp" + +#include + +namespace aesni +{ + typedef AesNI_Block128 Block128; + + inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) + { + dest = aesni_make_block128(hi3, hi2, lo1, lo0); + } + + inline void load_block(Block128& dest, const void* src) + { + dest = aesni_load_block128(src); + } + + inline void load_block_aligned(Block128& dest, const void* src) + { + dest = aesni_load_block128_aligned(src); + } + + inline void store_block(void* dest, Block128& src) + { + aesni_store_block128(dest, src); + } + + inline void store_block_aligned(void* dest, Block128& src) + { + aesni_store_block128_aligned(dest, src); + } + + inline Block128 xor_blocks(Block128& a, Block128& b) + { + return aesni_xor_block128(a, b); + } + + inline Block128 reverse_byte_order(Block128& block) + { + return aesni_reverse_byte_order_block128(block); + } + + inline Block128 inc_block(Block128& block) + { + return aesni_inc_block128(block); + } +} diff --git a/cxx/include/aesxx/debug.hpp b/cxx/include/aesxx/debug.hpp new file mode 100644 index 0000000..f3e35f4 --- /dev/null +++ b/cxx/include/aesxx/debug.hpp @@ -0,0 +1,177 @@ +/** + * \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 + +#ifdef WIN32 +#include +#include +#pragma comment(lib, "DbgHelp.Lib") +#endif + +#include + +#include +#include + +namespace aesni +{ + 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 + } + + private: + template + static std::string put_between_brackets(const T& x) + { + std::ostringstream oss; + oss << "[" << x << "]"; + return oss.str(); + } + + template + static std::string stringify(const T& x) + { + std::ostringstream oss; + oss << x; + return oss.str(); + } + + static std::string format_address_fallback(const void* addr) + { + return put_between_brackets(addr); + } + + 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); + } + + 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); + } + } + 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 + }; + } +} diff --git a/cxx/include/aesxx/error.hpp b/cxx/include/aesxx/error.hpp new file mode 100644 index 0000000..1f22535 --- /dev/null +++ b/cxx/include/aesxx/error.hpp @@ -0,0 +1,98 @@ +/** + * \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 "debug.hpp" + +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace aesni +{ + class Error : public std::runtime_error + { + public: + Error(const AesNI_ErrorDetails& err_details) + : std::runtime_error(format_error_message(err_details)) + { + copy_call_stack(err_details); + } + + void for_each_in_call_stack(const std::function& callback) const + { + aux::CallStackFormatter formatter; + std::for_each(call_stack, call_stack + call_stack_size, [&formatter, &callback] (void* addr) + { + callback(addr, formatter.format_address(addr)); + }); + } + + private: + static std::string format_error_message(const AesNI_ErrorDetails& err_details) + { + std::vector buf; + buf.resize(aesni_format_error(&err_details, NULL, 0)); + aesni_format_error(&err_details, buf.data(), buf.size()); + return { buf.begin(), buf.end() }; + } + + void copy_call_stack(const AesNI_ErrorDetails& err_details) + { + call_stack_size = err_details.call_stack_size; + std::memcpy(call_stack, err_details.call_stack, call_stack_size * sizeof(void*)); + } + + void* call_stack[AESNI_MAX_CALL_STACK_LENGTH]; + std::size_t call_stack_size; + }; + + std::ostream& operator<<(std::ostream& os, const Error& e) + { + os << "AesNI error: " << e.what() << '\n'; + os << "Call stack:\n"; + e.for_each_in_call_stack([&os] (void* addr, const std::string& name) + { + os << '\t' << addr << ' ' << name << '\n'; + }); + return os; + } + + class ErrorDetailsThrowsInDestructor + { + public: + ErrorDetailsThrowsInDestructor() + { + aesni_success(get()); + } + + ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) + { + if (aesni_is_error(aesni_get_error_code(get()))) + throw Error(impl); + } + + AesNI_ErrorDetails* get() { return &impl; } + + operator AesNI_ErrorDetails*() { return get(); } + + private: + AesNI_ErrorDetails impl; + }; +} diff --git a/cxx/include/aesxx/mode.hpp b/cxx/include/aesxx/mode.hpp new file mode 100644 index 0000000..706c4b5 --- /dev/null +++ b/cxx/include/aesxx/mode.hpp @@ -0,0 +1,156 @@ +/** + * \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 + +#include + +namespace aesni +{ + typedef AesNI_Mode Mode; + + template + struct ModeRequiresInitializationVector : public std::true_type + { }; + + template <> + struct ModeRequiresInitializationVector : public std::false_type + { }; + + template + struct ModeUsesEncryptionKeysOnly : public std::true_type + { }; + + inline bool mode_requires_initialization_vector(Mode mode) + { + return mode != AESNI_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 != 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); \ + } +} -- cgit v1.2.3