des cbc and ecb done, decryption with -a flag doesn't work
This commit is contained in:
parent
19b52a1deb
commit
28c7f78365
26 changed files with 466 additions and 147 deletions
18
Makefile
18
Makefile
|
@ -138,24 +138,30 @@ 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_parse_args.c \
|
||||
ft_des_arg_parsers.c \
|
||||
ft_des.c \
|
||||
ft_des_ecb.c \
|
||||
ft_des_cbc.c \
|
||||
ft_des_init_ctx.c \
|
||||
ft_des_set_raw_key.c \
|
||||
ft_des_set_raw_iv.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_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_encrypt.c \
|
||||
ft_des_decrypt.c \
|
||||
ft_des_decrypt_b64.c \
|
||||
ft_des_encrypt_b64.c \
|
||||
ft_des_encode_process_chunk.c \
|
||||
ft_des_hex_to_bit.c \
|
||||
ft_des_hex_to_byte.c \
|
||||
ft_des_byte_to_bits.c \
|
||||
ft_des_ecb_finish_encrypt.c
|
||||
ft_des_bits_to_bytes.c \
|
||||
ft_des_finish_encrypt.c \
|
||||
ft_des_buffered_read.c
|
||||
|
||||
KD2_SRC = ft_hmac_sha256_init_ctx.c \
|
||||
ft_hmac_sha256.c \
|
||||
|
|
95
inc/ft_des.h
95
inc/ft_des.h
|
@ -14,6 +14,7 @@
|
|||
# define FT_DES_H
|
||||
|
||||
# include <stdint.h>
|
||||
# include <stddef.h>
|
||||
|
||||
# define FT_DES_BYTE_BLOCK_SIZE 8
|
||||
# define FT_DES_BIT_BLOCK_SIZE 64
|
||||
|
@ -32,6 +33,13 @@
|
|||
typedef uint64_t t_byte8;
|
||||
typedef unsigned char t_byte1;
|
||||
|
||||
typedef void (*t_ft_des_mode)
|
||||
(
|
||||
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]
|
||||
);
|
||||
|
||||
typedef struct s_des_ctx
|
||||
{
|
||||
|
@ -40,14 +48,16 @@ typedef struct s_des_ctx
|
|||
int decode;
|
||||
int b64;
|
||||
int readed;
|
||||
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
|
||||
t_byte1 key[FT_DES_BIT_BLOCK_SIZE];
|
||||
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE];
|
||||
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||
t_byte1 round_keys[FT_DES_ROUND_COUNT]
|
||||
[FT_DES_ROUND_KEY_SIZE];
|
||||
const char *raw_password;
|
||||
const char *raw_salt;
|
||||
const char *raw_key;
|
||||
const char *raw_iv;
|
||||
t_ft_des_mode mode;
|
||||
} t_des_ctx;
|
||||
|
||||
typedef int (*t_ft_des_arg_parser_function)
|
||||
|
@ -220,10 +230,9 @@ void ft_des_print_error
|
|||
const char *error
|
||||
);
|
||||
|
||||
void ft_des_ecb
|
||||
void ft_des
|
||||
(
|
||||
int argc,
|
||||
char **argv
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_init_ctx
|
||||
|
@ -231,6 +240,13 @@ void ft_des_init_ctx
|
|||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_parse_args
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
int ft_des_key_arg_parser
|
||||
(
|
||||
int argc,
|
||||
|
@ -239,6 +255,14 @@ int ft_des_key_arg_parser
|
|||
t_des_ctx *c
|
||||
);
|
||||
|
||||
int ft_des_iv_arg_parser
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
int position,
|
||||
t_des_ctx *c
|
||||
);
|
||||
|
||||
int ft_des_decode_arg_parser
|
||||
(
|
||||
int argc,
|
||||
|
@ -303,34 +327,51 @@ int ft_des_salt_arg_parser
|
|||
t_des_ctx *c
|
||||
);
|
||||
|
||||
void ft_des_ecb_decrypt
|
||||
void ft_des
|
||||
(
|
||||
t_des_ctx *c
|
||||
);
|
||||
|
||||
void ft_des_ecb
|
||||
(
|
||||
int argc,
|
||||
char **argv
|
||||
);
|
||||
|
||||
void ft_des_cbc
|
||||
(
|
||||
int argc,
|
||||
char **argv
|
||||
);
|
||||
|
||||
void ft_des_decrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_ecb_decrypt_b64
|
||||
void ft_des_decrypt_b64
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_ecb_encrypt
|
||||
void ft_des_encrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_ecb_encrypt_b64
|
||||
void ft_des_encrypt_b64
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_ecb_encode_process_chunk
|
||||
void ft_des_encode_process_chunk
|
||||
(
|
||||
t_des_ctx *ctx,
|
||||
t_byte8 reaed,
|
||||
t_byte1 buffer[FT_DES_READ_SIZE]
|
||||
);
|
||||
|
||||
void ft_des_ecb_finish_encrypt
|
||||
void ft_des_finish_encrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
@ -363,6 +404,14 @@ void ft_des_byte_to_bits
|
|||
t_byte8 bits_len
|
||||
);
|
||||
|
||||
void ft_des_bits_to_bytes
|
||||
(
|
||||
t_byte1 *bits,
|
||||
t_byte8 bits_len,
|
||||
t_byte1 *bytes,
|
||||
t_byte8 byte_len
|
||||
);
|
||||
|
||||
void ft_des_encryption_key_routine
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
|
@ -378,16 +427,36 @@ void ft_des_set_raw_key
|
|||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_get_password
|
||||
void ft_des_set_raw_iv
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
void ft_des_derive_key
|
||||
void ft_des_get_password
|
||||
(
|
||||
char pass[128]
|
||||
);
|
||||
|
||||
void ft_des_derive_key_and_iv
|
||||
(
|
||||
t_byte1 key[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE],
|
||||
char salt[FT_DES_BYTE_BLOCK_SIZE],
|
||||
char *pass
|
||||
);
|
||||
|
||||
void ft_des_parse_args
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
t_des_ctx *ctx
|
||||
);
|
||||
|
||||
int ft_des_buffered_read
|
||||
(
|
||||
int fd,
|
||||
char *buffer,
|
||||
size_t buff_size
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
24
src/des/ft_des.c
Normal file
24
src/des/ft_des.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <stdlib.h>
|
||||
#include "ft_des.h"
|
||||
|
||||
void ft_des
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
{
|
||||
if (ctx->decode)
|
||||
{
|
||||
if (ctx->b64)
|
||||
ft_des_decrypt_b64(ctx);
|
||||
else
|
||||
ft_des_decrypt(ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ctx->b64)
|
||||
ft_des_encrypt_b64(ctx);
|
||||
else
|
||||
ft_des_encrypt(ctx);
|
||||
}
|
||||
exit(0);
|
||||
}
|
|
@ -34,6 +34,22 @@ int ft_des_key_arg_parser
|
|||
return (position + 2);
|
||||
}
|
||||
|
||||
|
||||
int ft_des_iv_arg_parser
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
int position,
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
{
|
||||
if (position + 1 >= argc)
|
||||
ft_des_print_error("there is no initialization vector after -k flag. \
|
||||
type -h for help.");
|
||||
ctx->raw_iv = argv[position + 1];
|
||||
return (position + 2);
|
||||
}
|
||||
|
||||
int ft_des_decode_arg_parser
|
||||
(
|
||||
int argc,
|
||||
|
|
29
src/des/ft_des_bits_to_bytes.c
Normal file
29
src/des/ft_des_bits_to_bytes.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
void ft_des_bits_to_bytes
|
||||
(
|
||||
t_byte1 *bits,
|
||||
t_byte8 bits_len,
|
||||
t_byte1 *bytes,
|
||||
t_byte8 bytes_len
|
||||
)
|
||||
{
|
||||
t_byte8 bits_count;
|
||||
t_byte8 bytes_count;
|
||||
|
||||
bits_count = 0;
|
||||
bytes_count = 0;
|
||||
while(bits_count < bits_len && bytes_count < bytes_len)
|
||||
{
|
||||
bytes[bytes_count] = (bits[bits_count] << 7) |
|
||||
(bits[bits_count + 1] << 6) |
|
||||
(bits[bits_count + 2] << 5) |
|
||||
(bits[bits_count + 3] << 4) |
|
||||
(bits[bits_count + 4] << 3) |
|
||||
(bits[bits_count + 5] << 2) |
|
||||
(bits[bits_count + 6] << 1) |
|
||||
(bits[bits_count + 7]);
|
||||
bytes_count++;
|
||||
bits_count += 8;
|
||||
}
|
||||
}
|
26
src/des/ft_des_buffered_read.c
Normal file
26
src/des/ft_des_buffered_read.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <unistd.h>
|
||||
#include "ft_des.h"
|
||||
|
||||
int ft_des_buffered_read
|
||||
(
|
||||
int fd,
|
||||
char *buffer,
|
||||
size_t buff_size
|
||||
)
|
||||
{
|
||||
size_t total_readed;
|
||||
size_t current_read;
|
||||
|
||||
total_readed = 0;
|
||||
current_read = 0;
|
||||
while(total_readed < buff_size)
|
||||
{
|
||||
if ((current_read = read(
|
||||
fd,
|
||||
buffer + total_readed,
|
||||
buff_size - total_readed)) == 0)
|
||||
return (total_readed + current_read);
|
||||
total_readed += current_read;
|
||||
}
|
||||
return (total_readed);
|
||||
}
|
52
src/des/ft_des_cbc.c
Normal file
52
src/des/ft_des_cbc.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
static void xor
|
||||
(
|
||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
plaintext[i] = plaintext[i] ^ iv[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void mode
|
||||
(
|
||||
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 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);
|
||||
ft_des_bits_to_bytes(input_bits, FT_DES_BIT_BLOCK_SIZE, input,
|
||||
FT_DES_BYTE_BLOCK_SIZE);
|
||||
ft_des_process_block(input, keys, output);
|
||||
ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, iv,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
void ft_des_cbc
|
||||
(
|
||||
int argc,
|
||||
char **argv
|
||||
)
|
||||
{
|
||||
t_des_ctx ctx;
|
||||
|
||||
ft_des_init_ctx(&ctx);
|
||||
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;
|
||||
ft_des(&ctx);
|
||||
}
|
|
@ -25,7 +25,7 @@ static t_byte8 buffered_read
|
|||
return (total_readed);
|
||||
}
|
||||
|
||||
void ft_des_ecb_decrypt
|
||||
void ft_des_decrypt
|
||||
(
|
||||
t_des_ctx *c
|
||||
)
|
||||
|
@ -44,7 +44,7 @@ void ft_des_ecb_decrypt
|
|||
ft_des_print_error("wrong message size");
|
||||
if (last_read)
|
||||
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
|
||||
ft_des_process_block(buffer, c->round_keys, message);
|
||||
c->mode(buffer, c->round_keys, c->iv, message);
|
||||
last_read = readed;
|
||||
}
|
||||
if (message[7] < 0 || message[7] > 8)
|
|
@ -12,13 +12,14 @@ static void base64_decode
|
|||
{
|
||||
t_base64_ctx b64_ctx;
|
||||
|
||||
close(pipe_fd[0]);
|
||||
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
|
||||
static void des_decrypt
|
||||
(
|
||||
t_des_ctx *ctx,
|
||||
int pipe_fd[2]
|
||||
|
@ -26,10 +27,10 @@ static void des_ecb_decrypt
|
|||
{
|
||||
close(pipe_fd[1]);
|
||||
ctx->input_fd = pipe_fd[0];
|
||||
ft_des_ecb_decrypt(ctx);
|
||||
ft_des_decrypt(ctx);
|
||||
}
|
||||
|
||||
void ft_des_ecb_decrypt_b64
|
||||
void ft_des_decrypt_b64
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
|
@ -37,7 +38,6 @@ void ft_des_ecb_decrypt_b64
|
|||
pid_t pid;
|
||||
int pipe_fd[2];
|
||||
|
||||
(void)ctx;
|
||||
if (pipe(pipe_fd))
|
||||
ft_des_print_error("failded to create pipe");
|
||||
pid = fork();
|
||||
|
@ -46,5 +46,5 @@ void ft_des_ecb_decrypt_b64
|
|||
else if (pid < 0)
|
||||
ft_des_print_error("failded to create child process");
|
||||
else
|
||||
des_ecb_decrypt(ctx, pipe_fd);
|
||||
des_decrypt(ctx, pipe_fd);
|
||||
}
|
|
@ -12,10 +12,12 @@ static void get_salt
|
|||
char buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||
int readed;
|
||||
|
||||
readed = read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
|
||||
readed = ft_des_buffered_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);
|
||||
ft_des_print_error("error reading input");
|
||||
if (ft_strncmp(buffer, "Salted__", FT_DES_BYTE_BLOCK_SIZE) != 0)
|
||||
ft_des_print_error("bad magic number");
|
||||
readed = ft_des_buffered_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);
|
||||
|
@ -27,14 +29,18 @@ void ft_des_decryption_key_routine
|
|||
)
|
||||
{
|
||||
char salt[FT_DES_BYTE_BLOCK_SIZE];
|
||||
|
||||
char pass[128];
|
||||
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);
|
||||
ft_des_get_password(pass);
|
||||
ctx->raw_password = pass;
|
||||
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)
|
||||
ft_des_set_raw_iv(ctx);
|
||||
}
|
|
@ -2,24 +2,28 @@
|
|||
#include "ft_pbkdf2.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_des_derive_key
|
||||
void ft_des_derive_key_and_iv
|
||||
(
|
||||
t_byte1 key[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 iv[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];
|
||||
t_byte1 key_iv[FT_DES_BYTE_BLOCK_SIZE * 2];
|
||||
t_byte1 key_iv_bit[FT_DES_BIT_BLOCK_SIZE * 2];
|
||||
|
||||
pbkdf_ctx.iterations = 10000;
|
||||
pbkdf_ctx.key = byte_key;
|
||||
pbkdf_ctx.key = key_iv;
|
||||
pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE;
|
||||
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE;
|
||||
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE * 2;
|
||||
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);
|
||||
ft_des_byte_to_bits(key_iv, FT_DES_BYTE_BLOCK_SIZE * 2, key_iv_bit,
|
||||
FT_DES_BIT_BLOCK_SIZE * 2);
|
||||
ft_memcpy(key, key_iv_bit, FT_DES_BIT_BLOCK_SIZE);
|
||||
ft_memcpy(iv, key_iv_bit + FT_DES_BIT_BLOCK_SIZE, FT_DES_BIT_BLOCK_SIZE);
|
||||
}
|
|
@ -1,71 +1,15 @@
|
|||
#include "ft_des.h"
|
||||
#include "ft_pbkdf2.h"
|
||||
#include "libft.h"
|
||||
|
||||
t_des_argument_parser g_arg_parsers[] = {
|
||||
{
|
||||
"-d",
|
||||
ft_des_decode_arg_parser,
|
||||
},
|
||||
{
|
||||
"-e",
|
||||
ft_des_encode_arg_parser,
|
||||
},
|
||||
{
|
||||
"-a",
|
||||
ft_des_base64_arg_parser,
|
||||
},
|
||||
{
|
||||
"-i",
|
||||
ft_des_input_file_arg_parser,
|
||||
},
|
||||
{
|
||||
"-o",
|
||||
ft_des_output_file_arg_parser,
|
||||
},
|
||||
{
|
||||
"-k",
|
||||
ft_des_key_arg_parser,
|
||||
},
|
||||
{
|
||||
"-p",
|
||||
ft_des_password_arg_parser,
|
||||
},
|
||||
{
|
||||
"-s",
|
||||
ft_des_salt_arg_parser,
|
||||
},
|
||||
{ NULL, NULL},
|
||||
};
|
||||
|
||||
static void parse_args
|
||||
static void mode
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
t_des_ctx *ctx
|
||||
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]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
char *current_arg;
|
||||
t_des_argument_parser *parser_walker;
|
||||
|
||||
i = 1;
|
||||
while(i < argc)
|
||||
{
|
||||
current_arg = argv[i];
|
||||
parser_walker = g_arg_parsers;
|
||||
while(parser_walker->arg_parser)
|
||||
{
|
||||
if (ft_strcmp(current_arg, parser_walker->arg) == 0)
|
||||
{
|
||||
i = parser_walker->arg_parser(argc, argv, i, ctx);
|
||||
break;
|
||||
}
|
||||
parser_walker++;
|
||||
}
|
||||
if (!parser_walker->arg_parser)
|
||||
ft_des_print_error("wrong argument. type -h for help.");
|
||||
}
|
||||
(void)iv;
|
||||
ft_des_process_block(input, keys, output);
|
||||
}
|
||||
|
||||
void ft_des_ecb
|
||||
|
@ -74,23 +18,10 @@ void ft_des_ecb
|
|||
char **argv
|
||||
)
|
||||
{
|
||||
t_des_ctx ctx;
|
||||
t_des_ctx ctx;
|
||||
|
||||
ft_des_init_ctx(&ctx);
|
||||
parse_args(argc, argv, &ctx);
|
||||
if (ctx.decode)
|
||||
{
|
||||
if (ctx.b64)
|
||||
ft_des_ecb_decrypt_b64(&ctx);
|
||||
else
|
||||
ft_des_ecb_decrypt(&ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ctx.b64)
|
||||
ft_des_ecb_encrypt_b64(&ctx);
|
||||
else
|
||||
ft_des_ecb_encrypt(&ctx);
|
||||
}
|
||||
exit(0);
|
||||
ft_des_parse_args(argc, argv, &ctx);
|
||||
ctx.mode = mode;
|
||||
ft_des(&ctx);
|
||||
}
|
|
@ -9,11 +9,11 @@ static void ft_des_ecb_write
|
|||
)
|
||||
{
|
||||
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
||||
ft_des_process_block(buffer, ctx->round_keys, cyphertext);
|
||||
ctx->mode(buffer, ctx->round_keys, ctx->iv, cyphertext);
|
||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
void ft_des_ecb_encode_process_chunk
|
||||
void ft_des_encode_process_chunk
|
||||
(
|
||||
t_des_ctx *ctx,
|
||||
t_byte8 readed,
|
|
@ -2,7 +2,7 @@
|
|||
#include "ft_des.h"
|
||||
#include "ft_base64.h"
|
||||
|
||||
void ft_des_ecb_encrypt
|
||||
void ft_des_encrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
|
@ -13,6 +13,6 @@ void ft_des_ecb_encrypt
|
|||
ft_des_encryption_key_routine(ctx);
|
||||
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_encode_process_chunk(ctx, readed, buffer);
|
||||
ft_des_ecb_finish_encrypt(ctx);
|
||||
ft_des_encode_process_chunk(ctx, readed, buffer);
|
||||
ft_des_finish_encrypt(ctx);
|
||||
}
|
|
@ -19,7 +19,7 @@ static void base64_encode
|
|||
ft_base64_encode(&b64_ctx);
|
||||
}
|
||||
|
||||
static void des_ecb_encrypt
|
||||
static void des_encrypt
|
||||
(
|
||||
t_des_ctx *ctx,
|
||||
int pipe_fd[2]
|
||||
|
@ -27,10 +27,10 @@ static void des_ecb_encrypt
|
|||
{
|
||||
close(pipe_fd[0]);
|
||||
ctx->output_fd = pipe_fd[1];
|
||||
ft_des_ecb_encrypt(ctx);
|
||||
ft_des_encrypt(ctx);
|
||||
}
|
||||
|
||||
void ft_des_ecb_encrypt_b64
|
||||
void ft_des_encrypt_b64
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
|
@ -38,12 +38,11 @@ void ft_des_ecb_encrypt_b64
|
|||
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);
|
||||
des_encrypt(ctx, pipe_fd);
|
||||
else if (pid < 0)
|
||||
ft_des_print_error("failded to create child process");
|
||||
else
|
|
@ -27,6 +27,7 @@ static void get_salt
|
|||
exit(1);
|
||||
}
|
||||
read(fd, salt, FT_DES_BYTE_BLOCK_SIZE);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,16 +37,24 @@ void ft_des_encryption_key_routine
|
|||
)
|
||||
{
|
||||
char salt[FT_DES_BYTE_BLOCK_SIZE];
|
||||
char pass[128];
|
||||
|
||||
if (ctx->raw_iv)
|
||||
ft_des_set_raw_iv(ctx);
|
||||
if (ctx->raw_key)
|
||||
ft_des_set_raw_key(ctx);
|
||||
else
|
||||
{
|
||||
ft_des_get_password(ctx);
|
||||
if (!ctx->raw_password)
|
||||
{
|
||||
ft_des_get_password(pass);
|
||||
ctx->raw_password = pass;
|
||||
}
|
||||
get_salt(ctx, salt);
|
||||
ft_des_derive_key(ctx->key, salt, (char *)ctx->raw_password);
|
||||
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
|
||||
(char *)ctx->raw_password);
|
||||
}
|
||||
if (ctx->raw_password || !ctx->raw_key)
|
||||
if (ctx->raw_password)
|
||||
{
|
||||
write(ctx->output_fd, "Salted__", 8);
|
||||
write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "ft_des.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_des_ecb_finish_encrypt
|
||||
void ft_des_finish_encrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
|
@ -18,6 +18,6 @@ void ft_des_ecb_finish_encrypt
|
|||
ctx->buffer[buffer_index] = padding_size;
|
||||
buffer_index++;
|
||||
}
|
||||
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
|
||||
ctx->mode(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext);
|
||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||
}
|
|
@ -3,21 +3,19 @@
|
|||
|
||||
void ft_des_get_password
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
char pass[128]
|
||||
)
|
||||
{
|
||||
char *first_try[128];
|
||||
char *second_try[128];
|
||||
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))
|
||||
ft_strcpy(first_try, getpass("enter password:"));
|
||||
if (!ft_strlen(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_strcpy(second_try, getpass("retype password:"));
|
||||
if (ft_strcmp(first_try, second_try) != 0)
|
||||
ft_des_print_error("passwords doesn't match");
|
||||
ctx->raw_password = (char *)second_try;
|
||||
ft_memcpy(pass, second_try, 128);
|
||||
}
|
|
@ -15,9 +15,9 @@ void ft_des_init_ctx
|
|||
ft_bzero(ctx->round_keys[i], FT_DES_ROUND_KEY_SIZE);
|
||||
i++;
|
||||
}
|
||||
ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE);
|
||||
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
|
||||
ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE);
|
||||
ft_bzero(ctx->key, FT_DES_BIT_BLOCK_SIZE);
|
||||
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
|
||||
ctx->readed = 0;
|
||||
ctx->decode = 0;
|
||||
ctx->b64 = 0;
|
||||
|
@ -26,4 +26,6 @@ void ft_des_init_ctx
|
|||
ctx->raw_password = NULL;
|
||||
ctx->raw_salt = NULL;
|
||||
ctx->raw_key = NULL;
|
||||
ctx->raw_iv = NULL;
|
||||
ctx->mode = NULL;
|
||||
}
|
73
src/des/ft_des_parse_args.c
Normal file
73
src/des/ft_des_parse_args.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include <stddef.h>
|
||||
#include "ft_des.h"
|
||||
#include "libft.h"
|
||||
|
||||
t_des_argument_parser g_arg_parsers[] = {
|
||||
{
|
||||
"-d",
|
||||
ft_des_decode_arg_parser,
|
||||
},
|
||||
{
|
||||
"-e",
|
||||
ft_des_encode_arg_parser,
|
||||
},
|
||||
{
|
||||
"-a",
|
||||
ft_des_base64_arg_parser,
|
||||
},
|
||||
{
|
||||
"-i",
|
||||
ft_des_input_file_arg_parser,
|
||||
},
|
||||
{
|
||||
"-o",
|
||||
ft_des_output_file_arg_parser,
|
||||
},
|
||||
{
|
||||
"-k",
|
||||
ft_des_key_arg_parser,
|
||||
},
|
||||
{
|
||||
"-v",
|
||||
ft_des_iv_arg_parser,
|
||||
},
|
||||
{
|
||||
"-p",
|
||||
ft_des_password_arg_parser,
|
||||
},
|
||||
{
|
||||
"-s",
|
||||
ft_des_salt_arg_parser,
|
||||
},
|
||||
{ NULL, NULL},
|
||||
};
|
||||
|
||||
void ft_des_parse_args
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
{
|
||||
int i;
|
||||
char *current_arg;
|
||||
t_des_argument_parser *parser_walker;
|
||||
|
||||
i = 1;
|
||||
while(i < argc)
|
||||
{
|
||||
current_arg = argv[i];
|
||||
parser_walker = g_arg_parsers;
|
||||
while(parser_walker->arg_parser)
|
||||
{
|
||||
if (ft_strcmp(current_arg, parser_walker->arg) == 0)
|
||||
{
|
||||
i = parser_walker->arg_parser(argc, argv, i, ctx);
|
||||
break;
|
||||
}
|
||||
parser_walker++;
|
||||
}
|
||||
if (!parser_walker->arg_parser)
|
||||
ft_des_print_error("wrong argument. type -h for help.");
|
||||
}
|
||||
}
|
14
src/des/ft_des_set_raw_iv.c
Normal file
14
src/des/ft_des_set_raw_iv.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
void ft_des_set_raw_iv
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
{
|
||||
const char *wrong_char;
|
||||
|
||||
wrong_char = ft_des_hex_to_bit(ctx->raw_iv, ctx->iv,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
if (wrong_char)
|
||||
ft_des_print_error("wrong char in hex iinitialization vector");
|
||||
}
|
|
@ -8,7 +8,7 @@ void ft_des_set_raw_key
|
|||
const char *wrong_char;
|
||||
|
||||
wrong_char = ft_des_hex_to_bit(ctx->raw_key, ctx->key,
|
||||
FT_DES_INITIAL_KEY_SIZE);
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
if (wrong_char)
|
||||
ft_des_print_error("wrong char in hex key");
|
||||
}
|
|
@ -38,6 +38,10 @@ t_algorithm g_algorithms[] = {
|
|||
"des-ecb",
|
||||
ft_des_ecb,
|
||||
},
|
||||
{
|
||||
"des-cbc",
|
||||
ft_des_cbc,
|
||||
},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
1
t/cases/des_ecb_b64.txt
Normal file
1
t/cases/des_ecb_b64.txt
Normal file
|
@ -0,0 +1 @@
|
|||
U2FsdGVkX1/+2bai0hAYzG6psw8usaQW
|
1
t/cases/one_deep_secret.txt
Normal file
1
t/cases/one_deep_secret.txt
Normal file
|
@ -0,0 +1 @@
|
|||
one deep secret
|
|
@ -630,7 +630,7 @@ int init_ctx()
|
|||
ft_des_init_ctx(&ctx);
|
||||
|
||||
i = 0;
|
||||
while(i < FT_DES_INITIAL_KEY_SIZE)
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
_is(ctx.key[i] == 0);
|
||||
i++;
|
||||
|
@ -667,6 +667,8 @@ int init_ctx()
|
|||
_is(ctx.raw_password == NULL);
|
||||
_is(ctx.raw_key == NULL);
|
||||
_is(ctx.raw_salt == NULL);
|
||||
_is(ctx.raw_iv == NULL);
|
||||
_is(ctx.mode == NULL);
|
||||
_end("shoud init ctx");
|
||||
}
|
||||
|
||||
|
@ -847,6 +849,58 @@ int convert_bytes_to_bits()
|
|||
_end("should convert 8 bytes to 64 bits");
|
||||
}
|
||||
|
||||
int convert_bits_to_bytes()
|
||||
{
|
||||
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
};
|
||||
t_byte1 expected[FT_DES_BYTE_BLOCK_SIZE] = {
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
};
|
||||
t_byte1 actual[FT_DES_BIT_BLOCK_SIZE];
|
||||
int i;
|
||||
|
||||
ft_bzero(actual, FT_DES_BIT_BLOCK_SIZE);
|
||||
ft_des_bits_to_bytes(bits, FT_DES_BIT_BLOCK_SIZE, actual,
|
||||
FT_DES_BYTE_BLOCK_SIZE);
|
||||
|
||||
i = 0;
|
||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||
{
|
||||
_is(expected[i] == actual[i]);
|
||||
i++;
|
||||
}
|
||||
t_byte1 bits1[FT_DES_BIT_BLOCK_SIZE] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 1, 0, 0, 0, 1,
|
||||
0, 0, 1, 0, 0, 0, 1, 0,
|
||||
0, 0, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 0, 0, 0, 1, 0, 0,
|
||||
0, 1, 0, 1, 0, 1, 0, 1,
|
||||
0, 1, 1, 0, 0, 1, 1, 0,
|
||||
0, 1, 1, 1, 0, 1, 1, 1,
|
||||
};
|
||||
t_byte1 expected1[FT_DES_BYTE_BLOCK_SIZE] = {
|
||||
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
||||
};
|
||||
ft_des_bits_to_bytes(bits1, FT_DES_BIT_BLOCK_SIZE, actual,
|
||||
FT_DES_BYTE_BLOCK_SIZE);
|
||||
i = 0;
|
||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||
{
|
||||
_is(expected1[i] == actual[i]);
|
||||
i++;
|
||||
}
|
||||
_end("should convert 64 bits to 8 bytes");
|
||||
}
|
||||
|
||||
int des_tests()
|
||||
{
|
||||
_should(perform_initial_permutation);
|
||||
|
@ -869,5 +923,6 @@ int des_tests()
|
|||
_should(convert_hex_string_to_bytes);
|
||||
_should(convert_short_hex_string_to_bytes);
|
||||
_should(convert_bytes_to_bits);
|
||||
_should(convert_bits_to_bytes);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue