aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-06-04 03:18:34 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-06-04 03:18:34 +0300
commit21b736e980709dad61c1412e22ec26265bfe4f0e (patch)
tree0a29fafff6a9b6c5a31d39c39480dca5084bbcb0 /include
parentadd BE-to-LE conversion functions (diff)
downloadaes-tools-21b736e980709dad61c1412e22ec26265bfe4f0e.tar.gz
aes-tools-21b736e980709dad61c1412e22ec26265bfe4f0e.zip
add CTR mode
Diffstat (limited to 'include')
-rw-r--r--include/aesni/api.h72
1 files changed, 72 insertions, 0 deletions
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));
+}