encode arbitraty message with key as argument

This commit is contained in:
Gregory 2019-03-17 18:21:22 +02:00
parent 9e9e697cdc
commit 819257f21f
8 changed files with 172 additions and 24 deletions

View file

@ -131,7 +131,9 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_print_error.c \
ft_des_arg_parsers.c \
ft_des_ecb_process.c \
ft_des_ecb_process_chunk.c
ft_des_ecb_process_chunk.c \
ft_des_hex_to_bit_key.c \
ft_des_ecb_finish_process.c
SRC = main.c \
ft_ssl_usage.c

View file

@ -27,6 +27,7 @@
# define FT_DES_REDUCED_KEY_SIZE 56
# define FT_DES_ROUND_COUNT 16
# define FT_DES_READ_SIZE 1024
# define FT_DES_MAX_HEX_KEY 16
typedef uint64_t t_byte8;
typedef unsigned char t_byte1;
@ -321,4 +322,10 @@ void ft_des_generete_key_from_hex
t_byte1 initla_key[FT_DES_INITIAL_KEY_SIZE]
);
const char *ft_des_hex_to_bit_key
(
const char *hex_key,
t_byte1 bits_key[FT_DES_INITIAL_KEY_SIZE]
);
#endif

View file

@ -1,4 +1,5 @@
#include "ft_des.h"
#include <stddef.h>
int ft_des_key_arg_parser
(
@ -8,26 +9,12 @@ int ft_des_key_arg_parser
t_des_ctx *ctx
)
{
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
};
int i;
(void)argv;
const char *wrong_key_char;
if (position + 1 >= argc)
ft_des_print_error("there is no key after -k flag. type -h for help.");
i = 0;
while (i < FT_DES_INITIAL_KEY_SIZE)
{
ctx->key[i] = key[i];
i++;
}
wrong_key_char = ft_des_hex_to_bit_key(argv[position + 1], ctx->key);
if (wrong_key_char != NULL)
ft_des_print_error("there wrong char in key string.");
return (position + 2);
}

View file

@ -0,0 +1,23 @@
#include "ft_des.h"
#include <unistd.h>
void ft_des_ecb_finish_process
(
t_des_ctx *ctx
)
{
t_byte1 buffer_index;
t_byte1 padding_size;
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE;
padding_size = FT_DES_BYTE_BLOCK_SIZE - buffer_index;
while(buffer_index < FT_DES_BYTE_BLOCK_SIZE)
{
ctx->buffer[buffer_index] = padding_size;
buffer_index++;
}
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}

View file

@ -12,5 +12,5 @@ void ft_des_ecb_process
ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys);
while((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0)
ft_des_ecb_process_chunk(ctx, readed, buffer);
// ft_des_ecb_finish_process(ctx);
ft_des_ecb_finish_process(ctx);
}

View file

@ -4,11 +4,12 @@
static void ft_des_ecb_write
(
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE],
t_des_ctx *ctx
)
{
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
ft_des_process_block(buffer, ctx->round_keys, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}
@ -29,11 +30,11 @@ void ft_des_ecb_process_chunk
if (readed >= free_space_in_buffer)
{
ft_memcpy(&ctx->buffer[buffer_index], buffer, free_space_in_buffer);
ft_des_ecb_write(ctx);
ft_des_ecb_write(ctx->buffer, ctx);
idx = free_space_in_buffer;
while(idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
{
ft_des_ecb_write(ctx);
ft_des_ecb_write(&buffer[idx], ctx);
idx += FT_DES_BYTE_BLOCK_SIZE;
}
buffer_index = 0;

View file

@ -0,0 +1,30 @@
#include "ft_des.h"
#include "libft.h"
const char *ft_des_hex_to_bit_key
(
const char *hex_key,
t_byte1 bit_key[FT_DES_INITIAL_KEY_SIZE]
)
{
t_byte1 bits4;
int i;
i = 0;
while(*hex_key && i < FT_DES_INITIAL_KEY_SIZE)
{
bits4 = ft_tolower(*hex_key);
if (bits4 >= '0' && bits4 <= '9')
bits4 = bits4 - '0';
else if (bits4 >= 'a' && bits4 <= 'f')
bits4 = bits4 - 'a' + 10;
else
return (hex_key);
bit_key[i] = bits4 >> 3 & 1;
bit_key[i + 1] = bits4 >> 2 & 1;
bit_key[i + 2] = bits4 >> 1 & 1;
bit_key[i + 3] = bits4 & 1;
i += 4;
hex_key++;
}
return (NULL);
}

View file

@ -662,6 +662,101 @@ int init_ctx()
_end("shoud init ctx");
}
int convert_hex_string_to_bits()
{
t_byte1 expected_key[FT_DES_INITIAL_KEY_SIZE] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
};
t_byte1 actual_key[FT_DES_INITIAL_KEY_SIZE];
const char *wrong_key_char;
int i;
wrong_key_char = ft_des_hex_to_bit_key("FFFFFFFFFFFFFFFF", actual_key);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_is(expected_key[i] == actual_key[i]);
i++;
}
wrong_key_char = ft_des_hex_to_bit_key("ffffffffffffffff", actual_key);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_is(expected_key[i] == actual_key[i]);
i++;
}
_end("should convert hex string to 64 bit key");
}
int convert_short_hex_string_to_bits()
{
t_byte1 expected_key[FT_DES_INITIAL_KEY_SIZE] = {
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
t_byte1 actual_key[FT_DES_INITIAL_KEY_SIZE];
const char *wrong_key_char;
int i;
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit_key("FF12CD", actual_key);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_is(expected_key[i] == actual_key[i]);
i++;
}
_end("should convert shorter hex string to 64 bit key");
}
int convert_longer_hex_string_to_bits()
{
t_byte1 expected_key[FT_DES_INITIAL_KEY_SIZE] = {
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0,
};
t_byte1 actual_key[FT_DES_INITIAL_KEY_SIZE];
const char *wrong_key_char;
int i;
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit_key("FF12CDFF12CDFF12CD", actual_key);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_is(expected_key[i] == actual_key[i]);
i++;
}
_end("should convert longer hex string to 64 bit key");
}
int des_tests()
{
_should(perform_initial_permutation);
@ -678,5 +773,8 @@ int des_tests()
_should(encrypt_block);
_should(decrypt_block);
_should(init_ctx);
_should(convert_hex_string_to_bits);
_should(convert_short_hex_string_to_bits);
_should(convert_longer_hex_string_to_bits);
return 0;
}