From 2d67b793e64f6ed4e2e6a7c56441b9bc019e2659 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 15 Jun 2015 02:55:10 +0300 Subject: code style --- include/aesni/block.h | 310 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 208 insertions(+), 102 deletions(-) (limited to 'include/aesni/block.h') diff --git a/include/aesni/block.h b/include/aesni/block.h index a2a4b1b..47697af 100644 --- a/include/aesni/block.h +++ b/include/aesni/block.h @@ -4,6 +4,7 @@ * \date 2015 * \copyright This file is licensed under the terms of the MIT License. * See LICENSE.txt for details. + * * \brief Declares 128-bit block encryption/decryption functions. */ @@ -61,7 +62,8 @@ extern "C" * \brief Expands a key schedule for AES-128 encryption. * * \param[in] key The AES-128 key. - * \param[out] key_schedule The AES-128 encryption key schedule. Must not be `NULL`. + * \param[out] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. */ static __inline void __fastcall aesni_expand_key_schedule128( AesNI_Block128 key, @@ -73,10 +75,12 @@ static __inline void __fastcall aesni_expand_key_schedule128( } /** - * \brief "Reverses" a key schedule for AES-128 "equivalent inverse cipher" decryption. + * \brief "Inverts" an AES-128 key schedule to prepare for decryption. * - * \param[in] key_schedule The AES-128 encryption key schedule. Must not be `NULL`. - * \param[out] inverted_schedule The AES-128 decryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. + * \param[out] inverted_schedule The AES-128 decryption key schedule. Must not + * be `NULL`. */ static __inline void __fastcall aesni_invert_key_schedule128( AesNI_KeySchedule128* key_schedule, @@ -92,7 +96,8 @@ static __inline void __fastcall aesni_invert_key_schedule128( * \brief Encrypts a 128-bit block using AES-128 in ECB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-128 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb128( @@ -108,7 +113,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb128( * \brief Decrypts a 128-bit block using AES-128 in ECB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-128 decryption ("reversed") key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-128 decryption key schedule. Must not + * be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb128( @@ -124,9 +130,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb128( * \brief Encrypts a 128-bit block using AES-128 in CBC mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-128 encryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc128( @@ -138,7 +146,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc128( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = aesni_raw_encrypt_block128(_mm_xor_si128(plain, init_vector), key_schedule); + AesNI_Block128 cipher = aesni_raw_encrypt_block128( + _mm_xor_si128(plain, init_vector), + key_schedule); *next_init_vector = cipher; return cipher; } @@ -147,9 +157,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc128( * \brief Decrypts a 128-bit block using AES-128 in CBC mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-128 decryption (reversed) key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-128 decryption key schedule. Must not + * be `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc128( @@ -161,7 +173,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc128( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block128(cipher, inverted_schedule), init_vector); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_decrypt_block128(cipher, inverted_schedule), + init_vector); *next_init_vector = cipher; return plain; } @@ -170,9 +184,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc128( * \brief Encrypts a 128-bit block using AES-128 in CFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-128 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb128( @@ -184,7 +200,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb128( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block128(init_vector, key_schedule), plain); + AesNI_Block128 cipher = _mm_xor_si128( + aesni_raw_encrypt_block128(init_vector, key_schedule), + plain); *next_init_vector = cipher; return cipher; } @@ -193,9 +211,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb128( * \brief Decrypts a 128-bit block using AES-128 in CFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb128( @@ -207,7 +227,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb128( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block128(init_vector, key_schedule), cipher); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_encrypt_block128(init_vector, key_schedule), + cipher); *next_init_vector = cipher; return plain; } @@ -216,9 +238,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb128( * \brief Encrypts a 128-bit block using AES-128 in OFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-128 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb128( @@ -239,9 +263,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb128( * \brief Decrypts a 128-bit block using AES-128 in OFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb128( @@ -262,9 +288,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb128( * \brief Encrypts a 128-bit block using AES-128 in CTR mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-128 encryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr128( @@ -275,19 +303,24 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr128( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(plain, aesni_raw_encrypt_block128(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + plain, + aesni_raw_encrypt_block128(init_vector, key_schedule)); } /** * \brief Decrypts a 128-bit block using AES-128 in CTR mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-128 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr128( @@ -298,10 +331,13 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr128( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(cipher, aesni_raw_encrypt_block128(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + cipher, + aesni_raw_encrypt_block128(init_vector, key_schedule)); } /** @@ -315,7 +351,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr128( * \brief Expands a key schedule for AES-192 encryption. * * \param[in] key The AES-192 key. Must not be `NULL`. - * \param[out] key_schedule The AES-192 encryption key schedule. Must not be `NULL`. + * \param[out] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. */ static __inline void __fastcall aesni_expand_key_schedule192( AesNI_Block192* key, @@ -328,10 +365,12 @@ static __inline void __fastcall aesni_expand_key_schedule192( } /** - * \brief "Reverses" a key schedule for AES-192 "equivalent inverse cipher" decryption. + * \brief "Inverts" an AES-192 key schedule to prepare for decryption. * - * \param[in] key_schedule The AES-192 encryption key schedule. Must not be `NULL`. - * \param[out] inverted_schedule The AES-192 decryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. + * \param[out] inverted_schedule The AES-192 decryption key schedule. Must not + * be `NULL`. */ static __inline void __fastcall aesni_invert_key_schedule192( AesNI_KeySchedule192* key_schedule, @@ -347,7 +386,8 @@ static __inline void __fastcall aesni_invert_key_schedule192( * \brief Encrypts a 128-bit block using AES-192 in ECB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-192 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb192( @@ -363,7 +403,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb192( * \brief Decrypts a 128-bit block using AES-192 in ECB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-192 decryption (reversed) key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-192 decryption key schedule. Must not + * be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb192( @@ -379,9 +420,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb192( * \brief Encrypts a 128-bit block using AES-192 in CBC mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-192 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc192( @@ -393,7 +436,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc192( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = aesni_raw_encrypt_block192(_mm_xor_si128(plain, init_vector), key_schedule); + AesNI_Block128 cipher = aesni_raw_encrypt_block192( + _mm_xor_si128(plain, init_vector), + key_schedule); *next_init_vector = cipher; return cipher; } @@ -402,9 +447,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc192( * \brief Decrypts a 128-bit block using AES-192 in CBC mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-192 decryption (reversed) key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-192 decryption key schedule. Must not + * be `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc192( @@ -416,7 +463,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc192( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block192(cipher, inverted_schedule), init_vector); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_decrypt_block192(cipher, inverted_schedule), + init_vector); *next_init_vector = cipher; return plain; } @@ -425,9 +474,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc192( * \brief Encrypts a 128-bit block using AES-192 in CFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-192 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb192( @@ -439,7 +490,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb192( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block192(init_vector, key_schedule), plain); + AesNI_Block128 cipher = _mm_xor_si128( + aesni_raw_encrypt_block192(init_vector, key_schedule), + plain); *next_init_vector = cipher; return cipher; } @@ -448,9 +501,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb192( * \brief Decrypts a 128-bit block using AES-192 in CFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb192( @@ -462,7 +517,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb192( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block192(init_vector, key_schedule), cipher); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_encrypt_block192(init_vector, key_schedule), + cipher); *next_init_vector = cipher; return plain; } @@ -471,9 +528,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb192( * \brief Encrypts a 128-bit block using AES-192 in OFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-192 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb192( @@ -494,9 +553,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb192( * \brief Decrypts a 128-bit block using AES-192 in OFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb192( @@ -517,9 +578,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb192( * \brief Encrypts a 128-bit block using AES-192 in CTR mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-192 encryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr192( @@ -530,19 +593,24 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr192( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(plain, aesni_raw_encrypt_block192(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + plain, + aesni_raw_encrypt_block192(init_vector, key_schedule)); } /** * \brief Decrypts a 128-bit block using AES-192 in CTR mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-192 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr192( @@ -553,10 +621,13 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr192( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(cipher, aesni_raw_encrypt_block192(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + cipher, + aesni_raw_encrypt_block192(init_vector, key_schedule)); } /** @@ -570,7 +641,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr192( * \brief Expands a key schedule for AES-256 encryption. * * \param[in] key The AES-256 key. Must not be `NULL`. - * \param[out] key_schedule The AES-256 encryption key schedule. Must not be `NULL`. + * \param[out] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. */ static __inline void __fastcall aesni_expand_key_schedule256( AesNI_Block256* key, @@ -583,10 +655,12 @@ static __inline void __fastcall aesni_expand_key_schedule256( } /** - * \brief "Reverses" a key schedule for AES-256 "equivalent inverse cipher" decryption. + * \brief "Inverts" an AES-256 key schedule to prepare for decryption. * - * \param[in] key_schedule The AES-256 encryption key schedule. Must not be `NULL`. - * \param[out] inverted_schedule The AES-256 decryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. + * \param[out] inverted_schedule The AES-256 decryption key schedule. Must not + * be `NULL`. */ static __inline void __fastcall aesni_invert_key_schedule256( AesNI_KeySchedule256* key_schedule, @@ -602,7 +676,8 @@ static __inline void __fastcall aesni_invert_key_schedule256( * \brief Encrypts a 128-bit block using AES-256 in ECB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-256 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb256( @@ -618,7 +693,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb256( * \brief Decrypts a 128-bit block using AES-256 in ECB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-256 decryption (reversed) key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-256 decryption key schedule. Must not + * be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb256( @@ -634,9 +710,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb256( * \brief Encrypts a 128-bit block using AES-256 in CBC mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-256 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc256( @@ -648,7 +726,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc256( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = aesni_raw_encrypt_block256(_mm_xor_si128(plain, init_vector), key_schedule); + AesNI_Block128 cipher = aesni_raw_encrypt_block256( + _mm_xor_si128(plain, init_vector), + key_schedule); *next_init_vector = cipher; return cipher; } @@ -657,9 +737,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc256( * \brief Decrypts a 128-bit block using AES-256 in CBC mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] inverted_schedule The AES-256 decryption (reversed) key schedule. Must not be `NULL`. + * \param[in] inverted_schedule The AES-256 decryption key schedule. Must not + * be `NULL`. * \param[in] init_vector The CBC initialization vector. - * \param[out] next_init_vector The next CBC initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CBC initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc256( @@ -671,7 +753,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc256( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block256(cipher, inverted_schedule), init_vector); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_decrypt_block256(cipher, inverted_schedule), + init_vector); *next_init_vector = cipher; return plain; } @@ -680,9 +764,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc256( * \brief Encrypts a 128-bit block using AES-256 in CFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-256 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb256( @@ -694,7 +780,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb256( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block256(init_vector, key_schedule), plain); + AesNI_Block128 cipher = _mm_xor_si128( + aesni_raw_encrypt_block256(init_vector, key_schedule), + plain); *next_init_vector = cipher; return cipher; } @@ -703,9 +791,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb256( * \brief Decrypts a 128-bit block using AES-256 in CFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CFB initialization vector. - * \param[out] next_init_vector The next CFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next CFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb256( @@ -717,7 +807,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb256( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block256(init_vector, key_schedule), cipher); + AesNI_Block128 plain = _mm_xor_si128( + aesni_raw_encrypt_block256(init_vector, key_schedule), + cipher); *next_init_vector = cipher; return plain; } @@ -726,9 +818,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb256( * \brief Encrypts a 128-bit block using AES-256 in OFB mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-256 encryption schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb256( @@ -749,9 +843,11 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb256( * \brief Decrypts a 128-bit block using AES-256 in OFB mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The OFB initialization vector. - * \param[out] next_init_vector The next OFB initialization vector to be used as the initialization vector for the next call. Must not be `NULL`. + * \param[out] next_init_vector The next OFB initialization vector to be used + * as the initialization vector for the next call. Must not be `NULL`. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb256( @@ -772,9 +868,11 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb256( * \brief Encrypts a 128-bit block using AES-256 in CTR mode of operation. * * \param[in] plain The plaintext to be encrypted. - * \param[in] key_schedule The AES-256 encryption key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 encryption key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The encrypted 128-bit ciphertext. */ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr256( @@ -785,19 +883,24 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr256( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(plain, aesni_raw_encrypt_block256(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + plain, + aesni_raw_encrypt_block256(init_vector, key_schedule)); } /** * \brief Decrypts a 128-bit block using AES-256 in CTR mode of operation. * * \param[in] cipher The ciphertext to be decrypted. - * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be `NULL`. + * \param[in] key_schedule The AES-256 **encryption** key schedule. Must not be + * `NULL`. * \param[in] init_vector The CTR initialization vector. - * \param[in] counter The counter, typically incremented between consecutive calls. + * \param[in] counter The counter, typically incremented between consecutive + * calls. * \return The decrypted 128-bit plaintext. */ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr256( @@ -808,10 +911,13 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr256( { assert(key_schedule); - init_vector = aesni_le2be128(init_vector); - init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); - init_vector = aesni_be2le128(init_vector); - return _mm_xor_si128(cipher, aesni_raw_encrypt_block256(init_vector, key_schedule)); + init_vector = aesni_be2le128(_mm_add_epi32( + aesni_le2be128(init_vector), + aesni_make_block128(0, 0, 0, counter))); + + return _mm_xor_si128( + cipher, + aesni_raw_encrypt_block256(init_vector, key_schedule)); } /** -- cgit v1.2.3