diff --git a/Makefile b/Makefile index f6656a2a..67302c1c 100644 --- a/Makefile +++ b/Makefile @@ -36,8 +36,10 @@ MUINUT_INC := -I $(MUINUT_DIR) # project source files -SRC = main.c +MD5_SRC = ft_md5_init.c +SRC = main.c +SRC += $(MD5_SRC) # project object files OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) @@ -47,10 +49,12 @@ OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) TEST_BIN = ft_ssl_test -TEST_SRC = main_test.c \ - test_tests.c +MD5_TESTS = md5_tests.c +MD5_TESTS += $(MD5_SRC) -TEST_SRC += munit.c +TEST_SRC = tests.c \ + munit.c +TEST_SRC += $(MD5_TESTS) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) @@ -80,12 +84,13 @@ endif # linking flags -LINK_FLAGS = "" +LINK_FLAGS = $(LIBFT_LIB) # header flags HEADER_FLAGS = -I $(INC_DIR) \ - $(MUINUT_INC) + $(MUINUT_INC) \ + $(LIBFT_INC) # compiler diff --git a/inc/ft_md5.h b/inc/ft_md5.h new file mode 100644 index 00000000..459c76a8 --- /dev/null +++ b/inc/ft_md5.h @@ -0,0 +1,26 @@ +#ifndef FT_MD5_H +# define FT_MD5_H + +# include + +# define FT_MD5_BUFFER_SIZE 64 + +typedef unsigned int BYTE4; +typedef unsigned char BYTE1; + +typedef struct s_md5_ctx +{ + BYTE4 a; + BYTE4 b; + BYTE4 c; + BYTE4 d; + BYTE1 buffer[FT_MD5_BUFFER_SIZE]; + uint64_t message_len; +} t_md5_ctx; + +void ft_md5_init(t_md5_ctx *ctx); +void ft_md5_update(t_md5_ctx *ctx, BYTE1 *chunk, BYTE4 len); +void ft_md5_transform(t_md5_ctx *ctx, BYTE1 buffer[64]); +void ft_md5_final(t_md5_ctx *ctx, BYTE1 *chunk, BYTE4 len); + +#endif diff --git a/inc/tests.h b/inc/tests.h index 8eb40f1a..1a14465f 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -6,8 +6,6 @@ #include "tests_macros.h" // test tests -CASE(should_pass); -CASE(should_skip); -CASE(should_fail); +CASE(should_init_ctx); #endif \ No newline at end of file diff --git a/src/ft_md5_init.c b/src/ft_md5_init.c new file mode 100644 index 00000000..53aab185 --- /dev/null +++ b/src/ft_md5_init.c @@ -0,0 +1,10 @@ +#include "ft_md5.h" +#include "libft.h" +void ft_md5_init(t_md5_ctx *ctx) +{ + ctx->a = 0x67452301; + ctx->a = 0xefcdab89; + ctx->a = 0x98badcfe; + ctx->a = 0x10325476; + ft_bzero(ctx->buffer, FT_MD5_BUFFER_SIZE); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index c9adfd03..dc7332de 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,10 @@ #include #include #include +#include "libft.h" #ifndef PROTOTYPES -#define PROTOTYPES 0 +#define PROTOTYPES 1 #endif /* PROTO_LIST is defined depending on how PROTOTYPES is defined above. @@ -46,7 +47,7 @@ typedef unsigned char *POINTER; typedef unsigned short int UINT2; /* UINT4 defines a four byte word */ -typedef unsigned long int UINT4; +typedef unsigned int UINT4; /* MD5 context. */ typedef struct @@ -66,7 +67,6 @@ void MD5Final PROTO_LIST((unsigned char[16], MD5_CTX *)); #define TEST_BLOCK_COUNT 1000 static void MDString PROTO_LIST((char *)); -static void MDTimeTrial PROTO_LIST((void)); static void MDTestSuite PROTO_LIST((void)); static void MDFile PROTO_LIST((char *)); static void MDFilter PROTO_LIST((void)); @@ -127,8 +127,7 @@ Rotation is separate from addition to prevent recomputation. /* MD5 initialization. Begins an MD5 operation, writing a new context. */ -void MD5Init(context) - MD5_CTX *context; /* context */ +void MD5Init(MD5_CTX *context) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants.*/ @@ -136,16 +135,14 @@ void MD5Init(context) context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476; + ft_bzero(context->buffer, 64); } /* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */ -void MD5Update(context, input, inputLen) - MD5_CTX *context; /* context */ - unsigned char *input; /* input block */ - unsigned int inputLen; /* length of input block */ +void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) { unsigned int i, index, partLen; @@ -181,9 +178,7 @@ void MD5Update(context, input, inputLen) /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ -void MD5Final(digest, context) -unsigned char digest[16]; /* message digest */ -MD5_CTX *context; /* context */ +void MD5Final(unsigned char digest[16], MD5_CTX *context) { unsigned char bits[8]; unsigned int index, padLen; @@ -202,15 +197,14 @@ MD5_CTX *context; /* context */ /* Store state in digest */ Encode(digest, context->state, 16); - /* Zeroize sensitive information. -*/ + /* Zeroize sensitive information.*/ MD5_memset((POINTER)context, 0, sizeof(*context)); } /* MD5 basic transformation. Transforms state based on block. */ static void MD5Transform(state, block) - UINT4 state[4]; +UINT4 state[4]; unsigned char block[64]; { UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; @@ -341,7 +335,7 @@ unsigned int len; output[i] = input[i]; } -/* Note: Replace "for loop" with standard memset if possible. +/* Note: Replace "for l */ static void MD5_memset(output, value, len) POINTER output; @@ -363,7 +357,7 @@ static void MDString(string) char *string; unsigned int len = strlen(string); MDInit(&context); - MDUpdate(&context, string, len); + MDUpdate(&context, (unsigned char *)string, len); MDFinal(digest, &context); printf("MD5 (\"%s\") = ", string); @@ -371,24 +365,88 @@ static void MDString(string) char *string; printf("\n"); } -int main(argc, argv) -int argc; -char *argv[]; +/* Digests the standard input and prints the result. + */ +static void MDFilter() { - int i; + MD_CTX context; + int len; + unsigned char buffer[16], digest[16]; + + MDInit(&context); + while ((len = fread(buffer, 1, 16, stdin))) + MDUpdate(&context, buffer, len); + MDFinal(digest, &context); + + MDPrint(digest); + printf("\n"); +} + +/* Prints a message digest in hexadecimal. + */ +static void MDPrint(digest) unsigned char digest[16]; +{ + unsigned int i; + + for (i = 0; i < 16; i++) + printf("%02x", digest[i]); +} + +static void MDFile(filename) char *filename; +{ + FILE *file; + MD_CTX context; + int len; + unsigned char buffer[1024], digest[16]; + + if ((file = fopen(filename, "rb")) == NULL) + printf("%s can't be opened\n", filename); - if (argc > 1) - for (i = 1; i < argc; i++) - if (argv[i][0] == '-' && argv[i][1] == 's') - MDString(argv[i] + 2); - else if (strcmp(argv[i], "-t") == 0) - MDTimeTrial(); - else if (strcmp(argv[i], "-x") == 0) - MDTestSuite(); - else - MDFile(argv[i]); else - MDFilter(); + { + MDInit(&context); + while ((len = fread(buffer, 1, 1024, file))) + MDUpdate(&context, buffer, len); + MDFinal(digest, &context); - return (0); -} \ No newline at end of file + fclose(file); + + printf("MD5 (%s) = ", filename); + MDPrint(digest); + printf("\n"); + } + } + + /* Digests a reference suite of strings and prints the results. + */ + static void MDTestSuite() + { + printf("MD5 test suite:\n"); + + MDString((char *)""); + MDString((char *)"a"); + MDString((char *)"abc"); + MDString((char *)"message digest"); + MDString((char *)"abcdefghijklmnopqrstuvwxyz"); + MDString((char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + MDString((char *)"1234567890123456789012345678901234567890\ +1234567890123456789012345678901234567890"); + } + + int main(int argc, char **argv) + { + int i; + + if (argc > 1) + for (i = 1; i < argc; i++) + if (argv[i][0] == '-' && argv[i][1] == 's') + MDString(argv[i] + 2); + else if (strcmp(argv[i], "-x") == 0) + MDTestSuite(); + else + MDFile(argv[i]); + else + MDFilter(); + + return (0); + } \ No newline at end of file diff --git a/t/main_test.c b/t/main_test.c deleted file mode 100644 index c4289b43..00000000 --- a/t/main_test.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "tests.h" - -MunitTest test_tests[] = { - IT("/should_pass", should_pass, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL), - IT("/should_fail", should_fail, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL), - IT("/should_skip", should_skip, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL), - END_IT -}; - -static const MunitSuite suite = { - (char *)"/test_tests_suite", /* name */ - test_tests, /* tests */ - NULL, /* suites */ - 1, /* iterations */ - MUNIT_SUITE_OPTION_NONE /* options */ -}; - -int main(int argc, char** argv) -{ - return munit_suite_main(&suite, NULL, argc, argv); -} diff --git a/t/md5_tests.c b/t/md5_tests.c new file mode 100644 index 00000000..f398d29b --- /dev/null +++ b/t/md5_tests.c @@ -0,0 +1,20 @@ +#include "tests.h" +#include "ft_md5.h" + +TEST_RESULT should_init_ctx(TEST_PARAMS, TEST_DATA) +{ + UNUSED(test_params); + UNUSED(test_data); + t_md5_ctx ctx; + + ft_md5_init(&ctx); + + // munit_assert_true(ctx.a == 0x67452301); + // munit_assert_uint(ctx.b, ==, 0xefcdab89); + // munit_assert_uint(ctx.c, ==, 0x98badcfe); + // munit_assert_uint(ctx.d, ==, 0x10325476); + for (int i = 0; i < 64; i++) + munit_assert_uchar(ctx.buffer[i], ==, 0); + + return MUNIT_OK; +} \ No newline at end of file diff --git a/t/test_tests.c b/t/test_tests.c deleted file mode 100644 index f1a3b833..00000000 --- a/t/test_tests.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "tests.h" - -TEST_RESULT should_pass(TEST_PARAMS, TEST_DATA) -{ - UNUSED(test_params); - UNUSED(test_data); - return MUNIT_OK; -} - -TEST_RESULT should_fail(TEST_PARAMS, TEST_DATA) -{ - UNUSED(test_params); - UNUSED(test_data); - return MUNIT_SKIP; - return MUNIT_FAIL; -} - -TEST_RESULT should_skip(TEST_PARAMS, TEST_DATA) -{ - UNUSED(test_params); - UNUSED(test_data); - return MUNIT_SKIP; -} \ No newline at end of file diff --git a/t/tests.c b/t/tests.c new file mode 100644 index 00000000..efc0c3ae --- /dev/null +++ b/t/tests.c @@ -0,0 +1,19 @@ +#include "tests.h" + +MunitTest md5_tests[] = { + IT("/zeroes_ctx", should_init_ctx, NULL, NULL, 0, NULL), + END_IT +}; + +static const MunitSuite suite = { + (char *)"/md5_suite", /* name */ + md5_tests, /* tests */ + NULL, /* suites */ + 1, /* iterations */ + MUNIT_SUITE_OPTION_NONE /* options */ +}; + +int main(int argc, char** argv) +{ + return munit_suite_main(&suite, NULL, argc, argv); +} \ No newline at end of file