sha224 complete

This commit is contained in:
Gregory 2018-10-22 18:07:47 +03:00
parent 752050aab3
commit 9c82b1fee3
10 changed files with 181 additions and 16 deletions

View file

@ -57,7 +57,11 @@ SHA_SRC = ft_sha256_init.c \
ft_sha256_padding.c \
ft_sha256_final.c \
ft_sha256_digest_string.c \
ft_sha256_constants.c
ft_sha256_constants.c \
ft_sha224_init.c \
ft_sha224_update.c \
ft_sha224_final.c \
ft_sha224_digest_string.c
SRC = main.c

View file

@ -10,6 +10,9 @@
# define FT_SHA256_STRING_SIZE_BYTE 65
# define FT_SHA256_REG_SIZE_BYTE 4
# define FT_SHA224_DIGEST_LENGTH_BYTE 28
# define FT_SHA224_STRING_SIZE_BYTE 57
/* Define the SHA shift, rotate left, and rotate right macros */
#define SHR(bits, word) ((word) >> (bits))
#define ROTL(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits))))
@ -55,10 +58,15 @@ typedef struct s_temp_registers
} t_temp_registers;
void ft_sha256_init(t_sha256_ctx *ctx);
void ft_sha224_init(t_sha256_ctx *ctx);
void ft_sha256_update(t_sha256_ctx *ctx,
BYTE1 *message, BYTE8 len);
void ft_sha224_update(t_sha256_ctx *ctx,
BYTE1 *message, BYTE8 len);
void ft_sha256_final(BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
t_sha256_ctx *ctx);
void ft_sha224_final(BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
t_sha256_ctx *ctx);
void ft_sha256_transform(t_sha256_ctx *ctx,
BYTE1 block[FT_SHA256_BLOCK_SIZE]);
void ft_sha256_decode(BYTE4 w[FT_SHA256_WORDS_COUNT],
@ -71,6 +79,9 @@ void ft_sha256_padding(BYTE1 padding[FT_SHA256_BLOCK_SIZE]);
void ft_sha256_digest_string(
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
BYTE1 digst_string[FT_SHA256_STRING_SIZE_BYTE]);
void ft_sha224_digest_string(
BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE],
BYTE1 digst_string[FT_SHA224_STRING_SIZE_BYTE]);
BYTE4 *ft_sha256_constants(void);
#endif

View file

@ -24,6 +24,8 @@ CASE(fill_buffer_sha256);
CASE(add_right_padding_sha256);
CASE(compute_digest_sha256);
CASE(create_digest_string_sha256);
CASE(should_init_ctx_sha224);
CASE(compute_digest_sha224);
CASE(create_digest_string_sha224);
#endif

View file

@ -0,0 +1,43 @@
#include "ft_sha.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_sha224_digest_string
(
BYTE1 digest[FT_SHA224_DIGEST_LENGTH_BYTE],
BYTE1 string[FT_SHA224_STRING_SIZE_BYTE])
{
BYTE8 i;
BYTE8 j;
BYTE1 chars[16];
i = 0;
j = 0;
init_chars(chars);
while (i < FT_SHA224_DIGEST_LENGTH_BYTE)
{
string[j] = chars[digest[i] / 16];
string[j + 1] = chars[digest[i] % 16];
i++;
j += 2;
}
string[FT_SHA224_STRING_SIZE_BYTE - 1] = 0;
}

10
src/sha/ft_sha224_final.c Normal file
View file

@ -0,0 +1,10 @@
#include "ft_sha.h"
void ft_sha224_final
(
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
t_sha256_ctx *ctx
)
{
ft_sha256_final(digest, ctx);
}

17
src/sha/ft_sha224_init.c Normal file
View file

@ -0,0 +1,17 @@
#include "ft_sha.h"
#include "libft.h"
void ft_sha224_init(t_sha256_ctx *ctx)
{
ctx->a = 0xc1059ed8;
ctx->b = 0x367cd507;
ctx->c = 0x3070dd17;
ctx->d = 0xf70e5939;
ctx->e = 0xffc00b31;
ctx->f = 0x68581511;
ctx->g = 0x64f98fa7;
ctx->h = 0xbefa4fa4;
ctx->bit_len = 0;
ft_bzero(ctx->block, FT_SHA256_BLOCK_SIZE);
}

View file

@ -0,0 +1,6 @@
#include "ft_sha.h"
void ft_sha224_update(t_sha256_ctx *ctx, BYTE1 *message, BYTE8 len)
{
ft_sha256_update(ctx, message, len);
}

View file

@ -36,5 +36,4 @@ void ft_sha256_final
ft_sha256_update(ctx, padding, padding_len);
ft_sha256_update(ctx, length_as_bytes, FT_SHA256_MESSAGE_LENGTH_BYTE);
fill_digest(digest, ctx);
// ft_bzero(ctx, sizeof(t_sha256_ctx));
}

View file

@ -25,6 +25,29 @@ TEST_RESULT should_init_ctx_sha256(TEST_PARAMS, TEST_DATA)
return MUNIT_OK;
}
TEST_RESULT should_init_ctx_sha224(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_data);
UNUSED(test_params);
t_sha256_ctx ctx;
ft_sha224_init(&ctx);
munit_assert_uint(ctx.a, ==, 0xc1059ed8);
munit_assert_uint(ctx.b, ==, 0x367cd507);
munit_assert_uint(ctx.c, ==, 0x3070dd17);
munit_assert_uint(ctx.d, ==, 0xf70e5939);
munit_assert_uint(ctx.e, ==, 0xffc00b31);
munit_assert_uint(ctx.f, ==, 0x68581511);
munit_assert_uint(ctx.g, ==, 0x64f98fa7);
munit_assert_uint(ctx.h, ==, 0xbefa4fa4);
munit_assert_true(ctx.bit_len == 0);
for (int i = 0; i < 64; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
return MUNIT_OK;
}
TEST_RESULT decode_string_to_int_sha256(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_params);
@ -191,6 +214,30 @@ TEST_RESULT compute_digest_sha256(TEST_PARAMS, TEST_DATA)
return MUNIT_OK;
}
TEST_RESULT compute_digest_sha224(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_data);
UNUSED(test_params);
BYTE1 message[] = "abc";
t_sha256_ctx ctx;
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
ft_sha224_init(&ctx);
ft_sha224_update(&ctx, message, ft_strlen((const char *)message));
ft_sha224_final(digest, &ctx);
munit_assert_uint32(ctx.a, ==, 0x23097d22);
munit_assert_uint32(ctx.b, ==, 0x3405d822);
munit_assert_uint32(ctx.c, ==, 0x8642a477);
munit_assert_uint32(ctx.d, ==, 0xbda255b3);
munit_assert_uint32(ctx.e, ==, 0x2aadbce4);
munit_assert_uint32(ctx.f, ==, 0xbda0b3f7);
munit_assert_uint32(ctx.g, ==, 0xe36c9da7);
return MUNIT_OK;
}
TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_data);
@ -213,3 +260,26 @@ TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA)
return MUNIT_OK;
}
TEST_RESULT create_digest_string_sha224(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_data);
UNUSED(test_params);
BYTE1 message[] = "abc";
BYTE1 message_digest[] =
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
t_sha256_ctx ctx;
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
BYTE1 digest_string[FT_SHA224_STRING_SIZE_BYTE];
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);
munit_assert_string_equal((const char *)message_digest,
(const char *)digest_string);
return MUNIT_OK;
}

View file

@ -12,15 +12,18 @@ MunitTest md5_tests[] = {
};
MunitTest sha_tests[] = {
IT("/init_ctx", should_init_ctx_sha256, NULL, NULL, 0, NULL),
IT("/init_ctx_256", should_init_ctx_sha256, NULL, NULL, 0, NULL),
IT("/init_ctx_224", should_init_ctx_sha224, NULL, NULL, 0, NULL),
IT("/decode_string", decode_string_to_int_sha256, NULL, NULL, 0, NULL),
IT("/encode_len", encode_len_to_string_sha256, NULL, NULL, 0, NULL),
IT("/enc_register", encode_register_to_string_sha256, NULL, NULL, 0, NULL),
IT("/update_bit_count", update_bit_count_sha256, NULL, NULL, 0, NULL),
IT("/fill_buffer", fill_buffer_sha256, NULL, NULL, 0, NULL),
IT("/add_right_padding", add_right_padding_sha256, NULL, NULL, 0, NULL),
IT("/compute_digest", compute_digest_sha256, NULL, NULL, 0, NULL),
IT("/creates_string", create_digest_string_sha256, NULL, NULL, 0, NULL),
IT("/compute_digest_256", compute_digest_sha256, NULL, NULL, 0, NULL),
IT("/compute_digest_224", compute_digest_sha224, NULL, NULL, 0, NULL),
IT("/creates_string_256", create_digest_string_sha256, NULL, NULL, 0, NULL),
IT("/creates_string_224", create_digest_string_sha224, NULL, NULL, 0, NULL),
END_IT};
static const MunitSuite ft_ssl_suites[] = {