From 21b736e980709dad61c1412e22ec26265bfe4f0e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 Jun 2015 03:18:34 +0300 Subject: add CTR mode --- include/aesni/api.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'include/aesni') diff --git a/include/aesni/api.h b/include/aesni/api.h index fff3672..3318aef 100644 --- a/include/aesni/api.h +++ b/include/aesni/api.h @@ -105,6 +105,30 @@ static __inline AesBlock128 __fastcall aes128ofb_decrypt( return _mm_xor_si128(tmp, cypher); } +static __inline AesBlock128 __fastcall aes128ctr_encrypt( + AesBlock128 plain, + Aes128KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(plain, raw_aes128ecb_encrypt(init_vector, key_schedule)); +} + +static __inline AesBlock128 __fastcall aes128ctr_decrypt( + AesBlock128 cypher, + Aes128KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(cypher, raw_aes128ecb_encrypt(init_vector, key_schedule)); +} + static __inline void __fastcall aes192_expand_key_schedule( AesBlock192* key, Aes192KeySchedule* key_schedule) @@ -199,6 +223,30 @@ static __inline AesBlock128 __fastcall aes192ofb_decrypt( return _mm_xor_si128(tmp, cypher); } +static __inline AesBlock128 __fastcall aes192ctr_encrypt( + AesBlock128 plain, + Aes192KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(plain, raw_aes192ecb_encrypt(init_vector, key_schedule)); +} + +static __inline AesBlock128 __fastcall aes192ctr_decrypt( + AesBlock128 cypher, + Aes192KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(cypher, raw_aes192ecb_encrypt(init_vector, key_schedule)); +} + static __inline void __fastcall aes256_expand_key_schedule( AesBlock256* key, Aes256KeySchedule* key_schedule) @@ -292,3 +340,27 @@ static __inline AesBlock128 __fastcall aes256ofb_decrypt( _mm_storeu_si128(next_init_vector, tmp); return _mm_xor_si128(tmp, cypher); } + +static __inline AesBlock128 __fastcall aes256ctr_encrypt( + AesBlock128 plain, + Aes256KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(plain, raw_aes256ecb_encrypt(init_vector, key_schedule)); +} + +static __inline AesBlock128 __fastcall aes256ctr_decrypt( + AesBlock128 cypher, + Aes256KeySchedule* key_schedule, + AesBlock128 init_vector, + int counter) +{ + init_vector = aes128_le2be(init_vector); + init_vector = _mm_add_epi32(init_vector, make_aes_block128(0, 0, 0, counter)); + init_vector = aes128_be2le(init_vector); + return _mm_xor_si128(cypher, raw_aes256ecb_encrypt(init_vector, key_schedule)); +} -- cgit v1.2.3