diff --git a/Makefile b/Makefile index 6698fb9b..15fa21f8 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ SRC_DIR := $(ROOT)/src/ MD5_DIR := $(SRC_DIR)/md5/ SHA_DIR := $(SRC_DIR)/sha/ B64_DIR := $(SRC_DIR)/base64/ +DES_DIR := $(SRC_DIR)/des/ OBJ_DIR := $(ROOT)/obj/ INC_DIR := $(ROOT)/inc/ LIB_DIR := $(ROOT)/lib/ @@ -32,10 +33,12 @@ MD5_HEADER := $(INC_DIR)/ft_md5.h SHA_HEADER := $(INC_DIR)/ft_sha.h SSL_HEADER := $(INC_DIR)/ft_ssl.h B64_HEADER := $(INC_DIR)/ft_base64.h +DES_HEADER := $(INC_DIR)/ft_des.h HEADERS := $(MD5_HEADER) \ $(SHA_HEADER) \ $(SSL_HEADER) \ - $(B64_HEADER) + $(B64_HEADER) \ + $(DES_HEADER) # libraries LIBFT_DIR := $(LIB_DIR)libft/ @@ -99,12 +102,15 @@ BASE64_SRC = ft_base64.c \ ft_base64_decode_transform.c \ ft_base64_decode_finish.c +DES_SRC = ft_des_initial_permutation.c + SRC = main.c \ ft_ssl_usage.c SRC += $(MD5_SRC) \ $(SHA_SRC) \ - $(BASE64_SRC) + $(BASE64_SRC) \ + $(DES_SRC) # project object files OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) @@ -123,10 +129,14 @@ SHA_TESTS += $(SHA_SRC) BASE64_TESTS = base64_tests.c BASE64_TESTS += $(BASE64_SRC) +DES_TESTS = des_tests.c +DES_TESTS += $(DES_SRC) + TEST_SRC = tests.c TEST_SRC += $(MD5_TESTS) \ $(SHA_TESTS) \ - $(BASE64_TESTS) + $(BASE64_TESTS) \ + $(DES_TESTS) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) @@ -207,7 +217,6 @@ re: fclean all multi: $(MAKE) $(LIBFT) - $(MAKE) check $(MAKE) $(NAME) # special stuff @@ -216,6 +225,7 @@ vpath %.c $(SRC_DIR) \ $(MD5_DIR) \ $(SHA_DIR) \ $(B64_DIR) \ + $(DES_DIR) \ $(TST_DIR) .PHONY: all check clean fclean re multi diff --git a/inc/ft_des.h b/inc/ft_des.h new file mode 100644 index 00000000..56f49e8b --- /dev/null +++ b/inc/ft_des.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_des.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/19 17:58:39 by gtertysh #+# #+# */ +/* Updated: 2019/01/19 17:59:19 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_DES_H +# define FT_DES_H + +# define FT_DES_BIT_BLOCK_SIZE 64 + +typedef unsigned char t_byte1; + +void ft_des_initial_permutation +( + t_byte1 message[FT_DES_BIT_BLOCK_SIZE], + t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE] +); + +#endif diff --git a/inc/tests.h b/inc/tests.h index 7c4ab775..4a1f5d83 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -16,5 +16,6 @@ int md5_tests(void); int sha_tests(void); int base64_tests(void); +int des_tests(void); #endif diff --git a/src/des/ft_des_initial_permutation.c b/src/des/ft_des_initial_permutation.c new file mode 100644 index 00000000..d76bea28 --- /dev/null +++ b/src/des/ft_des_initial_permutation.c @@ -0,0 +1,28 @@ +#include "ft_des.h" + +void ft_des_initial_permutation +( + t_byte1 message[FT_DES_BIT_BLOCK_SIZE], + t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE] +) +{ + static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = { + 58, 50, 42, 34, 26, 18, 10, 2, + 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, + 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, + 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, + 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 new file mode 100644 index 00000000..4d7f23f1 --- /dev/null +++ b/t/des_tests.c @@ -0,0 +1,54 @@ +#include "t.h" +#include "tests.h" +#include "ft_des.h" +#include "libft.h" + +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]) { + 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, + }; + + // 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]) { + 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, + }; + + t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE]; + + ft_bzero(initial_permutation, FT_DES_BIT_BLOCK_SIZE); + + ft_des_initial_permutation(message, initial_permutation); + + int i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + _is(initial_permutation[i] == expect[i]); + i++; + } + _end("perform initial permutation"); +} + +int des_tests() +{ + _should(perform_initial_permutation); + return 0; +} \ No newline at end of file diff --git a/t/tests.c b/t/tests.c index e1707fbe..f2d4f867 100644 --- a/t/tests.c +++ b/t/tests.c @@ -18,6 +18,7 @@ int all_tests() _verify("md5:", md5_tests); _verify("sha:", sha_tests); _verify("base64:", base64_tests); + _verify("des:", des_tests); return 0; }