Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/03/15 11:20:25 by cpieri #+# #+# #
# Updated: 2020/02/04 10:12:31 by cpieri ### ########.fr #
# Updated: 2020/04/29 11:50:04 by cpieri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -82,6 +82,8 @@ SRC_NAME= main.c \
symmetric/symmetric_parsing_pt_f.c \
symmetric/symmetric_parsing_pt_f_2.c \
symmetric/des/des.c \
symmetric/des/des_tools.c \
symmetric/tools/des_key_schedule.c \

OBJ_NAME= $(SRC_NAME:.c=.o)

Expand Down
15 changes: 13 additions & 2 deletions includes/symmetric/des/des.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/06 10:13:19 by cpieri #+# #+# */
/* Updated: 2019/05/20 08:09:22 by cpieri ### ########.fr */
/* Updated: 2020/05/05 12:10:40 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,17 @@
# include "../../../libft/include/libft.h"
# include "../../structure.h"

void *des(void *opt, size_t len_opt);
typedef struct s_des_key
{
uint8_t *key_left;
uint8_t *key_right;
size_t len_key_left;
size_t len_key_right;
} t_des_key;

void *des(void *opt, size_t len_opt);
uint8_t *des_permute(uint8_t *to_perm, const int *arr, size_t new_len);
t_des_key des_key_schedule(void *key, size_t key_len, int i);
uint32_t *des_key_schedule_32(void *key, size_t key_len, int i);

#endif
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2017/11/07 18:38:31 by tmilon #+# #+# #
# Updated: 2020/01/06 12:49:31 by cpieri ### ########.fr #
# Updated: 2020/04/27 14:19:05 by cpieri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -124,6 +124,7 @@ SRCS = ft_atoi.c \
ft_atol.c \
ft_strbits.c \
ft_membits.c \
ft_membit_pos.c \
ft_left_shift.c \
ft_left_rotate.c \
ft_right_rotate.c \
Expand Down
3 changes: 2 additions & 1 deletion libft/include/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 16:47:24 by cpieri #+# #+# */
/* Updated: 2020/01/06 12:53:34 by cpieri ### ########.fr */
/* Updated: 2020/04/27 14:18:20 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -83,6 +83,7 @@ void ft_bzero(void *s, size_t n);
void ft_memdel(void **ap);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void ft_membits(void *s, size_t len, size_t size);
void ft_membit_pos(void *s, size_t len, size_t size);
void ft_memput(void *s, size_t len);

/*
Expand Down
66 changes: 66 additions & 0 deletions libft/srcs/ft_membit_pos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_membit_pos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/27 14:07:35 by cpieri #+# #+# */
/* Updated: 2020/04/27 16:23:33 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

static void ft_putbits_pos(unsigned char octet)
{
int z;
int oct;

oct = octet;
z = 128;
while (z > 0)
{
ft_putstr("|");
if (oct & z)
ft_putchar('1');
else
ft_putchar('0');
z >>= 1;
}
}

void ft_membit_pos(void *s, size_t len, size_t size)
{
size_t i;
size_t y;
unsigned char *data;
unsigned char b;

data = (unsigned char*)s;
i = 0;
ft_putstr("Position: ");
while (i < 64)
{
ft_putstr("|");
ft_putnbr((i + 1) % 8);
if ((i + 1) % 8 == 0)
ft_putstr("| - ");
i++;
}
ft_putendl("");
ft_putstr("Bits : ");
i = 0;
while (i < (len * size))
{
y = 0;
while (y < size)
{
b = data[i] >> ((size - y - 1) * 8);
ft_putbits_pos(data[i++]);
y++;
}
ft_putstr("| - ");
}
ft_putchar('\n');
}
50 changes: 48 additions & 2 deletions srcs/symmetric/des/des.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,61 @@
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/06 10:05:42 by cpieri #+# #+# */
/* Updated: 2019/05/20 08:09:40 by cpieri ### ########.fr */
/* Updated: 2020/05/05 12:22:48 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

#include "symmetric/des/des.h"
#include "symmetric/symmetric.h"

void *des(void *opt, size_t len_opt)
static const int g_des_ip[64] = {
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
};

static const int g_des_cycle[48] = {
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
};

static void des_cycle(uint8_t *block, size_t len_block, void *key, int dk_l)
{
uint32_t part_1;
uint32_t part_2;
uint32_t *key_32;
int i;
uint8_t *permute;

(void)(len_block);
i = 0;
part_1 = ((uint32_t*)block)[0];
part_2 = ((uint32_t*)block)[1];
while (i < 16)
{
permute = des_permute(((uint8_t*)(&part_2)), g_des_cycle, 48);
key_32 = des_key_schedule_32(key, dk_l, i);
i++;
}
}

void *des(void *opt, size_t len_opt)
{
t_data *data;
uint8_t *block_permuted;
size_t offest;
// t_evp *evp_data;

(void)len_opt;
offest = 0;
data = ((t_opt*)opt)->data;
while (offest < data->len_data)
{
block_permuted = des_permute((uint8_t*)(data + offest), g_des_ip, 64);
des_cycle(block_permuted, 8, ((t_evp*)data->pass)->key, ((t_evp*)data->pass)->dk_len);
offest += 8;
}
return (opt);
}
30 changes: 30 additions & 0 deletions srcs/symmetric/des/des_tools.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* des_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/29 11:38:58 by cpieri #+# #+# */
/* Updated: 2020/04/29 11:50:26 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

#include "symmetric/des/des.h"

uint8_t *des_permute(uint8_t *to_perm, const int *arr, size_t new_len)
{
uint8_t *result;
size_t i;

i = 0;
if (!(result = (uint8_t*)ft_memalloc(sizeof(uint8_t) * (new_len / 8))))
return (NULL);
while (i < new_len)
{
if ((to_perm[(arr[i] - 1) / 8] >> (7 - ((arr[i] - 1) % 8))) & 1)
result[i / 8] |= (1 << (7 - (i % 8)));
i++;
}
return (result);
}
File renamed without changes.
15 changes: 15 additions & 0 deletions srcs/symmetric/encryption_mode/ecb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ebc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/22 10:37:44 by cpieri #+# #+# */
/* Updated: 2020/04/22 10:38:01 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

#include "symmetric/symmetric.h"

void *ebc(void *msg, void *key, size_t msg_len, size_t key_len);
70 changes: 70 additions & 0 deletions srcs/symmetric/tools/des_key_schedule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* des_key_schedule.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpieri <cpieri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/22 11:08:06 by cpieri #+# #+# */
/* Updated: 2020/05/05 12:21:56 by cpieri ### ########.fr */
/* */
/* ************************************************************************** */

#include "symmetric/des/des.h"

static const int g_des_key_r[28] = {
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
};

static const int g_des_key_l[28] = {
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
};

static const int g_des_key_left_shift[16] = {
1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
};

static const int g_des_key_32[64] = {
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
};

t_des_key des_key_schedule(void *key, size_t key_len, int i)
{
t_des_key keys;
uint32_t left_tmp;
uint32_t right_tmp;
int nb;

(void)key_len;
nb = g_des_key_left_shift[i];
keys.key_left = des_permute(key, g_des_key_l, 28);
keys.key_right = des_permute(key, g_des_key_r, 28);
left_tmp = left_shift(((uint32_t*)keys.key_left)[0], nb);
right_tmp = left_shift(((uint32_t*)keys.key_right)[0], nb);
return (keys);
}

uint32_t *des_key_schedule_32(void *key, size_t key_len, int i)
{
t_des_key keys;
uint32_t left_tmp;
uint32_t right_tmp;
uint32_t *new_key;
int nb;

(void)key_len;
nb = g_des_key_left_shift[i];
keys.key_left = des_permute(key, g_des_key_l, 28);
keys.key_right = des_permute(key, g_des_key_r, 28);
left_tmp = left_shift(((uint32_t*)keys.key_left)[0], nb);
right_tmp = left_shift(((uint32_t*)keys.key_right)[0], nb);
new_key = (uint32_t*)ft_memalloc(sizeof(uint32_t) * 2);
new_key[0] = left_tmp;
new_key[1] = right_tmp;
new_key = (uint32_t*)des_permute((uint8_t*)new_key, g_des_key_32, 32);
return (new_key);
}