decode and encode done
This commit is contained in:
parent
819257f21f
commit
67af77126e
14 changed files with 208 additions and 30 deletions
8
Makefile
8
Makefile
|
@ -130,10 +130,12 @@ DES_SRC = ft_des_initial_permutation.c \
|
||||||
ft_des_init_ctx.c \
|
ft_des_init_ctx.c \
|
||||||
ft_des_print_error.c \
|
ft_des_print_error.c \
|
||||||
ft_des_arg_parsers.c \
|
ft_des_arg_parsers.c \
|
||||||
ft_des_ecb_process.c \
|
ft_des_ecb_encrypt.c \
|
||||||
ft_des_ecb_process_chunk.c \
|
ft_des_ecb_decrypt.c \
|
||||||
|
ft_des_ecb_encode_process_chunk.c \
|
||||||
ft_des_hex_to_bit_key.c \
|
ft_des_hex_to_bit_key.c \
|
||||||
ft_des_ecb_finish_process.c
|
ft_des_ecb_finish_encrypt.c
|
||||||
|
# ft_des_ecb_finish_decrypt.c
|
||||||
|
|
||||||
SRC = main.c \
|
SRC = main.c \
|
||||||
ft_ssl_usage.c
|
ft_ssl_usage.c
|
||||||
|
|
16
inc/ft_des.h
16
inc/ft_des.h
|
@ -14,6 +14,7 @@
|
||||||
# define FT_DES_H
|
# define FT_DES_H
|
||||||
|
|
||||||
# include <stdint.h>
|
# include <stdint.h>
|
||||||
|
# include "ft_base64.h"
|
||||||
|
|
||||||
# define FT_DES_BYTE_BLOCK_SIZE 8
|
# define FT_DES_BYTE_BLOCK_SIZE 8
|
||||||
# define FT_DES_BIT_BLOCK_SIZE 64
|
# define FT_DES_BIT_BLOCK_SIZE 64
|
||||||
|
@ -38,12 +39,16 @@ typedef struct s_des_ctx
|
||||||
int input_fd;
|
int input_fd;
|
||||||
int output_fd;
|
int output_fd;
|
||||||
int decode;
|
int decode;
|
||||||
|
int b64;
|
||||||
int output_in_base64;
|
int output_in_base64;
|
||||||
int readed;
|
int readed;
|
||||||
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
|
t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
|
||||||
t_byte1 round_keys[FT_DES_ROUND_COUNT]
|
t_byte1 round_keys[FT_DES_ROUND_COUNT]
|
||||||
[FT_DES_ROUND_KEY_SIZE];
|
[FT_DES_ROUND_KEY_SIZE];
|
||||||
|
t_base64_ctx b64_ctx;
|
||||||
|
t_base64_encode_buffer b64_encode_buffer;
|
||||||
|
t_base64_decode_buffer b64_decode_buffer;
|
||||||
} t_des_ctx;
|
} t_des_ctx;
|
||||||
|
|
||||||
typedef int (*t_ft_des_arg_parser_function)
|
typedef int (*t_ft_des_arg_parser_function)
|
||||||
|
@ -299,19 +304,24 @@ int ft_des_salt_arg_parser
|
||||||
t_des_ctx *c
|
t_des_ctx *c
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_ecb_process
|
void ft_des_ecb_decrypt
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_ecb_process_chunk
|
void ft_des_ecb_encrypt
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx
|
||||||
|
);
|
||||||
|
|
||||||
|
void ft_des_ecb_encode_process_chunk
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx,
|
t_des_ctx *ctx,
|
||||||
t_byte8 reaed,
|
t_byte8 reaed,
|
||||||
t_byte1 buffer[FT_DES_READ_SIZE]
|
t_byte1 buffer[FT_DES_READ_SIZE]
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_ecb_finish_process
|
void ft_des_ecb_finish_encrypt
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
int ft_des_base64_arg_parser
|
||||||
|
(
|
||||||
|
int argc,
|
||||||
|
char **argv,
|
||||||
|
int position,
|
||||||
|
t_des_ctx *ctx
|
||||||
|
)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
ctx->b64 = 1;
|
||||||
|
return (++position);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int ft_des_key_arg_parser
|
int ft_des_key_arg_parser
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -17,4 +32,18 @@ int ft_des_key_arg_parser
|
||||||
if (wrong_key_char != NULL)
|
if (wrong_key_char != NULL)
|
||||||
ft_des_print_error("there wrong char in key string.");
|
ft_des_print_error("there wrong char in key string.");
|
||||||
return (position + 2);
|
return (position + 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_des_decode_arg_parser
|
||||||
|
(
|
||||||
|
int argc,
|
||||||
|
char **argv,
|
||||||
|
int position,
|
||||||
|
t_des_ctx *ctx
|
||||||
|
)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
ctx->decode = 1;
|
||||||
|
return (++position);
|
||||||
}
|
}
|
|
@ -6,6 +6,14 @@ t_des_argument_parser g_arg_parsers[] = {
|
||||||
"-k",
|
"-k",
|
||||||
ft_des_key_arg_parser,
|
ft_des_key_arg_parser,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"-d",
|
||||||
|
ft_des_decode_arg_parser,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"-a",
|
||||||
|
ft_des_base64_arg_parser,
|
||||||
|
},
|
||||||
{ NULL, NULL},
|
{ NULL, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -49,6 +57,15 @@ void ft_des_ecb
|
||||||
|
|
||||||
ft_des_init_ctx(&ctx);
|
ft_des_init_ctx(&ctx);
|
||||||
parse_args(argc, argv, &ctx);
|
parse_args(argc, argv, &ctx);
|
||||||
ft_des_ecb_process(&ctx);
|
if (ctx.decode)
|
||||||
|
{
|
||||||
|
ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys);
|
||||||
|
ft_des_ecb_decrypt(&ctx);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys);
|
||||||
|
ft_des_ecb_encrypt(&ctx);
|
||||||
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
80
src/des/ft_des_ecb_decrypt.c
Normal file
80
src/des/ft_des_ecb_decrypt.c
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "ft_des.h"
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static t_byte8 buffered_read
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx,
|
||||||
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte8 total_readed;
|
||||||
|
t_byte8 current_read;
|
||||||
|
|
||||||
|
total_readed = 0;
|
||||||
|
current_read = 0;
|
||||||
|
while(total_readed < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
|
{
|
||||||
|
if ((current_read = read(
|
||||||
|
ctx->input_fd,
|
||||||
|
buffer + total_readed,
|
||||||
|
FT_DES_BYTE_BLOCK_SIZE - total_readed)) == 0)
|
||||||
|
return (total_readed + current_read);
|
||||||
|
total_readed += current_read;
|
||||||
|
}
|
||||||
|
return (total_readed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ouput
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx,
|
||||||
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (ctx->b64)
|
||||||
|
ft_base64_decode_chunk(
|
||||||
|
&ctx->b64_ctx,
|
||||||
|
FT_DES_BYTE_BLOCK_SIZE, buffer, &ctx->b64_decode_buffer);
|
||||||
|
else
|
||||||
|
write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void finish
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx,
|
||||||
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (ctx->b64)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (buffer[7] < 0 || buffer[7] > 8)
|
||||||
|
ft_des_print_error("wrong padding");
|
||||||
|
write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE - buffer[7]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_des_ecb_decrypt
|
||||||
|
(
|
||||||
|
t_des_ctx *c
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte8 readed;
|
||||||
|
t_byte8 last_read;
|
||||||
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
|
t_byte1 message[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
|
|
||||||
|
last_read = 0;
|
||||||
|
while((readed = buffered_read(c, buffer)))
|
||||||
|
{
|
||||||
|
if (readed != FT_DES_BYTE_BLOCK_SIZE)
|
||||||
|
ft_des_print_error("wrong message size");
|
||||||
|
if (last_read)
|
||||||
|
ouput(c, message);
|
||||||
|
ft_des_process_block(buffer, c->round_keys, message);
|
||||||
|
last_read = readed;
|
||||||
|
}
|
||||||
|
finish(c, message);
|
||||||
|
}
|
|
@ -10,10 +10,18 @@ static void ft_des_ecb_write
|
||||||
{
|
{
|
||||||
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
ft_des_process_block(buffer, ctx->round_keys, cyphertext);
|
ft_des_process_block(buffer, ctx->round_keys, cyphertext);
|
||||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
if (ctx->b64)
|
||||||
|
ft_base64_encode_chunk(
|
||||||
|
&ctx->b64_ctx,
|
||||||
|
FT_DES_BYTE_BLOCK_SIZE,
|
||||||
|
cyphertext,
|
||||||
|
&ctx->b64_encode_buffer
|
||||||
|
);
|
||||||
|
else
|
||||||
|
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_des_ecb_process_chunk
|
void ft_des_ecb_encode_process_chunk
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx,
|
t_des_ctx *ctx,
|
||||||
t_byte8 readed,
|
t_byte8 readed,
|
16
src/des/ft_des_ecb_encrypt.c
Normal file
16
src/des/ft_des_ecb_encrypt.c
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "ft_des.h"
|
||||||
|
#include "ft_base64.h"
|
||||||
|
|
||||||
|
void ft_des_ecb_encrypt
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte1 buffer[FT_DES_READ_SIZE];
|
||||||
|
t_byte8 readed;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
16
src/des/ft_des_ecb_finish_decrypt.c
Normal file
16
src/des/ft_des_ecb_finish_decrypt.c
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "ft_des.h"
|
||||||
|
|
||||||
|
void ft_des_ecb_finish_decrypt
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx,
|
||||||
|
t_des_decrypt_buffer *buff
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte1 idx;
|
||||||
|
|
||||||
|
idx = 0;
|
||||||
|
ctx->readed += buff->readed[buff->current_buffer];
|
||||||
|
if (ctx->readed % FT_DES_BYTE_BLOCK_SIZE != 0)
|
||||||
|
ft_des_print_error("wrong message size, maybe corrupted?");
|
||||||
|
while (idx + FT_DES_BYTE_BLOCK_SIZE <=)
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void ft_des_ecb_finish_process
|
void ft_des_ecb_finish_encrypt
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
)
|
)
|
||||||
|
@ -19,5 +19,14 @@ void ft_des_ecb_finish_process
|
||||||
buffer_index++;
|
buffer_index++;
|
||||||
}
|
}
|
||||||
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
|
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
|
||||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
if (ctx->b64)
|
||||||
|
{
|
||||||
|
ft_base64_encode_chunk(&ctx->b64_ctx, FT_DES_BYTE_BLOCK_SIZE,
|
||||||
|
cyphertext,
|
||||||
|
&ctx->b64_encode_buffer
|
||||||
|
);
|
||||||
|
ft_base64_encode_finish(&ctx->b64_ctx, &ctx->b64_encode_buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
}
|
}
|
|
@ -1,16 +0,0 @@
|
||||||
#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);
|
|
||||||
}
|
|
|
@ -19,7 +19,11 @@ void ft_des_init_ctx
|
||||||
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
|
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
ctx->readed = 0;
|
ctx->readed = 0;
|
||||||
ctx->decode = 0;
|
ctx->decode = 0;
|
||||||
|
ctx->b64 = 0;
|
||||||
ctx->output_in_base64 = 0;
|
ctx->output_in_base64 = 0;
|
||||||
ctx->input_fd = STDIN_FILENO;
|
ctx->input_fd = STDIN_FILENO;
|
||||||
ctx->output_fd = STDOUT_FILENO;
|
ctx->output_fd = STDOUT_FILENO;
|
||||||
|
ft_base64_init(&ctx->b64_ctx);
|
||||||
|
ft_base64_init_encode_buffer(&ctx->b64_encode_buffer);
|
||||||
|
ft_base64_init_decode_buffer(&ctx->b64_decode_buffer);
|
||||||
}
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <unistd.h>
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -6,8 +7,8 @@ void ft_des_print_error
|
||||||
const char *error
|
const char *error
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
ft_putstr("des: ");
|
ft_putstr_fd("des: ", STDERR_FILENO);
|
||||||
ft_putstr(error);
|
ft_putstr_fd(error, STDERR_FILENO);
|
||||||
ft_putstr("\n");
|
ft_putstr_fd("\n", STDERR_FILENO);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
1
t/cases/foo_bar.txt
Normal file
1
t/cases/foo_bar.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
foo bar
|
1
t/cases/foo_bar_enc.txt
Normal file
1
t/cases/foo_bar_enc.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
a‘w@¦šmu0ÄŘ7ˇ¸L
|
Loading…
Reference in a new issue