generate round keys beforehand

This commit is contained in:
Gregory 2019-03-13 18:28:15 +02:00
parent 648f54db4a
commit c77c663976
5 changed files with 34 additions and 10 deletions

View file

@ -122,7 +122,8 @@ DES_SRC = ft_des_initial_permutation.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_encryption_round.c \
ft_des_encrypt_block.c ft_des_encrypt_block.c \
ft_des_generate_round_keys.c
SRC = main.c \ SRC = main.c \
ft_ssl_usage.c ft_ssl_usage.c

View file

@ -131,6 +131,12 @@ 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
(
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_rotate_half_key_left void ft_des_rotate_half_key_left
( (
t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2], t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2],
@ -153,7 +159,7 @@ void ft_des_encryption_round
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],
t_byte1 key[FT_DES_INITIAL_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 ciphertext[FT_DES_BIT_BLOCK_SIZE] t_byte1 ciphertext[FT_DES_BIT_BLOCK_SIZE]
); );

View file

@ -20,25 +20,21 @@ static void switch_halves
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],
t_byte1 key[FT_DES_INITIAL_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE] t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE]
) )
{ {
t_byte1 ip_message[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; int i;
ft_des_initial_permutation(plaintext, ip_message); ft_des_initial_permutation(plaintext, ip_message);
ft_des_key_permuted_choice_one(key, reduced_key);
i = 1; i = 1;
while(i <= FT_DES_ROUND_COUNT) while(i <= FT_DES_ROUND_COUNT)
{ {
ft_des_derive_encryption_round_key(reduced_key, i, round_key);
if (i % 2 != 0) if (i % 2 != 0)
ft_des_encryption_round(ip_message, ip_message + 32, round_key); ft_des_encryption_round(ip_message, ip_message + 32, keys[i - 1]);
else else
ft_des_encryption_round(ip_message + 32, ip_message, round_key); ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]);
i++; i++;
} }
switch_halves(ip_message); switch_halves(ip_message);

View file

@ -0,0 +1,17 @@
#include "ft_des.h"
void ft_des_generate_round_keys
(
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
)
{
int i;
i = 1;
while(i <= FT_DES_ROUND_COUNT)
{
ft_des_derive_encryption_round_key(key, i, round_keys[i - 1]);
i++;
}
}

View file

@ -587,9 +587,13 @@ int encrypt_block()
1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 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 cyphertext[FT_DES_BIT_BLOCK_SIZE]; t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE];
ft_des_encrypt_block(message, key, cyphertext); ft_des_key_permuted_choice_one(key, reduced_key);
ft_des_generate_round_keys(reduced_key, round_keys);
ft_des_encrypt_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)