2018-10-23 21:07:38 +03:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* sha_tests.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2018/10/23 21:06:43 by gtertysh #+# #+# */
|
|
|
|
/* Updated: 2018/10/23 21:06:44 by gtertysh ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
#include "t.h"
|
2018-10-21 20:46:05 +03:00
|
|
|
#include "ft_sha.h"
|
|
|
|
#include "libft.h"
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int init_sha256_ctx()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
|
|
|
t_sha256_ctx ctx;
|
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.a == 0x6a09e667);
|
|
|
|
_IS(ctx.b == 0xbb67ae85);
|
|
|
|
_IS(ctx.c == 0x3c6ef372);
|
|
|
|
_IS(ctx.d == 0xa54ff53a);
|
|
|
|
_IS(ctx.e == 0x510e527f);
|
|
|
|
_IS(ctx.f == 0x9b05688c);
|
|
|
|
_IS(ctx.g == 0x1f83d9ab);
|
|
|
|
_IS(ctx.h == 0x5be0cd19);
|
|
|
|
_IS(ctx.bit_len == 0);
|
2018-10-21 20:46:05 +03:00
|
|
|
for (int i = 0; i < 64; i++)
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.block[i] == 0);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("init sha256 ctx");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int init_sha224_ctx()
|
2018-10-22 18:07:47 +03:00
|
|
|
{
|
|
|
|
t_sha256_ctx ctx;
|
|
|
|
|
|
|
|
ft_sha224_init(&ctx);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.a == 0xc1059ed8);
|
|
|
|
_IS(ctx.b == 0x367cd507);
|
|
|
|
_IS(ctx.c == 0x3070dd17);
|
|
|
|
_IS(ctx.d == 0xf70e5939);
|
|
|
|
_IS(ctx.e == 0xffc00b31);
|
|
|
|
_IS(ctx.f == 0x68581511);
|
|
|
|
_IS(ctx.g == 0x64f98fa7);
|
|
|
|
_IS(ctx.h == 0xbefa4fa4);
|
|
|
|
_IS(ctx.bit_len == 0);
|
2018-10-22 18:07:47 +03:00
|
|
|
for (int i = 0; i < 64; i++)
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.block[i] == 0);
|
2018-10-22 18:07:47 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("init sha224 ctx");
|
2018-10-22 18:07:47 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int decode_string_to_int_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 block[FT_SHA256_BLOCK_SIZE];
|
|
|
|
t_byte4 words[FT_SHA256_WORDS_COUNT];
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_bzero(block, FT_SHA256_BLOCK_SIZE);
|
|
|
|
block[0] = 'a';
|
|
|
|
block[1] = 'b';
|
|
|
|
block[2] = 'c';
|
|
|
|
|
|
|
|
ft_sha256_decode(words, block);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(words[0] == 0x61626300);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("decode string to int sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int encode_len_to_string_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte8 len;
|
|
|
|
t_byte1 bits[FT_SHA256_MESSAGE_LENGTH_BYTE];
|
2018-10-21 20:46:05 +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_sha256_encode_len(bits, len);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(bits[0] == (t_byte1)((len >> 56) & 0xff));
|
|
|
|
_IS(bits[7] == (t_byte1)(len & 0xff));
|
|
|
|
_IS(bits[6] == (t_byte1)((len >> 8) & 0xff));
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("encode len to string sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int encode_register_to_string_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 digest_part[4];
|
|
|
|
t_byte4 reg;
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
reg = 0xba7816bf;
|
|
|
|
|
|
|
|
ft_sha256_encode_register(digest_part, reg);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(digest_part[0] == 0xba);
|
|
|
|
_IS(digest_part[1] == 0x78);
|
|
|
|
_IS(digest_part[2] == 0x16);
|
|
|
|
_IS(digest_part[3] == 0xbf);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("encode register to string sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int update_bit_count_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
ft_sha256_update(&ctx, message, sizeof(message));
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.bit_len == sizeof(message) * 8);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("update bit count sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int fill_buffer_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
ft_sha256_update(&ctx, message, sizeof(message));
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ft_strcmp((const char *)message, (const char *)ctx.block) == 0);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("fill buffer sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void block_with_right_padding
|
|
|
|
(
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 *message,
|
|
|
|
t_byte8 message_len,
|
|
|
|
t_byte1 padding[FT_SHA256_BLOCK_SIZE]
|
2018-10-21 20:46:05 +03:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ft_bzero(padding, FT_SHA256_BLOCK_SIZE);
|
|
|
|
ft_memcpy(padding, message, message_len);
|
|
|
|
padding[message_len] = 0x80;
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int add_right_padding_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
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];
|
2019-03-02 20:52:33 +02:00
|
|
|
int i;
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
ft_sha256_update(&ctx, message, sizeof(message));
|
|
|
|
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);
|
|
|
|
|
|
|
|
block_with_right_padding(message, sizeof(message),
|
|
|
|
block_with_message_and_pading);
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
i = 0;
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ft_memcmp(ctx.block, block_with_message_and_pading,
|
2019-03-02 20:52:33 +02:00
|
|
|
FT_SHA256_BLOCK_SIZE) == 0);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("add right padding sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int compute_digest_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
2018-10-21 20:46:05 +03:00
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
ft_sha256_update(&ctx, message, ft_strlen((const char *)message));
|
|
|
|
ft_sha256_final(digest, &ctx);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.a == 0xba7816bf);
|
|
|
|
_IS(ctx.b == 0x8f01cfea);
|
|
|
|
_IS(ctx.c == 0x414140de);
|
|
|
|
_IS(ctx.d == 0x5dae2223);
|
|
|
|
_IS(ctx.e == 0xb00361a3);
|
|
|
|
_IS(ctx.f == 0x96177a9c);
|
|
|
|
_IS(ctx.g == 0xb410ff61);
|
|
|
|
_IS(ctx.h == 0xf20015ad);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("compute digest sha256");
|
2018-10-21 20:46:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int compute_digest_sha224()
|
2018-10-22 18:07:47 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
2018-10-22 18:07:47 +03:00
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
|
2018-10-22 18:07:47 +03:00
|
|
|
|
|
|
|
ft_sha224_init(&ctx);
|
|
|
|
ft_sha224_update(&ctx, message, ft_strlen((const char *)message));
|
|
|
|
ft_sha224_final(digest, &ctx);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ctx.a == 0x23097d22);
|
|
|
|
_IS(ctx.b == 0x3405d822);
|
|
|
|
_IS(ctx.c == 0x8642a477);
|
|
|
|
_IS(ctx.d == 0xbda255b3);
|
|
|
|
_IS(ctx.e == 0x2aadbce4);
|
|
|
|
_IS(ctx.f == 0xbda0b3f7);
|
|
|
|
_IS(ctx.g == 0xe36c9da7);
|
2018-10-22 18:07:47 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("computee digst sha224");
|
2018-10-22 18:07:47 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int create_digest_string_sha256()
|
2018-10-21 20:46:05 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
|
|
|
t_byte1 message_digest[] =
|
2018-10-21 20:46:05 +03:00
|
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
|
|
|
|
t_byte1 digest_string[FT_SHA256_STRING_SIZE_BYTE];
|
2018-10-21 20:46:05 +03:00
|
|
|
|
|
|
|
ft_sha256_init(&ctx);
|
|
|
|
ft_sha256_update(&ctx, message, ft_strlen((const char *)message));
|
|
|
|
ft_sha256_final(digest, &ctx);
|
|
|
|
ft_sha256_digest_string(digest, digest_string);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ft_strcmp((const char *)message_digest,
|
2019-03-02 20:52:33 +02:00
|
|
|
(const char *)digest_string) == 0);
|
2018-10-21 20:46:05 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("create digest string sha256");
|
2018-10-22 18:07:47 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 20:52:33 +02:00
|
|
|
static int create_digest_string_sha224()
|
2018-10-22 18:07:47 +03:00
|
|
|
{
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 message[] = "abc";
|
|
|
|
t_byte1 message_digest[] =
|
2018-10-22 18:07:47 +03:00
|
|
|
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
|
|
|
|
t_sha256_ctx ctx;
|
2018-10-23 20:41:44 +03:00
|
|
|
t_byte1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
|
|
|
|
t_byte1 digest_string[FT_SHA224_STRING_SIZE_BYTE];
|
2018-10-22 18:07:47 +03:00
|
|
|
|
|
|
|
ft_sha224_init(&ctx);
|
|
|
|
ft_sha224_update(&ctx, message, ft_strlen((const char *)message));
|
|
|
|
ft_sha224_final(digest, &ctx);
|
|
|
|
ft_sha224_digest_string(digest, digest_string);
|
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_IS(ft_strcmp((const char *)message_digest,
|
2019-03-02 20:52:33 +02:00
|
|
|
(const char *)digest_string) == 0);
|
2018-10-22 18:07:47 +03:00
|
|
|
|
2019-04-10 17:20:22 +03:00
|
|
|
_END("create digest strinf sha224");
|
2018-10-23 21:07:38 +03:00
|
|
|
}
|
2019-03-02 20:52:33 +02:00
|
|
|
|
|
|
|
int sha_tests()
|
|
|
|
{
|
2019-04-10 17:20:22 +03:00
|
|
|
_SHOULD(init_sha256_ctx);
|
|
|
|
_SHOULD(init_sha224_ctx);
|
|
|
|
_SHOULD(decode_string_to_int_sha256);
|
|
|
|
_SHOULD(encode_len_to_string_sha256);
|
|
|
|
_SHOULD(encode_register_to_string_sha256);
|
|
|
|
_SHOULD(update_bit_count_sha256);
|
|
|
|
_SHOULD(fill_buffer_sha256);
|
|
|
|
_SHOULD(add_right_padding_sha256);
|
|
|
|
_SHOULD(compute_digest_sha256);
|
|
|
|
_SHOULD(compute_digest_sha224);
|
|
|
|
_SHOULD(create_digest_string_sha256);
|
|
|
|
_SHOULD(create_digest_string_sha224);
|
2019-03-02 20:52:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|