diff --git a/Makefile b/Makefile index 4684ff59..ec0a9a50 100644 --- a/Makefile +++ b/Makefile @@ -130,10 +130,12 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_init_ctx.c \ ft_des_print_error.c \ ft_des_arg_parsers.c \ - ft_des_ecb_process.c \ - ft_des_ecb_process_chunk.c \ + ft_des_ecb_encrypt.c \ + ft_des_ecb_decrypt.c \ + ft_des_ecb_encode_process_chunk.c \ ft_des_hex_to_bit_key.c \ - ft_des_ecb_finish_process.c + ft_des_ecb_finish_encrypt.c + # ft_des_ecb_finish_decrypt.c SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_des.h b/inc/ft_des.h index 84f3dbea..eb5970fb 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -14,6 +14,7 @@ # define FT_DES_H # include +# include "ft_base64.h" # define FT_DES_BYTE_BLOCK_SIZE 8 # define FT_DES_BIT_BLOCK_SIZE 64 @@ -38,12 +39,16 @@ typedef struct s_des_ctx int input_fd; int output_fd; int decode; + int b64; int output_in_base64; int readed; t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 key[FT_DES_INITIAL_KEY_SIZE]; t_byte1 round_keys[FT_DES_ROUND_COUNT] [FT_DES_ROUND_KEY_SIZE]; + t_base64_ctx b64_ctx; + t_base64_encode_buffer b64_encode_buffer; + t_base64_decode_buffer b64_decode_buffer; } t_des_ctx; typedef int (*t_ft_des_arg_parser_function) @@ -299,19 +304,24 @@ int ft_des_salt_arg_parser t_des_ctx *c ); -void ft_des_ecb_process +void ft_des_ecb_decrypt ( t_des_ctx *ctx ); -void ft_des_ecb_process_chunk +void ft_des_ecb_encrypt +( + t_des_ctx *ctx +); + +void ft_des_ecb_encode_process_chunk ( t_des_ctx *ctx, t_byte8 reaed, t_byte1 buffer[FT_DES_READ_SIZE] ); -void ft_des_ecb_finish_process +void ft_des_ecb_finish_encrypt ( t_des_ctx *ctx ); diff --git a/src/des/ft_des_arg_parsers.c b/src/des/ft_des_arg_parsers.c index ecf952a3..ae3ed30a 100644 --- a/src/des/ft_des_arg_parsers.c +++ b/src/des/ft_des_arg_parsers.c @@ -1,6 +1,21 @@ #include "ft_des.h" #include +int ft_des_base64_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *ctx +) +{ + (void)argc; + (void)argv; + ctx->b64 = 1; + return (++position); +} + + int ft_des_key_arg_parser ( int argc, @@ -17,4 +32,18 @@ int ft_des_key_arg_parser if (wrong_key_char != NULL) ft_des_print_error("there wrong char in key string."); return (position + 2); +} + +int ft_des_decode_arg_parser +( + int argc, + char **argv, + int position, + t_des_ctx *ctx +) +{ + (void)argc; + (void)argv; + ctx->decode = 1; + return (++position); } \ No newline at end of file diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 88a37262..5c17e70c 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -6,6 +6,14 @@ t_des_argument_parser g_arg_parsers[] = { "-k", ft_des_key_arg_parser, }, + { + "-d", + ft_des_decode_arg_parser, + }, + { + "-a", + ft_des_base64_arg_parser, + }, { NULL, NULL}, }; @@ -49,6 +57,15 @@ void ft_des_ecb ft_des_init_ctx(&ctx); parse_args(argc, argv, &ctx); - ft_des_ecb_process(&ctx); + if (ctx.decode) + { + ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); + ft_des_ecb_decrypt(&ctx); + } + else + { + ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys); + ft_des_ecb_encrypt(&ctx); + } exit(0); } \ No newline at end of file diff --git a/src/des/ft_des_ecb_decrypt.c b/src/des/ft_des_ecb_decrypt.c new file mode 100644 index 00000000..ad3bdc94 --- /dev/null +++ b/src/des/ft_des_ecb_decrypt.c @@ -0,0 +1,80 @@ +#include +#include "ft_des.h" +#include "libft.h" + +static t_byte8 buffered_read +( + t_des_ctx *ctx, + t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE] +) +{ + t_byte8 total_readed; + t_byte8 current_read; + + total_readed = 0; + current_read = 0; + while(total_readed < FT_DES_BYTE_BLOCK_SIZE) + { + if ((current_read = read( + ctx->input_fd, + buffer + total_readed, + FT_DES_BYTE_BLOCK_SIZE - total_readed)) == 0) + return (total_readed + current_read); + total_readed += current_read; + } + return (total_readed); +} + +static void ouput +( + t_des_ctx *ctx, + t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE] +) +{ + if (ctx->b64) + ft_base64_decode_chunk( + &ctx->b64_ctx, + FT_DES_BYTE_BLOCK_SIZE, buffer, &ctx->b64_decode_buffer); + else + write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); +} + +static void finish +( + t_des_ctx *ctx, + t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE] +) +{ + if (ctx->b64) + { + } + else + { + if (buffer[7] < 0 || buffer[7] > 8) + ft_des_print_error("wrong padding"); + write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE - buffer[7]); + } +} + +void ft_des_ecb_decrypt +( + t_des_ctx *c +) +{ + t_byte8 readed; + t_byte8 last_read; + t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; + t_byte1 message[FT_DES_BYTE_BLOCK_SIZE]; + + last_read = 0; + while((readed = buffered_read(c, buffer))) + { + if (readed != FT_DES_BYTE_BLOCK_SIZE) + ft_des_print_error("wrong message size"); + if (last_read) + ouput(c, message); + ft_des_process_block(buffer, c->round_keys, message); + last_read = readed; + } + finish(c, message); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_process_chunk.c b/src/des/ft_des_ecb_encode_process_chunk.c similarity index 80% rename from src/des/ft_des_ecb_process_chunk.c rename to src/des/ft_des_ecb_encode_process_chunk.c index f24a2f21..1d8d799a 100644 --- a/src/des/ft_des_ecb_process_chunk.c +++ b/src/des/ft_des_ecb_encode_process_chunk.c @@ -10,10 +10,18 @@ static void ft_des_ecb_write { t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; ft_des_process_block(buffer, ctx->round_keys, cyphertext); - write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); + if (ctx->b64) + ft_base64_encode_chunk( + &ctx->b64_ctx, + FT_DES_BYTE_BLOCK_SIZE, + cyphertext, + &ctx->b64_encode_buffer + ); + else + write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } -void ft_des_ecb_process_chunk +void ft_des_ecb_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_ecb_encrypt.c new file mode 100644 index 00000000..d7df0b3c --- /dev/null +++ b/src/des/ft_des_ecb_encrypt.c @@ -0,0 +1,16 @@ +#include +#include "ft_des.h" +#include "ft_base64.h" + +void ft_des_ecb_encrypt +( + t_des_ctx *ctx +) +{ + t_byte1 buffer[FT_DES_READ_SIZE]; + t_byte8 readed; + + 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); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_finish_decrypt.c b/src/des/ft_des_ecb_finish_decrypt.c new file mode 100644 index 00000000..4d13c797 --- /dev/null +++ b/src/des/ft_des_ecb_finish_decrypt.c @@ -0,0 +1,16 @@ +#include "ft_des.h" + +void ft_des_ecb_finish_decrypt +( + t_des_ctx *ctx, + t_des_decrypt_buffer *buff +) +{ + t_byte1 idx; + + idx = 0; + ctx->readed += buff->readed[buff->current_buffer]; + if (ctx->readed % FT_DES_BYTE_BLOCK_SIZE != 0) + ft_des_print_error("wrong message size, maybe corrupted?"); + while (idx + FT_DES_BYTE_BLOCK_SIZE <=) +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_finish_process.c b/src/des/ft_des_ecb_finish_encrypt.c similarity index 59% rename from src/des/ft_des_ecb_finish_process.c rename to src/des/ft_des_ecb_finish_encrypt.c index 8b2134f1..33ad6dd7 100644 --- a/src/des/ft_des_ecb_finish_process.c +++ b/src/des/ft_des_ecb_finish_encrypt.c @@ -1,7 +1,7 @@ #include "ft_des.h" #include -void ft_des_ecb_finish_process +void ft_des_ecb_finish_encrypt ( t_des_ctx *ctx ) @@ -19,5 +19,14 @@ void ft_des_ecb_finish_process buffer_index++; } ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext); - write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); + if (ctx->b64) + { + ft_base64_encode_chunk(&ctx->b64_ctx, FT_DES_BYTE_BLOCK_SIZE, + cyphertext, + &ctx->b64_encode_buffer + ); + ft_base64_encode_finish(&ctx->b64_ctx, &ctx->b64_encode_buffer); + } + else + write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } \ No newline at end of file diff --git a/src/des/ft_des_ecb_process.c b/src/des/ft_des_ecb_process.c deleted file mode 100644 index c3a1e25d..00000000 --- a/src/des/ft_des_ecb_process.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include "ft_des.h" - -void ft_des_ecb_process -( - t_des_ctx *ctx -) -{ - t_byte1 buffer[FT_DES_READ_SIZE]; - t_byte8 readed; - - 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_process_chunk(ctx, readed, buffer); - ft_des_ecb_finish_process(ctx); -} \ 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 35ce1546..896fc9e9 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -19,7 +19,11 @@ void ft_des_init_ctx ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE); ctx->readed = 0; ctx->decode = 0; + ctx->b64 = 0; ctx->output_in_base64 = 0; ctx->input_fd = STDIN_FILENO; ctx->output_fd = STDOUT_FILENO; + ft_base64_init(&ctx->b64_ctx); + ft_base64_init_encode_buffer(&ctx->b64_encode_buffer); + ft_base64_init_decode_buffer(&ctx->b64_decode_buffer); } \ No newline at end of file diff --git a/src/des/ft_des_print_error.c b/src/des/ft_des_print_error.c index d48aeb70..8796b3fc 100644 --- a/src/des/ft_des_print_error.c +++ b/src/des/ft_des_print_error.c @@ -1,3 +1,4 @@ +#include #include "ft_des.h" #include "libft.h" @@ -6,8 +7,8 @@ void ft_des_print_error const char *error ) { - ft_putstr("des: "); - ft_putstr(error); - ft_putstr("\n"); + ft_putstr_fd("des: ", STDERR_FILENO); + ft_putstr_fd(error, STDERR_FILENO); + ft_putstr_fd("\n", STDERR_FILENO); exit(1); } \ No newline at end of file diff --git a/t/cases/foo_bar.txt b/t/cases/foo_bar.txt new file mode 100644 index 00000000..d675fa44 --- /dev/null +++ b/t/cases/foo_bar.txt @@ -0,0 +1 @@ +foo bar diff --git a/t/cases/foo_bar_enc.txt b/t/cases/foo_bar_enc.txt new file mode 100644 index 00000000..1663167f --- /dev/null +++ b/t/cases/foo_bar_enc.txt @@ -0,0 +1 @@ +a‘w@¦šmu0ÄØ7¡¸L \ No newline at end of file