diff --git a/Makefile b/Makefile index b2d3ea7b..13f1dc25 100644 --- a/Makefile +++ b/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 \ diff --git a/inc/ft_des.h b/inc/ft_des.h index a9510af6..5225ba3b 100644 --- a/inc/ft_des.h +++ b/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 diff --git a/src/base64/ft_base64.c b/src/base64/ft_base64.c index 9caa50a5..388a0929 100644 --- a/src/base64/ft_base64.c +++ b/src/base64/ft_base64.c @@ -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++; diff --git a/src/des/ft_des_arg_parsers.c b/src/des/ft_des_arg_parsers.c index ccdb36ff..0a6eee3b 100644 --- a/src/des/ft_des_arg_parsers.c +++ b/src/des/ft_des_arg_parsers.c @@ -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); +} diff --git a/src/des/ft_des_cbc.c b/src/des/ft_des_cbc.c index 7a91a18a..f439d213 100644 --- a/src/des/ft_des_cbc.c +++ b/src/des/ft_des_cbc.c @@ -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++; } } diff --git a/src/des/ft_des_parse_args.c b/src/des/ft_des_parse_args.c index f3e99329..9a4450e1 100644 --- a/src/des/ft_des_parse_args.c +++ b/src/des/ft_des_parse_args.c @@ -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}, }; diff --git a/src/des/ft_des_pcbc.c b/src/des/ft_des_pcbc.c new file mode 100644 index 00000000..60b72300 --- /dev/null +++ b/src/des/ft_des_pcbc.c @@ -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); +} \ No newline at end of file diff --git a/src/des/ft_des_usage.c b/src/des/ft_des_usage.c new file mode 100644 index 00000000..e763c22b --- /dev/null +++ b/src/des/ft_des_usage.c @@ -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); +} \ No newline at end of file diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c index a10807ef..dfe36a94 100644 --- a/src/ft_ssl_usage.c +++ b/src/ft_ssl_usage.c @@ -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); diff --git a/src/main.c b/src/main.c index 83f77a02..f64f82d3 100644 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,10 @@ t_algorithm g_algorithms[] = { "des-cbc", ft_des_cbc, }, + { + "des-pcbc", + ft_des_pcbc, + }, {NULL, NULL} }; diff --git a/src/md5/ft_md5.c b/src/md5/ft_md5.c index 18c9d9ec..ae9bb749 100644 --- a/src/md5/ft_md5.c +++ b/src/md5/ft_md5.c @@ -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) diff --git a/src/sha/ft_sha224.c b/src/sha/ft_sha224.c index dcf0b49c..591b4990 100644 --- a/src/sha/ft_sha224.c +++ b/src/sha/ft_sha224.c @@ -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++; } diff --git a/src/sha/ft_sha256.c b/src/sha/ft_sha256.c index 3a04ddc7..9176eca4 100644 --- a/src/sha/ft_sha256.c +++ b/src/sha/ft_sha256.c @@ -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++; } diff --git a/src/sha/ft_sha_usage.c b/src/sha/ft_sha_usage.c index f771de3f..a839244b 100644 --- a/src/sha/ft_sha_usage.c +++ b/src/sha/ft_sha_usage.c @@ -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");