diff --git a/Makefile b/Makefile index ec383f65..c2204da8 100644 --- a/Makefile +++ b/Makefile @@ -138,11 +138,15 @@ 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_arg_parsers.c \ ft_des_ecb.c \ ft_des_init_ctx.c \ + ft_des_set_raw_key.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_arg_parsers.c \ ft_des_ecb_encrypt.c \ ft_des_ecb_decrypt.c \ ft_des_ecb_decrypt_b64.c \ diff --git a/inc/ft_des.h b/inc/ft_des.h index d3abdff2..8f1c0ad5 100644 --- a/inc/ft_des.h +++ b/inc/ft_des.h @@ -41,7 +41,6 @@ typedef struct s_des_ctx int b64; int readed; t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; - t_byte1 salt[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 key[FT_DES_INITIAL_KEY_SIZE]; t_byte1 iv[FT_DES_BIT_BLOCK_SIZE]; t_byte1 round_keys[FT_DES_ROUND_COUNT] @@ -369,4 +368,26 @@ void ft_des_encryption_key_routine t_des_ctx *ctx ); +void ft_des_decryption_key_routine +( + t_des_ctx *ctx +); + +void ft_des_set_raw_key +( + t_des_ctx *ctx +); + +void ft_des_get_password +( + t_des_ctx *ctx +); + +void ft_des_derive_key +( + t_byte1 key[FT_DES_BIT_BLOCK_SIZE], + char salt[FT_DES_BYTE_BLOCK_SIZE], + char *pass +); + #endif diff --git a/src/des/ft_des_decryption_key_routine.c b/src/des/ft_des_decryption_key_routine.c new file mode 100644 index 00000000..94800b32 --- /dev/null +++ b/src/des/ft_des_decryption_key_routine.c @@ -0,0 +1,40 @@ +#include +#include +#include "ft_des.h" +#include "libft.h" + +static void get_salt +( + t_des_ctx *c, + char salt[FT_DES_BYTE_BLOCK_SIZE] +) +{ + char buffer[FT_DES_BYTE_BLOCK_SIZE]; + int readed; + + readed = 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); + if (readed != FT_DES_BYTE_BLOCK_SIZE) + ft_des_print_error("error reading input"); + ft_memcpy(salt, buffer, FT_DES_BYTE_BLOCK_SIZE); +} + +void ft_des_decryption_key_routine +( + t_des_ctx *ctx +) +{ + char salt[FT_DES_BYTE_BLOCK_SIZE]; + + 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); + } + if (ctx->raw_key) + ft_des_set_raw_key(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 new file mode 100644 index 00000000..cfb3a928 --- /dev/null +++ b/src/des/ft_des_derive_key.c @@ -0,0 +1,25 @@ +#include "ft_des.h" +#include "ft_pbkdf2.h" +#include "libft.h" + +void ft_des_derive_key +( + t_byte1 key[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]; + + pbkdf_ctx.iterations = 10000; + pbkdf_ctx.key = byte_key; + pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE; + pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE; + 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); +} \ No newline at end of file diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 20dd3373..ca8602cf 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -87,7 +87,6 @@ void ft_des_ecb } else { - ft_des_encryption_key_routine(&ctx); if (ctx.b64) ft_des_ecb_encrypt_b64(&ctx); else diff --git a/src/des/ft_des_ecb_decrypt.c b/src/des/ft_des_ecb_decrypt.c index c8953489..941725de 100644 --- a/src/des/ft_des_ecb_decrypt.c +++ b/src/des/ft_des_ecb_decrypt.c @@ -35,6 +35,7 @@ void ft_des_ecb_decrypt t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 message[FT_DES_BYTE_BLOCK_SIZE]; + ft_des_decryption_key_routine(c); ft_des_generate_decryption_round_keys(c->key, c->round_keys); last_read = 0; while((readed = buffered_read(c, buffer))) diff --git a/src/des/ft_des_ecb_encrypt.c b/src/des/ft_des_ecb_encrypt.c index e76cc32a..c2adc980 100644 --- a/src/des/ft_des_ecb_encrypt.c +++ b/src/des/ft_des_ecb_encrypt.c @@ -10,12 +10,8 @@ void ft_des_ecb_encrypt t_byte1 buffer[FT_DES_READ_SIZE]; t_byte8 readed; + ft_des_encryption_key_routine(ctx); ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys); - if (ctx->raw_password) - { - write(ctx->output_fd, "Salted__", 8); - write(ctx->output_fd, ctx->salt, FT_DES_BYTE_BLOCK_SIZE); - } 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); diff --git a/src/des/ft_des_encryption_key_routine.c b/src/des/ft_des_encryption_key_routine.c index 58d94c10..028a7fbc 100644 --- a/src/des/ft_des_encryption_key_routine.c +++ b/src/des/ft_des_encryption_key_routine.c @@ -1,76 +1,33 @@ #include -#include #include #include "ft_des.h" -#include "ft_pbkdf2.h" #include "libft.h" -static void derive_key -( - t_des_ctx *ctx -) -{ - t_pbkdf2_sha256_ctx pbkdf_ctx; - t_byte1 byte_key[FT_DES_BYTE_BLOCK_SIZE]; - - pbkdf_ctx.iterations = 10000; - pbkdf_ctx.key = byte_key; - pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE; - pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE; - pbkdf_ctx.pass_len = ft_strlen((char *)ctx->raw_password); - pbkdf_ctx.pass = (t_byte1 *)ctx->raw_password; - pbkdf_ctx.salt = (t_byte1 *)ctx->salt; - ft_pbkdf2_sha256(&pbkdf_ctx); - ft_des_byte_to_bits(byte_key, FT_DES_BYTE_BLOCK_SIZE, ctx->key, - FT_DES_INITIAL_KEY_SIZE); -} - -static void get_pass -( - char pass[128] -) -{ - char *first_try[128]; - char *second_try[128]; - - 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)) - exit(1); - ft_strcpy((char *)second_try, getpass("retype password:")); - if (ft_strcmp((const char *)first_try, (const char *)second_try) != 0) - ft_des_print_error("passwords doesn't match"); - ft_strcpy(pass, (const char *)first_try); -} - static void get_salt ( + t_des_ctx *c, char salt[FT_DES_BYTE_BLOCK_SIZE] ) { - int fd; + int fd; - fd = open("/dev/random", O_RDONLY); - if (fd == -1) + if (c->raw_salt) { - perror("des"); - exit(1); + ft_bzero(salt, FT_DES_BYTE_BLOCK_SIZE); + if (ft_des_hex_to_byte(c->raw_salt, (t_byte1 *)salt, + FT_DES_BYTE_BLOCK_SIZE)) + ft_des_print_error("wrong char in salt"); + } + else + { + fd = open("/dev/random", O_RDONLY); + if (fd == -1) + { + perror("des"); + exit(1); + } + read(fd, salt, FT_DES_BYTE_BLOCK_SIZE); } - read(fd, salt, FT_DES_BYTE_BLOCK_SIZE); -} - -static void get_key -( - t_des_ctx *ctx -) -{ - const char *wrong_char; - - wrong_char = ft_des_hex_to_bit(ctx->raw_key, ctx->key, - FT_DES_INITIAL_KEY_SIZE); - if (wrong_char) - ft_des_print_error("wrong char in hex key"); } void ft_des_encryption_key_routine @@ -78,19 +35,19 @@ void ft_des_encryption_key_routine t_des_ctx *ctx ) { - char pass[128]; + char salt[FT_DES_BYTE_BLOCK_SIZE]; if (ctx->raw_key) + ft_des_set_raw_key(ctx); + else { - get_key(ctx); - return ; + ft_des_get_password(ctx); + get_salt(ctx, salt); + ft_des_derive_key(ctx->key, salt, (char *)ctx->raw_password); } - if (!ctx->raw_password) + if (ctx->raw_password || !ctx->raw_key) { - get_pass(pass); - ctx->raw_password = pass; + write(ctx->output_fd, "Salted__", 8); + write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE); } - if (!ctx->raw_salt) - get_salt((char *)ctx->salt); - derive_key(ctx); } \ No newline at end of file diff --git a/src/des/ft_des_get_password.c b/src/des/ft_des_get_password.c new file mode 100644 index 00000000..786d664e --- /dev/null +++ b/src/des/ft_des_get_password.c @@ -0,0 +1,23 @@ +#include "ft_des.h" +#include "libft.h" + +void ft_des_get_password +( + t_des_ctx *ctx +) +{ + 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)) + exit(1); + ft_strcpy((char *)second_try, getpass("retype password:")); + if (ft_strcmp((const char *)first_try, (const char *)second_try) != 0) + ft_des_print_error("passwords doesn't match"); + ctx->raw_password = (char *)second_try; +} \ 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 78a473f7..be400aad 100644 --- a/src/des/ft_des_init_ctx.c +++ b/src/des/ft_des_init_ctx.c @@ -17,7 +17,6 @@ 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; diff --git a/src/des/ft_des_set_raw_key.c b/src/des/ft_des_set_raw_key.c new file mode 100644 index 00000000..5f59f948 --- /dev/null +++ b/src/des/ft_des_set_raw_key.c @@ -0,0 +1,14 @@ +#include "ft_des.h" + +void ft_des_set_raw_key +( + t_des_ctx *ctx +) +{ + const char *wrong_char; + + wrong_char = ft_des_hex_to_bit(ctx->raw_key, ctx->key, + FT_DES_INITIAL_KEY_SIZE); + if (wrong_char) + ft_des_print_error("wrong char in hex key"); +} \ No newline at end of file diff --git a/t/des_tests.c b/t/des_tests.c index 73917c13..a4609b83 100644 --- a/t/des_tests.c +++ b/t/des_tests.c @@ -655,12 +655,6 @@ int init_ctx() } 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++;