Compare commits
No commits in common. "a517fbf50ce4cd5e27b296155ebb28036e5d6887" and "bb9bc8cba1f88af3e0dcd84ea2d91db00bcd43ad" have entirely different histories.
a517fbf50c
...
bb9bc8cba1
99 changed files with 997 additions and 2466 deletions
36
Makefile
36
Makefile
|
@ -138,33 +138,20 @@ DES_SRC = ft_des_initial_permutation.c \
|
||||||
ft_des_process_block.c \
|
ft_des_process_block.c \
|
||||||
ft_des_generate_encryption_round_keys.c \
|
ft_des_generate_encryption_round_keys.c \
|
||||||
ft_des_generate_decryption_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_ecb.c \
|
||||||
ft_des_cbc.c \
|
|
||||||
ft_des_pcbc.c \
|
|
||||||
ft_des_init_ctx.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_derive_key.c \
|
||||||
ft_des_encryption_key_routine.c \
|
|
||||||
ft_des_decryption_key_routine.c \
|
|
||||||
ft_des_print_error.c \
|
ft_des_print_error.c \
|
||||||
ft_des_encrypt.c \
|
ft_des_arg_parsers.c \
|
||||||
ft_des_decrypt.c \
|
ft_des_ecb_encrypt.c \
|
||||||
ft_des_decrypt_b64.c \
|
ft_des_ecb_decrypt.c \
|
||||||
ft_des_encrypt_b64.c \
|
ft_des_ecb_decrypt_b64.c \
|
||||||
ft_des_encode_process_chunk.c \
|
ft_des_ecb_encrypt_b64.c \
|
||||||
|
ft_des_ecb_encode_process_chunk.c \
|
||||||
ft_des_hex_to_bit.c \
|
ft_des_hex_to_bit.c \
|
||||||
ft_des_hex_to_byte.c \
|
ft_des_hex_to_byte.c \
|
||||||
ft_des_byte_to_bits.c \
|
ft_des_byte_to_bits.c \
|
||||||
ft_des_bits_to_bytes.c \
|
ft_des_ecb_finish_encrypt.c
|
||||||
ft_des_finish_encrypt.c \
|
|
||||||
ft_des_buffered_read.c
|
|
||||||
|
|
||||||
KD2_SRC = ft_hmac_sha256_init_ctx.c \
|
KD2_SRC = ft_hmac_sha256_init_ctx.c \
|
||||||
ft_hmac_sha256.c \
|
ft_hmac_sha256.c \
|
||||||
|
@ -264,18 +251,12 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR)
|
||||||
$(OBJ_DIR):
|
$(OBJ_DIR):
|
||||||
mkdir $(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)
|
$(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS)
|
||||||
|
|
||||||
$(LIBFT):
|
$(LIBFT):
|
||||||
$(MAKE) -C $(LIBFT_DIR)
|
$(MAKE) -C $(LIBFT_DIR)
|
||||||
|
|
||||||
$(OPENSSL_CRY) $(OPENSSL_SSL):
|
|
||||||
cd $(OPENSSL_DIR) && \
|
|
||||||
./config --prefix=$(OPENSSL_BLD) && \
|
|
||||||
make -j 4 && \
|
|
||||||
make -j 4 install
|
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
check: $(TEST_BIN)
|
check: $(TEST_BIN)
|
||||||
|
@ -290,7 +271,6 @@ clean:
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -f $(NAME)
|
rm -f $(NAME)
|
||||||
rm -f $(TEST_BIN)
|
rm -f $(TEST_BIN)
|
||||||
rm -rf $(OPENSSL_BLD)
|
|
||||||
$(MAKE) -C $(LIBFT_DIR) fclean
|
$(MAKE) -C $(LIBFT_DIR) fclean
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
|
@ -23,13 +23,13 @@
|
||||||
typedef uint64_t t_byte8;
|
typedef uint64_t t_byte8;
|
||||||
typedef unsigned char t_byte1;
|
typedef unsigned char t_byte1;
|
||||||
|
|
||||||
typedef struct s_base64_encode_buffer
|
typedef struct s_base64_encode_buffer
|
||||||
{
|
{
|
||||||
t_byte1 block[FT_BASE64_ENCODE_BLOCK_SIZE];
|
t_byte1 block[FT_BASE64_ENCODE_BLOCK_SIZE];
|
||||||
t_byte8 readed;
|
t_byte8 readed;
|
||||||
} t_base64_encode_buffer;
|
} t_base64_encode_buffer;
|
||||||
|
|
||||||
typedef struct s_base64_decode_buffer
|
typedef struct s_base64_decode_buffer
|
||||||
{
|
{
|
||||||
t_byte1 block[FT_BASE64_DECODE_BLOCK_SIZE];
|
t_byte1 block[FT_BASE64_DECODE_BLOCK_SIZE];
|
||||||
t_byte8 readed;
|
t_byte8 readed;
|
||||||
|
@ -42,7 +42,7 @@ typedef struct s_base64_ctx
|
||||||
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
|
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
|
||||||
} t_base64_ctx;
|
} t_base64_ctx;
|
||||||
|
|
||||||
typedef struct s_base64_flags
|
typedef struct s_base64_flags
|
||||||
{
|
{
|
||||||
int decode;
|
int decode;
|
||||||
} t_base64_flags;
|
} t_base64_flags;
|
||||||
|
@ -113,7 +113,7 @@ void ft_base64_init_decode_buffer
|
||||||
t_base64_decode_buffer *buff
|
t_base64_decode_buffer *buff
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_base64_decode_chunk
|
void ft_base64_decode_chunk
|
||||||
(
|
(
|
||||||
t_base64_ctx *ctx,
|
t_base64_ctx *ctx,
|
||||||
t_byte8 len,
|
t_byte8 len,
|
||||||
|
@ -121,7 +121,7 @@ void ft_base64_decode_chunk
|
||||||
t_base64_decode_buffer *dec_buff
|
t_base64_decode_buffer *dec_buff
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_base64_decode_transform
|
void ft_base64_decode_transform
|
||||||
(
|
(
|
||||||
t_base64_ctx *ctx,
|
t_base64_ctx *ctx,
|
||||||
t_byte1 *message,
|
t_byte1 *message,
|
||||||
|
|
144
inc/ft_des.h
144
inc/ft_des.h
|
@ -14,7 +14,6 @@
|
||||||
# define FT_DES_H
|
# define FT_DES_H
|
||||||
|
|
||||||
# include <stdint.h>
|
# include <stdint.h>
|
||||||
# include <stddef.h>
|
|
||||||
|
|
||||||
# define FT_DES_BYTE_BLOCK_SIZE 8
|
# define FT_DES_BYTE_BLOCK_SIZE 8
|
||||||
# define FT_DES_BIT_BLOCK_SIZE 64
|
# define FT_DES_BIT_BLOCK_SIZE 64
|
||||||
|
@ -33,13 +32,6 @@
|
||||||
typedef uint64_t t_byte8;
|
typedef uint64_t t_byte8;
|
||||||
typedef unsigned char t_byte1;
|
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
|
typedef struct s_des_ctx
|
||||||
{
|
{
|
||||||
|
@ -48,29 +40,27 @@ typedef struct s_des_ctx
|
||||||
int decode;
|
int decode;
|
||||||
int b64;
|
int b64;
|
||||||
int readed;
|
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 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]
|
t_byte1 round_keys[FT_DES_ROUND_COUNT]
|
||||||
[FT_DES_ROUND_KEY_SIZE];
|
[FT_DES_ROUND_KEY_SIZE];
|
||||||
const char *raw_password;
|
const char *raw_password;
|
||||||
const char *raw_salt;
|
const char *raw_salt;
|
||||||
const char *raw_key;
|
const char *raw_key;
|
||||||
const char *raw_iv;
|
|
||||||
t_ft_des_mode encrypt;
|
|
||||||
t_ft_des_mode decrypt;
|
|
||||||
} t_des_ctx;
|
} t_des_ctx;
|
||||||
|
|
||||||
typedef int (*t_ft_des_arg_parser_function)
|
typedef int (*t_ft_des_arg_parser_function)
|
||||||
(int argc, char **argv, int position, t_des_ctx *c);
|
(int argc, char **argv, int position, t_des_ctx *c);
|
||||||
|
|
||||||
typedef struct s_des_argument_parser
|
typedef struct s_des_argument_parser
|
||||||
{
|
{
|
||||||
const char *arg;
|
const char *arg;
|
||||||
t_ft_des_arg_parser_function arg_parser;
|
t_ft_des_arg_parser_function arg_parser;
|
||||||
} t_des_argument_parser;
|
} t_des_argument_parser;
|
||||||
|
|
||||||
typedef struct s_des_chunk_buffer
|
typedef struct s_des_chunk_buffer
|
||||||
{
|
{
|
||||||
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
t_byte8 readed;
|
t_byte8 readed;
|
||||||
|
@ -231,9 +221,10 @@ void ft_des_print_error
|
||||||
const char *error
|
const char *error
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des
|
void ft_des_ecb
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
int argc,
|
||||||
|
char **argv
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_init_ctx
|
void ft_des_init_ctx
|
||||||
|
@ -241,13 +232,6 @@ void ft_des_init_ctx
|
||||||
t_des_ctx *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 ft_des_key_arg_parser
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -256,14 +240,6 @@ int ft_des_key_arg_parser
|
||||||
t_des_ctx *c
|
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 ft_des_decode_arg_parser
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -328,70 +304,34 @@ int ft_des_salt_arg_parser
|
||||||
t_des_ctx *c
|
t_des_ctx *c
|
||||||
);
|
);
|
||||||
|
|
||||||
int ft_des_help_arg_parser
|
void ft_des_ecb_decrypt
|
||||||
(
|
|
||||||
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
|
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_decrypt_b64
|
void ft_des_ecb_decrypt_b64
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_encrypt
|
void ft_des_ecb_encrypt
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_encrypt_b64
|
void ft_des_ecb_encrypt_b64
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_encode_process_chunk
|
void ft_des_ecb_encode_process_chunk
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx,
|
t_des_ctx *ctx,
|
||||||
t_byte8 reaed,
|
t_byte8 reaed,
|
||||||
t_byte1 buffer[FT_DES_READ_SIZE]
|
t_byte1 buffer[FT_DES_READ_SIZE]
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_finish_encrypt
|
void ft_des_ecb_finish_encrypt
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
t_des_ctx *ctx
|
||||||
);
|
);
|
||||||
|
@ -424,59 +364,9 @@ void ft_des_byte_to_bits
|
||||||
t_byte8 bits_len
|
t_byte8 bits_len
|
||||||
);
|
);
|
||||||
|
|
||||||
void ft_des_bits_to_bytes
|
void ft_des_derive_key
|
||||||
(
|
|
||||||
t_byte1 *bits,
|
|
||||||
t_byte8 bits_len,
|
|
||||||
t_byte1 *bytes,
|
|
||||||
t_byte8 byte_len
|
|
||||||
);
|
|
||||||
|
|
||||||
void ft_des_encryption_key_routine
|
|
||||||
(
|
(
|
||||||
t_des_ctx *ctx
|
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
|
#endif
|
||||||
|
|
|
@ -1,21 +1,10 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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
|
#ifndef FT_PBKDF2_H
|
||||||
# define FT_PBKDF2_H
|
# define FT_PBKDF2_H
|
||||||
|
|
||||||
# include "ft_sha.h"
|
# include "ft_sha.h"
|
||||||
|
|
||||||
typedef struct s_pbkdf2_sha256_ctx
|
typedef struct s_pbkdf2_sha256_ctx
|
||||||
{
|
{
|
||||||
unsigned char *pass;
|
unsigned char *pass;
|
||||||
unsigned char *salt;
|
unsigned char *salt;
|
||||||
|
@ -26,7 +15,7 @@ typedef struct s_pbkdf2_sha256_ctx
|
||||||
unsigned int iterations;
|
unsigned int iterations;
|
||||||
} t_pbkdf2_sha256_ctx;
|
} t_pbkdf2_sha256_ctx;
|
||||||
|
|
||||||
typedef struct s_hmac_sha256_ctx
|
typedef struct s_hmac_sha256_ctx
|
||||||
{
|
{
|
||||||
unsigned char *key;
|
unsigned char *key;
|
||||||
unsigned char *msg;
|
unsigned char *msg;
|
||||||
|
@ -76,4 +65,5 @@ void ft_pbkdf2_sha256
|
||||||
t_pbkdf2_sha256_ctx *ctx
|
t_pbkdf2_sha256_ctx *ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
#endif
|
|
||||||
|
#endif
|
10
inc/t.h
10
inc/t.h
|
@ -15,10 +15,10 @@
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
|
||||||
# define FAIL() printf("\nfail in %s() %s:%d\n\n", __func__, __FILE__, __LINE__)
|
# 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 _is(t) do { if (!(t)) { FAIL(); return 1; } } while(0)
|
||||||
# define _SHOULD(t) do { int r = t(); if(r) return r; } 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 _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 _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 _end(m) do { printf(" %s - OK\n", m); return 0; } while(0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
2
obj/.gitignore
vendored
Normal file
2
obj/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
./*
|
||||||
|
!.gitignore
|
29
openssl_cases.txt
Normal file
29
openssl_cases.txt
Normal 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.
|
|
@ -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 <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -17,17 +5,10 @@
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include "ft_base64.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)
|
static int open_input(char *filename)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
struct stat stat_buff;
|
struct stat stat_buff;
|
||||||
|
|
||||||
if ((fd = open((const char *)filename, O_RDONLY, 0)) == -1)
|
if ((fd = open((const char *)filename, O_RDONLY, 0)) == -1)
|
||||||
{
|
{
|
||||||
|
@ -40,23 +21,27 @@ static int open_input(char *filename)
|
||||||
ft_putstr_fd("base64: input path is not a file", STDERR_FILENO);
|
ft_putstr_fd("base64: input path is not a file", STDERR_FILENO);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return (fd);
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int open_output(char *filename)
|
static int open_output(char *filename)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = open((const char *)filename,
|
if ((fd = open((const char *)filename,
|
||||||
O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1)
|
O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1)
|
||||||
{
|
{
|
||||||
perror("base64: output stream error");
|
perror("base64: output stream error");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return (fd);
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_args
|
static void init_flags(t_base64_flags *flags)
|
||||||
|
{
|
||||||
|
flags->decode = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_args
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
|
@ -64,12 +49,12 @@ static void read_args
|
||||||
t_base64_ctx *ctx
|
t_base64_ctx *ctx
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *current_arg;
|
char *current_arg;
|
||||||
char *next_arg;
|
char *next_arg;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < argc)
|
while(i < argc)
|
||||||
{
|
{
|
||||||
current_arg = argv[i];
|
current_arg = argv[i];
|
||||||
next_arg = i + 1 < argc ? argv[i + 1] : NULL;
|
next_arg = i + 1 < argc ? argv[i + 1] : NULL;
|
||||||
|
@ -81,8 +66,6 @@ static void read_args
|
||||||
ctx->input_fd = open_input(next_arg);
|
ctx->input_fd = open_input(next_arg);
|
||||||
else if (ft_strcmp(current_arg, "-o") == 0)
|
else if (ft_strcmp(current_arg, "-o") == 0)
|
||||||
ctx->output_fd = open_output(next_arg);
|
ctx->output_fd = open_output(next_arg);
|
||||||
else if (ft_strcmp(current_arg, "-h") == 0)
|
|
||||||
usage();
|
|
||||||
else if (++i)
|
else if (++i)
|
||||||
continue;
|
continue;
|
||||||
i++;
|
i++;
|
||||||
|
@ -94,7 +77,7 @@ void ft_base64(int argc, char **argv)
|
||||||
t_base64_flags flags;
|
t_base64_flags flags;
|
||||||
t_base64_ctx ctx;
|
t_base64_ctx ctx;
|
||||||
|
|
||||||
flags.decode = 0;
|
init_flags(&flags);
|
||||||
ft_base64_init(&ctx);
|
ft_base64_init(&ctx);
|
||||||
read_args(argc, argv, &flags, &ctx);
|
read_args(argc, argv, &flags, &ctx);
|
||||||
if (flags.decode)
|
if (flags.decode)
|
||||||
|
@ -102,4 +85,4 @@ void ft_base64(int argc, char **argv)
|
||||||
else
|
else
|
||||||
ft_base64_encode(&ctx);
|
ft_base64_encode(&ctx);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
|
@ -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 <unistd.h>
|
||||||
#include "ft_base64.h"
|
#include "ft_base64.h"
|
||||||
|
|
||||||
|
@ -20,10 +8,10 @@ void ft_base64_decode(t_base64_ctx *ctx)
|
||||||
t_byte8 readed;
|
t_byte8 readed;
|
||||||
|
|
||||||
ft_base64_init_decode_buffer(&decode_buff);
|
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);
|
readed = ft_base64_decode_filter(readed, read_buff);
|
||||||
ft_base64_decode_chunk(ctx, readed, read_buff, &decode_buff);
|
ft_base64_decode_chunk(ctx, readed, read_buff, &decode_buff);
|
||||||
}
|
}
|
||||||
ft_base64_decode_finish(&decode_buff);
|
ft_base64_decode_finish(&decode_buff);
|
||||||
}
|
}
|
|
@ -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 <unistd.h>
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include "ft_base64.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_memcpy(&buff->block[buffer_index], message, free_space_in_buffer);
|
||||||
ft_base64_decode_write(ctx, buff->block);
|
ft_base64_decode_write(ctx, buff->block);
|
||||||
idx = free_space_in_buffer;
|
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]);
|
ft_base64_decode_write(ctx, &message[idx]);
|
||||||
idx += FT_BASE64_DECODE_BLOCK_SIZE;
|
idx += FT_BASE64_DECODE_BLOCK_SIZE;
|
||||||
|
@ -61,4 +49,4 @@ void ft_base64_decode_chunk
|
||||||
else
|
else
|
||||||
idx = 0;
|
idx = 0;
|
||||||
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
|
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
|
||||||
}
|
}
|
|
@ -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 "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -24,7 +12,7 @@ t_byte8 ft_base64_decode_filter
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
last_free_index = 0;
|
last_free_index = 0;
|
||||||
while (i < readed)
|
while(i < readed)
|
||||||
{
|
{
|
||||||
if (!ft_isspace(buffer[i]))
|
if (!ft_isspace(buffer[i]))
|
||||||
{
|
{
|
||||||
|
@ -34,4 +22,4 @@ t_byte8 ft_base64_decode_filter
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (last_free_index);
|
return (last_free_index);
|
||||||
}
|
}
|
|
@ -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 <fcntl.h>
|
||||||
#include "ft_base64.h"
|
#include "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
@ -30,4 +18,4 @@ void ft_base64_decode_finish
|
||||||
bytes count isn't multiple of 4\n", STDERR_FILENO);
|
bytes count isn't multiple of 4\n", STDERR_FILENO);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,19 +1,7 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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 "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static t_byte1 get_alphabet_index
|
static t_byte1 get_alphabet_index
|
||||||
(
|
(
|
||||||
t_byte1 *alphabet,
|
t_byte1 *alphabet,
|
||||||
t_byte1 ch
|
t_byte1 ch
|
||||||
|
@ -32,7 +20,7 @@ static t_byte1 get_alphabet_index
|
||||||
return (alphabet_addr - (char *)alphabet);
|
return (alphabet_addr - (char *)alphabet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_base64_decode_transform
|
void ft_base64_decode_transform
|
||||||
(
|
(
|
||||||
t_base64_ctx *c,
|
t_base64_ctx *c,
|
||||||
t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE],
|
t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE],
|
||||||
|
@ -51,4 +39,4 @@ void ft_base64_decode_transform
|
||||||
decoded_block[0] = first_index << 2 | second_index >> 4;
|
decoded_block[0] = first_index << 2 | second_index >> 4;
|
||||||
decoded_block[1] = second_index << 4 | third_index >> 2;
|
decoded_block[1] = second_index << 4 | third_index >> 2;
|
||||||
decoded_block[2] = third_index << 6 | fourth_index;
|
decoded_block[2] = third_index << 6 | fourth_index;
|
||||||
}
|
}
|
|
@ -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 "ft_base64.h"
|
||||||
#include "libft.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_memcpy(&buff->block[buffer_index], message, free_space_in_buffer);
|
||||||
ft_base64_encode_write(ctx, buff->block);
|
ft_base64_encode_write(ctx, buff->block);
|
||||||
idx = free_space_in_buffer;
|
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]);
|
ft_base64_encode_write(ctx, &message[idx]);
|
||||||
idx += FT_BASE64_ENCODE_BLOCK_SIZE;
|
idx += FT_BASE64_ENCODE_BLOCK_SIZE;
|
||||||
|
@ -55,4 +43,4 @@ void ft_base64_encode_chunk
|
||||||
else
|
else
|
||||||
idx = 0;
|
idx = 0;
|
||||||
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
|
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
|
||||||
}
|
}
|
|
@ -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 "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -38,4 +26,4 @@ void ft_base64_encode_finish
|
||||||
padding_size);
|
padding_size);
|
||||||
write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE);
|
write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE);
|
||||||
write(ctx->output_fd, "\n", 1);
|
write(ctx->output_fd, "\n", 1);
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */
|
/* 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);
|
second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf);
|
||||||
third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3);
|
third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3);
|
||||||
fourth_char = data[2] & 0x3F;
|
fourth_char = data[2] & 0x3F;
|
||||||
|
|
||||||
chars[0] = ctx->alphabet[first_char];
|
chars[0] = ctx->alphabet[first_char];
|
||||||
chars[1] = ctx->alphabet[second_char];
|
chars[1] = ctx->alphabet[second_char];
|
||||||
chars[2] = ctx->alphabet[third_char];
|
chars[2] = ctx->alphabet[third_char];
|
||||||
chars[3] = ctx->alphabet[fourth_char];
|
chars[3] = ctx->alphabet[fourth_char];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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 "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_base64_init_decode_buffer
|
void ft_base64_init_decode_buffer
|
||||||
(
|
(
|
||||||
t_base64_decode_buffer *buff
|
t_base64_decode_buffer *buff
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
ft_bzero(buff->block, FT_BASE64_DECODE_BLOCK_SIZE);
|
ft_bzero(buff->block, FT_BASE64_DECODE_BLOCK_SIZE);
|
||||||
buff->readed = 0;
|
buff->readed = 0;
|
||||||
}
|
}
|
|
@ -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 "ft_base64.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -20,4 +8,4 @@ void ft_base64_init_encode_buffer
|
||||||
{
|
{
|
||||||
ft_bzero(buff->block, FT_BASE64_ENCODE_BLOCK_SIZE);
|
ft_bzero(buff->block, FT_BASE64_ENCODE_BLOCK_SIZE);
|
||||||
buff->readed = 0;
|
buff->readed = 0;
|
||||||
}
|
}
|
|
@ -1,19 +1,7 @@
|
||||||
/* ************************************************************************** */
|
# include "ft_base64.h"
|
||||||
/* */
|
# include <unistd.h>
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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>
|
|
||||||
|
|
||||||
void ft_base64_write(t_base64_ctx *ctx)
|
void ft_base64_write(t_base64_ctx *ctx)
|
||||||
{
|
{
|
||||||
write(ctx->output_fd, ctx->chars, FT_BASE64_CHARS_SIZE);
|
write(ctx->output_fd, ctx->chars, FT_BASE64_CHARS_SIZE);
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -1,15 +1,8 @@
|
||||||
/* ************************************************************************** */
|
#include <stddef.h>
|
||||||
/* */
|
#include <fcntl.h>
|
||||||
/* ::: :::::::: */
|
#include <stdio.h>
|
||||||
/* ft_des_arg_parsers.c :+: :+: :+: */
|
#include <stdlib.h>
|
||||||
/* +:+ +:+ +:+ */
|
#include <sys/stat.h>
|
||||||
/* 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 "ft_des.h"
|
#include "ft_des.h"
|
||||||
|
|
||||||
int ft_des_base64_arg_parser
|
int ft_des_base64_arg_parser
|
||||||
|
@ -26,6 +19,7 @@ int ft_des_base64_arg_parser
|
||||||
return (++position);
|
return (++position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int ft_des_key_arg_parser
|
int ft_des_key_arg_parser
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
|
@ -40,22 +34,7 @@ int ft_des_key_arg_parser
|
||||||
return (position + 2);
|
return (position + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_des_iv_arg_parser
|
int ft_des_decode_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,
|
int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
|
@ -69,7 +48,7 @@ int ft_des_decode_arg_parser
|
||||||
return (++position);
|
return (++position);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_des_encode_arg_parser
|
int ft_des_encode_arg_parser
|
||||||
(
|
(
|
||||||
int argc,
|
int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
|
@ -82,3 +61,73 @@ int ft_des_encode_arg_parser
|
||||||
ctx->decode = 0;
|
ctx->decode = 0;
|
||||||
return (++position);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_byte_to_bits
|
void ft_des_byte_to_bits
|
||||||
|
@ -25,7 +13,7 @@ void ft_des_byte_to_bits
|
||||||
|
|
||||||
bits_count = 0;
|
bits_count = 0;
|
||||||
bytes_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] = bytes[bytes_count] >> 7 & 1;
|
||||||
bits[bits_count + 1] = bytes[bytes_count] >> 6 & 1;
|
bits[bits_count + 1] = bytes[bytes_count] >> 6 & 1;
|
||||||
|
@ -38,4 +26,4 @@ void ft_des_byte_to_bits
|
||||||
bytes_count++;
|
bytes_count++;
|
||||||
bits_count += 8;
|
bits_count += 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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]);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_derive_decryption_round_key
|
void ft_des_derive_decryption_round_key
|
||||||
|
@ -30,4 +18,4 @@ void ft_des_derive_decryption_round_key
|
||||||
ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 2);
|
ft_des_rotate_half_key_right(key + FT_DES_REDUCED_KEY_SIZE / 2, 2);
|
||||||
}
|
}
|
||||||
ft_des_key_permuted_choice_two(key, round_key);
|
ft_des_key_permuted_choice_two(key, round_key);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_derive_encryption_round_key
|
void ft_des_derive_encryption_round_key
|
||||||
|
@ -30,4 +18,4 @@ void ft_des_derive_encryption_round_key
|
||||||
ft_des_rotate_half_key_left(input_key + FT_DES_REDUCED_KEY_SIZE / 2, 2);
|
ft_des_rotate_half_key_left(input_key + FT_DES_REDUCED_KEY_SIZE / 2, 2);
|
||||||
}
|
}
|
||||||
ft_des_key_permuted_choice_two(input_key, round_key);
|
ft_des_key_permuted_choice_two(input_key, round_key);
|
||||||
}
|
}
|
|
@ -1,41 +1,45 @@
|
||||||
/* ************************************************************************** */
|
#include <unistd.h>
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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 "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include "ft_pbkdf2.h"
|
#include "ft_pbkdf2.h"
|
||||||
#include "libft.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_des_ctx *ctx
|
||||||
t_byte1 iv[FT_DES_BIT_BLOCK_SIZE],
|
|
||||||
char salt[FT_DES_BYTE_BLOCK_SIZE],
|
|
||||||
char *pass
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_pbkdf2_sha256_ctx pbkdf_ctx;
|
t_pbkdf2_sha256_ctx pbkdf_ctx;
|
||||||
t_byte1 key_iv[FT_DES_BYTE_BLOCK_SIZE * 2];
|
t_byte1 key[FT_DES_BYTE_BLOCK_SIZE];
|
||||||
t_byte1 key_iv_bit[FT_DES_BIT_BLOCK_SIZE * 2];
|
|
||||||
|
|
||||||
|
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.iterations = 10000;
|
||||||
pbkdf_ctx.key = key_iv;
|
pbkdf_ctx.key = key;
|
||||||
pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE;
|
pbkdf_ctx.salt_len = FT_DES_BYTE_BLOCK_SIZE;
|
||||||
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE * 2;
|
pbkdf_ctx.pass = (t_byte1 *)ctx->raw_password;
|
||||||
pbkdf_ctx.pass_len = ft_strlen(pass);
|
pbkdf_ctx.salt = ctx->salt;
|
||||||
pbkdf_ctx.pass = (t_byte1 *)pass;
|
pbkdf_ctx.pass_len = ft_strlen((char *)ctx->raw_password);
|
||||||
pbkdf_ctx.salt = (t_byte1 *)salt;
|
pbkdf_ctx.key_len = FT_DES_BYTE_BLOCK_SIZE;
|
||||||
ft_pbkdf2_sha256(&pbkdf_ctx);
|
ft_pbkdf2_sha256(&pbkdf_ctx);
|
||||||
ft_des_byte_to_bits(key_iv, FT_DES_BYTE_BLOCK_SIZE * 2, key_iv_bit,
|
ft_des_byte_to_bits(key, FT_DES_BYTE_BLOCK_SIZE, ctx->key,
|
||||||
FT_DES_BIT_BLOCK_SIZE * 2);
|
FT_DES_INITIAL_KEY_SIZE);
|
||||||
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);
|
|
||||||
}
|
|
|
@ -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_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],
|
int argc,
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
char **argv,
|
||||||
t_byte1 iv[FT_DES_BYTE_BLOCK_SIZE],
|
t_des_ctx *ctx
|
||||||
t_byte1 output[FT_DES_BYTE_BLOCK_SIZE]
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
(void)iv;
|
int i;
|
||||||
ft_des_process_block(input, keys, output);
|
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
|
void ft_des_ecb
|
||||||
|
@ -30,11 +74,24 @@ void ft_des_ecb
|
||||||
char **argv
|
char **argv
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_des_ctx ctx;
|
t_des_ctx ctx;
|
||||||
|
|
||||||
ft_des_init_ctx(&ctx);
|
ft_des_init_ctx(&ctx);
|
||||||
ft_des_parse_args(argc, argv, &ctx);
|
parse_args(argc, argv, &ctx);
|
||||||
ctx.encrypt = encrypt;
|
ft_des_derive_key(&ctx);
|
||||||
ctx.decrypt = encrypt;
|
if (ctx.decode)
|
||||||
ft_des(&ctx);
|
{
|
||||||
}
|
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);
|
||||||
|
}
|
52
src/des/ft_des_ecb_decrypt.c
Normal file
52
src/des/ft_des_ecb_decrypt.c
Normal 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]);
|
||||||
|
}
|
50
src/des/ft_des_ecb_decrypt_b64.c
Normal file
50
src/des/ft_des_ecb_decrypt_b64.c
Normal 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);
|
||||||
|
}
|
45
src/des/ft_des_ecb_encode_process_chunk.c
Normal file
45
src/des/ft_des_ecb_encode_process_chunk.c
Normal 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);
|
||||||
|
}
|
17
src/des/ft_des_ecb_encrypt.c
Normal file
17
src/des/ft_des_ecb_encrypt.c
Normal 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);
|
||||||
|
}
|
51
src/des/ft_des_ecb_encrypt_b64.c
Normal file
51
src/des/ft_des_ecb_encrypt_b64.c
Normal 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);
|
||||||
|
}
|
16
src/des/ft_des_ecb_finish_decrypt.c
Normal file
16
src/des/ft_des_ecb_finish_decrypt.c
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "ft_des.h"
|
||||||
|
|
||||||
|
void ft_des_ecb_finish_decrypt
|
||||||
|
(
|
||||||
|
t_des_ctx *ctx,
|
||||||
|
t_des_decrypt_buffer *buff
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_byte1 idx;
|
||||||
|
|
||||||
|
idx = 0;
|
||||||
|
ctx->readed += buff->readed[buff->current_buffer];
|
||||||
|
if (ctx->readed % FT_DES_BYTE_BLOCK_SIZE != 0)
|
||||||
|
ft_des_print_error("wrong message size, maybe corrupted?");
|
||||||
|
while (idx + FT_DES_BYTE_BLOCK_SIZE <=)
|
||||||
|
}
|
23
src/des/ft_des_ecb_finish_encrypt.c
Normal file
23
src/des/ft_des_ecb_finish_encrypt.c
Normal 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);
|
||||||
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_expansion_box
|
void ft_des_expansion_box
|
||||||
|
@ -18,17 +6,22 @@ void ft_des_expansion_box
|
||||||
t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
|
t_byte1 expanded[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 permutation_table[FT_DES_EXPANDED_HALF_BLOCK_SIZE] = {
|
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,
|
32, 1, 2, 3, 4, 5,
|
||||||
16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28,
|
4, 5, 6, 7, 8, 9,
|
||||||
29, 28, 29, 30, 31, 32, 1,
|
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;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
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];
|
expanded[i] = half[permutation_table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,35 +1,21 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
static void merge_key
|
static void merge_key
|
||||||
(
|
(
|
||||||
t_byte1 expanded_half[FT_DES_EXPANDED_HALF_BLOCK_SIZE],
|
t_byte1 expanded_half[FT_DES_EXPANDED_HALF_BLOCK_SIZE],
|
||||||
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
|
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
|
||||||
t_byte1 output[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
|
t_byte1 output[FT_DES_EXPANDED_HALF_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
|
while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
|
||||||
i = 0;
|
|
||||||
while (i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
|
|
||||||
{
|
{
|
||||||
output[i] = expanded_half[i] ^ key[i];
|
output[i] = expanded_half[i] ^ key[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_des_feistel_function
|
void ft_des_feistel_function
|
||||||
(
|
(
|
||||||
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
|
t_byte1 key[FT_DES_ROUND_KEY_SIZE],
|
||||||
|
@ -50,4 +36,4 @@ void ft_des_feistel_function
|
||||||
ft_des_s_box_7(expanded + 36, all_s_boxes + 24);
|
ft_des_s_box_7(expanded + 36, all_s_boxes + 24);
|
||||||
ft_des_s_box_8(expanded + 42, all_s_boxes + 28);
|
ft_des_s_box_8(expanded + 42, all_s_boxes + 28);
|
||||||
ft_des_feistel_function_permutation(all_s_boxes, output);
|
ft_des_feistel_function_permutation(all_s_boxes, output);
|
||||||
}
|
}
|
|
@ -1,33 +1,24 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_feistel_function_permutation
|
void ft_des_feistel_function_permutation
|
||||||
(
|
(
|
||||||
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 input[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
|
t_byte1 output[FT_DES_BIT_BLOCK_SIZE / 2]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE / 2] = {
|
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,
|
16, 7, 20, 21, 29, 12, 28, 17,
|
||||||
32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
|
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;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
|
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||||
{
|
{
|
||||||
output[i] = input[table[i] - 1];
|
output[i] = input[table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_final_permutation
|
void ft_des_final_permutation
|
||||||
|
@ -18,18 +6,22 @@ void ft_des_final_permutation
|
||||||
t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE]
|
t_byte1 final_permutation[FT_DES_BIT_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = {
|
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,
|
40, 8, 48, 16, 56, 24, 64, 32,
|
||||||
14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52,
|
39, 7, 47, 15, 55, 23, 63, 31,
|
||||||
20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58,
|
38, 6, 46, 14, 54, 22, 62, 30,
|
||||||
26, 33, 1, 41, 9, 49, 17, 57, 25,
|
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;
|
t_byte1 i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
final_permutation[i] = before[table[i] - 1];
|
final_permutation[i] = before[table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_generate_decryption_round_keys
|
void ft_des_generate_decryption_round_keys
|
||||||
|
@ -18,14 +6,14 @@ void ft_des_generate_decryption_round_keys
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
ft_des_key_permuted_choice_one(input_key, reduced_key);
|
ft_des_key_permuted_choice_one(input_key, reduced_key);
|
||||||
i = 1;
|
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]);
|
ft_des_derive_decryption_round_key(reduced_key, i, keys[i - 1]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_generate_encryption_round_keys
|
void ft_des_generate_encryption_round_keys
|
||||||
|
@ -18,14 +6,14 @@ void ft_des_generate_encryption_round_keys
|
||||||
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
|
t_byte1 round_keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
ft_des_key_permuted_choice_one(key, reduced_key);
|
ft_des_key_permuted_choice_one(key, reduced_key);
|
||||||
i = 1;
|
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]);
|
ft_des_derive_encryption_round_key(reduced_key, i, round_keys[i - 1]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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 "ft_des.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -22,9 +10,8 @@ const char *ft_des_hex_to_bit
|
||||||
{
|
{
|
||||||
t_byte1 bits4;
|
t_byte1 bits4;
|
||||||
t_byte8 i;
|
t_byte8 i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (*hex && i < bit_len)
|
while(*hex && i < bit_len)
|
||||||
{
|
{
|
||||||
bits4 = ft_tolower(*hex);
|
bits4 = ft_tolower(*hex);
|
||||||
if (bits4 >= '0' && bits4 <= '9')
|
if (bits4 >= '0' && bits4 <= '9')
|
||||||
|
@ -41,4 +28,4 @@ const char *ft_des_hex_to_bit
|
||||||
hex++;
|
hex++;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
|
@ -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 "ft_des.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
|
@ -41,9 +29,8 @@ const char *ft_des_hex_to_byte
|
||||||
t_byte1 first_byte_half;
|
t_byte1 first_byte_half;
|
||||||
t_byte1 second_byte_half;
|
t_byte1 second_byte_half;
|
||||||
t_byte8 i;
|
t_byte8 i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (*hex && i < byte_len)
|
while(*hex && i < byte_len)
|
||||||
{
|
{
|
||||||
if (char_to_num(*hex, &first_byte_half))
|
if (char_to_num(*hex, &first_byte_half))
|
||||||
return (hex);
|
return (hex);
|
||||||
|
@ -57,4 +44,4 @@ const char *ft_des_hex_to_byte
|
||||||
hex += 2;
|
hex += 2;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
|
@ -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 <unistd.h>
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
@ -22,14 +10,15 @@ void ft_des_init_ctx
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
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);
|
ft_bzero(ctx->round_keys[i], FT_DES_ROUND_KEY_SIZE);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
ft_bzero(ctx->iv, FT_DES_BIT_BLOCK_SIZE);
|
ft_bzero(ctx->key, FT_DES_INITIAL_KEY_SIZE);
|
||||||
ft_bzero(ctx->key, FT_DES_BIT_BLOCK_SIZE);
|
|
||||||
ft_bzero(ctx->buffer, FT_DES_BYTE_BLOCK_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->readed = 0;
|
||||||
ctx->decode = 0;
|
ctx->decode = 0;
|
||||||
ctx->b64 = 0;
|
ctx->b64 = 0;
|
||||||
|
@ -38,7 +27,4 @@ void ft_des_init_ctx
|
||||||
ctx->raw_password = NULL;
|
ctx->raw_password = NULL;
|
||||||
ctx->raw_salt = NULL;
|
ctx->raw_salt = NULL;
|
||||||
ctx->raw_key = NULL;
|
ctx->raw_key = NULL;
|
||||||
ctx->raw_iv = NULL;
|
}
|
||||||
ctx->encrypt = NULL;
|
|
||||||
ctx->decrypt = NULL;
|
|
||||||
}
|
|
10
src/des/ft_des_init_flags.c
Normal file
10
src/des/ft_des_init_flags.c
Normal 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;
|
||||||
|
}
|
|
@ -1,35 +1,27 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_initial_permutation
|
void ft_des_initial_permutation
|
||||||
(
|
(
|
||||||
t_byte1 message[FT_DES_BIT_BLOCK_SIZE],
|
t_byte1 message[FT_DES_BIT_BLOCK_SIZE],
|
||||||
t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE]
|
t_byte1 initial_permutation[FT_DES_BIT_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 table[FT_DES_BIT_BLOCK_SIZE] = {
|
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,
|
58, 50, 42, 34, 26, 18, 10, 2,
|
||||||
46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33,
|
60, 52, 44, 36, 28, 20, 12, 4,
|
||||||
25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21,
|
62, 54, 46, 38, 30, 22, 14, 6,
|
||||||
13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
|
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;
|
t_byte1 i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
initial_permutation[i] = message[table[i] - 1];
|
initial_permutation[i] = message[table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_key_permuted_choice_one
|
void ft_des_key_permuted_choice_one
|
||||||
|
@ -18,17 +6,21 @@ void ft_des_key_permuted_choice_one
|
||||||
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE]
|
t_byte1 reduced_key[FT_DES_REDUCED_KEY_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 table[FT_DES_REDUCED_KEY_SIZE] = {
|
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,
|
57, 49, 41, 33, 25, 17, 9, 1,
|
||||||
35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62,
|
58, 50, 42, 34, 26, 18, 10, 2,
|
||||||
54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4,
|
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;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_REDUCED_KEY_SIZE)
|
while(i < FT_DES_REDUCED_KEY_SIZE)
|
||||||
{
|
{
|
||||||
reduced_key[i] = initial_key[table[i] - 1];
|
reduced_key[i] = initial_key[table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_key_permuted_choice_two
|
void ft_des_key_permuted_choice_two
|
||||||
|
@ -18,17 +6,20 @@ void ft_des_key_permuted_choice_two
|
||||||
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
|
t_byte1 round_key[FT_DES_ROUND_KEY_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 table[FT_DES_ROUND_KEY_SIZE] = {
|
static t_byte1 table[FT_DES_ROUND_KEY_SIZE] = {
|
||||||
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16,
|
14, 17, 11, 24, 1, 5, 3, 28,
|
||||||
7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44,
|
15, 6, 21, 10, 23, 19, 12, 4,
|
||||||
49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
|
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;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_ROUND_KEY_SIZE)
|
while(i < FT_DES_ROUND_KEY_SIZE)
|
||||||
{
|
{
|
||||||
round_key[i] = input_key[table[i] - 1];
|
round_key[i] = input_key[table[i] - 1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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 <unistd.h>
|
||||||
#include "ft_des.h"
|
#include "ft_des.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
@ -23,4 +11,4 @@ void ft_des_print_error
|
||||||
ft_putstr_fd(error, STDERR_FILENO);
|
ft_putstr_fd(error, STDERR_FILENO);
|
||||||
ft_putstr_fd("\n", STDERR_FILENO);
|
ft_putstr_fd("\n", STDERR_FILENO);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
|
@ -1,27 +1,14 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
static void switch_halves
|
static void switch_halves
|
||||||
(
|
(
|
||||||
t_byte1 message[FT_DES_BIT_BLOCK_SIZE]
|
t_byte1 message[FT_DES_BIT_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int tmp;
|
int tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
|
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||||
{
|
{
|
||||||
tmp = message[i];
|
tmp = message[i];
|
||||||
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
|
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
|
||||||
|
@ -36,12 +23,12 @@ static void bytes_to_bits
|
||||||
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE]
|
t_byte1 bits[FT_DES_BIT_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 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] = bytes[i] >> 7 & 1;
|
||||||
bits[j + 1] = bytes[i] >> 6 & 1;
|
bits[j + 1] = bytes[i] >> 6 & 1;
|
||||||
|
@ -62,12 +49,12 @@ static void bits_to_bytes
|
||||||
t_byte1 bytes[FT_DES_BYTE_BLOCK_SIZE]
|
t_byte1 bytes[FT_DES_BYTE_BLOCK_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
j = 0;
|
||||||
while (i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
bytes[i] = bits[j] << 7
|
bytes[i] = bits[j] << 7
|
||||||
| bits[j + 1] << 6
|
| bits[j + 1] << 6
|
||||||
|
@ -82,7 +69,7 @@ static void bits_to_bytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_des_process_block
|
void ft_des_process_block
|
||||||
(
|
(
|
||||||
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
|
||||||
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
|
||||||
|
@ -92,12 +79,12 @@ void ft_des_process_block
|
||||||
t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE];
|
t_byte1 input_bits[FT_DES_BIT_BLOCK_SIZE];
|
||||||
t_byte1 output_bits[FT_DES_BIT_BLOCK_SIZE];
|
t_byte1 output_bits[FT_DES_BIT_BLOCK_SIZE];
|
||||||
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
|
t_byte1 ip_message[FT_DES_BIT_BLOCK_SIZE];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
bytes_to_bits(input, input_bits);
|
bytes_to_bits(input, input_bits);
|
||||||
ft_des_initial_permutation(input_bits, ip_message);
|
ft_des_initial_permutation(input_bits, ip_message);
|
||||||
i = 1;
|
i = 1;
|
||||||
while (i <= FT_DES_ROUND_COUNT)
|
while(i <= FT_DES_ROUND_COUNT)
|
||||||
{
|
{
|
||||||
if (i % 2 != 0)
|
if (i % 2 != 0)
|
||||||
ft_des_round(ip_message, ip_message + 32, keys[i - 1]);
|
ft_des_round(ip_message, ip_message + 32, keys[i - 1]);
|
||||||
|
@ -108,4 +95,4 @@ void ft_des_process_block
|
||||||
switch_halves(ip_message);
|
switch_halves(ip_message);
|
||||||
ft_des_final_permutation(ip_message, output_bits);
|
ft_des_final_permutation(ip_message, output_bits);
|
||||||
bits_to_bytes(output_bits, output);
|
bits_to_bytes(output_bits, output);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_rotate_half_key_left
|
void ft_des_rotate_half_key_left
|
||||||
|
@ -23,11 +11,11 @@ void ft_des_rotate_half_key_left
|
||||||
t_byte1 tmp;
|
t_byte1 tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < rotation_number)
|
while(i < rotation_number)
|
||||||
{
|
{
|
||||||
tmp = half[0];
|
tmp = half[0];
|
||||||
j = 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];
|
half[j] = half[j + 1];
|
||||||
j++;
|
j++;
|
||||||
|
@ -35,4 +23,4 @@ void ft_des_rotate_half_key_left
|
||||||
half[FT_DES_REDUCED_KEY_SIZE / 2 - 1] = tmp;
|
half[FT_DES_REDUCED_KEY_SIZE / 2 - 1] = tmp;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_rotate_half_key_right
|
void ft_des_rotate_half_key_right
|
||||||
|
@ -23,11 +11,11 @@ void ft_des_rotate_half_key_right
|
||||||
t_byte1 tmp;
|
t_byte1 tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < rotation_number)
|
while(i < rotation_number)
|
||||||
{
|
{
|
||||||
tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1];
|
tmp = half[FT_DES_REDUCED_KEY_SIZE / 2 - 1];
|
||||||
j = 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];
|
half[j] = half[j - 1];
|
||||||
j--;
|
j--;
|
||||||
|
@ -35,4 +23,4 @@ void ft_des_rotate_half_key_right
|
||||||
half[0] = tmp;
|
half[0] = tmp;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,18 +1,6 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
static void xor
|
static void xor
|
||||||
(
|
(
|
||||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 f_function_result[FT_DES_BIT_BLOCK_SIZE / 2]
|
t_byte1 f_function_result[FT_DES_BIT_BLOCK_SIZE / 2]
|
||||||
|
@ -21,14 +9,14 @@ static void xor
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
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];
|
left_half[i] = left_half[i] ^ f_function_result[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_des_round
|
void ft_des_round
|
||||||
(
|
(
|
||||||
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 left_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
t_byte1 right_half[FT_DES_BIT_BLOCK_SIZE / 2],
|
||||||
|
@ -39,4 +27,4 @@ void ft_des_round
|
||||||
|
|
||||||
ft_des_feistel_function(right_half, round_key, feistel_function_result);
|
ft_des_feistel_function(right_half, round_key, feistel_function_result);
|
||||||
xor(left_half, feistel_function_result);
|
xor(left_half, feistel_function_result);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box
|
void ft_des_s_box
|
||||||
|
@ -30,4 +18,4 @@ void ft_des_s_box
|
||||||
output[1] = selected >> 2 & 1;
|
output[1] = selected >> 2 & 1;
|
||||||
output[2] = selected >> 1 & 1;
|
output[2] = selected >> 1 & 1;
|
||||||
output[3] = selected & 1;
|
output[3] = selected & 1;
|
||||||
}
|
}
|
|
@ -1,29 +1,16 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_1
|
void ft_des_s_box_1
|
||||||
(
|
(
|
||||||
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
|
t_byte1 input[FT_DES_S_BOX_INPUT_SIZE],
|
||||||
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
|
t_byte1 output[FT_DES_S_BOX_OUTPUT_SIZE]
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
|
static t_byte1 tbl[FT_DES_S_BOX_TABLE_ROWS][FT_DES_S_BOX_TABLE_COLUMNS] = {
|
||||||
{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
|
{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
|
||||||
{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
|
{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
|
||||||
{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
|
{ 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 }
|
{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_2
|
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] = {
|
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 },
|
{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 },
|
{ 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 },
|
{ 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 },
|
{13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_3
|
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] = {
|
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 },
|
{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, 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 },
|
{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 },
|
{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_4
|
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] = {
|
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 },
|
{ 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 },
|
{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 },
|
{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 },
|
{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_5
|
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] = {
|
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 },
|
{ 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 },
|
{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 },
|
{ 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 },
|
{11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_6
|
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] = {
|
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 },
|
{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 },
|
{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 },
|
{ 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 },
|
{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_7
|
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] = {
|
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 },
|
{ 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 },
|
{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 },
|
{ 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 },
|
{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_s_box_8
|
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] = {
|
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 },
|
{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 },
|
{ 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 },
|
{ 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 },
|
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11},
|
||||||
};
|
};
|
||||||
|
|
||||||
ft_des_s_box(input, output, tbl);
|
ft_des_s_box(input, output, tbl);
|
||||||
}
|
}
|
|
@ -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");
|
|
||||||
}
|
|
|
@ -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");
|
|
||||||
}
|
|
|
@ -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"
|
#include "ft_des.h"
|
||||||
|
|
||||||
void ft_des_switch_message_halves
|
void ft_des_switch_message_halves
|
||||||
|
@ -19,13 +7,13 @@ void ft_des_switch_message_halves
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int tmp;
|
int tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_DES_BIT_BLOCK_SIZE / 2)
|
|
||||||
|
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||||
{
|
{
|
||||||
tmp = message[i];
|
tmp = message[i];
|
||||||
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
|
message[i] = message[i + FT_DES_BIT_BLOCK_SIZE / 2];
|
||||||
message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp;
|
message[i + FT_DES_BIT_BLOCK_SIZE / 2] = tmp;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -19,7 +19,7 @@ void ft_ssl_usage(void)
|
||||||
ft_putstr("Message Digest commands:\n");
|
ft_putstr("Message Digest commands:\n");
|
||||||
ft_putstr("md5\nsha256\nsha224\n\n");
|
ft_putstr("md5\nsha256\nsha224\n\n");
|
||||||
ft_putstr("Cipher commands:\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("To get help for specific command:\n");
|
||||||
ft_putstr("ft_ssl command -h\n\n");
|
ft_putstr("ft_ssl command -h\n\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
12
src/main.c
12
src/main.c
|
@ -34,22 +34,10 @@ t_algorithm g_algorithms[] = {
|
||||||
"base64",
|
"base64",
|
||||||
ft_base64,
|
ft_base64,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"des",
|
|
||||||
ft_des_cbc,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"des-ecb",
|
"des-ecb",
|
||||||
ft_des_ecb,
|
ft_des_ecb,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"des-cbc",
|
|
||||||
ft_des_cbc,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"des-pcbc",
|
|
||||||
ft_des_pcbc,
|
|
||||||
},
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
static void ft_md5_usage(void)
|
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");
|
ft_putstr("ft_ssl md5 [-p|-q|-r] [[-s string...] [file...]]\n\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -35,8 +35,6 @@ static int read_flags(int argc, char **argv, t_md5_flags *flags)
|
||||||
i = 1;
|
i = 1;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
if (ft_strcmp("-h", argv[i]) == 0)
|
|
||||||
ft_md5_usage();
|
|
||||||
if (ft_strcmp("-r", argv[i]) == 0)
|
if (ft_strcmp("-r", argv[i]) == 0)
|
||||||
flags->reverse = 1;
|
flags->reverse = 1;
|
||||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
else if (ft_strcmp("-q", argv[i]) == 0)
|
||||||
|
|
|
@ -1,20 +1,8 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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 "ft_pbkdf2.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include "ft_sha.h"
|
#include "ft_sha.h"
|
||||||
|
|
||||||
static void set_padding
|
static void set_padding
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx,
|
t_hmac_sha256_ctx *ctx,
|
||||||
unsigned char key[FT_SHA256_BLOCK_SIZE]
|
unsigned char key[FT_SHA256_BLOCK_SIZE]
|
||||||
|
@ -23,7 +11,7 @@ static void set_padding
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_SHA256_BLOCK_SIZE)
|
while(i < FT_SHA256_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
ctx->ipad[i] = key[i] ^ 0x36;
|
ctx->ipad[i] = key[i] ^ 0x36;
|
||||||
ctx->opad[i] = key[i] ^ 0x5c;
|
ctx->opad[i] = key[i] ^ 0x5c;
|
||||||
|
@ -31,7 +19,7 @@ static void set_padding
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_hmac_sha256_finish
|
void ft_hmac_sha256_finish
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx,
|
t_hmac_sha256_ctx *ctx,
|
||||||
unsigned char out[FT_SHA256_DIGEST_LENGTH_BYTE]
|
unsigned char out[FT_SHA256_DIGEST_LENGTH_BYTE]
|
||||||
|
@ -44,7 +32,7 @@ void ft_hmac_sha256_finish
|
||||||
ft_sha256_final(out, &ctx->sha_ctx);
|
ft_sha256_final(out, &ctx->sha_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_hmac_sha256_start
|
void ft_hmac_sha256_start
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx
|
t_hmac_sha256_ctx *ctx
|
||||||
)
|
)
|
||||||
|
@ -65,7 +53,7 @@ void ft_hmac_sha256_start
|
||||||
ft_sha256_update(&ctx->sha_ctx, ctx->ipad, FT_SHA256_BLOCK_SIZE);
|
ft_sha256_update(&ctx->sha_ctx, ctx->ipad, FT_SHA256_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_hmac_sha256_update
|
void ft_hmac_sha256_update
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx,
|
t_hmac_sha256_ctx *ctx,
|
||||||
unsigned char *msg,
|
unsigned char *msg,
|
||||||
|
@ -75,7 +63,7 @@ void ft_hmac_sha256_update
|
||||||
ft_sha256_update(&ctx->sha_ctx, msg, msg_size);
|
ft_sha256_update(&ctx->sha_ctx, msg, msg_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_hmac_sha256
|
void ft_hmac_sha256
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx,
|
t_hmac_sha256_ctx *ctx,
|
||||||
unsigned char out[FT_SHA256_DIGEST_LENGTH_BYTE]
|
unsigned char out[FT_SHA256_DIGEST_LENGTH_BYTE]
|
||||||
|
@ -84,4 +72,4 @@ void ft_hmac_sha256
|
||||||
ft_hmac_sha256_start(ctx);
|
ft_hmac_sha256_start(ctx);
|
||||||
ft_hmac_sha256_update(ctx, ctx->msg, ctx->msg_size);
|
ft_hmac_sha256_update(ctx, ctx->msg, ctx->msg_size);
|
||||||
ft_hmac_sha256_finish(ctx, out);
|
ft_hmac_sha256_finish(ctx, out);
|
||||||
}
|
}
|
|
@ -1,21 +1,9 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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 <stddef.h>
|
||||||
#include "ft_pbkdf2.h"
|
#include "ft_pbkdf2.h"
|
||||||
#include "ft_sha.h"
|
#include "ft_sha.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_hmac_sha256_init_ctx
|
void ft_hmac_sha256_init_ctx
|
||||||
(
|
(
|
||||||
t_hmac_sha256_ctx *ctx
|
t_hmac_sha256_ctx *ctx
|
||||||
)
|
)
|
||||||
|
@ -26,4 +14,4 @@ void ft_hmac_sha256_init_ctx
|
||||||
ctx->msg_size = 0;
|
ctx->msg_size = 0;
|
||||||
ft_bzero(ctx->opad, FT_SHA256_BLOCK_SIZE);
|
ft_bzero(ctx->opad, FT_SHA256_BLOCK_SIZE);
|
||||||
ft_bzero(ctx->ipad, FT_SHA256_BLOCK_SIZE);
|
ft_bzero(ctx->ipad, FT_SHA256_BLOCK_SIZE);
|
||||||
}
|
}
|
|
@ -1,20 +1,8 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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_pbkdf2.h"
|
||||||
#include "ft_sha.h"
|
#include "ft_sha.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static void set_key_and_message
|
static void set_key_and_message
|
||||||
(
|
(
|
||||||
t_pbkdf2_sha256_ctx *ctx,
|
t_pbkdf2_sha256_ctx *ctx,
|
||||||
t_hmac_sha256_ctx *hmac_ctx
|
t_hmac_sha256_ctx *hmac_ctx
|
||||||
|
@ -26,7 +14,7 @@ static void set_key_and_message
|
||||||
hmac_ctx->msg_size = ctx->salt_len;
|
hmac_ctx->msg_size = ctx->salt_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void xor
|
static void xor
|
||||||
(
|
(
|
||||||
unsigned char x[FT_SHA256_DIGEST_LENGTH_BYTE],
|
unsigned char x[FT_SHA256_DIGEST_LENGTH_BYTE],
|
||||||
unsigned char y[FT_SHA256_DIGEST_LENGTH_BYTE],
|
unsigned char y[FT_SHA256_DIGEST_LENGTH_BYTE],
|
||||||
|
@ -36,7 +24,7 @@ static void xor
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
||||||
{
|
{
|
||||||
result[i] = x[i] ^ y[i];
|
result[i] = x[i] ^ y[i];
|
||||||
i++;
|
i++;
|
||||||
|
@ -49,16 +37,17 @@ static int ceil
|
||||||
unsigned int y
|
unsigned int y
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return (1 + ((x - 1) / y));
|
return 1 + ((x - 1) / y);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void block_function
|
static void block_function
|
||||||
(
|
(
|
||||||
t_pbkdf2_sha256_ctx *ctx,
|
t_pbkdf2_sha256_ctx *ctx,
|
||||||
t_hmac_sha256_ctx *hmac_ctx,
|
t_hmac_sha256_ctx *hmac_ctx,
|
||||||
unsigned int iteration,
|
unsigned int iteration,
|
||||||
unsigned char block[FT_SHA256_DIGEST_LENGTH_BYTE]
|
unsigned char block[FT_SHA256_DIGEST_LENGTH_BYTE]
|
||||||
)
|
)
|
||||||
|
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
unsigned char c[4];
|
unsigned char c[4];
|
||||||
|
@ -100,7 +89,7 @@ void ft_pbkdf2_sha256
|
||||||
block_count = ceil(c->key_len, FT_SHA256_DIGEST_LENGTH_BYTE);
|
block_count = ceil(c->key_len, FT_SHA256_DIGEST_LENGTH_BYTE);
|
||||||
i = 1;
|
i = 1;
|
||||||
key_index = 0;
|
key_index = 0;
|
||||||
while (i < block_count)
|
while(i < block_count)
|
||||||
{
|
{
|
||||||
block_function(c, &hmac_ctx, i, block);
|
block_function(c, &hmac_ctx, i, block);
|
||||||
ft_memcpy(c->key + key_index, block, FT_SHA256_DIGEST_LENGTH_BYTE);
|
ft_memcpy(c->key + key_index, block, FT_SHA256_DIGEST_LENGTH_BYTE);
|
||||||
|
@ -108,6 +97,6 @@ void ft_pbkdf2_sha256
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
block_function(c, &hmac_ctx, i, block);
|
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);
|
c->key_len - (block_count - 1) * FT_SHA256_DIGEST_LENGTH_BYTE);
|
||||||
}
|
}
|
|
@ -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 <stddef.h>
|
||||||
#include "ft_pbkdf2.h"
|
#include "ft_pbkdf2.h"
|
||||||
|
|
||||||
|
@ -25,4 +13,4 @@ void ft_pbkdf2_sha256_init_ctx
|
||||||
ctx->key_len = 0;
|
ctx->key_len = 0;
|
||||||
ctx->pass_len = 0;
|
ctx->pass_len = 0;
|
||||||
ctx->salt_len = 0;
|
ctx->salt_len = 0;
|
||||||
}
|
}
|
|
@ -28,8 +28,6 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
|
||||||
i = 1;
|
i = 1;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
if (ft_strcmp("-h", argv[i]) == 0)
|
|
||||||
ft_sha_usage("sha224");
|
|
||||||
if (ft_strcmp("-r", argv[i]) == 0)
|
if (ft_strcmp("-r", argv[i]) == 0)
|
||||||
flags->reverse = 1;
|
flags->reverse = 1;
|
||||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
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 (ft_strcmp("-s", argv[i]) == 0)
|
||||||
{
|
{
|
||||||
if (i + 1 >= argc)
|
if (i + 1 >= argc)
|
||||||
ft_sha_usage("sha224");
|
ft_sha_usage("SHA224");
|
||||||
ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags);
|
ft_sha224_string(ctx, (t_byte1 *)argv[++i], flags);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,6 @@ static int read_flags(int argc, char **argv, t_sha_flags *flags)
|
||||||
i = 1;
|
i = 1;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
if (ft_strcmp("-h", argv[i]) == 0)
|
|
||||||
ft_sha_usage("sha256");
|
|
||||||
if (ft_strcmp("-r", argv[i]) == 0)
|
if (ft_strcmp("-r", argv[i]) == 0)
|
||||||
flags->reverse = 1;
|
flags->reverse = 1;
|
||||||
else if (ft_strcmp("-q", argv[i]) == 0)
|
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 (ft_strcmp("-s", argv[i]) == 0)
|
||||||
{
|
{
|
||||||
if (i + 1 >= argc)
|
if (i + 1 >= argc)
|
||||||
ft_sha_usage("sha256");
|
ft_sha_usage("SHA256");
|
||||||
ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags);
|
ft_sha256_string(ctx, (t_byte1 *)argv[++i], flags);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
void ft_sha_usage(const char *algorithm)
|
void ft_sha_usage(const char *algorithm)
|
||||||
{
|
{
|
||||||
ft_putstr(algorithm);
|
ft_putstr(algorithm);
|
||||||
ft_putstr(" digest usage:\n");
|
ft_putstr(" Digest usage:\n");
|
||||||
ft_putstr("ft_ssl ");
|
ft_putstr("ft_ssl ");
|
||||||
ft_putstr(algorithm);
|
ft_putstr(algorithm);
|
||||||
ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n");
|
ft_putstr(" [-p|-q|-r] [[-s string...] [file...]]\n\n");
|
||||||
|
|
|
@ -12,12 +12,12 @@ abcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
ft_base64_init(&ctx);
|
ft_base64_init(&ctx);
|
||||||
|
|
||||||
_IS(ctx.input_fd == STDIN_FILENO);
|
_is(ctx.input_fd == STDIN_FILENO);
|
||||||
_IS(ctx.output_fd == STDOUT_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()
|
static int transform_block()
|
||||||
|
@ -28,17 +28,17 @@ static int transform_block()
|
||||||
ft_base64_init(&ctx);
|
ft_base64_init(&ctx);
|
||||||
|
|
||||||
ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff);
|
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);
|
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)
|
int base64_tests(void)
|
||||||
{
|
{
|
||||||
_SHOULD(init_ctx);
|
_should(init_ctx);
|
||||||
_SHOULD(transform_block);
|
_should(transform_block);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -1 +0,0 @@
|
||||||
U2FsdGVkX1/+2bai0hAYzG6psw8usaQW
|
|
|
@ -1 +0,0 @@
|
||||||
one deep secret
|
|
|
@ -1 +0,0 @@
|
||||||
Salted__oñîa³(_@Ž Á=_€<ý·©<C2B7>üBúÝr^q
|
|
238
t/des_tests.c
238
t/des_tests.c
|
@ -44,10 +44,10 @@ int perform_initial_permutation()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(initial_permutation[i] == expect[i]);
|
_is(initial_permutation[i] == expect[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("perform initial permutation");
|
_end("perform initial permutation");
|
||||||
}
|
}
|
||||||
|
|
||||||
int perform_final_permutation()
|
int perform_final_permutation()
|
||||||
|
@ -89,13 +89,13 @@ int perform_final_permutation()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(final_permutation[i] == expect[i]);
|
_is(final_permutation[i] == expect[i]);
|
||||||
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] = {
|
t_byte1 message[FT_DES_BIT_BLOCK_SIZE] = {
|
||||||
1, 0, 1, 0, 1, 0, 1, 0,
|
1, 0, 1, 0, 1, 0, 1, 0,
|
||||||
|
@ -119,10 +119,10 @@ int final_permutation_IS_reverse_of_initial()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(final_permutation[i] == message[i]);
|
_is(final_permutation[i] == message[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("final permutation is reverse of initial");
|
_end("final permutation is reverse of initial");
|
||||||
}
|
}
|
||||||
|
|
||||||
int perform_expansion_in_feistel_function()
|
int perform_expansion_in_feistel_function()
|
||||||
|
@ -156,10 +156,10 @@ int perform_expansion_in_feistel_function()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_EXPANDED_HALF_BLOCK_SIZE)
|
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++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("perform expansion in feistel function");
|
_end("perform expansion in feistel function");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int s_box_check
|
static int s_box_check
|
||||||
|
@ -201,7 +201,7 @@ int s_boxes_confuse()
|
||||||
{0, 1, 1, 1},
|
{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] = {
|
t_byte1 s2_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -215,7 +215,7 @@ int s_boxes_confuse()
|
||||||
{0, 1, 0, 1},
|
{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] = {
|
t_byte1 s3_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -229,7 +229,7 @@ int s_boxes_confuse()
|
||||||
{1, 0, 1, 1},
|
{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] = {
|
t_byte1 s4_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -243,7 +243,7 @@ int s_boxes_confuse()
|
||||||
{1, 1, 1, 1},
|
{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] = {
|
t_byte1 s5_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -257,7 +257,7 @@ int s_boxes_confuse()
|
||||||
{0, 0, 1, 1},
|
{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] = {
|
t_byte1 s6_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -271,7 +271,7 @@ int s_boxes_confuse()
|
||||||
{1, 1, 0, 1},
|
{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] = {
|
t_byte1 s7_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -285,7 +285,7 @@ int s_boxes_confuse()
|
||||||
{1, 1, 0, 0},
|
{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] = {
|
t_byte1 s8_inputs[S_BOX_CASES_NUMBER][FT_DES_S_BOX_INPUT_SIZE] = {
|
||||||
{0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
@ -299,9 +299,9 @@ int s_boxes_confuse()
|
||||||
{1, 0, 1, 1},
|
{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()
|
int perform_premutation_in_feistel_function()
|
||||||
|
@ -327,10 +327,10 @@ int perform_premutation_in_feistel_function()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||||
{
|
{
|
||||||
_IS(actual[i] == expected[i]);
|
_is(actual[i] == expected[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should perform permutation in feistel function");
|
_end("should perform permutation in feistel function");
|
||||||
}
|
}
|
||||||
|
|
||||||
int perform_feistel_function()
|
int perform_feistel_function()
|
||||||
|
@ -373,10 +373,10 @@ int perform_feistel_function()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
while(i < FT_DES_BIT_BLOCK_SIZE / 2)
|
||||||
{
|
{
|
||||||
_IS(output[i] == expected[i]);
|
_is(output[i] == expected[i]);
|
||||||
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()
|
int reduce_key_to_56_bits()
|
||||||
|
@ -409,11 +409,11 @@ int reduce_key_to_56_bits()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_REDUCED_KEY_SIZE)
|
while(i < FT_DES_REDUCED_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(actual_reduced[i] == expected_reduced_key[i]);
|
_is(actual_reduced[i] == expected_reduced_key[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
_END("should reduce key size to 56 bits");
|
_end("should reduce key size to 56 bits");
|
||||||
}
|
}
|
||||||
|
|
||||||
int rotate_half_key()
|
int rotate_half_key()
|
||||||
|
@ -442,10 +442,10 @@ int rotate_half_key()
|
||||||
|
|
||||||
while(i < FT_DES_REDUCED_KEY_SIZE / 2)
|
while(i < FT_DES_REDUCED_KEY_SIZE / 2)
|
||||||
{
|
{
|
||||||
_IS(half_key[i] == expected[i]);
|
_is(half_key[i] == expected[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should rotate half of reduced key");
|
_end("should rotate half of reduced key");
|
||||||
}
|
}
|
||||||
|
|
||||||
int derive_round_key()
|
int derive_round_key()
|
||||||
|
@ -492,17 +492,17 @@ int derive_round_key()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_REDUCED_KEY_SIZE)
|
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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_ROUND_KEY_SIZE)
|
while(i < FT_DES_ROUND_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(round_key[i] == expected_round_key[i]);
|
_is(round_key[i] == expected_round_key[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should derive round key");
|
_end("should derive round key");
|
||||||
}
|
}
|
||||||
|
|
||||||
int perform_encryption_round()
|
int perform_encryption_round()
|
||||||
|
@ -550,10 +550,10 @@ int perform_encryption_round()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(initial_permuatation[i] == expected_message[i]);
|
_is(initial_permuatation[i] == expected_message[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should perform encryption round");
|
_end("should perform encryption round");
|
||||||
}
|
}
|
||||||
|
|
||||||
int encrypt_block()
|
int encrypt_block()
|
||||||
|
@ -583,10 +583,10 @@ int encrypt_block()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(cyphertext[i] == expected_cypertext[i]);
|
_is(cyphertext[i] == expected_cypertext[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should encrypt block");
|
_end("should encrypt block");
|
||||||
}
|
}
|
||||||
|
|
||||||
int decrypt_block()
|
int decrypt_block()
|
||||||
|
@ -616,10 +616,10 @@ int decrypt_block()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(plaintext[i] == expected_plaintext[i]);
|
_is(plaintext[i] == expected_plaintext[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should decrypt block");
|
_end("should decrypt block");
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_ctx()
|
int init_ctx()
|
||||||
|
@ -630,9 +630,9 @@ int init_ctx()
|
||||||
ft_des_init_ctx(&ctx);
|
ft_des_init_ctx(&ctx);
|
||||||
|
|
||||||
i = 0;
|
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++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -642,7 +642,7 @@ int init_ctx()
|
||||||
j = 0;
|
j = 0;
|
||||||
while(j < FT_DES_ROUND_KEY_SIZE)
|
while(j < FT_DES_ROUND_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(ctx.round_keys[i][j] == 0);
|
_is(ctx.round_keys[i][j] == 0);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
@ -650,27 +650,30 @@ int init_ctx()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(ctx.buffer[i] == 0);
|
_is(ctx.buffer[i] == 0);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(ctx.iv[i] == 0);
|
_is(ctx.salt[i] == 0);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_IS(ctx.readed == 0);
|
i = 0;
|
||||||
_IS(ctx.input_fd == STDIN_FILENO);
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
_IS(ctx.output_fd == STDOUT_FILENO);
|
{
|
||||||
_IS(ctx.decode == 0);
|
_is(ctx.iv[i] == 0);
|
||||||
_IS(ctx.b64 == 0);
|
i++;
|
||||||
_IS(ctx.raw_password == NULL);
|
}
|
||||||
_IS(ctx.raw_key == NULL);
|
_is(ctx.readed == 0);
|
||||||
_IS(ctx.raw_salt == NULL);
|
_is(ctx.input_fd == STDIN_FILENO);
|
||||||
_IS(ctx.raw_iv == NULL);
|
_is(ctx.output_fd == STDOUT_FILENO);
|
||||||
_IS(ctx.encrypt == NULL);
|
_is(ctx.decode == 0);
|
||||||
_IS(ctx.decrypt == NULL);
|
_is(ctx.b64 == 0);
|
||||||
_END("shoud init ctx");
|
_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()
|
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,
|
wrong_key_char = ft_des_hex_to_bit("FFFFFFFFFFFFFFFF", actual_key,
|
||||||
FT_DES_INITIAL_KEY_SIZE);
|
FT_DES_INITIAL_KEY_SIZE);
|
||||||
_IS(wrong_key_char == NULL);
|
_is(wrong_key_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_INITIAL_KEY_SIZE)
|
while(i < FT_DES_INITIAL_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected_key[i] == actual_key[i]);
|
_is(expected_key[i] == actual_key[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrong_key_char = ft_des_hex_to_bit("ffffffffffffffff", actual_key,
|
wrong_key_char = ft_des_hex_to_bit("ffffffffffffffff", actual_key,
|
||||||
FT_DES_INITIAL_KEY_SIZE);
|
FT_DES_INITIAL_KEY_SIZE);
|
||||||
_IS(wrong_key_char == NULL);
|
_is(wrong_key_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_INITIAL_KEY_SIZE)
|
while(i < FT_DES_INITIAL_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected_key[i] == actual_key[i]);
|
_is(expected_key[i] == actual_key[i]);
|
||||||
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()
|
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);
|
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
|
||||||
wrong_key_char = ft_des_hex_to_bit("FF12CD", actual_key,
|
wrong_key_char = ft_des_hex_to_bit("FF12CD", actual_key,
|
||||||
FT_DES_INITIAL_KEY_SIZE);
|
FT_DES_INITIAL_KEY_SIZE);
|
||||||
_IS(wrong_key_char == NULL);
|
_is(wrong_key_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_INITIAL_KEY_SIZE)
|
while(i < FT_DES_INITIAL_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected_key[i] == actual_key[i]);
|
_is(expected_key[i] == actual_key[i]);
|
||||||
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()
|
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);
|
ft_bzero(actual_key, FT_DES_INITIAL_KEY_SIZE);
|
||||||
wrong_key_char = ft_des_hex_to_bit("FF12CDFF12CDFF12CD", actual_key,
|
wrong_key_char = ft_des_hex_to_bit("FF12CDFF12CDFF12CD", actual_key,
|
||||||
FT_DES_INITIAL_KEY_SIZE);
|
FT_DES_INITIAL_KEY_SIZE);
|
||||||
_IS(wrong_key_char == NULL);
|
_is(wrong_key_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_INITIAL_KEY_SIZE)
|
while(i < FT_DES_INITIAL_KEY_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected_key[i] == actual_key[i]);
|
_is(expected_key[i] == actual_key[i]);
|
||||||
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()
|
int convert_hex_string_to_bytes()
|
||||||
|
@ -784,15 +787,15 @@ int convert_hex_string_to_bytes()
|
||||||
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
|
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
wrong_char = ft_des_hex_to_byte("FFFFFFFFFFFFFFFF", actual,
|
wrong_char = ft_des_hex_to_byte("FFFFFFFFFFFFFFFF", actual,
|
||||||
FT_DES_BYTE_BLOCK_SIZE);
|
FT_DES_BYTE_BLOCK_SIZE);
|
||||||
_IS(wrong_char == NULL);
|
_is(wrong_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected[i] == actual[i]);
|
_is(expected[i] == actual[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should convert hex to 8 byte");
|
_end("should convert hex to 8 byte");
|
||||||
}
|
}
|
||||||
|
|
||||||
int convert_short_hex_string_to_bytes()
|
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);
|
ft_bzero(actual, FT_DES_BYTE_BLOCK_SIZE);
|
||||||
wrong_char = ft_des_hex_to_byte("CC565", actual,
|
wrong_char = ft_des_hex_to_byte("CC565", actual,
|
||||||
FT_DES_BYTE_BLOCK_SIZE);
|
FT_DES_BYTE_BLOCK_SIZE);
|
||||||
_IS(wrong_char == NULL);
|
_is(wrong_char == NULL);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
while(i < FT_DES_BYTE_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected[i] == actual[i]);
|
_is(expected[i] == actual[i]);
|
||||||
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()
|
int convert_bytes_to_bits()
|
||||||
|
@ -844,86 +847,33 @@ int convert_bytes_to_bits()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < FT_DES_BIT_BLOCK_SIZE)
|
while(i < FT_DES_BIT_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(expected[i] == actual[i]);
|
_is(expected[i] == actual[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("should convert 8 bytes to 64 bits");
|
_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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int des_tests()
|
int des_tests()
|
||||||
{
|
{
|
||||||
_SHOULD(perform_initial_permutation);
|
_should(perform_initial_permutation);
|
||||||
_SHOULD(perform_final_permutation);
|
_should(perform_final_permutation);
|
||||||
_SHOULD(final_permutation_IS_reverse_of_initial);
|
_should(final_permutation_is_reverse_of_initial);
|
||||||
_SHOULD(perform_expansion_in_feistel_function);
|
_should(perform_expansion_in_feistel_function);
|
||||||
_SHOULD(s_boxes_confuse);
|
_should(s_boxes_confuse);
|
||||||
_SHOULD(perform_premutation_in_feistel_function);
|
_should(perform_premutation_in_feistel_function);
|
||||||
_SHOULD(perform_feistel_function);
|
_should(perform_feistel_function);
|
||||||
_SHOULD(reduce_key_to_56_bits);
|
_should(reduce_key_to_56_bits);
|
||||||
_SHOULD(rotate_half_key);
|
_should(rotate_half_key);
|
||||||
_SHOULD(derive_round_key);
|
_should(derive_round_key);
|
||||||
_SHOULD(perform_encryption_round);
|
_should(perform_encryption_round);
|
||||||
_SHOULD(encrypt_block);
|
_should(encrypt_block);
|
||||||
_SHOULD(decrypt_block);
|
_should(decrypt_block);
|
||||||
_SHOULD(init_ctx);
|
_should(init_ctx);
|
||||||
_SHOULD(convert_hex_string_to_bits);
|
_should(convert_hex_string_to_bits);
|
||||||
_SHOULD(convert_short_hex_string_to_bits);
|
_should(convert_short_hex_string_to_bits);
|
||||||
_SHOULD(convert_longer_hex_string_to_bits);
|
_should(convert_longer_hex_string_to_bits);
|
||||||
_SHOULD(convert_hex_string_to_bytes);
|
_should(convert_hex_string_to_bytes);
|
||||||
_SHOULD(convert_short_hex_string_to_bytes);
|
_should(convert_short_hex_string_to_bytes);
|
||||||
_SHOULD(convert_bytes_to_bits);
|
_should(convert_bytes_to_bits);
|
||||||
_SHOULD(convert_bits_to_bytes);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -20,15 +20,15 @@ static int init_ctx()
|
||||||
|
|
||||||
ft_md5_init(&ctx);
|
ft_md5_init(&ctx);
|
||||||
|
|
||||||
_IS(ctx.a == 0x67452301);
|
_is(ctx.a == 0x67452301);
|
||||||
_IS(ctx.b == 0xefcdab89);
|
_is(ctx.b == 0xefcdab89);
|
||||||
_IS(ctx.c == 0x98badcfe);
|
_is(ctx.c == 0x98badcfe);
|
||||||
_IS(ctx.d == 0x10325476);
|
_is(ctx.d == 0x10325476);
|
||||||
_IS(ctx.bit_len == 0);
|
_is(ctx.bit_len == 0);
|
||||||
for (int i = 0; i < FT_MD5_BLOCK_SIZE; i++)
|
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()
|
static int decode_string_to_int()
|
||||||
|
@ -43,11 +43,11 @@ static int decode_string_to_int()
|
||||||
|
|
||||||
ft_md5_decode(words, block);
|
ft_md5_decode(words, block);
|
||||||
|
|
||||||
_IS((words[0] & 0xff) == 97);
|
_is((words[0] & 0xff) == 97);
|
||||||
_IS(((words[2] >> 8) & 0xff) == 98);
|
_is(((words[2] >> 8) & 0xff) == 98);
|
||||||
_IS(((words[15] >> 24) & 0xff) == 99);
|
_is(((words[15] >> 24) & 0xff) == 99);
|
||||||
|
|
||||||
_END("decode string to int");
|
_end("decode string to int");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int update_change_count()
|
static int update_change_count()
|
||||||
|
@ -59,9 +59,9 @@ static int update_change_count()
|
||||||
ft_md5_init(&ctx);
|
ft_md5_init(&ctx);
|
||||||
ft_md5_update(&ctx, (t_byte1 *)message, size);
|
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()
|
static int encode_bits_to_string()
|
||||||
|
@ -83,11 +83,11 @@ static int encode_bits_to_string()
|
||||||
|
|
||||||
ft_md5_encode_len(bits, len);
|
ft_md5_encode_len(bits, len);
|
||||||
|
|
||||||
_IS(bits[7] == (t_byte1)((len >> 56) & 0xff));
|
_is(bits[7] == (t_byte1)((len >> 56) & 0xff));
|
||||||
_IS(bits[0] == (t_byte1)(len & 0xff));
|
_is(bits[0] == (t_byte1)(len & 0xff));
|
||||||
_IS(bits[1] == (t_byte1)((len >> 8) & 0xff));
|
_is(bits[1] == (t_byte1)((len >> 8) & 0xff));
|
||||||
|
|
||||||
_END("encode bits to string");
|
_end("encode bits to string");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int encode_register()
|
static int encode_register()
|
||||||
|
@ -105,12 +105,12 @@ static int encode_register()
|
||||||
|
|
||||||
ft_md5_encode_register(digest_part, reg);
|
ft_md5_encode_register(digest_part, reg);
|
||||||
|
|
||||||
_IS(digest_part[0] == (t_byte1)(reg & 0xff));
|
_is(digest_part[0] == (t_byte1)(reg & 0xff));
|
||||||
_IS(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
|
_is(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
|
||||||
_IS(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
|
_is(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
|
||||||
_IS(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
|
_is(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
|
||||||
|
|
||||||
_END("encode register");
|
_end("encode register");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_digest()
|
static int create_digest()
|
||||||
|
@ -129,7 +129,7 @@ static int create_digest()
|
||||||
ft_md5_final(digest, &ctx);
|
ft_md5_final(digest, &ctx);
|
||||||
ft_md5_digest_string(digest, 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);
|
"d41d8cd98f00b204e9800998ecf8427e") == 0);
|
||||||
|
|
||||||
ft_md5_init(&ctx);
|
ft_md5_init(&ctx);
|
||||||
|
@ -137,7 +137,7 @@ static int create_digest()
|
||||||
ft_md5_final(digest, &ctx);
|
ft_md5_final(digest, &ctx);
|
||||||
ft_md5_digest_string(digest, digest_string);
|
ft_md5_digest_string(digest, digest_string);
|
||||||
|
|
||||||
_IS(ft_strcmp((const char *)digest_string,
|
_is(ft_strcmp((const char *)digest_string,
|
||||||
"0cc175b9c0f1b6a831c399e269772661") == 0);
|
"0cc175b9c0f1b6a831c399e269772661") == 0);
|
||||||
|
|
||||||
ft_md5_init(&ctx);
|
ft_md5_init(&ctx);
|
||||||
|
@ -145,10 +145,10 @@ static int create_digest()
|
||||||
ft_md5_final(digest, &ctx);
|
ft_md5_final(digest, &ctx);
|
||||||
ft_md5_digest_string(digest, digest_string);
|
ft_md5_digest_string(digest, digest_string);
|
||||||
|
|
||||||
_IS(ft_strcmp((const char *)digest_string,
|
_is(ft_strcmp((const char *)digest_string,
|
||||||
"2580a0aff7ef5e80f6b5432666530926") == 0);
|
"2580a0aff7ef5e80f6b5432666530926") == 0);
|
||||||
|
|
||||||
_END("create digest");
|
_end("create digest");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_digest_string()
|
static int create_digest_string()
|
||||||
|
@ -173,20 +173,20 @@ static int create_digest_string()
|
||||||
|
|
||||||
ft_md5_digest_string(digest, 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);
|
"d41d8cd98f00b204e9800998ecf8427e") == 0);
|
||||||
|
|
||||||
_END("create digest string");
|
_end("create digest string");
|
||||||
}
|
}
|
||||||
|
|
||||||
int md5_tests()
|
int md5_tests()
|
||||||
{
|
{
|
||||||
_SHOULD(init_ctx);
|
_should(init_ctx);
|
||||||
_SHOULD(decode_string_to_int);
|
_should(decode_string_to_int);
|
||||||
_SHOULD(update_change_count);
|
_should(update_change_count);
|
||||||
_SHOULD(encode_bits_to_string);
|
_should(encode_bits_to_string);
|
||||||
_SHOULD(encode_register);
|
_should(encode_register);
|
||||||
_SHOULD(create_digest);
|
_should(create_digest);
|
||||||
_SHOULD(create_digest_string);
|
_should(create_digest_string);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -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.
|
|
103
t/pbkdf2_tests.c
103
t/pbkdf2_tests.c
|
@ -3,25 +3,24 @@
|
||||||
#include "ft_pbkdf2.h"
|
#include "ft_pbkdf2.h"
|
||||||
#include "ft_sha.h"
|
#include "ft_sha.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include "openssl/evp.h"
|
|
||||||
|
|
||||||
static int init_hmac_sha256_ctx()
|
static int init_hmac_sha256_ctx()
|
||||||
{
|
{
|
||||||
t_hmac_sha256_ctx ctx;
|
t_hmac_sha256_ctx ctx;
|
||||||
|
|
||||||
ft_hmac_sha256_init_ctx(&ctx);
|
ft_hmac_sha256_init_ctx(&ctx);
|
||||||
_IS(ctx.key == NULL);
|
_is(ctx.key == NULL);
|
||||||
_IS(ctx.msg == NULL);
|
_is(ctx.msg == NULL);
|
||||||
_IS(ctx.key_size == 0);
|
_is(ctx.key_size == 0);
|
||||||
_IS(ctx.msg_size == 0);
|
_is(ctx.msg_size == 0);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_SHA256_BLOCK_SIZE)
|
while(i < FT_SHA256_BLOCK_SIZE)
|
||||||
{
|
{
|
||||||
_IS(ctx.opad[i] == 0);
|
_is(ctx.opad[i] == 0);
|
||||||
_IS(ctx.ipad[i] == 0);
|
_is(ctx.ipad[i] == 0);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("init hmac sha256 ctx");
|
_end("init hmac sha256 ctx");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int perform_hmac_256_computation_short_key()
|
static int perform_hmac_256_computation_short_key()
|
||||||
|
@ -53,10 +52,10 @@ static int perform_hmac_256_computation_short_key()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
||||||
{
|
{
|
||||||
_IS(out[i] == expected_result[i]);
|
_is(out[i] == expected_result[i]);
|
||||||
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()
|
static int perform_hmac_256_computation_long_key()
|
||||||
|
@ -120,10 +119,10 @@ static int perform_hmac_256_computation_long_key()
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
while(i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
||||||
{
|
{
|
||||||
_IS(out[i] == expected_result[i]);
|
_is(out[i] == expected_result[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("perform hamc sha256 computation with long key");
|
_end("perform hamc sha256 computation with long key");
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_pbkdf2_sha256_ctx()
|
int init_pbkdf2_sha256_ctx()
|
||||||
|
@ -132,15 +131,15 @@ int init_pbkdf2_sha256_ctx()
|
||||||
|
|
||||||
ft_pbkdf2_sha256_init_ctx(&ctx);
|
ft_pbkdf2_sha256_init_ctx(&ctx);
|
||||||
|
|
||||||
_IS(ctx.iterations == 0);
|
_is(ctx.iterations == 0);
|
||||||
_IS(ctx.key_len == 0);
|
_is(ctx.key_len == 0);
|
||||||
_IS(ctx.salt_len == 0);
|
_is(ctx.salt_len == 0);
|
||||||
_IS(ctx.pass_len == 0);
|
_is(ctx.pass_len == 0);
|
||||||
_IS(ctx.salt == NULL);
|
_is(ctx.salt == NULL);
|
||||||
_IS(ctx.key == NULL);
|
_is(ctx.key == NULL);
|
||||||
_IS(ctx.pass == NULL);
|
_is(ctx.pass == NULL);
|
||||||
|
|
||||||
_END("init pbkdf2 sha256 ctx");
|
_end("init pbkdf2 sha256 ctx");
|
||||||
}
|
}
|
||||||
|
|
||||||
int perform_pbkdf2_sha256()
|
int perform_pbkdf2_sha256()
|
||||||
|
@ -171,7 +170,7 @@ int perform_pbkdf2_sha256()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < 32)
|
while(i < 32)
|
||||||
{
|
{
|
||||||
_IS(ctx.key[i] == expected1[i]);
|
_is(ctx.key[i] == expected1[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,7 +194,7 @@ int perform_pbkdf2_sha256()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < 32)
|
while(i < 32)
|
||||||
{
|
{
|
||||||
_IS(ctx.key[i] == expected2[i]);
|
_is(ctx.key[i] == expected2[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +218,7 @@ int perform_pbkdf2_sha256()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < 32)
|
while(i < 32)
|
||||||
{
|
{
|
||||||
_IS(ctx.key[i] == expected3[i]);
|
_is(ctx.key[i] == expected3[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,62 +246,18 @@ int perform_pbkdf2_sha256()
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < 40)
|
while(i < 40)
|
||||||
{
|
{
|
||||||
_IS(ctx.key[i] == expected4[i]);
|
_is(ctx.key[i] == expected4[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
_END("perform pbkdf2 sha256");
|
_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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int pbkdf2_tests()
|
int pbkdf2_tests()
|
||||||
{
|
{
|
||||||
_SHOULD(init_hmac_sha256_ctx);
|
_should(init_hmac_sha256_ctx);
|
||||||
_SHOULD(perform_hmac_256_computation_short_key);
|
_should(perform_hmac_256_computation_short_key);
|
||||||
_SHOULD(perform_hmac_256_computation_long_key);
|
_should(perform_hmac_256_computation_long_key);
|
||||||
_SHOULD(init_pbkdf2_sha256_ctx);
|
_should(init_pbkdf2_sha256_ctx);
|
||||||
_SHOULD(perform_pbkdf2_sha256);
|
_should(perform_pbkdf2_sha256);
|
||||||
_SHOULD(copy_openssl_pbkdf2);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
144
t/sha_tests.c
144
t/sha_tests.c
|
@ -20,19 +20,19 @@ static int init_sha256_ctx()
|
||||||
|
|
||||||
ft_sha256_init(&ctx);
|
ft_sha256_init(&ctx);
|
||||||
|
|
||||||
_IS(ctx.a == 0x6a09e667);
|
_is(ctx.a == 0x6a09e667);
|
||||||
_IS(ctx.b == 0xbb67ae85);
|
_is(ctx.b == 0xbb67ae85);
|
||||||
_IS(ctx.c == 0x3c6ef372);
|
_is(ctx.c == 0x3c6ef372);
|
||||||
_IS(ctx.d == 0xa54ff53a);
|
_is(ctx.d == 0xa54ff53a);
|
||||||
_IS(ctx.e == 0x510e527f);
|
_is(ctx.e == 0x510e527f);
|
||||||
_IS(ctx.f == 0x9b05688c);
|
_is(ctx.f == 0x9b05688c);
|
||||||
_IS(ctx.g == 0x1f83d9ab);
|
_is(ctx.g == 0x1f83d9ab);
|
||||||
_IS(ctx.h == 0x5be0cd19);
|
_is(ctx.h == 0x5be0cd19);
|
||||||
_IS(ctx.bit_len == 0);
|
_is(ctx.bit_len == 0);
|
||||||
for (int i = 0; i < 64; i++)
|
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()
|
static int init_sha224_ctx()
|
||||||
|
@ -41,19 +41,19 @@ static int init_sha224_ctx()
|
||||||
|
|
||||||
ft_sha224_init(&ctx);
|
ft_sha224_init(&ctx);
|
||||||
|
|
||||||
_IS(ctx.a == 0xc1059ed8);
|
_is(ctx.a == 0xc1059ed8);
|
||||||
_IS(ctx.b == 0x367cd507);
|
_is(ctx.b == 0x367cd507);
|
||||||
_IS(ctx.c == 0x3070dd17);
|
_is(ctx.c == 0x3070dd17);
|
||||||
_IS(ctx.d == 0xf70e5939);
|
_is(ctx.d == 0xf70e5939);
|
||||||
_IS(ctx.e == 0xffc00b31);
|
_is(ctx.e == 0xffc00b31);
|
||||||
_IS(ctx.f == 0x68581511);
|
_is(ctx.f == 0x68581511);
|
||||||
_IS(ctx.g == 0x64f98fa7);
|
_is(ctx.g == 0x64f98fa7);
|
||||||
_IS(ctx.h == 0xbefa4fa4);
|
_is(ctx.h == 0xbefa4fa4);
|
||||||
_IS(ctx.bit_len == 0);
|
_is(ctx.bit_len == 0);
|
||||||
for (int i = 0; i < 64; i++)
|
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()
|
static int decode_string_to_int_sha256()
|
||||||
|
@ -68,9 +68,9 @@ static int decode_string_to_int_sha256()
|
||||||
|
|
||||||
ft_sha256_decode(words, block);
|
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()
|
static int encode_len_to_string_sha256()
|
||||||
|
@ -92,11 +92,11 @@ static int encode_len_to_string_sha256()
|
||||||
|
|
||||||
ft_sha256_encode_len(bits, len);
|
ft_sha256_encode_len(bits, len);
|
||||||
|
|
||||||
_IS(bits[0] == (t_byte1)((len >> 56) & 0xff));
|
_is(bits[0] == (t_byte1)((len >> 56) & 0xff));
|
||||||
_IS(bits[7] == (t_byte1)(len & 0xff));
|
_is(bits[7] == (t_byte1)(len & 0xff));
|
||||||
_IS(bits[6] == (t_byte1)((len >> 8) & 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()
|
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);
|
ft_sha256_encode_register(digest_part, reg);
|
||||||
|
|
||||||
_IS(digest_part[0] == 0xba);
|
_is(digest_part[0] == 0xba);
|
||||||
_IS(digest_part[1] == 0x78);
|
_is(digest_part[1] == 0x78);
|
||||||
_IS(digest_part[2] == 0x16);
|
_is(digest_part[2] == 0x16);
|
||||||
_IS(digest_part[3] == 0xbf);
|
_is(digest_part[3] == 0xbf);
|
||||||
|
|
||||||
_END("encode register to string sha256");
|
_end("encode register to string sha256");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int update_bit_count_sha256()
|
static int update_bit_count_sha256()
|
||||||
|
@ -124,9 +124,9 @@ static int update_bit_count_sha256()
|
||||||
ft_sha256_init(&ctx);
|
ft_sha256_init(&ctx);
|
||||||
ft_sha256_update(&ctx, message, sizeof(message));
|
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()
|
static int fill_buffer_sha256()
|
||||||
|
@ -137,9 +137,9 @@ static int fill_buffer_sha256()
|
||||||
ft_sha256_init(&ctx);
|
ft_sha256_init(&ctx);
|
||||||
ft_sha256_update(&ctx, message, sizeof(message));
|
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
|
static void block_with_right_padding
|
||||||
|
@ -176,10 +176,10 @@ static int add_right_padding_sha256()
|
||||||
|
|
||||||
i = 0;
|
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);
|
FT_SHA256_BLOCK_SIZE) == 0);
|
||||||
|
|
||||||
_END("add right padding sha256");
|
_end("add right padding sha256");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compute_digest_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_update(&ctx, message, ft_strlen((const char *)message));
|
||||||
ft_sha256_final(digest, &ctx);
|
ft_sha256_final(digest, &ctx);
|
||||||
|
|
||||||
_IS(ctx.a == 0xba7816bf);
|
_is(ctx.a == 0xba7816bf);
|
||||||
_IS(ctx.b == 0x8f01cfea);
|
_is(ctx.b == 0x8f01cfea);
|
||||||
_IS(ctx.c == 0x414140de);
|
_is(ctx.c == 0x414140de);
|
||||||
_IS(ctx.d == 0x5dae2223);
|
_is(ctx.d == 0x5dae2223);
|
||||||
_IS(ctx.e == 0xb00361a3);
|
_is(ctx.e == 0xb00361a3);
|
||||||
_IS(ctx.f == 0x96177a9c);
|
_is(ctx.f == 0x96177a9c);
|
||||||
_IS(ctx.g == 0xb410ff61);
|
_is(ctx.g == 0xb410ff61);
|
||||||
_IS(ctx.h == 0xf20015ad);
|
_is(ctx.h == 0xf20015ad);
|
||||||
|
|
||||||
_END("compute digest sha256");
|
_end("compute digest sha256");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compute_digest_sha224()
|
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_update(&ctx, message, ft_strlen((const char *)message));
|
||||||
ft_sha224_final(digest, &ctx);
|
ft_sha224_final(digest, &ctx);
|
||||||
|
|
||||||
_IS(ctx.a == 0x23097d22);
|
_is(ctx.a == 0x23097d22);
|
||||||
_IS(ctx.b == 0x3405d822);
|
_is(ctx.b == 0x3405d822);
|
||||||
_IS(ctx.c == 0x8642a477);
|
_is(ctx.c == 0x8642a477);
|
||||||
_IS(ctx.d == 0xbda255b3);
|
_is(ctx.d == 0xbda255b3);
|
||||||
_IS(ctx.e == 0x2aadbce4);
|
_is(ctx.e == 0x2aadbce4);
|
||||||
_IS(ctx.f == 0xbda0b3f7);
|
_is(ctx.f == 0xbda0b3f7);
|
||||||
_IS(ctx.g == 0xe36c9da7);
|
_is(ctx.g == 0xe36c9da7);
|
||||||
|
|
||||||
_END("computee digst sha224");
|
_end("computee digst sha224");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_digest_string_sha256()
|
static int create_digest_string_sha256()
|
||||||
|
@ -239,10 +239,10 @@ static int create_digest_string_sha256()
|
||||||
ft_sha256_final(digest, &ctx);
|
ft_sha256_final(digest, &ctx);
|
||||||
ft_sha256_digest_string(digest, digest_string);
|
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);
|
(const char *)digest_string) == 0);
|
||||||
|
|
||||||
_END("create digest string sha256");
|
_end("create digest string sha256");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_digest_string_sha224()
|
static int create_digest_string_sha224()
|
||||||
|
@ -259,26 +259,26 @@ static int create_digest_string_sha224()
|
||||||
ft_sha224_final(digest, &ctx);
|
ft_sha224_final(digest, &ctx);
|
||||||
ft_sha224_digest_string(digest, digest_string);
|
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);
|
(const char *)digest_string) == 0);
|
||||||
|
|
||||||
_END("create digest strinf sha224");
|
_end("create digest strinf sha224");
|
||||||
}
|
}
|
||||||
|
|
||||||
int sha_tests()
|
int sha_tests()
|
||||||
{
|
{
|
||||||
_SHOULD(init_sha256_ctx);
|
_should(init_sha256_ctx);
|
||||||
_SHOULD(init_sha224_ctx);
|
_should(init_sha224_ctx);
|
||||||
_SHOULD(decode_string_to_int_sha256);
|
_should(decode_string_to_int_sha256);
|
||||||
_SHOULD(encode_len_to_string_sha256);
|
_should(encode_len_to_string_sha256);
|
||||||
_SHOULD(encode_register_to_string_sha256);
|
_should(encode_register_to_string_sha256);
|
||||||
_SHOULD(update_bit_count_sha256);
|
_should(update_bit_count_sha256);
|
||||||
_SHOULD(fill_buffer_sha256);
|
_should(fill_buffer_sha256);
|
||||||
_SHOULD(add_right_padding_sha256);
|
_should(add_right_padding_sha256);
|
||||||
_SHOULD(compute_digest_sha256);
|
_should(compute_digest_sha256);
|
||||||
_SHOULD(compute_digest_sha224);
|
_should(compute_digest_sha224);
|
||||||
_SHOULD(create_digest_string_sha256);
|
_should(create_digest_string_sha256);
|
||||||
_SHOULD(create_digest_string_sha224);
|
_should(create_digest_string_sha224);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
10
t/tests.c
10
t/tests.c
|
@ -15,11 +15,11 @@
|
||||||
|
|
||||||
int all_tests()
|
int all_tests()
|
||||||
{
|
{
|
||||||
_VERIFY("md5:", md5_tests);
|
_verify("md5:", md5_tests);
|
||||||
_VERIFY("sha:", sha_tests);
|
_verify("sha:", sha_tests);
|
||||||
_VERIFY("base64:", base64_tests);
|
_verify("base64:", base64_tests);
|
||||||
_VERIFY("des:", des_tests);
|
_verify("des:", des_tests);
|
||||||
_VERIFY("pbkdf2:", pbkdf2_tests);
|
_verify("pbkdf2:", pbkdf2_tests);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue