diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-24 01:16:40 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-24 01:16:40 +0300 |
commit | 724583d4ab12e520b29c00c36ffe2840b6962059 (patch) | |
tree | 6f62422ccd66afc60724c86a54a6bf374a203124 /include/aesni | |
parent | test/toolkit.py: fix executable names (diff) | |
download | aes-tools-724583d4ab12e520b29c00c36ffe2840b6962059.tar.gz aes-tools-724583d4ab12e520b29c00c36ffe2840b6962059.zip |
use macros to get rid of repetitive code
Diffstat (limited to '')
-rw-r--r-- | include/aesni/aes.h | 717 | ||||
-rw-r--r-- | include/aesni/mode.h | 135 |
2 files changed, 210 insertions, 642 deletions
diff --git a/include/aesni/aes.h b/include/aesni/aes.h index bad5463..427ca85 100644 --- a/include/aesni/aes.h +++ b/include/aesni/aes.h @@ -10,6 +10,7 @@ #include "data.h" #include "error.h" +#include "mode.h" #include <assert.h> @@ -202,258 +203,122 @@ AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_( AesNI_Aes_Block ciphertext, const AesNI_Aes256_RoundKeys*); -static __inline AesNI_Aes_Block __fastcall aesni_aes_inc_counter(AesNI_Aes_Block block) +static __inline AesNI_Aes_Block __fastcall aesni_aes_xor_blocks( + AesNI_Aes_Block a, + AesNI_Aes_Block b) { - block = aesni_reverse_byte_order_block128(block); - block = aesni_inc_block128(block); - return aesni_reverse_byte_order_block128(block); + return aesni_xor_block128(a, b); } -/** - * \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) +static __inline AesNI_Aes_Block __fastcall aesni_aes128_xor_blocks( + AesNI_Aes_Block a, + AesNI_Aes_Block b) { - assert(encryption_keys); - - aesni_aes128_expand_key_(key->key, encryption_keys); + return aesni_aes_xor_blocks(a, b); } -/** - * \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) +static __inline AesNI_Aes_Block __fastcall aesni_aes192_xor_blocks( + AesNI_Aes_Block a, + AesNI_Aes_Block b) { - assert(encryption_keys); - assert(decryption_keys); - - aesni_aes128_derive_decryption_keys_(encryption_keys, decryption_keys); + return aesni_aes_xor_blocks(a, b); } -/** - * \brief Encrypts a 128-bit block using AES-128 in ECB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_ecb( - AesNI_Aes_Block plaintext, - const AesNI_Aes128_RoundKeys* encryption_keys) +static __inline AesNI_Aes_Block __fastcall aesni_aes256_xor_blocks( + AesNI_Aes_Block a, + AesNI_Aes_Block b) { - assert(encryption_keys); - - return aesni_aes128_encrypt_block_(plaintext, encryption_keys); + return aesni_aes_xor_blocks(a, b); } -/** - * \brief Decrypts a 128-bit block using AES-128 in ECB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-128 decryption round keys. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_ecb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes128_RoundKeys* decryption_keys) +static __inline AesNI_Aes_Block __fastcall aesni_aes_inc_block( + AesNI_Aes_Block block) { - assert(decryption_keys); - - return aesni_aes128_decrypt_block_(ciphertext, decryption_keys); + block = aesni_reverse_byte_order_block128(block); + block = aesni_inc_block128(block); + return aesni_reverse_byte_order_block128(block); } -/** - * \brief Encrypts a 128-bit block using AES-128 in CBC mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_cbc( - AesNI_Aes_Block plaintext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) +static __inline AesNI_Aes_Block __fastcall aesni_aes128_inc_block( + AesNI_Aes_Block block) { - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_aes128_encrypt_block_(aesni_xor_block128(plaintext, init_vector), encryption_keys); - *next_init_vector = ciphertext; - return ciphertext; + return aesni_aes_inc_block(block); } -/** - * \brief Decrypts a 128-bit block using AES-128 in CBC mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-128 decryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_cbc( - AesNI_Aes_Block ciphertext, - const AesNI_Aes128_RoundKeys* decryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) +static __inline AesNI_Aes_Block __fastcall aesni_aes192_inc_block( + AesNI_Aes_Block block) { - assert(decryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes128_decrypt_block_(ciphertext, decryption_keys), init_vector); - *next_init_vector = ciphertext; - return plaintext; + return aesni_aes_inc_block(block); } -/** - * \brief Encrypts a 128-bit block using AES-128 in CFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_cfb( - AesNI_Aes_Block plaintext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) +static __inline AesNI_Aes_Block __fastcall aesni_aes256_inc_block( + AesNI_Aes_Block block) { - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(aesni_aes128_encrypt_block_(init_vector, encryption_keys), plaintext); - *next_init_vector = ciphertext; - return ciphertext; + return aesni_aes_inc_block(block); } -/** - * \brief Decrypts a 128-bit block using AES-128 in CFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-128 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_cfb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); +AESNI_ENCRYPT_BLOCK_ECB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_DECRYPT_BLOCK_ECB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_ENCRYPT_BLOCK_CBC(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_DECRYPT_BLOCK_CBC(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_ENCRYPT_BLOCK_CFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_DECRYPT_BLOCK_CFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_ENCRYPT_BLOCK_OFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_DECRYPT_BLOCK_OFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_ENCRYPT_BLOCK_CTR(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); +AESNI_DECRYPT_BLOCK_CTR(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys); - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes128_encrypt_block_(init_vector, encryption_keys), ciphertext); - *next_init_vector = ciphertext; - return plaintext; -} +AESNI_ENCRYPT_BLOCK_ECB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_DECRYPT_BLOCK_ECB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_ENCRYPT_BLOCK_CBC(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_DECRYPT_BLOCK_CBC(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_ENCRYPT_BLOCK_CFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_DECRYPT_BLOCK_CFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_ENCRYPT_BLOCK_OFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_DECRYPT_BLOCK_OFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_ENCRYPT_BLOCK_CTR(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); +AESNI_DECRYPT_BLOCK_CTR(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys); + +AESNI_ENCRYPT_BLOCK_ECB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_DECRYPT_BLOCK_ECB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_ENCRYPT_BLOCK_CBC(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_DECRYPT_BLOCK_CBC(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_ENCRYPT_BLOCK_CFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_DECRYPT_BLOCK_CFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_ENCRYPT_BLOCK_OFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_DECRYPT_BLOCK_OFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_ENCRYPT_BLOCK_CTR(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); +AESNI_DECRYPT_BLOCK_CTR(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys); /** - * \brief Encrypts a 128-bit block using AES-128 in OFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. + * \brief Expands an AES-128 key into 10 encryption round keys. * - * \return The encrypted 128-bit ciphertext. + * \param[in] key The AES-128 key. + * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_ofb( - AesNI_Aes_Block plaintext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) +static __inline void __fastcall aesni_aes128_expand_key( + const AesNI_Aes128_Key* key, + AesNI_Aes128_RoundKeys* encryption_keys) { assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block tmp = aesni_aes128_encrypt_block_(init_vector, encryption_keys); - *next_init_vector = tmp; - return aesni_xor_block128(tmp, plaintext); -} -/** - * \brief Decrypts a 128-bit block using AES-128 in OFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-128 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_ofb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes128_encrypt_block_ofb(ciphertext, encryption_keys, init_vector, next_init_vector); + aesni_aes128_expand_key_(key->key, encryption_keys); } /** - * \brief Encrypts a 128-bit block using AES-128 in CTR mode of operation. + * \brief Derives AES-128 decryption round keys from AES-128 encryption round keys. * - * \param[in] plaintext The plaintext to be encrypted. * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. + * \param[out] decryption_keys The AES-128 decryption round keys. Must not be `NULL`. */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_ctr( - AesNI_Aes_Block plaintext, +static __inline void __fastcall aesni_aes128_derive_decryption_keys( const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) + AesNI_Aes128_RoundKeys* decryption_keys) { assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(plaintext, aesni_aes128_encrypt_block_(init_vector, encryption_keys)); - *next_init_vector = aesni_aes_inc_counter(init_vector); - return ciphertext; -} + assert(decryption_keys); -/** - * \brief Decrypts a 128-bit block using AES-128 in CTR mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-128 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_ctr( - AesNI_Aes_Block ciphertext, - const AesNI_Aes128_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes128_encrypt_block_ctr(ciphertext, encryption_keys, init_vector, next_init_vector); + aesni_aes128_derive_decryption_keys_(encryption_keys, decryption_keys); } /** @@ -489,222 +354,6 @@ static __inline void __fastcall aesni_aes192_derive_decryption_keys( } /** - * \brief Encrypts a 128-bit block using AES-192 in ECB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_ecb( - AesNI_Aes_Block plaintext, - const AesNI_Aes192_RoundKeys* encryption_keys) -{ - assert(encryption_keys); - - return aesni_aes192_encrypt_block_(plaintext, encryption_keys); -} - -/** - * \brief Decrypts a 128-bit block using AES-192 in ECB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-192 decryption round keys. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_ecb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes192_RoundKeys* decryption_keys) -{ - assert(decryption_keys); - - return aesni_aes192_decrypt_block_(ciphertext, decryption_keys); -} - -/** - * \brief Encrypts a 128-bit block using AES-192 in CBC mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_cbc( - AesNI_Aes_Block plaintext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_aes192_encrypt_block_(aesni_xor_block128(plaintext, init_vector), encryption_keys); - *next_init_vector = ciphertext; - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-192 in CBC mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-192 decryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_cbc( - AesNI_Aes_Block ciphertext, - const AesNI_Aes192_RoundKeys* decryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(decryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes192_decrypt_block_(ciphertext, decryption_keys), init_vector); - *next_init_vector = ciphertext; - return plaintext; -} - -/** - * \brief Encrypts a 128-bit block using AES-192 in CFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_cfb( - AesNI_Aes_Block plaintext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(aesni_aes192_encrypt_block_(init_vector, encryption_keys), plaintext); - *next_init_vector = ciphertext; - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-192 in CFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-192 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_cfb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes192_encrypt_block_(init_vector, encryption_keys), ciphertext); - *next_init_vector = ciphertext; - return plaintext; -} - -/** - * \brief Encrypts a 128-bit block using AES-192 in OFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_ofb( - AesNI_Aes_Block plaintext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block tmp = aesni_aes192_encrypt_block_(init_vector, encryption_keys); - *next_init_vector = tmp; - return aesni_xor_block128(tmp, plaintext); -} - -/** - * \brief Decrypts a 128-bit block using AES-192 in OFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-192 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_ofb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes192_encrypt_block_ofb(ciphertext, encryption_keys, init_vector, next_init_vector); -} - -/** - * \brief Encrypts a 128-bit block using AES-192 in CTR mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_ctr( - AesNI_Aes_Block plaintext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(plaintext, aesni_aes192_encrypt_block_(init_vector, encryption_keys)); - *next_init_vector = aesni_aes_inc_counter(init_vector); - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-192 in CTR mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-192 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_ctr( - AesNI_Aes_Block ciphertext, - const AesNI_Aes192_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes192_encrypt_block_ctr(ciphertext, encryption_keys, init_vector, next_init_vector); -} - -/** * \brief Expands an AES-256 key into 14 encryption round keys. * * \param[in] key The AES-256 key. @@ -736,222 +385,6 @@ static __inline void __fastcall aesni_aes256_derive_decryption_keys( aesni_aes256_derive_decryption_keys_(encryption_keys, decryption_keys); } -/** - * \brief Encrypts a 128-bit block using AES-256 in ECB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_ecb( - AesNI_Aes_Block plaintext, - const AesNI_Aes256_RoundKeys* encryption_keys) -{ - assert(encryption_keys); - - return aesni_aes256_encrypt_block_(plaintext, encryption_keys); -} - -/** - * \brief Decrypts a 128-bit block using AES-256 in ECB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-256 decryption round keys. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_ecb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes256_RoundKeys* decryption_keys) -{ - assert(decryption_keys); - - return aesni_aes256_decrypt_block_(ciphertext, decryption_keys); -} - -/** - * \brief Encrypts a 128-bit block using AES-256 in CBC mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_cbc( - AesNI_Aes_Block plaintext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_aes256_encrypt_block_(aesni_xor_block128(plaintext, init_vector), encryption_keys); - *next_init_vector = ciphertext; - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-256 in CBC mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] decryption_keys The AES-256 decryption round keys. Must not be `NULL`. - * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_cbc( - AesNI_Aes_Block ciphertext, - const AesNI_Aes256_RoundKeys* decryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(decryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes256_decrypt_block_(ciphertext, decryption_keys), init_vector); - *next_init_vector = ciphertext; - return plaintext; -} - -/** - * \brief Encrypts a 128-bit block using AES-256 in CFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_cfb( - AesNI_Aes_Block plaintext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(aesni_aes256_encrypt_block_(init_vector, encryption_keys), plaintext); - *next_init_vector = ciphertext; - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-256 in CFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-256 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_cfb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block plaintext = aesni_xor_block128(aesni_aes256_encrypt_block_(init_vector, encryption_keys), ciphertext); - *next_init_vector = ciphertext; - return plaintext; -} - -/** - * \brief Encrypts a 128-bit block using AES-256 in OFB mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_ofb( - AesNI_Aes_Block plaintext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block tmp = aesni_aes256_encrypt_block_(init_vector, encryption_keys); - *next_init_vector = tmp; - return aesni_xor_block128(tmp, plaintext); -} - -/** - * \brief Decrypts a 128-bit block using AES-256 in OFB mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-256 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_ofb( - AesNI_Aes_Block ciphertext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes256_encrypt_block_ofb(ciphertext, encryption_keys, init_vector, next_init_vector); -} - -/** - * \brief Encrypts a 128-bit block using AES-256 in CTR mode of operation. - * - * \param[in] plaintext The plaintext to be encrypted. - * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The encrypted 128-bit ciphertext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_ctr( - AesNI_Aes_Block plaintext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - assert(encryption_keys); - assert(next_init_vector); - - AesNI_Aes_Block ciphertext = aesni_xor_block128(plaintext, aesni_aes256_encrypt_block_(init_vector, encryption_keys)); - *next_init_vector = aesni_aes_inc_counter(init_vector); - return ciphertext; -} - -/** - * \brief Decrypts a 128-bit block using AES-256 in CTR mode of operation. - * - * \param[in] ciphertext The ciphertext to be decrypted. - * \param[in] encryption_keys The AES-256 **encryption** round keys. Must not be `NULL`. - * \param[in] init_vector The CTR initialization vector. - * \param[out] next_init_vector The initialization vector to be used for the next call. Must not be `NULL`. - * - * \return The decrypted 128-bit plaintext. - */ -static __inline AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_ctr( - AesNI_Aes_Block ciphertext, - const AesNI_Aes256_RoundKeys* encryption_keys, - AesNI_Aes_Block init_vector, - AesNI_Aes_Block* next_init_vector) -{ - return aesni_aes256_encrypt_block_ctr(ciphertext, encryption_keys, init_vector, next_init_vector); -} - #ifdef __cplusplus } #endif diff --git a/include/aesni/mode.h b/include/aesni/mode.h index fc00e9c..122b842 100644 --- a/include/aesni/mode.h +++ b/include/aesni/mode.h @@ -8,6 +8,8 @@ #pragma once +#include <assert.h> + typedef enum { AESNI_ECB, @@ -17,3 +19,136 @@ typedef enum AESNI_CTR, } AesNI_Mode; + +#define AESNI_ENCRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ecb( \ + BlockT plaintext, \ + const KeyT* key) \ +{ \ + assert(key); \ +\ + return aesni_## prefix ##_encrypt_block_(plaintext, key); \ +} + +#define AESNI_DECRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ecb( \ + BlockT ciphertext, \ + const KeyT* key) \ +{ \ + assert(key); \ +\ + return aesni_## prefix ##_decrypt_block_(ciphertext, key); \ +} + +#define AESNI_ENCRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cbc( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_encrypt_block_ecb( \ + aesni_## prefix ##_xor_blocks(plaintext, init_vector), key); \ +} + +#define AESNI_DECRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cbc( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_decrypt_block_ecb(ciphertext, key), init_vector); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cfb( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_ecb(init_vector, key), plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cfb( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_ecb(init_vector, key), ciphertext); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ofb( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT tmp = aesni_## prefix ##_encrypt_block_ecb(init_vector, key); \ + *next_init_vector = tmp; \ + return aesni_## prefix ##_xor_blocks(tmp, plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ofb( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + return aesni_## prefix ##_encrypt_block_ofb( \ + ciphertext, key, init_vector, next_init_vector); \ +} + +#define AESNI_ENCRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ctr( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT ciphertext = aesni_## prefix ##_xor_blocks( \ + plaintext, aesni_## prefix ##_encrypt_block_ecb(init_vector, key)); \ + *next_init_vector = aesni_## prefix ##_inc_block(init_vector); \ + return ciphertext; \ +} + +#define AESNI_DECRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ctr( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + return aesni_## prefix ##_encrypt_block_ctr( \ + ciphertext, key, init_vector, next_init_vector); \ +} |