norm formatting done

This commit is contained in:
Gregory 2019-04-10 17:20:22 +03:00
parent 203532eff0
commit 2c3bd2fa7a
83 changed files with 1439 additions and 669 deletions

View file

@ -140,6 +140,7 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_generate_decryption_round_keys.c \ ft_des_generate_decryption_round_keys.c \
ft_des_parse_args.c \ ft_des_parse_args.c \
ft_des_arg_parsers.c \ ft_des_arg_parsers.c \
ft_des_more_arg_parsers.c \
ft_des_usage.c \ ft_des_usage.c \
ft_des.c \ ft_des.c \
ft_des_ecb.c \ ft_des_ecb.c \

View file

@ -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,

View file

@ -33,7 +33,7 @@
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) typedef void (*t_ft_des_mode)
( (
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],
@ -52,7 +52,7 @@ typedef struct s_des_ctx
t_byte1 iv[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 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;
@ -61,16 +61,16 @@ typedef struct s_des_ctx
t_ft_des_mode decrypt; 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;

View file

@ -1,10 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
@ -15,7 +26,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;
@ -65,5 +76,4 @@ void ft_pbkdf2_sha256
t_pbkdf2_sha256_ctx *ctx t_pbkdf2_sha256_ctx *ctx
); );
#endif
#endif

10
inc/t.h
View file

@ -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
View file

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

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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>
@ -14,8 +26,8 @@ static void usage(void)
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)
{ {
@ -28,27 +40,23 @@ 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 init_flags(t_base64_flags *flags) static void read_args
{
flags->decode = 0;
}
static void read_args
( (
int argc, int argc,
char **argv, char **argv,
@ -56,12 +64,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;
@ -86,7 +94,7 @@ void ft_base64(int argc, char **argv)
t_base64_flags flags; t_base64_flags flags;
t_base64_ctx ctx; t_base64_ctx ctx;
init_flags(&flags); flags.decode = 0;
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)
@ -94,4 +102,4 @@ void ft_base64(int argc, char **argv)
else else
ft_base64_encode(&ctx); ft_base64_encode(&ctx);
exit(0); exit(0);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -8,10 +20,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -39,7 +51,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;
@ -49,4 +61,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -12,7 +24,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]))
{ {
@ -22,4 +34,4 @@ t_byte8 ft_base64_decode_filter
i++; i++;
} }
return (last_free_index); return (last_free_index);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -18,4 +30,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);
} }
} }

View file

@ -1,7 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -20,7 +32,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],
@ -39,4 +51,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;
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -33,7 +45,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;
@ -43,4 +55,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -26,4 +38,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);
} }

View file

@ -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/02/02 18:26:51 by gtertysh ### ########.fr */ /* Updated: 2019/04/10 16:12:05 by gtertysh ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,10 +29,8 @@ 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];
} }

View file

@ -1,11 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -8,4 +20,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;
} }

View file

@ -1,7 +1,19 @@
# 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <stdlib.h>
#include "ft_des.h" #include "ft_des.h"
@ -21,4 +33,4 @@ void ft_des
ft_des_encrypt(ctx); ft_des_encrypt(ctx);
} }
exit(0); exit(0);
} }

View file

@ -1,8 +1,15 @@
#include <stddef.h> /* ************************************************************************** */
#include <fcntl.h> /* */
#include <stdio.h> /* ::: :::::::: */
#include <stdlib.h> /* ft_des_arg_parsers.c :+: :+: :+: */
#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
@ -19,7 +26,6 @@ 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,
@ -34,7 +40,6 @@ int ft_des_key_arg_parser
return (position + 2); return (position + 2);
} }
int ft_des_iv_arg_parser int ft_des_iv_arg_parser
( (
int argc, int argc,
@ -50,7 +55,7 @@ type -h for help.");
return (position + 2); return (position + 2);
} }
int ft_des_decode_arg_parser int ft_des_decode_arg_parser
( (
int argc, int argc,
char **argv, char **argv,
@ -64,7 +69,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,
@ -77,89 +82,3 @@ 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);
}
int ft_des_help_arg_parser
(
int argc,
char **argv,
int position,
t_des_ctx *ctx
)
{
(void)argc;
(void)argv;
(void)position;
(void)ctx;
ft_des_usage();
return (position);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "ft_des.h"
void ft_des_bits_to_bytes void ft_des_bits_to_bytes
@ -13,7 +25,7 @@ void ft_des_bits_to_bytes
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)
{ {
bytes[bytes_count] = (bits[bits_count] << 7) | bytes[bytes_count] = (bits[bits_count] << 7) |
(bits[bits_count + 1] << 6) | (bits[bits_count + 1] << 6) |
@ -26,4 +38,4 @@ void ft_des_bits_to_bytes
bytes_count++; bytes_count++;
bits_count += 8; bits_count += 8;
} }
} }

View file

@ -1,7 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <unistd.h>
#include "ft_des.h" #include "ft_des.h"
int ft_des_buffered_read int ft_des_buffered_read
( (
int fd, int fd,
char *buffer, char *buffer,
@ -13,7 +25,7 @@ int ft_des_buffered_read
total_readed = 0; total_readed = 0;
current_read = 0; current_read = 0;
while(total_readed < buff_size) while (total_readed < buff_size)
{ {
if ((current_read = read( if ((current_read = read(
fd, fd,
@ -23,4 +35,4 @@ int ft_des_buffered_read
total_readed += current_read; total_readed += current_read;
} }
return (total_readed); return (total_readed);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -13,7 +25,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;
@ -26,4 +38,4 @@ void ft_des_byte_to_bits
bytes_count++; bytes_count++;
bits_count += 8; bits_count += 8;
} }
} }

View file

@ -1,6 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "ft_des.h"
static void xor static void xor
( (
t_byte1 a[FT_DES_BIT_BLOCK_SIZE], t_byte1 a[FT_DES_BIT_BLOCK_SIZE],
t_byte1 b[FT_DES_BIT_BLOCK_SIZE] t_byte1 b[FT_DES_BIT_BLOCK_SIZE]
@ -9,14 +21,14 @@ static void xor
int i; int i;
i = 0; i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE) while (i < FT_DES_BIT_BLOCK_SIZE)
{ {
a[i] = a[i] ^ b[i]; a[i] = a[i] ^ b[i];
i++; i++;
} }
} }
static void encrypt static void encrypt
( (
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
@ -36,7 +48,7 @@ static void encrypt
FT_DES_BIT_BLOCK_SIZE); FT_DES_BIT_BLOCK_SIZE);
} }
static void decrypt static void decrypt
( (
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],
@ -56,7 +68,7 @@ static void decrypt
FT_DES_BYTE_BLOCK_SIZE); FT_DES_BYTE_BLOCK_SIZE);
} }
void ft_des_cbc void ft_des_cbc
( (
int argc, int argc,
char **argv char **argv
@ -71,4 +83,4 @@ void ft_des_cbc
ctx.encrypt = encrypt; ctx.encrypt = encrypt;
ctx.decrypt = decrypt; ctx.decrypt = decrypt;
ft_des(&ctx); ft_des(&ctx);
} }

View file

@ -1,8 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <unistd.h>
#include "ft_des.h" #include "ft_des.h"
#include "libft.h" #include "libft.h"
static t_byte8 buffered_read static t_byte8 buffered_read
( (
t_des_ctx *ctx, t_des_ctx *ctx,
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE] t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
@ -13,7 +25,7 @@ static t_byte8 buffered_read
total_readed = 0; total_readed = 0;
current_read = 0; current_read = 0;
while(total_readed < FT_DES_BYTE_BLOCK_SIZE) while (total_readed < FT_DES_BYTE_BLOCK_SIZE)
{ {
if ((current_read = read( if ((current_read = read(
ctx->input_fd, ctx->input_fd,
@ -25,7 +37,7 @@ static t_byte8 buffered_read
return (total_readed); return (total_readed);
} }
void ft_des_decrypt void ft_des_decrypt
( (
t_des_ctx *c t_des_ctx *c
) )
@ -38,7 +50,7 @@ void ft_des_decrypt
ft_des_decryption_key_routine(c); ft_des_decryption_key_routine(c);
ft_des_generate_decryption_round_keys(c->key, c->round_keys); ft_des_generate_decryption_round_keys(c->key, c->round_keys);
last_read = 0; last_read = 0;
while((readed = buffered_read(c, buffer))) while ((readed = buffered_read(c, buffer)))
{ {
if (readed != FT_DES_BYTE_BLOCK_SIZE) if (readed != FT_DES_BYTE_BLOCK_SIZE)
ft_des_print_error("wrong message size"); ft_des_print_error("wrong message size");
@ -50,4 +62,4 @@ void ft_des_decrypt
if (message[7] < 0 || message[7] > 8) if (message[7] < 0 || message[7] > 8)
ft_des_print_error("wrong padding"); ft_des_print_error("wrong padding");
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]); write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]);
} }

View file

@ -1,10 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include "ft_des.h" #include "ft_des.h"
#include "ft_base64.h" #include "ft_base64.h"
static void base64_decode static void base64_decode
( (
t_des_ctx *ctx, t_des_ctx *ctx,
int pipe_fd[2] int pipe_fd[2]
@ -30,13 +42,13 @@ static void des_decrypt
ft_des_decrypt(ctx); ft_des_decrypt(ctx);
} }
void ft_des_decrypt_b64 void ft_des_decrypt_b64
( (
t_des_ctx *ctx t_des_ctx *ctx
) )
{ {
pid_t pid; pid_t pid;
int pipe_fd[2]; int pipe_fd[2];
if (pipe(pipe_fd)) if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe"); ft_des_print_error("failded to create pipe");
@ -47,4 +59,4 @@ void ft_des_decrypt_b64
ft_des_print_error("failded to create child process"); ft_des_print_error("failded to create child process");
else else
des_decrypt(ctx, pipe_fd); des_decrypt(ctx, pipe_fd);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include "ft_des.h" #include "ft_des.h"
@ -19,17 +31,18 @@ static void get_salt
ft_des_print_error("bad magic number"); ft_des_print_error("bad magic number");
readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE); readed = ft_des_buffered_read(c->input_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
if (readed != FT_DES_BYTE_BLOCK_SIZE) if (readed != FT_DES_BYTE_BLOCK_SIZE)
ft_des_print_error("error reading input"); ft_des_print_error("error reading input");
ft_memcpy(salt, buffer, FT_DES_BYTE_BLOCK_SIZE); ft_memcpy(salt, buffer, FT_DES_BYTE_BLOCK_SIZE);
} }
void ft_des_decryption_key_routine void ft_des_decryption_key_routine
( (
t_des_ctx *ctx t_des_ctx *ctx
) )
{ {
char salt[FT_DES_BYTE_BLOCK_SIZE]; char salt[FT_DES_BYTE_BLOCK_SIZE];
char pass[128]; char pass[128];
if (ctx->raw_password || !ctx->raw_key) if (ctx->raw_password || !ctx->raw_key)
get_salt(ctx, salt); get_salt(ctx, salt);
if (!ctx->raw_key && !ctx->raw_password) if (!ctx->raw_key && !ctx->raw_password)
@ -39,9 +52,9 @@ void ft_des_decryption_key_routine
} }
if (ctx->raw_password) if (ctx->raw_password)
ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt, ft_des_derive_key_and_iv(ctx->key, ctx->iv, salt,
(char *)ctx->raw_password); (char *)ctx->raw_password);
if (ctx->raw_key) if (ctx->raw_key)
ft_des_set_raw_key(ctx); ft_des_set_raw_key(ctx);
if (ctx->raw_iv) if (ctx->raw_iv)
ft_des_set_raw_iv(ctx); ft_des_set_raw_iv(ctx);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -18,4 +30,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -18,4 +30,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -26,4 +38,4 @@ void ft_des_derive_key_and_iv
FT_DES_BIT_BLOCK_SIZE * 2); FT_DES_BIT_BLOCK_SIZE * 2);
ft_memcpy(key, key_iv_bit, FT_DES_BIT_BLOCK_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); ft_memcpy(iv, key_iv_bit + FT_DES_BIT_BLOCK_SIZE, FT_DES_BIT_BLOCK_SIZE);
} }

View file

@ -1,6 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
static void encrypt static void encrypt
( (
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
@ -25,4 +37,4 @@ void ft_des_ecb
ctx.encrypt = encrypt; ctx.encrypt = encrypt;
ctx.decrypt = encrypt; ctx.decrypt = encrypt;
ft_des(&ctx); ft_des(&ctx);
} }

View file

@ -1,16 +0,0 @@
#include "ft_des.h"
void ft_des_ecb_finish_decrypt
(
t_des_ctx *ctx,
t_des_decrypt_buffer *buff
)
{
t_byte1 idx;
idx = 0;
ctx->readed += buff->readed[buff->current_buffer];
if (ctx->readed % FT_DES_BYTE_BLOCK_SIZE != 0)
ft_des_print_error("wrong message size, maybe corrupted?");
while (idx + FT_DES_BYTE_BLOCK_SIZE <=)
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <unistd.h>
#include "ft_des.h" #include "ft_des.h"
#include "libft.h" #include "libft.h"
@ -9,11 +21,12 @@ static void ft_des_ecb_write
) )
{ {
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ctx->encrypt(buffer, ctx->round_keys, ctx->iv, cyphertext); ctx->encrypt(buffer, ctx->round_keys, ctx->iv, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
} }
void ft_des_encode_process_chunk void ft_des_encode_process_chunk
( (
t_des_ctx *ctx, t_des_ctx *ctx,
t_byte8 readed, t_byte8 readed,
@ -32,7 +45,7 @@ void ft_des_encode_process_chunk
ft_memcpy(&ctx->buffer[buffer_index], buffer, free_space_in_buffer); ft_memcpy(&ctx->buffer[buffer_index], buffer, free_space_in_buffer);
ft_des_ecb_write(ctx->buffer, ctx); ft_des_ecb_write(ctx->buffer, ctx);
idx = free_space_in_buffer; idx = free_space_in_buffer;
while(idx + FT_DES_BYTE_BLOCK_SIZE <= readed) while (idx + FT_DES_BYTE_BLOCK_SIZE <= readed)
{ {
ft_des_ecb_write(&buffer[idx], ctx); ft_des_ecb_write(&buffer[idx], ctx);
idx += FT_DES_BYTE_BLOCK_SIZE; idx += FT_DES_BYTE_BLOCK_SIZE;
@ -42,4 +55,4 @@ void ft_des_encode_process_chunk
else else
idx = 0; idx = 0;
ft_memcpy(&ctx->buffer[buffer_index], &buffer[idx], readed - idx); ft_memcpy(&ctx->buffer[buffer_index], &buffer[idx], readed - idx);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <unistd.h>
#include "ft_des.h" #include "ft_des.h"
#include "ft_base64.h" #include "ft_base64.h"
@ -12,7 +24,7 @@ void ft_des_encrypt
ft_des_encryption_key_routine(ctx); ft_des_encryption_key_routine(ctx);
ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys); ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys);
while((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0) while ((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0)
ft_des_encode_process_chunk(ctx, readed, buffer); ft_des_encode_process_chunk(ctx, readed, buffer);
ft_des_finish_encrypt(ctx); ft_des_finish_encrypt(ctx);
} }

View file

@ -1,10 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include "ft_des.h" #include "ft_des.h"
#include "ft_base64.h" #include "ft_base64.h"
static void base64_encode static void base64_encode
( (
t_des_ctx *ctx, t_des_ctx *ctx,
int pipe_fd[2] int pipe_fd[2]
@ -30,13 +42,13 @@ static void des_encrypt
ft_des_encrypt(ctx); ft_des_encrypt(ctx);
} }
void ft_des_encrypt_b64 void ft_des_encrypt_b64
( (
t_des_ctx *ctx t_des_ctx *ctx
) )
{ {
pid_t pid; pid_t pid;
int pipe_fd[2]; int pipe_fd[2];
if (pipe(pipe_fd)) if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe"); ft_des_print_error("failded to create pipe");
@ -47,4 +59,4 @@ void ft_des_encrypt_b64
ft_des_print_error("failded to create child process"); ft_des_print_error("failded to create child process");
else else
base64_encode(ctx, pipe_fd); base64_encode(ctx, pipe_fd);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include "ft_des.h" #include "ft_des.h"
@ -31,7 +43,7 @@ static void get_salt
} }
} }
void ft_des_encryption_key_routine void ft_des_encryption_key_routine
( (
t_des_ctx *ctx t_des_ctx *ctx
) )
@ -59,4 +71,4 @@ void ft_des_encryption_key_routine
write(ctx->output_fd, "Salted__", 8); write(ctx->output_fd, "Salted__", 8);
write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE); write(ctx->output_fd, salt, FT_DES_BYTE_BLOCK_SIZE);
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,22 +18,17 @@ 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, 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15,
4, 5, 6, 7, 8, 9, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28,
8, 9, 10, 11, 12, 13, 29, 28, 29, 30, 31, 32, 1,
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++;
} }
} }

View file

@ -1,21 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 = 0; int i;
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],
@ -36,4 +50,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);
} }

View file

@ -1,24 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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, 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14,
1, 15, 23, 26, 5, 18, 31, 10, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25,
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++;
} }
}
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,22 +18,18 @@ 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, 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46,
39, 7, 47, 15, 55, 23, 63, 31, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52,
38, 6, 46, 14, 54, 22, 62, 30, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58,
37, 5, 45, 13, 53, 21, 61, 29, 26, 33, 1, 41, 9, 49, 17, 57, 25,
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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 "ft_des.h"
#include <unistd.h> #include <unistd.h>
@ -12,12 +24,11 @@ void ft_des_finish_encrypt
buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE; buffer_index = ctx->readed % FT_DES_BYTE_BLOCK_SIZE;
padding_size = FT_DES_BYTE_BLOCK_SIZE - buffer_index; padding_size = FT_DES_BYTE_BLOCK_SIZE - buffer_index;
while (buffer_index < FT_DES_BYTE_BLOCK_SIZE)
while(buffer_index < FT_DES_BYTE_BLOCK_SIZE)
{ {
ctx->buffer[buffer_index] = padding_size; ctx->buffer[buffer_index] = padding_size;
buffer_index++; buffer_index++;
} }
ctx->encrypt(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext); ctx->encrypt(ctx->buffer, ctx->round_keys, ctx->iv, cyphertext);
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE); write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,14 +18,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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,14 +18,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++;
} }
} }

View file

@ -1,7 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 "ft_des.h"
#include "libft.h" #include "libft.h"
void ft_des_get_password void ft_des_get_password
( (
char pass[128] char pass[128]
) )
@ -18,4 +30,4 @@ void ft_des_get_password
if (ft_strcmp(first_try, second_try) != 0) if (ft_strcmp(first_try, second_try) != 0)
ft_des_print_error("passwords doesn't match"); ft_des_print_error("passwords doesn't match");
ft_memcpy(pass, second_try, 128); ft_memcpy(pass, second_try, 128);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -10,8 +22,9 @@ 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')
@ -28,4 +41,4 @@ const char *ft_des_hex_to_bit
hex++; hex++;
} }
return (NULL); return (NULL);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -29,8 +41,9 @@ 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);
@ -44,4 +57,4 @@ const char *ft_des_hex_to_byte
hex += 2; hex += 2;
} }
return (NULL); return (NULL);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -10,7 +22,7 @@ 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++;
@ -29,4 +41,4 @@ void ft_des_init_ctx
ctx->raw_iv = NULL; ctx->raw_iv = NULL;
ctx->encrypt = NULL; ctx->encrypt = NULL;
ctx->decrypt = NULL; ctx->decrypt = NULL;
} }

View file

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

View file

@ -1,27 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54,
60, 52, 44, 36, 28, 20, 12, 4, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33,
62, 54, 46, 38, 30, 22, 14, 6, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21,
64, 56, 48, 40, 32, 24, 16, 8, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,21 +18,17 @@ 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, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43,
58, 50, 42, 34, 26, 18, 10, 2, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62,
59, 51, 43, 35, 27, 19, 11, 3, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4,
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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -6,20 +18,17 @@ 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, 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16,
15, 6, 21, 10, 23, 19, 12, 4, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44,
26, 8, 16, 7, 27, 20, 13, 2, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
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++;
} }
} }

View file

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

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 <stddef.h>
#include "ft_des.h" #include "ft_des.h"
#include "libft.h" #include "libft.h"
@ -46,32 +58,32 @@ t_des_argument_parser g_arg_parsers[] = {
{ NULL, NULL}, { NULL, NULL},
}; };
void ft_des_parse_args void ft_des_parse_args
( (
int argc, int argc,
char **argv, char **argv,
t_des_ctx *ctx t_des_ctx *ctx
) )
{ {
int i; int i;
char *current_arg; char *current_arg;
t_des_argument_parser *parser_walker; t_des_argument_parser *parser_walker;
i = 1; i = 1;
while(i < argc) while (i < argc)
{ {
current_arg = argv[i]; current_arg = argv[i];
parser_walker = g_arg_parsers; parser_walker = g_arg_parsers;
while(parser_walker->arg_parser) while (parser_walker->arg_parser)
{ {
if (ft_strcmp(current_arg, parser_walker->arg) == 0) if (ft_strcmp(current_arg, parser_walker->arg) == 0)
{ {
i = parser_walker->arg_parser(argc, argv, i, ctx); i = parser_walker->arg_parser(argc, argv, i, ctx);
break; break ;
} }
parser_walker++; parser_walker++;
} }
if (!parser_walker->arg_parser) if (!parser_walker->arg_parser)
ft_des_usage(); ft_des_usage();
} }
} }

View file

@ -1,6 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "ft_des.h"
static void xor static void xor
( (
t_byte1 a[FT_DES_BIT_BLOCK_SIZE], t_byte1 a[FT_DES_BIT_BLOCK_SIZE],
t_byte1 b[FT_DES_BIT_BLOCK_SIZE] t_byte1 b[FT_DES_BIT_BLOCK_SIZE]
@ -9,14 +21,14 @@ static void xor
int i; int i;
i = 0; i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE) while (i < FT_DES_BIT_BLOCK_SIZE)
{ {
a[i] = a[i] ^ b[i]; a[i] = a[i] ^ b[i];
i++; i++;
} }
} }
static void encrypt static void encrypt
( (
t_byte1 input[FT_DES_BYTE_BLOCK_SIZE], t_byte1 input[FT_DES_BYTE_BLOCK_SIZE],
t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE], t_byte1 keys[FT_DES_ROUND_COUNT][FT_DES_ROUND_KEY_SIZE],
@ -38,7 +50,7 @@ static void encrypt
xor(iv, input_bits); xor(iv, input_bits);
} }
static void decrypt static void decrypt
( (
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],
@ -72,4 +84,4 @@ void ft_des_pcbc
ctx.encrypt = encrypt; ctx.encrypt = encrypt;
ctx.decrypt = decrypt; ctx.decrypt = decrypt;
ft_des(&ctx); ft_des(&ctx);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -11,4 +23,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);
} }

View file

@ -1,14 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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];
@ -23,12 +36,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;
@ -49,12 +62,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
@ -69,7 +82,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],
@ -79,12 +92,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]);
@ -95,4 +108,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -11,11 +23,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++;
@ -23,4 +35,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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -11,11 +23,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--;
@ -23,4 +35,4 @@ void ft_des_rotate_half_key_right
half[0] = tmp; half[0] = tmp;
i++; i++;
} }
} }

View file

@ -1,6 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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]
@ -9,14 +21,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],
@ -27,4 +39,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -18,4 +30,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;
} }

View file

@ -1,16 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,10 +19,11 @@ 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, 03, 5, 8}, { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}, { 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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "ft_des.h"
void ft_des_set_raw_iv void ft_des_set_raw_iv
@ -11,4 +23,4 @@ void ft_des_set_raw_iv
FT_DES_BIT_BLOCK_SIZE); FT_DES_BIT_BLOCK_SIZE);
if (wrong_char) if (wrong_char)
ft_des_print_error("wrong char in hex iinitialization vector"); ft_des_print_error("wrong char in hex iinitialization vector");
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "ft_des.h"
void ft_des_set_raw_key void ft_des_set_raw_key
@ -11,4 +23,4 @@ void ft_des_set_raw_key
FT_DES_BIT_BLOCK_SIZE); FT_DES_BIT_BLOCK_SIZE);
if (wrong_char) if (wrong_char)
ft_des_print_error("wrong char in hex key"); ft_des_print_error("wrong char in hex key");
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -7,13 +19,13 @@ void ft_des_switch_message_halves
{ {
int i; int i;
int tmp; int tmp;
i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE / 2) i = 0;
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++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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" #include "libft.h"
void ft_des_usage void ft_des_usage
@ -20,4 +32,4 @@ void ft_des_usage
ft_putstr("where {mode} is one of:\n"); ft_putstr("where {mode} is one of:\n");
ft_putstr("ecb\ncbc\npcbc\n"); ft_putstr("ecb\ncbc\npcbc\n");
exit(1); exit(1);
} }

View file

@ -1,8 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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]
@ -11,7 +23,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;
@ -19,7 +31,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]
@ -32,7 +44,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
) )
@ -53,7 +65,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,
@ -63,7 +75,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]
@ -72,4 +84,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);
} }

View file

@ -1,9 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
) )
@ -14,4 +26,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);
} }

View file

@ -1,8 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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
@ -14,7 +26,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],
@ -24,7 +36,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++;
@ -37,17 +49,16 @@ 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];
@ -89,7 +100,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);
@ -97,6 +108,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);
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
@ -13,4 +25,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;
} }

View file

@ -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;
} }

View file

@ -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()
@ -632,7 +632,7 @@ int init_ctx()
i = 0; i = 0;
while(i < FT_DES_BIT_BLOCK_SIZE) while(i < FT_DES_BIT_BLOCK_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,27 @@ 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.iv[i] == 0);
i++; i++;
} }
_is(ctx.readed == 0); _IS(ctx.readed == 0);
_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(ctx.decode == 0); _IS(ctx.decode == 0);
_is(ctx.b64 == 0); _IS(ctx.b64 == 0);
_is(ctx.raw_password == NULL); _IS(ctx.raw_password == NULL);
_is(ctx.raw_key == NULL); _IS(ctx.raw_key == NULL);
_is(ctx.raw_salt == NULL); _IS(ctx.raw_salt == NULL);
_is(ctx.raw_iv == NULL); _IS(ctx.raw_iv == NULL);
_is(ctx.encrypt == NULL); _IS(ctx.encrypt == NULL);
_is(ctx.decrypt == NULL); _IS(ctx.decrypt == NULL);
_end("shoud init ctx"); _END("shoud init ctx");
} }
int convert_hex_string_to_bits() int convert_hex_string_to_bits()
@ -691,26 +691,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 +731,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 +761,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 +784,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 +807,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,10 +844,10 @@ 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() int convert_bits_to_bytes()
@ -875,7 +875,7 @@ int convert_bits_to_bytes()
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++;
} }
t_byte1 bits1[FT_DES_BIT_BLOCK_SIZE] = { t_byte1 bits1[FT_DES_BIT_BLOCK_SIZE] = {
@ -896,34 +896,34 @@ int convert_bits_to_bytes()
i = 0; i = 0;
while(i < FT_DES_BYTE_BLOCK_SIZE) while(i < FT_DES_BYTE_BLOCK_SIZE)
{ {
_is(expected1[i] == actual[i]); _IS(expected1[i] == actual[i]);
i++; i++;
} }
_end("should convert 64 bits to 8 bytes"); _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); _SHOULD(convert_bits_to_bytes);
return 0; return 0;
} }

View file

@ -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;
} }

View file

@ -10,18 +10,18 @@ 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 +53,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 +120,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 +132,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 +171,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 +195,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 +219,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,10 +247,10 @@ 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() int copy_openssl_pbkdf2()
@ -290,19 +290,19 @@ int copy_openssl_pbkdf2()
i = 0; i = 0;
while(i < 8) while(i < 8)
{ {
_is(my_key[i] == orig_key[i]); _IS(my_key[i] == orig_key[i]);
i++; i++;
} }
_end("copy openssl pbkdf2"); _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); _SHOULD(copy_openssl_pbkdf2);
return 0; return 0;
} }

View file

@ -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;
} }

View file

@ -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;
} }