add help output, des-pcbc
This commit is contained in:
parent
a73c34859b
commit
9ac3cd2ec9
14 changed files with 166 additions and 8 deletions
2
Makefile
2
Makefile
|
@ -140,9 +140,11 @@ DES_SRC = ft_des_initial_permutation.c \
|
|||
ft_des_generate_decryption_round_keys.c \
|
||||
ft_des_parse_args.c \
|
||||
ft_des_arg_parsers.c \
|
||||
ft_des_usage.c \
|
||||
ft_des.c \
|
||||
ft_des_ecb.c \
|
||||
ft_des_cbc.c \
|
||||
ft_des_pcbc.c \
|
||||
ft_des_init_ctx.c \
|
||||
ft_des_set_raw_key.c \
|
||||
ft_des_set_raw_iv.c \
|
||||
|
|
19
inc/ft_des.h
19
inc/ft_des.h
|
@ -328,6 +328,19 @@ int ft_des_salt_arg_parser
|
|||
t_des_ctx *c
|
||||
);
|
||||
|
||||
int ft_des_help_arg_parser
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
int position,
|
||||
t_des_ctx *c
|
||||
);
|
||||
|
||||
void ft_des_usage
|
||||
(
|
||||
void
|
||||
);
|
||||
|
||||
void ft_des
|
||||
(
|
||||
t_des_ctx *c
|
||||
|
@ -345,6 +358,12 @@ void ft_des_cbc
|
|||
char **argv
|
||||
);
|
||||
|
||||
void ft_des_pcbc
|
||||
(
|
||||
int argc,
|
||||
char **argv
|
||||
);
|
||||
|
||||
void ft_des_decrypt
|
||||
(
|
||||
t_des_ctx *ctx
|
||||
|
|
|
@ -5,6 +5,13 @@
|
|||
#include "libft.h"
|
||||
#include "ft_base64.h"
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
ft_putstr("base64 usage:\n");
|
||||
ft_putstr("ft_ssl base64 [-d|-e] [-i in_file] [-o out_file]\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static int open_input(char *filename)
|
||||
{
|
||||
int fd;
|
||||
|
@ -66,6 +73,8 @@ static void read_args
|
|||
ctx->input_fd = open_input(next_arg);
|
||||
else if (ft_strcmp(current_arg, "-o") == 0)
|
||||
ctx->output_fd = open_output(next_arg);
|
||||
else if (ft_strcmp(current_arg, "-h") == 0)
|
||||
usage();
|
||||
else if (++i)
|
||||
continue;
|
||||
i++;
|
||||
|
|
|
@ -147,3 +147,19 @@ int ft_des_salt_arg_parser
|
|||
ctx->raw_salt = argv[position + 1];
|
||||
return (position + 2);
|
||||
}
|
||||
|
||||
int ft_des_help_arg_parser
|
||||
(
|
||||
int argc,
|
||||
char **argv,
|
||||
int position,
|
||||
t_des_ctx *ctx
|
||||
)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)position;
|
||||
(void)ctx;
|
||||
ft_des_usage();
|
||||
return (position);
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
static void xor
|
||||
(
|
||||
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE]
|
||||
t_byte1 a[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 b[FT_DES_BIT_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
@ -11,7 +11,7 @@ static void xor
|
|||
i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
plaintext[i] = plaintext[i] ^ iv[i];
|
||||
a[i] = a[i] ^ b[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,10 @@ t_des_argument_parser g_arg_parsers[] = {
|
|||
"-s",
|
||||
ft_des_salt_arg_parser,
|
||||
},
|
||||
{
|
||||
"-h",
|
||||
ft_des_help_arg_parser,
|
||||
},
|
||||
{ NULL, NULL},
|
||||
};
|
||||
|
||||
|
|
75
src/des/ft_des_pcbc.c
Normal file
75
src/des/ft_des_pcbc.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include "ft_des.h"
|
||||
|
||||
static void xor
|
||||
(
|
||||
t_byte1 a[FT_DES_BIT_BLOCK_SIZE],
|
||||
t_byte1 b[FT_DES_BIT_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||
{
|
||||
a[i] = a[i] ^ b[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void encrypt
|
||||
(
|
||||
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];
|
||||
t_byte1 input_with_iv[FT_DES_BYTE_BLOCK_SIZE];
|
||||
|
||||
ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, input_bits,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
xor(iv, input_bits);
|
||||
ft_des_bits_to_bytes(input_bits, FT_DES_BIT_BLOCK_SIZE, input_with_iv,
|
||||
FT_DES_BYTE_BLOCK_SIZE);
|
||||
ft_des_process_block(input_with_iv, keys, output);
|
||||
ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, iv,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
xor(iv, input_bits);
|
||||
}
|
||||
|
||||
static void decrypt
|
||||
(
|
||||
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 output_bits[FT_DES_BIT_BLOCK_SIZE];
|
||||
|
||||
ft_des_process_block(input, keys, output);
|
||||
ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, output_bits,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
xor(output_bits, iv);
|
||||
ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, iv,
|
||||
FT_DES_BIT_BLOCK_SIZE);
|
||||
xor(iv, output_bits);
|
||||
}
|
||||
|
||||
void ft_des_pcbc
|
||||
(
|
||||
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.encrypt = encrypt;
|
||||
ctx.decrypt = decrypt;
|
||||
ft_des(&ctx);
|
||||
}
|
23
src/des/ft_des_usage.c
Normal file
23
src/des/ft_des_usage.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "libft.h"
|
||||
|
||||
void ft_des_usage
|
||||
(
|
||||
void
|
||||
)
|
||||
{
|
||||
ft_putstr("des cipher usage:\n\n");
|
||||
ft_putstr("ft_ssl des-{mode} [flags]\n\n");
|
||||
ft_putstr("flags:\n");
|
||||
ft_putstr(" -a\t\tdecode/encode the input/output in base64\n");
|
||||
ft_putstr(" -e\t\tecrypt mode\n");
|
||||
ft_putstr(" -d\t\tdecrypt mode\n");
|
||||
ft_putstr(" -i\t\tinput file for message\n");
|
||||
ft_putstr(" -o\t\toutput file for message\n");
|
||||
ft_putstr(" -p pass\tpassword in ascii format\n");
|
||||
ft_putstr(" -k key\t\tkey in hex format\n");
|
||||
ft_putstr(" -s salt\tsalt in hex format\n");
|
||||
ft_putstr(" -v vector\tinitialization vector in hex format\n\n");
|
||||
ft_putstr("where {mode} is one of:\n");
|
||||
ft_putstr("ecb\ncbc\npcbc\n");
|
||||
exit(1);
|
||||
}
|
|
@ -19,7 +19,7 @@ void ft_ssl_usage(void)
|
|||
ft_putstr("Message Digest commands:\n");
|
||||
ft_putstr("md5\nsha256\nsha224\n\n");
|
||||
ft_putstr("Cipher commands:\n");
|
||||
ft_putstr("base64\ndes-ecb\n\n");
|
||||
ft_putstr("base64\ndes-ecb\ndes-cbc\ndes-pcbc\n\n");
|
||||
ft_putstr("To get help for specific command:\n");
|
||||
ft_putstr("ft_ssl command -h\n\n");
|
||||
exit(1);
|
||||
|
|
|
@ -42,6 +42,10 @@ t_algorithm g_algorithms[] = {
|
|||
"des-cbc",
|
||||
ft_des_cbc,
|
||||
},
|
||||
{
|
||||
"des-pcbc",
|
||||
ft_des_pcbc,
|
||||
},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
static void ft_md5_usage(void)
|
||||
{
|
||||
ft_putstr("MD5 Digest usage:\n");
|
||||
ft_putstr("md5 digest usage:\n");
|
||||
ft_putstr("ft_ssl md5 [-p|-q|-r] [[-s string...] [file...]]\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ static int read_flags(int argc, char **argv, t_md5_flags *flags)
|
|||
i = 1;
|
||||
while (i < argc)
|
||||
{
|
||||
if (ft_strcmp("-h", argv[i]) == 0)
|
||||
ft_md5_usage();
|
||||
if (ft_strcmp("-r", argv[i]) == 0)
|
||||
flags->reverse = 1;
|
||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
||||
|
|
|
@ -28,6 +28,8 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
|
|||
i = 1;
|
||||
while (i < argc)
|
||||
{
|
||||
if (ft_strcmp("-h", argv[i]) == 0)
|
||||
ft_sha_usage("sha224");
|
||||
if (ft_strcmp("-r", argv[i]) == 0)
|
||||
flags->reverse = 1;
|
||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
||||
|
@ -57,7 +59,7 @@ static void process_strings_and_files
|
|||
if (ft_strcmp("-s", argv[i]) == 0)
|
||||
{
|
||||
if (i + 1 >= argc)
|
||||
ft_sha_usage("SHA224");
|
||||
ft_sha_usage("sha224");
|
||||
ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags);
|
||||
i++;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
|
|||
i = 1;
|
||||
while (i < argc)
|
||||
{
|
||||
if (ft_strcmp("-h", argv[i]) == 0)
|
||||
ft_sha_usage("sha256");
|
||||
if (ft_strcmp("-r", argv[i]) == 0)
|
||||
flags->reverse = 1;
|
||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
||||
|
@ -57,7 +59,7 @@ static void process_strings_and_files
|
|||
if (ft_strcmp("-s", argv[i]) == 0)
|
||||
{
|
||||
if (i + 1 >= argc)
|
||||
ft_sha_usage("SHA256");
|
||||
ft_sha_usage("sha256");
|
||||
ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags);
|
||||
i++;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
void ft_sha_usage(const char *algorithm)
|
||||
{
|
||||
ft_putstr(algorithm);
|
||||
ft_putstr(" Digest usage:\n");
|
||||
ft_putstr(" digest usage:\n");
|
||||
ft_putstr("ft_ssl ");
|
||||
ft_putstr(algorithm);
|
||||
ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n");
|
||||
|
|
Loading…
Reference in a new issue