diff --git a/Makefile b/Makefile index 9be9a9e6..3dd65d73 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,12 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_s_box_7.c \ ft_des_s_box_8.c \ ft_des_feistel_function_permutation.c \ - ft_des_feistel_function.c + ft_des_feistel_function.c \ + ft_des_key_permuted_choice_one.c \ + ft_des_encryption_round_key.c \ + ft_des_rotate_half_key_left.c \ + ft_des_rotate_half_key_right.c \ + ft_des_key_permuted_choice_two.c SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_des.h b/inc/ft_des.h index 68add0ee..eefd7556 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -20,6 +20,8 @@ # define FT_DES_S_BOX_TABLE_ROWS 4 # define FT_DES_S_BOX_TABLE_COLUMNS 16 # define FT_DES_FEISTEL_FUNCTION_KEY_SIZE 48 +# define FT_DES_INITIAL_KEY_SIZE 64 +# define FT_DES_REDUCED_KEY_SIZE 56 typedef unsigned char t_byte1; @@ -109,4 +111,35 @@ void ft_des_feistel_function t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2] ); +void ft_des_key_permuted_choice_one +( + t_byte1 initial_key[FT_DES_INITIAL_KEY_SIZE], + t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE] +); + +void ft_des_key_permuted_choice_two +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +); + +void ft_des_encryption_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_rotate_half_key_left +( + t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], + t_byte1 rotation_number +); + +void ft_des_rotate_half_key_right +( + t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], + t_byte1 rotation_number +); + #endif diff --git a/src/des/ft_des_encryption_round_key.c b/src/des/ft_des_encryption_round_key.c new file mode 100644 index 00000000..6c2472b1 --- /dev/null +++ b/src/des/ft_des_encryption_round_key.c @@ -0,0 +1,21 @@ +#include "ft_des.h" + +void ft_des_encryption_round_key +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 round, + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +) +{ + if (round == 1 || round == 2 || round == 9 || round == 16) + { + ft_des_rotate_half_key_left(input_key, 1); + ft_des_rotate_half_key_left(input_key + FT_DES_REDUCED_KEY_SIZE / 2, 1); + } + else + { + ft_des_rotate_half_key_left(input_key, 2); + ft_des_rotate_half_key_left(input_key + FT_DES_REDUCED_KEY_SIZE / 2, 2); + } + ft_des_key_permuted_choice_two(input_key, round_key); +} \ No newline at end of file diff --git a/src/des/ft_des_key_permuted_choice_one.c b/src/des/ft_des_key_permuted_choice_one.c new file mode 100644 index 00000000..5ecab164 --- /dev/null +++ b/src/des/ft_des_key_permuted_choice_one.c @@ -0,0 +1,26 @@ +#include "ft_des.h" + +void ft_des_key_permuted_choice_one +( + t_byte1 initial_key[FT_DES_INITIAL_KEY_SIZE], + t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE] +) +{ + static t_byte1 table[FT_DES_REDUCED_KEY_SIZE] = { + 57, 49, 41, 33, 25, 17, 9, 1, + 58, 50, 42, 34, 26, 18, 10, 2, + 59, 51, 43, 35, 27, 19, 11, 3, + 60, 52, 44, 36, 63, 55, 47, 39, + 31, 23, 15, 7, 62, 54, 46, 38, + 30, 22, 14, 6, 61, 53, 45, 37, + 29, 21, 13, 5, 28, 20, 12, 4, + }; + int i; + + i = 0; + while(i < FT_DES_REDUCED_KEY_SIZE) + { + reduced_key[i] = initial_key[table[i] - 1]; + i++; + } +} \ No newline at end of file diff --git a/src/des/ft_des_key_permuted_choice_two.c b/src/des/ft_des_key_permuted_choice_two.c new file mode 100644 index 00000000..af739a64 --- /dev/null +++ b/src/des/ft_des_key_permuted_choice_two.c @@ -0,0 +1,25 @@ +#include "ft_des.h" + +void ft_des_key_permuted_choice_two +( + t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE], + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] +) +{ + static t_byte1 table[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] = { + 14, 17, 11, 24, 1, 5, 3, 28, + 15, 6, 21, 10, 23, 19, 12, 4, + 26, 8, 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, 30, 40, + 51, 45, 33, 48, 44, 49, 39, 56, + 34, 53, 46, 42, 50, 36, 29, 32, + }; + int i; + + i = 0; + while(i < FT_DES_FEISTEL_FUNCTION_KEY_SIZE) + { + round_key[i] = input_key[table[i] - 1]; + i++; + } +} \ No newline at end of file diff --git a/src/des/ft_des_rotate_half_key_left.c b/src/des/ft_des_rotate_half_key_left.c new file mode 100644 index 00000000..3e0fb285 --- /dev/null +++ b/src/des/ft_des_rotate_half_key_left.c @@ -0,0 +1,26 @@ +#include "ft_des.h" + +void ft_des_rotate_half_key_left +( + t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], + t_byte1 rotation_number +) +{ + t_byte1 i; + t_byte1 j; + t_byte1 tmp; + + tmp = half[0]; + i = 0; + while(i < rotation_number) + { + j = 0; + while(j < FT_DES_REDUCED_KEY_SIZE / 2 - 1) + { + half[j] = half[j + 1]; + j++; + } + half[FT_DES_REDUCED_KEY_SIZE / 2 - 1] = tmp; + i++; + } +} \ 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 new file mode 100644 index 00000000..033a9a25 --- /dev/null +++ b/src/des/ft_des_rotate_half_key_right.c @@ -0,0 +1,26 @@ +#include "ft_des.h" + +void ft_des_rotate_half_key_right +( + t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], + t_byte1 rotation_number +) +{ + t_byte1 i; + t_byte1 j; + t_byte1 tmp; + + tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1]; + i = 0; + while(i < rotation_number) + { + j = FT_DES_REDUCED_KEY_SIZE / 2 - 1; + while(j > 0) + { + half[j] = half[j - 1]; + j--; + } + half[0] = tmp; + i++; + } +} \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index 741e0a63..5945c3b1 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -378,6 +378,127 @@ int perform_feistel_function() _end("feistel function should encode half of a block"); } +int reduce_key_to_56_bits() +{ + t_byte1 initial_key[FT_DES_INITIAL_KEY_SIZE] = { + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 1, 1, 1, 1, 0, + }; + + t_byte1 expected_reduced_key[FT_DES_REDUCED_KEY_SIZE] = { + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, + }; + t_byte1 actual_reduced[FT_DES_REDUCED_KEY_SIZE]; + + ft_des_key_permuted_choice_one(initial_key, actual_reduced); + + int i = 0; + while(i < FT_DES_REDUCED_KEY_SIZE) + { + _is(actual_reduced[i] == expected_reduced_key[i]); + i++; + } + + _end("should reduce key size to 56 bits"); +} + +int rotate_half_key() +{ + t_byte1 half_key[FT_DES_REDUCED_KEY_SIZE / 2] = { + 1, 1, 1, 1, 0, 0, 0, + 0, 1, 1, 0, 0, 1, 1, + 0, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 1, 1, 1, + }; + + t_byte1 expected[FT_DES_REDUCED_KEY_SIZE / 2]; + + ft_memcpy(expected, half_key, FT_DES_REDUCED_KEY_SIZE / 2); + + ft_des_rotate_half_key_left(half_key, 1); + ft_des_rotate_half_key_left(half_key, 1); + ft_des_rotate_half_key_right(half_key, 2); + + int i = 0; + + while(i < FT_DES_REDUCED_KEY_SIZE / 2) + { + _is(half_key[i] == expected[i]); + i++; + } + _end("should rotate half of reduced key"); +} + +int derive_round_key() +{ + t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE] = { + 1, 1, 1, 1, 0, 0, 0, + 0, 1, 1, 0, 0, 1, 1, + 0, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 1, 1, 1, + 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 1, 0, 0, 1, + 1, 0, 0, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 1, 1, + }; + + t_byte1 shifted_reduced_key[FT_DES_REDUCED_KEY_SIZE] = { + 1, 1, 1, 0, 0, 0, 0, + 1, 1, 0, 0, 1, 1, 0, + 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 1, 1, 1, 1, + 1, 0, 1, 0, 1, 0, 1, + 0, 1, 1, 0, 0, 1, 1, + 0, 0, 1, 1, 1, 1, 0, + 0, 0, 1, 1, 1, 1, 0, + }; + + t_byte1 expected_round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] = { + 0, 0, 0, 1, 1, 0, + 1, 1, 0, 0, 0, 0, + 0, 0, 1, 0, 1, 1, + 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, + 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 1, + 1, 1, 0, 0, 1, 0, + }; + + t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]; + + ft_des_encryption_round_key(reduced_key, 1, round_key); + + int i; + + i = 0; + while(i < FT_DES_REDUCED_KEY_SIZE) + { + _is(shifted_reduced_key[i] == reduced_key[i]); + i++; + } + + i = 0; + while(i < FT_DES_FEISTEL_FUNCTION_KEY_SIZE) + { + _is(round_key[i] == expected_round_key[i]); + i++; + } + _end("should derive round key"); +} + int des_tests() { _should(perform_initial_permutation); @@ -387,5 +508,8 @@ int des_tests() _should(s_boxes_confuse); _should(perform_premutation_in_feistel_function); _should(perform_feistel_function); + _should(reduce_key_to_56_bits); + _should(rotate_half_key); + _should(derive_round_key); return 0; } \ No newline at end of file