ecode entire 64 bit block
This commit is contained in:
parent
cbf339b04d
commit
648f54db4a
7 changed files with 204 additions and 8 deletions
4
Makefile
4
Makefile
|
@ -120,7 +120,9 @@ DES_SRC = ft_des_initial_permutation.c \
|
|||
ft_des_encryption_round_key.c \
|
||||
ft_des_rotate_half_key_left.c \
|
||||
ft_des_rotate_half_key_right.c \
|
||||
ft_des_key_permuted_choice_two.c
|
||||
ft_des_key_permuted_choice_two.c \
|
||||
ft_des_encryption_round.c \
|
||||
ft_des_encrypt_block.c
|
||||
|
||||
SRC = main.c \
|
||||
ft_ssl_usage.c
|
||||
|
|
17
inc/ft_des.h
17
inc/ft_des.h
|
@ -22,6 +22,7 @@
|
|||
# define FT_DES_FEISTEL_FUNCTION_KEY_SIZE 48
|
||||
# define FT_DES_INITIAL_KEY_SIZE 64
|
||||
# define FT_DES_REDUCED_KEY_SIZE 56
|
||||
# define FT_DES_ROUND_COUNT 16
|
||||
|
||||
typedef unsigned char t_byte1;
|
||||
|
||||
|
@ -123,7 +124,7 @@ void ft_des_key_permuted_choice_two
|
|||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||
);
|
||||
|
||||
void ft_des_encryption_round_key
|
||||
void ft_des_derive_encryption_round_key
|
||||
(
|
||||
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||
t_byte1 round,
|
||||
|
@ -142,4 +143,18 @@ void ft_des_rotate_half_key_right
|
|||
t_byte1 rotation_number
|
||||
);
|
||||
|
||||
void ft_des_encryption_round
|
||||
(
|
||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||
);
|
||||
|
||||
void ft_des_encrypt_block
|
||||
(
|
||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
|
||||
t_byte1 ciphertext[FT_DES_BIT_BLOCK_SIZE]
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
46
src/des/ft_des_encrypt_block.c
Normal file
46
src/des/ft_des_encrypt_block.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
static void switch_halves
|
||||
(
|
||||
t_byte1 message[FT_DES_BIT_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
int tmp;
|
||||
i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||
{
|
||||
tmp = message[i];
|
||||
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
|
||||
message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_des_encrypt_block
|
||||
(
|
||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
|
||||
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
|
||||
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
||||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
|
||||
int i;
|
||||
|
||||
ft_des_initial_permutation(plaintext, ip_message);
|
||||
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||
i = 1;
|
||||
while(i <= FT_DES_ROUND_COUNT)
|
||||
{
|
||||
ft_des_derive_encryption_round_key(reduced_key, i, round_key);
|
||||
if (i % 2 != 0)
|
||||
ft_des_encryption_round(ip_message, ip_message + 32, round_key);
|
||||
else
|
||||
ft_des_encryption_round(ip_message + 32, ip_message, round_key);
|
||||
i++;
|
||||
}
|
||||
switch_halves(ip_message);
|
||||
ft_des_final_permutation(ip_message, cyphertext);
|
||||
}
|
30
src/des/ft_des_encryption_round.c
Normal file
30
src/des/ft_des_encryption_round.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
static void xor
|
||||
(
|
||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||
t_byte1 f_function_result[FT_DES_BIT_BLOCK_SIZE / 2]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||
{
|
||||
left_half[i] = left_half[i] ^ f_function_result[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_des_encryption_round
|
||||
(
|
||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||
)
|
||||
{
|
||||
t_byte1 feistel_function_result[FT_DES_BIT_BLOCK_SIZE / 2];
|
||||
|
||||
ft_des_feistel_function(right_half, round_key, feistel_function_result);
|
||||
xor(left_half, feistel_function_result);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
void ft_des_encryption_round_key
|
||||
void ft_des_derive_encryption_round_key
|
||||
(
|
||||
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||
t_byte1 round,
|
||||
|
|
|
@ -10,10 +10,10 @@ void ft_des_rotate_half_key_left
|
|||
t_byte1 j;
|
||||
t_byte1 tmp;
|
||||
|
||||
tmp = half[0];
|
||||
i = 0;
|
||||
while(i < rotation_number)
|
||||
{
|
||||
tmp = half[0];
|
||||
j = 0;
|
||||
while(j < FT_DES_REDUCED_KEY_SIZE / 2 - 1)
|
||||
{
|
||||
|
|
111
t/des_tests.c
111
t/des_tests.c
|
@ -424,13 +424,18 @@ int rotate_half_key()
|
|||
0, 1, 0, 1, 1, 1, 1,
|
||||
};
|
||||
|
||||
t_byte1 expected[FT_DES_REDUCED_KEY_SIZE / 2];
|
||||
t_byte1 expected[FT_DES_REDUCED_KEY_SIZE / 2] = {
|
||||
1, 1, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 1, 1, 0, 0,
|
||||
1, 0, 1, 0, 1, 0, 1,
|
||||
0, 1, 1, 1, 1, 1, 1,
|
||||
};
|
||||
|
||||
ft_memcpy(expected, half_key, FT_DES_REDUCED_KEY_SIZE / 2);
|
||||
// ft_memcpy(expected, half_key, FT_DES_REDUCED_KEY_SIZE / 2);
|
||||
|
||||
ft_des_rotate_half_key_left(half_key, 1);
|
||||
ft_des_rotate_half_key_left(half_key, 1);
|
||||
ft_des_rotate_half_key_right(half_key, 2);
|
||||
// ft_des_rotate_half_key_right(half_key, 2);
|
||||
|
||||
int i = 0;
|
||||
|
||||
|
@ -479,7 +484,7 @@ int derive_round_key()
|
|||
|
||||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
|
||||
|
||||
ft_des_encryption_round_key(reduced_key, 1, round_key);
|
||||
ft_des_derive_encryption_round_key(reduced_key, 1, round_key);
|
||||
|
||||
int i;
|
||||
|
||||
|
@ -499,6 +504,102 @@ int derive_round_key()
|
|||
_end("should derive round key");
|
||||
}
|
||||
|
||||
int perform_encryption_round()
|
||||
{
|
||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
|
||||
0, 0, 1, 1, 1, 0, 1, 1,
|
||||
0, 0, 1, 1, 1, 0, 0, 0,
|
||||
1, 0, 0, 1, 1, 0, 0, 0,
|
||||
0, 0, 1, 1, 0, 1, 1, 1,
|
||||
0, 0, 0, 1, 0, 1, 0, 1,
|
||||
0, 0, 1, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 0, 1, 1, 1,
|
||||
0, 1, 0, 1, 1, 1, 1, 0,
|
||||
};
|
||||
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
||||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
|
||||
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 0, 0,
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 0, 0,
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
};
|
||||
t_byte1 initial_permuatation[FT_DES_BIT_BLOCK_SIZE];
|
||||
t_byte1 expected_message[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
0, 0, 1, 1, 0, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 0, 0, 0, 1, 0,
|
||||
1, 1, 0, 1, 1, 1, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 1, 0, 0, 1, 0,
|
||||
};
|
||||
|
||||
ft_des_initial_permutation(message, initial_permuatation);
|
||||
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||
ft_des_derive_encryption_round_key(reduced_key, 1, round_key);
|
||||
ft_des_encryption_round(initial_permuatation,
|
||||
initial_permuatation + 32, round_key);
|
||||
|
||||
int i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
_is(initial_permuatation[i] == expected_message[i]);
|
||||
i++;
|
||||
}
|
||||
_end("should perform encryption round");
|
||||
}
|
||||
|
||||
int encrypt_block()
|
||||
{
|
||||
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 0, 0,
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 0, 0,
|
||||
0, 1, 1, 0, 0, 0, 0, 1,
|
||||
0, 1, 1, 1, 0, 0, 1, 1,
|
||||
};
|
||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
|
||||
0, 0, 1, 1, 1, 0, 1, 1,
|
||||
0, 0, 1, 1, 1, 0, 0, 0,
|
||||
1, 0, 0, 1, 1, 0, 0, 0,
|
||||
0, 0, 1, 1, 0, 1, 1, 1,
|
||||
0, 0, 0, 1, 0, 1, 0, 1,
|
||||
0, 0, 1, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 0, 1, 1, 1,
|
||||
0, 1, 0, 1, 1, 1, 1, 0,
|
||||
};
|
||||
t_byte1 expected_cypertext[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
0, 0, 0, 0, 0, 1, 1, 0,
|
||||
1, 0, 1, 0, 1, 1, 1, 1,
|
||||
0, 1, 1, 0, 0, 0, 1, 1,
|
||||
1, 1, 0, 1, 0, 0, 0, 0,
|
||||
0, 1, 0, 0, 0, 1, 1, 1,
|
||||
1, 0, 0, 1, 1, 1, 1, 0,
|
||||
1, 0, 1, 0, 1, 1, 1, 1,
|
||||
0, 0, 0, 1, 1, 1, 0, 0,
|
||||
};
|
||||
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE];
|
||||
|
||||
ft_des_encrypt_block(message, key, cyphertext);
|
||||
|
||||
int i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
_is(cyphertext[i] == expected_cypertext[i]);
|
||||
i++;
|
||||
}
|
||||
_end("should encrypt block");
|
||||
}
|
||||
|
||||
int des_tests()
|
||||
{
|
||||
_should(perform_initial_permutation);
|
||||
|
@ -511,5 +612,7 @@ int des_tests()
|
|||
_should(reduce_key_to_56_bits);
|
||||
_should(rotate_half_key);
|
||||
_should(derive_round_key);
|
||||
_should(perform_encryption_round);
|
||||
_should(encrypt_block);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue