Skip to content
Draft
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
25 changes: 25 additions & 0 deletions tests/api/test_mldsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -4867,6 +4867,27 @@ int test_mldsa_make_key_from_seed(void)
0xF0, 0xC6, 0xF6, 0xAB, 0xDD, 0x2D, 0xBB, 0x29,
0x85, 0xFE, 0x26, 0x74, 0x82, 0x85, 0x8A, 0x1E
};
/* s2[4] of this seed needs a third SHAKE256 block: FIPS 204 Alg 31, 33.
* Expected bytes are its BitPack encoding, Alg 17 and 24. */
static const byte seed_65_refill[32] = { 0x4F, 0x27 };
static const byte s2_4_65_refill[] = {
0x16, 0x13, 0x82, 0x83, 0x61, 0x52, 0x03, 0x61,
0x62, 0x73, 0x58, 0x75, 0x53, 0x75, 0x50, 0x16,
0x35, 0x07, 0x46, 0x40, 0x74, 0x88, 0x85, 0x24,
0x12, 0x82, 0x87, 0x42, 0x47, 0x27, 0x13, 0x41,
0x43, 0x33, 0x61, 0x37, 0x58, 0x11, 0x32, 0x65,
0x28, 0x17, 0x07, 0x24, 0x60, 0x02, 0x22, 0x05,
0x85, 0x15, 0x43, 0x71, 0x45, 0x46, 0x58, 0x57,
0x12, 0x25, 0x35, 0x50, 0x03, 0x47, 0x88, 0x44,
0x32, 0x67, 0x13, 0x22, 0x03, 0x53, 0x02, 0x24,
0x72, 0x55, 0x43, 0x83, 0x45, 0x85, 0x87, 0x57,
0x05, 0x62, 0x30, 0x60, 0x24, 0x78, 0x18, 0x16,
0x28, 0x50, 0x50, 0x38, 0x88, 0x42, 0x65, 0x13,
0x42, 0x48, 0x02, 0x82, 0x03, 0x37, 0x88, 0x15,
0x18, 0x33, 0x85, 0x65, 0x35, 0x16, 0x22, 0x76,
0x34, 0x66, 0x08, 0x18, 0x56, 0x82, 0x58, 0x75,
0x50, 0x76, 0x44, 0x74, 0x48, 0x63, 0x16, 0x67
};
#endif /* WOLFSSL_NO_ML_DSA_65 */
#ifndef WOLFSSL_NO_ML_DSA_87
static const byte seed_87[] = {
Expand Down Expand Up @@ -8047,6 +8068,10 @@ int test_mldsa_make_key_from_seed(void)
ExpectIntEQ(wc_MlDsaKey_MakeKeyFromSeed(key, seed_65), SEED_OK);
ExpectIntEQ(XMEMCMP(key->p, pk_65, sizeof(pk_65)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_65, sizeof(sk_65)), 0);
ExpectIntEQ(wc_MlDsaKey_MakeKeyFromSeed(key, seed_65_refill), SEED_OK);
/* rho, K, tr, then five 128-byte s1 and four s2 polys: Alg 24. */
ExpectIntEQ(XMEMCMP(key->k + 32 + 32 + 64 + (5 + 4) * 128,
s2_4_65_refill, sizeof(s2_4_65_refill)), 0);
#ifdef WOLFSSL_MLDSA_FIPS204_DRAFT
ExpectIntEQ(wc_MlDsaKey_SetParams(key, WC_ML_DSA_65_DRAFT), 0);
ExpectIntEQ(wc_MlDsaKey_MakeKeyFromSeed(key, seed_65_draft), SEED_OK);
Expand Down
25 changes: 25 additions & 0 deletions tests/api/test_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,28 @@ int test_wc_Sha224_Flags(void)
return EXPECT_RESULT();
}

/* Final of a different variant than Init is refused. */
int test_wc_Sha224_Sha256_MixedFinal(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_SHA224) && !defined(NO_SHA256) && \
defined(WC_HASH_CHECK_VARIANT)
wc_Sha256 sha;
byte hash[WC_SHA256_DIGEST_SIZE];

ExpectIntEQ(wc_InitSha224(&sha), 0);
ExpectIntEQ(wc_Sha224Update(&sha, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Sha256Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#ifndef WOLFSSL_NO_HASH_RAW
ExpectIntEQ(wc_Sha256FinalRaw(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#endif
ExpectIntEQ(wc_Sha224Final(&sha, hash), 0);
wc_Sha224Free(&sha);

ExpectIntEQ(wc_InitSha256(&sha), 0);
ExpectIntEQ(wc_Sha224Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha256Final(&sha, hash), 0);
wc_Sha256Free(&sha);
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int test_wc_Sha224_other(void);
int test_wc_Sha224Copy(void);
int test_wc_Sha224GetHash(void);
int test_wc_Sha224_Flags(void);
int test_wc_Sha224_Sha256_MixedFinal(void);

#define TEST_SHA256_DECLS \
TEST_DECL_GROUP("sha256", test_wc_InitSha256), \
Expand All @@ -66,6 +67,7 @@ int test_wc_Sha224_Flags(void);
TEST_DECL_GROUP("sha224", test_wc_Sha224_other), \
TEST_DECL_GROUP("sha224", test_wc_Sha224Copy), \
TEST_DECL_GROUP("sha224", test_wc_Sha224GetHash), \
TEST_DECL_GROUP("sha224", test_wc_Sha224_Flags)
TEST_DECL_GROUP("sha224", test_wc_Sha224_Flags), \
TEST_DECL_GROUP("sha224", test_wc_Sha224_Sha256_MixedFinal)

#endif /* WOLFCRYPT_TEST_SHA256_H */
137 changes: 137 additions & 0 deletions tests/api/test_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,19 @@ int test_wc_Shake128_Absorb(void)

ExpectIntEQ(wc_Shake128_Absorb(&shake128, NULL, 0), 0);
#endif
#ifdef WC_HASH_CHECK_VARIANT
{
byte hash[1];
/* Once absorbed, only SqueezeBlocks may follow until Init. */
ExpectIntEQ(wc_Shake128_Absorb(&shake128, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake128_Update(&shake128, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake128_Final(&shake128, hash, sizeof(hash)),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_InitShake128(&shake128, HEAP_HINT, INVALID_DEVID), 0);
}
#endif

ExpectIntEQ(wc_Shake128_Absorb(&shake128, (byte*)"a", 1), 0);

Expand All @@ -952,6 +965,15 @@ int test_wc_Shake128_SqueezeBlocks(void)
ExpectIntEQ(wc_Shake128_SqueezeBlocks(NULL , NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

#ifdef WC_HASH_CHECK_VARIANT
/* Squeeze needs input padded by Absorb first. */
ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, hash, 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake128_Update(&shake128, (byte*)"a", 1), 0);
ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, hash, 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake128_Absorb(&shake128, NULL, 0), 0);
#endif
ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, NULL, 0), 0);
#endif
ExpectIntEQ(wc_Shake128_SqueezeBlocks(&shake128, hash, 1), 0);
Expand Down Expand Up @@ -1351,6 +1373,19 @@ int test_wc_Shake256_Absorb(void)
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

ExpectIntEQ(wc_Shake256_Absorb(&shake256, NULL, 0), 0);
#endif
#ifdef WC_HASH_CHECK_VARIANT
{
byte hash[1];
/* Once absorbed, only SqueezeBlocks may follow until Init. */
ExpectIntEQ(wc_Shake256_Absorb(&shake256, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Update(&shake256, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Final(&shake256, hash, sizeof(hash)),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_InitShake256(&shake256, HEAP_HINT, INVALID_DEVID), 0);
}
#endif
ExpectIntEQ(wc_Shake256_Absorb(&shake256, (byte*)"a", 1), 0);

Expand All @@ -1376,6 +1411,15 @@ int test_wc_Shake256_SqueezeBlocks(void)
ExpectIntEQ(wc_Shake256_SqueezeBlocks(NULL , NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

#ifdef WC_HASH_CHECK_VARIANT
/* Squeeze needs input padded by Absorb first. */
ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, hash, 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Update(&shake256, (byte*)"a", 1), 0);
ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, hash, 1),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Absorb(&shake256, NULL, 0), 0);
#endif
ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, NULL, 0), 0);
#endif
ExpectIntEQ(wc_Shake256_SqueezeBlocks(&shake256, hash, 1), 0);
Expand Down Expand Up @@ -2606,3 +2650,96 @@ int test_wc_Cshake_Copy(void)
#endif /* WOLFSSL_CSHAKE128 */
return EXPECT_RESULT();
}

/* Update and Final at a different rate than the first call are refused. */
int test_wc_Sha3_MixedRate(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) && \
!defined(WOLFSSL_NOSHA3_512) && defined(WC_HASH_CHECK_VARIANT)
wc_Sha3 sha3;
byte hash[WC_SHA3_512_DIGEST_SIZE];

ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Sha3_224_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Sha3_512_Update(&sha3, (const byte*)"abc", 3),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
/* Final resets the state, so another variant may follow. */
ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
wc_Sha3_224_Free(&sha3);
#ifdef WOLFSSL_SHAKE128
ExpectIntEQ(wc_InitShake128(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Shake128_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
wc_Shake128_Free(&sha3);
#endif
#if defined(WOLFSSL_SHAKE128) && defined(WOLFSSL_SHAKE256)
{
/* SHAKE128("abc") and SHAKE256("abc"), 32 bytes: FIPS 202. */
static const byte shake128Abc[] = {
0x58, 0x81, 0x09, 0x2d, 0xd8, 0x18, 0xbf, 0x5c,
0xf8, 0xa3, 0xdd, 0xb7, 0x93, 0xfb, 0xcb, 0xa7,
0x40, 0x97, 0xd5, 0xc5, 0x26, 0xa6, 0xd3, 0x5f,
0x97, 0xb8, 0x33, 0x51, 0x94, 0x0f, 0x2c, 0xc8
};
static const byte shake256Abc[] = {
0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39
};

/* A refused Absorb keeps the input already buffered. */
ExpectIntEQ(wc_InitShake256(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Shake256_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Shake128_Absorb(&sha3, (const byte*)"abc", 3),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Final(&sha3, hash, sizeof(shake256Abc)), 0);
ExpectBufEQ(hash, shake256Abc, sizeof(shake256Abc));
wc_Shake256_Free(&sha3);

ExpectIntEQ(wc_InitShake128(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Shake128_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Shake256_Absorb(&sha3, (const byte*)"abc", 3),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake128_Final(&sha3, hash, sizeof(shake128Abc)), 0);
ExpectBufEQ(hash, shake128Abc, sizeof(shake128Abc));
wc_Shake128_Free(&sha3);
#ifndef WOLFSSL_NOSHA3_256
{
/* SHA3-256("abc"): FIPS 202. Same rate as SHAKE256. */
static const byte sha3_256Abc[] = {
0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2,
0x04, 0x5c, 0x17, 0x2d, 0x6b, 0xd3, 0x90, 0xbd,
0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b,
0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43, 0x15, 0x32
};

ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Sha3_256_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Shake256_Update(&sha3, (const byte*)"abc", 3),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Final(&sha3, hash, sizeof(shake256Abc)),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
ExpectBufEQ(hash, sha3_256Abc, sizeof(sha3_256Abc));
wc_Sha3_256_Free(&sha3);

ExpectIntEQ(wc_InitShake256(&sha3, HEAP_HINT, INVALID_DEVID), 0);
ExpectIntEQ(wc_Shake256_Update(&sha3, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Sha3_256_Update(&sha3, (const byte*)"abc", 3),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash),
WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Shake256_Final(&sha3, hash, sizeof(shake256Abc)), 0);
ExpectBufEQ(hash, shake256Abc, sizeof(shake256Abc));
wc_Shake256_Free(&sha3);
}
#endif
}
#endif
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int test_wc_Kmac_Copy(void);
int test_wc_Cshake128(void);
int test_wc_Cshake256(void);
int test_wc_Cshake_Copy(void);
int test_wc_Sha3_MixedRate(void);

#define TEST_SHA3_DECLS \
TEST_DECL_GROUP("sha3", test_wc_InitSha3), \
Expand All @@ -81,7 +82,8 @@ int test_wc_Cshake_Copy(void);
TEST_DECL_GROUP("sha3", test_wc_Sha3_other), \
TEST_DECL_GROUP("sha3", test_wc_Sha3_Copy), \
TEST_DECL_GROUP("sha3", test_wc_Sha3_GetHash), \
TEST_DECL_GROUP("sha3", test_wc_Sha3_Flags)
TEST_DECL_GROUP("sha3", test_wc_Sha3_Flags), \
TEST_DECL_GROUP("sha3", test_wc_Sha3_MixedRate)

#define TEST_SHAKE128_DECLS \
TEST_DECL_GROUP("shake128", test_wc_InitShake128), \
Expand Down
44 changes: 44 additions & 0 deletions tests/api/test_sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,3 +1201,47 @@ int test_wc_sha512_cryptocb_free(void)
return EXPECT_RESULT();
}

/* Final of a different variant than Init is refused. */
int test_wc_Sha512_Family_MixedFinal(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384) && \
defined(WC_HASH_CHECK_VARIANT)
wc_Sha512 sha;
byte hash[WC_SHA512_DIGEST_SIZE];

ExpectIntEQ(wc_InitSha384(&sha), 0);
ExpectIntEQ(wc_Sha384Update(&sha, (const byte*)"abc", 3), 0);
ExpectIntEQ(wc_Sha512Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#ifndef WOLFSSL_NO_HASH_RAW
ExpectIntEQ(wc_Sha512FinalRaw(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#endif
ExpectIntEQ(wc_Sha384Final(&sha, hash), 0);
wc_Sha384Free(&sha);

ExpectIntEQ(wc_InitSha512(&sha), 0);
ExpectIntEQ(wc_Sha384Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#ifndef WOLFSSL_NO_HASH_RAW
ExpectIntEQ(wc_Sha384FinalRaw(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#endif
#ifdef TEST_WC_SHA512_224_FALLBACK
ExpectIntEQ(wc_Sha512_224Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
#endif
ExpectIntEQ(wc_Sha512Final(&sha, hash), 0);
wc_Sha512Free(&sha);

#if defined(TEST_WC_SHA512_224_FALLBACK) && defined(TEST_WC_SHA512_256_FALLBACK)
ExpectIntEQ(wc_InitSha512_224(&sha), 0);
ExpectIntEQ(wc_Sha512_256Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha512Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha512_224Final(&sha, hash), 0);
wc_Sha512_224Free(&sha);

ExpectIntEQ(wc_InitSha512_256(&sha), 0);
ExpectIntEQ(wc_Sha384Final(&sha, hash), WC_NO_ERR_TRACE(BAD_STATE_E));
ExpectIntEQ(wc_Sha512_256Final(&sha, hash), 0);
wc_Sha512_256Free(&sha);
#endif
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_sha512.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ int test_wc_Sha384_other(void);
int test_wc_Sha384Copy(void);
int test_wc_Sha384GetHash(void);
int test_wc_Sha384_Flags(void);
int test_wc_Sha512_Family_MixedFinal(void);
int test_wc_sha512_cryptocb_fallback(void);
int test_wc_sha512_variants_default_devid(void);
int test_wc_sha512_cryptocb_free(void);
Expand Down Expand Up @@ -118,6 +119,7 @@ int test_wc_sha512_cryptocb_free(void);
TEST_DECL_GROUP("sha384", test_wc_Sha384_other), \
TEST_DECL_GROUP("sha384", test_wc_Sha384Copy), \
TEST_DECL_GROUP("sha384", test_wc_Sha384GetHash), \
TEST_DECL_GROUP("sha384", test_wc_Sha384_Flags)
TEST_DECL_GROUP("sha384", test_wc_Sha384_Flags), \
TEST_DECL_GROUP("sha384", test_wc_Sha512_Family_MixedFinal)

#endif /* WOLFCRYPT_TEST_SHA512_H */
21 changes: 21 additions & 0 deletions wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ static int InitSha256(wc_Sha256* sha256)
sha256->len = 0;
sha256->used = 0;
#endif
#ifdef WC_HASH_CHECK_VARIANT
sha256->digestSz = WC_SHA256_DIGEST_SIZE;
#endif
Comment thread
kaleb-himes marked this conversation as resolved.
Resolved

#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
Expand Down Expand Up @@ -2524,6 +2527,11 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
if (sha256 == NULL || hash == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WC_HASH_CHECK_VARIANT
if (sha256->digestSz != WC_SHA256_DIGEST_SIZE) {
return BAD_STATE_E;
}
#endif
#if defined(WOLFSSL_CHECK_MEM_ZERO) && defined(LITTLE_ENDIAN_ORDER) && \
!defined(WOLFSSL_WIDE_BYTE)
wc_MemZero_Add("wc_Sha256FinalRaw digest", digest, sizeof(digest));
Expand Down Expand Up @@ -2557,6 +2565,11 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
if (sha256 == NULL || hash == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WC_HASH_CHECK_VARIANT
if (sha256->digestSz != WC_SHA256_DIGEST_SIZE) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHA-256 Final always fails on ESP32-HW and Freescale MMCAU builds: their init paths never set digestSz · Logic errors

Only the generic InitSha256() (sha256.c:309) sets digestSz. The ESP32-HW InitSha256() (sha256.c:1253) and MMCAU wc_InitSha256_ex() (sha256.c:988) don't, so with WC_HASH_CHECK_VARIANT every wc_Sha256Final/wc_Sha256FinalRaw on those builds returns BAD_STATE_E.

Suggested fix: Set digestSz = WC_SHA256_DIGEST_SIZE under WC_HASH_CHECK_VARIANT in the ESP32 InitSha256() and in the Freescale MMCAU wc_InitSha256_ex().
Basis: wolfCrypt invariant added by this PR: wc_Sha256Final and wc_Sha256FinalRaw (sha256.c:2531, 2569) accept a state only if its init path recorded digestSz.

Related known findings (similar but distinct; listed for context, not part of this finding)

  • F-11173 (open): Only F-11173 is materially related, and it is not the same defect. (1) Location: the candidate is wc_Sha256Final's digestSz check (:2569), rooted in the ESP32 InitSha256 and MMCAU wc_InitSha256_ex init paths. F-11173 is the Freescale wc_InitSha256_ex (:824-877), so the two share only the Freescale init function. (2) Operation: the candidate has Final/FinalRaw reject a state whose digestSz was never recorded, returning BAD_STATE_E. F-11173 has the init dereference a NULL context. (3) Root cause: the

return BAD_STATE_E;
}
#endif

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
Expand Down Expand Up @@ -2995,6 +3008,9 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
XMEMSET(sha224->buffer, 0, sizeof(sha224->buffer));
sha224->loLen = 0;
sha224->hiLen = 0;
#ifdef WC_HASH_CHECK_VARIANT
sha224->digestSz = WC_SHA224_DIGEST_SIZE;
#endif

#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
Expand Down Expand Up @@ -3180,6 +3196,11 @@ int wc_Sha224Reset(wc_Sha224* sha224) {
if (sha224 == NULL || hash == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WC_HASH_CHECK_VARIANT
if (sha224->digestSz != WC_SHA224_DIGEST_SIZE) {
return BAD_STATE_E;
}
#endif
#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (sha224->devId != INVALID_DEVID)
Expand Down
Loading
Loading