complete feistel function

This commit is contained in:
Gregory 2019-03-11 00:22:54 +02:00
parent ffeb3f4ccf
commit 301de3be7b
14 changed files with 517 additions and 2 deletions

View file

@ -104,7 +104,18 @@ BASE64_SRC = ft_base64.c \
DES_SRC = ft_des_initial_permutation.c \
ft_des_final_permutation.c \
ft_des_expansion_box.c
ft_des_expansion_box.c \
ft_des_s_box.c \
ft_des_s_box_1.c \
ft_des_s_box_2.c \
ft_des_s_box_3.c \
ft_des_s_box_4.c \
ft_des_s_box_5.c \
ft_des_s_box_6.c \
ft_des_s_box_7.c \
ft_des_s_box_8.c \
ft_des_feistel_function_permutation.c \
ft_des_feistel_function.c
SRC = main.c \
ft_ssl_usage.c

View file

@ -15,7 +15,11 @@
# define FT_DES_BIT_BLOCK_SIZE 64
# define FT_DES_EXPANDED_HALF_BLOCK_SIZE 48
# define FT_DES_S_BOX_SIZE 6
# define FT_DES_S_BOX_INPUT_SIZE 6
# define FT_DES_S_BOX_OUTPUT_SIZE 4
# define FT_DES_S_BOX_TABLE_ROWS 4
# define FT_DES_S_BOX_TABLE_COLUMNS 16
# define FT_DES_FEISTEL_FUNCTION_KEY_SIZE 48
typedef unsigned char t_byte1;
@ -37,4 +41,72 @@ void ft_des_expansion_box
t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
);
void ft_des_s_box
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE],
t_byte1 table[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS]
);
void ft_des_s_box_1
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_2
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_3
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_4
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_5
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_6
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_7
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_8
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_feistel_function_permutation
(
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
);
void ft_des_feistel_function
(
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
);
#endif

View file

@ -0,0 +1,39 @@
#include "ft_des.h"
static void merge_key
(
t_byte1 expanded_half[FT_DES_EXPANDED_HALF_BLOCK_SIZE],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 output[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
)
{
int i = 0;
while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
{
output[i] = expanded_half[i] ^ key[i];
i++;
}
}
void ft_des_feistel_function
(
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
)
{
t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE];
t_byte1 all_s_boxes[FT_DES_BIT_BLOCK_SIZE / 2];
ft_des_expansion_box(right_half, expanded);
merge_key(expanded, key, expanded);
ft_des_s_box_1(expanded, all_s_boxes);
ft_des_s_box_2(expanded + 6, all_s_boxes + 4);
ft_des_s_box_3(expanded + 12, all_s_boxes + 8);
ft_des_s_box_4(expanded + 18, all_s_boxes + 12);
ft_des_s_box_5(expanded + 24, all_s_boxes + 16);
ft_des_s_box_6(expanded + 30, all_s_boxes + 20);
ft_des_s_box_7(expanded + 36, all_s_boxes + 24);
ft_des_s_box_8(expanded + 42, all_s_boxes + 28);
ft_des_feistel_function_permutation(all_s_boxes, output);
}

View file

@ -0,0 +1,24 @@
#include "ft_des.h"
void ft_des_feistel_function_permutation
(
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
)
{
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE / 2] = {
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25,
};
int i;
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
output[i] = input[table[i] - 1];
i++;
}
}

21
src/des/ft_des_s_box.c Normal file
View file

@ -0,0 +1,21 @@
#include "ft_des.h"
void ft_des_s_box
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE],
t_byte1 table[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS]
)
{
t_byte1 row;
t_byte1 column;
t_byte1 selected;
row = input[0] << 1 | input[5];
column = input[1] << 3 | input[2] << 2 | input[3] << 1 | input[4];
selected = table[row][column];
output[0] = selected >> 3 & 1;
output[1] = selected >> 2 & 1;
output[2] = selected >> 1 & 1;
output[3] = selected & 1;
}

16
src/des/ft_des_s_box_1.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_1
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_2.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_2
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
{13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_3.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_3
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
{13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
{13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_4.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_4
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
{13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
{10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_5.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_5
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
{14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
{11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_6.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_6
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
{10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_7.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_7
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
{13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
};
ft_des_s_box(input, output, tbl);
}

16
src/des/ft_des_s_box_8.c Normal file
View file

@ -0,0 +1,16 @@
#include "ft_des.h"
void ft_des_s_box_8
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 03, 5, 8},
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -3,6 +3,8 @@
#include "ft_des.h"
#include "libft.h"
#define S_BOX_CASES_NUMBER 3
int perform_initial_permutation()
{
// all 64 bits:
@ -159,7 +161,222 @@ int perform_expansion_in_feistel_function()
_end("perform expansion in feistel function");
}
static int s_box_check
(
t_byte1 inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE],
t_byte1 expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE],
void (*box)(t_byte1 in[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 out[FT_DES_S_BOX_OUTPUT_SIZE])
)
{
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE];
int j = 0;
while(j < S_BOX_CASES_NUMBER)
{
box(inputs[j], output);
int i = 0;
while(i < FT_DES_S_BOX_OUTPUT_SIZE)
{
if(output[i] != expected[j][i])
return 0;
i++;
}
j++;
}
return 1;
}
int s_boxes_confuse()
{
t_byte1 s1_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{1, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0},
};
t_byte1 s1_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{1, 0, 0, 0},
{1, 1, 1, 0},
{0, 1, 1, 1},
};
_is(s_box_check(s1_inputs, s1_expected, ft_des_s_box_1));
t_byte1 s2_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0},
};
t_byte1 s2_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{1, 1, 1, 1},
{1, 1, 0, 1},
{0, 1, 0, 1},
};
_is(s_box_check(s2_inputs, s2_expected, ft_des_s_box_2));
t_byte1 s3_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0},
};
t_byte1 s3_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{1, 0, 1, 0},
{0, 0, 0, 1},
{1, 0, 1, 1},
};
_is(s_box_check(s3_inputs, s3_expected, ft_des_s_box_3));
t_byte1 s4_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0},
};
t_byte1 s4_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{0, 1, 1, 1},
{0, 0, 1, 1},
{1, 1, 1, 1},
};
_is(s_box_check(s4_inputs, s4_expected, ft_des_s_box_4));
t_byte1 s5_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1},
};
t_byte1 s5_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{0, 0, 1, 0},
{1, 0, 1, 1},
{0, 0, 1, 1},
};
_is(s_box_check(s5_inputs, s5_expected, ft_des_s_box_5));
t_byte1 s6_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1},
};
t_byte1 s6_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 1},
};
_is(s_box_check(s6_inputs, s6_expected, ft_des_s_box_6));
t_byte1 s7_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1},
};
t_byte1 s7_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{0, 1, 0, 0},
{0, 1, 1, 0},
{1, 1, 0, 0},
};
_is(s_box_check(s7_inputs, s7_expected, ft_des_s_box_7));
t_byte1 s8_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1},
};
t_byte1 s8_expected[S_BOX_CASES_NUMBER][FT_DES_S_BOX_OUTPUT_SIZE] = {
{1, 1, 0, 1},
{0, 0, 1, 0},
{1, 0, 1, 1},
};
_is(s_box_check(s8_inputs, s8_expected, ft_des_s_box_8));
_end("s boxes confuse");
}
int perform_premutation_in_feistel_function()
{
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2] = {
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
};
t_byte1 expected[FT_DES_BIT_BLOCK_SIZE / 2] = {
1, 1, 0, 1, 0, 1, 1, 1,
1, 0, 1, 1, 1, 0, 0, 1,
0, 0, 0, 1, 1, 0, 1, 0,
1, 0, 1, 0, 0, 0, 0, 0,
};
t_byte1 actual[FT_DES_BIT_BLOCK_SIZE / 2];
ft_des_feistel_function_permutation(input, actual);
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
_is(actual[i] == expected[i]);
i++;
}
_end("should perform permutation in feistel function");
}
int perform_feistel_function()
{
t_byte1 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 right_half[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,
};
t_byte1 expected[FT_DES_BIT_BLOCK_SIZE / 2] = {
0, 0, 1, 0,
0, 0, 1, 1,
0, 1, 0, 0,
1, 0, 1, 0,
1, 0, 1, 0,
1, 0, 0, 1,
1, 0, 1, 1,
1, 0, 1, 1,
};
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2];
ft_des_feistel_function(right_half, key, output);
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
_is(output[i] == expected[i]);
i++;
}
_end("feistel function should encode half of a block");
}
int des_tests()
{
@ -167,5 +384,8 @@ int des_tests()
_should(perform_final_permutation);
_should(final_permutation_is_reverse_of_initial);
_should(perform_expansion_in_feistel_function);
_should(s_boxes_confuse);
_should(perform_premutation_in_feistel_function);
_should(perform_feistel_function);
return 0;
}