working sha256 hash
This commit is contained in:
parent
41ecca9dc7
commit
752050aab3
17 changed files with 659 additions and 20 deletions
23
Makefile
23
Makefile
|
@ -19,6 +19,7 @@ NAME := ft_ssl
|
|||
ROOT := $(shell pwd)
|
||||
SRC_DIR := $(ROOT)/src/
|
||||
MD5_DIR := $(SRC_DIR)/md5/
|
||||
SHA_DIR := $(SRC_DIR)/sha/
|
||||
OBJ_DIR := $(ROOT)/obj/
|
||||
INC_DIR := $(ROOT)/inc/
|
||||
LIB_DIR := $(ROOT)/lib/
|
||||
|
@ -47,6 +48,18 @@ MD5_SRC = ft_md5_init.c \
|
|||
ft_md5_padding.c \
|
||||
ft_md5_digest_string.c
|
||||
|
||||
SHA_SRC = ft_sha256_init.c \
|
||||
ft_sha256_update.c \
|
||||
ft_sha256_decode.c \
|
||||
ft_sha256_encode_len.c \
|
||||
ft_sha256_encode_register.c \
|
||||
ft_sha256_transform.c \
|
||||
ft_sha256_padding.c \
|
||||
ft_sha256_final.c \
|
||||
ft_sha256_digest_string.c \
|
||||
ft_sha256_constants.c
|
||||
|
||||
|
||||
SRC = main.c
|
||||
SRC += $(MD5_SRC)
|
||||
# project object files
|
||||
|
@ -61,9 +74,14 @@ TEST_BIN = ft_ssl_test
|
|||
MD5_TESTS = md5_tests.c
|
||||
MD5_TESTS += $(MD5_SRC)
|
||||
|
||||
TEST_SRC = tests.c \
|
||||
SHA_TESTS = sha_tests.c
|
||||
SHA_TESTS += $(SHA_SRC)
|
||||
|
||||
TEST_SRC = tests.c \
|
||||
munit.c
|
||||
TEST_SRC += $(MD5_TESTS)
|
||||
|
||||
TEST_SRC += $(MD5_TESTS) \
|
||||
$(SHA_TESTS)
|
||||
|
||||
TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o))
|
||||
|
||||
|
@ -151,6 +169,7 @@ multi:
|
|||
|
||||
vpath %.c $(SRC_DIR) \
|
||||
$(MD5_DIR) \
|
||||
$(SHA_DIR) \
|
||||
$(TST_DIR) \
|
||||
$(MUINUT_DIR)
|
||||
|
||||
|
|
76
inc/ft_sha.h
Normal file
76
inc/ft_sha.h
Normal file
|
@ -0,0 +1,76 @@
|
|||
#ifndef FT_SHA_H
|
||||
# define FT_SHA_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
# define FT_SHA256_BLOCK_SIZE 64
|
||||
# define FT_SHA256_WORDS_COUNT 16
|
||||
# define FT_SHA256_MESSAGE_LENGTH_BYTE 8
|
||||
# define FT_SHA256_DIGEST_LENGTH_BYTE 32
|
||||
# define FT_SHA256_STRING_SIZE_BYTE 65
|
||||
# define FT_SHA256_REG_SIZE_BYTE 4
|
||||
|
||||
/* 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))))
|
||||
#define ROTR(bits, word) (((word) >> (bits)) | ((word) << (32 - (bits))))
|
||||
|
||||
/* Define the SHA SIGMA and sigma macros */
|
||||
#define SHA256_SIGMA0(w) (ROTR(2, w) ^ ROTR(13, w) ^ ROTR(22, w))
|
||||
#define SHA256_SIGMA1(word) (ROTR(6, word) ^ ROTR(11, word) ^ ROTR(25, word))
|
||||
#define SHA256_sigma0(word) (ROTR(7, word) ^ ROTR(18, word) ^ SHR(3, word))
|
||||
#define SHA256_sigma1(word) (ROTR(17, word) ^ ROTR(19, word) ^ SHR(10, word))
|
||||
|
||||
#define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
|
||||
#define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
|
||||
|
||||
typedef uint64_t BYTE8;
|
||||
typedef uint32_t BYTE4;
|
||||
typedef unsigned char BYTE1;
|
||||
|
||||
typedef struct s_sha256_ctx
|
||||
{
|
||||
BYTE4 a;
|
||||
BYTE4 b;
|
||||
BYTE4 c;
|
||||
BYTE4 d;
|
||||
BYTE4 e;
|
||||
BYTE4 f;
|
||||
BYTE4 g;
|
||||
BYTE4 h;
|
||||
BYTE1 block[FT_SHA256_BLOCK_SIZE];
|
||||
BYTE8 bit_len;
|
||||
} t_sha256_ctx;
|
||||
|
||||
typedef struct s_temp_registers
|
||||
{
|
||||
BYTE4 a;
|
||||
BYTE4 b;
|
||||
BYTE4 c;
|
||||
BYTE4 d;
|
||||
BYTE4 e;
|
||||
BYTE4 f;
|
||||
BYTE4 g;
|
||||
BYTE4 h;
|
||||
} t_temp_registers;
|
||||
|
||||
void ft_sha256_init(t_sha256_ctx *ctx);
|
||||
void ft_sha256_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_sha256_transform(t_sha256_ctx *ctx,
|
||||
BYTE1 block[FT_SHA256_BLOCK_SIZE]);
|
||||
void ft_sha256_decode(BYTE4 w[FT_SHA256_WORDS_COUNT],
|
||||
BYTE1 b[FT_SHA256_BLOCK_SIZE]);
|
||||
void ft_sha256_encode_len(BYTE1 b[FT_SHA256_MESSAGE_LENGTH_BYTE],
|
||||
BYTE8 len);
|
||||
void ft_sha256_encode_register(
|
||||
BYTE1 digest_part[FT_SHA256_REG_SIZE_BYTE], BYTE4 reg);
|
||||
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]);
|
||||
BYTE4 *ft_sha256_constants(void);
|
||||
|
||||
#endif
|
14
inc/tests.h
14
inc/tests.h
|
@ -5,7 +5,7 @@
|
|||
#include "munit.h"
|
||||
#include "tests_macros.h"
|
||||
|
||||
// md5 test tests
|
||||
// md5 tests
|
||||
CASE(should_init_ctx);
|
||||
CASE(update_should_change_count);
|
||||
CASE(decode_string_to_int);
|
||||
|
@ -14,4 +14,16 @@ CASE(encode_register);
|
|||
CASE(create_digest);
|
||||
CASE(create_string);
|
||||
|
||||
// sha tests
|
||||
CASE(should_init_ctx_sha256);
|
||||
CASE(decode_string_to_int_sha256);
|
||||
CASE(encode_len_to_string_sha256);
|
||||
CASE(encode_register_to_string_sha256);
|
||||
CASE(update_bit_count_sha256);
|
||||
CASE(fill_buffer_sha256);
|
||||
CASE(add_right_padding_sha256);
|
||||
CASE(compute_digest_sha256);
|
||||
CASE(create_digest_string_sha256);
|
||||
|
||||
|
||||
#endif
|
|
@ -5,14 +5,14 @@ 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;
|
||||
unsigned int curent_block_index;
|
||||
|
||||
cuurent_block_index = (c->bit_len / 8) % 64;
|
||||
curent_block_index = (c->bit_len / 8) % 64;
|
||||
c->bit_len += message_len * 8;
|
||||
part_block_len = FT_MD5_BLOCK_SIZE - cuurent_block_index;
|
||||
part_block_len = FT_MD5_BLOCK_SIZE - curent_block_index;
|
||||
if (message_len >= part_block_len)
|
||||
{
|
||||
ft_memcpy(&c->block[cuurent_block_index], message, part_block_len);
|
||||
ft_memcpy(&c->block[curent_block_index], message, part_block_len);
|
||||
ft_md5_transform(c, c->block);
|
||||
i = part_block_len;
|
||||
while(i + 63 < message_len)
|
||||
|
@ -20,9 +20,9 @@ void ft_md5_update(t_md5_ctx *c, BYTE1 * message, BYTE8 message_len)
|
|||
ft_md5_transform(c, &c->block[i]);
|
||||
i += FT_MD5_BLOCK_SIZE;
|
||||
}
|
||||
cuurent_block_index = 0;
|
||||
curent_block_index = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
ft_memcpy(&c->block[cuurent_block_index], &message[i], message_len - i);
|
||||
ft_memcpy(&c->block[curent_block_index], &message[i], message_len - i);
|
||||
}
|
21
src/sha/ft_sha256_constants.c
Normal file
21
src/sha/ft_sha256_constants.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "ft_sha.h"
|
||||
|
||||
BYTE4 *ft_sha256_constants(void)
|
||||
{
|
||||
static BYTE4 k[FT_SHA256_BLOCK_SIZE] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
|
||||
0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
|
||||
0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
|
||||
0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152,
|
||||
0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
|
||||
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
|
||||
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
|
||||
0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
|
||||
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
||||
|
||||
return k;
|
||||
}
|
24
src/sha/ft_sha256_decode.c
Normal file
24
src/sha/ft_sha256_decode.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "ft_sha.h"
|
||||
|
||||
void ft_sha256_decode
|
||||
(
|
||||
BYTE4 w[FT_SHA256_WORDS_COUNT],
|
||||
BYTE1 b[FT_SHA256_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
BYTE8 i;
|
||||
BYTE8 j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
|
||||
while (i < FT_SHA256_WORDS_COUNT)
|
||||
{
|
||||
w[i] = ((((BYTE4)b[j]) << 24) |
|
||||
(((BYTE4)b[j + 1]) << 16) |
|
||||
(((BYTE4)b[j + 2]) << 8) |
|
||||
((BYTE4)b[j + 3]));
|
||||
i++;
|
||||
j += 4;
|
||||
}
|
||||
}
|
44
src/sha/ft_sha256_digest_string.c
Normal file
44
src/sha/ft_sha256_digest_string.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#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_sha256_digest_string
|
||||
(
|
||||
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
||||
BYTE1 string[FT_SHA256_STRING_SIZE_BYTE]
|
||||
)
|
||||
{
|
||||
BYTE8 i;
|
||||
BYTE8 j;
|
||||
BYTE1 chars[16];
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
init_chars(chars);
|
||||
while (i < FT_SHA256_DIGEST_LENGTH_BYTE)
|
||||
{
|
||||
string[j] = chars[digest[i] / 16];
|
||||
string[j + 1] = chars[digest[i] % 16];
|
||||
i++;
|
||||
j += 2;
|
||||
}
|
||||
string[FT_SHA256_STRING_SIZE_BYTE - 1] = 0;
|
||||
}
|
13
src/sha/ft_sha256_encode_len.c
Normal file
13
src/sha/ft_sha256_encode_len.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "ft_sha.h"
|
||||
|
||||
void ft_sha256_encode_len(BYTE1 b[FT_SHA256_MESSAGE_LENGTH_BYTE], BYTE8 len)
|
||||
{
|
||||
b[7] = (BYTE1)(len & 0xff);
|
||||
b[6] = (BYTE1)(len >> 8 & 0xff);
|
||||
b[5] = (BYTE1)(len >> 16 & 0xff);
|
||||
b[4] = (BYTE1)(len >> 24 & 0xff);
|
||||
b[3] = (BYTE1)(len >> 32 & 0xff);
|
||||
b[2] = (BYTE1)(len >> 40 & 0xff);
|
||||
b[1] = (BYTE1)(len >> 48 & 0xff);
|
||||
b[0] = (BYTE1)(len >> 56 & 0xff);
|
||||
}
|
13
src/sha/ft_sha256_encode_register.c
Normal file
13
src/sha/ft_sha256_encode_register.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "ft_sha.h"
|
||||
|
||||
void ft_sha256_encode_register
|
||||
(
|
||||
BYTE1 digest_part[FT_SHA256_REG_SIZE_BYTE],
|
||||
BYTE4 reg
|
||||
)
|
||||
{
|
||||
digest_part[0] = reg >> 24 & 0xff;
|
||||
digest_part[1] = reg >> 16 & 0xff;
|
||||
digest_part[2] = reg >> 8 & 0xff;
|
||||
digest_part[3] = reg & 0xff;
|
||||
}
|
40
src/sha/ft_sha256_final.c
Normal file
40
src/sha/ft_sha256_final.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include "ft_sha.h"
|
||||
#include "libft.h"
|
||||
|
||||
static void fill_digest
|
||||
(
|
||||
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
||||
t_sha256_ctx *ctx
|
||||
)
|
||||
{
|
||||
ft_sha256_encode_register(digest, ctx->a);
|
||||
ft_sha256_encode_register(&digest[4], ctx->b);
|
||||
ft_sha256_encode_register(&digest[8], ctx->c);
|
||||
ft_sha256_encode_register(&digest[12], ctx->d);
|
||||
ft_sha256_encode_register(&digest[16], ctx->e);
|
||||
ft_sha256_encode_register(&digest[20], ctx->f);
|
||||
ft_sha256_encode_register(&digest[24], ctx->g);
|
||||
ft_sha256_encode_register(&digest[28], ctx->h);
|
||||
}
|
||||
|
||||
void ft_sha256_final
|
||||
(
|
||||
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE],
|
||||
t_sha256_ctx *ctx
|
||||
)
|
||||
{
|
||||
BYTE1 length_as_bytes[FT_SHA256_MESSAGE_LENGTH_BYTE];
|
||||
BYTE1 padding[FT_SHA256_BLOCK_SIZE];
|
||||
BYTE8 buff_index;
|
||||
BYTE8 padding_len;
|
||||
|
||||
ft_sha256_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_sha256_padding(padding);
|
||||
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));
|
||||
}
|
17
src/sha/ft_sha256_init.c
Normal file
17
src/sha/ft_sha256_init.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "ft_sha.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_sha256_init(t_sha256_ctx *ctx)
|
||||
{
|
||||
ctx->a = 0x6a09e667;
|
||||
ctx->b = 0xbb67ae85;
|
||||
ctx->c = 0x3c6ef372;
|
||||
ctx->d = 0xa54ff53a;
|
||||
ctx->e = 0x510e527f;
|
||||
ctx->f = 0x9b05688c;
|
||||
ctx->g = 0x1f83d9ab;
|
||||
ctx->h = 0x5be0cd19;
|
||||
|
||||
ctx->bit_len = 0;
|
||||
ft_bzero(ctx->block, FT_SHA256_BLOCK_SIZE);
|
||||
}
|
8
src/sha/ft_sha256_padding.c
Normal file
8
src/sha/ft_sha256_padding.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include "ft_sha.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_sha256_padding(BYTE1 padding[FT_SHA256_BLOCK_SIZE])
|
||||
{
|
||||
padding[0] = 0x80;
|
||||
ft_bzero(&padding[1], FT_SHA256_BLOCK_SIZE - 1);
|
||||
}
|
79
src/sha/ft_sha256_transform.c
Normal file
79
src/sha/ft_sha256_transform.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include "ft_sha.h"
|
||||
|
||||
static void fill_up_words(BYTE4 w[FT_SHA256_BLOCK_SIZE])
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 16;
|
||||
while (i < FT_SHA256_BLOCK_SIZE)
|
||||
{
|
||||
w[i] = SHA256_sigma1(w[i - 2]) + w[i - 7] +
|
||||
SHA256_sigma0(w[i - 15]) + w[i - 16];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void copy_from_registers(t_sha256_ctx *from, t_temp_registers *to)
|
||||
{
|
||||
to->a = from->a;
|
||||
to->b = from->b;
|
||||
to->c = from->c;
|
||||
to->d = from->d;
|
||||
to->e = from->e;
|
||||
to->f = from->f;
|
||||
to->g = from->g;
|
||||
to->h = from->h;
|
||||
}
|
||||
|
||||
static void add_to_registers(t_temp_registers *from, t_sha256_ctx *to)
|
||||
{
|
||||
to->a += from->a;
|
||||
to->b += from->b;
|
||||
to->c += from->c;
|
||||
to->d += from->d;
|
||||
to->e += from->e;
|
||||
to->f += from->f;
|
||||
to->g += from->g;
|
||||
to->h += from->h;
|
||||
}
|
||||
|
||||
static void main_transform_loop
|
||||
(
|
||||
t_temp_registers *tr,
|
||||
BYTE4 w[FT_SHA256_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
int i;
|
||||
BYTE4 temp1, temp2;
|
||||
BYTE4 *k;
|
||||
|
||||
k = ft_sha256_constants();
|
||||
i = 0;
|
||||
while (i < FT_SHA256_BLOCK_SIZE)
|
||||
{
|
||||
temp1 = tr->h + SHA256_SIGMA1(tr->e) +
|
||||
SHA_Ch(tr->e, tr->f, tr->g) + k[i] + w[i];
|
||||
temp2 = SHA256_SIGMA0(tr->a) + SHA_Maj(tr->a, tr->b, tr->c);
|
||||
tr->h = tr->g;
|
||||
tr->g = tr->f;
|
||||
tr->f = tr->e;
|
||||
tr->e = tr->d + temp1;
|
||||
tr->d = tr->c;
|
||||
tr->c = tr->b;
|
||||
tr->b = tr->a;
|
||||
tr->a = temp1 + temp2;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_sha256_transform(t_sha256_ctx *c, BYTE1 block[FT_SHA256_BLOCK_SIZE])
|
||||
{
|
||||
BYTE4 w[FT_SHA256_BLOCK_SIZE];
|
||||
t_temp_registers tr;
|
||||
|
||||
ft_sha256_decode(w, block);
|
||||
fill_up_words(w);
|
||||
copy_from_registers(c, &tr);
|
||||
main_transform_loop(&tr, w);
|
||||
add_to_registers(&tr, c);
|
||||
}
|
28
src/sha/ft_sha256_update.c
Normal file
28
src/sha/ft_sha256_update.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "ft_sha.h"
|
||||
#include "libft.h"
|
||||
|
||||
void ft_sha256_update(t_sha256_ctx *c, BYTE1 *message, BYTE8 message_len)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int part_block_len;
|
||||
unsigned int curent_block_index;
|
||||
|
||||
curent_block_index = (c->bit_len / 8) % 64;
|
||||
c->bit_len += message_len * 8;
|
||||
part_block_len = FT_SHA256_BLOCK_SIZE - curent_block_index;
|
||||
if (message_len >= part_block_len)
|
||||
{
|
||||
ft_memcpy(&c->block[curent_block_index], message, part_block_len);
|
||||
ft_sha256_transform(c, c->block);
|
||||
i = part_block_len;
|
||||
while (i + 63 < message_len)
|
||||
{
|
||||
ft_sha256_transform(c, &c->block[i]);
|
||||
i += FT_SHA256_BLOCK_SIZE;
|
||||
}
|
||||
curent_block_index = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
ft_memcpy(&c->block[curent_block_index], &message[i], message_len - i);
|
||||
}
|
|
@ -10,8 +10,8 @@ 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.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);
|
||||
|
|
215
t/sha_tests.c
Normal file
215
t/sha_tests.c
Normal file
|
@ -0,0 +1,215 @@
|
|||
#include "tests.h"
|
||||
#include "ft_sha.h"
|
||||
#include "libft.h"
|
||||
|
||||
TEST_RESULT should_init_ctx_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
t_sha256_ctx ctx;
|
||||
|
||||
ft_sha256_init(&ctx);
|
||||
|
||||
munit_assert_uint(ctx.a, ==, 0x6a09e667);
|
||||
munit_assert_uint(ctx.b, ==, 0xbb67ae85);
|
||||
munit_assert_uint(ctx.c, ==, 0x3c6ef372);
|
||||
munit_assert_uint(ctx.d, ==, 0xa54ff53a);
|
||||
munit_assert_uint(ctx.e, ==, 0x510e527f);
|
||||
munit_assert_uint(ctx.f, ==, 0x9b05688c);
|
||||
munit_assert_uint(ctx.g, ==, 0x1f83d9ab);
|
||||
munit_assert_uint(ctx.h, ==, 0x5be0cd19);
|
||||
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);
|
||||
UNUSED(test_data);
|
||||
|
||||
BYTE1 block[FT_SHA256_BLOCK_SIZE];
|
||||
BYTE4 words[FT_SHA256_WORDS_COUNT];
|
||||
|
||||
ft_bzero(block, FT_SHA256_BLOCK_SIZE);
|
||||
block[0] = 'a';
|
||||
block[1] = 'b';
|
||||
block[2] = 'c';
|
||||
|
||||
ft_sha256_decode(words, block);
|
||||
|
||||
munit_assert_uint(words[0], ==, 0x61626300);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT encode_len_to_string_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
|
||||
BYTE8 len;
|
||||
BYTE1 bits[FT_SHA256_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_sha256_encode_len(bits, len);
|
||||
|
||||
munit_assert_uchar(bits[0], ==, (BYTE1)((len >> 56) & 0xff));
|
||||
munit_assert_uchar(bits[7], ==, (BYTE1)(len & 0xff));
|
||||
munit_assert_uchar(bits[6], ==, (BYTE1)((len >> 8) & 0xff));
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT encode_register_to_string_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_params);
|
||||
UNUSED(test_data);
|
||||
|
||||
BYTE1 digest_part[4];
|
||||
BYTE4 reg;
|
||||
|
||||
reg = 0xba7816bf;
|
||||
|
||||
ft_sha256_encode_register(digest_part, reg);
|
||||
|
||||
munit_assert_uchar(digest_part[0], ==, 0xba);
|
||||
munit_assert_uchar(digest_part[1], ==, 0x78);
|
||||
munit_assert_uchar(digest_part[2], ==, 0x16);
|
||||
munit_assert_uchar(digest_part[3], ==, 0xbf);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT update_bit_count_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_data);
|
||||
UNUSED(test_params);
|
||||
|
||||
t_sha256_ctx ctx;
|
||||
BYTE1 message[] = "abc";
|
||||
|
||||
ft_sha256_init(&ctx);
|
||||
ft_sha256_update(&ctx, message, sizeof(message));
|
||||
|
||||
munit_assert_uint(ctx.bit_len, ==, sizeof(message) * 8);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT fill_buffer_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_data);
|
||||
UNUSED(test_params);
|
||||
|
||||
t_sha256_ctx ctx;
|
||||
BYTE1 message[] = "abc";
|
||||
|
||||
ft_sha256_init(&ctx);
|
||||
ft_sha256_update(&ctx, message, sizeof(message));
|
||||
|
||||
munit_assert_string_equal((const char *)message, (const char *)ctx.block);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void block_with_right_padding
|
||||
(
|
||||
BYTE1 *message,
|
||||
BYTE8 message_len,
|
||||
BYTE1 padding[FT_SHA256_BLOCK_SIZE]
|
||||
)
|
||||
{
|
||||
ft_bzero(padding, FT_SHA256_BLOCK_SIZE);
|
||||
ft_memcpy(padding, message, message_len);
|
||||
padding[message_len] = 0x80;
|
||||
}
|
||||
|
||||
TEST_RESULT add_right_padding_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_data);
|
||||
UNUSED(test_params);
|
||||
|
||||
t_sha256_ctx ctx;
|
||||
BYTE1 message[] = "abc";
|
||||
BYTE8 buff_index;
|
||||
BYTE8 padding_len;
|
||||
BYTE1 padding[FT_SHA256_BLOCK_SIZE];
|
||||
BYTE1 block_with_message_and_pading[FT_SHA256_BLOCK_SIZE];
|
||||
|
||||
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);
|
||||
munit_assert_memory_equal(FT_SHA256_BLOCK_SIZE, ctx.block,
|
||||
block_with_message_and_pading);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT compute_digest_sha256(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_sha256_init(&ctx);
|
||||
ft_sha256_update(&ctx, message, ft_strlen((const char *)message));
|
||||
ft_sha256_final(digest, &ctx);
|
||||
|
||||
|
||||
munit_assert_uint32(ctx.a, ==, 0xba7816bf);
|
||||
munit_assert_uint32(ctx.b, ==, 0x8f01cfea);
|
||||
munit_assert_uint32(ctx.c, ==, 0x414140de);
|
||||
munit_assert_uint32(ctx.d, ==, 0x5dae2223);
|
||||
munit_assert_uint32(ctx.e, ==, 0xb00361a3);
|
||||
munit_assert_uint32(ctx.f, ==, 0x96177a9c);
|
||||
munit_assert_uint32(ctx.g, ==, 0xb410ff61);
|
||||
munit_assert_uint32(ctx.h, ==, 0xf20015ad);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
TEST_RESULT create_digest_string_sha256(TEST_PARAMS, TEST_DATA)
|
||||
{
|
||||
UNUSED(test_data);
|
||||
UNUSED(test_params);
|
||||
|
||||
BYTE1 message[] = "abc";
|
||||
BYTE1 message_digest[] =
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
||||
t_sha256_ctx ctx;
|
||||
BYTE1 digest[FT_SHA256_DIGEST_LENGTH_BYTE];
|
||||
BYTE1 digest_string[FT_SHA256_STRING_SIZE_BYTE];
|
||||
|
||||
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);
|
||||
|
||||
munit_assert_string_equal((const char *)message_digest,
|
||||
(const char *)digest_string);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
48
t/tests.c
48
t/tests.c
|
@ -1,7 +1,7 @@
|
|||
#include "tests.h"
|
||||
|
||||
MunitTest md5_tests[] = {
|
||||
IT("/zeroes_ctx", should_init_ctx, NULL, NULL, 0, NULL),
|
||||
IT("/init_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),
|
||||
|
@ -11,15 +11,45 @@ MunitTest md5_tests[] = {
|
|||
END_IT
|
||||
};
|
||||
|
||||
static const MunitSuite suite = {
|
||||
(char *)"/md5_suite", /* name */
|
||||
md5_tests, /* tests */
|
||||
NULL, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
MunitTest sha_tests[] = {
|
||||
IT("/init_ctx", should_init_ctx_sha256, 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),
|
||||
END_IT};
|
||||
|
||||
static const MunitSuite ft_ssl_suites[] = {
|
||||
{
|
||||
(char *)"/md5_suite", /* name */
|
||||
md5_tests, /* tests */
|
||||
NULL, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
},
|
||||
{
|
||||
(char *)"/sha_suite", /* name */
|
||||
sha_tests, /* tests */
|
||||
NULL, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
},
|
||||
{NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
static const MunitSuite main_suite = {
|
||||
(char *)"/ft_ssl", /* name */
|
||||
NULL, /* tests */
|
||||
(MunitSuite *)ft_ssl_suites, /* suites */
|
||||
1, /* iterations */
|
||||
MUNIT_SUITE_OPTION_NONE /* options */
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return munit_suite_main(&suite, NULL, argc, argv);
|
||||
return munit_suite_main(&main_suite, NULL, argc, argv);
|
||||
}
|
Loading…
Reference in a new issue