complete md5 algorythm
This commit is contained in:
parent
d65d5bf11b
commit
3197dff3a9
16 changed files with 538 additions and 480 deletions
6
.editorconfig
Normal file
6
.editorconfig
Normal file
|
@ -0,0 +1,6 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
10
Makefile
10
Makefile
|
@ -36,7 +36,15 @@ MUINUT_INC := -I $(MUINUT_DIR)
|
|||
|
||||
# project source files
|
||||
|
||||
MD5_SRC = ft_md5_init.c
|
||||
MD5_SRC = ft_md5_init.c \
|
||||
ft_md5_update.c \
|
||||
ft_md5_transform.c \
|
||||
ft_md5_decode.c \
|
||||
ft_md5_encode_len.c \
|
||||
ft_md5_encode_register.c \
|
||||
ft_md5_final.c \
|
||||
ft_md5_padding.c \
|
||||
ft_md5_digest_string.c
|
||||
|
||||
SRC = main.c
|
||||
SRC += $(MD5_SRC)
|
||||
|
|
90
inc/ft_md5.h
90
inc/ft_md5.h
|
@ -3,9 +3,73 @@
|
|||
|
||||
# include <stdint.h>
|
||||
|
||||
# define FT_MD5_BUFFER_SIZE 64
|
||||
# define FT_MD5_BLOCK_SIZE 64
|
||||
# define FT_MD5_WORDS_COUNT 16
|
||||
# define FT_MD5_MESSAGE_LENGTH_BYTE 8
|
||||
# define FT_MD5_DIGEST_LENGTH_BYTE 16
|
||||
# define FT_MD5_STRING_SIZE_BYTE 33
|
||||
|
||||
typedef unsigned int BYTE4;
|
||||
# define S11 7
|
||||
# define S12 12
|
||||
# define S13 17
|
||||
# define S14 22
|
||||
# define S21 5
|
||||
# define S22 9
|
||||
# define S23 14
|
||||
# define S24 20
|
||||
# define S31 4
|
||||
# define S32 11
|
||||
# define S33 16
|
||||
# define S34 23
|
||||
# define S41 6
|
||||
# define S42 10
|
||||
# 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))))
|
||||
|
||||
/* 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 GG(a, b, c, d, x, s, ac) \
|
||||
{ \
|
||||
(a) += G((b), (c), (d)) + (x) + (BYTE4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(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 II(a, b, c, d, x, s, ac) \
|
||||
{ \
|
||||
(a) += I((b), (c), (d)) + (x) + (BYTE4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
typedef uint64_t BYTE8;
|
||||
typedef uint32_t BYTE4;
|
||||
typedef unsigned char BYTE1;
|
||||
|
||||
typedef struct s_md5_ctx
|
||||
|
@ -14,13 +78,25 @@ typedef struct s_md5_ctx
|
|||
BYTE4 b;
|
||||
BYTE4 c;
|
||||
BYTE4 d;
|
||||
BYTE1 buffer[FT_MD5_BUFFER_SIZE];
|
||||
uint64_t message_len;
|
||||
BYTE1 block[FT_MD5_BLOCK_SIZE];
|
||||
BYTE8 bit_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);
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -5,7 +5,13 @@
|
|||
#include "munit.h"
|
||||
#include "tests_macros.h"
|
||||
|
||||
// test tests
|
||||
// md5 test 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);
|
||||
|
||||
#endif
|
20
src/ft_md5_decode.c
Normal file
20
src/ft_md5_decode.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "ft_md5.h"
|
||||
|
||||
void ft_md5_decode(BYTE4 w[FT_MD5_WORDS_COUNT], BYTE1 b[FT_MD5_BLOCK_SIZE])
|
||||
{
|
||||
BYTE8 i;
|
||||
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;
|
||||
}
|
||||
}
|
42
src/ft_md5_digest_string.c
Normal file
42
src/ft_md5_digest_string.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
static void init_chars(BYTE1 chars[16])
|
||||
{
|
||||
chars[0] = '0';
|
||||
chars[1] = '1';
|
||||
chars[2] = '2';
|
||||
chars[3] = '3';
|
||||
chars[4] = '4';
|
||||
chars[5] = '5';
|
||||
chars[6] = '6';
|
||||
chars[7] = '7';
|
||||
chars[8] = '8';
|
||||
chars[9] = '9';
|
||||
chars[10] = 'a';
|
||||
chars[11] = 'b';
|
||||
chars[12] = 'c';
|
||||
chars[13] = 'd';
|
||||
chars[14] = 'e';
|
||||
chars[15] = 'f';
|
||||
}
|
||||
|
||||
void ft_md5_digest_string(BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE],
|
||||
BYTE1 string[FT_MD5_STRING_SIZE_BYTE])
|
||||
{
|
||||
BYTE8 i;
|
||||
BYTE8 j;
|
||||
BYTE1 chars[16];
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
init_chars(chars);
|
||||
while(i < FT_MD5_DIGEST_LENGTH_BYTE)
|
||||
{
|
||||
string[j] = chars[digest[i] / 16];
|
||||
string[j + 1] = chars[digest[i] % 16];
|
||||
i++;
|
||||
j += 2;
|
||||
}
|
||||
string[FT_MD5_STRING_SIZE_BYTE - 1] = 0;
|
||||
}
|
13
src/ft_md5_encode_len.c
Normal file
13
src/ft_md5_encode_len.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "ft_md5.h"
|
||||
|
||||
void ft_md5_encode_len(BYTE1 bits[FT_MD5_MESSAGE_LENGTH_BYTE], 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);
|
||||
}
|
9
src/ft_md5_encode_register.c
Normal file
9
src/ft_md5_encode_register.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include "ft_md5.h"
|
||||
|
||||
void ft_md5_encode_register(BYTE1 *digest_part, 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);
|
||||
}
|
23
src/ft_md5_final.c
Normal file
23
src/ft_md5_final.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_md5_final(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;
|
||||
|
||||
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);
|
||||
ft_md5_encode_register(digest, ctx->a);
|
||||
ft_md5_encode_register(&digest[4], ctx->b);
|
||||
ft_md5_encode_register(&digest[8], ctx->c);
|
||||
ft_md5_encode_register(&digest[12], ctx->d);
|
||||
ft_bzero(ctx, sizeof(t_md5_ctx));
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
#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);
|
||||
ctx->b = 0xefcdab89;
|
||||
ctx->c = 0x98badcfe;
|
||||
ctx->d = 0x10325476;
|
||||
ctx->bit_len = 0;
|
||||
ft_bzero(ctx->block, FT_MD5_BLOCK_SIZE);
|
||||
}
|
8
src/ft_md5_padding.c
Normal file
8
src/ft_md5_padding.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_md5_padding(BYTE1 padding[FT_MD5_BLOCK_SIZE])
|
||||
{
|
||||
padding[0] = 0x80;
|
||||
ft_bzero(&padding[1], FT_MD5_BLOCK_SIZE - 1);
|
||||
}
|
97
src/ft_md5_transform.c
Normal file
97
src/ft_md5_transform.c
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_md5_transform(t_md5_ctx *ctx, BYTE1 block[FT_MD5_BLOCK_SIZE])
|
||||
{
|
||||
BYTE4 words[FT_MD5_WORDS_COUNT];
|
||||
BYTE4 a;
|
||||
BYTE4 b;
|
||||
BYTE4 c;
|
||||
BYTE4 d;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
ft_md5_decode(words, block);
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* 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));
|
||||
}
|
28
src/ft_md5_update.c
Normal file
28
src/ft_md5_update.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_md5_update(t_md5_ctx *c, BYTE1 * message, BYTE8 message_len)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int part_block_len;
|
||||
unsigned int cuurent_block_index;
|
||||
|
||||
cuurent_block_index = (c->bit_len / 8) % 64;
|
||||
c->bit_len += message_len * 8;
|
||||
part_block_len = FT_MD5_BLOCK_SIZE - cuurent_block_index;
|
||||
if (message_len >= part_block_len)
|
||||
{
|
||||
ft_memcpy(&c->block[cuurent_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, &c->block[i]);
|
||||
i += FT_MD5_BLOCK_SIZE;
|
||||
}
|
||||
cuurent_block_index = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
ft_memcpy(&c->block[cuurent_block_index], &message[i], message_len - i);
|
||||
}
|
456
src/main.c
456
src/main.c
|
@ -1,459 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include "libft.h"
|
||||
|
||||
#ifndef PROTOTYPES
|
||||
#define PROTOTYPES 1
|
||||
#endif
|
||||
|
||||
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
|
||||
If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
|
||||
returns an empty list.
|
||||
*/
|
||||
#if PROTOTYPES
|
||||
#define PROTO_LIST(list) list
|
||||
#else
|
||||
#define PROTO_LIST(list) ()
|
||||
#endif
|
||||
|
||||
#define S11 7
|
||||
#define S12 12
|
||||
#define S13 17
|
||||
#define S14 22
|
||||
#define S21 5
|
||||
#define S22 9
|
||||
#define S23 14
|
||||
#define S24 20
|
||||
#define S31 4
|
||||
#define S32 11
|
||||
#define S33 16
|
||||
#define S34 23
|
||||
#define S41 6
|
||||
#define S42 10
|
||||
#define S43 15
|
||||
#define S44 21
|
||||
|
||||
#define MD MD5
|
||||
#define MD_CTX MD5_CTX
|
||||
#define MDInit MD5Init
|
||||
#define MDUpdate MD5Update
|
||||
#define MDFinal MD5Final
|
||||
|
||||
#define MD5_BLOCK_SIZE 64
|
||||
|
||||
/* POINTER defines a generic pointer type */
|
||||
typedef unsigned char *POINTER;
|
||||
|
||||
/* UINT4 defines a four byte word */
|
||||
typedef uint32_t UINT4;
|
||||
|
||||
/* UINT4 defines a four byte word */
|
||||
typedef uint64_t UINT8;
|
||||
|
||||
/* MD5 context. */
|
||||
typedef struct
|
||||
int main()
|
||||
{
|
||||
UINT4 a;
|
||||
UINT4 b;
|
||||
UINT4 c;
|
||||
UINT4 d; /* state (ABCD) */
|
||||
UINT8 count; /* number of bits, modulo 2^64 (lsb first) */
|
||||
unsigned char buffer[MD5_BLOCK_SIZE]; /* input buffer */
|
||||
} MD5_CTX;
|
||||
|
||||
void MD5Init PROTO_LIST((MD5_CTX *));
|
||||
void MD5Update PROTO_LIST((MD5_CTX *, unsigned char *, unsigned int));
|
||||
void MD5Final PROTO_LIST((unsigned char[16], MD5_CTX *));
|
||||
|
||||
/* Length of test block, number of test blocks.
|
||||
*/
|
||||
#define TEST_BLOCK_LEN 1000
|
||||
#define TEST_BLOCK_COUNT 1000
|
||||
|
||||
static void MDString PROTO_LIST((char *));
|
||||
static void MDTestSuite PROTO_LIST((void));
|
||||
static void MDFile PROTO_LIST((char *));
|
||||
static void MDFilter PROTO_LIST((void));
|
||||
static void MDPrint PROTO_LIST((unsigned char[16]));
|
||||
|
||||
static void MD5Transform PROTO_LIST((UINT4[4], unsigned char[64]));
|
||||
static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int));
|
||||
static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int));
|
||||
static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int));
|
||||
static void MD5_memset PROTO_LIST((POINTER, int, unsigned int));
|
||||
|
||||
static unsigned char PADDING[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
/* 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))))
|
||||
|
||||
/* 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) + (UINT4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
#define GG(a, b, c, d, x, s, ac) \
|
||||
{ \
|
||||
(a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
#define HH(a, b, c, d, x, s, ac) \
|
||||
{ \
|
||||
(a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
#define II(a, b, c, d, x, s, ac) \
|
||||
{ \
|
||||
(a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \
|
||||
(a) = ROTATE_LEFT((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
/* MD5 initialization. Begins an MD5 operation, writing a new context.
|
||||
*/
|
||||
void MD5Init(MD5_CTX *context)
|
||||
{
|
||||
context->count = 0;
|
||||
/* Load magic initialization constants.*/
|
||||
context->a = 0x67452301;
|
||||
context->b = 0xefcdab89;
|
||||
context->c = 0x98badcfe;
|
||||
context->d = 0x10325476;
|
||||
ft_bzero(context->buffer, MD5_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
/* MD5 block update operation. Continues an MD5 message-digest
|
||||
operation, processing another message block, and updating the
|
||||
context.
|
||||
*/
|
||||
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
|
||||
{
|
||||
unsigned int i, index, partLen;
|
||||
|
||||
/* Compute number of bytes mod 64 */
|
||||
index = (unsigned int)((context->count / 8) % 64);
|
||||
|
||||
context->count += inputLen * 8;
|
||||
|
||||
partLen = MD5_BLOCK_SIZE - index;
|
||||
|
||||
/* Transform as many times as possible.*/
|
||||
if (inputLen >= partLen)
|
||||
{
|
||||
MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
|
||||
MD5Transform(context, context->buffer);
|
||||
|
||||
for (i = partLen; i + 63 < inputLen; i += MD5_BLOCK_SIZE)
|
||||
MD5Transform(context, &input[i]);
|
||||
|
||||
index = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
|
||||
/* Buffer remaining input */
|
||||
MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],
|
||||
inputLen - i);
|
||||
}
|
||||
|
||||
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||
the message digest and zeroizing the context.
|
||||
*/
|
||||
void MD5Final(unsigned char digest[16], MD5_CTX *context)
|
||||
{
|
||||
unsigned char bits[8];
|
||||
unsigned int index, padLen;
|
||||
|
||||
/* Save number of bits */
|
||||
bits[0] = (unsigned char)(context->count & 0xff);
|
||||
bits[1] = (unsigned char)(context->count >> 8 & 0xff);
|
||||
bits[2] = (unsigned char)(context->count >> 16 & 0xff);
|
||||
bits[3] = (unsigned char)(context->count >> 24 & 0xff);
|
||||
bits[4] = (unsigned char)(context->count >> 32 & 0xff);
|
||||
bits[5] = (unsigned char)(context->count >> 40 & 0xff);
|
||||
bits[6] = (unsigned char)(context->count >> 48 & 0xff);
|
||||
bits[7] = (unsigned char)(context->count >> 56 & 0xff);
|
||||
|
||||
/* Pad out to 56 mod 64. */
|
||||
index = (unsigned int)((context->count / 8) % 64);
|
||||
|
||||
padLen = (index < 56) ? (56 - index) : (120 - index);
|
||||
MD5Update(context, PADDING, padLen);
|
||||
|
||||
/* Append length (before padding) */
|
||||
MD5Update(context, bits, 8);
|
||||
/* Store state in digest */
|
||||
Encode(digest, context->state, 16);
|
||||
|
||||
/* Zeroize sensitive information.*/
|
||||
MD5_memset((POINTER)context, 0, sizeof(*context));
|
||||
}
|
||||
|
||||
/* MD5 basic transformation. Transforms state based on block.
|
||||
*/
|
||||
static void MD5Transform(MD5_CTX *ctx, unsigned char block[MD5_BLOCK_SIZE])
|
||||
{
|
||||
UINT4 a = ctx->a, b = ctx->b, c = ctx->c, d = ctx->d, x[16];
|
||||
|
||||
Decode(x, block, MD5_BLOCK_SIZE);
|
||||
|
||||
/* Round 1 */
|
||||
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
|
||||
FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
|
||||
FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
|
||||
FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
|
||||
FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
|
||||
FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
|
||||
FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
|
||||
FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
|
||||
FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
|
||||
FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
|
||||
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
||||
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
||||
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
||||
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
||||
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
||||
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
||||
|
||||
/* Round 2 */
|
||||
GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
|
||||
GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
|
||||
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
||||
GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
|
||||
GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
|
||||
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
||||
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
||||
GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
|
||||
GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
|
||||
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
||||
GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
|
||||
GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
|
||||
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
||||
GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
|
||||
GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
|
||||
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
||||
|
||||
/* Round 3 */
|
||||
HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
|
||||
HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
|
||||
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
||||
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
||||
HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
|
||||
HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
|
||||
HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
|
||||
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
||||
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
||||
HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
|
||||
HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
|
||||
HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
|
||||
HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
|
||||
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
||||
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
||||
HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
|
||||
|
||||
/* Round 4 */
|
||||
II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
|
||||
II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
|
||||
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
||||
II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
|
||||
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
||||
II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
|
||||
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
||||
II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
|
||||
II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
|
||||
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
||||
II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
|
||||
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
||||
II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
|
||||
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
||||
II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
|
||||
II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
|
||||
|
||||
ctx->a += a;
|
||||
ctx->b += b;
|
||||
ctx->c += c;
|
||||
ctx->d += d;
|
||||
/* Zeroize sensitive information.*/
|
||||
MD5_memset((POINTER)x, 0, sizeof(x));
|
||||
}
|
||||
|
||||
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
|
||||
a multiple of 4.
|
||||
*/
|
||||
static void Encode(output, input, len) unsigned char *output;
|
||||
UINT4 *input;
|
||||
unsigned int len;
|
||||
{
|
||||
unsigned int i, j;
|
||||
|
||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||
{
|
||||
output[j] = (unsigned char)(input[i] & 0xff);
|
||||
output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
|
||||
output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
|
||||
output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
|
||||
a multiple of 4.
|
||||
*/
|
||||
static void Decode(output, input, len)
|
||||
UINT4 *output;
|
||||
unsigned char *input;
|
||||
unsigned int len;
|
||||
{
|
||||
unsigned int i, j;
|
||||
|
||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||
output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |
|
||||
(((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);
|
||||
}
|
||||
|
||||
static void MD5_memcpy(output, input, len)
|
||||
POINTER output;
|
||||
POINTER input;
|
||||
unsigned int len;
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
output[i] = input[i];
|
||||
}
|
||||
|
||||
/* Note: Replace "for l
|
||||
*/
|
||||
static void MD5_memset(output, value, len)
|
||||
POINTER output;
|
||||
int value;
|
||||
unsigned int len;
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
((char *)output)[i] = (char)value;
|
||||
}
|
||||
|
||||
/* Digests a string and prints the result.
|
||||
*/
|
||||
static void MDString(string) char *string;
|
||||
{
|
||||
MD_CTX context;
|
||||
unsigned char digest[16];
|
||||
unsigned int len = strlen(string);
|
||||
|
||||
MDInit(&context);
|
||||
MDUpdate(&context, (unsigned char *)string, len);
|
||||
MDFinal(digest, &context);
|
||||
|
||||
printf("MD5 (\"%s\") = ", string);
|
||||
MDPrint(digest);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* Digests the standard input and prints the result.
|
||||
*/
|
||||
static void MDFilter()
|
||||
{
|
||||
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);
|
||||
|
||||
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();
|
||||
|
||||
printf("hello from sha256");
|
||||
return(0);
|
||||
}
|
176
t/md5_tests.c
176
t/md5_tests.c
|
@ -1,5 +1,6 @@
|
|||
#include "tests.h"
|
||||
#include "ft_md5.h"
|
||||
#include "libft.h"
|
||||
|
||||
TEST_RESULT should_init_ctx(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
|
@ -9,12 +10,177 @@ TEST_RESULT should_init_ctx(TEST_PARAMS, TEST_DATA)
|
|||
|
||||
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);
|
||||
munit_assert_true(ctx.a == 0x67452301);
|
||||
munit_assert_uint(ctx.b, ==, 0xefcdab89);
|
||||
munit_assert_uint(ctx.c, ==, 0x98badcfe);
|
||||
munit_assert_uint(ctx.d, ==, 0x10325476);
|
||||
munit_assert_true(ctx.bit_len == 0);
|
||||
for (int i = 0; i < 64; i++)
|
||||
munit_assert_uchar(ctx.buffer[i], ==, 0);
|
||||
munit_assert_uchar(ctx.block[i], ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
ft_bzero(block, FT_MD5_BLOCK_SIZE);
|
||||
block[0] = 'a';
|
||||
block[9] = 'b';
|
||||
block[63] = 'c';
|
||||
|
||||
ft_md5_decode(words, block);
|
||||
|
||||
munit_assert_true((words[0] & 0xff) == 97);
|
||||
munit_assert_true(((words[2] >> 8) & 0xff) == 98);
|
||||
munit_assert_true(((words[15] >> 24) & 0xff) == 99);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT update_should_change_count(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
t_md5_ctx ctx;
|
||||
char message[] = "hello, World!";
|
||||
BYTE8 size = ft_strlen(message);
|
||||
|
||||
ft_md5_init(&ctx);
|
||||
ft_md5_update(&ctx, (BYTE1 *)message, size);
|
||||
|
||||
munit_assert_true(size * 8 == ctx.bit_len);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
/*
|
||||
1111 0000 : 63 - 56
|
||||
0000 0000 : 55 - 48
|
||||
0000 0000 : 47 - 40
|
||||
0000 0000 : 39 - 32
|
||||
0000 0000 : 31 - 24
|
||||
0000 0000 : 23 - 16
|
||||
0110 0000 : 15 - 8
|
||||
1001 0000 : 7 - 0
|
||||
*/
|
||||
len = 17293822569102729360U;
|
||||
|
||||
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));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT encode_register(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
|
||||
BYTE1 digest_part[4];
|
||||
BYTE4 reg;
|
||||
|
||||
/*
|
||||
1000 0000 : 31 - 24
|
||||
1000 0000 : 23 - 16
|
||||
1000 0000 : 15 - 8
|
||||
1000 0000 : 7 - 0
|
||||
*/
|
||||
reg = 2155905152;
|
||||
|
||||
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));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT create_digest(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
|
||||
t_md5_ctx ctx;
|
||||
BYTE1 digest[FT_MD5_DIGEST_LENGTH_BYTE];
|
||||
BYTE1 digest_string[FT_MD5_STRING_SIZE_BYTE];
|
||||
|
||||
BYTE1 case1[] = "";
|
||||
BYTE1 case2[] = "a";
|
||||
BYTE1 case7[] = "1234567890123456789012345678901234567\
|
||||
891234567890123456789012345678901234567890";
|
||||
|
||||
ft_md5_init(&ctx);
|
||||
ft_md5_update(&ctx, case1, ft_strlen((const char *)case1));
|
||||
ft_md5_final(digest, &ctx);
|
||||
ft_md5_digest_string(digest, digest_string);
|
||||
|
||||
munit_assert_string_equal((const char *)digest_string,
|
||||
"d41d8cd98f00b204e9800998ecf8427e");
|
||||
|
||||
ft_md5_init(&ctx);
|
||||
ft_md5_update(&ctx, case2, ft_strlen((const char *)case2));
|
||||
ft_md5_final(digest, &ctx);
|
||||
ft_md5_digest_string(digest, digest_string);
|
||||
|
||||
munit_assert_string_equal((const char *)digest_string,
|
||||
"0cc175b9c0f1b6a831c399e269772661");
|
||||
|
||||
ft_md5_init(&ctx);
|
||||
ft_md5_update(&ctx, case7, ft_strlen((const char *)case7));
|
||||
ft_md5_final(digest, &ctx);
|
||||
ft_md5_digest_string(digest, digest_string);
|
||||
|
||||
munit_assert_string_equal((const char *)digest_string,
|
||||
"2580a0aff7ef5e80f6b5432666530926");
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/* final registers values for empty input */
|
||||
a = 3649838548;
|
||||
b = 78774415;
|
||||
c = 2550759657;
|
||||
d = 2118318316;
|
||||
|
||||
ft_md5_encode_register(digest, a);
|
||||
ft_md5_encode_register(&digest[4], b);
|
||||
ft_md5_encode_register(&digest[8], c);
|
||||
ft_md5_encode_register(&digest[12], d);
|
||||
|
||||
ft_md5_digest_string(digest, digest_string);
|
||||
|
||||
munit_assert_string_equal((const char *)digest_string, "d41d8cd98f00b204e9800998ecf8427e");
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
|
@ -2,6 +2,12 @@
|
|||
|
||||
MunitTest md5_tests[] = {
|
||||
IT("/zeroes_ctx", should_init_ctx, NULL, NULL, 0, NULL),
|
||||
IT("/updates_ctx_count", update_should_change_count, NULL, NULL, 0, NULL),
|
||||
IT("/decode_string_to_int", decode_string_to_int, NULL, NULL, 0, NULL),
|
||||
IT("/encode_bits_to_string", encode_bits_to_string, NULL, NULL, 0, NULL),
|
||||
IT("/encode_register", encode_register, NULL, NULL, 0, NULL),
|
||||
IT("/creates_digest", create_digest, NULL, NULL, 0, NULL),
|
||||
IT("/creates_string", create_string, NULL, NULL, 0, NULL),
|
||||
END_IT
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue