diff --git a/inc/ft_md5.h b/inc/ft_md5.h index 9080b38e..6d5bf038 100644 --- a/inc/ft_md5.h +++ b/inc/ft_md5.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:11:58 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:12:43 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef FT_MD5_H # define FT_MD5_H @@ -26,77 +38,79 @@ # define S43 15 # define S44 21 -/* F, G, H and I are basic MD5 functions. - */ # define F(x, y, z) (((x) & (y)) | ((~x) & (z))) # define G(x, y, z) (((x) & (z)) | ((y) & (~z))) # define H(x, y, z) ((x) ^ (y) ^ (z)) # define I(x, y, z) ((y) ^ ((x) | (~z))) -/* ROTATE_LEFT rotates x left n bits. - */ -# define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +# define RL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) -/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. -Rotation is separate from addition to prevent recomputation. - */ -# define FF(a, b, c, d, x, s, ac) \ - { \ - (a) += F((b), (c), (d)) + (x) + (BYTE4)(ac); \ - (a) = ROTATE_LEFT((a), (s)); \ - (a) += (b); \ - } +# define FF1(a, b, c, d, x, ac) (a) += F((b), (c), (d)) + (x) + (t_byte4)(ac) +# define FF2(a, s) (a) = RL((a), (s)) +# define FF3(a, b) (a) += (b) -# define GG(a, b, c, d, x, s, ac) \ - { \ - (a) += G((b), (c), (d)) + (x) + (BYTE4)(ac); \ - (a) = ROTATE_LEFT((a), (s)); \ - (a) += (b); \ - } +# define FF(a, b, c, d, x, s, ac) FF1(a, b, c, d, x, ac); FF2(a, s); FF3(a, b); -# define HH(a, b, c, d, x, s, ac) \ - { \ - (a) += H((b), (c), (d)) + (x) + (BYTE4)(ac); \ - (a) = ROTATE_LEFT((a), (s)); \ - (a) += (b); \ - } +# define GG1(a, b, c, d, x, ac) (a) += G((b), (c), (d)) + (x) + (t_byte4)(ac) +# define GG2(a, s) (a) = RL((a), (s)) +# define GG3(a, b) (a) += (b) -# define II(a, b, c, d, x, s, ac) \ - { \ - (a) += I((b), (c), (d)) + (x) + (BYTE4)(ac); \ - (a) = ROTATE_LEFT((a), (s)); \ - (a) += (b); \ - } +# define GG(a, b, c, d, x, s, ac) GG1(a, b, c, d, x, ac); GG2(a, s); GG3(a, b); -typedef uint64_t BYTE8; -typedef uint32_t BYTE4; -typedef unsigned char BYTE1; +# define HH1(a, b, c, d, x, ac) (a) += H((b), (c), (d)) + (x) + (t_byte4)(ac) +# define HH2(a, s) (a) = RL((a), (s)) +# define HH3(a, b) (a) += (b) -typedef struct s_md5_ctx +# define HH(a, b, c, d, x, s, ac) HH1(a, b, c, d, x, ac); HH2(a, s); HH3(a, b); + +# define II1(a, b, c, d, x, ac) (a) += I((b), (c), (d)) + (x) + (t_byte4)(ac) +# define II2(a, s) (a) = RL((a), (s)) +# define II3(a, b) (a) += (b) + +# define II(a, b, c, d, x, s, ac) II1(a, b, c, d, x, ac); II2(a, s); II3(a, b); + +typedef uint64_t t_byte8; +typedef uint32_t t_byte4; +typedef unsigned char t_byte1; + +typedef struct s_md5_ctx { - BYTE4 a; - BYTE4 b; - BYTE4 c; - BYTE4 d; - BYTE1 block[FT_MD5_BLOCK_SIZE]; - BYTE8 bit_len; -} t_md5_ctx; + t_byte4 a; + t_byte4 b; + t_byte4 c; + t_byte4 d; + t_byte1 block[FT_MD5_BLOCK_SIZE]; + t_byte8 bit_len; +} t_md5_ctx; -void ft_md5_init(t_md5_ctx *ctx); -void ft_md5_update(t_md5_ctx *ctx, BYTE1 *chunk, BYTE8 len); -void ft_md5_transform(t_md5_ctx *ctx, BYTE1 blck[FT_MD5_BLOCK_SIZE]); -void ft_md5_final(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], - t_md5_ctx *ctx); -void ft_md5_decode(BYTE4 w[FT_MD5_WORDS_COUNT], - BYTE1 b[FT_MD5_BLOCK_SIZE]); -void ft_md5_encode_len(BYTE1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], - BYTE8 bit_len); -void ft_md5_encode_register(BYTE1 *digest_part, BYTE4 reg); -void ft_md5_padding(BYTE1 padding[FT_MD5_BLOCK_SIZE]); -void ft_md5_digest_string(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], - BYTE1 string[FT_MD5_STRING_SIZE_BYTE]); -void ft_md5_string(BYTE1 *str); -void ft_md5_file(BYTE1 *filename); -void ft_md5_filter(void); +typedef struct s_md5_intr_registers +{ + t_byte4 a; + t_byte4 b; + t_byte4 c; + t_byte4 d; +} t_md5_intr_registers; + +void ft_md5_init(t_md5_ctx *ctx); +void ft_md5_update(t_md5_ctx *ctx, t_byte1 *chunk, + t_byte8 len); +void ft_md5_transform(t_md5_ctx *ctx, + t_byte1 blck[FT_MD5_BLOCK_SIZE]); +void ft_md5_final(t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], + t_md5_ctx *ctx); +void ft_md5_decode(t_byte4 w[FT_MD5_WORDS_COUNT], + t_byte1 b[FT_MD5_BLOCK_SIZE]); +void ft_md5_encode_len( + t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], + t_byte8 bit_len); +void ft_md5_encode_register(t_byte1 *digest_part, + t_byte4 reg); +void ft_md5_padding(t_byte1 padding[FT_MD5_BLOCK_SIZE]); +void ft_md5_digest_string( + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], + t_byte1 string[FT_MD5_STRING_SIZE_BYTE]); +void ft_md5_string(t_byte1 *str); +void ft_md5_file(t_byte1 *filename); +void ft_md5_filter(void); #endif diff --git a/inc/ft_sha.h b/inc/ft_sha.h index a7b69fcc..c37975ec 100644 --- a/inc/ft_sha.h +++ b/inc/ft_sha.h @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:20:23 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:29:10 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef FT_SHA_H # define FT_SHA_H -#include +# include # define FT_SHA256_BLOCK_SIZE 64 # define FT_SHA256_WORDS_COUNT 16 @@ -13,75 +25,78 @@ # define FT_SHA224_DIGEST_LENGTH_BYTE 28 # define FT_SHA224_STRING_SIZE_BYTE 57 -/* Define the SHA shift, rotate left, and rotate right macros */ -#define SHR(bits, word) ((word) >> (bits)) -#define ROTL(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) -#define ROTR(bits, word) (((word) >> (bits)) | ((word) << (32 - (bits)))) +# define SHR(bits, word) ((word) >> (bits)) +# define ROTL(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) +# define ROTR(bits, word) (((word) >> (bits)) | ((word) << (32 - (bits)))) -/* Define the SHA SIGMA and sigma macros */ -#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_sigma0(word) (ROTR(7, word) ^ ROTR(18, word) ^ SHR(3, word)) -#define SHA256_sigma1(word) (ROTR(17, word) ^ ROTR(19, word) ^ SHR(10, word)) +# 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)) -#define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z)) -#define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) +# define SHA_CH(x, y, z) (((x) & ((y) ^ (z))) ^ (z)) +# define SHA_MAJ(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) -typedef uint64_t BYTE8; -typedef uint32_t BYTE4; -typedef unsigned char BYTE1; +typedef uint64_t t_byte8; +typedef uint32_t t_byte4; +typedef unsigned char t_byte1; -typedef struct s_sha256_ctx +typedef struct s_sha256_ctx { - BYTE4 a; - BYTE4 b; - BYTE4 c; - BYTE4 d; - BYTE4 e; - BYTE4 f; - BYTE4 g; - BYTE4 h; - BYTE1 block[FT_SHA256_BLOCK_SIZE]; - BYTE8 bit_len; -} t_sha256_ctx; + 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; -typedef struct s_temp_registers +typedef struct s_temp_registers { - BYTE4 a; - BYTE4 b; - BYTE4 c; - BYTE4 d; - BYTE4 e; - BYTE4 f; - BYTE4 g; - BYTE4 h; -} t_temp_registers; + 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; -void ft_sha256_init(t_sha256_ctx *ctx); -void ft_sha224_init(t_sha256_ctx *ctx); -void ft_sha256_update(t_sha256_ctx *ctx, - BYTE1 *message, BYTE8 len); -void ft_sha224_update(t_sha256_ctx *ctx, - BYTE1 *message, BYTE8 len); -void ft_sha256_final(BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - t_sha256_ctx *ctx); -void ft_sha224_final(BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - t_sha256_ctx *ctx); -void ft_sha256_transform(t_sha256_ctx *ctx, - BYTE1 block[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_decode(BYTE4 w[FT_SHA256_WORDS_COUNT], - BYTE1 b[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_encode_len(BYTE1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], - BYTE8 len); -void ft_sha256_encode_register( - BYTE1 digest_part[FT_SHA256_REG_SIZE_BYTE], BYTE4 reg); -void ft_sha256_padding(BYTE1 padding[FT_SHA256_BLOCK_SIZE]); -void ft_sha256_digest_string( - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - BYTE1 digst_string[FT_SHA256_STRING_SIZE_BYTE]); -void ft_sha224_digest_string( - BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], - BYTE1 digst_string[FT_SHA224_STRING_SIZE_BYTE]); -BYTE4 *ft_sha256_constants(void); +void ft_sha256_init(t_sha256_ctx *ctx); +void ft_sha224_init(t_sha256_ctx *ctx); +void ft_sha256_update(t_sha256_ctx *ctx, + t_byte1 *message, t_byte8 len); +void ft_sha224_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_sha224_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]); +void ft_sha224_digest_string( + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], + t_byte1 digst_string[FT_SHA224_STRING_SIZE_BYTE]); +t_byte4 *ft_sha256_constants(void); -#endif \ No newline at end of file +#endif diff --git a/inc/ft_ssl.h b/inc/ft_ssl.h index 7d9f1111..32c019b6 100644 --- a/inc/ft_ssl.h +++ b/inc/ft_ssl.h @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/10/22 20:07:36 by gtertysh #+# #+# */ -/* Updated: 2018/10/22 20:15:49 by gtertysh ### ########.fr */ +/* Updated: 2018/10/23 19:36:23 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,26 +56,26 @@ void ft_ssl_md5_string(const char *strng, t_ft_ssl *ft_ssl); void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl); -void ft_ssl_md5_print(const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl); +void ft_ssl_md5_print(const char *target, + unsigned char *digest, + t_ft_ssl *ft_ssl); -void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl); +void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl); void ft_ssl_sha256_string(const char *strng, t_ft_ssl *ft_ssl); void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl); void ft_ssl_sha224_print(const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl); + unsigned char *digest, + t_ft_ssl *ft_ssl); -void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl); +void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl); void ft_ssl_sha224_string(const char *strng, t_ft_ssl *ft_ssl); void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl); void ft_ssl_sha256_print(const char *target, - unsigned char *digest, - t_ft_ssl *ft_ssl); + unsigned char *digest, + t_ft_ssl *ft_ssl); #endif diff --git a/inc/tests.h b/inc/tests.h index 8c601a8a..00c2f9f1 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -1,31 +1,58 @@ -#ifndef TESTS -# define TESTS +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:36:58 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:40:12 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ -#define MUNIT_ENABLE_ASSERT_ALIASES -#include "munit.h" -#include "tests_macros.h" +#ifndef TESTS_H +# define TESTS_H -// md5 tests -CASE(should_init_ctx); -CASE(update_should_change_count); -CASE(decode_string_to_int); -CASE(encode_bits_to_string); -CASE(encode_register); -CASE(create_digest); -CASE(create_string); +# define MUNIT_ENABLE_ASSERT_ALIASES +# include "munit.h" -// sha tests -CASE(should_init_ctx_sha256); -CASE(decode_string_to_int_sha256); -CASE(encode_len_to_string_sha256); -CASE(encode_register_to_string_sha256); -CASE(update_bit_count_sha256); -CASE(fill_buffer_sha256); -CASE(add_right_padding_sha256); -CASE(compute_digest_sha256); -CASE(create_digest_string_sha256); -CASE(should_init_ctx_sha224); -CASE(compute_digest_sha224); -CASE(create_digest_string_sha224); +MunitResult should_init_ctx(const MunitParameter test_params[], + void *test_data); +MunitResult update_should_change_count(const MunitParameter test_params[], + void *test_data); +MunitResult decode_string_to_int(const MunitParameter test_params[], + void *test_data); +MunitResult encode_bits_to_string(const MunitParameter test_params[], + void *test_data); +MunitResult encode_register(const MunitParameter test_params[], + void *test_data); +MunitResult create_digest(const MunitParameter test_params[], + void *test_data); +MunitResult create_string(const MunitParameter test_params[], + void *test_data); +MunitResult should_init_ctx_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult decode_string_to_int_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult encode_len_to_string_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult encode_register_to_string_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult update_bit_count_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult fill_buffer_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult add_right_padding_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult compute_digest_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult create_digest_string_sha256(const MunitParameter test_params[], + void *test_data); +MunitResult should_init_ctx_sha224(const MunitParameter test_params[], + void *test_data); +MunitResult compute_digest_sha224(const MunitParameter test_params[], + void *test_data); +MunitResult create_digest_string_sha224(const MunitParameter test_params[], + void *test_data); -#endif \ No newline at end of file +#endif diff --git a/inc/tests_macros.h b/inc/tests_macros.h index 17763793..583061c8 100644 --- a/inc/tests_macros.h +++ b/inc/tests_macros.h @@ -1,22 +1,34 @@ -#ifndef TESTS_MACROS -# define TESTS_MACROS +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_macros.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:46:23 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:46:33 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ -#include "munit.h" +#ifndef TESTS_MACROS_H +# define TESTS_MACROS_H -#define UNUSED(x) (void)(x) +# include "munit.h" -#define TEST_RESULT MunitResult -#define TEST_PARAMS const MunitParameter test_params[] -#define TEST_DATA void *test_data -#define CASE(f) TEST_RESULT f(TEST_PARAMS, TEST_DATA) +# define UNUSED(x) (void)(x) -#define N(name) (char *)name -#define T(test_function) test_function -#define S(setup_function) setup_function -#define TD(tear_down_function) tear_down_function -#define O(options) options -#define P(paramenters) paramenters -#define IT(n, t, s, td, o, p) { N(n), T(t), S(s), TD(td), O(o), P(p) } -#define END_IT IT(NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL) +# define TEST_RESULT MunitResult +# define TEST_PARAMS const MunitParameter test_params[] +# define TEST_DATA void *test_data +# define CASE(f) TEST_RESULT f(TEST_PARAMS, TEST_DATA) -#endif \ No newline at end of file +# define N(name) (char *)name +# define T(test_function) test_function +# define S(setup_function) setup_function +# define TD(tear_down_function) tear_down_function +# define O(options) options +# define P(paramenters) paramenters +# define IT(n, t, s, td, o, p) { N(n), T(t), S(s), TD(td), O(o), P(p) } +# define END_IT IT(NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL) + +#endif diff --git a/src/ft_ssl_init.c b/src/ft_ssl_init.c index a6b327bb..b8f66705 100644 --- a/src/ft_ssl_init.c +++ b/src/ft_ssl_init.c @@ -1,29 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:21:21 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:28:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "libft.h" static t_algorithm *init_algorithms(void) { static t_algorithm algs[FT_SSL_ALGS_COUNT] = { - { - "md5", - ft_ssl_md5_stdin, - ft_ssl_md5_file, - ft_ssl_md5_string, - }, - { - "sha256", - ft_ssl_sha256_stdin, - ft_ssl_sha256_file, - ft_ssl_sha256_string, - }, - { - "sha224", - ft_ssl_sha224_stdin, - ft_ssl_sha224_file, - ft_ssl_sha224_string, - } + {"md5", ft_ssl_md5_stdin, ft_ssl_md5_file, ft_ssl_md5_string}, + {"sha256", ft_ssl_sha256_stdin, ft_ssl_sha256_file, ft_ssl_sha256_string}, + {"sha224", ft_ssl_sha224_stdin, ft_ssl_sha224_file, ft_ssl_sha224_string} }; - return algs; + + return (algs); } static void init_flags(t_ft_ssl *ft_ssl) @@ -43,13 +41,12 @@ static void set_algorithm(t_algorithm *alg, t_ft_ssl *ft_ssl) void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl) { - int i; - t_algorithm *alg; - t_algorithm *alg_walker; + int i; + t_algorithm *alg; + t_algorithm *alg_walker; i = 0; alg = 0; - init_flags(ft_ssl); alg_walker = init_algorithms(); while (i < FT_SSL_ALGS_COUNT) @@ -57,11 +54,11 @@ void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl) if (ft_strcmp(alg_name, alg_walker[i].name) == 0) { alg = &alg_walker[i]; - break; + break ; } i++; } if (!alg) ft_ssl_usage(); set_algorithm(alg, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_md5_file.c b/src/ft_ssl_md5_file.c index 67d2e73e..c522645a 100644 --- a/src/ft_ssl_md5_file.c +++ b/src/ft_ssl_md5_file.c @@ -1,20 +1,32 @@ -#include "ft_ssl.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_md5_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:30:28 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:30:30 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "fcntl.h" #include "stdio.h" #include "unistd.h" #include "libft.h" +#include "ft_ssl.h" void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl) { int fd; int len; - BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; t_md5_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; - if((fd = open(filename, O_RDONLY)) == -1) + if ((fd = open(filename, O_RDONLY)) == -1) { perror("Error: "); exit(1); @@ -24,4 +36,4 @@ void ft_ssl_md5_file(const char *filename, t_ft_ssl *ft_ssl) ft_md5_update(&ctx, buf, len); ft_md5_final(digest, &ctx); ft_ssl_md5_print(filename, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_md5_print.c b/src/ft_ssl_md5_print.c index cd136fb8..94ad183d 100644 --- a/src/ft_ssl_md5_print.c +++ b/src/ft_ssl_md5_print.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_md5_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:28:46 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:28:58 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "ft_ssl.h" #include "libft.h" -static void print_name(const char *name, int reverse) +static void print_name(const char *name, int reverse) { if (reverse) { @@ -17,14 +29,14 @@ static void print_name(const char *name, int reverse) } } -void ft_ssl_md5_print +void ft_ssl_md5_print ( const char *target, unsigned char *digest, t_ft_ssl *ft_ssl ) { - BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE]; + t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE]; ft_md5_digest_string(digest, digest_string); if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) @@ -41,4 +53,4 @@ void ft_ssl_md5_print ft_putstr((const char *)digest_string); ft_putstr("\n"); ft_ssl->flags.something_printed = 1; -} \ No newline at end of file +} diff --git a/src/ft_ssl_md5_stdin.c b/src/ft_ssl_md5_stdin.c index 4444aa1a..1d821497 100644 --- a/src/ft_ssl_md5_stdin.c +++ b/src/ft_ssl_md5_stdin.c @@ -1,15 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_md5_stdin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:29:11 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:29:38 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_md5.h" #include "fcntl.h" #include "stdio.h" #include "unistd.h" -void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) +void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) { - int len; - BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; - t_md5_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + int len; + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_md5_ctx ctx; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; ft_md5_init(&ctx); @@ -21,4 +33,4 @@ void ft_ssl_md5_stdin(t_ft_ssl *ft_ssl) } ft_md5_final(digest, &ctx); ft_ssl_md5_print(NULL, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_md5_string.c b/src/ft_ssl_md5_string.c index 12e3f806..792422cf 100644 --- a/src/ft_ssl_md5_string.c +++ b/src/ft_ssl_md5_string.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_md5_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:29:48 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:30:06 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_md5.h" #include "fcntl.h" @@ -5,13 +17,13 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl) +void ft_ssl_md5_string(const char *str, t_ft_ssl *ft_ssl) { - BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; - t_md5_ctx ctx; + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_md5_ctx ctx; ft_md5_init(&ctx); ft_md5_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_md5_final(digest, &ctx); ft_ssl_md5_print(str, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha224_file.c b/src/ft_ssl_sha224_file.c index c741650e..b72e1c42 100644 --- a/src/ft_ssl_sha224_file.c +++ b/src/ft_ssl_sha224_file.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha224_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:30:48 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:31:18 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,13 +17,13 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl) +void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl) { - int fd; - int len; - BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + int fd; + int len; + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; if ((fd = open(filename, O_RDONLY)) == -1) @@ -24,4 +36,4 @@ void ft_ssl_sha224_file(const char *filename, t_ft_ssl *ft_ssl) ft_sha224_update(&ctx, buf, len); ft_sha224_final(digest, &ctx); ft_ssl_sha224_print(filename, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha224_print.c b/src/ft_ssl_sha224_print.c index e2730df3..03e2ebae 100644 --- a/src/ft_ssl_sha224_print.c +++ b/src/ft_ssl_sha224_print.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha224_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:31:27 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:31:42 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "ft_ssl.h" #include "libft.h" -static void print_name(const char *name, int reverse) +static void print_name(const char *name, int reverse) { if (reverse) { @@ -17,14 +29,14 @@ static void print_name(const char *name, int reverse) } } -void ft_ssl_sha224_print +void ft_ssl_sha224_print ( const char *target, unsigned char *digest, t_ft_ssl *ft_ssl ) { - BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; + t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; ft_sha224_digest_string(digest, digest_string); if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) @@ -41,4 +53,4 @@ void ft_ssl_sha224_print ft_putstr((const char *)digest_string); ft_putstr("\n"); ft_ssl->flags.something_printed = 1; -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha224_stdin.c b/src/ft_ssl_sha224_stdin.c index 0ab15765..b2fba97b 100644 --- a/src/ft_ssl_sha224_stdin.c +++ b/src/ft_ssl_sha224_stdin.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha224_stdin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:31:54 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:32:14 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,12 +17,12 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl) +void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl) { - int len; - BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + int len; + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; ft_sha224_init(&ctx); @@ -22,4 +34,4 @@ void ft_ssl_sha224_stdin(t_ft_ssl *ft_ssl) } ft_sha224_final(digest, &ctx); ft_ssl_sha224_print(NULL, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha224_string.c b/src/ft_ssl_sha224_string.c index ae547558..44bb90b1 100644 --- a/src/ft_ssl_sha224_string.c +++ b/src/ft_ssl_sha224_string.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha224_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:32:29 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:32:48 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,14 +17,14 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha224_string(const char *str, t_ft_ssl *ft_ssl) +void ft_ssl_sha224_string(const char *str, t_ft_ssl *ft_ssl) { - BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; (void)ft_ssl; ft_sha224_init(&ctx); ft_sha224_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_sha224_final(digest, &ctx); ft_ssl_sha224_print(str, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha256_file.c b/src/ft_ssl_sha256_file.c index cf6ab7f3..fd7f103a 100644 --- a/src/ft_ssl_sha256_file.c +++ b/src/ft_ssl_sha256_file.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha256_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:33:03 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:33:23 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,13 +17,13 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl) +void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl) { - int fd; - int len; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + int fd; + int len; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; if ((fd = open(filename, O_RDONLY)) == -1) @@ -24,4 +36,4 @@ void ft_ssl_sha256_file(const char *filename, t_ft_ssl *ft_ssl) ft_sha256_update(&ctx, buf, len); ft_sha256_final(digest, &ctx); ft_ssl_sha256_print(filename, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha256_print.c b/src/ft_ssl_sha256_print.c index 98a96579..c9b5804a 100644 --- a/src/ft_ssl_sha256_print.c +++ b/src/ft_ssl_sha256_print.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha256_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:33:33 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:33:43 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "ft_ssl.h" #include "libft.h" -static void print_name(const char *name, int reverse) +static void print_name(const char *name, int reverse) { if (reverse) { @@ -17,14 +29,14 @@ static void print_name(const char *name, int reverse) } } -void ft_ssl_sha256_print +void ft_ssl_sha256_print ( const char *target, unsigned char *digest, t_ft_ssl *ft_ssl ) { - BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; + t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; ft_sha256_digest_string(digest, digest_string); if (target && !ft_ssl->flags.quiet && ft_ssl->flags.reverse) @@ -41,4 +53,4 @@ void ft_ssl_sha256_print ft_putstr((const char *)digest_string); ft_putstr("\n"); ft_ssl->flags.something_printed = 1; -} \ No newline at end of file +} diff --git a/src/ft_ssl_sha256_stdin.c b/src/ft_ssl_sha256_stdin.c index 94c9a570..0f3ff63f 100644 --- a/src/ft_ssl_sha256_stdin.c +++ b/src/ft_ssl_sha256_stdin.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha256_stdin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:34:09 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:34:32 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,12 +17,12 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl) +void ft_ssl_sha256_stdin(t_ft_ssl *ft_ssl) { - int len; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; - BYTE1 buf[FT_SSL_BUFFER_SIZE]; + int len; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; + t_byte1 buf[FT_SSL_BUFFER_SIZE]; (void)ft_ssl; ft_sha256_init(&ctx); diff --git a/src/ft_ssl_sha256_string.c b/src/ft_ssl_sha256_string.c index c0126d27..8fd80c93 100644 --- a/src/ft_ssl_sha256_string.c +++ b/src/ft_ssl_sha256_string.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_sha256_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:34:49 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:34:59 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "ft_sha.h" #include "fcntl.h" @@ -5,14 +17,14 @@ #include "unistd.h" #include "libft.h" -void ft_ssl_sha256_string(const char *str, t_ft_ssl *ft_ssl) +void ft_ssl_sha256_string(const char *str, t_ft_ssl *ft_ssl) { - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - t_sha256_ctx ctx; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_sha256_ctx ctx; (void)ft_ssl; ft_sha256_init(&ctx); ft_sha256_update(&ctx, (unsigned char *)str, ft_strlen((const char *)str)); ft_sha256_final(digest, &ctx); ft_ssl_sha256_print(str, digest, ft_ssl); -} \ No newline at end of file +} diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c index 12249985..f30e8134 100644 --- a/src/ft_ssl_usage.c +++ b/src/ft_ssl_usage.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ssl_usage.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:35:14 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:35:16 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "libft.h" @@ -8,4 +20,4 @@ void ft_ssl_usage(void) ft_putstr("algorithms:\n"); ft_putstr("md5 sha256 sha224\n\n\n"); exit(1); -} \ No newline at end of file +} diff --git a/src/main.c b/src/main.c index 8eafa65b..7fb89a87 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:36:18 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:39:16 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_ssl.h" #include "libft.h" -static int find_flag(const char *expected, char *real) +static int find_flag(const char *expected, char *real) { - return ft_strcmp(real, expected) == 0; + return (ft_strcmp(real, expected) == 0); } -static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl) +static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl) { int i; @@ -20,12 +32,13 @@ static int process_flags(int argc, char **argv, t_ft_ssl *ft_ssl) else if (find_flag("-p", argv[i])) ft_ssl->flags.print_stdin = 1; else - break; + break ; i++; } - return i; + return (i); } -static void process_strings_and_files + +static void process_strings_and_files ( int i, int argc, @@ -46,7 +59,8 @@ static void process_strings_and_files ft_ssl->process_file(argv[i++], ft_ssl); } } -int main(int argc, char **argv) + +int main(int argc, char **argv) { t_ft_ssl ft_ssl; int i; @@ -61,4 +75,4 @@ int main(int argc, char **argv) if (!ft_ssl.flags.something_printed) ft_ssl.process_stdin(&ft_ssl); return (0); -} \ No newline at end of file +} diff --git a/src/md5/ft_md5_decode.c b/src/md5/ft_md5_decode.c index 6bf0a226..d2f2b9d8 100644 --- a/src/md5/ft_md5_decode.c +++ b/src/md5/ft_md5_decode.c @@ -1,20 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_decode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:48:18 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:49:27 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" -void ft_md5_decode(BYTE4 w[FT_MD5_WORDS_COUNT], BYTE1 b[FT_MD5_BLOCK_SIZE]) +void ft_md5_decode +( + t_byte4 w[FT_MD5_WORDS_COUNT], + t_byte1 b[FT_MD5_BLOCK_SIZE] +) { - BYTE8 i; - BYTE8 j; + t_byte8 i; + t_byte8 j; - i = 0; - j = 0; - - while (j < FT_MD5_BLOCK_SIZE) - { - w[i] = ((BYTE4)b[j]) | - (((BYTE4)b[j + 1]) << 8) | - (((BYTE4)b[j + 2]) << 16) | - (((BYTE4)b[j + 3]) << 24); - i++; - j += 4; - } -} \ No newline at end of file + i = 0; + j = 0; + while (j < FT_MD5_BLOCK_SIZE) + { + w[i] = ((t_byte4)b[j]) | + (((t_byte4)b[j + 1]) << 8) | + (((t_byte4)b[j + 2]) << 16) | + (((t_byte4)b[j + 3]) << 24); + i++; + j += 4; + } +} diff --git a/src/md5/ft_md5_digest_string.c b/src/md5/ft_md5_digest_string.c index 9d6c632a..60081739 100644 --- a/src/md5/ft_md5_digest_string.c +++ b/src/md5/ft_md5_digest_string.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_digest_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:51:35 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:52:07 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -static void init_chars(BYTE1 chars[16]) +static void init_chars(t_byte1 chars[16]) { chars[0] = '0'; chars[1] = '1'; @@ -21,17 +33,19 @@ static void init_chars(BYTE1 chars[16]) chars[15] = 'f'; } -void ft_md5_digest_string(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], - BYTE1 string[FT_MD5_STRING_SIZE_BYTE]) +void ft_md5_digest_string +( + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], + t_byte1 string[FT_MD5_STRING_SIZE_BYTE]) { - BYTE8 i; - BYTE8 j; - BYTE1 chars[16]; + t_byte8 i; + t_byte8 j; + t_byte1 chars[16]; i = 0; j = 0; init_chars(chars); - while(i < FT_MD5_DIGEST_LENGTH_BYTE) + while (i < FT_MD5_DIGEST_LENGTH_BYTE) { string[j] = chars[digest[i] / 16]; string[j + 1] = chars[digest[i] % 16]; @@ -39,4 +53,4 @@ void ft_md5_digest_string(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], j += 2; } string[FT_MD5_STRING_SIZE_BYTE - 1] = 0; -} \ No newline at end of file +} diff --git a/src/md5/ft_md5_encode_len.c b/src/md5/ft_md5_encode_len.c index eabd6f54..8d135d26 100644 --- a/src/md5/ft_md5_encode_len.c +++ b/src/md5/ft_md5_encode_len.c @@ -1,13 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_encode_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:49:45 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:49:58 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" -void ft_md5_encode_len(BYTE1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], BYTE8 len) +void ft_md5_encode_len(t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], t_byte8 len) { - bits[0] = (BYTE1)(len & 0xff); - bits[1] = (BYTE1)(len >> 8 & 0xff); - bits[2] = (BYTE1)(len >> 16 & 0xff); - bits[3] = (BYTE1)(len >> 24 & 0xff); - bits[4] = (BYTE1)(len >> 32 & 0xff); - bits[5] = (BYTE1)(len >> 40 & 0xff); - bits[6] = (BYTE1)(len >> 48 & 0xff); - bits[7] = (BYTE1)(len >> 56 & 0xff); -} \ No newline at end of file + bits[0] = (t_byte1)(len & 0xff); + bits[1] = (t_byte1)(len >> 8 & 0xff); + bits[2] = (t_byte1)(len >> 16 & 0xff); + bits[3] = (t_byte1)(len >> 24 & 0xff); + bits[4] = (t_byte1)(len >> 32 & 0xff); + bits[5] = (t_byte1)(len >> 40 & 0xff); + bits[6] = (t_byte1)(len >> 48 & 0xff); + bits[7] = (t_byte1)(len >> 56 & 0xff); +} diff --git a/src/md5/ft_md5_encode_register.c b/src/md5/ft_md5_encode_register.c index 92556ed7..5c35dfdd 100644 --- a/src/md5/ft_md5_encode_register.c +++ b/src/md5/ft_md5_encode_register.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_encode_register.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:52:22 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:52:32 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" -void ft_md5_encode_register(BYTE1 *digest_part, BYTE4 reg) +void ft_md5_encode_register(t_byte1 *digest_part, t_byte4 reg) { - digest_part[0] = (BYTE1)(reg & 0xff); - digest_part[1] = (BYTE1)(reg >> 8 & 0xff); - digest_part[2] = (BYTE1)(reg >> 16 & 0xff); - digest_part[3] = (BYTE1)(reg >> 24 & 0xff); -} \ No newline at end of file + digest_part[0] = (t_byte1)(reg & 0xff); + digest_part[1] = (t_byte1)(reg >> 8 & 0xff); + digest_part[2] = (t_byte1)(reg >> 16 & 0xff); + digest_part[3] = (t_byte1)(reg >> 24 & 0xff); +} diff --git a/src/md5/ft_md5_final.c b/src/md5/ft_md5_final.c index 4debfd62..bb2c0a67 100644 --- a/src/md5/ft_md5_final.c +++ b/src/md5/ft_md5_final.c @@ -1,17 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_final.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:50:09 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:50:25 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -void ft_md5_final(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], t_md5_ctx *ctx) +void ft_md5_final(t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE], t_md5_ctx *ctx) { - BYTE1 length_as_bytes[FT_MD5_MESSAGE_LENGTH_BYTE]; - BYTE1 padding[FT_MD5_BLOCK_SIZE]; - BYTE8 buff_index; - BYTE8 padding_len; + t_byte1 length_as_bytes[FT_MD5_MESSAGE_LENGTH_BYTE]; + t_byte1 padding[FT_MD5_BLOCK_SIZE]; + t_byte8 buff_index; + t_byte8 padding_len; ft_md5_encode_len(length_as_bytes, ctx->bit_len); buff_index = (ctx->bit_len / 8) % 64; padding_len = (buff_index < 56) ? (56 - buff_index) : (120 - buff_index); - ft_md5_padding(padding); ft_md5_update(ctx, padding, padding_len); ft_md5_update(ctx, length_as_bytes, FT_MD5_MESSAGE_LENGTH_BYTE); @@ -20,4 +31,4 @@ void ft_md5_final(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE], t_md5_ctx *ctx) ft_md5_encode_register(&digest[8], ctx->c); ft_md5_encode_register(&digest[12], ctx->d); ft_bzero(ctx, sizeof(t_md5_ctx)); -} \ No newline at end of file +} diff --git a/src/md5/ft_md5_init.c b/src/md5/ft_md5_init.c index a315a4dd..fbd46837 100644 --- a/src/md5/ft_md5_init.c +++ b/src/md5/ft_md5_init.c @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:52:44 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:52:54 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -void ft_md5_init(t_md5_ctx *ctx) +void ft_md5_init(t_md5_ctx *ctx) { - ctx->a = 0x67452301; - ctx->b = 0xefcdab89; - ctx->c = 0x98badcfe; - ctx->d = 0x10325476; - ctx->bit_len = 0; - ft_bzero(ctx->block, FT_MD5_BLOCK_SIZE); -} \ No newline at end of file + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + ctx->bit_len = 0; + ft_bzero(ctx->block, FT_MD5_BLOCK_SIZE); +} diff --git a/src/md5/ft_md5_padding.c b/src/md5/ft_md5_padding.c index 9c85acd3..39a086ac 100644 --- a/src/md5/ft_md5_padding.c +++ b/src/md5/ft_md5_padding.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_padding.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:50:37 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:50:42 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -void ft_md5_padding(BYTE1 padding[FT_MD5_BLOCK_SIZE]) +void ft_md5_padding(t_byte1 padding[FT_MD5_BLOCK_SIZE]) { padding[0] = 0x80; ft_bzero(&padding[1], FT_MD5_BLOCK_SIZE - 1); -} \ No newline at end of file +} diff --git a/src/md5/ft_md5_transform.c b/src/md5/ft_md5_transform.c index 4df6f567..eb786f39 100644 --- a/src/md5/ft_md5_transform.c +++ b/src/md5/ft_md5_transform.c @@ -1,97 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_transform.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:08:38 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:10:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -void ft_md5_transform(t_md5_ctx *ctx, BYTE1 block[FT_MD5_BLOCK_SIZE]) +static void round_1(t_md5_intr_registers *r, t_byte4 w[FT_MD5_WORDS_COUNT]) { - BYTE4 words[FT_MD5_WORDS_COUNT]; - BYTE4 a; - BYTE4 b; - BYTE4 c; - BYTE4 d; + FF(r->a, r->b, r->c, r->d, w[0], S11, 0xd76aa478); + FF(r->d, r->a, r->b, r->c, w[1], S12, 0xe8c7b756); + FF(r->c, r->d, r->a, r->b, w[2], S13, 0x242070db); + FF(r->b, r->c, r->d, r->a, w[3], S14, 0xc1bdceee); + FF(r->a, r->b, r->c, r->d, w[4], S11, 0xf57c0faf); + FF(r->d, r->a, r->b, r->c, w[5], S12, 0x4787c62a); + FF(r->c, r->d, r->a, r->b, w[6], S13, 0xa8304613); + FF(r->b, r->c, r->d, r->a, w[7], S14, 0xfd469501); + FF(r->a, r->b, r->c, r->d, w[8], S11, 0x698098d8); + FF(r->d, r->a, r->b, r->c, w[9], S12, 0x8b44f7af); + FF(r->c, r->d, r->a, r->b, w[10], S13, 0xffff5bb1); + FF(r->b, r->c, r->d, r->a, w[11], S14, 0x895cd7be); + FF(r->a, r->b, r->c, r->d, w[12], S11, 0x6b901122); + FF(r->d, r->a, r->b, r->c, w[13], S12, 0xfd987193); + FF(r->c, r->d, r->a, r->b, w[14], S13, 0xa679438e); + FF(r->b, r->c, r->d, r->a, w[15], S14, 0x49b40821); +} - a = ctx->a; - b = ctx->b; - c = ctx->c; - d = ctx->d; +static void round_2(t_md5_intr_registers *r, t_byte4 w[FT_MD5_WORDS_COUNT]) +{ + GG(r->a, r->b, r->c, r->d, w[1], S21, 0xf61e2562); + GG(r->d, r->a, r->b, r->c, w[6], S22, 0xc040b340); + GG(r->c, r->d, r->a, r->b, w[11], S23, 0x265e5a51); + GG(r->b, r->c, r->d, r->a, w[0], S24, 0xe9b6c7aa); + GG(r->a, r->b, r->c, r->d, w[5], S21, 0xd62f105d); + GG(r->d, r->a, r->b, r->c, w[10], S22, 0x2441453); + GG(r->c, r->d, r->a, r->b, w[15], S23, 0xd8a1e681); + GG(r->b, r->c, r->d, r->a, w[4], S24, 0xe7d3fbc8); + GG(r->a, r->b, r->c, r->d, w[9], S21, 0x21e1cde6); + GG(r->d, r->a, r->b, r->c, w[14], S22, 0xc33707d6); + GG(r->c, r->d, r->a, r->b, w[3], S23, 0xf4d50d87); + GG(r->b, r->c, r->d, r->a, w[8], S24, 0x455a14ed); + GG(r->a, r->b, r->c, r->d, w[13], S21, 0xa9e3e905); + GG(r->d, r->a, r->b, r->c, w[2], S22, 0xfcefa3f8); + GG(r->c, r->d, r->a, r->b, w[7], S23, 0x676f02d9); + GG(r->b, r->c, r->d, r->a, w[12], S24, 0x8d2a4c8a); +} - ft_md5_decode(words, block); +static void round_3(t_md5_intr_registers *r, t_byte4 w[FT_MD5_WORDS_COUNT]) +{ + HH(r->a, r->b, r->c, r->d, w[5], S31, 0xfffa3942); + HH(r->d, r->a, r->b, r->c, w[8], S32, 0x8771f681); + HH(r->c, r->d, r->a, r->b, w[11], S33, 0x6d9d6122); + HH(r->b, r->c, r->d, r->a, w[14], S34, 0xfde5380c); + HH(r->a, r->b, r->c, r->d, w[1], S31, 0xa4beea44); + HH(r->d, r->a, r->b, r->c, w[4], S32, 0x4bdecfa9); + HH(r->c, r->d, r->a, r->b, w[7], S33, 0xf6bb4b60); + HH(r->b, r->c, r->d, r->a, w[10], S34, 0xbebfbc70); + HH(r->a, r->b, r->c, r->d, w[13], S31, 0x289b7ec6); + HH(r->d, r->a, r->b, r->c, w[0], S32, 0xeaa127fa); + HH(r->c, r->d, r->a, r->b, w[3], S33, 0xd4ef3085); + HH(r->b, r->c, r->d, r->a, w[6], S34, 0x4881d05); + HH(r->a, r->b, r->c, r->d, w[9], S31, 0xd9d4d039); + HH(r->d, r->a, r->b, r->c, w[12], S32, 0xe6db99e5); + HH(r->c, r->d, r->a, r->b, w[15], S33, 0x1fa27cf8); + HH(r->b, r->c, r->d, r->a, w[2], S34, 0xc4ac5665); +} - /* Round 1 */ - FF(a, b, c, d, words[0], S11, 0xd76aa478); /* 1 */ - FF(d, a, b, c, words[1], S12, 0xe8c7b756); /* 2 */ - FF(c, d, a, b, words[2], S13, 0x242070db); /* 3 */ - FF(b, c, d, a, words[3], S14, 0xc1bdceee); /* 4 */ - FF(a, b, c, d, words[4], S11, 0xf57c0faf); /* 5 */ - FF(d, a, b, c, words[5], S12, 0x4787c62a); /* 6 */ - FF(c, d, a, b, words[6], S13, 0xa8304613); /* 7 */ - FF(b, c, d, a, words[7], S14, 0xfd469501); /* 8 */ - FF(a, b, c, d, words[8], S11, 0x698098d8); /* 9 */ - FF(d, a, b, c, words[9], S12, 0x8b44f7af); /* 10 */ - FF(c, d, a, b, words[10], S13, 0xffff5bb1); /* 11 */ - FF(b, c, d, a, words[11], S14, 0x895cd7be); /* 12 */ - FF(a, b, c, d, words[12], S11, 0x6b901122); /* 13 */ - FF(d, a, b, c, words[13], S12, 0xfd987193); /* 14 */ - FF(c, d, a, b, words[14], S13, 0xa679438e); /* 15 */ - FF(b, c, d, a, words[15], S14, 0x49b40821); /* 16 */ +static void round_4(t_md5_intr_registers *r, t_byte4 w[FT_MD5_WORDS_COUNT]) +{ + II(r->a, r->b, r->c, r->d, w[0], S41, 0xf4292244); + II(r->d, r->a, r->b, r->c, w[7], S42, 0x432aff97); + II(r->c, r->d, r->a, r->b, w[14], S43, 0xab9423a7); + II(r->b, r->c, r->d, r->a, w[5], S44, 0xfc93a039); + II(r->a, r->b, r->c, r->d, w[12], S41, 0x655b59c3); + II(r->d, r->a, r->b, r->c, w[3], S42, 0x8f0ccc92); + II(r->c, r->d, r->a, r->b, w[10], S43, 0xffeff47d); + II(r->b, r->c, r->d, r->a, w[1], S44, 0x85845dd1); + II(r->a, r->b, r->c, r->d, w[8], S41, 0x6fa87e4f); + II(r->d, r->a, r->b, r->c, w[15], S42, 0xfe2ce6e0); + II(r->c, r->d, r->a, r->b, w[6], S43, 0xa3014314); + II(r->b, r->c, r->d, r->a, w[13], S44, 0x4e0811a1); + II(r->a, r->b, r->c, r->d, w[4], S41, 0xf7537e82); + II(r->d, r->a, r->b, r->c, w[11], S42, 0xbd3af235); + II(r->c, r->d, r->a, r->b, w[2], S43, 0x2ad7d2bb); + II(r->b, r->c, r->d, r->a, w[9], S44, 0xeb86d391); +} - /* Round 2 */ - GG(a, b, c, d, words[1], S21, 0xf61e2562); /* 17 */ - GG(d, a, b, c, words[6], S22, 0xc040b340); /* 18 */ - GG(c, d, a, b, words[11], S23, 0x265e5a51); /* 19 */ - GG(b, c, d, a, words[0], S24, 0xe9b6c7aa); /* 20 */ - GG(a, b, c, d, words[5], S21, 0xd62f105d); /* 21 */ - GG(d, a, b, c, words[10], S22, 0x2441453); /* 22 */ - GG(c, d, a, b, words[15], S23, 0xd8a1e681); /* 23 */ - GG(b, c, d, a, words[4], S24, 0xe7d3fbc8); /* 24 */ - GG(a, b, c, d, words[9], S21, 0x21e1cde6); /* 25 */ - GG(d, a, b, c, words[14], S22, 0xc33707d6); /* 26 */ - GG(c, d, a, b, words[3], S23, 0xf4d50d87); /* 27 */ - GG(b, c, d, a, words[8], S24, 0x455a14ed); /* 28 */ - GG(a, b, c, d, words[13], S21, 0xa9e3e905); /* 29 */ - GG(d, a, b, c, words[2], S22, 0xfcefa3f8); /* 30 */ - GG(c, d, a, b, words[7], S23, 0x676f02d9); /* 31 */ - GG(b, c, d, a, words[12], S24, 0x8d2a4c8a); /* 32 */ +void ft_md5_transform(t_md5_ctx *ctx, t_byte1 block[FT_MD5_BLOCK_SIZE]) +{ + t_byte4 words[FT_MD5_WORDS_COUNT]; + t_md5_intr_registers t; - /* Round 3 */ - HH(a, b, c, d, words[5], S31, 0xfffa3942); /* 33 */ - HH(d, a, b, c, words[8], S32, 0x8771f681); /* 34 */ - HH(c, d, a, b, words[11], S33, 0x6d9d6122); /* 35 */ - HH(b, c, d, a, words[14], S34, 0xfde5380c); /* 36 */ - HH(a, b, c, d, words[1], S31, 0xa4beea44); /* 37 */ - HH(d, a, b, c, words[4], S32, 0x4bdecfa9); /* 38 */ - HH(c, d, a, b, words[7], S33, 0xf6bb4b60); /* 39 */ - HH(b, c, d, a, words[10], S34, 0xbebfbc70); /* 40 */ - HH(a, b, c, d, words[13], S31, 0x289b7ec6); /* 41 */ - HH(d, a, b, c, words[0], S32, 0xeaa127fa); /* 42 */ - HH(c, d, a, b, words[3], S33, 0xd4ef3085); /* 43 */ - HH(b, c, d, a, words[6], S34, 0x4881d05); /* 44 */ - HH(a, b, c, d, words[9], S31, 0xd9d4d039); /* 45 */ - HH(d, a, b, c, words[12], S32, 0xe6db99e5); /* 46 */ - HH(c, d, a, b, words[15], S33, 0x1fa27cf8); /* 47 */ - HH(b, c, d, a, words[2], S34, 0xc4ac5665); /* 48 */ - - /* Round 4 */ - II(a, b, c, d, words[0], S41, 0xf4292244); /* 49 */ - II(d, a, b, c, words[7], S42, 0x432aff97); /* 50 */ - II(c, d, a, b, words[14], S43, 0xab9423a7); /* 51 */ - II(b, c, d, a, words[5], S44, 0xfc93a039); /* 52 */ - II(a, b, c, d, words[12], S41, 0x655b59c3); /* 53 */ - II(d, a, b, c, words[3], S42, 0x8f0ccc92); /* 54 */ - II(c, d, a, b, words[10], S43, 0xffeff47d); /* 55 */ - II(b, c, d, a, words[1], S44, 0x85845dd1); /* 56 */ - II(a, b, c, d, words[8], S41, 0x6fa87e4f); /* 57 */ - II(d, a, b, c, words[15], S42, 0xfe2ce6e0); /* 58 */ - II(c, d, a, b, words[6], S43, 0xa3014314); /* 59 */ - II(b, c, d, a, words[13], S44, 0x4e0811a1); /* 60 */ - II(a, b, c, d, words[4], S41, 0xf7537e82); /* 61 */ - II(d, a, b, c, words[11], S42, 0xbd3af235); /* 62 */ - II(c, d, a, b, words[2], S43, 0x2ad7d2bb); /* 63 */ - II(b, c, d, a, words[9], S44, 0xeb86d391); /* 64 */ - - ctx->a += a; - ctx->b += b; - ctx->c += c; - ctx->d += d; - /* Zeroize sensitive information.*/ - ft_bzero(words, sizeof(words)); -} \ No newline at end of file + ft_md5_decode(words, block); + t.a = ctx->a; + t.b = ctx->b; + t.c = ctx->c; + t.d = ctx->d; + round_1(&t, words); + round_2(&t, words); + round_3(&t, words); + round_4(&t, words); + ctx->a += t.a; + ctx->b += t.b; + ctx->c += t.c; + ctx->d += t.d; + ft_bzero(words, sizeof(words)); +} diff --git a/src/md5/ft_md5_update.c b/src/md5/ft_md5_update.c index e489bf64..a7ce1232 100644 --- a/src/md5/ft_md5_update.c +++ b/src/md5/ft_md5_update.c @@ -1,28 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_md5_update.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 19:50:54 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 19:51:24 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_md5.h" #include "libft.h" -void ft_md5_update(t_md5_ctx *c, BYTE1 * message, BYTE8 message_len) +void ft_md5_update(t_md5_ctx *c, t_byte1 *message, t_byte8 message_len) { - unsigned int i; - unsigned int part_block_len; - unsigned int curent_block_index; + unsigned int i; + unsigned int part_block_len; + unsigned int curent_block_index; curent_block_index = (c->bit_len / 8) % 64; c->bit_len += message_len * 8; - part_block_len = FT_MD5_BLOCK_SIZE - curent_block_index; - if (message_len >= part_block_len) - { - ft_memcpy(&c->block[curent_block_index], message, part_block_len); - ft_md5_transform(c, c->block); - i = part_block_len; - while(i + 63 < message_len) - { - ft_md5_transform(c, &message[i]); - i += FT_MD5_BLOCK_SIZE; - } - curent_block_index = 0; - } - else - i = 0; - ft_memcpy(&c->block[curent_block_index], &message[i], message_len - i); -} \ No newline at end of file + part_block_len = FT_MD5_BLOCK_SIZE - curent_block_index; + if (message_len >= part_block_len) + { + ft_memcpy(&c->block[curent_block_index], message, part_block_len); + ft_md5_transform(c, c->block); + i = part_block_len; + while (i + 63 < message_len) + { + ft_md5_transform(c, &message[i]); + i += FT_MD5_BLOCK_SIZE; + } + curent_block_index = 0; + } + else + i = 0; + ft_memcpy(&c->block[curent_block_index], &message[i], message_len - i); +} diff --git a/src/sha/ft_sha224_digest_string.c b/src/sha/ft_sha224_digest_string.c index f09af63b..7dfeaee0 100644 --- a/src/sha/ft_sha224_digest_string.c +++ b/src/sha/ft_sha224_digest_string.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha224_digest_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:10:41 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:10:54 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -static void init_chars(BYTE1 chars[16]) +static void init_chars(t_byte1 chars[16]) { chars[0] = '0'; chars[1] = '1'; @@ -20,14 +32,14 @@ static void init_chars(BYTE1 chars[16]) chars[15] = 'f'; } -void ft_sha224_digest_string +void ft_sha224_digest_string ( - BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], - BYTE1 string[FT_SHA224_STRING_SIZE_BYTE]) + t_byte1 digest[FT_SHA224_DIGEST_LENGTH_BYTE], + t_byte1 string[FT_SHA224_STRING_SIZE_BYTE]) { - BYTE8 i; - BYTE8 j; - BYTE1 chars[16]; + t_byte8 i; + t_byte8 j; + t_byte1 chars[16]; i = 0; j = 0; @@ -40,4 +52,4 @@ void ft_sha224_digest_string j += 2; } string[FT_SHA224_STRING_SIZE_BYTE - 1] = 0; -} \ No newline at end of file +} diff --git a/src/sha/ft_sha224_final.c b/src/sha/ft_sha224_final.c index d43b942c..45d0a752 100644 --- a/src/sha/ft_sha224_final.c +++ b/src/sha/ft_sha224_final.c @@ -1,10 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha224_final.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:11:43 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:11:47 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -void ft_sha224_final +void ft_sha224_final ( - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], t_sha256_ctx *ctx ) { ft_sha256_final(digest, ctx); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha224_init.c b/src/sha/ft_sha224_init.c index 86f1faf7..58e159c4 100644 --- a/src/sha/ft_sha224_init.c +++ b/src/sha/ft_sha224_init.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha224_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:12:17 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:12:31 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "libft.h" -void ft_sha224_init(t_sha256_ctx *ctx) +void ft_sha224_init(t_sha256_ctx *ctx) { ctx->a = 0xc1059ed8; ctx->b = 0x367cd507; @@ -11,7 +23,6 @@ void ft_sha224_init(t_sha256_ctx *ctx) ctx->f = 0x68581511; ctx->g = 0x64f98fa7; ctx->h = 0xbefa4fa4; - ctx->bit_len = 0; ft_bzero(ctx->block, FT_SHA256_BLOCK_SIZE); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha224_update.c b/src/sha/ft_sha224_update.c index 01ed2ea9..f08eb7cd 100644 --- a/src/sha/ft_sha224_update.c +++ b/src/sha/ft_sha224_update.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha224_update.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:12:58 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:13:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -void ft_sha224_update(t_sha256_ctx *ctx, BYTE1 *message, BYTE8 len) +void ft_sha224_update(t_sha256_ctx *ctx, t_byte1 *message, t_byte8 len) { ft_sha256_update(ctx, message, len); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_constants.c b/src/sha/ft_sha256_constants.c index 08ec3d26..ab58af2d 100644 --- a/src/sha/ft_sha256_constants.c +++ b/src/sha/ft_sha256_constants.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_constants.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:13:18 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:13:41 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -BYTE4 *ft_sha256_constants(void) +t_byte4 *ft_sha256_constants(void) { - static BYTE4 k[FT_SHA256_BLOCK_SIZE] = { + static t_byte4 k[FT_SHA256_BLOCK_SIZE] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, @@ -17,5 +29,5 @@ BYTE4 *ft_sha256_constants(void) 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - return k; -} \ No newline at end of file + return (k); +} diff --git a/src/sha/ft_sha256_decode.c b/src/sha/ft_sha256_decode.c index 744f126f..606a4378 100644 --- a/src/sha/ft_sha256_decode.c +++ b/src/sha/ft_sha256_decode.c @@ -1,24 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_decode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:13:59 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:14:28 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -void ft_sha256_decode +void ft_sha256_decode ( - BYTE4 w[FT_SHA256_WORDS_COUNT], - BYTE1 b[FT_SHA256_BLOCK_SIZE] + t_byte4 w[FT_SHA256_WORDS_COUNT], + t_byte1 b[FT_SHA256_BLOCK_SIZE] ) { - BYTE8 i; - BYTE8 j; + t_byte8 i; + t_byte8 j; i = 0; j = 0; - while (i < FT_SHA256_WORDS_COUNT) { - w[i] = ((((BYTE4)b[j]) << 24) | - (((BYTE4)b[j + 1]) << 16) | - (((BYTE4)b[j + 2]) << 8) | - ((BYTE4)b[j + 3])); + w[i] = ((((t_byte4)b[j]) << 24) | + (((t_byte4)b[j + 1]) << 16) | + (((t_byte4)b[j + 2]) << 8) | + ((t_byte4)b[j + 3])); i++; j += 4; } -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_digest_string.c b/src/sha/ft_sha256_digest_string.c index 625087a7..92898940 100644 --- a/src/sha/ft_sha256_digest_string.c +++ b/src/sha/ft_sha256_digest_string.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_digest_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:14:41 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:14:53 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -static void init_chars(BYTE1 chars[16]) +static void init_chars(t_byte1 chars[16]) { chars[0] = '0'; chars[1] = '1'; @@ -20,15 +32,15 @@ static void init_chars(BYTE1 chars[16]) chars[15] = 'f'; } -void ft_sha256_digest_string +void ft_sha256_digest_string ( - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], - BYTE1 string[FT_SHA256_STRING_SIZE_BYTE] + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_byte1 string[FT_SHA256_STRING_SIZE_BYTE] ) { - BYTE8 i; - BYTE8 j; - BYTE1 chars[16]; + t_byte8 i; + t_byte8 j; + t_byte1 chars[16]; i = 0; j = 0; @@ -41,4 +53,4 @@ void ft_sha256_digest_string j += 2; } string[FT_SHA256_STRING_SIZE_BYTE - 1] = 0; -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_encode_len.c b/src/sha/ft_sha256_encode_len.c index e9b7e541..0f9f6b29 100644 --- a/src/sha/ft_sha256_encode_len.c +++ b/src/sha/ft_sha256_encode_len.c @@ -1,13 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_encode_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:15:16 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:15:34 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -void ft_sha256_encode_len(BYTE1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], BYTE8 len) +void ft_sha256_encode_len +( + t_byte1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], + t_byte8 len +) { - b[7] = (BYTE1)(len & 0xff); - b[6] = (BYTE1)(len >> 8 & 0xff); - b[5] = (BYTE1)(len >> 16 & 0xff); - b[4] = (BYTE1)(len >> 24 & 0xff); - b[3] = (BYTE1)(len >> 32 & 0xff); - b[2] = (BYTE1)(len >> 40 & 0xff); - b[1] = (BYTE1)(len >> 48 & 0xff); - b[0] = (BYTE1)(len >> 56 & 0xff); -} \ No newline at end of file + b[7] = (t_byte1)(len & 0xff); + b[6] = (t_byte1)(len >> 8 & 0xff); + b[5] = (t_byte1)(len >> 16 & 0xff); + b[4] = (t_byte1)(len >> 24 & 0xff); + b[3] = (t_byte1)(len >> 32 & 0xff); + b[2] = (t_byte1)(len >> 40 & 0xff); + b[1] = (t_byte1)(len >> 48 & 0xff); + b[0] = (t_byte1)(len >> 56 & 0xff); +} diff --git a/src/sha/ft_sha256_encode_register.c b/src/sha/ft_sha256_encode_register.c index bad16a2f..7c543d2a 100644 --- a/src/sha/ft_sha256_encode_register.c +++ b/src/sha/ft_sha256_encode_register.c @@ -1,13 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_encode_register.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:15:49 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:15:52 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -void ft_sha256_encode_register +void ft_sha256_encode_register ( - BYTE1 digest_part[FT_SHA256_REG_SIZE_BYTE], - BYTE4 reg + t_byte1 digest_part[FT_SHA256_REG_SIZE_BYTE], + t_byte4 reg ) { digest_part[0] = reg >> 24 & 0xff; digest_part[1] = reg >> 16 & 0xff; digest_part[2] = reg >> 8 & 0xff; digest_part[3] = reg & 0xff; -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_final.c b/src/sha/ft_sha256_final.c index b858bbab..107a35f9 100644 --- a/src/sha/ft_sha256_final.c +++ b/src/sha/ft_sha256_final.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_final.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:16:08 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:16:26 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "libft.h" -static void fill_digest +static void fill_digest ( - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], t_sha256_ctx *ctx ) { @@ -17,23 +29,22 @@ static void fill_digest ft_sha256_encode_register(&digest[28], ctx->h); } -void ft_sha256_final +void ft_sha256_final ( - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE], t_sha256_ctx *ctx ) { - BYTE1 length_as_bytes[FT_SHA256_MESSAGE_LENGTH_BYTE]; - BYTE1 padding[FT_SHA256_BLOCK_SIZE]; - BYTE8 buff_index; - BYTE8 padding_len; + t_byte1 length_as_bytes[FT_SHA256_MESSAGE_LENGTH_BYTE]; + t_byte1 padding[FT_SHA256_BLOCK_SIZE]; + t_byte8 buff_index; + t_byte8 padding_len; ft_sha256_encode_len(length_as_bytes, ctx->bit_len); buff_index = (ctx->bit_len / 8) % 64; padding_len = (buff_index < 56) ? (56 - buff_index) : (120 - buff_index); - ft_sha256_padding(padding); ft_sha256_update(ctx, padding, padding_len); ft_sha256_update(ctx, length_as_bytes, FT_SHA256_MESSAGE_LENGTH_BYTE); fill_digest(digest, ctx); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_init.c b/src/sha/ft_sha256_init.c index 7af5ba43..3ce5db30 100644 --- a/src/sha/ft_sha256_init.c +++ b/src/sha/ft_sha256_init.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:16:36 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:16:47 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "libft.h" -void ft_sha256_init(t_sha256_ctx *ctx) +void ft_sha256_init(t_sha256_ctx *ctx) { ctx->a = 0x6a09e667; ctx->b = 0xbb67ae85; @@ -11,7 +23,6 @@ void ft_sha256_init(t_sha256_ctx *ctx) ctx->f = 0x9b05688c; ctx->g = 0x1f83d9ab; ctx->h = 0x5be0cd19; - ctx->bit_len = 0; ft_bzero(ctx->block, FT_SHA256_BLOCK_SIZE); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_padding.c b/src/sha/ft_sha256_padding.c index d6828b7d..05c7aa4c 100644 --- a/src/sha/ft_sha256_padding.c +++ b/src/sha/ft_sha256_padding.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_padding.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:16:55 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:17:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "libft.h" -void ft_sha256_padding(BYTE1 padding[FT_SHA256_BLOCK_SIZE]) +void ft_sha256_padding(t_byte1 padding[FT_SHA256_BLOCK_SIZE]) { padding[0] = 0x80; ft_bzero(&padding[1], FT_SHA256_BLOCK_SIZE - 1); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_transform.c b/src/sha/ft_sha256_transform.c index af4b349d..a9ca2f93 100644 --- a/src/sha/ft_sha256_transform.c +++ b/src/sha/ft_sha256_transform.c @@ -1,19 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_transform.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:17:19 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:20:10 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" -static void fill_up_words(BYTE4 w[FT_SHA256_BLOCK_SIZE]) +static void fill_up_words +( + t_byte4 w[FT_SHA256_BLOCK_SIZE] +) { int i; i = 16; while (i < FT_SHA256_BLOCK_SIZE) { - w[i] = SHA256_sigma1(w[i - 2]) + w[i - 7] + - SHA256_sigma0(w[i - 15]) + w[i - 16]; + w[i] = SHA256_SIG1(w[i - 2]) + w[i - 7] + + SHA256_SIG0(w[i - 15]) + w[i - 16]; i++; } } -static void copy_from_registers(t_sha256_ctx *from, t_temp_registers *to) +static void copy_from_registers +( + t_sha256_ctx *from, + t_temp_registers *to +) { to->a = from->a; to->b = from->b; @@ -25,7 +44,11 @@ static void copy_from_registers(t_sha256_ctx *from, t_temp_registers *to) to->h = from->h; } -static void add_to_registers(t_temp_registers *from, t_sha256_ctx *to) +static void add_to_registers +( + t_temp_registers *from, + t_sha256_ctx *to +) { to->a += from->a; to->b += from->b; @@ -37,23 +60,24 @@ static void add_to_registers(t_temp_registers *from, t_sha256_ctx *to) to->h += from->h; } -static void main_transform_loop +static void main_transform_loop ( t_temp_registers *tr, - BYTE4 w[FT_SHA256_BLOCK_SIZE] + t_byte4 w[FT_SHA256_BLOCK_SIZE] ) { - int i; - BYTE4 temp1, temp2; - BYTE4 *k; + int i; + t_byte4 temp1; + t_byte4 temp2; + t_byte4 *k; k = ft_sha256_constants(); i = 0; while (i < FT_SHA256_BLOCK_SIZE) { temp1 = tr->h + SHA256_SIGMA1(tr->e) + - SHA_Ch(tr->e, tr->f, tr->g) + k[i] + w[i]; - temp2 = SHA256_SIGMA0(tr->a) + SHA_Maj(tr->a, tr->b, tr->c); + SHA_CH(tr->e, tr->f, tr->g) + k[i] + w[i]; + temp2 = SHA256_SIGMA0(tr->a) + SHA_MAJ(tr->a, tr->b, tr->c); tr->h = tr->g; tr->g = tr->f; tr->f = tr->e; @@ -66,9 +90,13 @@ static void main_transform_loop } } -void ft_sha256_transform(t_sha256_ctx *c, BYTE1 block[FT_SHA256_BLOCK_SIZE]) +void ft_sha256_transform +( + t_sha256_ctx *c, + t_byte1 block[FT_SHA256_BLOCK_SIZE] +) { - BYTE4 w[FT_SHA256_BLOCK_SIZE]; + t_byte4 w[FT_SHA256_BLOCK_SIZE]; t_temp_registers tr; ft_sha256_decode(w, block); @@ -76,4 +104,4 @@ void ft_sha256_transform(t_sha256_ctx *c, BYTE1 block[FT_SHA256_BLOCK_SIZE]) copy_from_registers(c, &tr); main_transform_loop(&tr, w); add_to_registers(&tr, c); -} \ No newline at end of file +} diff --git a/src/sha/ft_sha256_update.c b/src/sha/ft_sha256_update.c index a8f59bf4..fc49723c 100644 --- a/src/sha/ft_sha256_update.c +++ b/src/sha/ft_sha256_update.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sha256_update.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/23 20:20:53 by gtertysh #+# #+# */ +/* Updated: 2018/10/23 20:21:00 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_sha.h" #include "libft.h" -void ft_sha256_update(t_sha256_ctx *c, BYTE1 *message, BYTE8 message_len) +void ft_sha256_update(t_sha256_ctx *c, t_byte1 *message, t_byte8 message_len) { unsigned int i; unsigned int part_block_len; @@ -25,4 +37,4 @@ void ft_sha256_update(t_sha256_ctx *c, BYTE1 *message, BYTE8 message_len) else i = 0; ft_memcpy(&c->block[curent_block_index], &message[i], message_len - i); -} \ No newline at end of file +} diff --git a/t/md5_tests.c b/t/md5_tests.c index a000620f..37ebf0e1 100644 --- a/t/md5_tests.c +++ b/t/md5_tests.c @@ -1,4 +1,5 @@ #include "tests.h" +#include "tests_macros.h" #include "ft_md5.h" #include "libft.h" @@ -26,8 +27,8 @@ TEST_RESULT decode_string_to_int(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE1 block[FT_MD5_BLOCK_SIZE]; - BYTE4 words[FT_MD5_WORDS_COUNT]; + t_byte1 block[FT_MD5_BLOCK_SIZE]; + t_byte4 words[FT_MD5_WORDS_COUNT]; ft_bzero(block, FT_MD5_BLOCK_SIZE); block[0] = 'a'; @@ -49,10 +50,10 @@ TEST_RESULT update_should_change_count(TEST_PARAMS, TEST_DATA) UNUSED(test_data); t_md5_ctx ctx; char message[] = "hello, World!"; - BYTE8 size = ft_strlen(message); + t_byte8 size = ft_strlen(message); ft_md5_init(&ctx); - ft_md5_update(&ctx, (BYTE1 *)message, size); + ft_md5_update(&ctx, (t_byte1 *)message, size); munit_assert_true(size * 8 == ctx.bit_len); @@ -64,8 +65,8 @@ TEST_RESULT encode_bits_to_string(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE8 len; - BYTE1 bits[FT_MD5_MESSAGE_LENGTH_BYTE]; + t_byte8 len; + t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE]; /* 1111 0000 : 63 - 56 @@ -81,9 +82,9 @@ TEST_RESULT encode_bits_to_string(TEST_PARAMS, TEST_DATA) ft_md5_encode_len(bits, len); - munit_assert_true(bits[7] == (BYTE1)((len >> 56) & 0xff)); - munit_assert_true(bits[0] == (BYTE1)(len & 0xff)); - munit_assert_true(bits[1] == (BYTE1)((len >> 8) & 0xff)); + munit_assert_true(bits[7] == (t_byte1)((len >> 56) & 0xff)); + munit_assert_true(bits[0] == (t_byte1)(len & 0xff)); + munit_assert_true(bits[1] == (t_byte1)((len >> 8) & 0xff)); return MUNIT_OK; } @@ -93,8 +94,8 @@ TEST_RESULT encode_register(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE1 digest_part[4]; - BYTE4 reg; + t_byte1 digest_part[4]; + t_byte4 reg; /* 1000 0000 : 31 - 24 @@ -106,10 +107,10 @@ TEST_RESULT encode_register(TEST_PARAMS, TEST_DATA) ft_md5_encode_register(digest_part, reg); - munit_assert_true(digest_part[0] == (BYTE1)(reg & 0xff)); - munit_assert_true(digest_part[1] == (BYTE1)((reg >> 8) & 0xff)); - munit_assert_true(digest_part[2] == (BYTE1)((reg >> 16) & 0xff)); - munit_assert_true(digest_part[3] == (BYTE1)((reg >> 24) & 0xff)); + munit_assert_true(digest_part[0] == (t_byte1)(reg & 0xff)); + munit_assert_true(digest_part[1] == (t_byte1)((reg >> 8) & 0xff)); + munit_assert_true(digest_part[2] == (t_byte1)((reg >> 16) & 0xff)); + munit_assert_true(digest_part[3] == (t_byte1)((reg >> 24) & 0xff)); return MUNIT_OK; } @@ -120,12 +121,12 @@ TEST_RESULT create_digest(TEST_PARAMS, TEST_DATA) UNUSED(test_data); t_md5_ctx ctx; - BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; - BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE]; + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE]; - BYTE1 case1[] = ""; - BYTE1 case2[] = "a"; - BYTE1 case7[] = "1234567890123456789012345678901234567\ + t_byte1 case1[] = ""; + t_byte1 case2[] = "a"; + t_byte1 case7[] = "1234567890123456789012345678901234567\ 891234567890123456789012345678901234567890"; ft_md5_init(&ctx); @@ -160,12 +161,12 @@ TEST_RESULT create_string(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; - BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE]; - BYTE4 a; - BYTE4 b; - BYTE4 c; - BYTE4 d; + t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE]; + t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE]; + t_byte4 a; + t_byte4 b; + t_byte4 c; + t_byte4 d; /* final registers values for empty input */ a = 3649838548; diff --git a/t/sha_tests.c b/t/sha_tests.c index 46e19a98..03630ea6 100644 --- a/t/sha_tests.c +++ b/t/sha_tests.c @@ -1,4 +1,5 @@ #include "tests.h" +#include "tests_macros.h" #include "ft_sha.h" #include "libft.h" @@ -53,8 +54,8 @@ TEST_RESULT decode_string_to_int_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE1 block[FT_SHA256_BLOCK_SIZE]; - BYTE4 words[FT_SHA256_WORDS_COUNT]; + t_byte1 block[FT_SHA256_BLOCK_SIZE]; + t_byte4 words[FT_SHA256_WORDS_COUNT]; ft_bzero(block, FT_SHA256_BLOCK_SIZE); block[0] = 'a'; @@ -73,8 +74,8 @@ TEST_RESULT encode_len_to_string_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE8 len; - BYTE1 bits[FT_SHA256_MESSAGE_LENGTH_BYTE]; + t_byte8 len; + t_byte1 bits[FT_SHA256_MESSAGE_LENGTH_BYTE]; /* 1111 0000 : 63 - 56 @@ -90,9 +91,9 @@ TEST_RESULT encode_len_to_string_sha256(TEST_PARAMS, TEST_DATA) ft_sha256_encode_len(bits, len); - munit_assert_uchar(bits[0], ==, (BYTE1)((len >> 56) & 0xff)); - munit_assert_uchar(bits[7], ==, (BYTE1)(len & 0xff)); - munit_assert_uchar(bits[6], ==, (BYTE1)((len >> 8) & 0xff)); + munit_assert_uchar(bits[0], ==, (t_byte1)((len >> 56) & 0xff)); + munit_assert_uchar(bits[7], ==, (t_byte1)(len & 0xff)); + munit_assert_uchar(bits[6], ==, (t_byte1)((len >> 8) & 0xff)); return MUNIT_OK; } @@ -102,8 +103,8 @@ TEST_RESULT encode_register_to_string_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); - BYTE1 digest_part[4]; - BYTE4 reg; + t_byte1 digest_part[4]; + t_byte4 reg; reg = 0xba7816bf; @@ -123,7 +124,7 @@ TEST_RESULT update_bit_count_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); t_sha256_ctx ctx; - BYTE1 message[] = "abc"; + t_byte1 message[] = "abc"; ft_sha256_init(&ctx); ft_sha256_update(&ctx, message, sizeof(message)); @@ -139,7 +140,7 @@ TEST_RESULT fill_buffer_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); t_sha256_ctx ctx; - BYTE1 message[] = "abc"; + t_byte1 message[] = "abc"; ft_sha256_init(&ctx); ft_sha256_update(&ctx, message, sizeof(message)); @@ -151,9 +152,9 @@ TEST_RESULT fill_buffer_sha256(TEST_PARAMS, TEST_DATA) static void block_with_right_padding ( - BYTE1 *message, - BYTE8 message_len, - BYTE1 padding[FT_SHA256_BLOCK_SIZE] + t_byte1 *message, + t_byte8 message_len, + t_byte1 padding[FT_SHA256_BLOCK_SIZE] ) { ft_bzero(padding, FT_SHA256_BLOCK_SIZE); @@ -167,11 +168,11 @@ TEST_RESULT add_right_padding_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_params); t_sha256_ctx ctx; - BYTE1 message[] = "abc"; - BYTE8 buff_index; - BYTE8 padding_len; - BYTE1 padding[FT_SHA256_BLOCK_SIZE]; - BYTE1 block_with_message_and_pading[FT_SHA256_BLOCK_SIZE]; + t_byte1 message[] = "abc"; + t_byte8 buff_index; + t_byte8 padding_len; + t_byte1 padding[FT_SHA256_BLOCK_SIZE]; + t_byte1 block_with_message_and_pading[FT_SHA256_BLOCK_SIZE]; ft_sha256_init(&ctx); ft_sha256_update(&ctx, message, sizeof(message)); @@ -193,9 +194,9 @@ TEST_RESULT compute_digest_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_data); UNUSED(test_params); - BYTE1 message[] = "abc"; + t_byte1 message[] = "abc"; t_sha256_ctx ctx; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; ft_sha256_init(&ctx); ft_sha256_update(&ctx, message, ft_strlen((const char *)message)); @@ -219,9 +220,9 @@ TEST_RESULT compute_digest_sha224(TEST_PARAMS, TEST_DATA) UNUSED(test_data); UNUSED(test_params); - BYTE1 message[] = "abc"; + t_byte1 message[] = "abc"; t_sha256_ctx ctx; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; ft_sha224_init(&ctx); ft_sha224_update(&ctx, message, ft_strlen((const char *)message)); @@ -243,12 +244,12 @@ TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA) UNUSED(test_data); UNUSED(test_params); - BYTE1 message[] = "abc"; - BYTE1 message_digest[] = + t_byte1 message[] = "abc"; + t_byte1 message_digest[] = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; t_sha256_ctx ctx; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE]; ft_sha256_init(&ctx); ft_sha256_update(&ctx, message, ft_strlen((const char *)message)); @@ -266,12 +267,12 @@ TEST_RESULT create_digest_string_sha224(TEST_PARAMS, TEST_DATA) UNUSED(test_data); UNUSED(test_params); - BYTE1 message[] = "abc"; - BYTE1 message_digest[] = + t_byte1 message[] = "abc"; + t_byte1 message_digest[] = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; t_sha256_ctx ctx; - BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; - BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; + t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE]; + t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE]; ft_sha224_init(&ctx); ft_sha224_update(&ctx, message, ft_strlen((const char *)message)); diff --git a/t/tests.c b/t/tests.c index 607c5519..9a04727e 100644 --- a/t/tests.c +++ b/t/tests.c @@ -1,4 +1,6 @@ #include "tests.h" +#include "tests_macros.h" + MunitTest md5_tests[] = { IT("/init_ctx", should_init_ctx, NULL, NULL, 0, NULL),