From 1109190cc2edd86f12cdd16847f3e80e48dccc50 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 30 May 2015 05:13:44 +0300 Subject: detach key expansion routines Block encryption/decryption routines now don't expand key schedules on each call. --- examples/aes128cbc_example.c | 25 +++++++++++++++++++++++-- examples/aes128ecb_example.c | 25 +++++++++++++++++++++++-- examples/aes192cbc_example.c | 25 +++++++++++++++++++++++-- examples/aes192ecb_example.c | 25 +++++++++++++++++++++++-- examples/aes256cbc_example.c | 25 +++++++++++++++++++++++-- examples/aes256ecb_example.c | 25 +++++++++++++++++++++++-- 6 files changed, 138 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/aes128cbc_example.c b/examples/aes128cbc_example.c index ce568ea..94fb0d4 100644 --- a/examples/aes128cbc_example.c +++ b/examples/aes128cbc_example.c @@ -13,6 +13,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv; + __declspec(align(16)) Aes128KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -32,13 +33,33 @@ int main() printf(" %s\n", format_aes_block128_fips_style(&iv).str); print_aes_block128_fips_matrix_style(&iv); - cypher = aes128cbc_encrypt(plain, key, &iv); + aes128_expand_key_schedule(key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 11; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + 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); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes128cbc_decrypt(cypher, key, &iv); + aes128_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 11; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + 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/aes128ecb_example.c b/examples/aes128ecb_example.c index afb8dd9..aff4182 100644 --- a/examples/aes128ecb_example.c +++ b/examples/aes128ecb_example.c @@ -13,6 +13,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted; + __declspec(align(16)) Aes128KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -26,13 +27,33 @@ int main() printf(" %s\n", format_aes_block128_fips_style(&key).str); print_aes_block128_fips_matrix_style(&key); - cypher = aes128ecb_encrypt(plain, key); + aes128_expand_key_schedule(key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 11; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + cypher = aes128ecb_encrypt(plain, &key_schedule); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes128ecb_decrypt(cypher, key); + aes128_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 11; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + decrypted = aes128ecb_decrypt(cypher, &inverted_schedule); 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 25719c8..4b583b6 100644 --- a/examples/aes192cbc_example.c +++ b/examples/aes192cbc_example.c @@ -14,6 +14,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; __declspec(align(16)) AesBlock192 key; + __declspec(align(16)) Aes192KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block192(0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -33,13 +34,33 @@ int main() printf(" %s\n", format_aes_block128_fips_style(&iv).str); print_aes_block128_fips_matrix_style(&iv); - cypher = aes192cbc_encrypt(plain, &key, &iv); + aes192_expand_key_schedule(&key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 13; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + 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); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes192cbc_decrypt(cypher, &key, &iv); + aes192_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 13; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + 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/aes192ecb_example.c b/examples/aes192ecb_example.c index eb9b79d..eb3b329 100644 --- a/examples/aes192ecb_example.c +++ b/examples/aes192ecb_example.c @@ -14,6 +14,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, cypher, decrypted; __declspec(align(16)) AesBlock192 key; + __declspec(align(16)) Aes192KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block192(0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -27,13 +28,33 @@ int main() printf(" %s\n", format_aes_block192_fips_style(&key).str); print_aes_block192_fips_matrix_style(&key); - cypher = aes192ecb_encrypt(plain, &key); + aes192_expand_key_schedule(&key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 13; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + cypher = aes192ecb_encrypt(plain, &key_schedule); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes192ecb_decrypt(cypher, &key); + aes192_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 13; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + decrypted = aes192ecb_decrypt(cypher, &inverted_schedule); 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 275b947..816683b 100644 --- a/examples/aes256cbc_example.c +++ b/examples/aes256cbc_example.c @@ -14,6 +14,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv; __declspec(align(16)) AesBlock256 key; + __declspec(align(16)) Aes256KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block256(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -33,13 +34,33 @@ int main() printf(" %s\n", format_aes_block128_fips_style(&iv).str); print_aes_block128_fips_matrix_style(&iv); - cypher = aes256cbc_encrypt(plain, &key, &iv); + aes256_expand_key_schedule(&key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 15; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + 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); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes256cbc_decrypt(cypher, &key, &iv); + aes256_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 15; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + 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/examples/aes256ecb_example.c b/examples/aes256ecb_example.c index d7a82ee..cd36b50 100644 --- a/examples/aes256ecb_example.c +++ b/examples/aes256ecb_example.c @@ -14,6 +14,7 @@ int main() { __declspec(align(16)) AesBlock128 plain, cypher, decrypted; __declspec(align(16)) AesBlock256 key; + __declspec(align(16)) Aes256KeySchedule key_schedule, inverted_schedule; plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100); key = make_aes_block256(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100); @@ -27,13 +28,33 @@ int main() printf(" %s\n", format_aes_block256_fips_style(&key).str); print_aes_block256_fips_matrix_style(&key); - cypher = aes256ecb_encrypt(plain, &key); + aes256_expand_key_schedule(&key, &key_schedule); + + printf("\n"); + printf("Key schedule:\n"); + for (int i = 0; i < 15; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&key_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&key_schedule.keys[i]).str); + } + + cypher = aes256ecb_encrypt(plain, &key_schedule); printf("\n"); printf("Cypher: %s\n", format_aes_block128(&cypher).str); printf(" %s\n", format_aes_block128_fips_style(&cypher).str); print_aes_block128_fips_matrix_style(&cypher); - decrypted = aes256ecb_decrypt(cypher, &key); + aes256_invert_key_schedule(&key_schedule, &inverted_schedule); + + printf("\n"); + printf("Inverted key schedule:\n"); + for (int i = 0; i < 15; ++i) + { + printf("\t[%d]: %s\n", i, format_aes_block128(&inverted_schedule.keys[i]).str); + printf("\t[%d]: %s\n", i, format_aes_block128_fips_style(&inverted_schedule.keys[i]).str); + } + + decrypted = aes256ecb_decrypt(cypher, &inverted_schedule); printf("\n"); printf("Decrypted: %s\n", format_aes_block128(&decrypted).str); printf(" %s\n", format_aes_block128_fips_style(&decrypted).str); -- cgit v1.2.3