From c77c663976fb8d3a2d372a7c4331f7eca55532e5 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 13 Mar 2019 18:28:15 +0200 Subject: [PATCH] generate round keys beforehand --- Makefile | 3 ++- inc/ft_des.h | 8 +++++++- src/des/ft_des_encrypt_block.c | 10 +++------- src/des/ft_des_generate_round_keys.c | 17 +++++++++++++++++ t/des_tests.c | 6 +++++- 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 src/des/ft_des_generate_round_keys.c diff --git a/Makefile b/Makefile index c4b815d4..ec29c20d 100644 --- a/Makefile +++ b/Makefile @@ -122,7 +122,8 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_rotate_half_key_right.c \ ft_des_key_permuted_choice_two.c \ ft_des_encryption_round.c \ - ft_des_encrypt_block.c + ft_des_encrypt_block.c \ + ft_des_generate_round_keys.c SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_des.h b/inc/ft_des.h index 2390355c..4b72def9 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -131,6 +131,12 @@ void ft_des_derive_encryption_round_key t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] ); +void ft_des_generate_round_keys +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +); + void ft_des_rotate_half_key_left ( t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], @@ -153,7 +159,7 @@ void ft_des_encryption_round void ft_des_encrypt_block ( t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], - t_byte1 key[FT_DES_INITIAL_KEY_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE], t_byte1 ciphertext[FT_DES_BIT_BLOCK_SIZE] ); diff --git a/src/des/ft_des_encrypt_block.c b/src/des/ft_des_encrypt_block.c index aa30a5fc..7fd465c6 100644 --- a/src/des/ft_des_encrypt_block.c +++ b/src/des/ft_des_encrypt_block.c @@ -20,25 +20,21 @@ static void switch_halves void ft_des_encrypt_block ( t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], - t_byte1 key[FT_DES_INITIAL_KEY_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE], t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE] ) { t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE]; - t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE]; - t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]; int i; ft_des_initial_permutation(plaintext, ip_message); - ft_des_key_permuted_choice_one(key, reduced_key); i = 1; while(i <= FT_DES_ROUND_COUNT) { - ft_des_derive_encryption_round_key(reduced_key, i, round_key); if (i % 2 != 0) - ft_des_encryption_round(ip_message, ip_message + 32, round_key); + ft_des_encryption_round(ip_message, ip_message + 32, keys[i - 1]); else - ft_des_encryption_round(ip_message + 32, ip_message, round_key); + ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]); i++; } switch_halves(ip_message); diff --git a/src/des/ft_des_generate_round_keys.c b/src/des/ft_des_generate_round_keys.c new file mode 100644 index 00000000..a297dbe1 --- /dev/null +++ b/src/des/ft_des_generate_round_keys.c @@ -0,0 +1,17 @@ +#include "ft_des.h" + +void ft_des_generate_round_keys +( + t_byte1 key[FT_DES_INITIAL_KEY_SIZE], + t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +) +{ + int i; + + i = 1; + while(i <= FT_DES_ROUND_COUNT) + { + ft_des_derive_encryption_round_key(key, i, round_keys[i - 1]); + i++; + } +} \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index 83bbc7ab..a9bbdd9a 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -587,9 +587,13 @@ int encrypt_block() 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, }; + t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE]; + t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]; t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE]; - ft_des_encrypt_block(message, key, cyphertext); + ft_des_key_permuted_choice_one(key, reduced_key); + ft_des_generate_round_keys(reduced_key, round_keys); + ft_des_encrypt_block(message, round_keys, cyphertext); int i = 0; while(i < FT_DES_BIT_BLOCK_SIZE)