des-ecb done
This commit is contained in:
parent
a5b4066b3f
commit
19b52a1deb
12 changed files with 157 additions and 84 deletions
6
Makefile
6
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 \
|
||||
|
|
23
inc/ft_des.h
23
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
|
||||
|
|
40
src/des/ft_des_decryption_key_routine.c
Normal file
40
src/des/ft_des_decryption_key_routine.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
25
src/des/ft_des_derive_key.c
Normal file
25
src/des/ft_des_derive_key.c
Normal file
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,76 +1,33 @@
|
|||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
23
src/des/ft_des_get_password.c
Normal file
23
src/des/ft_des_get_password.c
Normal file
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
|
|
14
src/des/ft_des_set_raw_key.c
Normal file
14
src/des/ft_des_set_raw_key.c
Normal file
|
@ -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");
|
||||
}
|
|
@ -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++;
|
||||
|
|
Loading…
Reference in a new issue