des initial permutation done

This commit is contained in:
Gregory 2019-03-06 23:41:59 +02:00
parent de73b25502
commit 941fde0be7
6 changed files with 124 additions and 4 deletions

View file

@ -21,6 +21,7 @@ SRC_DIR := $(ROOT)/src/
MD5_DIR := $(SRC_DIR)/md5/ MD5_DIR := $(SRC_DIR)/md5/
SHA_DIR := $(SRC_DIR)/sha/ SHA_DIR := $(SRC_DIR)/sha/
B64_DIR := $(SRC_DIR)/base64/ B64_DIR := $(SRC_DIR)/base64/
DES_DIR := $(SRC_DIR)/des/
OBJ_DIR := $(ROOT)/obj/ OBJ_DIR := $(ROOT)/obj/
INC_DIR := $(ROOT)/inc/ INC_DIR := $(ROOT)/inc/
LIB_DIR := $(ROOT)/lib/ LIB_DIR := $(ROOT)/lib/
@ -32,10 +33,12 @@ MD5_HEADER := $(INC_DIR)/ft_md5.h
SHA_HEADER := $(INC_DIR)/ft_sha.h SHA_HEADER := $(INC_DIR)/ft_sha.h
SSL_HEADER := $(INC_DIR)/ft_ssl.h SSL_HEADER := $(INC_DIR)/ft_ssl.h
B64_HEADER := $(INC_DIR)/ft_base64.h B64_HEADER := $(INC_DIR)/ft_base64.h
DES_HEADER := $(INC_DIR)/ft_des.h
HEADERS := $(MD5_HEADER) \ HEADERS := $(MD5_HEADER) \
$(SHA_HEADER) \ $(SHA_HEADER) \
$(SSL_HEADER) \ $(SSL_HEADER) \
$(B64_HEADER) $(B64_HEADER) \
$(DES_HEADER)
# libraries # libraries
LIBFT_DIR := $(LIB_DIR)libft/ LIBFT_DIR := $(LIB_DIR)libft/
@ -99,12 +102,15 @@ BASE64_SRC = ft_base64.c \
ft_base64_decode_transform.c \ ft_base64_decode_transform.c \
ft_base64_decode_finish.c ft_base64_decode_finish.c
DES_SRC = ft_des_initial_permutation.c
SRC = main.c \ SRC = main.c \
ft_ssl_usage.c ft_ssl_usage.c
SRC += $(MD5_SRC) \ SRC += $(MD5_SRC) \
$(SHA_SRC) \ $(SHA_SRC) \
$(BASE64_SRC) $(BASE64_SRC) \
$(DES_SRC)
# project object files # project object files
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
@ -123,10 +129,14 @@ SHA_TESTS += $(SHA_SRC)
BASE64_TESTS = base64_tests.c BASE64_TESTS = base64_tests.c
BASE64_TESTS += $(BASE64_SRC) BASE64_TESTS += $(BASE64_SRC)
DES_TESTS = des_tests.c
DES_TESTS += $(DES_SRC)
TEST_SRC = tests.c TEST_SRC = tests.c
TEST_SRC += $(MD5_TESTS) \ TEST_SRC += $(MD5_TESTS) \
$(SHA_TESTS) \ $(SHA_TESTS) \
$(BASE64_TESTS) $(BASE64_TESTS) \
$(DES_TESTS)
TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o))
@ -207,7 +217,6 @@ re: fclean all
multi: multi:
$(MAKE) $(LIBFT) $(MAKE) $(LIBFT)
$(MAKE) check
$(MAKE) $(NAME) $(MAKE) $(NAME)
# special stuff # special stuff
@ -216,6 +225,7 @@ vpath %.c $(SRC_DIR) \
$(MD5_DIR) \ $(MD5_DIR) \
$(SHA_DIR) \ $(SHA_DIR) \
$(B64_DIR) \ $(B64_DIR) \
$(DES_DIR) \
$(TST_DIR) $(TST_DIR)
.PHONY: all check clean fclean re multi .PHONY: all check clean fclean re multi

26
inc/ft_des.h Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -16,5 +16,6 @@
int md5_tests(void); int md5_tests(void);
int sha_tests(void); int sha_tests(void);
int base64_tests(void); int base64_tests(void);
int des_tests(void);
#endif #endif

View file

@ -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++;
}
}

54
t/des_tests.c Normal file
View file

@ -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;
}

View file

@ -18,6 +18,7 @@ int all_tests()
_verify("md5:", md5_tests); _verify("md5:", md5_tests);
_verify("sha:", sha_tests); _verify("sha:", sha_tests);
_verify("base64:", base64_tests); _verify("base64:", base64_tests);
_verify("des:", des_tests);
return 0; return 0;
} }