From 3baadcabe01da1148f0738f260cbba1d7389ea86 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 17 Jun 2015 16:37:34 +0300 Subject: factor out 128-bit XORing --- include/aesni/block.h | 48 ++++++++++++++++++++++++------------------------ include/aesni/data.h | 31 +++++++++++++++++++++++++------ src/box.c | 2 +- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/include/aesni/block.h b/include/aesni/block.h index 47697af..208e9d4 100644 --- a/include/aesni/block.h +++ b/include/aesni/block.h @@ -147,7 +147,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc128( assert(next_init_vector); AesNI_Block128 cipher = aesni_raw_encrypt_block128( - _mm_xor_si128(plain, init_vector), + aesni_xor_block128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -173,7 +173,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc128( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_decrypt_block128(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; @@ -200,7 +200,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb128( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128( + AesNI_Block128 cipher = aesni_xor_block128( aesni_raw_encrypt_block128(init_vector, key_schedule), plain); *next_init_vector = cipher; @@ -227,7 +227,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb128( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_encrypt_block128(init_vector, key_schedule), cipher); *next_init_vector = cipher; @@ -256,7 +256,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb128( AesNI_Block128 tmp = aesni_raw_encrypt_block128(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, plain); + return aesni_xor_block128(tmp, plain); } /** @@ -281,7 +281,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb128( AesNI_Block128 tmp = aesni_raw_encrypt_block128(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, cipher); + return aesni_xor_block128(tmp, cipher); } /** @@ -307,7 +307,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr128( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( plain, aesni_raw_encrypt_block128(init_vector, key_schedule)); } @@ -335,7 +335,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr128( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( cipher, aesni_raw_encrypt_block128(init_vector, key_schedule)); } @@ -437,7 +437,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc192( assert(next_init_vector); AesNI_Block128 cipher = aesni_raw_encrypt_block192( - _mm_xor_si128(plain, init_vector), + aesni_xor_block128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -463,7 +463,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc192( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_decrypt_block192(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; @@ -490,7 +490,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb192( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128( + AesNI_Block128 cipher = aesni_xor_block128( aesni_raw_encrypt_block192(init_vector, key_schedule), plain); *next_init_vector = cipher; @@ -517,7 +517,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb192( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_encrypt_block192(init_vector, key_schedule), cipher); *next_init_vector = cipher; @@ -546,7 +546,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb192( AesNI_Block128 tmp = aesni_raw_encrypt_block192(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, plain); + return aesni_xor_block128(tmp, plain); } /** @@ -571,7 +571,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb192( AesNI_Block128 tmp = aesni_raw_encrypt_block192(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, cipher); + return aesni_xor_block128(tmp, cipher); } /** @@ -597,7 +597,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr192( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( plain, aesni_raw_encrypt_block192(init_vector, key_schedule)); } @@ -625,7 +625,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr192( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( cipher, aesni_raw_encrypt_block192(init_vector, key_schedule)); } @@ -727,7 +727,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc256( assert(next_init_vector); AesNI_Block128 cipher = aesni_raw_encrypt_block256( - _mm_xor_si128(plain, init_vector), + aesni_xor_block128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -753,7 +753,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc256( assert(inverted_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_decrypt_block256(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; @@ -780,7 +780,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb256( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 cipher = _mm_xor_si128( + AesNI_Block128 cipher = aesni_xor_block128( aesni_raw_encrypt_block256(init_vector, key_schedule), plain); *next_init_vector = cipher; @@ -807,7 +807,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb256( assert(key_schedule); assert(next_init_vector); - AesNI_Block128 plain = _mm_xor_si128( + AesNI_Block128 plain = aesni_xor_block128( aesni_raw_encrypt_block256(init_vector, key_schedule), cipher); *next_init_vector = cipher; @@ -836,7 +836,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb256( AesNI_Block128 tmp = aesni_raw_encrypt_block256(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, plain); + return aesni_xor_block128(tmp, plain); } /** @@ -861,7 +861,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb256( AesNI_Block128 tmp = aesni_raw_encrypt_block256(init_vector, key_schedule); *next_init_vector = tmp; - return _mm_xor_si128(tmp, cipher); + return aesni_xor_block128(tmp, cipher); } /** @@ -887,7 +887,7 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr256( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( plain, aesni_raw_encrypt_block256(init_vector, key_schedule)); } @@ -915,7 +915,7 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr256( aesni_le2be128(init_vector), aesni_make_block128(0, 0, 0, counter))); - return _mm_xor_si128( + return aesni_xor_block128( cipher, aesni_raw_encrypt_block256(init_vector, key_schedule)); } diff --git a/include/aesni/data.h b/include/aesni/data.h index 6f4da76..fbe6e31 100644 --- a/include/aesni/data.h +++ b/include/aesni/data.h @@ -37,6 +37,7 @@ 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) @@ -47,8 +48,8 @@ static __inline AesNI_Block128 aesni_load_block128(const void* 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`. + * \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) @@ -60,10 +61,12 @@ static __inline AesNI_Block128 aesni_load_block128_aligned(const void* 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) + void* dest, + AesNI_Block128 block) { _mm_storeu_si128((AesNI_Block128*) dest, block); } @@ -71,16 +74,32 @@ static __inline void __fastcall aesni_store_block128( /** * \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[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) + 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. * diff --git a/src/box.c b/src/box.c index 01f3b20..61f4439 100644 --- a/src/box.c +++ b/src/box.c @@ -13,7 +13,7 @@ static AesNI_StatusCode aesni_box_xor_state_aes( const AesNI_State* src, AesNI_ErrorDetails* err_details) { - dest->aes_block = _mm_xor_si128(dest->aes_block, src->aes_block); + dest->aes_block = aesni_xor_block128(dest->aes_block, src->aes_block); return AESNI_SUCCESS; } -- cgit v1.2.3