some core des rework, basic des context

This commit is contained in:
Gregory 2019-03-15 00:00:15 +02:00
parent 13ade8cde6
commit f600a2d0c6
20 changed files with 482 additions and 152 deletions

View file

@ -126,7 +126,11 @@ 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_ecb.c
ft_des_ecb.c \
ft_des_init_ctx.c \
ft_des_print_error.c \
ft_des_arg_parsers.c \
ft_des_ecb_process.c
SRC = main.c \
ft_ssl_usage.c

View file

@ -13,184 +13,312 @@
#ifndef FT_DES_H
# define FT_DES_H
# include <stdint.h>
# define FT_DES_BYTE_BLOCK_SIZE 8
# define FT_DES_BIT_BLOCK_SIZE 64
# define FT_DES_EXPANDED_HALF_BLOCK_SIZE 48
# define FT_DES_S_BOX_INPUT_SIZE 6
# define FT_DES_S_BOX_OUTPUT_SIZE 4
# define FT_DES_S_BOX_TABLE_ROWS 4
# define FT_DES_S_BOX_TABLE_COLUMNS 16
# define FT_DES_FEISTEL_FUNCTION_KEY_SIZE 48
# define FT_DES_ROUND_KEY_SIZE 48
# define FT_DES_INITIAL_KEY_SIZE 64
# define FT_DES_REDUCED_KEY_SIZE 56
# define FT_DES_ROUND_COUNT 16
# define FT_DES_READ_SIZE 1024
typedef unsigned char t_byte1;
typedef uint64_t t_byte8;
typedef unsigned char t_byte1;
typedef struct s_des_ctx
typedef struct s_des_ctx
{
int input_fd;
int output_fd;
int decode;
int output_in_base64;
char *key;
char *init_vector;
char *pass;
} t_des_ctx;
int input_fd;
int output_fd;
int decode;
int output_in_base64;
int readed;
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT]
[FT_DES_ROUND_KEY_SIZE];
} t_des_ctx;
void ft_des_initial_permutation
typedef int (*t_ft_des_arg_parser_function)
(int argc, char **argv, int position, t_des_ctx *c);
typedef struct s_des_argument_parser
{
const char *arg;
t_ft_des_arg_parser_function arg_parser;
} t_des_argument_parser;
typedef struct s_des_chunk_buffer
{
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
t_byte8 readed;
} t_des_chunk_buffer;
void ft_des_initial_permutation
(
t_byte1 message[FT_DES_BIT_BLOCK_SIZE],
t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE]
);
void ft_des_final_permutation
void ft_des_final_permutation
(
t_byte1 before[FT_DES_BIT_BLOCK_SIZE],
t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE]
);
void ft_des_expansion_box
void ft_des_expansion_box
(
t_byte1 half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
);
void ft_des_s_box
void ft_des_s_box
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE],
t_byte1 table[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS]
);
void ft_des_s_box_1
void ft_des_s_box_1
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_2
void ft_des_s_box_2
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_3
void ft_des_s_box_3
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_4
void ft_des_s_box_4
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_5
void ft_des_s_box_5
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_6
void ft_des_s_box_6
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_7
void ft_des_s_box_7
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_s_box_8
void ft_des_s_box_8
(
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
);
void ft_des_feistel_function_permutation
void ft_des_feistel_function_permutation
(
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
);
void ft_des_feistel_function
void ft_des_feistel_function
(
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
);
void ft_des_key_permuted_choice_one
void ft_des_key_permuted_choice_one
(
t_byte1 initial_key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE]
);
void ft_des_key_permuted_choice_two
void ft_des_key_permuted_choice_two
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
);
void ft_des_derive_encryption_round_key
void ft_des_derive_encryption_round_key
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round,
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
);
void ft_des_derive_decryption_round_key
void ft_des_derive_decryption_round_key
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round,
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
);
void ft_des_generate_encryption_round_keys
void ft_des_generate_encryption_round_keys
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 input_key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
);
void ft_des_generate_decryption_round_keys
void ft_des_generate_decryption_round_keys
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 input_key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
);
void ft_des_rotate_half_key_left
void ft_des_rotate_half_key_left
(
t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2],
t_byte1 rotation_number
);
void ft_des_rotate_half_key_right
void ft_des_rotate_half_key_right
(
t_byte1 half[FT_DES_REDUCED_KEY_SIZE / 2],
t_byte1 rotation_number
);
void ft_des_round
void ft_des_round
(
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 key[FT_DES_ROUND_KEY_SIZE]
);
void ft_des_process_block
void ft_des_process_block
(
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 ciphertext[FT_DES_BIT_BLOCK_SIZE]
t_byte1 plaintext[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
t_byte1 ciphertext[FT_DES_BYTE_BLOCK_SIZE]
);
void ft_des_ecb
void ft_des_print_error
(
const char *error
);
void ft_des_ecb
(
int argc,
char **argv
);
void ft_des_init_ctx
(
t_des_ctx *ctx
);
int ft_des_key_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_decode_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_encode_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_base64_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_input_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_output_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_password_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_init_vector_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_salt_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
void ft_des_ecb_process
(
t_des_ctx *ctx
);
void ft_des_ecb_process_chunk
(
t_des_ctx *ctx,
t_byte8 reaed,
t_byte1 buffer[FT_DES_READ_SIZE]
);
void ft_des_ecb_finish_process
(
t_des_ctx *ctx
);
void ft_des_generete_key_from_hex
(
char *hex_key,
t_byte1 initla_key[FT_DES_INITIAL_KEY_SIZE]
);
#endif

View file

@ -0,0 +1,16 @@
#include "ft_des.h"
int ft_des_key_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
(void)ctx;
(void)argv;
if (position + 1 >= argc)
ft_des_print_error("there is no key after -k flag. type -h for help.");
return (position + 2);
}

View file

@ -1,25 +0,0 @@
#include "ft_des.h"
void ft_des_encrypt_block
(
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE]
)
{
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
int i;
ft_des_initial_permutation(plaintext, ip_message);
i = 1;
while(i <= FT_DES_ROUND_COUNT)
{
if (i % 2 != 0)
ft_des_encryption_round(ip_message, ip_message + 32, keys[i - 1]);
else
ft_des_encryption_round(ip_message + 32, ip_message, keys[i - 1]);
i++;
}
ft_des_switch_message_halves(ip_message);
ft_des_final_permutation(ip_message, cyphertext);
}

View file

@ -4,7 +4,7 @@ void ft_des_derive_decryption_round_key
(
t_byte1 key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round,
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
)
{
if (round == 2 || round == 9 || round == 16)

View file

@ -4,7 +4,7 @@ void ft_des_derive_encryption_round_key
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round,
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
)
{
if (round == 1 || round == 2 || round == 9 || round == 16)

View file

@ -1,13 +1,54 @@
#include <stdlib.h>
#include "ft_des.h"
#include "libft.h"
void ft_des_ecb
t_des_argument_parser g_arg_parsers[] = {
{
"-k",
ft_des_key_arg_parser,
},
{ NULL, NULL},
};
static void 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.");
}
}
void ft_des_ecb
(
int argc,
char **argv
)
{
(void)argc;
(void)argv;
t_des_ctx ctx;
ft_des_init_ctx(&ctx);
parse_args(argc, argv, &ctx);
// ft_des_ecb_process(&ctx);
exit(0);
}

View file

@ -0,0 +1,16 @@
#include <unistd.h>
#include "ft_des.h"
void ft_des_ecb_process
(
t_des_ctx *ctx
)
{
// t_byte1 buffer[FT_DES_READ_SIZE];
// t_byte8 readed;
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_process_chunk(ctx, readed, buffer);
// ft_des_ecb_finish_process(ctx);
}

View file

@ -0,0 +1,32 @@
#include "ft_des.h"
void ft_des_ecb_process_chunk
(
t_des_ctx *ctx,
t_byte8 readed,
t_byte8 buffer[FT_DES_READ_SIZE]
)
{
t_byte8 free_space_in_buffer;
t_byte8 buffer_index;
t_byte8 idx;
buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE;
ctx->readed += readed;
free_space_in_buffer = FT_DES_BYTE_BLOCK_SIZE - buffer_index;
if (readed >= free_space_in_buffer)
{
ft_memcpy(&ctx->buffer[buffer_index], buffer, free_space_in_buffer);
ft_base64_encode_write(ctx, ctx->buffer);
idx = free_space_in_buffer;
while(idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
{
ft_base64_encode_write(ctx, &buffer[idx]);
idx += FT_DES_BYTE_BLOCK_SIZE;
}
buffer_index = 0;
}
else
idx = 0;
ft_memcpy(&ctx->buffer[buffer_index], &buffer[idx], readed - idx);
}

View file

@ -3,7 +3,7 @@
static void merge_key
(
t_byte1 expanded_half[FT_DES_EXPANDED_HALF_BLOCK_SIZE],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
t_byte1 output[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
)
{
@ -18,7 +18,7 @@ static void merge_key
void ft_des_feistel_function
(
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
)
{

View file

@ -2,16 +2,18 @@
void ft_des_generate_decryption_round_keys
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 input_key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
)
{
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
int i;
ft_des_key_permuted_choice_one(input_key, reduced_key);
i = 1;
while(i <= FT_DES_ROUND_COUNT)
{
ft_des_derive_decryption_round_key(input_key, i, keys[i - 1]);
ft_des_derive_decryption_round_key(reduced_key, i, keys[i - 1]);
i++;
}
}

View file

@ -3,15 +3,17 @@
void ft_des_generate_encryption_round_keys
(
t_byte1 key[FT_DES_INITIAL_KEY_SIZE],
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
)
{
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
int i;
ft_des_key_permuted_choice_one(key, reduced_key);
i = 1;
while(i <= FT_DES_ROUND_COUNT)
{
ft_des_derive_encryption_round_key(key, i, round_keys[i - 1]);
ft_des_derive_encryption_round_key(reduced_key, i, round_keys[i - 1]);
i++;
}
}

25
src/des/ft_des_init_ctx.c Normal file
View file

@ -0,0 +1,25 @@
#include <unistd.h>
#include "ft_des.h"
#include "libft.h"
void ft_des_init_ctx
(
t_des_ctx *ctx
)
{
int i;
i = 0;
while(i < FT_DES_ROUND_COUNT)
{
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);
ctx->readed = 0;
ctx->decode = 0;
ctx->output_in_base64 = 0;
ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO;
}

View file

@ -0,0 +1,10 @@
#include "ft_des.h"
void ft_des_init_flags
(
t_des_flags *flags
)
{
flags->decode = 0;
flags->output_in_base64 = 0;
}

View file

@ -3,10 +3,10 @@
void ft_des_key_permuted_choice_two
(
t_byte1 input_key[FT_DES_REDUCED_KEY_SIZE],
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
)
{
static t_byte1 table[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] = {
static t_byte1 table[FT_DES_ROUND_KEY_SIZE] = {
14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
@ -17,7 +17,7 @@ void ft_des_key_permuted_choice_two
int i;
i = 0;
while(i < FT_DES_FEISTEL_FUNCTION_KEY_SIZE)
while(i < FT_DES_ROUND_KEY_SIZE)
{
round_key[i] = input_key[table[i] - 1];
i++;

View file

@ -0,0 +1,13 @@
#include "ft_des.h"
#include "libft.h"
void ft_des_print_error
(
const char *error
)
{
ft_putstr("des: ");
ft_putstr(error);
ft_putstr("\n");
exit(1);
}

View file

@ -17,17 +17,72 @@ static void switch_halves
}
}
void ft_des_process_block
static void bytes_to_bits
(
t_byte1 input[FT_DES_BIT_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE],
t_byte1 output[FT_DES_BIT_BLOCK_SIZE]
t_byte1 bytes[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE]
)
{
int i;
int j;
i = 0;
j = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
bits[j] = bytes[i] >> 7 & 1;
bits[j + 1] = bytes[i] >> 6 & 1;
bits[j + 2] = bytes[i] >> 5 & 1;
bits[j + 3] = bytes[i] >> 4 & 1;
bits[j + 4] = bytes[i] >> 3 & 1;
bits[j + 5] = bytes[i] >> 2 & 1;
bits[j + 6] = bytes[i] >> 1 & 1;
bits[j + 7] = bytes[i] & 1;
j += FT_DES_BYTE_BLOCK_SIZE;
i++;
}
}
static void bits_to_bytes
(
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE],
t_byte1 bytes[FT_DES_BYTE_BLOCK_SIZE]
)
{
int i;
int j;
i = 0;
j = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
bytes[i] = bits[j] << 7
| bits[j + 1] << 6
| bits[j + 2] << 5
| bits[j + 3] << 4
| bits[j + 4] << 3
| bits[j + 5] << 2
| bits[j + 6] << 1
| bits[j + 7];
j += FT_DES_BYTE_BLOCK_SIZE;
i++;
}
}
void ft_des_process_block
(
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
t_byte1 output[FT_DES_BYTE_BLOCK_SIZE]
)
{
t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE];
t_byte1 output_bits[FT_DES_BIT_BLOCK_SIZE];
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
int i;
ft_des_initial_permutation(input, ip_message);
bytes_to_bits(input, input_bits);
ft_des_initial_permutation(input_bits, ip_message);
i = 1;
while(i <= FT_DES_ROUND_COUNT)
{
@ -38,5 +93,6 @@ void ft_des_process_block
i++;
}
switch_halves(ip_message);
ft_des_final_permutation(ip_message, output);
ft_des_final_permutation(ip_message, output_bits);
bits_to_bytes(output_bits, output);
}

View file

@ -20,7 +20,7 @@ void ft_des_round
(
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE]
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
)
{
t_byte1 feistel_function_result[FT_DES_BIT_BLOCK_SIZE / 2];

View file

@ -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\n\n");
ft_putstr("base64\ndes-ecb\n\n");
ft_putstr("To get help for specific command:\n");
ft_putstr("ft_ssl command -h\n\n");
exit(1);

View file

@ -2,6 +2,8 @@
#include "tests.h"
#include "ft_des.h"
#include "libft.h"
#include <unistd.h>
#define S_BOX_CASES_NUMBER 3
@ -334,7 +336,7 @@ int perform_premutation_in_feistel_function()
int perform_feistel_function()
{
t_byte1 key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] = {
t_byte1 key[FT_DES_ROUND_KEY_SIZE] = {
0, 0, 0, 1, 1, 0,
1, 1, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1,
@ -471,7 +473,7 @@ int derive_round_key()
0, 0, 1, 1, 1, 1, 0,
};
t_byte1 expected_round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE] = {
t_byte1 expected_round_key[FT_DES_ROUND_KEY_SIZE] = {
0, 0, 0, 1, 1, 0,
1, 1, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1,
@ -482,7 +484,7 @@ int derive_round_key()
1, 1, 0, 0, 1, 0,
};
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE];
ft_des_derive_encryption_round_key(reduced_key, 1, round_key);
@ -496,7 +498,7 @@ int derive_round_key()
}
i = 0;
while(i < FT_DES_FEISTEL_FUNCTION_KEY_SIZE)
while(i < FT_DES_ROUND_KEY_SIZE)
{
_is(round_key[i] == expected_round_key[i]);
i++;
@ -517,7 +519,7 @@ int perform_encryption_round()
0, 1, 0, 1, 1, 1, 1, 0,
};
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
t_byte1 round_key[FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE];
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
@ -557,15 +559,11 @@ int perform_encryption_round()
int encrypt_block()
{
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
t_byte1 message[FT_DES_BYTE_BLOCK_SIZE] = {
97, 115, 100, 97, 115, 100, 97, 115
};
t_byte1 expected_cypertext[FT_DES_BYTE_BLOCK_SIZE] = {
6, 175, 99, 208, 71, 158, 175, 28
};
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
0, 0, 1, 1, 1, 0, 1, 1,
@ -577,26 +575,14 @@ int encrypt_block()
1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 0,
};
t_byte1 expected_cypertext[FT_DES_BIT_BLOCK_SIZE] = {
0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 1, 0, 1, 1, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
1, 1, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 1, 1,
1, 0, 0, 1, 1, 1, 1, 0,
1, 0, 1, 0, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 0,
};
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
t_byte1 cyphertext[FT_DES_BIT_BLOCK_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE];
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ft_des_key_permuted_choice_one(key, reduced_key);
ft_des_generate_encryption_round_keys(reduced_key, round_keys);
ft_des_generate_encryption_round_keys(key, round_keys);
ft_des_process_block(message, round_keys, cyphertext);
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_is(cyphertext[i] == expected_cypertext[i]);
i++;
@ -606,25 +592,11 @@ int encrypt_block()
int decrypt_block()
{
t_byte1 cypertext[FT_DES_BIT_BLOCK_SIZE] = {
0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 1, 0, 1, 1, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
1, 1, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 1, 1,
1, 0, 0, 1, 1, 1, 1, 0,
1, 0, 1, 0, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 0,
t_byte1 cypertext[FT_DES_BYTE_BLOCK_SIZE] = {
6, 175, 99, 208, 71, 158, 175, 28
};
t_byte1 expected_plaintext[FT_DES_BIT_BLOCK_SIZE] = {
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1,
t_byte1 expected_plaintext[FT_DES_BYTE_BLOCK_SIZE] = {
97, 115, 100, 97, 115, 100, 97, 115
};
t_byte1 key[FT_DES_INITIAL_KEY_SIZE] = {
0, 0, 1, 1, 1, 0, 1, 1,
@ -636,16 +608,14 @@ int decrypt_block()
1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 0,
};
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_FEISTEL_FUNCTION_KEY_SIZE];
t_byte1 plaintext[FT_DES_BIT_BLOCK_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE];
t_byte1 plaintext[FT_DES_BYTE_BLOCK_SIZE];
ft_des_key_permuted_choice_one(key, reduced_key);
ft_des_generate_decryption_round_keys(reduced_key, round_keys);
ft_des_generate_decryption_round_keys(key, round_keys);
ft_des_process_block(cypertext, round_keys, plaintext);
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_is(plaintext[i] == expected_plaintext[i]);
i++;
@ -653,6 +623,45 @@ int decrypt_block()
_end("should decrypt block");
}
int init_ctx()
{
t_des_ctx ctx;
int i;
int j;
ft_des_init_ctx(&ctx);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_is(ctx.key[i] == 0);
i++;
}
i = 0;
while(i < FT_DES_ROUND_COUNT)
{
j = 0;
while(j < FT_DES_ROUND_KEY_SIZE)
{
_is(ctx.round_keys[i][j] == 0);
j++;
}
i++;
}
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_is(ctx.buffer[i] == 0);
i++;
}
_is(ctx.readed == 0);
_is(ctx.input_fd == STDIN_FILENO);
_is(ctx.output_fd == STDOUT_FILENO);
_is(ctx.decode == 0);
_is(ctx.output_in_base64 == 0);
_end("shoud init ctx");
}
int des_tests()
{
_should(perform_initial_permutation);
@ -668,5 +677,6 @@ int des_tests()
_should(perform_encryption_round);
_should(encrypt_block);
_should(decrypt_block);
_should(init_ctx);
return 0;
}