decrypt block
This commit is contained in:
parent
c77c663976
commit
6a3af7423e
12 changed files with 178 additions and 31 deletions
10
Makefile
10
Makefile
|
@ -117,13 +117,15 @@ DES_SRC = ft_des_initial_permutation.c \
|
||||||
ft_des_feistel_function_permutation.c \
|
ft_des_feistel_function_permutation.c \
|
||||||
ft_des_feistel_function.c \
|
ft_des_feistel_function.c \
|
||||||
ft_des_key_permuted_choice_one.c \
|
ft_des_key_permuted_choice_one.c \
|
||||||
ft_des_encryption_round_key.c \
|
ft_des_derive_encryption_round_key.c \
|
||||||
|
ft_des_derive_decryption_round_key.c \
|
||||||
ft_des_rotate_half_key_left.c \
|
ft_des_rotate_half_key_left.c \
|
||||||
ft_des_rotate_half_key_right.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_round.c \
|
||||||
ft_des_encrypt_block.c \
|
ft_des_process_block.c \
|
||||||
ft_des_generate_round_keys.c
|
ft_des_generate_encryption_round_keys.c \
|
||||||
|
ft_des_generate_decryption_round_keys.c \
|
||||||
|
|
||||||
SRC = main.c \
|
SRC = main.c \
|
||||||
ft_ssl_usage.c
|
ft_ssl_usage.c
|
||||||
|
|
19
inc/ft_des.h
19
inc/ft_des.h
|
@ -131,7 +131,20 @@ void ft_des_derive_encryption_round_key
|
||||||
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_generate_round_keys
|
void ft_des_derive_decryption_round_key
|
||||||
|
(
|
||||||
|
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||||
|
t_byte1 round,
|
||||||
|
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
|
);
|
||||||
|
|
||||||
|
void ft_des_generate_encryption_round_keys
|
||||||
|
(
|
||||||
|
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||||
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
|
);
|
||||||
|
|
||||||
|
void ft_des_generate_decryption_round_keys
|
||||||
(
|
(
|
||||||
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
|
@ -149,14 +162,14 @@ void ft_des_rotate_half_key_right
|
||||||
t_byte1 rotation_number
|
t_byte1 rotation_number
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_encryption_round
|
void ft_des_round
|
||||||
(
|
(
|
||||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 right_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]
|
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_encrypt_block
|
void ft_des_process_block
|
||||||
(
|
(
|
||||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
|
||||||
|
|
|
@ -1,22 +1,5 @@
|
||||||
#include "ft_des.h"
|
#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
|
void ft_des_encrypt_block
|
||||||
(
|
(
|
||||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||||
|
@ -37,6 +20,6 @@ void ft_des_encrypt_block
|
||||||
ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]);
|
ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
switch_halves(ip_message);
|
ft_des_switch_message_halves(ip_message);
|
||||||
ft_des_final_permutation(ip_message, cyphertext);
|
ft_des_final_permutation(ip_message, cyphertext);
|
||||||
}
|
}
|
21
src/des/ft_des_derive_decryption_round_key.c
Normal file
21
src/des/ft_des_derive_decryption_round_key.c
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#include "ft_des.h"
|
||||||
|
|
||||||
|
void ft_des_derive_decryption_round_key
|
||||||
|
(
|
||||||
|
t_byte1 key[FT_DES_REDUCED_KEY_SIZE],
|
||||||
|
t_byte1 round,
|
||||||
|
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (round == 2 || round == 9 || round == 16)
|
||||||
|
{
|
||||||
|
ft_des_rotate_half_key_right(key, 1);
|
||||||
|
ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 1);
|
||||||
|
}
|
||||||
|
else if (round != 1)
|
||||||
|
{
|
||||||
|
ft_des_rotate_half_key_right(key, 2);
|
||||||
|
ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 2);
|
||||||
|
}
|
||||||
|
ft_des_key_permuted_choice_two(key, round_key);
|
||||||
|
}
|
17
src/des/ft_des_generate_decryption_round_keys.c
Normal file
17
src/des/ft_des_generate_decryption_round_keys.c
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#include "ft_des.h"
|
||||||
|
|
||||||
|
void ft_des_generate_decryption_round_keys
|
||||||
|
(
|
||||||
|
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
|
||||||
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
while(i <= FT_DES_ROUND_COUNT)
|
||||||
|
{
|
||||||
|
ft_des_derive_decryption_round_key(input_key, i, keys[i - 1]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_generate_round_keys
|
void ft_des_generate_encryption_round_keys
|
||||||
(
|
(
|
||||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
|
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
|
||||||
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
|
42
src/des/ft_des_process_block.c
Normal file
42
src/des/ft_des_process_block.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#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_process_block
|
||||||
|
(
|
||||||
|
t_byte1 input[FT_DES_BIT_BLOCK_SIZE],
|
||||||
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
|
||||||
|
t_byte1 output[FT_DES_BIT_BLOCK_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
ft_des_initial_permutation(input, ip_message);
|
||||||
|
i = 1;
|
||||||
|
while(i <= FT_DES_ROUND_COUNT)
|
||||||
|
{
|
||||||
|
if (i % 2 != 0)
|
||||||
|
ft_des_round(ip_message, ip_message + 32, keys[i - 1]);
|
||||||
|
else
|
||||||
|
ft_des_round(ip_message + 32, ip_message, keys[i - 1]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
switch_halves(ip_message);
|
||||||
|
ft_des_final_permutation(ip_message, output);
|
||||||
|
}
|
|
@ -10,10 +10,10 @@ void ft_des_rotate_half_key_right
|
||||||
t_byte1 j;
|
t_byte1 j;
|
||||||
t_byte1 tmp;
|
t_byte1 tmp;
|
||||||
|
|
||||||
tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1];
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < rotation_number)
|
while(i < rotation_number)
|
||||||
{
|
{
|
||||||
|
tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1];
|
||||||
j = FT_DES_REDUCED_KEY_SIZE / 2 - 1;
|
j = FT_DES_REDUCED_KEY_SIZE / 2 - 1;
|
||||||
while(j > 0)
|
while(j > 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,7 +16,7 @@ static void xor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_des_encryption_round
|
void ft_des_round
|
||||||
(
|
(
|
||||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
19
src/des/ft_des_switch_message_halves.c
Normal file
19
src/des/ft_des_switch_message_halves.c
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#include "ft_des.h"
|
||||||
|
|
||||||
|
void ft_des_switch_message_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++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -543,7 +543,7 @@ int perform_encryption_round()
|
||||||
ft_des_initial_permutation(message, initial_permuatation);
|
ft_des_initial_permutation(message, initial_permuatation);
|
||||||
ft_des_key_permuted_choice_one(key, reduced_key);
|
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||||
ft_des_derive_encryption_round_key(reduced_key, 1, round_key);
|
ft_des_derive_encryption_round_key(reduced_key, 1, round_key);
|
||||||
ft_des_encryption_round(initial_permuatation,
|
ft_des_round(initial_permuatation,
|
||||||
initial_permuatation + 32, round_key);
|
initial_permuatation + 32, round_key);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -592,8 +592,8 @@ int encrypt_block()
|
||||||
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE];
|
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE];
|
||||||
|
|
||||||
ft_des_key_permuted_choice_one(key, reduced_key);
|
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||||
ft_des_generate_round_keys(reduced_key, round_keys);
|
ft_des_generate_encryption_round_keys(reduced_key, round_keys);
|
||||||
ft_des_encrypt_block(message, round_keys, cyphertext);
|
ft_des_process_block(message, round_keys, cyphertext);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
|
@ -604,6 +604,55 @@ int encrypt_block()
|
||||||
_end("should encrypt block");
|
_end("should encrypt block");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int decrypt_block()
|
||||||
|
{
|
||||||
|
t_byte1 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 expected_plaintext[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 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
||||||
|
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
|
||||||
|
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE];
|
||||||
|
|
||||||
|
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||||
|
ft_des_generate_decryption_round_keys(reduced_key, round_keys);
|
||||||
|
ft_des_process_block(cypertext, round_keys, plaintext);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
|
{
|
||||||
|
_is(plaintext[i] == expected_plaintext[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
_end("should decrypt block");
|
||||||
|
}
|
||||||
|
|
||||||
int des_tests()
|
int des_tests()
|
||||||
{
|
{
|
||||||
_should(perform_initial_permutation);
|
_should(perform_initial_permutation);
|
||||||
|
@ -618,5 +667,6 @@ int des_tests()
|
||||||
_should(derive_round_key);
|
_should(derive_round_key);
|
||||||
_should(perform_encryption_round);
|
_should(perform_encryption_round);
|
||||||
_should(encrypt_block);
|
_should(encrypt_block);
|
||||||
|
_should(decrypt_block);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue