From 6b070da3e039d2ca7accae8489d8bc4a44ba652a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 3 Jun 2015 20:55:09 +0300 Subject: implement CBC wrappers in C --- examples/aes128cbc_example.c | 4 ++-- examples/aes192cbc_example.c | 4 ++-- examples/aes256cbc_example.c | 4 ++-- include/aesni/api.h | 24 ++++++++++++------------ include/aesni/raw.h | 27 --------------------------- src/aes128.asm | 11 ----------- src/aes192.asm | 11 ----------- src/aes256.asm | 11 ----------- test/aes128cbc_decrypt_block.c | 2 +- test/aes128cbc_encrypt_block.c | 2 +- test/aes192cbc_decrypt_block.c | 2 +- test/aes192cbc_encrypt_block.c | 2 +- test/aes256cbc_decrypt_block.c | 2 +- test/aes256cbc_encrypt_block.c | 2 +- 14 files changed, 24 insertions(+), 84 deletions(-) diff --git a/examples/aes128cbc_example.c b/examples/aes128cbc_example.c index bae250b..cb5684e 100644 --- a/examples/aes128cbc_example.c +++ b/examples/aes128cbc_example.c @@ -43,7 +43,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); } - cypher = aes128cbc_encrypt(plain, &key_schedule, &iv); + cypher = aes128cbc_encrypt(plain, &key_schedule, iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); @@ -59,7 +59,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); } - decrypted = aes128cbc_decrypt(cypher, &inverted_schedule, &iv); + decrypted = aes128cbc_decrypt(cypher, &inverted_schedule, iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); printf(" %s\n", format_aes_block128_fips_style(&decrypted).str); diff --git a/examples/aes192cbc_example.c b/examples/aes192cbc_example.c index 4b583b6..1edff77 100644 --- a/examples/aes192cbc_example.c +++ b/examples/aes192cbc_example.c @@ -44,7 +44,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); } - cypher = aes192cbc_encrypt(plain, &key_schedule, &iv); + cypher = aes192cbc_encrypt(plain, &key_schedule, iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); @@ -60,7 +60,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); } - decrypted = aes192cbc_decrypt(cypher, &inverted_schedule, &iv); + decrypted = aes192cbc_decrypt(cypher, &inverted_schedule, iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); printf(" %s\n", format_aes_block128_fips_style(&decrypted).str); diff --git a/examples/aes256cbc_example.c b/examples/aes256cbc_example.c index 816683b..bfd88fb 100644 --- a/examples/aes256cbc_example.c +++ b/examples/aes256cbc_example.c @@ -44,7 +44,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); } - cypher = aes256cbc_encrypt(plain, &key_schedule, &iv); + cypher = aes256cbc_encrypt(plain, &key_schedule, iv); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); @@ -60,7 +60,7 @@ int main() printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); } - decrypted = aes256cbc_decrypt(cypher, &inverted_schedule, &iv); + decrypted = aes256cbc_decrypt(cypher, &inverted_schedule, iv); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); printf(" %s\n", format_aes_block128_fips_style(&decrypted).str); diff --git a/include/aesni/api.h b/include/aesni/api.h index efcecc4..72e689b 100644 --- a/include/aesni/api.h +++ b/include/aesni/api.h @@ -42,17 +42,17 @@ static __inline AesBlock128 __fastcall aes128ecb_decrypt( static __inline AesBlock128 __fastcall aes128cbc_encrypt( AesBlock128 plain, Aes128KeySchedule* key_schedule, - AesBlock128* init_vector) + AesBlock128 init_vector) { - return raw_aes128cbc_encrypt(plain, key_schedule, init_vector); + return raw_aes128ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); } static __inline AesBlock128 __fastcall aes128cbc_decrypt( AesBlock128 cypher, Aes128KeySchedule* inverted_schedule, - AesBlock128* init_vector) + AesBlock128 init_vector) { - return raw_aes128cbc_decrypt(cypher, inverted_schedule, init_vector); + return _mm_xor_si128(raw_aes128ecb_decrypt(cypher, inverted_schedule), init_vector); } static __inline void __fastcall aes192_expand_key_schedule( @@ -86,17 +86,17 @@ static __inline AesBlock128 __fastcall aes192ecb_decrypt( static __inline AesBlock128 __fastcall aes192cbc_encrypt( AesBlock128 plain, Aes192KeySchedule* key_schedule, - AesBlock128* initialization_vector) + AesBlock128 init_vector) { - return raw_aes192cbc_encrypt(plain, key_schedule, initialization_vector); + return raw_aes192ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); } static __inline AesBlock128 __fastcall aes192cbc_decrypt( AesBlock128 cypher, Aes192KeySchedule* inverted_schedule, - AesBlock128* initialization_vector) + AesBlock128 init_vector) { - return raw_aes192cbc_decrypt(cypher, inverted_schedule, initialization_vector); + return _mm_xor_si128(raw_aes192ecb_decrypt(cypher, inverted_schedule), init_vector); } static __inline void __fastcall aes256_expand_key_schedule( @@ -130,15 +130,15 @@ static __inline AesBlock128 __fastcall aes256ecb_decrypt( static __inline AesBlock128 __fastcall aes256cbc_encrypt( AesBlock128 plain, Aes256KeySchedule* key_schedule, - AesBlock128* initialization_vector) + AesBlock128 init_vector) { - return raw_aes256cbc_encrypt(plain, key_schedule, initialization_vector); + return raw_aes256ecb_encrypt(_mm_xor_si128(plain, init_vector), key_schedule); } static __inline AesBlock128 __fastcall aes256cbc_decrypt( AesBlock128 cypher, Aes256KeySchedule* inverted_schedule, - AesBlock128* initialization_vector) + AesBlock128 init_vector) { - return raw_aes256cbc_decrypt(cypher, inverted_schedule, initialization_vector); + return _mm_xor_si128(raw_aes256ecb_decrypt(cypher, inverted_schedule), init_vector); } diff --git a/include/aesni/raw.h b/include/aesni/raw.h index db342c7..d6b9518 100644 --- a/include/aesni/raw.h +++ b/include/aesni/raw.h @@ -24,15 +24,6 @@ AesBlock128 __fastcall raw_aes128ecb_decrypt( AesBlock128 cypher, Aes128KeySchedule* inverted_schedule); -AesBlock128 __fastcall raw_aes128cbc_encrypt( - AesBlock128 plain, - Aes128KeySchedule* key_schedule, - AesBlock128* iv); -AesBlock128 __fastcall raw_aes128cbc_decrypt( - AesBlock128 cypher, - Aes128KeySchedule* inverted_schedule, - AesBlock128* iv); - void __fastcall raw_aes192_expand_key_schedule( AesBlock128 key_lo, AesBlock128 key_hi, @@ -48,15 +39,6 @@ AesBlock128 __fastcall raw_aes192ecb_decrypt( AesBlock128 cypher, Aes192KeySchedule* inverted_schedule); -AesBlock128 __fastcall raw_aes192cbc_encrypt( - AesBlock128 plain, - Aes192KeySchedule* key_schedule, - AesBlock128 *iv); -AesBlock128 __fastcall raw_aes192cbc_decrypt( - AesBlock128 cypher, - Aes192KeySchedule* inverted_schedule, - AesBlock128 *iv); - void __fastcall raw_aes256_expand_key_schedule( AesBlock128 key_lo, AesBlock128 key_hi, @@ -71,12 +53,3 @@ AesBlock128 __fastcall raw_aes256ecb_encrypt( AesBlock128 __fastcall raw_aes256ecb_decrypt( AesBlock128 cypher, Aes256KeySchedule* inverted_schedule); - -AesBlock128 __fastcall raw_aes256cbc_encrypt( - AesBlock128 plain, - Aes256KeySchedule* key_schedule, - AesBlock128 *iv); -AesBlock128 __fastcall raw_aes256cbc_decrypt( - AesBlock128 cypher, - Aes256KeySchedule* inverted_schedule, - AesBlock128 *iv); diff --git a/src/aes128.asm b/src/aes128.asm index 577e582..0153feb 100644 --- a/src/aes128.asm +++ b/src/aes128.asm @@ -23,11 +23,6 @@ ret @raw_aes128ecb_encrypt@20 endp -@raw_aes128cbc_encrypt@24 proc - pxor xmm0, [edx] - jmp @raw_aes128ecb_encrypt@20 -@raw_aes128cbc_encrypt@24 endp - @raw_aes128ecb_decrypt@20 proc pxor xmm0, [ecx] aesdec xmm0, [ecx + 10h] @@ -43,12 +38,6 @@ ret @raw_aes128ecb_decrypt@20 endp -@raw_aes128cbc_decrypt@24 proc - call @raw_aes128ecb_decrypt@20 - pxor xmm0, [edx] - ret -@raw_aes128cbc_decrypt@24 endp - @raw_aes128_expand_key_schedule@20 proc ; A "word" (in terms of the FIPS 187 standard) is a 32-bit block. ; Words are denoted by `w[N]`. diff --git a/src/aes192.asm b/src/aes192.asm index 2cba7ba..f3a56a8 100644 --- a/src/aes192.asm +++ b/src/aes192.asm @@ -25,11 +25,6 @@ ret @raw_aes192ecb_encrypt@20 endp -@raw_aes192cbc_encrypt@24 proc - pxor xmm0, [edx] - jmp @raw_aes192ecb_encrypt@20 -@raw_aes192cbc_encrypt@24 endp - @raw_aes192ecb_decrypt@20 proc pxor xmm0, [ecx] aesdec xmm0, [ecx + 10h] @@ -47,12 +42,6 @@ ret @raw_aes192ecb_decrypt@20 endp -@raw_aes192cbc_decrypt@24 proc - call @raw_aes192ecb_decrypt@20 - pxor xmm0, [edx] - ret -@raw_aes192cbc_decrypt@24 endp - @raw_aes192_expand_key_schedule@36 proc ; A "word" (in terms of the FIPS 187 standard) is a 32-bit block. ; Words are denoted by `w[N]`. diff --git a/src/aes256.asm b/src/aes256.asm index a2bee04..41921dd 100644 --- a/src/aes256.asm +++ b/src/aes256.asm @@ -27,11 +27,6 @@ ret @raw_aes256ecb_encrypt@20 endp -@raw_aes256cbc_encrypt@24 proc - pxor xmm0, [edx] - jmp @raw_aes256ecb_encrypt@20 -@raw_aes256cbc_encrypt@24 endp - @raw_aes256ecb_decrypt@20 proc pxor xmm0, [ecx] aesdec xmm0, [ecx + 10h] @@ -51,12 +46,6 @@ ret @raw_aes256ecb_decrypt@20 endp -@raw_aes256cbc_decrypt@24 proc - call @raw_aes256ecb_decrypt@20 - pxor xmm0, [edx] - ret -@raw_aes256cbc_decrypt@24 endp - @raw_aes256_expand_key_schedule@36 proc ; A "word" (in terms of the FIPS 187 standard) is a 32-bit block. ; Words are denoted by `w[N]`. diff --git a/test/aes128cbc_decrypt_block.c b/test/aes128cbc_decrypt_block.c index 82a1b61..f0d95ee 100644 --- a/test/aes128cbc_decrypt_block.c +++ b/test/aes128cbc_decrypt_block.c @@ -47,7 +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); + plain = aes128cbc_decrypt(cipher, &inverted_schedule, iv); iv = cipher; print_aes_block128(&plain); } diff --git a/test/aes128cbc_encrypt_block.c b/test/aes128cbc_encrypt_block.c index 38faf1a..14e7df7 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); + iv = cipher = aes128cbc_encrypt(plain, &key_schedule, iv); print_aes_block128(&cipher); } diff --git a/test/aes192cbc_decrypt_block.c b/test/aes192cbc_decrypt_block.c index 79eb692..57786ef 100644 --- a/test/aes192cbc_decrypt_block.c +++ b/test/aes192cbc_decrypt_block.c @@ -48,7 +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); + plain = aes192cbc_decrypt(cipher, &inverted_schedule, iv); iv = cipher; print_aes_block128(&plain); } diff --git a/test/aes192cbc_encrypt_block.c b/test/aes192cbc_encrypt_block.c index 45f469b..8a5a9b8 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); + iv = cipher = aes192cbc_encrypt(plain, &key_schedule, iv); print_aes_block128(&cipher); } diff --git a/test/aes256cbc_decrypt_block.c b/test/aes256cbc_decrypt_block.c index 43b8091..f7c5fbc 100644 --- a/test/aes256cbc_decrypt_block.c +++ b/test/aes256cbc_decrypt_block.c @@ -48,7 +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); + plain = aes256cbc_decrypt(cipher, &inverted_schedule, iv); iv = cipher; print_aes_block128(&plain); } diff --git a/test/aes256cbc_encrypt_block.c b/test/aes256cbc_encrypt_block.c index 2877d31..9e1ada7 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); + iv = cipher = aes256cbc_encrypt(plain, &key_schedule, iv); print_aes_block128(&cipher); } -- cgit v1.2.3