partial pdbkdf2 intergration

This commit is contained in:
Gregory 2019-04-06 14:40:50 +03:00
parent a13302adf4
commit 93ad85fc40
12 changed files with 342 additions and 51 deletions

View file

@ -36,7 +36,9 @@ SSL_HEADER := $(INC_DIR)/ft_ssl.h
B64_HEADER := $(INC_DIR)/ft_base64.h B64_HEADER := $(INC_DIR)/ft_base64.h
DES_HEADER := $(INC_DIR)/ft_des.h DES_HEADER := $(INC_DIR)/ft_des.h
KD2_HEADER := $(INC_DIR)/ft_pbkdf2.h KD2_HEADER := $(INC_DIR)/ft_pbkdf2.h
HEADERS := $(MD5_HEADER) \ TST_HEADER := $(INC_DIR)/t.h
HEADERS := $(TST_HEADER) \
$(MD5_HEADER) \
$(SHA_HEADER) \ $(SHA_HEADER) \
$(SSL_HEADER) \ $(SSL_HEADER) \
$(B64_HEADER) \ $(B64_HEADER) \
@ -131,6 +133,7 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_generate_decryption_round_keys.c \ ft_des_generate_decryption_round_keys.c \
ft_des_ecb.c \ ft_des_ecb.c \
ft_des_init_ctx.c \ ft_des_init_ctx.c \
ft_des_derive_key.c \
ft_des_print_error.c \ ft_des_print_error.c \
ft_des_arg_parsers.c \ ft_des_arg_parsers.c \
ft_des_ecb_encrypt.c \ ft_des_ecb_encrypt.c \
@ -138,7 +141,9 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_ecb_decrypt_b64.c \ ft_des_ecb_decrypt_b64.c \
ft_des_ecb_encrypt_b64.c \ ft_des_ecb_encrypt_b64.c \
ft_des_ecb_encode_process_chunk.c \ ft_des_ecb_encode_process_chunk.c \
ft_des_hex_to_bit_key.c \ ft_des_hex_to_bit.c \
ft_des_hex_to_byte.c \
ft_des_byte_to_bits.c \
ft_des_ecb_finish_encrypt.c ft_des_ecb_finish_encrypt.c
KD2_SRC = ft_hmac_sha256_init_ctx.c \ KD2_SRC = ft_hmac_sha256_init_ctx.c \

View file

@ -42,8 +42,13 @@ typedef struct s_des_ctx
int readed; int readed;
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
t_byte1 key[FT_DES_INITIAL_KEY_SIZE]; t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
t_byte1 salt[FT_DES_BIT_BLOCK_SIZE];
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT] t_byte1 round_keys[FT_DES_ROUND_COUNT]
[FT_DES_ROUND_KEY_SIZE]; [FT_DES_ROUND_KEY_SIZE];
const char *raw_password;
const char *raw_salt;
const char *raw_key;
} t_des_ctx; } t_des_ctx;
typedef int (*t_ft_des_arg_parser_function) typedef int (*t_ft_des_arg_parser_function)
@ -337,10 +342,31 @@ 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 *ft_des_hex_to_bit
( (
const char *hex_key, const char *hex,
t_byte1 bits_key[FT_DES_INITIAL_KEY_SIZE] t_byte1 *bits,
t_byte8 bit_len
);
const char *ft_des_hex_to_byte
(
const char *hex,
t_byte1 *bytes,
t_byte8 byte_len
);
void ft_des_byte_to_bits
(
t_byte1 *bytes,
t_byte8 byte_len,
t_byte1 *bits,
t_byte8 bits_len
);
void ft_des_derive_key
(
t_des_ctx *ctx
); );
#endif #endif

View file

@ -28,13 +28,9 @@ int ft_des_key_arg_parser
t_des_ctx *ctx t_des_ctx *ctx
) )
{ {
const char *wrong_key_char;
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); ctx->raw_key = argv[position + 1];
if (wrong_key_char != NULL)
ft_des_print_error("there wrong char in key string.");
return (position + 2); return (position + 2);
} }
@ -107,3 +103,31 @@ int ft_des_output_file_arg_parser
} }
return (position + 2); return (position + 2);
} }
int ft_des_password_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
ctx->raw_password = argv[position + 1];
return (position + 2);
}
int ft_des_salt_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
ctx->raw_salt = argv[position + 1];
return (position + 2);
}

View file

@ -0,0 +1,29 @@
#include "ft_des.h"
void ft_des_byte_to_bits
(
t_byte1 *bytes,
t_byte8 bytes_len,
t_byte1 *bits,
t_byte8 bits_len
)
{
t_byte8 bits_count;
t_byte8 bytes_count;
bits_count = 0;
bytes_count = 0;
while(bits_count < bits_len && bytes_count < bytes_len)
{
bits[bits_count] = bytes[bytes_count] >> 7 & 1;
bits[bits_count + 1] = bytes[bytes_count] >> 6 & 1;
bits[bits_count + 2] = bytes[bytes_count] >> 5 & 1;
bits[bits_count + 3] = bytes[bytes_count] >> 4 & 1;
bits[bits_count + 4] = bytes[bytes_count] >> 3 & 1;
bits[bits_count + 5] = bytes[bytes_count] >> 2 & 1;
bits[bits_count + 6] = bytes[bytes_count] >> 1 & 1;
bits[bits_count + 7] = bytes[bytes_count] & 1;
bytes_count++;
bits_count += 8;
}
}

View file

@ -0,0 +1,45 @@
#include <unistd.h>
#include "ft_des.h"
#include "ft_pbkdf2.h"
#include "libft.h"
void ft_des_derive_key
(
t_des_ctx *ctx
)
{
t_pbkdf2_sha256_ctx pbkdf_ctx;
t_byte1 key[FT_DES_BYTE_BLOCK_SIZE];
if (ctx->raw_key)
{
if (ft_des_hex_to_bit(ctx->raw_key, ctx->key, FT_DES_BIT_BLOCK_SIZE))
ft_des_print_error("wrong char in key");
return ;
}
// if (ctx->decode)
// {
// readed = read(ctx->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
// if (readed != FT_DES_BYTE_BLOCK_SIZE)
// ft_des_print_error("wrong input");
// if (ft_strcmp("Salted__", buffer) == 0)
// {
// readed = read(ctx->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
// if (readed != FT_DES_BYTE_BLOCK_SIZE)
// ft_des_print_error("wrong salt");
// pbkdf_ctx.salt = buffer;
// }
// else
// ft_des_pr
// }
pbkdf_ctx.iterations = 10000;
pbkdf_ctx.key = key;
pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE;
pbkdf_ctx.pass = (t_byte1 *)ctx->raw_password;
pbkdf_ctx.salt = ctx->salt;
pbkdf_ctx.pass_len = ft_strlen((char *)ctx->raw_password);
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE;
ft_pbkdf2_sha256(&pbkdf_ctx);
ft_des_byte_to_bits(key, FT_DES_BYTE_BLOCK_SIZE, ctx->key,
FT_DES_INITIAL_KEY_SIZE);
}

View file

@ -1,11 +1,8 @@
#include "ft_des.h" #include "ft_des.h"
#include "ft_pbkdf2.h"
#include "libft.h" #include "libft.h"
t_des_argument_parser g_arg_parsers[] = { t_des_argument_parser g_arg_parsers[] = {
{
"-k",
ft_des_key_arg_parser,
},
{ {
"-d", "-d",
ft_des_decode_arg_parser, ft_des_decode_arg_parser,
@ -26,6 +23,18 @@ t_des_argument_parser g_arg_parsers[] = {
"-o", "-o",
ft_des_output_file_arg_parser, ft_des_output_file_arg_parser,
}, },
{
"-k",
ft_des_key_arg_parser,
},
{
"-p",
ft_des_password_arg_parser,
},
{
"-s",
ft_des_salt_arg_parser,
},
{ NULL, NULL}, { NULL, NULL},
}; };
@ -69,6 +78,7 @@ void ft_des_ecb
ft_des_init_ctx(&ctx); ft_des_init_ctx(&ctx);
parse_args(argc, argv, &ctx); parse_args(argc, argv, &ctx);
ft_des_derive_key(&ctx);
if (ctx.decode) if (ctx.decode)
{ {
ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys);

View file

@ -0,0 +1,31 @@
#include "ft_des.h"
#include "libft.h"
const char *ft_des_hex_to_bit
(
const char *hex,
t_byte1 *bits,
t_byte8 bit_len
)
{
t_byte1 bits4;
t_byte8 i;
i = 0;
while(*hex && i < bit_len)
{
bits4 = ft_tolower(*hex);
if (bits4 >= '0' && bits4 <= '9')
bits4 = bits4 - '0';
else if (bits4 >= 'a' && bits4 <= 'f')
bits4 = bits4 - 'a' + 10;
else
return (hex);
bits[i] = bits4 >> 3 & 1;
bits[i + 1] = bits4 >> 2 & 1;
bits[i + 2] = bits4 >> 1 & 1;
bits[i + 3] = bits4 & 1;
i += 4;
hex++;
}
return (NULL);
}

View file

@ -1,30 +0,0 @@
#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

@ -0,0 +1,47 @@
#include "ft_des.h"
#include "libft.h"
static int char_to_num
(
unsigned char c,
unsigned char *n
)
{
t_byte1 bits4;
bits4 = ft_tolower(c);
if (bits4 >= '0' && bits4 <= '9')
*n = bits4 - '0';
else if (bits4 >= 'a' && bits4 <= 'f')
*n = bits4 - 'a' + 10;
else
return (1);
return (0);
}
const char *ft_des_hex_to_byte
(
const char *hex,
t_byte1 *bytes,
t_byte8 byte_len
)
{
t_byte1 first_byte_half;
t_byte1 second_byte_half;
t_byte8 i;
i = 0;
while(*hex && i < byte_len)
{
if (char_to_num(*hex, &first_byte_half))
return (hex);
bytes[i] = ((first_byte_half << 4) & 0xf0);
if (!*(hex + 1))
break ;
if (char_to_num(*(hex + 1), &second_byte_half))
return (hex + 1);
bytes[i] |= (second_byte_half & 0xf);
i++;
hex += 2;
}
return (NULL);
}

View file

@ -17,9 +17,14 @@ void ft_des_init_ctx
} }
ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE); ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE);
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE); ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
ft_bzero(ctx->salt, FT_DES_BIT_BLOCK_SIZE);
ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE);
ctx->readed = 0; ctx->readed = 0;
ctx->decode = 0; ctx->decode = 0;
ctx->b64 = 0; ctx->b64 = 0;
ctx->input_fd = STDIN_FILENO; ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO; ctx->output_fd = STDOUT_FILENO;
ctx->raw_password = NULL;
ctx->raw_salt = NULL;
ctx->raw_key = NULL;
} }

View file

@ -11,8 +11,8 @@ void ft_des_s_box
t_byte1 column; t_byte1 column;
t_byte1 selected; t_byte1 selected;
row = input[0] << 1 | input[5]; row = (input[0] << 1 | input[5]) & 0xf;
column = input[1] << 3 | input[2] << 2 | input[3] << 1 | input[4]; column = (input[1] << 3 | input[2] << 2 | input[3] << 1 | input[4]) & 0xf;
selected = table[row][column]; selected = table[row][column];
output[0] = selected >> 3 & 1; output[0] = selected >> 3 & 1;
output[1] = selected >> 2 & 1; output[1] = selected >> 2 & 1;

View file

@ -4,7 +4,6 @@
#include "libft.h" #include "libft.h"
#include <unistd.h> #include <unistd.h>
#define S_BOX_CASES_NUMBER 3 #define S_BOX_CASES_NUMBER 3
int perform_initial_permutation() int perform_initial_permutation()
@ -654,11 +653,26 @@ int init_ctx()
_is(ctx.buffer[i] == 0); _is(ctx.buffer[i] == 0);
i++; i++;
} }
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_is(ctx.salt[i] == 0);
i++;
}
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_is(ctx.iv[i] == 0);
i++;
}
_is(ctx.readed == 0); _is(ctx.readed == 0);
_is(ctx.input_fd == STDIN_FILENO); _is(ctx.input_fd == STDIN_FILENO);
_is(ctx.output_fd == STDOUT_FILENO); _is(ctx.output_fd == STDOUT_FILENO);
_is(ctx.decode == 0); _is(ctx.decode == 0);
_is(ctx.b64 == 0); _is(ctx.b64 == 0);
_is(ctx.raw_password == NULL);
_is(ctx.raw_key == NULL);
_is(ctx.raw_salt == NULL);
_end("shoud init ctx"); _end("shoud init ctx");
} }
@ -678,7 +692,8 @@ int convert_hex_string_to_bits()
const char *wrong_key_char; const char *wrong_key_char;
int i; int i;
wrong_key_char = ft_des_hex_to_bit_key("FFFFFFFFFFFFFFFF", actual_key); wrong_key_char = ft_des_hex_to_bit("FFFFFFFFFFFFFFFF", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_is(wrong_key_char == NULL); _is(wrong_key_char == NULL);
i = 0; i = 0;
@ -688,7 +703,8 @@ int convert_hex_string_to_bits()
i++; i++;
} }
wrong_key_char = ft_des_hex_to_bit_key("ffffffffffffffff", actual_key); wrong_key_char = ft_des_hex_to_bit("ffffffffffffffff", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_is(wrong_key_char == NULL); _is(wrong_key_char == NULL);
i = 0; i = 0;
@ -716,7 +732,8 @@ int convert_short_hex_string_to_bits()
int i; int i;
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE); ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit_key("FF12CD", actual_key); wrong_key_char = ft_des_hex_to_bit("FF12CD", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_is(wrong_key_char == NULL); _is(wrong_key_char == NULL);
i = 0; i = 0;
@ -745,7 +762,8 @@ int convert_longer_hex_string_to_bits()
int i; int i;
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE); ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit_key("FF12CDFF12CDFF12CD", actual_key); wrong_key_char = ft_des_hex_to_bit("FF12CDFF12CDFF12CD", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_is(wrong_key_char == NULL); _is(wrong_key_char == NULL);
i = 0; i = 0;
@ -757,6 +775,84 @@ int convert_longer_hex_string_to_bits()
_end("should convert longer hex string to 64 bit key"); _end("should convert longer hex string to 64 bit key");
} }
int convert_hex_string_to_bytes()
{
t_byte1 expected[FT_DES_BYTE_BLOCK_SIZE] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
t_byte1 actual[FT_DES_BYTE_BLOCK_SIZE];
const char *wrong_char;
int i;
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
wrong_char = ft_des_hex_to_byte("FFFFFFFFFFFFFFFF", actual,
FT_DES_BYTE_BLOCK_SIZE);
_is(wrong_char == NULL);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_is(expected[i] == actual[i]);
i++;
}
_end("should convert hex to 8 byte");
}
int convert_short_hex_string_to_bytes()
{
t_byte1 expected[FT_DES_BYTE_BLOCK_SIZE] = {
0xcc, 0x56, 0x50, 0, 0, 0, 0, 0,
};
t_byte1 actual[FT_DES_BYTE_BLOCK_SIZE];
const char *wrong_char;
int i;
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
wrong_char = ft_des_hex_to_byte("CC565", actual,
FT_DES_BYTE_BLOCK_SIZE);
_is(wrong_char == NULL);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_is(expected[i] == actual[i]);
i++;
}
_end("should convert short hex string to 8 bytes");
}
int convert_bytes_to_bits()
{
t_byte1 expected[FT_DES_BIT_BLOCK_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[FT_DES_BIT_BLOCK_SIZE];
t_byte1 bytes[FT_DES_BYTE_BLOCK_SIZE];
const char *wrong_char;
int i;
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
wrong_char = ft_des_hex_to_byte("FF12CDFF12CDFF12CD", bytes,
FT_DES_BYTE_BLOCK_SIZE);
ft_des_byte_to_bits(bytes, FT_DES_BYTE_BLOCK_SIZE, actual,
FT_DES_BIT_BLOCK_SIZE);
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_is(expected[i] == actual[i]);
i++;
}
_end("should convert 8 bytes to 64 bits");
}
int des_tests() int des_tests()
{ {
_should(perform_initial_permutation); _should(perform_initial_permutation);
@ -776,5 +872,8 @@ int des_tests()
_should(convert_hex_string_to_bits); _should(convert_hex_string_to_bits);
_should(convert_short_hex_string_to_bits); _should(convert_short_hex_string_to_bits);
_should(convert_longer_hex_string_to_bits); _should(convert_longer_hex_string_to_bits);
_should(convert_hex_string_to_bytes);
_should(convert_short_hex_string_to_bytes);
_should(convert_bytes_to_bits);
return 0; return 0;
} }