diff --git a/Makefile b/Makefile index ec0a9a50..409b230c 100644 --- a/Makefile +++ b/Makefile @@ -132,6 +132,8 @@ DES_SRC = ft_des_initial_permutation.c \ ft_des_arg_parsers.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_hex_to_bit_key.c \ ft_des_ecb_finish_encrypt.c diff --git a/inc/ft_des.h b/inc/ft_des.h index eb5970fb..407a9966 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -14,7 +14,6 @@ # define FT_DES_H # include -# include "ft_base64.h" # define FT_DES_BYTE_BLOCK_SIZE 8 # define FT_DES_BIT_BLOCK_SIZE 64 @@ -46,9 +45,6 @@ typedef struct s_des_ctx 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) @@ -309,11 +305,21 @@ void ft_des_ecb_decrypt t_des_ctx *ctx ); +void ft_des_ecb_decrypt_b64 +( + t_des_ctx *ctx +); + void ft_des_ecb_encrypt ( t_des_ctx *ctx ); +void ft_des_ecb_encrypt_b64 +( + t_des_ctx *ctx +); + void ft_des_ecb_encode_process_chunk ( t_des_ctx *ctx, diff --git a/src/base64/ft_base64_decode_chunk.c b/src/base64/ft_base64_decode_chunk.c index a800d452..b3d409d1 100644 --- a/src/base64/ft_base64_decode_chunk.c +++ b/src/base64/ft_base64_decode_chunk.c @@ -11,7 +11,12 @@ static void ft_base64_decode_write t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE]; ft_base64_decode_transform(ctx, block, decoded_block); - write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE); + if (block[3] == '=' && block[2] == '=') + write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE - 2); + else if (block[3] == '=') + write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE - 1); + else + write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE); } void ft_base64_decode_chunk diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 5c17e70c..1fd59324 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -60,12 +60,18 @@ void ft_des_ecb if (ctx.decode) { ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); - ft_des_ecb_decrypt(&ctx); + if (ctx.b64) + ft_des_ecb_decrypt_b64(&ctx); + else + ft_des_ecb_decrypt(&ctx); } else { ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys); - ft_des_ecb_encrypt(&ctx); + if (ctx.b64) + ft_des_ecb_encrypt_b64(&ctx); + else + 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 index ad3bdc94..87413770 100644 --- a/src/des/ft_des_ecb_decrypt.c +++ b/src/des/ft_des_ecb_decrypt.c @@ -25,37 +25,6 @@ static t_byte8 buffered_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 @@ -72,9 +41,11 @@ void ft_des_ecb_decrypt if (readed != FT_DES_BYTE_BLOCK_SIZE) ft_des_print_error("wrong message size"); if (last_read) - ouput(c, message); + write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE); ft_des_process_block(buffer, c->round_keys, message); last_read = readed; } - finish(c, message); + if (message[7] < 0 || message[7] > 8) + ft_des_print_error("wrong padding"); + write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]); } \ No newline at end of file diff --git a/src/des/ft_des_ecb_decrypt_b64.c b/src/des/ft_des_ecb_decrypt_b64.c new file mode 100644 index 00000000..11326ce6 --- /dev/null +++ b/src/des/ft_des_ecb_decrypt_b64.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include "ft_des.h" +#include "ft_base64.h" + +static void base64_decode +( + t_des_ctx *ctx, + int pipe_fd[2] +) +{ + t_base64_ctx b64_ctx; + + 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 +( + t_des_ctx *ctx, + int pipe_fd[2] +) +{ + close(pipe_fd[1]); + ctx->input_fd = pipe_fd[0]; + ft_des_ecb_decrypt(ctx); +} + +void ft_des_ecb_decrypt_b64 +( + t_des_ctx *ctx +) +{ + 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) + base64_decode(ctx, pipe_fd); + else if (pid < 0) + ft_des_print_error("failded to create child process"); + else + des_ecb_decrypt(ctx, pipe_fd); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_encode_process_chunk.c b/src/des/ft_des_ecb_encode_process_chunk.c index 1d8d799a..f16dc6d3 100644 --- a/src/des/ft_des_ecb_encode_process_chunk.c +++ b/src/des/ft_des_ecb_encode_process_chunk.c @@ -10,15 +10,7 @@ static void ft_des_ecb_write { t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; ft_des_process_block(buffer, ctx->round_keys, cyphertext); - 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); + write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); } void ft_des_ecb_encode_process_chunk diff --git a/src/des/ft_des_ecb_encrypt_b64.c b/src/des/ft_des_ecb_encrypt_b64.c new file mode 100644 index 00000000..b1e0f047 --- /dev/null +++ b/src/des/ft_des_ecb_encrypt_b64.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include "ft_des.h" +#include "ft_base64.h" + +static void base64_encode +( + t_des_ctx *ctx, + int pipe_fd[2] +) +{ + t_base64_ctx b64_ctx; + + close(pipe_fd[1]); + ft_base64_init(&b64_ctx); + b64_ctx.output_fd = ctx->output_fd; + b64_ctx.input_fd = pipe_fd[0]; + ft_base64_encode(&b64_ctx); +} + +static void des_ecb_encrypt +( + t_des_ctx *ctx, + int pipe_fd[2] +) +{ + close(pipe_fd[0]); + ctx->output_fd = pipe_fd[1]; + ft_des_ecb_encrypt(ctx); +} + +void ft_des_ecb_encrypt_b64 +( + t_des_ctx *ctx +) +{ + 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); + else if (pid < 0) + ft_des_print_error("failded to create child process"); + else + base64_encode(ctx, pipe_fd); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb_finish_encrypt.c b/src/des/ft_des_ecb_finish_encrypt.c index 33ad6dd7..ed0ba282 100644 --- a/src/des/ft_des_ecb_finish_encrypt.c +++ b/src/des/ft_des_ecb_finish_encrypt.c @@ -19,14 +19,5 @@ void ft_des_ecb_finish_encrypt buffer_index++; } ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext); - 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); + 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 896fc9e9..fb75872f 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -23,7 +23,4 @@ void ft_des_init_ctx 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