From 91923133903515c98973543c0e17705537449c22 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 3 Jun 2015 22:46:18 +0300 Subject: init vectors are calculated by API functions --- examples/aes128cbc_example.c | 14 ++++- examples/aes128cfb_example.c | 14 ++++- examples/aes128ofb_example.c | 14 ++++- examples/aes192cbc_example.c | 14 ++++- examples/aes192cfb_example.c | 14 ++++- examples/aes192ofb_example.c | 14 ++++- examples/aes256cbc_example.c | 14 ++++- examples/aes256cfb_example.c | 14 ++++- examples/aes256ofb_example.c | 14 ++++- include/aesni/api.h | 126 +++++++++++++++++++++++++++++------------ test/aes128cbc_decrypt_block.c | 3 +- test/aes128cbc_encrypt_block.c | 2 +- test/aes128cfb_decrypt_block.c | 3 +- test/aes128cfb_encrypt_block.c | 2 +- test/aes128ofb_decrypt_block.c | 3 +- test/aes128ofb_encrypt_block.c | 3 +- test/aes192cbc_decrypt_block.c | 3 +- test/aes192cbc_encrypt_block.c | 2 +- test/aes192cfb_decrypt_block.c | 3 +- test/aes192cfb_encrypt_block.c | 2 +- test/aes192ofb_decrypt_block.c | 3 +- test/aes192ofb_encrypt_block.c | 3 +- test/aes256cbc_decrypt_block.c | 3 +- test/aes256cbc_encrypt_block.c | 2 +- test/aes256cfb_decrypt_block.c | 3 +- test/aes256cfb_encrypt_block.c | 2 +- test/aes256ofb_decrypt_block.c | 3 +- test/aes256ofb_encrypt_block.c | 3 +- 28 files changed, 207 insertions(+), 93 deletions(-) diff --git a/examples/aes128cbc_example.c b/examples/aes128cbc_example.c index 53156c4..44df122 100644 --- a/examples/aes128cbc_example.c +++ b/examples/aes128cbc_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv, next_iv; __declspec(align(16)) Aes128KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); @@ -37,11 +37,15 @@ int main() for (int i = 0; i < 11; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes128cbc_encrypt(plain, &key_schedule, iv); + cypher = aes128cbc_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + aes128_invert_key_schedule(&key_schedule, &inverted_schedule); printf("\n"); @@ -49,10 +53,14 @@ int main() for (int i = 0; i < 11; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); - decrypted = aes128cbc_decrypt(cypher, &inverted_schedule, iv); + decrypted = aes128cbc_decrypt(cypher, &inverted_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes128cfb_example.c b/examples/aes128cfb_example.c index c466c11..9d63a9a 100644 --- a/examples/aes128cfb_example.c +++ b/examples/aes128cfb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv, next_iv; __declspec(align(16)) Aes128KeySchedule key_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); @@ -37,15 +37,23 @@ int main() for (int i = 0; i < 11; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes128cfb_encrypt(plain, &key_schedule, iv); + cypher = aes128cfb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes128cfb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes128cfb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes128ofb_example.c b/examples/aes128ofb_example.c index 07137f6..7685008 100644 --- a/examples/aes128ofb_example.c +++ b/examples/aes128ofb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv, next_iv; __declspec(align(16)) Aes128KeySchedule key_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); @@ -37,15 +37,23 @@ int main() for (int i = 0; i < 11; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes128ofb_encrypt(plain, &key_schedule, iv); + cypher = aes128ofb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes128ofb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes128ofb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes192cbc_example.c b/examples/aes192cbc_example.c index dc6d478..2133a17 100644 --- a/examples/aes192cbc_example.c +++ b/examples/aes192cbc_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock192 key; __declspec(align(16)) Aes192KeySchedule key_schedule, inverted_schedule; @@ -38,11 +38,15 @@ int main() for (int i = 0; i < 13; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes192cbc_encrypt(plain, &key_schedule, iv); + cypher = aes192cbc_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + aes192_invert_key_schedule(&key_schedule, &inverted_schedule); printf("\n"); @@ -50,10 +54,14 @@ int main() for (int i = 0; i < 13; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); - decrypted = aes192cbc_decrypt(cypher, &inverted_schedule, iv); + decrypted = aes192cbc_decrypt(cypher, &inverted_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes192cfb_example.c b/examples/aes192cfb_example.c index fe6340b..d3ac8bc 100644 --- a/examples/aes192cfb_example.c +++ b/examples/aes192cfb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock192 key; __declspec(align(16)) Aes192KeySchedule key_schedule; @@ -38,15 +38,23 @@ int main() for (int i = 0; i < 13; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes192cfb_encrypt(plain, &key_schedule, iv); + cypher = aes192cfb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes192cfb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes192cfb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes192ofb_example.c b/examples/aes192ofb_example.c index 6e611d0..1a50dfb 100644 --- a/examples/aes192ofb_example.c +++ b/examples/aes192ofb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock192 key; __declspec(align(16)) Aes192KeySchedule key_schedule; @@ -38,15 +38,23 @@ int main() for (int i = 0; i < 13; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes192ofb_encrypt(plain, &key_schedule, iv); + cypher = aes192ofb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes192ofb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes192ofb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes256cbc_example.c b/examples/aes256cbc_example.c index 423cd5d..21ae58d 100644 --- a/examples/aes256cbc_example.c +++ b/examples/aes256cbc_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock256 key; __declspec(align(16)) Aes256KeySchedule key_schedule, inverted_schedule; @@ -38,11 +38,15 @@ int main() for (int i = 0; i < 15; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes256cbc_encrypt(plain, &key_schedule, iv); + cypher = aes256cbc_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + aes256_invert_key_schedule(&key_schedule, &inverted_schedule); printf("\n"); @@ -50,10 +54,14 @@ int main() for (int i = 0; i < 15; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); - decrypted = aes256cbc_decrypt(cypher, &inverted_schedule, iv); + decrypted = aes256cbc_decrypt(cypher, &inverted_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes256cfb_example.c b/examples/aes256cfb_example.c index 596f293..35dd21f 100644 --- a/examples/aes256cfb_example.c +++ b/examples/aes256cfb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock256 key; __declspec(align(16)) Aes256KeySchedule key_schedule; @@ -38,15 +38,23 @@ int main() for (int i = 0; i < 15; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes256cfb_encrypt(plain, &key_schedule, iv); + cypher = aes256cfb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes256cfb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes256cfb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/examples/aes256ofb_example.c b/examples/aes256ofb_example.c index 3481c0c..749103e 100644 --- a/examples/aes256ofb_example.c +++ b/examples/aes256ofb_example.c @@ -12,7 +12,7 @@ int main() { - __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; + __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv, next_iv; __declspec(align(16)) AesBlock256 key; __declspec(align(16)) Aes256KeySchedule key_schedule; @@ -38,15 +38,23 @@ int main() for (int i = 0; i < 15; ++i) printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); - cypher = aes256ofb_encrypt(plain, &key_schedule, iv); + cypher = aes256ofb_encrypt(plain, &key_schedule, iv, &next_iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes256ofb_decrypt(cypher, &key_schedule, iv); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + + decrypted = aes256ofb_decrypt(cypher, &key_schedule, iv, &next_iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); print_aes_block128_fips_matrix_style(&decrypted); + printf("\n"); + printf("Next initialization vector: %s\n", format_aes_block128(&next_iv).str); + print_aes_block128_fips_matrix_style(&next_iv); + return 0; } diff --git a/include/aesni/api.h b/include/aesni/api.h index 6d275a5..fff3672 100644 --- a/include/aesni/api.h +++ b/include/aesni/api.h @@ -42,49 +42,67 @@ static __inline AesBlock128 __fastcall aes128ecb_decrypt( static __inline AesBlock128 __fastcall aes128cbc_encrypt( AesBlock128 plain, Aes128KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return raw_aes128ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + AesBlock128 cypher = raw_aes128ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes128cbc_decrypt( AesBlock128 cypher, Aes128KeySchedule* inverted_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes128ecb_decrypt(cypher, inverted_schedule), init_vector); + AesBlock128 plain = _mm_xor_si128(raw_aes128ecb_decrypt(cypher, inverted_schedule), init_vector); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes128cfb_encrypt( AesBlock128 plain, Aes128KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 cypher = _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), plain); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes128cfb_decrypt( AesBlock128 cypher, Aes128KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 plain = _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), cypher); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes128ofb_encrypt( AesBlock128 plain, Aes128KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 tmp = raw_aes128ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, plain); } static __inline AesBlock128 __fastcall aes128ofb_decrypt( AesBlock128 cypher, Aes128KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes128ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 tmp = raw_aes128ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, cypher); } static __inline void __fastcall aes192_expand_key_schedule( @@ -118,49 +136,67 @@ static __inline AesBlock128 __fastcall aes192ecb_decrypt( static __inline AesBlock128 __fastcall aes192cbc_encrypt( AesBlock128 plain, Aes192KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return raw_aes192ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + AesBlock128 cypher = raw_aes192ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes192cbc_decrypt( AesBlock128 cypher, Aes192KeySchedule* inverted_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes192ecb_decrypt(cypher, inverted_schedule), init_vector); + AesBlock128 plain = _mm_xor_si128(raw_aes192ecb_decrypt(cypher, inverted_schedule), init_vector); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes192cfb_encrypt( AesBlock128 plain, Aes192KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 cypher = _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), plain); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes192cfb_decrypt( AesBlock128 cypher, Aes192KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 plain = _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), cypher); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes192ofb_encrypt( AesBlock128 plain, Aes192KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 tmp = raw_aes192ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, plain); } static __inline AesBlock128 __fastcall aes192ofb_decrypt( AesBlock128 cypher, Aes192KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes192ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 tmp = raw_aes192ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, cypher); } static __inline void __fastcall aes256_expand_key_schedule( @@ -194,47 +230,65 @@ static __inline AesBlock128 __fastcall aes256ecb_decrypt( static __inline AesBlock128 __fastcall aes256cbc_encrypt( AesBlock128 plain, Aes256KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return raw_aes256ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + AesBlock128 cypher = raw_aes256ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes256cbc_decrypt( AesBlock128 cypher, Aes256KeySchedule* inverted_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes256ecb_decrypt(cypher, inverted_schedule), init_vector); + AesBlock128 plain = _mm_xor_si128(raw_aes256ecb_decrypt(cypher, inverted_schedule), init_vector); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes256cfb_encrypt( AesBlock128 plain, Aes256KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 cypher = _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), plain); + _mm_storeu_si128(next_init_vector, cypher); + return cypher; } static __inline AesBlock128 __fastcall aes256cfb_decrypt( AesBlock128 cypher, Aes256KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 plain = _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), cypher); + _mm_storeu_si128(next_init_vector, cypher); + return plain; } static __inline AesBlock128 __fastcall aes256ofb_encrypt( AesBlock128 plain, Aes256KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), plain); + AesBlock128 tmp = raw_aes256ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, plain); } static __inline AesBlock128 __fastcall aes256ofb_decrypt( AesBlock128 cypher, Aes256KeySchedule* key_schedule, - AesBlock128 init_vector) + AesBlock128 init_vector, + AesBlock128* next_init_vector) { - return _mm_xor_si128(raw_aes256ecb_encrypt(init_vector, key_schedule), cypher); + AesBlock128 tmp = raw_aes256ecb_encrypt(init_vector, key_schedule); + _mm_storeu_si128(next_init_vector, tmp); + return _mm_xor_si128(tmp, cypher); } diff --git a/test/aes128cbc_decrypt_block.c b/test/aes128cbc_decrypt_block.c index f0d95ee..791110a 100644 --- a/test/aes128cbc_decrypt_block.c +++ b/test/aes128cbc_decrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes128cbc_decrypt(cipher, &inverted_schedule, iv); - iv = cipher; + plain = aes128cbc_decrypt(cipher, &inverted_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes128cbc_encrypt_block.c b/test/aes128cbc_encrypt_block.c index 14e7df7..a4a1675 100644 --- a/test/aes128cbc_encrypt_block.c +++ b/test/aes128cbc_encrypt_block.c @@ -46,7 +46,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes128cbc_encrypt(plain, &key_schedule, iv); + cipher = aes128cbc_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes128cfb_decrypt_block.c b/test/aes128cfb_decrypt_block.c index 90d0fca..bfc7ea7 100644 --- a/test/aes128cfb_decrypt_block.c +++ b/test/aes128cfb_decrypt_block.c @@ -46,8 +46,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes128cfb_decrypt(cipher, &key_schedule, iv); - iv = cipher; + plain = aes128cfb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes128cfb_encrypt_block.c b/test/aes128cfb_encrypt_block.c index d1e30d0..b4e2885 100644 --- a/test/aes128cfb_encrypt_block.c +++ b/test/aes128cfb_encrypt_block.c @@ -46,7 +46,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes128cfb_encrypt(plain, &key_schedule, iv); + cipher = aes128cfb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes128ofb_decrypt_block.c b/test/aes128ofb_decrypt_block.c index 92add4b..74fb314 100644 --- a/test/aes128ofb_decrypt_block.c +++ b/test/aes128ofb_decrypt_block.c @@ -46,8 +46,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes128cfb_decrypt(cipher, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + plain = aes128ofb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes128ofb_encrypt_block.c b/test/aes128ofb_encrypt_block.c index ef32a59..df30777 100644 --- a/test/aes128ofb_encrypt_block.c +++ b/test/aes128ofb_encrypt_block.c @@ -46,8 +46,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - cipher = aes128ofb_encrypt(plain, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + cipher = aes128ofb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes192cbc_decrypt_block.c b/test/aes192cbc_decrypt_block.c index 57786ef..6a953ad 100644 --- a/test/aes192cbc_decrypt_block.c +++ b/test/aes192cbc_decrypt_block.c @@ -48,8 +48,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes192cbc_decrypt(cipher, &inverted_schedule, iv); - iv = cipher; + plain = aes192cbc_decrypt(cipher, &inverted_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes192cbc_encrypt_block.c b/test/aes192cbc_encrypt_block.c index 8a5a9b8..a1269b4 100644 --- a/test/aes192cbc_encrypt_block.c +++ b/test/aes192cbc_encrypt_block.c @@ -47,7 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes192cbc_encrypt(plain, &key_schedule, iv); + cipher = aes192cbc_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes192cfb_decrypt_block.c b/test/aes192cfb_decrypt_block.c index 55db87e..906ecf9 100644 --- a/test/aes192cfb_decrypt_block.c +++ b/test/aes192cfb_decrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes192cfb_decrypt(cipher, &key_schedule, iv); - iv = cipher; + plain = aes192cfb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes192cfb_encrypt_block.c b/test/aes192cfb_encrypt_block.c index 70d1505..3261a03 100644 --- a/test/aes192cfb_encrypt_block.c +++ b/test/aes192cfb_encrypt_block.c @@ -47,7 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes192cfb_encrypt(plain, &key_schedule, iv); + cipher = aes192cfb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes192ofb_decrypt_block.c b/test/aes192ofb_decrypt_block.c index 19c873f..268b00c 100644 --- a/test/aes192ofb_decrypt_block.c +++ b/test/aes192ofb_decrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes192cfb_decrypt(cipher, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + plain = aes192ofb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes192ofb_encrypt_block.c b/test/aes192ofb_encrypt_block.c index a3fb944..8c866c3 100644 --- a/test/aes192ofb_encrypt_block.c +++ b/test/aes192ofb_encrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - cipher = aes192cfb_encrypt(plain, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + cipher = aes192ofb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes256cbc_decrypt_block.c b/test/aes256cbc_decrypt_block.c index f7c5fbc..52abaff 100644 --- a/test/aes256cbc_decrypt_block.c +++ b/test/aes256cbc_decrypt_block.c @@ -48,8 +48,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes256cbc_decrypt(cipher, &inverted_schedule, iv); - iv = cipher; + plain = aes256cbc_decrypt(cipher, &inverted_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes256cbc_encrypt_block.c b/test/aes256cbc_encrypt_block.c index 9e1ada7..0b10f54 100644 --- a/test/aes256cbc_encrypt_block.c +++ b/test/aes256cbc_encrypt_block.c @@ -47,7 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes256cbc_encrypt(plain, &key_schedule, iv); + cipher = aes256cbc_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes256cfb_decrypt_block.c b/test/aes256cfb_decrypt_block.c index f0b7fa0..d3f6075 100644 --- a/test/aes256cfb_decrypt_block.c +++ b/test/aes256cfb_decrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes256cfb_decrypt(cipher, &key_schedule, iv); - iv = cipher; + plain = aes256cfb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes256cfb_encrypt_block.c b/test/aes256cfb_encrypt_block.c index 948b658..c14e931 100644 --- a/test/aes256cfb_encrypt_block.c +++ b/test/aes256cfb_encrypt_block.c @@ -47,7 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - iv = cipher = aes256cfb_encrypt(plain, &key_schedule, iv); + cipher = aes256cfb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } diff --git a/test/aes256ofb_decrypt_block.c b/test/aes256ofb_decrypt_block.c index 45dcd7b..282dc6a 100644 --- a/test/aes256ofb_decrypt_block.c +++ b/test/aes256ofb_decrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - plain = aes256cfb_decrypt(cipher, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + plain = aes256ofb_decrypt(cipher, &key_schedule, iv, &iv); print_aes_block128(&plain); } diff --git a/test/aes256ofb_encrypt_block.c b/test/aes256ofb_encrypt_block.c index 4498bc8..b74f29f 100644 --- a/test/aes256ofb_encrypt_block.c +++ b/test/aes256ofb_encrypt_block.c @@ -47,8 +47,7 @@ int main(int argc, char** argv) fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]); continue; } - cipher = aes256cfb_encrypt(plain, &key_schedule, iv); - iv = _mm_xor_si128(cipher, plain); + cipher = aes256ofb_encrypt(plain, &key_schedule, iv, &iv); print_aes_block128(&cipher); } -- cgit v1.2.3