diff --git a/doc/dox_comments/header_files/sha256.h b/doc/dox_comments/header_files/sha256.h index 8a896ff795f..b88b0269a21 100644 --- a/doc/dox_comments/header_files/sha256.h +++ b/doc/dox_comments/header_files/sha256.h @@ -402,12 +402,16 @@ int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz); \ingroup SHA \brief Copies SHA256 context. + The memory regions occupied by the source and destination structures + must not overlap. If they overlap, including when src == dst, the + behavior is undefined. + \return 0 on success \return negative on error \param src Source SHA256 structure \param dst Destination SHA256 structure; - (must be zeroed or previously initialized) + (must be zeroed or previously initialized and must not overlap src) _Example_ \code @@ -613,12 +617,16 @@ int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash); \ingroup SHA \brief Copies SHA224 context. + The memory regions occupied by the source and destination structures + must not overlap. If they overlap, including when src == dst, the + behavior is undefined. + \return 0 on success \return negative on error \param src Source SHA224 structure \param dst Destination SHA224 structure - (must be zeroed or previously initialized) + (must be zeroed or previously initialized and must not overlap src) _Example_ \code diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 12e95b6aef8..0b19caea404 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -3426,7 +3426,7 @@ int wc_Sha224Reset(wc_Sha224* sha224) { { int ret = 0; /* assume success unless proven otherwise */ - if (src == NULL || dst == NULL) { + if (src == NULL || dst == NULL || src == dst) { return BAD_FUNC_ARG; } @@ -3575,7 +3575,7 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) { int ret = 0; - if (src == NULL || dst == NULL) { + if (src == NULL || dst == NULL || src == dst) { return BAD_FUNC_ARG; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 3ca57e524ca..aceb49786a8 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -6573,6 +6573,20 @@ static wc_test_ret_t sha224_copy_test(wc_Sha224* sha, wc_Sha224* shaCopy) ret = wc_Sha224Update(shaCopy, (byte*)b_input, (word32)XSTRLEN(b_input)); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); + /* The common implementation rejects self-copy with BAD_FUNC_ARG. + * Platform-specific implementations differ: some accept self-copy as a + * no-op, while others do not check for it. Keep this assertion limited + * to configurations that use the common implementation. */ +#if !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_RENESAS_RX64_HASH) && \ + (!defined(WOLFSSL_RENESAS_RSIP) || \ + defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) && \ + (!defined(WOLFSSL_KCAPI_HASH) || defined(WOLFSSL_NO_KCAPI_SHA224)) && \ + (!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \ + !defined(MAX3266X_SHA) + ret = wc_Sha224Copy(sha, sha); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + ERROR_OUT(WC_TEST_RET_ENC_NC, exit); +#endif ret = wc_Sha224Update(sha, (byte*)a_input, (word32)XSTRLEN(a_input)); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); @@ -6876,6 +6890,25 @@ static wc_test_ret_t sha256_copy_test(wc_Sha256* sha, wc_Sha256* shaCopy) ret = wc_Sha256Update(shaCopy, (byte*)b_input, (word32)XSTRLEN(b_input)); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); +#endif + /* The common implementation rejects self-copy with BAD_FUNC_ARG. + * Platform-specific implementations differ: some accept self-copy as a + * no-op, while others do not check for it. Keep this assertion limited + * to configurations that use the common implementation. */ +#if !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_AFALG_HASH) && \ + !defined(WOLFSSL_DEVCRYPTO_HASH) && \ + ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \ + !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \ + defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \ + ((!defined(WOLFSSL_RENESAS_SCEPROTECT) && \ + !defined(WOLFSSL_RENESAS_RSIP)) || \ + defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) && \ + !defined(WOLFSSL_IMXRT_DCP) && !defined(WOLFSSL_KCAPI_HASH) && \ + (!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \ + !defined(WOLFSSL_RENESAS_RX64_HASH) && !defined(MAX3266X_SHA) + ret = wc_Sha256Copy(sha, sha); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + ERROR_OUT(WC_TEST_RET_ENC_NC, exit); #endif ret = wc_Sha256Update(sha, (byte*)a_input, (word32)XSTRLEN(a_input)); if (ret != 0)