diff --git a/Makefile b/Makefile index 15fa21f8..41cdbcb2 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,9 @@ BASE64_SRC = ft_base64.c \ ft_base64_decode_transform.c \ ft_base64_decode_finish.c -DES_SRC = ft_des_initial_permutation.c +DES_SRC = ft_des_initial_permutation.c \ + ft_des_final_permutation.c \ + ft_des_expansion_box.c SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_des.h b/inc/ft_des.h index 56f49e8b..50e53b6b 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -14,6 +14,8 @@ # define FT_DES_H # define FT_DES_BIT_BLOCK_SIZE 64 +# define FT_DES_EXPANDED_HALF_BLOCK_SIZE 48 +# define FT_DES_S_BOX_SIZE 6 typedef unsigned char t_byte1; @@ -23,4 +25,16 @@ void ft_des_initial_permutation t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE] ); +void ft_des_final_permutation +( + t_byte1 before[FT_DES_BIT_BLOCK_SIZE], + t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE] +); + +void ft_des_expansion_box +( + t_byte1 half[FT_DES_BIT_BLOCK_SIZE / 2], + t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE] +); + #endif diff --git a/src/des/ft_des_expansion_box.c b/src/des/ft_des_expansion_box.c new file mode 100644 index 00000000..bd01b683 --- /dev/null +++ b/src/des/ft_des_expansion_box.c @@ -0,0 +1,27 @@ +#include "ft_des.h" + +void ft_des_expansion_box +( + t_byte1 half[FT_DES_BIT_BLOCK_SIZE / 2], + t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE] +) +{ + static t_byte1 permutation_table[FT_DES_EXPANDED_HALF_BLOCK_SIZE] = { + 32, 1, 2, 3, 4, 5, + 4, 5, 6, 7, 8, 9, + 8, 9, 10, 11, 12, 13, + 12, 13, 14, 15, 16, 17, + 16, 17, 18, 19, 20, 21, + 20, 21, 22, 23, 24, 25, + 24, 25, 26, 27, 28, 29, + 28, 29, 30, 31, 32, 1, + }; + int i; + + i = 0; + while(i< FT_DES_EXPANDED_HALF_BLOCK_SIZE) + { + expanded[i] = half[permutation_table[i] - 1]; + i++; + } +} \ No newline at end of file diff --git a/src/des/ft_des_final_permutation.c b/src/des/ft_des_final_permutation.c new file mode 100644 index 00000000..349614b8 --- /dev/null +++ b/src/des/ft_des_final_permutation.c @@ -0,0 +1,27 @@ +#include "ft_des.h" + +void ft_des_final_permutation +( + t_byte1 before[FT_DES_BIT_BLOCK_SIZE], + t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE] +) +{ + static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = { + 40, 8, 48, 16, 56, 24, 64, 32, + 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, + 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, + 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, + 33, 1, 41, 9, 49, 17, 57, 25, + }; + t_byte1 i; + + i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + final_permutation[i] = before[table[i] - 1]; + i++; + } +} \ No newline at end of file diff --git a/src/des/ft_des_initial_permutation.c b/src/des/ft_des_initial_permutation.c index d76bea28..d481ab02 100644 --- a/src/des/ft_des_initial_permutation.c +++ b/src/des/ft_des_initial_permutation.c @@ -17,12 +17,11 @@ void ft_des_initial_permutation 63, 55, 47, 39, 31, 23, 15, 7, }; t_byte1 i; - + i = 0; while(i < FT_DES_BIT_BLOCK_SIZE) { initial_permutation[i] = message[table[i] - 1]; i++; } - } \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index 4d7f23f1..ed2cce1d 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -8,7 +8,7 @@ int perform_initial_permutation() // all 64 bits: // 00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111 // block contains message bits in big-endian order - t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = (t_byte1 [FT_DES_BIT_BLOCK_SIZE]) { + t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, @@ -21,7 +21,7 @@ int perform_initial_permutation() // all 64 bits after initial permutation: // 11001100 00000000 11001100 11111111 11110000 10101010 11110000 10101010 - t_byte1 expect[FT_DES_BIT_BLOCK_SIZE] = (t_byte1 [FT_DES_BIT_BLOCK_SIZE]) { + t_byte1 expect[FT_DES_BIT_BLOCK_SIZE] = { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, @@ -47,8 +47,125 @@ int perform_initial_permutation() _end("perform initial permutation"); } +int perform_final_permutation() +{ + // all 64 bits: + // 11001100 00000000 11001100 11111111 11110000 10101010 11110000 10101010 + // block contains message bits in big-endian order + t_byte1 before[FT_DES_BIT_BLOCK_SIZE] = { + 1, 1, 0, 0, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 0, 0, 1, 1, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 0, 0, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 1, 1, 1, 0, 0, 0, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + }; + + // all 64 bits after final permutation: + // 00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111 + // block contains message bits in big-endian order + t_byte1 expect[FT_DES_BIT_BLOCK_SIZE] = { + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 1, 0, 0, 0, 1, 1, + 0, 1, 0, 0, 0, 1, 0, 1, + 0, 1, 1, 0, 0, 1, 1, 1, + 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 1, + 1, 1, 0, 0, 1, 1, 0, 1, + 1, 1, 1, 0, 1, 1, 1, 1, + }; + + t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE]; + + ft_bzero(final_permutation, FT_DES_BIT_BLOCK_SIZE); + + ft_des_final_permutation(before, final_permutation); + + int i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + _is(final_permutation[i] == expect[i]); + i++; + } + _end("perform final permutation"); +} + +int final_permutation_is_reverse_of_initial() +{ + t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = { + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + }; + + t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE]; + t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE]; + + ft_bzero(initial_permutation, FT_DES_BIT_BLOCK_SIZE); + ft_bzero(final_permutation, FT_DES_BIT_BLOCK_SIZE); + ft_des_initial_permutation(message, initial_permutation); + ft_des_final_permutation(initial_permutation, final_permutation); + + int i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + _is(final_permutation[i] == message[i]); + i++; + } + _end("final permutation is reverse of initial"); +} + +int perform_expansion_in_feistel_function() +{ + t_byte1 expanded_half_actual[FT_DES_EXPANDED_HALF_BLOCK_SIZE]; + // half block 32 bits: + // 1111 0000 1010 1010 1111 0000 1010 1010 + t_byte1 half_block[FT_DES_BIT_BLOCK_SIZE / 2] = { + 1, 1, 1, 1, 0, 0, 0, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 1, 1, 1, 0, 0, 0, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + }; + + // expanded 32 bits to 48 bits: + // 011110 100001 010101 010101 011110 100001 010101 010101 + t_byte1 expanded_half_expected[FT_DES_EXPANDED_HALF_BLOCK_SIZE] = { + 0, 1, 1, 1, 1, 0, + 1, 0, 0, 0, 0, 1, + 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, + 0, 1, 1, 1, 1, 0, + 1, 0, 0, 0, 0, 1, + 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, + }; + + ft_bzero(expanded_half_actual, FT_DES_EXPANDED_HALF_BLOCK_SIZE); + ft_des_expansion_box(half_block, expanded_half_actual); + + int i = 0; + while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE) + { + _is(expanded_half_actual[i] == expanded_half_expected[i]); + i++; + } + _end("perform expansion in feistel function"); +} + + + int des_tests() { _should(perform_initial_permutation); + _should(perform_final_permutation); + _should(final_permutation_is_reverse_of_initial); + _should(perform_expansion_in_feistel_function); return 0; } \ No newline at end of file