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 --- 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 ---- 20 files changed, 1364 insertions(+), 1364 deletions(-) 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 (limited to 'include') 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 -- cgit v1.2.3