From 6a3af7423e800f6c351f68da03ed391b7df662e1 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 13 Mar 2019 19:40:38 +0200 Subject: [PATCH] decrypt block --- Makefile | 10 ++-- inc/ft_des.h | 19 ++++++- ...encrypt_block.c => ft_des_decrypt_block.c} | 19 +------ src/des/ft_des_derive_decryption_round_key.c | 21 +++++++ ...c => ft_des_derive_encryption_round_key.c} | 0 .../ft_des_generate_decryption_round_keys.c | 17 ++++++ ...> ft_des_generate_encryption_round_keys.c} | 2 +- src/des/ft_des_process_block.c | 42 ++++++++++++++ src/des/ft_des_rotate_half_key_right.c | 2 +- ..._des_encryption_round.c => ft_des_round.c} | 2 +- src/des/ft_des_switch_message_halves.c | 19 +++++++ t/des_tests.c | 56 ++++++++++++++++++- 12 files changed, 178 insertions(+), 31 deletions(-) rename src/des/{ft_des_encrypt_block.c => ft_des_decrypt_block.c} (65%) create mode 100644 src/des/ft_des_derive_decryption_round_key.c rename src/des/{ft_des_encryption_round_key.c => ft_des_derive_encryption_round_key.c} (100%) create mode 100644 src/des/ft_des_generate_decryption_round_keys.c rename src/des/{ft_des_generate_round_keys.c => ft_des_generate_encryption_round_keys.c} (86%) create mode 100644 src/des/ft_des_process_block.c rename src/des/{ft_des_encryption_round.c => ft_des_round.c} (95%) create mode 100644 src/des/ft_des_switch_message_halves.c diff --git a/Makefile b/Makefile index ec29c20d..a86531ac 100644 --- a/Makefile +++ b/Makefile @@ -117,13 +117,15 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_feistel_function_permutation.c \ ft_des_feistel_function.c \ ft_des_key_permuted_choice_one.c \ - ft_des_encryption_round_key.c \ + ft_des_derive_encryption_round_key.c \ + ft_des_derive_decryption_round_key.c \ ft_des_rotate_half_key_left.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_generate_round_keys.c + ft_des_round.c \ + ft_des_process_block.c \ + ft_des_generate_encryption_round_keys.c \ + ft_des_generate_decryption_round_keys.c \ SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_des.h b/inc/ft_des.h index 4b72def9..778fb00e 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -131,7 +131,20 @@ void ft_des_derive_encryption_round_key t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] ); -void ft_des_generate_round_keys +void ft_des_derive_decryption_round_key +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 round, + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +); + +void ft_des_generate_encryption_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_generate_decryption_round_keys ( t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE] @@ -149,14 +162,14 @@ void ft_des_rotate_half_key_right t_byte1 rotation_number ); -void ft_des_encryption_round +void ft_des_round ( t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2], t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2], t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] ); -void ft_des_encrypt_block +void ft_des_process_block ( t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE], diff --git a/src/des/ft_des_encrypt_block.c b/src/des/ft_des_decrypt_block.c similarity index 65% rename from src/des/ft_des_encrypt_block.c rename to src/des/ft_des_decrypt_block.c index 7fd465c6..263ae359 100644 --- a/src/des/ft_des_encrypt_block.c +++ b/src/des/ft_des_decrypt_block.c @@ -1,22 +1,5 @@ #include "ft_des.h" -static void switch_halves -( - t_byte1 message[FT_DES_BIT_BLOCK_SIZE] -) -{ - int i; - int tmp; - i = 0; - while(i < FT_DES_BIT_BLOCK_SIZE / 2) - { - tmp = message[i]; - message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2]; - message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp; - i++; - } -} - void ft_des_encrypt_block ( t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], @@ -37,6 +20,6 @@ void ft_des_encrypt_block ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]); i++; } - switch_halves(ip_message); + ft_des_switch_message_halves(ip_message); ft_des_final_permutation(ip_message, cyphertext); } \ No newline at end of file diff --git a/src/des/ft_des_derive_decryption_round_key.c b/src/des/ft_des_derive_decryption_round_key.c new file mode 100644 index 00000000..be016f03 --- /dev/null +++ b/src/des/ft_des_derive_decryption_round_key.c @@ -0,0 +1,21 @@ +#include "ft_des.h" + +void ft_des_derive_decryption_round_key +( + t_byte1 key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 round, + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +) +{ + if (round == 2 || round == 9 || round == 16) + { + ft_des_rotate_half_key_right(key, 1); + ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 1); + } + else if (round != 1) + { + ft_des_rotate_half_key_right(key, 2); + ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 2); + } + ft_des_key_permuted_choice_two(key, round_key); +} \ No newline at end of file diff --git a/src/des/ft_des_encryption_round_key.c b/src/des/ft_des_derive_encryption_round_key.c similarity index 100% rename from src/des/ft_des_encryption_round_key.c rename to src/des/ft_des_derive_encryption_round_key.c diff --git a/src/des/ft_des_generate_decryption_round_keys.c b/src/des/ft_des_generate_decryption_round_keys.c new file mode 100644 index 00000000..370ad0d5 --- /dev/null +++ b/src/des/ft_des_generate_decryption_round_keys.c @@ -0,0 +1,17 @@ +#include "ft_des.h" + +void ft_des_generate_decryption_round_keys +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +) +{ + int i; + + i = 1; + while(i <= FT_DES_ROUND_COUNT) + { + ft_des_derive_decryption_round_key(input_key, i, keys[i - 1]); + i++; + } +} \ No newline at end of file diff --git a/src/des/ft_des_generate_round_keys.c b/src/des/ft_des_generate_encryption_round_keys.c similarity index 86% rename from src/des/ft_des_generate_round_keys.c rename to src/des/ft_des_generate_encryption_round_keys.c index a297dbe1..68e32885 100644 --- a/src/des/ft_des_generate_round_keys.c +++ b/src/des/ft_des_generate_encryption_round_keys.c @@ -1,6 +1,6 @@ #include "ft_des.h" -void ft_des_generate_round_keys +void ft_des_generate_encryption_round_keys ( t_byte1 key[FT_DES_INITIAL_KEY_SIZE], t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE] diff --git a/src/des/ft_des_process_block.c b/src/des/ft_des_process_block.c new file mode 100644 index 00000000..04d42213 --- /dev/null +++ b/src/des/ft_des_process_block.c @@ -0,0 +1,42 @@ +#include "ft_des.h" + +static void switch_halves +( + t_byte1 message[FT_DES_BIT_BLOCK_SIZE] +) +{ + int i; + int tmp; + i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE / 2) + { + tmp = message[i]; + message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2]; + message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp; + i++; + } +} + +void ft_des_process_block +( + t_byte1 input[FT_DES_BIT_BLOCK_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE], + t_byte1 output[FT_DES_BIT_BLOCK_SIZE] +) +{ + t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE]; + int i; + + ft_des_initial_permutation(input, ip_message); + i = 1; + while(i <= FT_DES_ROUND_COUNT) + { + if (i % 2 != 0) + ft_des_round(ip_message, ip_message + 32, keys[i - 1]); + else + ft_des_round(ip_message + 32, ip_message, keys[i - 1]); + i++; + } + switch_halves(ip_message); + ft_des_final_permutation(ip_message, output); +} \ No newline at end of file diff --git a/src/des/ft_des_rotate_half_key_right.c b/src/des/ft_des_rotate_half_key_right.c index 033a9a25..dfa4322c 100644 --- a/src/des/ft_des_rotate_half_key_right.c +++ b/src/des/ft_des_rotate_half_key_right.c @@ -10,10 +10,10 @@ void ft_des_rotate_half_key_right t_byte1 j; t_byte1 tmp; - tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1]; i = 0; while(i < rotation_number) { + tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1]; j = FT_DES_REDUCED_KEY_SIZE / 2 - 1; while(j > 0) { diff --git a/src/des/ft_des_encryption_round.c b/src/des/ft_des_round.c similarity index 95% rename from src/des/ft_des_encryption_round.c rename to src/des/ft_des_round.c index 45dc0c85..3c542181 100644 --- a/src/des/ft_des_encryption_round.c +++ b/src/des/ft_des_round.c @@ -16,7 +16,7 @@ static void xor } } -void ft_des_encryption_round +void ft_des_round ( t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2], t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2], diff --git a/src/des/ft_des_switch_message_halves.c b/src/des/ft_des_switch_message_halves.c new file mode 100644 index 00000000..3b7d7c24 --- /dev/null +++ b/src/des/ft_des_switch_message_halves.c @@ -0,0 +1,19 @@ +#include "ft_des.h" + +void ft_des_switch_message_halves +( + t_byte1 message[FT_DES_BIT_BLOCK_SIZE] +) +{ + int i; + int tmp; + i = 0; + + while(i < FT_DES_BIT_BLOCK_SIZE / 2) + { + tmp = message[i]; + message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2]; + message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp; + i++; + } +} \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index a9bbdd9a..92c7b848 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -543,7 +543,7 @@ int perform_encryption_round() ft_des_initial_permutation(message, initial_permuatation); ft_des_key_permuted_choice_one(key, reduced_key); ft_des_derive_encryption_round_key(reduced_key, 1, round_key); - ft_des_encryption_round(initial_permuatation, + ft_des_round(initial_permuatation, initial_permuatation + 32, round_key); int i = 0; @@ -592,8 +592,8 @@ int encrypt_block() t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE]; 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); + ft_des_generate_encryption_round_keys(reduced_key, round_keys); + ft_des_process_block(message, round_keys, cyphertext); int i = 0; while(i < FT_DES_BIT_BLOCK_SIZE) @@ -604,6 +604,55 @@ int encrypt_block() _end("should encrypt block"); } +int decrypt_block() +{ + t_byte1 cypertext[FT_DES_BIT_BLOCK_SIZE] = { + 0, 0, 0, 0, 0, 1, 1, 0, + 1, 0, 1, 0, 1, 1, 1, 1, + 0, 1, 1, 0, 0, 0, 1, 1, + 1, 1, 0, 1, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 1, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 0, + 1, 0, 1, 0, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 0, 0, + }; + t_byte1 expected_plaintext[FT_DES_BIT_BLOCK_SIZE] = { + 0, 1, 1, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 0, 0, 1, 1, + 0, 1, 1, 0, 0, 1, 0, 0, + 0, 1, 1, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 0, 0, 1, 1, + 0, 1, 1, 0, 0, 1, 0, 0, + 0, 1, 1, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 0, 0, 1, 1, + }; + t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = { + 0, 0, 1, 1, 1, 0, 1, 1, + 0, 0, 1, 1, 1, 0, 0, 0, + 1, 0, 0, 1, 1, 0, 0, 0, + 0, 0, 1, 1, 0, 1, 1, 1, + 0, 0, 0, 1, 0, 1, 0, 1, + 0, 0, 1, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 0, 1, 1, 1, + 0, 1, 0, 1, 1, 1, 1, 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 plaintext[FT_DES_BIT_BLOCK_SIZE]; + + ft_des_key_permuted_choice_one(key, reduced_key); + ft_des_generate_decryption_round_keys(reduced_key, round_keys); + ft_des_process_block(cypertext, round_keys, plaintext); + + int i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + _is(plaintext[i] == expected_plaintext[i]); + i++; + } + _end("should decrypt block"); +} + int des_tests() { _should(perform_initial_permutation); @@ -618,5 +667,6 @@ int des_tests() _should(derive_round_key); _should(perform_encryption_round); _should(encrypt_block); + _should(decrypt_block); return 0; } \ No newline at end of file