working copy
This commit is contained in:
parent
c1bf1b2193
commit
8f1c5c61bd
9 changed files with 179 additions and 87 deletions
17
Makefile
17
Makefile
|
@ -36,8 +36,10 @@ MUINUT_INC := -I $(MUINUT_DIR)
|
||||||
|
|
||||||
# project source files
|
# project source files
|
||||||
|
|
||||||
SRC = main.c
|
MD5_SRC = ft_md5_init.c
|
||||||
|
|
||||||
|
SRC = main.c
|
||||||
|
SRC += $(MD5_SRC)
|
||||||
# project object files
|
# project object files
|
||||||
|
|
||||||
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
||||||
|
@ -47,10 +49,12 @@ OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
||||||
|
|
||||||
TEST_BIN = ft_ssl_test
|
TEST_BIN = ft_ssl_test
|
||||||
|
|
||||||
TEST_SRC = main_test.c \
|
MD5_TESTS = md5_tests.c
|
||||||
test_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))
|
TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o))
|
||||||
|
|
||||||
|
@ -80,12 +84,13 @@ endif
|
||||||
|
|
||||||
# linking flags
|
# linking flags
|
||||||
|
|
||||||
LINK_FLAGS = ""
|
LINK_FLAGS = $(LIBFT_LIB)
|
||||||
|
|
||||||
# header flags
|
# header flags
|
||||||
|
|
||||||
HEADER_FLAGS = -I $(INC_DIR) \
|
HEADER_FLAGS = -I $(INC_DIR) \
|
||||||
$(MUINUT_INC)
|
$(MUINUT_INC) \
|
||||||
|
$(LIBFT_INC)
|
||||||
|
|
||||||
# compiler
|
# compiler
|
||||||
|
|
||||||
|
|
26
inc/ft_md5.h
Normal file
26
inc/ft_md5.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef FT_MD5_H
|
||||||
|
# define FT_MD5_H
|
||||||
|
|
||||||
|
# include <stdint.h>
|
||||||
|
|
||||||
|
# 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
|
|
@ -6,8 +6,6 @@
|
||||||
#include "tests_macros.h"
|
#include "tests_macros.h"
|
||||||
|
|
||||||
// test tests
|
// test tests
|
||||||
CASE(should_pass);
|
CASE(should_init_ctx);
|
||||||
CASE(should_skip);
|
|
||||||
CASE(should_fail);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
10
src/ft_md5_init.c
Normal file
10
src/ft_md5_init.c
Normal file
|
@ -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);
|
||||||
|
}
|
126
src/main.c
126
src/main.c
|
@ -1,9 +1,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
#ifndef PROTOTYPES
|
#ifndef PROTOTYPES
|
||||||
#define PROTOTYPES 0
|
#define PROTOTYPES 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
|
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
|
||||||
|
@ -46,7 +47,7 @@ typedef unsigned char *POINTER;
|
||||||
typedef unsigned short int UINT2;
|
typedef unsigned short int UINT2;
|
||||||
|
|
||||||
/* UINT4 defines a four byte word */
|
/* UINT4 defines a four byte word */
|
||||||
typedef unsigned long int UINT4;
|
typedef unsigned int UINT4;
|
||||||
|
|
||||||
/* MD5 context. */
|
/* MD5 context. */
|
||||||
typedef struct
|
typedef struct
|
||||||
|
@ -66,7 +67,6 @@ void MD5Final PROTO_LIST((unsigned char[16], MD5_CTX *));
|
||||||
#define TEST_BLOCK_COUNT 1000
|
#define TEST_BLOCK_COUNT 1000
|
||||||
|
|
||||||
static void MDString PROTO_LIST((char *));
|
static void MDString PROTO_LIST((char *));
|
||||||
static void MDTimeTrial PROTO_LIST((void));
|
|
||||||
static void MDTestSuite PROTO_LIST((void));
|
static void MDTestSuite PROTO_LIST((void));
|
||||||
static void MDFile PROTO_LIST((char *));
|
static void MDFile PROTO_LIST((char *));
|
||||||
static void MDFilter PROTO_LIST((void));
|
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.
|
/* MD5 initialization. Begins an MD5 operation, writing a new context.
|
||||||
*/
|
*/
|
||||||
void MD5Init(context)
|
void MD5Init(MD5_CTX *context)
|
||||||
MD5_CTX *context; /* context */
|
|
||||||
{
|
{
|
||||||
context->count[0] = context->count[1] = 0;
|
context->count[0] = context->count[1] = 0;
|
||||||
/* Load magic initialization constants.*/
|
/* Load magic initialization constants.*/
|
||||||
|
@ -136,16 +135,14 @@ void MD5Init(context)
|
||||||
context->state[1] = 0xefcdab89;
|
context->state[1] = 0xefcdab89;
|
||||||
context->state[2] = 0x98badcfe;
|
context->state[2] = 0x98badcfe;
|
||||||
context->state[3] = 0x10325476;
|
context->state[3] = 0x10325476;
|
||||||
|
ft_bzero(context->buffer, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* MD5 block update operation. Continues an MD5 message-digest
|
/* MD5 block update operation. Continues an MD5 message-digest
|
||||||
operation, processing another message block, and updating the
|
operation, processing another message block, and updating the
|
||||||
context.
|
context.
|
||||||
*/
|
*/
|
||||||
void MD5Update(context, input, inputLen)
|
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
|
||||||
MD5_CTX *context; /* context */
|
|
||||||
unsigned char *input; /* input block */
|
|
||||||
unsigned int inputLen; /* length of input block */
|
|
||||||
{
|
{
|
||||||
unsigned int i, index, partLen;
|
unsigned int i, index, partLen;
|
||||||
|
|
||||||
|
@ -181,9 +178,7 @@ void MD5Update(context, input, inputLen)
|
||||||
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||||
the message digest and zeroizing the context.
|
the message digest and zeroizing the context.
|
||||||
*/
|
*/
|
||||||
void MD5Final(digest, context)
|
void MD5Final(unsigned char digest[16], MD5_CTX *context)
|
||||||
unsigned char digest[16]; /* message digest */
|
|
||||||
MD5_CTX *context; /* context */
|
|
||||||
{
|
{
|
||||||
unsigned char bits[8];
|
unsigned char bits[8];
|
||||||
unsigned int index, padLen;
|
unsigned int index, padLen;
|
||||||
|
@ -202,15 +197,14 @@ MD5_CTX *context; /* context */
|
||||||
/* Store state in digest */
|
/* Store state in digest */
|
||||||
Encode(digest, context->state, 16);
|
Encode(digest, context->state, 16);
|
||||||
|
|
||||||
/* Zeroize sensitive information.
|
/* Zeroize sensitive information.*/
|
||||||
*/
|
|
||||||
MD5_memset((POINTER)context, 0, sizeof(*context));
|
MD5_memset((POINTER)context, 0, sizeof(*context));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* MD5 basic transformation. Transforms state based on block.
|
/* MD5 basic transformation. Transforms state based on block.
|
||||||
*/
|
*/
|
||||||
static void MD5Transform(state, block)
|
static void MD5Transform(state, block)
|
||||||
UINT4 state[4];
|
UINT4 state[4];
|
||||||
unsigned char block[64];
|
unsigned char block[64];
|
||||||
{
|
{
|
||||||
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
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];
|
output[i] = input[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Note: Replace "for loop" with standard memset if possible.
|
/* Note: Replace "for l
|
||||||
*/
|
*/
|
||||||
static void MD5_memset(output, value, len)
|
static void MD5_memset(output, value, len)
|
||||||
POINTER output;
|
POINTER output;
|
||||||
|
@ -363,7 +357,7 @@ static void MDString(string) char *string;
|
||||||
unsigned int len = strlen(string);
|
unsigned int len = strlen(string);
|
||||||
|
|
||||||
MDInit(&context);
|
MDInit(&context);
|
||||||
MDUpdate(&context, string, len);
|
MDUpdate(&context, (unsigned char *)string, len);
|
||||||
MDFinal(digest, &context);
|
MDFinal(digest, &context);
|
||||||
|
|
||||||
printf("MD5 (\"%s\") = ", string);
|
printf("MD5 (\"%s\") = ", string);
|
||||||
|
@ -371,24 +365,88 @@ static void MDString(string) char *string;
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(argc, argv)
|
/* Digests the standard input and prints the result.
|
||||||
int argc;
|
*/
|
||||||
char *argv[];
|
static void MDFilter()
|
||||||
{
|
{
|
||||||
int i;
|
MD_CTX context;
|
||||||
|
int len;
|
||||||
|
unsigned char buffer[16], digest[16];
|
||||||
|
|
||||||
if (argc > 1)
|
MDInit(&context);
|
||||||
for (i = 1; i < argc; i++)
|
while ((len = fread(buffer, 1, 16, stdin)))
|
||||||
if (argv[i][0] == '-' && argv[i][1] == 's')
|
MDUpdate(&context, buffer, len);
|
||||||
MDString(argv[i] + 2);
|
MDFinal(digest, &context);
|
||||||
else if (strcmp(argv[i], "-t") == 0)
|
|
||||||
MDTimeTrial();
|
|
||||||
else if (strcmp(argv[i], "-x") == 0)
|
|
||||||
MDTestSuite();
|
|
||||||
else
|
|
||||||
MDFile(argv[i]);
|
|
||||||
else
|
|
||||||
MDFilter();
|
|
||||||
|
|
||||||
return (0);
|
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);
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MDInit(&context);
|
||||||
|
while ((len = fread(buffer, 1, 1024, file)))
|
||||||
|
MDUpdate(&context, buffer, len);
|
||||||
|
MDFinal(digest, &context);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -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);
|
|
||||||
}
|
|
20
t/md5_tests.c
Normal file
20
t/md5_tests.c
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
19
t/tests.c
Normal file
19
t/tests.c
Normal file
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in a new issue