encode arbitraty message with key as argument
This commit is contained in:
parent
9e9e697cdc
commit
819257f21f
8 changed files with 172 additions and 24 deletions
4
Makefile
4
Makefile
|
@ -131,7 +131,9 @@ DES_SRC = ft_des_initial_permutation.c \
|
||||||
ft_des_print_error.c \
|
ft_des_print_error.c \
|
||||||
ft_des_arg_parsers.c \
|
ft_des_arg_parsers.c \
|
||||||
ft_des_ecb_process.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 \
|
SRC = main.c \
|
||||||
ft_ssl_usage.c
|
ft_ssl_usage.c
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
# define FT_DES_REDUCED_KEY_SIZE 56
|
# define FT_DES_REDUCED_KEY_SIZE 56
|
||||||
# define FT_DES_ROUND_COUNT 16
|
# define FT_DES_ROUND_COUNT 16
|
||||||
# define FT_DES_READ_SIZE 1024
|
# define FT_DES_READ_SIZE 1024
|
||||||
|
# define FT_DES_MAX_HEX_KEY 16
|
||||||
|
|
||||||
typedef uint64_t t_byte8;
|
typedef uint64_t t_byte8;
|
||||||
typedef unsigned char t_byte1;
|
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]
|
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
|
#endif
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
int ft_des_key_arg_parser
|
int ft_des_key_arg_parser
|
||||||
(
|
(
|
||||||
|
@ -8,26 +9,12 @@ int ft_des_key_arg_parser
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
|
const char *wrong_key_char;
|
||||||
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;
|
|
||||||
if (position + 1 >= argc)
|
if (position + 1 >= argc)
|
||||||
ft_des_print_error("there is no key after -k flag. type -h for help.");
|
ft_des_print_error("there is no key after -k flag. type -h for help.");
|
||||||
|
wrong_key_char = ft_des_hex_to_bit_key(argv[position + 1], ctx->key);
|
||||||
i = 0;
|
if (wrong_key_char != NULL)
|
||||||
while (i < FT_DES_INITIAL_KEY_SIZE)
|
ft_des_print_error("there wrong char in key string.");
|
||||||
{
|
|
||||||
ctx->key[i] = key[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (position + 2);
|
return (position + 2);
|
||||||
}
|
}
|
23
src/des/ft_des_ecb_finish_process.c
Normal file
23
src/des/ft_des_ecb_finish_process.c
Normal 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);
|
||||||
|
}
|
|
@ -12,5 +12,5 @@ void ft_des_ecb_process
|
||||||
ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys);
|
ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys);
|
||||||
while((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0)
|
while((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0)
|
||||||
ft_des_ecb_process_chunk(ctx, readed, buffer);
|
ft_des_ecb_process_chunk(ctx, readed, buffer);
|
||||||
// ft_des_ecb_finish_process(ctx);
|
ft_des_ecb_finish_process(ctx);
|
||||||
}
|
}
|
|
@ -4,11 +4,12 @@
|
||||||
|
|
||||||
static void ft_des_ecb_write
|
static void ft_des_ecb_write
|
||||||
(
|
(
|
||||||
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE],
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
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);
|
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)
|
if (readed >= free_space_in_buffer)
|
||||||
{
|
{
|
||||||
ft_memcpy(&ctx->buffer[buffer_index], buffer, 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;
|
idx = free_space_in_buffer;
|
||||||
while(idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
|
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;
|
idx += FT_DES_BYTE_BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
buffer_index = 0;
|
buffer_index = 0;
|
||||||
|
|
30
src/des/ft_des_hex_to_bit_key.c
Normal file
30
src/des/ft_des_hex_to_bit_key.c
Normal 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);
|
||||||
|
}
|
|
@ -662,6 +662,101 @@ int init_ctx()
|
||||||
_end("shoud 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()
|
int des_tests()
|
||||||
{
|
{
|
||||||
_should(perform_initial_permutation);
|
_should(perform_initial_permutation);
|
||||||
|
@ -678,5 +773,8 @@ int des_tests()
|
||||||
_should(encrypt_block);
|
_should(encrypt_block);
|
||||||
_should(decrypt_block);
|
_should(decrypt_block);
|
||||||
_should(init_ctx);
|
_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;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue