Compare commits

..

No commits in common. "a517fbf50ce4cd5e27b296155ebb28036e5d6887" and "bb9bc8cba1f88af3e0dcd84ea2d91db00bcd43ad" have entirely different histories.

99 changed files with 997 additions and 2466 deletions

View file

@ -138,33 +138,20 @@ 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_parse_args.c \
ft_des_arg_parsers.c \
ft_des_more_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 \
ft_des_get_password.c \
ft_des_derive_key.c \
ft_des_encryption_key_routine.c \
ft_des_decryption_key_routine.c \
ft_des_print_error.c \
ft_des_encrypt.c \
ft_des_decrypt.c \
ft_des_decrypt_b64.c \
ft_des_encrypt_b64.c \
ft_des_encode_process_chunk.c \
ft_des_arg_parsers.c \
ft_des_ecb_encrypt.c \
ft_des_ecb_decrypt.c \
ft_des_ecb_decrypt_b64.c \
ft_des_ecb_encrypt_b64.c \
ft_des_ecb_encode_process_chunk.c \
ft_des_hex_to_bit.c \
ft_des_hex_to_byte.c \
ft_des_byte_to_bits.c \
ft_des_bits_to_bytes.c \
ft_des_finish_encrypt.c \
ft_des_buffered_read.c
ft_des_ecb_finish_encrypt.c
KD2_SRC = ft_hmac_sha256_init_ctx.c \
ft_hmac_sha256.c \
@ -264,18 +251,12 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR)
$(OBJ_DIR):
mkdir $(OBJ_DIR)
$(OBJ_DIR)%.o: %.c $(LIBFT) $(OPENSSL_CRY) $(OPENSSL_SSL) $(HEADERS)
$(OBJ_DIR)%.o: %.c $(LIBFT) $(HEADERS)
$(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS)
$(LIBFT):
$(MAKE) -C $(LIBFT_DIR)
$(OPENSSL_CRY) $(OPENSSL_SSL):
cd $(OPENSSL_DIR) && \
./config --prefix=$(OPENSSL_BLD) && \
make -j 4 && \
make -j 4 install
all: $(NAME)
check: $(TEST_BIN)
@ -290,7 +271,6 @@ clean:
fclean: clean
rm -f $(NAME)
rm -f $(TEST_BIN)
rm -rf $(OPENSSL_BLD)
$(MAKE) -C $(LIBFT_DIR) fclean
re: fclean all

View file

@ -14,7 +14,6 @@
# define FT_DES_H
# include <stdint.h>
# include <stddef.h>
# define FT_DES_BYTE_BLOCK_SIZE 8
# define FT_DES_BIT_BLOCK_SIZE 64
@ -33,13 +32,6 @@
typedef uint64_t t_byte8;
typedef unsigned char t_byte1;
typedef void (*t_ft_des_mode)
(
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]
);
typedef struct s_des_ctx
{
@ -48,17 +40,15 @@ typedef struct s_des_ctx
int decode;
int b64;
int readed;
t_byte1 key[FT_DES_BIT_BLOCK_SIZE];
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE];
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
t_byte1 salt[FT_DES_BIT_BLOCK_SIZE];
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT]
[FT_DES_ROUND_KEY_SIZE];
const char *raw_password;
const char *raw_salt;
const char *raw_key;
const char *raw_iv;
t_ft_des_mode encrypt;
t_ft_des_mode decrypt;
} t_des_ctx;
typedef int (*t_ft_des_arg_parser_function)
@ -231,9 +221,10 @@ void ft_des_print_error
const char *error
);
void ft_des
void ft_des_ecb
(
t_des_ctx *ctx
int argc,
char **argv
);
void ft_des_init_ctx
@ -241,13 +232,6 @@ void ft_des_init_ctx
t_des_ctx *ctx
);
void ft_des_parse_args
(
int argc,
char **argv,
t_des_ctx *ctx
);
int ft_des_key_arg_parser
(
int argc,
@ -256,14 +240,6 @@ int ft_des_key_arg_parser
t_des_ctx *c
);
int ft_des_iv_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *c
);
int ft_des_decode_arg_parser
(
int argc,
@ -328,70 +304,34 @@ 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
);
void ft_des_ecb
(
int argc,
char **argv
);
void ft_des_cbc
(
int argc,
char **argv
);
void ft_des_pcbc
(
int argc,
char **argv
);
void ft_des_decrypt
void ft_des_ecb_decrypt
(
t_des_ctx *ctx
);
void ft_des_decrypt_b64
void ft_des_ecb_decrypt_b64
(
t_des_ctx *ctx
);
void ft_des_encrypt
void ft_des_ecb_encrypt
(
t_des_ctx *ctx
);
void ft_des_encrypt_b64
void ft_des_ecb_encrypt_b64
(
t_des_ctx *ctx
);
void ft_des_encode_process_chunk
void ft_des_ecb_encode_process_chunk
(
t_des_ctx *ctx,
t_byte8 reaed,
t_byte1 buffer[FT_DES_READ_SIZE]
);
void ft_des_finish_encrypt
void ft_des_ecb_finish_encrypt
(
t_des_ctx *ctx
);
@ -424,59 +364,9 @@ void ft_des_byte_to_bits
t_byte8 bits_len
);
void ft_des_bits_to_bytes
(
t_byte1 *bits,
t_byte8 bits_len,
t_byte1 *bytes,
t_byte8 byte_len
);
void ft_des_encryption_key_routine
void ft_des_derive_key
(
t_des_ctx *ctx
);
void ft_des_decryption_key_routine
(
t_des_ctx *ctx
);
void ft_des_set_raw_key
(
t_des_ctx *ctx
);
void ft_des_set_raw_iv
(
t_des_ctx *ctx
);
void ft_des_get_password
(
char pass[128]
);
void ft_des_derive_key_and_iv
(
t_byte1 key[FT_DES_BIT_BLOCK_SIZE],
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE],
char salt[FT_DES_BYTE_BLOCK_SIZE],
char *pass
);
void ft_des_parse_args
(
int argc,
char **argv,
t_des_ctx *ctx
);
int ft_des_buffered_read
(
int fd,
char *buffer,
size_t buff_size
);
#endif

View file

@ -1,14 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pbkdf2.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 15:56:11 by gtertysh #+# #+# */
/* Updated: 2019/04/10 15:56:16 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PBKDF2_H
# define FT_PBKDF2_H
@ -76,4 +65,5 @@ void ft_pbkdf2_sha256
t_pbkdf2_sha256_ctx *ctx
);
#endif

10
inc/t.h
View file

@ -15,10 +15,10 @@
# include <stdio.h>
# define FAIL() printf("\nfail in %s() %s:%d\n\n", __func__, __FILE__, __LINE__)
# define _IS(t) do { if (!(t)) { FAIL(); return 1; } } while(0)
# define _SHOULD(t) do { int r = t(); if(r) return r; } while(0)
# define _P_START(m) do { printf("%s\n", m); } while(0)
# define _VERIFY(m, t) do { _P_START(m); int r = t(); if(r) return r; } while(0)
# define _END(m) do { printf(" %s - OK\n", m); return 0; } while(0)
# define _is(t) do { if (!(t)) { FAIL(); return 1; } } while(0)
# define _should(t) do { int r = t(); if(r) return r; } while(0)
# define _p_start(m) do { printf("%s\n", m); } while(0)
# define _verify(m, t) do { _p_start(m); int r = t(); if(r) return r; } while(0)
# define _end(m) do { printf(" %s - OK\n", m); return 0; } while(0)
#endif

2
obj/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
./*
!.gitignore

29
openssl_cases.txt Normal file
View file

@ -0,0 +1,29 @@
// ENCRYPTING
1. "openssl des-ecb -S a"
prompts to enter password, generate key with given pass and salt,
prepend salt header.
2. "openssl des-ecb -pass pass:asd"
generate salt and key, prepend salt header.
3. "openssl des-ecb -S a -pass:asd"
generate key from givent salt and pass, prepend header.
4. "openssl des-ecb -S a -pass:asd -K 1"
encrypt with GIVEN key, prepend GIVEN salt hedaer.
5. "openssl des-ecb -K 1"
encrypt with given key, doesn't generate and prepend salt.
6. "openssl des-ecb -S 0 -pass pass:asd -P -pbkdf2"
generate key using PKCS5_PBKDF2_HMAC with 10000 iteration,
8 byte salt, 3 byte key, sha256 hash function
// DECRYPTING
7. "echo -n Salted__ | openssl des-ecb -d"
prompts to enter password, generate key but fails to decode.
8. "echo -n Salted__aaaaaaaa | openssl des-ecb -d"
prompts to enter password, generate key but fails to validate padding.

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:05:46 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:07:56 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/stat.h>
@ -17,13 +5,6 @@
#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;
@ -40,20 +21,24 @@ static int open_input(char *filename)
ft_putstr_fd("base64: input path is not a file", STDERR_FILENO);
exit(1);
}
return (fd);
return fd;
}
static int open_output(char *filename)
{
int fd;
if ((fd = open((const char *)filename,
O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1)
{
perror("base64: output stream error");
exit(1);
}
return (fd);
return fd;
}
static void init_flags(t_base64_flags *flags)
{
flags->decode = 0;
}
static void read_args
@ -69,7 +54,7 @@ static void read_args
char *next_arg;
i = 0;
while (i < argc)
while(i < argc)
{
current_arg = argv[i];
next_arg = i + 1 < argc ? argv[i + 1] : NULL;
@ -81,8 +66,6 @@ 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++;
@ -94,7 +77,7 @@ void ft_base64(int argc, char **argv)
t_base64_flags flags;
t_base64_ctx ctx;
flags.decode = 0;
init_flags(&flags);
ft_base64_init(&ctx);
read_args(argc, argv, &flags, &ctx);
if (flags.decode)

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_decode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:08:40 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:09:10 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_base64.h"
@ -20,7 +8,7 @@ void ft_base64_decode(t_base64_ctx *ctx)
t_byte8 readed;
ft_base64_init_decode_buffer(&decode_buff);
while ((readed = read(ctx->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0)
while((readed = read(ctx->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0)
{
readed = ft_base64_decode_filter(readed, read_buff);
ft_base64_decode_chunk(ctx, readed, read_buff, &decode_buff);

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_decode_chunk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:09:28 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:09:50 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
#include "ft_base64.h"
@ -51,7 +39,7 @@ void ft_base64_decode_chunk
ft_memcpy(&buff->block[buffer_index], message, free_space_in_buffer);
ft_base64_decode_write(ctx, buff->block);
idx = free_space_in_buffer;
while (idx + FT_BASE64_DECODE_BLOCK_SIZE <= message_len)
while(idx + FT_BASE64_DECODE_BLOCK_SIZE <= message_len)
{
ft_base64_decode_write(ctx, &message[idx]);
idx += FT_BASE64_DECODE_BLOCK_SIZE;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_decode_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:09:58 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:10:14 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"
@ -24,7 +12,7 @@ t_byte8 ft_base64_decode_filter
i = 0;
last_free_index = 0;
while (i < readed)
while(i < readed)
{
if (!ft_isspace(buffer[i]))
{

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_decode_finish.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:10:18 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:10:31 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "ft_base64.h"
#include "libft.h"

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_decode_transform.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:10:34 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:11:02 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_encode_chunk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:11:15 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:11:28 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"
@ -45,7 +33,7 @@ void ft_base64_encode_chunk
ft_memcpy(&buff->block[buffer_index], message, free_space_in_buffer);
ft_base64_encode_write(ctx, buff->block);
idx = free_space_in_buffer;
while (idx + FT_BASE64_ENCODE_BLOCK_SIZE <= message_len)
while(idx + FT_BASE64_ENCODE_BLOCK_SIZE <= message_len)
{
ft_base64_encode_write(ctx, &message[idx]);
idx += FT_BASE64_ENCODE_BLOCK_SIZE;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_encode_finish.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:11:30 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:11:42 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:12:05 by gtertysh ### ########.fr */
/* Updated: 2019/02/02 18:26:51 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,8 +29,10 @@ void ft_base64_encode_transform
second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf);
third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3);
fourth_char = data[2] & 0x3F;
chars[0] = ctx->alphabet[first_char];
chars[1] = ctx->alphabet[second_char];
chars[2] = ctx->alphabet[third_char];
chars[3] = ctx->alphabet[fourth_char];
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_init_decode_buffer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:12:17 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:12:33 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_init_encode_buffer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:12:46 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:12:48 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include "libft.h"

View file

@ -1,17 +1,5 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_write.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:12:56 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:13:12 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_base64.h"
#include <unistd.h>
# include "ft_base64.h"
# include <unistd.h>
void ft_base64_write(t_base64_ctx *ctx)
{

View file

@ -1,36 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:18:03 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:18:15 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_des.h"
void ft_des
(
t_des_ctx *ctx
)
{
if (ctx->decode)
{
if (ctx->b64)
ft_des_decrypt_b64(ctx);
else
ft_des_decrypt(ctx);
}
else
{
if (ctx->b64)
ft_des_encrypt_b64(ctx);
else
ft_des_encrypt(ctx);
}
exit(0);
}

View file

@ -1,15 +1,8 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_arg_parsers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:18:18 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:20:51 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "ft_des.h"
int ft_des_base64_arg_parser
@ -26,6 +19,7 @@ int ft_des_base64_arg_parser
return (++position);
}
int ft_des_key_arg_parser
(
int argc,
@ -40,21 +34,6 @@ int ft_des_key_arg_parser
return (position + 2);
}
int ft_des_iv_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no initialization vector after -k flag. \
type -h for help.");
ctx->raw_iv = argv[position + 1];
return (position + 2);
}
int ft_des_decode_arg_parser
(
int argc,
@ -82,3 +61,73 @@ int ft_des_encode_arg_parser
ctx->decode = 0;
return (++position);
}
int ft_des_input_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
struct stat stat_buff;
if (position + 1 >= argc)
ft_des_print_error("there is no filaname after -i flag.");
if ((ctx->input_fd = open(argv[position + 1], O_RDONLY, 0)) == -1)
{
perror("des");
exit(1);
}
stat(argv[position + 1], &stat_buff);
if (S_ISDIR(stat_buff.st_mode))
ft_des_print_error("input path is not a file.");
return (position + 2);
}
int ft_des_output_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no filaname after -o flag.");
if ((ctx->output_fd = open(
argv[position + 1], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1)
{
perror("des");
exit(1);
}
return (position + 2);
}
int ft_des_password_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
ctx->raw_password = argv[position + 1];
return (position + 2);
}
int ft_des_salt_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
ctx->raw_salt = argv[position + 1];
return (position + 2);
}

View file

@ -1,41 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_bits_to_bytes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:20:55 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:21:03 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_bits_to_bytes
(
t_byte1 *bits,
t_byte8 bits_len,
t_byte1 *bytes,
t_byte8 bytes_len
)
{
t_byte8 bits_count;
t_byte8 bytes_count;
bits_count = 0;
bytes_count = 0;
while (bits_count < bits_len && bytes_count < bytes_len)
{
bytes[bytes_count] = (bits[bits_count] << 7) |
(bits[bits_count + 1] << 6) |
(bits[bits_count + 2] << 5) |
(bits[bits_count + 3] << 4) |
(bits[bits_count + 4] << 3) |
(bits[bits_count + 5] << 2) |
(bits[bits_count + 6] << 1) |
(bits[bits_count + 7]);
bytes_count++;
bits_count += 8;
}
}

View file

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_buffered_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:21:20 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:21:49 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
int ft_des_buffered_read
(
int fd,
char *buffer,
size_t buff_size
)
{
size_t total_readed;
size_t current_read;
total_readed = 0;
current_read = 0;
while (total_readed < buff_size)
{
if ((current_read = read(
fd,
buffer + total_readed,
buff_size - total_readed)) == 0)
return (total_readed + current_read);
total_readed += current_read;
}
return (total_readed);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_byte_to_bits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:21:52 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:22:09 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_byte_to_bits
@ -25,7 +13,7 @@ void ft_des_byte_to_bits
bits_count = 0;
bytes_count = 0;
while (bits_count < bits_len && bytes_count < bytes_len)
while(bits_count < bits_len && bytes_count < bytes_len)
{
bits[bits_count] = bytes[bytes_count] >> 7 & 1;
bits[bits_count + 1] = bytes[bytes_count] >> 6 & 1;

View file

@ -1,86 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_cbc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:22:17 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:23:17 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#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];
ft_des_byte_to_bits(input, FT_DES_BYTE_BLOCK_SIZE, input_bits,
FT_DES_BIT_BLOCK_SIZE);
xor(input_bits, iv);
ft_des_bits_to_bytes(input_bits, FT_DES_BIT_BLOCK_SIZE, input,
FT_DES_BYTE_BLOCK_SIZE);
ft_des_process_block(input, keys, output);
ft_des_byte_to_bits(output, FT_DES_BYTE_BLOCK_SIZE, iv,
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
(
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);
}

View file

@ -1,65 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_decrypt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:23:37 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:23:58 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#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);
}
void ft_des_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];
ft_des_decryption_key_routine(c);
ft_des_generate_decryption_round_keys(c->key, c->round_keys);
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)
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
c->decrypt(buffer, c->round_keys, c->iv, message);
last_read = readed;
}
if (message[7] < 0 || message[7] > 8)
ft_des_print_error("wrong padding");
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]);
}

View file

@ -1,62 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_decrypt_b64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:24:25 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:24:56 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_decode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
close(pipe_fd[0]);
ft_base64_init(&b64_ctx);
b64_ctx.input_fd = ctx->input_fd;
b64_ctx.output_fd = pipe_fd[1];
ft_base64_decode(&b64_ctx);
}
static void des_decrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[1]);
ctx->input_fd = pipe_fd[0];
ft_des_decrypt(ctx);
}
void ft_des_decrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
base64_decode(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
des_decrypt(ctx, pipe_fd);
}

View file

@ -1,60 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_decryption_key_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:26:30 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:27:41 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdio.h>
#include "ft_des.h"
#include "libft.h"
static void get_salt
(
t_des_ctx *c,
char salt[FT_DES_BYTE_BLOCK_SIZE]
)
{
char buffer[FT_DES_BYTE_BLOCK_SIZE];
int readed;
readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
if (readed != FT_DES_BYTE_BLOCK_SIZE)
ft_des_print_error("error reading input");
if (ft_strncmp(buffer, "Salted__", FT_DES_BYTE_BLOCK_SIZE) != 0)
ft_des_print_error("bad magic number");
readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
if (readed != FT_DES_BYTE_BLOCK_SIZE)
ft_des_print_error("error reading input");
ft_memcpy(salt, buffer, FT_DES_BYTE_BLOCK_SIZE);
}
void ft_des_decryption_key_routine
(
t_des_ctx *ctx
)
{
char salt[FT_DES_BYTE_BLOCK_SIZE];
char pass[128];
if (ctx->raw_password || !ctx->raw_key)
get_salt(ctx, salt);
if (!ctx->raw_key && !ctx->raw_password)
{
ft_des_get_password(pass);
ctx->raw_password = pass;
}
if (ctx->raw_password)
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
(char *)ctx->raw_password);
if (ctx->raw_key)
ft_des_set_raw_key(ctx);
if (ctx->raw_iv)
ft_des_set_raw_iv(ctx);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_derive_decryption_round_key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:28:02 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:28:06 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_derive_decryption_round_key

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_derive_encryption_round_key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:28:18 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:28:20 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_derive_encryption_round_key

View file

@ -1,41 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_derive_key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:29:33 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:29:40 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
#include "ft_pbkdf2.h"
#include "libft.h"
void ft_des_derive_key_and_iv
void ft_des_derive_key
(
t_byte1 key[FT_DES_BIT_BLOCK_SIZE],
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE],
char salt[FT_DES_BYTE_BLOCK_SIZE],
char *pass
t_des_ctx *ctx
)
{
t_pbkdf2_sha256_ctx pbkdf_ctx;
t_byte1 key_iv[FT_DES_BYTE_BLOCK_SIZE * 2];
t_byte1 key_iv_bit[FT_DES_BIT_BLOCK_SIZE * 2];
t_byte1 key[FT_DES_BYTE_BLOCK_SIZE];
if (ctx->raw_key)
{
if (ft_des_hex_to_bit(ctx->raw_key, ctx->key, FT_DES_BIT_BLOCK_SIZE))
ft_des_print_error("wrong char in key");
return ;
}
// if (ctx->decode)
// {
// readed = read(ctx->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
// if (readed != FT_DES_BYTE_BLOCK_SIZE)
// ft_des_print_error("wrong input");
// if (ft_strcmp("Salted__", buffer) == 0)
// {
// readed = read(ctx->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
// if (readed != FT_DES_BYTE_BLOCK_SIZE)
// ft_des_print_error("wrong salt");
// pbkdf_ctx.salt = buffer;
// }
// else
// ft_des_pr
// }
pbkdf_ctx.iterations = 10000;
pbkdf_ctx.key = key_iv;
pbkdf_ctx.key = key;
pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE;
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE * 2;
pbkdf_ctx.pass_len = ft_strlen(pass);
pbkdf_ctx.pass = (t_byte1 *)pass;
pbkdf_ctx.salt = (t_byte1 *)salt;
pbkdf_ctx.pass = (t_byte1 *)ctx->raw_password;
pbkdf_ctx.salt = ctx->salt;
pbkdf_ctx.pass_len = ft_strlen((char *)ctx->raw_password);
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE;
ft_pbkdf2_sha256(&pbkdf_ctx);
ft_des_byte_to_bits(key_iv, FT_DES_BYTE_BLOCK_SIZE * 2, key_iv_bit,
FT_DES_BIT_BLOCK_SIZE * 2);
ft_memcpy(key, key_iv_bit, FT_DES_BIT_BLOCK_SIZE);
ft_memcpy(iv, key_iv_bit + FT_DES_BIT_BLOCK_SIZE, FT_DES_BIT_BLOCK_SIZE);
ft_des_byte_to_bits(key, FT_DES_BYTE_BLOCK_SIZE, ctx->key,
FT_DES_INITIAL_KEY_SIZE);
}

View file

@ -1,27 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_ecb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:30:08 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:30:27 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
#include "ft_pbkdf2.h"
#include "libft.h"
static void encrypt
t_des_argument_parser g_arg_parsers[] = {
{
"-d",
ft_des_decode_arg_parser,
},
{
"-e",
ft_des_encode_arg_parser,
},
{
"-a",
ft_des_base64_arg_parser,
},
{
"-i",
ft_des_input_file_arg_parser,
},
{
"-o",
ft_des_output_file_arg_parser,
},
{
"-k",
ft_des_key_arg_parser,
},
{
"-p",
ft_des_password_arg_parser,
},
{
"-s",
ft_des_salt_arg_parser,
},
{ NULL, NULL},
};
static void parse_args
(
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]
int argc,
char **argv,
t_des_ctx *ctx
)
{
(void)iv;
ft_des_process_block(input, keys, output);
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
@ -33,8 +77,21 @@ void ft_des_ecb
t_des_ctx ctx;
ft_des_init_ctx(&ctx);
ft_des_parse_args(argc, argv, &ctx);
ctx.encrypt = encrypt;
ctx.decrypt = encrypt;
ft_des(&ctx);
parse_args(argc, argv, &ctx);
ft_des_derive_key(&ctx);
if (ctx.decode)
{
if (ctx.b64)
ft_des_ecb_decrypt_b64(&ctx);
else
ft_des_ecb_decrypt(&ctx);
}
else
{
if (ctx.b64)
ft_des_ecb_encrypt_b64(&ctx);
else
ft_des_ecb_encrypt(&ctx);
}
exit(0);
}

View file

@ -0,0 +1,52 @@
#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);
}
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];
ft_des_generate_decryption_round_keys(c->key, c->round_keys);
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)
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
ft_des_process_block(buffer, c->round_keys, message);
last_read = readed;
}
if (message[7] < 0 || message[7] > 8)
ft_des_print_error("wrong padding");
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]);
}

View file

@ -0,0 +1,50 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_decode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
ft_base64_init(&b64_ctx);
b64_ctx.input_fd = ctx->input_fd;
b64_ctx.output_fd = pipe_fd[1];
ft_base64_decode(&b64_ctx);
}
static void des_ecb_decrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[1]);
ctx->input_fd = pipe_fd[0];
ft_des_ecb_decrypt(ctx);
}
void ft_des_ecb_decrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
(void)ctx;
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
base64_decode(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
des_ecb_decrypt(ctx, pipe_fd);
}

View file

@ -0,0 +1,45 @@
#include <unistd.h>
#include "ft_des.h"
#include "libft.h"
static void ft_des_ecb_write
(
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE],
t_des_ctx *ctx
)
{
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ft_des_process_block(buffer, ctx->round_keys, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}
void ft_des_ecb_encode_process_chunk
(
t_des_ctx *ctx,
t_byte8 readed,
t_byte1 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_des_ecb_write(ctx->buffer, ctx);
idx = free_space_in_buffer;
while(idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
{
ft_des_ecb_write(&buffer[idx], ctx);
idx += FT_DES_BYTE_BLOCK_SIZE;
}
buffer_index = 0;
}
else
idx = 0;
ft_memcpy(&ctx->buffer[buffer_index], &buffer[idx], readed - idx);
}

View file

@ -0,0 +1,17 @@
#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;
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_encode_process_chunk(ctx, readed, buffer);
ft_des_ecb_finish_encrypt(ctx);
}

View file

@ -0,0 +1,51 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_encode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
close(pipe_fd[1]);
ft_base64_init(&b64_ctx);
b64_ctx.output_fd = ctx->output_fd;
b64_ctx.input_fd = pipe_fd[0];
ft_base64_encode(&b64_ctx);
}
static void des_ecb_encrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[0]);
ctx->output_fd = pipe_fd[1];
ft_des_ecb_encrypt(ctx);
}
void ft_des_ecb_encrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
(void)ctx;
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
des_ecb_encrypt(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
base64_encode(ctx, pipe_fd);
}

View 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 <=)
}

View file

@ -0,0 +1,23 @@
#include "ft_des.h"
#include <unistd.h>
void ft_des_ecb_finish_encrypt
(
t_des_ctx *ctx
)
{
t_byte1 buffer_index;
t_byte1 padding_size;
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE;
padding_size = FT_DES_BYTE_BLOCK_SIZE - buffer_index;
while(buffer_index < FT_DES_BYTE_BLOCK_SIZE)
{
ctx->buffer[buffer_index] = padding_size;
buffer_index++;
}
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}

View file

@ -1,58 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_encode_process_chunk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:32:07 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:32:37 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
#include "libft.h"
static void ft_des_ecb_write
(
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE],
t_des_ctx *ctx
)
{
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ctx->encrypt(buffer, ctx->round_keys, ctx->iv, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}
void ft_des_encode_process_chunk
(
t_des_ctx *ctx,
t_byte8 readed,
t_byte1 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_des_ecb_write(ctx->buffer, ctx);
idx = free_space_in_buffer;
while (idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
{
ft_des_ecb_write(&buffer[idx], ctx);
idx += FT_DES_BYTE_BLOCK_SIZE;
}
buffer_index = 0;
}
else
idx = 0;
ft_memcpy(&ctx->buffer[buffer_index], &buffer[idx], readed - idx);
}

View file

@ -1,30 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_encrypt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:33:05 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:33:11 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
#include "ft_base64.h"
void ft_des_encrypt
(
t_des_ctx *ctx
)
{
t_byte1 buffer[FT_DES_READ_SIZE];
t_byte8 readed;
ft_des_encryption_key_routine(ctx);
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_encode_process_chunk(ctx, readed, buffer);
ft_des_finish_encrypt(ctx);
}

View file

@ -1,62 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_encrypt_b64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:33:39 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:34:06 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_encode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
close(pipe_fd[1]);
ft_base64_init(&b64_ctx);
b64_ctx.output_fd = ctx->output_fd;
b64_ctx.input_fd = pipe_fd[0];
ft_base64_encode(&b64_ctx);
}
static void des_encrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[0]);
ctx->output_fd = pipe_fd[1];
ft_des_encrypt(ctx);
}
void ft_des_encrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
des_encrypt(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
base64_encode(ctx, pipe_fd);
}

View file

@ -1,74 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_encryption_key_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:34:23 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:34:35 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdio.h>
#include "ft_des.h"
#include "libft.h"
static void get_salt
(
t_des_ctx *c,
char salt[FT_DES_BYTE_BLOCK_SIZE]
)
{
int fd;
if (c->raw_salt)
{
ft_bzero(salt, FT_DES_BYTE_BLOCK_SIZE);
if (ft_des_hex_to_byte(c->raw_salt, (t_byte1 *)salt,
FT_DES_BYTE_BLOCK_SIZE))
ft_des_print_error("wrong char in salt");
}
else
{
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
{
perror("des");
exit(1);
}
read(fd, salt, FT_DES_BYTE_BLOCK_SIZE);
close(fd);
}
}
void ft_des_encryption_key_routine
(
t_des_ctx *ctx
)
{
char salt[FT_DES_BYTE_BLOCK_SIZE];
char pass[128];
if (ctx->raw_iv)
ft_des_set_raw_iv(ctx);
if (ctx->raw_key)
ft_des_set_raw_key(ctx);
else
{
if (!ctx->raw_password)
{
ft_des_get_password(pass);
ctx->raw_password = pass;
}
get_salt(ctx, salt);
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
(char *)ctx->raw_password);
}
if (ctx->raw_password)
{
write(ctx->output_fd, "Salted__", 8);
write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE);
}
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_expansion_box.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:34:49 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:38:05 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_expansion_box
@ -19,14 +7,19 @@ void ft_des_expansion_box
)
{
static t_byte1 permutation_table[FT_DES_EXPANDED_HALF_BLOCK_SIZE] = {
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15,
16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28,
29, 28, 29, 30, 31, 32, 1,
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1,
};
int i;
i = 0;
while (i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
while(i< FT_DES_EXPANDED_HALF_BLOCK_SIZE)
{
expanded[i] = half[permutation_table[i] - 1];
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_feistel_function.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:38:27 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:38:53 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
static void merge_key
@ -19,10 +7,8 @@ static void merge_key
t_byte1 output[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
)
{
int i;
i = 0;
while (i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
int i = 0;
while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
{
output[i] = expanded_half[i] ^ key[i];
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_feistel_function_permutation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:39:02 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:40:48 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_feistel_function_permutation
@ -19,15 +7,18 @@ void ft_des_feistel_function_permutation
)
{
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE / 2] = {
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14,
32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25,
};
int i;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
output[i] = input[table[i] - 1];
i++;
}
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_final_permutation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:41:16 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:43:25 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_final_permutation
@ -19,15 +7,19 @@ void ft_des_final_permutation
)
{
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = {
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46,
14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52,
20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58,
26, 33, 1, 41, 9, 49, 17, 57, 25,
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25,
};
t_byte1 i;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE)
while(i < FT_DES_BIT_BLOCK_SIZE)
{
final_permutation[i] = before[table[i] - 1];
i++;

View file

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_finish_encrypt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:43:34 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:43:55 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
#include <unistd.h>
void ft_des_finish_encrypt
(
t_des_ctx *ctx
)
{
t_byte1 buffer_index;
t_byte1 padding_size;
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE;
padding_size = FT_DES_BYTE_BLOCK_SIZE - buffer_index;
while (buffer_index < FT_DES_BYTE_BLOCK_SIZE)
{
ctx->buffer[buffer_index] = padding_size;
buffer_index++;
}
ctx->encrypt(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_generate_decryption_round_keys.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:44:14 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:44:22 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_generate_decryption_round_keys
@ -23,7 +11,7 @@ void ft_des_generate_decryption_round_keys
ft_des_key_permuted_choice_one(input_key, reduced_key);
i = 1;
while (i <= FT_DES_ROUND_COUNT)
while(i <= FT_DES_ROUND_COUNT)
{
ft_des_derive_decryption_round_key(reduced_key, i, keys[i - 1]);
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_generate_encryption_round_keys.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:44:32 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:44:40 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_generate_encryption_round_keys
@ -23,7 +11,7 @@ void ft_des_generate_encryption_round_keys
ft_des_key_permuted_choice_one(key, reduced_key);
i = 1;
while (i <= FT_DES_ROUND_COUNT)
while(i <= FT_DES_ROUND_COUNT)
{
ft_des_derive_encryption_round_key(reduced_key, i, round_keys[i - 1]);
i++;

View file

@ -1,33 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_get_password.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:44:55 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:45:07 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
#include "libft.h"
void ft_des_get_password
(
char pass[128]
)
{
char first_try[128];
char second_try[128];
ft_bzero(first_try, 128);
ft_bzero(second_try, 128);
ft_strcpy(first_try, getpass("enter password:"));
if (!ft_strlen(first_try))
exit(1);
ft_strcpy(second_try, getpass("retype password:"));
if (ft_strcmp(first_try, second_try) != 0)
ft_des_print_error("passwords doesn't match");
ft_memcpy(pass, second_try, 128);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_hex_to_bit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:45:24 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:45:33 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
#include "libft.h"
@ -22,9 +10,8 @@ const char *ft_des_hex_to_bit
{
t_byte1 bits4;
t_byte8 i;
i = 0;
while (*hex && i < bit_len)
while(*hex && i < bit_len)
{
bits4 = ft_tolower(*hex);
if (bits4 >= '0' && bits4 <= '9')

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_hex_to_byte.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:45:44 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:45:51 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
#include "libft.h"
@ -41,9 +29,8 @@ const char *ft_des_hex_to_byte
t_byte1 first_byte_half;
t_byte1 second_byte_half;
t_byte8 i;
i = 0;
while (*hex && i < byte_len)
while(*hex && i < byte_len)
{
if (char_to_num(*hex, &first_byte_half))
return (hex);

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_init_ctx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:46:08 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:46:12 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
#include "libft.h"
@ -22,14 +10,15 @@ void ft_des_init_ctx
int i;
i = 0;
while (i < FT_DES_ROUND_COUNT)
while(i < FT_DES_ROUND_COUNT)
{
ft_bzero(ctx->round_keys[i], FT_DES_ROUND_KEY_SIZE);
i++;
}
ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE);
ft_bzero(ctx->key, FT_DES_BIT_BLOCK_SIZE);
ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE);
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_SIZE);
ft_bzero(ctx->salt, FT_DES_BIT_BLOCK_SIZE);
ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE);
ctx->readed = 0;
ctx->decode = 0;
ctx->b64 = 0;
@ -38,7 +27,4 @@ void ft_des_init_ctx
ctx->raw_password = NULL;
ctx->raw_salt = NULL;
ctx->raw_key = NULL;
ctx->raw_iv = NULL;
ctx->encrypt = NULL;
ctx->decrypt = NULL;
}

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

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_initial_permutation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:47:22 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:48:36 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_initial_permutation
@ -19,15 +7,19 @@ void ft_des_initial_permutation
)
{
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = {
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54,
46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33,
25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21,
13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
};
t_byte1 i;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE)
while(i < FT_DES_BIT_BLOCK_SIZE)
{
initial_permutation[i] = message[table[i] - 1];
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_key_permuted_choice_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:48:53 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:50:03 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_key_permuted_choice_one
@ -19,14 +7,18 @@ void ft_des_key_permuted_choice_one
)
{
static t_byte1 table[FT_DES_REDUCED_KEY_SIZE] = {
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43,
35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62,
54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4,
57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39,
31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4,
};
int i;
i = 0;
while (i < FT_DES_REDUCED_KEY_SIZE)
while(i < FT_DES_REDUCED_KEY_SIZE)
{
reduced_key[i] = initial_key[table[i] - 1];
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_key_permuted_choice_two.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:50:17 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:51:15 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_key_permuted_choice_two
@ -19,14 +7,17 @@ void ft_des_key_permuted_choice_two
)
{
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, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44,
49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32,
};
int i;
i = 0;
while (i < FT_DES_ROUND_KEY_SIZE)
while(i < FT_DES_ROUND_KEY_SIZE)
{
round_key[i] = input_key[table[i] - 1];
i++;

View file

@ -1,104 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_more_arg_parsers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:17:08 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:17:09 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "ft_des.h"
int ft_des_input_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
struct stat stat_buff;
if (position + 1 >= argc)
ft_des_print_error("there is no filaname after -i flag.");
if ((ctx->input_fd = open(argv[position + 1], O_RDONLY, 0)) == -1)
{
perror("des");
exit(1);
}
stat(argv[position + 1], &stat_buff);
if (S_ISDIR(stat_buff.st_mode))
ft_des_print_error("input path is not a file.");
return (position + 2);
}
int ft_des_output_file_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no filaname after -o flag.");
if ((ctx->output_fd = open(
argv[position + 1], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1)
{
perror("des");
exit(1);
}
return (position + 2);
}
int ft_des_password_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
ctx->raw_password = argv[position + 1];
return (position + 2);
}
int ft_des_salt_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
if (position + 1 >= argc)
ft_des_print_error("there is no password after -p flag.");
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);
}

View file

@ -1,89 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_parse_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:52:24 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:53:19 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "ft_des.h"
#include "libft.h"
t_des_argument_parser g_arg_parsers[] = {
{
"-d",
ft_des_decode_arg_parser,
},
{
"-e",
ft_des_encode_arg_parser,
},
{
"-a",
ft_des_base64_arg_parser,
},
{
"-i",
ft_des_input_file_arg_parser,
},
{
"-o",
ft_des_output_file_arg_parser,
},
{
"-k",
ft_des_key_arg_parser,
},
{
"-v",
ft_des_iv_arg_parser,
},
{
"-p",
ft_des_password_arg_parser,
},
{
"-s",
ft_des_salt_arg_parser,
},
{
"-h",
ft_des_help_arg_parser,
},
{ NULL, NULL},
};
void ft_des_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_usage();
}
}

View file

@ -1,87 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_pcbc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:53:37 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:54:07 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#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);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_print_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:54:22 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:54:25 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_des.h"
#include "libft.h"

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_process_block.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:54:39 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:55:29 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
static void switch_halves
@ -19,9 +7,8 @@ static void switch_halves
{
int i;
int tmp;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
tmp = message[i];
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
@ -41,7 +28,7 @@ static void bytes_to_bits
i = 0;
j = 0;
while (i < FT_DES_BYTE_BLOCK_SIZE)
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
bits[j] = bytes[i] >> 7 & 1;
bits[j + 1] = bytes[i] >> 6 & 1;
@ -67,7 +54,7 @@ static void bits_to_bytes
i = 0;
j = 0;
while (i < FT_DES_BYTE_BLOCK_SIZE)
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
bytes[i] = bits[j] << 7
| bits[j + 1] << 6
@ -97,7 +84,7 @@ void ft_des_process_block
bytes_to_bits(input, input_bits);
ft_des_initial_permutation(input_bits, ip_message);
i = 1;
while (i <= FT_DES_ROUND_COUNT)
while(i <= FT_DES_ROUND_COUNT)
{
if (i % 2 != 0)
ft_des_round(ip_message, ip_message + 32, keys[i - 1]);

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_rotate_half_key_left.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:56:02 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:56:11 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_rotate_half_key_left
@ -23,11 +11,11 @@ void ft_des_rotate_half_key_left
t_byte1 tmp;
i = 0;
while (i < rotation_number)
while(i < rotation_number)
{
tmp = half[0];
j = 0;
while (j < FT_DES_REDUCED_KEY_SIZE / 2 - 1)
while(j < FT_DES_REDUCED_KEY_SIZE / 2 - 1)
{
half[j] = half[j + 1];
j++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_rotate_half_key_right.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:56:19 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:56:25 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_rotate_half_key_right
@ -23,11 +11,11 @@ void ft_des_rotate_half_key_right
t_byte1 tmp;
i = 0;
while (i < rotation_number)
while(i < rotation_number)
{
tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1];
j = FT_DES_REDUCED_KEY_SIZE / 2 - 1;
while (j > 0)
while(j > 0)
{
half[j] = half[j - 1];
j--;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_round.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:13:31 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:13:43 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
static void xor
@ -21,7 +9,7 @@ static void xor
int i;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
left_half[i] = left_half[i] ^ f_function_result[i];
i++;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:56:47 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:56:51 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:57:05 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:59:02 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_1
@ -24,6 +12,5 @@ void ft_des_s_box_1
{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:59:10 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:00:36 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_2
@ -19,11 +7,10 @@ void ft_des_s_box_2
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
{ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
{13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:03:07 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:04:25 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_3
@ -19,11 +7,10 @@ void ft_des_s_box_3
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
{ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
{ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
{13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
{13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_4.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:05:47 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:05:48 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_4
@ -19,11 +7,10 @@ void ft_des_s_box_4
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
{ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
{ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
{13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
{10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_5.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:05:56 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:07:19 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_5
@ -19,11 +7,10 @@ void ft_des_s_box_5
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
{ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
{ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
{14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
{11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_6.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:07:23 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:08:34 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_6
@ -19,11 +7,10 @@ void ft_des_s_box_6
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
{ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
{10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_7.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:08:42 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:10:08 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_7
@ -19,11 +7,10 @@ void ft_des_s_box_7
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
{ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
{13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_s_box_8.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:10:15 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:11:30 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_s_box_8
@ -19,11 +7,10 @@ void ft_des_s_box_8
)
{
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
{ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 },
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 03, 5, 8},
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
};
ft_des_s_box(input, output, tbl);
}

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_set_raw_iv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:12:17 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:12:19 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_set_raw_iv
(
t_des_ctx *ctx
)
{
const char *wrong_char;
wrong_char = ft_des_hex_to_bit(ctx->raw_iv, ctx->iv,
FT_DES_BIT_BLOCK_SIZE);
if (wrong_char)
ft_des_print_error("wrong char in hex iinitialization vector");
}

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_set_raw_key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:12:01 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:12:06 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_set_raw_key
(
t_des_ctx *ctx
)
{
const char *wrong_char;
wrong_char = ft_des_hex_to_bit(ctx->raw_key, ctx->key,
FT_DES_BIT_BLOCK_SIZE);
if (wrong_char)
ft_des_print_error("wrong char in hex key");
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_switch_message_halves.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:12:41 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:12:47 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_des.h"
void ft_des_switch_message_halves
@ -19,9 +7,9 @@ void ft_des_switch_message_halves
{
int i;
int tmp;
i = 0;
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
tmp = message[i];
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];

View file

@ -1,36 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_des_usage.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 17:13:04 by gtertysh #+# #+# */
/* Updated: 2019/04/10 17:13:05 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_des_usage
(
void
)
{
ft_putstr("des cipher usage:\n\n");
ft_putstr("ft_ssl des|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\n");
ft_putstr("des cipher is an alias for des-cbc.\n");
exit(1);
}

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\ndes\ndes-ecb\ndes-cbc\ndes-pcbc\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

@ -34,22 +34,10 @@ t_algorithm g_algorithms[] = {
"base64",
ft_base64,
},
{
"des",
ft_des_cbc,
},
{
"des-ecb",
ft_des_ecb,
},
{
"des-cbc",
ft_des_cbc,
},
{
"des-pcbc",
ft_des_pcbc,
},
{NULL, NULL}
};

View file

@ -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,8 +35,6 @@ 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)

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hmac_sha256.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:14:22 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:15:06 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pbkdf2.h"
#include "libft.h"
#include "ft_sha.h"
@ -23,7 +11,7 @@ static void set_padding
int i;
i = 0;
while (i < FT_SHA256_BLOCK_SIZE)
while(i < FT_SHA256_BLOCK_SIZE)
{
ctx->ipad[i] = key[i] ^ 0x36;
ctx->opad[i] = key[i] ^ 0x5c;

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hmac_sha256_init_ctx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:15:12 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:15:33 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "ft_pbkdf2.h"
#include "ft_sha.h"

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pbkdf2_sha256.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:15:38 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:17:16 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pbkdf2.h"
#include "ft_sha.h"
#include "libft.h"
@ -36,7 +24,7 @@ static void xor
unsigned int i;
i = 0;
while (i < FT_SHA256_DIGEST_LENGTH_BYTE)
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
{
result[i] = x[i] ^ y[i];
i++;
@ -49,7 +37,7 @@ static int ceil
unsigned int y
)
{
return (1 + ((x - 1) / y));
return 1 + ((x - 1) / y);
}
static void block_function
@ -59,6 +47,7 @@ static void block_function
unsigned int iteration,
unsigned char block[FT_SHA256_DIGEST_LENGTH_BYTE]
)
{
unsigned int i;
unsigned char c[4];
@ -100,7 +89,7 @@ void ft_pbkdf2_sha256
block_count = ceil(c->key_len, FT_SHA256_DIGEST_LENGTH_BYTE);
i = 1;
key_index = 0;
while (i < block_count)
while(i < block_count)
{
block_function(c, &hmac_ctx, i, block);
ft_memcpy(c->key + key_index, block, FT_SHA256_DIGEST_LENGTH_BYTE);
@ -108,6 +97,6 @@ void ft_pbkdf2_sha256
i++;
}
block_function(c, &hmac_ctx, i, block);
ft_memcpy(c->key + key_index, block,
ft_memcpy(c->key + key_index ,block,
c->key_len - (block_count - 1) * FT_SHA256_DIGEST_LENGTH_BYTE);
}

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pbkdf2_sha256_init_ctx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/10 16:17:30 by gtertysh #+# #+# */
/* Updated: 2019/04/10 16:17:37 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "ft_pbkdf2.h"

View file

@ -28,8 +28,6 @@ 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)
@ -59,7 +57,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++;
}

View file

@ -28,8 +28,6 @@ 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)
@ -59,7 +57,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++;
}

View file

@ -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");

View file

@ -12,12 +12,12 @@ abcdefghijklmnopqrstuvwxyz0123456789+/";
ft_base64_init(&ctx);
_IS(ctx.input_fd == STDIN_FILENO);
_IS(ctx.output_fd == STDOUT_FILENO);
_is(ctx.input_fd == STDIN_FILENO);
_is(ctx.output_fd == STDOUT_FILENO);
_IS(ft_strcmp(alphabet, (char *)ctx.alphabet) == 0);
_is(ft_strcmp(alphabet, (char *)ctx.alphabet) == 0);
_END("init ctx");
_end("init ctx");
}
static int transform_block()
@ -28,17 +28,17 @@ static int transform_block()
ft_base64_init(&ctx);
ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff);
_IS(ft_strncmp((char *)buff, "TWFu", 4) == 0);
_is(ft_strncmp((char *)buff, "TWFu", 4) == 0);
ft_base64_encode_transform(&ctx, (t_byte1 *)"LOL", buff);
_IS(ft_strncmp((char *)buff, "TE9M", 4) == 0);
_is(ft_strncmp((char *)buff, "TE9M", 4) == 0);
_END("transform block");
_end("transform block");
}
int base64_tests(void)
{
_SHOULD(init_ctx);
_SHOULD(transform_block);
_should(init_ctx);
_should(transform_block);
return 0;
}

View file

@ -1 +0,0 @@
U2FsdGVkX1/+2bai0hAYzG6psw8usaQW

View file

@ -1 +0,0 @@
one deep secret

View file

@ -1 +0,0 @@
Salted__oñîa³(_@Ž  Á=_€<ý·©<C2B7>üBúÝr^q

View file

@ -44,10 +44,10 @@ int perform_initial_permutation()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(initial_permutation[i] == expect[i]);
_is(initial_permutation[i] == expect[i]);
i++;
}
_END("perform initial permutation");
_end("perform initial permutation");
}
int perform_final_permutation()
@ -89,13 +89,13 @@ int perform_final_permutation()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(final_permutation[i] == expect[i]);
_is(final_permutation[i] == expect[i]);
i++;
}
_END("perform final permutation");
_end("perform final permutation");
}
int final_permutation_IS_reverse_of_initial()
int final_permutation_is_reverse_of_initial()
{
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
1, 0, 1, 0, 1, 0, 1, 0,
@ -119,10 +119,10 @@ int final_permutation_IS_reverse_of_initial()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(final_permutation[i] == message[i]);
_is(final_permutation[i] == message[i]);
i++;
}
_END("final permutation is reverse of initial");
_end("final permutation is reverse of initial");
}
int perform_expansion_in_feistel_function()
@ -156,10 +156,10 @@ int perform_expansion_in_feistel_function()
int i = 0;
while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
{
_IS(expanded_half_actual[i] == expanded_half_expected[i]);
_is(expanded_half_actual[i] == expanded_half_expected[i]);
i++;
}
_END("perform expansion in feistel function");
_end("perform expansion in feistel function");
}
static int s_box_check
@ -201,7 +201,7 @@ int s_boxes_confuse()
{0, 1, 1, 1},
};
_IS(s_box_check(s1_inputs, s1_expected, ft_des_s_box_1));
_is(s_box_check(s1_inputs, s1_expected, ft_des_s_box_1));
t_byte1 s2_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -215,7 +215,7 @@ int s_boxes_confuse()
{0, 1, 0, 1},
};
_IS(s_box_check(s2_inputs, s2_expected, ft_des_s_box_2));
_is(s_box_check(s2_inputs, s2_expected, ft_des_s_box_2));
t_byte1 s3_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -229,7 +229,7 @@ int s_boxes_confuse()
{1, 0, 1, 1},
};
_IS(s_box_check(s3_inputs, s3_expected, ft_des_s_box_3));
_is(s_box_check(s3_inputs, s3_expected, ft_des_s_box_3));
t_byte1 s4_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -243,7 +243,7 @@ int s_boxes_confuse()
{1, 1, 1, 1},
};
_IS(s_box_check(s4_inputs, s4_expected, ft_des_s_box_4));
_is(s_box_check(s4_inputs, s4_expected, ft_des_s_box_4));
t_byte1 s5_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -257,7 +257,7 @@ int s_boxes_confuse()
{0, 0, 1, 1},
};
_IS(s_box_check(s5_inputs, s5_expected, ft_des_s_box_5));
_is(s_box_check(s5_inputs, s5_expected, ft_des_s_box_5));
t_byte1 s6_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -271,7 +271,7 @@ int s_boxes_confuse()
{1, 1, 0, 1},
};
_IS(s_box_check(s6_inputs, s6_expected, ft_des_s_box_6));
_is(s_box_check(s6_inputs, s6_expected, ft_des_s_box_6));
t_byte1 s7_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -285,7 +285,7 @@ int s_boxes_confuse()
{1, 1, 0, 0},
};
_IS(s_box_check(s7_inputs, s7_expected, ft_des_s_box_7));
_is(s_box_check(s7_inputs, s7_expected, ft_des_s_box_7));
t_byte1 s8_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
{0, 0, 0, 0, 0, 0},
@ -299,9 +299,9 @@ int s_boxes_confuse()
{1, 0, 1, 1},
};
_IS(s_box_check(s8_inputs, s8_expected, ft_des_s_box_8));
_is(s_box_check(s8_inputs, s8_expected, ft_des_s_box_8));
_END("s boxes confuse");
_end("s boxes confuse");
}
int perform_premutation_in_feistel_function()
@ -327,10 +327,10 @@ int perform_premutation_in_feistel_function()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
_IS(actual[i] == expected[i]);
_is(actual[i] == expected[i]);
i++;
}
_END("should perform permutation in feistel function");
_end("should perform permutation in feistel function");
}
int perform_feistel_function()
@ -373,10 +373,10 @@ int perform_feistel_function()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
{
_IS(output[i] == expected[i]);
_is(output[i] == expected[i]);
i++;
}
_END("feistel function should encode half of a block");
_end("feistel function should encode half of a block");
}
int reduce_key_to_56_bits()
@ -409,11 +409,11 @@ int reduce_key_to_56_bits()
int i = 0;
while(i < FT_DES_REDUCED_KEY_SIZE)
{
_IS(actual_reduced[i] == expected_reduced_key[i]);
_is(actual_reduced[i] == expected_reduced_key[i]);
i++;
}
_END("should reduce key size to 56 bits");
_end("should reduce key size to 56 bits");
}
int rotate_half_key()
@ -442,10 +442,10 @@ int rotate_half_key()
while(i < FT_DES_REDUCED_KEY_SIZE / 2)
{
_IS(half_key[i] == expected[i]);
_is(half_key[i] == expected[i]);
i++;
}
_END("should rotate half of reduced key");
_end("should rotate half of reduced key");
}
int derive_round_key()
@ -492,17 +492,17 @@ int derive_round_key()
i = 0;
while(i < FT_DES_REDUCED_KEY_SIZE)
{
_IS(shifted_reduced_key[i] == reduced_key[i]);
_is(shifted_reduced_key[i] == reduced_key[i]);
i++;
}
i = 0;
while(i < FT_DES_ROUND_KEY_SIZE)
{
_IS(round_key[i] == expected_round_key[i]);
_is(round_key[i] == expected_round_key[i]);
i++;
}
_END("should derive round key");
_end("should derive round key");
}
int perform_encryption_round()
@ -550,10 +550,10 @@ int perform_encryption_round()
int i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(initial_permuatation[i] == expected_message[i]);
_is(initial_permuatation[i] == expected_message[i]);
i++;
}
_END("should perform encryption round");
_end("should perform encryption round");
}
int encrypt_block()
@ -583,10 +583,10 @@ int encrypt_block()
int i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(cyphertext[i] == expected_cypertext[i]);
_is(cyphertext[i] == expected_cypertext[i]);
i++;
}
_END("should encrypt block");
_end("should encrypt block");
}
int decrypt_block()
@ -616,10 +616,10 @@ int decrypt_block()
int i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(plaintext[i] == expected_plaintext[i]);
_is(plaintext[i] == expected_plaintext[i]);
i++;
}
_END("should decrypt block");
_end("should decrypt block");
}
int init_ctx()
@ -630,9 +630,9 @@ int init_ctx()
ft_des_init_ctx(&ctx);
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_IS(ctx.key[i] == 0);
_is(ctx.key[i] == 0);
i++;
}
@ -642,7 +642,7 @@ int init_ctx()
j = 0;
while(j < FT_DES_ROUND_KEY_SIZE)
{
_IS(ctx.round_keys[i][j] == 0);
_is(ctx.round_keys[i][j] == 0);
j++;
}
i++;
@ -650,27 +650,30 @@ int init_ctx()
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(ctx.buffer[i] == 0);
_is(ctx.buffer[i] == 0);
i++;
}
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(ctx.iv[i] == 0);
_is(ctx.salt[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.b64 == 0);
_IS(ctx.raw_password == NULL);
_IS(ctx.raw_key == NULL);
_IS(ctx.raw_salt == NULL);
_IS(ctx.raw_iv == NULL);
_IS(ctx.encrypt == NULL);
_IS(ctx.decrypt == NULL);
_END("shoud init ctx");
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_is(ctx.iv[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.b64 == 0);
_is(ctx.raw_password == NULL);
_is(ctx.raw_key == NULL);
_is(ctx.raw_salt == NULL);
_end("shoud init ctx");
}
int convert_hex_string_to_bits()
@ -691,26 +694,26 @@ int convert_hex_string_to_bits()
wrong_key_char = ft_des_hex_to_bit("FFFFFFFFFFFFFFFF", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_IS(wrong_key_char == NULL);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_IS(expected_key[i] == actual_key[i]);
_is(expected_key[i] == actual_key[i]);
i++;
}
wrong_key_char = ft_des_hex_to_bit("ffffffffffffffff", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_IS(wrong_key_char == NULL);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_IS(expected_key[i] == actual_key[i]);
_is(expected_key[i] == actual_key[i]);
i++;
}
_END("should convert hex string to 64 bit key");
_end("should convert hex string to 64 bit key");
}
int convert_short_hex_string_to_bits()
@ -731,15 +734,15 @@ int convert_short_hex_string_to_bits()
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit("FF12CD", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_IS(wrong_key_char == NULL);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_IS(expected_key[i] == actual_key[i]);
_is(expected_key[i] == actual_key[i]);
i++;
}
_END("should convert shorter hex string to 64 bit key");
_end("should convert shorter hex string to 64 bit key");
}
int convert_longer_hex_string_to_bits()
@ -761,15 +764,15 @@ int convert_longer_hex_string_to_bits()
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
wrong_key_char = ft_des_hex_to_bit("FF12CDFF12CDFF12CD", actual_key,
FT_DES_INITIAL_KEY_SIZE);
_IS(wrong_key_char == NULL);
_is(wrong_key_char == NULL);
i = 0;
while(i < FT_DES_INITIAL_KEY_SIZE)
{
_IS(expected_key[i] == actual_key[i]);
_is(expected_key[i] == actual_key[i]);
i++;
}
_END("should convert longer hex string to 64 bit key");
_end("should convert longer hex string to 64 bit key");
}
int convert_hex_string_to_bytes()
@ -784,15 +787,15 @@ int convert_hex_string_to_bytes()
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
wrong_char = ft_des_hex_to_byte("FFFFFFFFFFFFFFFF", actual,
FT_DES_BYTE_BLOCK_SIZE);
_IS(wrong_char == NULL);
_is(wrong_char == NULL);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(expected[i] == actual[i]);
_is(expected[i] == actual[i]);
i++;
}
_END("should convert hex to 8 byte");
_end("should convert hex to 8 byte");
}
int convert_short_hex_string_to_bytes()
@ -807,15 +810,15 @@ int convert_short_hex_string_to_bytes()
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
wrong_char = ft_des_hex_to_byte("CC565", actual,
FT_DES_BYTE_BLOCK_SIZE);
_IS(wrong_char == NULL);
_is(wrong_char == NULL);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(expected[i] == actual[i]);
_is(expected[i] == actual[i]);
i++;
}
_END("should convert short hex string to 8 bytes");
_end("should convert short hex string to 8 bytes");
}
int convert_bytes_to_bits()
@ -844,86 +847,33 @@ int convert_bytes_to_bits()
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE)
{
_IS(expected[i] == actual[i]);
_is(expected[i] == actual[i]);
i++;
}
_END("should convert 8 bytes to 64 bits");
}
int convert_bits_to_bytes()
{
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
};
t_byte1 expected[FT_DES_BYTE_BLOCK_SIZE] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
t_byte1 actual[FT_DES_BIT_BLOCK_SIZE];
int i;
ft_bzero(actual, FT_DES_BIT_BLOCK_SIZE);
ft_des_bits_to_bytes(bits, FT_DES_BIT_BLOCK_SIZE, actual,
FT_DES_BYTE_BLOCK_SIZE);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(expected[i] == actual[i]);
i++;
}
t_byte1 bits1[FT_DES_BIT_BLOCK_SIZE] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 0, 0, 1, 1,
0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 1, 0, 1, 1, 1,
};
t_byte1 expected1[FT_DES_BYTE_BLOCK_SIZE] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
};
ft_des_bits_to_bytes(bits1, FT_DES_BIT_BLOCK_SIZE, actual,
FT_DES_BYTE_BLOCK_SIZE);
i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE)
{
_IS(expected1[i] == actual[i]);
i++;
}
_END("should convert 64 bits to 8 bytes");
_end("should convert 8 bytes to 64 bits");
}
int des_tests()
{
_SHOULD(perform_initial_permutation);
_SHOULD(perform_final_permutation);
_SHOULD(final_permutation_IS_reverse_of_initial);
_SHOULD(perform_expansion_in_feistel_function);
_SHOULD(s_boxes_confuse);
_SHOULD(perform_premutation_in_feistel_function);
_SHOULD(perform_feistel_function);
_SHOULD(reduce_key_to_56_bits);
_SHOULD(rotate_half_key);
_SHOULD(derive_round_key);
_SHOULD(perform_encryption_round);
_SHOULD(encrypt_block);
_SHOULD(decrypt_block);
_SHOULD(init_ctx);
_SHOULD(convert_hex_string_to_bits);
_SHOULD(convert_short_hex_string_to_bits);
_SHOULD(convert_longer_hex_string_to_bits);
_SHOULD(convert_hex_string_to_bytes);
_SHOULD(convert_short_hex_string_to_bytes);
_SHOULD(convert_bytes_to_bits);
_SHOULD(convert_bits_to_bytes);
_should(perform_initial_permutation);
_should(perform_final_permutation);
_should(final_permutation_is_reverse_of_initial);
_should(perform_expansion_in_feistel_function);
_should(s_boxes_confuse);
_should(perform_premutation_in_feistel_function);
_should(perform_feistel_function);
_should(reduce_key_to_56_bits);
_should(rotate_half_key);
_should(derive_round_key);
_should(perform_encryption_round);
_should(encrypt_block);
_should(decrypt_block);
_should(init_ctx);
_should(convert_hex_string_to_bits);
_should(convert_short_hex_string_to_bits);
_should(convert_longer_hex_string_to_bits);
_should(convert_hex_string_to_bytes);
_should(convert_short_hex_string_to_bytes);
_should(convert_bytes_to_bits);
return 0;
}

View file

@ -20,15 +20,15 @@ static int init_ctx()
ft_md5_init(&ctx);
_IS(ctx.a == 0x67452301);
_IS(ctx.b == 0xefcdab89);
_IS(ctx.c == 0x98badcfe);
_IS(ctx.d == 0x10325476);
_IS(ctx.bit_len == 0);
_is(ctx.a == 0x67452301);
_is(ctx.b == 0xefcdab89);
_is(ctx.c == 0x98badcfe);
_is(ctx.d == 0x10325476);
_is(ctx.bit_len == 0);
for (int i = 0; i < FT_MD5_BLOCK_SIZE; i++)
_IS(ctx.block[i] == 0);
_is(ctx.block[i] == 0);
_END("init ctx");
_end("init ctx");
}
static int decode_string_to_int()
@ -43,11 +43,11 @@ static int decode_string_to_int()
ft_md5_decode(words, block);
_IS((words[0] & 0xff) == 97);
_IS(((words[2] >> 8) & 0xff) == 98);
_IS(((words[15] >> 24) & 0xff) == 99);
_is((words[0] & 0xff) == 97);
_is(((words[2] >> 8) & 0xff) == 98);
_is(((words[15] >> 24) & 0xff) == 99);
_END("decode string to int");
_end("decode string to int");
}
static int update_change_count()
@ -59,9 +59,9 @@ static int update_change_count()
ft_md5_init(&ctx);
ft_md5_update(&ctx, (t_byte1 *)message, size);
_IS(size * 8 == ctx.bit_len);
_is(size * 8 == ctx.bit_len);
_END("update change count");
_end("update change count");
}
static int encode_bits_to_string()
@ -83,11 +83,11 @@ static int encode_bits_to_string()
ft_md5_encode_len(bits, len);
_IS(bits[7] == (t_byte1)((len >> 56) & 0xff));
_IS(bits[0] == (t_byte1)(len & 0xff));
_IS(bits[1] == (t_byte1)((len >> 8) & 0xff));
_is(bits[7] == (t_byte1)((len >> 56) & 0xff));
_is(bits[0] == (t_byte1)(len & 0xff));
_is(bits[1] == (t_byte1)((len >> 8) & 0xff));
_END("encode bits to string");
_end("encode bits to string");
}
static int encode_register()
@ -105,12 +105,12 @@ static int encode_register()
ft_md5_encode_register(digest_part, reg);
_IS(digest_part[0] == (t_byte1)(reg & 0xff));
_IS(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
_IS(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
_IS(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
_is(digest_part[0] == (t_byte1)(reg & 0xff));
_is(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
_is(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
_is(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
_END("encode register");
_end("encode register");
}
static int create_digest()
@ -129,7 +129,7 @@ static int create_digest()
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)digest_string,
_is(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
ft_md5_init(&ctx);
@ -137,7 +137,7 @@ static int create_digest()
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)digest_string,
_is(ft_strcmp((const char *)digest_string,
"0cc175b9c0f1b6a831c399e269772661") == 0);
ft_md5_init(&ctx);
@ -145,10 +145,10 @@ static int create_digest()
ft_md5_final(digest, &ctx);
ft_md5_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)digest_string,
_is(ft_strcmp((const char *)digest_string,
"2580a0aff7ef5e80f6b5432666530926") == 0);
_END("create digest");
_end("create digest");
}
static int create_digest_string()
@ -173,20 +173,20 @@ static int create_digest_string()
ft_md5_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)digest_string,
_is(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
_END("create digest string");
_end("create digest string");
}
int md5_tests()
{
_SHOULD(init_ctx);
_SHOULD(decode_string_to_int);
_SHOULD(update_change_count);
_SHOULD(encode_bits_to_string);
_SHOULD(encode_register);
_SHOULD(create_digest);
_SHOULD(create_digest_string);
_should(init_ctx);
_should(decode_string_to_int);
_should(update_change_count);
_should(encode_bits_to_string);
_should(encode_register);
_should(create_digest);
_should(create_digest_string);
return 0;
}

View file

@ -1,37 +0,0 @@
// ENCRYPTING
"openssl des-ecb"
prompts to enter password, derive key with generated salt and pass,
prepend salt header.
"openssl des-ecb -S a"
prompts to enter password, derive key with given salt and generated pass,
prepend salt header.
"openssl des-ecb -pass pass:asd"
generate salt and key, prepend salt header.
"openssl des-ecb -S a -pass:asd"
generate key from givent salt and pass, prepend header.
"openssl des-ecb -S a -pass:asd -K 1"
encrypt with GIVEN key, prepend GIVEN salt headaer.
"openssl des-ecb -K 1"
encrypt with given key, doesn't generate and prepend salt.
"openssl des-ecb -S 0 -pass pass:asd -P -pbkdf2"
generate key using PKCS5_PBKDF2_HMAC with 10000 iteration,
8 byte salt, 3 byte key, sha256 hash function, prepend salt header.
// DECRYPTING
"echo -n Salted__ | openssl des-ecb -d"
prompts to enter password, generate key but fails to decode.
"echo -n Salted__aaaaaaaa | openssl des-ecb -d"
prompts to enter password, generate key but fails to validate padding.
openssl doesn't expect "Salted__" header only when -K or -S flag supplied
(or both simultaneously, in this case salt is discarded),
in any other case salt should be readed from message.

View file

@ -3,25 +3,24 @@
#include "ft_pbkdf2.h"
#include "ft_sha.h"
#include "libft.h"
#include "openssl/evp.h"
static int init_hmac_sha256_ctx()
{
t_hmac_sha256_ctx ctx;
ft_hmac_sha256_init_ctx(&ctx);
_IS(ctx.key == NULL);
_IS(ctx.msg == NULL);
_IS(ctx.key_size == 0);
_IS(ctx.msg_size == 0);
_is(ctx.key == NULL);
_is(ctx.msg == NULL);
_is(ctx.key_size == 0);
_is(ctx.msg_size == 0);
int i = 0;
while(i < FT_SHA256_BLOCK_SIZE)
{
_IS(ctx.opad[i] == 0);
_IS(ctx.ipad[i] == 0);
_is(ctx.opad[i] == 0);
_is(ctx.ipad[i] == 0);
i++;
}
_END("init hmac sha256 ctx");
_end("init hmac sha256 ctx");
}
static int perform_hmac_256_computation_short_key()
@ -53,10 +52,10 @@ static int perform_hmac_256_computation_short_key()
int i = 0;
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
{
_IS(out[i] == expected_result[i]);
_is(out[i] == expected_result[i]);
i++;
}
_END("perform hamc sha256 computation with short key");
_end("perform hamc sha256 computation with short key");
}
static int perform_hmac_256_computation_long_key()
@ -120,10 +119,10 @@ static int perform_hmac_256_computation_long_key()
int i = 0;
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
{
_IS(out[i] == expected_result[i]);
_is(out[i] == expected_result[i]);
i++;
}
_END("perform hamc sha256 computation with long key");
_end("perform hamc sha256 computation with long key");
}
int init_pbkdf2_sha256_ctx()
@ -132,15 +131,15 @@ int init_pbkdf2_sha256_ctx()
ft_pbkdf2_sha256_init_ctx(&ctx);
_IS(ctx.iterations == 0);
_IS(ctx.key_len == 0);
_IS(ctx.salt_len == 0);
_IS(ctx.pass_len == 0);
_IS(ctx.salt == NULL);
_IS(ctx.key == NULL);
_IS(ctx.pass == NULL);
_is(ctx.iterations == 0);
_is(ctx.key_len == 0);
_is(ctx.salt_len == 0);
_is(ctx.pass_len == 0);
_is(ctx.salt == NULL);
_is(ctx.key == NULL);
_is(ctx.pass == NULL);
_END("init pbkdf2 sha256 ctx");
_end("init pbkdf2 sha256 ctx");
}
int perform_pbkdf2_sha256()
@ -171,7 +170,7 @@ int perform_pbkdf2_sha256()
i = 0;
while(i < 32)
{
_IS(ctx.key[i] == expected1[i]);
_is(ctx.key[i] == expected1[i]);
i++;
}
@ -195,7 +194,7 @@ int perform_pbkdf2_sha256()
i = 0;
while(i < 32)
{
_IS(ctx.key[i] == expected2[i]);
_is(ctx.key[i] == expected2[i]);
i++;
}
@ -219,7 +218,7 @@ int perform_pbkdf2_sha256()
i = 0;
while(i < 32)
{
_IS(ctx.key[i] == expected3[i]);
_is(ctx.key[i] == expected3[i]);
i++;
}
@ -247,62 +246,18 @@ int perform_pbkdf2_sha256()
i = 0;
while(i < 40)
{
_IS(ctx.key[i] == expected4[i]);
_is(ctx.key[i] == expected4[i]);
i++;
}
_END("perform pbkdf2 sha256");
}
int copy_openssl_pbkdf2()
{
t_pbkdf2_sha256_ctx ctx;
int i;
int iterations = 10000;
int salt_len = 8;
int pass_len = 8;
int key_len = 8;
unsigned char my_key[8];
unsigned char orig_key[8];
unsigned char pass[] = "password";
unsigned char salt[] = {0, 0, 0, 0, 0, 0, 0, 0};
ft_pbkdf2_sha256_init_ctx(&ctx);
ctx.key = my_key;
ctx.pass = pass;
ctx.salt = salt;
ctx.pass_len = pass_len;
ctx.salt_len = salt_len;
ctx.key_len = key_len;
ctx.iterations = iterations;
ft_pbkdf2_sha256(&ctx);
PKCS5_PBKDF2_HMAC(
(const char *)pass,
pass_len,
salt,
salt_len,
iterations,
EVP_sha256(),
key_len,
orig_key
);
i = 0;
while(i < 8)
{
_IS(my_key[i] == orig_key[i]);
i++;
}
_END("copy openssl pbkdf2");
_end("perform pbkdf2 sha256");
}
int pbkdf2_tests()
{
_SHOULD(init_hmac_sha256_ctx);
_SHOULD(perform_hmac_256_computation_short_key);
_SHOULD(perform_hmac_256_computation_long_key);
_SHOULD(init_pbkdf2_sha256_ctx);
_SHOULD(perform_pbkdf2_sha256);
_SHOULD(copy_openssl_pbkdf2);
_should(init_hmac_sha256_ctx);
_should(perform_hmac_256_computation_short_key);
_should(perform_hmac_256_computation_long_key);
_should(init_pbkdf2_sha256_ctx);
_should(perform_pbkdf2_sha256);
return 0;
}

View file

@ -20,19 +20,19 @@ static int init_sha256_ctx()
ft_sha256_init(&ctx);
_IS(ctx.a == 0x6a09e667);
_IS(ctx.b == 0xbb67ae85);
_IS(ctx.c == 0x3c6ef372);
_IS(ctx.d == 0xa54ff53a);
_IS(ctx.e == 0x510e527f);
_IS(ctx.f == 0x9b05688c);
_IS(ctx.g == 0x1f83d9ab);
_IS(ctx.h == 0x5be0cd19);
_IS(ctx.bit_len == 0);
_is(ctx.a == 0x6a09e667);
_is(ctx.b == 0xbb67ae85);
_is(ctx.c == 0x3c6ef372);
_is(ctx.d == 0xa54ff53a);
_is(ctx.e == 0x510e527f);
_is(ctx.f == 0x9b05688c);
_is(ctx.g == 0x1f83d9ab);
_is(ctx.h == 0x5be0cd19);
_is(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
_IS(ctx.block[i] == 0);
_is(ctx.block[i] == 0);
_END("init sha256 ctx");
_end("init sha256 ctx");
}
static int init_sha224_ctx()
@ -41,19 +41,19 @@ static int init_sha224_ctx()
ft_sha224_init(&ctx);
_IS(ctx.a == 0xc1059ed8);
_IS(ctx.b == 0x367cd507);
_IS(ctx.c == 0x3070dd17);
_IS(ctx.d == 0xf70e5939);
_IS(ctx.e == 0xffc00b31);
_IS(ctx.f == 0x68581511);
_IS(ctx.g == 0x64f98fa7);
_IS(ctx.h == 0xbefa4fa4);
_IS(ctx.bit_len == 0);
_is(ctx.a == 0xc1059ed8);
_is(ctx.b == 0x367cd507);
_is(ctx.c == 0x3070dd17);
_is(ctx.d == 0xf70e5939);
_is(ctx.e == 0xffc00b31);
_is(ctx.f == 0x68581511);
_is(ctx.g == 0x64f98fa7);
_is(ctx.h == 0xbefa4fa4);
_is(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
_IS(ctx.block[i] == 0);
_is(ctx.block[i] == 0);
_END("init sha224 ctx");
_end("init sha224 ctx");
}
static int decode_string_to_int_sha256()
@ -68,9 +68,9 @@ static int decode_string_to_int_sha256()
ft_sha256_decode(words, block);
_IS(words[0] == 0x61626300);
_is(words[0] == 0x61626300);
_END("decode string to int sha256");
_end("decode string to int sha256");
}
static int encode_len_to_string_sha256()
@ -92,11 +92,11 @@ static int encode_len_to_string_sha256()
ft_sha256_encode_len(bits, len);
_IS(bits[0] == (t_byte1)((len >> 56) & 0xff));
_IS(bits[7] == (t_byte1)(len & 0xff));
_IS(bits[6] == (t_byte1)((len >> 8) & 0xff));
_is(bits[0] == (t_byte1)((len >> 56) & 0xff));
_is(bits[7] == (t_byte1)(len & 0xff));
_is(bits[6] == (t_byte1)((len >> 8) & 0xff));
_END("encode len to string sha256");
_end("encode len to string sha256");
}
static int encode_register_to_string_sha256()
@ -108,12 +108,12 @@ static int encode_register_to_string_sha256()
ft_sha256_encode_register(digest_part, reg);
_IS(digest_part[0] == 0xba);
_IS(digest_part[1] == 0x78);
_IS(digest_part[2] == 0x16);
_IS(digest_part[3] == 0xbf);
_is(digest_part[0] == 0xba);
_is(digest_part[1] == 0x78);
_is(digest_part[2] == 0x16);
_is(digest_part[3] == 0xbf);
_END("encode register to string sha256");
_end("encode register to string sha256");
}
static int update_bit_count_sha256()
@ -124,9 +124,9 @@ static int update_bit_count_sha256()
ft_sha256_init(&ctx);
ft_sha256_update(&ctx, message, sizeof(message));
_IS(ctx.bit_len == sizeof(message) * 8);
_is(ctx.bit_len == sizeof(message) * 8);
_END("update bit count sha256");
_end("update bit count sha256");
}
static int fill_buffer_sha256()
@ -137,9 +137,9 @@ static int fill_buffer_sha256()
ft_sha256_init(&ctx);
ft_sha256_update(&ctx, message, sizeof(message));
_IS(ft_strcmp((const char *)message, (const char *)ctx.block) == 0);
_is(ft_strcmp((const char *)message, (const char *)ctx.block) == 0);
_END("fill buffer sha256");
_end("fill buffer sha256");
}
static void block_with_right_padding
@ -176,10 +176,10 @@ static int add_right_padding_sha256()
i = 0;
_IS(ft_memcmp(ctx.block, block_with_message_and_pading,
_is(ft_memcmp(ctx.block, block_with_message_and_pading,
FT_SHA256_BLOCK_SIZE) == 0);
_END("add right padding sha256");
_end("add right padding sha256");
}
static int compute_digest_sha256()
@ -192,16 +192,16 @@ static int compute_digest_sha256()
ft_sha256_update(&ctx, message, ft_strlen((const char *)message));
ft_sha256_final(digest, &ctx);
_IS(ctx.a == 0xba7816bf);
_IS(ctx.b == 0x8f01cfea);
_IS(ctx.c == 0x414140de);
_IS(ctx.d == 0x5dae2223);
_IS(ctx.e == 0xb00361a3);
_IS(ctx.f == 0x96177a9c);
_IS(ctx.g == 0xb410ff61);
_IS(ctx.h == 0xf20015ad);
_is(ctx.a == 0xba7816bf);
_is(ctx.b == 0x8f01cfea);
_is(ctx.c == 0x414140de);
_is(ctx.d == 0x5dae2223);
_is(ctx.e == 0xb00361a3);
_is(ctx.f == 0x96177a9c);
_is(ctx.g == 0xb410ff61);
_is(ctx.h == 0xf20015ad);
_END("compute digest sha256");
_end("compute digest sha256");
}
static int compute_digest_sha224()
@ -214,15 +214,15 @@ static int compute_digest_sha224()
ft_sha224_update(&ctx, message, ft_strlen((const char *)message));
ft_sha224_final(digest, &ctx);
_IS(ctx.a == 0x23097d22);
_IS(ctx.b == 0x3405d822);
_IS(ctx.c == 0x8642a477);
_IS(ctx.d == 0xbda255b3);
_IS(ctx.e == 0x2aadbce4);
_IS(ctx.f == 0xbda0b3f7);
_IS(ctx.g == 0xe36c9da7);
_is(ctx.a == 0x23097d22);
_is(ctx.b == 0x3405d822);
_is(ctx.c == 0x8642a477);
_is(ctx.d == 0xbda255b3);
_is(ctx.e == 0x2aadbce4);
_is(ctx.f == 0xbda0b3f7);
_is(ctx.g == 0xe36c9da7);
_END("computee digst sha224");
_end("computee digst sha224");
}
static int create_digest_string_sha256()
@ -239,10 +239,10 @@ static int create_digest_string_sha256()
ft_sha256_final(digest, &ctx);
ft_sha256_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)message_digest,
_is(ft_strcmp((const char *)message_digest,
(const char *)digest_string) == 0);
_END("create digest string sha256");
_end("create digest string sha256");
}
static int create_digest_string_sha224()
@ -259,26 +259,26 @@ static int create_digest_string_sha224()
ft_sha224_final(digest, &ctx);
ft_sha224_digest_string(digest, digest_string);
_IS(ft_strcmp((const char *)message_digest,
_is(ft_strcmp((const char *)message_digest,
(const char *)digest_string) == 0);
_END("create digest strinf sha224");
_end("create digest strinf sha224");
}
int sha_tests()
{
_SHOULD(init_sha256_ctx);
_SHOULD(init_sha224_ctx);
_SHOULD(decode_string_to_int_sha256);
_SHOULD(encode_len_to_string_sha256);
_SHOULD(encode_register_to_string_sha256);
_SHOULD(update_bit_count_sha256);
_SHOULD(fill_buffer_sha256);
_SHOULD(add_right_padding_sha256);
_SHOULD(compute_digest_sha256);
_SHOULD(compute_digest_sha224);
_SHOULD(create_digest_string_sha256);
_SHOULD(create_digest_string_sha224);
_should(init_sha256_ctx);
_should(init_sha224_ctx);
_should(decode_string_to_int_sha256);
_should(encode_len_to_string_sha256);
_should(encode_register_to_string_sha256);
_should(update_bit_count_sha256);
_should(fill_buffer_sha256);
_should(add_right_padding_sha256);
_should(compute_digest_sha256);
_should(compute_digest_sha224);
_should(create_digest_string_sha256);
_should(create_digest_string_sha224);
return 0;
}

View file

@ -15,11 +15,11 @@
int all_tests()
{
_VERIFY("md5:", md5_tests);
_VERIFY("sha:", sha_tests);
_VERIFY("base64:", base64_tests);
_VERIFY("des:", des_tests);
_VERIFY("pbkdf2:", pbkdf2_tests);
_verify("md5:", md5_tests);
_verify("sha:", sha_tests);
_verify("base64:", base64_tests);
_verify("des:", des_tests);
_verify("pbkdf2:", pbkdf2_tests);
return 0;
}