From a73c34859b262d3e028183c3ec53a1b11013cca1 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 10 Apr 2019 01:09:38 +0300 Subject: [PATCH] working ecb and cbc --- inc/ft_des.h | 3 ++- src/des/ft_des_cbc.c | 26 +++++++++++++++++++++++-- src/des/ft_des_decrypt.c | 2 +- src/des/ft_des_decryption_key_routine.c | 5 +++-- src/des/ft_des_ecb.c | 5 +++-- src/des/ft_des_encode_process_chunk.c | 2 +- src/des/ft_des_finish_encrypt.c | 2 +- src/des/ft_des_init_ctx.c | 3 ++- t/cases/one_deep_secret_enc.txt | 1 + t/des_tests.c | 3 ++- 10 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 t/cases/one_deep_secret_enc.txt diff --git a/inc/ft_des.h b/inc/ft_des.h index 37c3341c..a9510af6 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -57,7 +57,8 @@ typedef struct s_des_ctx const char *raw_salt; const char *raw_key; const char *raw_iv; - t_ft_des_mode mode; + t_ft_des_mode encrypt; + t_ft_des_mode decrypt; } t_des_ctx; typedef int (*t_ft_des_arg_parser_function) diff --git a/src/des/ft_des_cbc.c b/src/des/ft_des_cbc.c index 769e66c2..7a91a18a 100644 --- a/src/des/ft_des_cbc.c +++ b/src/des/ft_des_cbc.c @@ -16,7 +16,7 @@ static void xor } } -static void mode +static void encrypt ( t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], @@ -25,6 +25,7 @@ static void mode ) { 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); @@ -35,6 +36,26 @@ static void mode FT_DES_BIT_BLOCK_SIZE); } +static void decrypt +( + 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 output_bits[FT_DES_BIT_BLOCK_SIZE]; + + ft_des_process_block(input, keys, output); + ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, output_bits, + FT_DES_BIT_BLOCK_SIZE); + xor(output_bits, iv); + ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, iv, + FT_DES_BIT_BLOCK_SIZE); + ft_des_bits_to_bytes(output_bits, FT_DES_BIT_BLOCK_SIZE, output, + FT_DES_BYTE_BLOCK_SIZE); +} + void ft_des_cbc ( int argc, @@ -47,6 +68,7 @@ void ft_des_cbc 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; + ctx.encrypt = encrypt; + ctx.decrypt = decrypt; ft_des(&ctx); } \ No newline at end of file diff --git a/src/des/ft_des_decrypt.c b/src/des/ft_des_decrypt.c index 22d21e98..c61a1f70 100644 --- a/src/des/ft_des_decrypt.c +++ b/src/des/ft_des_decrypt.c @@ -44,7 +44,7 @@ void ft_des_decrypt ft_des_print_error("wrong message size"); if (last_read) write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE); - c->mode(buffer, c->round_keys, c->iv, message); + c->decrypt(buffer, c->round_keys, c->iv, message); last_read = readed; } if (message[7] < 0 || message[7] > 8) diff --git a/src/des/ft_des_decryption_key_routine.c b/src/des/ft_des_decryption_key_routine.c index 0c45cc0a..421a0745 100644 --- a/src/des/ft_des_decryption_key_routine.c +++ b/src/des/ft_des_decryption_key_routine.c @@ -32,13 +32,14 @@ void ft_des_decryption_key_routine char pass[128]; if (ctx->raw_password || !ctx->raw_key) get_salt(ctx, salt); - if (!ctx->raw_password && !ctx->raw_key) + if (!ctx->raw_key && !ctx->raw_password) { ft_des_get_password(pass); ctx->raw_password = pass; + } + if (ctx->raw_password) 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) diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 34b10279..e31489c8 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -1,6 +1,6 @@ #include "ft_des.h" -static void mode +static void encrypt ( t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], @@ -22,6 +22,7 @@ void ft_des_ecb ft_des_init_ctx(&ctx); ft_des_parse_args(argc, argv, &ctx); - ctx.mode = mode; + ctx.encrypt = encrypt; + ctx.decrypt = encrypt; ft_des(&ctx); } \ No newline at end of file diff --git a/src/des/ft_des_encode_process_chunk.c b/src/des/ft_des_encode_process_chunk.c index d701baf4..e941970c 100644 --- a/src/des/ft_des_encode_process_chunk.c +++ b/src/des/ft_des_encode_process_chunk.c @@ -9,7 +9,7 @@ static void ft_des_ecb_write ) { t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; - ctx->mode(buffer, ctx->round_keys, ctx->iv, cyphertext); + ctx->encrypt(buffer, ctx->round_keys, ctx->iv, cyphertext); write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } diff --git a/src/des/ft_des_finish_encrypt.c b/src/des/ft_des_finish_encrypt.c index 21800894..460fa406 100644 --- a/src/des/ft_des_finish_encrypt.c +++ b/src/des/ft_des_finish_encrypt.c @@ -18,6 +18,6 @@ void ft_des_finish_encrypt ctx->buffer[buffer_index] = padding_size; buffer_index++; } - ctx->mode(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext); + ctx->encrypt(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_init_ctx.c b/src/des/ft_des_init_ctx.c index b5674004..7b99969b 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -27,5 +27,6 @@ void ft_des_init_ctx ctx->raw_salt = NULL; ctx->raw_key = NULL; ctx->raw_iv = NULL; - ctx->mode = NULL; + ctx->encrypt = NULL; + ctx->decrypt = NULL; } \ No newline at end of file diff --git a/t/cases/one_deep_secret_enc.txt b/t/cases/one_deep_secret_enc.txt new file mode 100644 index 00000000..ed6d39a8 --- /dev/null +++ b/t/cases/one_deep_secret_enc.txt @@ -0,0 +1 @@ +Salted__oñîa³(_@Ž  Á=_€<ý·©üBúÝr^q \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index df7c3f05..23a4b452 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -668,7 +668,8 @@ int init_ctx() _is(ctx.raw_key == NULL); _is(ctx.raw_salt == NULL); _is(ctx.raw_iv == NULL); - _is(ctx.mode == NULL); + _is(ctx.encrypt == NULL); + _is(ctx.decrypt == NULL); _end("shoud init ctx"); }