From 93ad85fc404b283a891ff09ebf3f623745f4901f Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 6 Apr 2019 14:40:50 +0300 Subject: [PATCH] partial pdbkdf2 intergration --- Makefile | 9 ++- inc/ft_des.h | 32 +++++++++- src/des/ft_des_arg_parsers.c | 34 ++++++++-- src/des/ft_des_byte_to_bits.c | 29 +++++++++ src/des/ft_des_derive_key.c | 45 +++++++++++++ src/des/ft_des_ecb.c | 18 ++++-- src/des/ft_des_hex_to_bit.c | 31 +++++++++ src/des/ft_des_hex_to_bit_key.c | 30 --------- src/des/ft_des_hex_to_byte.c | 47 ++++++++++++++ src/des/ft_des_init_ctx.c | 5 ++ src/des/ft_des_s_box.c | 4 +- t/des_tests.c | 109 ++++++++++++++++++++++++++++++-- 12 files changed, 342 insertions(+), 51 deletions(-) create mode 100644 src/des/ft_des_byte_to_bits.c create mode 100644 src/des/ft_des_derive_key.c create mode 100644 src/des/ft_des_hex_to_bit.c delete mode 100644 src/des/ft_des_hex_to_bit_key.c create mode 100644 src/des/ft_des_hex_to_byte.c diff --git a/Makefile b/Makefile index f3170572..2d6bd560 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,9 @@ SSL_HEADER := $(INC_DIR)/ft_ssl.h B64_HEADER := $(INC_DIR)/ft_base64.h DES_HEADER := $(INC_DIR)/ft_des.h KD2_HEADER := $(INC_DIR)/ft_pbkdf2.h -HEADERS := $(MD5_HEADER) \ +TST_HEADER := $(INC_DIR)/t.h +HEADERS := $(TST_HEADER) \ + $(MD5_HEADER) \ $(SHA_HEADER) \ $(SSL_HEADER) \ $(B64_HEADER) \ @@ -131,6 +133,7 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_generate_decryption_round_keys.c \ ft_des_ecb.c \ ft_des_init_ctx.c \ + ft_des_derive_key.c \ ft_des_print_error.c \ ft_des_arg_parsers.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_encrypt_b64.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 KD2_SRC = ft_hmac_sha256_init_ctx.c \ diff --git a/inc/ft_des.h b/inc/ft_des.h index 4dedd85b..4284e532 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -42,8 +42,13 @@ typedef struct s_des_ctx int readed; t_byte1 buffer[FT_DES_BYTE_BLOCK_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] [FT_DES_ROUND_KEY_SIZE]; + const char *raw_password; + const char *raw_salt; + const char *raw_key; } t_des_ctx; 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] ); -const char *ft_des_hex_to_bit_key +const char *ft_des_hex_to_bit ( - const char *hex_key, - t_byte1 bits_key[FT_DES_INITIAL_KEY_SIZE] + const char *hex, + 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 diff --git a/src/des/ft_des_arg_parsers.c b/src/des/ft_des_arg_parsers.c index b549c627..d677dc64 100644 --- a/src/des/ft_des_arg_parsers.c +++ b/src/des/ft_des_arg_parsers.c @@ -28,13 +28,9 @@ int ft_des_key_arg_parser t_des_ctx *ctx ) { - const char *wrong_key_char; - if (position + 1 >= argc) 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); - if (wrong_key_char != NULL) - ft_des_print_error("there wrong char in key string."); + ctx->raw_key = argv[position + 1]; return (position + 2); } @@ -107,3 +103,31 @@ int ft_des_output_file_arg_parser } 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); +} diff --git a/src/des/ft_des_byte_to_bits.c b/src/des/ft_des_byte_to_bits.c new file mode 100644 index 00000000..48a30ecd --- /dev/null +++ b/src/des/ft_des_byte_to_bits.c @@ -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; + } +} \ No newline at end of file diff --git a/src/des/ft_des_derive_key.c b/src/des/ft_des_derive_key.c new file mode 100644 index 00000000..1fe40557 --- /dev/null +++ b/src/des/ft_des_derive_key.c @@ -0,0 +1,45 @@ +#include +#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); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 2eb2a858..37c547be 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -1,11 +1,8 @@ #include "ft_des.h" +#include "ft_pbkdf2.h" #include "libft.h" t_des_argument_parser g_arg_parsers[] = { - { - "-k", - ft_des_key_arg_parser, - }, { "-d", ft_des_decode_arg_parser, @@ -26,6 +23,18 @@ t_des_argument_parser g_arg_parsers[] = { "-o", 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}, }; @@ -69,6 +78,7 @@ void ft_des_ecb ft_des_init_ctx(&ctx); parse_args(argc, argv, &ctx); + ft_des_derive_key(&ctx); if (ctx.decode) { ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); diff --git a/src/des/ft_des_hex_to_bit.c b/src/des/ft_des_hex_to_bit.c new file mode 100644 index 00000000..27542759 --- /dev/null +++ b/src/des/ft_des_hex_to_bit.c @@ -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); +} \ No newline at end of file diff --git a/src/des/ft_des_hex_to_bit_key.c b/src/des/ft_des_hex_to_bit_key.c deleted file mode 100644 index 51447176..00000000 --- a/src/des/ft_des_hex_to_bit_key.c +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/src/des/ft_des_hex_to_byte.c b/src/des/ft_des_hex_to_byte.c new file mode 100644 index 00000000..075fab9d --- /dev/null +++ b/src/des/ft_des_hex_to_byte.c @@ -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); +} \ No newline at end of file diff --git a/src/des/ft_des_init_ctx.c b/src/des/ft_des_init_ctx.c index da111af7..78a473f7 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -17,9 +17,14 @@ void ft_des_init_ctx } ft_bzero(ctx->key, FT_DES_INITIAL_KEY_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->decode = 0; ctx->b64 = 0; ctx->input_fd = STDIN_FILENO; ctx->output_fd = STDOUT_FILENO; + ctx->raw_password = NULL; + ctx->raw_salt = NULL; + ctx->raw_key = NULL; } \ No newline at end of file diff --git a/src/des/ft_des_s_box.c b/src/des/ft_des_s_box.c index 3cacf7da..1316c7f6 100644 --- a/src/des/ft_des_s_box.c +++ b/src/des/ft_des_s_box.c @@ -11,8 +11,8 @@ void ft_des_s_box t_byte1 column; t_byte1 selected; - row = input[0] << 1 | input[5]; - column = input[1] << 3 | input[2] << 2 | input[3] << 1 | input[4]; + row = (input[0] << 1 | input[5]) & 0xf; + column = (input[1] << 3 | input[2] << 2 | input[3] << 1 | input[4]) & 0xf; selected = table[row][column]; output[0] = selected >> 3 & 1; output[1] = selected >> 2 & 1; diff --git a/t/des_tests.c b/t/des_tests.c index f4554568..73917c13 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -4,7 +4,6 @@ #include "libft.h" #include - #define S_BOX_CASES_NUMBER 3 int perform_initial_permutation() @@ -654,11 +653,26 @@ int init_ctx() _is(ctx.buffer[i] == 0); 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.input_fd == STDIN_FILENO); _is(ctx.output_fd == STDOUT_FILENO); _is(ctx.decode == 0); _is(ctx.b64 == 0); + _is(ctx.raw_password == NULL); + _is(ctx.raw_key == NULL); + _is(ctx.raw_salt == NULL); _end("shoud init ctx"); } @@ -678,7 +692,8 @@ int convert_hex_string_to_bits() const char *wrong_key_char; 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); i = 0; @@ -688,7 +703,8 @@ int convert_hex_string_to_bits() 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); i = 0; @@ -716,7 +732,8 @@ int convert_short_hex_string_to_bits() int i; 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); i = 0; @@ -745,7 +762,8 @@ int convert_longer_hex_string_to_bits() int i; 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); i = 0; @@ -757,6 +775,84 @@ int convert_longer_hex_string_to_bits() _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() { _should(perform_initial_permutation); @@ -776,5 +872,8 @@ int des_tests() _should(convert_hex_string_to_bits); _should(convert_short_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; } \ No newline at end of file