diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-17 20:07:32 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-17 20:07:32 +0300 |
commit | 522a5b906d7620bcebddb5a8f476b022c140ab27 (patch) | |
tree | 8b02e4f5b5948da91e4117c6892d850eeeac8445 /include/aesni | |
parent | refactoring (diff) | |
download | aes-tools-522a5b906d7620bcebddb5a8f476b022c140ab27.tar.gz aes-tools-522a5b906d7620bcebddb5a8f476b022c140ab27.zip |
factoring out AES-specific stuff
Diffstat (limited to '')
-rw-r--r-- | include/aesni/algorithm.h | 17 | ||||
-rw-r--r-- | include/aesni/all.h | 4 | ||||
-rw-r--r-- | include/aesni/box.h | 60 | ||||
-rw-r--r-- | include/aesni/box_aes.h | 155 | ||||
-rw-r--r-- | include/aesni/box_data.h | 110 | ||||
-rw-r--r-- | include/aesni/mode.h | 19 |
6 files changed, 276 insertions, 89 deletions
diff --git a/include/aesni/algorithm.h b/include/aesni/algorithm.h deleted file mode 100644 index 8aacfdc..0000000 --- a/include/aesni/algorithm.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -typedef enum -{ - AESNI_AES128, - AESNI_AES192, - AESNI_AES256, -} -AesNI_Algorithm; diff --git a/include/aesni/all.h b/include/aesni/all.h index 26f7a41..dfe9494 100644 --- a/include/aesni/all.h +++ b/include/aesni/all.h @@ -16,9 +16,9 @@ */ #include "aes.h" -#include "algorithm.h" #include "box.h" +#include "box_aes.h" +#include "box_data.h" #include "buffer.h" #include "data.h" #include "error.h" -#include "mode.h" diff --git a/include/aesni/box.h b/include/aesni/box.h index 70b2787..fd28cf1 100644 --- a/include/aesni/box.h +++ b/include/aesni/box.h @@ -8,74 +8,32 @@ #pragma once -#include "algorithm.h" -#include "data.h" +#include "box_data.h" #include "error.h" -#include "mode.h" #ifdef __cplusplus extern "C" { #endif -typedef union -{ - AesNI_Aes128_RoundKeys aes128_key_schedule; - AesNI_Aes192_RoundKeys aes192_key_schedule; - AesNI_Aes256_RoundKeys aes256_key_schedule; -} -AesNI_EncryptionParams; - -typedef union -{ - AesNI_Aes128_RoundKeys aes128_key_schedule; - AesNI_Aes192_RoundKeys aes192_key_schedule; - AesNI_Aes256_RoundKeys aes256_key_schedule; -} -AesNI_DecryptionParams; - -typedef union -{ - AesNI_Block128 aes_block; -} -AesNI_State; - -typedef union -{ - AesNI_Block128 aes128_key; - AesNI_Block192 aes192_key; - AesNI_Block256 aes256_key; -} -AesNI_AlgorithmParams; - -typedef struct -{ - AesNI_Algorithm algorithm; - AesNI_EncryptionParams encrypt_params; - AesNI_DecryptionParams decrypt_params; - AesNI_Mode mode; - AesNI_State iv; -} -AesNI_Box; - AesNI_StatusCode aesni_box_init( AesNI_Box*, - AesNI_Algorithm, - const AesNI_AlgorithmParams*, - AesNI_Mode, - const AesNI_State* iv, + AesNI_BoxAlgorithm, + const AesNI_BoxAlgorithmParams*, + AesNI_BoxMode, + const AesNI_BoxBlock* iv, AesNI_ErrorDetails*); AesNI_StatusCode aesni_box_encrypt( AesNI_Box*, - const AesNI_State*, - AesNI_State*, + const AesNI_BoxBlock*, + AesNI_BoxBlock*, AesNI_ErrorDetails*); AesNI_StatusCode aesni_box_decrypt( AesNI_Box*, - const AesNI_State*, - AesNI_State*, + const AesNI_BoxBlock*, + AesNI_BoxBlock*, AesNI_ErrorDetails*); #ifdef __cplusplus diff --git a/include/aesni/box_aes.h b/include/aesni/box_aes.h new file mode 100644 index 0000000..391f13e --- /dev/null +++ b/include/aesni/box_aes.h @@ -0,0 +1,155 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \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 "box_aes.h" +#include "box_data.h" +#include "data.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +static __inline AesNI_StatusCode aesni_box_derive_params_aes128( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes128_expand_key_( + algorithm_params->aes128_key, + &encrypt_params->aes128_encryption_keys); + aesni_aes128_derive_decryption_keys_( + &encrypt_params->aes128_encryption_keys, + &decrypt_params->aes128_decryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_derive_params_aes192( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes192_expand_key_( + algorithm_params->aes192_key.lo, + algorithm_params->aes192_key.hi, + &encrypt_params->aes192_encryption_keys); + aesni_aes192_derive_decryption_keys_( + &encrypt_params->aes192_encryption_keys, + &decrypt_params->aes192_decryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_derive_params_aes256( + const AesNI_BoxAlgorithmParams* algorithm_params, + AesNI_BoxEncryptionParams* encrypt_params, + AesNI_BoxDecryptionParams* decrypt_params, + AesNI_ErrorDetails* err_details) +{ + aesni_aes256_expand_key_( + algorithm_params->aes256_key.lo, + algorithm_params->aes256_key.hi, + &encrypt_params->aes256_encryption_keys); + aesni_aes256_derive_decryption_keys_( + &encrypt_params->aes256_encryption_keys, + &decrypt_params->aes256_decryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_xor_block_aes( + AesNI_BoxBlock* dest, + const AesNI_BoxBlock* src, + AesNI_ErrorDetails* err_details) +{ + dest->aes_block = aesni_xor_block128(dest->aes_block, src->aes_block); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_encrypt_aes128( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes128_encrypt_block_( + input->aes_block, + ¶ms->aes128_encryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_decrypt_aes128( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes128_decrypt_block_( + input->aes_block, + ¶ms->aes128_decryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_encrypt_aes192( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes192_encrypt_block_( + input->aes_block, + ¶ms->aes192_encryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_decrypt_aes192( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes192_decrypt_block_( + input->aes_block, + ¶ms->aes192_decryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_encrypt_aes256( + const AesNI_BoxBlock* input, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes256_encrypt_block_( + input->aes_block, + ¶ms->aes256_encryption_keys); + return AESNI_SUCCESS; +} + +static __inline AesNI_StatusCode aesni_box_decrypt_aes256( + const AesNI_BoxBlock* input, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* output, + AesNI_ErrorDetails* err_details) +{ + output->aes_block = aesni_aes256_decrypt_block_( + input->aes_block, + ¶ms->aes256_decryption_keys); + return AESNI_SUCCESS; +} + +extern AesNI_BoxAlgorithmInterface aesni_box_aes128_iface; +extern AesNI_BoxAlgorithmInterface aesni_box_aes192_iface; +extern AesNI_BoxAlgorithmInterface aesni_box_aes256_iface; + +#ifdef __cplusplus +} +#endif diff --git a/include/aesni/box_data.h b/include/aesni/box_data.h new file mode 100644 index 0000000..160cf34 --- /dev/null +++ b/include/aesni/box_data.h @@ -0,0 +1,110 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "error.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef union +{ + AesNI_Block128 aes128_key; + AesNI_Block192 aes192_key; + AesNI_Block256 aes256_key; +} +AesNI_BoxAlgorithmParams; + +typedef enum +{ + AESNI_AES128, + AESNI_AES192, + AESNI_AES256, +} +AesNI_BoxAlgorithm; + +typedef enum +{ + AESNI_ECB, + AESNI_CBC, + AESNI_CFB, + AESNI_OFB, + AESNI_CTR, +} +AesNI_BoxMode; + +typedef union +{ + AesNI_Aes128_RoundKeys aes128_encryption_keys; + AesNI_Aes192_RoundKeys aes192_encryption_keys; + AesNI_Aes256_RoundKeys aes256_encryption_keys; +} +AesNI_BoxEncryptionParams; + +typedef union +{ + AesNI_Aes128_RoundKeys aes128_decryption_keys; + AesNI_Aes192_RoundKeys aes192_decryption_keys; + AesNI_Aes256_RoundKeys aes256_decryption_keys; +} +AesNI_BoxDecryptionParams; + +typedef union +{ + AesNI_Block128 aes_block; +} +AesNI_BoxBlock; + +typedef AesNI_StatusCode (*AesNI_BoxDeriveParams)( + const AesNI_BoxAlgorithmParams* params, + AesNI_BoxEncryptionParams*, + AesNI_BoxDecryptionParams*, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxEncrypt)( + const AesNI_BoxBlock* plaintext, + const AesNI_BoxEncryptionParams* params, + AesNI_BoxBlock* ciphertext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxDecrypt)( + const AesNI_BoxBlock* ciphertext, + const AesNI_BoxDecryptionParams* params, + AesNI_BoxBlock* plaintext, + AesNI_ErrorDetails* err_details); + +typedef AesNI_StatusCode (*AesNI_BoxXorBlock)( + AesNI_BoxBlock*, + const AesNI_BoxBlock*, + AesNI_ErrorDetails*); + +typedef struct +{ + AesNI_BoxDeriveParams derive_params; + AesNI_BoxEncrypt encrypt; + AesNI_BoxDecrypt decrypt; + AesNI_BoxXorBlock xor_block; +} +AesNI_BoxAlgorithmInterface; + +typedef struct +{ + const AesNI_BoxAlgorithmInterface* algorithm_iface; + AesNI_BoxEncryptionParams encrypt_params; + AesNI_BoxDecryptionParams decrypt_params; + AesNI_BoxMode mode; + AesNI_BoxBlock iv; +} +AesNI_Box; + +#ifdef __cplusplus +} +#endif diff --git a/include/aesni/mode.h b/include/aesni/mode.h deleted file mode 100644 index fc00e9c..0000000 --- a/include/aesni/mode.h +++ /dev/null @@ -1,19 +0,0 @@ -/** - * \file - * \author Egor Tensin <Egor.Tensin@gmail.com> - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -typedef enum -{ - AESNI_ECB, - AESNI_CBC, - AESNI_CFB, - AESNI_OFB, - AESNI_CTR, -} -AesNI_Mode; |