Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/api/test_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,57 @@ int test_wc_Sha256Transform(void)
return EXPECT_RESULT();
}

/*
* wc_Sha256HashBlock() takes a caller block pointer of any alignment, so the
* same bytes at an aligned and an unaligned offset must hash the same.
*/
int test_wc_Sha256HashBlock_unaligned(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
wc_Sha256 sha256;
byte buf[WC_SHA256_BLOCK_SIZE * 2];
byte aligned[WC_SHA256_DIGEST_SIZE];
byte unaligned[WC_SHA256_DIGEST_SIZE];
int initDone = 0;
int off;
word32 i;

for (i = 0; i < (word32)sizeof(buf); i++) {
buf[i] = (byte)(i * 7 + 1);
}

XMEMSET(&sha256, 0, sizeof(sha256));
ExpectIntEQ(wc_InitSha256_ex(&sha256, HEAP_HINT, testDevId), 0);
if (EXPECT_SUCCESS()) initDone = 1;

/* Reference digest of the block at offset 0. */
ExpectIntEQ(wc_Sha256HashBlock(&sha256, buf, aligned), 0);

/* Same bytes at every non-zero offset within a word. */
for (off = 1; off < 4; off++) {
if (!EXPECT_SUCCESS()) break;

for (i = 0; i < (word32)WC_SHA256_BLOCK_SIZE; i++) {
buf[(word32)off + i] = (byte)(i * 7 + 1);
}
if (initDone) {
wc_Sha256Free(&sha256);
initDone = 0;
}
ExpectIntEQ(wc_InitSha256_ex(&sha256, HEAP_HINT, testDevId), 0);
if (EXPECT_SUCCESS()) initDone = 1;
ExpectIntEQ(wc_Sha256HashBlock(&sha256, buf + off, unaligned), 0);
ExpectBufEQ(unaligned, aligned, WC_SHA256_DIGEST_SIZE);
}

if (initDone) {
wc_Sha256Free(&sha256);
}
#endif
return EXPECT_RESULT();
}

int test_wc_Sha256_Flags(void)
{
EXPECT_DECLS;
Expand Down
4 changes: 3 additions & 1 deletion tests/api/test_sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int test_wc_Sha256Copy(void);
int test_wc_Sha256GetHash(void);
int test_wc_Sha256Transform(void);
int test_wc_Sha256_Flags(void);
int test_wc_Sha256HashBlock_unaligned(void);

int test_wc_InitSha224(void);
int test_wc_Sha224Update(void);
Expand All @@ -54,7 +55,8 @@ int test_wc_Sha224_Flags(void);
TEST_DECL_GROUP("sha256", test_wc_Sha256Copy), \
TEST_DECL_GROUP("sha256", test_wc_Sha256GetHash), \
TEST_DECL_GROUP("sha256", test_wc_Sha256Transform), \
TEST_DECL_GROUP("sha256", test_wc_Sha256_Flags)
TEST_DECL_GROUP("sha256", test_wc_Sha256_Flags), \
TEST_DECL_GROUP("sha256", test_wc_Sha256HashBlock_unaligned)

#define TEST_SHA224_DECLS \
TEST_DECL_GROUP("sha224", test_wc_InitSha224), \
Expand Down
Loading
Loading