working ecb and cbc
This commit is contained in:
parent
28c7f78365
commit
a73c34859b
10 changed files with 40 additions and 12 deletions
|
@ -57,7 +57,8 @@ typedef struct s_des_ctx
|
||||||
const char *raw_salt;
|
const char *raw_salt;
|
||||||
const char *raw_key;
|
const char *raw_key;
|
||||||
const char *raw_iv;
|
const char *raw_iv;
|
||||||
t_ft_des_mode mode;
|
t_ft_des_mode encrypt;
|
||||||
|
t_ft_des_mode decrypt;
|
||||||
} t_des_ctx;
|
} t_des_ctx;
|
||||||
|
|
||||||
typedef int (*t_ft_des_arg_parser_function)
|
typedef int (*t_ft_des_arg_parser_function)
|
||||||
|
|
|
@ -16,7 +16,7 @@ static void xor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mode
|
static void encrypt
|
||||||
(
|
(
|
||||||
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
||||||
|
@ -25,6 +25,7 @@ static void mode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE];
|
t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE];
|
||||||
|
|
||||||
ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, input_bits,
|
ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, input_bits,
|
||||||
FT_DES_BIT_BLOCK_SIZE);
|
FT_DES_BIT_BLOCK_SIZE);
|
||||||
xor(input_bits, iv);
|
xor(input_bits, iv);
|
||||||
|
@ -35,6 +36,26 @@ static void mode
|
||||||
FT_DES_BIT_BLOCK_SIZE);
|
FT_DES_BIT_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
ft_des_bits_to_bytes(output_bits, FT_DES_BIT_BLOCK_SIZE, output,
|
||||||
|
FT_DES_BYTE_BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
void ft_des_cbc
|
void ft_des_cbc
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -47,6 +68,7 @@ void ft_des_cbc
|
||||||
ft_des_parse_args(argc, argv, &ctx);
|
ft_des_parse_args(argc, argv, &ctx);
|
||||||
if (ctx.raw_key && !ctx.raw_iv)
|
if (ctx.raw_key && !ctx.raw_iv)
|
||||||
ft_des_print_error("initialization vector required when key specified");
|
ft_des_print_error("initialization vector required when key specified");
|
||||||
ctx.mode = mode;
|
ctx.encrypt = encrypt;
|
||||||
|
ctx.decrypt = decrypt;
|
||||||
ft_des(&ctx);
|
ft_des(&ctx);
|
||||||
}
|
}
|
|
@ -44,7 +44,7 @@ void ft_des_decrypt
|
||||||
ft_des_print_error("wrong message size");
|
ft_des_print_error("wrong message size");
|
||||||
if (last_read)
|
if (last_read)
|
||||||
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
|
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
c->mode(buffer, c->round_keys, c->iv, message);
|
c->decrypt(buffer, c->round_keys, c->iv, message);
|
||||||
last_read = readed;
|
last_read = readed;
|
||||||
}
|
}
|
||||||
if (message[7] < 0 || message[7] > 8)
|
if (message[7] < 0 || message[7] > 8)
|
||||||
|
|
|
@ -32,13 +32,14 @@ void ft_des_decryption_key_routine
|
||||||
char pass[128];
|
char pass[128];
|
||||||
if (ctx->raw_password || !ctx->raw_key)
|
if (ctx->raw_password || !ctx->raw_key)
|
||||||
get_salt(ctx, salt);
|
get_salt(ctx, salt);
|
||||||
if (!ctx->raw_password && !ctx->raw_key)
|
if (!ctx->raw_key && !ctx->raw_password)
|
||||||
{
|
{
|
||||||
ft_des_get_password(pass);
|
ft_des_get_password(pass);
|
||||||
ctx->raw_password = pass;
|
ctx->raw_password = pass;
|
||||||
|
}
|
||||||
|
if (ctx->raw_password)
|
||||||
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
|
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
|
||||||
(char *)ctx->raw_password);
|
(char *)ctx->raw_password);
|
||||||
}
|
|
||||||
if (ctx->raw_key)
|
if (ctx->raw_key)
|
||||||
ft_des_set_raw_key(ctx);
|
ft_des_set_raw_key(ctx);
|
||||||
if (ctx->raw_iv)
|
if (ctx->raw_iv)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
|
|
||||||
static void mode
|
static void encrypt
|
||||||
(
|
(
|
||||||
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
||||||
|
@ -22,6 +22,7 @@ void ft_des_ecb
|
||||||
|
|
||||||
ft_des_init_ctx(&ctx);
|
ft_des_init_ctx(&ctx);
|
||||||
ft_des_parse_args(argc, argv, &ctx);
|
ft_des_parse_args(argc, argv, &ctx);
|
||||||
ctx.mode = mode;
|
ctx.encrypt = encrypt;
|
||||||
|
ctx.decrypt = encrypt;
|
||||||
ft_des(&ctx);
|
ft_des(&ctx);
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ static void ft_des_ecb_write
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
ctx->mode(buffer, ctx->round_keys, ctx->iv, cyphertext);
|
ctx->encrypt(buffer, ctx->round_keys, ctx->iv, cyphertext);
|
||||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,6 @@ void ft_des_finish_encrypt
|
||||||
ctx->buffer[buffer_index] = padding_size;
|
ctx->buffer[buffer_index] = padding_size;
|
||||||
buffer_index++;
|
buffer_index++;
|
||||||
}
|
}
|
||||||
ctx->mode(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext);
|
ctx->encrypt(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext);
|
||||||
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
}
|
}
|
|
@ -27,5 +27,6 @@ void ft_des_init_ctx
|
||||||
ctx->raw_salt = NULL;
|
ctx->raw_salt = NULL;
|
||||||
ctx->raw_key = NULL;
|
ctx->raw_key = NULL;
|
||||||
ctx->raw_iv = NULL;
|
ctx->raw_iv = NULL;
|
||||||
ctx->mode = NULL;
|
ctx->encrypt = NULL;
|
||||||
|
ctx->decrypt = NULL;
|
||||||
}
|
}
|
1
t/cases/one_deep_secret_enc.txt
Normal file
1
t/cases/one_deep_secret_enc.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Salted__oñîa³(_@Ž Á=_€<ý·©<C2B7>üBúÝr^q
|
|
@ -668,7 +668,8 @@ int init_ctx()
|
||||||
_is(ctx.raw_key == NULL);
|
_is(ctx.raw_key == NULL);
|
||||||
_is(ctx.raw_salt == NULL);
|
_is(ctx.raw_salt == NULL);
|
||||||
_is(ctx.raw_iv == NULL);
|
_is(ctx.raw_iv == NULL);
|
||||||
_is(ctx.mode == NULL);
|
_is(ctx.encrypt == NULL);
|
||||||
|
_is(ctx.decrypt == NULL);
|
||||||
_end("shoud init ctx");
|
_end("shoud init ctx");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue