add help output, des-pcbc

This commit is contained in:
Gregory 2019-04-10 15:39:45 +03:00
parent a73c34859b
commit 9ac3cd2ec9
14 changed files with 166 additions and 8 deletions

View file

@ -140,9 +140,11 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_generate_decryption_round_keys.c \ ft_des_generate_decryption_round_keys.c \
ft_des_parse_args.c \ ft_des_parse_args.c \
ft_des_arg_parsers.c \ ft_des_arg_parsers.c \
ft_des_usage.c \
ft_des.c \ ft_des.c \
ft_des_ecb.c \ ft_des_ecb.c \
ft_des_cbc.c \ ft_des_cbc.c \
ft_des_pcbc.c \
ft_des_init_ctx.c \ ft_des_init_ctx.c \
ft_des_set_raw_key.c \ ft_des_set_raw_key.c \
ft_des_set_raw_iv.c \ ft_des_set_raw_iv.c \

View file

@ -328,6 +328,19 @@ int ft_des_salt_arg_parser
t_des_ctx *c 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 void ft_des
( (
t_des_ctx *c t_des_ctx *c
@ -345,6 +358,12 @@ void ft_des_cbc
char **argv char **argv
); );
void ft_des_pcbc
(
int argc,
char **argv
);
void ft_des_decrypt void ft_des_decrypt
( (
t_des_ctx *ctx t_des_ctx *ctx

View file

@ -5,6 +5,13 @@
#include "libft.h" #include "libft.h"
#include "ft_base64.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) static int open_input(char *filename)
{ {
int fd; int fd;
@ -66,6 +73,8 @@ static void read_args
ctx->input_fd = open_input(next_arg); ctx->input_fd = open_input(next_arg);
else if (ft_strcmp(current_arg, "-o") == 0) else if (ft_strcmp(current_arg, "-o") == 0)
ctx->output_fd = open_output(next_arg); ctx->output_fd = open_output(next_arg);
else if (ft_strcmp(current_arg, "-h") == 0)
usage();
else if (++i) else if (++i)
continue; continue;
i++; i++;

View file

@ -147,3 +147,19 @@ int ft_des_salt_arg_parser
ctx->raw_salt = argv[position + 1]; ctx->raw_salt = argv[position + 1];
return (position + 2); 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);
}

View file

@ -2,8 +2,8 @@
static void xor static void xor
( (
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE], t_byte1 a[FT_DES_BIT_BLOCK_SIZE],
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE] t_byte1 b[FT_DES_BIT_BLOCK_SIZE]
) )
{ {
int i; int i;
@ -11,7 +11,7 @@ static void xor
i = 0; i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE) while(i < FT_DES_BIT_BLOCK_SIZE)
{ {
plaintext[i] = plaintext[i] ^ iv[i]; a[i] = a[i] ^ b[i];
i++; i++;
} }
} }

View file

@ -39,6 +39,10 @@ t_des_argument_parser g_arg_parsers[] = {
"-s", "-s",
ft_des_salt_arg_parser, ft_des_salt_arg_parser,
}, },
{
"-h",
ft_des_help_arg_parser,
},
{ NULL, NULL}, { NULL, NULL},
}; };

75
src/des/ft_des_pcbc.c Normal file
View 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
View 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);
}

View file

@ -19,7 +19,7 @@ void ft_ssl_usage(void)
ft_putstr("Message Digest commands:\n"); ft_putstr("Message Digest commands:\n");
ft_putstr("md5\nsha256\nsha224\n\n"); ft_putstr("md5\nsha256\nsha224\n\n");
ft_putstr("Cipher commands:\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("To get help for specific command:\n");
ft_putstr("ft_ssl command -h\n\n"); ft_putstr("ft_ssl command -h\n\n");
exit(1); exit(1);

View file

@ -42,6 +42,10 @@ t_algorithm g_algorithms[] = {
"des-cbc", "des-cbc",
ft_des_cbc, ft_des_cbc,
}, },
{
"des-pcbc",
ft_des_pcbc,
},
{NULL, NULL} {NULL, NULL}
}; };

View file

@ -15,7 +15,7 @@
static void ft_md5_usage(void) 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"); ft_putstr("ft_ssl md5 [-p|-q|-r] [[-s string...] [file...]]\n\n");
exit(1); exit(1);
} }
@ -35,6 +35,8 @@ static int read_flags(int argc, char **argv, t_md5_flags *flags)
i = 1; i = 1;
while (i < argc) while (i < argc)
{ {
if (ft_strcmp("-h", argv[i]) == 0)
ft_md5_usage();
if (ft_strcmp("-r", argv[i]) == 0) if (ft_strcmp("-r", argv[i]) == 0)
flags->reverse = 1; flags->reverse = 1;
else if (ft_strcmp("-q", argv[i]) == 0) else if (ft_strcmp("-q", argv[i]) == 0)

View file

@ -28,6 +28,8 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
i = 1; i = 1;
while (i < argc) while (i < argc)
{ {
if (ft_strcmp("-h", argv[i]) == 0)
ft_sha_usage("sha224");
if (ft_strcmp("-r", argv[i]) == 0) if (ft_strcmp("-r", argv[i]) == 0)
flags->reverse = 1; flags->reverse = 1;
else if (ft_strcmp("-q", argv[i]) == 0) 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 (ft_strcmp("-s", argv[i]) == 0)
{ {
if (i + 1 >= argc) if (i + 1 >= argc)
ft_sha_usage("SHA224"); ft_sha_usage("sha224");
ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags); ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags);
i++; i++;
} }

View file

@ -28,6 +28,8 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
i = 1; i = 1;
while (i < argc) while (i < argc)
{ {
if (ft_strcmp("-h", argv[i]) == 0)
ft_sha_usage("sha256");
if (ft_strcmp("-r", argv[i]) == 0) if (ft_strcmp("-r", argv[i]) == 0)
flags->reverse = 1; flags->reverse = 1;
else if (ft_strcmp("-q", argv[i]) == 0) 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 (ft_strcmp("-s", argv[i]) == 0)
{ {
if (i + 1 >= argc) if (i + 1 >= argc)
ft_sha_usage("SHA256"); ft_sha_usage("sha256");
ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags); ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags);
i++; i++;
} }

View file

@ -15,7 +15,7 @@
void ft_sha_usage(const char *algorithm) void ft_sha_usage(const char *algorithm)
{ {
ft_putstr(algorithm); ft_putstr(algorithm);
ft_putstr(" Digest usage:\n"); ft_putstr(" digest usage:\n");
ft_putstr("ft_ssl "); ft_putstr("ft_ssl ");
ft_putstr(algorithm); ft_putstr(algorithm);
ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n"); ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n");