2018-10-23 20:41:44 +03:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* ft_sha.h :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2018/10/23 19:20:23 by gtertysh #+# #+# */
|
2019-02-03 22:05:32 +02:00
|
|
|
/* Updated: 2019/02/03 18:41:48 by gtertysh ### ########.fr */
|
2018-10-23 20:41:44 +03:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2018-10-21 20:46:05 +03:00
|
|
|
#ifndef FT_SHA_H
|
|
|
|
# define FT_SHA_H
|
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
# include <stdint.h>
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-02-03 22:05:32 +02:00
|
|
|
# define FT_SHA256_READ_BLOCK_SIZE 1024
|
2018-10-21 20:46:05 +03:00
|
|
|
# define FT_SHA256_BLOCK_SIZE 64
|
|
|
|
# define FT_SHA256_WORDS_COUNT 16
|
|
|
|
# define FT_SHA256_MESSAGE_LENGTH_BYTE 8
|
|
|
|
# define FT_SHA256_DIGEST_LENGTH_BYTE 32
|
|
|
|
# define FT_SHA256_STRING_SIZE_BYTE 65
|
|
|
|
# define FT_SHA256_REG_SIZE_BYTE 4
|
|
|
|
|
2018-10-22 18:07:47 +03:00
|
|
|
# define FT_SHA224_DIGEST_LENGTH_BYTE 28
|
|
|
|
# define FT_SHA224_STRING_SIZE_BYTE 57
|
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
# define SHR(bits, word) ((word) >> (bits))
|
|
|
|
# define ROTL(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits))))
|
|
|
|
# define ROTR(bits, word) (((word) >> (bits)) | ((word) << (32 - (bits))))
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
# define SHA256_SIGMA0(w) (ROTR(2, w) ^ ROTR(13, w) ^ ROTR(22, w))
|
|
|
|
# define SHA256_SIGMA1(word) (ROTR(6, word) ^ ROTR(11, word) ^ ROTR(25, word))
|
|
|
|
# define SHA256_SIG0(word) (ROTR(7, word) ^ ROTR(18, word) ^ SHR(3, word))
|
|
|
|
# define SHA256_SIG1(word) (ROTR(17, word) ^ ROTR(19, word) ^ SHR(10, word))
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
# define SHA_CH(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
|
|
|
|
# define SHA_MAJ(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
typedef uint64_t t_byte8;
|
|
|
|
typedef uint32_t t_byte4;
|
|
|
|
typedef unsigned char t_byte1;
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
typedef struct s_sha256_ctx
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte4 a;
|
|
|
|
t_byte4 b;
|
|
|
|
t_byte4 c;
|
|
|
|
t_byte4 d;
|
|
|
|
t_byte4 e;
|
|
|
|
t_byte4 f;
|
|
|
|
t_byte4 g;
|
|
|
|
t_byte4 h;
|
|
|
|
t_byte1 block[FT_SHA256_BLOCK_SIZE];
|
|
|
|
t_byte8 bit_len;
|
|
|
|
} t_sha256_ctx;
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
typedef struct s_temp_registers
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte4 a;
|
|
|
|
t_byte4 b;
|
|
|
|
t_byte4 c;
|
|
|
|
t_byte4 d;
|
|
|
|
t_byte4 e;
|
|
|
|
t_byte4 f;
|
|
|
|
t_byte4 g;
|
|
|
|
t_byte4 h;
|
|
|
|
} t_temp_registers;
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-02-03 22:05:32 +02:00
|
|
|
typedef struct s_sha_flags
|
|
|
|
{
|
|
|
|
int quiet;
|
|
|
|
int reverse;
|
|
|
|
int print_stdin;
|
|
|
|
int something_printed;
|
|
|
|
} t_sha_flags;
|
|
|
|
|
|
|
|
void ft_sha256
|
|
|
|
(
|
|
|
|
int argc,
|
|
|
|
char **argv
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_string
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *message,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_file
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *filename,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_stdin
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_init
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_update
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *message,
|
|
|
|
t_byte8 len
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_final
|
|
|
|
(
|
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
|
|
|
t_sha256_ctx *ctx
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_transform
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 block[FT_SHA256_BLOCK_SIZE]
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_decode
|
|
|
|
(
|
|
|
|
t_byte4 w[FT_SHA256_WORDS_COUNT],
|
|
|
|
t_byte1 b[FT_SHA256_BLOCK_SIZE]
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_encode_len
|
|
|
|
(
|
|
|
|
t_byte1 b[FT_SHA256_MESSAGE_LENGTH_BYTE],
|
|
|
|
t_byte8 len
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_encode_register
|
|
|
|
(
|
|
|
|
t_byte1 digest_part[FT_SHA256_REG_SIZE_BYTE],
|
|
|
|
t_byte4 reg
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_padding
|
|
|
|
(
|
|
|
|
t_byte1 padding[FT_SHA256_BLOCK_SIZE]
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha256_digest_string
|
|
|
|
(
|
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
|
|
|
t_byte1 digst_string[FT_SHA256_STRING_SIZE_BYTE]
|
|
|
|
);
|
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte4 *ft_sha256_constants(void);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-02-03 22:05:32 +02:00
|
|
|
void ft_sha224
|
|
|
|
(
|
|
|
|
int argc,
|
|
|
|
char **argv
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_string
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *message,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_file
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *filename,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_stdin
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_init
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_update
|
|
|
|
(
|
|
|
|
t_sha256_ctx *ctx,
|
|
|
|
t_byte1 *message,
|
|
|
|
t_byte8 len
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_final
|
|
|
|
(
|
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
|
|
|
t_sha256_ctx *ctx
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha224_digest_string
|
|
|
|
(
|
|
|
|
t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE],
|
|
|
|
t_byte1 digst_string[FT_SHA224_STRING_SIZE_BYTE]
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha_usage
|
|
|
|
(
|
|
|
|
const char *alorithm
|
|
|
|
);
|
|
|
|
|
|
|
|
void ft_sha_print
|
|
|
|
(
|
|
|
|
const char *algorithm,
|
|
|
|
t_byte1 *message,
|
|
|
|
t_byte1 *digest,
|
|
|
|
t_sha_flags *flags
|
|
|
|
);
|
|
|
|
|
2018-10-23 20:41:44 +03:00
|
|
|
#endif
|