From 28c7f783655c0169a98571bbec619da0cb0038bc Mon Sep 17 00:00:00 2001 From: Gregory Date: Tue, 9 Apr 2019 22:02:00 +0300 Subject: [PATCH] des cbc and ecb done, decryption with -a flag doesn't work --- Makefile | 18 ++-- inc/ft_des.h | 95 ++++++++++++++++--- src/des/ft_des.c | 24 +++++ src/des/ft_des_arg_parsers.c | 16 ++++ src/des/ft_des_bits_to_bytes.c | 29 ++++++ src/des/ft_des_buffered_read.c | 26 +++++ src/des/ft_des_cbc.c | 52 ++++++++++ ...{ft_des_ecb_decrypt.c => ft_des_decrypt.c} | 4 +- ...ecb_decrypt_b64.c => ft_des_decrypt_b64.c} | 10 +- src/des/ft_des_decryption_key_routine.c | 18 ++-- src/des/ft_des_derive_key.c | 16 ++-- src/des/ft_des_ecb.c | 91 +++--------------- ..._chunk.c => ft_des_encode_process_chunk.c} | 4 +- ...{ft_des_ecb_encrypt.c => ft_des_encrypt.c} | 6 +- ...ecb_encrypt_b64.c => ft_des_encrypt_b64.c} | 9 +- src/des/ft_des_encryption_key_routine.c | 15 ++- ...nish_encrypt.c => ft_des_finish_encrypt.c} | 4 +- src/des/ft_des_get_password.c | 18 ++-- src/des/ft_des_init_ctx.c | 6 +- src/des/ft_des_parse_args.c | 73 ++++++++++++++ src/des/ft_des_set_raw_iv.c | 14 +++ src/des/ft_des_set_raw_key.c | 2 +- src/main.c | 4 + t/cases/des_ecb_b64.txt | 1 + t/cases/one_deep_secret.txt | 1 + t/des_tests.c | 57 ++++++++++- 26 files changed, 466 insertions(+), 147 deletions(-) create mode 100644 src/des/ft_des.c create mode 100644 src/des/ft_des_bits_to_bytes.c create mode 100644 src/des/ft_des_buffered_read.c create mode 100644 src/des/ft_des_cbc.c rename src/des/{ft_des_ecb_decrypt.c => ft_des_decrypt.c} (93%) rename src/des/{ft_des_ecb_decrypt_b64.c => ft_des_decrypt_b64.c} (84%) rename src/des/{ft_des_ecb_encode_process_chunk.c => ft_des_encode_process_chunk.c} (90%) rename src/des/{ft_des_ecb_encrypt.c => ft_des_encrypt.c} (73%) rename src/des/{ft_des_ecb_encrypt_b64.c => ft_des_encrypt_b64.c} (84%) rename src/des/{ft_des_ecb_finish_encrypt.c => ft_des_finish_encrypt.c} (82%) create mode 100644 src/des/ft_des_parse_args.c create mode 100644 src/des/ft_des_set_raw_iv.c create mode 100644 t/cases/des_ecb_b64.txt create mode 100644 t/cases/one_deep_secret.txt diff --git a/Makefile b/Makefile index c2204da8..b2d3ea7b 100644 --- a/Makefile +++ b/Makefile @@ -138,24 +138,30 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_process_block.c \ ft_des_generate_encryption_round_keys.c \ ft_des_generate_decryption_round_keys.c \ + ft_des_parse_args.c \ ft_des_arg_parsers.c \ + ft_des.c \ ft_des_ecb.c \ + ft_des_cbc.c \ ft_des_init_ctx.c \ ft_des_set_raw_key.c \ + ft_des_set_raw_iv.c \ ft_des_get_password.c \ ft_des_derive_key.c \ ft_des_encryption_key_routine.c \ ft_des_decryption_key_routine.c \ ft_des_print_error.c \ - ft_des_ecb_encrypt.c \ - ft_des_ecb_decrypt.c \ - ft_des_ecb_decrypt_b64.c \ - ft_des_ecb_encrypt_b64.c \ - ft_des_ecb_encode_process_chunk.c \ + ft_des_encrypt.c \ + ft_des_decrypt.c \ + ft_des_decrypt_b64.c \ + ft_des_encrypt_b64.c \ + ft_des_encode_process_chunk.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_bits_to_bytes.c \ + ft_des_finish_encrypt.c \ + ft_des_buffered_read.c KD2_SRC = ft_hmac_sha256_init_ctx.c \ ft_hmac_sha256.c \ diff --git a/inc/ft_des.h b/inc/ft_des.h index 8f1c0ad5..37c3341c 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -14,6 +14,7 @@ # define FT_DES_H # include +# include # define FT_DES_BYTE_BLOCK_SIZE 8 # define FT_DES_BIT_BLOCK_SIZE 64 @@ -32,6 +33,13 @@ typedef uint64_t t_byte8; typedef unsigned char t_byte1; +typedef void (*t_ft_des_mode) +( + t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], + t_byte1 iv[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 output[FT_DES_BYTE_BLOCK_SIZE] +); typedef struct s_des_ctx { @@ -40,14 +48,16 @@ typedef struct s_des_ctx int decode; int b64; int readed; - t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; - t_byte1 key[FT_DES_INITIAL_KEY_SIZE]; + t_byte1 key[FT_DES_BIT_BLOCK_SIZE]; t_byte1 iv[FT_DES_BIT_BLOCK_SIZE]; + t_byte1 buffer[FT_DES_BYTE_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; + const char *raw_iv; + t_ft_des_mode mode; } t_des_ctx; typedef int (*t_ft_des_arg_parser_function) @@ -220,10 +230,9 @@ void ft_des_print_error const char *error ); -void ft_des_ecb +void ft_des ( - int argc, - char **argv + t_des_ctx *ctx ); void ft_des_init_ctx @@ -231,6 +240,13 @@ void ft_des_init_ctx t_des_ctx *ctx ); +void ft_des_parse_args +( + int argc, + char **argv, + t_des_ctx *ctx +); + int ft_des_key_arg_parser ( int argc, @@ -239,6 +255,14 @@ int ft_des_key_arg_parser t_des_ctx *c ); +int ft_des_iv_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *c +); + int ft_des_decode_arg_parser ( int argc, @@ -303,34 +327,51 @@ int ft_des_salt_arg_parser t_des_ctx *c ); -void ft_des_ecb_decrypt +void ft_des +( + t_des_ctx *c +); + +void ft_des_ecb +( + int argc, + char **argv +); + +void ft_des_cbc +( + int argc, + char **argv +); + +void ft_des_decrypt ( t_des_ctx *ctx ); -void ft_des_ecb_decrypt_b64 +void ft_des_decrypt_b64 ( t_des_ctx *ctx ); -void ft_des_ecb_encrypt +void ft_des_encrypt ( t_des_ctx *ctx ); -void ft_des_ecb_encrypt_b64 +void ft_des_encrypt_b64 ( t_des_ctx *ctx ); -void ft_des_ecb_encode_process_chunk +void ft_des_encode_process_chunk ( t_des_ctx *ctx, t_byte8 reaed, t_byte1 buffer[FT_DES_READ_SIZE] ); -void ft_des_ecb_finish_encrypt +void ft_des_finish_encrypt ( t_des_ctx *ctx ); @@ -363,6 +404,14 @@ void ft_des_byte_to_bits t_byte8 bits_len ); +void ft_des_bits_to_bytes +( + t_byte1 *bits, + t_byte8 bits_len, + t_byte1 *bytes, + t_byte8 byte_len +); + void ft_des_encryption_key_routine ( t_des_ctx *ctx @@ -378,16 +427,36 @@ void ft_des_set_raw_key t_des_ctx *ctx ); -void ft_des_get_password +void ft_des_set_raw_iv ( t_des_ctx *ctx ); -void ft_des_derive_key +void ft_des_get_password +( + char pass[128] +); + +void ft_des_derive_key_and_iv ( t_byte1 key[FT_DES_BIT_BLOCK_SIZE], + t_byte1 iv[FT_DES_BIT_BLOCK_SIZE], char salt[FT_DES_BYTE_BLOCK_SIZE], char *pass ); +void ft_des_parse_args +( + int argc, + char **argv, + t_des_ctx *ctx +); + +int ft_des_buffered_read +( + int fd, + char *buffer, + size_t buff_size +); + #endif diff --git a/src/des/ft_des.c b/src/des/ft_des.c new file mode 100644 index 00000000..2a53d1f8 --- /dev/null +++ b/src/des/ft_des.c @@ -0,0 +1,24 @@ +#include +#include "ft_des.h" + +void ft_des +( + t_des_ctx *ctx +) +{ + if (ctx->decode) + { + if (ctx->b64) + ft_des_decrypt_b64(ctx); + else + ft_des_decrypt(ctx); + } + else + { + if (ctx->b64) + ft_des_encrypt_b64(ctx); + else + ft_des_encrypt(ctx); + } + exit(0); +} \ No newline at end of file diff --git a/src/des/ft_des_arg_parsers.c b/src/des/ft_des_arg_parsers.c index d677dc64..ccdb36ff 100644 --- a/src/des/ft_des_arg_parsers.c +++ b/src/des/ft_des_arg_parsers.c @@ -34,6 +34,22 @@ int ft_des_key_arg_parser return (position + 2); } + +int ft_des_iv_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *ctx +) +{ + if (position + 1 >= argc) + ft_des_print_error("there is no initialization vector after -k flag. \ +type -h for help."); + ctx->raw_iv = argv[position + 1]; + return (position + 2); +} + int ft_des_decode_arg_parser ( int argc, diff --git a/src/des/ft_des_bits_to_bytes.c b/src/des/ft_des_bits_to_bytes.c new file mode 100644 index 00000000..ecad5965 --- /dev/null +++ b/src/des/ft_des_bits_to_bytes.c @@ -0,0 +1,29 @@ +#include "ft_des.h" + +void ft_des_bits_to_bytes +( + t_byte1 *bits, + t_byte8 bits_len, + t_byte1 *bytes, + t_byte8 bytes_len +) +{ + t_byte8 bits_count; + t_byte8 bytes_count; + + bits_count = 0; + bytes_count = 0; + while(bits_count < bits_len && bytes_count < bytes_len) + { + bytes[bytes_count] = (bits[bits_count] << 7) | + (bits[bits_count + 1] << 6) | + (bits[bits_count + 2] << 5) | + (bits[bits_count + 3] << 4) | + (bits[bits_count + 4] << 3) | + (bits[bits_count + 5] << 2) | + (bits[bits_count + 6] << 1) | + (bits[bits_count + 7]); + bytes_count++; + bits_count += 8; + } +} \ No newline at end of file diff --git a/src/des/ft_des_buffered_read.c b/src/des/ft_des_buffered_read.c new file mode 100644 index 00000000..9fe68099 --- /dev/null +++ b/src/des/ft_des_buffered_read.c @@ -0,0 +1,26 @@ +#include +#include "ft_des.h" + +int ft_des_buffered_read +( + int fd, + char *buffer, + size_t buff_size +) +{ + size_t total_readed; + size_t current_read; + + total_readed = 0; + current_read = 0; + while(total_readed < buff_size) + { + if ((current_read = read( + fd, + buffer + total_readed, + buff_size - total_readed)) == 0) + return (total_readed + current_read); + total_readed += current_read; + } + return (total_readed); +} \ No newline at end of file diff --git a/src/des/ft_des_cbc.c b/src/des/ft_des_cbc.c new file mode 100644 index 00000000..769e66c2 --- /dev/null +++ b/src/des/ft_des_cbc.c @@ -0,0 +1,52 @@ +#include "ft_des.h" + +static void xor +( + t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], + t_byte1 iv[FT_DES_BIT_BLOCK_SIZE] +) +{ + int i; + + i = 0; + while(i < FT_DES_BIT_BLOCK_SIZE) + { + plaintext[i] = plaintext[i] ^ iv[i]; + i++; + } +} + +static void mode +( + t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], + t_byte1 iv[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 output[FT_DES_BYTE_BLOCK_SIZE] +) +{ + t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE]; + ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, input_bits, + FT_DES_BIT_BLOCK_SIZE); + xor(input_bits, iv); + ft_des_bits_to_bytes(input_bits, FT_DES_BIT_BLOCK_SIZE, input, + FT_DES_BYTE_BLOCK_SIZE); + ft_des_process_block(input, keys, output); + ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, iv, + FT_DES_BIT_BLOCK_SIZE); +} + +void ft_des_cbc +( + int argc, + char **argv +) +{ + t_des_ctx ctx; + + ft_des_init_ctx(&ctx); + ft_des_parse_args(argc, argv, &ctx); + if (ctx.raw_key && !ctx.raw_iv) + ft_des_print_error("initialization vector required when key specified"); + ctx.mode = mode; + ft_des(&ctx); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_decrypt.c b/src/des/ft_des_decrypt.c similarity index 93% rename from src/des/ft_des_ecb_decrypt.c rename to src/des/ft_des_decrypt.c index 941725de..22d21e98 100644 --- a/src/des/ft_des_ecb_decrypt.c +++ b/src/des/ft_des_decrypt.c @@ -25,7 +25,7 @@ static t_byte8 buffered_read return (total_readed); } -void ft_des_ecb_decrypt +void ft_des_decrypt ( t_des_ctx *c ) @@ -44,7 +44,7 @@ void ft_des_ecb_decrypt ft_des_print_error("wrong message size"); if (last_read) write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE); - ft_des_process_block(buffer, c->round_keys, message); + c->mode(buffer, c->round_keys, c->iv, message); last_read = readed; } if (message[7] < 0 || message[7] > 8) diff --git a/src/des/ft_des_ecb_decrypt_b64.c b/src/des/ft_des_decrypt_b64.c similarity index 84% rename from src/des/ft_des_ecb_decrypt_b64.c rename to src/des/ft_des_decrypt_b64.c index 11326ce6..48e5da17 100644 --- a/src/des/ft_des_ecb_decrypt_b64.c +++ b/src/des/ft_des_decrypt_b64.c @@ -12,13 +12,14 @@ static void base64_decode { t_base64_ctx b64_ctx; + close(pipe_fd[0]); ft_base64_init(&b64_ctx); b64_ctx.input_fd = ctx->input_fd; b64_ctx.output_fd = pipe_fd[1]; ft_base64_decode(&b64_ctx); } -static void des_ecb_decrypt +static void des_decrypt ( t_des_ctx *ctx, int pipe_fd[2] @@ -26,10 +27,10 @@ static void des_ecb_decrypt { close(pipe_fd[1]); ctx->input_fd = pipe_fd[0]; - ft_des_ecb_decrypt(ctx); + ft_des_decrypt(ctx); } -void ft_des_ecb_decrypt_b64 +void ft_des_decrypt_b64 ( t_des_ctx *ctx ) @@ -37,7 +38,6 @@ void ft_des_ecb_decrypt_b64 pid_t pid; int pipe_fd[2]; - (void)ctx; if (pipe(pipe_fd)) ft_des_print_error("failded to create pipe"); pid = fork(); @@ -46,5 +46,5 @@ void ft_des_ecb_decrypt_b64 else if (pid < 0) ft_des_print_error("failded to create child process"); else - des_ecb_decrypt(ctx, pipe_fd); + des_decrypt(ctx, pipe_fd); } \ No newline at end of file diff --git a/src/des/ft_des_decryption_key_routine.c b/src/des/ft_des_decryption_key_routine.c index 94800b32..0c45cc0a 100644 --- a/src/des/ft_des_decryption_key_routine.c +++ b/src/des/ft_des_decryption_key_routine.c @@ -12,10 +12,12 @@ static void get_salt char buffer[FT_DES_BYTE_BLOCK_SIZE]; int readed; - readed = read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); + readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); if (readed != FT_DES_BYTE_BLOCK_SIZE) - ft_des_print_error("error reading input"); - readed = read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); + ft_des_print_error("error reading input"); + if (ft_strncmp(buffer, "Salted__", FT_DES_BYTE_BLOCK_SIZE) != 0) + ft_des_print_error("bad magic number"); + readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); if (readed != FT_DES_BYTE_BLOCK_SIZE) ft_des_print_error("error reading input"); ft_memcpy(salt, buffer, FT_DES_BYTE_BLOCK_SIZE); @@ -27,14 +29,18 @@ void ft_des_decryption_key_routine ) { char salt[FT_DES_BYTE_BLOCK_SIZE]; - + char pass[128]; if (ctx->raw_password || !ctx->raw_key) get_salt(ctx, salt); if (!ctx->raw_password && !ctx->raw_key) { - ft_des_get_password(ctx); - ft_des_derive_key(ctx->key, salt, (char *)ctx->raw_password); + ft_des_get_password(pass); + ctx->raw_password = pass; + ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt, + (char *)ctx->raw_password); } if (ctx->raw_key) ft_des_set_raw_key(ctx); + if (ctx->raw_iv) + ft_des_set_raw_iv(ctx); } \ No newline at end of file diff --git a/src/des/ft_des_derive_key.c b/src/des/ft_des_derive_key.c index cfb3a928..a8a33a19 100644 --- a/src/des/ft_des_derive_key.c +++ b/src/des/ft_des_derive_key.c @@ -2,24 +2,28 @@ #include "ft_pbkdf2.h" #include "libft.h" -void ft_des_derive_key +void ft_des_derive_key_and_iv ( t_byte1 key[FT_DES_BIT_BLOCK_SIZE], + t_byte1 iv[FT_DES_BIT_BLOCK_SIZE], char salt[FT_DES_BYTE_BLOCK_SIZE], char *pass ) { t_pbkdf2_sha256_ctx pbkdf_ctx; - t_byte1 byte_key[FT_DES_BYTE_BLOCK_SIZE]; + t_byte1 key_iv[FT_DES_BYTE_BLOCK_SIZE * 2]; + t_byte1 key_iv_bit[FT_DES_BIT_BLOCK_SIZE * 2]; pbkdf_ctx.iterations = 10000; - pbkdf_ctx.key = byte_key; + pbkdf_ctx.key = key_iv; pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE; - pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE; + pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE * 2; pbkdf_ctx.pass_len = ft_strlen(pass); pbkdf_ctx.pass = (t_byte1 *)pass; pbkdf_ctx.salt = (t_byte1 *)salt; ft_pbkdf2_sha256(&pbkdf_ctx); - ft_des_byte_to_bits(byte_key, FT_DES_BYTE_BLOCK_SIZE, key, - FT_DES_INITIAL_KEY_SIZE); + ft_des_byte_to_bits(key_iv, FT_DES_BYTE_BLOCK_SIZE * 2, key_iv_bit, + FT_DES_BIT_BLOCK_SIZE * 2); + ft_memcpy(key, key_iv_bit, FT_DES_BIT_BLOCK_SIZE); + ft_memcpy(iv, key_iv_bit + FT_DES_BIT_BLOCK_SIZE, FT_DES_BIT_BLOCK_SIZE); } \ No newline at end of file diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index ca8602cf..34b10279 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -1,71 +1,15 @@ #include "ft_des.h" -#include "ft_pbkdf2.h" -#include "libft.h" -t_des_argument_parser g_arg_parsers[] = { - { - "-d", - ft_des_decode_arg_parser, - }, - { - "-e", - ft_des_encode_arg_parser, - }, - { - "-a", - ft_des_base64_arg_parser, - }, - { - "-i", - ft_des_input_file_arg_parser, - }, - { - "-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}, -}; - -static void parse_args +static void mode ( - int argc, - char **argv, - t_des_ctx *ctx + t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], + t_byte1 iv[FT_DES_BYTE_BLOCK_SIZE], + t_byte1 output[FT_DES_BYTE_BLOCK_SIZE] ) { - int i; - char *current_arg; - t_des_argument_parser *parser_walker; - - i = 1; - while(i < argc) - { - current_arg = argv[i]; - parser_walker = g_arg_parsers; - while(parser_walker->arg_parser) - { - if (ft_strcmp(current_arg, parser_walker->arg) == 0) - { - i = parser_walker->arg_parser(argc, argv, i, ctx); - break; - } - parser_walker++; - } - if (!parser_walker->arg_parser) - ft_des_print_error("wrong argument. type -h for help."); - } + (void)iv; + ft_des_process_block(input, keys, output); } void ft_des_ecb @@ -74,23 +18,10 @@ void ft_des_ecb char **argv ) { - t_des_ctx ctx; + t_des_ctx ctx; ft_des_init_ctx(&ctx); - parse_args(argc, argv, &ctx); - if (ctx.decode) - { - if (ctx.b64) - ft_des_ecb_decrypt_b64(&ctx); - else - ft_des_ecb_decrypt(&ctx); - } - else - { - if (ctx.b64) - ft_des_ecb_encrypt_b64(&ctx); - else - ft_des_ecb_encrypt(&ctx); - } - exit(0); + ft_des_parse_args(argc, argv, &ctx); + ctx.mode = mode; + ft_des(&ctx); } \ No newline at end of file diff --git a/src/des/ft_des_ecb_encode_process_chunk.c b/src/des/ft_des_encode_process_chunk.c similarity index 90% rename from src/des/ft_des_ecb_encode_process_chunk.c rename to src/des/ft_des_encode_process_chunk.c index f16dc6d3..d701baf4 100644 --- a/src/des/ft_des_ecb_encode_process_chunk.c +++ b/src/des/ft_des_encode_process_chunk.c @@ -9,11 +9,11 @@ static void ft_des_ecb_write ) { t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; - ft_des_process_block(buffer, ctx->round_keys, cyphertext); + ctx->mode(buffer, ctx->round_keys, ctx->iv, cyphertext); write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } -void ft_des_ecb_encode_process_chunk +void ft_des_encode_process_chunk ( t_des_ctx *ctx, t_byte8 readed, diff --git a/src/des/ft_des_ecb_encrypt.c b/src/des/ft_des_encrypt.c similarity index 73% rename from src/des/ft_des_ecb_encrypt.c rename to src/des/ft_des_encrypt.c index c2adc980..f43b7197 100644 --- a/src/des/ft_des_ecb_encrypt.c +++ b/src/des/ft_des_encrypt.c @@ -2,7 +2,7 @@ #include "ft_des.h" #include "ft_base64.h" -void ft_des_ecb_encrypt +void ft_des_encrypt ( t_des_ctx *ctx ) @@ -13,6 +13,6 @@ void ft_des_ecb_encrypt ft_des_encryption_key_routine(ctx); 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_encode_process_chunk(ctx, readed, buffer); - ft_des_ecb_finish_encrypt(ctx); + ft_des_encode_process_chunk(ctx, readed, buffer); + ft_des_finish_encrypt(ctx); } \ No newline at end of file diff --git a/src/des/ft_des_ecb_encrypt_b64.c b/src/des/ft_des_encrypt_b64.c similarity index 84% rename from src/des/ft_des_ecb_encrypt_b64.c rename to src/des/ft_des_encrypt_b64.c index b1e0f047..6e800ed4 100644 --- a/src/des/ft_des_ecb_encrypt_b64.c +++ b/src/des/ft_des_encrypt_b64.c @@ -19,7 +19,7 @@ static void base64_encode ft_base64_encode(&b64_ctx); } -static void des_ecb_encrypt +static void des_encrypt ( t_des_ctx *ctx, int pipe_fd[2] @@ -27,10 +27,10 @@ static void des_ecb_encrypt { close(pipe_fd[0]); ctx->output_fd = pipe_fd[1]; - ft_des_ecb_encrypt(ctx); + ft_des_encrypt(ctx); } -void ft_des_ecb_encrypt_b64 +void ft_des_encrypt_b64 ( t_des_ctx *ctx ) @@ -38,12 +38,11 @@ void ft_des_ecb_encrypt_b64 pid_t pid; int pipe_fd[2]; - (void)ctx; if (pipe(pipe_fd)) ft_des_print_error("failded to create pipe"); pid = fork(); if (pid == 0) - des_ecb_encrypt(ctx, pipe_fd); + des_encrypt(ctx, pipe_fd); else if (pid < 0) ft_des_print_error("failded to create child process"); else diff --git a/src/des/ft_des_encryption_key_routine.c b/src/des/ft_des_encryption_key_routine.c index 028a7fbc..90fc5e36 100644 --- a/src/des/ft_des_encryption_key_routine.c +++ b/src/des/ft_des_encryption_key_routine.c @@ -27,6 +27,7 @@ static void get_salt exit(1); } read(fd, salt, FT_DES_BYTE_BLOCK_SIZE); + close(fd); } } @@ -36,16 +37,24 @@ void ft_des_encryption_key_routine ) { char salt[FT_DES_BYTE_BLOCK_SIZE]; + char pass[128]; + if (ctx->raw_iv) + ft_des_set_raw_iv(ctx); if (ctx->raw_key) ft_des_set_raw_key(ctx); else { - ft_des_get_password(ctx); + if (!ctx->raw_password) + { + ft_des_get_password(pass); + ctx->raw_password = pass; + } get_salt(ctx, salt); - ft_des_derive_key(ctx->key, salt, (char *)ctx->raw_password); + ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt, + (char *)ctx->raw_password); } - if (ctx->raw_password || !ctx->raw_key) + if (ctx->raw_password) { write(ctx->output_fd, "Salted__", 8); write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE); diff --git a/src/des/ft_des_ecb_finish_encrypt.c b/src/des/ft_des_finish_encrypt.c similarity index 82% rename from src/des/ft_des_ecb_finish_encrypt.c rename to src/des/ft_des_finish_encrypt.c index ed0ba282..21800894 100644 --- a/src/des/ft_des_ecb_finish_encrypt.c +++ b/src/des/ft_des_finish_encrypt.c @@ -1,7 +1,7 @@ #include "ft_des.h" #include -void ft_des_ecb_finish_encrypt +void ft_des_finish_encrypt ( t_des_ctx *ctx ) @@ -18,6 +18,6 @@ void ft_des_ecb_finish_encrypt ctx->buffer[buffer_index] = padding_size; buffer_index++; } - ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext); + ctx->mode(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext); write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } \ No newline at end of file diff --git a/src/des/ft_des_get_password.c b/src/des/ft_des_get_password.c index 786d664e..893e9953 100644 --- a/src/des/ft_des_get_password.c +++ b/src/des/ft_des_get_password.c @@ -3,21 +3,19 @@ void ft_des_get_password ( - t_des_ctx *ctx + char pass[128] ) { - char *first_try[128]; - char *second_try[128]; + char first_try[128]; + char second_try[128]; - if (ctx->raw_key || ctx->raw_password) - return ; ft_bzero(first_try, 128); ft_bzero(second_try, 128); - ft_strcpy((char *)first_try, getpass("enter password:")); - if (!ft_strlen((const char *)first_try)) + ft_strcpy(first_try, getpass("enter password:")); + if (!ft_strlen(first_try)) exit(1); - ft_strcpy((char *)second_try, getpass("retype password:")); - if (ft_strcmp((const char *)first_try, (const char *)second_try) != 0) + ft_strcpy(second_try, getpass("retype password:")); + if (ft_strcmp(first_try, second_try) != 0) ft_des_print_error("passwords doesn't match"); - ctx->raw_password = (char *)second_try; + ft_memcpy(pass, second_try, 128); } \ 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 be400aad..b5674004 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -15,9 +15,9 @@ void ft_des_init_ctx ft_bzero(ctx->round_keys[i], FT_DES_ROUND_KEY_SIZE); i++; } - ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE); - ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE); ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE); + ft_bzero(ctx->key, FT_DES_BIT_BLOCK_SIZE); + ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE); ctx->readed = 0; ctx->decode = 0; ctx->b64 = 0; @@ -26,4 +26,6 @@ void ft_des_init_ctx ctx->raw_password = NULL; ctx->raw_salt = NULL; ctx->raw_key = NULL; + ctx->raw_iv = NULL; + ctx->mode = NULL; } \ No newline at end of file diff --git a/src/des/ft_des_parse_args.c b/src/des/ft_des_parse_args.c new file mode 100644 index 00000000..f3e99329 --- /dev/null +++ b/src/des/ft_des_parse_args.c @@ -0,0 +1,73 @@ +#include +#include "ft_des.h" +#include "libft.h" + +t_des_argument_parser g_arg_parsers[] = { + { + "-d", + ft_des_decode_arg_parser, + }, + { + "-e", + ft_des_encode_arg_parser, + }, + { + "-a", + ft_des_base64_arg_parser, + }, + { + "-i", + ft_des_input_file_arg_parser, + }, + { + "-o", + ft_des_output_file_arg_parser, + }, + { + "-k", + ft_des_key_arg_parser, + }, + { + "-v", + ft_des_iv_arg_parser, + }, + { + "-p", + ft_des_password_arg_parser, + }, + { + "-s", + ft_des_salt_arg_parser, + }, + { NULL, NULL}, +}; + +void ft_des_parse_args +( + int argc, + char **argv, + t_des_ctx *ctx +) +{ + int i; + char *current_arg; + t_des_argument_parser *parser_walker; + + i = 1; + while(i < argc) + { + current_arg = argv[i]; + parser_walker = g_arg_parsers; + while(parser_walker->arg_parser) + { + if (ft_strcmp(current_arg, parser_walker->arg) == 0) + { + i = parser_walker->arg_parser(argc, argv, i, ctx); + break; + } + parser_walker++; + } + if (!parser_walker->arg_parser) + ft_des_print_error("wrong argument. type -h for help."); + } +} \ No newline at end of file diff --git a/src/des/ft_des_set_raw_iv.c b/src/des/ft_des_set_raw_iv.c new file mode 100644 index 00000000..5bfae5ae --- /dev/null +++ b/src/des/ft_des_set_raw_iv.c @@ -0,0 +1,14 @@ +#include "ft_des.h" + +void ft_des_set_raw_iv +( + t_des_ctx *ctx +) +{ + const char *wrong_char; + + wrong_char = ft_des_hex_to_bit(ctx->raw_iv, ctx->iv, + FT_DES_BIT_BLOCK_SIZE); + if (wrong_char) + ft_des_print_error("wrong char in hex iinitialization vector"); +} \ No newline at end of file diff --git a/src/des/ft_des_set_raw_key.c b/src/des/ft_des_set_raw_key.c index 5f59f948..093dd174 100644 --- a/src/des/ft_des_set_raw_key.c +++ b/src/des/ft_des_set_raw_key.c @@ -8,7 +8,7 @@ void ft_des_set_raw_key const char *wrong_char; wrong_char = ft_des_hex_to_bit(ctx->raw_key, ctx->key, - FT_DES_INITIAL_KEY_SIZE); + FT_DES_BIT_BLOCK_SIZE); if (wrong_char) ft_des_print_error("wrong char in hex key"); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 2616318e..83f77a02 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,10 @@ t_algorithm g_algorithms[] = { "des-ecb", ft_des_ecb, }, + { + "des-cbc", + ft_des_cbc, + }, {NULL, NULL} }; diff --git a/t/cases/des_ecb_b64.txt b/t/cases/des_ecb_b64.txt new file mode 100644 index 00000000..b7aacefb --- /dev/null +++ b/t/cases/des_ecb_b64.txt @@ -0,0 +1 @@ +U2FsdGVkX1/+2bai0hAYzG6psw8usaQW diff --git a/t/cases/one_deep_secret.txt b/t/cases/one_deep_secret.txt new file mode 100644 index 00000000..068943f7 --- /dev/null +++ b/t/cases/one_deep_secret.txt @@ -0,0 +1 @@ +one deep secret diff --git a/t/des_tests.c b/t/des_tests.c index a4609b83..df7c3f05 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -630,7 +630,7 @@ int init_ctx() ft_des_init_ctx(&ctx); i = 0; - while(i < FT_DES_INITIAL_KEY_SIZE) + while(i < FT_DES_BIT_BLOCK_SIZE) { _is(ctx.key[i] == 0); i++; @@ -667,6 +667,8 @@ int init_ctx() _is(ctx.raw_password == NULL); _is(ctx.raw_key == NULL); _is(ctx.raw_salt == NULL); + _is(ctx.raw_iv == NULL); + _is(ctx.mode == NULL); _end("shoud init ctx"); } @@ -847,6 +849,58 @@ int convert_bytes_to_bits() _end("should convert 8 bytes to 64 bits"); } +int convert_bits_to_bytes() +{ + t_byte1 bits[FT_DES_BIT_BLOCK_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 expected[FT_DES_BYTE_BLOCK_SIZE] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }; + t_byte1 actual[FT_DES_BIT_BLOCK_SIZE]; + int i; + + ft_bzero(actual, FT_DES_BIT_BLOCK_SIZE); + ft_des_bits_to_bytes(bits, FT_DES_BIT_BLOCK_SIZE, actual, + FT_DES_BYTE_BLOCK_SIZE); + + i = 0; + while(i < FT_DES_BYTE_BLOCK_SIZE) + { + _is(expected[i] == actual[i]); + i++; + } + t_byte1 bits1[FT_DES_BIT_BLOCK_SIZE] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 1, 0, 0, 0, 1, 0, + 0, 0, 1, 1, 0, 0, 1, 1, + 0, 1, 0, 0, 0, 1, 0, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 1, 0, 0, 1, 1, 0, + 0, 1, 1, 1, 0, 1, 1, 1, + }; + t_byte1 expected1[FT_DES_BYTE_BLOCK_SIZE] = { + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + }; + ft_des_bits_to_bytes(bits1, FT_DES_BIT_BLOCK_SIZE, actual, + FT_DES_BYTE_BLOCK_SIZE); + i = 0; + while(i < FT_DES_BYTE_BLOCK_SIZE) + { + _is(expected1[i] == actual[i]); + i++; + } + _end("should convert 64 bits to 8 bytes"); +} + int des_tests() { _should(perform_initial_permutation); @@ -869,5 +923,6 @@ int des_tests() _should(convert_hex_string_to_bytes); _should(convert_short_hex_string_to_bytes); _should(convert_bytes_to_bits); + _should(convert_bits_to_bytes); return 0; } \ No newline at end of file