ssl_des/t/md5_tests.c

192 lines
4.7 KiB
C
Raw Normal View History

2018-10-23 21:07:38 +03:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* md5_tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/23 21:06:22 by gtertysh #+# #+# */
/* Updated: 2018/10/23 21:06:33 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "t.h"
2018-10-18 21:57:26 +03:00
#include "ft_md5.h"
2018-10-20 20:56:18 +03:00
#include "libft.h"
2018-10-18 21:57:26 +03:00
static int init_ctx()
2018-10-18 21:57:26 +03:00
{
2018-10-23 21:07:38 +03:00
t_md5_ctx ctx;
2018-10-18 21:57:26 +03:00
2018-10-23 21:07:38 +03:00
ft_md5_init(&ctx);
2018-10-18 21:57:26 +03:00
2019-04-10 17:20:22 +03:00
_IS(ctx.a == 0x67452301);
_IS(ctx.b == 0xefcdab89);
_IS(ctx.c == 0x98badcfe);
_IS(ctx.d == 0x10325476);
_IS(ctx.bit_len == 0);
for (int i = 0; i < FT_MD5_BLOCK_SIZE; i++)
2019-04-10 17:20:22 +03:00
_IS(ctx.block[i] == 0);
2018-10-18 21:57:26 +03:00
2019-04-10 17:20:22 +03:00
_END("init ctx");
2018-10-20 20:56:18 +03:00
}
static int decode_string_to_int()
2018-10-20 20:56:18 +03:00
{
2018-10-23 21:07:38 +03:00
t_byte1 block[FT_MD5_BLOCK_SIZE];
t_byte4 words[FT_MD5_WORDS_COUNT];
2018-10-20 20:56:18 +03:00
2018-10-23 21:07:38 +03:00
ft_bzero(block, FT_MD5_BLOCK_SIZE);
block[0] = 'a';
block[9] = 'b';
block[63] = 'c';
2018-10-20 20:56:18 +03:00
2018-10-23 21:07:38 +03:00
ft_md5_decode(words, block);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_IS((words[0] & 0xff) == 97);
_IS(((words[2] >> 8) & 0xff) == 98);
_IS(((words[15] >> 24) & 0xff) == 99);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("decode string to int");
2018-10-20 20:56:18 +03:00
}
static int update_change_count()
2018-10-20 20:56:18 +03:00
{
2018-10-23 21:07:38 +03:00
t_md5_ctx ctx;
char message[] = "hello, World!";
t_byte8 size = ft_strlen(message);
2018-10-20 20:56:18 +03:00
2018-10-23 21:07:38 +03:00
ft_md5_init(&ctx);
ft_md5_update(&ctx, (t_byte1 *)message, size);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_IS(size * 8 == ctx.bit_len);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("update change count");
2018-10-20 20:56:18 +03:00
}
static int encode_bits_to_string()
2018-10-20 20:56:18 +03:00
{
2018-10-23 20:41:44 +03:00
t_byte8 len;
t_byte1 bits[FT_MD5_MESSAGE_LENGTH_BYTE];
2018-10-20 20:56:18 +03:00
/*
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);
2019-04-10 17:20:22 +03:00
_IS(bits[7] == (t_byte1)((len >> 56) & 0xff));
_IS(bits[0] == (t_byte1)(len & 0xff));
_IS(bits[1] == (t_byte1)((len >> 8) & 0xff));
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("encode bits to string");
2018-10-20 20:56:18 +03:00
}
static int encode_register()
2018-10-20 20:56:18 +03:00
{
2018-10-23 20:41:44 +03:00
t_byte1 digest_part[4];
t_byte4 reg;
2018-10-20 20:56:18 +03:00
/*
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);
2019-04-10 17:20:22 +03:00
_IS(digest_part[0] == (t_byte1)(reg & 0xff));
_IS(digest_part[1] == (t_byte1)((reg >> 8) & 0xff));
_IS(digest_part[2] == (t_byte1)((reg >> 16) & 0xff));
_IS(digest_part[3] == (t_byte1)((reg >> 24) & 0xff));
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("encode register");
2018-10-20 20:56:18 +03:00
}
static int create_digest()
2018-10-20 20:56:18 +03:00
{
t_md5_ctx ctx;
2018-10-23 20:41:44 +03:00
t_byte1 digest[FT_MD5_DIGEST_LENGTH_BYTE];
t_byte1 digest_string[FT_MD5_STRING_SIZE_BYTE];
2018-10-20 20:56:18 +03:00
2018-10-23 20:41:44 +03:00
t_byte1 case1[] = "";
t_byte1 case2[] = "a";
t_byte1 case7[] = "1234567890123456789012345678901234567\
2018-10-20 20:56:18 +03:00
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);
2019-04-10 17:20:22 +03:00
_IS(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
2018-10-20 20:56:18 +03:00
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);
2019-04-10 17:20:22 +03:00
_IS(ft_strcmp((const char *)digest_string,
"0cc175b9c0f1b6a831c399e269772661") == 0);
2018-10-20 20:56:18 +03:00
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);
2019-04-10 17:20:22 +03:00
_IS(ft_strcmp((const char *)digest_string,
"2580a0aff7ef5e80f6b5432666530926") == 0);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("create digest");
2018-10-20 20:56:18 +03:00
}
static int create_digest_string()
2018-10-20 20:56:18 +03:00
{
2018-10-23 20:41:44 +03:00
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;
2018-10-20 20:56:18 +03:00
/* 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);
2019-04-10 17:20:22 +03:00
_IS(ft_strcmp((const char *)digest_string,
"d41d8cd98f00b204e9800998ecf8427e") == 0);
2018-10-20 20:56:18 +03:00
2019-04-10 17:20:22 +03:00
_END("create digest string");
2018-10-23 21:07:38 +03:00
}
int md5_tests()
{
2019-04-10 17:20:22 +03:00
_SHOULD(init_ctx);
_SHOULD(decode_string_to_int);
_SHOULD(update_change_count);
_SHOULD(encode_bits_to_string);
_SHOULD(encode_register);
_SHOULD(create_digest);
_SHOULD(create_digest_string);
return 0;
}