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 --- CMakeLists.txt | 20 +- 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 +++++++++++ include/aes/aes.h | 529 ++++++++++++++++++++++++++++++++++++++ include/aes/algorithm.h | 26 ++ include/aes/all.h | 26 ++ include/aes/box.h | 84 ++++++ include/aes/box_aes.h | 24 ++ include/aes/box_data.h | 156 +++++++++++ include/aes/data.h | 125 +++++++++ include/aes/error.h | 184 +++++++++++++ include/aes/mode.h | 169 ++++++++++++ include/aes/padding.h | 41 +++ include/aesni/aes.h | 529 -------------------------------------- include/aesni/algorithm.h | 26 -- include/aesni/all.h | 26 -- include/aesni/box.h | 84 ------ include/aesni/box_aes.h | 24 -- include/aesni/box_data.h | 156 ----------- include/aesni/data.h | 125 --------- include/aesni/error.h | 184 ------------- include/aesni/mode.h | 169 ------------ include/aesni/padding.h | 41 --- src/aes.c | 2 +- src/box.c | 2 +- src/box_aes.c | 2 +- src/c/aes128.c | 2 +- src/c/aes192.c | 2 +- src/c/aes256.c | 2 +- src/error.c | 2 +- src/padding.c | 2 +- utils/CMakeLists.txt | 12 +- utils/block_cmd_parser.hpp | 2 +- utils/block_dumper.hpp | 2 +- utils/data_parsers.hpp | 2 +- utils/decrypt_block.cpp | 2 +- utils/decrypt_bmp.cpp | 2 +- utils/decrypt_file.cpp | 2 +- utils/encrypt_block.cpp | 2 +- utils/encrypt_bmp.cpp | 2 +- utils/encrypt_file.cpp | 2 +- utils/file_cmd_parser.hpp | 2 +- 59 files changed, 2585 insertions(+), 2585 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 create mode 100644 include/aes/aes.h create mode 100644 include/aes/algorithm.h create mode 100644 include/aes/all.h create mode 100644 include/aes/box.h create mode 100644 include/aes/box_aes.h create mode 100644 include/aes/box_data.h create mode 100644 include/aes/data.h create mode 100644 include/aes/error.h create mode 100644 include/aes/mode.h create mode 100644 include/aes/padding.h delete mode 100644 include/aesni/aes.h delete mode 100644 include/aesni/algorithm.h delete mode 100644 include/aesni/all.h delete mode 100644 include/aesni/box.h delete mode 100644 include/aesni/box_aes.h delete mode 100644 include/aesni/box_data.h delete mode 100644 include/aesni/data.h delete mode 100644 include/aesni/error.h delete mode 100644 include/aesni/mode.h delete mode 100644 include/aesni/padding.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 582309a..db1581f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,18 @@ -project(aesni C CXX ASM_MASM) +project(aes_tools C CXX ASM_MASM) -file(GLOB_RECURSE libaesni_headers "include/*.h") +file(GLOB_RECURSE libaes_headers "include/*.h") if(AESNI_USE_ASM) - file(GLOB libaesni_c_sources "src/*.c") - file(GLOB libaesni_asm_sources "src/asm/*.asm") - set(libaesni_sources ${libaesni_asm_sources} ${libaesni_c_sources}) - set_source_files_properties(${libaesni_asm_sources} PROPERTIES COMPILE_FLAGS "/safeseh") + file(GLOB libaes_c_sources "src/*.c") + file(GLOB libaes_asm_sources "src/asm/*.asm") + set(libaes_sources ${libaes_asm_sources} ${libaes_c_sources}) + set_source_files_properties(${libaes_asm_sources} PROPERTIES COMPILE_FLAGS "/safeseh") # Setting CMAKE_ASM_MASM_FLAGS doesn't work: http://www.cmake.org/Bug/view.php?id=14711 else() - file(GLOB_RECURSE libaesni_sources "src/*.c") + file(GLOB_RECURSE libaes_sources "src/*.c") endif() -add_library(libaesni ${libaesni_sources} ${libaesni_headers}) -target_include_directories(libaesni PUBLIC include/) -target_compile_definitions(libaesni PRIVATE _CRT_SECURE_NO_WARNINGS) +add_library(libaes ${libaes_sources} ${libaes_headers}) +target_include_directories(libaes PUBLIC include/) +target_compile_definitions(libaes PRIVATE _CRT_SECURE_NO_WARNINGS) add_subdirectory(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); \ + } +} diff --git a/include/aes/aes.h b/include/aes/aes.h new file mode 100644 index 0000000..ea859a3 --- /dev/null +++ b/include/aes/aes.h @@ -0,0 +1,529 @@ +/** + * \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 "data.h" +#include "error.h" +#include "mode.h" + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef AesNI_Block128 AesNI_AES_Block; +typedef AesNI_AES_Block AesNI_AES128_Block; +typedef AesNI_AES_Block AesNI_AES192_Block; +typedef AesNI_AES_Block AesNI_AES256_Block; + +typedef struct +{ + AesNI_AES_Block key; +} +AesNI_AES128_Key; + +typedef struct +{ + AesNI_AES_Block hi; + AesNI_AES_Block lo; +} +AesNI_AES192_Key; + +typedef struct +{ + AesNI_AES_Block hi; + AesNI_AES_Block lo; +} +AesNI_AES256_Key; + +static __inline void aesni_AES_make_block(AesNI_AES_Block* dest, int hi3, int hi2, int lo1, int lo0) +{ + *dest = aesni_make_block128(hi3, hi2, lo1, lo0); +} + +static __inline void aesni_AES128_make_block(AesNI_AES128_Block* dest, int hi3, int hi2, int lo1, int lo0) +{ + aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); +} + +static __inline void aesni_AES192_make_block(AesNI_AES192_Block* dest, int hi3, int hi2, int lo1, int lo0) +{ + aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); +} + +static __inline void aesni_AES256_make_block(AesNI_AES256_Block* dest, int hi3, int hi2, int lo1, int lo0) +{ + aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); +} + +static __inline void aesni_AES128_make_key(AesNI_AES128_Key* dest, int hi3, int hi2, int lo1, int lo0) +{ + aesni_AES_make_block(&dest->key, hi3, hi2, lo1, lo0); +} + +static __inline void aesni_AES192_make_key(AesNI_AES192_Key* dest, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) +{ + aesni_AES_make_block(&dest->hi, 0, 0, hi5, hi4); + aesni_AES_make_block(&dest->lo, lo3, lo2, lo1, lo0); +} + +static __inline void aesni_AES256_make_key(AesNI_AES256_Key* dest, int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) +{ + aesni_AES_make_block(&dest->hi, hi7, hi6, hi5, hi4); + aesni_AES_make_block(&dest->lo, lo3, lo2, lo1, lo0); +} + +typedef struct { char str[33]; } AesNI_AES_BlockString; +typedef AesNI_AES_BlockString AesNI_AES128_BlockString; +typedef AesNI_AES_BlockString AesNI_AES192_BlockString; +typedef AesNI_AES_BlockString AesNI_AES256_BlockString; + +typedef struct { char str[49]; } AesNI_AES_BlockMatrixString; +typedef AesNI_AES_BlockMatrixString AesNI_AES128_BlockMatrixString; +typedef AesNI_AES_BlockMatrixString AesNI_AES192_BlockMatrixString; +typedef AesNI_AES_BlockMatrixString AesNI_AES256_BlockMatrixString; + +AesNI_StatusCode aesni_AES_format_block( + AesNI_AES_BlockString*, + const AesNI_AES_Block*, + AesNI_ErrorDetails*); + +static __inline AesNI_StatusCode aesni_AES128_format_block( + AesNI_AES128_BlockString* dest, + const AesNI_AES128_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES192_format_block( + AesNI_AES192_BlockString* dest, + const AesNI_AES192_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES256_format_block( + AesNI_AES256_BlockString* dest, + const AesNI_AES256_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block(dest, src, err_details); +} + +AesNI_StatusCode aesni_AES_format_block_as_matrix( + AesNI_AES_BlockMatrixString*, + const AesNI_AES_Block*, + AesNI_ErrorDetails*); + +static __inline AesNI_StatusCode aesni_AES128_format_block_as_matrix( + AesNI_AES128_BlockMatrixString* dest, + const AesNI_AES128_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block_as_matrix(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES192_format_block_as_matrix( + AesNI_AES192_BlockMatrixString* dest, + const AesNI_AES192_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block_as_matrix(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES256_format_block_as_matrix( + AesNI_AES256_BlockMatrixString* dest, + const AesNI_AES256_Block* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_format_block_as_matrix(dest, src, err_details); +} + +AesNI_StatusCode aesni_AES_print_block( + const AesNI_AES_Block*, + AesNI_ErrorDetails*); + +static __inline AesNI_StatusCode aesni_AES128_print_block( + const AesNI_AES128_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block(block, err_details); +} + +static __inline AesNI_StatusCode aesni_AES192_print_block( + const AesNI_AES192_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block(block, err_details); +} + +static __inline AesNI_StatusCode aesni_AES256_print_block( + const AesNI_AES256_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block(block, err_details); +} + +AesNI_StatusCode aesni_AES_print_block_as_matrix( + const AesNI_AES_Block*, + AesNI_ErrorDetails*); + +static __inline AesNI_StatusCode aesni_AES128_print_block_as_matrix( + const AesNI_AES128_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block_as_matrix(block, err_details); +} + +static __inline AesNI_StatusCode aesni_AES192_print_block_as_matrix( + const AesNI_AES192_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block_as_matrix(block, err_details); +} + +static __inline AesNI_StatusCode aesni_AES256_print_block_as_matrix( + const AesNI_AES256_Block* block, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_print_block_as_matrix(block, err_details); +} + +AesNI_StatusCode aesni_AES_parse_block( + AesNI_AES_Block* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +static __inline AesNI_StatusCode aesni_AES128_parse_block( + AesNI_AES128_Block* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_parse_block(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES192_parse_block( + AesNI_AES192_Block* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_parse_block(dest, src, err_details); +} + +static __inline AesNI_StatusCode aesni_AES256_parse_block( + AesNI_AES256_Block* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_AES_parse_block(dest, src, err_details); +} + +typedef struct { char str[33]; } AesNI_AES128_KeyString; +typedef struct { char str[49]; } AesNI_AES192_KeyString; +typedef struct { char str[65]; } AesNI_AES256_KeyString; + +AesNI_StatusCode aesni_AES128_format_key( + AesNI_AES128_KeyString*, + const AesNI_AES128_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES192_format_key( + AesNI_AES192_KeyString*, + const AesNI_AES192_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES256_format_key( + AesNI_AES256_KeyString*, + const AesNI_AES256_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES128_print_key( + const AesNI_AES128_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES192_print_key( + const AesNI_AES192_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES256_print_key( + const AesNI_AES256_Key*, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_AES128_parse_key( + AesNI_AES128_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_AES192_parse_key( + AesNI_AES192_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_AES256_parse_key( + AesNI_AES256_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +typedef struct +{ + AesNI_AES_Block keys[11]; +} +AesNI_AES128_RoundKeys; + +typedef struct +{ + AesNI_AES_Block keys[13]; +} +AesNI_AES192_RoundKeys; + +typedef struct +{ + AesNI_AES_Block keys[15]; +} +AesNI_AES256_RoundKeys; + +void __fastcall aesni_AES128_expand_key_( + AesNI_AES_Block key, + AesNI_AES128_RoundKeys* encryption_keys); + +void __fastcall aesni_AES192_expand_key_( + AesNI_AES_Block key_lo, + AesNI_AES_Block key_hi, + AesNI_AES192_RoundKeys* encryption_keys); + +void __fastcall aesni_AES256_expand_key_( + AesNI_AES_Block key_lo, + AesNI_AES_Block key_hi, + AesNI_AES256_RoundKeys* encryption_keys); + +void __fastcall aesni_AES128_derive_decryption_keys_( + const AesNI_AES128_RoundKeys* encryption_keys, + AesNI_AES128_RoundKeys* decryption_keys); + +void __fastcall aesni_AES192_derive_decryption_keys_( + const AesNI_AES192_RoundKeys* encryption_keys, + AesNI_AES192_RoundKeys* decryption_keys); + +void __fastcall aesni_AES256_derive_decryption_keys_( + const AesNI_AES256_RoundKeys* encryption_keys, + AesNI_AES256_RoundKeys* decryption_keys); + +AesNI_AES_Block __fastcall aesni_AES128_encrypt_block_( + AesNI_AES_Block plaintext, + const AesNI_AES128_RoundKeys*); + +AesNI_AES_Block __fastcall aesni_AES192_encrypt_block_( + AesNI_AES_Block plaintext, + const AesNI_AES192_RoundKeys*); + +AesNI_AES_Block __fastcall aesni_AES256_encrypt_block_( + AesNI_AES_Block plaintext, + const AesNI_AES256_RoundKeys*); + +AesNI_AES_Block __fastcall aesni_AES128_decrypt_block_( + AesNI_AES_Block ciphertext, + const AesNI_AES128_RoundKeys*); + +AesNI_AES_Block __fastcall aesni_AES192_decrypt_block_( + AesNI_AES_Block ciphertext, + const AesNI_AES192_RoundKeys*); + +AesNI_AES_Block __fastcall aesni_AES256_decrypt_block_( + AesNI_AES_Block ciphertext, + const AesNI_AES256_RoundKeys*); + +static __inline AesNI_AES_Block __fastcall aesni_AES_xor_blocks( + AesNI_AES_Block a, + AesNI_AES_Block b) +{ + return aesni_xor_block128(a, b); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES128_xor_blocks( + AesNI_AES128_Block a, + AesNI_AES128_Block b) +{ + return aesni_AES_xor_blocks(a, b); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES192_xor_blocks( + AesNI_AES192_Block a, + AesNI_AES192_Block b) +{ + return aesni_AES_xor_blocks(a, b); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES256_xor_blocks( + AesNI_AES256_Block a, + AesNI_AES256_Block b) +{ + return aesni_AES_xor_blocks(a, b); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES_inc_block( + AesNI_AES_Block block) +{ + block = aesni_reverse_byte_order_block128(block); + block = aesni_inc_block128(block); + return aesni_reverse_byte_order_block128(block); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES128_inc_block( + AesNI_AES128_Block block) +{ + return aesni_AES_inc_block(block); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES192_inc_block( + AesNI_AES192_Block block) +{ + return aesni_AES_inc_block(block); +} + +static __inline AesNI_AES_Block __fastcall aesni_AES256_inc_block( + AesNI_AES256_Block block) +{ + return aesni_AES_inc_block(block); +} + +AESNI_ENCRYPT_BLOCK_ECB(AES128); +AESNI_DECRYPT_BLOCK_ECB(AES128); +AESNI_ENCRYPT_BLOCK_CBC(AES128); +AESNI_DECRYPT_BLOCK_CBC(AES128); +AESNI_ENCRYPT_BLOCK_CFB(AES128); +AESNI_DECRYPT_BLOCK_CFB(AES128); +AESNI_ENCRYPT_BLOCK_OFB(AES128); +AESNI_DECRYPT_BLOCK_OFB(AES128); +AESNI_ENCRYPT_BLOCK_CTR(AES128); +AESNI_DECRYPT_BLOCK_CTR(AES128); + +AESNI_ENCRYPT_BLOCK_ECB(AES192); +AESNI_DECRYPT_BLOCK_ECB(AES192); +AESNI_ENCRYPT_BLOCK_CBC(AES192); +AESNI_DECRYPT_BLOCK_CBC(AES192); +AESNI_ENCRYPT_BLOCK_CFB(AES192); +AESNI_DECRYPT_BLOCK_CFB(AES192); +AESNI_ENCRYPT_BLOCK_OFB(AES192); +AESNI_DECRYPT_BLOCK_OFB(AES192); +AESNI_ENCRYPT_BLOCK_CTR(AES192); +AESNI_DECRYPT_BLOCK_CTR(AES192); + +AESNI_ENCRYPT_BLOCK_ECB(AES256); +AESNI_DECRYPT_BLOCK_ECB(AES256); +AESNI_ENCRYPT_BLOCK_CBC(AES256); +AESNI_DECRYPT_BLOCK_CBC(AES256); +AESNI_ENCRYPT_BLOCK_CFB(AES256); +AESNI_DECRYPT_BLOCK_CFB(AES256); +AESNI_ENCRYPT_BLOCK_OFB(AES256); +AESNI_DECRYPT_BLOCK_OFB(AES256); +AESNI_ENCRYPT_BLOCK_CTR(AES256); +AESNI_DECRYPT_BLOCK_CTR(AES256); + +/** + * \brief Expands an AES-128 key into 10 encryption round keys. + * + * \param[in] key The AES-128 key. + * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES128_expand_key( + const AesNI_AES128_Key* key, + AesNI_AES128_RoundKeys* encryption_keys) +{ + assert(encryption_keys); + + aesni_AES128_expand_key_(key->key, encryption_keys); +} + +/** + * \brief Derives AES-128 decryption round keys from AES-128 encryption round keys. + * + * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. + * \param[out] decryption_keys The AES-128 decryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES128_derive_decryption_keys( + const AesNI_AES128_RoundKeys* encryption_keys, + AesNI_AES128_RoundKeys* decryption_keys) +{ + assert(encryption_keys); + assert(decryption_keys); + + aesni_AES128_derive_decryption_keys_(encryption_keys, decryption_keys); +} + +/** + * \brief Expands an AES-192 key into 12 encryption round keys. + * + * \param[in] key The AES-192 key. + * \param[out] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES192_expand_key( + const AesNI_AES192_Key* key, + AesNI_AES192_RoundKeys* encryption_keys) +{ + assert(key); + assert(encryption_keys); + + aesni_AES192_expand_key_(key->lo, key->hi, encryption_keys); +} + +/** + * \brief Derives AES-192 decryption round keys from AES-192 encryption round keys. + * + * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. + * \param[out] decryption_keys The AES-192 decryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES192_derive_decryption_keys( + const AesNI_AES192_RoundKeys* encryption_keys, + AesNI_AES192_RoundKeys* decryption_keys) +{ + assert(encryption_keys); + assert(decryption_keys); + + aesni_AES192_derive_decryption_keys_(encryption_keys, decryption_keys); +} + +/** + * \brief Expands an AES-256 key into 14 encryption round keys. + * + * \param[in] key The AES-256 key. + * \param[out] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES256_expand_key( + const AesNI_AES256_Key* key, + AesNI_AES256_RoundKeys* encryption_keys) +{ + assert(key); + assert(encryption_keys); + + aesni_AES256_expand_key_(key->lo, key->hi, encryption_keys); +} + +/** + * \brief Derives AES-256 decryption round keys from AES-256 encryption round keys. + * + * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. + * \param[out] decryption_keys The AES-256 decryption round keys. Must not be `NULL`. + */ +static __inline void __fastcall aesni_AES256_derive_decryption_keys( + const AesNI_AES256_RoundKeys* encryption_keys, + AesNI_AES256_RoundKeys* decryption_keys) +{ + assert(encryption_keys); + assert(decryption_keys); + + aesni_AES256_derive_decryption_keys_(encryption_keys, decryption_keys); +} + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/algorithm.h b/include/aes/algorithm.h new file mode 100644 index 0000000..a73b412 --- /dev/null +++ b/include/aes/algorithm.h @@ -0,0 +1,26 @@ +/** + * \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 __cplusplus +extern "C" +{ +#endif + +typedef enum +{ + AESNI_AES128, + AESNI_AES192, + AESNI_AES256, +} +AesNI_Algorithm; + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/all.h b/include/aes/all.h new file mode 100644 index 0000000..bbea6f9 --- /dev/null +++ b/include/aes/all.h @@ -0,0 +1,26 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + * + * \brief Include this file to use the library. Includes all the other header + * files. + */ + +#pragma once + +/** + * \defgroup aesni AesNI + */ + +#include "aes.h" +#include "algorithm.h" +#include "box.h" +#include "box_aes.h" +#include "box_data.h" +#include "data.h" +#include "error.h" +#include "mode.h" +#include "padding.h" diff --git a/include/aes/box.h b/include/aes/box.h new file mode 100644 index 0000000..58517e1 --- /dev/null +++ b/include/aes/box.h @@ -0,0 +1,84 @@ +/** + * \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.h" +#include "box_data.h" +#include "error.h" + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +AesNI_StatusCode aesni_box_init( + AesNI_Box* box, + AesNI_Algorithm algorithm, + const AesNI_BoxKey* box_key, + AesNI_Mode mode, + const AesNI_BoxBlock* iv, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_parse_key( + AesNI_BoxKey* dest, + AesNI_Algorithm algorithm, + const char* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_parse_block( + AesNI_BoxBlock* dest, + AesNI_Algorithm algorithm, + const char* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_format_key( + AesNI_BoxKeyString* dest, + AesNI_Algorithm algorithm, + const AesNI_BoxKey* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_format_block( + AesNI_BoxBlockString* dest, + AesNI_Algorithm algorithm, + const AesNI_BoxBlock* src, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_encrypt_block( + AesNI_Box* box, + const AesNI_BoxBlock* plaintext, + AesNI_BoxBlock* ciphertext, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_decrypt_block( + AesNI_Box* box, + const AesNI_BoxBlock* ciphertext, + AesNI_BoxBlock* plaintext, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_encrypt_buffer( + AesNI_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_box_decrypt_buffer( + AesNI_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AesNI_ErrorDetails* err_details); + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/box_aes.h b/include/aes/box_aes.h new file mode 100644 index 0000000..3d7faec --- /dev/null +++ b/include/aes/box_aes.h @@ -0,0 +1,24 @@ +/** + * \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 "box_data.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes128; +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192; +extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256; + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/box_data.h b/include/aes/box_data.h new file mode 100644 index 0000000..888c7c0 --- /dev/null +++ b/include/aes/box_data.h @@ -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 "aes.h" +#include "error.h" +#include "mode.h" + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef union +{ + AesNI_AES128_Key aes128_key; + AesNI_AES192_Key aes192_key; + AesNI_AES256_Key aes256_key; +} +AesNI_BoxKey; + +typedef union +{ + AesNI_AES128_RoundKeys aes128_encryption_keys; + AesNI_AES192_RoundKeys aes192_encryption_keys; + AesNI_AES256_RoundKeys aes256_encryption_keys; +} +AesNI_BoxEncryptionRoundKeys; + +typedef union +{ + AesNI_AES128_RoundKeys aes128_decryption_keys; + AesNI_AES192_RoundKeys aes192_decryption_keys; + AesNI_AES256_RoundKeys aes256_decryption_keys; +} +AesNI_BoxDecryptionRoundKeys; + +typedef union +{ + AesNI_AES128_KeyString aes128; + AesNI_AES192_KeyString aes192; + AesNI_AES256_KeyString aes256; +} +AesNI_BoxKeyString; + +typedef union +{ + AesNI_AES_Block aes_block; +} +AesNI_BoxBlock; + +typedef union +{ + AesNI_AES_BlockString aes; +} +AesNI_BoxBlockString; + +typedef AesNI_StatusCode (*AesNI_BoxCalculateRoundKeys)( + const AesNI_BoxKey* params, + AesNI_BoxEncryptionRoundKeys*, + AesNI_BoxDecryptionRoundKeys*, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxParseBlock)( + AesNI_BoxBlock* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxParseKey)( + AesNI_BoxKey* dest, + const char* src, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxFormatBlock)( + AesNI_BoxBlockString* dest, + const AesNI_BoxBlock* src, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxFormatKey)( + AesNI_BoxKeyString* dest, + const AesNI_BoxKey* src, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxEncryptBlock)( + const AesNI_BoxBlock* plaintext, + const AesNI_BoxEncryptionRoundKeys* params, + AesNI_BoxBlock* ciphertext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxDecryptBlock)( + const AesNI_BoxBlock* ciphertext, + const AesNI_BoxDecryptionRoundKeys* params, + AesNI_BoxBlock* plaintext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxXorBlock)( + AesNI_BoxBlock*, + const AesNI_BoxBlock*, + AesNI_ErrorDetails*); + +typedef AesNI_StatusCode (*AesNI_BoxIncBlock)( + AesNI_BoxBlock*, + AesNI_ErrorDetails*); + +typedef AesNI_StatusCode (*AesNI_BoxGetBlockSize)( + size_t*, + AesNI_ErrorDetails*); + +typedef AesNI_StatusCode (*AesNI_BoxStoreBlock)( + void*, + const AesNI_BoxBlock*, + AesNI_ErrorDetails*); + +typedef AesNI_StatusCode (*AesNI_BoxLoadBlock)( + AesNI_BoxBlock*, + const void*, + AesNI_ErrorDetails*); + +typedef struct +{ + AesNI_BoxCalculateRoundKeys calc_round_keys; + AesNI_BoxParseBlock parse_block; + AesNI_BoxParseKey parse_key; + AesNI_BoxFormatBlock format_block; + AesNI_BoxFormatKey format_key; + AesNI_BoxEncryptBlock encrypt_block; + AesNI_BoxDecryptBlock decrypt_block; + AesNI_BoxXorBlock xor_block; + AesNI_BoxIncBlock inc_block; + AesNI_BoxGetBlockSize get_block_size; + AesNI_BoxStoreBlock store_block; + AesNI_BoxLoadBlock load_block; +} +AesNI_BoxAlgorithmInterface; + +typedef struct +{ + const AesNI_BoxAlgorithmInterface* algorithm; + AesNI_BoxEncryptionRoundKeys encryption_keys; + AesNI_BoxDecryptionRoundKeys decryption_keys; + AesNI_Mode mode; + AesNI_BoxBlock iv; +} +AesNI_Box; + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/data.h b/include/aes/data.h new file mode 100644 index 0000000..94cff2c --- /dev/null +++ b/include/aes/data.h @@ -0,0 +1,125 @@ +/** + * \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 + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * \brief Represents a 128-bit block. + */ +typedef __m128i AesNI_Block128; + +/** + * \brief Loads a 128-bit block from a memory location. + * + * \param[in] src The pointer to a memory location. Must not be `NULL`. + * + * \return The loaded 128-bit block. + */ +static __inline AesNI_Block128 aesni_load_block128(const void* src) +{ + return _mm_loadu_si128((AesNI_Block128*) src); +} + +/** + * \brief Loads a 128-bit block from a 16-byte aligned memory location. + * + * \param[in] src The pointer to a 16-byte aligned memory location. Must not be `NULL`. + * + * \return The loaded 128-bit block. + */ +static __inline AesNI_Block128 aesni_load_block128_aligned(const void* src) +{ + return _mm_load_si128((AesNI_Block128*) src); +} + +/** + * \brief Stores a 128-bit block in a memory location. + * + * \param[out] dest The pointer to a memory location. Must not be `NULL`. + * + * \param[in] block The block to be stored. + */ +static __inline void __fastcall aesni_store_block128( + void* dest, + AesNI_Block128 block) +{ + _mm_storeu_si128((AesNI_Block128*) dest, block); +} + +/** + * \brief Stores a 128-bit block in a 16-byte aligned memory location. + * + * \param[out] dest The pointer to a 16-byte aligned memory location. Must not be `NULL`. + * + * \param[in] block The block to be stored. + */ +static __inline void __fastcall aesni_store_block128_aligned( + void* dest, + AesNI_Block128 block) +{ + _mm_store_si128((AesNI_Block128*) dest, block); +} + +/** + * \brief XORs two 128-bit blocks. + * + * \param[in] a The first XOR operand. + * \param[in] b The second XOR operand. + * + * \return `a^b`. + */ +static __inline AesNI_Block128 __fastcall aesni_xor_block128( + AesNI_Block128 a, + AesNI_Block128 b) +{ + return _mm_xor_si128(a, b); +} + +/** + * \brief Builds a 128-bit block from four 4-byte values. + * + * Builds a 128-bit block like this: + * + * * dest[127:96] = hi3 + * * dest[95:64] = hi2 + * * dest[63:32] = lo1 + * * dest[31:0] = lo0 + * + * \param[in] hi3 The most significant 4-byte value. + * \param[in] hi2 The more significant 4-byte value. + * \param[in] lo1 The less significant 4-byte value. + * \param[in] lo0 The least significant 4-byte value. + * + * \return The built 128-bit block. + */ +static __inline AesNI_Block128 __fastcall aesni_make_block128(int hi3, int hi2, int lo1, int lo0) +{ + return _mm_set_epi32(hi3, hi2, lo1, lo0); +} + +static __inline AesNI_Block128 __fastcall aesni_reverse_byte_order_block128(AesNI_Block128 block) +{ + return _mm_shuffle_epi8(block, aesni_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); +} + +static __inline AesNI_Block128 __fastcall aesni_inc_block128(AesNI_Block128 x) +{ + return _mm_add_epi32(x, aesni_make_block128(0, 0, 0, 1)); +} + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/error.h b/include/aes/error.h new file mode 100644 index 0000000..a51284f --- /dev/null +++ b/include/aes/error.h @@ -0,0 +1,184 @@ +/** + * \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 + +/** + * \defgroup aesni_error_handling Error handling + * \ingroup aesni + * \brief Error data structures and formatting functions. + * + * Some library functions cannot fail, which is simple. + * Other functions return an error code. + * You can check if a function exited with an error by passing the returned + * error code to aesni_is_error(). + * + * Some possibly-may-fail functions accept a pointer to an "error details" + * structure. + * This pointer can always be `NULL`. + * In this case, simply an error code is returned. + * Otherwise, the error details structure is filled with appropriate info about + * the error, possibly including a few details like invalid arguments names, + * etc. + * + * You can format an error details structure using the formatting functions. + * \{ + */ + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * \brief API status codes. + */ +typedef enum +{ + AESNI_SUCCESS, ///< Everything went fine + AESNI_NULL_ARGUMENT_ERROR, ///< Invalid argument value NULL + AESNI_PARSE_ERROR, ///< Couldn't parse + AESNI_INVALID_PKCS7_PADDING_ERROR, ///< Invalid PKCS7 padding while decrypting + AESNI_NOT_IMPLEMENTED_ERROR, ///< Not implemented + AESNI_MISSING_PADDING_ERROR, + AESNI_MEMORY_ALLOCATION_ERROR, +} +AesNI_StatusCode; + +static __inline int aesni_is_error(AesNI_StatusCode ec) +{ + return ec != AESNI_SUCCESS; +} + +/** + * \brief Retrieves a simple error message for an error code. + * + * For example, + * \code{.c} + * printf("%s\n", aesni_strerror(AESNI_NULL_ARGUMENT_ERROR)); + * \endcode + * would print + * \code + * Invalid argument value NULL + * \endcode + * + * \param[in] ec The error code. + * \return A pointer to a statically-allocated C string. + */ +const char* aesni_strerror(AesNI_StatusCode ec); + +#define AESNI_MAX_CALL_STACK_LENGTH 32 + +/** + * \brief Stores error details: error code & possibly a few parameters. + */ +typedef struct +{ + AesNI_StatusCode ec; ///< Error code + + union + { + struct { char param_name[32]; } null_arg; + struct + { + char src[128]; + char what[32]; + } + parse_error; + struct { char what[128]; } not_implemented; + } + params; + + void* call_stack[AESNI_MAX_CALL_STACK_LENGTH]; + size_t call_stack_size; +} +AesNI_ErrorDetails; + +/** + * \brief Extracts an error code from error details. + * + * \param[in] err_details The error details structure. Must not be `NULL`. + * \return The error code stored in the error details. + */ +static __inline AesNI_StatusCode aesni_get_error_code( + const AesNI_ErrorDetails* err_details) +{ + return err_details->ec; +} + +/** + * \brief Formats a pretty error message, including error parameters. + * + * \param[in] err_details The pointer to error details. Must not be `NULL`. + * \param[out] dest The pointer to the destination string buffer. + * \param[in] dest_size The size of the destination buffer, in bytes. + * \return If `dest` is NULL, the number of bytes required to store the full + * error message, and the number of characters written (excluding the + * terminating '\0' character) otherwise. + */ +size_t aesni_format_error( + const AesNI_ErrorDetails* err_details, + char* dest, + size_t dest_size); + +/** + * \brief Initializes an error details structure. + * + * \param[out] err_details The error details structure to fill. + */ +AesNI_StatusCode aesni_success( + AesNI_ErrorDetails* err_details); + +/** + * \brief Builds error details from a `NULL` argument error. + * + * \param[out] err_details The error details structure to fill. + * \param[in] param_name The parameter name. Must not be `NULL`. + */ +AesNI_StatusCode aesni_error_null_argument( + AesNI_ErrorDetails* err_details, + const char* param_name); + +/** + * \brief Builds error details from a parse error. + * + * \param[out] err_details The error details structure to fill. + * \param[in] src The string that failed to be parsed. + */ +AesNI_StatusCode aesni_error_parse( + AesNI_ErrorDetails* err_details, + const char* src, + const char* what); + +/** + * \brief Builds error details from an invalid PKCS7 padding error. + * + * \param[out] err_details The error details structure to fill. + */ +AesNI_StatusCode aesni_error_invalid_pkcs7_padding( + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_error_not_implemented( + AesNI_ErrorDetails* err_details, + const char* what); + +AesNI_StatusCode aesni_error_missing_padding( + AesNI_ErrorDetails* err_details); + +AesNI_StatusCode aesni_error_memory_allocation( + AesNI_ErrorDetails* err_details); + +#ifdef __cplusplus +} +#endif + +/** + * \} + */ diff --git a/include/aes/mode.h b/include/aes/mode.h new file mode 100644 index 0000000..7650ec0 --- /dev/null +++ b/include/aes/mode.h @@ -0,0 +1,169 @@ +/** + * \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 + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef enum +{ + AESNI_ECB, + AESNI_CBC, + AESNI_CFB, + AESNI_OFB, + AESNI_CTR, +} +AesNI_Mode; + +#define AESNI_ENCRYPT_BLOCK_ECB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_ECB( \ + AesNI_## prefix ##_Block plaintext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys) \ +{ \ + assert(encryption_keys); \ +\ + return aesni_## prefix ##_encrypt_block_(plaintext, encryption_keys); \ +} + +#define AESNI_DECRYPT_BLOCK_ECB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_ECB( \ + AesNI_## prefix ##_Block ciphertext, \ + const AesNI_## prefix ##_RoundKeys* decryption_keys) \ +{ \ + assert(decryption_keys); \ +\ + return aesni_## prefix ##_decrypt_block_(ciphertext, decryption_keys); \ +} + +#define AESNI_ENCRYPT_BLOCK_CBC(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CBC( \ + AesNI_## prefix ##_Block plaintext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_encrypt_block_( \ + aesni_## prefix ##_xor_blocks(plaintext, init_vector), encryption_keys); \ +} + +#define AESNI_DECRYPT_BLOCK_CBC(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CBC( \ + AesNI_## prefix ##_Block ciphertext, \ + const AesNI_## prefix ##_RoundKeys* decryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(decryption_keys); \ + assert(next_init_vector); \ +\ + AesNI_## prefix ##_Block plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_decrypt_block_(ciphertext, decryption_keys), init_vector); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_CFB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CFB( \ + AesNI_## prefix ##_Block plaintext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys), plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_CFB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CFB( \ + AesNI_## prefix ##_Block ciphertext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + AesNI_## prefix ##_Block plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys), ciphertext); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_OFB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_OFB( \ + AesNI_## prefix ##_Block plaintext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + AesNI_## prefix ##_Block tmp = aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys); \ + *next_init_vector = tmp; \ + return aesni_## prefix ##_xor_blocks(tmp, plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_OFB(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_OFB( \ + AesNI_## prefix ##_Block ciphertext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + return aesni_## prefix ##_encrypt_block_OFB( \ + ciphertext, encryption_keys, init_vector, next_init_vector); \ +} + +#define AESNI_ENCRYPT_BLOCK_CTR(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CTR( \ + AesNI_## prefix ##_Block plaintext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + AesNI_## prefix ##_Block ciphertext = aesni_## prefix ##_xor_blocks( \ + plaintext, aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys)); \ + *next_init_vector = aesni_## prefix ##_inc_block(init_vector); \ + return ciphertext; \ +} + +#define AESNI_DECRYPT_BLOCK_CTR(prefix) \ +static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CTR( \ + AesNI_## prefix ##_Block ciphertext, \ + const AesNI_## prefix ##_RoundKeys* encryption_keys, \ + AesNI_## prefix ##_Block init_vector, \ + AesNI_## prefix ##_Block* next_init_vector) \ +{ \ + assert(encryption_keys); \ + assert(next_init_vector); \ +\ + return aesni_## prefix ##_encrypt_block_CTR( \ + ciphertext, encryption_keys, init_vector, next_init_vector); \ +} + +#ifdef __cplusplus +} +#endif diff --git a/include/aes/padding.h b/include/aes/padding.h new file mode 100644 index 0000000..7f19b18 --- /dev/null +++ b/include/aes/padding.h @@ -0,0 +1,41 @@ +/** + * \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.h" + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef enum +{ + AESNI_PADDING_PKCS7, +} +AesNI_PaddingMethod; + +AesNI_StatusCode aesni_extract_padding_size( + AesNI_PaddingMethod, + const void* src, + size_t src_size, + size_t* padding_size, + AesNI_ErrorDetails*); + +AesNI_StatusCode aesni_fill_with_padding( + AesNI_PaddingMethod, + void* dest, + size_t padding_size, + AesNI_ErrorDetails*); + +#ifdef __cplusplus +} +#endif diff --git a/include/aesni/aes.h b/include/aesni/aes.h deleted file mode 100644 index ea859a3..0000000 --- a/include/aesni/aes.h +++ /dev/null @@ -1,529 +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 "data.h" -#include "error.h" -#include "mode.h" - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef AesNI_Block128 AesNI_AES_Block; -typedef AesNI_AES_Block AesNI_AES128_Block; -typedef AesNI_AES_Block AesNI_AES192_Block; -typedef AesNI_AES_Block AesNI_AES256_Block; - -typedef struct -{ - AesNI_AES_Block key; -} -AesNI_AES128_Key; - -typedef struct -{ - AesNI_AES_Block hi; - AesNI_AES_Block lo; -} -AesNI_AES192_Key; - -typedef struct -{ - AesNI_AES_Block hi; - AesNI_AES_Block lo; -} -AesNI_AES256_Key; - -static __inline void aesni_AES_make_block(AesNI_AES_Block* dest, int hi3, int hi2, int lo1, int lo0) -{ - *dest = aesni_make_block128(hi3, hi2, lo1, lo0); -} - -static __inline void aesni_AES128_make_block(AesNI_AES128_Block* dest, int hi3, int hi2, int lo1, int lo0) -{ - aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); -} - -static __inline void aesni_AES192_make_block(AesNI_AES192_Block* dest, int hi3, int hi2, int lo1, int lo0) -{ - aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); -} - -static __inline void aesni_AES256_make_block(AesNI_AES256_Block* dest, int hi3, int hi2, int lo1, int lo0) -{ - aesni_AES_make_block(dest, hi3, hi2, lo1, lo0); -} - -static __inline void aesni_AES128_make_key(AesNI_AES128_Key* dest, int hi3, int hi2, int lo1, int lo0) -{ - aesni_AES_make_block(&dest->key, hi3, hi2, lo1, lo0); -} - -static __inline void aesni_AES192_make_key(AesNI_AES192_Key* dest, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) -{ - aesni_AES_make_block(&dest->hi, 0, 0, hi5, hi4); - aesni_AES_make_block(&dest->lo, lo3, lo2, lo1, lo0); -} - -static __inline void aesni_AES256_make_key(AesNI_AES256_Key* dest, int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) -{ - aesni_AES_make_block(&dest->hi, hi7, hi6, hi5, hi4); - aesni_AES_make_block(&dest->lo, lo3, lo2, lo1, lo0); -} - -typedef struct { char str[33]; } AesNI_AES_BlockString; -typedef AesNI_AES_BlockString AesNI_AES128_BlockString; -typedef AesNI_AES_BlockString AesNI_AES192_BlockString; -typedef AesNI_AES_BlockString AesNI_AES256_BlockString; - -typedef struct { char str[49]; } AesNI_AES_BlockMatrixString; -typedef AesNI_AES_BlockMatrixString AesNI_AES128_BlockMatrixString; -typedef AesNI_AES_BlockMatrixString AesNI_AES192_BlockMatrixString; -typedef AesNI_AES_BlockMatrixString AesNI_AES256_BlockMatrixString; - -AesNI_StatusCode aesni_AES_format_block( - AesNI_AES_BlockString*, - const AesNI_AES_Block*, - AesNI_ErrorDetails*); - -static __inline AesNI_StatusCode aesni_AES128_format_block( - AesNI_AES128_BlockString* dest, - const AesNI_AES128_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES192_format_block( - AesNI_AES192_BlockString* dest, - const AesNI_AES192_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES256_format_block( - AesNI_AES256_BlockString* dest, - const AesNI_AES256_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block(dest, src, err_details); -} - -AesNI_StatusCode aesni_AES_format_block_as_matrix( - AesNI_AES_BlockMatrixString*, - const AesNI_AES_Block*, - AesNI_ErrorDetails*); - -static __inline AesNI_StatusCode aesni_AES128_format_block_as_matrix( - AesNI_AES128_BlockMatrixString* dest, - const AesNI_AES128_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block_as_matrix(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES192_format_block_as_matrix( - AesNI_AES192_BlockMatrixString* dest, - const AesNI_AES192_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block_as_matrix(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES256_format_block_as_matrix( - AesNI_AES256_BlockMatrixString* dest, - const AesNI_AES256_Block* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_format_block_as_matrix(dest, src, err_details); -} - -AesNI_StatusCode aesni_AES_print_block( - const AesNI_AES_Block*, - AesNI_ErrorDetails*); - -static __inline AesNI_StatusCode aesni_AES128_print_block( - const AesNI_AES128_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block(block, err_details); -} - -static __inline AesNI_StatusCode aesni_AES192_print_block( - const AesNI_AES192_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block(block, err_details); -} - -static __inline AesNI_StatusCode aesni_AES256_print_block( - const AesNI_AES256_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block(block, err_details); -} - -AesNI_StatusCode aesni_AES_print_block_as_matrix( - const AesNI_AES_Block*, - AesNI_ErrorDetails*); - -static __inline AesNI_StatusCode aesni_AES128_print_block_as_matrix( - const AesNI_AES128_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block_as_matrix(block, err_details); -} - -static __inline AesNI_StatusCode aesni_AES192_print_block_as_matrix( - const AesNI_AES192_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block_as_matrix(block, err_details); -} - -static __inline AesNI_StatusCode aesni_AES256_print_block_as_matrix( - const AesNI_AES256_Block* block, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_print_block_as_matrix(block, err_details); -} - -AesNI_StatusCode aesni_AES_parse_block( - AesNI_AES_Block* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -static __inline AesNI_StatusCode aesni_AES128_parse_block( - AesNI_AES128_Block* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_parse_block(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES192_parse_block( - AesNI_AES192_Block* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_parse_block(dest, src, err_details); -} - -static __inline AesNI_StatusCode aesni_AES256_parse_block( - AesNI_AES256_Block* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - return aesni_AES_parse_block(dest, src, err_details); -} - -typedef struct { char str[33]; } AesNI_AES128_KeyString; -typedef struct { char str[49]; } AesNI_AES192_KeyString; -typedef struct { char str[65]; } AesNI_AES256_KeyString; - -AesNI_StatusCode aesni_AES128_format_key( - AesNI_AES128_KeyString*, - const AesNI_AES128_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES192_format_key( - AesNI_AES192_KeyString*, - const AesNI_AES192_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES256_format_key( - AesNI_AES256_KeyString*, - const AesNI_AES256_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES128_print_key( - const AesNI_AES128_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES192_print_key( - const AesNI_AES192_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES256_print_key( - const AesNI_AES256_Key*, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_AES128_parse_key( - AesNI_AES128_Key* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_AES192_parse_key( - AesNI_AES192_Key* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_AES256_parse_key( - AesNI_AES256_Key* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -typedef struct -{ - AesNI_AES_Block keys[11]; -} -AesNI_AES128_RoundKeys; - -typedef struct -{ - AesNI_AES_Block keys[13]; -} -AesNI_AES192_RoundKeys; - -typedef struct -{ - AesNI_AES_Block keys[15]; -} -AesNI_AES256_RoundKeys; - -void __fastcall aesni_AES128_expand_key_( - AesNI_AES_Block key, - AesNI_AES128_RoundKeys* encryption_keys); - -void __fastcall aesni_AES192_expand_key_( - AesNI_AES_Block key_lo, - AesNI_AES_Block key_hi, - AesNI_AES192_RoundKeys* encryption_keys); - -void __fastcall aesni_AES256_expand_key_( - AesNI_AES_Block key_lo, - AesNI_AES_Block key_hi, - AesNI_AES256_RoundKeys* encryption_keys); - -void __fastcall aesni_AES128_derive_decryption_keys_( - const AesNI_AES128_RoundKeys* encryption_keys, - AesNI_AES128_RoundKeys* decryption_keys); - -void __fastcall aesni_AES192_derive_decryption_keys_( - const AesNI_AES192_RoundKeys* encryption_keys, - AesNI_AES192_RoundKeys* decryption_keys); - -void __fastcall aesni_AES256_derive_decryption_keys_( - const AesNI_AES256_RoundKeys* encryption_keys, - AesNI_AES256_RoundKeys* decryption_keys); - -AesNI_AES_Block __fastcall aesni_AES128_encrypt_block_( - AesNI_AES_Block plaintext, - const AesNI_AES128_RoundKeys*); - -AesNI_AES_Block __fastcall aesni_AES192_encrypt_block_( - AesNI_AES_Block plaintext, - const AesNI_AES192_RoundKeys*); - -AesNI_AES_Block __fastcall aesni_AES256_encrypt_block_( - AesNI_AES_Block plaintext, - const AesNI_AES256_RoundKeys*); - -AesNI_AES_Block __fastcall aesni_AES128_decrypt_block_( - AesNI_AES_Block ciphertext, - const AesNI_AES128_RoundKeys*); - -AesNI_AES_Block __fastcall aesni_AES192_decrypt_block_( - AesNI_AES_Block ciphertext, - const AesNI_AES192_RoundKeys*); - -AesNI_AES_Block __fastcall aesni_AES256_decrypt_block_( - AesNI_AES_Block ciphertext, - const AesNI_AES256_RoundKeys*); - -static __inline AesNI_AES_Block __fastcall aesni_AES_xor_blocks( - AesNI_AES_Block a, - AesNI_AES_Block b) -{ - return aesni_xor_block128(a, b); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES128_xor_blocks( - AesNI_AES128_Block a, - AesNI_AES128_Block b) -{ - return aesni_AES_xor_blocks(a, b); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES192_xor_blocks( - AesNI_AES192_Block a, - AesNI_AES192_Block b) -{ - return aesni_AES_xor_blocks(a, b); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES256_xor_blocks( - AesNI_AES256_Block a, - AesNI_AES256_Block b) -{ - return aesni_AES_xor_blocks(a, b); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES_inc_block( - AesNI_AES_Block block) -{ - block = aesni_reverse_byte_order_block128(block); - block = aesni_inc_block128(block); - return aesni_reverse_byte_order_block128(block); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES128_inc_block( - AesNI_AES128_Block block) -{ - return aesni_AES_inc_block(block); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES192_inc_block( - AesNI_AES192_Block block) -{ - return aesni_AES_inc_block(block); -} - -static __inline AesNI_AES_Block __fastcall aesni_AES256_inc_block( - AesNI_AES256_Block block) -{ - return aesni_AES_inc_block(block); -} - -AESNI_ENCRYPT_BLOCK_ECB(AES128); -AESNI_DECRYPT_BLOCK_ECB(AES128); -AESNI_ENCRYPT_BLOCK_CBC(AES128); -AESNI_DECRYPT_BLOCK_CBC(AES128); -AESNI_ENCRYPT_BLOCK_CFB(AES128); -AESNI_DECRYPT_BLOCK_CFB(AES128); -AESNI_ENCRYPT_BLOCK_OFB(AES128); -AESNI_DECRYPT_BLOCK_OFB(AES128); -AESNI_ENCRYPT_BLOCK_CTR(AES128); -AESNI_DECRYPT_BLOCK_CTR(AES128); - -AESNI_ENCRYPT_BLOCK_ECB(AES192); -AESNI_DECRYPT_BLOCK_ECB(AES192); -AESNI_ENCRYPT_BLOCK_CBC(AES192); -AESNI_DECRYPT_BLOCK_CBC(AES192); -AESNI_ENCRYPT_BLOCK_CFB(AES192); -AESNI_DECRYPT_BLOCK_CFB(AES192); -AESNI_ENCRYPT_BLOCK_OFB(AES192); -AESNI_DECRYPT_BLOCK_OFB(AES192); -AESNI_ENCRYPT_BLOCK_CTR(AES192); -AESNI_DECRYPT_BLOCK_CTR(AES192); - -AESNI_ENCRYPT_BLOCK_ECB(AES256); -AESNI_DECRYPT_BLOCK_ECB(AES256); -AESNI_ENCRYPT_BLOCK_CBC(AES256); -AESNI_DECRYPT_BLOCK_CBC(AES256); -AESNI_ENCRYPT_BLOCK_CFB(AES256); -AESNI_DECRYPT_BLOCK_CFB(AES256); -AESNI_ENCRYPT_BLOCK_OFB(AES256); -AESNI_DECRYPT_BLOCK_OFB(AES256); -AESNI_ENCRYPT_BLOCK_CTR(AES256); -AESNI_DECRYPT_BLOCK_CTR(AES256); - -/** - * \brief Expands an AES-128 key into 10 encryption round keys. - * - * \param[in] key The AES-128 key. - * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES128_expand_key( - const AesNI_AES128_Key* key, - AesNI_AES128_RoundKeys* encryption_keys) -{ - assert(encryption_keys); - - aesni_AES128_expand_key_(key->key, encryption_keys); -} - -/** - * \brief Derives AES-128 decryption round keys from AES-128 encryption round keys. - * - * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * \param[out] decryption_keys The AES-128 decryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES128_derive_decryption_keys( - const AesNI_AES128_RoundKeys* encryption_keys, - AesNI_AES128_RoundKeys* decryption_keys) -{ - assert(encryption_keys); - assert(decryption_keys); - - aesni_AES128_derive_decryption_keys_(encryption_keys, decryption_keys); -} - -/** - * \brief Expands an AES-192 key into 12 encryption round keys. - * - * \param[in] key The AES-192 key. - * \param[out] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES192_expand_key( - const AesNI_AES192_Key* key, - AesNI_AES192_RoundKeys* encryption_keys) -{ - assert(key); - assert(encryption_keys); - - aesni_AES192_expand_key_(key->lo, key->hi, encryption_keys); -} - -/** - * \brief Derives AES-192 decryption round keys from AES-192 encryption round keys. - * - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * \param[out] decryption_keys The AES-192 decryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES192_derive_decryption_keys( - const AesNI_AES192_RoundKeys* encryption_keys, - AesNI_AES192_RoundKeys* decryption_keys) -{ - assert(encryption_keys); - assert(decryption_keys); - - aesni_AES192_derive_decryption_keys_(encryption_keys, decryption_keys); -} - -/** - * \brief Expands an AES-256 key into 14 encryption round keys. - * - * \param[in] key The AES-256 key. - * \param[out] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES256_expand_key( - const AesNI_AES256_Key* key, - AesNI_AES256_RoundKeys* encryption_keys) -{ - assert(key); - assert(encryption_keys); - - aesni_AES256_expand_key_(key->lo, key->hi, encryption_keys); -} - -/** - * \brief Derives AES-256 decryption round keys from AES-256 encryption round keys. - * - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * \param[out] decryption_keys The AES-256 decryption round keys. Must not be `NULL`. - */ -static __inline void __fastcall aesni_AES256_derive_decryption_keys( - const AesNI_AES256_RoundKeys* encryption_keys, - AesNI_AES256_RoundKeys* decryption_keys) -{ - assert(encryption_keys); - assert(decryption_keys); - - aesni_AES256_derive_decryption_keys_(encryption_keys, decryption_keys); -} - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/algorithm.h b/include/aesni/algorithm.h deleted file mode 100644 index a73b412..0000000 --- a/include/aesni/algorithm.h +++ /dev/null @@ -1,26 +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 __cplusplus -extern "C" -{ -#endif - -typedef enum -{ - AESNI_AES128, - AESNI_AES192, - AESNI_AES256, -} -AesNI_Algorithm; - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/all.h b/include/aesni/all.h deleted file mode 100644 index bbea6f9..0000000 --- a/include/aesni/all.h +++ /dev/null @@ -1,26 +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. - * - * \brief Include this file to use the library. Includes all the other header - * files. - */ - -#pragma once - -/** - * \defgroup aesni AesNI - */ - -#include "aes.h" -#include "algorithm.h" -#include "box.h" -#include "box_aes.h" -#include "box_data.h" -#include "data.h" -#include "error.h" -#include "mode.h" -#include "padding.h" diff --git a/include/aesni/box.h b/include/aesni/box.h deleted file mode 100644 index 58517e1..0000000 --- a/include/aesni/box.h +++ /dev/null @@ -1,84 +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.h" -#include "box_data.h" -#include "error.h" - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -AesNI_StatusCode aesni_box_init( - AesNI_Box* box, - AesNI_Algorithm algorithm, - const AesNI_BoxKey* box_key, - AesNI_Mode mode, - const AesNI_BoxBlock* iv, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_parse_key( - AesNI_BoxKey* dest, - AesNI_Algorithm algorithm, - const char* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_parse_block( - AesNI_BoxBlock* dest, - AesNI_Algorithm algorithm, - const char* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_format_key( - AesNI_BoxKeyString* dest, - AesNI_Algorithm algorithm, - const AesNI_BoxKey* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_format_block( - AesNI_BoxBlockString* dest, - AesNI_Algorithm algorithm, - const AesNI_BoxBlock* src, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_encrypt_block( - AesNI_Box* box, - const AesNI_BoxBlock* plaintext, - AesNI_BoxBlock* ciphertext, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_decrypt_block( - AesNI_Box* box, - const AesNI_BoxBlock* ciphertext, - AesNI_BoxBlock* plaintext, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_encrypt_buffer( - AesNI_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_box_decrypt_buffer( - AesNI_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AesNI_ErrorDetails* err_details); - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/box_aes.h b/include/aesni/box_aes.h deleted file mode 100644 index 3d7faec..0000000 --- a/include/aesni/box_aes.h +++ /dev/null @@ -1,24 +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 "box_data.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes128; -extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes192; -extern AesNI_BoxAlgorithmInterface aesni_box_algorithm_aes256; - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h deleted file mode 100644 index 888c7c0..0000000 --- a/include/aesni/box_data.h +++ /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 "aes.h" -#include "error.h" -#include "mode.h" - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef union -{ - AesNI_AES128_Key aes128_key; - AesNI_AES192_Key aes192_key; - AesNI_AES256_Key aes256_key; -} -AesNI_BoxKey; - -typedef union -{ - AesNI_AES128_RoundKeys aes128_encryption_keys; - AesNI_AES192_RoundKeys aes192_encryption_keys; - AesNI_AES256_RoundKeys aes256_encryption_keys; -} -AesNI_BoxEncryptionRoundKeys; - -typedef union -{ - AesNI_AES128_RoundKeys aes128_decryption_keys; - AesNI_AES192_RoundKeys aes192_decryption_keys; - AesNI_AES256_RoundKeys aes256_decryption_keys; -} -AesNI_BoxDecryptionRoundKeys; - -typedef union -{ - AesNI_AES128_KeyString aes128; - AesNI_AES192_KeyString aes192; - AesNI_AES256_KeyString aes256; -} -AesNI_BoxKeyString; - -typedef union -{ - AesNI_AES_Block aes_block; -} -AesNI_BoxBlock; - -typedef union -{ - AesNI_AES_BlockString aes; -} -AesNI_BoxBlockString; - -typedef AesNI_StatusCode (*AesNI_BoxCalculateRoundKeys)( - const AesNI_BoxKey* params, - AesNI_BoxEncryptionRoundKeys*, - AesNI_BoxDecryptionRoundKeys*, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxParseBlock)( - AesNI_BoxBlock* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxParseKey)( - AesNI_BoxKey* dest, - const char* src, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxFormatBlock)( - AesNI_BoxBlockString* dest, - const AesNI_BoxBlock* src, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxFormatKey)( - AesNI_BoxKeyString* dest, - const AesNI_BoxKey* src, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxEncryptBlock)( - const AesNI_BoxBlock* plaintext, - const AesNI_BoxEncryptionRoundKeys* params, - AesNI_BoxBlock* ciphertext, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxDecryptBlock)( - const AesNI_BoxBlock* ciphertext, - const AesNI_BoxDecryptionRoundKeys* params, - AesNI_BoxBlock* plaintext, - AesNI_ErrorDetails* err_details); - -typedef AesNI_StatusCode (*AesNI_BoxXorBlock)( - AesNI_BoxBlock*, - const AesNI_BoxBlock*, - AesNI_ErrorDetails*); - -typedef AesNI_StatusCode (*AesNI_BoxIncBlock)( - AesNI_BoxBlock*, - AesNI_ErrorDetails*); - -typedef AesNI_StatusCode (*AesNI_BoxGetBlockSize)( - size_t*, - AesNI_ErrorDetails*); - -typedef AesNI_StatusCode (*AesNI_BoxStoreBlock)( - void*, - const AesNI_BoxBlock*, - AesNI_ErrorDetails*); - -typedef AesNI_StatusCode (*AesNI_BoxLoadBlock)( - AesNI_BoxBlock*, - const void*, - AesNI_ErrorDetails*); - -typedef struct -{ - AesNI_BoxCalculateRoundKeys calc_round_keys; - AesNI_BoxParseBlock parse_block; - AesNI_BoxParseKey parse_key; - AesNI_BoxFormatBlock format_block; - AesNI_BoxFormatKey format_key; - AesNI_BoxEncryptBlock encrypt_block; - AesNI_BoxDecryptBlock decrypt_block; - AesNI_BoxXorBlock xor_block; - AesNI_BoxIncBlock inc_block; - AesNI_BoxGetBlockSize get_block_size; - AesNI_BoxStoreBlock store_block; - AesNI_BoxLoadBlock load_block; -} -AesNI_BoxAlgorithmInterface; - -typedef struct -{ - const AesNI_BoxAlgorithmInterface* algorithm; - AesNI_BoxEncryptionRoundKeys encryption_keys; - AesNI_BoxDecryptionRoundKeys decryption_keys; - AesNI_Mode mode; - AesNI_BoxBlock iv; -} -AesNI_Box; - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/data.h b/include/aesni/data.h deleted file mode 100644 index 94cff2c..0000000 --- a/include/aesni/data.h +++ /dev/null @@ -1,125 +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 - -#ifdef __cplusplus -extern "C" -{ -#endif - -/** - * \brief Represents a 128-bit block. - */ -typedef __m128i AesNI_Block128; - -/** - * \brief Loads a 128-bit block from a memory location. - * - * \param[in] src The pointer to a memory location. Must not be `NULL`. - * - * \return The loaded 128-bit block. - */ -static __inline AesNI_Block128 aesni_load_block128(const void* src) -{ - return _mm_loadu_si128((AesNI_Block128*) src); -} - -/** - * \brief Loads a 128-bit block from a 16-byte aligned memory location. - * - * \param[in] src The pointer to a 16-byte aligned memory location. Must not be `NULL`. - * - * \return The loaded 128-bit block. - */ -static __inline AesNI_Block128 aesni_load_block128_aligned(const void* src) -{ - return _mm_load_si128((AesNI_Block128*) src); -} - -/** - * \brief Stores a 128-bit block in a memory location. - * - * \param[out] dest The pointer to a memory location. Must not be `NULL`. - * - * \param[in] block The block to be stored. - */ -static __inline void __fastcall aesni_store_block128( - void* dest, - AesNI_Block128 block) -{ - _mm_storeu_si128((AesNI_Block128*) dest, block); -} - -/** - * \brief Stores a 128-bit block in a 16-byte aligned memory location. - * - * \param[out] dest The pointer to a 16-byte aligned memory location. Must not be `NULL`. - * - * \param[in] block The block to be stored. - */ -static __inline void __fastcall aesni_store_block128_aligned( - void* dest, - AesNI_Block128 block) -{ - _mm_store_si128((AesNI_Block128*) dest, block); -} - -/** - * \brief XORs two 128-bit blocks. - * - * \param[in] a The first XOR operand. - * \param[in] b The second XOR operand. - * - * \return `a^b`. - */ -static __inline AesNI_Block128 __fastcall aesni_xor_block128( - AesNI_Block128 a, - AesNI_Block128 b) -{ - return _mm_xor_si128(a, b); -} - -/** - * \brief Builds a 128-bit block from four 4-byte values. - * - * Builds a 128-bit block like this: - * - * * dest[127:96] = hi3 - * * dest[95:64] = hi2 - * * dest[63:32] = lo1 - * * dest[31:0] = lo0 - * - * \param[in] hi3 The most significant 4-byte value. - * \param[in] hi2 The more significant 4-byte value. - * \param[in] lo1 The less significant 4-byte value. - * \param[in] lo0 The least significant 4-byte value. - * - * \return The built 128-bit block. - */ -static __inline AesNI_Block128 __fastcall aesni_make_block128(int hi3, int hi2, int lo1, int lo0) -{ - return _mm_set_epi32(hi3, hi2, lo1, lo0); -} - -static __inline AesNI_Block128 __fastcall aesni_reverse_byte_order_block128(AesNI_Block128 block) -{ - return _mm_shuffle_epi8(block, aesni_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); -} - -static __inline AesNI_Block128 __fastcall aesni_inc_block128(AesNI_Block128 x) -{ - return _mm_add_epi32(x, aesni_make_block128(0, 0, 0, 1)); -} - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/error.h b/include/aesni/error.h deleted file mode 100644 index a51284f..0000000 --- a/include/aesni/error.h +++ /dev/null @@ -1,184 +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 - -/** - * \defgroup aesni_error_handling Error handling - * \ingroup aesni - * \brief Error data structures and formatting functions. - * - * Some library functions cannot fail, which is simple. - * Other functions return an error code. - * You can check if a function exited with an error by passing the returned - * error code to aesni_is_error(). - * - * Some possibly-may-fail functions accept a pointer to an "error details" - * structure. - * This pointer can always be `NULL`. - * In this case, simply an error code is returned. - * Otherwise, the error details structure is filled with appropriate info about - * the error, possibly including a few details like invalid arguments names, - * etc. - * - * You can format an error details structure using the formatting functions. - * \{ - */ - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -/** - * \brief API status codes. - */ -typedef enum -{ - AESNI_SUCCESS, ///< Everything went fine - AESNI_NULL_ARGUMENT_ERROR, ///< Invalid argument value NULL - AESNI_PARSE_ERROR, ///< Couldn't parse - AESNI_INVALID_PKCS7_PADDING_ERROR, ///< Invalid PKCS7 padding while decrypting - AESNI_NOT_IMPLEMENTED_ERROR, ///< Not implemented - AESNI_MISSING_PADDING_ERROR, - AESNI_MEMORY_ALLOCATION_ERROR, -} -AesNI_StatusCode; - -static __inline int aesni_is_error(AesNI_StatusCode ec) -{ - return ec != AESNI_SUCCESS; -} - -/** - * \brief Retrieves a simple error message for an error code. - * - * For example, - * \code{.c} - * printf("%s\n", aesni_strerror(AESNI_NULL_ARGUMENT_ERROR)); - * \endcode - * would print - * \code - * Invalid argument value NULL - * \endcode - * - * \param[in] ec The error code. - * \return A pointer to a statically-allocated C string. - */ -const char* aesni_strerror(AesNI_StatusCode ec); - -#define AESNI_MAX_CALL_STACK_LENGTH 32 - -/** - * \brief Stores error details: error code & possibly a few parameters. - */ -typedef struct -{ - AesNI_StatusCode ec; ///< Error code - - union - { - struct { char param_name[32]; } null_arg; - struct - { - char src[128]; - char what[32]; - } - parse_error; - struct { char what[128]; } not_implemented; - } - params; - - void* call_stack[AESNI_MAX_CALL_STACK_LENGTH]; - size_t call_stack_size; -} -AesNI_ErrorDetails; - -/** - * \brief Extracts an error code from error details. - * - * \param[in] err_details The error details structure. Must not be `NULL`. - * \return The error code stored in the error details. - */ -static __inline AesNI_StatusCode aesni_get_error_code( - const AesNI_ErrorDetails* err_details) -{ - return err_details->ec; -} - -/** - * \brief Formats a pretty error message, including error parameters. - * - * \param[in] err_details The pointer to error details. Must not be `NULL`. - * \param[out] dest The pointer to the destination string buffer. - * \param[in] dest_size The size of the destination buffer, in bytes. - * \return If `dest` is NULL, the number of bytes required to store the full - * error message, and the number of characters written (excluding the - * terminating '\0' character) otherwise. - */ -size_t aesni_format_error( - const AesNI_ErrorDetails* err_details, - char* dest, - size_t dest_size); - -/** - * \brief Initializes an error details structure. - * - * \param[out] err_details The error details structure to fill. - */ -AesNI_StatusCode aesni_success( - AesNI_ErrorDetails* err_details); - -/** - * \brief Builds error details from a `NULL` argument error. - * - * \param[out] err_details The error details structure to fill. - * \param[in] param_name The parameter name. Must not be `NULL`. - */ -AesNI_StatusCode aesni_error_null_argument( - AesNI_ErrorDetails* err_details, - const char* param_name); - -/** - * \brief Builds error details from a parse error. - * - * \param[out] err_details The error details structure to fill. - * \param[in] src The string that failed to be parsed. - */ -AesNI_StatusCode aesni_error_parse( - AesNI_ErrorDetails* err_details, - const char* src, - const char* what); - -/** - * \brief Builds error details from an invalid PKCS7 padding error. - * - * \param[out] err_details The error details structure to fill. - */ -AesNI_StatusCode aesni_error_invalid_pkcs7_padding( - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_error_not_implemented( - AesNI_ErrorDetails* err_details, - const char* what); - -AesNI_StatusCode aesni_error_missing_padding( - AesNI_ErrorDetails* err_details); - -AesNI_StatusCode aesni_error_memory_allocation( - AesNI_ErrorDetails* err_details); - -#ifdef __cplusplus -} -#endif - -/** - * \} - */ diff --git a/include/aesni/mode.h b/include/aesni/mode.h deleted file mode 100644 index 7650ec0..0000000 --- a/include/aesni/mode.h +++ /dev/null @@ -1,169 +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 - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef enum -{ - AESNI_ECB, - AESNI_CBC, - AESNI_CFB, - AESNI_OFB, - AESNI_CTR, -} -AesNI_Mode; - -#define AESNI_ENCRYPT_BLOCK_ECB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_ECB( \ - AesNI_## prefix ##_Block plaintext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys) \ -{ \ - assert(encryption_keys); \ -\ - return aesni_## prefix ##_encrypt_block_(plaintext, encryption_keys); \ -} - -#define AESNI_DECRYPT_BLOCK_ECB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_ECB( \ - AesNI_## prefix ##_Block ciphertext, \ - const AesNI_## prefix ##_RoundKeys* decryption_keys) \ -{ \ - assert(decryption_keys); \ -\ - return aesni_## prefix ##_decrypt_block_(ciphertext, decryption_keys); \ -} - -#define AESNI_ENCRYPT_BLOCK_CBC(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CBC( \ - AesNI_## prefix ##_Block plaintext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return *next_init_vector = aesni_## prefix ##_encrypt_block_( \ - aesni_## prefix ##_xor_blocks(plaintext, init_vector), encryption_keys); \ -} - -#define AESNI_DECRYPT_BLOCK_CBC(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CBC( \ - AesNI_## prefix ##_Block ciphertext, \ - const AesNI_## prefix ##_RoundKeys* decryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(decryption_keys); \ - assert(next_init_vector); \ -\ - AesNI_## prefix ##_Block plaintext = aesni_## prefix ##_xor_blocks( \ - aesni_## prefix ##_decrypt_block_(ciphertext, decryption_keys), init_vector); \ - *next_init_vector = ciphertext; \ - return plaintext; \ -} - -#define AESNI_ENCRYPT_BLOCK_CFB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CFB( \ - AesNI_## prefix ##_Block plaintext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return *next_init_vector = aesni_## prefix ##_xor_blocks( \ - aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys), plaintext); \ -} - -#define AESNI_DECRYPT_BLOCK_CFB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CFB( \ - AesNI_## prefix ##_Block ciphertext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AesNI_## prefix ##_Block plaintext = aesni_## prefix ##_xor_blocks( \ - aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys), ciphertext); \ - *next_init_vector = ciphertext; \ - return plaintext; \ -} - -#define AESNI_ENCRYPT_BLOCK_OFB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_OFB( \ - AesNI_## prefix ##_Block plaintext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AesNI_## prefix ##_Block tmp = aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys); \ - *next_init_vector = tmp; \ - return aesni_## prefix ##_xor_blocks(tmp, plaintext); \ -} - -#define AESNI_DECRYPT_BLOCK_OFB(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_OFB( \ - AesNI_## prefix ##_Block ciphertext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return aesni_## prefix ##_encrypt_block_OFB( \ - ciphertext, encryption_keys, init_vector, next_init_vector); \ -} - -#define AESNI_ENCRYPT_BLOCK_CTR(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_encrypt_block_CTR( \ - AesNI_## prefix ##_Block plaintext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AesNI_## prefix ##_Block ciphertext = aesni_## prefix ##_xor_blocks( \ - plaintext, aesni_## prefix ##_encrypt_block_(init_vector, encryption_keys)); \ - *next_init_vector = aesni_## prefix ##_inc_block(init_vector); \ - return ciphertext; \ -} - -#define AESNI_DECRYPT_BLOCK_CTR(prefix) \ -static __inline AesNI_## prefix ##_Block __fastcall aesni_## prefix ##_decrypt_block_CTR( \ - AesNI_## prefix ##_Block ciphertext, \ - const AesNI_## prefix ##_RoundKeys* encryption_keys, \ - AesNI_## prefix ##_Block init_vector, \ - AesNI_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return aesni_## prefix ##_encrypt_block_CTR( \ - ciphertext, encryption_keys, init_vector, next_init_vector); \ -} - -#ifdef __cplusplus -} -#endif diff --git a/include/aesni/padding.h b/include/aesni/padding.h deleted file mode 100644 index 7f19b18..0000000 --- a/include/aesni/padding.h +++ /dev/null @@ -1,41 +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.h" - -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef enum -{ - AESNI_PADDING_PKCS7, -} -AesNI_PaddingMethod; - -AesNI_StatusCode aesni_extract_padding_size( - AesNI_PaddingMethod, - const void* src, - size_t src_size, - size_t* padding_size, - AesNI_ErrorDetails*); - -AesNI_StatusCode aesni_fill_with_padding( - AesNI_PaddingMethod, - void* dest, - size_t padding_size, - AesNI_ErrorDetails*); - -#ifdef __cplusplus -} -#endif diff --git a/src/aes.c b/src/aes.c index 82db7cc..f3dc178 100644 --- a/src/aes.c +++ b/src/aes.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/box.c b/src/box.c index 8869f69..7a26333 100644 --- a/src/box.c +++ b/src/box.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/box_aes.c b/src/box_aes.c index 62cb4f2..66eacae 100644 --- a/src/box_aes.c +++ b/src/box_aes.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/c/aes128.c b/src/c/aes128.c index a99c8a7..94e8a05 100644 --- a/src/c/aes128.c +++ b/src/c/aes128.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/c/aes192.c b/src/c/aes192.c index a8ae6cf..570ab0e 100644 --- a/src/c/aes192.c +++ b/src/c/aes192.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/c/aes256.c b/src/c/aes256.c index 6099204..23ac9ca 100644 --- a/src/c/aes256.c +++ b/src/c/aes256.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/error.c b/src/error.c index 10550f3..49d8c72 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/src/padding.c b/src/padding.c index 08cafb1..f76e7ca 100644 --- a/src/padding.c +++ b/src/padding.c @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include +#include #include #include diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 0d57e27..6a69af0 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -2,30 +2,30 @@ find_package(Boost REQUIRED COMPONENTS filesystem program_options system) add_executable(util_encrypt_block encrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp block_input.hpp data_parsers.hpp) target_include_directories(util_encrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_encrypt_block libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_encrypt_block libaesxx ${Boost_LIBRARIES}) set_target_properties(util_encrypt_block PROPERTIES OUTPUT_NAME encrypt_block) add_executable(util_decrypt_block decrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp block_input.hpp data_parsers.hpp) target_include_directories(util_decrypt_block PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_decrypt_block libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_decrypt_block libaesxx ${Boost_LIBRARIES}) set_target_properties(util_decrypt_block PROPERTIES OUTPUT_NAME decrypt_block) add_executable(util_encrypt_file encrypt_file.cpp file_cmd_parser.hpp data_parsers.hpp) target_include_directories(util_encrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_encrypt_file libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_encrypt_file libaesxx ${Boost_LIBRARIES}) set_target_properties(util_encrypt_file PROPERTIES OUTPUT_NAME encrypt_file) add_executable(util_decrypt_file decrypt_file.cpp file_cmd_parser.hpp data_parsers.hpp) target_include_directories(util_decrypt_file PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_decrypt_file libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_decrypt_file libaesxx ${Boost_LIBRARIES}) set_target_properties(util_decrypt_file PROPERTIES OUTPUT_NAME decrypt_file) add_executable(util_encrypt_bmp encrypt_bmp.cpp file_cmd_parser.hpp data_parsers.hpp) target_include_directories(util_encrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_encrypt_bmp libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_encrypt_bmp libaesxx ${Boost_LIBRARIES}) set_target_properties(util_encrypt_bmp PROPERTIES OUTPUT_NAME encrypt_bmp) add_executable(util_decrypt_bmp decrypt_bmp.cpp file_cmd_parser.hpp data_parsers.hpp) target_include_directories(util_decrypt_bmp PRIVATE ${Boost_INCLUDE_DIRS}) -target_link_libraries(util_decrypt_bmp libaesnixx ${Boost_LIBRARIES}) +target_link_libraries(util_decrypt_bmp libaesxx ${Boost_LIBRARIES}) set_target_properties(util_decrypt_bmp PROPERTIES OUTPUT_NAME decrypt_bmp) diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 62b0f36..db6da84 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -11,7 +11,7 @@ #include "block_input.hpp" #include "data_parsers.hpp" -#include +#include #include #include diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index a473cd1..f41018d 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -8,7 +8,7 @@ #pragma once -#include +#include #include diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp index 333962e..d081659 100644 --- a/utils/data_parsers.hpp +++ b/utils/data_parsers.hpp @@ -8,7 +8,7 @@ #pragma once -#include +#include #include #include diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index 456ef6c..7369052 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -10,7 +10,7 @@ #include "block_dumper.hpp" #include "block_input.hpp" -#include +#include #include diff --git a/utils/decrypt_bmp.cpp b/utils/decrypt_bmp.cpp index dac042a..c1f36bf 100644 --- a/utils/decrypt_bmp.cpp +++ b/utils/decrypt_bmp.cpp @@ -8,7 +8,7 @@ #include "file_cmd_parser.hpp" -#include +#include #include diff --git a/utils/decrypt_file.cpp b/utils/decrypt_file.cpp index dd95928..ad7e9d0 100644 --- a/utils/decrypt_file.cpp +++ b/utils/decrypt_file.cpp @@ -8,7 +8,7 @@ #include "file_cmd_parser.hpp" -#include +#include #include diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp index 98fdec6..34b6d84 100644 --- a/utils/encrypt_block.cpp +++ b/utils/encrypt_block.cpp @@ -10,7 +10,7 @@ #include "block_dumper.hpp" #include "block_input.hpp" -#include +#include #include diff --git a/utils/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp index 2dc3ea5..ea498cb 100644 --- a/utils/encrypt_bmp.cpp +++ b/utils/encrypt_bmp.cpp @@ -8,7 +8,7 @@ #include "file_cmd_parser.hpp" -#include +#include #include diff --git a/utils/encrypt_file.cpp b/utils/encrypt_file.cpp index bde7d83..3ae8744 100644 --- a/utils/encrypt_file.cpp +++ b/utils/encrypt_file.cpp @@ -8,7 +8,7 @@ #include "file_cmd_parser.hpp" -#include +#include #include diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index bc641ed..c8061fa 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -10,7 +10,7 @@ #include "data_parsers.hpp" -#include +#include #include #include -- cgit v1.2.3